Exemplo n.º 1
0
    def load(self, db, root_path):
        """ Load content
        :param db:
        :param root_path:
        :return:
        """
        assert isinstance(db, list)
        assert isinstance(root_path, str)

        img_obj = uboot.new_img(**self._header)
        if img_obj.header.image_type == uboot.EnumImageType.FIRMWARE:
            with open(get_full_path(root_path, self.path)[0], 'rb') as f:
                img_obj.data = f.read()
        elif img_obj.header.image_type == uboot.EnumImageType.SCRIPT:
            if self.path is None:
                img_obj.load(self._txt_data)
            else:
                with open(get_full_path(root_path, self.path)[0], 'r') as f:
                    img_obj.load(f.read())
        elif img_obj.header.image_type == uboot.EnumImageType.MULTI:
            for img_path in get_full_path(root_path, self.path):
                with open(img_path, 'rb') as f:
                    img_obj.append(uboot.parse_img(f.read()))
        else:
            with open(get_full_path(root_path, self.path)[0], 'rb') as f:
                img_obj.data = f.read()

        self.data = img_obj.export()
Exemplo n.º 2
0
def test_02_funky_chars_in_name():
    # --------------------------------------------------------------------------------
    # create dummy firmware image (u-boot executable image)
    # --------------------------------------------------------------------------------
    fwimg = uboot.StdImage(
        bytes([1] * 16),
        name=str(
            '\x03\x00\x00\x04\x52\x54\x2d\x41\x43\x35\x35\x55\x48\x50\x00\x00\x00\x00\x00\xa9'
        ),
        laddr=0,
        eaddr=0,
        arch=uboot.EnumArchType.ARM,
        os=uboot.EnumOsType.LINUX,
        image=uboot.EnumImageType.FIRMWARE,
        compress=uboot.EnumCompressionType.NONE)

    # --------------------------------------------------------------------------------
    # save created image into file: uboot_mimg.img
    # --------------------------------------------------------------------------------
    with open(UBOOT_IMG_TEMP, "wb") as f:
        f.write(fwimg.export())

    # --------------------------------------------------------------------------------
    # open and read image file: uboot_mimg.img
    # --------------------------------------------------------------------------------
    with open(UBOOT_IMG_TEMP, "rb") as f:
        data = f.read()

    # --------------------------------------------------------------------------------
    # parse binary data into new img object of specific image type
    # --------------------------------------------------------------------------------
    img = uboot.parse_img(data)
    # check if are identical
    assert fwimg == img
Exemplo n.º 3
0
def test_01_multi_file_image():
    # --------------------------------------------------------------------------------
    # create dummy firmware image (u-boot executable image)
    # --------------------------------------------------------------------------------
    fwimg = uboot.StdImage(bytes([1] * 512),
                           name="Firmware Test Image",
                           laddr=0,
                           eaddr=0,
                           arch=uboot.EnumArchType.ARM,
                           os=uboot.EnumOsType.LINUX,
                           image=uboot.EnumImageType.FIRMWARE,
                           compress=uboot.EnumCompressionType.NONE)

    # --------------------------------------------------------------------------------
    # create script image (u-boot executable image)
    # --------------------------------------------------------------------------------
    scimg = uboot.ScriptImage()
    scimg.Name = "Test Script Image"
    scimg.OsType = uboot.EnumOsType.LINUX
    scimg.ArchType = uboot.EnumArchType.ARM
    scimg.Compression = uboot.EnumCompressionType.NONE
    scimg.EntryAddress = 0
    scimg.LoadAddress = 0
    scimg.append("echo", "'===== U-Boot settings ====='")
    scimg.append("setenv", "stdin serial")
    scimg.append("setenv", "stdout serial")
    scimg.append("setenv", "rootdev mmcblk2p2")

    # --------------------------------------------------------------------------------
    # create multi-file image
    # --------------------------------------------------------------------------------
    mimg = uboot.MultiImage(name="Multi-File Test Image",
                            laddr=0,
                            eaddr=0,
                            arch=uboot.EnumArchType.ARM,
                            os=uboot.EnumOsType.LINUX,
                            compress=uboot.EnumCompressionType.NONE)
    mimg.append(fwimg)
    mimg.append(scimg)

    # --------------------------------------------------------------------------------
    # save created image into file: uboot_mimg.img
    # --------------------------------------------------------------------------------
    with open(UBOOT_IMG_TEMP, "wb") as f:
        f.write(mimg.export())

    # --------------------------------------------------------------------------------
    # open and read image file: uboot_mimg.img
    # --------------------------------------------------------------------------------
    with open(UBOOT_IMG_TEMP, "rb") as f:
        data = f.read()

    # --------------------------------------------------------------------------------
    # parse binary data into new img object of specific image type
    # --------------------------------------------------------------------------------
    img = uboot.parse_img(data)

    # check if are identical
    assert mimg == img
Exemplo n.º 4
0
def test_03_bad_crc():
    bad_crc_image = b'\x27\x05\x19\x56\x4F\x7c\x89\x60\x60\x2d\xa6\xec\x00\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x00\x0b\xcd\x6a\xaf\x05\x02\x05\x00\x03\x00\x00\x04\x52\x54\x2d\x41\x43\x35\x35\x55\x48\x50\x00\x00\x00\x00\x00\xc2\xa9\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01\x01'

    # --------------------------------------------------------------------------------
    # parse binary data into new img object of specific image type
    # --------------------------------------------------------------------------------
    try:
        img = uboot.parse_img(bad_crc_image)
    except Exception:
        pass
    else:
        assert False, 'Expected Exception when reading header with bad CRC'

    # Don't expect an error
    img = uboot.parse_img(bad_crc_image, ignore_crc=True)
    expected_data = bytes([1] * 64)
    assert img.data == expected_data, "Data wasn't correct"
Exemplo n.º 5
0
def info(file):
    """ List old image content in readable format """
    try:
        with open(file, 'rb') as f:
            img = uboot.parse_img(f.read())
            click.echo(img.info())

    except Exception as e:
        click.echo(str(e) if str(e) else "Unknown Error !")
        sys.exit(ERROR_CODE)
Exemplo n.º 6
0
def extract(file):
    """ Extract content from old U-Boot image """
    def get_file_ext(img):
        ext = ('bin', 'gz', 'bz2', 'lzma', 'lzo', 'lz4')
        return ext[img.compression]

    try:
        with open(file, 'rb') as f:
            raw_data = f.read()

        img = uboot.parse_img(raw_data)

        file_path, file_name = os.path.split(file)
        dest_dir = os.path.normpath(os.path.join(file_path, file_name + ".ex"))
        os.makedirs(dest_dir, exist_ok=True)

        if img.header.image_type == uboot.EnumImageType.MULTI:
            n = 0
            for simg in img:
                with open(
                        os.path.join(dest_dir, 'image_{0:02d}.bin'.format(n)),
                        'wb') as f:
                    f.write(simg.export())
                n += 1
        elif img.header.image_type == uboot.EnumImageType.SCRIPT:
            with open(os.path.join(dest_dir, 'script.txt'), 'w') as f:
                f.write(img.store())

        else:
            with open(os.path.join(dest_dir, 'image.' + get_file_ext(img)),
                      'wb') as f:
                f.write(img.data)

        with open(os.path.join(dest_dir, 'info.txt'), 'w') as f:
            f.write(img.info())

    except Exception as e:
        click.echo(str(e) if str(e) else "Unknown Error !")
        sys.exit(ERROR_CODE)

    click.secho("\n Image extracted into dir: %s" % dest_dir)
Exemplo n.º 7
0
def create(arch, ostype, imgtype, compress, laddr, epaddr, name, outfile,
           infiles):
    """ Create old U-Boot image from attached files """
    try:
        img_type = uboot.EnumImageType[imgtype]

        if img_type == uboot.EnumImageType.MULTI:
            img = uboot.MultiImage
            for file in infiles:
                with open(file, 'rb') as f:
                    simg = uboot.parse_img(f.read())
                    img.append(simg)

        elif img_type == uboot.EnumImageType.MULTI:
            img = uboot.ScriptImage()
            with open(infiles[0], 'r') as f:
                img.load(f.read())

        else:
            img = uboot.StdImage(image=img_type)
            with open(infiles[0], 'rb') as f:
                img.data = bytearray(f.read())

        img.header.arch_type = uboot.EnumArchType[arch]
        img.header.os_type = uboot.EnumOsType[ostype]
        img.header.compression = uboot.EnumCompressionType[compress]
        img.header.load_address = laddr
        img.header.entry_point = epaddr
        img.header.name = name

        click.echo(img.info())
        with open(outfile, 'wb') as f:
            f.write(img.export())

    except Exception as e:
        click.echo(str(e) if str(e) else "Unknown Error !")
        sys.exit(ERROR_CODE)

    click.secho("\n Created Image: %s" % outfile)