From 9e929d3db61462edd91cad8b56472ba510fc132b Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Fri, 3 Jan 2020 13:56:35 +0100 Subject: [PATCH] assembler/qemu: refactor grub2 boot image patching As noted in earlier commits the grub2 boot image needs to be patched to contain the position of the grub2 core. By default, the location in the boot image is hard-coded to be the mbr gap (sector 1) but for GPT partition schemes a separate BIOS boot partition is used that is located at a "random" location. Refactor the code to generalize the boot image patching, where the default mbr gap location is just a special case of the general. --- assemblers/org.osbuild.qemu | 46 ++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/assemblers/org.osbuild.qemu b/assemblers/org.osbuild.qemu index 99e772f0..728a6890 100755 --- a/assemblers/org.osbuild.qemu +++ b/assemblers/org.osbuild.qemu @@ -360,15 +360,25 @@ def partition_table_from_options(options) -> PartitionTable: def grub2_write_boot_image(boot_f: BinaryIO, - image_f: BinaryIO): + image_f: BinaryIO, + core_location: int): """Write the boot image (grub2 stage 1) to the MBR""" # The boot.img file is 512 bytes, but we must only copy the first 440 # bytes, as these contain the bootstrapping code. The rest of the # first sector contains the partition table, and must not be # overwritten. + image_f.seek(0) image_f.write(boot_f.read(440)) + # Additionally, write the location (in sectors) of + # the grub core image, into the boot image, so the + # latter can find the former. To exact location is + # taken from grub2's "boot.S": + # GRUB_BOOT_MACHINE_KERNEL_SECTOR 0x5c (= 92) + image_f.seek(0x5c) + image_f.write(struct.pack("