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 json 17import os 18import sys 19import subprocess 20import shutil 21import tempfile 22 23 24sys.path.append( 25 os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname( 26 os.path.abspath(__file__)))))) 27from build_scripts.build import find_top 28 29 30def run_cmd(cmd: str): 31 res = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, 32 stderr=subprocess.PIPE) 33 sout, serr = res.communicate() 34 35 return res.pid, res.returncode, sout, serr 36 37 38def build_rootdir(src_dir: str) -> str: 39 tmp_dir = tempfile.mkdtemp(prefix="tmp") 40 index = src_dir.rfind('/') 41 root_dir = "%sroot" % src_dir[:index + 1] 42 43 if root_dir: 44 shutil.rmtree(tmp_dir) 45 shutil.copytree(root_dir, tmp_dir, symlinks=True) 46 tmp_dir_system = os.path.join(tmp_dir, "system") 47 shutil.rmtree(tmp_dir_system, ignore_errors=True) 48 shutil.copytree(src_dir, tmp_dir_system, symlinks=True) 49 return tmp_dir 50 51 52def load_config(config_file: str) -> list: 53 mk_configs = [] 54 with open(config_file, "r") as file: 55 for line in file: 56 line = line.strip() 57 if not line or line.startswith("#"): 58 continue 59 mk_configs.append(line) 60 mk_configs = " ".join(mk_configs) 61 if "ext4" in mk_configs: 62 fs_type = "ext4" 63 mkfs_tools = "mkextimage.py" 64 elif "f2fs" in mk_configs: 65 mkfs_tools = "mkf2fsimage.py" 66 fs_type = "f2fs" 67 elif "cpio" in mk_configs: 68 mkfs_tools = "mkcpioimage.py" 69 fs_type = "cpio" 70 else: 71 print("not support filesystem type!!") 72 sys.exit(1) 73 return mkfs_tools, mk_configs, fs_type 74 75 76def verify_ret(res: list): 77 if res[1]: 78 print(" ".join(["pid ", str(res[0]), " ret ", str(res[1]), "\n", 79 res[2].decode(), res[3].decode()])) 80 print("MkImages failed errno: %s" % str(res[1])) 81 sys.exit(2) 82 83 84def sparse_img2simg(is_sparse: str, device: str): 85 # we don't support sparse in mktools. 86 if "sparse" in is_sparse: 87 tmp_device = "%s.tmp" % device 88 run_cmd(" ".join(["img2simg ", device, " ", tmp_device])) 89 os.rename(tmp_device, device) 90 91 92def mk_system_img(mkfs_tools: str, mk_configs: str, device: str, src_dir: str, is_sparse: str): 93 src_dir = build_rootdir(src_dir) 94 mk_configs = " ".join([src_dir, device, mk_configs]) 95 res = run_cmd(" ".join([mkfs_tools, mk_configs])) 96 verify_ret(res) 97 sparse_img2simg(is_sparse, device) 98 if os.path.isdir(src_dir): 99 shutil.rmtree(src_dir) 100 101 102def mk_ramdisk_img(mkfs_tools: str, mk_configs: str, device: str, src_dir: str, is_sparse: str): 103 # get ramdisk sieze frome ramdisk_image_conf.txt 104 ramdisk_size = mk_configs.split(" ")[1] 105 mk_configs = \ 106 " ".join([src_dir, device, ramdisk_size]) 107 res = run_cmd(" ".join([mkfs_tools, mk_configs])) 108 verify_ret(res) 109 110 111def mk_other_img(mkfs_tools: str, mk_configs: str, device: str, src_dir: str, is_sparse: str): 112 mk_configs = " ".join([src_dir, device, mk_configs]) 113 res = run_cmd(" ".join([mkfs_tools, mk_configs])) 114 verify_ret(res) 115 sparse_img2simg(is_sparse, device) 116 117 118def mk_images(args): 119 if len(args) != 4: 120 print("mk_images need 4 args!!!") 121 sys.exit(1) 122 root_path = find_top() 123 config_json = os.path.join(root_path, "out/ohos_config.json") 124 config = {} 125 with open(config_json, 'rb') as f: 126 config = json.load(f) 127 src_dir = args[0] 128 config_file = args[1] 129 device = args[2] 130 is_sparse = args[3] 131 mkfs_tools, mk_configs, _ = load_config(config_file) 132 image_name = device.split("/")[-1] 133 if image_name == "system.img": 134 mk_system_img(mkfs_tools, mk_configs, device, src_dir, is_sparse) 135 elif image_name == "ramdisk.img": 136 mk_ramdisk_img(mkfs_tools, mk_configs, device, src_dir, is_sparse) 137 elif image_name == "updater_ramdisk.img": 138 if config.get('component_type', '') == 'system_component': 139 return 140 mk_ramdisk_img(mkfs_tools, mk_configs, device, src_dir, is_sparse) 141 else: 142 mk_other_img(mkfs_tools, mk_configs, device, src_dir, is_sparse) 143 144 145if __name__ == '__main__': 146 mk_images(sys.argv[1:]) 147