def SignImage(RawData, OutFile, PrivKey): # # Generate the new image layout # 1. write firmware update header # unsigned_image = bytearray(sizeof(Firmware_Update_Header)) header = Firmware_Update_Header.from_buffer(unsigned_image, 0) file_size = len(RawData) header.FileGuid = (c_ubyte *16).from_buffer_copy(FIRMWARE_UPDATE_IMAGE_FILE_GUID.bytes_le) header.HeaderSize = sizeof(Firmware_Update_Header) header.FirmwreVersion = 1 header.CapsuleFlags = 0 header.ImageOffset = header.HeaderSize header.ImageSize = file_size header.SignatureOffset = header.ImageOffset + header.ImageSize header.SignatureSize = 256 header.PubKeyOffset = header.SignatureOffset + header.SignatureSize pubkey_file = 'fwu_public_key.bin' gen_pub_key(PrivKey, pubkey_file) header.PubKeySize = os.path.getsize(pubkey_file) fwupdate_bin_file = 'fwupdate_unsigned.bin' open(fwupdate_bin_file, 'wb').write(unsigned_image + RawData) rsa_sign_file(PrivKey, pubkey_file, fwupdate_bin_file, OutFile, True, True) os.remove(pubkey_file) os.remove(fwupdate_bin_file)
def sign_image(Infile, OutFile, priv_key, img_type): # # Generate the new image layout # 1. write firmware update header # unsigned_image = bytearray(sizeof(Firmware_Update_Header)) header = Firmware_Update_Header.from_buffer(unsigned_image, 0) file_size_alignment = 0x1000 file_size = os.path.getsize(Infile) aligned_size = (file_size + file_size_alignment - 1) & ~(file_size_alignment - 1) append_size = aligned_size - file_size header.FileGuid = (ctypes.c_ubyte * 16).from_buffer_copy( FIRMWARE_UPDATE_IMAGE_FILE_GUID.get_bytes_le()) header.HeaderSize = sizeof(Firmware_Update_Header) header.FirmwreVersion = 1 header.CapsuleFlags = 0 if img_type == 'b' else 1 header.ImageOffset = header.HeaderSize header.ImageSize = aligned_size header.SignatureOffset = header.ImageOffset + header.ImageSize header.SignatureSize = 256 header.PubKeyOffset = header.SignatureOffset + header.SignatureSize pubkey_file = 'fwu_public_key.bin' gen_pub_key(priv_key, pubkey_file) header.PubKeySize = os.path.getsize(pubkey_file) InfileData = open(Infile, 'rb').read() if img_type == 'b': # for BIOS image, adding padding from head unsigned_image.extend('\xff' * append_size + InfileData) else: # for CFGDATA image, adding padding to tail unsigned_image.extend(InfileData + '\xff' * append_size) fwupdate_bin_file = 'fwupdate_unsigned.bin' open(fwupdate_bin_file, 'wb').write(unsigned_image) rsa_sign_file(priv_key, pubkey_file, fwupdate_bin_file, OutFile, True, True) os.remove(pubkey_file) os.remove(fwupdate_bin_file)