Skip to content

Commit 661b478

Browse files
authored
Merge pull request #90 from rust-osdev/stricter-typing
Stricter typing + better split of modules + bugfix
2 parents 8dcc7f5 + fb92f56 commit 661b478

File tree

9 files changed

+103
-87
lines changed

9 files changed

+103
-87
lines changed

src/boot_loader_name.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use crate::TagType;
2+
13
/// This Tag contains the name of the bootloader that is booting the kernel.
24
///
35
/// The name is a normal C-style UTF-8 zero-terminated string that can be
46
/// obtained via the `name` method.
57
#[derive(Clone, Copy, Debug)]
68
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
79
pub struct BootLoaderNameTag {
8-
typ: u32,
10+
typ: TagType,
911
size: u32,
1012
string: u8,
1113
}

src/command_line.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
use crate::TagType;
2+
13
/// This Tag contains the command line string.
24
///
35
/// The string is a normal C-style UTF-8 zero-terminated string that can be
46
/// obtained via the `command_line` method.
57
#[derive(Clone, Copy, Debug)]
68
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
79
pub struct CommandLineTag {
8-
typ: u32,
10+
typ: TagType,
911
size: u32,
1012
string: u8,
1113
}

src/efi.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//! All MBI tags related to (U)EFI.
2+
3+
use crate::TagType;
4+
5+
/// EFI system table in 32 bit mode
6+
#[derive(Clone, Copy, Debug)]
7+
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
8+
pub struct EFISdt32 {
9+
typ: TagType,
10+
size: u32,
11+
pointer: u32,
12+
}
13+
14+
impl EFISdt32 {
15+
/// The Physical address of a i386 EFI system table.
16+
pub fn sdt_address(&self) -> usize {
17+
self.pointer as usize
18+
}
19+
}
20+
21+
/// EFI system table in 64 bit mode
22+
#[derive(Clone, Copy, Debug)]
23+
#[repr(C)]
24+
pub struct EFISdt64 {
25+
typ: TagType,
26+
size: u32,
27+
pointer: u64,
28+
}
29+
30+
impl EFISdt64 {
31+
/// The Physical address of a x86_64 EFI system table.
32+
pub fn sdt_address(&self) -> usize {
33+
self.pointer as usize
34+
}
35+
}
36+
37+
/// Contains pointer to boot loader image handle.
38+
#[derive(Debug)]
39+
#[repr(C)]
40+
pub struct EFIImageHandle32 {
41+
typ: TagType,
42+
size: u32,
43+
pointer: u32,
44+
}
45+
46+
/// Contains pointer to boot loader image handle.
47+
#[derive(Debug)]
48+
#[repr(C)]
49+
pub struct EFIImageHandle64 {
50+
typ: TagType,
51+
size: u32,
52+
pointer: u64,
53+
}

src/image_load_addr.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use crate::TagType;
2+
3+
/// If the image has relocatable header tag, this tag contains the image's
4+
/// base physical address.
5+
#[derive(Debug)]
6+
#[repr(C)]
7+
pub struct ImageLoadPhysAddr {
8+
typ: TagType,
9+
size: u32,
10+
load_base_addr: u32,
11+
}

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ pub use memory_map::{
3939
};
4040
pub use module::{ModuleIter, ModuleTag};
4141
pub use rsdp::{
42-
EFIImageHandle32, EFIImageHandle64, EFISdt32, EFISdt64, ImageLoadPhysAddr, RsdpV1Tag, RsdpV2Tag,
42+
RsdpV1Tag, RsdpV2Tag,
4343
};
44+
pub use image_load_addr::ImageLoadPhysAddr;
45+
pub use efi::{EFIImageHandle32, EFIImageHandle64, EFISdt32, EFISdt64};
4446
pub use vbe_info::{
4547
VBECapabilities, VBEControlInfo, VBEDirectColorAttributes, VBEField, VBEInfoTag,
4648
VBEMemoryModel, VBEModeAttributes, VBEModeInfo, VBEWindowAttributes,
@@ -58,6 +60,8 @@ mod memory_map;
5860
mod module;
5961
mod rsdp;
6062
mod vbe_info;
63+
mod efi;
64+
mod image_load_addr;
6165

6266
/// Load the multiboot boot information struct from an address.
6367
///

src/memory_map.rs

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use core::marker::PhantomData;
2+
use crate::TagType;
23

34
/// This Tag provides an initial host memory map.
45
///
@@ -13,7 +14,7 @@ use core::marker::PhantomData;
1314
#[derive(Debug)]
1415
#[repr(C)]
1516
pub struct MemoryMapTag {
16-
typ: u32,
17+
typ: TagType,
1718
size: u32,
1819
entry_size: u32,
1920
entry_version: u32,
@@ -23,7 +24,7 @@ pub struct MemoryMapTag {
2324
impl MemoryMapTag {
2425
/// Return an iterator over all AVAILABLE marked memory areas.
2526
pub fn memory_areas(&self) -> impl Iterator<Item = &MemoryArea> {
26-
self.all_memory_areas().filter(|entry| entry.typ == 1)
27+
self.all_memory_areas().filter(|entry| matches!(entry.typ, MemoryAreaType::Available))
2728
}
2829

2930
/// Return an iterator over all marked memory areas.
@@ -45,7 +46,7 @@ impl MemoryMapTag {
4546
pub struct MemoryArea {
4647
base_addr: u64,
4748
length: u64,
48-
typ: u32,
49+
typ: MemoryAreaType,
4950
_reserved: u32,
5051
}
5152

@@ -67,33 +68,32 @@ impl MemoryArea {
6768

6869
/// The type of the memory region.
6970
pub fn typ(&self) -> MemoryAreaType {
70-
match self.typ {
71-
1 => MemoryAreaType::Available,
72-
3 => MemoryAreaType::AcpiAvailable,
73-
4 => MemoryAreaType::ReservedHibernate,
74-
5 => MemoryAreaType::Defective,
75-
_ => MemoryAreaType::Reserved,
76-
}
71+
self.typ
7772
}
7873
}
7974

8075
/// An enum of possible reported region types.
81-
#[derive(Debug, PartialEq, Eq)]
76+
/// Inside the Multiboot2 spec this is kind of hidden
77+
/// inside the implementation of `struct multiboot_mmap_entry`.
78+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
79+
#[repr(u32)]
8280
pub enum MemoryAreaType {
83-
/// A reserved area that must not be used.
84-
Reserved,
85-
8681
/// Available memory free to be used by the OS.
87-
Available,
82+
Available = 1,
83+
84+
/// A reserved area that must not be used.
85+
Reserved = 2,
8886

8987
/// Usable memory holding ACPI information.
90-
AcpiAvailable,
88+
AcpiAvailable = 3,
9189

9290
/// Reserved memory which needs to be preserved on hibernation.
93-
ReservedHibernate,
91+
/// Also called NVS in spec, which stands for "Non-Volatile Sleep/Storage",
92+
/// which is part of ACPI specification.
93+
ReservedHibernate = 4,
9494

9595
/// Memory which is occupied by defective RAM modules.
96-
Defective,
96+
Defective = 5,
9797
}
9898

9999
/// An iterator over all memory areas
@@ -122,7 +122,7 @@ impl<'a> Iterator for MemoryAreaIter<'a> {
122122
#[derive(Debug)]
123123
#[repr(C)]
124124
pub struct EFIMemoryMapTag {
125-
typ: u32,
125+
typ: TagType,
126126
size: u32,
127127
desc_size: u32,
128128
desc_version: u32,

src/module.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use core::fmt::{Debug, Formatter};
66
#[derive(Clone, Copy)]
77
#[repr(C, packed)] // only repr(C) would add unwanted padding near name_byte.
88
pub struct ModuleTag {
9-
typ: u32,
9+
typ: TagType,
1010
size: u32,
1111
mod_start: u32,
1212
mod_end: u32,

src/rsdp.rs

Lines changed: 6 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,24 @@
1+
//! Module for RSDP/ACPI. RSDP (Root System Description Pointer) is a data structure used in the
2+
//! ACPI programming interface.
3+
//!
14
//! The tag that the bootloader passes will depend on the ACPI version the hardware supports.
25
//! For ACPI Version 1.0, a `RsdpV1Tag` will be provided, which can be accessed from
36
//! `BootInformation` using the `rsdp_v1_tag` function. For subsequent versions of ACPI, a
47
//! `RsdpV2Tag` will be provided, which can be accessed with `rsdp_v2_tag`.
58
//!
69
//! Even though the bootloader should give the address of the real RSDP/XSDT, the checksum and
710
//! signature should be manually verified.
8-
911
use core::slice;
1012
use core::str;
13+
use crate::TagType;
1114

1215
const RSDPV1_LENGTH: usize = 20;
1316

14-
/// EFI system table in 32 bit mode
15-
#[derive(Clone, Copy, Debug)]
16-
#[repr(C, packed)] // only repr(C) would add unwanted padding before first_section
17-
pub struct EFISdt32 {
18-
typ: u32,
19-
size: u32,
20-
pointer: u32,
21-
}
22-
23-
impl EFISdt32 {
24-
/// The Physical address of a i386 EFI system table.
25-
pub fn sdt_address(&self) -> usize {
26-
self.pointer as usize
27-
}
28-
}
29-
30-
/// EFI system table in 64 bit mode
31-
#[derive(Clone, Copy, Debug)]
32-
#[repr(C)]
33-
pub struct EFISdt64 {
34-
typ: u32,
35-
size: u32,
36-
pointer: u64,
37-
}
38-
39-
impl EFISdt64 {
40-
/// The Physical address of a x86_64 EFI system table.
41-
pub fn sdt_address(&self) -> usize {
42-
self.pointer as usize
43-
}
44-
}
45-
46-
/// Contains pointer to boot loader image handle.
47-
#[derive(Debug)]
48-
#[repr(C)]
49-
pub struct EFIImageHandle32 {
50-
typ: u32,
51-
size: u32,
52-
pointer: u32,
53-
}
54-
55-
/// Contains pointer to boot loader image handle.
56-
#[derive(Debug)]
57-
#[repr(C)]
58-
pub struct EFIImageHandle64 {
59-
typ: u32,
60-
size: u32,
61-
pointer: u64,
62-
}
63-
64-
/// If the image has relocatable header tag, this tag contains the image's
65-
/// base physical address.
66-
#[derive(Debug)]
67-
#[repr(C)]
68-
pub struct ImageLoadPhysAddr {
69-
typ: u32,
70-
size: u32,
71-
load_base_addr: u32,
72-
}
73-
7417
/// This tag contains a copy of RSDP as defined per ACPI 1.0 specification.
7518
#[derive(Clone, Copy, Debug)]
7619
#[repr(C, packed)]
7720
pub struct RsdpV1Tag {
78-
typ: u32,
21+
typ: TagType,
7922
size: u32,
8023
signature: [u8; 8],
8124
checksum: u8,
@@ -122,7 +65,7 @@ impl RsdpV1Tag {
12265
#[derive(Clone, Copy, Debug)]
12366
#[repr(C, packed)]
12467
pub struct RsdpV2Tag {
125-
typ: u32,
68+
typ: TagType,
12669
size: u32,
12770
signature: [u8; 8],
12871
checksum: u8,

src/vbe_info.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use core::fmt;
2+
use crate::TagType;
23

34
/// This tag contains VBE metadata, VBE controller information returned by the
45
/// VBE Function 00h and VBE mode information returned by the VBE Function 01h.
56
#[derive(Debug, Copy, Clone)]
67
#[repr(C, packed)]
78
pub struct VBEInfoTag {
8-
typ: u32,
9+
typ: TagType,
910
length: u32,
1011

1112
/// Indicates current video mode in the format specified in VBE 3.0.

0 commit comments

Comments
 (0)