work_script/CP02/README.md
Ching L 40dcc0db8c docs(cp02): add documentation for new firmware utilities
- Document bin_to_txt_converter.py for binary to text conversion
  - Document txt_to_bin_converter.py for text to binary conversion
  - Document merge_encrypted_firmware.py for encrypted firmware merging
  - Add usage examples for all three new tools
2026-01-07 11:28:08 +08:00

143 lines
6.1 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# CP02 固件处理工具
## 文件说明
### split_and_merge.sh
- **功能**: 切分固件文件并与 updater.bin 合并,用于制作 40W 固件
- **用法**: `./split_and_merge.sh <待切分文件> <updater.bin路径> <输出文件名>`
- **描述**:
- 将输入固件文件切分为前面部分和最后256字节
- 调用合并脚本生成最终固件
- 自动删除最后1字节并检查文件大小限制
### merge_2323_firmware.sh
- **功能**: 合并 PA768 updater、2323固件和IUM数据为单一固件文件
- **用法**: `./merge_2323_firmware.sh -u <updater> -f <firmware> -i <ium> -o <output>`
- **描述**:
- 创建基础0xFF填充文件
- 在指定偏移位置写入各组件
- 生成固件元数据
- 输出完整内存布局信息
### extract_firmware.sh
- **功能**: 从合并的 bin 文件中提取 firmware、IUM 和 metadata
- **用法**: `./extract_firmware.sh <merged_bin> [output_prefix]`
- **描述**:
- 从固定偏移位置提取 firmware (0x04000)
- 提取 IUM 数据 (0x1dc00, 256 bytes)
- 解析并生成 metadata.json (blocks 和 last_block_size)
- 如未指定前缀,自动使用原文件名
### get_last_bytes.sh
- **功能**: 获取 IUM 文件的最后两个字节并以十六进制格式返回
- **用法**: `./get_last_bytes.sh [ium_file_path]`
- **描述**:
- 读取 IUM 文件的最后 2 个字节
- 反转字节顺序(交换第一和第二个字节)
- 以十六进制格式输出结果
- 如未指定文件路径,默认使用 ium.bin
### read_bytes.sh
- **功能**: 从二进制文件的特定偏移量读取 2 个字节
- **用法**: `./read_bytes.sh <bin_file>`
- **描述**:
- 从偏移量 0x1dcfe 读取 2 个字节
- 反转字节顺序(小端序转换)
- 返回十六进制格式的结果
- 包含文件大小和偏移量验证
### pack_resources.sh
- **功能**: 打包 CP02S 资源目录中的 .bin 文件
- **用法**: `./pack_resources.sh`(无参数)
- **描述**:
- 从指定目录收集所有 .bin 文件
- 创建包含 resources/ 作为顶级目录的 tar.gz 压缩包
- 自动上传到 S3 存储(需要配置 AWS CLI
- 源目录:`/Users/ching/develop/IonBridge/files/CP02S/littlefs/resources`
### signer_new.py
- **功能**: 用于签名 CP02 固件和引导加载程序的 Python 工具
- **用法**: `python3 signer_new.py --output_dir <output_directory> [options]`
- **选项**:
- `--firmware <path>` - 未签名的用户应用程序固件路径
- `--bootloader <path>` - 未签名的引导加载程序路径
- `--output_dir <path>` - 保存签名文件的目录(必需)
- `--enable_swd` - 启用 SWD 功能
- `--boot_directly` - 启用直接引导到用户应用程序
- **描述**:
- 实现自定义 SHA1 签名算法(基于逆向工程)
- 为固件和引导加载程序生成签名的二进制文件
- 创建包含版本号、校验和和版权信息的元数据
- 支持多种芯片 ID 的引导加载程序变体
- 输出签名的二进制文件和相关 JSON 元数据
### bin_to_txt_converter.py
- **功能**: 将二进制文件转换为文本格式
- **用法**: `python3 bin_to_txt_converter.py <input.bin> [output.txt]`
- **描述**:
- 将二进制文件(如 IUM转换为文本格式
- 按 4 字节块处理数据,转换为十六进制字符串
- 支持小端序字节序转换
- 输出文件包含 start/end 标记
- 未指定输出文件时自动生成文件名
### txt_to_bin_converter.py
- **功能**: 将文本格式转换回二进制文件
- **用法**: `python3 txt_to_bin_converter.py <input.txt> <output.bin>`
- **描述**:
- 将文本格式(如 ium11.txt转换回二进制文件
- 处理 8 字符十六进制字符串为 4 字节整数
- 写入前 63 个完整的 4 字节值
- 第 64 个值仅写入第一个字节
- 跳过 start/end 标记行
### merge_encrypted_firmware.py
- **功能**: 合成已加密的固件和 IUM 为最终 programmer 固件
- **用法**: `python3 merge_encrypted_firmware.py <base.bin> <encrypted.bin> <ium.bin> -o <output.bin>`
- **描述**:
- 合并 Base firmware (16KB)、加密固件 (最大 119KB) 和 IUM storage (256 bytes)
- 固件布局:
- 0x0000 - 0x4000: Base firmware
- 0x4000 - 0x1DC00: 加密用户固件
- 0x1DC00 - 0x1DD00: IUM storage
- 0x1DD00 - 0x1DD10: Metadata (16 bytes)
- 自动生成 metadata包含 blocks 和 last_block_size
- 显示完整的内存布局和校验信息
- IUM storage 包含 CRC8 和 CRC16 校验
## 使用示例
```bash
# 切分并合并固件
./split_and_merge.sh /Users/ching/Library/Containers/com.tencent.xinWeChat/Data/Documents/xwechat_files/looching_5217/msg/attach/61f8147fd472041d44f609e3618e827c/2025-12/Rec/ff6a18f4ab8db2a9/F/3/SW2303P_B_V1.0_00_D1F6_UFCS.bin ~/Downloads/固件/PA503_Updater.bin 40_ufcs2.bin
# 直接合并固件组件
./merge_2323_firmware.sh -u PA768_Updater.bin -f 2323_firmware.bin -i 2323_ium.bin -o merged.bin
# 提取固件组件
./extract_firmware.sh 40_ufcs2.bin firmware # 指定输出前缀
./extract_firmware.sh merged.bin # 使用原文件名作为前缀
# 获取 IUM 文件的最后两个字节
./get_last_bytes.sh ium.bin # 使用默认文件
./get_last_bytes.sh custom_ium.bin # 指定文件路径
# 从二进制文件读取特定偏移量的字节
./read_bytes.sh 40_ufcs2.bin
# 打包资源文件
./pack_resources.sh # 打包并上传到 S3
# 签名固件
python3 signer_new.py --firmware firmware.bin --output_dir ./output
python3 signer_new.py --bootloader bootloader.bin --output_dir ./output --enable_swd
python3 signer_new.py --firmware firmware.bin --bootloader bootloader.bin --output_dir ./output --boot_directly
# 二进制与文本格式转换
python3 bin_to_txt_converter.py ium.bin # 自动生成输出文件名
python3 bin_to_txt_converter.py ium.bin ium_converted.txt # 指定输出文件
python3 txt_to_bin_converter.py ium11.txt ium11.bin # 文本转二进制
# 合成加密固件
python3 merge_encrypted_firmware.py base.bin encrypted.bin ium.bin -o programmer.bin
```