# SPDX-FileCopyrightText: Copyright (c) 2024-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# Build the Orin FSKP Blob for BMC

This section explains how to build a BMC FSKP blob and subsequent tarball.
These steps must be completed using a X64_64 linux build machine. The usb
connection to IGX is not required for this process.

0. Prepare the following fuse files:
    - fuse.xml
    - fskp_conf.txt
    - <AK_KEY>
    - <EK_KEY>

1. Download the following packages under the same path:
    - host_overlay_fskp_tools_<VERSION>_aarch64.tbz2
    - Jetson_Linux_<VERSION>_aarch64.tbz2
    - arm_tegraflash_<VERSION>_aarch64.tbz2

2. Install the BSP package

3. Follow the steps to untar the packages:
    a. `tar xvjf host_overlay_fskp_tools_<VERSION>_aarch64.tbz2`
    b. `cd Linux_for_Tegra/`
    c. `cp /path/to/arm_tegraflash_<VERSION>_aarch64.tbz2 ../`

4. Make sure the board specification file `./l4t/tools/flashtools/fuseburn/igx-board-spec.txt` matches with your board. The variables can be
   retrieved by `flash.sh` with the `--read-info` command option.
    - For example, `sudo ./flash.sh --read-info igx-orin-devkit internal`
        Board ID(3701) version(500) sku(0008) revision(R.0)
        RAMCODE is 4
        Chip SKU(00:00:00:90) ramcode(4) fuselevel(fuselevel_production) board_FAB(500)
    - The corresponding board-spec variables will be:
        BOARDID=3701
        FAB=500
        BOARDSKU=0008
        CHIP_SKU=00:00:00:90
        RAMCODE_ID=4

5. Run the FSKP Blob generation script
    - For testing:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x23 -f <FUSE_CONFIG> --fskpcfg <EXP_KEY_CONFIG> -k <AK> <EK> -t`
    - For burning fuses:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x23 -f <FUSE_CONFIG> --fskpcfg <EXP_KEY_CONFIG> -k <AK> <EK> -b`
    - For testing without fskp keys:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x23 -f <FUSE_CONFIG> --skipfskpkey -t`


    The script will create bmc_fuse_<VERSION>.tbz2 under Linux_for_Tegra/

# Build the Thor FSKP Blob for BMC

This section explains how to build a BMC FSKP blob and subsequent tarball.
These steps must be completed using a X64_64 linux build machine. The usb
connection to IGX is not required for this process.

0. Prepare the following fuse files:
    - fuse.xml
    - fskp_select.txt
    - <FSKP_KEY>

1. Download the following packages under the same path:
    - host_overlay_fskp_tools_<VERSION>_aarch64.tbz2
    - Jetson_Linux_<VERSION>_aarch64.tbz2
    - arm_tegraflash_<VERSION>_aarch64.tbz2

2. Install the BSP package

3. Follow the steps to untar the packages:
    a. `tar xvjf host_overlay_fskp_tools_<VERSION>_aarch64.tbz2`
    b. `cd Linux_for_Tegra/`
    c. `cp /path/to/arm_tegraflash_<VERSION>_aarch64.tbz2 ../`

4. Make sure the board specification file `./l4t/tools/flashtools/fuseburn/igx-thor-board-spec.txt` matches with your board. The variables can be
   retrieved by `flash.sh` with the `--read-info` command option.
    - For example, `sudo ./flash.sh --read-info igx-thor-devkit internal`
        Board ID(3834) version(400) sku(0008) revision(H.1) Platform ID() Board Boot Device()
        RAMCODE is 12
        Chip SKU(00:00:00:A0) ramcode(12) fuselevel(fuselevel_production) board_FAB(400)
    - The corresponding board-spec variables will be:
        BOARDID=3834
        FAB=400
        BOARDSKU=0008
        CHIP_SKU=00:00:00:A0
        RAMCODE_ID=12

5. Get the FSKP select:
    `FSKP_SELECT=$(cat fskp_select.txt)`

6. Run the FSKP Blob generation script
    - For testing:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x26 -f <FUSE_CONFIG> -k <FSKP_KEY> -i $FSKP_SELECT -t`
    - For burning fuses:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x26 -f <FUSE_CONFIG> -k <FSKP_KEY> -i $FSKP_SELECT -b`
    - For testing without fskp keys:
        `sudo ./l4t/tools/flashtools/fuseburn/bmc_fuse/package_bmc_fskp.sh -c 0x26 -f <FUSE_CONFIG> --skipfskpkey -t`

    The script will create bmc_fuse_<VERSION>.tbz2 under Linux_for_Tegra/

# Burn Tegra's fuse from IGX's BMC

This section explains how to copy the built tarball from the Linux build machine to the IGX's BMC
and run the BMC script to burn Tegra's fuse.

1. Copy the tarball to the BMC
    `scp bmc_fuse_<VERSION>.tbz2 root@<bmc_ip>:/tmp`

2. Log into the BMC with its credentials
    `ssh root@<bmc_ip>`

3. Untar the tarball as following
    a. `cd /tmp`
    b. `tar -xvf bmc_fuse_<VERSION>.tbz2`

4. Burn the Tegra fuse from the BMC
    `bash bmcfuse/bmcfuse.sh`

Note:
1. Please replace <bmc_ip> with the IP address actually assigned to BMC(For example,192.168.1.110).
2. Replace <VERSION> with the actual version number of the L4T BSP release(for exmaple, R36.3.0).

