1515const VOL_LABEL = 'RETROROCKET ' ;
1616const HEADROOM_MB = 64 ;
1717
18-
19-
2018if (!is_file (LIMINE_BIN ) || !is_executable (LIMINE_BIN )) {
2119 chdir (LIMINE_DIR );
2220 run ('env -u MAKEFLAGS -u MFLAGS -u MAKELEVEL make -j1 CC="/usr/bin/gcc -B/usr/bin/" >/dev/null ' );
2523 throw new RuntimeException ("limine build failed: missing or non-executable ' " . LIMINE_BIN . "' " );
2624 }
2725}
26+
2827function run (string $ cmd , array $ env = []): void {
2928 $ proc = proc_open ($ cmd , [1 =>['pipe ' ,'w ' ], 2 =>['pipe ' ,'w ' ]], $ pipes , null , $ env + $ _ENV );
3029 if (!is_resource ($ proc )) {
@@ -70,37 +69,50 @@ function align_up(int $x, int $a): int {
7069$ sector_size = 512 ;
7170$ start_lba = 2048 ;
7271$ offset_bytes = $ start_lba * $ sector_size ;
73- $ total_sectors = intdiv ($ img_bytes , $ sector_size );
74- $ part_sectors = $ total_sectors - $ start_lba ;
7572
7673if (is_file (OUT_IMG )) {
7774 unlink (OUT_IMG );
7875}
79- run (sprintf ("truncate -s %d %s " , $ img_bytes , escapeshellarg (OUT_IMG )));
8076
81- $ sfdisk_spec = <<<EOT
82- label: dos
83- unit: sectors
77+ run (sprintf ("truncate -s %d %s " , $ img_bytes , escapeshellarg (OUT_IMG )));
8478
85- $ {start_lba} $ {part_sectors} ef *
86- EOT ;
79+ run (sprintf ("sgdisk -o -a 1 -n 1:34:2047 -t 1:ef02 -n 2:%d:0 -t 2:ef00 %s " , $ start_lba , escapeshellarg (OUT_IMG )));
80+ run (sprintf ("sgdisk --hybrid 2:EE %s " , escapeshellarg (OUT_IMG )));
81+ run (sprintf ("printf 'a \n1 \nw \n' | fdisk %s " , escapeshellarg (OUT_IMG )));
8782
88- $ spec_tmp = tempnam (sys_get_temp_dir (), 'sfdisk_ ' );
89- file_put_contents ($ spec_tmp , $ sfdisk_spec );
90- run (sprintf ("sfdisk %s < %s " , escapeshellarg (OUT_IMG ), escapeshellarg ($ spec_tmp )));
91- @unlink ($ spec_tmp );
9283$ env = ['MTOOLS_SKIP_CHECK ' => '1 ' ];
84+
9385run (sprintf ("mformat -i %s@@%d -F -v %s :: " , escapeshellarg (OUT_IMG ), $ offset_bytes , escapeshellarg (VOL_LABEL )), $ env );
86+
9487$ img_spec = escapeshellarg (OUT_IMG . '@@ ' . (string )$ offset_bytes );
88+
9589run ("mmd -i $ img_spec ::/EFI " , $ env );
9690run ("mmd -i $ img_spec ::/EFI/BOOT " , $ env );
91+
9792run (sprintf ("mcopy -i %s %s ::/EFI/BOOT/BOOTX64.EFI " , $ img_spec , escapeshellarg (LIMINE_DIR . '/BOOTX64.EFI ' )), $ env );
9893run (sprintf ("mcopy -i %s %s ::/limine-bios.sys " , $ img_spec , escapeshellarg (LIMINE_DIR . '/limine-bios.sys ' )), $ env );
99- run (sprintf ("mcopy -i %s %s ::/kernel.bin " , $ img_spec , escapeshellarg (KERNEL_BIN )), $ env );
100- run (sprintf ("mcopy -i %s %s ::/kernel.sym " , $ img_spec , escapeshellarg (KERNEL_SYM )), $ env );
101- run (sprintf ("mcopy -i %s %s ::/root.iso.gz " , $ img_spec , escapeshellarg ($ root_gz )), $ env );
94+ run (sprintf ("mcopy -i %s %s ::/kernel.bin " , $ img_spec , escapeshellarg (KERNEL_BIN )), $ env );
95+ run (sprintf ("mcopy -i %s %s ::/kernel.sym " , $ img_spec , escapeshellarg (KERNEL_SYM )), $ env );
96+ run (sprintf ("mcopy -i %s %s ::/root.iso.gz " , $ img_spec , escapeshellarg ($ root_gz )), $ env );
10297run (sprintf ("mcopy -i %s %s ::/limine.conf " , $ img_spec , escapeshellarg (LIMINE_CONF )), $ env );
103- run (sprintf ("%s bios-install %s " , escapeshellarg (LIMINE_BIN ), escapeshellarg (OUT_IMG )));
98+
99+ // Install BIOS bootloader
100+ //run(sprintf("%s bios-install %s", escapeshellarg(LIMINE_BIN), escapeshellarg(OUT_IMG)));
101+ run (sprintf ("%s bios-install %s 1 " , escapeshellarg (LIMINE_BIN ), escapeshellarg (OUT_IMG )));
102+
103+ $ fp = fopen (OUT_IMG , 'r+b ' );
104+ if ($ fp === false ) {
105+ throw new RuntimeException ("failed to open image for MBR boot flag update " );
106+ }
107+ if (fseek ($ fp , 446 ) !== 0 ) {
108+ fclose ($ fp );
109+ throw new RuntimeException ("failed to seek to MBR partition entry " );
110+ }
111+ if (fwrite ($ fp , "\x80" ) !== 1 ) {
112+ fclose ($ fp );
113+ throw new RuntimeException ("failed to write MBR boot flag " );
114+ }
115+ fclose ($ fp );
104116
105117$ mb = (int ) round ($ img_bytes / (1024 * 1024 ));
106- echo "USB image created: " . OUT_IMG . " ( " . $ mb . " MB) \n" ;
118+ echo "USB image created: " . OUT_IMG . " ( " . $ mb . " MB) \n" ;
0 commit comments