Last active
May 9, 2025 21:07
-
-
Save playday3008/ec0ed33ea6f799b86692f7b09f603f5c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create label and assign type from address array | |
| # @author PlayDay (@playday3008) | |
| # @category Analysis | |
| # @runtime PyGhidra | |
| from ghidra.program.model.symbol import SourceType | |
| from ghidra.program.model.data import DataTypeManager, Undefined | |
| from ghidra.util.data import DataTypeParser | |
| from ghidra.util.data.DataTypeParser import AllowedDataTypes | |
| from ghidra.app.services import DataTypeManagerService | |
| from ghidra.framework.plugintool import PluginTool | |
| # Get necessary Ghidra program references | |
| program = getCurrentProgram() | |
| memory = program.getMemory() | |
| listing = program.getListing() | |
| symTable = program.getSymbolTable() | |
| dataTypeManager = program.getDataTypeManager() | |
| dtParser = DataTypeParser(dataTypeManager, dataTypeManager, None, AllowedDataTypes.ALL) | |
| def get_data_type_manager_by_name(name): | |
| tool = state.getTool() | |
| service = tool.getService(DataTypeManagerService) | |
| if service is not None: | |
| for manager in service.getDataTypeManagers(): | |
| if name == manager.getName(): | |
| return manager | |
| return None | |
| def find_data_type_in_archives(type_name): | |
| # Try direct path lookup first | |
| dt = dataTypeManager.getDataType("/" + type_name) | |
| if dt is not None: | |
| print(f"Found type in program DTM: {type_name}") | |
| return dt | |
| # Try searching through all data types in program DTM | |
| dtIter = dataTypeManager.getAllDataTypes() | |
| while dtIter.hasNext(): | |
| dt = dtIter.next() | |
| if dt.getName() == type_name: | |
| print(f"Found type in program DTM by name: {type_name}") | |
| return dt | |
| # Get all available managers | |
| tool = state.getTool() | |
| service = tool.getService(DataTypeManagerService) | |
| if service is not None: | |
| managers = service.getDataTypeManagers() | |
| for manager in managers: | |
| if manager == dataTypeManager: | |
| continue | |
| # Try direct path lookup | |
| try: | |
| dt = manager.getDataType("/" + type_name) | |
| if dt is not None: | |
| print(f"Found type in {manager.getName()}: {type_name}") | |
| return dataTypeManager.addDataType(dt, None) | |
| except: | |
| pass | |
| # Search through all data types in this manager | |
| try: | |
| dtIter = manager.getAllDataTypes() | |
| while dtIter.hasNext(): | |
| dt = dtIter.next() | |
| if dt.getName() == type_name: | |
| print(f"Found type in {manager.getName()} by name: {type_name}") | |
| return dataTypeManager.addDataType(dt, None) | |
| except: | |
| continue | |
| print(f"Type not found: {type_name}") | |
| return None | |
| def split_type_path(type_name): | |
| parts = type_name.split('/') | |
| if len(parts) > 1: | |
| return '/'.join(parts[:-1]), parts[-1] | |
| return "", type_name | |
| # Array of address, label, and type information | |
| # Format: ["address", "label_name", "type_name"] | |
| target_data = [ | |
| ["0x4007C000", "AIPSTZ1", "AIPSTZ_Type"], | |
| ["0x40080000", "DCDC", "DCDC_Type"], | |
| ["0x40084000", "PIT", "PIT_Type"], | |
| ["0x40094000", "CMP1", "CMP_Type"], | |
| ["0x40094008", "CMP2", "CMP_Type"], | |
| ["0x40094010", "CMP3", "CMP_Type"], | |
| ["0x40094018", "CMP4", "CMP_Type"], | |
| ["0x400A4000", "IOMUXC_SNVS_GPR", "IOMUXC_SNVS_GPR_Type"], | |
| ["0x400A8000", "IOMUXC_SNVS", "IOMUXC_SNVS_Type"], | |
| ["0x400AC000", "IOMUXC_GPR", "IOMUXC_GPR_Type"], | |
| ["0x400B0000", "FLEXRAM", "FLEXRAM_Type"], | |
| ["0x400B4000", "EWM", "EWM_Type"], | |
| ["0x400B8000", "WDOG1", "WDOG_Type"], | |
| ["0x400BC000", "RTWDOG", "RTWDOG_Type"], | |
| ["0x400C0000", "GPIO5", "GPIO_Type"], | |
| ["0x400C4000", "ADC1", "ADC_Type"], | |
| ["0x400C8000", "ADC2", "ADC_Type"], | |
| ["0x400CC000", "TRNG", "TRNG_Type"], | |
| ["0x400D0000", "WDOG2", "WDOG_Type"], | |
| ["0x400D4000", "SNVS", "SNVS_Type"], | |
| #["0x400D8000", "CCM_ANALOG", "CCM_ANALOG_Type"], | |
| #["0x400D8000", "PMU", "PMU_Type"], | |
| #["0x400D8000", "TEMPMON", "TEMPMON_Type"], | |
| #["0x400D8000", "USB_ANALOG", "USB_ANALOG_Type"], | |
| #["0x400D8000", "XTALOSC24M", "XTALOSC24M_Type"], | |
| ["0x400D9000", "USBPHY1", "USBPHY_Type"], | |
| ["0x400DA000", "USBPHY2", "USBPHY_Type"], | |
| ["0x400DC000", "CSU", "CSU_Type"], | |
| ["0x400E0000", "TSC", "TSC_Type"], | |
| ["0x400E8000", "DMA0", "DMA_Type"], | |
| ["0x400EC000", "DMAMUX", "DMAMUX_Type"], | |
| #["0x400F4000", "GPC", "GPC_Type"], | |
| #["0x400F4000", "PGC", "PGC_Type"], | |
| ["0x400F8000", "SRC", "SRC_Type"], | |
| ["0x400FC000", "CCM", "CCM_Type"], | |
| ["0x4017C000", "AIPSTZ2", "AIPSTZ_Type"], | |
| ["0x40180000", "ROMC", "ROMC_Type"], | |
| ["0x40184000", "LPUART1", "LPUART_Type"], | |
| ["0x40188000", "LPUART2", "LPUART_Type"], | |
| ["0x4018C000", "LPUART3", "LPUART_Type"], | |
| ["0x40190000", "LPUART4", "LPUART_Type"], | |
| ["0x40194000", "LPUART5", "LPUART_Type"], | |
| ["0x40198000", "LPUART6", "LPUART_Type"], | |
| ["0x4019C000", "LPUART7", "LPUART_Type"], | |
| ["0x401A0000", "LPUART8", "LPUART_Type"], | |
| ["0x401AC000", "FLEXIO1", "FLEXIO_Type"], | |
| ["0x401B0000", "FLEXIO2", "FLEXIO_Type"], | |
| ["0x401B8000", "GPIO1", "GPIO_Type"], | |
| ["0x401BC000", "GPIO2", "GPIO_Type"], | |
| ["0x401C0000", "GPIO3", "GPIO_Type"], | |
| ["0x401C4000", "GPIO4", "GPIO_Type"], | |
| ["0x401C8000", "GPIO10", "GPIO_Type"], | |
| ["0x401D0000", "CAN1", "CAN_Type"], | |
| ["0x401D4000", "CAN2", "CAN_Type"], | |
| ["0x401D8000", "CAN3", "CAN_Type"], | |
| ["0x401DC000", "TMR1", "TMR_Type"], | |
| ["0x401E0000", "TMR2", "TMR_Type"], | |
| ["0x401E4000", "TMR3", "TMR_Type"], | |
| ["0x401E8000", "TMR4", "TMR_Type"], | |
| ["0x401EC000", "GPT1", "GPT_Type"], | |
| ["0x401F0000", "GPT2", "GPT_Type"], | |
| ["0x401F4000", "OCOTP", "OCOTP_Type"], | |
| ["0x401F8000", "IOMUXC", "IOMUXC_Type"], | |
| ["0x401FC000", "KPP", "KPP_Type"], | |
| ["0x4027C000", "AIPSTZ3", "AIPSTZ_Type"], | |
| ["0x402A4000", "FLEXSPI2", "FLEXSPI_Type"], | |
| ["0x402A8000", "FLEXSPI", "FLEXSPI_Type"], | |
| ["0x402C0000", "USDHC1", "USDHC_Type"], | |
| ["0x402C4000", "USDHC2", "USDHC_Type"], | |
| ["0x402D4000", "ENET2", "ENET_Type"], | |
| ["0x402D8000", "ENET", "ENET_Type"], | |
| ["0x402E0000", "USB1", "USB_Type"], | |
| #["0x402E0000", "USBNC1", "USBNC_Type"], | |
| #["0x402E0004", "USBNC2", "USBNC_Type"], | |
| ["0x402E0200", "USB2", "USB_Type"], | |
| ["0x402F0000", "SEMC", "SEMC_Type"], | |
| ["0x402FC000", "DCP", "DCP_Type"], | |
| ["0x4037C000", "AIPSTZ4", "AIPSTZ_Type"], | |
| ["0x40380000", "SPDIF", "SPDIF_Type"], | |
| ["0x40384000", "SAI1", "I2S_Type"], | |
| ["0x40388000", "SAI2", "I2S_Type"], | |
| ["0x4038C000", "SAI3", "I2S_Type"], | |
| ["0x40394000", "LPSPI1", "LPSPI_Type"], | |
| ["0x40398000", "LPSPI2", "LPSPI_Type"], | |
| ["0x4039C000", "LPSPI3", "LPSPI_Type"], | |
| ["0x403A0000", "LPSPI4", "LPSPI_Type"], | |
| ["0x403B0000", "ADC_ETC", "ADC_ETC_Type"], | |
| ["0x403B4000", "AOI1", "AOI_Type"], | |
| ["0x403B8000", "AOI2", "AOI_Type"], | |
| ["0x403BC000", "XBARA1", "XBARA_Type"], | |
| ["0x403C0000", "XBARB2", "XBARB_Type"], | |
| ["0x403C4000", "XBARB3", "XBARB_Type"], | |
| ["0x403C8000", "ENC1", "ENC_Type"], | |
| ["0x403CC000", "ENC2", "ENC_Type"], | |
| ["0x403D0000", "ENC3", "ENC_Type"], | |
| ["0x403D4000", "ENC4", "ENC_Type"], | |
| ["0x403DC000", "PWM1", "PWM_Type"], | |
| ["0x403E0000", "PWM2", "PWM_Type"], | |
| ["0x403E4000", "PWM3", "PWM_Type"], | |
| ["0x403E8000", "PWM4", "PWM_Type"], | |
| ["0x403EC000", "BEE", "BEE_Type"], | |
| ["0x403F0000", "LPI2C1", "LPI2C_Type"], | |
| ["0x403F4000", "LPI2C2", "LPI2C_Type"], | |
| ["0x403F8000", "LPI2C3", "LPI2C_Type"], | |
| ["0x403FC000", "LPI2C4", "LPI2C_Type"], | |
| ["0x42000000", "GPIO6", "GPIO_Type"], | |
| ["0x42004000", "GPIO7", "GPIO_Type"], | |
| ["0x42008000", "GPIO8", "GPIO_Type"], | |
| ["0x4200C000", "GPIO9", "GPIO_Type"], | |
| ["0x42020000", "FLEXIO3", "FLEXIO_Type"], | |
| ["0xE0080000", "CM7_MCM", "CM7_MCM_Type"], | |
| ] | |
| def apply_label_and_type(addr_str, label_name, type_name): | |
| try: | |
| # Convert address string to Address object | |
| addr = toAddr(addr_str) | |
| if addr is None or not memory.contains(addr): | |
| print(f"Invalid address: {addr_str}") | |
| return False | |
| # Create label at address | |
| if label_name: | |
| try: | |
| # Create or get the Peripherals namespace, with better error handling | |
| namespace = None | |
| try: | |
| namespace = symTable.getNamespace("Peripherals", None) | |
| except Exception: | |
| pass | |
| if namespace is None: | |
| namespace = symTable.createNameSpace(None, "Peripherals", SourceType.USER_DEFINED) | |
| if namespace is not None: | |
| # Check if label already exists and remove it | |
| existingSyms = symTable.getSymbols(label_name, namespace) | |
| for sym in existingSyms: | |
| symTable.removeSymbolSpecial(sym) | |
| # Create label within the Peripherals namespace | |
| symTable.createLabel(addr, label_name, namespace, SourceType.USER_DEFINED) | |
| print(f"Created label 'Peripherals::{label_name}' at {addr_str}") | |
| else: | |
| print(f"Failed to create/get namespace for label at {addr_str}") | |
| except Exception as e: | |
| print(f"Failed to create label at {addr_str}: {str(e)}") | |
| # Apply data type | |
| if type_name: | |
| try: | |
| # First try to find the data type in all archives | |
| dataType = find_data_type_in_archives(type_name) | |
| if dataType is None: | |
| # If not found in archives, try parsing it | |
| dataType = dtParser.parse(type_name) | |
| if dataType is not None: | |
| # Get the length of any existing data at this address | |
| existingData = listing.getDataAt(addr) | |
| if existingData is not None: | |
| endAddr = addr.add(existingData.getLength() - 1) | |
| else: | |
| endAddr = addr.add(dataType.getLength() - 1) | |
| # Clear any existing data | |
| listing.clearCodeUnits(addr, endAddr, False) | |
| # Create the data | |
| createdData = listing.createData(addr, dataType) | |
| if createdData is not None: | |
| print(f"Applied type '{type_name}' at {addr_str}") | |
| else: | |
| print(f"Failed to create data of type '{type_name}' at {addr_str}") | |
| else: | |
| print(f"Could not find or parse data type: {type_name}") | |
| except Exception as e: | |
| print(f"Failed to apply type at {addr_str}: {str(e)}") | |
| return True | |
| except Exception as e: | |
| print(f"Error processing {addr_str}: {str(e)}") | |
| return False | |
| def main(): | |
| print(f"Starting to process {len(target_data)} addresses...") | |
| success_count = 0 | |
| for entry in target_data: | |
| if len(entry) >= 3: | |
| addr_str, label_name, type_name = entry | |
| if apply_label_and_type(addr_str, label_name, type_name): | |
| success_count += 1 | |
| else: | |
| print(f"Invalid entry: {entry}") | |
| print(f"Processed {success_count} out of {len(target_data)} entries successfully") | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment