Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2018 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | """ |
| 18 | Usage: build_super_image input_file output_dir_or_file |
| 19 | |
| 20 | input_file: one of the following: |
| 21 | - directory containing extracted target files. It will load info from |
| 22 | META/misc_info.txt and build full super image / split images using source |
| 23 | images from IMAGES/. |
| 24 | - target files package. Same as above, but extracts the archive before |
| 25 | building super image. |
| 26 | - a dictionary file containing input arguments to build. Check |
| 27 | `dump_dynamic_partitions_info' for details. |
| 28 | In addition: |
| 29 | - "ab_update" needs to be true for A/B devices. |
| 30 | - If source images should be included in the output image (for super.img |
| 31 | and super split images), a list of "*_image" should be paths of each |
| 32 | source images. |
| 33 | |
| 34 | output_dir_or_file: |
| 35 | If a single super image is built (for super_empty.img, or super.img for |
| 36 | launch devices), this argument is the output file. |
| 37 | If a collection of split images are built (for retrofit devices), this |
| 38 | argument is the output directory. |
| 39 | """ |
| 40 | |
| 41 | from __future__ import print_function |
| 42 | |
| 43 | import logging |
| 44 | import os.path |
| 45 | import shlex |
| 46 | import sys |
| 47 | import zipfile |
| 48 | |
| 49 | import common |
| 50 | import sparse_img |
| 51 | |
| 52 | if sys.hexversion < 0x02070000: |
| 53 | print("Python 2.7 or newer is required.", file=sys.stderr) |
| 54 | sys.exit(1) |
| 55 | |
| 56 | logger = logging.getLogger(__name__) |
| 57 | |
| 58 | |
| 59 | UNZIP_PATTERN = ["IMAGES/*", "META/*"] |
| 60 | |
| 61 | |
| 62 | def GetPartitionSizeFromImage(img): |
| 63 | try: |
| 64 | simg = sparse_img.SparseImage(img) |
| 65 | return simg.blocksize * simg.total_blocks |
| 66 | except ValueError: |
| 67 | return os.path.getsize(img) |
| 68 | |
| 69 | |
Yifan Hong | cc46eae | 2019-01-02 11:51:19 -0800 | [diff] [blame] | 70 | def GetArgumentsForImage(partition, group, image=None): |
| 71 | image_size = GetPartitionSizeFromImage(image) if image else 0 |
| 72 | |
| 73 | cmd = ["--partition", |
| 74 | "{}:readonly:{}:{}".format(partition, image_size, group)] |
| 75 | if image: |
| 76 | cmd += ["--image", "{}={}".format(partition, image)] |
| 77 | |
| 78 | return cmd |
| 79 | |
| 80 | |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 81 | def BuildSuperImageFromDict(info_dict, output): |
| 82 | |
| 83 | cmd = [info_dict["lpmake"], |
| 84 | "--metadata-size", "65536", |
| 85 | "--super-name", info_dict["super_metadata_device"]] |
| 86 | |
| 87 | ab_update = info_dict.get("ab_update") == "true" |
| 88 | retrofit = info_dict.get("dynamic_partition_retrofit") == "true" |
| 89 | block_devices = shlex.split(info_dict.get("super_block_devices", "").strip()) |
| 90 | groups = shlex.split(info_dict.get("super_partition_groups", "").strip()) |
| 91 | |
| 92 | if ab_update: |
| 93 | cmd += ["--metadata-slots", "2"] |
| 94 | else: |
| 95 | cmd += ["--metadata-slots", "1"] |
| 96 | |
| 97 | if ab_update and retrofit: |
| 98 | cmd.append("--auto-slot-suffixing") |
| 99 | |
| 100 | for device in block_devices: |
| 101 | size = info_dict["super_{}_device_size".format(device)] |
| 102 | cmd += ["--device", "{}:{}".format(device, size)] |
| 103 | |
| 104 | append_suffix = ab_update and not retrofit |
| 105 | has_image = False |
| 106 | for group in groups: |
| 107 | group_size = info_dict["super_{}_group_size".format(group)] |
| 108 | if append_suffix: |
| 109 | cmd += ["--group", "{}_a:{}".format(group, group_size), |
| 110 | "--group", "{}_b:{}".format(group, group_size)] |
| 111 | else: |
| 112 | cmd += ["--group", "{}:{}".format(group, group_size)] |
| 113 | |
| 114 | partition_list = shlex.split( |
| 115 | info_dict["super_{}_partition_list".format(group)].strip()) |
| 116 | |
| 117 | for partition in partition_list: |
| 118 | image = info_dict.get("{}_image".format(partition)) |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 119 | if image: |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 120 | has_image = True |
Yifan Hong | cc46eae | 2019-01-02 11:51:19 -0800 | [diff] [blame] | 121 | |
| 122 | if not append_suffix: |
| 123 | cmd += GetArgumentsForImage(partition, group, image) |
| 124 | continue |
| 125 | |
| 126 | # For A/B devices, super partition always contains sub-partitions in |
| 127 | # the _a slot, because this image should only be used for |
| 128 | # bootstrapping / initializing the device. When flashing the image, |
| 129 | # bootloader fastboot should always mark _a slot as bootable. |
| 130 | cmd += GetArgumentsForImage(partition + "_a", group + "_a", image) |
| 131 | |
| 132 | other_image = None |
| 133 | if partition == "system" and "system_other_image" in info_dict: |
| 134 | other_image = info_dict["system_other_image"] |
| 135 | has_image = True |
| 136 | |
| 137 | cmd += GetArgumentsForImage(partition + "_b", group + "_b", other_image) |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 138 | |
| 139 | if has_image: |
| 140 | cmd.append("--sparse") |
| 141 | |
| 142 | cmd += ["--output", output] |
| 143 | |
| 144 | common.RunAndCheckOutput(cmd) |
| 145 | |
| 146 | if retrofit and has_image: |
| 147 | logger.info("Done writing images to directory %s", output) |
| 148 | else: |
| 149 | logger.info("Done writing image %s", output) |
| 150 | |
Yifan Hong | e98427a | 2018-12-07 10:08:27 -0800 | [diff] [blame] | 151 | return True |
| 152 | |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 153 | |
| 154 | def BuildSuperImageFromExtractedTargetFiles(inp, out): |
| 155 | info_dict = common.LoadInfoDict(inp) |
| 156 | partition_list = shlex.split( |
| 157 | info_dict.get("dynamic_partition_list", "").strip()) |
Yifan Hong | cc46eae | 2019-01-02 11:51:19 -0800 | [diff] [blame] | 158 | |
| 159 | if "system" in partition_list: |
| 160 | image_path = os.path.join(inp, "IMAGES", "system_other.img") |
| 161 | if os.path.isfile(image_path): |
| 162 | info_dict["system_other_image"] = image_path |
| 163 | |
Yifan Hong | e98427a | 2018-12-07 10:08:27 -0800 | [diff] [blame] | 164 | missing_images = [] |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 165 | for partition in partition_list: |
Yifan Hong | e98427a | 2018-12-07 10:08:27 -0800 | [diff] [blame] | 166 | image_path = os.path.join(inp, "IMAGES", "{}.img".format(partition)) |
| 167 | if not os.path.isfile(image_path): |
| 168 | missing_images.append(image_path) |
| 169 | else: |
| 170 | info_dict["{}_image".format(partition)] = image_path |
| 171 | if missing_images: |
| 172 | logger.warning("Skip building super image because the following " |
| 173 | "images are missing from target files:\n%s", |
| 174 | "\n".join(missing_images)) |
| 175 | return False |
Yifan Hong | 2b891ac | 2018-11-29 12:06:31 -0800 | [diff] [blame] | 176 | return BuildSuperImageFromDict(info_dict, out) |
| 177 | |
| 178 | |
| 179 | def BuildSuperImageFromTargetFiles(inp, out): |
| 180 | input_tmp = common.UnzipTemp(inp, UNZIP_PATTERN) |
| 181 | return BuildSuperImageFromExtractedTargetFiles(input_tmp, out) |
| 182 | |
| 183 | |
| 184 | def BuildSuperImage(inp, out): |
| 185 | |
| 186 | if isinstance(inp, dict): |
| 187 | logger.info("Building super image from info dict...") |
| 188 | return BuildSuperImageFromDict(inp, out) |
| 189 | |
| 190 | if isinstance(inp, str): |
| 191 | if os.path.isdir(inp): |
| 192 | logger.info("Building super image from extracted target files...") |
| 193 | return BuildSuperImageFromExtractedTargetFiles(inp, out) |
| 194 | |
| 195 | if zipfile.is_zipfile(inp): |
| 196 | logger.info("Building super image from target files...") |
| 197 | return BuildSuperImageFromTargetFiles(inp, out) |
| 198 | |
| 199 | if os.path.isfile(inp): |
| 200 | with open(inp) as f: |
| 201 | lines = f.read() |
| 202 | logger.info("Building super image from info dict...") |
| 203 | return BuildSuperImageFromDict(common.LoadDictionaryFromLines(lines.split("\n")), out) |
| 204 | |
| 205 | raise ValueError("{} is not a dictionary or a valid path".format(inp)) |
| 206 | |
| 207 | |
| 208 | def main(argv): |
| 209 | |
| 210 | args = common.ParseOptions(argv, __doc__) |
| 211 | |
| 212 | if len(args) != 2: |
| 213 | common.Usage(__doc__) |
| 214 | sys.exit(1) |
| 215 | |
| 216 | common.InitLogging() |
| 217 | |
| 218 | BuildSuperImage(args[0], args[1]) |
| 219 | |
| 220 | |
| 221 | if __name__ == "__main__": |
| 222 | try: |
| 223 | common.CloseInheritedPipes() |
| 224 | main(sys.argv[1:]) |
| 225 | except common.ExternalError: |
| 226 | logger.exception("\n ERROR:\n") |
| 227 | sys.exit(1) |
| 228 | finally: |
| 229 | common.Cleanup() |