1"""Repacks GKI boot images with the given kernel images.""" 2import argparse 3import json 4import os 5import shutil 6import tempfile 7 8from treble.fetcher import fetcher_lib 9from treble.gki import repack_gki_lib 10 11 12def main(): 13 parser = argparse.ArgumentParser( 14 description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) 15 parser.add_argument( 16 '--json_keyfile', 17 help='JSON keyfile containing credentials. ' 18 '(Default: Use default credential file)') 19 parser.add_argument( 20 '--bootimg_build_id', help='Download from the specified build.') 21 parser.add_argument( 22 '--ramdisk_build_id', help='DEPRECATED. Use --bootimg_build_id instead.') 23 parser.add_argument( 24 '--bootimg_target', 25 help='Name of the bootimg target from the bootimg branch.') 26 parser.add_argument( 27 '--ramdisk_target', help='DEPRECATED. Use --bootimg_target instead.') 28 parser.add_argument( 29 '--kernel_build_id', 30 required=True, 31 help='Download from the specified build.') 32 parser.add_argument( 33 '--kernel_target', 34 required=True, 35 help='Name of the kernel target from the kernel branch.') 36 parser.add_argument( 37 '--kernel_debug_target', 38 required=True, 39 help='Name of the kernel debug target from the kernel branch.') 40 parser.add_argument( 41 '--kernel_version', 42 required=True, 43 help='The Kernel version to use when repacking.') 44 parser.add_argument( 45 '--out_dir', required=True, help='Save output to this directory.') 46 47 args = parser.parse_args() 48 client = fetcher_lib.create_client_from_json_keyfile( 49 json_keyfile_name=args.json_keyfile) 50 51 if not os.path.exists(args.out_dir): 52 os.makedirs(args.out_dir) 53 54 bootimg_build_id = args.bootimg_build_id or args.ramdisk_build_id 55 bootimg_target = args.bootimg_target or args.ramdisk_target 56 57 with tempfile.TemporaryDirectory() as tmp_bootimg_dir, \ 58 tempfile.TemporaryDirectory() as tmp_kernel_dir: 59 # Fetch boot images. 60 repack_gki_lib.fetch_bootimg( 61 client=client, 62 out_dir=tmp_bootimg_dir, 63 build_id=bootimg_build_id, 64 kernel_version=args.kernel_version, 65 target=bootimg_target, 66 ) 67 68 # Fetch kernel artifacts. 69 kernel_dir, kernel_debug_dir = repack_gki_lib.fetch_kernel( 70 client=client, 71 out_dir=tmp_kernel_dir, 72 build_id=args.kernel_build_id, 73 kernel_target=args.kernel_target, 74 kernel_debug_target=args.kernel_debug_target, 75 ) 76 77 # Save kernel artifacts to the out dir. 78 kernel_out_dir = os.path.join(args.out_dir, 'kernel', args.kernel_version) 79 if not os.path.exists(kernel_out_dir): 80 os.makedirs(kernel_out_dir) 81 82 def copy_kernel_file(in_dir, filename, outname=None): 83 if not outname: 84 outname = filename 85 shutil.copy( 86 os.path.join(in_dir, filename), os.path.join(kernel_out_dir, outname)) 87 88 copy_kernel_file(kernel_dir, 'System.map') 89 copy_kernel_file(kernel_dir, 'abi_symbollist') 90 copy_kernel_file(kernel_dir, 'vmlinux') 91 copy_kernel_file(kernel_dir, 'vmlinux.symvers') 92 copy_kernel_file(kernel_dir, 'Image', 93 'kernel-{}'.format(args.kernel_version)) 94 copy_kernel_file(kernel_dir, 'Image.lz4', 95 'kernel-{}-lz4'.format(args.kernel_version)) 96 copy_kernel_file(kernel_dir, 'Image.gz', 97 'kernel-{}-gz'.format(args.kernel_version)) 98 copy_kernel_file(kernel_debug_dir, 'System.map', 'System.map-allsyms') 99 copy_kernel_file(kernel_debug_dir, 'abi-generated.xml') 100 copy_kernel_file(kernel_debug_dir, 'abi-full-generated.xml') 101 copy_kernel_file(kernel_debug_dir, 'Image', 102 'kernel-{}-allsyms'.format(args.kernel_version)) 103 copy_kernel_file(kernel_debug_dir, 'Image.lz4', 104 'kernel-{}-lz4-allsyms'.format(args.kernel_version)) 105 copy_kernel_file(kernel_debug_dir, 'Image.gz', 106 'kernel-{}-gz-allsyms'.format(args.kernel_version)) 107 copy_kernel_file(kernel_debug_dir, 'vmlinux', 'vmlinux-allsyms') 108 copy_kernel_file(kernel_debug_dir, 'vmlinux.symvers', 'vmlinux.symvers-allsyms') 109 110 # Repack individual boot images using the fetched kernel artifacts, 111 # then save to the out dir. 112 repack_gki_lib.repack_bootimgs(tmp_bootimg_dir, kernel_dir, 113 kernel_debug_dir) 114 shutil.copytree(tmp_bootimg_dir, args.out_dir, dirs_exist_ok=True) 115 116 # Repack boot images inside the img.zip and save to the out dir. 117 img_zip_name = [f for f in os.listdir(tmp_bootimg_dir) if '-img-' in f][0] 118 img_zip_path = os.path.join(tmp_bootimg_dir, img_zip_name) 119 repack_gki_lib.repack_img_zip(img_zip_path, kernel_dir, kernel_debug_dir, 120 args.kernel_version) 121 shutil.copy(img_zip_path, args.out_dir) 122 123 target_files_zip_name = [ 124 f for f in os.listdir(tmp_bootimg_dir) if '-target_files-' in f 125 ][0] 126 target_files_zip_path = os.path.join(tmp_bootimg_dir, target_files_zip_name) 127 repack_gki_lib.replace_target_files_zip_kernels(target_files_zip_path, 128 kernel_out_dir, 129 args.kernel_version) 130 shutil.copy(target_files_zip_path, args.out_dir) 131 132 # Copy otatools.zip from the bootimg build, used for GKI signing. 133 shutil.copy(os.path.join(tmp_bootimg_dir, 'otatools.zip'), args.out_dir) 134 135 # Write prebuilt-info.txt using the prebuilt artifact build IDs. 136 data = { 137 'kernel-build-id': int(args.kernel_build_id), 138 } 139 with open(os.path.join(kernel_out_dir, 'prebuilt-info.txt'), 'w') as f: 140 json.dump(data, f, indent=4) 141 142 143if __name__ == '__main__': 144 main() 145