Example #1
0
 def get_adt(infile):
     try:
         fdt = restruct.parse(FlattenedDeviceTree, infile)
         _, adt = from_fdt(fdt.structs)
         return adt
     except:
         infile.seek(0)
         return restruct.parse(AppleDeviceTree, infile)
Example #2
0
def regs(node, path):
    path = path[:]
    addrspaces = []
    last_addr_size = None
    last_size_size = None

    while True:
        props = {p.name: p.value for p in node.properties}

        if '#address-cells' in props and '#size-cells' in props:
            this_addr_size = props['#address-cells']
            this_size_size = props['#size-cells']
            if 'ranges' in props:
                range_spec = DeviceTreeRange[this_addr_size * 32,
                                             last_addr_size * 32,
                                             this_size_size * 32]
                ranges = restruct.parse(restruct.Arr(range_spec),
                                        props['ranges'])
                addrspaces.append(ranges)
            last_addr_size = this_addr_size
            last_size_size = this_size_size

        if not path:
            if 'reg' in props:
                reg_spec = DeviceTreeRegister[last_addr_size * 32,
                                              last_size_size * 32]
                regs = restruct.parse(restruct.Arr(reg_spec), props['reg'])
            else:
                regs = []
            break

        for child in node.children:
            cprops = {p.name: p.value for p in child.properties}
            cname = cprops['name']
            if cname == path[0]:
                path.pop(0)
                node = child
                break
        else:
            raise ValueError('404')

    rs = []
    for reg in regs:
        addr = reg.address

        for addrspace in reversed(addrspaces):
            for r in addrspace:
                if addr >= r.child_address and addr + reg.length <= r.child_address + r.length:
                    addr = r.parent_address + (addr - r.child_address)
                    break
            else:
                if addrspace:
                    raise ValueError(
                        'could not map child to parent address space')

        rs.append((addr, reg.length))

    return rs
Example #3
0
    def do_dump(args):
        nvram = restruct.parse(NVRAM, args.infile)

        for section in nvram.sections:
            name = section.header.name.rstrip('\x00')
            if not name:
                continue
            print(name + ':')
            for val in section.values:
                v = val.value
                if all(0x20 <= b <= 0x7E for b in v):
                    v = repr(v.decode('ascii'))
                print('  ' + val.key + ': ' + restruct.format_value(v, str))
Example #4
0
 def on_parse_data(self, spec, context):
     self.data = restruct.parse(
         FDT_PROPERTY_TYPES[determine_type(self.name, self.data)],
         self.data)
Example #5
0
 def do_conv_fdt(args):
     adt = restruct.parse(AppleDeviceTree, args.infile)
     n, dt = to_fdt(adt)
     restruct.emit(FlattenedDeviceTree, dt, args.outfile)
Example #6
0
 def on_parse_value(self, spec, context):
     self.value = restruct.parse(
         ADT_PROPERTY_TYPES[determine_type(self.name, self.value)],
         self.value)
Example #7
0
class MachO(Struct, partials={'C', 'S'}):
    magic: UInt(32)
    cpu_type: Enum(CPUType, UInt(24))
    cpu_type_flags: CPUTypeFlags
    cpu_sub_type: Switch(fallback=UInt(24),
                         options={
                             (CPUType.ARM, False): Enum(ARMSubType, UInt(24)),
                             (CPUType.ARM, True): Enum(ARM64SubType, UInt(24))
                         })
    cpu_sub_type_flags: UInt(8)
    file_type: Enum(FileType, UInt(32))
    command_count: UInt(32) @ C.count
    command_size: UInt(32) @ S.limit
    flags: UInt(32)
    reserved: UInt(32)
    commands: Sized(Arr(LoadCommand) @ C) @ S

    def on_parse_cpu_type_flags(self, spec, context):
        context.user.cpu_type = self.cpu_type
        spec.cpu_sub_type.selector = (self.cpu_type,
                                      self.cpu_type_flags.is_64_bit)


MachO_.resolve(MachO)

if __name__ == '__main__':
    import sys, os, os.path
    f = open(sys.argv[1], 'rb')
    m = restruct.parse(MachO, f)
    print(m)