Beispiel #1
0
def generate_sub_region_fv(
        image_file,
        sub_region_descriptor,
        output_fv_file=os.path.join(os.path.curdir, "SubRegion.FV")
):

    sub_region_image = "SubRegionImage.bin"
    fv_ffs_file_list = []

    for file_index, ffs_file in enumerate(sub_region_descriptor.ffs_files):

        sbrgn_image.generate_sub_region_image(ffs_file, sub_region_image)
        ip, ip_ops = sbrgn_image.ip_info_from_guid(ffs_file.ffs_guid)

        # if ffs GUID is not found exit.
        if ip is None:
            print("FFS GUIS {} not found".format(ffs_file.ffs_guid))
            exit(-1)

        # Inputfiles should be minium of two files to work with function.
        inputfiles, num_files = sbrgn_image.ip_inputfiles(
            [None, sub_region_image],
            ip
            )

        cmds = sbrgn_image.build_command_list(ip_ops, inputfiles, num_files)

        if utils.execute_cmds(logger, cmds) == 1:
            exit(-1)

        ffs_file_path = "tmp.{}.ffs".format(file_index)
        os.rename("tmp.ffs", ffs_file_path)
        fv_ffs_file_list.append(ffs_file_path)

        fv_cmd_list = sbrgn_image.build_fv_from_ffs_files(
                     sub_region_descriptor,
                     output_fv_file,
                     fv_ffs_file_list)

    if utils.execute_cmds(logger, fv_cmd_list) == 1:
        print("Error generating FV File")
        exit(-1)
Beispiel #2
0
def main():
    """Entry to script."""

    # files created that needs to be remove
    to_remove = [
        "tmp.fmmt.txt", "tmp.raw", "tmp.ui", "tmp.all", "tmp.cmps", "tmp.guid",
        "tmp.pe32", "tmp.ffs"
    ]
    try:
        parser = parse_cmdline()
        args = parser.parse_args()

        outfile = Path(args.OUTPUT_FILE).resolve()
        outfile = utils.file_not_exist(outfile, logger)

        for f in (FMMT, GENFV, GENFFS, GENSEC, LZCOMPRESS, RSA_HELPER,
                  FMMT_CFG):
            if not os.path.exists(f):
                raise FileNotFoundError(
                    "Thirdparty tool not found ({})".format(f))

        # Use absolute path because GenSec does not like relative ones
        IFWI_file = Path(args.IFWI_IN.name).resolve()

        # If input IP file is a JSON file, convert it to binary as the real input file
        if args.IPNAME_IN.name.lower().endswith('.json'):
            logger.info(
                "Found JSON as input file. Converting it to binary ...\n")

            desc = SubRegionDescriptor()
            desc.parse_json_data(args.IPNAME_IN.name)

            # Currently only creates the first file
            generate_sub_region_image(desc.ffs_files[0],
                                      output_file="tmp.payload.bin")
            IPNAME_file = Path("tmp.payload.bin").resolve()

            # add to remove files
            to_remove.append("tmp.payload.bin")
        else:
            IPNAME_file = Path(args.IPNAME_IN.name).resolve()

        filenames = [str(IFWI_file), str(IPNAME_file)]
        if args.ipname in ["gop", "gfxpeim", "vbt"]:
            if not args.private_key or not os.path.exists(args.private_key):
                logger.critical(
                    "\nMissing RSA key to stitch GOP/PEIM GFX/VBT from command line\n"
                )
                parser.print_help()
                sys.exit(2)
            else:
                key_file = Path(args.private_key).resolve()
                status = utils.check_key(key_file, "rsa", logger)
                if status != 0:
                    sys.exit(status)
                filenames.append(key_file)

        # Verify file is not empty or the IP files are smaller than the input file
        status = utils.check_file_size(logger, filenames)
        if status != 0:
            sys.exit(status)

        # Copy key file to the required name needed for the rsa_helper.py
        if args.private_key:
            shutil.copyfile(key_file, os.path.join(TOOLS_DIR, "privkey.pem"))
            to_remove.append(os.path.join(TOOLS_DIR, 'privkey.pem'))
            filenames.remove(key_file)

        logger.info("*** Replacing {} ...".format(args.ipname))
        stitch_and_update(args.IFWI_IN.name, args.ipname, filenames, outfile)

        # Update OBB digest after stitching any data inside OBB region
        if args.ipname in ["gop", "vbt", "gfxpeim"]:

            if args.ipname == "gop":
                ipname = "obbdxe_digest"
                fv_list = [GUID_FVOSBOOT, GUID_FVUEFIBOOT, GUID_FVADVANCED]
            else:
                ipname = "obbpei_digest"
                fv_list = [GUID_FVPOSTMEMORY, GUID_FVFSPS]

            digest_file = "tmp.obb.hash.bin"

            to_remove.append(digest_file)

            calculate_new_obb_digest(outfile, fv_list, digest_file)

            filenames = [
                str(Path(f).resolve()) for f in [outfile, digest_file]
            ]

            logger.info("*** Replacing {} ...".format(ipname))
            stitch_and_update(outfile, ipname, filenames, outfile)
    finally:
        utils.cleanup(to_remove)