Skip to content

Instantly share code, notes, and snippets.

@N1kroks
Last active September 29, 2024 10:14
Show Gist options
  • Save N1kroks/0b3942a951a2d4504efe82ab82bc7a50 to your computer and use it in GitHub Desktop.
Save N1kroks/0b3942a951a2d4504efe82ab82bc7a50 to your computer and use it in GitHub Desktop.
from argparse import ArgumentParser
from typing import Any
def ParseArgs():
parser = ArgumentParser()
parser.add_argument('-i', dest="UefiPlatPath", required=True)
return parser.parse_args()
class MemoryDescriptor:
def __init__(self, name, space, base, size, hob_option, resource_attribute, arm_attribute, resource_type, cache_type):
self.name = str(name)
self.space = str(space)
self.base = int(base, 16)
self.size = int(size, 16)
self.hob_option = str(hob_option)
self.resource_attribute = str(resource_attribute)
self.arm_attribute = str(arm_attribute)
self.resource_type = str(resource_type)
self.cache_type = str(cache_type)
def toMessage(self):
return "{{{},{}0x{:08X}, 0x{:08X}, {}, {}, {}, {}, {}}},".format(self.name, self.space, self.base, self.size, self.hob_option, self.resource_attribute, self.arm_attribute, self.resource_type, self.cache_type)
def ParseLineToMemoryDescriptor(line):
if line[-1] == "\n":
line = line[0:-1]
base, size, name, hob, resource_attribute, arm_attribute, resource_type, cache_type = line.split(',')
while name[0] == " ":
name = name[1:]
while resource_attribute[0] == " ":
resource_attribute = resource_attribute[1:]
while arm_attribute[0] == " ":
arm_attribute = arm_attribute[1:]
while resource_type[0] == " ":
resource_type = resource_type[1:]
while cache_type[0] == " ":
cache_type = cache_type[1:]
space_count = hob.count(' ')
hob = hob[(space_count):]
return MemoryDescriptor(name, ' ' * space_count, base, size, hob, resource_attribute, arm_attribute, resource_type, cache_type)
def GapChecker(inDescriptors):
result = inDescriptors.copy()
descriptors = inDescriptors.copy()
last_desc = descriptors[0]
descriptors.pop(0)
for desc in descriptors:
if last_desc.base + last_desc.size < desc.base:
ram_region = MemoryDescriptor("\"RAM Partition\"", " ", "0x0", "0x0", "AddMem", "SYS_MEM", "SYS_MEM_CAP", "Conv", "WRITE_BACK_XN")
ram_region.base = last_desc.base + last_desc.size
ram_region.size = desc.base - (last_desc.base + last_desc.size)
if ram_region.size:
result.append(ram_region)
last_desc = desc
result.sort(key=lambda r: r.base)
return result
def ParseLineToConfig(line):
if line[-1] == "\n":
line = line[0:-1]
name, value = line.split(' = ')
while name[-1] == " ":
name = name[:-1]
while value[-1] == " ":
value = value[:-1]
if value[0] == '\"' or name == "ConfigParameterCount":
return ''
msg = "{{\"{}\", {}}},\n".format(name, value)
return msg
def Main(UefiPlatPath):
file = open(UefiPlatPath, "r", encoding="utf-8")
this_line = None
# Find [MemoryMap]
while this_line != "[MemoryMap]\n":
this_line = file.readline()
if this_line == '':
print("Invalid UefiPlat.cfg")
break
if this_line[0] == "#":
continue
# Parse [MemoryMap]
regions = []
while True:
this_line = file.readline()
if this_line == '' or this_line[0] == '[':
break
if this_line[0] == '\n' or this_line[0] == '#' and this_line[0:2] != '#-':
continue
if this_line[0:2] == "#-":
this_line = this_line.replace("-", "")[1:-1]
while this_line[-1] == " " or this_line[-1] == "\n":
this_line = this_line[:-1]
this_line = "//" + this_line + " Regions"
print(this_line)
continue
regions.append(ParseLineToMemoryDescriptor(this_line))
# Sort regions by base
regions.sort(key=lambda r: r.base)
regions = GapChecker(regions)
for region in regions:
print(region.toMessage())
# Find [RegisterMap]
while this_line != "[RegisterMap]\n":
this_line = file.readline()
if this_line == '':
print("Invalid UefiPlat.cfg")
break
if this_line[0] == "#":
continue
# Parse [RegisterMap]
while True:
this_line = file.readline()
if this_line == '' or this_line[0] == '[':
break
if this_line[0] == '\n' or this_line[0] == '#' and this_line[0:2] != '#-':
continue
if this_line[0:2] == "#-":
this_line = this_line.replace("-", "")[1:-1]
while this_line[-1] == " " or this_line[-1] == "\n":
this_line = this_line[:-1]
this_line = "\n//" + this_line + " Regions"
print(this_line)
continue
print(ParseLineToMemoryDescriptor(this_line).toMessage())
print("\n// Configuration Map")
# Find [ConfigParameters]
while this_line != "[ConfigParameters]\n":
this_line = file.readline()
if this_line == '':
print("Invalid UefiPlat.cfg")
break
if this_line[0] == "#":
continue
# Parse [ConfigParameters]
while True:
this_line = file.readline()
if this_line == '' or this_line[0] == '[':
break
if this_line[0] == '\n' or this_line[0] == '#' and this_line[0:2] != '#-':
continue
print(ParseLineToConfig(this_line), end='')
print("\nDynamic RAM Start Address: 0x{:08X}".format(regions[-1].base + regions[-1].size))
file.close()
if __name__ == '__main__':
Main(**vars(ParseArgs()))
@N1kroks
Copy link
Author

N1kroks commented Jun 2, 2024

It will print memory map and configuration map
Example:

$ python ./MemoryMapGenerator.py -i uefiplat.cfg
// DDR Regions
{"HYP",               0x80000000, 0x00600000, AddMem, MEM_RES, UNCACHEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"MPSS_EFS",          0x80600000, 0x00200000, AddMem, SYS_MEM, SYS_MEM_CAP, Reserv, UNCACHED_UNBUFFERED_XN},
{"RAM Partition",     0x80800000, 0x00020000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"AOP CMD DB",        0x80820000, 0x00020000, AddMem, MEM_RES, UNCACHEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"GPU PRR",           0x80840000, 0x00010000, AddMem, MEM_RES, WRITE_COMBINEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"RAM Partition",     0x80850000, 0x000B0000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"SMEM",              0x80900000, 0x00200000, AddMem, MEM_RES, UNCACHEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"PIL Reserved",      0x80B00000, 0x0EB00000, AddMem, MEM_RES, UNCACHEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"TGCM",              0x8F600000, 0x01400000, AddMem, MEM_RES, WRITE_COMBINEABLE, Reserv, UNCACHED_UNBUFFERED_XN},
{"RAM Partition",     0x90A00000, 0x03200000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"DXE Heap",          0x93C00000, 0x04500000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"RAM Partition",     0x98100000, 0x03700000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"Sched Heap",        0x9B800000, 0x00400000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK_XN},
{"RAM Partition",     0x9BC00000, 0x00400000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"Display Reserved",  0x9C000000, 0x01800000, AddMem, MEM_RES, SYS_MEM_CAP, Reserv, WRITE_THROUGH_XN},
{"RAM Partition",     0x9D800000, 0x02000000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"FV Region",         0x9F800000, 0x00200000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK_XN},
{"RAM Partition",     0x9FA00000, 0x00200000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"UEFI FD",           0x9FC00000, 0x00300000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK},
{"SEC Heap",          0x9FF00000, 0x0008C000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK_XN},
{"CPU Vectors",       0x9FF8C000, 0x00001000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK},
{"MMU PageTables",    0x9FF8D000, 0x00003000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK_XN},
{"UEFI Stack",        0x9FF90000, 0x00040000, AddMem, SYS_MEM, SYS_MEM_CAP, BsData, WRITE_BACK_XN},
{"RAM Partition",     0x9FFD0000, 0x00027000, AddMem, SYS_MEM, SYS_MEM_CAP, Conv, WRITE_BACK_XN},
{"Log Buffer",        0x9FFF7000, 0x00008000, AddMem, SYS_MEM, SYS_MEM_CAP, RtData, WRITE_BACK_XN},
{"Info Blk",          0x9FFFF000, 0x00001000, AddMem, SYS_MEM, SYS_MEM_CAP, RtData, WRITE_BACK_XN},

// Other Regions
{"AOP_SS_MSG_RAM",     0x0C300000, 0x00100000, NoHob, MMAP_IO, INITIALIZED, Conv, NS_DEVICE},
{"IMEM Base",          0x14680000, 0x0002B000, NoHob, MMAP_IO, INITIALIZED, Conv, NS_DEVICE},
{"IMEM Cookie Base",   0x146AA000, 0x00001000, AddDev, MMAP_IO, INITIALIZED, Conv, NS_DEVICE},

// Register Regions
{"GCC CLK CTL",        0x00100000, 0x00200000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"SECURITY CONTROL",   0x00780000, 0x00007000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"PRNG_CFG_PRNG",      0x00790000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"SDC1_REG",           0x007C0000, 0x00020000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"QUPV3_0_GSI",        0x00800000, 0x00100000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"QUPV3_1_GSI",        0x00A00000, 0x00100000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"UFS UFS REGS",       0x01D80000, 0x00020000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"CRYPTO0 CRYPTO",     0x01DC0000, 0x00040000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TCSR_TCSR_REGS",     0x01FC0000, 0x00040000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TLMM_WEST",          0x03500000, 0x00300000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TLMM_NORTH",         0x03900000, 0x00300000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TLMM_SOUTH",         0x03D00000, 0x00300000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"GPU_CC",             0x05090000, 0x00009000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"GPU_CPR",            0x0509A000, 0x00004000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"RPMH_CPRF",          0x0C200000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"QUPV3_SSC_GSI",      0x62B00000, 0x00060000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"PERIPH_SS",          0x08800000, 0x00200000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"MCCC_MCCC_MSTR",     0x090B0000, 0x00001000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"USB30_PRIM",         0x0A600000, 0x0011B000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"USB_RUMI",           0x0A720000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"PDC_DISPLAY",        0x0B2A0000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"PDC_DISP_SEQ",       0x0B4A0000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"RPMH_BCM_BCM_TOP",   0x0BA00000, 0x00200000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"SLP_CNTR_TSENS",     0x0C221000, 0x00003000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TSENS0_TM_PSHOLD",   0x0C263000, 0x00003000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"PMIC ARB SPMI",      0x0C400000, 0x02800000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"VIDEO_CC",           0x0AB00000, 0x00020000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"NPU_CC",             0x09980000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"TITAN_CAM_CC",       0x0AD00000, 0x00020000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"DISP_CC",            0x0AF00000, 0x00020000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"SILVER_CLK_CTL",     0x18280000, 0x00001000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"GOLD_CLK_CTL",       0x18282000, 0x00001000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"L3_CLK_CTL",         0x18284000, 0x00001000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"APSS_RSC_RSCCR",     0x18200000, 0x00030000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"APSS_GIC500_GICD",   0x17A00000, 0x00010000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"APSS_GIC500_GICR",   0x17A60000, 0x00100000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"QTIMER",             0x17C00000, 0x00110000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"APSS_ACTPM_WRAP",    0x18300000, 0x000B0000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"MDSS",               0x0AE00000, 0x00134000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},
{"SMMU",               0x15000000, 0x000D0000, AddDev, MMAP_IO, UNCACHEABLE, MmIO, NS_DEVICE},

// Configuration Map
{"NumCpusFuseAddr", 0x5C04C},
{"EnableShell", 0x1},
{"SharedIMEMBaseAddr", 0x146AA000},
{"DloadCookieAddr", 0x01FD3000},
{"DloadCookieValue", 0x10},
{"NumCpus", 4},
{"NumActiveCores", 8},
{"MaxLogFileSize", 0x400000},
{"USBHS1_Config", 0x0},
{"UsbFnIoRevNum", 0x00010001},
{"PwrBtnShutdownFlag", 0x0},
{"Sdc1GpioConfigOn", 0x1E92},
{"Sdc2GpioConfigOn", 0x1E92},
{"Sdc1GpioConfigOff", 0xA00},
{"Sdc2GpioConfigOff", 0xA00},
{"EnableSDHCSwitch", 0x1},
{"SecurityFlag", 0xC4},
{"TzAppsRegnAddr", 0x82200000},
{"TzAppsRegnSize", 0x02200000},
{"EnableLogFsSyncInRetail", 0x0},
{"DetectRetailUserAttentionHotkey", 0x00},
{"DetectRetailUserAttentionHotkeyCode", 0x17},
{"EnableOEMSetupAppInRetail", 0x0},
{"twoabl", 2},
{"EnableMultiThreading", 1},
{"MaxCoreCnt", 8},
{"EarlyInitCoreCnt", 1},
{"EnableDisplayThread", 0},
{"AllowNonPersistentVarsInRetail", 0x1},
{"EnableUefiSecAppDebugLogDump", 0x0},
{"DDRInfoNotifyFlag", 0x1},
{"MinidumpTALoadingCfg", 0x0},

Dynamic RAM Start Address: 0xA0000000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment