Skip to content

Commit 5111f7b

Browse files
committed
Make physical memory offset customizable through environment variable
1 parent f48ec03 commit 5111f7b

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

build.rs

+20
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,24 @@ 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 option_env!("BOOTLOADER_PHYSICAL_MEMORY_OFFSET") {
132+
None => 0o_177777_770_000_000_000_0000u64,
133+
Some(s) => s.parse().expect(
134+
"The `BOOTLOADER_PHYSICAL_MEMORY_OFFSET` environment variable must be an integer.",
135+
),
136+
};
137+
file.write_all(
138+
format!(
139+
"const PHYSICAL_MEMORY_OFFSET: u64 = {:#x};",
140+
physical_memory_offset
141+
)
142+
.as_bytes(),
143+
)
144+
.expect("write to physical_memory_offset.rs failed");
145+
126146
// pass link arguments to rustc
127147
println!("cargo:rustc-link-search=native={}", out_dir.display());
128148
println!(

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)