work_script/CP02/README.md
Ching L 8f928a93e8 feat(cp02): add firmware processing and signing utilities
- Add get_last_bytes.sh to extract final 2 bytes from IUM files
  - Add read_bytes.sh for reading specific byte offsets from binaries
  - Add pack_resources.sh to package and upload CP02S resources to S3
  - Add signer_new.py for firmware and bootloader signing with custom SHA1
  - Update extract_firmware.sh to use fixed output filenames
  - Update README.md with comprehensive documentation for new tools
2026-01-04 15:08:17 +08:00

101 lines
4.2 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 元数据
## 使用示例
```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
```