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