Exemple #1
0
    def __call__(self):
        """Generate module headers, pack modules into single file."""

        def compute_signature(fin):
            """Compute sha256 signature of module."""

            crypto = hashlib.sha256()
            chunk = fin.read(BLOCK_SIZE)
            while chunk:
                crypto.update(chunk)
                chunk = fin.read(BLOCK_SIZE)
            return crypto.digest()

        # Gather module info from config and hdr files.
        cfg_parser = common.HoffloadConfigParser(self.args.config,
                                                 enum=self.args.enum_file)
        module_cnt = len(self.args.module)
        with open(self.args.result, 'wb') as fp:
            for counter, module in enumerate(self.args.module):
                module_name = os.path.splitext(
                    os.path.basename(module).strip())[0]
                logging.info('Packing module %s', module_name)
                header = Header()
                header.loc_idx = cfg_parser.get_module_index(module_name)
                header.id = cfg_parser.get_module_id(module_name)
                module_size = os.stat(module).st_size
                header.size = module_size
                pad = BLOCK_SIZE - ((module_size + ctypes.sizeof(header)) %
                                    BLOCK_SIZE)
                if counter < module_cnt - 1:
                    # Set the "next" bit (bit 16 of offset fields).
                    header.pad = (pad | 0x8000)
                else:
                    header.pad = pad
                # Truncate to size allowed by header.
                header.name = module_name[:MAX_NAME]
                # Compute checksum
                with open(module, 'rb') as fin:
                    header.digest = (zlib.crc32(fin.read(), 0) &
                                     CRC32_MASK) ^ CRC32_MASK
                    # Compute digital signature of module and write it
                    fin.seek(0)
                    sign = compute_signature(fin)
                    header.pad -= len(sign)
                    # Write module header and digital signature.
                    fp.write(header)
                    fp.write(sign)
                    pad -= len(sign)
                    # write module image
                    fin.seek(0)
                    fp.write(fin.read())
                # Fill to block size.
                if pad:
                    fp.write('\0' * pad)
Exemple #2
0
    def __call__(self):
        """Replace hoffload placeholder(s) in loader template."""

        # Read config.
        cfg_parser = common.HoffloadConfigParser(self.args.config)
        hoffload_reg = {}
        try:
            hoffload_reg[common.HOFFLOAD_MEM_TAG] = cfg_parser.get_element(
                common.HOFFLOAD_MEM_TAG)
        except ValueError:
            raise common.Error(self.args.config +
                               ', %s not found\n' % common.HOFFLOAD_MEM_TAG)

        def format_hoffload_reg(reg_name, reg_size):
            """Format hoffload region.

            Result:
                reg_name = .;
                . = . + reg_size;
                reg_name_end = .;
            """
            return """
            %s = .;
            . = . + %s;
            %s_end = .;
            """ % (reg_name, reg_size, reg_name)

        found = False
        lines = open(self.args.ldsin, 'r').readlines()
        with open(self.args.ldsin, 'w') as out:
            for line in lines:
                out.write(line)
                if HOFFLOAD_SECTION in line:
                    found = True
                if found and '}' in line:
                    out.write(''.join(
                        format_hoffload_reg(
                            common.HOFFLOAD_MEM_TAG,
                            hoffload_reg[common.HOFFLOAD_MEM_TAG])))
                    found = False
Exemple #3
0
 def __init__(self, *args, **kws):
     super(GenJumpTable, self).__init__(*args, **kws)
     self.functions = []
     self.symbol_list = []
     self.cparser = common.HoffloadConfigParser(self.args.config,
                                                enum=self.args.enum_file)