diff --git a/otpset/otpset b/otpset/otpset index ad60402..b1743db 100755 --- a/otpset/otpset +++ b/otpset/otpset @@ -12,13 +12,13 @@ import sys customer_otp_offset = 36 -legacy_offset = 8 +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") @@ -34,9 +34,16 @@ 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. + # 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", @@ -62,21 +69,37 @@ 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 + 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 - 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])) - else : - print(i + legacy_offset, "0x{:08x} b'{:032b} {}".format(result[i], result[i], row_info.get(i + legacy_offset, ""))) - + 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, ""))) + def WriteOTP(args): @@ -92,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) @@ -103,7 +126,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)) @@ -118,7 +141,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") @@ -134,11 +157,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)