Search code examples
u-bootcmp

How to compare two memory location using CMP command in u-boot


I'm trying to compare two memory locations using the u-boot (CMP) command. I've u-boot-swap.bin which is flashed at the MTD partition (/dev/mtd2). There is the same u-boot-swap.bin file present at MMC. I want to see if the u-boot flashed at the MTD location (/dev/mtd2) & one present at MMC partition is the same or not.

I tried following commands,

Read u-boot-swap.bin from /dev/mtd2 to memory address 0x81000000

=> sf read 0x81000000 0x40000 0xc0000
device 0 offset 0x40000, size 0xc0000
SF: 786432 bytes @ 0x40000 Read: OK

Read u-boot-swap.bin from MMC to memory address 0x82000000

=> ext4load mmc 0:2 0x82000000 /boot/u-boot-swap.bin
445512 bytes read in 144 ms (2.9 MiB/s)

Then I tried to compare two memory locations using cmp command, But I'm getting mismatch

=> cmp 0x81000000 0x82000000 445512
word at 0x8105c7dc (0x76203033) != word at 0x8205c7dc (0x76203130)
Total of 94711 word(s) were the same
=> cmp 0x81000000 0x82000000 786432
word at 0x8105c7dc (0x76203033) != word at 0x8205c7dc (0x76203130)
Total of 94711 word(s) were the same

What I'm doing wrong here? How to compare two memory location?


Solution

  • First of all, some u-boot commands assume arguments are hexadecimal values, you should try replacing cmp 0x81000000 0x82000000 445512 by cmp 0x81000000 0x82000000 0x0006cc48 (0x0006cc48= 445512d) or use the $filesize variable which is populated after loading a file.

    This being said, if you have the original u-boot-swap.bin available, a convenient way to understand what exactly is going on would be to compare the content of your file with the content of the two memory area you loaded using sf and ext4load. This can be done by computing an MD5 hash on the file and the two memory areas (I created one for the purpose of my answer):

    On my PC:

    dd if=/dev/random of=u-boot-swap.bin bs=445512 count=1
    ls -l u-boot-swap.bin
    md5sum -b u-boot-swap.bin
    def7d2bd063bc9d383889a4fc148aff6 *u-boot-swap.bin
    

    On the system running u-boot, loading u-boot-swap.bin and verifying the MD5 hash computed on the memory area that received u-boot-swap.bin has the same MD5 hash:

    => loadb
    ## Ready for binary (kermit) download to 0x80800000 at 115200 bps...
    CACHE: Misaligned operation at range [80800000, 8086cc48]
    ## Total Size      = 0x0006cc48 = 445512 Bytes
    ## Start Addr      = 0x80800000
    => hash md5 0x80800000 0x0006cc48
    md5 for 80800000 ... 8086cc47 ==> def7d2bd063bc9d383889a4fc148aff6
    

    The two computed MD5 hashes are the same.

    If you don't have the original file available, you wil just be able to compute the MD5 hashes on the two memory areas, but you will not be able to know which one (if any) matches the content of your original file. On my system:

    # Copying file content previously loaded at `0x80800000` to `0x81000000`  and `0x820000001  :
    cp.b 80800000 0x81000000 0x0006cc48
    cp.b 80800000 0x82000000 0x0006cc48
    hash md5 0x81000000 0x0006cc48
    md5 for 81000000 ... 8106cc47 ==> def7d2bd063bc9d383889a4fc148aff6
    hash md5 0x82000000 0x0006cc48
    md5 for 82000000 ... 8206cc47 ==> def7d2bd063bc9d383889a4fc148aff6
    

    If your u-boot does not have the hash command available, you may use crc32 instead - on my PC:

    sudo apt install libarchive-zip-perl
    crc32 u-boot-swap.bin
    b952fb8d
    

    On my system running u-boot:

    crc32  80800000 0x0006cc48
    crc32 for 80800000 ... 8086cc47 ==> b952fb8d
    crc32  0x81000000 0x0006cc48
    crc32 for 81000000 ... 8106cc47 ==> b952fb8d
    crc32  0x82000000 0x0006cc48
    crc32 for 82000000 ... 8206cc47 ==> b952fb8d
    

    In the case you would not have any of the hash and crc32 commands available, I would strongly suggest to rebuild your u-boot with the hash command and update it on your target system.