Ejemplo n.º 1
0
    def _set_register_fields(self, reg, reg_node):
        fields_node_list = reg_node.findall("./reg_fieldsets/fields")

        for fields_node in fields_node_list:
            fieldset = Fieldset(int(reg.size))
            fieldset_condition_node = fields_node.find("./fields_condition")
            field_node_list = fields_node.findall("./field")
            dbg_msg = "fieldset: "

            if fieldset_condition_node is not None:
                fieldset.condition = fieldset_condition_node.text

            for field_node in field_node_list:
                name_node = field_node.find("./field_name")
                if name_node is None:
                    name_node = field_node.find("./field_shortdesc")
                if name_node is None:
                    error_msg = str(reg.name) + " invalid field"
                    logger.error(error_msg)
                    raise ShoulderParserException(error_msg)

                name = name_node.text
                name = name.replace(" ", "_")
                msb = int(field_node.find("./field_msb").text)
                lsb = int(field_node.find("./field_lsb").text)
                fieldset.add_field(name, msb, lsb)
                dbg_msg += str(name) + " (" + str(msb) + ":" + str(lsb) + ") "

            reg.add_fieldset(fieldset)
            logger.debug(dbg_msg)
Ejemplo n.º 2
0
 def _set_register_name(self, reg, reg_node):
     name_node = reg_node.find("./reg_short_name")
     if name_node is not None:
         reg.name = name_node.text
         logger.debug("name = " + reg.name)
     else:
         logger.error("register name not found")
         raise ShoulderParserException()
Ejemplo n.º 3
0
    def _generate_register_set(self, outfile, reg):
        """
        Generate a C function that writes the given register
        """

        if not reg.is_writeable():
            return

        rname = reg.name.lower()
        prefix = self._register_function_prefix(reg)
        suffix = config.register_write_function
        size_type = self._register_size_type(reg)

        gadget = self.gadgets["shoulder.c.function_definition"]
        gadget.name = prefix + "_" + rname + "_" + suffix
        gadget.return_type = "void"
        gadget.args = [(size_type, "val")]

        if reg.access_mechanisms["msr_register"]:
            am = reg.access_mechanisms["msr_register"][0]
            if config.encoded_functions:
                self._generate_aarch64_encoded_set(outfile, reg, am)
            else:
                self._generate_msr_register_set(outfile, reg, am)

        elif reg.access_mechanisms["mcr"]:
            am = reg.access_mechanisms["mcr"][0]
            self._generate_mcr_set(outfile, reg, am)

        elif reg.access_mechanisms["mcrr"]:
            gadget.args = [("uint64_t", "val")]

            am = reg.access_mechanisms["mcrr"][0]
            self._generate_mcrr_set(outfile, reg, am)

        elif reg.access_mechanisms["msr_banked"]:
            am = reg.access_mechanisms["msr_banked"][0]
            self._generate_msr_banked_set(outfile, reg, am)

        elif reg.access_mechanisms["vmsr"]:
            am = reg.access_mechanisms["vmsr"][0]
            self._generate_vmsr_set(outfile, reg, am)

        elif reg.access_mechanisms["str"]:
            am = reg.access_mechanisms["str"][0]
            if not reg.is_readable():
                self._generate_external_constants(outfile, reg, am)
            self._generate_str_set(outfile, reg, am)

        elif reg.access_mechanisms["msr_immediate"]:
            msg = "MSRimmediate access mechanism not supported for register {r}"
            logger.warn(msg.format(r=reg.name.lower()))

        else:
            msg = "Failed to generate {f} function for writeable register {r}"
            msg = msg.format(f=config.register_write_function,
                             r=reg.name.lower())
            logger.error(msg)
Ejemplo n.º 4
0
    def _generate_register_get(self, outfile, reg):
        """
        Generate a C function that reads the given register
        """

        if not reg.is_readable():
            return

        rname = reg.name.lower()
        prefix = self._register_function_prefix(reg)
        suffix = config.register_read_function

        gadget = self.gadgets["shoulder.c.function_definition"]
        gadget.name = prefix + "_" + rname + "_" + suffix
        gadget.return_type = self._register_size_type(reg)
        gadget.args = []

        if reg.access_mechanisms["mrs_register"]:
            am = reg.access_mechanisms["mrs_register"][0]
            if config.encoded_functions:
                self._generate_aarch64_encoded_get(outfile, reg, am)
            else:
                self._generate_mrs_register_get(outfile, reg, am)

        elif reg.access_mechanisms["mrs_banked"]:
            am = reg.access_mechanisms["mrs_banked"][0]
            self._generate_mrs_banked_get(outfile, reg, am)

        elif reg.access_mechanisms["mrc"]:
            am = reg.access_mechanisms["mrc"][0]
            self._generate_mrc_get(outfile, reg, am)

        elif reg.access_mechanisms["mrrc"]:
            gadget.return_type = "uint64_t"

            am = reg.access_mechanisms["mrrc"][0]
            self._generate_mrrc_get(outfile, reg, am)

        elif reg.access_mechanisms["vmrs"]:
            am = reg.access_mechanisms["vmrs"][0]
            self._generate_vmrs_get(outfile, reg, am)

        elif reg.access_mechanisms["ldr"]:
            am = reg.access_mechanisms["ldr"][0]
            self._generate_external_constants(outfile, reg, am)
            self._generate_ldr_get(outfile, reg, am)

        else:
            msg = "Failed to generate {f} function for readable register {r}"
            msg = msg.format(f=config.register_read_function,
                             r=reg.name.lower())
            logger.error(msg)
Ejemplo n.º 5
0
def parse_registers(spec_path):
    if not os.path.exists(spec_path):
        msg = "Failed to parse registers, spec not found at: " + str(spec_path)
        logger.error(msg)
        raise ShoulderParserException(msg)

    logger.info("Parsing registers from: " + str(spec_path))

    paths = glob.glob(spec_path + "/*.xml")
    regs = []
    parser = ArmV8XmlParser()

    for path in paths:
        results = parser.parse_registers(path)
        if results:
            regs.append(results[0])

    return regs
Ejemplo n.º 6
0
def parse_cmd_args(args: List[str]) -> config:
    try:
        opts, args = getopt.getopt(args, short_opts, long_opts)

        for opt, arg in opts:
            if opt == "-i":
                config.xml_register_dir = str(arg)
            elif opt == "-o":
                config.shoulder_output_dir = str(arg)
            else:
                opt_name = opt[2:]
                if config[opt_name]:
                    config[opt_name] = arg

    except getopt.GetoptError:
        logger.error("Invalid options specified, usage:")
        print_usage()
        print_options()
        sys.exit(2)

    return config