11#[ cfg( not( feature = "binary" ) ) ]
2- fn main ( ) { }
2+ #[ allow( unreachable_code) ]
3+ fn main ( ) {
4+ #[ cfg( target_arch = "x86" ) ]
5+ panic ! ( "This crate currently does not support 32-bit protected mode. \
6+ See https://github.com/rust-osdev/bootloader/issues/70 for more information.") ;
7+
8+ #[ cfg( not( target_arch = "x86_64" ) ) ]
9+ panic ! ( "This crate only supports the x86_64 architecture." ) ;
10+ }
311
412#[ cfg( feature = "binary" ) ]
5- fn address_from_env ( env : & ' static str ) -> Option < u64 > {
13+ fn num_from_env ( env : & ' static str , aligned : bool ) -> Option < u64 > {
614 use std:: env;
715 match env:: var ( env) {
816 Err ( env:: VarError :: NotPresent ) => None ,
917 Err ( env:: VarError :: NotUnicode ( _) ) => {
1018 panic ! ( "The `{}` environment variable must be valid unicode" , env, )
1119 }
1220 Ok ( s) => {
13- let addr = if s. starts_with ( "0x" ) {
21+ let num = if s. starts_with ( "0x" ) {
1422 u64:: from_str_radix ( & s[ 2 ..] , 16 )
1523 } else {
1624 u64:: from_str_radix ( & s, 10 )
1725 } ;
1826
19- let addr = addr . expect ( & format ! (
27+ let num = num . expect ( & format ! (
2028 "The `{}` environment variable must be an integer\
2129 (is `{}`).",
2230 env, s
2331 ) ) ;
2432
25- if addr % 0x1000 != 0 {
33+ if aligned && num % 0x1000 != 0 {
2634 panic ! (
2735 "The `{}` environment variable must be aligned to 0x1000 (is `{:#x}`)." ,
28- env, addr
36+ env, num
2937 ) ;
3038 }
3139
32- Some ( addr )
40+ Some ( num )
3341 }
3442 }
3543}
@@ -176,31 +184,26 @@ fn main() {
176184 process:: exit ( 1 ) ;
177185 }
178186
179- // create a file with the `PHYSICAL_MEMORY_OFFSET` constant
180- let file_path = out_dir. join ( "physical_memory_offset.rs" ) ;
181- let mut file = File :: create ( file_path) . expect ( "failed to create physical_memory_offset.rs" ) ;
182- let physical_memory_offset = address_from_env ( "BOOTLOADER_PHYSICAL_MEMORY_OFFSET" ) ;
183- file. write_all (
184- format ! (
185- "const PHYSICAL_MEMORY_OFFSET: Option<u64> = {:?};" ,
186- physical_memory_offset
187- )
188- . as_bytes ( ) ,
189- )
190- . expect ( "write to physical_memory_offset.rs failed" ) ;
191-
192- // create a file with the `KERNEL_STACK_ADDRESS` constant
193- let file_path = out_dir. join ( "kernel_stack_address.rs" ) ;
194- let mut file = File :: create ( file_path) . expect ( "failed to create kernel_stack_address.rs" ) ;
195- let kernel_stack_address = address_from_env ( "BOOTLOADER_KERNEL_STACK_ADDRESS" ) ;
187+ // Configure constants for the bootloader
188+ // We leave some variables as Option<T> rather than hardcoding their defaults so that they
189+ // can be calculated dynamically by the bootloader.
190+ let file_path = out_dir. join ( "bootloader_config.rs" ) ;
191+ let mut file = File :: create ( file_path) . expect ( "failed to create bootloader_config.rs" ) ;
192+ let physical_memory_offset = num_from_env ( "BOOTLOADER_PHYSICAL_MEMORY_OFFSET" , true ) ;
193+ let kernel_stack_address = num_from_env ( "BOOTLOADER_KERNEL_STACK_ADDRESS" , true ) ;
194+ let kernel_stack_size = num_from_env ( "BOOTLOADER_KERNEL_STACK_SIZE" , false ) ;
196195 file. write_all (
197196 format ! (
198- "const KERNEL_STACK_ADDRESS: Option<u64> = {:?};" ,
197+ "const PHYSICAL_MEMORY_OFFSET: Option<u64> = {:?};
198+ const KERNEL_STACK_ADDRESS: Option<u64> = {:?};
199+ const KERNEL_STACK_SIZE: u64 = {};" ,
200+ physical_memory_offset,
199201 kernel_stack_address,
202+ kernel_stack_size. unwrap_or( 512 ) , // size is in number of pages
200203 )
201204 . as_bytes ( ) ,
202205 )
203- . expect ( "write to kernel_stack_address .rs failed" ) ;
206+ . expect ( "write to bootloader_config .rs failed" ) ;
204207
205208 // pass link arguments to rustc
206209 println ! ( "cargo:rustc-link-search=native={}" , out_dir. display( ) ) ;
@@ -212,6 +215,7 @@ fn main() {
212215 println ! ( "cargo:rerun-if-env-changed=KERNEL" ) ;
213216 println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_PHYSICAL_MEMORY_OFFSET" ) ;
214217 println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_ADDRESS" ) ;
218+ println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_SIZE" ) ;
215219 println ! ( "cargo:rerun-if-changed={}" , kernel. display( ) ) ;
216220 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
217221}
0 commit comments