From cbe8cccbf895d6165c278eeb5fb26571289f9c75 Mon Sep 17 00:00:00 2001 From: Roy Date: Tue, 13 Oct 2026 10:16:49 +0800 Subject: [PATCH 1/4] otpset: Fix reading OTP on BCM2712 On Pi 5 / CM5, `vcgencmd otp_dump` starts at row 00, skips rows 61-76 and 93-108, and has three digit row numbers from 109 up. ReadOTP assumed the dump starts at row 08, is contiguous and that every row number has two digits, so: - `otpset -c` and `otpset -p` crash with "ValueError: invalid literal for int() with base 16: b':19a519a6'" - before crashing, `otpset -c` prints rows 28-35 as customer rows 0-7, which on CM5 includes the serial number and revision code, while the real customer OTP is all zero Parse the row number from each ":" line instead of the line index, read the customer rows with the documented mailbox call (0x00030021) rather than assuming where they are in the dump, and drop the BCM2711 row labels on BCM2712 where they don't apply. Output with --fake is unchanged. Co-Authored-By: Claude Opus 5.5 (1M context) Signed-off-by: Roy --- otpset/otpset | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/otpset/otpset b/otpset/otpset index ad60402..6d99991 100755 --- a/otpset/otpset +++ b/otpset/otpset @@ -34,6 +34,13 @@ def ParseCommandLine(): return parser.parse_args() +def is_bcm2712(): + try: + with open('/proc/device-tree/compatible', 'rb') as f: + return b'bcm2712' in f.read() + except OSError: + return False + def ReadOTP(args): # Map key is actually the value from the docs, so we do need to subtract/add the legacy_offset from it where necessary. @@ -62,21 +69,36 @@ def ReadOTP(args): if args.fake: result = b'08:00000000\n09:00000000\n10:00000000\n11:00000000\n12:00000000\n13:00000000\n14:00000000\n15:00000000\n16:00280000\n17:000008b0\n18:000008b0\n19:ffffffff\n20:ffffffff\n21:ffffffff\n22:ffffffff\n23:ffffffff\n24:ffffffff\n25:ffffffff\n26:ffffffff\n27:00005d5d\n28:58d2eb5c\n29:a72d14a3\n30:00c03111\n31:00000000\n32:00000000\n33:00000000\n34:00000000\n35:00000000\n36:00000000\n37:00000000\n38:00000000\n39:00000000\n40:00000000\n41:00000000\n42:00000000\n43:00000000\n44:00000000\n45:00000000\n46:00000000\n47:00000000\n48:00000000\n49:00000000\n50:00000000\n51:00000000\n52:00000000\n53:00000000\n54:00000000\n55:00000000\n56:00000000\n57:00000000\n58:00000000\n59:00000000\n60:00000000\n61:00000000\n62:00000000\n63:00000000\n64:00650000\n65:00000000\n66:00000000\n' else: - result = subprocess.check_output(['vcgencmd', 'otp_dump']) + if args.customer: + # The customer rows are not at a fixed place in otp_dump on every + # SoC (BCM2712 starts the dump at row 0 and uses a different + # layout), so ask the firmware for them directly + customer = subprocess.check_output(['vcmailbox', '0x00030021', '40', '40', '0', '8'] + ['0'] * 8).split() + for i, val in enumerate(customer[7:15]): + val = int(val, base=16) + print(i, "0x{:08x} b'{:032b}".format(val, val)) + return - result = result.split(b"\n") + result = subprocess.check_output(['vcgencmd', 'otp_dump']) - result.pop() # don't need the last entry, it's fluff + # The row numbers above only describe the BCM2711 and earlier layout + if not args.fake and is_bcm2712(): + row_info = {} - for i, val in enumerate(result): - result[i] = int(val[3:], base=16) + # Each line is ":"; rows are not contiguous and can have three digits + for line in result.splitlines(): + row, sep, val = line.partition(b":") + if not sep: + continue + row = int(row) + val = int(val, base=16) if args.customer: - if i >= customer_otp_offset - legacy_offset and i < 44 - legacy_offset: - print(i - (customer_otp_offset - legacy_offset), "0x{:08x} b'{:032b}".format(result[i], result[i])) + if row >= customer_otp_offset and row < customer_otp_offset + 8: + print(row - customer_otp_offset, "0x{:08x} b'{:032b}".format(val, val)) else : - print(i + legacy_offset, "0x{:08x} b'{:032b} {}".format(result[i], result[i], row_info.get(i + legacy_offset, ""))) - + print(row, "0x{:08x} b'{:032b} {}".format(val, val, row_info.get(row, ""))) + def WriteOTP(args): From ea7ee7ccea3636805f1e1ea5de6d3cd1e5ab7525 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Fri, 20 Nov 2026 10:54:44 +0000 Subject: [PATCH 2/4] otpset: Remove spaces before colons (cosmetic) Be consistent with space usage. --- otpset/otpset | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/otpset/otpset b/otpset/otpset index 6d99991..bfa36cf 100755 --- a/otpset/otpset +++ b/otpset/otpset @@ -96,7 +96,7 @@ def ReadOTP(args): if args.customer: if row >= customer_otp_offset and row < customer_otp_offset + 8: print(row - customer_otp_offset, "0x{:08x} b'{:032b}".format(val, val)) - else : + else: print(row, "0x{:08x} b'{:032b} {}".format(val, val, row_info.get(row, ""))) @@ -125,7 +125,7 @@ def WriteOTP(args): for i, val in enumerate(args.data): cmd.append(hex(int(val, 0))) - if args.verbose : + if args.verbose: print('Setting {} rows of customer OTP from row {} onwards'.format(num_rows, first_row)) print('Sending VC mailbox call: ',' '.join(cmd)) @@ -140,7 +140,7 @@ def LockOTP(args): cmd = ['vcmailbox', '0x00038021', '8', '8', '0xffffffff', '0xaffe0000' ] - if args.verbose : + if args.verbose: print('Sending VC mailbox call to lock OTP: ',' '.join(cmd)) print("Are you ABSOLUTELY sure you want to permamently disable changes to the customer OTP?. This operation is IRREVERSIBLE. Type YES to confirm") @@ -156,11 +156,11 @@ def LockOTP(args): args = ParseCommandLine() -if args.customer or args.pi : +if args.customer or args.pi: ReadOTP(args) sys.exit(0) -if args.lock : +if args.lock: LockOTP(args) sys.exit(0) From 6fbf1897b783b05eb74eb65a0bd476aac2a745c6 Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Fri, 20 Nov 2026 11:01:31 +0000 Subject: [PATCH 3/4] otpset: Remove unused legacy_offset --- otpset/otpset | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/otpset/otpset b/otpset/otpset index bfa36cf..4e82643 100755 --- a/otpset/otpset +++ b/otpset/otpset @@ -12,7 +12,6 @@ import sys customer_otp_offset = 36 -legacy_offset = 8 def ParseCommandLine(): parser = argparse.ArgumentParser(description='A helper application for reading and setting Raspberry Pi OTP (One Time Programmable) bits.\n Take care when using this application, changes to OTP are irreversible.', epilog="Script provided by Raspberry Pi Ltd under a BSD-3-Clause license") @@ -43,7 +42,7 @@ def is_bcm2712(): def ReadOTP(args): - # Map key is actually the value from the docs, so we do need to subtract/add the legacy_offset from it where necessary. + # Map key is the row number from the docs, which matches the row number in otp_dump row_info = { 17: "Bootmode register", 18: "Bootmode register copy", From 35403c1947fd0b7cc07e2e6ebe0cfcc5a7955e5a Mon Sep 17 00:00:00 2001 From: Phil Elwell Date: Fri, 20 Nov 2026 10:54:44 +0000 Subject: [PATCH 4/4] otpset: Name the customer OTP row count Replace the literal 8s that mean "number of customer OTP rows" with a customer_otp_rows constant, and derive the mailbox buffer size for the customer OTP read from it. --- otpset/otpset | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/otpset/otpset b/otpset/otpset index 4e82643..b1743db 100755 --- a/otpset/otpset +++ b/otpset/otpset @@ -12,12 +12,13 @@ import sys customer_otp_offset = 36 +customer_otp_rows = 8 def ParseCommandLine(): parser = argparse.ArgumentParser(description='A helper application for reading and setting Raspberry Pi OTP (One Time Programmable) bits.\n Take care when using this application, changes to OTP are irreversible.', epilog="Script provided by Raspberry Pi Ltd under a BSD-3-Clause license") parser.add_argument("data", nargs="*", type=str, help="Customer OTP data to set. 1 to 8 32 bit values. Can be decimal, hex (0x...), or binary (0b...)") - parser.add_argument("-r", "--row", type=int, help="Customer OTP start row. Valid values are 0 to 7", default=-1, choices=range(0,8), metavar="0-7") + parser.add_argument("-r", "--row", type=int, help="Customer OTP start row. Valid values are 0 to 7", default=-1, choices=range(0,customer_otp_rows), metavar="0-7") parser.add_argument("-c", "--customer", action='store_true', help="Read customer OTP bits") parser.add_argument("-p", "--pi", action='store_true', help="Read Raspberry Pi OTP bits") @@ -72,8 +73,9 @@ def ReadOTP(args): # The customer rows are not at a fixed place in otp_dump on every # SoC (BCM2712 starts the dump at row 0 and uses a different # layout), so ask the firmware for them directly - customer = subprocess.check_output(['vcmailbox', '0x00030021', '40', '40', '0', '8'] + ['0'] * 8).split() - for i, val in enumerate(customer[7:15]): + num_bytes = str(8 + customer_otp_rows * 4) + customer = subprocess.check_output(['vcmailbox', '0x00030021', num_bytes, num_bytes, '0', str(customer_otp_rows)] + ['0'] * customer_otp_rows).split() + for i, val in enumerate(customer[7:7 + customer_otp_rows]): val = int(val, base=16) print(i, "0x{:08x} b'{:032b}".format(val, val)) return @@ -93,7 +95,7 @@ def ReadOTP(args): val = int(val, base=16) if args.customer: - if row >= customer_otp_offset and row < customer_otp_offset + 8: + if row >= customer_otp_offset and row < customer_otp_offset + customer_otp_rows: print(row - customer_otp_offset, "0x{:08x} b'{:032b}".format(val, val)) else: print(row, "0x{:08x} b'{:032b} {}".format(val, val, row_info.get(row, ""))) @@ -113,7 +115,7 @@ def WriteOTP(args): num_rows = len(args.data) first_row = 0 if args.row == -1 else args.row - if (num_rows < 1 or num_rows > 8 - first_row): + if (num_rows < 1 or num_rows > customer_otp_rows - first_row): print("Invalid combination of start row ({}) and number of words to write to OTP ({})". format(first_row, num_rows)) sys.exit(-1)