Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | # |
| 3 | # Copyright (C) 2011 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 | Build image output_image_file from input_directory and properties_file. |
| 19 | |
| 20 | Usage: build_image input_directory properties_file output_image_file |
| 21 | |
| 22 | """ |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame^] | 23 | import datetime |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 24 | import os |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 25 | import os.path |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 26 | import re |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 27 | import subprocess |
| 28 | import sys |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 29 | import commands |
| 30 | import shutil |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 31 | import tempfile |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 32 | |
Geremy Condra | e8e982a | 2014-05-16 19:14:30 -0700 | [diff] [blame] | 33 | FIXED_SALT = "aee087a5be3b982978c923f566a94613496b417f2af592639bc80d141e34dfe7" |
| 34 | |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 35 | def RunCommand(cmd): |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 36 | """Echo and run the given command. |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 37 | |
| 38 | Args: |
| 39 | cmd: the command represented as a list of strings. |
| 40 | Returns: |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 41 | A tuple of the output and the exit code. |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 42 | """ |
| 43 | print "Running: ", " ".join(cmd) |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 44 | p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT) |
| 45 | output, _ = p.communicate() |
| 46 | print "%s" % (output.rstrip(),) |
| 47 | return (output, p.returncode) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 48 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 49 | def GetVerityTreeSize(partition_size): |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 50 | cmd = "build_verity_tree -s %d" |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 51 | cmd %= partition_size |
| 52 | status, output = commands.getstatusoutput(cmd) |
| 53 | if status: |
| 54 | print output |
| 55 | return False, 0 |
| 56 | return True, int(output) |
| 57 | |
| 58 | def GetVerityMetadataSize(partition_size): |
| 59 | cmd = "system/extras/verity/build_verity_metadata.py -s %d" |
| 60 | cmd %= partition_size |
| 61 | status, output = commands.getstatusoutput(cmd) |
| 62 | if status: |
| 63 | print output |
| 64 | return False, 0 |
| 65 | return True, int(output) |
| 66 | |
| 67 | def AdjustPartitionSizeForVerity(partition_size): |
| 68 | """Modifies the provided partition size to account for the verity metadata. |
| 69 | |
| 70 | This information is used to size the created image appropriately. |
| 71 | Args: |
| 72 | partition_size: the size of the partition to be verified. |
| 73 | Returns: |
| 74 | The size of the partition adjusted for verity metadata. |
| 75 | """ |
| 76 | success, verity_tree_size = GetVerityTreeSize(partition_size) |
| 77 | if not success: |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 78 | return 0 |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 79 | success, verity_metadata_size = GetVerityMetadataSize(partition_size) |
| 80 | if not success: |
| 81 | return 0 |
| 82 | return partition_size - verity_tree_size - verity_metadata_size |
| 83 | |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 84 | def BuildVerityTree(sparse_image_path, verity_image_path, prop_dict): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 85 | cmd = "build_verity_tree -A %s %s %s" % ( |
| 86 | FIXED_SALT, sparse_image_path, verity_image_path) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 87 | print cmd |
| 88 | status, output = commands.getstatusoutput(cmd) |
| 89 | if status: |
| 90 | print "Could not build verity tree! Error: %s" % output |
| 91 | return False |
| 92 | root, salt = output.split() |
| 93 | prop_dict["verity_root_hash"] = root |
| 94 | prop_dict["verity_salt"] = salt |
| 95 | return True |
| 96 | |
| 97 | def BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
| 98 | block_device, signer_path, key): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 99 | cmd_template = ( |
| 100 | "system/extras/verity/build_verity_metadata.py %s %s %s %s %s %s %s") |
| 101 | cmd = cmd_template % (image_size, verity_metadata_path, root_hash, salt, |
| 102 | block_device, signer_path, key) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 103 | print cmd |
| 104 | status, output = commands.getstatusoutput(cmd) |
| 105 | if status: |
| 106 | print "Could not build verity metadata! Error: %s" % output |
| 107 | return False |
| 108 | return True |
| 109 | |
| 110 | def Append2Simg(sparse_image_path, unsparse_image_path, error_message): |
| 111 | """Appends the unsparse image to the given sparse image. |
| 112 | |
| 113 | Args: |
| 114 | sparse_image_path: the path to the (sparse) image |
| 115 | unsparse_image_path: the path to the (unsparse) image |
| 116 | Returns: |
| 117 | True on success, False on failure. |
| 118 | """ |
| 119 | cmd = "append2simg %s %s" |
| 120 | cmd %= (sparse_image_path, unsparse_image_path) |
| 121 | print cmd |
| 122 | status, output = commands.getstatusoutput(cmd) |
| 123 | if status: |
| 124 | print "%s: %s" % (error_message, output) |
| 125 | return False |
| 126 | return True |
| 127 | |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 128 | def BuildVerifiedImage(data_image_path, verity_image_path, |
| 129 | verity_metadata_path): |
| 130 | if not Append2Simg(data_image_path, verity_metadata_path, |
| 131 | "Could not append verity metadata!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 132 | return False |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 133 | if not Append2Simg(data_image_path, verity_image_path, |
| 134 | "Could not append verity tree!"): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 135 | return False |
| 136 | return True |
| 137 | |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 138 | def UnsparseImage(sparse_image_path, replace=True): |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 139 | img_dir = os.path.dirname(sparse_image_path) |
| 140 | unsparse_image_path = "unsparse_" + os.path.basename(sparse_image_path) |
| 141 | unsparse_image_path = os.path.join(img_dir, unsparse_image_path) |
| 142 | if os.path.exists(unsparse_image_path): |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 143 | if replace: |
| 144 | os.unlink(unsparse_image_path) |
| 145 | else: |
| 146 | return True, unsparse_image_path |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 147 | inflate_command = ["simg2img", sparse_image_path, unsparse_image_path] |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 148 | (_, exit_code) = RunCommand(inflate_command) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 149 | if exit_code != 0: |
| 150 | os.remove(unsparse_image_path) |
| 151 | return False, None |
| 152 | return True, unsparse_image_path |
| 153 | |
| 154 | def MakeVerityEnabledImage(out_file, prop_dict): |
| 155 | """Creates an image that is verifiable using dm-verity. |
| 156 | |
| 157 | Args: |
| 158 | out_file: the location to write the verifiable image at |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 159 | prop_dict: a dictionary of properties required for image creation and |
| 160 | verification |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 161 | Returns: |
| 162 | True on success, False otherwise. |
| 163 | """ |
| 164 | # get properties |
| 165 | image_size = prop_dict["partition_size"] |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 166 | block_dev = prop_dict["verity_block_device"] |
Paul Lawrence | a37b2bb | 2014-11-13 17:54:30 -0800 | [diff] [blame] | 167 | signer_key = prop_dict["verity_key"] + ".pk8" |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 168 | signer_path = prop_dict["verity_signer_cmd"] |
| 169 | |
| 170 | # make a tempdir |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 171 | tempdir_name = tempfile.mkdtemp(suffix="_verity_images") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 172 | |
| 173 | # get partial image paths |
| 174 | verity_image_path = os.path.join(tempdir_name, "verity.img") |
| 175 | verity_metadata_path = os.path.join(tempdir_name, "verity_metadata.img") |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 176 | |
| 177 | # build the verity tree and get the root hash and salt |
Colin Cross | 477cf2b | 2014-04-16 18:49:56 -0700 | [diff] [blame] | 178 | if not BuildVerityTree(out_file, verity_image_path, prop_dict): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 179 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 180 | return False |
| 181 | |
| 182 | # build the metadata blocks |
| 183 | root_hash = prop_dict["verity_root_hash"] |
| 184 | salt = prop_dict["verity_salt"] |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 185 | if not BuildVerityMetadata(image_size, verity_metadata_path, root_hash, salt, |
| 186 | block_dev, signer_path, signer_key): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 187 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 188 | return False |
| 189 | |
| 190 | # build the full verified image |
| 191 | if not BuildVerifiedImage(out_file, |
| 192 | verity_image_path, |
| 193 | verity_metadata_path): |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 194 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 195 | return False |
| 196 | |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 197 | shutil.rmtree(tempdir_name, ignore_errors=True) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 198 | return True |
| 199 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 200 | def BuildImage(in_dir, prop_dict, out_file): |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 201 | """Build an image to out_file from in_dir with property prop_dict. |
| 202 | |
| 203 | Args: |
| 204 | in_dir: path of input directory. |
| 205 | prop_dict: property dictionary. |
| 206 | out_file: path of the output image file. |
| 207 | |
| 208 | Returns: |
| 209 | True iff the image is built successfully. |
| 210 | """ |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 211 | # system_root_image=true: build a system.img that combines the contents of |
| 212 | # /system and the ramdisk, and can be mounted at the root of the file system. |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 213 | origin_in = in_dir |
| 214 | fs_config = prop_dict.get("fs_config") |
| 215 | if (prop_dict.get("system_root_image") == "true" |
| 216 | and prop_dict["mount_point"] == "system"): |
| 217 | in_dir = tempfile.mkdtemp() |
| 218 | # Change the mount point to "/" |
| 219 | prop_dict["mount_point"] = "/" |
| 220 | if fs_config: |
| 221 | # We need to merge the fs_config files of system and ramdisk. |
| 222 | fd, merged_fs_config = tempfile.mkstemp(prefix="root_fs_config", |
| 223 | suffix=".txt") |
| 224 | os.close(fd) |
| 225 | with open(merged_fs_config, "w") as fw: |
| 226 | if "ramdisk_fs_config" in prop_dict: |
| 227 | with open(prop_dict["ramdisk_fs_config"]) as fr: |
| 228 | fw.writelines(fr.readlines()) |
| 229 | with open(fs_config) as fr: |
| 230 | fw.writelines(fr.readlines()) |
| 231 | fs_config = merged_fs_config |
| 232 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 233 | build_command = [] |
| 234 | fs_type = prop_dict.get("fs_type", "") |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 235 | run_fsck = False |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 236 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 237 | fs_spans_partition = True |
| 238 | if fs_type.startswith("squash"): |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 239 | fs_spans_partition = False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 240 | |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 241 | is_verity_partition = "verity_block_device" in prop_dict |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 242 | verity_supported = prop_dict.get("verity") == "true" |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 243 | # Adjust the partition size to make room for the hashes if this is to be |
| 244 | # verified. |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 245 | if verity_supported and is_verity_partition and fs_spans_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 246 | partition_size = int(prop_dict.get("partition_size")) |
| 247 | adjusted_size = AdjustPartitionSizeForVerity(partition_size) |
| 248 | if not adjusted_size: |
| 249 | return False |
| 250 | prop_dict["partition_size"] = str(adjusted_size) |
| 251 | prop_dict["original_partition_size"] = str(partition_size) |
| 252 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 253 | if fs_type.startswith("ext"): |
| 254 | build_command = ["mkuserimg.sh"] |
| 255 | if "extfs_sparse_flag" in prop_dict: |
| 256 | build_command.append(prop_dict["extfs_sparse_flag"]) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 257 | run_fsck = True |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 258 | build_command.extend([in_dir, out_file, fs_type, |
| 259 | prop_dict["mount_point"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 260 | build_command.append(prop_dict["partition_size"]) |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 261 | if "journal_size" in prop_dict: |
| 262 | build_command.extend(["-j", prop_dict["journal_size"]]) |
Doug Zongker | 850b807 | 2013-12-05 15:54:55 -0800 | [diff] [blame] | 263 | if "timestamp" in prop_dict: |
| 264 | build_command.extend(["-T", str(prop_dict["timestamp"])]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 265 | if fs_config: |
Doug Zongker | 8282282 | 2014-06-16 09:10:55 -0700 | [diff] [blame] | 266 | build_command.extend(["-C", fs_config]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 267 | if "block_list" in prop_dict: |
| 268 | build_command.extend(["-B", prop_dict["block_list"]]) |
Christoffer Dall | 8ed01f3 | 2014-12-17 21:34:12 +0100 | [diff] [blame] | 269 | build_command.extend(["-L", prop_dict["mount_point"]]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 270 | if "selinux_fc" in prop_dict: |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 271 | build_command.append(prop_dict["selinux_fc"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 272 | elif fs_type.startswith("squash"): |
| 273 | build_command = ["mksquashfsimage.sh"] |
| 274 | build_command.extend([in_dir, out_file]) |
Mohamad Ayyash | 2cd51cc | 2015-06-24 10:44:29 -0700 | [diff] [blame] | 275 | build_command.extend(["-s"]) |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 276 | build_command.extend(["-m", prop_dict["mount_point"]]) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 277 | if "selinux_fc" in prop_dict: |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 278 | build_command.extend(["-c", prop_dict["selinux_fc"]]) |
Simon Wilson | 011ea06 | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 279 | if "squashfs_compressor" in prop_dict: |
| 280 | build_command.extend(["-z", prop_dict["squashfs_compressor"]]) |
| 281 | if "squashfs_compressor_opt" in prop_dict: |
| 282 | build_command.extend(["-zo", prop_dict["squashfs_compressor_opt"]]) |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 283 | elif fs_type.startswith("f2fs"): |
| 284 | build_command = ["mkf2fsuserimg.sh"] |
| 285 | build_command.extend([out_file, prop_dict["partition_size"]]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 286 | else: |
| 287 | build_command = ["mkyaffs2image", "-f"] |
| 288 | if prop_dict.get("mkyaffs2_extra_flags", None): |
| 289 | build_command.extend(prop_dict["mkyaffs2_extra_flags"].split()) |
| 290 | build_command.append(in_dir) |
| 291 | build_command.append(out_file) |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 292 | if "selinux_fc" in prop_dict: |
| 293 | build_command.append(prop_dict["selinux_fc"]) |
| 294 | build_command.append(prop_dict["mount_point"]) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 295 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 296 | if in_dir != origin_in: |
| 297 | # Construct a staging directory of the root file system. |
| 298 | ramdisk_dir = prop_dict.get("ramdisk_dir") |
| 299 | if ramdisk_dir: |
| 300 | shutil.rmtree(in_dir) |
| 301 | shutil.copytree(ramdisk_dir, in_dir, symlinks=True) |
| 302 | staging_system = os.path.join(in_dir, "system") |
| 303 | shutil.rmtree(staging_system, ignore_errors=True) |
| 304 | shutil.copytree(origin_in, staging_system, symlinks=True) |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 305 | |
| 306 | reserved_blocks = prop_dict.get("has_ext4_reserved_blocks") == "true" |
| 307 | ext4fs_output = None |
| 308 | |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 309 | try: |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 310 | if reserved_blocks and fs_type.startswith("ext4"): |
| 311 | (ext4fs_output, exit_code) = RunCommand(build_command) |
| 312 | else: |
| 313 | (_, exit_code) = RunCommand(build_command) |
Ying Wang | a2292c9 | 2015-03-24 19:07:40 -0700 | [diff] [blame] | 314 | finally: |
| 315 | if in_dir != origin_in: |
| 316 | # Clean up temporary directories and files. |
| 317 | shutil.rmtree(in_dir, ignore_errors=True) |
| 318 | if fs_config: |
| 319 | os.remove(fs_config) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 320 | if exit_code != 0: |
| 321 | return False |
| 322 | |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 323 | # Bug: 21522719, 22023465 |
| 324 | # There are some reserved blocks on ext4 FS (lesser of 4096 blocks and 2%). |
| 325 | # We need to deduct those blocks from the available space, since they are |
| 326 | # not writable even with root privilege. It only affects devices using |
| 327 | # file-based OTA and a kernel version of 3.10 or greater (currently just |
| 328 | # sprout). |
| 329 | if reserved_blocks and fs_type.startswith("ext4"): |
| 330 | assert ext4fs_output is not None |
| 331 | ext4fs_stats = re.compile( |
| 332 | r'Created filesystem with .* (?P<used_blocks>[0-9]+)/' |
| 333 | r'(?P<total_blocks>[0-9]+) blocks') |
| 334 | m = ext4fs_stats.match(ext4fs_output.strip().split('\n')[-1]) |
| 335 | used_blocks = int(m.groupdict().get('used_blocks')) |
| 336 | total_blocks = int(m.groupdict().get('total_blocks')) |
| 337 | reserved_blocks = min(4096, int(total_blocks * 0.02)) |
| 338 | adjusted_blocks = total_blocks - reserved_blocks |
| 339 | if used_blocks > adjusted_blocks: |
| 340 | mount_point = prop_dict.get("mount_point") |
| 341 | print("Error: Not enough room on %s (total: %d blocks, used: %d blocks, " |
| 342 | "reserved: %d blocks, available: %d blocks)" % ( |
| 343 | mount_point, total_blocks, used_blocks, reserved_blocks, |
| 344 | adjusted_blocks)) |
| 345 | return False |
| 346 | |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 347 | if not fs_spans_partition: |
| 348 | mount_point = prop_dict.get("mount_point") |
| 349 | partition_size = int(prop_dict.get("partition_size")) |
| 350 | image_size = os.stat(out_file).st_size |
| 351 | if image_size > partition_size: |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 352 | print("Error: %s image size of %d is larger than partition size of " |
| 353 | "%d" % (mount_point, image_size, partition_size)) |
| 354 | return False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 355 | if verity_supported and is_verity_partition: |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 356 | if 2 * image_size - AdjustPartitionSizeForVerity(image_size) > partition_size: |
| 357 | print "Error: No more room on %s to fit verity data" % mount_point |
| 358 | return False |
Mohamad Ayyash | dd06352 | 2015-03-24 12:42:03 -0700 | [diff] [blame] | 359 | prop_dict["original_partition_size"] = prop_dict["partition_size"] |
| 360 | prop_dict["partition_size"] = str(image_size) |
| 361 | |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 362 | # create the verified image if this is to be verified |
Geremy Condra | 5b5f495 | 2014-05-05 22:19:37 -0700 | [diff] [blame] | 363 | if verity_supported and is_verity_partition: |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 364 | if not MakeVerityEnabledImage(out_file, prop_dict): |
| 365 | return False |
| 366 | |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 367 | if run_fsck and prop_dict.get("skip_fsck") != "true": |
Geremy Condra | 6e8f53c | 2013-12-05 17:09:18 -0800 | [diff] [blame] | 368 | success, unsparse_image = UnsparseImage(out_file, replace=False) |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 369 | if not success: |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 370 | return False |
| 371 | |
| 372 | # Run e2fsck on the inflated image file |
| 373 | e2fsck_command = ["e2fsck", "-f", "-n", unsparse_image] |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 374 | (_, exit_code) = RunCommand(e2fsck_command) |
Ying Wang | 69e9b4d | 2012-11-26 18:10:23 -0800 | [diff] [blame] | 375 | |
| 376 | os.remove(unsparse_image) |
| 377 | |
| 378 | return exit_code == 0 |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 379 | |
| 380 | |
| 381 | def ImagePropFromGlobalDict(glob_dict, mount_point): |
| 382 | """Build an image property dictionary from the global dictionary. |
| 383 | |
| 384 | Args: |
| 385 | glob_dict: the global dictionary from the build system. |
| 386 | mount_point: such as "system", "data" etc. |
| 387 | """ |
Doug Zongker | 1ad7ade | 2013-12-06 11:53:27 -0800 | [diff] [blame] | 388 | d = {} |
Tao Bao | 052ae35 | 2015-09-28 13:44:13 -0700 | [diff] [blame^] | 389 | |
| 390 | # Use a fixed timestamp (01/01/2009) for all the files in an image. |
| 391 | # Bug: 24377993 |
| 392 | epoch = datetime.datetime.fromtimestamp(0) |
| 393 | timestamp = (datetime.datetime(2009, 1, 1) - epoch).total_seconds() |
| 394 | d["timestamp"] = int(timestamp) |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 395 | |
| 396 | def copy_prop(src_p, dest_p): |
| 397 | if src_p in glob_dict: |
| 398 | d[dest_p] = str(glob_dict[src_p]) |
| 399 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 400 | common_props = ( |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 401 | "extfs_sparse_flag", |
| 402 | "mkyaffs2_extra_flags", |
Kenny Root | f32dc71 | 2012-04-08 10:42:34 -0700 | [diff] [blame] | 403 | "selinux_fc", |
Ying Wang | 6a42a25 | 2013-02-27 13:54:02 -0800 | [diff] [blame] | 404 | "skip_fsck", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 405 | "verity", |
Geremy Condra | fd6f751 | 2013-06-16 17:26:08 -0700 | [diff] [blame] | 406 | "verity_key", |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 407 | "verity_signer_cmd" |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 408 | ) |
| 409 | for p in common_props: |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 410 | copy_prop(p, p) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 411 | |
| 412 | d["mount_point"] = mount_point |
| 413 | if mount_point == "system": |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 414 | copy_prop("fs_type", "fs_type") |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 415 | # Copy the generic sysetem fs type first, override with specific one if |
| 416 | # available. |
Mohamad Ayyash | b97746e | 2015-03-03 12:30:37 -0800 | [diff] [blame] | 417 | copy_prop("system_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 418 | copy_prop("system_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 419 | copy_prop("system_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 420 | copy_prop("system_verity_block_device", "verity_block_device") |
Tao Bao | 2ed665a | 2015-04-01 11:21:55 -0700 | [diff] [blame] | 421 | copy_prop("system_root_image", "system_root_image") |
| 422 | copy_prop("ramdisk_dir", "ramdisk_dir") |
Tao Bao | 84e7568 | 2015-07-19 02:38:53 -0700 | [diff] [blame] | 423 | copy_prop("ramdisk_fs_config", "ramdisk_fs_config") |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 424 | copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks") |
Simon Wilson | 011ea06 | 2015-06-17 12:35:15 -0700 | [diff] [blame] | 425 | copy_prop("system_squashfs_compressor", "squashfs_compressor") |
| 426 | copy_prop("system_squashfs_compressor_opt", "squashfs_compressor_opt") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 427 | elif mount_point == "data": |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 428 | # Copy the generic fs type first, override with specific one if available. |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 429 | copy_prop("fs_type", "fs_type") |
JP Abgrall | 5bfed5a | 2014-06-16 14:17:40 -0700 | [diff] [blame] | 430 | copy_prop("userdata_fs_type", "fs_type") |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 431 | copy_prop("userdata_size", "partition_size") |
| 432 | elif mount_point == "cache": |
| 433 | copy_prop("cache_fs_type", "fs_type") |
| 434 | copy_prop("cache_size", "partition_size") |
Ying Wang | a0febe5 | 2013-03-20 11:02:05 -0700 | [diff] [blame] | 435 | elif mount_point == "vendor": |
| 436 | copy_prop("vendor_fs_type", "fs_type") |
| 437 | copy_prop("vendor_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 438 | copy_prop("vendor_journal_size", "journal_size") |
Daniel Rosenberg | f4eabc3 | 2014-07-10 15:42:38 -0700 | [diff] [blame] | 439 | copy_prop("vendor_verity_block_device", "verity_block_device") |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 440 | copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks") |
Ying Wang | b888843 | 2014-03-11 17:13:27 -0700 | [diff] [blame] | 441 | elif mount_point == "oem": |
| 442 | copy_prop("fs_type", "fs_type") |
| 443 | copy_prop("oem_size", "partition_size") |
Ying Wang | f3b8635 | 2014-11-18 18:03:13 -0800 | [diff] [blame] | 444 | copy_prop("oem_journal_size", "journal_size") |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 445 | copy_prop("has_ext4_reserved_blocks", "has_ext4_reserved_blocks") |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 446 | |
| 447 | return d |
| 448 | |
| 449 | |
| 450 | def LoadGlobalDict(filename): |
| 451 | """Load "name=value" pairs from filename""" |
| 452 | d = {} |
| 453 | f = open(filename) |
| 454 | for line in f: |
| 455 | line = line.strip() |
| 456 | if not line or line.startswith("#"): |
| 457 | continue |
| 458 | k, v = line.split("=", 1) |
| 459 | d[k] = v |
| 460 | f.close() |
| 461 | return d |
| 462 | |
| 463 | |
| 464 | def main(argv): |
| 465 | if len(argv) != 3: |
| 466 | print __doc__ |
| 467 | sys.exit(1) |
| 468 | |
| 469 | in_dir = argv[0] |
| 470 | glob_dict_file = argv[1] |
| 471 | out_file = argv[2] |
| 472 | |
| 473 | glob_dict = LoadGlobalDict(glob_dict_file) |
Ying Wang | 4540a85 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 474 | if "mount_point" in glob_dict: |
Tao Bao | 4e66343 | 2015-06-23 11:16:05 -0700 | [diff] [blame] | 475 | # The caller knows the mount point and provides a dictionay needed by |
| 476 | # BuildImage(). |
Ying Wang | 4540a85 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 477 | image_properties = glob_dict |
Ying Wang | 9f8e8db | 2011-11-04 11:37:01 -0700 | [diff] [blame] | 478 | else: |
Ying Wang | 4540a85 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 479 | image_filename = os.path.basename(out_file) |
| 480 | mount_point = "" |
| 481 | if image_filename == "system.img": |
| 482 | mount_point = "system" |
| 483 | elif image_filename == "userdata.img": |
| 484 | mount_point = "data" |
| 485 | elif image_filename == "cache.img": |
| 486 | mount_point = "cache" |
| 487 | elif image_filename == "vendor.img": |
| 488 | mount_point = "vendor" |
| 489 | elif image_filename == "oem.img": |
| 490 | mount_point = "oem" |
| 491 | else: |
| 492 | print >> sys.stderr, "error: unknown image file name ", image_filename |
| 493 | exit(1) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 494 | |
Ying Wang | 4540a85 | 2015-03-12 18:30:39 -0700 | [diff] [blame] | 495 | image_properties = ImagePropFromGlobalDict(glob_dict, mount_point) |
| 496 | |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 497 | if not BuildImage(in_dir, image_properties, out_file): |
Dan Albert | 8b72aef | 2015-03-23 19:13:21 -0700 | [diff] [blame] | 498 | print >> sys.stderr, "error: failed to build %s from %s" % (out_file, |
| 499 | in_dir) |
Ying Wang | bd93d42 | 2011-10-28 17:02:30 -0700 | [diff] [blame] | 500 | exit(1) |
| 501 | |
| 502 | |
| 503 | if __name__ == '__main__': |
| 504 | main(sys.argv[1:]) |