From 0a6c955dbe14dbceab52228cea74baca69bdd87f Mon Sep 17 00:00:00 2001 From: Maurice Ma Date: Sun, 8 Dec 2019 10:02:27 -0800 Subject: [PATCH] Add bytes print function in CommonUtility There are many cases that it is required to print the values of byte-array for debug. It is not convenient in current SBL python scripts. This patch added a print_bytes() function in CommonUtility to provide generic function to print out a byte array object. Signed-off-by: Maurice Ma --- BootloaderCorePkg/Tools/CommonUtility.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/BootloaderCorePkg/Tools/CommonUtility.py b/BootloaderCorePkg/Tools/CommonUtility.py index 738cbaff..624d4116 100644 --- a/BootloaderCorePkg/Tools/CommonUtility.py +++ b/BootloaderCorePkg/Tools/CommonUtility.py @@ -17,6 +17,7 @@ import shutil import subprocess import struct import hashlib +import string from functools import reduce from ctypes import * @@ -61,6 +62,18 @@ HASH_DIGEST_SIZE = { "SM3_256" : 32, } +def print_bytes (data, indent=0, offset=0, show_ascii = True): + bytes_per_line = 16 + printable = ' ' + string.ascii_letters + string.digits + string.punctuation + str_fmt = '{:s}{:04x}: {:%ds} {:s}' % (bytes_per_line * 3) + bytes_per_line + data_array = bytearray(data) + for idx in range(0, len(data_array), bytes_per_line): + hex_str = ' '.join('%02X' % val for val in data_array[idx:idx + bytes_per_line]) + asc_str = ''.join('%c' % (val if (chr(val) in printable) else '.') + for val in data_array[idx:idx + bytes_per_line]) + print (str_fmt.format(indent * ' ', offset + idx, hex_str, ' ' + asc_str if show_ascii else '')) + def get_bits_from_bytes (bytes, start, length): value = bytes_to_value (bytes) bitlen = 8 * len(bytes)