Example #1
0
def load_file(f, neflags, format):
    idaapi.set_processor_type("arm:armv8", idaapi.SETPROC_LOADER)

    f.seek(-0x20, os.SEEK_END)
    nseg, = struct.unpack("<12xI16x", f.read(0x20))
    print(f"Number of segments: {nseg}")

    for sno in range(nseg):
        f.seek(-0x20-0x20*(nseg-sno), os.SEEK_END)
        mem_addr, file_addr, size, name = struct.unpack("<QII8x8s", f.read(0x20))
        name, _, _ = name.partition(b'\0')
        name = name.decode()
        print(f"Segment {sno}: {name} at mem={hex(mem_addr)} file={hex(file_addr)} size={hex(size)}")

        ida_seg_type = None
        if name == "__TEXT":
            ida_seg_type = "CODE"
        if name == "__DATA":
            ida_seg_type = "DATA"

        idaapi.add_segm(0, mem_addr, mem_addr + size, name, ida_seg_type)
        f.file2base(file_addr, mem_addr, mem_addr + size, True)

    f.seek(-0x20-0x20*nseg, os.SEEK_END)
    footer_start = f.tell()
    footer_end = footer_start + 0x20 + 0x20 * nseg
    idaapi.add_segm(0, footer_start, footer_end, "__FOOTER", "DATA")
    f.file2base(footer_start, footer_start, footer_end, True)

    header_start = footer_start + 0x20 * nseg
    idaapi.add_extra_line(header_start, True, "")
    idaapi.add_extra_cmt(header_start, True, f"File Header")
    idaapi.create_strlit(header_start, 4, 0)
    idaapi.set_cmt(header_start, "Magic", False)
    idaapi.create_dword(header_start + 4, 4)
    idaapi.set_cmt(header_start + 4, "Version?", False)
    idaapi.create_dword(header_start + 8, 4)
    idaapi.set_cmt(header_start + 8, "File length minus headers", False)
    idaapi.create_dword(header_start + 12, 4)
    idaapi.set_cmt(header_start + 12, "Section count", False)
    for sno in range(nseg):
        header_start = footer_start + 0x20 * sno
        idaapi.add_extra_line(header_start, True, "")
        idaapi.add_extra_cmt(header_start, True, f"Segment {sno + 1}")
        idaapi.create_qword(header_start, 8)
        idaapi.set_cmt(header_start, "Memory Address", False)
        idaapi.create_dword(header_start + 8, 4)
        idaapi.set_cmt(header_start + 8, "File Offset", False)
        idaapi.create_dword(header_start + 12, 4)
        idaapi.create_qword(header_start + 16, 8)
        idaapi.set_cmt(header_start + 12, "Segment Length", False)
        idaapi.create_strlit(header_start + 24, 8, 0)
        idaapi.set_cmt(header_start + 24, "Segment Name", False)

    idaapi.add_entry(0, 0, "start", 1)

    return 1
Example #2
0
 def __call__(self):
     idaapi.del_extra_cmt(self.ea, self.line_idx)
     isprev = 1 if self.line_idx - 1000 < 1000 else 0
     if not self.cmt:
         return 0
     idaapi.add_extra_cmt(self.ea, isprev, self.cmt.encode('utf-8'))
Example #3
0
def load_file(li, neflags, format):
    if format != ROM_FORMAT_NAME:
        Warning("Unknown format name: '%s'" % format)
        return 0
    jump = dwordAt(li, 0)
    # Test ARM branch
    if jump & 0xFF000000 != 0xEA000000:
        Warning("Unknown format name: '%s'" % format)
        return 0
    idaapi.set_processor_type("arm", idc.SETPROC_LOADER_NON_FATAL|idc.SETPROC_LOADER)
    li.seek(0, idaapi.SEEK_END)
    size = li.tell()

    # Adding Header Section
    idc.AddSeg(ROM_START, ROM_START + SIZE_HEADER, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(ROM_START, "HEADER")
    idc.set_segm_type(ROM_START, idc.SEG_CODE)
    li.seek(0)
    li.file2base(0, ROM_START, ROM_START + SIZE_HEADER, 0)

    # Adding OEP
    idaapi.add_entry(ROM_START, ROM_START, "start", 1)
    idaapi.cvar.inf.startIP = ROM_START
    idaapi.cvar.inf.beginEA = ROM_START

    # Adding ROM Section
    idc.AddSeg(ROM_START + SIZE_HEADER, ROM_START + (ROM_SIZE - SIZE_HEADER), 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(ROM_START + SIZE_HEADER, "ROM")
    idc.set_segm_type(ROM_START + SIZE_HEADER, idc.SEG_CODE)
    li.seek(SIZE_HEADER)
    li.file2base(0, ROM_START + SIZE_HEADER, ROM_START + size, 0)

    # Adding EWRAM
    idc.AddSeg(0x02000000, 0x02040000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(0x02000000, "EWRAM")
    memset_seg(0x02000000, 0x40000)

    # Adding IWRAM
    idc.AddSeg(0x03000000, 0x03008000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(0x03000000, "IWRAM")
    memset_seg(0x03000000, 0x8000)

    # Adding IO / Registers
    idc.AddSeg(0x04000000, 0x04000400, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(0x04000000, "IOregisters")
    memset_seg(0x04000000, 0x400)

    # Adding BIOS System ROM
    idc.AddSeg(0x00000000, 0x00004000, 0, 1, idaapi.saRelPara, idaapi.scPub)
    idc.set_segm_name(0x00000000, "BIOS")
    memset_seg(0x00000000, 0x4000)
    idc.set_segm_type(0x0000000, idc.SEG_CODE)

    idaapi.add_extra_cmt(ROM_START, True, "ROM HEADER")
    li.seek(0xA0)
    idc.update_extra_cmt(ROM_START, 1,  "; Game Title : %s" % li.read(12))
    idc.update_extra_cmt(ROM_START, 2,  "; Game Code : %s" % li.read(4))
    idc.update_extra_cmt(ROM_START, 3,  "; Marker Code : %s" % li.read(2))
    idc.update_extra_cmt(ROM_START, 4,  "; Fixed value : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.update_extra_cmt(ROM_START, 5,  "; Main unit code : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.update_extra_cmt(ROM_START, 6,  "; Device type : %02X" % struct.unpack("<B", li.read(1))[0])
    idc.update_extra_cmt(ROM_START, 7,  "; Reserved Area : db 7 dup(0)")
    li.read(7)
    idc.update_extra_cmt(ROM_START, 8,  "; Software version %02X" % struct.unpack("<B", li.read(1))[0])
    idc.update_extra_cmt(ROM_START, 9,  "; Complement Check %02X" % struct.unpack("<B", li.read(1))[0])
    idc.update_extra_cmt(ROM_START, 10,  "; Reserved Area : db 2 dup(0)")
    
    io_naming()
    print("[+] Load OK")
    return 1