Exemplo n.º 1
0
def create_apps_sqif0_layout(devkit_path, transport_uri, layout_dict):
    """
    Write the new image layout to the cache location and execute it.
    """
    global SQIF0_CONFIG_FILE
    from csr.dev.tools.flash_image_builder import ImageBuilder

    builder = ImageBuilder(layout_dict)
    builder.build()

    print("Boot Image - offset 0x%x, size 0x%x" %
          (builder.boot_image["offset"], builder.boot_image["size"]))

    print("Image output sections:")
    for section in builder.image:
        print("Flash offset 0x%08x size 0x%x" % (section[0], len(section[1])))
    print("Image Header:")
    print(builder.image_header)

    fd, tmpfile = tempfile.mkstemp(suffix=".xuv")
    os.close(fd)
    builder.write_complete_flash_image_xuv(tmpfile)

    # Write the new configuration to the local cache
    sqif0_config_file = os.path.join(devkit_path, "tmp",
                                     transport_uri.replace(":", "_"),
                                     SQIF0_CONFIG_FILE)
    if not os.path.isdir(os.path.dirname(sqif0_config_file)):
        os.makedirs(os.path.dirname(sqif0_config_file), 0o775)
    with open(sqif0_config_file, "w") as config_file:
        config_file.write("flash0 = %s" %
                          pprint.pformat(layout_dict, indent=2, width=80))

    return tmpfile
    def build_flash_image(self):
        if flash0["encrypt"] == True:
            if flash0["hardware_encrypted"] == True:
                # Need to attach to a device to be able to encrypt from it
                tools_spec = os.path.join(flash0["host_tools"]["devkit"], "apps", "fw", "tools"
                    ).replace('/', os.path.sep)
                if not os.path.isdir(tools_spec):
                    print("Folder {} not found\n".format(tools_spec))
                    return None
                sys.path.insert(0,tools_spec)
                # Add the path to apps\fw\tools\pylib to sys.path
                pylib_spec = os.path.join(flash0["host_tools"]["devkit"], "apps", "fw",
                    "tools", "pylib").replace('/', os.path.sep)
                if not os.path.isdir(pylib_spec):
                    print("Folder {} not found\n".format(pylib_spec))
                    return None
                sys.path.insert(0,pylib_spec)
                from csr.transport.trbtrans import TrbErrorCouldNotEnumerateDevice
                try:
                    device = self.attach_to_device()
                except (TrbErrorCouldNotEnumerateDevice, NotImplementedError):
                    print("Unable to attach to device to encrypt")
                    sys.stdout.flush()
                    sys.exit(1)
                try:
                    builder = ImageBuilder(flash0, device.chip.apps_subsystem.dma,
                        select_bank=flash0["dfu_image"]["bank"])
                    device.chip.curator_subsystem.siflash.identify(4,0)
                except:
                    print("Unable to communicate with the device to encrypt")
                    sys.stdout.flush()
                    sys.exit(1)
            else:
                try:
                    builder = ImageBuilder(flash0, file=flash0["encryption_file"],
                        select_bank=flash0["dfu_image"]["bank"])
                except KeyError:
                    print("No software encryption file supplied")
                    sys.stdout.flush()
                    sys.exit(1)
        else:
            # Not encrypted, so don't need to attach to a device
            builder = ImageBuilder(flash0, select_bank=flash0["dfu_image"]["bank"])

        builder.build()
        print("Boot Image - offset 0x%x, size 0x%x" % (
                builder.boot_image["offset"], builder.boot_image["size"]))

        print("Image output sections:")
        for section in builder.image:
            print("Flash offset 0x%08x size 0x%x" % (section[0], len(section[1])))
        print("Image Header:")
        print(builder.image_header)

        print("Writing image section xuv files to %s" % (self.dfu_dir))
        builder.write_image_section_xuv_files(os.path.join(self.dfu_dir, "dfu"))
        print("Build completed\n")
        sys.stdout.flush()
Exemplo n.º 3
0
    def create_flash_image(self,
                           devkit,
                           output_path,
                           encrypted=False,
                           hardware_encrypted=False,
                           software_encryption_file=False,
                           include_all_flash_sections=True,
                           include_header_section=True):
        """
        Create the configuration for the ImageBuilder and run it.
        """
        from csr.dev.tools.flash_image_builder import ImageBuilder
        flash0 = self.flash_config
        map = {}
        for i in range(len(flash0["layout"])):
            map[flash0["layout"][i][0]] = i

        # Put together the configuration needed by ImageBuilder
        flash_image_xuv = os.path.join(output_path, "flash_image.xuv")
        flash_image_parameters = {
            "flash_image_xuv": flash_image_xuv,
            "bank": "bank0"
        }

        audio_image_xuv = None

        authenticate = None
        if encrypted == True:
            authenticate = True

        xuv_filename = None
        if "curator_config" in self._projects:
            xuv_filename = self._projects["curator_config"]
        flash0["layout"][map["curator_fs"]] = self._add_layout(
            "curator_fs", xuv_filename, flash0["layout"][map["curator_fs"]],
            encrypted)

        xuv_filename = None
        if "apps0" in self._projects:
            xuv_filename = self._projects["apps0"]
        flash0["layout"][map["apps_p0"]] = self._add_layout(
            "apps_p0", xuv_filename, flash0["layout"][map["apps_p0"]],
            encrypted)

        xuv_filename = None
        if "audio" in self._projects:
            xuv_filename = self._projects["audio"]
        if self._generate_audio_image_xuv and xuv_filename:
            # Copy the audio image directly to the output folder.
            source = xuv_filename
            destination = os.path.join(output_path, "flash_audio_image.xuv")
            try:
                shutil.copy(source, destination)
                audio_image_xuv = destination
                print("Copied %s to %s" % (source, destination))
            except IOError as err:
                print("Unable to copy %s to %s: %s" %
                      (source, destination, err))
                return None, None
        try:
            flash0["layout"][map["audio"]] = self._add_layout(
                "audio", xuv_filename, flash0["layout"][map["audio"]], False)
        except KeyError:
            pass

        xuv_filename = None
        if "app/p1" in self._projects:
            xuv_filename = self._projects["app/p1"]
        flash0["layout"][map["apps_p1"]] = self._add_layout(
            "apps_p1", xuv_filename, flash0["layout"][map["apps_p1"]],
            encrypted)

        xuv_filename = None
        if "device_config" in self._projects:
            xuv_filename = self._projects["device_config"]
        flash0["layout"][map["device_ro_fs"]] = self._add_layout(
            "device_ro_fs", xuv_filename,
            flash0["layout"][map["device_ro_fs"]], encrypted)

        xuv_filename = None
        if "firmware_config" in self._projects:
            xuv_filename = self._projects["firmware_config"]
        flash0["layout"][map["ro_cfg_fs"]] = self._add_layout(
            "ro_cfg_fs", xuv_filename, flash0["layout"][map["ro_cfg_fs"]],
            encrypted)

        xuv_filename = None
        if "customer_ro" in self._projects:
            xuv_filename = self._projects["customer_ro"]
        flash0["layout"][map["ro_fs"]] = self._add_layout(
            "ro_fs", xuv_filename, flash0["layout"][map["ro_fs"]], encrypted)

        xuv_filename = None
        if "user_ps" in self._projects:
            xuv_filename = self._projects["user_ps"]
        flash0["layout"][map["rw_config"]] = self._add_layout(
            "rw_config", xuv_filename, flash0["layout"][map["rw_config"]],
            encrypted)

        flash0["layout"][map["rw_fs"]] = self._add_layout(
            "rw_fs", None, flash0["layout"][map["rw_fs"]], None)

        flash0["flash_image"] = flash_image_parameters
        flash0["encrypt"] = encrypted

        import pprint
        pp = pprint.PrettyPrinter(indent=2)
        print("flash0 ", )
        pp.pprint(flash0)
        sys.stdout.flush()

        # Create an instance of ImageBuilder with the required configuration.
        if encrypted == True:
            if hardware_encrypted == True:
                # Need to attach to a device to be able to encrypt from it
                from csr.transport.trbtrans import TrbErrorCouldNotEnumerateDevice
                try:
                    device = self._attach_to_device()
                except (TrbErrorCouldNotEnumerateDevice, NotImplementedError):
                    print("Unable to attach to device to encrypt")
                    sys.stdout.flush()
                    sys.exit(1)
                if device is None:
                    print("Unable to attach to device to encrypt")
                    sys.stdout.flush()
                    sys.exit(1)
                try:
                    builder = ImageBuilder(
                        flash0,
                        device.chip.apps_subsystem.dma,
                        select_bank=flash0["flash_image"]["bank"])
                    device.chip.curator_subsystem.siflash.identify(4, 0)
                except:
                    print("Unable to communicate with the device to encrypt")
                    sys.stdout.flush()
                    sys.exit(1)
            else:
                fname = os.path.join(devkit, "tools", "bin", "SecurityCmd.exe")
                if os.path.isfile(fname):
                    host_tools_parameters = {"SecurityCmd": r"%s" % fname}
                    flash0["host_tools"] = host_tools_parameters

                try:
                    builder = ImageBuilder(
                        flash0,
                        file=software_encryption_file,
                        select_bank=flash0["flash_image"]["bank"])
                except KeyError:
                    print("No software encryption file supplied")
                    sys.stdout.flush()
                    sys.exit(1)
        else:
            builder = ImageBuilder(flash0,
                                   select_bank=flash0["flash_image"]["bank"])

        builder.build()
        print("Boot Image - offset 0x%x, size 0x%x" %
              (builder.boot_image["offset"], builder.boot_image["size"]))

        print("Image Header:")
        print(builder.image_header)

        PrepareSingleImage.print_flash_usage_table(builder)

        print("Writing '%s'" % flash0["flash_image"]["flash_image_xuv"])
        builder.write_image_section_xuv_files(
            flash0["flash_image"]["flash_image_xuv"])

        if include_all_flash_sections:
            builder.write_complete_flash_image_xuv(
                flash0["flash_image"]["flash_image_xuv"])
        else:
            builder.write_image_xuv(flash0["flash_image"]["flash_image_xuv"],
                                    include_header=include_header_section)

        if encrypted == True and "audio" in map:
            self._relocate_image(
                os.path.join(output_path, "flash_image.xuv_audio.xuv"),
                os.path.join(output_path, "flash_audio.xuv"))

        print("Build completed\n")
        sys.stdout.flush()
        return flash_image_xuv, audio_image_xuv
pylibdir = os.path.join(scriptdir, "..", "pylib")
if not pylibdir in sys.path:
    sys.path.append(pylibdir)

from csr.dev.tools.flash_image_builder import ImageBuilder

if __name__ == "__main__":
    flash_config_python_file = sys.argv[1]
    if flash_config_python_file.endswith(".py"):
        execfile(flash_config_python_file)
        if len(sys.argv) > 3:
            select_bank_type = sys.argv[3]
        else:
            select_bank_type = "bank0"
        builder = ImageBuilder(flash0, select_bank=select_bank_type)
        builder.build()

        print "Boot Image - offset 0x%x, size 0x%x" % (
            builder.boot_image["offset"], builder.boot_image["size"])

        print "Image output sections:"
        for section in builder.image:
            print "Flash offset 0x%08x size 0x%x" % (section[0], len(
                section[1]))
        print "Image Header:"
        print builder.image_header

        if len(sys.argv) > 2:
            print "Writing '%s'" % sys.argv[2]
            builder.write_complete_flash_image_xuv(sys.argv[2])