blob: 4b88e73b0bb9da62d26845bcd5ba997162cf1103 [file] [log] [blame]
Doug Zongkereef39442009-04-02 12:14:19 -07001#!/usr/bin/env python
2#
3# Copyright (C) 2008 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"""
18Given a target-files zipfile, produces an image zipfile suitable for
19use with 'fastboot update'.
20
21Usage: img_from_target_files [flags] input_target_files output_image_zip
22
Doug Zongker55d93282011-01-25 17:03:34 -080023 -z (--bootable_zip)
24 Include only the bootable images (eg 'boot' and 'recovery') in
25 the output.
26
Doug Zongkereef39442009-04-02 12:14:19 -070027"""
28
29import sys
30
Doug Zongkercf6d5a92014-02-18 10:57:07 -080031if sys.hexversion < 0x02070000:
32 print >> sys.stderr, "Python 2.7 or newer is required."
Doug Zongkereef39442009-04-02 12:14:19 -070033 sys.exit(1)
34
Mike Rittere44fade2009-09-15 11:18:31 -070035import errno
Doug Zongkereef39442009-04-02 12:14:19 -070036import os
37import re
38import shutil
39import subprocess
40import tempfile
41import zipfile
42
43# missing in Python 2.4 and before
44if not hasattr(os, "SEEK_SET"):
45 os.SEEK_SET = 0
46
47import common
48
49OPTIONS = common.OPTIONS
50
Ying Wanga0febe52013-03-20 11:02:05 -070051
Doug Zongkereef39442009-04-02 12:14:19 -070052def CopyInfo(output_zip):
53 """Copy the android-info.txt file from the input to the output."""
54 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
55 "android-info.txt")
56
57
58def main(argv):
Doug Zongker55d93282011-01-25 17:03:34 -080059 bootable_only = [False]
Doug Zongkereef39442009-04-02 12:14:19 -070060
61 def option_handler(o, a):
Doug Zongker55d93282011-01-25 17:03:34 -080062 if o in ("-z", "--bootable_zip"):
63 bootable_only[0] = True
Doug Zongkereef39442009-04-02 12:14:19 -070064 else:
65 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -070066 return True
Doug Zongkereef39442009-04-02 12:14:19 -070067
68 args = common.ParseOptions(argv, __doc__,
Doug Zongker3c84f562014-07-31 11:06:30 -070069 extra_opts="z",
70 extra_long_opts=["bootable_zip"],
Doug Zongkereef39442009-04-02 12:14:19 -070071 extra_option_handler=option_handler)
72
Doug Zongker55d93282011-01-25 17:03:34 -080073 bootable_only = bootable_only[0]
74
Doug Zongkereef39442009-04-02 12:14:19 -070075 if len(args) != 2:
76 common.Usage(__doc__)
77 sys.exit(1)
78
Doug Zongker55d93282011-01-25 17:03:34 -080079 OPTIONS.input_tmp, input_zip = common.UnzipTemp(args[0])
Doug Zongkereef39442009-04-02 12:14:19 -070080 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
Doug Zongker3c84f562014-07-31 11:06:30 -070081 CopyInfo(output_zip)
Doug Zongkereef39442009-04-02 12:14:19 -070082
Doug Zongker3c84f562014-07-31 11:06:30 -070083 try:
84 done = False
85 images_path = os.path.join(OPTIONS.input_tmp, "IMAGES")
86 if os.path.exists(images_path):
87 # If this is a new target-files, it already contains the images,
88 # and all we have to do is copy them to the output zip.
89 images = os.listdir(images_path)
90 if images:
91 for i in images:
92 if bootable_only and i not in ("boot.img", "recovery.img"): continue
Doug Zongkerf21cb5a2014-08-12 14:16:55 -070093 if not i.endswith(".img"): continue
Doug Zongker3c84f562014-07-31 11:06:30 -070094 with open(os.path.join(images_path, i), "r") as f:
95 common.ZipWriteStr(output_zip, i, f.read())
96 done = True
Doug Zongker55d93282011-01-25 17:03:34 -080097
Doug Zongker3c84f562014-07-31 11:06:30 -070098 if not done:
99 # We have an old target-files that doesn't already contain the
100 # images, so build them.
101 import add_img_to_target_files
Doug Zongkerc8b4e842014-06-16 15:16:31 -0700102
Doug Zongker3c84f562014-07-31 11:06:30 -0700103 OPTIONS.info_dict = common.LoadInfoDict(input_zip)
Doug Zongkereef39442009-04-02 12:14:19 -0700104
Doug Zongker3c84f562014-07-31 11:06:30 -0700105 # If this image was originally labelled with SELinux contexts,
106 # make sure we also apply the labels in our new image. During
107 # building, the "file_contexts" is in the out/ directory tree,
108 # but for repacking from target-files.zip it's in the root
109 # directory of the ramdisk.
110 if "selinux_fc" in OPTIONS.info_dict:
111 OPTIONS.info_dict["selinux_fc"] = os.path.join(
112 OPTIONS.input_tmp, "BOOT", "RAMDISK", "file_contexts")
113
114 boot_image = common.GetBootableImage(
115 "boot.img", "boot.img", OPTIONS.input_tmp, "BOOT")
116 if boot_image:
117 boot_image.AddToZip(output_zip)
118 recovery_image = common.GetBootableImage(
119 "recovery.img", "recovery.img", OPTIONS.input_tmp, "RECOVERY")
120 if recovery_image:
121 recovery_image.AddToZip(output_zip)
122
123 def banner(s):
124 print "\n\n++++ " + s + " ++++\n\n"
125
126 if not bootable_only:
127 banner("AddSystem")
128 add_img_to_target_files.AddSystem(output_zip, prefix="")
129 try:
130 input_zip.getinfo("VENDOR/")
131 banner("AddVendor")
132 add_img_to_target_files.AddVendor(output_zip, prefix="")
133 except KeyError:
134 pass # no vendor partition for this device
135 banner("AddUserdata")
136 add_img_to_target_files.AddUserdata(output_zip, prefix="")
137 banner("AddCache")
138 add_img_to_target_files.AddCache(output_zip, prefix="")
139
140 finally:
141 print "cleaning up..."
142 output_zip.close()
143 shutil.rmtree(OPTIONS.input_tmp)
Doug Zongkereef39442009-04-02 12:14:19 -0700144
145 print "done."
146
147
148if __name__ == '__main__':
149 try:
Ying Wang7e6d4e42010-12-13 16:25:36 -0800150 common.CloseInheritedPipes()
Doug Zongkereef39442009-04-02 12:14:19 -0700151 main(sys.argv[1:])
152 except common.ExternalError, e:
153 print
154 print " ERROR: %s" % (e,)
155 print
156 sys.exit(1)