Merged
bingo: 修复uboot镜像制作时偏移信息未自适应问题 #90
bingo: 修复uboot镜像制作时偏移信息未自适应问题 #90
Merged
xuhaijunAdd on Jul 1, 2025
Total: 5 files changed+120-30
@@ -9,4 +9,4 @@
9# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.9# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
10# See the Mulan PSL v2 for more details.10# See the Mulan PSL v2 for more details.
11 11 
12-__version__ = '0.5.261'12+__version__ = '0.5.262'
@@ -1,4 +1,4 @@
1-#!/usr/bin/env python1+#!/usr/bin/python3
2# coding: utf-82# coding: utf-8
3# Copyright (c) 2024 Huawei Technologies Co., Ltd.3# Copyright (c) 2024 Huawei Technologies Co., Ltd.
4# openUBMC is licensed under Mulan PSL v2.4# openUBMC is licensed under Mulan PSL v2.
@@ -21,3 +21,4 @@ chmod +x /usr/local/bin/get_img_parma_area.sh
21chmod +x /usr/local/bin/luajit2luac.sh21chmod +x /usr/local/bin/luajit2luac.sh
22chmod +x /usr/local/bin/cms_sign_hpm.sh22chmod +x /usr/local/bin/cms_sign_hpm.sh
23chmod +x /usr/local/bin/make_uboot_img.sh23chmod +x /usr/local/bin/make_uboot_img.sh
24+chmod +x /usr/local/bin/make_uboot_img.py
@@ -0,0 +1,116 @@
1+#!/usr/bin/python3
2+# coding: utf-8
3+# Copyright (c) 2025 Huawei Technologies Co., Ltd.
4+# openUBMC is licensed under Mulan PSL v2.
5+# You can use this software according to the terms and conditions of the Mulan PSL v2.
6+# You may obtain a copy of Mulan PSL v2 at:
7+# http://license.coscl.org.cn/MulanPSL2
8+# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9+# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10+# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11+# See the Mulan PSL v2 for more details.
12+ 
13+import os
14+import sys
15+import io
16+from bmcgo.utils.tools import Tools
17+ 
18+"""
19+功 能:打包uboot签名固件
20+版权信息:华为技术有限公司,版本所有(C) 2025-2025
21+ 
22+打包顺序:l0l1 emmc_sub_header rootca.der cms cms.crl rootca.der cms cms.crl uboot.bin
23+"""
24+ 
25+tools = Tools()
26+ 
27+ 
28+class Secion:
29+ def __init__(self, handler: io.BufferedReader):
30+ self.handler = handler
31+ self.offset = 0
32+ self.length = 0
33+ self.id = self._read_uint32(handler)
34+ if self.id == 0x33cc33cc:
35+ return
36+ self.offset = self._read_uint32(handler)
37+ self.length = self._read_uint32(handler)
38+ 
39+ def _read_uint32(self):
40+ # 从流中读取4个字节
41+ read_len = 4
42+ ctx = self.handler.read(read_len)
43+ val = 0
44+ # 按小端模式生成val
45+ for i in range(0, read_len):
46+ # 每个字节向左移(i * 8)位
47+ val |= (ctx[i] << (i * 8))
48+ return val
49+ 
50+ 
51+def make_img(uboot_offset, secsions: list[Secion], fw_header: int):
52+ img = "Hi1711_boot_4096.bin"
53+ tools.log.info(f"Start make {img}")
54+ cmd = f"dd if=/dev/zero of={img} bs=1M count=2"
55+ tools.run_command(cmd)
56+ cmd = f"dd if=l0l1.bin of={img} conv=notrunc"
57+ tools.run_command(cmd)
58+ cmd = f"dd if=u-boot_cms.bin of={img} seek={uboot_offset * 1024} bs=1 count={fw_header} conv=notrunc"
59+ tools.run_command(cmd)
60+ # 从emmc_sub_header中提取86个字符存储在输出文件的uboot_offset + 76字节位置
61+ cmd = f"dd if=emmc_sub_header of={img} seek={uboot_offset * 1024 + 76} bs=1 skip=76 count=84 conv=notrunc"
62+ tools.run_command(cmd)
63+ for sec in secsions:
64+ src = ""
65+ if sec.id in [0x10, 0x18]:
66+ src = "rootca.der"
67+ elif sec.id in [0x11, 0x19]:
68+ src = "uboot.bin.cms"
69+ elif sec.id in [0x12, 0x1a]:
70+ src = "cms.crl"
71+ elif sec.id == 0x04:
72+ cmd = f"dd if=uboot.bin of={img} seek={int(uboot_offset + sec.offset / 0x400)} bs=1k conv=notrunc"
73+ tools.run_command(cmd)
74+ continue
75+ else:
76+ raise Exception(f"Unsupported section {sec.id} get")
77+ offset = uboot_offset + int(sec.offset / 0x400)
78+ cmd = f"dd if={src} of={img} seek={offset} bs=1K conv=notrunc"
79+ tools.run_command(cmd)
80+ 
81+ 
82+if __name__ == "__main__":
83+ if len(sys.argv) != 2:
84+ raise Exception(f"Parameter error, usage: {sys.argv[0]} uboot_offset, for example {sys.argv[0]} 768")
85+ uboot_offset = int(sys.argv[1])
86+ 
87+ tools.log.info("Start load emmc_sub_header")
88+ file_handler = open("emmc_sub_header", "rb")
89+ # 定位到0x4c位置,该位置存储了section信息
90+ file_handler.seek(0x4c, os.SEEK_SET)
91+ sections: list[Secion] = []
92+ while True:
93+ sec = Secion(file_handler)
94+ if sec.id == 0x33cc33cc:
95+ break
96+ sections.append(sec)
97+ file_handler.close()
98+ 
99+ file_handler = open("u-boot_cms.bin", "rb")
100+ # 定位到0x4c位置,该位置存储了section信息
101+ file_handler.seek(0x4c, os.SEEK_SET)
102+ fw_sec = None
103+ while True:
104+ sec = Secion(file_handler)
105+ if sec.id == 0x33cc33cc:
106+ break
107+ if sec.id == 0x04:
108+ fw_sec = sec
109+ 
110+ header_len = file_handler.tell()
111+ file_handler.close()
112+ 
113+ if not fw_sec:
114+ raise Exception(f"Find section 0x04 failed")
115+ 
116+ make_img(uboot_offset, sections, header_len)
@@ -11,31 +11,4 @@
11# See the Mulan PSL v2 for more details.11# See the Mulan PSL v2 for more details.
12 12 
13set -e13set -e
14- 14+make_uboot_img.py $1
15-if [ -z "$1" ]; then
16- echo "ERROR: Size parameter does not exist."
17- exit 1
18-fi
19- 
20-if [[ ! "$1" =~ ^[1-9][0-9]*$ ]]; then
21- echo "ERROR: Size parameter must be positive integer."
22- exit 1
23-fi
24- 
25-size=$1
26-uboot_bin=Hi1711_boot_4096.bin
27- 
28-# 从$size开始存储顺序: |emmc_sub_header | rootca.der | cms | cms.crl | rootca.der | cms | cms.crl | uboot.bin
29- 
30-# image的整个bin最大为2M。不到2M的部分,使用零填充。
31-dd if=/dev/zero of=./$uboot_bin bs=1M count=2
32-dd if=./l0l1.bin of=./$uboot_bin seek=0 bs=1K conv=notrunc
33-dd if=./u-boot_cms.bin of=./$uboot_bin seek=$size bs=1K conv=notrunc
34-# 只截取签名和证书部分
35-dd if=./emmc_sub_header of=./$uboot_bin skip=76 seek=$(($size * 1024 + 76)) bs=1 count=72 conv=notrunc
36-dd if=./rootca.der of=./$uboot_bin seek=$(($size + 2)) bs=1K conv=notrunc
37-dd if=./uboot.bin.cms of=./$uboot_bin seek=$(($size + 4)) bs=1K conv=notrunc
38-dd if=./cms.crl of=./$uboot_bin seek=$(($size + 10)) bs=1K conv=notrunc
39-dd if=./rootca.der of=./$uboot_bin seek=$(($size + 18)) bs=1K conv=notrunc
40-dd if=./uboot.bin.cms of=./$uboot_bin seek=$(($size + 20)) bs=1K conv=notrunc
41-dd if=./cms.crl of=./$uboot_bin seek=$(($size + 26)) bs=1K conv=notrunc