Skip to content

Commit 12645da

Browse files
bors[bot]phil-opp
andcommitted
Merge #58
58: Allow overriding the physical memory offset through an environment variable r=phil-opp a=phil-opp The plan is that the `bootimage` crate gets an additional `Cargo.toml` config key for this property and sets the environment variable automatically. cc @64 Co-authored-by: Philipp Oppermann <[email protected]>
2 parents f48ec03 + a9b8d89 commit 12645da

File tree

4 files changed

+37
-3
lines changed

4 files changed

+37
-3
lines changed

Changelog.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
- Make the physical memory offset configurable through a `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable ([#58](https://github.com/rust-osdev/bootloader/pull/58)).
2+
13
# 0.6.0
24

35
- **Breaking**: Don't set the `#[cfg(not(test))]` attribute for the entry point function in the `entry_point` macro

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ The bootloader crate can be configured through some cargo features:
6464
- `vga_320x200`: This feature switches the VGA hardware to mode 0x13, a graphics mode with resolution 320x200 and 256 colors per pixel. The framebuffer is linear and lives at address `0xa0000`.
6565
- `recursive_page_table`: Maps the level 4 page table recursively and adds the [`recursive_page_table_address`](https://docs.rs/bootloader/0.4.0/bootloader/bootinfo/struct.BootInfo.html#structfield.recursive_page_table_addr) field to the passed `BootInfo`.
6666
- `map_physical_memory`: Maps the complete physical memory in the virtual address space and passes a [`physical_memory_offset`](https://docs.rs/bootloader/0.4.0/bootloader/bootinfo/struct.BootInfo.html#structfield.physical_memory_offset) field in the `BootInfo`.
67+
- The virtual address where the physical memory should be mapped is configurable by setting the `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable (supports decimal and hex numbers (prefixed with `0x`)).
6768

6869
## License
6970

build.rs

+31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::{
22
env,
3+
fs::File,
4+
io::Write,
35
path::{Path, PathBuf},
46
process::{self, Command},
57
};
@@ -123,6 +125,34 @@ fn main() {
123125
process::exit(1);
124126
}
125127

128+
// create a file with the `PHYSICAL_MEMORY_OFFSET` constant
129+
let file_path = out_dir.join("physical_memory_offset.rs");
130+
let mut file = File::create(file_path).expect("failed to create physical_memory_offset.rs");
131+
let physical_memory_offset = match env::var("BOOTLOADER_PHYSICAL_MEMORY_OFFSET") {
132+
Err(env::VarError::NotPresent) => 0o_177777_770_000_000_000_0000u64,
133+
Err(env::VarError::NotUnicode(_)) => panic!(
134+
"The `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable must be valid unicode"
135+
),
136+
Ok(s) => if s.starts_with("0x") {
137+
u64::from_str_radix(&s[2..], 16)
138+
} else {
139+
u64::from_str_radix(&s, 10)
140+
}
141+
.expect(&format!(
142+
"The `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable must be an integer\
143+
(is `{}`).",
144+
s
145+
)),
146+
};
147+
file.write_all(
148+
format!(
149+
"const PHYSICAL_MEMORY_OFFSET: u64 = {:#x};",
150+
physical_memory_offset
151+
)
152+
.as_bytes(),
153+
)
154+
.expect("write to physical_memory_offset.rs failed");
155+
126156
// pass link arguments to rustc
127157
println!("cargo:rustc-link-search=native={}", out_dir.display());
128158
println!(
@@ -131,6 +161,7 @@ fn main() {
131161
);
132162

133163
println!("cargo:rerun-if-env-changed=KERNEL");
164+
println!("cargo:rerun-if-env-changed=BOOTLOADER_PHYSICAL_MEMORY_OFFSET");
134165
println!("cargo:rerun-if-changed={}", kernel.display());
135166
println!("cargo:rerun-if-changed=build.rs");
136167
}

src/main.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use x86_64::structures::paging::{
2222
use x86_64::ux::u9;
2323
use x86_64::{PhysAddr, VirtAddr};
2424

25-
/// The offset into the virtual address space where the physical memory is mapped if
26-
/// the `map_physical_memory` is activated.
27-
const PHYSICAL_MEMORY_OFFSET: u64 = 0o_177777_770_000_000_000_0000;
25+
// The offset into the virtual address space where the physical memory is mapped if
26+
// the `map_physical_memory` is activated. Set by the build script.
27+
include!(concat!(env!("OUT_DIR"), "/physical_memory_offset.rs"));
2828

2929
global_asm!(include_str!("stage_1.s"));
3030
global_asm!(include_str!("stage_2.s"));

0 commit comments

Comments
 (0)