Saturday, 1 February 2020


U-Boot, gpio input

I have been doing this exercise to boot to a recovery partition when at power up/reset time a  GPIO pin is low. I am using an iMX# board.

I chosen to change the uBOOT source code in uboot/include/configs/*.h header instead putting it into a boot script.

The theory behind this is to have an sdcard or an eMMC with 1 partition with the dtb's and kernel, one with the live system and one with the recovery partition.

My board boots from eMMC. On the eMMC I created 3 partitions. p1 is the boot with dtb file and the kernel, the second the live system and the third the recovery partition.
The 3 partitions are mmcblk1p1 mmcblk1p2 and mmcblk1p3.
p2  and p3 would have initially the same root file system. The idea is that I keep p2 untouched, as a recovery and boot into it if the p1 wont boot or get's nonfunctional.

In my u-boot environment the variable holding the root partition is mmcrootpart.  All I had to do is to change it's value from 2 to 3 when the GPIO level is LOW, or HI..
Here is what I did.

  •  Go to <...>/src/uboot/include  folder and  identify your board.h  file.
    Is very important that you put this line in this string.
  • at the very end, or somewhere in between existent lines of the EXTRA_ENV_SETTINGS string add a line similar to

"gpiobut=if gpio input XXX; then setenv mmcrootpart 3 ; else setenv mmcrootpart 2; fi;\0" \     

  • replace the XXX with the GPIO#
  • Next search for MMC_BOOT_ENV_SETTINGS, and locate where the boot command for your mmc / sdcard is called.
  • Patch the mmcboot  with  run gpiobut

     "mmcboot=echo Booting from mmc ...; " \
        "run gpiobut; " \
        "run mmcargs; " \
        "run optargs; " \

  • Save, compile and flash.


 If you somehow want to boot from an USB stick storage, you can stop at boot prompt and set the environment variables with: (replace the /dev/sdaX, your_dtb.dtb and the memory locations according to your printenv variables you have) Test if you have the usb first.

usb start
ls usb 0:1
ls usb 0:2

usb tree


setenv usbbut 'usb start;if load usb 0:1 0x82000000 /zImage;then;setenv loadfdt load usb 0:1 0x83000000 /your_dtb.dtb;setenv mmcargs setenv bootargs console=${console},${baudrate} root=/dev/sda2 rootwait rw ${cma_size};boot;fi'
run usbbut

That's it

No comments:

Post a Comment