#!/usr/bin/python3
# -*- coding: utf-8 -*-
# (c) B.Kerler 2018-2021 MIT License
ErrorCodes = {
    0x0: "OK",
    0x1000: "CRYPTO_INIT_FAIL",
    0x1001: "CRYPTO_DEINIT_FAIL",
    0x1002: "CRYPTO_MODE_INVALID",
    0x1003: "CRYPTO_KEY_INVALID",
    0x1004: "CRYPTO_DATA_UNALIGNED",
    0X1005: "CRYPTO_SEEK_LEN_ERROR",
    0x1D00: "ADDRESS_TOO_HIGH_80000000",
    0x1D01: "ADDRESS_TOO_HIGH_40000000",
    0x1D02: "INVALID_ADDRESS",
    0x1D0C: "",
    0x1D0D: "SLA_ERROR",
    0x1D0E: "DA_OVERLAP",
    0x1D0F: "DA_INVALID_JUMP_ADDR",
    0x1D10: "DA_EXCEED_MAX_DA_BLOCKS",
    0x1D11: "DA_ADDR_NOT_16BITS_ALIGNED",
    0x1D12: "DA_INVALID_ADDR_AND_LEN",
    0x1D13: "DA_LEN_IS_ZERO",
    0x1D14: "DA_SIG_LEN_EXCEED_DA_LEN",
    0x1D15: "DA_SIG_LEN_IS_ZERO_AND_DAA_ACTIVE",
    0x1D16: "DA_NOT_EXIST",
    0x1D17: "DA_BLOCKS_INCONSISTENT_WITH_TOOL_AUTH",
    0x1D21: "DA_VERSION_NOT_SUPPORTED",
    0x2000: "AUTH_IMAGE_VERIFY_FAIL",
    0x2001: "DA_IMAGE_SIG_VERIFY_FAIL",
    0x2002: "DA_IMAGE_NO_MEM_FAIL",
    0x2003: "DA_INIT_KEY_FAIL",
    0x2004: "IMG_INIT_KEY_FAIL",
    0x2005: "HASH_IMAGE_FAIL",
    0x2006: "DA_RELOCATE_SIZE_NOT_ENOUGH",
    0x3000: "LIB_SEC_CFG_NOT_EXIST",
    0x3001: "LIB_VER_INVALID",
    0x3002: "LIB_SEC_CFG_ERASE_FAIL",
    0x3003: "LIB_SEC_CFG_CANNOT_WRITE",
    0x3004: "LIB_SEC_CFG_END_PATTERN_NOT_EXIST",
    0x3005: "LIB_SEC_CFG_STATUS_INVALID",
    0x3006: "LIB_SEC_CFG_READ_SIZE_NOT_ENOUGH",
    0x3007: "LIB_SEC_CFG_RSA_KEY_INIT_FAIL",
    0x4000: "IMG_VERIFY_THIS_IMG_INFO_NOT_EXIST",
    0x4001: "IMG_VERIFY_HASH_COMPARE_FAIL",
    0x4002: "IMG_VERIFY_NO_SPACE_ADD_IMG_INFO",
    0x4003: "SEC_DL_TOKEN_NOT_FOUND_IN_IMG",
    0x4004: "SEC_DL_FLOW_ERROR",
    0x4005: "IMG_VERIFY_INVALID_IMG_INFO_ATTR",
    0x4006: "IMG_SECROIMG_NOT_FOUND",
    0x4007: "IMG_READ_FAIL",
    0x4008: "IMG_VERIFY_SIGNATURE_FAIL",
    0x4009: "IMG_SIGN_FORMAT_NOT_MATCH",
    0x400A: "IMG_EXTENSION_HDR_NOT_FOUND",
    0x400B: "IMG_EXTENSION_MAGIC_WRONG",
    0x400C: "IMG_EXTENSION_TYPE_NOT_SUPPORT",
    0x400D: "IMG_EXTENSION_HASH_CAL_FAIL",
    0x5000: "IMG_LOCK_TABLE_NOT_EXIST",
    0x5001: "IMG_LOCK_ALL_LOCK",
    0x5002: "IMG_LOCK_NO_SPACE_ADD_LOCK_INFO",
    0x5003: "IMG_LOCK_THIS_IMG_INFO_NOT_EXIST",
    0x5004: "IMG_LOCK_MAGIC_ERROR",
    0x6000: "SBC_KEY_NOT_FOUND",
    0x6001: "BR_SEC_CFG_NOT_FOUND",
    0x7000: "REGION_INVALID_INCLUDE",
    0x7001: "REGION_INVALID_OVERLAP",
    0x7002: "REGION_INVALID_OVERFLOW",
    0x7003: "DA_INVALID_LOCATION",
    0x7004: "DA_INVALID_LENGTH",
    0x7015: "DAA_Security_Error_Signature",
    0x7017: "DAA_Security_Error",
    0x7024: "DAA_SIG_VERIFY_FAILED",
    0x8000: "INVALID_SIG_SZ",
    0x8001: "INVALID_PADDING_SZ",
    0x9000: "SW_ID_MISMATCH",
    0x9001: "INVALID_IMG_TYPE",
    0x9002: "BUF_NOT_ENOUGH_FOR_CERT",
    0x9003: "IMG_NOT_FOUND",
    0x9004: "IMG_HDR_HASH_VFY_FAIL",
    0x9005: "IMG_HASH_VFY_FAIL",
    0x9006: "INVALID_OID_SZ",
    0x9007: "INVALID_OID_BUF_SZ",
    0x9008: "END_OF_CERT",
    0x9009: "CERT_OBJ_ID_NOT_MATCH",
    0x900a: "CERT_OBJ_SZ_NOT_MATCH",
    0x900b: "CERT_TRAVERSE_UNKNOWN_MODE",
    0x900c: "INVALID_OID_IDX",
    0x900d: "OID_NOT_FOUND",
    0x900e: "OBJ_NOT_FOUND",
    0x900f: "E_KEY_LEN_NOT_MATCH",
    0x9010: "E_KEY_NOT_MATCH",
    0x9011: "N_KEY_LEN_NOT_MATCH",
    0x9012: "N_KEY_NOT_MATCH",
    0x9013: "PUBK_AUTH_FAIL",
    0x9014: "INVALID_IMG_HDR",
    0x9015: "NOT_EXPECTED_IMG",
    0xA000: "CERT_IMG_VER_NOT_SYNC",
    0xA001: "IMAGE_IMG_VER_NOT_SYNC",
    0xA002: "IMG_VER_ROLLBACK",
    0xA003: "OTP_FIELD_NOT_ENOUGH",
    0xA004: "OTP_REG_BIT_CONFIG_WRONG",
    0xA005: "OTP_WRITE_FAIL"
}

COMMON=1
AUTHENTICATION=2
LIBRARY=3
DEV_HW=4
FUNCTION=5
BROM=6
DA=7
PRELOADER=8

SUCCESS=(0 << 30)
INFO=(1 << 30)
WARNING=(2 << 30)
ERROR=(3 << 30)

ErrorCodes_XFlash={
    0:"OK",
    0xc0010001: "Error",
    0xc0010002: "Abort",
    0xc0010003: "Unsupported command",
    0xc0010004: "Unsupported ctrl code",
    0xc0010005: "Protocol error",
    0xc0010006: "Protocol buffer overflow",
    0xc0010007: "Insufficient buffer",
    0xc0010008: "USB SCAN error",
    0xc0010009: "Invalid hsession",
    0xc001000A: "Invalid session",
    0xc001000B: "Invalid stage",
    0xc001000C: "Not implemented",
    0xc001000D: "File not found",
    0xc001000E: "Open file error",
    0xc001000F: "Write file error",
    0xc0010010: "Read file error",
    0xc0010011: "Create File error / Unsupported Version",
    0xc0020001: "Rom info not found",
    0xc0020002: "Cust name not found",
    0xc0020003: "Device not supported",
    0xc0020004: "DL forbidden",
    0xc0020005: "Img too large",
    0xc0020006: "PL verify fail",
    0xc0020007: "Image verify fail",
    0xc0020008: "Hash operation fail",
    0xc0020009: "Hash binding check fail",
    0xc002000a: "Invalid buf",
    0xc002000b: "Binding hash not available",
    0xc002000c: "Write data not allowed",
    0xc002000d: "Format not allowed",
    0xc002000e: "SV5 public key auth failed",
    0xc002000f: "SV5 hash verify failed",
    0xc0020010: "SV5 RSA OP failed",
    0xc0020011: "SV5 RSA verify failed",
    0xc0020012: "SV5 GFH not found",
    0xc0020013: "Cert1 invalid",
    0xc0020014: "Cert2 invalid",
    0xc0020015: "Imghdr invalid",
    0xc0020016: "Sig size invalid",
    0xc0020017: "RSA pss op fail",
    0xc0020018: "Cert auth failed",
    0xc0020019: "Public key auth mismatch n size",
    0xc002001a: "Public key auth mismatch e size",
    0xc002001b: "Public key auth mismatch n",
    0xc002001c: "Public key auth mismatch e",
    0xc002001d: "Public key auth mismatch hash",
    0xc002001e: "Cert obj not found",
    0xc002001f: "Cert oid not found",
    0xc0020020: "Cert out of range",
    0xc0020021: "Oid doesn't match",
    0xc0020022: "Length doesn't match",
    0xc0020023: "ASN1 unknown op",
    0xc0020024: "OID index out of range",
    0xc0020025: "OID too large",
    0xc0020026: "Public key size mismatch",
    0xc0020027: "SWID mismatch",
    0xc0020028: "Hash size mismatch",
    0xc0020029: "IMGHDR type mismatch",
    0xc002002a: "IMG type mismatch",
    0xc002002b: "IMGHDR hash verify failed",
    0xc002002c: "IMG has verify failed",
    0xc002002d: "Anti rollback violation",
    0xc002002e: "SECCFG not found",
    0xc002002f: "SECCFG magic incorrect",
    0xc0020030: "SECCFG invalid",
    0xc0020031: "Cipher mode invalid",
    0xc0020032: "Cipher key invalid",
    0xc0020033: "Cipher data unaligned",
    0xc0020034: "GFH file info not found",
    0xc0020035: "GFH anti clone not found",
    0xc0020036: "GFH sec cfg not found",
    0xc0020037: "Unsupported source type",
    0xc0020038: "Cust name mismatch",
    0xc0030001: "Scatter file invalid",
    0xc0030002: "DA file invalid",
    0xc0030003: "DA selection error",
    0xc0030004: "Preloader invalid",
    0xc0030005: "EMI hdr invalid",
    0xc0030006: "Storage mismatch",
    0xc0030007: "Invalid parameters",
    0xc0030008: "Invalid GPT",
    0xc0030009: "Invalid PMT",
    0xc003000a: "Layout changed",
    0xc003000b: "Invalid format param",
    0xc003000c: "Unknown storage section type",
    0xc003000d: "Unknown scatter field",
    0xc003000e: "Partition tbl doesn't exist",
    0xc003000f: "Scatter hw chip id mismatch",
    0xc0030010: "SEC cert file not found",
    0xc0030011: "SEC auth file not found",
    0xc0030012: "SEC auth file needed",
    0xc0030013: "EMI containter file not found",
    0xc0030014: "Scatter file not found",
    0xc0030015: "Xml file op error",
    0xc0030016: "Unsupported page size",
    0xc0030017: "EMI info length offset invalid",
    0xc0030018: "EMI info length invalid",
    0xc0040001: "Unsupported operation",
    0xc0040002: "Thread error",
    0xc0040003: "Checksum error",
    0xc0040004: "Unknown sparse",
    0xc0040005: "Unknown sparse chunk type",
    0xc0040006: "Partition not found",
    0xc0040007: "Read parttbl failed",
    0xc0040008: "Exceeded max partition number",
    0xc0040009: "Unknown storage type",
    0xc004000A: "Dram Test failed",
    0xc004000b: "Exceed available range",
    0xc004000c: "Write sparse image failed",
    0xc0040030: "MMC error",
    0xc0040040: "Nand error",
    0xc0040041: "Nand in progress",
    0xc0040042: "Nand timeout",
    0xc0040043: "Nand bad block",
    0xc0040044: "Nand erase failed",
    0xc0040045: "Nand page program failed",
    0xc0040050: "EMI setting version error",
    0xc0040060: "UFS error",
    0xc0040100: "DA OTP not supported",
    0xc0040102: "DA OTP lock failed",
    0xc0040200: "EFUSE unknown error",
    0xc0040201: "EFUSE write timeout without verify",
    0xc0040202: "EFUSE blown",
    0xc0040203: "EFUSE revert bit",
    0xc0040204: "EFUSE blown partly",
    0xc0040205: "EFUSE invalid argument",
    0xc0040206: "EFUSE value is not zero",
    0xc0040207: "EFUSE blown incorrect data",
    0xc0040208: "EFUSE broken",
    0xc0040209: "EFUSE blow error",
    0xc004020A: "EFUSE data process error",
    0xc004020B: "EFUSE unlock bpkey error",
    0xc004020C: "EFUSE create list error",
    0xc004020D: "EFUSE write register error",
    0xc004020E: "EFUSE padding type mismatch",
    0xc0050001: "Device ctrl exception",
    0xc0050002: "Shutdown Cmd exception",
    0xc0050003: "Download exception",
    0xc0050004: "Upload exception",
    0xc0050005: "Ext Ram exception",
    0xc0050006: "Notify Switch Usb Speed exception",
    0xc0050007: "Read data exception",
    0xc0050008: "Write data exception",
    0xc0050009: "Format exception",
    0xc005000A: "OTP operation exception",
    0xc005000B: "Switch usb exception",
    0xc005000C: "Write efuse exception",
    0xc005000D: "Read efuse exception",
    0xc0060001: "Brom start cmd/connect not preloader failed",
    0xc0060002: "Brom get bbchip hw ver failed",
    0xc0060003: "Brom cmd send da failed",
    0xc0060004: "Brom cmd jump da failed",
    0xc0060005: "Brom cmd failed",
    0xc0060006: "Brom stage callback failed",
    0xc0070001: "DA Version mismatch",
    0xc0070002: "DA not found",
    0xc0070003: "DA section not found",
    0xc0070004: "DA hash mismatch",
    0xc0070005: "DA exceed max num"
}

class ErrorHandler:
    def __init__(self):
        self.ec = ErrorCodes
        self.xec = ErrorCodes_XFlash

    def status(self, status):
        if status in self.ec:
            return self.ec[status]+" ("+hex(status)+")"
        if status in self.xec:
            return self.xec[status]+" ("+hex(status)+")"
        return "Unknown: " + hex(status)
