You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+56-28Lines changed: 56 additions & 28 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,12 +14,21 @@ You need a nightly [Rust](https://www.rust-lang.org) compiler with the `llvm-too
14
14
15
15
To use this crate, you need to adjust your kernel to be bootable first. Then you can create a bootable disk image from your compiled kernel. These steps are explained in detail below.
16
16
17
+
### Migrating from older bootloader version
18
+
17
19
If you're already using an older version of the `bootloader` crate, follow our [migration guides](docs/migration).
18
20
19
-
### Kernel
21
+
### Starting from scratch
22
+
23
+
Our [basic example](examples/basic/basic-os.md) showcases an OS that boots a minimal kernel using `bootloader`.
24
+
25
+
### Using an existing kernel
20
26
21
-
To make your kernel compatible with `bootloader`:
27
+
To combine your kernel with `bootloader` and create a bootable disk image, follow these steps:
22
28
29
+
#### Make your kernel compatible with `bootloader`
30
+
31
+
- Move your full kernel code into a `kernel` subdirectory.
23
32
- Add a dependency on the `bootloader_api` crate in your kernel's `Cargo.toml`.
24
33
- Your kernel binary should be `#![no_std]` and `#![no_main]`.
25
34
- Define an entry point function with the signature `fn kernel_main(boot_info: &'static mut bootloader_api::BootInfo) -> !`. The function name can be arbitrary.
@@ -35,37 +44,56 @@ To make your kernel compatible with `bootloader`:
- Compile your kernel to an ELF executable by running **`cargo build --target x86_64-unknown-none`**. You might need to run `rustup target add x86_64-unknown-none` before to download precompiled versions of the `core` and `alloc` crates.
47
+
- Compile your kernel to an ELF executable by running **`cargo build --target x86_64-unknown-none`**. You might need to run `rustup target add x86_64-unknown-none` before to download precompiled versions of the `core` and `alloc` crates. You can add `x86_64-unknown-none` as default target and add it to your toolchain so that `cargo build` takes care of this.
48
+
```toml
49
+
# .cargo/config.toml
50
+
[build]
51
+
target = "x86_64-unknown-none"
52
+
```
53
+
```sh
54
+
$ cargo build
55
+
```
39
56
- Thanks to the `entry_point` macro, the compiled executable contains a special section with metadata and the serialized config, which will enable the `bootloader` crate to load it.
40
57
41
-
### Booting
42
-
43
-
To combine your kernel with a bootloader and create a bootable disk image, follow these steps:
58
+
#### Setup the workspace
44
59
45
-
- Move your full kernel code into a `kernel` subdirectory.
46
60
- Create a new `os` crate at the top level that defines a [workspace](https://doc.rust-lang.org/cargo/reference/workspaces.html).
47
-
- Add a `build-dependencies` on the `bootloader` crate.
48
-
- Create a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) build script.
49
-
- Set up an [artifact dependency](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) to add your `kernel` crate as a `build-dependency`:
Alternatively, you can use [`std::process::Command`](https://doc.rust-lang.org/stable/std/process/struct.Command.html) to invoke the build command of your kernel in the `build.rs` script.
64
-
- Obtain the path to the kernel executable. When using an artifact dependency, you can retrieve this path using `std::env::var_os("CARGO_BIN_FILE_MY_KERNEL_my-kernel")`
65
-
- Use `bootloader::UefiBoot` and/or `bootloader::BiosBoot` to create a bootable disk image with your kernel.
61
+
```sh
62
+
$ cargo new os --bin
63
+
```
64
+
- Add your kernel as a workspace member
65
+
```toml
66
+
# in Cargo.toml
67
+
[workspace]
68
+
resolver = "3"
69
+
members = ["kernel"]
70
+
```
71
+
- Enable the workspace to build your kernel:
72
+
- Set up an [artifact dependency](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) to add your `kernel` crate as a `build-dependency`:
Enable the unstable artifact-dependencies feature:
79
+
```toml
80
+
# .cargo/config.toml
81
+
[unstable]
82
+
bindeps = true
83
+
```
84
+
Experimental features are only available on the nightly channel:
85
+
```toml
86
+
# rust-toolchain.toml
87
+
[toolchain]
88
+
channel = "nightly"
89
+
targets = ["x86_64-unknown-none"]
90
+
```
91
+
- Alternatively, you can use [`std::process::Command`](https://doc.rust-lang.org/stable/std/process/struct.Command.html) to invoke the build command of your kernel in the `build.rs` script.
92
+
- Create a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) build script in the `os` crate. See our [disk image creation template](docs/create-disk-image.md) for a more detailed example.
93
+
- Obtain the path to the kernel executable. When using an artifact dependency, you can retrieve this path using `std::env::var_os("CARGO_BIN_FILE_MY_KERNEL_my-kernel")`
94
+
- Use `bootloader::UefiBoot` and/or `bootloader::BiosBoot` to create a bootable disk image with your kernel.
66
95
- Do something with the bootable disk images in your `main.rs` function. For example, run them with QEMU.
67
-
68
-
See our [disk image creation template](docs/create-disk-image.md) for a more detailed example.
Copy file name to clipboardExpand all lines: docs/create-disk-image.md
+15-80Lines changed: 15 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,83 +5,18 @@ The [`bootloader`](https://docs.rs/bootloader/0.11) crate provides simple functi
5
5
A good way to implement this is to move your kernel into a `kernel` subdirectory. Then you can create
6
6
a new `os` crate at the top level that defines a [workspace](https://doc.rust-lang.org/cargo/reference/workspaces.html). The root package has build-dependencies on the `kernel` [artifact](https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies) and on the bootloader crate. This allows you to create the bootable disk image in a [cargo build script](https://doc.rust-lang.org/cargo/reference/build-scripts.html) and launch the created image in QEMU in the `main` function.
7
7
8
-
The files could look like this:
9
-
10
-
```toml
11
-
# .cargo/config.toml
12
-
13
-
[unstable]
14
-
# enable the unstable artifact-dependencies feature, see
Now you should be able to use `cargo build` to create a bootable disk image and `cargo run` to run in QEMU. Your kernel is automatically recompiled when it changes. For more advanced usage, you can add command-line arguments to your `main.rs` to e.g. pass additional arguments to QEMU or to copy the disk images to some path to make it easier to find them (e.g. for copying them to an thumb drive).
8
+
Our [basic example](examples/basic/basic-os.md) showcases this setup:
Now you should be able to use `cargo build` to create a bootable disk image and `cargo run bios` and `cargo run uefi` to run it in QEMU. Your kernel is automatically recompiled when it changes. For more advanced usage, you can add command-line arguments to your `main.rs` to e.g. pass additional arguments to QEMU or to copy the disk images to some path to make it easier to find them (e.g. for copying them to an thumb drive).
0 commit comments