Skip to content

Commit 454f707

Browse files
committed
Update usage instructions and architecture description in README
1 parent 667e57f commit 454f707

File tree

1 file changed

+47
-51
lines changed

1 file changed

+47
-51
lines changed

README.md

+47-51
Original file line numberDiff line numberDiff line change
@@ -12,60 +12,56 @@ You need a nightly [Rust](https://www.rust-lang.org) compiler with the `llvm-too
1212

1313
## Usage
1414

15-
See our [documentation](https://docs.rs/bootloader). Note that the `bootimage` crate is no longer used since version 0.10.0.
15+
To make your kernel compatible with `bootloader`:
16+
17+
- Add a dependency on the `bootloader_api` crate in your kernel's `Cargo.toml`.
18+
- Your kernel binary should be `#![no_std]` and `#![no_main]`.
19+
- Define an entry point function with the signature `fn kernel_main(boot_info: &'static mut bootloader_api::BootInfo) -> !`. The function name can be arbitrary.
20+
- The `boot_info` argument provides information about available memory, the framebuffer, and more. See the API docs for `bootloader_api` crate for details.
21+
- Use the `entry_point` macro to register the entry point function: `bootloader_api::entry_point!(kernel_main);`
22+
- The macro checks the signature of your entry point function and generates a `_start` entry point symbol for it. (If you use a linker script, make sure that you don't change the entry point name to something else.)
23+
- To use non-standard configuration, you can pass a second argument of type `&'static bootloader_api::BootloaderConfig` to the `entry_point` macro. For example, you can require a specific stack size for your kernel:
24+
```rust
25+
const CONFIG: bootloader_api::BootloaderConfig = {
26+
let mut config = bootloader_api::BootloaderConfig::new_default();
27+
config.kernel_stack_size = 100 * 1024; // 100 KiB
28+
config
29+
};
30+
bootloader_api::entry_point!(kernel_main, config = &CONFIG);
31+
```
32+
- Compile your kernel as normal to an ELF executable. The executable will contain a special section with metadata and the serialized config, which will enable the `bootloader` crate to load it.
33+
34+
To combine your kernel with a bootloader and create a bootable disk image, follow these steps:
35+
36+
- Create a new runner crate, e.g. through `cargo new runner --bin`.
37+
- Add the `bootloader` crate as a `dependency` in the `runner/Cargo.toml`.
38+
- In the `main.rs`, invoke the build commands for your kernel.
39+
- Alternatively, you can set up an [artifact dependency](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) on your kernel, provided that you use a `rustup`-supported target for your kernel:
40+
```toml
41+
[dependencies]
42+
my-kernel = { path = "..", artifact = "bin", target = "x86_64-unknown-none" }
43+
```
44+
- After building your kernel, obtain the path to the kernel executable.
45+
- When using an artifact dependency, you can retrieve this path using `env!("CARGO_BIN_FILE_MY_KERNEL_my-kernel")`
46+
- Use the `bootloader::create_boot_partition` function to create a bootable FAT partition at some chosen path.
47+
- Use one or multiple `bootloader::create_*_disk_image` functions to transform the bootable FAT partition into a disk image.
48+
- Use the `bootloader::create_uefi_disk_image` function to create an UEFI-compatible GPT-formatted disk image.
49+
- Use the `bootloader::create_bios_disk_image` function to create a BIOS-compatible MBR-formatted disk image.
1650

1751
## Architecture
1852

19-
This project consists of three separate entities:
20-
21-
- A library with the entry point and boot info definitions that kernels can include as a normal cargo dependency.
22-
- BIOS and UEFI binaries that contain the actual bootloader implementation.
23-
- A `builder` binary to simplify the build process of the BIOS and UEFI binaries.
24-
25-
These three entities are currently all combined in a single crate using cargo feature flags. The reason for this is that the kernel and bootloader must use the exact same version of the `BootInfo` struct to prevent undefined behavior (we did not stabilize the boot info format yet, so it might change between versions).
26-
27-
### Build and Boot
28-
29-
The build and boot process works the following way:
30-
31-
- The `builder` binary is a small command line tool that takes the path to the kernel manifest and binary as arguments. Optionally, it allows to override the cargo target and output dirs. It also accepts a `--quiet` switch and allows to only build the BIOS or UEFI binary instead of both.
32-
- After parsing the arguments, the `builder` binary invokes the actual build command for the BIOS/UEFI binaries, which includes the correct `--target` and `--features` arguments (and `-Zbuild-std`). The kernel manifest and binary paths are passed as `KERNEL_MANIFEST` and `KERNEL` environment variables.
33-
- The next step in the build process is the `build.rs` build script. It only does something when building the BIOS/UEFI binaries (indicated by the `binary` feature), otherwise it is a no-op.
34-
- The script first runs some sanity checks, e.g. the kernel manifest and binary should be specified in env variables and should exist, the correct target triple should be used, and the `llvm-tools` rustup component should be installed.
35-
- Then it copies the kernel executable and strips the debug symbols from it to make it smaller. This does not affect the original kernel binary. The stripped binary is then converted to a byte array and provided to the BIOS/UEFI binaries, either as a Rust `static` or through a linker argument.
36-
- Next, the bootloader configuration is parsed, which can be specified in a `package.metadata.bootloader` table in the kernel manifest file. This requires some custom string parsing since TOML does not support unsigned 64-bit integers. Parse errors are turned into `compile_error!` calls to give nicer error messages.
37-
- After parsing the configuration, it is written as a Rust struct definition into a new `bootloader_config.rs` file in the cargo `OUT_DIR`. This file is then included by the UEFI/BIOS binaries.
38-
- After the build script, the compilation continues with either the `bin/uefi.rs` or the `bin/bios.rs`:
39-
- The `bin/uefi.rs` specifies an UEFI entry point function called `efi_main`. It uses the [`uefi`](https://docs.rs/uefi/0.8.0/uefi/) crate to set up a pixel-based framebuffer using the UEFI GOP protocol. Then it exits the UEFI boot services and stores the physical memory map. The final step is to create some page table abstractions and call into `load_and_switch_to_kernel` function that is shared with the BIOS boot code.
40-
- The `bin/bios.rs` function does not provide a direct entry point. Instead it includes several assembly files (`asm/stage-*.rs`) that implement the CPU initialization (from real mode to long mode), the framebuffer setup (via VESA), and the memory map creation (via a BIOS call). The assembly stages are explained in more detail below. After the assembly stages, the execution jumps to the `bootloader_main` function in `bios.rs`. There we set up some additional identity mapping, translate the memory map and framebuffer into Rust structs, detect the RSDP table, and create some page table abstractions. Then we call into the `load_and_switch_to_kernel` function like the `bin/uefi.rs`.
41-
- The common `load_and_switch_to_kernel` function is defined in `src/binary/mod.rs`. This is also the file that includes the `bootloader_config.rs` generated by the build script. The `load_and_switch_to_kernel` functions performs the following steps:
42-
- Parse the kernel binary and map it in a new page table. This includes setting up the correct permissions for each page, initializing `.bss` sections, and allocating a stack with guard page. The relevant functions for these steps are `set_up_mappings` and `load_kernel`.
43-
- Create the `BootInfo` struct, which abstracts over the differences between BIOS and UEFI booting. This step is implemented in the `create_boot_info` function.
44-
- Do a context switch and jump to the kernel entry point function. This involves identity-mapping the context switch function itself in both the kernel and bootloader page tables to prevent a page fault after switching page tables. This switch step is implemented in the `switch_to_kernel` and `context_switch` functions.
45-
- As a last step after a successful build, the `builder` binary turns the compiled bootloader executable (includes the kernel) into a bootable disk image. For UEFI, this means that a FAT partition and a GPT disk image are created. For BIOS, the `llvm-objcopy` tool is used to convert the `bootloader` executable to a flat binary, as it already contains a basic MBR.
46-
47-
### BIOS Assembly Stages
48-
49-
When you press the power button the computer loads the BIOS from some flash memory stored on the motherboard. The BIOS initializes and self tests the hardware then loads the first 512 bytes into memory from the media device (i.e. the cdrom or floppy disk). If the last two bytes equal 0xAA55 then the BIOS will jump to location 0x7C00 effectively transferring control to the bootloader.
50-
51-
At this point the CPU is running in 16 bit mode, meaning only the 16 bit registers are available. Also since the BIOS only loads the first 512 bytes this means our bootloader code has to stay below that limit, otherwise we’ll hit uninitialised memory! Using [Bios interrupt calls](https://en.wikipedia.org/wiki/BIOS_interrupt_call) the bootloader prints debug information to the screen.
52-
53-
For more information on how to write a bootloader click [here](http://3zanders.co.uk/2017/10/13/writing-a-bootloader/). The assembler files get imported through the [global_asm feature](https://doc.rust-lang.org/unstable-book/library-features/global-asm.html). The assembler syntax definition used is the one llvm uses: [GNU Assembly](http://microelectronics.esa.int/erc32/doc/as.pdf).
54-
55-
The purposes of the individual assembly stages in this project are the following:
56-
57-
- stage_1.s: This stage initializes the stack, enables the A20 line, loads the rest of the bootloader from disk, and jumps to stage_2.
58-
- stage_2.s: This stage sets the target operating mode, loads the kernel from disk,creates an e820 memory map, enters protected mode, and jumps to the third stage.
59-
- stage_3.s: This stage performs some checks on the CPU (cpuid, long mode), sets up an initial page table mapping (identity map the bootloader, map the P4 recursively, map the kernel blob to 4MB), enables paging, switches to long mode, and jumps to stage_4.
60-
61-
## Future Plans
62-
63-
- [ ] Create a `multiboot2` compatible disk image in addition to the BIOS and UEFI disk images. This would make it possible to use it on top of the GRUB bootloader.
64-
- [ ] Rewrite most of the BIOS assembly stages in Rust. This has already started.
65-
- [ ] Instead of linking the kernel bytes directly with the bootloader, use a filesystem (e.g. FAT) and load the kernel as a separate file.
66-
- [ ] Stabilize the boot info format and make it possible to check the version at runtime.
67-
- [ ] Instead of searching the bootloader source in the cargo cache on building, use the upcoming ["artifact dependencies"](https://github.com/rust-lang/cargo/issues/9096) feature of cargo to download the builder binary separately. Requires doing a boot info version check on build time.
68-
- [ ] Transform this "Future Plans" list into issues and a roadmap.
53+
This project is split into three separate entities:
54+
55+
- A [`bootloader_api`](./api) library with the entry point, configuration, and boot info definitions.
56+
- Kernels should include this library as a normal cargo dependency.
57+
- The provided `entry_point` macro will encode the configuration settings into a separate ELF section of the compiled kernel executable.
58+
- [BIOS](./bios) and [UEFI](./uefi) binaries that contain the actual bootloader implementation.
59+
- The implementations share a higher-level [common library](./common).
60+
- Both implementations load the kernel at runtime from a FAT partition. This FAT partition is created
61+
- The configuration is read from a special section of the kernel's ELF file, which is created by the `entry_point` macro of teh `bootloader_api` library.
62+
- A `bootloader` library to create bootable disk images that run a given kernel. This library is the top-level crate in this project.
63+
- The library builds the BIOS and UEFI implementations in the [`build.rs`](./build.rs).
64+
- It provides functions to create FAT-formatted bootable disk images, based on the compiled BIOS and UEFI bootloaders.
6965

7066
## License
7167

0 commit comments

Comments
 (0)