From 52cbb1e756f84a889887fcceeccad3526c318b41 Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Thu, 25 Apr 2019 13:17:40 +0200 Subject: [PATCH] Set up a cargo runner to make `cargo xrun` work --- .../posts/02-minimal-rust-kernel/index.md | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md b/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md index 91ff5624..e4b358dd 100644 --- a/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md +++ b/blog/content/second-edition/posts/02-minimal-rust-kernel/index.md @@ -431,5 +431,27 @@ It is also possible to write it to an USB stick and boot it on a real machine: Where `sdX` is the device name of your USB stick. **Be careful** to choose the correct device name, because everything on that device is overwritten. +After writing the image to the USB stick, you can run it on real hardware by booting from it. You probably need to use a special boot menu or change the boot order in your BIOS configuration to boot from the USB stick. Note that it currently doesn't work for UEFI machines, since the `bootloader` crate has no UEFI support yet. + +### Using `cargo run` + +To make it easier to run our kernel in QEMU, we can set the `runner` configuration key for cargo: + +```toml +# in .cargo/config + +[target.'cfg(target_os = "none")'] +runner = "bootimage runner" +``` + +The `target.'cfg(target_os = "none")'` table applies to all targets that have set the `"os"` field of their target configuration file to `"none"`. This includes our `x86_64-blog_os.json` target. The `runner` key specifies the command that should be invoked for `cargo run`. The command is run after a successful build with the executable path passed as first argument. See the [cargo documentation][cargo configuration] for more details. + +The `bootimage runner` command is specifically designed to be usable as a `runner` executable. It links the given executable with the project's bootloader dependency and then launches QEMU. See the [Readme of `bootimage`] for more details and possible configuration options. + +[Readme of `bootimage`]: https://github.com/rust-osdev/bootimage + +Now we can use `cargo xrun` to compile our kernel and boot it in QEMU. Like `xbuild`, the `xrun` subcommand builds the sysroot crates before invoking the actual cargo command. The subcommand is also provided by `cargo-xbuild`, so you don't need to install an additional tool. + ## What's next? + In the next post, we will explore the VGA text buffer in more detail and write a safe interface for it. We will also add support for the `println` macro.