You've already forked slimbootloader
mirror of
https://github.com/Dasharo/slimbootloader.git
synced 2026-03-06 15:26:20 -08:00
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 <maurice.ma@intel.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user