From 6ad3ab93a7254e9ef5bfb022099234ae298faf44 Mon Sep 17 00:00:00 2001 From: Ching L Date: Thu, 26 Feb 2026 13:57:52 +0800 Subject: [PATCH] feat(cp02): add build GUI, flash guide, and comment removal tool Add build_gui.html for visual command generation, flash_guide.md for flashing reference, and remove_chinese_comments.py for cleaning source files. Update extract_firmware.sh to use output directory instead of prefix, and update README with all new tools. Co-Authored-By: Claude Opus 4.6 --- CP02/README.md | 38 ++- CP02/build_gui.html | 546 ++++++++++++++++++++++++++++++++ CP02/extract_firmware.sh | 34 +- CP02/flash_guide.md | 38 +++ CP02/remove_chinese_comments.py | 152 +++++++++ 5 files changed, 784 insertions(+), 24 deletions(-) create mode 100644 CP02/build_gui.html create mode 100644 CP02/flash_guide.md create mode 100755 CP02/remove_chinese_comments.py diff --git a/CP02/README.md b/CP02/README.md index 8345fc6..addbda1 100644 --- a/CP02/README.md +++ b/CP02/README.md @@ -21,12 +21,13 @@ ### extract_firmware.sh - **功能**: 从合并的 bin 文件中提取 firmware、IUM 和 metadata -- **用法**: `./extract_firmware.sh [output_prefix]` +- **用法**: `./extract_firmware.sh [output_dir]` - **描述**: - 从固定偏移位置提取 firmware (0x04000) - 提取 IUM 数据 (0x1dc00, 256 bytes) - 解析并生成 metadata.json (blocks 和 last_block_size) - - 如未指定前缀,自动使用原文件名 + - 如未指定输出目录,默认输出到当前目录 + - 自动创建不存在的输出目录 ### get_last_bytes.sh - **功能**: 获取 IUM 文件的最后两个字节并以十六进制格式返回 @@ -99,6 +100,31 @@ - 自动处理重名文件(添加 _1, _2 等后缀) - 显示复制进度和统计信息 +### remove_chinese_comments.py +- **功能**: 从源代码文件中移除中文注释 +- **用法**: `python3 remove_chinese_comments.py [--check] [file2 ...]` +- **描述**: + - 支持 C/C++(`//` 注释)和 Python(`#` 注释)文件 + - 整行中文注释直接删除,行尾中文注释只移除注释部分 + - `--check` 模式仅显示中文注释,不做修改 + - 交互式确认后才执行删除 + +### build_gui.html +- **功能**: IonBridge 构建工具的可视化命令生成器 +- **用法**: 在浏览器中打开 `build_gui.html` +- **描述**: + - 支持选择 variant(CP02、CP02S、3536、GPU Demo、Bird) + - 可配置 build 选项(production、coredump)和 flash 选项(littlefs、protected_data、nvs 等) + - 一键生成 Build、Flash、Monitor、Clean 等命令并复制 + - 自动保存配置和命令历史到 localStorage + +### flash_guide.md +- **功能**: IonBridge 烧录操作参考文档 +- **描述**: + - 非 Secure 设备(开发板)的 build/flash/monitor 命令示例 + - Secure 设备(量产板)的签名烧录流程 + - sign_key 和 psn 参数说明 + ### merge_encrypted_firmware.py - **功能**: 合成已加密的固件和 IUM 为最终 programmer 固件 - **用法**: `python3 merge_encrypted_firmware.py -o ` @@ -123,8 +149,8 @@ ./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 # 使用原文件名作为前缀 +./extract_firmware.sh 40_ufcs2.bin /tmp/output # 输出到指定目录 +./extract_firmware.sh merged.bin # 输出到当前目录 # 获取 IUM 文件的最后两个字节 ./get_last_bytes.sh ium.bin # 使用默认文件 @@ -151,4 +177,8 @@ python3 merge_encrypted_firmware.py base.bin encrypted.bin ium.bin -o programmer # 复制 .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 # 交互式移除 ``` \ No newline at end of file diff --git a/CP02/build_gui.html b/CP02/build_gui.html new file mode 100644 index 0000000..6341564 --- /dev/null +++ b/CP02/build_gui.html @@ -0,0 +1,546 @@ + + + + + +IonBridge Build Tool + + + + +
+

IonBridge Build Tool

+
+ +
+ + +
+
+
+
Select an action to generate commands
+
+ +
+
+ + + + diff --git a/CP02/extract_firmware.sh b/CP02/extract_firmware.sh index e2cb02a..973c025 100755 --- a/CP02/extract_firmware.sh +++ b/CP02/extract_firmware.sh @@ -1,5 +1,5 @@ #!/bin/bash -# 用法: ./extract_firmware.sh [output_prefix] +# 用法: ./extract_firmware.sh [output_dir] # 从合并的 bin 文件中提取 head file (firmware), tail file (IUM), 以及生成 metadata.json # # 文件布局: @@ -8,26 +8,17 @@ # 0x1dd00: metadata (blocks: uint16, last_block_size: uint8) INPUT="$1" -# 如果没有传 PREFIX,使用原文件名(去掉扩展名) -if [ -n "$2" ]; then - PREFIX="$2" -else - # 获取文件名(不含路径),然后去掉扩展名 - BASENAME=$(basename "$INPUT") - PREFIX="${BASENAME%.*}" -fi +OUTPUT_DIR="${2:-.}" if [ -z "$INPUT" ]; then - echo "用法: $0 [output_prefix]" - echo "示例: $0 40_ufcs2.bin firmware" - echo " $0 40_ufcs2.bin # 使用原文件名作为前缀" + echo "用法: $0 [output_dir]" + echo "示例: $0 40_ufcs2.bin /tmp/output" + echo " $0 40_ufcs2.bin # 输出到当前目录" echo "" echo "输出文件:" - echo " {prefix}.firmware - firmware 部分" - echo " {prefix}.ium - IUM 部分 (256 bytes)" - echo " {prefix}_metadata.json - 元数据" - echo "" - echo "注: 如果不指定 output_prefix,将使用原文件名(去掉扩展名)" + echo " firmware.bin - firmware 部分" + echo " ium.bin - IUM 部分 (256 bytes)" + echo " metadata.json - 元数据" exit 1 fi @@ -36,6 +27,9 @@ if [ ! -f "$INPUT" ]; then exit 1 fi +# 创建输出目录(如果不存在) +mkdir -p "$OUTPUT_DIR" + # 常量定义 FIRMWARE_OFFSET=$((0x4000)) # 16384 IUM_OFFSET=$((0x1dc00)) # 121856 @@ -79,7 +73,7 @@ echo " 计算出 firmware 大小: $FIRMWARE_SIZE bytes" echo "" # 提取 head file (firmware: 从 0x4000 开始) -HEAD_FILE="firmware.bin" +HEAD_FILE="$OUTPUT_DIR/firmware.bin" echo "提取 head file (firmware)..." dd if="$INPUT" of="$HEAD_FILE" bs=1 skip=$FIRMWARE_OFFSET count=$FIRMWARE_SIZE status=none @@ -92,7 +86,7 @@ else fi # 提取 tail file (IUM: 从 0x1dc00 开始, 256 bytes) -TAIL_FILE="ium.bin" +TAIL_FILE="$OUTPUT_DIR/ium.bin" echo "提取 tail file (IUM)..." dd if="$INPUT" of="$TAIL_FILE" bs=1 skip=$IUM_OFFSET count=$IUM_SIZE status=none @@ -105,7 +99,7 @@ else fi # 生成 metadata.json -METADATA_FILE="metadata.json" +METADATA_FILE="$OUTPUT_DIR/metadata.json" echo "生成 metadata.json..." cat > "$METADATA_FILE" << EOF diff --git a/CP02/flash_guide.md b/CP02/flash_guide.md new file mode 100644 index 0000000..d45f204 --- /dev/null +++ b/CP02/flash_guide.md @@ -0,0 +1,38 @@ +# IonBridge 烧录指南 + +所有命令在 IonBridge 项目根目录下执行(`cd /Users/ching/develop/IonBridge`)。 + +## 非 Secure 设备(开发板) + +```bash +# Build + Flash firmware + littlefs +make build variant=cp02 +make flash littlefs=1 PORT=/dev/tty.usbmodem2101 + +# 只烧 firmware +make build variant=cp02 +make flash PORT=/dev/tty.usbmodem2101 + +# 查看日志 +make monitor PORT=/dev/tty.usbmodem2101 +``` + +## Secure 设备(量产板,Secure Boot + Flash Encryption) + +```bash +# Build + Flash firmware + littlefs +make build variant=cp02 production=1 +make flash littlefs=1 sign_key=/Users/ching/Downloads/1331040606001272_sign_key.pem PORT=/dev/tty.usbmodem2101 + +# 只烧 littlefs(不更新 firmware,不需要重新 build) +make flash_littlefs_partition secure=1 PORT=/dev/tty.usbmodem2101 + +# 查看日志 +make monitor PORT=/dev/tty.usbmodem2101 +``` + +## 备注 + +- `sign_key` 可用 `psn` 代替:`psn=1331040606001272`(自动查找 `sign_keys/` 下的 key) +- Secure 设备用 `production=1` 构建后,`secure=1` 会自动检测 +- `PORT` 根据实际设备端口修改 diff --git a/CP02/remove_chinese_comments.py b/CP02/remove_chinese_comments.py new file mode 100755 index 0000000..e89813b --- /dev/null +++ b/CP02/remove_chinese_comments.py @@ -0,0 +1,152 @@ +#!/usr/bin/env python3 +"""Remove Chinese comments (// and # style) from source files.""" + +from __future__ import annotations + +import re +import sys +from pathlib import Path + +# File extensions and their comment patterns +COMMENT_PATTERNS = { + # C/C++ style + '.c': r'//.*', + '.cpp': r'//.*', + '.h': r'//.*', + '.hpp': r'//.*', + # Python style + '.py': r'#.*', +} + + +def get_comment_pattern(filepath: str) -> str: + """Get comment pattern based on file extension.""" + ext = Path(filepath).suffix.lower() + return COMMENT_PATTERNS.get(ext, r'//.*') + + +def get_comment_prefix(filepath: str) -> str: + """Get comment prefix based on file extension.""" + ext = Path(filepath).suffix.lower() + if ext == '.py': + return '#' + return '//' + + +def contains_chinese(text: str) -> bool: + """Check if text contains Chinese characters.""" + return bool(re.search(r'[\u4e00-\u9fff]', text)) + + +def find_chinese_comments(filepath: str): + """Find all lines with Chinese comments. + + Returns list of (line_number, line_content) tuples. + """ + results = [] + path = Path(filepath) + + if not path.exists(): + print(f"Error: File not found: {filepath}") + sys.exit(1) + + pattern = get_comment_pattern(filepath) + + with open(path, 'r', encoding='utf-8') as f: + for line_num, line in enumerate(f, 1): + match = re.search(pattern, line) + if match and contains_chinese(match.group()): + results.append((line_num, line.rstrip())) + + return results + + +def remove_chinese_comments(filepath: str) -> str: + """Remove Chinese comments from file content. + + Returns the modified content. + """ + path = Path(filepath) + pattern = get_comment_pattern(filepath) + prefix = get_comment_prefix(filepath) + + with open(path, 'r', encoding='utf-8') as f: + lines = f.readlines() + + new_lines = [] + for line in lines: + match = re.search(pattern, line) + if match and contains_chinese(match.group()): + stripped = line.lstrip() + if stripped.startswith(prefix): + # Entire line is a comment - skip it + continue + else: + # Comment is at end of code - remove just the comment + comment_start = match.start() + new_line = line[:comment_start].rstrip() + '\n' + new_lines.append(new_line) + else: + new_lines.append(line) + + return ''.join(new_lines) + + +def main(): + if len(sys.argv) < 2: + print("Usage: python remove_chinese_comments.py [file2 ...]") + print(" python remove_chinese_comments.py --check [file2 ...]") + print("\nOptions:") + print(" --check Only show Chinese comments without removing") + sys.exit(1) + + check_only = False + files = sys.argv[1:] + + if files[0] == '--check': + check_only = True + files = files[1:] + + if not files: + print("Error: No files specified") + sys.exit(1) + + total_found = 0 + + for filepath in files: + comments = find_chinese_comments(filepath) + + if not comments: + print(f"\n{filepath}: No Chinese comments found") + continue + + total_found += len(comments) + print(f"\n{'='*60}") + print(f"File: {filepath}") + print(f"Found {len(comments)} Chinese comment(s):") + print('-'*60) + + for line_num, line in comments: + print(f" L{line_num}: {line}") + + if check_only: + continue + + # Ask for confirmation + print('-'*60) + response = input(f"Remove these {len(comments)} comment(s)? [y/N]: ").strip().lower() + + if response == 'y': + new_content = remove_chinese_comments(filepath) + with open(filepath, 'w', encoding='utf-8') as f: + f.write(new_content) + print(f"✓ Removed {len(comments)} Chinese comment(s) from {filepath}") + else: + print(f"✗ Skipped {filepath}") + + print(f"\n{'='*60}") + print(f"Total: {total_found} Chinese comment(s) found") + + +if __name__ == '__main__': + main()