1
1
#[ 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
+ }
3
11
4
12
#[ 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 > {
6
14
use std:: env;
7
15
match env:: var ( env) {
8
16
Err ( env:: VarError :: NotPresent ) => None ,
9
17
Err ( env:: VarError :: NotUnicode ( _) ) => {
10
18
panic ! ( "The `{}` environment variable must be valid unicode" , env, )
11
19
}
12
20
Ok ( s) => {
13
- let addr = if s. starts_with ( "0x" ) {
21
+ let num = if s. starts_with ( "0x" ) {
14
22
u64:: from_str_radix ( & s[ 2 ..] , 16 )
15
23
} else {
16
24
u64:: from_str_radix ( & s, 10 )
17
25
} ;
18
26
19
- let addr = addr . expect ( & format ! (
27
+ let num = num . expect ( & format ! (
20
28
"The `{}` environment variable must be an integer\
21
29
(is `{}`).",
22
30
env, s
23
31
) ) ;
24
32
25
- if addr % 0x1000 != 0 {
33
+ if aligned && num % 0x1000 != 0 {
26
34
panic ! (
27
35
"The `{}` environment variable must be aligned to 0x1000 (is `{:#x}`)." ,
28
- env, addr
36
+ env, num
29
37
) ;
30
38
}
31
39
32
- Some ( addr )
40
+ Some ( num )
33
41
}
34
42
}
35
43
}
@@ -176,31 +184,26 @@ fn main() {
176
184
process:: exit ( 1 ) ;
177
185
}
178
186
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 ) ;
196
195
file. write_all (
197
196
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,
199
201
kernel_stack_address,
202
+ kernel_stack_size. unwrap_or( 512 ) , // size is in number of pages
200
203
)
201
204
. as_bytes ( ) ,
202
205
)
203
- . expect ( "write to kernel_stack_address .rs failed" ) ;
206
+ . expect ( "write to bootloader_config .rs failed" ) ;
204
207
205
208
// pass link arguments to rustc
206
209
println ! ( "cargo:rustc-link-search=native={}" , out_dir. display( ) ) ;
@@ -212,6 +215,7 @@ fn main() {
212
215
println ! ( "cargo:rerun-if-env-changed=KERNEL" ) ;
213
216
println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_PHYSICAL_MEMORY_OFFSET" ) ;
214
217
println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_ADDRESS" ) ;
218
+ println ! ( "cargo:rerun-if-env-changed=BOOTLOADER_KERNEL_STACK_SIZE" ) ;
215
219
println ! ( "cargo:rerun-if-changed={}" , kernel. display( ) ) ;
216
220
println ! ( "cargo:rerun-if-changed=build.rs" ) ;
217
221
}
0 commit comments