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