1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
#![no_std]
#![no_main]
#![allow(static_mut_refs, unsafe_code)]
use core::panic::PanicInfo;
use rsnix::uefi::*;
// ---------------------------------------------------------------------------
// Panic handler
// ---------------------------------------------------------------------------
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
use core::fmt::Write;
rsnix::serial::write_str("\r\n*** PANIC ***\r\n");
let _ = write!(&mut rsnix::serial::SerialWriter, "{}\r\n", info.message());
if let Some(loc) = info.location() {
let _ = write!(
&mut rsnix::serial::SerialWriter,
" at {}:{}\r\n",
loc.file(),
loc.line()
);
}
loop {
unsafe {
core::arch::asm!("cli; hlt");
}
}
}
// ---------------------------------------------------------------------------
// EFI entry point
// ---------------------------------------------------------------------------
#[no_mangle]
pub extern "efiapi" fn efi_main(image_handle: Handle, system_table: *mut SystemTable) -> ! {
let st = unsafe { &mut *system_table };
let bs = unsafe { &mut *st.boot_services };
let con_out = unsafe { &mut *st.con_out };
// ---- Phase 1: UEFI boot services alive ----
// Print banner via UEFI console
unsafe {
let s = concat!("rsnix v", env!("CARGO_PKG_VERSION"), " booting..\n");
let bytes = s.as_bytes();
let mut buf: [u16; 48] = [0; 48];
for (i, &b) in bytes.iter().enumerate().take(47) {
buf[i] = b as u16;
}
(con_out.output_string)(con_out, buf.as_ptr());
}
rsnix::serial::init();
rsnix::serial_println!("=== rsnix boot ===");
// Stash the memory map
let map_key = match rsnix::memory::capture_memory_map(bs) {
Ok(k) => k,
Err(e) => {
rsnix::serial_println!("mem: capture_memory_map failed 0x{:x}", e);
0
}
};
// Locate GOP (Graphics Output Protocol)
let mut gop_interface: *mut core::ffi::c_void = core::ptr::null_mut();
let gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
let gop_result = unsafe {
(bs.locate_protocol)(
&gop_guid as *const Guid,
core::ptr::null_mut(),
&mut gop_interface,
)
};
if gop_result == EFI_SUCCESS {
let gop = unsafe { &mut *(gop_interface as *mut GraphicsOutputProtocol) };
rsnix::serial_println!("gop: framebuffer found");
rsnix::gop::init(gop);
} else {
rsnix::serial_println!("gop: not found (0x{:x})", gop_result);
}
// ExitBootServices — we now own the machine
if unsafe { (bs.exit_boot_services)(image_handle, map_key) } != EFI_SUCCESS {
rsnix::serial_println!("WARN: ExitBootServices failed, retrying...");
match rsnix::memory::refresh_and_exit(bs, image_handle) {
Ok(_) => {}
Err(e) => {
rsnix::serial_println!("FATAL: cannot ExitBootServices (0x{:x})", e);
loop {
unsafe {
core::arch::asm!("cli; hlt");
}
}
}
}
}
// ---- Phase 2: Native mode ----
rsnix::serial::init();
rsnix::serial_println!("serial: COM1 115200 8N1");
rsnix::ps2::init();
rsnix::serial_println!("ps2: keyboard initialized");
rsnix::interrupts::init();
rsnix::serial_println!("intr: IDT loaded, PIC remapped, timer ~100 Hz");
dump_memory_summary();
rsnix::serial_println!("boot: entering shell");
shell::run();
}
// ---------------------------------------------------------------------------
// Print a memory summary from the stashed memory map
// ---------------------------------------------------------------------------
fn dump_memory_summary() {
let (map, key) = rsnix::memory::get_map();
let mut conventional_4k = 0u64;
let mut addr_space_4k = 0u64;
for desc in map.iter() {
let pages = desc.number_of_pages;
addr_space_4k += pages;
if desc.kind == 7 {
conventional_4k += pages;
}
}
rsnix::serial_println!(
"mem: {} entries, key=0x{:x}, usable: {} KB ({} MB)",
map.len(),
key,
conventional_4k * 4,
(conventional_4k * 4) / 1024,
);
rsnix::serial_println!(
"mem: address space: {} MB (incl. PCIe MMIO)",
(addr_space_4k * 4) / 1024,
);
}
|