1 | #!/bin/bash␊ |
2 | ␊ |
3 | echo "==============================================="␊ |
4 | echo "Entering CheckDiskMicrocode:"␊ |
5 | echo "****************************"␊ |
6 | ␊ |
7 | # This script is passed the targetDisk and diskSigCheck.␊ |
8 | #␊ |
9 | # it then reads the GPTdiskProtectiveMBR and searches for an existing␊ |
10 | # Windows bootloader and also for an existing Chameleon stage 0 loader␊ |
11 | # which might be better changed depending on whether or not a Windows␊ |
12 | # signature is found or not.␊ |
13 | #␊ |
14 | # The script then exits with the value 1 to indicate that Chameleon stage0␊ |
15 | # loader can be written, or 0 to indicate not to write the stage0 loader.␊ |
16 | ␊ |
17 | if [ "$#" -eq 2 ]; then␊ |
18 | ␉targetDisk="$1"␊ |
19 | ␉diskSigCheck="$2"␊ |
20 | ␉echo "DEBUG: passed argument for targetDisk = $targetDisk"␊ |
21 | ␉echo "DEBUG: passed argument for diskSigCheck = $diskSigCheck"␊ |
22 | else␊ |
23 | ␉echo "Error - wrong number of values passed - Exiting"␊ |
24 | ␉exit 9␊ |
25 | fi␊ |
26 | ␊ |
27 | ␊ |
28 | # read the first 437 bytes of the MBR␊ |
29 | ␊ |
30 | 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}' )␊ |
31 | #mbr437md5=$( dd 2>/dev/null if="$targetDisk" count=1 | dd 2>/dev/null count=1 bs=437 | md5 )␊ |
32 | ␊ |
33 | #echo "DEBUG: ${mbr437}"␊ |
34 | ␊ |
35 | if [ $( echo "${mbr437}" | awk -F0 '{print NF-1}' ) = 874 ]; then␊ |
36 | ␉echo "The first 437 bytes of the MBR Disk Sector is blank - Updating"␊ |
37 | ␉echo "diskupdate is now set to true."␊ |
38 | ␉echo "-----------------------------------------------"␊ |
39 | ␉echo ""␊ |
40 | ␉exit 1␊ |
41 | else␊ |
42 | ␉# There is already something on the MBR ␊ |
43 | ␊ |
44 | ␉# See if a Windows bootloader already exists␊ |
45 | ␉# Check bytes 440-443 of the GPTdiskProtectiveMBR for a Windows Disk Signature␊ |
46 | ␉windowsloader=$( dd 2>/dev/null if="$targetDisk" count=4 bs=1 | xxd | awk '{print $2$3}' )␊ |
47 | ␉if [ "${windowsloader}" == "33c08ed0" ] ; then␊ |
48 | ␉␉echo "Found existing Windows Boot Loader so will replace with Chameleon Boot0hfs"␊ |
49 | ␉␉echo "diskupdate is now set to true."␊ |
50 | ␉␉echo "-----------------------------------------------"␊ |
51 | ␉␉echo ""␊ |
52 | ␉␉exit 1␊ |
53 | ␉fi␊ |
54 | ␊ |
55 | ␉# See if a Chameleon stage0 boot file already exists␊ |
56 | ␊ |
57 | ␉# Note: The checks for Boot0 and Boot0hfs assume the code stays the same.␊ |
58 | ␉# if the code changes then the hex values 0b807c and 0a803c used for matching␊ |
59 | ␉# need to be checked to see if they are the same or not.␊ |
60 | ␊ |
61 | ␉stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )␊ |
62 | ␉echo ${stage0type}␊ |
63 | ␉if [ "${stage0type}" == "0b807c" ]; then␊ |
64 | ␉␉echo "Found existing Chameleon stage 0 loader - Boot0hfs"␊ |
65 | ␊ |
66 | ␉␉# CheckDiskSignature.sh returns 0 if a Windows installation was NOT found␊ |
67 | ␉␉if [ "$diskSigCheck" == "0" ]; then␊ |
68 | ␉␉␉echo "Found no existing Windows installation so will replace stage 0 loader with Boot0"␊ |
69 | ␉␉␉echo "diskupdate is now set to true."␊ |
70 | ␉␉␉echo "-----------------------------------------------"␊ |
71 | ␉␉␉echo ""␊ |
72 | ␉␉␉exit 1␊ |
73 | ␉␉fi␊ |
74 | ␉fi␊ |
75 | ␊ |
76 | ␉if [ "${stage0type}" == "0a803c" ]; then␊ |
77 | ␉␉echo "Found existing Chameleon stage 0 loader - Boot0"␊ |
78 | ␊ |
79 | ␉␉# CheckDiskSignature.sh returns 1 if a Windows installation was found␊ |
80 | ␉␉if [ "$diskSigCheck" = "1" ]; then␊ |
81 | ␉␉␉echo "Found existing Windows installation so will replace stage 0 loader with Boot0hfs"␊ |
82 | ␉␉␉echo "diskupdate is now set to true."␊ |
83 | ␉␉␉echo "-----------------------------------------------"␊ |
84 | ␉␉␉echo ""␊ |
85 | ␉␉␉exit 1␊ |
86 | ␉␉fi␊ |
87 | ␉fi␊ |
88 | ␊ |
89 | ␉# If neither Chameleon Boot Loader or Windows loader exists␊ |
90 | ␉if [ "${stage0type}" != "0b807c" ] && [ "${stage0type}" != "0a803c" ] && [ "${windowsloader}" != "33c08ed0" ] ; then␊ |
91 | ␉␉test=$(echo "${mbr437}" | awk -F0 '{print NF-1}' )␊ |
92 | ␉␉echo "Disk microcode found: ${test} - Preserving."␊ |
93 | ␉␉echo "diskupdate is left at false"␊ |
94 | ␉␉echo "-----------------------------------------------"␊ |
95 | ␉␉echo ""␊ |
96 | ␉␉exit 0␊ |
97 | ␉fi␊ |
98 | fi␊ |
99 | ␊ |
100 | ␊ |
101 | echo "-----------------------------------------------"␊ |
102 | echo ""␊ |
103 | echo ""␊ |
104 | ␊ |
105 | exit 0␊ |
106 | |