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