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 'chipset', '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 if target_cpu == 'arm64': 49 os.symlink('/system/lib64', os.path.join(root_dir, 'lib')) 50 else: 51 os.symlink('/system/lib', os.path.join(root_dir, 'lib')) 52 53 54def _prepare_updater(updater_path): 55 _dir_list = ['dev', 'proc', 'sys'] 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 63 64def _prepare_ramdisk(ramdisk_path): 65 _dir_list = ['bin', 'dev', 'etc', 'lib', 'proc', 'sys', 'system', 'usr', 'mnt'] 66 for _dir_name in _dir_list: 67 _path = os.path.join(ramdisk_path, _dir_name) 68 if os.path.exists(_path): 69 continue 70 os.makedirs(_path, exist_ok=True) 71 os.symlink('bin/init', os.path.join(ramdisk_path, 'init')) 72 73 74def _make_image(args): 75 if args.image_name == 'system': 76 _prepare_root(args.input_path, args.target_cpu) 77 elif args.image_name == 'updater': 78 _prepare_updater(args.input_path) 79 elif args.image_name == 'ramdisk': 80 _prepare_ramdisk(args.input_path) 81 image_type = "raw" 82 if args.sparse_image: 83 image_type = "sparse" 84 config_file = args.image_config_file 85 if (os.path.exists(args.device_image_config_file)): 86 config_file = args.device_image_config_file 87 mk_image_args = [ 88 args.input_path, config_file, args.output_image_path, 89 image_type 90 ] 91 if args.build_image_tools_path: 92 env_path = ':'.join(args.build_image_tools_path) 93 os.environ['PATH'] = '{}:{}'.format(env_path, os.environ.get('PATH')) 94 mkimages.mk_images(mk_image_args) 95 96 97def main(argv): 98 parser = argparse.ArgumentParser() 99 parser.add_argument('--depfile', required=True) 100 parser.add_argument('--image-name', required=True) 101 parser.add_argument('--image-config-file', required=True) 102 parser.add_argument('--device-image-config-file', required=True) 103 parser.add_argument('--input-path', required=True) 104 parser.add_argument('--output-image-path', required=True) 105 parser.add_argument('--sparse-image', 106 dest="sparse_image", 107 action='store_true') 108 parser.set_defaults(sparse_image=False) 109 parser.add_argument('--build-image-tools-path', nargs='*', required=False) 110 parser.add_argument('--target-cpu', required=False) 111 args = parser.parse_args(argv) 112 113 if os.path.exists(args.output_image_path): 114 os.remove(args.output_image_path) 115 if args.image_name == 'userdata': 116 _prepare_userdata(args.input_path) 117 if os.path.isdir(args.input_path): 118 _make_image(args) 119 _dep_files = [] 120 for _root, _, _files in os.walk(args.input_path): 121 for _file in _files: 122 _dep_files.append(os.path.join(_root, _file)) 123 if (os.path.exists(args.device_image_config_file)): 124 _dep_files.append(args.device_image_config_file) 125 build_utils.write_depfile(args.depfile, 126 args.output_image_path, 127 _dep_files, 128 add_pydeps=False) 129 return 0 130 131 132if __name__ == '__main__': 133 sys.exit(main(sys.argv[1:])) 134