U-Boot : SDCard read/write support

Moderator: nferre

BunnyHug
Posts: 9
Joined: Thu Sep 10, 2015 12:20 pm

U-Boot : SDCard read/write support

Thu Feb 04, 2016 6:04 pm

Good morning,
My aim is to boot the board with the U-Boot environment on SDCard.

I have the at91boostrap in NAND flash compiled to load U-Boot from SDCard in which there is also the uboot.env text file with my own environment.

The boot procedure is correctly executed, at91bootstrap loads U-Boot until I get this error:

Code: Select all

U-Boot 2016.01-00039-g077678e (Feb 04 2016 - 14:47:00 +0100)

CPU: SAMA5D36
Crystal frequency:       12 MHz
CPU clock        :      528 MHz
Master clock     :      132 MHz
DRAM:  256 MiB
NAND:  256 MiB
MMC:   mci: 0
reading uboot.env
*** Warning - bad CRC, using default environment
It seems uboot.env is not correctly read.

I tried to format the SDCard both FAT16 and FAT32 but nothing changed.
I tried both Atmel u-boot-at91 and official u-boot repositories compiling with MMC support but nothing changed.

I found out (only with the official U-Boot code) that uboot.env is correctly read only if I previously write it using the saveenv command.
Whenever I unplug the SDCard from the board and read/modify uboot.env with an editor (I use mousepad or nano on Xubuntu 14.04), it seems it got corrupted and U-Boot reports the above error.

It there a proper way to create and edit the uboot.env file?

PS: I already used successfully the U-Boot + environment file on SDCard with the Beagle Bone Board.

Thank you in advance
blue_z
Location: USA
Posts: 2094
Joined: Thu Apr 19, 2007 10:15 pm

Re: U-Boot : SDCard read/write support

Thu Feb 04, 2016 10:15 pm

BunnyHug wrote:I have the at91boostrap in NAND flash compiled to load U-Boot from SDCard in which there is also the uboot.env text file with my own environment.
...
Whenever I unplug the SDCard from the board and read/modify uboot.env with an editor (I use mousepad or nano on Xubuntu 14.04), it seems it got corrupted and U-Boot reports the above error.
Then obviously you are not writing a properly constructed uboot.env file as the saveenv command does.

You should inspect your malforrned uboot.env (as well as a good uboot.env created by the saveenv command) using a binary/hex editor. Do not use a text editor.
The salient characteristics of a valid environment file or image are (1) the first four bytes (in little-endian order) contain a CRC32 hash of the remaining bytes, and (2) each environment variable is a null-terminated string.
Despite seeing the concise message of "bad CRC", you make no mention of how you maintain/calculate the CRC, and seem oblivious to its existence.
Creating a proper U-Boot environment file using a text editor is probably impossible.
BunnyHug wrote:I found out (only with the official U-Boot code) that uboot.env is correctly read only if I previously write it using the saveenv command.
Beware that U-Boot tries to live up to its name (i.e. "universal"), and that there are two schemes for storing the environment variable on an SDcard, i.e. as a FAT file (as you are trying to use), or as a dedicated region on the SDcard (that should not be allocated to a MBR partition).
You need to verify that the configuration (of Linux4SAM versions) matches your assumption

Regards
BunnyHug
Posts: 9
Joined: Thu Sep 10, 2015 12:20 pm

Re: U-Boot : SDCard read/write support

Fri Feb 05, 2016 11:59 am

Thanking to your answer I have now understood clearly what is the problem.
uboot.env is not a text file and effectively I can see the CRC in the first 4 bytes with an HEX editor.

My original aim was to find the easiest method to change the U-Boot configuration and I was thinking it could have been just editing the uboot.env file with a text editor.
So, how it is possible the U-Boot developers have thought so such tricky operation?
Why uboot.env couldnt be a text file and have the CRC checksum in a separated file like it happens with MD5 files?

Anyway, at this point I need to write a script in order to add the CRC automatically.

Blue_z thank u again for yr useful reply
blue_z
Location: USA
Posts: 2094
Joined: Thu Apr 19, 2007 10:15 pm

Re: U-Boot : SDCard read/write support

Fri Feb 05, 2016 10:08 pm

BunnyHug wrote:So, how it is possible the U-Boot developers have thought so such tricky operation?
Seems like you're just trying to justify your erroneous assumption that it was a text file
Long before environment variables could be saved in a FAT file, U-Boot has always used that format to retain the environment variables in various raw media.
The FAT file is simply a truncated version, and reuses a lot of existing code.
BunnyHug wrote:Why uboot.env couldnt be a text file and have the CRC checksum in a separated file like it happens with MD5 files?
You're confusing a security issue with a simple integrity issue.
But if you think two files is better than one, then you're welcome to develop your version of U-Boot with the features you want.
BunnyHug wrote:Anyway, at this point I need to write a script in order to add the CRC automatically.
Read this


Regards
BunnyHug
Posts: 9
Joined: Thu Sep 10, 2015 12:20 pm

Re: U-Boot : SDCard read/write support

Wed Feb 10, 2016 11:50 am

I have understood everything now.
At the end all the utilities to handle u-boot are in /tools,
mkenvimage is that one to edit the environment

A big thank u blue_z

Return to “SAMA5-based”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests