b99
Posts: 10
Joined: Mon Jun 11, 2018 10:45 am

Boot order: change so USB is tried first

Thu May 09, 2019 10:45 am

I have a Pi 3B + running headless inside a case which leaves only the power, Ethernet, and 2 USB ports accessible. When a mistake in dhcpcd.conf cut off ssh access, I had to disassemble to case to remove the SD card to correct. What I'd like to do is set it up so a similar situation in the future could be fixed by booting from USB. Is there a way set the boot sequence to boot from a USB if it is plugged in even if the SD card is inserted?

BespokeTech
Posts: 71
Joined: Thu Apr 04, 2019 8:14 pm

Re: Boot order: change so USB is tried first

Thu May 09, 2019 11:21 am

I don't think this is possible, the boot up sequence is stored in the boot rom which as far as I am aware cannot be changed.

But... it basically looks for a file called bootcode.bin on the SD card, if it cannot find it then it will move onto a USB boot. So as you are only talking about a loss of network scenario as oppose to a complete failure to boot from the SD card, you could write a little script which runs off the SD card image which watches for a USB stick being plugged in and then it could look for a certain file on this usb stick say for example "changeboot". If it finds the required file then the script renames the bootcode.bin file on the SD card and reboots the PI. In theory it should then boot from usb!

b99
Posts: 10
Joined: Mon Jun 11, 2018 10:45 am

Re: Boot order: change so USB is tried first

Thu May 09, 2019 11:36 am

Thanks! That's very creative. Then I'd need to remember to fix bootcode.bin before the reboot. I'd prefer a neater option if anyone is aware of one.

BespokeTech
Posts: 71
Joined: Thu Apr 04, 2019 8:14 pm

Re: Boot order: change so USB is tried first

Thu May 09, 2019 11:53 am

b99 wrote:
Thu May 09, 2019 11:36 am
Thanks! That's very creative. Then I'd need to remember to fix bootcode.bin before the reboot. I'd prefer a neater option if anyone is aware of one.
Well the USB stick you use to recover the situation could just have a startup script that renames the bootcode.bin file on the SD card back to it's original name.

For a detailed write up on how the boot modes work....

https://www.raspberrypi.org/blog/pi-3-b ... rage-boot/

All of the boot code has to fit into 32kB so I don't think it is possible to do any fancy boot sequence ordering unfortunately.

Thinking about what I proposed, the script that checks for the presence of a USB stick with the "changeboot" file on it only needs to run once during the bootup of the SD image. So in summary the process would be.

1. Lose network connectivity to PI (Booted from SD card)

2. Insert USB stick containing a bootable image and also "changeboot" empty text file.

3. Power cycle the PI, the SD card will boot as normal and run the script to check for the presence of the USB stick. If it finds it then, rename the bootcode.bin file and reboot.

4. PI should then boot from the USB stick, and as I said above you could have a boot up script that renames the original bootcode.bin on the SD card.

fruitoftheloom
Posts: 21086
Joined: Tue Mar 25, 2014 12:40 pm
Location: Delightful Dorset

Re: Boot order: change so USB is tried first

Thu May 09, 2019 11:58 am

b99 wrote:
Thu May 09, 2019 11:36 am
Thanks! That's very creative. Then I'd need to remember to fix bootcode.bin before the reboot. I'd prefer a neater option if anyone is aware of one.

https://www.raspberrypi.org/documentati ... bootmodes/
Retired disgracefully.....
This at present is my daily "computer" https://www.asus.com/us/Chrome-Devices/Chromebit-CS10/

User avatar
HawaiianPi
Posts: 4878
Joined: Mon Apr 08, 2013 4:53 am
Location: Aloha, Oregon USA

Re: Boot order: change so USB is tried first

Thu May 09, 2019 12:03 pm

You can change the boot device by setting an additional OTP bit and pulling certain GPIO pins high or low. This does not change the boot order, it tells the Pi to boot from either SD or USB, depending on the state of the GPIO pins (you could add a switch to the case to force USB boot).

https://www.raspberrypi.org/documentati ... ootflow.md
My mind is like a browser. 27 tabs are open, 9 aren't responding,
lots of pop-ups...and where is that annoying music coming from?

b99
Posts: 10
Joined: Mon Jun 11, 2018 10:45 am

Re: Boot order: change so USB is tried first

Fri May 10, 2019 10:10 am

Thanks all for the replies. I am going to try and implement BespokeTechs solution.

epoch1970
Posts: 3882
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: Boot order: change so USB is tried first

Fri May 10, 2019 1:20 pm

The solution presented forces to boot twice in order to start the system in the nominal case, USB present:
- SD boot: if USB present -> bootcode.hide + reboot
- USB boot: -> bootcode.bin
It will be a little slow and FAT is not the most resilient file system.

Another possibility is to boot in uboot on the SD and choose the final target dynamically from a script. Uboot is often used in OTA update scenarios (rootfs A/B) so besides scripting it has a few features allowing to fail safe(r).
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

b99
Posts: 10
Joined: Mon Jun 11, 2018 10:45 am

Re: Boot order: change so USB is tried first

Sun May 12, 2019 4:19 pm

BespokeTech wrote:
b99 wrote:
Thu May 09, 2019 11:36 am
Thanks! That's very creative. Then I'd need to remember to fix bootcode.bin before the reboot. I'd prefer a neater option if anyone is aware of one.
Well the USB stick you use to recover the situation could just have a startup script that renames the bootcode.bin file on the SD card back to it's original name.

For a detailed write up on how the boot modes work....

https://www.raspberrypi.org/blog/pi-3-b ... rage-boot/

All of the boot code has to fit into 32kB so I don't think it is possible to do any fancy boot sequence ordering unfortunately.

Thinking about what I proposed, the script that checks for the presence of a USB stick with the "changeboot" file on it only needs to run once during the bootup of the SD image. So in summary the process would be.

1. Lose network connectivity to PI (Booted from SD card)

2. Insert USB stick containing a bootable image and also "changeboot" empty text file.

3. Power cycle the PI, the SD card will boot as normal and run the script to check for the presence of the USB stick. If it finds it then, rename the bootcode.bin file and reboot.

4. PI should then boot from the USB stick, and as I said above you could have a boot up script that renames the original bootcode.bin on the SD card.
For stage 3 I'm using the following script:

Code: Select all

#!/bin/bash

if [ $(find /media/pi/ -name changeboot) ]; then
    echo "Boot usb inserted"

    mv /boot/bootcode.bin /boot/bootcode.bak

fi
This works because Raspbian automounts USB drives in /media/pi.

I manually executed the script and rebooted and the Pi booted from the USB. So far, great.

For stage 4, it doesn't seem like Raspbian automounts the SD card so I'll have to have the script do it. The problem is finding the name of the boot partition. It is currently /dev/mmcblk0p1 as it has been every time I've looked in the past. Will it always be so?

BespokeTech
Posts: 71
Joined: Thu Apr 04, 2019 8:14 pm

Re: Boot order: change so USB is tried first

Mon May 13, 2019 11:34 am

For stage 4, it doesn't seem like Raspbian automounts the SD card so I'll have to have the script do it. The problem is finding the name of the boot partition. It is currently /dev/mmcblk0p1 as it has been every time I've looked in the past. Will it always be so?
Yes they will stay the same....

mmcblk0p1 - Boot partition of the SD Card. (fat)
mmcblk0p2 - Main raspbian partition on the SD card (ext4)

BespokeTech
Posts: 71
Joined: Thu Apr 04, 2019 8:14 pm

Re: Boot order: change so USB is tried first

Mon May 13, 2019 11:40 am

With regards to auto mounting the SD card, have a look at this...

http://www.embeddedpi.com/documentation ... ng-sd-card

b99
Posts: 10
Joined: Mon Jun 11, 2018 10:45 am

Re: Boot order: change so USB is tried first

Tue May 14, 2019 11:40 am

BespokeTech wrote:
Mon May 13, 2019 11:40 am
With regards to auto mounting the SD card, have a look at this...

http://www.embeddedpi.com/documentation ... ng-sd-card
Since this is all only for a rescue scenario, I think just having the script on the USB mount the SD card is simpler.

Thanks for all of your help, I've successfully implemented your proposal.

On the Pi:

$ cat /usr/local/bin/usbbootcheck.sh

Code: Select all

#!/bin/bash

if [ $(find /media/pi/ -name changeboot) ]; then
    echo "Boot usb inserted"
    sudo mv /boot/bootcode.bin /boot/bootcode.bak

fi

$ crontab -e

Code: Select all

@reboot sleep 15 && sudo sh /usr/local/bin/usbbootcheck.sh
It wasn't working without the sleep 15, I'm guessing cron was running the script before the USB was mounted.


On the USB:

$ cat /usr/local/bin/restorebootcode.sh

Code: Select all

#! /bin/bash

mount /dev/mmcblk0p1 /mnt/sdcard

mv /mnt/sdcard/bootcode.bak /mnt/sdcard/bootcode.bin

$ crontab -e

Code: Select all

@reboot sleep 15 && sudo sh /usr/local/bin/restorebootcode.sh


Return to “Beginners”