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