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