morpheus_bootloader/uefi/file_system/
protocols.rs

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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// UEFI Simple File System Protocol implementation

use crate::BootServices;

// Protocol GUIDs
pub const SIMPLE_FILE_SYSTEM_PROTOCOL_GUID: [u8; 16] = [
    0x22, 0x5b, 0x4e, 0x96, 0x59, 0x64, 0xd2, 0x11, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b,
];

pub const LOADED_IMAGE_PROTOCOL_GUID: [u8; 16] = [
    0xa1, 0x31, 0x1b, 0x5b, 0x62, 0x95, 0xd2, 0x11, 0x8e, 0x3f, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b,
];

// EFI_FILE_INFO_ID: 09576e92-6d3f-11d2-8e39-00a0c969723b
pub const FILE_INFO_GUID: [u8; 16] = [
    0x92, 0x6e, 0x57, 0x09, 0x3f, 0x6d, 0xd2, 0x11, 0x8e, 0x39, 0x00, 0xa0, 0xc9, 0x69, 0x72, 0x3b,
];

// File attributes
pub const EFI_FILE_MODE_READ: u64 = 0x0000000000000001;
pub const EFI_FILE_MODE_WRITE: u64 = 0x0000000000000002;
pub const EFI_FILE_MODE_CREATE: u64 = 0x8000000000000000;

pub const EFI_FILE_DIRECTORY: u64 = 0x0000000000000010;

#[repr(C)]
pub struct LoadedImageProtocol {
    revision: u32,
    parent_handle: *mut (),
    system_table: *mut (),
    pub device_handle: *mut (),
    file_path: *mut (),
    _reserved: *mut (),
    load_options_size: u32,
    load_options: *mut (),
    pub image_base: *mut (),
    pub image_size: u64,
    image_code_type: u32,
    image_data_type: u32,
    unload: usize,
}

#[repr(C)]
pub struct SimpleFileSystemProtocol {
    revision: u64,
    pub open_volume: extern "efiapi" fn(
        this: *mut SimpleFileSystemProtocol,
        root: *mut *mut FileProtocol,
    ) -> usize,
}

#[repr(C)]
pub struct FileProtocol {
    revision: u64,
    pub open: extern "efiapi" fn(
        this: *mut FileProtocol,
        new_handle: *mut *mut FileProtocol,
        file_name: *const u16,
        open_mode: u64,
        attributes: u64,
    ) -> usize,
    pub close: extern "efiapi" fn(this: *mut FileProtocol) -> usize,
    pub delete: extern "efiapi" fn(this: *mut FileProtocol) -> usize,
    pub read: extern "efiapi" fn(
        this: *mut FileProtocol,
        buffer_size: *mut usize,
        buffer: *mut u8,
    ) -> usize,
    pub write: extern "efiapi" fn(
        this: *mut FileProtocol,
        buffer_size: *mut usize,
        buffer: *const u8,
    ) -> usize,
    pub get_position: usize,
    pub set_position: extern "efiapi" fn(this: *mut FileProtocol, position: u64) -> usize,
    pub get_info: usize,
    pub set_info: usize,
    pub flush: extern "efiapi" fn(this: *mut FileProtocol) -> usize,
}

/// Get Simple File System Protocol for a disk handle
pub unsafe fn get_file_system_protocol(
    bs: &BootServices,
    disk_handle: *mut (),
) -> Result<*mut SimpleFileSystemProtocol, ()> {
    let mut fs_protocol: *mut () = core::ptr::null_mut();

    let status = (bs.handle_protocol)(
        disk_handle,
        &SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
        &mut fs_protocol,
    );

    if status != 0 {
        return Err(());
    }

    Ok(fs_protocol as *mut SimpleFileSystemProtocol)
}

/// Open root directory of a volume
pub unsafe fn open_root_volume(
    fs_protocol: *mut SimpleFileSystemProtocol,
) -> Result<*mut FileProtocol, ()> {
    let mut root: *mut FileProtocol = core::ptr::null_mut();

    let status = ((*fs_protocol).open_volume)(fs_protocol, &mut root);

    if status != 0 {
        return Err(());
    }

    Ok(root)
}

/// Create directory (recursive if needed)
pub unsafe fn create_directory(
    root: *mut FileProtocol,
    path: &[u16], // UTF-16 path
) -> Result<*mut FileProtocol, ()> {
    let mut dir: *mut FileProtocol = core::ptr::null_mut();

    let status = ((*root).open)(
        root,
        &mut dir,
        path.as_ptr(),
        EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
        EFI_FILE_DIRECTORY,
    );

    if status != 0 {
        return Err(());
    }

    Ok(dir)
}

/// Create or open a file for writing
pub unsafe fn create_file(
    root: *mut FileProtocol,
    path: &[u16], // UTF-16 path
) -> Result<*mut FileProtocol, ()> {
    let mut file: *mut FileProtocol = core::ptr::null_mut();

    let status = ((*root).open)(
        root,
        &mut file,
        path.as_ptr(),
        EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | EFI_FILE_MODE_CREATE,
        0, // Regular file, no special attributes
    );

    if status != 0 {
        return Err(());
    }

    Ok(file)
}

/// Write data to a file
pub unsafe fn write_file(file: *mut FileProtocol, data: &[u8]) -> Result<(), ()> {
    let mut size = data.len();

    let status = ((*file).write)(file, &mut size, data.as_ptr());

    if status != 0 || size != data.len() {
        return Err(());
    }

    Ok(())
}

/// Flush file buffers to disk
pub unsafe fn flush_file(file: *mut FileProtocol) -> Result<(), ()> {
    let status = ((*file).flush)(file);

    if status != 0 {
        return Err(());
    }

    Ok(())
}

/// Close a file handle
pub unsafe fn close_file(file: *mut FileProtocol) -> Result<(), ()> {
    let status = ((*file).close)(file);

    if status != 0 {
        return Err(());
    }

    Ok(())
}