morpheus_bootloader/tui/distro_downloader/
manifest_io.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//! Manifest I/O operations
//!
//! Handles reading and writing ISO manifests to/from the ESP filesystem.
//! Manifests are stored at `/.iso/<CRC32-hash>.MFS`.
//!
//! # Storage Layout
//!
//! ```text
//! ESP:/
//! └── .iso/
//!     ├── A1B2C3D4.MFS    (e.g., for tails-6.10.iso)
//!     ├── E5F6A7B8.MFS    (e.g., for ubuntu-24.04.iso)
//!     └── ...
//! ```

use crate::uefi::file_system::{
    ascii_to_utf16, close_file, create_directory, create_file, flush_file, get_loaded_image,
    open_file_read, write_file, FileProtocol, EFI_FILE_MODE_READ,
};
use crate::BootServices;
use alloc::string::String;
use alloc::vec::Vec;
use morpheus_core::iso::{IsoManifest, IsoStorageManager, MAX_MANIFEST_SIZE};

/// Manifest directory path on ESP (without leading backslash for open)
const MANIFEST_DIR: &str = "\\.iso";

/// Maximum number of manifests to scan
const MAX_MANIFESTS: usize = 16;

/// Result type for manifest I/O operations
pub type ManifestIoResult<T> = Result<T, ManifestIoError>;

/// Manifest I/O error types
#[derive(Debug, Clone, Copy)]
pub enum ManifestIoError {
    /// Failed to get ESP root
    EspAccessFailed,
    /// Failed to create directory
    DirectoryCreateFailed,
    /// Failed to create/open file
    FileCreateFailed,
    /// Failed to write file
    WriteFailed,
    /// Failed to read file
    ReadFailed,
    /// Failed to serialize manifest
    SerializeFailed,
    /// Failed to deserialize manifest
    DeserializeFailed,
    /// File not found
    NotFound,
}

/// Persist a manifest to the ESP filesystem
///
/// Creates the manifest directory if it doesn't exist, then writes
/// the serialized manifest to `/.iso/<CRC32-hash>.MFS`.
///
/// # Arguments
/// * `bs` - UEFI Boot Services
/// * `image_handle` - Current image handle
/// * `manifest` - The manifest to persist
///
/// # Returns
/// * `Ok(())` on success
/// * `Err(ManifestIoError)` on failure
pub unsafe fn persist_manifest(
    bs: &BootServices,
    image_handle: *mut (),
    manifest: &IsoManifest,
) -> ManifestIoResult<()> {
    // Get ESP root
    let root = get_esp_root(bs, image_handle)?;

    // Ensure .iso directory exists on ESP root
    let mut iso_path = [0u16; 32];
    ascii_to_utf16("\\.iso", &mut iso_path);
    let _ = create_directory(root, &iso_path); // Ignore error if exists

    // Build manifest filename using 8.3 compatible hash: <CRC32>.MFS
    // This matches the format used by network post-EBS code
    let name = manifest.name_str();
    let manifest_filename = morpheus_core::fs::generate_8_3_manifest_name(name);
    let mut filename = String::new();
    filename.push_str("\\.iso\\");
    filename.push_str(&manifest_filename);

    // Convert to UTF-16
    let mut path_utf16 = [0u16; 128];
    ascii_to_utf16(&filename, &mut path_utf16);

    // Create/open the manifest file
    let file = create_file(root, &path_utf16).map_err(|_| ManifestIoError::FileCreateFailed)?;

    // Serialize manifest
    let mut buffer = [0u8; MAX_MANIFEST_SIZE];
    let size = manifest
        .serialize(&mut buffer)
        .map_err(|_| ManifestIoError::SerializeFailed)?;

    // Write to file
    write_file(file, &buffer[..size]).map_err(|_| ManifestIoError::WriteFailed)?;

    // Flush and close
    let _ = flush_file(file);
    let _ = close_file(file);
    let _ = close_file(root);

    morpheus_core::logger::log("Manifest persisted to ESP");

    Ok(())
}

/// Load all manifests from ESP and populate a storage manager
///
/// Scans `/.iso/` for .MFS manifest files and loads them.
///
/// # Arguments
/// * `bs` - UEFI Boot Services
/// * `image_handle` - Current image handle
/// * `storage` - Storage manager to populate
///
/// # Returns
/// * `Ok(count)` - Number of manifests loaded
/// * `Err(ManifestIoError)` on failure
pub unsafe fn load_manifests_from_esp(
    bs: &BootServices,
    image_handle: *mut (),
    storage: &mut IsoStorageManager,
) -> ManifestIoResult<usize> {
    // Get ESP root
    let root = get_esp_root(bs, image_handle)?;

    // Open manifest directory
    let mut dir_path = [0u16; 32];
    ascii_to_utf16("\\.iso", &mut dir_path);

    let mut dir: *mut FileProtocol = core::ptr::null_mut();
    let status = ((*root).open)(root, &mut dir, dir_path.as_ptr(), EFI_FILE_MODE_READ, 0);

    if status != 0 || dir.is_null() {
        let _ = close_file(root);
        // Directory doesn't exist yet - that's OK, no manifests
        morpheus_core::logger::log("Manifest dir not found or cannot open");
        return Ok(0);
    }

    morpheus_core::logger::log("Scanning manifest directory...");

    // Scan directory for .manifest files
    let mut count = 0;
    let mut buffer = [0u8; 512]; // For directory entry

    loop {
        let mut size = buffer.len();
        let status = ((*dir).read)(dir, &mut size, buffer.as_mut_ptr());

        if status != 0 || size == 0 {
            break; // End of directory
        }

        // Parse EFI_FILE_INFO structure
        // Offset 0x50 (80) is where filename starts in EFI_FILE_INFO
        // Attribute is at offset 0x48 (72) for directory flag
        if size < 82 {
            continue;
        }

        let attributes = u64::from_le_bytes([
            buffer[0x48],
            buffer[0x49],
            buffer[0x4A],
            buffer[0x4B],
            buffer[0x4C],
            buffer[0x4D],
            buffer[0x4E],
            buffer[0x4F],
        ]);

        // Skip directories (attribute bit 4 = EFI_FILE_DIRECTORY)
        if attributes & 0x10 != 0 {
            continue;
        }

        // Get filename from UTF-16 at offset 0x50
        let filename = extract_filename_from_file_info(&buffer);

        // Debug: log each file found
        morpheus_core::logger::log(alloc::format!("Found file: {}", filename).leak());

        // Check if it ends with .MFS or .manifest (support both, case insensitive)
        let filename_upper = filename.to_uppercase();
        if !filename_upper.ends_with(".MFS") && !filename_upper.ends_with(".MANIFEST") {
            morpheus_core::logger::log("  -> Not a manifest file, skipping");
            continue;
        }

        morpheus_core::logger::log(alloc::format!("Loading manifest: {}", filename).leak());

        // Load this manifest
        match load_single_manifest(root, &filename) {
            Ok(manifest) => {
                morpheus_core::logger::log(
                    alloc::format!(
                        "  -> Loaded OK: name='{}', size={}, flags=0x{:02x}",
                        manifest.name_str(),
                        manifest.total_size,
                        manifest.flags
                    )
                    .leak(),
                );
                if storage.add_entry(manifest).is_ok() {
                    count += 1;
                    morpheus_core::logger::log("  -> Added to storage");
                    if count >= MAX_MANIFESTS {
                        break;
                    }
                } else {
                    morpheus_core::logger::log("  -> Failed to add to storage");
                }
            }
            Err(e) => {
                morpheus_core::logger::log(alloc::format!("  -> FAILED to load: {:?}", e).leak());
            }
        }
    }

    let _ = close_file(dir);
    let _ = close_file(root);

    morpheus_core::logger::log(alloc::format!("Loaded {} manifests from ESP", count).leak());

    Ok(count)
}

/// Load a single manifest file by name
unsafe fn load_single_manifest(
    root: *mut FileProtocol,
    filename: &str,
) -> ManifestIoResult<IsoManifest> {
    // Build full path
    let mut full_path = String::new();
    full_path.push_str("\\.iso\\");
    full_path.push_str(filename);

    // Convert to UTF-16
    let mut path_utf16 = [0u16; 128];
    ascii_to_utf16(&full_path, &mut path_utf16);

    // Open file
    let mut file: *mut FileProtocol = core::ptr::null_mut();
    let status = ((*root).open)(root, &mut file, path_utf16.as_ptr(), EFI_FILE_MODE_READ, 0);

    if status != 0 || file.is_null() {
        return Err(ManifestIoError::NotFound);
    }

    // Read manifest data
    let mut buffer = [0u8; MAX_MANIFEST_SIZE];
    let mut size = buffer.len();
    let status = ((*file).read)(file, &mut size, buffer.as_mut_ptr());

    let _ = close_file(file);

    if status != 0 || size == 0 {
        return Err(ManifestIoError::ReadFailed);
    }

    // Deserialize
    IsoManifest::deserialize(&buffer[..size]).map_err(|_| ManifestIoError::DeserializeFailed)
}

/// Get ESP root directory handle
unsafe fn get_esp_root(
    bs: &BootServices,
    image_handle: *mut (),
) -> ManifestIoResult<*mut FileProtocol> {
    // Get loaded image to find device
    let loaded_image =
        get_loaded_image(bs, image_handle).map_err(|_| ManifestIoError::EspAccessFailed)?;

    let device_handle = (*loaded_image).device_handle;

    // Get filesystem protocol
    let mut fs_protocol: *mut () = core::ptr::null_mut();
    let status = (bs.handle_protocol)(
        device_handle,
        &crate::uefi::file_system::SIMPLE_FILE_SYSTEM_PROTOCOL_GUID,
        &mut fs_protocol,
    );

    if status != 0 || fs_protocol.is_null() {
        return Err(ManifestIoError::EspAccessFailed);
    }

    let fs = fs_protocol as *mut crate::uefi::file_system::SimpleFileSystemProtocol;

    // Open root volume
    let mut root: *mut FileProtocol = core::ptr::null_mut();
    let status = ((*fs).open_volume)(fs, &mut root);

    if status != 0 || root.is_null() {
        return Err(ManifestIoError::EspAccessFailed);
    }

    Ok(root)
}

/// Extract filename from EFI_FILE_INFO buffer (UTF-16 at offset 0x50)
fn extract_filename_from_file_info(buffer: &[u8]) -> String {
    let mut filename = String::new();
    let mut offset = 0x50; // FileName starts at offset 80

    while offset + 1 < buffer.len() {
        let c = u16::from_le_bytes([buffer[offset], buffer[offset + 1]]);
        if c == 0 {
            break;
        }
        if let Some(ch) = char::from_u32(c as u32) {
            filename.push(ch);
        }
        offset += 2;
    }

    filename
}

/// Delete a manifest file from ESP
///
/// # Arguments
/// * `bs` - UEFI Boot Services
/// * `image_handle` - Current image handle  
/// * `name` - ISO name (manifest filename will be generated as CRC32 hash)
pub unsafe fn delete_manifest(
    bs: &BootServices,
    image_handle: *mut (),
    name: &str,
) -> ManifestIoResult<()> {
    let root = get_esp_root(bs, image_handle)?;

    // Build manifest filename using 8.3 compatible hash
    let manifest_filename = morpheus_core::fs::generate_8_3_manifest_name(name);
    let mut filename = String::new();
    filename.push_str("\\.iso\\");
    filename.push_str(&manifest_filename);

    // Convert to UTF-16
    let mut path_utf16 = [0u16; 128];
    ascii_to_utf16(&filename, &mut path_utf16);

    // Open file
    let mut file: *mut FileProtocol = core::ptr::null_mut();
    let status = ((*root).open)(
        root,
        &mut file,
        path_utf16.as_ptr(),
        EFI_FILE_MODE_READ | crate::uefi::file_system::EFI_FILE_MODE_WRITE,
        0,
    );

    if status != 0 || file.is_null() {
        let _ = close_file(root);
        return Err(ManifestIoError::NotFound);
    }

    // Delete the file
    let status = ((*file).delete)(file);
    // Note: delete() closes the handle on success or failure

    let _ = close_file(root);

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

    Ok(())
}