-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
efibootmgr.py
179 lines (155 loc) · 6.39 KB
/
efibootmgr.py
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
import abc
import logging
import re
import subprocess
from dataclasses import dataclass
@dataclass
class ParsedEfibootmgrEntry:
"""Stores a single entry parsed from efibootmgr command"""
num: str
active: bool
name: str
path: str
parameters: str
@dataclass
class ParsedEfibootmgr:
"""Stores all information parsed from efibootmgr command"""
entries: list[ParsedEfibootmgrEntry]
boot_order: list
boot_next: str
boot_current: str
timeout: int
class Efibootmgr(abc.ABC):
version_regex = re.compile(r'version ([0-9]+)')
parse_line_regex = re.compile(r'^Boot([0-9A-F]+)(\*)? (.+)\t(?:.+/File\((.+)\)|.*\))(.*)$')
log = logging.getLogger('Efibootmgr')
@staticmethod
def get_version() -> str:
output = subprocess.run(["efibootmgr", "--version"], check=True, capture_output=True, text=True).stdout
matched = Efibootmgr.version_regex.match(output)
version = matched.group(1)
Efibootmgr.log.info("efibootmgr version %s detected", version)
return version
@staticmethod
def get_instance() -> 'Efibootmgr':
version = Efibootmgr.get_version()
match version:
case "17":
return EfibootmgrV17()
case "18":
return EfibootmgrV18()
case _:
raise NotImplementedError(f"efibootmgr version {version} is not supported")
@abc.abstractmethod
def run(self) -> list[str]:
pass
@staticmethod
@abc.abstractmethod
def parse_line(line: str) -> tuple[str, object]:
pass
@classmethod
def parse(cls, boot: list[str]) -> ParsedEfibootmgr:
parser_logger = logging.getLogger("parser")
parsed_efi = {
'entries': [],
'boot_order': [],
'boot_next': None,
'boot_current': None,
'timeout': None
}
for line in boot:
try:
key, value = cls.parse_line(line)
if key == 'entry':
parsed_efi['entries'].append(value)
else:
parsed_efi[key] = value
except ValueError as e:
parser_logger.warning("line didn't match: %s", e.args[1])
return ParsedEfibootmgr(**parsed_efi)
class EfibootmgrV17(Efibootmgr):
def run(self) -> list[str]:
output = subprocess.run(["efibootmgr", "-v"], check=True, capture_output=True,
text=True).stdout.strip().split('\n')
logging.debug(repr(output))
return output
@staticmethod
def decode_params(code: str) -> str:
if '.' not in code:
return code
if code.endswith('.') and code.count('.') == 1:
return code
if code.startswith('WINDOWS'):
return 'WINDOWS' + EfibootmgrV17.decode_params(code[len('WINDOWS'):])
try:
# Decode as UTF-16 (why efibootmgr displays it like that?)
code_bytes = bytearray(code, 'utf-8')
for i, byte in enumerate(code_bytes):
if i % 2 == 1 and byte == ord('.'):
code_bytes[i] = 0
decoded = code_bytes.decode('utf-16')
return decoded
except UnicodeDecodeError as e:
logging.warning("Could not decode '%s': %s", code, e)
return code
@staticmethod
def parse_line(line: str) -> tuple[str, object]:
parser_logger = logging.getLogger("parser")
match = Efibootmgr.parse_line_regex.match(line)
if match and match.group(1) and match.group(3):
num, active, name, path, params = match.groups()
params = EfibootmgrV17.decode_params(params)
parsed_entry = ParsedEfibootmgrEntry(num=num, active=active is not None, name=name,
path=path if path else '', parameters=params)
parser_logger.debug("Entry: %s", parsed_entry)
return 'entry', parsed_entry
if line.startswith("BootOrder"):
parsed = line.split(':')[1].strip().split(',')
parser_logger.debug("BootOrder: %s", parsed)
return 'boot_order', parsed
if line.startswith("BootNext"):
parsed = line.split(':')[1].strip()
parser_logger.debug("BootNext: %s", parsed)
return 'boot_next', parsed
if line.startswith("BootCurrent"):
parsed = line.split(':')[1].strip()
parser_logger.debug("BootCurrent: %s", parsed)
return 'boot_current', parsed
if line.startswith("Timeout"):
parsed = int(line.split(':')[1].split()[0].strip())
parser_logger.debug("Timeout: %s", parsed)
return 'timeout', parsed
raise ValueError("line didn't match", repr(line))
class EfibootmgrV18(Efibootmgr):
def run(self):
output = subprocess.run(["efibootmgr", "--unicode"], check=True, capture_output=True,
text=True).stdout.strip().split('\n')
logging.debug(repr(output))
return output
@staticmethod
def parse_line(line: str) -> tuple[str, object]:
parser_logger = logging.getLogger("parser")
match = Efibootmgr.parse_line_regex.match(line)
if match and match.group(1) and match.group(3):
num, active, name, path, params = match.groups()
parsed_entry = ParsedEfibootmgrEntry(num=num, active=active is not None, name=name,
path=path if path else '', parameters=params)
parser_logger.debug("%s", parsed_entry)
return 'entry', parsed_entry
if line.startswith("BootOrder"):
parsed = line.split(':')[1].strip().split(',')
parser_logger.debug("BootOrder: %s", parsed)
return 'boot_order', parsed
if line.startswith("BootNext"):
parsed = line.split(':')[1].strip()
parser_logger.debug("BootNext: %s", parsed)
return 'boot_next', parsed
if line.startswith("BootCurrent"):
parsed = line.split(':')[1].strip()
parser_logger.debug("BootCurrent: %s", parsed)
return 'boot_current', parsed
if line.startswith("Timeout"):
parsed = int(line.split(':')[1].split()[0].strip())
parser_logger.debug("Timeout: %s", parsed)
return 'timeout', parsed
raise ValueError("line didn't match", repr(line))