xref: /DragonBoot/src/lib.rs (revision 1971aeeee1b8252821527c319e1327d93b88ffc4)
1 #![no_std]
2 #![no_main]
3 #![feature(fmt_internals)]
4 
5 use core::{
6     ffi::c_void,
7     fmt::{Formatter, Write},
8 };
9 extern crate alloc;
10 extern crate elf;
11 
12 use alloc::string::String;
13 use log::info;
14 use uefi::{
15     table::{Boot, SystemTable},
16     CStr16, Handle,
17 };
18 
19 mod arch;
20 
21 extern "C" {
_start() -> !22     fn _start() -> !;
23 }
24 
25 #[no_mangle]
efi_main( handle_ptr: *mut c_void, system_table_ptr: *mut c_void, ) -> usize26 unsafe extern "efiapi" fn efi_main(
27     handle_ptr: *mut c_void,
28     system_table_ptr: *mut c_void,
29 ) -> usize {
30     rs_efi_main(handle_ptr, system_table_ptr)
31         .map(|_| uefi::Status::SUCCESS.0 as usize)
32         .unwrap_or_else(|e| e.0 as usize)
33 }
34 
rs_efi_main( handle_ptr: *mut c_void, system_table_ptr: *mut c_void, ) -> Result<(), uefi::prelude::Status>35 fn rs_efi_main(
36     handle_ptr: *mut c_void,
37     system_table_ptr: *mut c_void,
38 ) -> Result<(), uefi::prelude::Status> {
39     let image_handle =
40         unsafe { Handle::from_ptr(handle_ptr).ok_or(uefi::Status::INVALID_PARAMETER)? };
41     let mut system_table: SystemTable<uefi::table::Boot> =
42         unsafe { SystemTable::from_ptr(system_table_ptr).ok_or(uefi::Status::INVALID_PARAMETER)? };
43     unsafe { system_table.boot_services().set_image_handle(image_handle) };
44 
45     uefi_services::init(&mut system_table).map_err(|e| e.status())?;
46 
47     let mut buf = [0u16; 32];
48     system_table.stdout().write_str("123455\n");
49     let x = CStr16::from_str_with_buf("DragonBoot Starting...\n", &mut buf).unwrap();
50     system_table
51         .stdout()
52         .output_string(x)
53         .map_err(|e| e.status())?;
54 
55     let x = String::from("AAAAAAAHello, world!\n");
56     system_table.stdout().write_str(x.as_str());
57 
58     let args = core::format_args!("hgfhgfhfHello, world!\n");
59     // 这里println的FormatWriter里面的&'a dyn (Write +'a)貌似不能在重定位之前访问,不然的话会出现错误:
60     // ```
61     // Found EFI removable media binary efi/boot/bootriscv64.efi
62     // 20336 bytes read in 4 ms (4.8 MiB/s)
63     // Booting /efi\boot\bootriscv64.efi
64     // 123455
65     // DragonBoot Starting...
66     // AAAAAAAHello, world!
67     // Unhandled exception: Illegal instruction
68     // EPC: 00000000000012b0 RA: 000000009deee240 TVAL: 0000000000000000
69     // EPC: ffffffffe0abf2b0 RA: 000000007e9ac240 reloc adjusted
70     // Code: 0000 0000 0000 0000 0000 0000 0000 0000 (0000)
71     // UEFI image [0x000000009deec000:0x000000009def0f6f] '/efi\boot\bootriscv64.efi
72     // ```
73     //
74     // Fault的PC和值都是错误的,因此猜想动态分发这块的代码不是PIC的,需要重定位,因此不能在重定位之前用println
75 
76     loop {}
77     // 执行下面这行会出错,就是上面注释说的那个错误
78     system_table.stdout().write_fmt(args);
79     // system_table.stdout().write_str(args);
80 
81     loop {}
82     return Ok(());
83 }
84 
85 #[no_mangle]
_relocate()86 fn _relocate() {
87     loop {}
88 }
89 
x()90 pub fn x() {}
91