@@ -14,28 +14,22 @@ use ftl_utils::alignment::align_up;
1414
1515use crate :: arch;
1616use crate :: arch:: MIN_PAGE_SIZE ;
17- use crate :: boot:: BootInfo ;
18- use crate :: initfs;
1917use crate :: memory:: PAGE_ALLOCATOR ;
2018use crate :: memory:: PageType ;
2119
22- const START_INFO : & StartInfo = & StartInfo {
23- sys_print : |bytes| {
24- println ! ( "{}" , core:: str :: from_utf8( bytes) . unwrap( ) ) ;
25- } ,
26- sys_panic : || {
27- error ! ( "server panicked" ) ;
28- crate :: scheduler:: return_to_user ( ) ;
29- } ,
30- } ;
31-
32- fn load_elf ( elf_file : & [ u8 ] ) {
33- let elf = match Elf :: parse ( elf_file, ftl_elf:: ET_DYN ) {
34- Ok ( elf) => elf,
35- Err ( e) => {
36- panic ! ( "failed to parse ELF file: {:?}" , e) ;
37- }
38- } ;
20+ pub type EntryFn = extern "Rust" fn ( start_info : * const StartInfo ) ;
21+
22+ #[ derive( Debug ) ]
23+ pub enum Error {
24+ ParseElf ,
25+ OutOfMemory ,
26+ BadRelocType ,
27+ BadRelocOffset ,
28+ BadRelocSize ,
29+ }
30+
31+ pub fn load_elf ( elf_file : & [ u8 ] ) -> Result < ( * const u8 , usize , EntryFn ) , Error > {
32+ let elf = Elf :: parse ( elf_file, ftl_elf:: ET_DYN ) . map_err ( |_| Error :: ParseElf ) ?;
3933
4034 // Find the end of the image to calculate the size of the memory it needs.
4135 let mut image_size = 0 ;
@@ -46,12 +40,9 @@ fn load_elf(elf_file: &[u8]) {
4640 }
4741
4842 let image_size = align_up ( image_size as usize , MIN_PAGE_SIZE ) ;
49- let image_paddr = match PAGE_ALLOCATOR . alloc ( image_size, PageType :: Zeroed ) {
50- Some ( paddr) => paddr,
51- None => {
52- panic ! ( "out of memory: {} bytes" , image_size) ;
53- }
54- } ;
43+ let image_paddr = PAGE_ALLOCATOR
44+ . alloc ( image_size, PageType :: Zeroed )
45+ . ok_or ( Error :: OutOfMemory ) ?;
5546
5647 let image_ptr: * mut u8 = arch:: paddr2vaddr ( image_paddr) . as_mut_ptr ( ) ;
5748 let image = unsafe { slice:: from_raw_parts_mut ( image_ptr, image_size) } ;
@@ -107,7 +98,7 @@ fn load_elf(elf_file: &[u8]) {
10798 // Apply relocations.
10899 if rela_addr != 0 && rela_size > 0 {
109100 if rela_size % size_of :: < Rela > ( ) != 0 {
110- panic ! ( "invalid relocation table size: {} bytes" , rela_size ) ;
101+ return Err ( Error :: BadRelocSize ) ;
111102 }
112103
113104 let relocations = unsafe {
@@ -120,12 +111,12 @@ fn load_elf(elf_file: &[u8]) {
120111 for rela in relocations {
121112 let target_off = rela. r_offset as usize ;
122113 if rela. r_sym ( ) != 0 || rela. r_type ( ) != R_X86_64_RELATIVE {
123- panic ! ( "unexpected relocation type: {}" , rela . r_type ( ) ) ;
114+ return Err ( Error :: BadRelocType ) ;
124115 }
125116
126117 let target_end = match target_off. checked_add ( size_of :: < u64 > ( ) ) {
127118 Some ( end) if end <= image. len ( ) => end,
128- _ => panic ! ( "invalid relocation target offset: {}" , target_off ) ,
119+ _ => return Err ( Error :: BadRelocOffset ) ,
129120 } ;
130121
131122 let base = image. as_ptr ( ) as u64 ;
@@ -139,19 +130,5 @@ fn load_elf(elf_file: &[u8]) {
139130 core:: mem:: transmute :: < * const u8 , extern "Rust" fn ( start_info : * const StartInfo ) > ( entry_ptr)
140131 } ;
141132
142- trace ! ( "Calling entry point: {:p}" , entry_fn) ;
143- entry_fn ( START_INFO ) ;
144- trace ! ( "Entry point returned" ) ;
145- }
146-
147- pub fn init ( bootinfo : & BootInfo ) {
148- for module in & bootinfo. modules {
149- let initfs = initfs:: InitFsLoader :: new ( module) ;
150- for file in initfs {
151- if file. name . starts_with ( b"servers/" ) && file. name . ends_with ( b".elf" ) {
152- trace ! ( "loading {}..." , core:: str :: from_utf8( file. name) . unwrap( ) ) ;
153- load_elf ( file. data ) ;
154- }
155- }
156- }
133+ Ok ( ( image. as_ptr ( ) , image_size, entry_fn) )
157134}
0 commit comments