blob: 00abde47d725c8332610b0320baea5c041a4a5ab [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>
Doug Zongkerfdd8e692009-08-03 17:27:48 -070024 Deprecated.
Doug Zongkereef39442009-04-02 12:14:19 -070025
26"""
27
28import sys
29
30if sys.hexversion < 0x02040000:
31 print >> sys.stderr, "Python 2.4 or newer is required."
32 sys.exit(1)
33
34import os
35import re
36import shutil
37import subprocess
38import tempfile
39import zipfile
40
41# missing in Python 2.4 and before
42if not hasattr(os, "SEEK_SET"):
43 os.SEEK_SET = 0
44
45import common
46
47OPTIONS = common.OPTIONS
48
49
50def AddUserdata(output_zip):
51 """Create an empty userdata image and store it in output_zip."""
52
53 print "creating userdata.img..."
54
55 # The name of the directory it is making an image out of matters to
56 # mkyaffs2image. So we create a temp dir, and within it we create an
57 # empty dir named "data", and build the image from that.
58 temp_dir = tempfile.mkdtemp()
59 user_dir = os.path.join(temp_dir, "data")
60 os.mkdir(user_dir)
61 img = tempfile.NamedTemporaryFile()
62
63 p = common.Run(["mkyaffs2image", "-f", user_dir, img.name])
64 p.communicate()
65 assert p.returncode == 0, "mkyaffs2image of userdata.img image failed"
66
67 common.CheckSize(img.name, "userdata.img")
68 output_zip.write(img.name, "userdata.img")
69 img.close()
70 os.rmdir(user_dir)
71 os.rmdir(temp_dir)
72
73
74def AddSystem(output_zip):
75 """Turn the contents of SYSTEM into a system image and store it in
76 output_zip."""
77
78 print "creating system.img..."
79
80 img = tempfile.NamedTemporaryFile()
81
82 # The name of the directory it is making an image out of matters to
83 # mkyaffs2image. It wants "system" but we have a directory named
84 # "SYSTEM", so create a symlink.
85 os.symlink(os.path.join(OPTIONS.input_tmp, "SYSTEM"),
86 os.path.join(OPTIONS.input_tmp, "system"))
87
88 p = common.Run(["mkyaffs2image", "-f",
89 os.path.join(OPTIONS.input_tmp, "system"), img.name])
90 p.communicate()
91 assert p.returncode == 0, "mkyaffs2image of system.img image failed"
92
93 img.seek(os.SEEK_SET, 0)
94 data = img.read()
95 img.close()
96
97 common.CheckSize(data, "system.img")
Doug Zongker048e7ca2009-06-15 14:31:53 -070098 common.ZipWriteStr(output_zip, "system.img", data)
Doug Zongkereef39442009-04-02 12:14:19 -070099
100
101def CopyInfo(output_zip):
102 """Copy the android-info.txt file from the input to the output."""
103 output_zip.write(os.path.join(OPTIONS.input_tmp, "OTA", "android-info.txt"),
104 "android-info.txt")
105
106
107def main(argv):
108
109 def option_handler(o, a):
110 if o in ("-b", "--board_config"):
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700111 pass # deprecated
Doug Zongkereef39442009-04-02 12:14:19 -0700112 else:
113 return False
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700114 return True
Doug Zongkereef39442009-04-02 12:14:19 -0700115
116 args = common.ParseOptions(argv, __doc__,
117 extra_opts="b:",
118 extra_long_opts=["board_config="],
119 extra_option_handler=option_handler)
120
121 if len(args) != 2:
122 common.Usage(__doc__)
123 sys.exit(1)
124
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700125 OPTIONS.input_tmp = common.UnzipTemp(args[0])
126
127 common.LoadMaxSizes()
Doug Zongkereef39442009-04-02 12:14:19 -0700128 if not OPTIONS.max_image_size:
129 print
Doug Zongkerfdd8e692009-08-03 17:27:48 -0700130 print " WARNING: Failed to load max image sizes; will not enforce"
131 print " image size limits."
Doug Zongkereef39442009-04-02 12:14:19 -0700132 print
133
Doug Zongkereef39442009-04-02 12:14:19 -0700134 output_zip = zipfile.ZipFile(args[1], "w", compression=zipfile.ZIP_DEFLATED)
135
136 common.AddBoot(output_zip)
137 common.AddRecovery(output_zip)
138 AddSystem(output_zip)
139 AddUserdata(output_zip)
140 CopyInfo(output_zip)
141
142 print "cleaning up..."
143 output_zip.close()
144 shutil.rmtree(OPTIONS.input_tmp)
145
146 print "done."
147
148
149if __name__ == '__main__':
150 try:
151 main(sys.argv[1:])
152 except common.ExternalError, e:
153 print
154 print " ERROR: %s" % (e,)
155 print
156 sys.exit(1)