-
Notifications
You must be signed in to change notification settings - Fork 222
Add basic example #519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
peppergrayxyz
wants to merge
1
commit into
rust-osdev:main
Choose a base branch
from
peppergrayxyz:basic_example
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add basic example #519
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,11 +14,19 @@ You need a nightly [Rust](https://www.rust-lang.org) compiler with the `llvm-too | |
|
|
||
| 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. | ||
|
|
||
| ### Migrating from older bootloader version | ||
|
|
||
| If you're already using an older version of the `bootloader` crate, follow our [migration guides](docs/migration). | ||
|
|
||
| ### Kernel | ||
| ### Starting from scratch | ||
|
|
||
| Our [basic example](examples/basic/basic-os.md) showcases an OS that boots a minimal kernel using `bootloader`. | ||
|
|
||
| ### Using an existing kernel | ||
|
|
||
| To make your kernel compatible with `bootloader`: | ||
| To combine your kernel with `bootloader` and create a bootable disk image, follow these steps: | ||
|
|
||
| #### Make your kernel compatible with `bootloader` | ||
|
|
||
| - Add a dependency on the `bootloader_api` crate in your kernel's `Cargo.toml`. | ||
| - Your kernel binary should be `#![no_std]` and `#![no_main]`. | ||
|
|
@@ -35,34 +43,55 @@ To make your kernel compatible with `bootloader`: | |
| }; | ||
| bootloader_api::entry_point!(kernel_main, config = &CONFIG); | ||
| ``` | ||
| - 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. | ||
| - 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. | ||
| ```toml | ||
| # .cargo/config.toml | ||
| [build] | ||
| target = "x86_64-unknown-none" | ||
| ``` | ||
| ```sh | ||
| $ cargo build | ||
| ``` | ||
| - 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. | ||
|
|
||
| ### Booting | ||
|
|
||
| To combine your kernel with a bootloader and create a bootable disk image, follow these steps: | ||
| #### Creating a bootable image | ||
|
|
||
| - Move your full kernel code into a `kernel` subdirectory. | ||
| - Create a new `os` crate at the top level that defines a [workspace](https://doc.rust-lang.org/cargo/reference/workspaces.html). | ||
| - Add a `build-dependencies` on the `bootloader` crate. | ||
| - Create a [`build.rs`](https://doc.rust-lang.org/cargo/reference/build-scripts.html) build script. | ||
| - 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`: | ||
| ```toml | ||
| # in Cargo.toml | ||
| [build-dependencies] | ||
| kernel = { path = "kernel", artifact = "bin", target = "x86_64-unknown-none" } | ||
| ``` | ||
| ```toml | ||
| # .cargo/config.toml | ||
|
|
||
| [unstable] | ||
| # enable the unstable artifact-dependencies feature, see | ||
| # https://doc.rust-lang.org/nightly/cargo/reference/unstable.html#artifact-dependencies | ||
| bindeps = true | ||
| ``` | ||
| 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. | ||
| - 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")` | ||
| - Use `bootloader::UefiBoot` and/or `bootloader::BiosBoot` to create a bootable disk image with your kernel. | ||
| - Create a new `os` crate at the top level | ||
| ```sh | ||
| $ cargo init --bin | ||
| ``` | ||
| - Define a [workspace](https://doc.rust-lang.org/cargo/reference/workspaces.html) and add your kernel as a workspace member. | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we're specifying the command used to create the top-level crate ( |
||
| ```toml | ||
| # in Cargo.toml | ||
| [workspace] | ||
| resolver = "3" | ||
| members = ["kernel"] | ||
| ``` | ||
| - Enable the workspace to build your kernel: | ||
| - 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`: | ||
| ```toml | ||
| # in Cargo.toml | ||
| [build-dependencies] | ||
| kernel = { path = "kernel", artifact = "bin", target = "x86_64-unknown-none" } | ||
| ``` | ||
| Enable the unstable artifact-dependencies feature: | ||
| ```toml | ||
| # .cargo/config.toml | ||
| [unstable] | ||
| bindeps = true | ||
| ``` | ||
| Experimental features are only available on the nightly channel: | ||
| ```toml | ||
| # rust-toolchain.toml | ||
| [toolchain] | ||
| channel = "nightly" | ||
| targets = ["x86_64-unknown-none"] | ||
| ``` | ||
| - 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. | ||
| - 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. | ||
| - 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")` | ||
| - Use `bootloader::UefiBoot` and/or `bootloader::BiosBoot` to create a bootable disk image with your kernel. | ||
| - Do something with the bootable disk images in your `main.rs` function. For example, run them with QEMU. | ||
|
|
||
| See our [disk image creation template](docs/create-disk-image.md) for a more detailed example. | ||
peppergrayxyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| [unstable] | ||
| bindeps = true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| /target/ | ||
| **/*.rs.bk |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's also remove this.