• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17import os
18import shutil
19import argparse
20import json
21from mkimage import mkimages
22
23sys.path.append(
24    os.path.dirname(os.path.dirname(os.path.dirname(
25        os.path.abspath(__file__)))))
26from scripts.util import build_utils
27from build_scripts.build import find_top  # noqa: E402
28
29
30def _prepare_userdata(userdata_path: str):
31    if os.path.exists(userdata_path):
32        shutil.rmtree(userdata_path)
33    os.makedirs(userdata_path, exist_ok=True)
34
35
36def _prepare_root(system_path: str, target_cpu: str):
37    root_dir = os.path.join(os.path.dirname(system_path), 'root')
38    if os.path.exists(root_dir):
39        shutil.rmtree(root_dir)
40    os.makedirs(root_dir, exist_ok=True)
41    _dir_list = [
42        'config', 'dev', 'proc', 'sys', 'updater', 'system', 'vendor', 'data', 'chip_ckm',
43        'storage', 'mnt', 'tmp', 'sys_prod', 'chip_prod', 'module_update', 'eng_system', 'eng_chipset'
44    ]
45    for _dir_name in _dir_list:
46        os.makedirs(os.path.join(root_dir, _dir_name), exist_ok=True)
47    os.symlink('/system/bin', os.path.join(root_dir, 'bin'))
48    os.symlink('/system/bin/init', os.path.join(root_dir, 'init'))
49    os.symlink('/system/etc', os.path.join(root_dir, 'etc'))
50    os.symlink('/vendor', os.path.join(root_dir, 'chipset'))
51    if target_cpu == 'arm64' or target_cpu == 'riscv64':
52        os.symlink('/system/lib64', os.path.join(root_dir, 'lib64'))
53    os.symlink('/system/lib', os.path.join(root_dir, 'lib'))
54
55
56def _prepare_updater(updater_path: str, target_cpu: str):
57    _dir_list = ['dev', 'proc', 'sys', 'system', 'tmp', 'lib', 'lib64', 'vendor']
58    for _dir_name in _dir_list:
59        _path = os.path.join(updater_path, _dir_name)
60        if os.path.exists(_path):
61            continue
62        os.makedirs(_path, exist_ok=True)
63    os.symlink('bin/init', os.path.join(updater_path, 'init'))
64    os.symlink('/bin', os.path.join(updater_path, 'system/bin'))
65    os.symlink('/lib', os.path.join(updater_path, 'system/lib'))
66    if target_cpu == 'arm64':
67        os.symlink('/lib64', os.path.join(updater_path, 'system/lib64'))
68        os.symlink('/lib64', os.path.join(updater_path, 'vendor/lib64'))
69    else:
70        os.symlink('/lib', os.path.join(updater_path, 'vendor/lib'))
71    os.symlink('/etc', os.path.join(updater_path, 'system/etc'))
72
73
74def _prepare_ramdisk(ramdisk_path: str):
75    _dir_list = ['bin', 'dev', 'etc', 'lib', 'proc', 'sys', 'system', 'usr', 'mnt', 'storage']
76    for _dir_name in _dir_list:
77        _path = os.path.join(ramdisk_path, _dir_name)
78        if os.path.exists(_path):
79            continue
80        os.makedirs(_path, exist_ok=True)
81    os.symlink('bin/init_early', os.path.join(ramdisk_path, 'init'))
82
83
84def _prepare_eng_ststem(eng_system_path: str, build_variant: str):
85    if os.path.exists(eng_system_path):
86        shutil.rmtree(eng_system_path)
87    os.makedirs(eng_system_path, exist_ok=True)
88    if build_variant == "user":
89        return
90    _dir_list_first = ['etc', 'bin']
91    for _dir_name in _dir_list_first:
92        _path = os.path.join(eng_system_path, _dir_name)
93        if os.path.exists(_path):
94            shutil.rmtree(_path)
95        os.makedirs(_path, exist_ok=True)
96    _dir_list_second = ['param', 'init', 'selinux']
97    for _dir_name in _dir_list_second:
98        _path = os.path.join(eng_system_path, 'etc', _dir_name)
99        if os.path.exists(_path):
100            shutil.rmtree(_path)
101        os.makedirs(_path, exist_ok=True)
102    _target_policy_path = os.path.join(eng_system_path, 'etc', 'selinux', 'targeted', 'policy')
103    if os.path.exists(_target_policy_path):
104        shutil.rmtree(_target_policy_path)
105    os.makedirs(_target_policy_path, exist_ok=True)
106    root_path = find_top()
107    copy_eng_system_config = os.path.join(root_path, "build/ohos/images/mkimage/root_image.json")
108    with open(copy_eng_system_config, 'rb') as input_f:
109        default_build_args = json.load(input_f)
110    for arg in default_build_args.values():
111        sources_file = arg.get('source_file')
112        dest_file = arg.get('dest_file')
113        if(os.path.exists(dest_file)):
114            os.remove(dest_file)
115        if(os.path.exists(sources_file)):
116            shutil.copy(sources_file, dest_file)
117
118
119def _make_image(args):
120    if args.image_name == 'system':
121        _prepare_root(args.input_path, args.target_cpu)
122    elif args.image_name == 'updater':
123        _prepare_updater(args.input_path, args.target_cpu)
124    elif args.image_name == 'updater_ramdisk':
125        _prepare_updater(args.input_path, args.target_cpu)
126    elif args.image_name == 'ramdisk':
127        _prepare_ramdisk(args.input_path)
128    image_type = "raw"
129    if args.sparse_image:
130        image_type = "sparse"
131    config_file = args.image_config_file
132    if (os.path.exists(args.device_image_config_file)):
133        config_file = args.device_image_config_file
134    mk_image_args = [
135        args.input_path, config_file, args.output_image_path,
136        image_type
137    ]
138    if args.build_image_tools_path:
139        env_path = ':'.join(args.build_image_tools_path)
140        os.environ['PATH'] = '{}:{}'.format(env_path, os.environ.get('PATH'))
141    mkimages.mk_images(mk_image_args)
142
143
144def main(argv):
145    parser = argparse.ArgumentParser()
146    parser.add_argument('--depfile', required=True)
147    parser.add_argument('--image-name', required=True)
148    parser.add_argument('--build-variant', required=True)
149    parser.add_argument('--image-config-file', required=True)
150    parser.add_argument('--device-image-config-file', required=True)
151    parser.add_argument('--input-path', required=True)
152    parser.add_argument('--output-image-path', required=True)
153    parser.add_argument('--sparse-image',
154                        dest="sparse_image",
155                        action='store_true')
156    parser.set_defaults(sparse_image=False)
157    parser.add_argument('--build-image-tools-path', nargs='*', required=False)
158    parser.add_argument('--target-cpu', required=False)
159    args = parser.parse_args(argv)
160
161    if os.path.exists(args.output_image_path):
162        os.remove(args.output_image_path)
163    if args.image_name == 'userdata':
164        _prepare_userdata(args.input_path)
165    elif args.image_name == 'eng_system':
166        _prepare_eng_ststem(args.input_path, args.build_variant)
167    if os.path.isdir(args.input_path):
168        _make_image(args)
169        _dep_files = []
170        for _root, _, _files in os.walk(args.input_path):
171            for _file in _files:
172                _dep_files.append(os.path.join(_root, _file))
173        if (os.path.exists(args.device_image_config_file)):
174            _dep_files.append(args.device_image_config_file)
175        build_utils.write_depfile(args.depfile,
176                                  args.output_image_path,
177                                  _dep_files,
178                                  add_pydeps=False)
179    return 0
180
181
182if __name__ == '__main__':
183    sys.exit(main(sys.argv[1:]))
184