1 | #!/bin/bash␊ |
2 | ␊ |
3 | echo "==============================================="␊ |
4 | echo "CheckDiskMicrocode: Any existing bootloaders?"␊ |
5 | echo "*********************************************"␊ |
6 | ␊ |
7 | # Reads the GPTdiskProtectiveMBR and searches for an existing␊ |
8 | # Windows bootloader and also for an existing Chameleon stage 0 loader␊ |
9 | # which might be better changed depending on whether or not a Windows␊ |
10 | # signature is found or not.␊ |
11 | # The script then exits with the value 0 to indicate that Chameleon stage0␊ |
12 | # loader can be written, or 1 to indicate not to write the stage0 loader.␊ |
13 | ␊ |
14 | # Receives targetDisk: for example, /dev/disk2.␊ |
15 | # Receives diskSigCheck: 0 = Windows not installed / 1 = Windows installed.␊ |
16 | # Receives targetVolume: Volume to install to.␊ |
17 | # Receives scriptDir: The location of the main script dir.␊ |
18 | ␊ |
19 | ␊ |
20 | if [ "$#" -eq 4 ]; then␊ |
21 | ␉targetDisk="$1"␊ |
22 | ␉diskSigCheck="$2"␊ |
23 | ␉targetVolume="$3"␊ |
24 | ␉scriptDir="$4"␊ |
25 | ␉echo "DEBUG: passed argument for targetDisk = $targetDisk"␊ |
26 | ␉echo "DEBUG: passed argument for diskSigCheck = $diskSigCheck"␊ |
27 | ␉echo "DEBUG: passed argument for targetVolume = $targetVolume"␊ |
28 | ␉echo "DEBUG: passed argument for scriptDir = $scriptDir"␊ |
29 | else␊ |
30 | ␉echo "Error - wrong number of values passed - Exiting"␊ |
31 | ␉exit 9␊ |
32 | fi␊ |
33 | ␊ |
34 | ␊ |
35 | # read the first 437 bytes of the MBR␊ |
36 | ␊ |
37 | mbr437=$( dd 2>/dev/null if="$targetDisk" count=1 | dd 2>/dev/null count=1 bs=437 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )␊ |
38 | #mbr437md5=$( dd 2>/dev/null if="$targetDisk" count=1 | dd 2>/dev/null count=1 bs=437 | md5 )␊ |
39 | ␊ |
40 | if [ $( echo "${mbr437}" | awk -F0 '{print NF-1}' ) != 874 ]; then␊ |
41 | ␉# There is already something on the MBR ␊ |
42 | ␊ |
43 | ␉# See if a Windows bootloader already exists␊ |
44 | ␉# Check bytes 440-443 of the GPTdiskProtectiveMBR for a Windows Disk Signature␊ |
45 | ␉windowsloader=$( dd 2>/dev/null if="$targetDisk" count=4 bs=1 | xxd | awk '{print $2$3}' )␊ |
46 | ␉if [ "${windowsloader}" == "33c08ed0" ] ; then␊ |
47 | ␉␉#echo "DEBUG: Found existing Windows Boot Loader so will replace with Chameleon boot0md"␊ |
48 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Target disk has existing Windows boot loader - Will replace with boot0md (boot0workV2)."␊ |
49 | ␉fi␊ |
50 | ␊ |
51 | ␉# See if a Chameleon stage0 boot file already exists␊ |
52 | ␊ |
53 | ␉# Note: The checks for Boot0, Boot0hfs, Boot0md and Boot0md (dmazar's Boot0workV2) assume the code stays the same.␊ |
54 | ␉# if the code changes then the hex values 0b807c, 0a803c, ee7505 and 742b80 used for matching␊ |
55 | ␉# need to be checked to see if they are the same or not.␊ |
56 | ␊ |
57 | ␉stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )␊ |
58 | ␉if [ "${stage0type}" == "0b807c" ]; then␊ |
59 | ␉␉#echo "DEBUG: Target has existing Chameleon stage 0 loader - Boot0hfs"␊ |
60 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Target disk already has existing Chameleon stage 0 loader - boot0hfs"␊ |
61 | ␊ |
62 | ␉␉# Script CheckDiskSignature.sh returned 0 if a Windows installation was NOT found␊ |
63 | ␉␉if [ "$diskSigCheck" == "0" ]; then␊ |
64 | ␉␉␉#echo "DEBUG: Found no existing Windows installation so will replace stage 0 loader with Boot0"␊ |
65 | ␉␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Will replace boot0hfs with boot0 as Windows is not on target disk."␊ |
66 | ␉␉␉exit 0␊ |
67 | ␉␉fi␊ |
68 | ␉␉exit 1␊ |
69 | ␉fi␊ |
70 | ␊ |
71 | ␉if [ "${stage0type}" == "0a803c" ]; then␊ |
72 | ␉␉#echo "DEBUG: Found existing Chameleon stage 0 loader - Boot0"␊ |
73 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Target disk already has existing Chameleon stage 0 loader - boot0"␊ |
74 | ␊ |
75 | ␉␉# Script CheckDiskSignature.sh returned 1 if a Windows installation was found␊ |
76 | ␉␉if [ "$diskSigCheck" = "1" ]; then␊ |
77 | ␉␉␉#echo "DEBUG: Found existing Windows installation so will replace stage 0 loader with boot0md"␊ |
78 | ␉␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Will replace boot0 with boot0md (boot0workV2) as Windows is on target disk."␊ |
79 | ␉␉␉exit 0␊ |
80 | ␉␉fi␊ |
81 | ␉␉exit 1␊ |
82 | ␉fi␊ |
83 | ␊ |
84 | ␉if [ "${stage0type}" == "ee7505" ]; then␊ |
85 | ␉␉#echo "DEBUG: Found existing Chameleon stage 0 loader - Boot0md"␊ |
86 | ␉␉#echo "DEBUG: And will leave boot0md installed."␊ |
87 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Target disk already has existing Chameleon stage 0 loader - boot0md (v1)."␊ |
88 | ␉␉exit 1␊ |
89 | ␉fi␊ |
90 | ␉␊ |
91 | ␉if [ "${stage0type}" == "742b80" ]; then␊ |
92 | ␉␉#echo "DEBUG: Found existing Chameleon stage 0 loader - Boot0workV2"␊ |
93 | ␉␉#echo "DEBUG: And will leave Boot0workV2 installed."␊ |
94 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "Target disk already has existing Chameleon stage 0 loader - boot0md (boot0workV2)."␊ |
95 | ␉␉exit 1␊ |
96 | ␉fi␊ |
97 | ␊ |
98 | ␉if [ "${stage0type}" != "0b807c" ] && [ "${stage0type}" != "0a803c" ] && [ "${stage0type}" != "ee7505" ] && [ "${stage0type}" != "742b80" ] && [ "${windowsloader}" != "33c08ed0" ] ; then␊ |
99 | ␉␉#echo "DEBUG: Something other than Chameleon or a Windows bootloader was found"␊ |
100 | ␉␉test=$(echo "${mbr437}" | awk -F0 '{print NF-1}' )␊ |
101 | ␉␉#echo "DEBUG: Disk microcode found: ${test} - Preserving."␊ |
102 | ␉␉#echo "DEBUG: diskupdate is set to false"␊ |
103 | ␉␉#echo "DEBUG: -----------------------------------------------"␊ |
104 | ␉␉"$scriptDir"InstallLog.sh "${targetVolume}" "NOTE: Target has existing unrecognised bootcode in the MBR. Leaving as is."␊ |
105 | ␉␉exit 1␊ |
106 | ␉fi␊ |
107 | #else␊ |
108 | ␉#echo "DEBUG: The first 437 bytes of the MBR Disk Sector is blank - Updating"␊ |
109 | ␉#"$scriptDir"InstallLog.sh "${targetVolume}" "Target has no bootcode in the MBR disk sector."␊ |
110 | fi␊ |
111 | ␊ |
112 | echo "diskupdate is now set to true."␊ |
113 | echo "-----------------------------------------------"␊ |
114 | echo ""␊ |
115 | ␊ |
116 | exit 0␊ |
117 | |