{"id":16616752,"url":"https://github.com/stecman/hw-boot-selection","last_synced_at":"2025-04-07T06:06:46.246Z","repository":{"id":43224527,"uuid":"364051298","full_name":"stecman/hw-boot-selection","owner":"stecman","description":"STM32 firmware for a physical switch to set the GRUB boot selection","archived":false,"fork":false,"pushed_at":"2022-03-12T22:25:38.000Z","size":60,"stargazers_count":361,"open_issues_count":0,"forks_count":21,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-03-31T04:07:14.287Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stecman.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-05-03T20:21:59.000Z","updated_at":"2025-03-11T12:17:57.000Z","dependencies_parsed_at":"2022-07-21T23:17:49.287Z","dependency_job_id":null,"html_url":"https://github.com/stecman/hw-boot-selection","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stecman%2Fhw-boot-selection","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stecman%2Fhw-boot-selection/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stecman%2Fhw-boot-selection/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stecman%2Fhw-boot-selection/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stecman","download_url":"https://codeload.github.com/stecman/hw-boot-selection/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-12T02:14:08.131Z","updated_at":"2025-04-07T06:06:46.220Z","avatar_url":"https://github.com/stecman.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GRUB boot selector switch\n\nFirmware for the [Hardware Boot Selection Switch](https://hackaday.io/project/179539-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.\n\n\n## Building\n\nOn Linux, you'll need `gcc-arm-none-eabi` and `python` installed to build and `openocd` or [`st-flash`](https://github.com/stlink-org/stlink) to flash.\n\n```sh\n# Pull in libopencm3\ngit submodule init\ngit submodule update\n\n# Build libopencm3 (only needed once)\nmake -C libopencm3 -j8\n\n# Build the firmware\nmake\n\n# Flash with a J-Link programmer connected by SWD\nmake flash\n\n# Flash with an ST-Link programmer connected by SWD\nmake flash PROGRAMMER=stlink\n\n# Flash using st-flash instead of openocd (only supports ST-Link programmers)\nmake flash PROGRAMMER=st-flash\n```\n\nThis project supports building and flashing multiple different parts. The following parts have been tested:\n\n- `stm32f103c4` (default): supports all variants of the F103 chip found on the \"blue pill\" and \"maple\" STM32 dev boards.\n- `stm32f103c6`\n- `stm32f103c8`\n- `stm32f070f6`\n\nTo build for your specific part, pass the `DEVICE` parameter to `make` on the command line (or export `DEVICE` as an environment variable): \n\n```sh\nmake DEVICE=stm32f070f6\nmake flash DEVICE=stm32f070f6\n```\n\nValid patterns for the `DEVICE` parameter can be found in `libopencm3/ld/devices.data`. If you are adding custom functionality, specify your exact variant to have access to the full RAM and flash; the default is the lowest spec F103 part.\n\n## Wiring\n\nThe 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.\n\n\n## GRUB config\n\nYou'll need to modify your system's GRUB configuration to look for and read from this device during boot.\n\nOn Debian and Arch based systems at least, this can be done non-destructively by creating an additional script in `/etc/grub.d/`:\n\n1. Create `/etc/grub.d/01_bootswitch` with the following contents:\n  \n  ```sh\n  #! /bin/sh\n\n  cat \u003c\u003c 'EOF'\n  # Look for hardware switch device by its hard-coded filesystem ID\n  search --no-floppy --fs-uuid --set hdswitch 55AA-6922\n\n  # If found, read dynamic config file and select appropriate entry for each position\n  if [ \"${hdswitch}\" ] ; then\n    source ($hdswitch)/switch_position_grub.cfg\n\n    if [ \"${os_hw_switch}\" == 0 ] ; then\n      # Boot Linux\n      set default=\"0\"\n    elif [ \"${os_hw_switch}\" == 1 ] ; then\n      # Boot Windows\n      set default=\"2\"\n    fi\n\n  fi\n  EOF\n  ```\n\n2. Make it executable: `chmod +x /etc/grub.d/01_bootswitch`\n\n3. Run `update-grub` to generate and validate the `/boot/grub/grub.cfg` file that's used when booting.\n\n\nThis overrides the `default` variable set in `00_header`. If the switch is not found, your existing default selection will be used.\n\nYou may need to change the values on the `set default=` lines if you have a different arrangement of choices in your GRUB menu.\n\n## Troubleshooting\n\n### Device not reliably detected\n\nIssues with intermittent USB enumeration have been reported when using the `cks32f103` clone chip. These chips will flash, but may not function correctly as they are not genuine ST parts. Check the markings on your chip if you experience detection issues. Avoid buying the \"CKS\" parts if possible.\n\n### The device is not accessible in GRUB\n\nEnsure 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.\n\n### Reading from an operating system\n\nThe 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.\n\nIf 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.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstecman%2Fhw-boot-selection","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstecman%2Fhw-boot-selection","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstecman%2Fhw-boot-selection/lists"}