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 20from mkimage import mkimages 21 22sys.path.append( 23 os.path.dirname(os.path.dirname(os.path.dirname( 24 os.path.abspath(__file__))))) 25from scripts.util import build_utils # noqa: E402 26 27 28def _prepare_userdata(userdata_path): 29 if os.path.exists(userdata_path): 30 shutil.rmtree(userdata_path) 31 os.makedirs(userdata_path) 32 33 34def _prepare_root(system_path, target_cpu): 35 root_dir = os.path.join(os.path.dirname(system_path), 'root') 36 if os.path.exists(root_dir): 37 shutil.rmtree(root_dir) 38 os.makedirs(root_dir, exist_ok=True) 39 _dir_list = [ 40 'config', 'dev', 'proc', 'sys', 'updater', 'system', 'vendor', 'data', 41 'storage', 'mnt', 'tmp', 'sys_prod', 'chip_prod' 42 ] 43 for _dir_name in _dir_list: 44 os.makedirs(os.path.join(root_dir, _dir_name), exist_ok=True) 45 os.symlink('/system/bin', os.path.join(root_dir, 'bin')) 46 os.symlink('/system/bin/init', os.path.join(root_dir, 'init')) 47 os.symlink('/system/etc', os.path.join(root_dir, 'etc')) 48 os.symlink('/vendor', os.path.join(root_dir, 'chipset')) 49 if target_cpu == 'arm64': 50 os.symlink('/system/lib64', os.path.join(root_dir, 'lib64')) 51 os.symlink('/system/lib', os.path.join(root_dir, 'lib')) 52 53 54def _prepare_updater(updater_path): 55 _dir_list = ['dev', 'proc', 'sys', 'system', 'tmp', 'lib', 'lib64', 'vendor' ] 56 for _dir_name in _dir_list: 57 _path = os.path.join(updater_path, _dir_name) 58 if os.path.exists(_path): 59 continue 60 os.makedirs(_path, exist_ok=True) 61 os.symlink('bin/init', os.path.join(updater_path, 'init')) 62 os.symlink('/bin', os.path.join(updater_path, 'system/bin')) 63 os.symlink('/lib', os.path.join(updater_path, 'system/lib')) 64 os.symlink('/lib64', os.path.join(updater_path, 'system/lib64')) 65 os.symlink('/lib64', os.path.join(updater_path, 'vendor/lib64')) 66 os.symlink('/etc', os.path.join(updater_path, 'system/etc')) 67 68 69def _prepare_ramdisk(ramdisk_path): 70 _dir_list = ['bin', 'dev', 'etc', 'lib', 'proc', 'sys', 'system', 'usr', 'mnt', 'storage'] 71 for _dir_name in _dir_list: 72 _path = os.path.join(ramdisk_path, _dir_name) 73 if os.path.exists(_path): 74 continue 75 os.makedirs(_path, exist_ok=True) 76 os.symlink('bin/init', os.path.join(ramdisk_path, 'init')) 77 78 79def _make_image(args): 80 if args.image_name == 'system': 81 _prepare_root(args.input_path, args.target_cpu) 82 elif args.image_name == 'updater': 83 _prepare_updater(args.input_path) 84 elif args.image_name == 'updater_ramdisk': 85 _prepare_updater(args.input_path) 86 elif args.image_name == 'ramdisk': 87 _prepare_ramdisk(args.input_path) 88 image_type = "raw" 89 if args.sparse_image: 90 image_type = "sparse" 91 config_file = args.image_config_file 92 if (os.path.exists(args.device_image_config_file)): 93 config_file = args.device_image_config_file 94 mk_image_args = [ 95 args.input_path, config_file, args.output_image_path, 96 image_type 97 ] 98 if args.build_image_tools_path: 99 env_path = ':'.join(args.build_image_tools_path) 100 os.environ['PATH'] = '{}:{}'.format(env_path, os.environ.get('PATH')) 101 mkimages.mk_images(mk_image_args) 102 103 104def main(argv): 105 parser = argparse.ArgumentParser() 106 parser.add_argument('--depfile', required=True) 107 parser.add_argument('--image-name', required=True) 108 parser.add_argument('--image-config-file', required=True) 109 parser.add_argument('--device-image-config-file', required=True) 110 parser.add_argument('--input-path', required=True) 111 parser.add_argument('--output-image-path', required=True) 112 parser.add_argument('--sparse-image', 113 dest="sparse_image", 114 action='store_true') 115 parser.set_defaults(sparse_image=False) 116 parser.add_argument('--build-image-tools-path', nargs='*', required=False) 117 parser.add_argument('--target-cpu', required=False) 118 args = parser.parse_args(argv) 119 120 if os.path.exists(args.output_image_path): 121 os.remove(args.output_image_path) 122 if args.image_name == 'userdata': 123 _prepare_userdata(args.input_path) 124 if os.path.isdir(args.input_path): 125 _make_image(args) 126 _dep_files = [] 127 for _root, _, _files in os.walk(args.input_path): 128 for _file in _files: 129 _dep_files.append(os.path.join(_root, _file)) 130 if (os.path.exists(args.device_image_config_file)): 131 _dep_files.append(args.device_image_config_file) 132 build_utils.write_depfile(args.depfile, 133 args.output_image_path, 134 _dep_files, 135 add_pydeps=False) 136 return 0 137 138 139if __name__ == '__main__': 140 sys.exit(main(sys.argv[1:])) 141