【问题标题】:U-boot: how to check if tftp command successfully loaded image into ram?U-boot:如何检查 tftp 命令是否成功将映像加载到 ram 中?
【发布时间】:2019-12-18 10:35:50
【问题描述】:

我通过 u-boot tftp 将 rootfs 映像加载到 RAM 中,并将其闪存到设备闪存中。目前这是手动完成的,但现在我想通过 u-boot 脚本自动完成:

tftp ${rootfs_image};
mmc write ${loadaddr} ${blk} ${cnt}

但是,当它使用 u-boot tftp ${rootfs_image} 命令从 tftp 服务器查找图像并且 不会 找到图像时,我不想运行 @987654323 @ 部分脚本。

如何检查 tftp 命令是否成功将图像下载到 RAM 中?

【问题讨论】:

    标签: u-boot


    【解决方案1】:

    使用 TFTP 协议并不能确保传输数据的完整性 - 请参阅article 中的安全考虑部分。

    假设您的 u-boot 具有可用的 hash 命令,或者您可以使用 CONFIG_CMD_HASH=y 重新编译它,您可以使用 SHA-256 哈希来验证您的图像是否已正确传输:

    在 Linux TFTP 服务器上:

    # create an image for the purpose of the example
    echo "Binary Image" > image.bin
    
    # display sha256 hash for image.bin
    sha256sum -b image.bin
    36949f85f1bff0d5d1dd5fcfdfd725e919b0ee64be24f7f3ccfb53908fd09550 *image.bin
    
    # create a file containing the hash in binary
    # credits:
    sha256sum -b image.bin | xxd -r -p > image.bin.sha256sum.bin
    
    # display content of binary file
    hexdump -C image.bin.sha256sum.bin
    00000000  36 94 9f 85 f1 bf f0 d5  d1 dd 5f cf df d7 25 e9  |6........._...%.|
    00000010  19 b0 ee 64 be 24 f7 f3  cc fb 53 90 8f d0 95 50  |...d.$....S....P|
    00000020
    

    在您的 u-boot 系统上(在此处使用我的 Alwinner H5 系统上可用的内存布局):

    # 0x40080000: address where image.bin will be transfered
    # 0x40090000: address where image.bin.sha256sum.bin will be transfrered
    # 0x40090000: address where the sha256 has will be computed by u-boot on the 13 bytes of image.bin
    
    # clearing memory
    mw.b 0x40080000 0 0x2000
    mw.b 0x40090000 0 0x20
    mw.b 0x400A0000 0 0x20
    
    md.b 0x40080000 0x20
    40080000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    40080010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    
    md.b 0x40090000 0x20
    40090000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    40090010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    
    md.b 0x400A0000 0x20
    400a0000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    400a0010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00    ................
    
    tftp 0x40080000 image.bin
    Using ethernet@1c30000 device
    TFTP from server 192.168.1.22; our IP address is 192.168.1.2
    Filename 'image.bin'.
    Load address: 0x40080000
    Loading: #
         5.9 KiB/s
    done
    Bytes transferred = 13 (d hex)
    
    tftp 0x40090000 image.bin.sha256sum.bin
    Using ethernet@1c30000 device
    TFTP from server 192.168.1.22; our IP address is 192.168.1.2
    Filename 'image.bin.sha256sum.bin'.
    Load address: 0x40090000
    Loading: #
         15.6 KiB/s
    done
    Bytes transferred = 32 (20 hex)
    
     md.b 0x40090000 0x20
    40090000: 36 94 9f 85 f1 bf f0 d5 d1 dd 5f cf df d7 25 e9    6........._...%.
    40090010: 19 b0 ee 64 be 24 f7 f3 cc fb 53 90 8f d0 95 50    ...d.$....S....P
    
    hash sha256  0x40080000 0x0d *0x400A0000
    sha256 for 40080000 ... 4008000c ==> 36949f85f1bff0d5d1dd5fcfdfd725e919b0ee64be24f7f3ccfb53908fd09550
    
    cmp.b 0x40090000 0x400A0000 0x20
    Total of 32 byte(s) were the same
    
    echo $?
    0
    

    如果 image.bin 和/或 image.bin.sha256sum.bin 传输不正确,计算出的 sha256 与传输的 sha256 匹配的可能性极小 - 使用 SHA-512 会更不可能. 结果将是在不正确转移的情况下:

    echo $?
    1
    

    在现实生活中,传输具有固定最大长度(例如用零填充)的图像会更实用,这样负责验证传输图像的 u-boot 脚本将使用固定长度,例如 8 KiB,即 0x2000 字节。

    ls -lgG image.bin
    -rw-rw-r-- 1 13 Dec 17 20:34 image.bin
    
    dd if=/dev/zero  of=image.bin  bs=8K count=1  oflag=append
    ls -lgG image.bin
    -rw-rw-r-- 1 8192 Dec 17 21:03 image.bin
    hexdump -C image.bin
    00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
    *
    00002000
    
    The correct u-boot command to use for computing the hash would be:
    hash sha256  0x40080000 0x2000 *0x400A0000
    

    当然还必须创建一个包含新哈希的新二进制文件:

    sha256sum -b image.bin | xxd -r -p > image.bin.sha256sum.bin
    

    出于示例的目的,我使用了两个文件,但您可以将image.bin.sha256sum.bin 附加到image.bin 并传输一个文件。

    您必须在hashcmp 命令中将0x400A0000 替换为0x40082000

    我希望这会有所帮助。

    【讨论】:

    • 非常彻底的回答!我没有过多考虑下载图像的完整性,但是检查它当然是有意义的(特别是因为我将使用它作为恢复闪烁)所以我一定会尝试实现这一点。谢谢。
    • 我似乎在将哈希函数导入 u-boot 时遇到了问题。我正在为我的 nvidia jetson nano 使用 u-boot-tegra,并且输入 CONFIG_CMD_HASH=y 是不够的。现在我正在尝试将#define CONFIG_CMD_HASH#define CONFIG_SHA256#define CONFIG_SHA_HW_ACCEL 添加到我的机器配置中,但是在构建时出现错误。您是否知道需要哪些行以及将它们放在 u-boot 文件中的什么位置?
    • u-boot 2019.09 中添加CONFIG_CMD_HASH=yorangepi_pc2_defconfig 就足够了。我可以从哪里下载u-boot-tegra,您使用的配置文件是什么?您收到的错误消息是什么?
    • 我设法将哈希函数放入 u-boot,我只需要从 u-boot-tegra/include/configs/p3450-porg.h 中删除 #define CONFIG_SHA_HW_ACCEL,所以我猜在我的情况下,hasing 算法不是硬件加速的。如果你还想看,u-boot-tegra 来自here。我通过 meta-tegra layer 在 Yocto 中使用它作为 bsp 的一部分。
    【解决方案2】:

    如果成功,tftp 命令返回 true。所以你可以写:

    tftp ${rootfs_image} && mmc write ${loadaddr} ${blk} ${cnt}
    

    现在mmc write只有在tftp命令成功时才会执行。

    【讨论】:

    • @Frant 答案的简短而肮脏的版本,没有安全性。我将使用它,直到我从他的回答中获得额外的完整性检查。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-01-21
    • 2012-09-21
    • 2017-11-17
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    相关资源
    最近更新 更多