Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

U-Boot: How to evaluate one environment variable inside another

In U-Boot I have these two environment variables:

filepath=myimages
kernelfile=${filepath}/uImage.bin

When I run this command:

echo ${kernelfile}

I get this output:

${filepath}/uImage.bin

However, I want it to instead evaluate the filepath variable like this:

myimages/uImage.bin

How can this be achieved?

like image 286
user5071535 Avatar asked Jul 29 '15 23:07

user5071535


2 Answers

As it stands this cannot be accomplished with the u-boot echo and shell right now. This is because the macro replacement you see with echo is being performed by the u-boot command line interpreter before the echo command has been launched. All the echo command does is basically print the array of strings passed to it.

Specifically, if you look at common/cli_simple.c from the current (7/29/15) git repository of u-boot, you will find the function cli_simple_run_command. This function is passed a string cmd that could contain multiple commands separated by ;. If you look inside the loop splitting this string you will find cli_simple_process_macros and after that you find a call to cli_simple_parse_line followed by cmd_process. Skipping cli_simple_process_macros for now, cli_simple_parse_line basically takes a string and splits into an array of strings similar to how a shell would, giving you an argv and argc that it passes to cmd_process which executes the command that is in argv[0].

The interesting part is cli_simple_process_macros. This takes an input string as the first argument and output string as the second. This function is a simple state machine that looks for u-boot environment variables (or macros as the function name suggests) and replaces them with the value of the environment variable. This is seen with the call to getenv and then the copy to the output string. If you look at how cli_simple_process_macros works you will notice that it only does one pass, that is if an environment variable contains another environment variable it does not process the second environment variable, instead it just copies the string value.

Finally, if you look at the source for the echo command you will see that it is very simple, just basically looping through the argv passed to it and printing each string with the appropriate spaces.

So basically, if you want the desired behavior, you will need to either change cli_simple_process_macros to be iterative for environment variables or change echo to look up environment variables. You could also perhaps change cli_simple_run_command to invoke cli_simple_process_macros multiple times so that all nested environment variables are expanded.

like image 60
missimer Avatar answered Dec 28 '22 10:12

missimer


Or as a lazy way you could do something like this:

filepath=myimages
set_kernelfile= setenv kernelfile ${filepath}/uImage.bin

then do:

run set_kernelfile; echo ${kernelfile}
like image 33
tmz Avatar answered Dec 28 '22 11:12

tmz