Skip to content

Commit c80fe46

Browse files
committed
chore(deps): update to uefi v0.32
1 parent bc6f370 commit c80fe46

File tree

16 files changed

+69
-101
lines changed

16 files changed

+69
-101
lines changed

docs/labs/0x00/tasks.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -335,10 +335,10 @@ extern crate log;
335335
extern crate alloc;
336336

337337
use core::arch::asm;
338-
use uefi::prelude::*;
338+
use uefi::{Status, entry};
339339

340340
#[entry]
341-
fn efi_main(image: uefi::Handle, system_table: SystemTable<Boot>) -> Status {
341+
fn efi_main() -> Status {
342342
uefi::helpers::init().expect("Failed to initialize utilities");
343343
log::set_max_level(log::LevelFilter::Info);
344344

@@ -356,9 +356,7 @@ fn efi_main(image: uefi::Handle, system_table: SystemTable<Boot>) -> Status {
356356
}
357357
```
358358

359-
`efi_main` 通过 `#[entry]` 被指定为 UEFI 程序的入口函数,`efi_main` 函数的参数 `system_table` 是一个 `SystemTable<Boot>` 类型的变量,它包含了 UEFI 程序运行时所需要的各种信息,如内存映射、文件系统、图形界面等。
360-
361-
`efi_main` 函数中,首先对 `system_table``log` 进行初始化,然后进入一个死循环,每次循环输出一条日志后等待一段时间。
359+
`efi_main` 通过 `#[entry]` 被指定为 UEFI 程序的入口函数,在 `efi_main` 函数中,首先对 UEFI 相关功能组件进行初始化,然后进入一个死循环,每次循环输出一条日志后等待一段时间。
362360

363361
在项目根目录下运行 `make run``python ysos.py run`,预期得到如下输出:
364362

docs/labs/0x01/tasks.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ SECTIONS {
111111
为了方便你的实现,在 `pkg/boot/src/fs.rs` 中,提供了一些函数可供调用,对于一个正常的文件读取流程,你可以参考如下代码:
112112

113113
```rust
114-
let mut file = open_file(bs, file_path);
115-
let buf = load_file(bs, &mut file);
114+
let mut file = open_file(file_path);
115+
let buf = load_file(&mut file);
116116
```
117117

118118
### 更新控制寄存器
@@ -158,12 +158,7 @@ unsafe {
158158
!!! tip "一些提示"
159159

160160
- `physical_memory_offset` 在配置结构体中,它描述了物理地址进行线性映射的偏移量,你可能会使用到。
161-
- 你可以使用如下的代码初始化帧分配器:
162-
163-
```rust
164-
let mut frame_allocator = UEFIFrameAllocator(bs);
165-
```
166-
161+
- 你可以使用 `&mut UEFIFrameAllocator` 表达式作为参数传递帧分配器。
167162
- `pkg/elf/src/lib.rs` 中的 `load_segment` 函数需要你进行补全。**请认真学习实验文档所提供的有关分页内存权限管理、内核 ELF 文件格式的内容,以便你能够完成这一部分的实现。**
168163
- 阅读配置文件定义中有关内核栈的内容,利用相关参数来初始化内核栈。
169164
- 别忘了将你修改过的控制寄存器恢复原样。

docs/labs/0x04/tasks.md

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,8 @@ pub struct BootInfo {
168168
/// Load apps into memory, when no fs implemented in kernel
169169
///
170170
/// List all file under "APP" and load them.
171-
pub fn load_apps(bs: &BootServices) -> AppList {
172-
let mut root = open_root(bs);
171+
pub fn load_apps() -> AppList {
172+
let mut root = open_root();
173173
let mut buf = [0; 8];
174174
let cstr_path = uefi::CStr16::from_str_with_buf("\\APP\\", &mut buf).unwrap();
175175

@@ -218,7 +218,7 @@ pub fn load_apps(bs: &BootServices) -> AppList {
218218

219219
let apps = if config.load_apps {
220220
info!("Loading apps...");
221-
Some(load_apps(system_table.boot_services()))
221+
Some(load_apps())
222222
} else {
223223
info!("Skip loading apps");
224224
None
@@ -966,27 +966,7 @@ The factorial of 999999 under modulo 1000000007 is 128233642.
966966

967967
3. 🤔 基于帧回收器的实现,在 `elf` 中实现 `unmap_range` 函数,从页表中取消映射一段连续的页面,并使用帧回收器进行回收。之后,在合适的地方,结合 `ProcessData` 中存储的页面信息,利用这个函数实现进程栈的回收。其他进程资源(如页表、代码段、数据段等)的回收将会在后续实验中实现,目前暂时不需要考虑。
968968

969-
4. 🤔 尝试利用 `UefiRuntime``chrono` crate,获取当前时间,并将其暴露给用户态,以实现 `sleep` 函数。
970-
971-
`UefiRuntime` 的实现,它可能需要使用锁进行保护:
972-
973-
```rust
974-
pub struct UefiRuntime {
975-
runtime_service: &'static RuntimeServices,
976-
}
977-
978-
impl UefiRuntime {
979-
pub unsafe fn new(boot_info: &'static BootInfo) -> Self {
980-
Self {
981-
runtime_service: boot_info.system_table.runtime_services(),
982-
}
983-
}
984-
985-
pub fn get_time(&self) -> Time {
986-
self.runtime_service.get_time().unwrap()
987-
}
988-
}
989-
```
969+
4. 🤔 尝试利用 `uefi::runtime::get_time()``chrono` crate,获取当前时间,并将其暴露给用户态,以实现 `sleep` 函数。
990970

991971
这里提供一个可能的 `sleep` 函数实现:
992972

src/0x00/pkg/boot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ edition = "2021"
55
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
66

77
[dependencies]
8-
uefi = { version = "0.31", default-features = false }
8+
uefi = { version = "0.32", default-features = false }
99
log = "0.4"
1010

1111
[features]

src/0x01/pkg/boot/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ edition = "2021"
66

77
[dependencies]
88
arrayvec = { version = "0.7", default-features = false }
9-
uefi = { version = "0.28", default-features = false }
9+
uefi = { version = "0.32", default-features = false }
1010
log = "0.4"
1111
x86_64 = "0.15"
1212
xmas-elf = "0.9"

src/0x01/pkg/boot/src/allocator.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
use uefi::table::boot::*;
1+
use uefi::boot::{AllocateType, MemoryType};
22
use x86_64::{structures::paging::*, PhysAddr};
33

44
/// Use `BootServices::allocate_pages()` as frame allocator
5-
pub struct UEFIFrameAllocator<'a>(pub &'a BootServices);
5+
pub struct UEFIFrameAllocator;
66

7-
unsafe impl FrameAllocator<Size4KiB> for UEFIFrameAllocator<'_> {
7+
unsafe impl FrameAllocator<Size4KiB> for UEFIFrameAllocator {
88
fn allocate_frame(&mut self) -> Option<PhysFrame> {
9-
let addr = self
10-
.0
11-
.allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, 1)
9+
let addr = uefi::boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, 1)
1210
.expect("Failed to allocate frame");
13-
let frame = PhysFrame::containing_address(PhysAddr::new(addr));
11+
let frame = PhysFrame::containing_address(PhysAddr::new(addr.as_ptr() as u64));
1412
Some(frame)
1513
}
1614
}

src/0x01/pkg/boot/src/fs.rs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,22 @@ use uefi::table::boot::*;
44
use xmas_elf::ElfFile;
55

66
/// Open root directory
7-
pub fn open_root(bs: &BootServices) -> Directory {
8-
let handle = bs
9-
.get_handle_for_protocol::<SimpleFileSystem>()
7+
pub fn open_root() -> Directory {
8+
let handle = uefi::boot::get_handle_for_protocol::<SimpleFileSystem>()
109
.expect("Failed to get handle for SimpleFileSystem");
11-
12-
let fs = bs
13-
.open_protocol_exclusive::<SimpleFileSystem>(handle)
10+
let mut fs = uefi::boot::open_protocol_exclusive::<SimpleFileSystem>(handle)
1411
.expect("Failed to get FileSystem");
15-
let mut fs = fs;
1612

1713
fs.open_volume().expect("Failed to open volume")
1814
}
1915

16+
2017
/// Open file at `path`
21-
pub fn open_file(bs: &BootServices, path: &str) -> RegularFile {
18+
pub fn open_file(path: &str) -> RegularFile {
2219
let mut buf = [0; 64];
2320
let cstr_path = uefi::CStr16::from_str_with_buf(path, &mut buf).unwrap();
2421

25-
let handle = open_root(bs)
22+
let handle = open_root()
2623
.open(cstr_path, FileMode::Read, FileAttribute::empty())
2724
.expect("Failed to open file");
2825

@@ -33,37 +30,38 @@ pub fn open_file(bs: &BootServices, path: &str) -> RegularFile {
3330
}
3431

3532
/// Load file to new allocated pages
36-
pub fn load_file(bs: &BootServices, file: &mut RegularFile) -> &'static mut [u8] {
33+
pub fn load_file(file: &mut RegularFile) -> &'static mut [u8] {
3734
let mut info_buf = [0u8; 0x100];
3835
let info = file
3936
.get_info::<FileInfo>(&mut info_buf)
4037
.expect("Failed to get file info");
4138

4239
let pages = info.file_size() as usize / 0x1000 + 1;
4340

44-
let mem_start = bs
45-
.allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, pages)
46-
.expect("Failed to allocate pages");
41+
let mem_start =
42+
uefi::boot::allocate_pages(AllocateType::AnyPages, MemoryType::LOADER_DATA, pages)
43+
.expect("Failed to allocate pages");
4744

48-
let buf = unsafe { core::slice::from_raw_parts_mut(mem_start as *mut u8, pages * 0x1000) };
45+
let buf = unsafe { core::slice::from_raw_parts_mut(mem_start.as_ptr(), pages * 0x1000) };
4946
let len = file.read(buf).expect("Failed to read file");
5047

5148
info!(
5249
"Load file \"{}\" to memory, size = {}",
5350
info.file_name(),
5451
len
5552
);
56-
5753
&mut buf[..len]
5854
}
5955

6056
/// Free ELF files for which the buffer was created using 'load_file'
61-
pub fn free_elf(bs: &BootServices, elf: ElfFile) {
57+
pub fn free_elf(elf: ElfFile) {
6258
let buffer = elf.input;
6359
let pages = buffer.len() / 0x1000 + 1;
64-
let mem_start = buffer.as_ptr() as u64;
60+
let mem_start = NonNull::new(buffer.as_ptr() as *mut u8).expect("Invalid pointer");
61+
62+
info!("Free ELF file, pages = {}, addr = {:#x?}", pages, mem_start);
6563

6664
unsafe {
67-
bs.free_pages(mem_start, pages).expect("Failed to free pages");
65+
uefi::boot::free_pages(mem_start, pages).expect("Failed to free pages");
6866
}
6967
}

src/0x01/pkg/boot/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub use uefi::table::Runtime;
1111
pub use uefi::Status as UefiStatus;
1212

1313
use arrayvec::ArrayVec;
14+
use core::ptr::NonNull;
1415
use x86_64::VirtAddr;
1516
use x86_64::registers::control::Cr3;
1617
use x86_64::structures::paging::{OffsetPageTable, PageTable};
@@ -35,8 +36,8 @@ pub struct BootInfo {
3536
/// The offset into the virtual address space where the physical memory is mapped.
3637
pub physical_memory_offset: u64,
3738

38-
/// UEFI SystemTable
39-
pub system_table: SystemTable<Runtime>,
39+
/// The system table virtual address
40+
pub system_table: NonNull<core::ffi::c_void>,
4041
}
4142

4243
/// Get current page table from CR3

src/0x01/pkg/boot/src/main.rs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern crate alloc;
88

99
use alloc::boxed::Box;
1010
use alloc::vec;
11-
use uefi::prelude::*;
11+
use uefi::{entry, Status};
1212
use x86_64::registers::control::*;
1313
use ysos_boot::*;
1414

@@ -17,14 +17,12 @@ mod config;
1717
const CONFIG_PATH: &str = "\\EFI\\BOOT\\boot.conf";
1818

1919
#[entry]
20-
fn efi_main(image: uefi::Handle, mut system_table: SystemTable<Boot>) -> Status {
21-
uefi::helpers::init(&mut system_table).expect("Failed to initialize utilities");
20+
fn efi_main() -> Status {
21+
uefi::helpers::init().expect("Failed to initialize utilities");
2222

2323
log::set_max_level(log::LevelFilter::Info);
2424
info!("Running UEFI bootloader...");
2525

26-
let bs = system_table.boot_services();
27-
2826
// 1. Load config
2927
let config = { /* FIXME: Load config file as Config */ };
3028

@@ -38,14 +36,7 @@ fn efi_main(image: uefi::Handle, mut system_table: SystemTable<Boot>) -> Status
3836
}
3937

4038
// 3. Load MemoryMap
41-
let max_mmap_size = system_table.boot_services().memory_map_size();
42-
let mmap_storage = Box::leak(
43-
vec![0; max_mmap_size.map_size + 10 * max_mmap_size.entry_size].into_boxed_slice(),
44-
);
45-
let mmap = system_table
46-
.boot_services()
47-
.memory_map(mmap_storage)
48-
.expect("Failed to get memory map");
39+
let mmap = uefi::boot::memory_map(MemoryType::LOADER_DATA).expect("Failed to get memory map");
4940

5041
let max_phys_addr = mmap
5142
.entries()
@@ -67,19 +58,24 @@ fn efi_main(image: uefi::Handle, mut system_table: SystemTable<Boot>) -> Status
6758

6859
// FIXME: recover write protect (Cr0)
6960

70-
free_elf(bs, elf);
61+
free_elf(elf);
62+
63+
// 5. Pass system table to kernel
64+
let ptr = uefi::table::system_table_raw().expect("Failed to get system table");
65+
let system_table = ptr.cast::<core::ffi::c_void>();
66+
7167

72-
// 5. Exit boot and jump to ELF entry
68+
// 6. Exit boot and jump to ELF entry
7369
info!("Exiting boot services...");
7470

75-
let (runtime, mmap) = system_table.exit_boot_services(MemoryType::LOADER_DATA);
71+
let mmap = unsafe { uefi::boot::exit_boot_services(MemoryType::LOADER_DATA) };
7672
// NOTE: alloc & log are no longer available
7773

7874
// construct BootInfo
7975
let bootinfo = BootInfo {
8076
memory_map: mmap.entries().copied().collect(),
8177
physical_memory_offset: config.physical_memory_offset,
82-
system_table: runtime,
78+
system_table,
8379
};
8480

8581
// align stack to 8 bytes

src/0x01/pkg/kernel/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ edition = "2021"
77
[dependencies]
88
boot = { package = "ysos_boot", path = "../boot", default-features = false }
99
lazy_static = { version = "1.4", features = ["spin_no_std"] }
10+
uefi = { version = "0.32", default-features = false }
1011
paste = "1.0"
1112
spin = "0.9"
1213
x86 = "0.52"

0 commit comments

Comments
 (0)