Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ass:labs-2025:03:tasks:01 [2025/08/06 12:52]
florin.stancu
ass:labs-2025:03:tasks:01 [2026/07/15 15:33] (current)
florin.stancu [01. Automatically booting our Linux]
Line 2: Line 2:
  
 You are probably be tired of using u-boot CLI to load the Linux FIT uImage into RAM and booting it manually. You are probably be tired of using u-boot CLI to load the Linux FIT uImage into RAM and booting it manually.
-Surely, there must be something to be done to automate this... and you're right! Let's learn to do this!+Surely, there must be something to be done to automate this (yep, there is!).
  
-First, boot your board into ''​u-boot''​ prompt. Enter ''​env print''​... ​+First, ​let's learn about u-boot environment more... Start your board to a ''​u-boot''​ prompt ​and enter ''​env print''​...
  
 <spoiler env print output...>​ <spoiler env print output...>​
Line 101: Line 101:
 First, let's create this file, let's say ''​mydefault.env''​ inside the uboot source directory. Let's start from a minimal example: First, let's create this file, let's say ''​mydefault.env''​ inside the uboot source directory. Let's start from a minimal example:
  
-<​code ​mydefault.env>+<​code ​C>
 /* default u-boot environment variables */ /* default u-boot environment variables */
 /* this file is passed through the C preprocessor (so we can use C-style macros!) */ /* this file is passed through the C preprocessor (so we can use C-style macros!) */
Line 114: Line 114:
 console=ttyLP0 console=ttyLP0
 bootargs=console=ttyLP0,​115200 earlycon,​115200 rdinit=/​linuxrc clk_ignore_unused bootargs=console=ttyLP0,​115200 earlycon,​115200 rdinit=/​linuxrc clk_ignore_unused
-# This is the command executed automatically when uboot starts: +# This is the command executed automatically when uboot starts... 
-bootcmd=echo Fastboot mode... press Ctrl-C to exit; fastboot ​0+bootcmd=echo Fastboot mode... press Ctrl-C to exit; fastboot ​auto
 # This runs instead of bootcmd when booted using `uuu` via USB # This runs instead of bootcmd when booted using `uuu` via USB
 bootcmd_mfg=run bootcmd bootcmd_mfg=run bootcmd
-bootdelay=2 +bootdelay=3 
-image=linux.itb+# TODO: enter a valid uImage file DRAM load address (the one used with '​bootm'​)
 loadaddr=TODO loadaddr=TODO
 +# TODO: enter the 'load mmc 0 ...' script (will be executed using 'run loadimage'​)
 loadimage=TODO loadimage=TODO
 +image=linux.itb
 +# this can be executed using 'run linux' to boot your Linux kernel!
 linux=echo Booting Linux ...; run loadimage; bootm ${loadaddr};​ linux=echo Booting Linux ...; run loadimage; bootm ${loadaddr};​
 </​code>​ </​code>​
Line 134: Line 137:
 Enter your boot script inside the ''​bootcmd''​ var and let's proceed with overwriting the default environment. Enter your boot script inside the ''​bootcmd''​ var and let's proceed with overwriting the default environment.
  
-Recall the ''​DEFAULT_ENV''​ menuconfig item? Modify it to point to your ''​mydefault.env''​ (you can simply use a relative path). Note that you need to enable ''​USE_DEFAULT_ENV_FILE''​ checkbox first to let you supply your value!+Recall the ''​ENV_DEFAULT_ENV_TEXT_FILE''​ menuconfig item? Modify it to point to your ''​mydefault.env''​ (you can simply use a relative path to ''​u-boot''​s source dir). Note that you need to enable ''​ENV_USE_DEFAULT_ENV_TEXT_FILE''​ checkbox first to let you modify that value!
  
-Afterwards[re]compile u-boot, copy the u-boot ''​.bin'' ​files again to the ''​imx-mkimage''​ directory and regenerate your ''​flash.bin''​.+For advanced use cases / build automationyou can override any config items by creating extra .config ​files and using the included ​''​./​scripts/​kconfig/​merge_config.sh'' ​u-boot host script!
  
-Test it by booting your new firmware using ''​uuu''​. Did it work? if not, you may need to repeat this process (this is where a script comes in handy!).+Afterwards, [re]compile u-boot, copy the newly compiled u-boot ''​.bin''​ files to ''​imx-mkimage''​ directory again (if not doing it automatically using a Makefile) and regenerate your ''​flash.bin''​. 
 + 
 +Test it by booting your new firmware using ''​uuu''​. 
 +Always check the UBoot version and build time printed on the serial console during the boot process to ensure you loaded your latest firmware image! 
 + 
 +Try using ''​run linux''​ to run the ''​linux''​ script (if you used that), otherwise edit the ''​bootcmd''​ to do that automatically after the timeout! 
 +Did it work? if not, you may need to repeat this process (this is where a script comes in handy!). 
 + 
 +Note that real devices have their uboot configuration proceed automatically with booting the OS if a physical button is NOT pressed (remember those old Android phones? you could stop this process by holding several volume keys together!). You can script this using GPIO (try the ''​gpio''​ command!), but it's out of scope for today (we don't have any physical buttons). 
 + 
 +=== Bonus: load uboot environment from FAT32 partition === 
 + 
 +If you wish to change without uboot'​s environment without recompiling u-boot itself, it's also possible (and very easy to do). 
 + 
 +Since this is usually desirable for flexibility during upgrades, there are many ways to do this: activate one of ''​CONFIG_ENV_IS_IN_*''​ config options or use a startup script which loads the env from a .txt file on the FAT32 partition. We'll prefer the latter since it's more fun (& failproof on most boards!). 
 + 
 +Basically, we will write a custom script to be executed by u-boot at startup. To do this, search for the ''​PREBOOT''​ config item inside u-boot'​s ''​menuconfig''​. 
 +Since we added a custom default.env file, we need to activate ''​USE_PREBOOT''​ and specify our script as a hardcoded ''​preboot=''​ environment variable. 
 + 
 +Sample script: 
 +<​code>​ 
 +preboot=if fatload mmc 0:1 ${loadaddr} uboot.txt; then env import -t ${loadaddr} ${filesize};​ fi 
 +</​code>​
  
 +That's it! Create this file on your board'​s FAT32 partition (using the same ''​key=value''​ assignments separated by newlines syntax) and boot it! You should now see your new environment values (''​env print''​)!
  
ass/labs-2025/03/tasks/01.1754473970.txt.gz · Last modified: 2025/08/06 12:52 by florin.stancu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0