[U-Boot-Users] Reading memory into environment variable

2,298 views
Skip to first unread message

mcj00_...@f-m.fm

unread,
Jul 4, 2008, 6:03:29 AM7/4/08
to u-boot...@lists.sourceforge.net
Hi,

I wish to read the kernel command line parameters in from a disk file
located e.g. on a FAT file system. My current idea is to implement it
like this:

1. Read disk file into RAM
2. Implement custom "mem2env" command to read memory into environment
variable, with destination variable as ${bootargs}

As far as I can see, I am not reinventing any wheels by doing this, but
does anyone know a better way?

Thanks & regards,

Martin

-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08
_______________________________________________
U-Boot-Users mailing list
U-Boot...@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/u-boot-users

Detlev Zundel

unread,
Jul 4, 2008, 10:36:16 AM7/4/08
to mcj00_...@f-m.fm, u-boot...@lists.sourceforge.net
Hi Martin,

> I wish to read the kernel command line parameters in from a disk file
> located e.g. on a FAT file system. My current idea is to implement it
> like this:
>
> 1. Read disk file into RAM
> 2. Implement custom "mem2env" command to read memory into environment
> variable, with destination variable as ${bootargs}
>
> As far as I can see, I am not reinventing any wheels by doing this, but
> does anyone know a better way?

I've seen the dbox guys doing something similar, although IIRC that
solution wasn't quite generic and was hacked into board specific code.

Thinking about it some more, if you can use mkimage somewhere in
userspace, you could create a script file, load that to ram and execute
it. This is very generic and not limited to setting environment
variables.

Thinking about that some more, maybe we could teach "autoscr" also to
run commands from a memory address without it being wrapped with
mkimage. This is also very generic but prone to errors...

Cheers
Detlev

--
0x2B | ~0x2B
--
DENX Software Engineering GmbH, MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-40 Fax: (+49)-8142-66989-80 Email: d...@denx.de

Alessandro Rubini

unread,
Jul 4, 2008, 10:51:12 AM7/4/08
to d...@denx.de, u-boot...@lists.sourceforge.net, mcj00_...@f-m.fm

>> 1. Read disk file into RAM
>> 2. Implement custom "mem2env" command to read memory into environment
>> variable, with destination variable as ${bootargs}

I've done the same (on u-boot-1.2.0) to read an upgrade script from a
network file or usb pen. I called the command "setenvram" (bad choice,
Wolfgang would refuse it). I'm sure later I found something similar
in mainline, but now I can't find it any more. I may have overlooked
another command.

Although it's not ready for prime time, I paste it here.
If useful I can make a proper patch against current git.


/* set environment variable from ram -- ARub */
int do_setenvram(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
{
unsigned long len, i;
char *addr;

if (argc != 4) {
printf ("Usage:\n%s\n", cmdtp->usage);
return 1;
}
addr = (char *)simple_strtol(argv[2], NULL, 16);
len = simple_strtol(argv[3], NULL, 16);
if (!addr || !len) {
printf ("Usage:\n%s\n", cmdtp->usage);
return 1;
}
addr[len] = '\0';
for (i=0; i<len; i++) {
/* turn newlines into semicolon */
if (addr[i]=='\n') addr[i] = ';';
/* ignore dos-style newlines */
if (addr[i]=='\r') addr[i] = ' ';
/* accept sh-comments and discard them */
if (addr[i]=='#') {
while (addr[i] && addr[i] != '\n')
addr[i++] = ' ';
i--;
}
}
setenv(argv[1], addr);
return 0;
}

U_BOOT_CMD(
setenvram, 4, 0, do_setenvram,
"setenvram - get environment variable from ram\n",
"name addr maxlen\n"
" - set environment variable 'name' from addr 'addr'\n"
);

mcj00_...@f-m.fm

unread,
Jul 4, 2008, 11:39:11 AM7/4/08
to Alessandro Rubini, d...@denx.de, u-boot...@lists.sourceforge.net
Thanks for the help!

Using a proper U-Boot payload script would also be my preferred way, as
suggested by Detlev. However, this isn't my choice to make, it's an
accepted way of doing it already at my company :(

This patch looks about exactly like what I need. In fact I am using
U-Boot 1.2.0, so it's just perfect!

I am somewhat new to U-Boot development. What further work would be
required to bring this up to mainline U-Boot standard?

Thanks & regards,

Martin

Alessandro Rubini

unread,
Jul 4, 2008, 1:02:47 PM7/4/08
to mcj00_...@f-m.fm, u-boot...@lists.sourceforge.net, d...@denx.de

> I am somewhat new to U-Boot development. What further work would be
> required to bring this up to mainline U-Boot standard?

Well, someone must cook it up as a patch, with proper indentation,
based on current tree, add some documentation, and hope it's
considered useful.

I might try next week, I have to study a little git first
/alessandro

Max Anurin

unread,
Nov 28, 2025, 5:51:32 PMNov 28
to osdeve.mirror.boot-loaders.u-boot
A little bit late)

I met same problem...

Currently, with U-Boot v2025.10 we have an ability to use something like this
wget 0x44000000 http://${serverip}:8000/cmdline
env import -t 0x44000000 ${filesize} bootargs
which loads cmdline file from HTTP server and evaluate variables.

Unfortunately, content of cmdline file must be prefixed by bootargs=
bootargs=panic=30 console=ttyS0,115200 ...

My intention to store kernel command line parameters as plain file like
panic=30 console=ttyS0,115200 ...

After research, I implemented following solution:
- write prefix bootargs= in memory at start of data block
- load content of cmdline file with offset 9 bytes
- use env import to evaluate variable

My boot.cmd looks like
dhcp
setenv serverip 192.168.0.20


# Write text "bootargs=" at 0x44000000 (9 bytes)
mw.b 0x44000000 0x62 1
mw.b 0x44000001 0x6F 1
mw.b 0x44000002 0x6F 1
mw.b 0x44000003 0x74 1
mw.b 0x44000004 0x61 1
mw.b 0x44000005 0x72 1
mw.b 0x44000006 0x67 1
mw.b 0x44000007 0x73 1
mw.b 0x44000008 0x3D 1
# Load "cmdline" file with 9 bytes offset
wget 0x44000009 http://${serverip}:8000/cmdline
# Increase filesize by 9
setexpr filesize ${filesize} + 9
# Evaluate variables (only "bootargs" variable)
env import -t 0x44000000 ${filesize} bootargs


wget ${kernel_addr_r} http://${serverip}:8000/zImage
wget ${fdt_addr_r} http://${serverip}:8000/${fdtfile}
bootz ${kernel_addr_r} - ${fdt_addr_r}

Probably will share more details in my post.

Reply all
Reply to author
Forward
0 new messages