morpheus_bootloader/tui/distro_launcher/
boot.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
use super::entry::BootEntry;
use super::ui::DistroLauncher;
use crate::boot::loader::BootError;
use crate::tui::input::Keyboard;
use crate::tui::renderer::{
    Screen, EFI_BLACK, EFI_CYAN, EFI_DARKGREEN, EFI_GREEN, EFI_LIGHTGREEN, EFI_RED, EFI_YELLOW,
};
use crate::uefi::file_system::FileProtocol;
use alloc::format;
use alloc::string::String;
use alloc::vec::Vec;

const MAX_KERNEL_BYTES: usize = 64 * 1024 * 1024;
const PAGE_SIZE: usize = 4096;

impl DistroLauncher {
    pub(super) fn boot_entry(
        &self,
        screen: &mut Screen,
        keyboard: &mut Keyboard,
        boot_services: &crate::BootServices,
        system_table: *mut (),
        image_handle: *mut (),
        entry: &BootEntry,
    ) {
        screen.clear();

        if entry.cmdline.starts_with("iso:") {
            self.boot_from_iso(
                screen,
                keyboard,
                boot_services,
                system_table,
                image_handle,
                entry,
            );
            return;
        }

        if entry.cmdline.starts_with("chunked_iso:") {
            self.boot_from_chunked_iso(
                screen,
                keyboard,
                boot_services,
                system_table,
                image_handle,
                entry,
            );
            return;
        }

        screen.put_str_at(5, 10, "Loading kernel...", EFI_LIGHTGREEN, EFI_BLACK);

        morpheus_core::logger::log("kernel read start");
        let (kernel_ptr, kernel_size) = match Self::read_file_to_uefi_pages(
            boot_services,
            image_handle,
            &entry.kernel_path,
            screen,
            MAX_KERNEL_BYTES,
            12,
        ) {
            Ok((ptr, size)) => {
                morpheus_core::logger::log("kernel read ok");
                (ptr, size)
            }
            Err(e) => {
                screen.put_str_at(5, 18, "ERROR: Failed to read kernel", EFI_RED, EFI_BLACK);
                morpheus_core::logger::log("kernel read failed");
                Self::dump_logs_to_screen(screen);
                keyboard.wait_for_key();
                return;
            }
        };

        // Load initrd using same GRUB-style approach
        morpheus_core::logger::log("BEFORE initrd match");
        let (initrd_ptr, initrd_size) = match &entry.initrd_path {
            Some(path) => {
                morpheus_core::logger::log("initrd path found");

                match Self::read_file_to_uefi_pages(
                    boot_services,
                    image_handle,
                    path,
                    screen,
                    512 * 1024 * 1024,
                    19,
                ) {
                    Ok((ptr, size)) => {
                        morpheus_core::logger::log("initrd read ok");
                        (Some(ptr), size)
                    }
                    Err(e) => {
                        screen.put_str_at(
                            5,
                            25,
                            "ERROR: Failed to read initrd",
                            EFI_RED,
                            EFI_BLACK,
                        );
                        morpheus_core::logger::log("failed to read initrd");
                        Self::dump_logs_to_screen(screen);
                        keyboard.wait_for_key();
                        return;
                    }
                }
            }
            None => {
                screen.put_str_at(5, 19, "No initrd found", EFI_DARKGREEN, EFI_BLACK);
                morpheus_core::logger::log("initrd not found");
                (None, 0)
            }
        };

        screen.put_str_at(5, 18, "Booting......", EFI_LIGHTGREEN, EFI_BLACK);

        // Boot the kernel - convert raw pointers to slices (GRUB style)
        let boot_result = unsafe {
            // Clear screen area for logs
            for i in 18..30 {
                screen.put_str_at(
                    5,
                    i,
                    "                                                    ",
                    EFI_BLACK,
                    EFI_BLACK,
                );
            }

            // Convert pointers to slices for boot call
            let kernel_slice = core::slice::from_raw_parts(kernel_ptr, kernel_size);
            let initrd_slice = initrd_ptr.map(|ptr| core::slice::from_raw_parts(ptr, initrd_size));

            crate::boot::loader::boot_linux_kernel(
                boot_services,
                system_table,
                image_handle,
                kernel_slice,
                initrd_slice,
                &entry.cmdline,
                screen,
            )
        };

        if let Err(error) = boot_result {
            let detail = Self::describe_boot_error(&error);
            let msg = alloc::format!("ERROR: {}", detail);
            Self::await_failure(screen, keyboard, 18, &msg, "kernel boot failed");
        }
    }

    fn boot_from_iso(
        &self,
        screen: &mut Screen,
        keyboard: &mut Keyboard,
        boot_services: &crate::BootServices,
        system_table: *mut (),
        image_handle: *mut (),
        entry: &BootEntry,
    ) {
        use crate::tui::boot_sequence::BootSequence;
        use crate::tui::widgets::progressbar::ProgressBar;

        screen.clear();
        screen.put_str_at(5, 2, "Booting from ISO", EFI_LIGHTGREEN, EFI_BLACK);
        screen.put_str_at(5, 3, "================", EFI_GREEN, EFI_BLACK);

        let mut boot_seq = BootSequence::new();
        let mut progress_bar = ProgressBar::new(5, 5, 60, "Loading ISO:");

        morpheus_core::logger::log("ISO Boot: Starting...");
        boot_seq.render(screen, 5, 8);
        progress_bar.render(screen);

        let esp_root = match unsafe { Self::get_esp_root(boot_services, image_handle) } {
            Ok(root) => root,
            Err(_) => {
                morpheus_core::logger::log("ISO Boot: FAILED to access ESP");
                boot_seq.render(screen, 5, 8);
                keyboard.wait_for_key();
                return;
            }
        };

        let iso_path = entry.cmdline.strip_prefix("iso:").unwrap_or(&entry.cmdline);

        // Use progress callback to continuously update screen
        let mut last_log_count = morpheus_core::logger::total_log_count();
        let mut last_percent = 0usize;
        let mut progress_callback = |bytes: usize, total: usize, _msg: &str| {
            if total > 0 {
                let percent = (bytes * 100) / total;
                if percent != last_percent {
                    progress_bar.set_progress(percent);
                    progress_bar.render(screen);
                    last_percent = percent;
                }
            }

            let current_log_count = morpheus_core::logger::total_log_count();
            if current_log_count != last_log_count {
                boot_seq.render(screen, 5, 8);
                last_log_count = current_log_count;
            }
        };

        // The extract function logs its own progress
        let (kernel_data, initrd_data, cmdline) = match super::iso_boot::extract_iso_with_progress(
            iso_path,
            esp_root,
            Some(&mut progress_callback),
        ) {
            Ok(files) => {
                progress_bar.set_progress(100);
                progress_bar.render(screen);
                boot_seq.render(screen, 5, 8);
                files
            }
            Err(e) => {
                morpheus_core::logger::log(alloc::format!("ISO Boot: FAILED - {:?}", e).leak());
                boot_seq.render(screen, 5, 8);
                unsafe {
                    ((*esp_root).close)(esp_root);
                }
                keyboard.wait_for_key();
                return;
            }
        };

        unsafe {
            ((*esp_root).close)(esp_root);
        }

        morpheus_core::logger::log("ISO Boot: Launching kernel...");
        boot_seq.render(screen, 5, 8);

        let boot_result = unsafe {
            let kernel_slice = core::slice::from_raw_parts(kernel_data.as_ptr(), kernel_data.len());
            let initrd_slice = initrd_data
                .as_ref()
                .map(|d| core::slice::from_raw_parts(d.as_ptr(), d.len()));

            crate::boot::loader::boot_linux_kernel(
                boot_services,
                system_table,
                image_handle,
                kernel_slice,
                initrd_slice,
                &cmdline,
                screen,
            )
        };

        if let Err(error) = boot_result {
            let detail = Self::describe_boot_error(&error);
            let msg = alloc::format!("ERROR: {}", detail);
            Self::await_failure(screen, keyboard, 18, &msg, "iso boot failed");
        }
    }

    /// Boot from a chunked ISO stored via IsoStorageManager
    fn boot_from_chunked_iso(
        &self,
        screen: &mut Screen,
        keyboard: &mut Keyboard,
        boot_services: &crate::BootServices,
        system_table: *mut (),
        image_handle: *mut (),
        entry: &BootEntry,
    ) {
        use crate::tui::boot_sequence::BootSequence;
        use crate::tui::widgets::progressbar::ProgressBar;
        use crate::uefi::block_io_adapter::UefiBlockIo;
        use morpheus_core::iso::{IsoBlockIoAdapter, IsoStorageManager};

        screen.clear();
        screen.put_str_at(5, 2, "Booting from Chunked ISO", EFI_LIGHTGREEN, EFI_BLACK);
        screen.put_str_at(5, 3, "========================", EFI_GREEN, EFI_BLACK);

        let mut boot_seq = BootSequence::new();
        let mut progress_bar = ProgressBar::new(5, 5, 60, "Loading ISO:");
        boot_seq.render(screen, 5, 8);
        progress_bar.render(screen);

        morpheus_core::logger::log("Chunked ISO Boot: Starting...");

        // Parse index from cmdline: "chunked_iso:N"
        let idx_str = entry.cmdline.strip_prefix("chunked_iso:").unwrap_or("0");
        let iso_idx: usize = idx_str.parse().unwrap_or(0);

        morpheus_core::logger::log(format!("Chunked ISO: index={}", iso_idx).leak());

        // Get disk info
        let (esp_lba, disk_lba) = {
            let mut dm = morpheus_core::disk::manager::DiskManager::new();
            if crate::uefi::disk::enumerate_disks(boot_services, &mut dm).is_ok()
                && dm.disk_count() > 0
            {
                if let Some(disk) = dm.get_disk(0) {
                    (2048_u64, disk.last_block + 1)
                } else {
                    screen.put_str_at(5, 18, "ERROR: No disk found", EFI_RED, EFI_BLACK);
                    keyboard.wait_for_key();
                    return;
                }
            } else {
                screen.put_str_at(5, 18, "ERROR: Disk enumeration failed", EFI_RED, EFI_BLACK);
                keyboard.wait_for_key();
                return;
            }
        };

        // Get ISO read context - load manifests from ESP first
        let mut storage = IsoStorageManager::new(esp_lba, disk_lba);

        // Load persisted manifests from ESP filesystem
        if let Err(_) = unsafe {
            crate::tui::distro_downloader::manifest_io::load_manifests_from_esp(
                boot_services,
                image_handle,
                &mut storage,
            )
        } {
            morpheus_core::logger::log("Warning: Could not load manifests from ESP");
        }

        let read_ctx = match storage.get_read_context(iso_idx) {
            Ok(ctx) => ctx,
            Err(_) => {
                screen.put_str_at(5, 18, "ERROR: ISO not found in storage", EFI_RED, EFI_BLACK);
                screen.put_str_at(
                    5,
                    19,
                    &format!("Index {} not in {} ISOs", iso_idx, storage.count()),
                    EFI_YELLOW,
                    EFI_BLACK,
                );
                keyboard.wait_for_key();
                return;
            }
        };

        morpheus_core::logger::log(
            format!(
                "Chunked ISO: {} chunks, {} bytes",
                read_ctx.num_chunks, read_ctx.total_size
            )
            .leak(),
        );

        // Get block I/O protocol for first physical disk
        let block_io_protocol = match Self::get_first_disk_block_io(boot_services) {
            Some(p) => p,
            None => {
                screen.put_str_at(5, 18, "ERROR: No block I/O device", EFI_RED, EFI_BLACK);
                keyboard.wait_for_key();
                return;
            }
        };

        // Create UEFI block I/O adapter
        // SAFETY: Protocol pointer is valid from get_first_disk_block_io
        let mut uefi_block_io = unsafe { UefiBlockIo::new(block_io_protocol) };

        // Create ISO adapter that bridges chunked storage to iso9660
        let mut iso_adapter = IsoBlockIoAdapter::new(read_ctx, &mut uefi_block_io);

        // Mount ISO filesystem
        progress_bar.set_progress(10);
        progress_bar.render(screen);
        morpheus_core::logger::log("Chunked ISO: Mounting filesystem...");

        let volume = match iso9660::mount(&mut iso_adapter, 0) {
            Ok(v) => v,
            Err(e) => {
                screen.put_str_at(5, 18, "ERROR: Failed to mount ISO", EFI_RED, EFI_BLACK);
                screen.put_str_at(5, 19, &format!("{:?}", e), EFI_YELLOW, EFI_BLACK);
                keyboard.wait_for_key();
                return;
            }
        };

        progress_bar.set_progress(15);
        progress_bar.render(screen);
        morpheus_core::logger::log("Chunked ISO: Looking for kernel...");

        // Find kernel - check common paths for various distros
        // Tails: /live/vmlinuz
        // Ubuntu: /casper/vmlinuz
        // Debian: /live/vmlinuz-*
        // Fedora: /images/pxeboot/vmlinuz or /isolinux/vmlinuz
        let kernel_paths = [
            "/live/vmlinuz",             // Tails, Debian Live
            "/casper/vmlinuz",           // Ubuntu
            "/boot/vmlinuz",             // Alpine, some others
            "/isolinux/vmlinuz",         // Generic syslinux
            "/images/pxeboot/vmlinuz",   // Fedora
            "/boot/x86_64/loader/linux", // openSUSE
        ];

        let mut kernel_data = None;
        for kpath in &kernel_paths {
            if let Ok(file_entry) = iso9660::find_file(&mut iso_adapter, &volume, kpath) {
                let size_mb = file_entry.size / (1024 * 1024);
                morpheus_core::logger::log(
                    format!("Found kernel at {} ({} MB)", kpath, size_mb).leak(),
                );
                screen.put_str_at(
                    5,
                    7,
                    &format!("Loading kernel: {} ({} MB)", kpath, size_mb),
                    EFI_CYAN,
                    EFI_BLACK,
                );
                progress_bar.set_progress(20);
                progress_bar.render(screen);

                if let Ok(data) = iso9660::read_file_vec(&mut iso_adapter, &file_entry) {
                    morpheus_core::logger::log(
                        format!("Kernel loaded: {} bytes", data.len()).leak(),
                    );
                    kernel_data = Some(data);
                    break;
                }
            }
        }

        let kernel_data = match kernel_data {
            Some(d) => d,
            None => {
                screen.put_str_at(5, 18, "ERROR: No kernel found in ISO", EFI_RED, EFI_BLACK);
                keyboard.wait_for_key();
                return;
            }
        };

        progress_bar.set_progress(40);
        progress_bar.render(screen);
        morpheus_core::logger::log("Chunked ISO: Looking for initrd...");

        // Find initrd - check common paths for various distros
        // Tails: /live/initrd.img
        // Ubuntu: /casper/initrd (no extension)
        // Debian: /live/initrd.img-*
        let initrd_paths = [
            "/live/initrd.img",           // Tails, Debian Live
            "/casper/initrd",             // Ubuntu (no extension)
            "/casper/initrd.lz",          // Ubuntu compressed
            "/boot/initrd.img",           // Alpine
            "/isolinux/initrd.img",       // Generic syslinux
            "/images/pxeboot/initrd.img", // Fedora
            "/boot/x86_64/loader/initrd", // openSUSE
        ];

        let mut initrd_data = None;
        for ipath in &initrd_paths {
            if let Ok(file_entry) = iso9660::find_file(&mut iso_adapter, &volume, ipath) {
                let size_mb = file_entry.size / (1024 * 1024);
                morpheus_core::logger::log(
                    format!("Found initrd at {} ({} MB)", ipath, size_mb).leak(),
                );
                screen.put_str_at(
                    5,
                    7,
                    &format!("Loading initrd: {} ({} MB)   ", ipath, size_mb),
                    EFI_CYAN,
                    EFI_BLACK,
                );
                progress_bar.set_progress(50);
                progress_bar.render(screen);

                if let Ok(data) = iso9660::read_file_vec(&mut iso_adapter, &file_entry) {
                    morpheus_core::logger::log(
                        format!("Initrd loaded: {} bytes", data.len()).leak(),
                    );
                    initrd_data = Some(data);
                    break;
                }
            }
        }

        progress_bar.set_progress(80);
        progress_bar.render(screen);
        morpheus_core::logger::log("Chunked ISO: Preparing boot...");
        boot_seq.render(screen, 5, 8);

        // Determine cmdline based on ISO name/type
        // Tails needs specific parameters for live boot
        let iso_name = storage
            .get(iso_idx)
            .map(|e| e.manifest.name_str())
            .unwrap_or("");

        let cmdline = if iso_name.to_lowercase().contains("tails") {
            // Tails-specific: needs boot=live and findiso for squashfs
            "boot=live nopersistence noprompt timezone=Etc/UTC splash noautologin module=Tails quiet"
        } else if iso_name.to_lowercase().contains("ubuntu") {
            "boot=casper quiet splash"
        } else if iso_name.to_lowercase().contains("fedora") {
            "rd.live.image quiet"
        } else {
            // Generic live boot cmdline
            "boot=live quiet splash"
        };

        progress_bar.set_progress(100);
        progress_bar.render(screen);

        screen.put_str_at(5, 16, "Starting kernel...", EFI_LIGHTGREEN, EFI_BLACK);

        // SAFETY: Calling kernel boot with properly loaded kernel/initrd data
        let boot_result = unsafe {
            crate::boot::loader::boot_linux_kernel(
                boot_services,
                system_table,
                image_handle,
                &kernel_data,
                initrd_data.as_deref(),
                cmdline,
                screen,
            )
        };

        if let Err(error) = boot_result {
            let detail = Self::describe_boot_error(&error);
            let msg = format!("ERROR: {}", detail);
            Self::await_failure(screen, keyboard, 18, &msg, "chunked iso boot failed");
        }
    }

    /// Get BlockIoProtocol pointer for first physical disk
    fn get_first_disk_block_io(
        boot_services: &crate::BootServices,
    ) -> Option<*mut crate::uefi::block_io::BlockIoProtocol> {
        use crate::uefi::block_io::{BlockIoProtocol, EFI_BLOCK_IO_PROTOCOL_GUID};

        // Get buffer size needed for all Block I/O handles
        let mut buffer_size: usize = 0;
        let _ = (boot_services.locate_handle)(
            2, // ByProtocol
            &EFI_BLOCK_IO_PROTOCOL_GUID,
            core::ptr::null(),
            &mut buffer_size,
            core::ptr::null_mut(),
        );

        if buffer_size == 0 {
            return None;
        }

        // Allocate buffer for handles
        let mut handle_buffer: *mut u8 = core::ptr::null_mut();
        let alloc_status = (boot_services.allocate_pool)(2, buffer_size, &mut handle_buffer);

        if alloc_status != 0 || handle_buffer.is_null() {
            return None;
        }

        // Get all Block I/O handles
        let status = (boot_services.locate_handle)(
            2,
            &EFI_BLOCK_IO_PROTOCOL_GUID,
            core::ptr::null(),
            &mut buffer_size,
            handle_buffer as *mut *mut (),
        );

        if status != 0 {
            (boot_services.free_pool)(handle_buffer);
            return None;
        }

        // Iterate through handles and find physical disks
        let handles = handle_buffer as *const *mut ();
        let handle_count = buffer_size / core::mem::size_of::<*mut ()>();

        // Find first physical disk (not a partition)
        let mut result = None;
        unsafe {
            for i in 0..handle_count {
                let handle = *handles.add(i);
                let mut block_io_ptr: *mut () = core::ptr::null_mut();

                let proto_status = (boot_services.handle_protocol)(
                    handle,
                    &EFI_BLOCK_IO_PROTOCOL_GUID,
                    &mut block_io_ptr,
                );

                if proto_status == 0 && !block_io_ptr.is_null() {
                    let block_io = &*(block_io_ptr as *const BlockIoProtocol);
                    let media = &*block_io.media;

                    // Only use physical disks, not partitions
                    if !media.logical_partition && media.media_present {
                        result = Some(block_io_ptr as *mut BlockIoProtocol);
                        break;
                    }
                }
            }
        }

        (boot_services.free_pool)(handle_buffer);
        result
    }

    unsafe fn get_esp_root(
        boot_services: &crate::BootServices,
        image_handle: *mut (),
    ) -> Result<*mut FileProtocol, ()> {
        use crate::uefi::file_system::{
            get_file_system_protocol, get_loaded_image, open_root_volume,
        };

        let loaded_image = get_loaded_image(boot_services, image_handle)?;
        let device_handle = (*loaded_image).device_handle;

        let fs_protocol = get_file_system_protocol(boot_services, device_handle)?;
        open_root_volume(fs_protocol)
    }
    fn read_file_to_uefi_pages(
        boot_services: &crate::BootServices,
        image_handle: *mut (),
        path: &str,
        screen: &mut Screen,
        max_size: usize,
        start_line: usize, // where to render status messages
    ) -> Result<(*mut u8, usize), &'static str> {
        use crate::uefi::file_system::*;

        unsafe {
            let loaded_image = get_loaded_image(boot_services, image_handle).map_err(|_| {
                morpheus_core::logger::log("FAIL: get_loaded_image");
                "Failed to get loaded image"
            })?;

            let device_handle = (*loaded_image).device_handle;
            let fs_protocol =
                get_file_system_protocol(boot_services, device_handle).map_err(|_| {
                    morpheus_core::logger::log("FAIL: get_file_system_protocol");
                    "Failed to get file system protocol"
                })?;
            let root = open_root_volume(fs_protocol).map_err(|_| {
                morpheus_core::logger::log("FAIL: open_root_volume");
                "Failed to open root volume"
            })?;

            screen.put_str_at(5, start_line, "Opening file...", EFI_DARKGREEN, EFI_BLACK);

            let mut utf16_path = [0u16; 256];
            ascii_to_utf16(path, &mut utf16_path);

            let file = open_file_read(root, &utf16_path).map_err(|status| {
                morpheus_core::logger::log("FAIL: open_file_read");
                if status == 0x80000000000000 | 14 {
                    "File not found"
                } else {
                    "Failed to open file"
                }
            })?;

            screen.put_str_at(
                5,
                start_line + 1,
                "Allocating pages...",
                EFI_DARKGREEN,
                EFI_BLACK,
            );

            // Here we try to allocate UEFI pages for the file read buffer.
            // Start with max_size, then try 256MB, 128MB, 64MB chunks
            // until one works. If none work, return error. This is not "beatifull" but it works so get of my ass.            const PAGE_SIZE: usize = 4096;
            let chunk_sizes = [
                max_size,
                256 * 1024 * 1024, // 256MB
                128 * 1024 * 1024, // 128MB
                64 * 1024 * 1024,  // 64MB
            ];

            let mut buffer_addr = 0u64;
            let mut chunk_size = 0usize;
            let alloc_type = 0; // EFI_ALLOCATE_ANY_PAGES - less fragmentation
            let mem_type = 2; // EFI_LOADER_DATA

            // Try progressively smaller allocations
            for &size in &chunk_sizes {
                let pages_needed = (size + PAGE_SIZE - 1) / PAGE_SIZE;
                let mut addr = 0u64; // ANY_PAGES doesn't need initial address

                let status =
                    (boot_services.allocate_pages)(alloc_type, mem_type, pages_needed, &mut addr);

                if status == 0 {
                    buffer_addr = addr;
                    chunk_size = size;
                    break;
                }
            }

            if buffer_addr == 0 {
                morpheus_core::logger::log("FAIL: all allocate_pages attempts");
                close_file(file).ok();
                close_file(root).ok();
                return Err("Failed to allocate UEFI pages");
            }

            let buffer_ptr = buffer_addr as *mut u8;
            let mut bytes_to_read = chunk_size;

            screen.put_str_at(
                5,
                start_line + 2,
                "Reading file...",
                EFI_DARKGREEN,
                EFI_BLACK,
            );

            // Read up to chunk_size - UEFI will update bytes_to_read with actual amount
            let status = ((*file).read)(file, &mut bytes_to_read, buffer_ptr);

            if status != 0 {
                morpheus_core::logger::log("FAIL: file.read");
                let pages = (chunk_size + PAGE_SIZE - 1) / PAGE_SIZE;
                (boot_services.free_pages)(buffer_addr, pages);
                close_file(file).ok();
                close_file(root).ok();
                return Err("Failed to read file");
            }

            close_file(file).ok();
            close_file(root).ok();

            screen.put_str_at(5, start_line + 3, "Success!", EFI_GREEN, EFI_BLACK);

            // Return pointer + actual bytes read
            Ok((buffer_ptr, bytes_to_read))
        }
    }
}