VTOYEFI: UEFI return chainloads Paravis GRUB; validate configs
This commit is contained in:
1 parent
d6ef2e2abb
commit
4e33ce0a62
1 file changed
+47
-49
@@ -1,50 +1,37 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build VTOYEFI/grub/grbp.cfg with the Paravis return entry.
|
||||
"""Validate the VTOYEFI GRUB configs for the Paravis flash.
|
||||
|
||||
grub.cfg is the normal Ventoy config (direct boot, UEFI and legacy) and must
|
||||
NOT contain the "return to Paravis" item. Only grbp.cfg - loaded by
|
||||
grubx64_parv.efi when Ventoy is chainloaded from the Paravis menu - gets it.
|
||||
Layout (do not change):
|
||||
|
||||
grub.cfg direct Ventoy boot (UEFI and legacy). It must NOT contain the
|
||||
UEFI "return to Paravis" branch. The legacy (MBR) return is
|
||||
allowed and expected there.
|
||||
|
||||
grbp.cfg loaded by grubx64_parv.efi when Ventoy is chainloaded from the
|
||||
Paravis menu. It must contain the full return entry:
|
||||
UEFI -> chainload Paravis GRUB (grubx64.efi), legacy -> MBR.
|
||||
|
||||
Usage:
|
||||
python3 patch_ventoy_paravis_return.py <path-to-grub/grub.cfg>
|
||||
|
||||
The script never modifies grub.cfg. If grub.cfg is already patched, it
|
||||
refuses to run, so a regression cannot be silently re-introduced.
|
||||
The configs are maintained as files; this script only checks them and never
|
||||
modifies anything. Exit code 0 = OK, 1 = problem.
|
||||
"""
|
||||
|
||||
import io
|
||||
import os
|
||||
import sys
|
||||
|
||||
FUNC = r'''function paravis_return {
|
||||
if [ "$grub_platform" = "efi" ]; then
|
||||
set pvroot=
|
||||
search --no-floppy --file --set=pvroot /efi/Paravis/Boot/grub2/grub.cfg
|
||||
if [ -z "$pvroot" ]; then
|
||||
echo "Paravis Flash not found"
|
||||
return
|
||||
fi
|
||||
if [ "$grub_cpu" = "x86_64" ]; then
|
||||
chainloader ($pvroot)/efi/BOOT/bootx64.efi
|
||||
else
|
||||
chainloader ($pvroot)/efi/BOOT/bootia32.efi
|
||||
fi
|
||||
else
|
||||
chainloader (${vtoydev})+1
|
||||
fi
|
||||
boot
|
||||
}
|
||||
'''
|
||||
UEFI_RETURN = 'chainloader ($pvroot)/efi/BOOT/bootx64.efi'
|
||||
UEFI_RETURN32 = 'chainloader ($pvroot)/efi/BOOT/bootia32.efi'
|
||||
PARAVIS_GRUB = 'chainloader ($pvroot)/efi/Paravis/Boot/grub2/grubx64.efi'
|
||||
MBR_RETURN = 'chainloader (${vtoydev})+1'
|
||||
ID = '--id="paravis_return"'
|
||||
|
||||
MENU = r'''menuentry "Возврат в главное меню Paravis Flash" --id="paravis_return" --class="vtoyret" {
|
||||
paravis_return
|
||||
}
|
||||
|
||||
set default="paravis_return"
|
||||
'''
|
||||
|
||||
ANCHOR_FUNC = 'function ventoy_ext_menu {'
|
||||
ANCHOR_MENU = '#special VTOY_DEFAULT_IMAGE process'
|
||||
def fail(msg):
|
||||
print('FAIL: ' + msg)
|
||||
return 1
|
||||
|
||||
|
||||
def main():
|
||||
@@ -53,26 +40,37 @@ def main():
|
||||
return 1
|
||||
|
||||
path = sys.argv[1]
|
||||
text = io.open(path, encoding='utf-8', newline='').read()
|
||||
try:
|
||||
direct = io.open(path, encoding='utf-8', newline='').read()
|
||||
except OSError as exc:
|
||||
return fail('cannot read ' + path + ': ' + str(exc))
|
||||
|
||||
if 'function paravis_return' in text or '--id="paravis_return"' in text:
|
||||
print('grub.cfg already contains paravis_return, expected a clean source: ' + path)
|
||||
return 1
|
||||
if text.count(ANCHOR_FUNC) != 1:
|
||||
print('anchor not found or not unique: ' + ANCHOR_FUNC)
|
||||
return 1
|
||||
if text.count(ANCHOR_MENU) != 1:
|
||||
print('anchor not found or not unique: ' + ANCHOR_MENU)
|
||||
return 1
|
||||
grbp_path = os.path.join(os.path.dirname(path), 'grbp.cfg')
|
||||
try:
|
||||
paravis = io.open(grbp_path, encoding='utf-8', newline='').read()
|
||||
except OSError as exc:
|
||||
return fail('cannot read ' + grbp_path + ': ' + str(exc))
|
||||
|
||||
patched = text.replace(ANCHOR_FUNC, FUNC + '\n' + ANCHOR_FUNC, 1)
|
||||
patched = patched.replace(ANCHOR_MENU, MENU + '\n' + ANCHOR_MENU, 1)
|
||||
# Direct config: no UEFI return, legacy MBR return is fine.
|
||||
if UEFI_RETURN in direct or UEFI_RETURN32 in direct:
|
||||
return fail('grub.cfg contains the UEFI Paravis return (must be in grbp.cfg only)')
|
||||
if PARAVIS_GRUB in direct:
|
||||
return fail('grub.cfg contains the UEFI Paravis chainload (must be in grbp.cfg only)')
|
||||
|
||||
grbp = os.path.join(os.path.dirname(path), 'grbp.cfg')
|
||||
io.open(grbp, 'w', encoding='utf-8', newline='').write(patched)
|
||||
print('written: ' + grbp)
|
||||
# Paravis config: full return entry, UEFI chainloads Paravis GRUB directly
|
||||
# (not the shim - loading the shim a second time fails with SHIM_LOCK).
|
||||
if ID not in paravis:
|
||||
return fail('grbp.cfg has no paravis_return menu entry')
|
||||
if PARAVIS_GRUB not in paravis:
|
||||
return fail('grbp.cfg does not chainload the Paravis GRUB (grubx64.efi)')
|
||||
if MBR_RETURN not in paravis:
|
||||
return fail('grbp.cfg has no legacy MBR return')
|
||||
if UEFI_RETURN in paravis or UEFI_RETURN32 in paravis:
|
||||
return fail('grbp.cfg still chainloads the shim (bootx64.efi); use grubx64.efi')
|
||||
|
||||
print('OK: grub.cfg = direct boot, grbp.cfg = Paravis return (UEFI grubx64.efi + legacy MBR)')
|
||||
return 0
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.exit(main())
|
||||
sys.exit(main())
|
||||
Reference in new issue
Block a user