Skip to content

Commit 535a1cc

Browse files
authored
Merge pull request #547 from rust-osdev/fix/large-fat16
don't use BPB_TotSec16 to determine FAT type
2 parents 1d41661 + afdebf5 commit 535a1cc

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

bios/stage-2/src/fat.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,14 @@ impl Bpb {
4848
let root_cluster;
4949
let fat_size_32;
5050

51-
if (total_sectors_16 == 0) && (total_sectors_32 != 0) {
51+
if fat_size_16 == 0 {
5252
// FAT32
5353
fat_size_32 = u32::from_le_bytes(raw[36..40].try_into().unwrap());
5454
root_cluster = u32::from_le_bytes(raw[44..48].try_into().unwrap());
55-
} else if (total_sectors_16 != 0) && (total_sectors_32 == 0) {
55+
} else {
5656
// FAT12 or FAT16
5757
fat_size_32 = 0;
5858
root_cluster = 0;
59-
} else {
60-
panic!("ExactlyOneTotalSectorsFieldMustBeZero");
6159
}
6260

6361
Self {

tests/ramdisk.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::path::Path;
22

33
use bootloader_test_runner::run_test_kernel_with_ramdisk;
4+
use tempfile::NamedTempFile;
5+
46
static RAMDISK_PATH: &str = "tests/ramdisk.txt";
57

68
#[test]
@@ -26,3 +28,15 @@ fn memory_map() {
2628
Some(Path::new(RAMDISK_PATH)),
2729
);
2830
}
31+
32+
#[test]
33+
fn large_ramdisk() {
34+
// Create a large file to act as the RAM disk.
35+
let ramdisk = NamedTempFile::new().unwrap();
36+
ramdisk.as_file().set_len(1024 * 1024 * 16).unwrap();
37+
38+
run_test_kernel_with_ramdisk(
39+
env!("CARGO_BIN_FILE_TEST_KERNEL_RAMDISK_large_ramdisk"),
40+
Some(ramdisk.as_ref()),
41+
);
42+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#![no_std] // don't link the Rust standard library
2+
#![no_main] // disable all Rust-level entry points
3+
4+
use bootloader_api::{BootInfo, entry_point};
5+
use core::fmt::Write;
6+
use test_kernel_ramdisk::{QemuExitCode, exit_qemu, serial};
7+
8+
entry_point!(kernel_main);
9+
10+
fn kernel_main(boot_info: &'static mut BootInfo) -> ! {
11+
writeln!(serial(), "Boot info: {boot_info:?}").unwrap();
12+
assert!(boot_info.ramdisk_addr.into_option().is_some());
13+
writeln!(serial(), "RAM disk size: {}", boot_info.ramdisk_len).unwrap();
14+
15+
exit_qemu(QemuExitCode::Success);
16+
}
17+
18+
/// This function is called on panic.
19+
#[cfg(not(test))]
20+
#[panic_handler]
21+
fn panic(info: &core::panic::PanicInfo) -> ! {
22+
let _ = writeln!(test_kernel_ramdisk::serial(), "PANIC: {info}");
23+
exit_qemu(QemuExitCode::Failed);
24+
}

0 commit comments

Comments
 (0)