STM32 firmware for a physical switch to set the GRUB boot selection

Overview

GRUB boot selector switch

Firmware for the Hardware Boot Selection Switch project. This acts as a USB mass storage device containing a dynamic GRUB script, which sets a variable to indicate the position of a physical switch.

Building

On Linux, you'll need gcc-arm-none-eabi installed to build and openocd to flash.

# Pull in libopencm3
git submodule init
git submodule update

# Build libopencm3 (only needed once)
make -C libopencm3 -j8

# Build the firmware
make

# Flash with a J-Link programmer connected by SWD
make flash

# Flash with an ST-Link programmer connected by SWD
make flash PROGRAMMER=stlink

This project supports building and flashing multiple different parts. The following parts have been tested:

  • stm32f103c8 (default): found on the "blue pill" and "maple" STM32 dev boards
  • stm32f070f6

To build for your specific part, pass the DEVICE parameter to make on the command line (or export DEVICE as an environment variable):

make DEVICE=stm32f070f6
make flash DEVICE=stm32f070f6

Valid patterns for the DEVICE parameter can be found in libopencm3/ld/devices.data.

Wiring

The switch position is read from pin A6, which is pulled high internally. Connect a toggle switch between this pin and GND to control the reported state.

GRUB config

You'll need to modify your system's GRUB config to look for and read from this device.

On Debian and Arch based systems, this involves editing a script in /etc/grub.d/, then running update-grub to generate and validate the /boot/grub/grub.cfg file used when booting. Check your distribution's docs for how to do this on your system.

Here's an example of how the switch position can be used in a GRUB script. This replaces the existing set default= line in your config:

# Look for hardware switch device by its hard-coded filesystem ID
search --no-floppy --fs-uuid --set hdswitch 55AA-6922

# If found, read dynamic config file and select appropriate entry for each position
if [ "${hdswitch}" ] ; then
  source ($hdswitch)/switch_position_grub.cfg

  if [ "${os_hw_switch}" == 0 ] ; then
    # Boot Linux
    set default="0"
  elif [ "${os_hw_switch}" == 1 ] ; then
    # Boot Windows
    set default="2"
  else
    # Fallback to default
    set default="${GRUB_DEFAULT}"
  fi

else
  set default="${GRUB_DEFAULT}"
fi

Troubleshooting

The device is not accessible in GRUB

Ensure your BIOS is configured to enumerate USB storage devices. On some motherboards, this requires changing from "fast boot" to "normal boot". If you can boot from a flash drive, you should be able to access this device from GRUB.

Reading from an operating system

The dynamic files presented by this device cannot be reliably used when mounted in Windows or Linux. Operating systems (quite rightly) assume that the files on an underlying device won't change without their knowledge, so they cache reads heavily.

If you want to access the switch state from an operating system, you'll need to implement a second USB interface on the device, or work with the unmounted block device.

Comments
  • GRUB 2.06 doesn't recognize filesystem on device.

    GRUB 2.06 doesn't recognize filesystem on device.

    Hello, I have followed the instructions in this github and the hackaday post and while I am in linux I can mount the "drive" and see the FAT filesystem and files. However, when I drop to a grub shell and attempt to see the files I get this:

    grub> ls (hd0)
    Device hd0: No known filesystem detected - Sector size 512B - Total size 64KiB
    

    I have another drive which is Arch Linux installation media and grub can identify that one as having a FAT partition just fine. Do you have any suggestions for how to debug this further?

    opened by bentglasstube 9
  • unable to enumerate USB device

    unable to enumerate USB device

    I bought a fresh STM32F103 (blue pill), as I initially bought a CKS32F103.

    The problem now is, that the device will not be recognized as a USB device.

    Aug 17 11:39:07 tobias-linux kernel: usb 1-4: new full-speed USB device number 115 using xhci_hcd
    Aug 17 11:39:07 tobias-linux kernel: usb 1-4: Device not responding to setup address.
    Aug 17 11:39:07 tobias-linux kernel: usb 1-4: Device not responding to setup address.
    Aug 17 11:39:08 tobias-linux kernel: usb 1-4: device not accepting address 115, error -71
    Aug 17 11:39:08 tobias-linux kernel: usb usb1-port4: unable to enumerate USB device
    

    If I plug in my clone stm32, I will get the following

    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: new full-speed USB device number 116 using xhci_hcd
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: device descriptor read/64, error -71
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: New USB device found, idVendor=26ba, idProduct=8003, bcdDevice= 2.00
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: New USB device strings: Mfr=1, Product=2, SerialNumber=3
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: Product: Boot Switch
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: Manufacturer: Stecman
    Aug 17 11:39:54 tobias-linux kernel: usb 1-4: SerialNumber: D000000000E7
    Aug 17 11:39:54 tobias-linux kernel: usb-storage 1-4:1.0: USB Mass Storage device detected
    Aug 17 11:39:54 tobias-linux kernel: scsi host7: usb-storage 1-4:1.0
    

    But flashing was successful of the new real STM32.

    $ make flash PROGRAMMER=stlink
      LD	boot-swtich.stm32f103c8.elf
      OBJCOPY	boot-swtich.stm32f103c8.bin
    
    Firmware size (boot-swtich.stm32f103c8.elf):
    
       text	   data	    bss	    dec	    hex	filename
       8128	    112	   1020	   9260	   242c	boot-swtich.stm32f103c8.elf
    
    Flash: 12% (8240 / 65536 bytes)
    RAM: 5% (1132 / 20480 bytes)
    
    BINARY=boot-swtich.stm32f103c8.elf openocd -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f1x.cfg -f openocd.cfg -c 'program_and_run ()'
    Open On-Chip Debugger 0.11.0-rc2
    Licensed under GNU GPL v2
    For bug reports, read
    	http://openocd.org/doc/doxygen/bugs.html
    hla_swd
    Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
    attach
    Info : clock speed 1000 kHz
    Info : STLINK V2J17S4 (API v2) VID:PID 0483:3748
    Info : Target voltage: 3.205253
    Info : stm32f1x.cpu: hardware has 6 breakpoints, 4 watchpoints
    Info : starting gdb server for stm32f1x.cpu on 3333
    Info : Listening on port 3333 for gdb connections
    target halted due to debug-request, current mode: Thread 
    xPSR: 0x01000000 pc: 0x080016a8 msp: 0x20005000
    Info : device id = 0x10006412
    Info : flash size = 32kbytes
    shutdown command invoked
    

    Do you have any idea? Unfortunately I'm no hardware programmer. I'm thankful for any help :)

    opened by tobiasgiese 9
  • Error while flashing

    Error while flashing

    I get this output while executing make flash PROGRAMMER=stlink :

      LD boot-swtich.stm32f103c8.elf
      OBJCOPY	boot-swtich.stm32f103c8.bin
    
    Firmware size (boot-swtich.stm32f103c8.elf):
    
       text	   data	    bss	    dec	    hex	filename
       8128	    112	   1020	   9260	   242c	boot-swtich.stm32f103c8.elf
    
    Flash: 12% (8240 / 65536 bytes)
    RAM: 5% (1132 / 20480 bytes)
    
    BINARY=boot-swtich.stm32f103c8.elf openocd -f interface/stlink.cfg -c "transport select hla_swd" -f target/stm32f1x.cfg -f openocd.cfg -c 'program_and_run ()'
    Open On-Chip Debugger 0.11.0-rc2
    Licensed under GNU GPL v2
    For bug reports, read
    	http://openocd.org/doc/doxygen/bugs.html
    hla_swd
    Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
    attach
    Info : clock speed 1000 kHz
    Info : STLINK V2J29S7 (API v2) VID:PID 0483:3748
    Info : Target voltage: 3.232848
    Warn : UNEXPECTED idcode: 0x2ba01477
    Error: expected 1 of 1: 0x1ba01477
    openocd.cfg:16: Error: 
    in procedure 'program_and_run' 
    in procedure 'program' called at file "openocd.cfg", line 16
    
    make: *** [Makefile:64: flash] Error 1
    

    I'm new to st-link programming so any help is appreciated

    opened by Matipolit 7
  • Q/A

    Q/A

    Very happy with the project, not an issue, just i though it would be easier for some people if you add to the readme a couple things.

    1. Switch is pull up detected on pin A6, so switch must be connected to A6 pin and GND.(As you published binaries , so they dont have to scroll through the code).
    2. Depending on the mobo the usb devices are not detected on grub, change from "fast boot" to "normal boot", nativedisk could also be used but it can make your grub not to know how to boot into windows as there is some problem with chainloader.
    3. The grub file to edit is, /boot/grub/grub.cfg(eventhough it is not recommended), as if you edit /etc/default/grub when you try to sudo update-grub it wont work. Also just paste it at the end of the file.(This can seem straight forward, but it isnt for newbies).
    opened by jesusvallejo 1
  • Add support to build for STM32F103 (common

    Add support to build for STM32F103 (common "blue pill" dev board)

    This project is currently set up to build for just the STM32F070, including some specific configuration required for the F6 package of that chip that is unlikely to compile for other STM32 models.

    To make this a little more accessible, it would be good to add support for building for the STM32F103, which is far more common on cheap development boards than the chip I've used.

    opened by stecman 1
  • Add options to flash using an stlink-v2

    Add options to flash using an stlink-v2

    openocd-jlink.cfg currently needs to be modified if someone wants to program their device using an ST-Link.

    Allow specifying the programmer to use as a parameter to make flash

    opened by stecman 0
  • Linux can't find USB device

    Linux can't find USB device

    I recognize there's a similar issue that's already closed, but I have been unable to resolve the issue. I'll provide the same information.

    • I'm using the stm32f103c8 "blue pill".
    • I am using a st-link v2 cloner to flash. I was unable to flash using stlink, but i am able to successfully flash using st-flash. However, I cannot get ubuntu (let alone grub) to recognize the device in any way.
    • Printing lsusb, does not list the device. Nor does blkid.
    • The only things listed in the grub menu are my actual harddrives.
    • I am able to reformat a flashdrive as FAT12 and both ubuntu and grub recognize it as such.
    • grub version: grub-install (GRUB) 2.06-2ubuntu7
    • search --fs-uuid 55AA-6922 returns device not found
    • I am able to flash a blinking led program with success

    My only thoughts are something has changed in the libopencm3 api making the usb initialization is not work?

    Only warning i get when flashing is regarding nRST and i assume that is not related.

    Let me know if anyone has any thoughts, thanks!

    opened by veronduh 2
Releases(20220312)
Owner
Stephen Holdaway
Stephen Holdaway
This is an upgrade to the initial TerminalOS source, supporting real hardware other than just QEMU, using GRUB as a bootloader instead of the crappy one i wrote

Terminal OS Author: Maheswaran Date: 20th Nov 2021 PROGRESS Multiboot compilance achieved VGA driver from complete with print_hex, print_dec, printf f

Maheswaran Parameswaran 1 Nov 28, 2021
Minimal setup required to develop firmware for STM32 using ceedling

STM32 & Ceedling Base Project I created this project as a starting point for anyone looking to create production quality firmware for an embedded syst

null 9 Oct 10, 2022
The artifact associated with our ISSTA 2021 paper "Seed Selection for Successful Fuzzing"

Seed Selection for Successful Fuzzing The artifact associated with our ISSTA 2021 paper "Seed Selection for Successful Fuzzing". While our primary art

HexHive 33 Nov 5, 2022
Trio-based Inference of Dominance and Selection

TIDES Trio-based inference of dominance and selection TIDES is a statistical model for inferring dominance and the strength of natural selection from

Gustavo Valadares Barroso 1 Oct 25, 2021
fuzzy matching selection gui

fm === fm provides a gui to select an item from a list using a fuzzy matching algorithm. When an item is selected, it is sent to the plumber `send` po

phil9 11 Nov 1, 2022
Source code for the data dependency part of Jan Kossmann's PhD thesis "Unsupervised Database Optimization: Efficient Index Selection & Data Dependency-driven Query Optimization"

Unsupervised Database Optimization: Data Dependency-Driven Query Optimization Source code for the experiments presented in Chapter 8 of Jan Kossmann's

Jan Koßmann 4 Apr 24, 2022
Gamepad firmware for RP2040 microcontrollers supporting Nintendo Switch, XInput and DirectInput

GP2040 Firmware GP2040 is a gamepad firmware for the RP2040 microcontroller that provides high performance and a rich feature set across multiple plat

null 363 Nov 25, 2022
Very minimalistic UEFI boot menu / Stivale2 bootloader

Tosaithe Tosaithe is a minimalistic UEFI-firmware menu/bootloader. It can chain-load other EFI programs and loaders, including Linux kernels, and has

Davin McCall 11 Nov 30, 2022
ColecoAdam a boot menu and configurator for ADE virtual drives

VHS HEAD For Coleco Adam users with Adamnet Disk Emulator (ADE) devices. VHS Head is a boot menu and configuration program for ADE (Pro and Lite) user

Thomas Cherryhomes 3 Dec 24, 2021
🎨 A secure boot compatible in-memory UxTheme patcher

SecureUxTheme About SecureUxTheme is a software that removes signature verification of styles from Windows. Features No system file modifications No d

null 1.4k Nov 24, 2022
A pre-boot execution environment for Apple boards built on top of checkra1n

archOS A pre-boot execution environment for Apple boards built on top of checkra1n - currently based off the Checkra1n/PongoOS Repo. Building on macOS

ScarletAI 2 Jan 17, 2022
Memory Process File System (MemProcFS) is an easy and convenient way of viewing physical memory as files in a virtual file system

The Memory Process File System (MemProcFS) is an easy and convenient way of viewing physical memory as files in a virtual file system.

Ulf Frisk 1.7k Dec 2, 2022
null 3 Jan 19, 2022
Physical Tic-Tac-Toe smart board with PvP mode and two levels of AI. Built atop a custom PCB connected to an Arduino Mega 2560.

TicTacToe_SmartBoard The files in TicTacToe_SmartBoard are based on the files in https://wiki.illinois.edu/wiki/display/ECE110HLSF15/Tic-Tac-Toe+Smart

Hyrum Dickinson 2 Jul 22, 2022
Breaking the physical limits of fonts

Breaking the physical limits of fonts The challenge: in the fewest resources possible, render meaningful text. How small can a font really go? How man

Dale Weiler 319 Nov 24, 2022
A graphical interface to set options on devices with coreboot firmware

Corevantage A graphical interface to set options on devices with coreboot firmware. Introduction This is a utility that allows users to view and modif

null 30 Sep 22, 2022
Full Firmware Package for the STM32WB series: HAL+LL drivers, CMSIS, BSP, MW, plus a set of Projects

STM32Cube is an STMicroelectronics original initiative to ease developers' life by reducing efforts, time and cost.

STMicroelectronics 140 Nov 28, 2022
oZKS (Ordered Zero-Knowledge Set) is a library that provides an implementation of an Ordered (and Append Only) Zero-Knowledge Set.

Ordered Zero-Knowledge Set - oZKS Introduction oZKS is a library that provides an implementation of an Ordered (and Append Only) Zero Knowledge Set. A

Microsoft 10 Oct 12, 2022