From 8507da36a25db63e900a477d9b1435a5c50cc4a7 Mon Sep 17 00:00:00 2001 From: Jack Rosenthal Date: Thu, 19 Dec 2019 13:10:19 -0700 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/1997736 Reviewed-by: Jett Rink --- chip/mchp/util/pack_ec.py | 33 +++++++++++++-------------------- 1 file changed, 13 insertions(+), 20 deletions(-) (limited to 'chip') 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('