blob: 345135237a15084bf05f2492615029698af22ce8 [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
23 -b (--board_config) <file>
24 Specifies a BoardConfig.mk file containing image max sizes
25 against which the generated image files are checked.
26
27"""
28
29import sys
30
31if sys.hexversion < 0x02040000:
32 print >> sys.stderr, "Python 2.4 or newer is required."
33 sys.exit(1)
34
35import os
36import re
37import shutil
38import subprocess
39import tempfile
40import zipfile
41
42# missing in Python 2.4 and before
43if not hasattr(os, "SEEK_SET"):
44 os.SEEK_SET = 0
45
46import common
47
48OPTIONS = common.OPTIONS
49
50
51def AddUserdata(output_zip):
52 """Create an empty userdata image and store it in output_zip."""
53
54 print "creating userdata.img..."
55
56 # The name of the directory it is making an image out of matters to
57 # mkyaffs2image. So we create a temp dir, and within it we create an
58 # empty dir named "data", and build the image from that.
59 temp_dir = tempfile.mkdtemp()
60 user_dir = os.path.join(temp_dir, "data")
61 os.mkdir(user_dir)
62 img = tempfile.NamedTemporaryFile()
63
64 p = common.Run(["mkyaffs2image", "-f", user_dir, img.name])
65 p.communicate()
66 assert p.returncode == 0, "mkyaffs2image of userdata.img image failed"
67
68 common.CheckSize(img.name, "userdata.img")
69 output_zip.write(img.name, "userdata.img")
70 img.close()
71 os.rmdir(user_dir)
72 os.rmdir(temp_dir)
73
74
75def AddSystem(output_zip):
76 """Turn the contents of SYSTEM into a system image and store it in
77 output_zip."""
78
79 print "creating system.img..."
80
81 img = tempfile.NamedTemporaryFile()
82
83 # The name of the directory it is making an image out of matters to
84 # mkyaffs2image. It wants "system" but we have a directory named
85 # "SYSTEM", so create a symlink.
86 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
87 os.path.join(OPTIONS.input_tmp, "system"))
88
89 p = common.Run(["mkyaffs2image", "-f",
90 os.path.join(OPTIONS.input_tmp, "system"), img.name])
91 p.communicate()
92 assert p.returncode == 0, "mkyaffs2image of system.img image failed"
93
94 img.seek(os.SEEK_SET, 0)
95 data = img.read()
96 img.close()
97
98 common.CheckSize(data, "system.img")
99 output_zip.writestr("system.img", data)
100
101
102def CopyInfo(output_zip):
103 """Copy the android-info.txt file from the input to the output."""
104 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
105 "android-info.txt")
106
107
108def main(argv):
109
110 def option_handler(o, a):
111 if o in ("-b", "--board_config"):
112 common.LoadBoardConfig(a)
113 return True
114 else:
115 return False
116
117 args = common.ParseOptions(argv, __doc__,
118 extra_opts="b:",
119 extra_long_opts=["board_config="],
120 extra_option_handler=option_handler)
121
122 if len(args) != 2:
123 common.Usage(__doc__)
124 sys.exit(1)
125
126 if not OPTIONS.max_image_size:
127 print
128 print " WARNING: No board config specified; will not check image"
129 print " sizes against limits. Use -b to make sure the generated"
130 print " images don't exceed partition sizes."
131 print
132
133 OPTIONS.input_tmp = common.UnzipTemp(args[0])
134
135 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
136
137 common.AddBoot(output_zip)
138 common.AddRecovery(output_zip)
139 AddSystem(output_zip)
140 AddUserdata(output_zip)
141 CopyInfo(output_zip)
142
143 print "cleaning up..."
144 output_zip.close()
145 shutil.rmtree(OPTIONS.input_tmp)
146
147 print "done."
148
149
150if __name__ == '__main__':
151 try:
152 main(sys.argv[1:])
153 except common.ExternalError, e:
154 print
155 print " ERROR: %s" % (e,)
156 print
157 sys.exit(1)