x86: add bootloader upgrade on sysupgrade
authorTomasz Maciej Nowak <tomek_n@o2.pl>
Tue, 14 Jan 2020 17:45:44 +0000 (18:45 +0100)
committerPetr Štetiar <ynezz@true.cz>
Sun, 1 Mar 2020 20:36:00 +0000 (21:36 +0100)
Currently bootloader always stays on the same version as when first
written to boot medium (not true if partition layout changed, which will
trigger sysupgrade process to write full disk image). That creates
inconveniences as it always stays with same features or/and bugs. Users
wishing to add support to additional modules or new version, would need
to write the whole image, potentially destroying previous system
configuration. To fix these, this commit adds additional routine to
sysupgrade which upgrades unconditionally the bootloader to the latest
state provided by OpenWrt.

Signed-off-by: Tomasz Maciej Nowak <tomek_n@o2.pl>
package/boot/grub2/Makefile
target/linux/x86/base-files/lib/upgrade/platform.sh
target/linux/x86/image/Makefile

index 1f92ba92502d096e609126e7430c315fd30653b7..b26ef64ca41190b7b0325acae3ad289229d95502 100644 (file)
@@ -82,6 +82,11 @@ define Host/Configure
        $(Host/Configure/Default)
 endef
 
+define Package/grub2/install
+       $(INSTALL_DIR) $(1)/usr/sbin
+       $(INSTALL_BIN) $(PKG_BUILD_DIR)/grub-bios-setup $(1)/usr/sbin/
+endef
+
 define Package/grub2-editenv/install
        $(INSTALL_DIR) $(1)/usr/sbin
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/grub-editenv $(1)/usr/sbin/
index 3b6c25877fc9daf2bedba33c27a0a510aebae167..8be96dfcd45f7a85e11683c1d4a218e1c4680719 100644 (file)
@@ -1,3 +1,5 @@
+RAMFS_COPY_BIN='grub-bios-setup'
+
 platform_check_image() {
        local diskdev partdev diff
        [ "$#" -gt 1 ] && return 1
@@ -44,6 +46,26 @@ platform_copy_config() {
        fi
 }
 
+platform_do_bootloader_upgrade() {
+       local bootpart
+       local diskdev="$1"
+
+       if export_partdevice bootpart 1; then
+               mkdir -p /tmp/boot
+               mount -o rw,noatime "/dev/$bootpart" /tmp/boot
+               echo "(hd0) /dev/$diskdev" > /tmp/device.map
+
+               echo "Upgrading bootloader on /dev/$diskdev..."
+               grub-bios-setup \
+                       -m "/tmp/device.map" \
+                       -d "/tmp/boot/boot/grub" \
+                       -r "hd0,msdos1" \
+                       "/dev/$diskdev"
+
+               umount /tmp/boot
+       fi
+}
+
 platform_do_upgrade() {
        local diskdev partdev diff
 
@@ -92,4 +114,6 @@ platform_do_upgrade() {
        #copy partition uuid
        echo "Writing new UUID to /dev/$diskdev..."
        get_image "$@" | dd of="/dev/$diskdev" bs=1 skip=440 count=4 seek=440 conv=fsync
+
+       platform_do_bootloader_upgrade "$diskdev"
 }
index 2838b3139c139305554242e260cada3205af2482..373f2396b7a6eaa94a4c4713a99422f994d95808 100644 (file)
@@ -66,11 +66,11 @@ ifneq ($(CONFIG_GRUB_IMAGES),)
        grub-mkimage \
                -p /boot/grub \
                -d $(STAGING_DIR_HOST)/lib/grub/i386-pc \
-               -o $(KDIR)/grub2/core.img \
+               -o $(KDIR)/root.grub/boot/grub/core.img \
                -O i386-pc \
                -c ./grub-early.cfg \
                $(GRUB2_MODULES)
-       $(CP) $(STAGING_DIR_HOST)/lib/grub/i386-pc/*.img $(KDIR)/grub2/
+       $(CP) $(STAGING_DIR_HOST)/lib/grub/i386-pc/*.img $(KDIR)/root.grub/boot/grub/
        echo '(hd0) $(BIN_DIR)/$(IMG_COMBINED)-$(1).img' > $(KDIR)/grub2/device.map
        sed \
                -e 's#@SERIAL_CONFIG@#$(strip $(GRUB_SERIAL_CONFIG))#g' \
@@ -80,6 +80,7 @@ ifneq ($(CONFIG_GRUB_IMAGES),)
                -e 's#@TITLE@#$(GRUB_TITLE)#g' \
                ./grub.cfg > $(KDIR)/root.grub/boot/grub/grub.cfg
        -$(CP) $(STAGING_DIR_ROOT)/boot/. $(KDIR)/root.grub/boot/
+       grub-bios-setup -V | cut -d' ' -f3 > $(KDIR)/root.grub/boot/grub/version
        PADDING="1" SIGNATURE="$(IMG_PART_SIGNATURE)" PATH="$(TARGET_PATH)" $(SCRIPT_DIR)/gen_image_generic.sh \
                $(BIN_DIR)/$(IMG_COMBINED)-$(1).img \
                $(CONFIG_TARGET_KERNEL_PARTSIZE) $(KDIR)/root.grub \
@@ -87,7 +88,7 @@ ifneq ($(CONFIG_GRUB_IMAGES),)
                256
        grub-bios-setup \
                --device-map="$(KDIR)/grub2/device.map" \
-               -d "$(KDIR)/grub2" \
+               -d "$(KDIR)/root.grub/boot/grub" \
                -r "hd0,msdos1" \
                "$(BIN_DIR)/$(IMG_COMBINED)-$(1).img"
   endef