]> git.proxmox.com Git - pve-edk2-firmware.git/blob - debian/python/UEFI/Filesystems.py
change default resolution to 1024x768
[pve-edk2-firmware.git] / debian / python / UEFI / Filesystems.py
1 #
2 # Copyright 2019-2021 Canonical Ltd.
3 # Authors:
4 # - dann frazier <dann.frazier@canonical.com>
5 #
6 # This program is free software: you can redistribute it and/or modify it
7 # under the terms of the GNU General Public License version 3, as published
8 # by the Free Software Foundation.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranties of MERCHANTABILITY,
12 # SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18
19 import os
20 import shutil
21 import subprocess
22 import tempfile
23
24
25 class FatFsImage:
26 def __init__(self, size_in_mb):
27 with tempfile.NamedTemporaryFile(delete=False) as f:
28 self.path = f.name
29
30 subprocess.check_call(
31 [
32 'dd', 'if=/dev/zero', 'of=%s' % (self.path),
33 'count=0', 'bs=1M', 'seek=%d' % (size_in_mb), 'status=none'
34 ]
35 )
36 new_env = os.environ.copy()
37 new_env['PATH'] = f"{os.environ['PATH']}:/sbin"
38 subprocess.check_call(['mkdosfs', '-F', '32', self.path], env=new_env)
39
40 def __del__(self):
41 os.unlink(self.path)
42
43 def mkdir(self, dir):
44 subprocess.run(['mmd', '-i', self.path, dir])
45
46 def makedirs(self, dir):
47 dirs = dir.split(os.path.sep)
48 for dir_idx in range(1, len(dirs)+1):
49 next_dir = os.path.sep.join(dirs[:dir_idx])
50 self.mkdir(next_dir)
51
52 def insert_file(self, src, dest):
53 subprocess.check_call(
54 [
55 'mcopy', '-i', self.path, src, '::%s' % (dest)
56 ]
57 )
58
59
60 class EfiBootableIsoImage:
61 def __init__(self, eltorito_img):
62 with tempfile.TemporaryDirectory() as iso_root:
63 eltorito_iso_root = 'boot'
64 eltorito_iso_path = os.path.join(eltorito_iso_root, 'efi.img')
65 eltorito_local_root = os.path.join(iso_root, eltorito_iso_root)
66 eltorito_local_path = os.path.join(iso_root, eltorito_iso_path)
67
68 os.makedirs(eltorito_local_root)
69 shutil.copyfile(eltorito_img.path, eltorito_local_path)
70
71 with tempfile.NamedTemporaryFile(delete=False) as f:
72 self.path = f.name
73
74 subprocess.check_call(
75 [
76 'xorriso', '-as', 'mkisofs', '-J', '-l',
77 '-c', 'boot/boot.cat',
78 '-partition_offset', '16', '-append_partition', '2',
79 '0xef', eltorito_local_path,
80 '-e', '--interval:appended_partition_2:all::',
81 '-no-emul-boot', '-o', self.path, iso_root
82 ]
83 )
84
85 def __del__(self):
86 os.unlink(self.path)
87
88
89 class GrubShellBootableIsoImage(EfiBootableIsoImage):
90 def __init__(self, efi_arch, use_signed):
91 EfiArchToGrubArch = {
92 'X64': "x86_64",
93 'AA64': "arm64",
94 }
95 efi_img = FatFsImage(64)
96 efi_img.makedirs(os.path.join('EFI', 'BOOT'))
97 removable_media_path = os.path.join(
98 'EFI', 'BOOT', 'BOOT%s.EFI' % (efi_arch.upper())
99 )
100 efi_ext = 'efi'
101 grub_subdir = "%s-efi" % EfiArchToGrubArch[efi_arch.upper()]
102 if use_signed:
103 efi_ext = "%s.signed" % (efi_ext)
104 grub_subdir = "%s-signed" % (grub_subdir)
105
106 shim_src = os.path.join(
107 os.path.sep, 'usr', 'lib', 'shim',
108 'shim%s.%s' % (efi_arch.lower(), efi_ext)
109 )
110 grub_src = os.path.join(
111 os.path.sep, 'usr', 'lib', 'grub',
112 '%s' % (grub_subdir),
113 "" if use_signed else "monolithic",
114 'grub%s.%s' % (efi_arch.lower(), efi_ext)
115 )
116 grub_dest = os.path.join(
117 'EFI', 'BOOT', 'GRUB%s.EFI' % (efi_arch.upper())
118 )
119 efi_img.insert_file(shim_src, removable_media_path)
120 efi_img.insert_file(grub_src, grub_dest)
121 super().__init__(efi_img)