I've been loading env variables via dhcp / tftp from an OpenWrt router:
This has the nice combination of getting ip addresses, netconsole setup and other variables all from a common server. Unfortunately, the return status of the dhcp or tftp command doesn't reflect the case where dhcp succeeded, but the file was not loaded. This happens for example, when the flash drive is unplugged from the router. This leads to unexpected behaviour.
On success, status is true:
On dhcp failure, status is false:
On tftp failure, status is true!
Workaround on success, status is true:
Workaround on dhcp failure, status is false:
Workaround on tftp failure, status is false:
The results are similar for the TFTP command instead of the DHCP command. This is not what I expected, but the workaround seems to be fine.
As a related question, is there anything that prevents tftp from loading a file that is too large? I don't think that should be an issue, I just thought about it while I was working on this.
edit: I found another workaround is to clear fileaddr and filesize before calling tftp. This makes the subsequent env import fail and prevents it from reloading variables from a previous tftp call. It's important to clear both fileaddr and filesize otherwise env import doesn't return false.
On success, status is true:
On dhcp failure, status is false:
On tftp failure, status is false!
Eureaka! I realized that clearing both fileaddr and filesize makes dhcp return the correct status:
I tested this on the other cases as well, and I believe this is the winner!
setenv preboot 'dhcp $uenv_addr boot/preboot.u-boot && env import -t $fileaddr $filesize && run uenv_run'
This has the nice combination of getting ip addresses, netconsole setup and other variables all from a common server. Unfortunately, the return status of the dhcp or tftp command doesn't reflect the case where dhcp succeeded, but the file was not loaded. This happens for example, when the flash drive is unplugged from the router. This leads to unexpected behaviour.
On success, status is true:
GoFlexNet> if dhcp $uenv_addr loadthis; then echo loaded ok; else echo loaded bad; fi DHCP client bound to address 192.168.1.3 (3 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: # 10.7 KiB/s done Bytes transferred = 11 (b hex) loaded ok
On dhcp failure, status is false:
GoFlexNet> if dhcp $uenv_addr loadthis; then echo loaded ok; else echo loaded bad; fi No link on egiga0 loaded bad
On tftp failure, status is true!
GoFlexNet> if dhcp $uenv_addr loadthis; then echo loaded ok; else echo loaded bad; fi DHCP client bound to address 192.168.1.3 (5 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: * TFTP error: 'file /mnt/horou/loadthis not found' (1) Not retrying... loaded ok
Workaround on success, status is true:
GoFlexNet> setenv filesize; if dhcp $uenv_addr loadthis && printenv filesize; then echo loaded ok; else echo loaded bad; fi BOOTP broadcast 1 DHCP client bound to address 192.168.1.3 (4 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: # 10.7 KiB/s done Bytes transferred = 11 (b hex) filesize=b loaded ok
Workaround on dhcp failure, status is false:
GoFlexNet> setenv filesize; if dhcp $uenv_addr loadthis && printenv filesize; then echo loaded ok; else echo loaded bad; fi No link on egiga0 loaded bad
Workaround on tftp failure, status is false:
GoFlexNet> setenv filesize; if dhcp $uenv_addr loadthis && printenv filesize; then echo loaded ok; else echo loaded bad; fi BOOTP broadcast 1 DHCP client bound to address 192.168.1.3 (5 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: * TFTP error: 'file /mnt/horou/loadthis not found' (1) Not retrying... loaded bad
The results are similar for the TFTP command instead of the DHCP command. This is not what I expected, but the workaround seems to be fine.
As a related question, is there anything that prevents tftp from loading a file that is too large? I don't think that should be an issue, I just thought about it while I was working on this.
edit: I found another workaround is to clear fileaddr and filesize before calling tftp. This makes the subsequent env import fail and prevents it from reloading variables from a previous tftp call. It's important to clear both fileaddr and filesize otherwise env import doesn't return false.
On success, status is true:
GoFlexNet> setenv fileaddr; setenv filesize; if dhcp $uenv_addr loadthis && env import -t $fileaddr $filesize; then echo loaded ok; else echo loaded bad; fi BOOTP broadcast 1 DHCP client bound to address 192.168.1.3 (4 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: # 11.7 KiB/s done Bytes transferred = 12 (c hex) loaded ok
On dhcp failure, status is false:
GoFlexNet> setenv fileaddr; setenv filesize; if dhcp $uenv_addr loadthis && env import -t $fileaddr $filesize; then echo loaded ok; else echo loaded bad; fi No link on egiga0 loaded bad
On tftp failure, status is false!
GoFlexNet> setenv fileaddr; setenv filesize; if dhcp $uenv_addr loadthis && env import -t $fileaddr $filesize; then echo loaded ok; else echo loaded bad; fi BOOTP broadcast 1 DHCP client bound to address 192.168.1.3 (4 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: * TFTP error: 'file /mnt/horou/loadthis not found' (1) Not retrying... loaded bad
Eureaka! I realized that clearing both fileaddr and filesize makes dhcp return the correct status:
GoFlexNet> setenv fileaddr; setenv filesize; if dhcp $uenv_addr loadthis; then echo loaded ok; else echo loaded bad; fi BOOTP broadcast 1 DHCP client bound to address 192.168.1.3 (3 ms) Using egiga0 device TFTP from server 192.168.1.1; our IP address is 192.168.1.3 Filename 'loadthis'. Load address: 0x810000 Loading: * TFTP error: 'file /mnt/horou/loadthis not found' (1) Not retrying... loaded bad
I tested this on the other cases as well, and I believe this is the winner!