U-boot:如何检查 tftp 命令是否成功将图像加载到 ram 中?

U-boot: how to check if tftp command successfully loaded image into ram?

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

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

然而,当它使用 u-boot tftp ${rootfs_image} 命令从 tftp 服务器查找图像时,它 没有 找到图像,我不知道不想 运行 脚本的 mmc write 部分。

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

tftp 命令returns 如果成功则为真。所以你可以写:

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

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

使用 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 and/or image.bin.sha256sum.bin 被不正确传输的情况下,计算出的 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

希望对您有所帮助。