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. 15import configparser 16import json 17import os 18import shutil 19import sys 20import argparse 21import subprocess 22import filecmp 23from judge_updater_image import judge_updater_img_available 24 25 26def args_parse(args): 27 parser = argparse.ArgumentParser(description='mkcpioimage.py') 28 29 parser.add_argument("src_dir", help="The source file for sload.") 30 parser.add_argument("device", help="The device for mkfs.") 31 parser.add_argument("conf_size", help="The deivce config image size.") 32 33 args = parser.parse_known_args(args)[0] 34 return args 35 36 37def run_cmd(cmd: str, dir_list=None): 38 res = subprocess.Popen(cmd, stdout=subprocess.PIPE, 39 stdin=subprocess.PIPE, 40 stderr=subprocess.PIPE) 41 if dir_list is not None: 42 for each_path in dir_list: 43 print("mkcpio image, cpio stdin: ", each_path) 44 res.stdin.write(("%s\n" % each_path).encode('utf-8')) 45 sout, serr = res.communicate() 46 res.wait() 47 return res.pid, res.returncode, sout, serr 48 49 50def get_dir_list(input_path: str, dir_list: list): 51 if os.path.isdir(input_path) and not os.path.islink(input_path): 52 dir_list.append(input_path) 53 tem_list = os.listdir(input_path) 54 for each in tem_list: 55 each_path = os.path.join(input_path, each) 56 get_dir_list(each_path, dir_list) 57 else: 58 dir_list.append(input_path) 59 60 61def build_run_cpio(args): 62 work_dir = os.getcwd() 63 os.chdir(args.src_dir) 64 65 conf_size = int(args.conf_size) 66 if args.device == "ramdisk.img": 67 output_path = os.path.join("%s/../images" % os.getcwd(), args.device) 68 elif args.device == "updater_ramdisk.img": 69 if not judge_updater_img_available(os.getcwd()): 70 sys.exit(1) 71 output_path = os.path.join("%s/../images" % os.getcwd(), "updater.img") 72 else: 73 output_path = os.path.join(work_dir, args.device) 74 ramdisk_cmd = ['cpio', '-o', '-H', 'newc', '-O', output_path] 75 dir_list = [] 76 get_dir_list("./", dir_list) 77 res = run_cmd(ramdisk_cmd, dir_list) 78 os.chdir(work_dir) 79 zip_ramdisk(args) 80 if conf_size < os.path.getsize(output_path): 81 print("Image size is larger than the conf image size. " 82 "conf_size: %d, image_size: %d" % 83 (conf_size, os.path.getsize(output_path))) 84 sys.exit(1) 85 if res[1] != 0: 86 print("error run cpio ramdisk errno: %s" % str(res)) 87 print(" ".join(["pid ", str(res[0]), " ret ", str(res[1]), "\n", 88 res[2].decode(), res[3].decode()])) 89 sys.exit(1) 90 return 91 92 93def zip_ramdisk(args): 94 src_dir = args.src_dir 95 src_index = src_dir.rfind('/') 96 root_dir = src_dir[:src_index] 97 98 if "ramdisk.img" == args.device: 99 ramdisk_img = os.path.join(root_dir, "images", "ramdisk.img") 100 ramdisk_gz = os.path.join(root_dir, "images", "ramdisk.img.gz") 101 elif "updater_ramdisk.img" == args.device: 102 ramdisk_img = os.path.join(root_dir, "images", "updater.img") 103 ramdisk_gz = os.path.join(root_dir, "images", "updater.img.gz") 104 if os.path.exists(ramdisk_gz): 105 os.remove(ramdisk_gz) 106 res_gzip = run_cmd(["gzip", ramdisk_img]) 107 res_mv_gz = run_cmd(["mv", ramdisk_gz, ramdisk_img]) 108 if res_gzip[1] != 0 or res_mv_gz[1] != 0: 109 print("Failed to compress ramdisk image. %s, %s" % 110 (str(res_gzip), str(res_mv_gz))) 111 sys.exit(1) 112 113 114def build_run_chmod(args): 115 src_dir = args.src_dir 116 src_index = src_dir.rfind('/') 117 root_dir = src_dir[:src_index] 118 119 if "updater_ramdisk.img" in args.device: 120 chmod_cmd = ['chmod', '664', os.path.join(root_dir, "images", "updater.img")] 121 else: 122 chmod_cmd = ['chmod', '664', os.path.join(root_dir, "images", "ramdisk.img")] 123 res = run_cmd(chmod_cmd) 124 if res[1] != 0: 125 print("error run chmod errno: %s" % str(res)) 126 print(" ".join(["pid ", str(res[0]), " ret ", str(res[1]), "\n", 127 res[2].decode(), res[3].decode()])) 128 sys.exit(3) 129 return res[1] 130 131 132def main(args): 133 args = args_parse(args) 134 print("Make cpio image!") 135 build_run_cpio(args) 136 build_run_chmod(args) 137 138 139if __name__ == '__main__': 140 main(sys.argv[1:]) 141