import sys import struct import math import os def merge_boot_sector(fat_img_path, boot_img_path, core_img_path): # READ FAT boot sector to get BPB with open(fat_img_path, 'rb') as f: fat_sector = f.read(512) # Read boot.img with open(boot_img_path, 'rb') as b: boot_sector = bytearray(b.read(512)) # Copy BPB from FAT sector (bytes 3 to 62) into boot sector for i in range(3, 62): boot_sector[i] = fat_sector[i] # Write patched boot sector to FAT image sector 0 with open(fat_img_path, 'r+b') as f: f.seek(0) # Patch boot sector with LBA of core.img (diskboot.img) # GRUB boot.S usually expects the LBA of the next stage at offset 0x5C (92) on some versions? # Or it uses a blocklist? # Standard GRUB boot.S loads one sector. # The sector number is stored at offset 0x5C is for kernel_sector? # Actually in GRUB2, boot.S uses a 'kernel_sector' field. # Let's check offset. # According to some docs, it is at 0x5C. # We want to load sector 1. # struct.pack_into(' 0: start_sector = 2 # Because boot sector is 0, diskboot is 1, so rest starts at 2 segment = 0x820 # Load at 0x8200 with open(core_img_path, 'r+b') as c: c.seek(0) diskboot = bytearray(c.read(512)) struct.pack_into(' ") sys.exit(1) merge_boot_sector(sys.argv[1], sys.argv[2], sys.argv[3])