36 Commits

Author SHA1 Message Date
160a7737fb sprout: version 0.0.9 2025-10-24 18:17:27 -07:00
68220d0de1 chore(workflows): repin all actions 2025-10-24 18:09:24 -07:00
e9b842a81f chore(workflows): separate release workflow 2025-10-24 18:04:55 -07:00
efb357d62b chore(workflows): add concurrency limiter 2025-10-24 18:01:50 -07:00
30600f0c81 fix(main): repair contextual replacement for entries 2025-10-24 17:16:42 -07:00
e10e98d669 chore(assets): update sprout logo 2025-10-24 16:49:34 -07:00
911b617d92 feat(options): --boot now supports selecting by entry name or index, not just title 2025-10-24 16:32:48 -07:00
d3f9e876fb fix(docs): cleanup rustdoc warnings 2025-10-24 15:55:56 -07:00
e096f8e236 sprout: version 0.0.8 2025-10-24 14:53:17 -07:00
a14686a286 update README with background section 2025-10-24 14:49:11 -07:00
5108b61a15 implement new argument parser with --help support 2025-10-21 19:12:16 -07:00
2aeb0474e6 dev: add support for automatic hypervisor.framework usage on macOS 2025-10-21 05:40:22 -07:00
22c8884f7e booting windows is supported! 2025-10-20 20:35:13 -07:00
3a2b314669 chore(options): move parsing code to a parser module 2025-10-20 19:42:39 -07:00
1171959a52 sprout: version v0.0.7 2025-10-20 19:23:05 -07:00
c5ec8dc6a6 add readme information about sprout command line arguments 2025-10-20 18:21:41 -07:00
c749c8d38e implement a new sprout command line options mechanism 2025-10-20 18:17:29 -07:00
3d2c31ee1a add a windows setup guide 2025-10-20 13:21:43 -07:00
a02ee88afd add a generic linux setup guide 2025-10-20 12:07:22 -07:00
b59626888e add bls quirk to support fedora out of the box 2025-10-20 11:33:33 -07:00
e3bae1dc63 repair fedora setup guide to include copying grub modules 2025-10-20 11:24:42 -07:00
3cd3491df0 fix bad autocomplete in fedora setup guide 2025-10-20 10:37:28 -07:00
e08f6e629f add fedora setup guide 2025-10-20 10:34:55 -07:00
398be12ac4 fix release tag 2025-10-20 09:26:03 -07:00
7407150bff attempt new release mechanism using draft releases and manual job 2025-10-20 09:24:27 -07:00
c23b11469d attempt at codeql config for the right target 2025-10-20 09:08:29 -07:00
29529ddacd remove broken codeql configs, will figure this out later 2025-10-20 01:51:20 -07:00
d2f47dcad6 attempt to configure rust extractor for the right target 2025-10-20 01:44:27 -07:00
f3b7007432 codeql advanced support 2025-10-20 01:29:03 -07:00
4f30d51bb2 make sure rust toolchain is from rust-toolchain.toml in github workflows 2025-10-20 01:18:28 -07:00
48e3644977 fix pull request actions 2025-10-20 01:14:08 -07:00
345e1c800c Merge pull request #11 from edera-dev/dependabot/cargo/cargo-updates-d94e05c250
Bump toml from 0.9.7 to 0.9.8 in the cargo-updates group
2025-10-20 00:33:30 -07:00
e5ae612398 Merge pull request #10 from edera-dev/dependabot/docker/docker-updates-d0b0844295
Bump rustlang/rust from `b8107fa` to `141e9a7` in the docker-updates group
2025-10-20 00:31:54 -07:00
dependabot[bot]
f0427faab2 Bump toml from 0.9.7 to 0.9.8 in the cargo-updates group
Bumps the cargo-updates group with 1 update: [toml](https://github.com/toml-rs/toml).


Updates `toml` from 0.9.7 to 0.9.8
- [Commits](https://github.com/toml-rs/toml/compare/toml-v0.9.7...toml-v0.9.8)

---
updated-dependencies:
- dependency-name: toml
  dependency-version: 0.9.8
  dependency-type: direct:production
  update-type: version-update:semver-patch
  dependency-group: cargo-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-20 07:29:29 +00:00
dependabot[bot]
6278274288 Bump rustlang/rust in the docker-updates group
Bumps the docker-updates group with 1 update: rustlang/rust.


Updates `rustlang/rust` from `b8107fa` to `141e9a7`

---
updated-dependencies:
- dependency-name: rustlang/rust
  dependency-version: nightly-alpine
  dependency-type: direct:production
  dependency-group: docker-updates
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-10-20 07:29:15 +00:00
4cd27a5010 add docker to dependabot 2025-10-20 00:17:49 -07:00
32 changed files with 921 additions and 122 deletions

5
.github/codeql/codeql-config.yaml vendored Normal file
View File

@@ -0,0 +1,5 @@
name: "codeql-config"
extractor-options:
rust:
cargo_target: x86_64-unknown-uefi

View File

@@ -26,3 +26,16 @@ updates:
cargo-dev-updates:
dependency-type: development
applies-to: version-updates
- package-ecosystem: docker
directory: /
schedule:
interval: weekly
cooldown:
default-days: 7
groups:
docker-updates:
dependency-type: production
applies-to: version-updates
docker-dev-updates:
dependency-type: development
applies-to: version-updates

View File

@@ -9,6 +9,10 @@ on:
permissions:
contents: read # Needed to checkout the repository.
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
cancel-in-progress: true
jobs:
zizmor:
name: zizmor
@@ -24,12 +28,12 @@ jobs:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: setup uv
uses: astral-sh/setup-uv@3259c6206f993105e3a61b142c2d97bf4b9ef83d # v7
uses: astral-sh/setup-uv@2ddd2b9cb38ad8efd50337e8ab201519a34c9f24 # v7.1.1
- name: zizmor
run: uvx zizmor --pedantic --format sarif . > results.sarif
@@ -37,7 +41,7 @@ jobs:
GH_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- name: upload
uses: github/codeql-action/upload-sarif@16140ae1a102900babc80a33c44059580f687047 # v4
uses: github/codeql-action/upload-sarif@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.0
with:
sarif_file: results.sarif
category: zizmor

View File

@@ -11,6 +11,10 @@ on:
permissions:
contents: read # Needed to checkout the repository.
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
cancel-in-progress: true
jobs:
rustfmt:
name: rustfmt
@@ -22,14 +26,13 @@ jobs:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: 'install nightly rust toolchain with rustfmt'
- name: 'install rust toolchain with rustfmt'
run: |
rustup update --no-self-update nightly
rustup default nightly
cargo version
rustup component add rustfmt
- name: 'cargo fmt'
@@ -53,14 +56,13 @@ jobs:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: 'install nightly rust toolchain'
- name: 'install rust toolchain'
run: |
rustup update --no-self-update nightly
rustup default nightly
cargo version
- name: cargo build
run: cargo build --target "${TARGET_ARCH}-unknown-uefi"
@@ -83,14 +85,13 @@ jobs:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: 'install nightly rust toolchain with clippy'
- name: 'install rust toolchain with clippy'
run: |
rustup update --no-self-update nightly
rustup default stable
cargo version
rustup component add clippy
- name: cargo clippy

57
.github/workflows/codeql.yaml vendored Normal file
View File

@@ -0,0 +1,57 @@
name: codeql
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
schedule:
- cron: '33 16 * * 0'
permissions:
contents: read # Needed to checkout the repository.
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
cancel-in-progress: true
jobs:
analyze:
name: analyze (${{ matrix.language }})
runs-on: 'ubuntu-latest'
permissions:
security-events: write # Needed to upload results.
packages: read # Needed to fetch internal or private CodeQL packs.
actions: read # Needed to read workflows.
contents: read # Needed to checkout the repository.
strategy:
fail-fast: false
matrix:
include:
- language: actions
build-mode: none
- language: rust
build-mode: none
steps:
- name: harden runner
uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
with:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: initialize codeql
uses: github/codeql-action/init@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.0
with:
languages: ${{ matrix.language }}
build-mode: ${{ matrix.build-mode }}
config-file: ./.github/codeql/codeql-config.yaml
- name: perform codeql analysis
uses: github/codeql-action/analyze@4e94bd11f71e507f7f87df81788dff88d1dacbfb # v4.31.0
with:
category: "/language:${{matrix.language}}"

View File

@@ -1,10 +1,6 @@
name: publish
on:
release:
types:
- created
push:
branches:
- main
@@ -22,11 +18,15 @@ on:
permissions:
contents: read # Needed to checkout the repository.
concurrency:
group: "${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}"
cancel-in-progress: true
jobs:
artifacts:
name: artifacts
permissions:
contents: write # Needed to upload release assets and artifacts.
contents: write # Needed to upload artifacts.
runs-on: ubuntu-latest
steps:
- name: harden runner
@@ -35,40 +35,25 @@ jobs:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: 'install nightly rust toolchain'
- name: 'install rust toolchain'
run: |
rustup update --no-self-update nightly
rustup default nightly
cargo version
- name: 'assemble artifacts'
run: ./hack/assemble.sh
- name: 'upload sprout-x86_64.efi artifact'
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
name: sprout-x86_64.efi
path: target/assemble/sprout-x86_64.efi
- name: 'upload sprout-aarch64.efi artifact'
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
uses: actions/upload-artifact@330a01c490aca151604b8cf639adc76d48f6c5d4 # v5.0.0
with:
name: sprout-aarch64.efi
path: target/assemble/sprout-aarch64.efi
- name: 'generate cultivator token'
uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4
id: generate-token
with:
app-id: "${{ secrets.EDERA_CULTIVATION_APP_ID }}"
private-key: "${{ secrets.EDERA_CULTIVATION_APP_PRIVATE_KEY }}"
- name: 'upload release artifacts'
run: ./hack/ci/upload-release-assets.sh
env:
GITHUB_TOKEN: "${{ steps.generate-token.outputs.token }}"
RELEASE_TAG: "${{ github.event.release.tag_name }}"
if: ${{ github.event_name == 'release' }}

62
.github/workflows/release.yaml vendored Normal file
View File

@@ -0,0 +1,62 @@
name: release
on:
workflow_dispatch:
inputs:
release-tag:
description: 'Release Tag'
required: true
type: string
permissions:
contents: read # Needed to checkout the repository.
concurrency:
group: "${{ github.workflow }}-${{ github.event.inputs.release-tag }}"
cancel-in-progress: true
jobs:
release:
name: release
permissions:
contents: write # Needed to upload release assets.
runs-on: ubuntu-latest
steps:
- name: harden runner
uses: step-security/harden-runner@f4a75cfd619ee5ce8d5b864b0d183aff3c69b55a # v2.13.1
with:
egress-policy: audit
- name: checkout
uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- name: 'install rust toolchain'
run: |
cargo version
- name: 'assemble artifacts'
run: ./hack/assemble.sh
- name: 'generate cultivator token'
uses: actions/create-github-app-token@bf559f85448f9380bcfa2899dbdc01eb5b37be3a # v3.0.0-beta.2
id: generate-token
with:
app-id: "${{ secrets.EDERA_CULTIVATION_APP_ID }}"
private-key: "${{ secrets.EDERA_CULTIVATION_APP_PRIVATE_KEY }}"
if: ${{ github.event.inputs.release-tag != '' }}
- name: 'upload release artifacts'
run: ./hack/ci/upload-release-assets.sh
env:
GITHUB_TOKEN: "${{ steps.generate-token.outputs.token }}"
RELEASE_TAG: "${{ github.event.inputs.release-tag }}"
if: ${{ github.event.inputs.release-tag != '' }}
- name: 'mark release as published'
run: gh release edit "${RELEASE_TAG}" --draft=false --verify-tag
env:
GITHUB_TOKEN: "${{ steps.generate-token.outputs.token }}"
RELEASE_TAG: "${{ github.event.inputs.release-tag }}"
if: ${{ github.event.inputs.release-tag != '' }}

22
Cargo.lock generated
View File

@@ -61,7 +61,7 @@ dependencies = [
[[package]]
name = "edera-sprout"
version = "0.0.4"
version = "0.0.9"
dependencies = [
"anyhow",
"image",
@@ -252,9 +252,9 @@ dependencies = [
[[package]]
name = "serde_spanned"
version = "1.0.2"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5417783452c2be558477e104686f7de5dae53dba813c28435e0e70f82d9b04ee"
checksum = "e24345aa0fe688594e73770a5f6d1b216508b4f93484c0026d521acd30134392"
dependencies = [
"serde_core",
]
@@ -277,9 +277,9 @@ dependencies = [
[[package]]
name = "toml"
version = "0.9.7"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00e5e5d9bf2475ac9d4f0d9edab68cc573dc2fd644b0dba36b0c30a92dd9eaa0"
checksum = "f0dc8b1fb61449e27716ec0e1bdf0f6b8f3e8f6b05391e8497b8b6d7804ea6d8"
dependencies = [
"indexmap",
"serde_core",
@@ -292,27 +292,27 @@ dependencies = [
[[package]]
name = "toml_datetime"
version = "0.7.2"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32f1085dec27c2b6632b04c80b3bb1b4300d6495d1e129693bdda7d91e72eec1"
checksum = "f2cdb639ebbc97961c51720f858597f7f24c4fc295327923af55b74c3c724533"
dependencies = [
"serde_core",
]
[[package]]
name = "toml_parser"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4cf893c33be71572e0e9aa6dd15e6677937abd686b066eac3f8cd3531688a627"
checksum = "c0cbe268d35bdb4bb5a56a2de88d0ad0eb70af5384a99d648cd4b3d04039800e"
dependencies = [
"winnow",
]
[[package]]
name = "toml_writer"
version = "1.0.3"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d163a63c116ce562a22cda521fcc4d79152e7aba014456fb5eb442f6d6a10109"
checksum = "df8b2b54733674ad286d16267dcfc7a71ed5c776e4ac7aa3c3e2561f7c637bf2"
[[package]]
name = "ucs2"

View File

@@ -2,14 +2,14 @@
name = "edera-sprout"
description = "Modern UEFI bootloader"
license = "Apache-2.0"
version = "0.0.4"
version = "0.0.9"
homepage = "https://sprout.edera.dev"
repository = "https://github.com/edera-dev/sprout"
edition = "2024"
[dependencies]
anyhow = "1.0.100"
toml = "0.9.7"
toml = "0.9.8"
log = "0.4.28"
[dependencies.image]

View File

@@ -2,7 +2,7 @@
ARG RUST_PROFILE=release
ARG RUST_TARGET_SUBDIR=release
FROM --platform=$BUILDPLATFORM rustlang/rust:nightly-alpine@sha256:b8107fa66d3e5ad7f729d3347c7feedbd3f4b60b01006edce39eb6b994ff00bd AS build
FROM --platform=$BUILDPLATFORM rustlang/rust:nightly-alpine@sha256:141e9a7f13f77237dd4d462364c3a1b21cb8a6791d8924c409573e77b788af5e AS build
RUN apk --no-cache add musl-dev busybox-static
ARG RUST_PROFILE
RUN adduser -S -s /bin/sh build

View File

@@ -1,6 +1,6 @@
<div align="center">
![Sprout Logo](assets/logo.png)
![Sprout Logo](assets/logo-small.png)
# Sprout
@@ -18,6 +18,34 @@ existing UEFI bootloader or booted by the hardware directly.
Sprout is licensed under Apache 2.0 and is open to modifications and contributions.
## Background
At [Edera] we make compute isolation technology for a wide variety of environments, often ones we do not fully control.
Our technology utilizes a hypervisor to boot the host system to provide a new isolation mechanism that works
with or without hardware virtualization support. To do this we need to inject our hypervisor at boot time.
Unfortunately, GRUB, the most common bootloader on Linux systems today, utilizes a shell-script like
configuration system. Both the code that runs to generate a GRUB config and the GRUB config
itself is fully turing complete. This makes modifying boot configuration difficult and error-prone.
Sprout was designed to take in a machine-readable, writable, and modifiable configuration that treats boot information
like data plus configuration, and can be chained from both UEFI firmware and GRUB alike.
Sprout aims to be flexible, secure, and modern. Written in Rust, it handles data safely and uses unsafe code as little
as possible. It also critically must be easy to install into all common distributions, relying on simple principles to
simplify installation and usage.
## Documentation
- [Fedora Setup Guide]
- [Generic Linux Setup Guide]
- [Windows Setup Guide]
- [Development Guide]
- [Contributing Guide]
- [Sprout License]
- [Code of Conduct]
- [Security Policy]
## Features
NOTE: Currently, Sprout is experimental and is not intended for production use. For example, it doesn't currently
@@ -29,6 +57,7 @@ have secure boot support. In fact, as of writing, it doesn't even have a boot me
- [x] [Bootloader specification (BLS)](https://uapi-group.org/specifications/specs/boot_loader_specification/) support
- [x] Chainload support
- [x] Linux boot support via EFI stub
- [x] Windows boot support via chainload
- [x] Load Linux initrd from disk
- [x] Boot first configured entry
@@ -37,7 +66,6 @@ have secure boot support. In fact, as of writing, it doesn't even have a boot me
- [ ] Boot menu
- [ ] Secure Boot support: work in progress
- [ ] UKI support: partial
- [ ] Windows boot support (untested via chainload)
- [ ] multiboot2 support
- [ ] Linux boot protocol (boot without EFI stub)
@@ -62,6 +90,17 @@ See [Configuration](#configuration) for how to configure sprout.
Sprout is configured using a TOML file at `\sprout.toml` on the root of the EFI partition sprout was booted from.
### Command Line Options
Sprout supports some command line options that can be combined to modify behavior without the configuration file.
```bash
# Boot Sprout with a specific configuration file.
$ sprout.efi --config=\path\to\config.toml
# Boot a specific entry, bypassing the menu.
$ sprout.efi --boot="Boot Xen"
```
### Boot Linux from ESP
```toml
@@ -116,3 +155,13 @@ chainload.path = "$boot\\$chainload"
chainload.options = ["$options"]
chainload.linux-initrd = "$boot\\$initrd"
```
[Edera]: https://edera.dev
[Fedora Setup Guide]: ./docs/fedora-setup.md
[Generic Linux Setup Guide]: ./docs/generic-linux-setup.md
[Windows Setup Guide]: ./docs/windows-setup.md
[Development Guide]: ./DEVELOPMENT.md
[Contributing Guide]: ./CONTRIBUTING.md
[Sprout License]: ./LICENSE
[Code of Conduct]: ./CODE_OF_CONDUCT.md
[Security Policy]: ./SECURITY.md

BIN
assets/logo-large.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 34 KiB

BIN
assets/logo-small.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

134
docs/fedora-setup.md Normal file
View File

@@ -0,0 +1,134 @@
# Setup Sprout on Fedora
## Prerequisites
- Modern Fedora release: tested on Fedora Workstation 42 and 43
- EFI System Partition mounted on `/boot/efi` (the default)
- ext4 or FAT32/exFAT formatted `/boot` partition
## Step 1: Base Installation
Download the latest sprout.efi release from the [GitHub releases page](https://github.com/edera-dev/sprout/releases).
For x86_64 systems, download the `sprout-x86_64.efi` file, and for ARM systems, download the `sprout-aarch64.efi` file.
Copy the downloaded `sprout.efi` file to `/boot/efi/EFI/BOOT/sprout.efi` on your EFI System Partition.
Additionally, you will want to install the `edk2-ext4` package, which provides the ext4 filesystem support for Sprout.
```bash
# Install the edk2-ext4 package which provides ext4 support for Sprout.
$ sudo dnf install edk2-ext4
# Create a directory for sprout drivers.
$ sudo mkdir -p /boot/efi/sprout/drivers
# For x86_64 systems, copy the ext4x64.efi driver to the sprout drivers directory.
$ sudo cp /usr/share/edk2/drivers/ext4x64.efi /boot/efi/sprout/drivers/ext4.efi
# For ARM64 systems, copy the ext4aa64.efi driver to the sprout drivers directory.
$ sudo cp /usr/share/edk2/drivers/ext4aa64.efi /boot/efi/sprout/drivers/ext4.efi
```
## Step 2: Configure Sprout
Since Fedora uses the BLS specification, you can use the `bls` generator to autoconfigure Sprout for Fedora.
Write the following file to `/boot/efi/sprout.toml`:
```toml
# sprout configuration: version 1
version = 1
# load an EFI driver for ext4.
[drivers.ext4]
path = "\\sprout\\drivers\\ext4.efi"
# extract the full path of the first filesystem
# that contains \loader\entries as a directory
# into the value called "boot"
[extractors.boot.filesystem-device-match]
has-item = "\\loader\\entries"
# use the sprout bls module to scan a bls
# directory for entries and load them as boot
# entries in sprout, using the entry template
# as specified here. the bls action below will
# be passed the extracted values from bls.
[generators.boot.bls]
path = "$boot\\loader\\entries"
entry.title = "$title"
entry.actions = ["bls"]
# the action that is used for each bls entry above.
[actions.bls]
chainload.path = "$boot\\$chainload"
chainload.options = ["$options"]
chainload.linux-initrd = "$boot\\$initrd"
```
## Step 3, Option 1: Configure GRUB to load Sprout (recommended)
You can configure GRUB to add a boot entry for Sprout, so you can continue to use GRUB without interruption.
GRUB needs to be configured with the chainloader module to load Sprout.
### x86_64
```bash
# Install x86_64 GRUB modules.
$ sudo dnf install grub2-efi-x64-modules
# Copy x86_64 GRUB modules to /boot/grub2 for use by GRUB if it isn't installed already.
$ [ ! -d /boot/grub2/x86_64-efi ] && sudo cp -r /usr/lib/grub/x86_64-efi /boot/grub2/x86_64-efi
```
### ARM64
```bash
# Install ARM64 GRUB modules.
$ sudo dnf install grub2-efi-aa64-modules
# Copy ARM64 GRUB modules to /boot/grub2 for use by GRUB if it isn't installed already.
$ [ ! -d /boot/grub2/arm64-efi ] && sudo cp -r /usr/lib/grub/arm64-efi /boot/grub2/x86_64-efi
```
You will need to find the UUID of your EFI System Partition. You can do this by running the following command:
```bash
$ grep "/boot/efi" /etc/fstab | awk '{print $1}' | awk -F '=' '{print $2}'
SAMPLE-VALUE
```
The GRUB configuration for Sprout is as follows, replace `SAMPLE-VALUE` with the UUID of your EFI System Partition:
```grub
menuentry 'Sprout' $menuentry_id_option 'sprout' {
insmod part_gpt
insmod fat
insmod chain
search --no-floppy --fs-uuid --set=root SAMPLE-VALUE
chainloader /EFI/BOOT/sprout.efi
}
```
You can append this to `/etc/grub.d/40_custom` and run the following command to update your configuration:
```bash
$ sudo grub2-mkconfig -o /boot/grub2/grub.cfg
```
To update your GRUB configuration.
Make sure to update your GRUB environment to show the menu:
```bash
$ sudo grub2-editenv - set menu_auto_hide=0
```
You may now reboot your system and select Sprout from the GRUB menu.
## Step 3, Option 2: Configure your EFI firmware for Sprout
You can configure your EFI boot menu to show Sprout as an option.
To do so, please find the partition device of your EFI System Partition and run the following:
```bash
$ sudo efibootmgr -d /dev/esp_partition_here -C -L 'Sprout' -l '\EFI\BOOT\sprout.efi'
```
This will add a new entry to your EFI boot menu called `Sprout` that will boot Sprout with your configuration.
Now if you boot into your UEFI firmware, you should see Sprout as an option to boot.

View File

@@ -0,0 +1,62 @@
# Setup Sprout to boot Linux
## Prerequisites
- EFI System Partition mounted on a known path
- Linux kernel installed with an optional initramfs
- Linux kernel must support the EFI stub (most distro kernels)
## Step 1: Base Installation
First, identify the path to your EFI System Partition. On most systems, this is `/boot/efi`.
Download the latest sprout.efi release from the [GitHub releases page](https://github.com/edera-dev/sprout/releases).
For x86_64 systems, download the `sprout-x86_64.efi` file, and for ARM systems, download the `sprout-aarch64.efi` file.
Copy the downloaded `sprout.efi` file to `/EFI/BOOT/sprout.efi` on your EFI System Partition.
## Step 2: Copy kernel and optional initramfs
Copy the Linux kernel to `/vmlinuz-sprout` on your EFI System Partition.
If needed, copy the initramfs to `/initramfs-sprout` on your EFI System Partition.
## Step 3: Configure Sprout
Write the following file to `/sprout.toml` on your EFI System Partition,
paying attention to place the correct values:
```toml
# sprout configuration: version 1
version = 1
# add a boot entry for booting linux
# which will run the boot-linux action.
[entries.boot-linux]
title = "Boot Linux"
actions = ["boot-linux"]
# use the chainload action to boot linux via the efi stub.
# the options below are passed to the efi stub as the
# kernel command line. the initrd is loaded using the efi stub
# initrd loader mechanism.
[actions.boot-linux]
chainload.path = "\\vmlinuz-sprout"
chainload.options = ["root=/dev/sda1", "my-kernel-option"]
chainload.linux-initrd = "\\initramfs-sprout"
```
You can specify any kernel command line options you want on the chainload options line.
They will be concatenated by a space and passed to the kernel.
## Step 4: Configure EFI firmware to boot Sprout
Since Sprout is still experimental, the following commands will add a boot entry to your EFI firmware for sprout but
intentionally do not set it as the default boot entry.
To add the entry, please find the partition device of your EFI System Partition and run the following:
```bash
$ sudo efibootmgr -d /dev/esp_partition_here -C -L 'Sprout' -l '\EFI\BOOT\sprout.efi'
```
This will add a new entry to your EFI boot menu called `Sprout` that will boot Sprout with your configuration.
Now if you boot into your UEFI firmware, you should see Sprout as an option to boot.

52
docs/windows-setup.md Normal file
View File

@@ -0,0 +1,52 @@
# Setup Sprout to boot Windows
## Prerequisites
- Secure Boot disabled
- UEFI Windows installation
## Step 1: Base Installation
First, mount the EFI System Partition on your Windows installation:
In an administrator command prompt, run:
```batch
> mountvol X: /s
```
This will mount the EFI System Partition to the drive letter `X:`.
Please note that Windows Explorer will not let you see the drive letter `X:` where the ESP is mounted.
You will need to use the command prompt or PowerShell to access the ESP.
Standard editors can, however, be used to edit files on the ESP.
Download the latest sprout.efi release from the [GitHub releases page](https://github.com/edera-dev/sprout/releases).
For x86_64 systems, download the `sprout-x86_64.efi` file, and for ARM systems, download the `sprout-aarch64.efi` file.
Copy the downloaded `sprout.efi` file to `X:\EFI\BOOT\sprout.efi` on your EFI System Partition.
## Step 3: Configure Sprout
Write the following file to `X:\sprout.toml`:
```toml
# sprout configuration: version 1
version = 1
# add a boot entry for booting Windows
# which will run the boot-windows action.
[entries.windows]
title = "Windows"
actions = ["boot-windows"]
# use the chainload action to boot the Windows bootloader.
[actions.boot-windows]
chainload.path = "\\EFI\\Microsoft\\Boot\\bootmgfw.efi"
```
## Step 4: Configure EFI Firmware to boot Sprout
It is not trivial to add an EFI boot entry inside Windows.
However, most firmware lets you load arbitrary EFI files from the firmware settings.
You can boot `\EFI\BOOT\sprout.efi` from firmware to boot Sprout.

View File

@@ -41,3 +41,8 @@ if [ -z "${QEMU_ACCEL}" ] && [ "${TARGET_ARCH}" = "${HOST_ARCH}" ] &&
grep -E '^flags.*:.+ vmx .*' /proc/cpuinfo >/dev/null; then
QEMU_ACCEL="kvm"
fi
if [ "$(uname)" = "Darwin" ] && [ "${TARGET_ARCH}" = "${HOST_ARCH}" ] &&
[ "$(sysctl -n kern.hv_support 2>&1 || true)" = "1" ]; then
QEMU_ACCEL="hvf"
fi

View File

@@ -19,7 +19,7 @@ elif [ "${TARGET_ARCH}" = "aarch64" ]; then
fi
if [ -n "${QEMU_ACCEL}" ]; then
set -- "${@}" "-accel" "kvm"
set -- "${@}" "-accel" "${QEMU_ACCEL}"
fi
if [ "${QEMU_GDB}" = "1" ]; then

View File

@@ -0,0 +1,11 @@
version = 1
[extractors.boot.filesystem-device-match]
has-item = "\\EFI\\BOOT\\shell.efi"
[actions.chainload-shell]
chainload.path = "$boot\\EFI\\BOOT\\shell.efi"
[entries.xen]
title = "Boot Shell"
actions = ["chainload-shell"]

View File

@@ -1,31 +1,36 @@
use crate::config::{RootConfiguration, latest_version};
use crate::options::SproutOptions;
use crate::utils;
use anyhow::{Context, Result, bail};
use log::info;
use std::ops::Deref;
use toml::Value;
use uefi::proto::device_path::LoadedImageDevicePath;
/// Loads the raw configuration from the sprout.toml file as data.
fn load_raw_config() -> Result<Vec<u8>> {
/// Loads the raw configuration from the sprout config file as data.
fn load_raw_config(options: &SproutOptions) -> Result<Vec<u8>> {
// Open the LoadedImageDevicePath protocol to get the path to the current image.
let current_image_device_path_protocol =
uefi::boot::open_protocol_exclusive::<LoadedImageDevicePath>(uefi::boot::image_handle())
.context("unable to get loaded image device path")?;
// Acquire the device path as a boxed device path.
let path = current_image_device_path_protocol.deref().to_boxed();
// Read the contents of the sprout.toml file.
let content = utils::read_file_contents(&path, "sprout.toml")
.context("unable to read sprout.toml file")?;
// Return the contents of the sprout.toml file.
info!("configuration file: {}", options.config);
// Read the contents of the sprout config file.
let content = utils::read_file_contents(&path, &options.config)
.context("unable to read sprout config file")?;
// Return the contents of the sprout config file.
Ok(content)
}
/// Loads the [RootConfiguration] for Sprout.
pub fn load() -> Result<RootConfiguration> {
// Load the raw configuration from the sprout.toml file.
let content = load_raw_config()?;
pub fn load(options: &SproutOptions) -> Result<RootConfiguration> {
// Load the raw configuration from the sprout config file.
let content = load_raw_config(options)?;
// Parse the raw configuration into a toml::Value which can represent any TOML file.
let value: Value = toml::from_slice(&content).context("unable to parse sprout.toml file")?;
let value: Value = toml::from_slice(&content).context("unable to parse sprout config file")?;
// Check the version of the configuration without parsing the full configuration.
let version = value

View File

@@ -1,4 +1,5 @@
use crate::actions::ActionDeclaration;
use crate::options::SproutOptions;
use anyhow::Result;
use anyhow::anyhow;
use std::collections::{BTreeMap, BTreeSet};
@@ -13,15 +14,18 @@ pub struct RootContext {
actions: BTreeMap<String, ActionDeclaration>,
/// The device path of the loaded Sprout image.
loaded_image_path: Option<Box<DevicePath>>,
/// The global options of Sprout.
options: SproutOptions,
}
impl RootContext {
/// Creates a new root context with the `loaded_image_device_path` which will be stored
/// in the context for easy access.
pub fn new(loaded_image_device_path: Box<DevicePath>) -> Self {
pub fn new(loaded_image_device_path: Box<DevicePath>, options: SproutOptions) -> Self {
Self {
actions: BTreeMap::new(),
loaded_image_path: Some(loaded_image_device_path),
options,
}
}
@@ -41,6 +45,11 @@ impl RootContext {
.as_deref()
.ok_or_else(|| anyhow!("no loaded image path"))
}
/// Access the global Sprout options.
pub fn options(&self) -> &SproutOptions {
&self.options
}
}
/// A context of Sprout. This is passed around different parts of Sprout and represents

View File

@@ -19,7 +19,7 @@ pub struct DriverDeclaration {
pub path: String,
}
/// Loads the driver specified by the [driver] declaration.
/// Loads the driver specified by the `driver` declaration.
fn load_driver(context: Rc<SproutContext>, driver: &DriverDeclaration) -> Result<()> {
// Acquire the handle and device path of the loaded image.
let sprout_image = uefi::boot::image_handle();

View File

@@ -1,5 +1,7 @@
use crate::context::SproutContext;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::rc::Rc;
/// Declares a boot entry to display in the boot menu.
///
@@ -8,6 +10,7 @@ use std::collections::BTreeMap;
#[derive(Serialize, Deserialize, Default, Clone)]
pub struct EntryDeclaration {
/// The title of the entry which will be display in the boot menu.
/// This is the pre-stamped value.
pub title: String,
/// The actions to run when the entry is selected.
#[serde(default)]
@@ -16,3 +19,64 @@ pub struct EntryDeclaration {
#[serde(default)]
pub values: BTreeMap<String, String>,
}
/// Represents an entry that is stamped and ready to be booted.
#[derive(Clone)]
pub struct BootableEntry {
name: String,
title: String,
context: Rc<SproutContext>,
declaration: EntryDeclaration,
}
impl BootableEntry {
/// Create a new bootable entry to represent the full context of an entry.
pub fn new(
name: String,
title: String,
context: Rc<SproutContext>,
declaration: EntryDeclaration,
) -> Self {
Self {
name,
title,
context,
declaration,
}
}
/// Fetch the name of the entry. This is usually a machine-identifiable key.
pub fn name(&self) -> &str {
&self.name
}
/// Fetch the title of the entry. This is usually a human-readable key.
pub fn title(&self) -> &str {
&self.title
}
/// Fetch the full context of the entry.
pub fn context(&self) -> Rc<SproutContext> {
Rc::clone(&self.context)
}
/// Fetch the declaration of the entry.
pub fn declaration(&self) -> &EntryDeclaration {
&self.declaration
}
/// Swap out the context of the entry.
pub fn swap_context(&mut self, context: Rc<SproutContext>) {
self.context = context;
}
/// Restamp the title with the current context.
pub fn restamp_title(&mut self) {
self.title = self.context.stamp(&self.title);
}
/// Prepend the name of the entry with `prefix`.
pub fn prepend_name_prefix(&mut self, prefix: &str) {
self.name.insert_str(0, prefix);
}
}

View File

@@ -1,5 +1,5 @@
use crate::context::SproutContext;
use crate::entries::EntryDeclaration;
use crate::entries::BootableEntry;
use crate::generators::bls::BlsConfiguration;
use crate::generators::matrix::MatrixConfiguration;
use anyhow::Result;
@@ -40,7 +40,7 @@ pub struct GeneratorDeclaration {
pub fn generate(
context: Rc<SproutContext>,
generator: &GeneratorDeclaration,
) -> Result<Vec<(Rc<SproutContext>, EntryDeclaration)>> {
) -> Result<Vec<BootableEntry>> {
if let Some(matrix) = &generator.matrix {
matrix::generate(context, matrix)
} else if let Some(bls) = &generator.bls {

View File

@@ -1,5 +1,5 @@
use crate::context::SproutContext;
use crate::entries::EntryDeclaration;
use crate::entries::{BootableEntry, EntryDeclaration};
use crate::generators::bls::entry::BlsEntry;
use crate::utils;
use anyhow::{Context, Result};
@@ -33,12 +33,16 @@ fn default_bls_path() -> String {
BLS_TEMPLATE_PATH.to_string()
}
// TODO(azenla): remove this once variable substitution is implemented.
/// This function is used to remove the `tuned_initrd` variable from entry values.
/// Fedora uses tuned which adds an initrd that shouldn't be used.
fn quirk_initrd_remove_tuned(input: String) -> String {
input.replace("$tuned_initrd", "").trim().to_string()
}
/// Generates entries from the BLS entries directory using the specified `bls` configuration and
/// `context`. The BLS conversion is best-effort and will ignore any unsupported entries.
pub fn generate(
context: Rc<SproutContext>,
bls: &BlsConfiguration,
) -> Result<Vec<(Rc<SproutContext>, EntryDeclaration)>> {
pub fn generate(context: Rc<SproutContext>, bls: &BlsConfiguration) -> Result<Vec<BootableEntry>> {
let mut entries = Vec::new();
// Stamp the path to the BLS entries directory.
@@ -108,13 +112,26 @@ pub fn generate(
// Produce a new sprout context for the entry with the extracted values.
let mut context = context.fork();
context.set("title", entry.title().unwrap_or(name));
context.set("chainload", entry.chainload_path().unwrap_or_default());
context.set("options", entry.options().unwrap_or_default());
context.set("initrd", entry.initrd_path().unwrap_or_default());
let title = entry.title().unwrap_or_else(|| name.clone());
let chainload = entry.chainload_path().unwrap_or_default();
let options = entry.options().unwrap_or_default();
// Put the initrd through a quirk modifier to support Fedora.
let initrd = quirk_initrd_remove_tuned(entry.initrd_path().unwrap_or_default());
context.set("title", title);
context.set("chainload", chainload);
context.set("options", options);
context.set("initrd", initrd);
// Add the entry to the list with a frozen context.
entries.push((context.freeze(), bls.entry.clone()));
entries.push(BootableEntry::new(
name,
bls.entry.title.clone(),
context.freeze(),
bls.entry.clone(),
));
}
Ok(entries)

View File

@@ -1,5 +1,5 @@
use crate::context::SproutContext;
use crate::entries::EntryDeclaration;
use crate::entries::{BootableEntry, EntryDeclaration};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
@@ -54,13 +54,13 @@ fn build_matrix(input: &BTreeMap<String, Vec<String>>) -> Vec<BTreeMap<String, S
pub fn generate(
context: Rc<SproutContext>,
matrix: &MatrixConfiguration,
) -> Result<Vec<(Rc<SproutContext>, EntryDeclaration)>> {
) -> Result<Vec<BootableEntry>> {
// Produce all the combinations of the input values.
let combinations = build_matrix(&matrix.values);
let mut entries = Vec::new();
// For each combination, create a new context and entry.
for combination in combinations {
for (index, combination) in combinations.into_iter().enumerate() {
let mut context = context.fork();
// Insert the combination into the context.
context.insert(&combination);
@@ -68,14 +68,18 @@ pub fn generate(
// Stamp the entry title and actions from the template.
let mut entry = matrix.entry.clone();
entry.title = context.stamp(&entry.title);
entry.actions = entry
.actions
.into_iter()
.map(|action| context.stamp(action))
.collect();
// Push the entry into the list with the new context.
entries.push((context, entry));
entries.push(BootableEntry::new(
index.to_string(),
entry.title.clone(),
context,
entry,
));
}
Ok(entries)

View File

@@ -2,8 +2,11 @@
#![feature(uefi_std)]
use crate::context::{RootContext, SproutContext};
use crate::entries::BootableEntry;
use crate::options::SproutOptions;
use crate::options::parser::OptionsRepresentable;
use crate::phases::phase;
use anyhow::{Context, Result, bail};
use anyhow::{Context, Result};
use log::info;
use std::collections::BTreeMap;
use std::ops::Deref;
@@ -37,6 +40,9 @@ pub mod phases;
/// setup: Code that initializes the UEFI environment for Sprout.
pub mod setup;
/// options: Parse the options of the Sprout executable.
pub mod options;
/// utils: Utility functions that are used by other parts of Sprout.
pub mod utils;
@@ -47,10 +53,13 @@ fn main() -> Result<()> {
// Initialize the basic UEFI environment.
setup::init()?;
// Parse the options to the sprout executable.
let options = SproutOptions::parse().context("unable to parse options")?;
// Load the configuration of sprout.
// At this point, the configuration has been validated and the specified
// version is checked to ensure compatibility.
let config = config::loader::load()?;
let config = config::loader::load(&options)?;
// Load the root context.
// This is done in a block to ensure the release of the LoadedImageDevicePath protocol.
@@ -64,7 +73,7 @@ fn main() -> Result<()> {
"loaded image path: {}",
loaded_image_path.to_string(DisplayOnly(false), AllowShortcuts(false))?
);
RootContext::new(loaded_image_path)
RootContext::new(loaded_image_path, options)
};
// Insert the configuration actions into the root context.
@@ -101,58 +110,75 @@ fn main() -> Result<()> {
// Execute the late phase.
phase(context.clone(), &config.phases.startup).context("unable to execute startup phase")?;
let mut staged_entries = Vec::new();
let mut entries = Vec::new();
// Insert all the static entries from the configuration into the entry list.
for (_name, entry) in config.entries {
for (name, entry) in config.entries {
// Associate the main context with the static entry.
staged_entries.push((context.clone(), entry));
entries.push(BootableEntry::new(
name,
entry.title.clone(),
context.clone(),
entry,
));
}
// Run all the generators declared in the configuration.
for (_name, generator) in config.generators {
for (name, generator) in config.generators {
let context = context.fork().freeze();
// We will prefix all entries with [name]-.
let prefix = format!("{}-", name);
// Add all the entries generated by the generator to the entry list.
// The generator specifies the context associated with the entry.
for entry in generators::generate(context.clone(), &generator)? {
staged_entries.push(entry);
for mut entry in generators::generate(context.clone(), &generator)? {
entry.prepend_name_prefix(&prefix);
entries.push(entry);
}
}
// Build a list of all the final boot entries.
let mut final_entries = Vec::new();
for (context, entry) in staged_entries {
let mut context = context.fork();
for entry in &mut entries {
let mut context = entry.context().fork();
// Insert the values from the entry configuration into the
// sprout context to use with the entry itself.
context.insert(&entry.values);
context.insert(&entry.declaration().values);
let context = context.finalize().freeze();
// Insert the entry configuration into final boot entries with the extended context.
final_entries.push((context, entry));
// Provide the new context to the bootable entry.
entry.swap_context(context);
// Restamp the title with any values.
entry.restamp_title();
}
// TODO(azenla): Implement boot menu here.
// For now, we just print all of the entries.
info!("entries:");
for (index, (context, entry)) in final_entries.iter().enumerate() {
let title = context.stamp(&entry.title);
info!(" entry {}: {}", index + 1, title);
for (index, entry) in entries.iter().enumerate() {
let title = entry.context().stamp(&entry.declaration().title);
info!(" entry {} [{}]: {}", index, entry.name(), title);
}
// Execute the late phase.
phase(context.clone(), &config.phases.late).context("unable to execute late phase")?;
// Pick the first entry from the list of final entries until a boot menu is implemented.
let Some((context, entry)) = final_entries.first() else {
bail!("no entries found");
// Use the boot option if possible, otherwise pick the first entry.
let entry = if let Some(ref boot) = context.root().options().boot {
entries
.iter()
.enumerate()
.find(|(index, entry)| {
entry.name() == boot || entry.title() == boot || &index.to_string() == boot
})
.context(format!("unable to find entry: {boot}"))?
.1 // select the bootable entry.
} else {
entries.first().context("no entries found")?
};
// Execute all the actions for the selected entry.
for action in &entry.actions {
let action = context.stamp(action);
actions::execute(context.clone(), &action)
for action in &entry.declaration().actions {
let action = entry.context().stamp(action);
actions::execute(entry.context().clone(), &action)
.context(format!("unable to execute action '{}'", action))?;
}

84
src/options.rs Normal file
View File

@@ -0,0 +1,84 @@
use crate::options::parser::{OptionDescription, OptionForm, OptionsRepresentable};
use anyhow::{Context, Result, bail};
use std::collections::BTreeMap;
/// The Sprout options parser.
pub mod parser;
/// Default configuration file path.
const DEFAULT_CONFIG_PATH: &str = "\\sprout.toml";
/// The parsed options of sprout.
#[derive(Debug)]
pub struct SproutOptions {
/// Path to a configuration file to load.
pub config: String,
/// Entry to boot without showing the boot menu.
pub boot: Option<String>,
}
/// The default Sprout options.
impl Default for SproutOptions {
fn default() -> Self {
Self {
config: DEFAULT_CONFIG_PATH.to_string(),
boot: None,
}
}
}
/// The options parser mechanism for Sprout.
impl OptionsRepresentable for SproutOptions {
/// Produce the [SproutOptions] structure.
type Output = Self;
/// All the Sprout options that are defined.
fn options() -> &'static [(&'static str, OptionDescription<'static>)] {
&[
(
"config",
OptionDescription {
description: "Path to Sprout configuration file",
form: OptionForm::Value,
},
),
(
"boot",
OptionDescription {
description: "Entry to boot, bypassing the menu",
form: OptionForm::Value,
},
),
(
"help",
OptionDescription {
description: "Display Sprout Help",
form: OptionForm::Help,
},
),
]
}
/// Produces [SproutOptions] from the parsed raw `options` map.
fn produce(options: BTreeMap<String, Option<String>>) -> Result<Self> {
// Use the default value of sprout options and have the raw options be parsed into it.
let mut result = Self::default();
for (key, value) in options {
match key.as_str() {
"config" => {
// The configuration file to load.
result.config = value.context("--config option requires a value")?;
}
"boot" => {
// The entry to boot.
result.boot = Some(value.context("--boot option requires a value")?);
}
_ => bail!("unknown option: --{key}"),
}
}
Ok(result)
}
}

150
src/options/parser.rs Normal file
View File

@@ -0,0 +1,150 @@
use anyhow::{Context, Result, bail};
use std::collections::BTreeMap;
/// The type of option. This disambiguates different behavior
/// of how options are handled.
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum OptionForm {
/// A flag, like --verbose.
Flag,
/// A value, in the form --abc 123 or --abc=123.
Value,
/// Help flag, like --help.
Help,
}
/// The description of an option, used in the options parser
/// to make decisions about how to progress.
#[derive(Debug, Clone)]
pub struct OptionDescription<'a> {
/// The description of the option.
pub description: &'a str,
/// The type of option to parse as.
pub form: OptionForm,
}
/// Represents a type that can be parsed from command line arguments.
/// This is a super minimal options parser mechanism just for Sprout.
pub trait OptionsRepresentable {
/// The output type that parsing will produce.
type Output;
/// The configured options for this type. This should describe all the options
/// that are valid to produce the type. The left hand side is the name of the option,
/// and the right hand side is the description.
fn options() -> &'static [(&'static str, OptionDescription<'static>)];
/// Produces the type by taking the `options` and processing it into the output.
fn produce(options: BTreeMap<String, Option<String>>) -> Result<Self::Output>;
/// For minimalism, we don't want a full argument parser. Instead, we use
/// a simple --xyz = xyz: None and --abc 123 = abc: Some("123") format.
/// We also support --abc=123 = abc: Some("123") format.
fn parse_raw() -> Result<BTreeMap<String, Option<String>>> {
// Access the configured options for this type.
let configured: BTreeMap<_, _> = BTreeMap::from_iter(Self::options().to_vec());
// Collect all the arguments to Sprout.
// Skip the first argument which is the path to our executable.
let args = std::env::args().skip(1).collect::<Vec<_>>();
// Represent options as key-value pairs.
let mut options = BTreeMap::new();
// Iterators makes this way easier.
let mut iterator = args.into_iter().peekable();
loop {
// Consume the next option, if any.
let Some(option) = iterator.next() else {
break;
};
// If the doesn't start with --, that is invalid.
if !option.starts_with("--") {
bail!("invalid option: {option}");
}
// Strip the -- prefix off.
let mut option = option["--".len()..].trim().to_string();
// An optional value.
let mut value = None;
// Check if the option is of the form --abc=123
if option.contains("=") {
let Some((part_key, part_value)) = option.split_once("=") else {
bail!("invalid option: {option}");
};
let part_key = part_key.to_string();
let part_value = part_value.to_string();
option = part_key;
value = Some(part_value);
}
// Error on empty option names.
if option.is_empty() {
bail!("invalid empty option");
}
// Find the description of the configured option, if any.
let Some(description) = configured.get(option.as_str()) else {
bail!("invalid option: --{option}");
};
// Check if the option requires a value and error if none was provided.
if description.form == OptionForm::Value && value.is_none() {
// Check for the next value.
let maybe_next = iterator.peek();
// If the next value isn't another option, set the value to the next value.
// Otherwise, it is an empty string.
value = if let Some(next) = maybe_next
&& !next.starts_with("--")
{
iterator.next()
} else {
None
};
}
// If the option form does not support a value and there is a value, error.
if description.form != OptionForm::Value && value.is_some() {
bail!("option --{} does not take a value", option);
}
// Handle the --help flag case.
if description.form == OptionForm::Help {
// Generic configured options output.
println!("Configured Options:");
for (name, description) in &configured {
println!(
" --{}{}: {}",
name,
if description.form == OptionForm::Value {
" <value>"
} else {
""
},
description.description
);
}
// Exit because the help has been displayed.
std::process::exit(1);
}
// Insert the option and the value into the map.
options.insert(option, value);
}
Ok(options)
}
/// Parses the program arguments as a [Self::Output], calling [Self::parse_raw] and [Self::produce].
fn parse() -> Result<Self::Output> {
// Parse the program arguments into a raw map.
let options = Self::parse_raw().context("unable to parse options")?;
// Produce the options from the map.
Self::produce(options)
}
}

View File

@@ -12,7 +12,7 @@ pub mod framebuffer;
/// Support code for the media loader protocol.
pub mod media_loader;
/// Parses the input [path] as a [DevicePath].
/// Parses the input `path` as a [DevicePath].
/// Uses the [DevicePathFromText] protocol exclusively, and will fail if it cannot acquire the protocol.
pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
let path = CString16::try_from(path).context("unable to convert path to CString16")?;
@@ -27,7 +27,7 @@ pub fn text_to_device_path(path: &str) -> Result<PoolDevicePath> {
.context("unable to convert text to device path")
}
/// Grabs the root part of the [path].
/// Grabs the root part of the `path`.
/// For example, given "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\EFI\BOOT\BOOTX64.efi"
/// it will give "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)"
pub fn device_path_root(path: &DevicePath) -> Result<String> {
@@ -52,7 +52,7 @@ pub fn device_path_root(path: &DevicePath) -> Result<String> {
Ok(path)
}
/// Grabs the part of the [path] after the root.
/// Grabs the part of the `path` after the root.
/// For example, given "PciRoot(0x0)/Pci(0x4,0x0)/NVMe(0x1,00-00-00-00-00-00-00-00)/HD(1,MBR,0xBE1AFDFA,0x3F,0xFBFC1)/\EFI\BOOT\BOOTX64.efi"
/// it will give "\EFI\BOOT\BOOTX64.efi"
pub fn device_path_subpath(path: &DevicePath) -> Result<String> {
@@ -92,8 +92,8 @@ pub struct ResolvedPath {
pub filesystem_handle: Handle,
}
/// Resolve a path specified by [input] to its various components.
/// Uses [default_root_path] as the base root if one is not specified in the path.
/// Resolve a path specified by `input` to its various components.
/// Uses `default_root_path` as the base root if one is not specified in the path.
/// Returns [ResolvedPath] which contains the resolved components.
pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<ResolvedPath> {
let mut path = text_to_device_path(input).context("unable to convert text to path")?;
@@ -137,9 +137,9 @@ pub fn resolve_path(default_root_path: &DevicePath, input: &str) -> Result<Resol
})
}
/// Read the contents of a file at the location specified with the [input] path.
/// Read the contents of a file at the location specified with the `input` path.
/// Internally, this uses [resolve_path] to resolve the path to its various components.
/// [resolve_path] is passed the [default_root_path] which should specify a base root.
/// [resolve_path] is passed the `default_root_path` which should specify a base root.
///
/// This acquires exclusive protocol access to the [SimpleFileSystem] protocol of the resolved
/// filesystem handle, so care must be taken to call this function outside a scope with

View File

@@ -46,8 +46,8 @@ pub struct MediaLoaderHandle {
impl MediaLoaderHandle {
/// The behavior of this function is derived from how Linux calls it.
///
/// Linux calls this function by first passing a NULL [buffer].
/// We must set the size of the buffer it should allocate in [buffer_size].
/// Linux calls this function by first passing a NULL `buffer`.
/// We must set the size of the buffer it should allocate in `buffer_size`.
/// The next call will pass a buffer of the right size, and we should copy
/// data into that buffer, checking whether it is safe to copy based on
/// the buffer size.
@@ -137,7 +137,7 @@ impl MediaLoaderHandle {
Ok(false)
}
/// Registers the provided [data] with the UEFI stack as media loader.
/// Registers the provided `data` with the UEFI stack as media loader.
/// This uses a special device path that other EFI programs will look at
/// to load the data from.
pub fn register(guid: Guid, data: Box<[u8]>) -> Result<MediaLoaderHandle> {