Skip to content

Commit 0df7c07

Browse files
committed
Move root dir buffer into main.rs to reuse it later
We can reuse it to load the kernel.
1 parent 3b83a08 commit 0df7c07

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

Diff for: bios/second_stage/src/disk.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
use crate::{dap, screen};
2-
use core::fmt::Write as _;
1+
use crate::dap;
32

43
#[derive(Clone)]
54
pub struct DiskAccess {
@@ -10,7 +9,7 @@ pub struct DiskAccess {
109

1110
impl Read for DiskAccess {
1211
fn read_exact(&mut self, len: usize) -> &[u8] {
13-
static mut TMP_BUF: AlignedBuffer<512> = AlignedBuffer {
12+
static mut TMP_BUF: AlignedArrayBuffer<512> = AlignedArrayBuffer {
1413
buffer: [0; 512],
1514
limit: 512,
1615
};
@@ -22,7 +21,7 @@ impl Read for DiskAccess {
2221
&buf.buffer[..len]
2322
}
2423

25-
fn read_exact_into(&mut self, buf: &mut dyn AlignedSlice) {
24+
fn read_exact_into(&mut self, buf: &mut dyn AlignedBuffer) {
2625
let buf = buf.slice_mut();
2726
assert_eq!(buf.len() % 512, 0);
2827

@@ -77,7 +76,7 @@ impl Seek for DiskAccess {
7776

7877
pub trait Read {
7978
fn read_exact(&mut self, len: usize) -> &[u8];
80-
fn read_exact_into(&mut self, buf: &mut dyn AlignedSlice);
79+
fn read_exact_into(&mut self, buf: &mut dyn AlignedBuffer);
8180
}
8281

8382
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -91,21 +90,26 @@ pub trait Seek {
9190
}
9291

9392
#[repr(align(2))]
94-
pub struct AlignedBuffer<const LEN: usize> {
93+
pub struct AlignedArrayBuffer<const LEN: usize> {
9594
pub buffer: [u8; LEN],
9695
pub limit: usize,
9796
}
9897

99-
pub trait AlignedSlice {
98+
pub trait AlignedBuffer {
10099
fn slice(&self) -> &[u8];
101100
fn slice_mut(&mut self) -> &mut [u8];
101+
fn set_limit(&mut self, limit: usize);
102102
}
103103

104-
impl<const LEN: usize> AlignedSlice for AlignedBuffer<LEN> {
104+
impl<const LEN: usize> AlignedBuffer for AlignedArrayBuffer<LEN> {
105105
fn slice(&self) -> &[u8] {
106106
&self.buffer[..self.limit]
107107
}
108108
fn slice_mut(&mut self) -> &mut [u8] {
109109
&mut self.buffer[..self.limit]
110110
}
111+
fn set_limit(&mut self, limit: usize) {
112+
assert!(limit <= LEN);
113+
self.limit = limit;
114+
}
111115
}

Diff for: bios/second_stage/src/fat.rs

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
// based on https://crates.io/crates/mini_fat by https://github.com/gridbugs
22

3-
use crate::{
4-
disk::{AlignedBuffer, AlignedSlice, Read, Seek, SeekFrom},
5-
screen,
6-
};
3+
use crate::disk::{AlignedBuffer, Read, Seek, SeekFrom};
74
use core::{char::DecodeUtf16Error, fmt::Write as _};
85

96
const DIRECTORY_ENTRY_BYTES: usize = 32;
@@ -158,8 +155,12 @@ impl<D: Read + Seek> FileSystem<D> {
158155
}
159156
}
160157

161-
pub fn find_file_in_root_dir(&mut self, name: &str) -> Option<File> {
162-
let mut root_entries = self.read_root_dir().filter_map(|e| e.ok());
158+
pub fn find_file_in_root_dir(
159+
&mut self,
160+
name: &str,
161+
buffer: &mut dyn AlignedBuffer,
162+
) -> Option<File> {
163+
let mut root_entries = self.read_root_dir(buffer).filter_map(|e| e.ok());
163164
let raw_entry = root_entries.find(|e| e.eq_name(name))?;
164165

165166
let entry = match raw_entry {
@@ -202,20 +203,18 @@ impl<D: Read + Seek> FileSystem<D> {
202203
}
203204
}
204205

205-
fn read_root_dir<'a>(&'a mut self) -> impl Iterator<Item = Result<RawDirectoryEntry, ()>> + 'a {
206+
fn read_root_dir<'a>(
207+
&'a mut self,
208+
buffer: &'a mut (dyn AlignedBuffer + 'a),
209+
) -> impl Iterator<Item = Result<RawDirectoryEntry, ()>> + 'a {
206210
match self.bpb.fat_type() {
207211
FatType::Fat32 => {
208212
self.bpb.root_cluster;
209213
unimplemented!();
210214
}
211215
FatType::Fat12 | FatType::Fat16 => {
212216
let root_directory_size = self.bpb.root_directory_size();
213-
static mut ROOT_DIR_BUFFER: AlignedBuffer<0x4000> = AlignedBuffer {
214-
buffer: [0; 0x4000],
215-
limit: 0x4000,
216-
};
217-
let buffer = unsafe { &mut ROOT_DIR_BUFFER };
218-
buffer.limit = root_directory_size;
217+
buffer.set_limit(root_directory_size);
219218

220219
self.disk
221220
.seek(SeekFrom::Start(self.bpb.root_directory_offset()));

Diff for: bios/second_stage/src/main.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#![no_main]
33

44
use byteorder::{ByteOrder, LittleEndian};
5-
use core::{arch::asm, fmt::Write as _, mem::size_of, slice};
5+
use core::{fmt::Write as _, slice};
6+
use disk::AlignedArrayBuffer;
67
use mbr_nostd::{PartitionTableEntry, PartitionType};
78

89
use crate::protected_mode::enter_unreal_mode;
@@ -24,6 +25,11 @@ fn second_stage_end() -> *const u8 {
2425
unsafe { &_second_stage_end }
2526
}
2627

28+
static mut DISK_BUFFER: AlignedArrayBuffer<0x4000> = AlignedArrayBuffer {
29+
buffer: [0; 0x4000],
30+
limit: 0x4000,
31+
};
32+
2733
#[no_mangle]
2834
#[link_section = ".start"]
2935
pub extern "C" fn _start(disk_number: u16, partition_table_start: *const u8) {
@@ -71,8 +77,10 @@ pub extern "C" fn _start(disk_number: u16, partition_table_start: *const u8) {
7177

7278
let mut fs = fat::FileSystem::parse(disk.clone());
7379

80+
let disk_buffer = unsafe { &mut DISK_BUFFER };
81+
7482
let kernel = fs
75-
.find_file_in_root_dir("kernel-x86_64")
83+
.find_file_in_root_dir("kernel-x86_64", disk_buffer)
7684
.expect("no `kernel-x86_64` file found");
7785

7886
for cluster in fs.file_clusters(&kernel) {

0 commit comments

Comments
 (0)