Close

Simplified hardware boot switch

A project log for Hardware boot selection switch

A physical switch for my computer to choose between Linux and Windows

stephen-holdawayStephen Holdaway 08/01/2021 at 03:305 Comments

If you liked the idea in the original post but found the microcontroller and firmware part a bit much, I've got good news! You can make your own equally effective dual-boot selection device using as little as a single USB flash drive.

Old USB Flash Drive and a toggle switch with the text "Any USB Drive" overlaid
An old 1GB flash drive and a well-loved toggle switch I had lying around.

Booting based on drive presence

Out of the box, GRUB supports scanning for a filesystem by its UUID using the search command. This is typically used to locate your operating system drive independent of the order hardware is connected, but it conveniently reveals a secondary bit of information: if the drive is missing.

If you only need to choose between two boot options, connecting or disconnecting a flash drive with a known filesystem ID gives a signal that can be easily read in the boot script to change the default selection:

# Detect if drive exists to determine OS to boot
search --fs-uuid --set switch_drive C539-84FE

if [ "${switch_drive}" ] ; then
    # Boot Windows
    set default="2"
else
    # Boot Linux
    set default="0"
fi

Searching for a filesystem is a reasonably generic operation, so if you're using a different bootloader there's probably something similar available that you could leverage.

To get the UUID for a drive under Linux, you can use blkid. If you don't know the device name, running blkid with no arguments will print information for all mounted filesystems.

Linux blkid command line output example

See the readme from the original build for instructions on integrating this snippet into your GRUB boot script.

A more sophisticated variant of this plain-old-flash-drive solution is used by a nicely documented project called The Grub Switch, which also has a microcontroller implementations for Teensy, ATMega32u4 and some other common maker boards.

Switching power to the flash drive

While you could physically plug and unplug the flash drive from a USB port as a functional boot selection method, it's not quite as satisfying or easy as flicking a big clunking toggle switch.

Off-the-shelf USB power switches are available from many suppliers, so it's possible to rig up a boot switch without touching a soldering iron at all, if that's more your style:

Product search results page for USB power switch

For demonstration purposes, I'm modifying an old 1GB flash drive to add a custom toggle switch. This isn't required, but it's one way to switch the drive's power if you're comfortable soldering small things.

I initially wanted to take the USB connector off the flash drive's PCB, but it was fairly solidly connected so I changed tack to splicing onto the 5V trace as it comes off the USB connector. All flash drives will be different, so if you're doing this, confirm which pin is 5V with a multimeter and inspect the traces to find a suitable place to hack onto your particular PCB.

Once the trace to splice is identified, scratch away the solder mask, cut the trace with a sharp blade, and solder small wires to each side of the cut.

With a toggle switch connected to the other end of the spliced in wires, this old flash drive is now ready to toggle boot selection:

Simple USB dual boot selection switch Linux and Windows

Happy hacking! Try to repurpose an old flash drive that's too small for general use, rather than buying a brand new one ⭐

Trade-offs vs a microcontroller solution

Limited window for selection

As this method relies on the BIOS / UEFI to detect if the mass storage device is turned on, you likely can't reliably change the boot selection once power-on / reset has begun. With the microcontroller solution, the switch position can change right up to the moment the GRUB script is executed.

A little messier to add more switch positions

One flash drive provides two states. If you wanted to select between three or more options, you'd need to add more flash drives and wire up a multi-position switch to power the right drive(s) for each position.

Discussions

harrison.worcester wrote 08/09/2021 at 09:33 point

"Try to repurpose an old flash drive that's too small for general use, rather than buying a brand new one"

How small should that flash drive be Stephen? Please forgive me for such a basic question but I am a newbie, and I prefer to ask than to mess something up, even if it means being called names. Your instruction is clear, very newbie-friendly but yeah, I always need to make sure I understand everything correctly.

------------

Harrison W.

https://www.miquido.com/fintech-app-development/

  Are you sure? yes | no

Stephen Holdaway wrote 09/06/2021 at 23:35 point

Hey Harrison, any size you have available will work - there's no technical requirement. This was more of a suggestion that life can be given to an old flash drive if you have any sitting in a drawer somewhere that don't get used :)

  Are you sure? yes | no

tobiasgiese wrote 08/06/2021 at 13:05 point

Wow, that's really awesome!!

Unfortunately, my STM32 is not always recognized. This solution is much more stable, tbh.

Thanks for sharing :)

  Are you sure? yes | no

Stephen Holdaway wrote 08/06/2021 at 21:16 point

Interesting that yours hasn't been reliably detected - I've probably had about 150 boot cycles now and haven't run into any issues like this. Any chance you have a clone chip on your blue-pill (CH32F103)?

  Are you sure? yes | no

tobiasgiese wrote 08/07/2021 at 09:43 point

You‘re right. I accidentally bought a CKS32F103

  Are you sure? yes | no