work_script/CP02/split_and_merge.sh
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

86 lines
2.6 KiB
Bash
Executable File
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.

#!/usr/bin/env bash
# 用法: ./split256_and_merge.sh <待切分文件> <updater.bin路径> <输出文件名>
# 示例: ./split256_and_merge.sh /Users/ching/Downloads/4_SW2303P_B_V1.0_00_E5C3.bin ~/Downloads/PA503_Updater.bin 40_4.bin
input="$1"
updater="$2"
output="$3"
if [ -z "$input" ] || [ -z "$updater" ] || [ -z "$output" ]; then
echo "用法: $0 <待切分文件> <updater.bin路径> <输出文件名>"
exit 1
fi
# 检查文件是否存在
if [ ! -f "$input" ]; then
echo "文件不存在: $input"
exit 1
fi
if [ ! -f "$updater" ]; then
echo "文件不存在: $updater"
exit 1
fi
# 获取文件大小(兼容 Linux / macOS
if stat --version >/dev/null 2>&1; then
size=$(stat -c%s "$input") # Linux
else
size=$(stat -f%z "$input") # macOS
fi
if [ "$size" -lt 256 ]; then
echo "文件太小 (<256 字节),无法切分"
exit 1
fi
head_size=$((size - 256))
# 输出文件名
head_file="${input}.head"
tail_file="${input}.tail"
# 切分
dd if="$input" of="$head_file" bs=1 count=$head_size status=none
dd if="$input" of="$tail_file" bs=1 skip=$head_size status=none
echo "切分完成:"
echo " 前面部分: $head_file ($head_size 字节)"
echo " 最后部分: $tail_file (256 字节)"
# 调用 merge 脚本
if ./merge_2323_firmware.sh -u "$updater" -f "$head_file" -i "$tail_file" -o "$output"; then
echo "✅ 合并成功,输出文件: $output"
# 获取输出文件大小
if stat --version >/dev/null 2>&1; then
out_size=$(stat -c%s "$output") # Linux
else
out_size=$(stat -f%z "$output") # macOS
fi
# 删除最后 1 字节
if [ "$out_size" -gt 0 ]; then
if command -v truncate >/dev/null 2>&1 && truncate -s $((out_size - 1)) "$output" 2>/dev/null; then
echo "已删除最后 1 字节(使用 truncate"
else
# 使用 dd 备用方案
dd if="$output" of="${output}.tmp" bs=1 count=$((out_size - 1)) status=none && mv "${output}.tmp" "$output"
echo "已删除最后 1 字节(使用 dd"
fi
echo "最终文件大小: $(stat -c%s "$output" 2>/dev/null || stat -f%z "$output")"
fi
# 检查是否 < 0x1df00
final_size=$(stat -c%s "$output" 2>/dev/null || stat -f%z "$output")
if [ "$final_size" -gt 122624 ]; then
echo "⚠️ 警告: 文件大小仍然 >= 0x1df00 (当前 $final_size 字节)"
fi
# 清理临时文件
rm -f "$head_file" "$tail_file"
echo "⚠️⚠️⚠️ Remember signing output file! ⚠️⚠️⚠️"
echo "python signer.py --output_dir output --firmware /tmp/40_xiaomi_0403.bin"
else
echo "合并失败,保留临时文件以便排查"
fi