forked from MrChromebox/scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfirmware.sh
1284 lines (1129 loc) · 47.1 KB
/
firmware.sh
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
#
###################
# flash RW_LEGACY #
###################
function flash_rwlegacy()
{
#set working dir
cd /tmp
# set dev mode legacy boot flag
if [ "${isChromeOS}" = true ]; then
crossystem dev_boot_legacy=1 > /dev/null 2>&1
fi
echo_green "\nInstall/Update RW_LEGACY Firmware (Legacy BIOS)"
#determine proper file
if [[ "$isHswBox" = true || "$isBdwBox" = true ]]; then
seabios_file=$seabios_hswbdw_box
elif [[ "$isHswBook" = true || "$isBdwBook" = true ]]; then
seabios_file=$seabios_hswbdw_book
elif [ "$isBaytrail" = true ]; then
seabios_file=$seabios_baytrail
elif [ "$isBraswell" = true ]; then
seabios_file=$seabios_braswell
elif [ "$isSkylake" = true ]; then
seabios_file=$seabios_skylake
elif [ "$isKbl" = true ]; then
seabios_file=$seabios_kbl
elif [ "$device" = "link" ]; then
seabios_file=$seabios_link
elif [ "$isApl" = true ]; then
seabios_file=$seabios_apl
else
echo_red "Unknown or unsupported device (${device}); cannot update RW_LEGACY firmware."; return 1
fi
preferUSB=false
useHeadless=false
if [ -z "$1" ]; then
echo -e ""
#USB boot priority
echo_yellow "Default to booting from USB?"
read -ep "If N, always boot from internal storage unless selected from boot menu. [y/N] "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] && preferUSB=true
echo -e ""
#headless?
if [[ "$seabios_file" = "$seabios_hswbdw_box" && "$device" != "monroe" ]]; then
echo_yellow "Install \"headless\" firmware?"
read -ep "This is only needed for servers running without a connected display. [y/N] "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] && useHeadless=true
echo -e ""
fi
fi
#download SeaBIOS update
echo_yellow "\nDownloading RW_LEGACY firmware update\n(${seabios_file})"
curl -s -L -O ${rwlegacy_source}${seabios_file}.md5
curl -s -L -O ${rwlegacy_source}${seabios_file}
#verify checksum on downloaded file
md5sum -c ${seabios_file}.md5 --quiet 2> /dev/null
[[ $? -ne 0 ]] && { exit_red "RW_LEGACY download checksum fail; download corrupted, cannot flash"; return 1; }
#preferUSB?
if [ "$preferUSB" = true ]; then
#swanky special case
if [[ "$device" = "swanky" ]]; then
curl -s -L -o bootorder "${cbfs_source}bootorder.usb2"
else
curl -s -L -o bootorder "${cbfs_source}bootorder.usb"
fi
if [ $? -ne 0 ]; then
echo_red "Unable to download bootorder file; boot order cannot be changed."
else
${cbfstoolcmd} ${seabios_file} remove -n bootorder > /dev/null 2>&1
${cbfstoolcmd} ${seabios_file} add -n bootorder -f /tmp/bootorder -t raw > /dev/null 2>&1
fi
fi
#useHeadless?
if [ "$useHeadless" = true ]; then
curl -s -L -O "${cbfs_source}${hswbdw_headless_vbios}"
if [ $? -ne 0 ]; then
echo_red "Unable to download headless VGA BIOS; headless firmware cannot be installed."
else
${cbfstoolcmd} ${seabios_file} remove -n pci8086,0406.rom > /dev/null 2>&1
rc0=$?
${cbfstoolcmd} ${seabios_file} add -f ${hswbdw_headless_vbios} -n pci8086,0406.rom -t optionrom > /dev/null 2>&1
rc1=$?
if [[ "$rc0" -ne 0 || "$rc1" -ne 0 ]]; then
echo_red "Warning: error installing headless VGA BIOS"
else
echo_yellow "Headless VGA BIOS installed"
fi
fi
fi
#handle NINJA VGABIOS
if [[ "$device" = "ninja" ]]; then
#extract vbios from stock BOOT_STUB, inject into RWL
${cbfstoolcmd} bios.bin extract -r BOOT_STUB -n pci8086,0f31.rom -f vgabios.bin > /dev/null 2>&1
rc0=$?
${cbfstoolcmd} ${seabios_file} remove -n pci8086,0f31.rom > /dev/null 2>&1
rc1=$?
${cbfstoolcmd} ${seabios_file} add -f vgabios.bin -n pci8086,0f31.rom -t optionrom > /dev/null 2>&1
rc2=$?
if [[ "$rc0" -ne 0 || "$rc1" -ne 0 || "$rc2" -ne 0 ]]; then
echo_red "Warning: error installing VGA BIOS"
else
echo_yellow "VGA BIOS installed"
fi
fi
#flash updated legacy BIOS
echo_yellow "Installing RW_LEGACY firmware"
${flashromcmd} -w -i RW_LEGACY:${seabios_file} > /dev/null 2>&1
echo_green "RW_LEGACY firmware successfully installed/updated."
if [ -z "$1" ]; then
read -ep "Press [Enter] to return to the main menu."
fi
}
#############################
# Install coreboot Firmware #
#############################
function flash_coreboot()
{
fwTypeStr=""
if [[ "$hasLegacyOption" = true && "$unlockMenu" = true ]]; then
fwTypeStr="Legacy/UEFI"
else
fwTypeStr="UEFI"
fi
echo_green "\nInstall/Update ${fwTypeStr} Full ROM Firmware"
echo_yellow "Standard disclaimer: flashing the firmware has the potential to
brick your device, requiring relatively inexpensive hardware and some
technical knowledge to recover. You have been warned."
[[ "$isChromeOS" = true ]] && echo_yellow "Also, flashing Full ROM firmware will remove your ability to run ChromeOS."
read -ep "Do you wish to continue? [y/N] "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] || return
#spacing
echo -e ""
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot flash Full ROM firmware."; return 1; }
#special warning for EVE
if [ "$device" = "eve" ]; then
echo_yellow "VERY IMPORTANT: flashing your Pixelbook is serious business.
There is currently no way easy to unbrick if something goes wrong.
Only do this if you understand and accept the risk, because it's a
paperweight if something goes wrong.
(there have been no bricks so far, but the possibility exists)"
echo_yellow "If you wish to continue, type: 'I ACCEPT' and press enter."
read -e
[[ "$REPLY" = "I ACCEPT" ]] || return
fi
#UEFI or legacy firmware
if [[ ! -z "$1" || ( "$isUEFI" = true && "$unlockMenu" = false ) || "$hasLegacyOption" = false ]]; then
useUEFI=true
else
useUEFI=false
if [[ "$hasUEFIoption" = true ]]; then
echo -e ""
echo_yellow "Install UEFI-compatible firmware?"
echo -e "UEFI firmware is the preferred option for all OSes.
Legacy SeaBIOS firmware is deprecated but available for Chromeboxes to enable
PXE (network boot) capability and compatibility with Legacy OS installations.
"
REPLY=""
while [[ "$REPLY" != "U" && "$REPLY" != "u" && "$REPLY" != "L" && "$REPLY" != "l" ]]
do
read -ep "Enter 'U' for UEFI, 'L' for Legacy: "
if [[ "$REPLY" = "U" || "$REPLY" = "u" ]]; then
useUEFI=true
fi
done
fi
fi
#UEFI notice if flashing from ChromeOS or Legacy
if [[ "$useUEFI" = true && ! -d /sys/firmware/efi ]]; then
[[ "$isChromeOS" = true ]] && currOS="ChromeOS" || currOS="Your Legacy-installed OS"
echo_yellow "
NOTE: After flashing UEFI firmware, you will need to install a UEFI-compatible
OS; ${currOS} will no longer be bootable. UEFI firmware supports
Windows and Linux on all devices. Debian/Ubuntu-based distros require a small
fix to boot after install -- see https://mrchromebox.tech/#faq for more info."
REPLY=""
read -ep "Press Y to continue or any other key to abort. "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] || return
fi
#determine correct file / URL
firmware_source=${fullrom_source}
if [[ "$hasUEFIoption" = true || "$hasLegacyOption" = true ]]; then
if [ "$useUEFI" = true ]; then
eval coreboot_file=$`echo "coreboot_uefi_${device}"`
else
eval coreboot_file=$`echo "coreboot_${device}"`
fi
else
exit_red "Unknown or unsupported device (${device^^}); cannot continue."; return 1
fi
#peppy special case
if [ "$device" = "peppy" ]; then
hasElan=$(cat /proc/bus/input/devices | grep "Elan")
hasCypress=$(cat /proc/bus/input/devices | grep "Cypress")
if [[ $hasElan = "" && $hasCypress = "" ]]; then
echo -e ""
read -ep "Unable to automatically determine trackpad type. Does your Peppy have an Elan pad? [y/N] "
if [[ "$REPLY" = "y" || "$REPLY" = "Y" ]]; then
if [ "$useUEFI" = true ]; then
coreboot_file=${coreboot_uefi_peppy_elan}
else
coreboot_file=${coreboot_peppy_elan}
fi
fi
elif [[ $hasElan != "" ]]; then
if [ "$useUEFI" = true ]; then
coreboot_file=${coreboot_uefi_peppy_elan}
else
coreboot_file=${coreboot_peppy_elan}
fi
fi
fi
#auron special case (upgrade from coolstar legacy rom)
if [ "$device" = "auron" ]; then
echo -e ""
echo_yellow "Unable to determine Chromebook model"
echo -e "Because of your current firmware, I'm unable to
determine the exact mode of your Chromebook. Are you using
an Acer C740 (Auron_Paine) or Acer C910/CB5-571 (Auron_Yuna)?
"
REPLY=""
while [[ "$REPLY" != "P" && "$REPLY" != "p" && "$REPLY" != "Y" && "$REPLY" != "y" ]]
do
read -ep "Enter 'P' for Auron_Paine, 'Y' for Auron_Yuna: "
if [[ "$REPLY" = "Y" || "$REPLY" = "y" ]]; then
if [ "$useUEFI" = true ]; then
coreboot_file=${coreboot_uefi_auron_yuna}
else
coreboot_file=${coreboot_auron_yuna}
fi
else
if [ "$useUEFI" = true ]; then
coreboot_file=${coreboot_uefi_auron_paine}
else
coreboot_file=${coreboot_auron_paine}
fi
fi
done
fi
#extract MAC address if needed
if [[ "$hasLAN" = true ]]; then
#check if contains MAC address, extract
extract_vpd /tmp/bios.bin
if [ $? -ne 0 ]; then
#TODO - user enter MAC manually?
echo_red "\nWarning: firmware doesn't contain VPD info - unable to persist MAC address."
read -ep "Do you wish to continue? [y/N] "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] || return
fi
fi
#check if existing firmware is stock
grep -obUa "vboot" /tmp/bios.bin >/dev/null
if [[ "$isStock" == "true" && $? -eq 0 ]]; then
echo_yellow "\nCreate a backup copy of your stock firmware?"
read -ep "This is highly recommended in case you wish to return your device to stock
configuration/run ChromeOS, or in the (unlikely) event that things go south
and you need to recover using an external EEPROM programmer. [Y/n] "
[[ "$REPLY" = "n" || "$REPLY" = "N" ]] || backup_firmware
fi
#check that backup succeeded
[ $? -ne 0 ] && return 1
#headless?
useHeadless=false
if [[ $useUEFI = false && ( "$isHswBox" = true || "$isBdwBox" = true ) ]]; then
echo -e ""
echo_yellow "Install \"headless\" firmware?"
read -ep "This is only needed for servers running without a connected display. [y/N] "
if [[ "$REPLY" = "Y" || "$REPLY" = "y" ]]; then
useHeadless=true
fi
fi
#USB boot priority
preferUSB=false
if [[ $useUEFI = false ]]; then
echo -e ""
echo_yellow "Default to booting from USB?"
echo -e "If you default to USB, then any bootable USB device
will have boot priority over the internal SSD.
If you default to SSD, you will need to manually select
the USB Device from the Boot Menu in order to boot it.
"
REPLY=""
while [[ "$REPLY" != "U" && "$REPLY" != "u" && "$REPLY" != "S" && "$REPLY" != "s" ]]
do
read -ep "Enter 'U' for USB, 'S' for SSD: "
if [[ "$REPLY" = "U" || "$REPLY" = "u" ]]; then
preferUSB=true
fi
done
fi
#add PXE?
addPXE=false
if [[ $useUEFI = false && "$hasLAN" = true ]]; then
echo -e ""
echo_yellow "Add PXE network booting capability?"
read -ep "(This is not needed for by most users) [y/N] "
if [[ "$REPLY" = "Y" || "$REPLY" = "y" ]]; then
addPXE=true
echo -e ""
echo_yellow "Boot PXE by default?"
read -ep "(will fall back to SSD/USB) [y/N] "
if [[ "$REPLY" = "Y" || "$REPLY" = "y" ]]; then
pxeDefault=true
fi
fi
fi
#download firmware file
cd /tmp
echo_yellow "\nDownloading Full ROM firmware\n(${coreboot_file})"
curl -s -L -O "${firmware_source}${coreboot_file}"
curl -s -L -O "${firmware_source}${coreboot_file}.sha1"
#verify checksum on downloaded file
sha1sum -c ${coreboot_file}.sha1 --quiet > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "Firmware download checksum fail; download corrupted, cannot flash."; return 1; }
#check if we have a VPD to restore
if [ -f /tmp/vpd.bin ]; then
${cbfstoolcmd} ${coreboot_file} add -n vpd.bin -f /tmp/vpd.bin -t raw > /dev/null 2>&1
fi
#preferUSB?
if [[ "$preferUSB" = true && $useUEFI = false ]]; then
curl -s -L -o bootorder "${cbfs_source}bootorder.usb"
if [ $? -ne 0 ]; then
echo_red "Unable to download bootorder file; boot order cannot be changed."
else
${cbfstoolcmd} ${coreboot_file} remove -n bootorder > /dev/null 2>&1
${cbfstoolcmd} ${coreboot_file} add -n bootorder -f /tmp/bootorder -t raw > /dev/null 2>&1
fi
fi
#useHeadless?
if [ "$useHeadless" = true ]; then
curl -s -L -O "${cbfs_source}${hswbdw_headless_vbios}"
if [ $? -ne 0 ]; then
echo_red "Unable to download headless VGA BIOS; headless firmware cannot be installed."
else
${cbfstoolcmd} ${coreboot_file} remove -n pci8086,0406.rom > /dev/null 2>&1
${cbfstoolcmd} ${coreboot_file} add -f ${hswbdw_headless_vbios} -n pci8086,0406.rom -t optionrom > /dev/null 2>&1
fi
fi
#addPXE?
if [ "$addPXE" = true ]; then
curl -s -L -O "${cbfs_source}${pxe_optionrom}"
if [ $? -ne 0 ]; then
echo_red "Unable to download PXE option ROM; PXE capability cannot be added."
else
${cbfstoolcmd} ${coreboot_file} add -f ${pxe_optionrom} -n pci10ec,8168.rom -t optionrom > /dev/null 2>&1
#PXE default?
if [ "$pxeDefault" = true ]; then
${cbfstoolcmd} ${coreboot_file} extract -n bootorder -f /tmp/bootorder > /dev/null 2>&1
${cbfstoolcmd} ${coreboot_file} remove -n bootorder > /dev/null 2>&1
sed -i '1s/^/\/pci@i0cf8\/pci-bridge@1c\/*@0\n/' /tmp/bootorder
${cbfstoolcmd} ${coreboot_file} add -n bootorder -f /tmp/bootorder -t raw > /dev/null 2>&1
fi
fi
fi
#Persist RW_MRC_CACHE for BSW Full ROM firmware
${cbfstoolcmd} /tmp/bios.bin read -r RW_MRC_CACHE -f /tmp/mrc.cache > /dev/null 2>&1
if [[ $isBraswell = "true" && $isFullRom = "true" && $? -eq 0 ]]; then
${cbfstoolcmd} ${coreboot_file} write -r RW_MRC_CACHE -f /tmp/mrc.cache > /dev/null 2>&1
fi
#Persist SMMSTORE if exists
${cbfstoolcmd} /tmp/bios.bin read -r SMMSTORE -f /tmp/smmstore > /dev/null 2>&1
if [[ $useUEFI = "true" && $? -eq 0 ]]; then
${cbfstoolcmd} ${coreboot_file} write -r SMMSTORE -f /tmp/smmstore > /dev/null 2>&1
fi
#disable software write-protect
echo_yellow "Disabling software write-protect and clearing the WP range"
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error disabling software write-protect; unable to flash firmware."; return 1
fi
#clear SW WP range
${flashromcmd} --wp-range 0 0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error clearing software write-protect range; unable to flash firmware."; return 1
fi
#flash Full ROM firmware
#flash only BIOS region, to avoid IFD mismatch upon verification
echo_yellow "Installing Full ROM firmware (may take up to 90s)"
${flashromcmd} -i BIOS -w "${coreboot_file}" -o /tmp/flashrom.log > /dev/null 2>&1
if [ $? -ne 0 ]; then
#try using SI_BIOS
${flashromcmd} -i SI_BIOS -w "${coreboot_file}" -o /tmp/flashrom.log > /dev/null 2>&1
if [ $? -ne 0 ]; then
#try without specifying region
${flashromcmd} -w "${coreboot_file}" -o /tmp/flashrom.log > /dev/null 2>&1
if [ $? -ne 0 ]; then
cat /tmp/flashrom.log
exit_red "An error occurred flashing the Full ROM firmware. DO NOT REBOOT!"; return 1
fi
fi
fi
if [ $? -eq 0 ]; then
echo_green "Full ROM firmware successfully installed/updated."
#Prevent from trying to boot stock ChromeOS install
if [[ "$isStock" = true && "$isChromeOS" = true ]]; then
rm -rf /tmp/boot/efi > /dev/null 2>&1
rm -rf /tmp/boot/syslinux > /dev/null 2>&1
fi
#Warn about long RAM training time, keyboard on Braswell
if [[ "$isBraswell" = true ]]; then
echo_yellow "IMPORTANT:\nThe first boot after flashing may take substantially
longer than subsequent boots -- up to 30s or more.
Be patient and eventually your device will boot :)"
echo_yellow "Additionally, GalliumOS users need to use the v3.0 ISO; the keyboard
will not work with the 2.1 ISO due a bug in the older kernel."
fi
#set vars to indicate new firmware type
isStock=false
isFullRom=true
if [[ "$useUEFI" = "true" ]]; then
firmwareType="Full ROM / UEFI (pending reboot)"
else
firmwareType="Full ROM / Legacy (pending reboot)"
fi
else
echo_red "An error occurred flashing the Full ROM firmware. DO NOT REBOOT!"
fi
read -ep "Press [Enter] to return to the main menu."
}
##########################
# Restore Stock Firmware #
##########################
function restore_stock_firmware()
{
echo_green "\nRestore Stock Firmware"
echo_yellow "Standard disclaimer: flashing the firmware has the potential to
brick your device, requiring relatively inexpensive hardware and some
technical knowledge to recover. You have been warned."
read -ep "Do you wish to continue? [y/N] "
[[ "$REPLY" = "Y" || "$REPLY" = "y" ]] || return
#spacing
echo -e ""
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot restore stock firmware."; return 1; }
firmware_file=""
read -ep "Do you have a firmware backup file on USB? [y/N] "
if [[ "$REPLY" = "y" || "$REPLY" = "Y" ]]; then
read -ep "
Connect the USB/SD device which contains the backed-up stock firmware and press [Enter] to continue. "
list_usb_devices
[ $? -eq 0 ] || { exit_red "No USB devices available to read firmware backup."; return 1; }
read -ep "Enter the number for the device which contains the stock firmware backup: " usb_dev_index
[ $usb_dev_index -gt 0 ] && [ $usb_dev_index -le $num_usb_devs ] || { exit_red "Error: Invalid option selected."; return 1; }
usb_device="/dev/sd${usb_devs[${usb_dev_index}-1]}"
mkdir /tmp/usb > /dev/null 2>&1
mount "${usb_device}" /tmp/usb > /dev/null 2>&1
if [ $? -ne 0 ]; then
mount "${usb_device}1" /tmp/usb
fi
if [ $? -ne 0 ]; then
echo_red "USB device failed to mount; cannot proceed."
read -ep "Press [Enter] to return to the main menu."
umount /tmp/usb > /dev/null 2>&1
return
fi
#select file from USB device
echo_yellow "\n(Potential) Firmware Files on USB:"
ls /tmp/usb/*.{rom,ROM,bin,BIN} 2>/dev/null | xargs -n 1 basename 2>/dev/null
if [ $? -ne 0 ]; then
echo_red "No firmware files found on USB device."
read -ep "Press [Enter] to return to the main menu."
umount /tmp/usb > /dev/null 2>&1
return
fi
echo -e ""
read -ep "Enter the firmware filename: " firmware_file
firmware_file=/tmp/usb/${firmware_file}
if [ ! -f ${firmware_file} ]; then
echo_red "Invalid filename entered; unable to restore stock firmware."
read -ep "Press [Enter] to return to the main menu."
umount /tmp/usb > /dev/null 2>&1
return
fi
#text spacing
echo -e ""
else
if [[ "$hasShellball" = false ]]; then
exit_red "\nUnfortunately I don't have a stock firmware available to download for '${device^^}' at this time."
return 1
fi
#download firmware extracted from recovery image
echo_yellow "\nThat's ok, I'll download a shellball firmware for you."
if [ "${device^^}" = "PANTHER" ]; then
echo -e "Which device do you have?\n"
echo "1) Asus CN60 [PANTHER]"
echo "2) HP CB1 [ZAKO]"
echo "3) Dell 3010 [TRICKY]"
echo "4) Acer CXI [MCCLOUD]"
echo "5) LG Chromebase [MONROE]"
echo ""
read -ep "? " fw_num
if [[ $fw_num -lt 1 || $fw_num -gt 5 ]]; then
exit_red "Invalid input - cancelling"
return 1
fi
#confirm menu selection
echo -e ""
read -ep "Confirm selection number ${fw_num} [y/N] "
[[ "$REPLY" = "y" || "$REPLY" = "Y" ]] || { exit_red "User cancelled restoring stock firmware"; return; }
#download firmware file
echo -e ""
echo_yellow "Downloading recovery image firmware file"
case "$fw_num" in
1) _device="panther";
;;
2) _device="zako";
;;
3) _device="tricky";
;;
4) _device="mccloud";
;;
5) _device="monroe";
;;
esac
else
#confirm device detection
echo_yellow "Confirm system details:"
echo -e "Device: ${deviceDesc}"
echo -e "Board Name: ${device^^}"
echo -e ""
read -ep "? [y/N] "
if [[ "$REPLY" != "y" && "$REPLY" != "Y" ]]; then
exit_red "Device detection failed; unable to restoring stock firmware"
return 1
fi
echo -e ""
_device=${device}
fi
#download shellball ROM
curl -s -L -o /tmp/stock-firmware.rom ${shellball_source}shellball.${_device}.bin;
[[ $? -ne 0 ]] && { exit_red "Error downloading; unable to restore stock firmware."; return 1; }
#extract VPD if present
if [[ "$hasLAN" = true ]]; then
#extract VPD from current firmware
extract_vpd /tmp/bios.bin
#merge with recovery image firmware
if [ -f /tmp/vpd.bin ]; then
echo_yellow "Merging VPD into recovery image firmware"
${cbfstoolcmd} /tmp/stock-firmware.rom write -r RO_VPD -f /tmp/vpd.bin > /dev/null 2>&1
fi
fi
firmware_file=/tmp/stock-firmware.rom
fi
#disable software write-protect
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
#if [[ $? -ne 0 && ( "$isBraswell" = false || "$isFullRom" = false ) ]]; then
exit_red "Error disabling software write-protect; unable to restore stock firmware."; return 1
fi
#clear SW WP range
${flashromcmd} --wp-range 0 0 > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error clearing software write-protect range; unable to restore stock firmware."; return 1
fi
#flash stock firmware
echo_yellow "Restoring stock firmware"
${flashromcmd} -w ${firmware_file} > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "An error occurred restoring the stock firmware. DO NOT REBOOT!"; return 1; }
#all good
echo_green "Stock firmware successfully restored."
echo_green "After rebooting, you will need to restore ChromeOS using the ChromeOS recovery media,
then re-run this script to reset the Firmware Boot Flags (GBB Flags) to factory default."
read -ep "Press [Enter] to return to the main menu."
#set vars to indicate new firmware type
isStock=true
isFullRom=false
firmwareType="Stock ChromeOS (pending reboot)"
}
########################
# Extract firmware VPD #
########################
function extract_vpd()
{
#check params
[[ -z "$1" ]] && { exit_red "Error: extract_vpd(): missing function parameter"; return 1; }
firmware_file="$1"
#check if file contains MAC address
grep -obUa "ethernet_mac" ${firmware_file} >/dev/null
if [ $? -eq 0 ]; then
#try FMAP extraction
${cbfstoolcmd} ${firmware_file} read -r RO_VPD -f /tmp/vpd.bin >/dev/null 2>&1
if [ $? -ne 0 ]; then
#try CBFS extraction
${cbfstoolcmd} ${firmware_file} extract -n vpd.bin -f /tmp/vpd.bin >/dev/null 2>&1
if [ $? -ne 0 ]; then
echo_red "Failure extracting MAC address from current firmware."
return 1
fi
fi
else
#file doesn't contain VPD
return 1
fi
return 0
}
#########################
# Backup stock firmware #
#########################
function backup_firmware()
{
echo -e ""
read -ep "Connect the USB/SD device to store the firmware backup and press [Enter]
to continue. This is non-destructive, but it is best to ensure no other
USB/SD devices are connected. "
list_usb_devices
if [ $? -ne 0 ]; then
backup_fail "No USB devices available to store firmware backup."
return 1
fi
read -ep "Enter the number for the device to be used for firmware backup: " usb_dev_index
if [ $usb_dev_index -le 0 ] || [ $usb_dev_index -gt $num_usb_devs ]; then
backup_fail "Error: Invalid option selected."
return 1
fi
usb_device="/dev/sd${usb_devs[${usb_dev_index}-1]}"
mkdir /tmp/usb > /dev/null 2>&1
mount "${usb_device}" /tmp/usb > /dev/null 2>&1
if [ $? != 0 ]; then
mount "${usb_device}1" /tmp/usb
fi
if [ $? -ne 0 ]; then
backup_fail "USB backup device failed to mount; cannot proceed."
return 1
fi
backupname="stock-firmware-${boardName}-$(date +%Y%m%d).rom"
echo_yellow "\nSaving firmware backup as ${backupname}"
cp /tmp/bios.bin /tmp/usb/${backupname}
if [ $? -ne 0 ]; then
backup_fail "Failure reading stock firmware for backup; cannot proceed."
return 1
fi
sync
umount /tmp/usb > /dev/null 2>&1
rmdir /tmp/usb
echo_green "Firmware backup complete. Remove the USB stick and press [Enter] to continue."
read -ep ""
}
function backup_fail()
{
umount /tmp/usb > /dev/null 2>&1
rmdir /tmp/usb > /dev/null 2>&1
exit_red "\n$@"
}
####################
# Set Boot Options #
####################
function set_boot_options()
{
# set boot options via firmware boot flags
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot set Boot Options / GBB Flags."; return 1; }
[[ -z "$1" ]] && legacy_text="Legacy Boot" || legacy_text="$1"
echo_green "\nSet Firmware Boot Options (GBB Flags)"
echo_yellow "Select your preferred boot delay and default boot option.
You can always override the default using [CTRL+D] or
[CTRL+L] on the Developer Mode boot screen"
echo -e "1) Short boot delay (1s) + ${legacy_text} default
2) Long boot delay (30s) + ${legacy_text} default
3) Short boot delay (1s) + ChromeOS default
4) Long boot delay (30s) + ChromeOS default
5) Reset to factory default
6) Cancel/exit
"
local _flags=0x0
while :
do
read -ep "? " n
case $n in
1) _flags=0x4A9; break;;
2) _flags=0x4A8; break;;
3) _flags=0xA9; break;;
4) _flags=0xA8; break;;
5) _flags=0x0; break;;
6) read -ep "Press [Enter] to return to the main menu."; break;;
*) echo -e "invalid option";;
esac
done
[[ $n -eq 6 ]] && return
echo_yellow "\nSetting boot options..."
#disable software write-protect
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error disabling software write-protect; unable to set GBB flags."; return 1
fi
${flashromcmd} -r -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError reading firmware (non-stock?); unable to set boot options."; return 1; }
${gbbutilitycmd} --set --flags="${_flags}" /tmp/gbb.temp > /dev/null
[[ $? -ne 0 ]] && { exit_red "\nError setting boot options."; return 1; }
${flashromcmd} -w -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError writing back firmware; unable to set boot options."; return 1; }
echo_green "\nFirmware Boot options successfully set."
read -ep "Press [Enter] to return to the main menu."
}
###################
# Set Hardware ID #
###################
function set_hwid()
{
# set HWID using gbb_utility
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot set HWID."; return 1; }
echo_green "Set Hardware ID (HWID) using gbb_utility"
#get current HWID
_hwid="$(crossystem hwid)" >/dev/null 2>&1
if [ $? -eq 0 ]; then
echo_yellow "Current HWID is $_hwid"
fi
read -ep "Enter a new HWID (use all caps): " hwid
echo -e ""
read -ep "Confirm changing HWID to $hwid [y/N] " confirm
if [[ "$confirm" = "Y" || "$confirm" = "y" ]]; then
echo_yellow "\nSetting hardware ID..."
#disable software write-protect
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error disabling software write-protect; unable to set HWID."; return 1
fi
${flashromcmd} -r -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError reading firmware (non-stock?); unable to set HWID."; return 1; }
${gbbutilitycmd} --set --hwid="${hwid}" /tmp/gbb.temp > /dev/null
[[ $? -ne 0 ]] && { exit_red "\nError setting HWID."; return 1; }
${flashromcmd} -w -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError writing back firmware; unable to set HWID."; return 1; }
echo_green "Hardware ID successfully set."
fi
read -ep "Press [Enter] to return to the main menu."
}
##################
# Remove Bitmaps #
##################
function remove_bitmaps()
{
# remove bitmaps from GBB using gbb_utility
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot remove bitmaps."; return 1; }
echo_green "\nRemove ChromeOS Boot Screen Bitmaps"
read -ep "Confirm removing ChromeOS bitmaps? [y/N] " confirm
if [[ "$confirm" = "Y" || "$confirm" = "y" ]]; then
echo_yellow "\nRemoving bitmaps..."
#disable software write-protect
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error disabling software write-protect; unable to remove bitmaps."; return 1
fi
${flashromcmd} -r -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError reading firmware (non-stock?); unable to remove bitmaps."; return 1; }
touch /tmp/null-images > /dev/null 2>&1
${gbbutilitycmd} --set --bmpfv=/tmp/null-images /tmp/gbb.temp > /dev/null
[[ $? -ne 0 ]] && { exit_red "\nError removing bitmaps."; return 1; }
${flashromcmd} -w -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError writing back firmware; unable to remove bitmaps."; return 1; }
echo_green "ChromeOS bitmaps successfully removed."
fi
read -ep "Press [Enter] to return to the main menu."
}
##################
# Restore Bitmaps #
##################
function restore_bitmaps()
{
# restore bitmaps from GBB using gbb_utility
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot restore bitmaps."; return 1; }
echo_green "\nRestore ChromeOS Boot Screen Bitmaps"
read -ep "Confirm restoring ChromeOS bitmaps? [y/N] " confirm
if [[ "$confirm" = "Y" || "$confirm" = "y" ]]; then
echo_yellow "\nRestoring bitmaps..."
#disable software write-protect
${flashromcmd} --wp-disable > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "Error disabling software write-protect; unable to restore bitmaps."; return 1
fi
#download shellball
curl -s -L -o /tmp/shellball.rom ${shellball_source}shellball.${device}.bin;
[[ $? -ne 0 ]] && { exit_red "Error downloading shellball; unable to restore bitmaps."; return 1; }
#extract GBB region, bitmaps
${cbfstoolcmd} /tmp/shellball.rom read -r GBB -f gbb.new >/dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "Error extracting GBB region from shellball; unable to restore bitmaps."; return 1; }
${flashromcmd} -r -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError reading firmware (non-stock?); unable to restore bitmaps."; return 1; }
${gbbutilitycmd} --get --bmpfv=/tmp/bmpfv /tmp/gbb.new > /dev/null
${gbbutilitycmd} --set --bmpfv=/tmp/bmpfv /tmp/gbb.temp > /dev/null
[[ $? -ne 0 ]] && { exit_red "\nError restoring bitmaps."; return 1; }
${flashromcmd} -w -i GBB:/tmp/gbb.temp > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "\nError writing back firmware; unable to restore bitmaps."; return 1; }
echo_green "ChromeOS bitmaps successfully restored."
fi
read -ep "Press [Enter] to return to the main menu."
}
####################
# Modify BOOT_STUB #
####################
function modify_boot_stub()
{
# backup BOOT_STUB into RW_LEGACY
# modify BOOT_STUB for legacy booting
# flash back modified slots
#check baytrail
[[ "$isBaytrail" = false ]] && { exit_red "\nThis functionality is only available for Baytrail ChromeOS devices currently"; return 1; }
echo_green "\nInstall/Update BOOT_STUB Firmware (Legacy BIOS)"
echo_yellow "Standard disclaimer: flashing the firmware has the potential to
brick your device, requiring relatively inexpensive hardware and some
technical knowledge to recover. You have been warned."
echo_yellow "Also, flashing the BOOT_STUB will remove the ability to run ChromeOS,
so only proceed if you're going to run Linux exclusively."
read -ep "Do you wish to continue? [y/N] "
[[ "$REPLY" = "Y"|| "$REPLY" = "y" ]] || return
# ensure hardware write protect disabled
[[ "$wpEnabled" = true ]] && { exit_red "\nHardware write-protect enabled, cannot flash/modify BOOT_STUB firmware."; return 1; }
# cd to working dir
cd /tmp
#download SeaBIOS payload
curl -s -L -O ${bootstub_source}/${bootstub_payload_baytrail}
curl -s -L -O ${bootstub_source}/${bootstub_payload_baytrail}.md5
#verify checksum on downloaded file
md5sum -c ${bootstub_payload_baytrail}.md5 --quiet > /dev/null 2>&1
[[ $? -ne 0 ]] && { exit_red "SeaBIOS payload download checksum fail; download corrupted, cannot flash."; return 1; }
#read BOOT_STUB and RW_LEGACY slots
echo_yellow "\nReading current firmware"
${flashromcmd} -r -i BOOT_STUB:boot_stub.bin > /dev/null 2>&1
rc0=$?
${flashromcmd} -r -i RW_LEGACY:rw_legacy.bin > /dev/null 2>&1
rc1=$?
[[ $rc0 -ne 0 || $rc1 -ne 0 ]] && { exit_red "Error reading current firmware, unable to flash."; return 1; }
#if BOOT_STUB is stock
${cbfstoolcmd} boot_stub.bin extract -n fallback/vboot -f whocares -m x86 > /dev/null 2>&1
if [[ "$isChromeOS" = true || $? -eq 0 ]]; then
#copy BOOT_STUB into top 1MB of RW_LEGACY
echo_yellow "Backing up stock BOOT_STUB"
dd if=boot_stub.bin of=rw_legacy.bin bs=1M conv=notrunc > /dev/null 2>&1
#flash back
${flashromcmd} -w -i RW_LEGACY:rw_legacy.bin > /dev/null 2>&1
else
echo_yellow "Non-stock BOOT_STUB, skipping backup"
fi
#USB boot priority
read -ep "Default to booting from USB? If N, always boot from internal storage unless selected from boot menu. [y/N] "
if [[ "$REPLY" = "y" || "$REPLY" = "Y" ]]; then
curl -s -L -o bootorder ${cbfs_source}/bootorder.usb
else
curl -s -L -o bootorder ${cbfs_source}/bootorder.emmc
fi
#modify BOOT_STUB for legacy booting
echo_yellow "\nModifying BOOT_STUB for legacy boot"
${cbfstoolcmd} boot_stub.bin remove -n fallback/payload > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n fallback/vboot > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n bootorder > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/boot-menu-wait > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard0 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard1 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard2 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard3 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard4 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard5 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin remove -n etc/sdcard6 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-payload -n fallback/payload -f ${bootstub_payload_baytrail} -c lzma > /dev/null 2>&1
if [ $? -ne 0 ]; then
exit_red "There was an error modifying the BOOT_STUB payload, nothing has been flashed."; return 1
else
${cbfstoolcmd} boot_stub.bin add -n bootorder -f bootorder -t raw > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 3000 -n etc/boot-menu-wait > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd071f000 -n etc/sdcard0 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd071d000 -n etc/sdcard1 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd071c000 -n etc/sdcard2 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd081f000 -n etc/sdcard3 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd081c000 -n etc/sdcard4 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd091f000 -n etc/sdcard5 > /dev/null 2>&1
${cbfstoolcmd} boot_stub.bin add-int -i 0xd091c000 -n etc/sdcard6 > /dev/null 2>&1
#flash modified BOOT_STUB back
echo_yellow "Flashing modified BOOT_STUB firmware"
${flashromcmd} -w -i BOOT_STUB:boot_stub.bin > /dev/null 2>&1
if [ $? -ne 0 ]; then
#flash back stock BOOT_STUB
dd if=rw_legacy.bin of=boot_stub.bin bs=1M count=1 > /dev/null 2>&1
${flashromcmd} -w -i BOOT_STUB:boot_stub.bin > /dev/null 2>&1
echo_red "There was an error flashing the modified BOOT_STUB, but the stock one has been restored."
else
echo_green "BOOT_STUB firmware successfully flashed"
fi
fi
read -ep "Press [Enter] to return to the main menu."
}