How to check if file exists in u-boot script?
Asked Answered
L

2

5

I would like, from my u-boot script, to check the existence of a file on a device before running the image on this device. Indeed, this would ensure to have access to the requested device before booting from it. How could I test the file existence using u-boot console commands?

The following test does a ls on the USB stick, and here are the results with USB stick present:

> if ext2ls usb 0; then echo "USB ON"; else echo "USB KO"; fi
    <DIR>       4096 .
    <DIR>       4096 ..
    <DIR>      16384 lost+found
    <DIR>       4096 boot
                4096 boot.scr
USB ON

Same test without USB stick:

> if ext2ls usb 0; then echo "USB ON"; else echo "USB KO"; fi
** Bad device usb 0 **
USB KO

My wish would be to test the presence of the boot.scr file in fact. How could I do that please?

Leialeibman answered 1/6, 2016 at 10:7 Comment(0)
B
7

You're fairly close. Since ext2ls (or the generic ls) only work on directories you instead need to do:

=> if load mmc 0:1 ${loadaddr} notfound; then echo found; else echo notfound;fi
** File not found notfound **
=> if load mmc 0:1 ${loadaddr} boot.scr; then echo found; else echo notfound;fi
27980 bytes read in 26 ms (1 MiB/s)
found

You can see more examples of this kind of test and fall back in include/config_distro_bootcmd.h

Brashear answered 1/6, 2016 at 12:27 Comment(0)
G
3

This is an old question, but answering anyways since I was looking for this info.

As Tom said, there are indeed examples in include/config_distro_bootcmd.h.

The way to check if a file exists is using test -e. This works in v2021.07 of u-boot.

Example: I have a u-boot.scr in the fat partition of my sdcard, which is the first partition. This is how I check if the file exists and then run it:

if test -e mmc 0:1 u-boot.scr; then fatload mmc 0:1 0x2000000 u-boot.scr; source 0x2000000; fi;

If you have the file in a subfolder, just reference it the same way: scripts/u-boot.scr.

Grosvenor answered 25/9, 2021 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.