import struct import sys, os, pathlib, struct, time, io, tqdm, logging import string import typing SUCCESS_STATUS = 0 ERROR_STATUS = 4 KB = 1024 MB = KB * 1024 GB = MB * 1024 TB = GB * 1024 PB = TB * 1024 COLOR_PURPLE = '\033[95m' COLOR_BLUE = '\033[94m' COLOR_LBLUE = '\033[96m' COLOR_GREEN = '\033[92m' COLOR_YELLOW = '\033[93m' COLOR_RED = '\033[91m' COLOR_END = '\033[0m' COLOR_BOLD = '\033[1m' COLOR_UNDERLINE = '\033[4m' # Python 2 support try: input = raw_input except NameError: pass def info(msg): print("[\033[34;1mi\033[0m] %s" % (msg)) def ok(msg): print("[\033[32;1m+\033[0m] %s" % (msg)) def warn(msg): print("[\033[33;1mw\033[0m] %s" % (msg)) def error(msg): print("[\033[31;1m!\033[0m] %s" % (msg)) def p64(val): return struct.pack(" int Unpack 32 bits integer from a little endian str representation """ return struct.unpack(' str Pack 32 bits integer (little endian) """ return struct.pack(' int Round down ``address`` to the nearest increment of ``align``. """ return address & ~(align-1) def page_align(address): """page_align(address) -> int Round down ``address`` to the nearest page boundary. """ return round_down(address, 0x1000) def align_top(address, align): """page_align_up(address) -> int Round up ``address`` to the nearest page boundary. """ return round_down(address + align, align) def page_align_top(address): """page_align_up(address) -> int Round up ``address`` to the nearest page boundary. """ return round_down(address + 0x1000, 0x1000) def util_get_mmu_entry(base, address): # May not work on other SDM # work only for level 1 return base + 8 * (0x200 + ((page_align(address) >> 12) & 0x1FF)) def simple_hexdump(src, length=16, sep='.', start = 0): FILTER = ''.join([(len(repr(chr(x))) == 3) and chr(x) or sep for x in range(256)]) lines = [] for c in range(0, len(src), length): chars = src[c:c+length] hexstr = ' '.join(["%02x" % ord(x) for x in chars]) if type(chars) is str else ' '.join(['{:02x}'.format(x) for x in chars]) if len(hexstr) > 24: hexstr = "%s %s" % (hexstr[:24], hexstr[24:]) printable = ''.join(["%s" % ((ord(x) <= 127 and FILTER[ord(x)]) or sep) for x in chars]) if type(chars) is str else ''.join(['{}'.format((x <= 127 and FILTER[x]) or sep) for x in chars]) lines.append("%08x: %-*s |%s|" % (c + start, length*3, hexstr, printable)) return lines def print_addr64(addr : bytes): print(hex(struct.unpack("