summaryrefslogtreecommitdiffstats
path: root/src/utils/uefi.py
blob: 179df4d02b3f7f0d7ae136dbb2ac2af30e042700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#
# Copyright (C) 2020-2024 Soleta Networks <info@soleta.eu>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the
# Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

import json
import logging
import os
import shlex
import subprocess
import shutil
from src.utils.disk import *
from src.utils.fs import *
from src.utils.probe import *
from src.log import OgError

import fdisk

EFIBOOTMGR_BIN='/opt/opengnsys/bin/efibootmgr'

def _find_bootentry(entries, label):
    for entry in entries:
        if entry['description'] == label:
            return entry
    else:
        raise OgError('Boot entry {label} not found')


def _strip_boot_prefix(entry):
    try:
        num = entry['name'][4:]
    except:
        raise OgError('Unable to strip "Boot" prefix from boot entry')
    return num


def _check_efibootmgr_json():
    """
    Checks if efibootmgr --json option is supported.
    Returns True if supported, False otherwise.
    """
    supported = True
    try:
        subprocess.run([EFIBOOTMGR_BIN, '--json'], stdout=subprocess.DEVNULL,
                       stderr=subprocess.DEVNULL, check=True)
    except subprocess.CalledProcessError:
        supported = False
    return supported


def is_uefi_supported():
    return os.path.exists("/sys/firmware/efi")

def _validate_efibootmgr_json(dict_json):
    missing_fields = []
    if not 'BootOrder' in dict_json:
        missing_fields.append('BootOrder')

    if not 'vars' in dict_json:
        missing_fields.append('vars')

    for i, entry in enumerate(dict_json.get('vars', [])):
        if not 'name' in entry:
            missing_fields.append(f'vars[{i}].name')

        if not 'active' in entry:
            missing_fields.append('vars[{i}].active')

        if not 'description' in entry:
            missing_fields.append('vars[{i}].description')

    if missing_fields:
        raise OgError('Missing required efibootmgr fields: ' + ', '.join(missing_fields))

def run_efibootmgr_json(validate):
    if _check_efibootmgr_json() is False:
        raise OgError(f'{EFIBOOTMGR_BIN} not available')

    proc = subprocess.run([EFIBOOTMGR_BIN, '--json'], capture_output=True, text=True)
    dict_json = json.loads(proc.stdout)

    if validate:
        _validate_efibootmgr_json(dict_json)

    return dict_json


def efibootmgr_bootnext(description):
    logging.info(f'Setting BootNext to value from boot entry with label {description}')
    bootnext_cmd = '{efibootmgr} -n {bootnum}'
    boot_entries = run_efibootmgr_json(validate=False).get('vars', [])

    entry = _find_bootentry(boot_entries, description)
    num = _strip_boot_prefix(entry)  # efibootmgr output uses BootXXXX for each entry, remove the "Boot" prefix.
    bootnext_cmd = bootnext_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN)
    subprocess.run(shlex.split(bootnext_cmd), check=True,
                   stdout=subprocess.DEVNULL)


def efibootmgr_delete_bootentry(label):
    dict_json = run_efibootmgr_json(validate=False)
    efibootmgr_cmd = '{efibootmgr} -q -b {bootnum} -B'

    for entry in dict_json.get('vars', []):
        if entry['description'] == label:
            num = entry['name'][4:]  # Remove "Boot" prefix to extract num
            efibootmgr_cmd = efibootmgr_cmd.format(bootnum=num, efibootmgr=EFIBOOTMGR_BIN)
            subprocess.run(shlex.split(efibootmgr_cmd), check=True)
            break
    else:
        logging.info(f'Cannot delete boot entry {label} because it was not found.')


def efibootmgr_create_bootentry(disk, part, loader, label, add_to_bootorder=True):
    entries = run_efibootmgr_json(validate=False).get('vars', [])
    create_opt = '-c' if add_to_bootorder else '-C'
    index_opt = f'-I {len(entries)}'  # Requires efibootmgr version 18
    efibootmgr_cmd = f'{EFIBOOTMGR_BIN} {create_opt} {index_opt} -d {disk} -p {part} -L {label} -l {loader}'
    logging.info(f'{EFIBOOTMGR_BIN} command creating boot entry: {efibootmgr_cmd}')
    try:
        proc = subprocess.run(shlex.split(efibootmgr_cmd), check=True, text=True)
    except OSError as e:
        raise OgError(f'Unexpected error adding boot entry to nvram. UEFI firmware might be buggy') from e

def efibootmgr_set_entry_order(label, position):
    logging.info(f'Setting {label} entry to position {position} of boot order')
    boot_info = run_efibootmgr_json(validate=False)
    boot_entries = boot_info.get('vars', [])
    boot_order = boot_info.get('BootOrder', [])

    entry = _find_bootentry(boot_entries, label)
    target_grub_entry = _strip_boot_prefix(entry)

    if target_grub_entry in boot_order:
        boot_order.remove(target_grub_entry)

    boot_order.insert(position, target_grub_entry)

    try:
        proc = subprocess.run([EFIBOOTMGR_BIN, "-o", ",".join(boot_order)], check=True, text=True)
    except OSError as e:
        raise OgError(f'Unexpected error setting boot order to NVRAM. UEFI firmware might be buggy') from e

def _find_efi_loader(loader_paths):
    for efi_app in loader_paths:
        logging.info(f'Searching for {efi_app}')
        if os.path.exists(efi_app):
            logging.info(f'Found bootloader at ESP partition: {efi_app}')
            return efi_app
    else:
        raise OgError('Unable to locate any EFI bootloader at ESP')


def find_windows_efi_loader(esp_mountpoint, bootlabel):
    loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/bootmgfw.efi',
                    f'{esp_mountpoint}/EFI/Microsoft/Boot/bootmgfw.efi']
    return _find_efi_loader(loader_paths)


def find_linux_efi_loader(esp_mountpoint, bootlabel):
    loader_paths = [f'{esp_mountpoint}/EFI/{bootlabel}/Boot/shimx64.efi',
                    f'{esp_mountpoint}/EFI/ubuntu/shimx64.efi']
    return _find_efi_loader(loader_paths)


def copy_windows_efi_bootloader(disk, partition):
    device = get_partition_device(disk, partition)
    mountpoint = device.replace('dev', 'mnt')
    if not mount_mkdir(device, mountpoint):
        raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}')

    os_family = get_os_family(mountpoint)
    is_uefi = is_uefi_supported()

    if not is_uefi or os_family != OSFamily.WINDOWS:
        return

    bootlabel = f'Part-{disk:02d}-{partition:02d}'
    esp, esp_disk, esp_part_number = get_efi_partition(disk, enforce_gpt=True)
    esp_mountpoint = esp.replace('dev', 'mnt')
    if not mount_mkdir(esp, esp_mountpoint):
        umount(mountpoint)
        raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')

    try:
        loader = find_windows_efi_loader(esp_mountpoint, bootlabel)

        loader_dir = os.path.dirname(loader)
        destination_dir = f'{mountpoint}/ogBoot'
        if os.path.exists(destination_dir):
            try:
                shutil.rmtree(destination_dir)
            except OSError as e:
                raise OgError(f'Failed to delete {destination_dir}: {e}') from e
        logging.info(f'Copying {loader_dir} into {destination_dir}')
        try:
            shutil.copytree(loader_dir, destination_dir)
        except OSError as e:
            raise OgError(f'Failed to copy {loader_dir} into {destination_dir}: {e}') from e
    finally:
        umount(mountpoint)
        umount(esp_mountpoint)


def restore_windows_efi_bootloader(disk, partition):
    device = get_partition_device(disk, partition)
    mountpoint = device.replace('dev', 'mnt')
    if not mount_mkdir(device, mountpoint):
        raise OgError(f'Cannot probe OS family. Unable to mount {device} into {mountpoint}')

    bootlabel = f'Part-{disk:02d}-{partition:02d}'
    esp, esp_disk, esp_part_number = get_efi_partition(disk, enforce_gpt=True)
    esp_mountpoint = esp.replace('dev', 'mnt')
    if not mount_mkdir(esp, esp_mountpoint):
        umount(mountpoint)
        raise OgError(f'Unable to mount detected EFI System Partition at {esp} into {esp_mountpoint}')

    try:
        loader_dir = f'{mountpoint}/ogBoot'
        destination_dir = f'{esp_mountpoint}/EFI/{bootlabel}/Boot'
        if os.path.exists(destination_dir):
            try:
                shutil.rmtree(destination_dir)
            except OSError as e:
                raise OgError(f'Failed to delete {destination_dir}: {e}') from e
        logging.info(f'Copying {loader_dir} into {destination_dir}')
        try:
            shutil.copytree(loader_dir, destination_dir)
        except OSError as e:
            raise OgError(f'Failed to copy {loader_dir} into {destination_dir}: {e}') from e
    finally:
        umount(mountpoint)
        umount(esp_mountpoint)