Skip to content

Commit a0f33d3

Browse files
committed
[ot] python/qemu: ot.otp.image: remove hard coded default OTP section.
Find OTP section based on their prefix names. Signed-off-by: Emmanuel Blot <[email protected]>
1 parent 22622fa commit a0f33d3

File tree

1 file changed

+21
-14
lines changed

1 file changed

+21
-14
lines changed

python/qemu/ot/otp/image.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ class OtpImage:
5454
0x2d, 0x2e, 0x2f, 0x31, 0x32, 0x33, 0x34, 0x35)
5555
)
5656

57-
DEFAULT_OTP_DEVICE = 'ot-otp-dj'
58-
"""Default OTP device name in configuration file."""
59-
6057
def __init__(self, ecc_bits: Optional[int] = None):
6158
self._log = getLogger('otp.img')
6259
self._header: dict[str, Any] = {}
@@ -237,24 +234,34 @@ def load_config(self, cfp: TextIO, section: Optional[str] = None) -> None:
237234
"""Load Present constant from a QEMU configuration file.
238235
239236
:param cfp: the configuration stream
240-
:param section: the section name where to find the constants,
241-
default to DEFAULT_OTP_DEVICE.
237+
:param section: the section name where to find the constants.
238+
if not set, try to discover the OTP section name
242239
"""
243240
cfg = ConfigParser()
244241
cfg.read_file(cfp)
242+
prefix = 'ot_device '
243+
ot_sections = [s for s in cfg.sections() if s.startswith(prefix)]
244+
devnames = [s.removeprefix(prefix).strip('"') for s in ot_sections]
245245
if not section:
246-
section = self.DEFAULT_OTP_DEVICE
246+
otpnames = {d.split('.')[0] for d in devnames
247+
if d.startswith('ot-otp')}
248+
if len(otpnames) != 1:
249+
# section should be explicitly defined in this case
250+
# this internal error may be raised if either:
251+
# - the configuration file does not contain an OTP section
252+
# - the OTP section name does not match the expected format
253+
# - several OTP-like sections could match, but there is no way
254+
# to discriminate which one is valid/usable
255+
raise ValueError('Unable to discover the name of OTP device')
256+
section = otpnames.pop()
257+
self._log.debug('Detected OTP section prefix: %s', section)
258+
elif not cfg.has_section(section):
259+
raise ValueError(f"No OTP device '{section}'")
247260
sectnames = set()
248-
for sectname in cfg.sections():
249-
if not sectname.startswith('ot_device '):
250-
continue
251-
try:
252-
devname = sectname.strip().split('"')[1]
253-
except IndexError:
254-
continue
261+
for devname in devnames:
255262
if devname != section and not devname.startswith(f'{section}.'):
256263
continue
257-
sectnames.add(sectname)
264+
sectnames.add(f'{prefix}"{devname}"')
258265
self._log.info('Found OTP candidate section %s', devname)
259266
if not sectnames:
260267
raise ValueError(f"Cannot find OTP device section '{section}'")

0 commit comments

Comments
 (0)