I'm developing a recovery mechanism on a embedded system. I want to boot into recovery mode if user pressed a GPIO button. Recovery mode means selecting different kernel and root partition than usual. I got stuck on using gpio value in conditional command. If I write if gpio input 20; then cmd; fi
cmd
will always run because gpio returns error status not the value of GPIO. How can I get the value?
Is it feasible to use U-Boot commands to implement boot selection (I need to blink LEDs for 15s and if the user presses the button for at least 5s then switch to recovery). Would it be easier to implement this logic in C code? If so where to look and are there any examples?
In general providing recovery system seems like a common task for an embedded engineer. What are the best practices in this scenario? What is the most common way of implementing this? I could not find any help or guidelines on the web.
Recovery may depend on what your system has available to you, and how robust you need to be. Remember to always keep everything you can read-only and seperated it from stuff that is writeable. Keep writeable stuff in a different partition in your NAND or wherever. Your method described above will get you back and running if the kernel/FS is bad. Which may be all you need. If you ever plan on allowing the updating of U-Boot itself then you may also want to consider the possibility of U-Boot getting corrupt when rewritten and have a way to recover.
Alot of the embedded processor's have a boot order defined by pins you pull one way or the other on start. A favorite method of mine is to connect a button to one of these that user can hold on power up to change the boot order (say from NAND to SD) and boot from another device that can then restore the system.
For your situation above it is definitely possible to check a gpio, flash an LED etc. from U-Boot. You can use U-Boot commands:
Or you can do it directly in the U-Boot C Code somewhere if you prefer, such as in your own U-Boot Command that would be compiled with U-Boot:
If you are getting errors running gpio commands from U-Boot make sure you have the gpio command stuff enabled, with the following in your config file:
If it still doesn't work then make sure U-Boot has drivers for GPIO on your part (which i can't imagine it doesn't).