garage44.eu/jeroen/pinnacle

logclone: git clone https://garage44.eu/jeroen/pinnacle.git

.. /boot/src/main.rs

raw

//! Pinnacle's loader: the UEFI program a firmware starts from the boot
//! partition (`EFI/BOOT/BOOTAA64.EFI` on ARM, `BOOTX64.EFI` on a PC; U-Boot
//! starts it as UEFI does). It reads the kernel and its command line from
//! the same partition (`PINNACLE/KERNEL`, `PINNACLE/CMDLINE`), leaves the
//! firmware's boot services and starts the kernel as the kernel expects to
//! be started:
//!
//! - **ARM**: as a boot image, MMU and caches off, the device tree in x0,
//!   with the command line put in its `/chosen/bootargs` (`fdt`).
//! - **PC**: by Multiboot2, as GRUB did: in 32-bit protected mode, paging
//!   off, with the command line, the memory map, the screen UEFI left on
//!   and ACPI's root pointer in the information EBX points to (`pc`).
//!
//! Written here with no outside code, so a PC needs no GRUB; nothing of
//! the firmware is used once the kernel runs.

#![no_std]
#![no_main]

// What UEFI defines, for both architectures: each uses part of it.
#[cfg(target_arch = "aarch64")]
mod arm;
#[allow(dead_code)]
mod efi;
#[cfg(target_arch = "aarch64")]
mod fdt;
#[cfg(target_arch = "x86_64")]
mod pc;

use efi::{Firmware, Handle, SystemTable};

/// What the command line may be at most.
pub const COMMAND_LINE: usize = 1024;

#[unsafe(no_mangle)]
pub extern "efiapi" fn efi_main(image: Handle, system: *mut SystemTable) -> efi::Status {
    let firmware = Firmware { image, system };
    firmware.print("Pinnacle: loading\n");
    // The firmware's watchdog off: it resets a machine whose loader takes
    // more than five minutes, which reading a large kernel off a slow stick
    // may.
    (firmware.boot().set_watchdog_timer)(0, 0, 0, core::ptr::null());
    let mut line = [0u8; COMMAND_LINE];
    let length = command_line(&firmware, &mut line);
    let line = core::str::from_utf8(&line[..length]).unwrap_or("");
    let why = start(&firmware, line);
    firmware.print("Pinnacle: ");
    firmware.print(why);
    firmware.print("\n");
    // Back to the firmware, which tries the next way to boot.
    1 << (usize::BITS - 1) | 14
}

/// The command line from `PINNACLE/CMDLINE`, one line, into `into`.
fn command_line(firmware: &Firmware, into: &mut [u8]) -> usize {
    let Some((at, size)) = firmware.read_file("\\PINNACLE\\CMDLINE", None) else {
        return 0;
    };
    // SAFETY: the pages the file was read into.
    let text = unsafe { core::slice::from_raw_parts(at as *const u8, size) };
    let line = text.split(|&b| b == b'\n' || b == b'\r').next().unwrap_or(&[]);
    let n = line.len().min(into.len());
    into[..n].copy_from_slice(&line[..n]);
    firmware.free(at, size.div_ceil(4096).max(1));
    n
}

/// Starts the kernel; returns only why it could not.
fn start(firmware: &Firmware, line: &str) -> &'static str {
    #[cfg(target_arch = "aarch64")]
    return arm::start(firmware, line);
    #[cfg(target_arch = "x86_64")]
    return pc::start(firmware, line);
}

#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
    loop {
        core::hint::spin_loop();
    }
}