From c83019a264d45a1c5a014122ee5d36f938324124 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Fri, 3 Jan 2020 12:01:33 +0100 Subject: [PATCH] assembler/qemu: extract grub2 boot image writing Extract the small piece of code that writes the grub2's boot image, i.e. the first stage of the bootloader that will in turn jump to the second stage. Currently the position of the core is hard-coded to be the MBR gap, i.e. the gap between the MBR and the start of the first partition. This is not a necessity, e.g. when using a dedicated BIOS boot partition on GPT partition layouts. This re- factoring should make it easier to add code dealing with such situations. --- assemblers/org.osbuild.qemu | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/assemblers/org.osbuild.qemu b/assemblers/org.osbuild.qemu index 0e5fdb4f..f105775d 100755 --- a/assemblers/org.osbuild.qemu +++ b/assemblers/org.osbuild.qemu @@ -350,6 +350,17 @@ def partition_table_from_options(options) -> PartitionTable: return PartitionTable(pttype, ptuuid, parts) +def grub2_write_boot_image(boot_f: BinaryIO, + image_f: BinaryIO): + """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.write(boot_f.read(440)) + + def grub2_write_core_mbrgap(core_f: BinaryIO, image_f: BinaryIO, pt: PartitionTable): @@ -445,11 +456,7 @@ def install_grub2(image: str, pt: PartitionTable, options): # The purpose of this is simply to jump into the stage-2 bootloader. # On ppc64le & Open Firmware stage-2 is loaded by the firmware with open(boot_path, "rb") as boot_f: - # 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.write(boot_f.read(440)) + grub2_write_boot_image(boot_f, image_f) with open(core_path, "rb") as core_f: if platform == "powerpc-ieee1275":