work_script/CP02/README.md
Ching L 8651878be8 feat(cp02): add PD decode tools and enhance build GUI
- Add charging_viewer.py for reading PowerZ .sqlite/.db charging data
  - Add powerz_pd_decode.py for decoding USB PD protocol messages
  - Add sdkconfig file path display to build_gui.html
  - Add signing reminder to split_and_merge.sh
2026-04-03 10:50:23 +08:00

216 lines
9.4 KiB
Markdown
Raw Permalink 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_dir]`
- **描述**:
- 从固定偏移位置提取 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 标记行
### copy_o_files.sh
- **功能**: 递归复制目录中的所有 .o 文件到指定目标目录
- **用法**: `./copy_o_files.sh <源目录> <目标目录>`
- **描述**:
- 递归搜索源目录中的所有 .o 文件
- 自动处理重名文件(添加 _1, _2 等后缀)
- 显示复制进度和统计信息
### remove_chinese_comments.py
- **功能**: 从源代码文件中移除中文注释
- **用法**: `python3 remove_chinese_comments.py [--check] <file> [file2 ...]`
- **描述**:
- 支持 C/C++`//` 注释)和 Python`#` 注释)文件
- 整行中文注释直接删除,行尾中文注释只移除注释部分
- `--check` 模式仅显示中文注释,不做修改
- 交互式确认后才执行删除
### charging_viewer.py
- **功能**: 充电数据查看器,读取 PowerZ 等工具导出的 .sqlite / .db 充电测试数据
- **用法**: `python3 charging_viewer.py [--no-page] [目录或文件路径 ...]`
- **描述**:
- 自动检测 pd_chart 和 table_1 表格式
- 显示功率、电压、电流、温度、充电量等统计信息
- 解析 PD 消息头、PDOFixed/Battery/Variable/PPS、RDO
- 从 VBUS/CC 波形分析 PD 协商事件(电压跳变检测)
- 支持翻页输出(自动使用 less
- 无参数时扫描当前目录下所有 .sqlite/.db 文件
### powerz_pd_decode.py
- **功能**: 解码 PowerZ 抓取的 USB PD 协议消息
- **用法**: `python3 powerz_pd_decode.py <sqlite文件> [--window START END]`
- **描述**:
- 从 PowerZ sqlite 文件的 pd_table 读取原始 PD 数据
- 解码 Source_CapabilitiesFixed/PPS/AVS PDO
- 解码 Request/EPR_RequestFixed RDO 和 PPS RDO
- 关联 Request 与对应的 Source_Cap 显示选中的 PDO
- `--window` 参数可查看指定时间窗口内的所有消息
### build_gui.html
- **功能**: IonBridge 构建工具的可视化命令生成器
- **用法**: 在浏览器中打开 `build_gui.html`
- **描述**:
- 支持选择 variantCP02、CP02S、3536、GPU Demo、Bird
- 可配置 build 选项production、coredump和 flash 选项littlefs、protected_data、nvs 等)
- 一键生成 Build、Flash、Monitor、Clean 等命令并复制
- 自动保存配置和命令历史到 localStorage
- 显示当前 variant 和模式对应的 sdkconfig 文件路径
### flash_guide.md
- **功能**: IonBridge 烧录操作参考文档
- **描述**:
- 非 Secure 设备(开发板)的 build/flash/monitor 命令示例
- Secure 设备(量产板)的签名烧录流程
- sign_key 和 psn 参数说明
### 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 /tmp/output # 输出到指定目录
./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
# 查看充电测试数据
python3 charging_viewer.py . # 扫描当前目录
python3 charging_viewer.py data.sqlite # 指定文件
python3 charging_viewer.py --no-page /path/to/captures/ # 不翻页输出
# 解码 PowerZ PD 消息
python3 powerz_pd_decode.py capture.sqlite # 显示摘要
python3 powerz_pd_decode.py capture.sqlite --window 1.0 5.0 # 查看时间窗口
# 复制 .o 文件
./copy_o_files.sh /path/to/build /path/to/output
# 移除中文注释
python3 remove_chinese_comments.py --check main.c # 仅检查
python3 remove_chinese_comments.py main.c utils.py # 交互式移除
```