blob: a615d1ac3a10779722fbf176fc281055113eefe7 [file] [log] [blame]
Ying Wangbd93d422011-10-28 17:02:30 -07001#!/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"""
18Build image output_image_file from input_directory and properties_file.
19
20Usage: build_image input_directory properties_file output_image_file
21
22"""
23import os
24import subprocess
25import sys
26
27
28def BuildImage(in_dir, prop_dict, out_file):
29 """Build an image to out_file from in_dir with property prop_dict.
30
31 Args:
32 in_dir: path of input directory.
33 prop_dict: property dictionary.
34 out_file: path of the output image file.
35
36 Returns:
37 True iff the image is built successfully.
38 """
39 build_command = []
40 fs_type = prop_dict.get("fs_type", "")
41 if fs_type.startswith("ext"):
42 build_command = ["mkuserimg.sh"]
43 if "extfs_sparse_flag" in prop_dict:
44 build_command.append(prop_dict["extfs_sparse_flag"])
45 build_command.extend([in_dir, out_file, fs_type,
46 prop_dict["mount_point"]])
47 if "partition_size" in prop_dict:
48 build_command.append(prop_dict["partition_size"])
Kenny Rootf32dc712012-04-08 10:42:34 -070049 if "selinux_fc" in prop_dict:
50 build_command.append(prop_dict["selinux_fc"])
Ying Wangbd93d422011-10-28 17:02:30 -070051 else:
52 build_command = ["mkyaffs2image", "-f"]
53 if prop_dict.get("mkyaffs2_extra_flags", None):
54 build_command.extend(prop_dict["mkyaffs2_extra_flags"].split())
55 build_command.append(in_dir)
56 build_command.append(out_file)
Kenny Rootf32dc712012-04-08 10:42:34 -070057 if "selinux_fc" in prop_dict:
58 build_command.append(prop_dict["selinux_fc"])
59 build_command.append(prop_dict["mount_point"])
Ying Wangbd93d422011-10-28 17:02:30 -070060
61 print "Running: ", " ".join(build_command)
62 p = subprocess.Popen(build_command);
63 p.communicate()
64 return p.returncode == 0
65
66
67def ImagePropFromGlobalDict(glob_dict, mount_point):
68 """Build an image property dictionary from the global dictionary.
69
70 Args:
71 glob_dict: the global dictionary from the build system.
72 mount_point: such as "system", "data" etc.
73 """
74 d = {}
Ying Wang9f8e8db2011-11-04 11:37:01 -070075
76 def copy_prop(src_p, dest_p):
77 if src_p in glob_dict:
78 d[dest_p] = str(glob_dict[src_p])
79
Ying Wangbd93d422011-10-28 17:02:30 -070080 common_props = (
Ying Wangbd93d422011-10-28 17:02:30 -070081 "extfs_sparse_flag",
82 "mkyaffs2_extra_flags",
Kenny Rootf32dc712012-04-08 10:42:34 -070083 "selinux_fc",
Ying Wangbd93d422011-10-28 17:02:30 -070084 )
85 for p in common_props:
Ying Wang9f8e8db2011-11-04 11:37:01 -070086 copy_prop(p, p)
Ying Wangbd93d422011-10-28 17:02:30 -070087
88 d["mount_point"] = mount_point
89 if mount_point == "system":
Ying Wang9f8e8db2011-11-04 11:37:01 -070090 copy_prop("fs_type", "fs_type")
91 copy_prop("system_size", "partition_size")
Ying Wangbd93d422011-10-28 17:02:30 -070092 elif mount_point == "data":
Ying Wang9f8e8db2011-11-04 11:37:01 -070093 copy_prop("fs_type", "fs_type")
94 copy_prop("userdata_size", "partition_size")
95 elif mount_point == "cache":
96 copy_prop("cache_fs_type", "fs_type")
97 copy_prop("cache_size", "partition_size")
Ying Wangbd93d422011-10-28 17:02:30 -070098
99 return d
100
101
102def LoadGlobalDict(filename):
103 """Load "name=value" pairs from filename"""
104 d = {}
105 f = open(filename)
106 for line in f:
107 line = line.strip()
108 if not line or line.startswith("#"):
109 continue
110 k, v = line.split("=", 1)
111 d[k] = v
112 f.close()
113 return d
114
115
116def main(argv):
117 if len(argv) != 3:
118 print __doc__
119 sys.exit(1)
120
121 in_dir = argv[0]
122 glob_dict_file = argv[1]
123 out_file = argv[2]
124
125 glob_dict = LoadGlobalDict(glob_dict_file)
126 image_filename = os.path.basename(out_file)
127 mount_point = ""
128 if image_filename == "system.img":
129 mount_point = "system"
130 elif image_filename == "userdata.img":
131 mount_point = "data"
Ying Wang9f8e8db2011-11-04 11:37:01 -0700132 elif image_filename == "cache.img":
133 mount_point = "cache"
134 else:
135 print >> sys.stderr, "error: unknown image file name ", image_filename
136 exit(1)
Ying Wangbd93d422011-10-28 17:02:30 -0700137
138 image_properties = ImagePropFromGlobalDict(glob_dict, mount_point)
139 if not BuildImage(in_dir, image_properties, out_file):
140 print >> sys.stderr, "error: failed to build %s from %s" % (out_file, in_dir)
141 exit(1)
142
143
144if __name__ == '__main__':
145 main(sys.argv[1:])