11#[ cfg( not( feature = "binary" ) ) ]
2- fn main ( ) { }
2+ fn main ( ) {
3+ #[ cfg( target_arch = "x86" ) ]
4+ compile_error ! (
5+ "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+
9+ #[ cfg( not( any( target_arch = "x86_64" , target_arch = "x86" ) ) ) ]
10+ compile_error ! ( "This crate only supports the x86_64 architecture." ) ;
11+ }
312
413#[ cfg( feature = "binary" ) ]
5- fn address_from_env ( env : & ' static str ) -> Option < u64 > {
14+ fn num_from_env ( env : & ' static str , aligned : bool ) -> Option < u64 > {
615 use std:: env;
716 match env:: var ( env) {
817 Err ( env:: VarError :: NotPresent ) => None ,
918 Err ( env:: VarError :: NotUnicode ( _) ) => {
1019 panic ! ( "The `{}` environment variable must be valid unicode" , env, )
1120 }
1221 Ok ( s) => {
13- let addr = if s. starts_with ( "0x" ) {
22+ let num = if s. starts_with ( "0x" ) {
1423 u64:: from_str_radix ( & s[ 2 ..] , 16 )
1524 } else {
1625 u64:: from_str_radix ( & s, 10 )
1726 } ;
1827
19- let addr = addr . expect ( & format ! (
28+ let num = num . expect ( & format ! (
2029 "The `{}` environment variable must be an integer\
2130 (is `{}`).",
2231 env, s
2332 ) ) ;
2433
25- if addr % 0x1000 != 0 {
34+ if aligned && num % 0x1000 != 0 {
2635 panic ! (
2736 "The `{}` environment variable must be aligned to 0x1000 (is `{:#x}`)." ,
28- env, addr
37+ env, num
2938 ) ;
3039 }
3140
32- Some ( addr )
41+ Some ( num )
3342 }
3443 }
3544}
@@ -176,31 +185,26 @@ fn main() {
176185 process:: exit ( 1 ) ;
177186 }
178187
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" ) ;
188+ // Configure constants for the bootloader
189+ // We leave some variables as Option<T> rather than hardcoding their defaults so that they
190+ // can be calculated dynamically by the bootloader.
191+ let file_path = out_dir. join ( "bootloader_config.rs" ) ;
192+ let mut file = File :: create ( file_path) . expect ( "failed to create bootloader_config.rs" ) ;
193+ let physical_memory_offset = num_from_env ( "BOOTLOADER_PHYSICAL_MEMORY_OFFSET" , true ) ;
194+ let kernel_stack_address = num_from_env ( "BOOTLOADER_KERNEL_STACK_ADDRESS" , true ) ;
195+ let kernel_stack_size = num_from_env ( "BOOTLOADER_KERNEL_STACK_SIZE" , false ) ;
196196 file. write_all (
197197 format ! (
198- "const KERNEL_STACK_ADDRESS: Option<u64> = {:?};" ,
198+ "const PHYSICAL_MEMORY_OFFSET: Option<u64> = {:?};
199+ const KERNEL_STACK_ADDRESS: Option<u64> = {:?};
200+ const KERNEL_STACK_SIZE: u64 = {};" ,
201+ physical_memory_offset,
199202 kernel_stack_address,
203+ kernel_stack_size. unwrap_or( 512 ) , // size is in number of pages
200204 )
201205 . as_bytes ( ) ,
202206 )
203- . expect ( "write to kernel_stack_address .rs failed" ) ;
207+ . expect ( "write to bootloader_config .rs failed" ) ;
204208
205209 // pass link arguments to rustc
206210 println ! ( "cargo:rustc-link-search=native={}" , out_dir. display( ) ) ;
@@ -212,6 +216,7 @@ fn main() {
212216 println ! ( "cargo:rerun-if-env-changed=KERNEL" ) ;
213217 println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_PHYSICAL_MEMORY_OFFSET" ) ;
214218 println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_ADDRESS" ) ;
219+ println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_SIZE" ) ;
215220 println ! ( "cargo:rerun-if-changed={}" , kernel. display( ) ) ;
216221 println ! ( "cargo:rerun-if-changed=build.rs" ) ;
217222}
0 commit comments