diff options
author | Jack Rosenthal <jrosenth@chromium.org> | 2019-12-19 13:10:19 -0700 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-01-13 21:19:35 +0000 |
commit | 8507da36a25db63e900a477d9b1435a5c50cc4a7 (patch) | |
tree | 46c9f71aa4b196cd4f0d8310cbda98800e918294 /chip | |
parent | b40d0ebfe31a1ed5503f6ae834dbac5a06487f22 (diff) | |
download | chrome-ec-8507da36a25db63e900a477d9b1435a5c50cc4a7.tar.gz |
mchp: convert pack_ec.py script to Python 3
Some changes to bytes/string handling to convert to Python 3. Most of
these changes came from the mec1322 script (since it seems large
sections were copy/pasted).
BUG=chromium:1031705
BRANCH=none
TEST=make BOARD=reef_mchp, ensure binary equivalent
Change-Id: Idffa2dbef7359aa176c1aafa1c504e9e29ee9d49
Signed-off-by: Jack Rosenthal <jrosenth@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1997736
Reviewed-by: Jett Rink <jettrink@chromium.org>
Diffstat (limited to 'chip')
-rwxr-xr-x | chip/mchp/util/pack_ec.py | 33 |
1 files changed, 13 insertions, 20 deletions
diff --git a/chip/mchp/util/pack_ec.py b/chip/mchp/util/pack_ec.py index c7fe74d028..22a147ed95 100755 --- a/chip/mchp/util/pack_ec.py +++ b/chip/mchp/util/pack_ec.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python2 +#!/usr/bin/env python3 # Copyright 2013 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be @@ -6,8 +6,6 @@ # A script to pack EC binary into SPI flash image for MEC17xx # Based on MEC170x_ROM_Description.pdf DS00002225C (07-28-17). -from __future__ import print_function - import argparse import hashlib import os @@ -16,9 +14,6 @@ import subprocess import tempfile import zlib # CRC32 -# from six import int2byte - - # MEC1701 has 256KB SRAM from 0xE0000 - 0x120000 # SRAM is divided into contiguous CODE & DATA # CODE at [0xE0000, 0x117FFF] DATA at [0x118000, 0x11FFFF] @@ -43,8 +38,7 @@ debug_print = dummy_print def Crc8(crc, data): """Update CRC8 value.""" - data_bytes = map(lambda b: ord(b) if isinstance(b, str) else b, data) - for v in data_bytes: + for v in data: crc = ((crc << 4) & 0xff) ^ (CRC_TABLE[(crc >> 4) ^ (v >> 4)]); crc = ((crc << 4) & 0xff) ^ (CRC_TABLE[(crc >> 4) ^ (v & 0xf)]); return crc ^ 0x55 @@ -63,7 +57,7 @@ def GetPayloadFromOffset(payload_file, offset): payload = bytearray(f.read()) rem_len = len(payload) % 64 if rem_len: - payload += '\0' * (64 - rem_len) + payload += b'\0' * (64 - rem_len) return payload def GetPayload(payload_file): @@ -72,11 +66,11 @@ def GetPayload(payload_file): def GetPublicKey(pem_file): """Extract public exponent and modulus from PEM file.""" - s = subprocess.check_output(['openssl', 'rsa', '-in', pem_file, - '-text', '-noout']) + result = subprocess.run(['openssl', 'rsa', '-in', pem_file, '-text', + '-noout'], stdout=subprocess.PIPE, encoding='utf-8') modulus_raw = [] in_modulus = False - for line in s.split('\n'): + for line in result.stdout.splitlines(): if line.startswith('modulus'): in_modulus = True elif not line.startswith(' '): @@ -86,8 +80,7 @@ def GetPublicKey(pem_file): if line.startswith('publicExponent'): exp = int(line.split(' ')[1], 10) modulus_raw.reverse() - modulus = bytearray(''.join(map(lambda x: chr(int(x, 16)), - modulus_raw[0:256]))) + modulus = bytearray((int(x, 16) for x in modulus_raw[:256])) return struct.pack('<Q', exp), modulus def GetSpiClockParameter(args): @@ -101,11 +94,11 @@ def GetSpiReadCmdParameter(args): return SPI_READ_CMD_LIST.index(args.spi_read_cmd) def PadZeroTo(data, size): - data.extend('\0' * (size - len(data))) + data.extend(b'\0' * (size - len(data))) def BuildHeader(args, payload_len, load_addr, rorofile): # Identifier and header version - header = bytearray(['P', 'H', 'C', 'M', '\0']) + header = bytearray(b'PHCM\0') # byte[5] b = GetSpiClockParameter(args) @@ -140,7 +133,7 @@ def BuildHeader(args, payload_len, load_addr, rorofile): def BuildHeader2(args, payload_len, load_addr, payload_entry): # Identifier and header version - header = bytearray(['P', 'H', 'C', 'M', '\0']) + header = bytearray(b'PHCM\0') # byte[5] b = GetSpiClockParameter(args) @@ -190,7 +183,7 @@ def HashByteArray(data): def SignByteArray(data): debug_print("Signature is SHA-256 of data") sigb = HashByteArray(data) - sigb.extend("\0" * 32) + sigb.extend(b'\0' * 32) return sigb @@ -521,7 +514,7 @@ def main(): if addr < s[0]: debug_print("Offset ",hex(addr)," Length", hex(s[0]-addr), "fill with 0xff") - f.write('\xff' * (s[0] - addr)) + f.write(b'\xff' * (s[0] - addr)) addr = s[0] debug_print("Offset ",hex(addr), " Length", hex(len(s[1])), "write data") @@ -531,7 +524,7 @@ def main(): if addr < spi_size: debug_print("Offset ",hex(addr), " Length", hex(spi_size - addr), "fill with 0xff") - f.write('\xff' * (spi_size - addr)) + f.write(b'\xff' * (spi_size - addr)) f.flush() |