@@ -14,6 +14,7 @@ Responsibilities:
1414"""
1515
1616import os
17+ import re
1718import sys
1819import textwrap
1920
@@ -175,9 +176,12 @@ def fix_bootloader(target_disk):
175176 LOG .info ("Mounting EFI partition %s to %s" , efi_partition , efi_mount )
176177 subp (["mount" , efi_partition , efi_mount ])
177178
178- # Look for GRUB EFI file
179- # Common locations: EFI/BOOT/bootx64.efi, EFI/kairos/grubx64.efi, etc.
179+ # Look for EFI boot file. Prefer signed shim for Secure Boot compatibility.
180+ # "Platform does not support this image" on reboot-after-deploy is often
181+ # Secure Boot rejecting an unsigned loader; using shimx64.efi when present
182+ # ensures the signed chain (shim -> grub -> kernel) is used.
180183 efi_paths = [
184+ os .path .join (efi_mount , "EFI/BOOT/shimx64.efi" ),
181185 os .path .join (efi_mount , "EFI/BOOT/bootx64.efi" ),
182186 os .path .join (efi_mount , "EFI/kairos/grubx64.efi" ),
183187 os .path .join (efi_mount , "EFI/BOOT/grubx64.efi" ),
@@ -241,9 +245,8 @@ def fix_bootloader(target_disk):
241245 try :
242246 out , _ = subp (["efibootmgr" ], capture = True )
243247 for line in out .splitlines ():
244- if "Kairos" in line or "kairos" in line :
248+ if "Kairos" in line or "kairos" in line :
245249 # Extract boot number (e.g., "Boot0001")
246- import re
247250 match = re .search (r"Boot(\d{4})" , line )
248251 if match :
249252 boot_num = match .group (1 )
@@ -252,16 +255,36 @@ def fix_bootloader(target_disk):
252255 except Exception as e :
253256 LOG .warning ("Failed to check/remove existing boot entries: %s" , e )
254257
255- # Create new boot entry
256- subp ([
258+ # Create new boot entry (efibootmgr -c prints the new BootXXXX line)
259+ out , _ = subp ([
257260 "efibootmgr" ,
258261 "-c" ,
259262 "-d" , disk_dev ,
260263 "-p" , part_num ,
261264 "-L" , "Kairos" ,
262265 "-l" , efi_path_windows
263- ])
264- LOG .info ("UEFI boot entry created successfully" )
266+ ], capture = True )
267+ LOG .info ("UEFI boot entry created: %s" , out .strip () if out else "" )
268+
269+ # Set this entry first in boot order so firmware uses it (avoids booting
270+ # another ESP with wrong/unsupported EFI and "Platform does not support this image")
271+ match = re .search (r"Boot(\d{4})" , out or "" )
272+ if match :
273+ new_boot = match .group (1 )
274+ try :
275+ order_out , _ = subp (["efibootmgr" ], capture = True )
276+ # Build new order: our entry first, then existing order without our entry
277+ existing = []
278+ for line in (order_out or "" ).splitlines ():
279+ m = re .search (r"BootOrder is (.+)" , line )
280+ if m :
281+ existing = [b for b in m .group (1 ).split ("," ) if b != new_boot ]
282+ break
283+ new_order = "," .join ([new_boot ] + existing [:9 ])
284+ subp (["efibootmgr" , "-o" , new_order ])
285+ LOG .info ("Set Kairos (Boot%s) as first boot option" , new_boot )
286+ except Exception as e :
287+ LOG .warning ("Could not set boot order: %s" , e )
265288 except Exception as e :
266289 LOG .warning ("efibootmgr failed (may not be available or firmware issue): %s" , e )
267290 LOG .info ("Bootloader files are in place, but UEFI boot entry may need manual configuration" )
0 commit comments