-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrm01-flasher.sh
More file actions
executable file
·2031 lines (1675 loc) · 60 KB
/
Copy pathrm01-flasher.sh
File metadata and controls
executable file
·2031 lines (1675 loc) · 60 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# RM-01 设备完整刷机脚本
# 版本: 1.0
# 日期: 2025年10月11日
# 描述: 用于RM-01设备的三阶段刷机流程:ESP32S3 + AGX + CFE卡
set -e # 遇到错误时退出
# ==================== 全局变量配置 ====================
# 脚本目录
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# 设备配置
ESP_PORT="/dev/ttyACM0"
SERIAL_PORT="/dev/ttyACM0"
CFE_DISK="${CFE_DISK:-/dev/sdd}" # CFE卡设备,可通过环境变量覆盖
TF_DISK="${TF_DISK:-/dev/sda}" # TF卡设备,可通过环境变量覆盖
# L4T目录
L4T_DIR="${L4T_DIR:-/home/rm01/nvidia/nvidia_sdk/JetPack_6.2.1_Linux_JETSON_AGX_ORIN_TARGETS/Linux_for_Tegra/}"
# robOS固件配置
ROBOS_VERSION="v1.1.0"
ROBOS_URL="https://github.com/thomas-hiddenpeak/robOS/releases/download/v1.1.0/robOS-esp32s3-v1.1.0.zip"
FIRMWARE_DIR="$SCRIPT_DIR/firmware"
ROBOS_ZIP="$FIRMWARE_DIR/robOS-esp32s3-v1.1.0.zip"
ROBOS_BUILD_DIR="$FIRMWARE_DIR/build"
# 日志配置
LOG_DIR="$SCRIPT_DIR/logs"
LOG_FILE="$LOG_DIR/rm01-flasher-$(date +%Y%m%d_%H%M%S).log"
# ==================== 颜色定义 ====================
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
WHITE='\033[1;37m'
NC='\033[0m' # No Color
# ==================== 日志和输出函数 ====================
# 初始化日志
init_logging() {
mkdir -p "$LOG_DIR"
echo "========================================" | tee "$LOG_FILE"
echo "RM-01刷机脚本启动时间: $(date)" | tee -a "$LOG_FILE"
echo "脚本版本: 1.0" | tee -a "$LOG_FILE"
echo "========================================" | tee -a "$LOG_FILE"
}
# 打印带颜色的状态信息
print_status() {
local message="$1"
echo -e "${BLUE}[INFO]${NC} $message" | tee -a "$LOG_FILE"
}
print_success() {
local message="$1"
echo -e "${GREEN}[SUCCESS]${NC} $message" | tee -a "$LOG_FILE"
}
print_warning() {
local message="$1"
echo -e "${YELLOW}[WARNING]${NC} $message" | tee -a "$LOG_FILE"
}
print_error() {
local message="$1"
echo -e "${RED}[ERROR]${NC} $message" | tee -a "$LOG_FILE"
}
print_step() {
local step="$1"
local message="$2"
echo -e "${PURPLE}[步骤 $step]${NC} ${WHITE}$message${NC}" | tee -a "$LOG_FILE"
}
print_separator() {
echo "============================================" | tee -a "$LOG_FILE"
}
# ==================== 用户交互函数 ====================
# 用户确认函数
confirm_action() {
local message="$1"
local default="${2:-n}"
if [ "$default" = "y" ]; then
local prompt="$message (Y/n): "
else
local prompt="$message (y/N): "
fi
echo -e "${CYAN}$prompt${NC}"
read -r response
case "$response" in
[yY]|[yY][eE][sS]) return 0 ;;
[nN]|[nN][oO]) return 1 ;;
"") [ "$default" = "y" ] && return 0 || return 1 ;;
*) echo -e "${RED}无效输入,请输入 y 或 n${NC}"; confirm_action "$message" "$default" ;;
esac
}
# 等待用户按键继续
wait_for_key() {
local message="${1:-按任意键继续...}"
echo -e "${CYAN}$message${NC}"
read -n 1 -s
}
# ==================== 环境检查函数 ====================
# 检查是否为root用户
check_root() {
if [[ $EUID -eq 0 ]]; then
print_warning "检测到以root用户运行"
if ! confirm_action "建议使用普通用户运行此脚本,是否继续?"; then
exit 1
fi
fi
}
# 检查必需的工具
check_dependencies() {
print_status "检查系统依赖..."
local missing_tools=()
local tools=("wget" "unzip" "esptool.py" "lsusb" "python3" "minicom" "e2label" "fdisk" "mkfs.ext4" "partprobe" "mkfs.fat" "git")
for tool in "${tools[@]}"; do
if ! command -v "$tool" >/dev/null 2>&1; then
missing_tools+=("$tool")
fi
done
if [ ${#missing_tools[@]} -gt 0 ]; then
print_error "缺少必需的工具: ${missing_tools[*]}"
print_status "正在尝试安装缺少的工具..."
if confirm_action "是否自动安装缺少的工具?" "y"; then
sudo apt update
for tool in "${missing_tools[@]}"; do
case "$tool" in
"esptool.py") sudo apt install -y python3-esptool ;;
"minicom") sudo apt install -y minicom ;;
"e2label"|"mkfs.ext4") sudo apt install -y e2fsprogs ;;
"fdisk"|"partprobe") sudo apt install -y util-linux parted ;;
"mkfs.fat") sudo apt install -y dosfstools ;;
"git") sudo apt install -y git ;;
*) sudo apt install -y "$tool" ;;
esac
done
else
print_error "请手动安装缺少的工具后重新运行脚本"
exit 1
fi
fi
print_success "所有依赖检查通过"
}
# 检查L4T环境
check_l4t_environment() {
print_status "检查L4T环境..."
if [ ! -d "$L4T_DIR" ]; then
print_error "L4T目录不存在: $L4T_DIR"
print_error "请确认NVIDIA JetPack SDK已正确安装"
exit 1
fi
if [ ! -f "$L4T_DIR/flash.sh" ]; then
print_error "flash.sh脚本不存在: $L4T_DIR/flash.sh"
exit 1
fi
if [ ! -f "$L4T_DIR/tools/kernel_flash/l4t_initrd_flash.sh" ]; then
print_error "l4t_initrd_flash.sh脚本不存在"
exit 1
fi
print_success "L4T环境检查通过: $L4T_DIR"
}
# 检查设备连接
check_device_connections() {
print_status "检查设备连接状态..."
# 检查串口设备
if [ ! -e "$ESP_PORT" ]; then
print_warning "串口设备不存在: $ESP_PORT"
print_status "请检查设备连接和驱动安装"
if ! confirm_action "是否继续?(如果设备稍后连接)"; then
exit 1
fi
else
print_success "串口设备已连接: $ESP_PORT"
fi
}
# ==================== robOS固件管理函数 ====================
# 下载robOS固件
download_robos_firmware() {
print_step "1" "准备robOS固件"
mkdir -p "$FIRMWARE_DIR"
if [ -f "$ROBOS_ZIP" ]; then
print_status "robOS固件已存在,跳过下载"
return 0
fi
print_status "正在下载robOS固件 $ROBOS_VERSION..."
if wget -O "$ROBOS_ZIP" "$ROBOS_URL"; then
print_success "robOS固件下载完成"
else
print_error "robOS固件下载失败"
return 1
fi
}
# 解压robOS固件
extract_robos_firmware() {
# 检查是否已经解压(通过检查build目录是否存在)
if [ -d "$FIRMWARE_DIR/build" ]; then
print_status "robOS固件已解压,跳过解压"
return 0
fi
print_status "正在解压robOS固件..."
if unzip -o -q "$ROBOS_ZIP" -d "$FIRMWARE_DIR"; then
print_success "robOS固件解压完成"
# 验证解压是否成功(检查关键文件)
if [ -d "$ROBOS_BUILD_DIR" ] && [ -f "$ROBOS_BUILD_DIR/flash_args" ]; then
print_success "固件文件验证通过"
else
print_error "固件解压后验证失败,缺少必要文件"
return 1
fi
else
print_error "robOS固件解压失败"
return 1
fi
}
# ==================== ESP32S3刷机函数 ====================
# ESP32S3刷机
flash_esp32s3() {
print_step "2" "ESP32S3刷入robOS固件"
print_separator
if [ ! -e "$ESP_PORT" ]; then
print_error "串口设备不存在: $ESP_PORT"
print_status "请连接ESP32S3设备到串口"
wait_for_key "连接完成后按任意键继续..."
if [ ! -e "$ESP_PORT" ]; then
print_error "串口设备仍然不存在,跳过ESP32S3刷机"
return 1
fi
fi
# 检查串口连接(ESP32S3)
if ! check_serial_connection "$ESP_PORT"; then
print_error "ESP32S3串口连接检查失败"
return 1
fi
# 切换到固件的build目录
cd "$ROBOS_BUILD_DIR"
print_status "开始刷写ESP32S3固件..."
print_status "使用参数: DIO 80MHz 16MB"
print_status "串口: $ESP_PORT"
# 用户确认擦除操作
print_warning "⚠️ 即将执行完整的Flash擦除和固件刷写:"
print_warning " 1. 擦除整个Flash存储器(清除固件、NVS、配置等)"
print_warning " 2. 刷写新的robOS固件"
print_warning " 3. 初始化设备参数"
echo
if ! confirm_action "确认要继续ESP32S3完整刷写流程吗?" "y"; then
print_warning "用户取消ESP32S3刷写"
cd "$SCRIPT_DIR"
return 1
fi
# 步骤1: 擦除flash
print_separator
print_status "🧹 步骤1: 擦除ESP32S3 Flash存储器..."
print_status "正在清除所有之前的固件、NVS数据和配置信息..."
local erase_cmd="esptool.py --chip esp32s3 --port $ESP_PORT --baud 460800 erase_flash"
print_status "执行擦除命令: $erase_cmd"
if eval "$erase_cmd"; then
print_success "✅ Flash擦除完成"
else
print_error "❌ Flash擦除失败"
cd "$SCRIPT_DIR"
return 1
fi
print_status "等待设备重启完成..."
sleep 3
# 步骤2: 刷写固件
print_separator
print_status "🔥 步骤2: 刷写robOS固件到ESP32S3..."
# 执行esptool命令 - 使用标准的重启参数
print_status "执行ESP32S3固件刷写(自动重启)..."
if esptool.py --chip esp32s3 --port "$ESP_PORT" --baud 460800 \
--before default_reset --after hard_reset \
write_flash --flash_mode dio --flash_freq 80m --flash_size 16MB \
0x0 bootloader/bootloader.bin \
0x10000 robOS.bin \
0x8000 partition_table/partition-table.bin; then
print_success "🎉 ESP32S3固件刷写完成"
print_status "完整流程: Flash擦除 ✅ → 固件刷写 ✅"
cd "$SCRIPT_DIR"
# esptool显示"Hard resetting via RTS pin..."但可能没有真正重启
# 使用我们的专用程序确保真正的硬件重启
print_separator
print_status "🔄 确保ESP32S3真正重启到正常模式..."
if [ -f "$SCRIPT_DIR/esp32s3_reset" ]; then
if "$SCRIPT_DIR/esp32s3_reset" "$ESP_PORT"; then
print_success "✅ ESP32S3硬件重启完成"
else
print_warning "⚠️ 硬件重启失败"
fi
else
print_warning "⚠️ 重启程序不存在"
fi
print_status "⏳ 等待ESP32S3启动完成..."
print_status "robOS固件启动和初始化中..."
# 等待4秒让固件完全启动
for i in {4..1}; do
print_status "等待倒计时: ${i}秒..."
sleep 1
done
# 直接进行参数初始化,因为硬件重启已经完成
print_status "🚀 开始执行参数初始化..."
# 初始化ESP32S3参数
if initialize_esp32s3_parameters; then
print_success "🎉 ESP32S3刷机和参数初始化全部完成!"
else
print_warning "⚠️ 参数初始化失败"
print_status "固件刷写已成功完成,如需重新初始化参数请使用选项3"
fi
return 0
else
print_error "ESP32S3固件刷写失败"
cd "$SCRIPT_DIR"
return 1
fi
}
# ESP32S3参数发送函数(专用于参数初始化)
send_esp32s3_parameter() {
local command="$1"
local port="$2"
local timeout="${3:-1}"
local show_echo="${4:-false}"
# 创建临时文件存储回显
local echo_file="/tmp/esp32s3_param_$$"
# 启动后台进程监听串口回显
timeout $((timeout + 1)) cat "$port" > "$echo_file" &
local cat_pid=$!
# 等待一下确保cat进程已启动
sleep 0.5
# 发送命令到串口(添加回车换行符)
printf "%s\r\n" "$command" > "$port" 2>/dev/null
# 等待指定时间
sleep "$timeout"
# 终止cat进程
kill $cat_pid 2>/dev/null || true
wait $cat_pid 2>/dev/null || true
# 检查是否有回显
local success=true
if [ -f "$echo_file" ] && [ -s "$echo_file" ]; then
# 显示回显内容
local echo_content=$(cat "$echo_file" | head -1 | tr -d '\r\n')
if [ -n "$echo_content" ]; then
# 检查是否有错误信息
if echo "$echo_content" | grep -q -i "error\|fail\|invalid\|unknown"; then
echo " ❌ 错误回显: $echo_content"
success=false
elif [ "$show_echo" = true ]; then
echo " 📟 回显: $echo_content"
fi
fi
else
# 对于某些命令,没有回显也是正常的
if [ "$show_echo" = true ]; then
case "$command" in
*"save"*|*"enable"*|*"set"*)
echo " 📝 (配置命令,无回显)"
;;
*)
echo " ⚪ (无回显)"
;;
esac
fi
fi
# 清理临时文件
rm -f "$echo_file"
if [ "$success" = true ]; then
return 0
else
return 1
fi
}
# ESP32S3参数初始化
initialize_esp32s3_parameters() {
print_step "3" "ESP32S3参数初始化"
print_separator
print_status "准备初始化ESP32S3设备参数..."
print_warning "⚠️ 此操作将配置以下参数:"
print_warning " • 电源管理和USB复用"
print_warning " • 网络配置 (IP: 10.10.99.97)"
print_warning " • 风扇控制和温度管理"
print_warning " • LED灯效和颜色设置"
echo
if ! confirm_action "确认要继续ESP32S3参数初始化吗?" "y"; then
print_warning "用户取消ESP32S3参数初始化"
return 1
fi
# 询问是否显示详细回显
local show_echo=false
if confirm_action "是否显示每条命令的串口回显?(推荐用于调试)" "n"; then
show_echo=true
print_status "将显示详细的串口回显信息"
else
print_status "将以简洁模式执行(仅显示错误)"
fi
print_status "等待ESP32S3重启完成..."
sleep 5
# 设置正确的波特率(参数通信使用115200)
print_status "设置串口波特率为115200..."
if command -v stty >/dev/null 2>&1; then
stty -F "$ESP_PORT" 115200 cs8 -cstopb -parenb 2>/dev/null || {
print_warning "设置波特率失败,但继续执行"
}
fi
print_status "开始发送初始化参数..."
# 定义所有需要发送的命令
local commands=(
"lpmu config auto-start on"
"usbmux lpmu"
"usbmux save"
"net config set ip 10.10.99.97"
"net config set gateway 10.10.99.100"
"net config set dns 8.8.8.8"
"net config set dhcp_lease_hours 24"
"net config save"
"fan gpio 0 41 1"
"fan enable 0 on"
"fan set 0 75"
"fan status"
"temp auto"
"fan mode 0 curve"
"fan config curve 0 40:20 50:40 60:55 70:70 80:100"
"fan config hysteresis 0 3.0 2000"
"fan config save"
"temp status"
"fan status"
"led touch set white"
"led touch config save"
"led board anim fire 40"
"led board config save"
"led matrix mode static"
"led matrix image import /sdcard/matrix.json"
"led matrix config save"
"color enable"
"color gamma 0.6"
"color saturation 1.5"
"color brightness 1.2"
"color save"
"reboot"
)
# 检查串口连接
if ! check_serial_connection "$ESP_PORT"; then
print_error "串口连接检查失败,无法进行参数初始化"
return 1
fi
local total_commands=${#commands[@]}
local current_command=1
local failed_commands=()
print_status "总共需要发送 $total_commands 条初始化命令"
print_separator
# 逐条发送命令
for cmd in "${commands[@]}"; do
print_status "[$current_command/$total_commands] 发送命令: $cmd"
# 根据命令类型决定等待时间(整数秒)
local wait_time=1
case "$cmd" in
"reboot")
wait_time=4
;;
*"save"*)
wait_time=1
;;
*"status"*)
wait_time=1
;;
esac
# 使用专用的参数发送函数
if send_esp32s3_parameter "$cmd" "$ESP_PORT" "$wait_time" "$show_echo"; then
print_success "✅ 命令执行成功"
else
print_warning "⚠️ 命令执行可能失败: $cmd"
failed_commands+=("$cmd")
fi
# 特殊处理某些需要更长等待时间的命令
case "$cmd" in
"reboot")
print_status "设备重启中,额外等待5秒..."
sleep 5
;;
esac
# 命令间间隔500毫秒
sleep 0.5
((current_command++))
echo
done
print_separator
# 总结初始化结果
if [ ${#failed_commands[@]} -eq 0 ]; then
print_success "🎉 所有初始化命令发送完成!"
print_separator
print_status "📋 ESP32S3参数配置汇总:"
print_status " • 电源管理:自动启动已启用"
print_status " • USB复用:已配置为ESP32S3模式"
print_status " • 网络配置:IP 10.10.99.97, 网关 10.10.99.100"
print_status " • DNS配置:8.8.8.8, DHCP租期 24小时"
print_status " • 风扇控制:GPIO 41启用,转速75%,曲线模式"
print_status " • 温度监控:自动模式"
print_status " • LED触摸:白色"
print_status " • LED板:火焰动画效果"
print_status " • LED矩阵:静态模式,导入matrix.json"
print_status " • 颜色校正:伽马0.6,饱和度1.5,亮度1.2"
print_status ""
print_status "设备正在重启,请等待启动完成..."
print_success "ESP32S3初始化完成,设备已就绪!"
return 0
else
print_warning "⚠️ 部分命令发送失败:"
for failed_cmd in "${failed_commands[@]}"; do
print_error " - $failed_cmd"
done
print_status "成功发送: $((total_commands - ${#failed_commands[@]}))/$total_commands 条命令"
print_warning "建议检查串口连接或手动发送失败的命令"
return 1
fi
}
# ==================== AGX刷机函数 ====================
# 检查串口连接状态
check_serial_connection() {
local port="$1"
print_status "🔍 检查串口连接状态: $port"
# 检查设备文件是否存在
if [ ! -e "$port" ]; then
print_error "串口设备文件不存在: $port"
return 1
fi
# 检查设备权限
if [ ! -r "$port" ] || [ ! -w "$port" ]; then
print_error "串口设备权限不足: $port"
print_status "当前权限: $(ls -l $port)"
print_status "尝试修复权限..."
if sudo chmod 666 "$port"; then
print_success "权限修复成功"
else
print_error "权限修复失败"
return 1
fi
fi
# 检查是否被其他进程占用
local processes=$(lsof "$port" 2>/dev/null || true)
if [ -n "$processes" ]; then
print_warning "串口设备正被其他进程占用:"
echo "$processes"
if confirm_action "是否终止占用进程并继续?" "y"; then
local pids=$(lsof -t "$port" 2>/dev/null || true)
if [ -n "$pids" ]; then
echo "$pids" | xargs -r kill 2>/dev/null || true
sleep 1
# 再次检查
if lsof "$port" >/dev/null 2>&1; then
print_error "无法释放串口设备"
return 1
else
print_success "串口设备已释放"
fi
fi
else
return 1
fi
fi
# 测试串口通信
print_status "测试串口通信..."
if timeout 2 bash -c "echo '' > $port" 2>/dev/null; then
print_success "串口设备可写入"
else
print_error "串口设备写入测试失败"
return 1
fi
# 显示串口设备信息
print_status "串口设备信息:"
echo " 设备路径: $port"
echo " 权限: $(ls -l $port | awk '{print $1, $3, $4}')"
# 尝试读取设备属性
if command -v stty >/dev/null 2>&1; then
local stty_info=$(stty -F "$port" 2>/dev/null || echo "无法获取")
echo " 波特率等信息: $stty_info"
fi
print_success "✅ 串口连接检查通过"
return 0
}
# 发送串口命令并显示回显
send_serial_command_with_echo() {
local command="$1"
local port="$2"
local timeout="${3:-3}"
local skip_precheck="${4:-false}" # 第4个参数:是否跳过预检测,默认false
print_status "向串口 $port 发送命令: $command"
# 创建临时文件存储回显
local echo_file="/tmp/serial_echo_$$"
print_status "📤 发送命令: $command"
# 启动后台进程监听串口回显并保存到文件
timeout $((timeout + 2)) cat "$port" > "$echo_file" &
local cat_pid=$!
# 等待一下确保cat进程已启动
sleep 0.5
# 发送命令到串口(添加回车换行符)
printf "%s\r\n" "$command" > "$port"
print_status "⏳ 等待 $timeout 秒并捕获串口回显..."
sleep "$timeout"
# 终止cat进程
kill $cat_pid 2>/dev/null || true
wait $cat_pid 2>/dev/null || true
# 显示捕获的回显
if [ -f "$echo_file" ] && [ -s "$echo_file" ]; then
print_status "📺 串口回显内容:"
echo -e "${CYAN}----------------------------------------${NC}"
# 使用 cat -v 显示控制字符,或者使用 strings 过滤
cat "$echo_file"
echo -e "${CYAN}----------------------------------------${NC}"
# 检查回显内容是否包含错误信息
if grep -q -i "error\|fail\|invalid" "$echo_file"; then
print_warning "⚠️ 回显中包含错误信息,请检查"
fi
else
print_warning "❌ 未捕获到串口回显"
print_status "📋 故障排除步骤:"
print_status "1. 确认RM-01设备已通电并开机"
print_status "2. 检查串口线缆连接是否牢固"
print_status "3. 确认设备正在监听串口命令"
print_status "4. 尝试手动测试串口通信"
fi
# 清理临时文件
rm -f "$echo_file"
echo # 换行
}
# 发送串口命令(原版本,用于简单命令)
send_serial_command() {
local command="$1"
local port="$2"
local timeout="${3:-3}"
print_status "向串口 $port 发送命令: $command"
# 使用原生方式发送命令到串口(添加回车换行符)
printf "%s\r\n" "$command" > "$port"
print_status "等待 $timeout 秒..."
sleep "$timeout"
}
# 检查NVIDIA APX设备
check_nvidia_apx() {
print_status "检查NVIDIA APX设备..."
local max_attempts=10
local attempt=1
while [ $attempt -le $max_attempts ]; do
print_status "尝试检测NVIDIA APX设备 (第 $attempt/$max_attempts 次)"
# 检查具体的设备名称:"NVIDIA Corp. APX"
local nvidia_device=$(lsusb | grep -i "NVIDIA Corp.*APX")
if [ -n "$nvidia_device" ]; then
print_success "检测到NVIDIA APX设备"
print_status "设备详情: $nvidia_device"
return 0
fi
# 兼容性检查:也检查其他可能的NVIDIA APX格式
if lsusb | grep -i "nvidia" | grep -i "apx"; then
print_success "检测到NVIDIA APX设备 (兼容格式)"
lsusb | grep -i "nvidia" | grep -i "apx"
return 0
fi
print_status "未检测到APX设备,等待1秒后重试..."
print_status "当前USB设备列表:"
lsusb | grep -i nvidia || print_status " (未发现NVIDIA设备)"
sleep 1
((attempt++))
done
print_warning "未检测到NVIDIA APX设备"
print_status "完整USB设备列表:"
lsusb
return 1
}
# AGX刷机
flash_agx() {
print_step "4" "AGX刷入引导镜像"
print_separator
# 详细检查串口连接
if ! check_serial_connection "$SERIAL_PORT"; then
print_error "串口连接检查失败,无法继续"
print_status "请检查:"
print_status "1. 设备是否正确连接到串口"
print_status "2. 串口驱动是否正确安装"
print_status "3. 用户是否有串口访问权限"
return 1
fi
print_status "准备让设备进入Recovery模式..."
local recovery_success=false
local max_recovery_attempts=3
local recovery_attempt=1
while [ $recovery_attempt -le $max_recovery_attempts ] && [ "$recovery_success" = false ]; do
print_status "Recovery尝试 $recovery_attempt/$max_recovery_attempts"
print_status "即将重启ESP32S3并发送recovery命令"
if confirm_action "是否配置USB多路复用器并发送 'agx recovery' 命令?" "y"; then
print_separator
print_status "📡 配置USB多路复用器..."
# 先发送 usbmux agx 命令(跳过预检测,因为设备可能静默)
print_status "发送 'usbmux agx' 命令"
send_serial_command_with_echo "usbmux agx" "$SERIAL_PORT" 2 true
sleep 1
# 再发送 usbmux save 命令(跳过预检测)
print_status "发送 'usbmux save' 命令"
send_serial_command_with_echo "usbmux save" "$SERIAL_PORT" 2 true
sleep 1
print_status "🔄 重启ESP32S3..."
# 发送重启命令
send_serial_command "reboot" "$SERIAL_PORT" 2
print_status "⏳ 等待ESP32S3重启完成 (5秒)..."
sleep 5
print_status "📡 发送recovery命令并显示串口回显:"
# 发送recovery命令并显示回显
send_serial_command_with_echo "agx recovery" "$SERIAL_PORT" 5
print_separator
print_status "请查看上面的串口回显信息"
if confirm_action "recovery命令执行成功了吗?(看到正确的回显信息)" "y"; then
recovery_success=true
print_success "用户确认recovery命令执行成功"
# 等待用户确认插入USB线缆
print_warning "请确保已将USB-C线缆连接到设备顶部的刷机接口!"
wait_for_key "连接完成后按任意键继续检测APX设备..."
else
print_warning "recovery命令似乎没有成功执行"
((recovery_attempt++))
if [ $recovery_attempt -le $max_recovery_attempts ]; then
print_status "准备重新尝试recovery命令..."
sleep 2
else
print_error "已达到最大重试次数,无法成功执行recovery命令"
if ! confirm_action "是否仍要继续尝试检测APX设备?"; then
return 1
fi
fi
fi
else
print_warning "用户取消发送recovery命令"
return 1
fi
done
# 检查recovery是否成功
if [ "$recovery_success" = false ]; then
print_error "Recovery命令未成功执行,无法继续刷机"
return 1
fi
# 检查APX设备
print_status "开始检测NVIDIA APX设备..."
if check_nvidia_apx; then
print_success "✅ 设备已成功进入Recovery模式,检测到APX设备"
else
print_error "❌ 未检测到NVIDIA APX设备,设备可能未正确进入Recovery模式"
print_error "刷机无法继续,请检查:"
print_error "1. USB-C线缆是否正确连接到设备顶部刷机接口"
print_error "2. 设备是否正确响应了recovery命令"
print_error "3. 设备驱动是否正确安装"
return 1
fi
sleep 1
# 执行刷机
print_status "🚀 开始执行AGX刷机..."
print_status "切换到L4T目录: $L4T_DIR"
cd "$L4T_DIR"
local flash_command="sudo ./flash.sh rm01-orin nvme0n1p1"
print_status "执行刷机命令: $flash_command"
if eval "$flash_command"; then
print_success "🎉 AGX引导镜像刷写完成"
cd "$SCRIPT_DIR"
return 0
else
print_error "❌ AGX引导镜像刷写失败"
cd "$SCRIPT_DIR"
return 1
fi
}
# ==================== CFE卡初始化函数 ====================
# 获取CFE卡信息
get_cfe_card_info() {
local disk="$CFE_DISK"
print_status "🔍 检测CFE卡信息..."
if [ ! -b "$disk" ]; then
print_error "未检测到CFE卡设备: $disk"
return 1
fi
# 获取磁盘大小(GB)
local size_bytes=$(lsblk -b -n -o SIZE "$disk" 2>/dev/null | head -1)
local size_gb=$((size_bytes / 1024 / 1024 / 1024))
# 获取磁盘型号
local model=$(lsblk -n -o MODEL "$disk" 2>/dev/null | head -1 | xargs)
print_status "📋 CFE卡详细信息:"
echo " 设备路径: $disk"
echo " 磁盘大小: ${size_gb}GB"
echo " 磁盘型号: ${model:-未知}"
# 显示当前分区信息
print_status "🗂️ 当前分区信息:"
lsblk "$disk" || echo " 无分区信息"
return 0
}
# 卸载CFE卡所有分区
unmount_all_cfe_partitions() {
local disk="$CFE_DISK"
local cfe_device=$(basename "$CFE_DISK")
print_status "🔧 卸载CFE卡所有分区..."
# 获取所有相关分区
local partitions=$(lsblk -n -o NAME "$disk" | grep -v "^${cfe_device}$" | sed 's/^/\/dev\//' || true)
if [ -n "$partitions" ]; then
echo "$partitions" | while read -r partition; do
if mount | grep -q "$partition"; then
print_status "卸载分区: $partition"
sudo umount "$partition" 2>/dev/null || true
fi
done
fi
# 强制卸载常见分区
for i in 1 2 3; do
local partition="${disk}${i}"
if mount | grep -q "$partition"; then
print_status "强制卸载分区: $partition"
sudo umount "$partition" 2>/dev/null || true
fi
done
sleep 2
print_success "所有分区已卸载"
}
# 删除CFE卡所有分区
delete_all_partitions() {
local disk="$CFE_DISK"
print_status "🗑️ 删除CFE卡所有分区..."
# 使用fdisk删除所有分区
sudo fdisk "$disk" << EOF >/dev/null 2>&1
o
w
EOF
# 等待设备更新
sleep 2
sudo partprobe "$disk" 2>/dev/null || true
sleep 1
print_success "所有分区已删除"
}
# 根据容量创建分区
create_partitions_by_size() {
local disk="$CFE_DISK"
local size_bytes=$(lsblk -b -n -o SIZE "$disk" 2>/dev/null | head -1)
local size_gb=$((size_bytes / 1024 / 1024 / 1024))