1 | #!/bin/bash␊ |
2 | ␊ |
3 | echo "==============================================="␊ |
4 | echo "CheckDiskMicrocode: Any existing bootloaders?"␊ |
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 0 to indicate that Chameleon stage0␊ |
15 | # loader can be written, or 1 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 | else␊ |
38 | ␉# There is already something on the MBR ␊ |
39 | ␊ |
40 | ␉# See if a Windows bootloader already exists␊ |
41 | ␉# Check bytes 440-443 of the GPTdiskProtectiveMBR for a Windows Disk Signature␊ |
42 | ␉windowsloader=$( dd 2>/dev/null if="$targetDisk" count=4 bs=1 | xxd | awk '{print $2$3}' )␊ |
43 | ␉if [ "${windowsloader}" == "33c08ed0" ] ; then␊ |
44 | ␉␉echo "Found existing Windows Boot Loader so will replace with Chameleon Boot0hfs"␊ |
45 | ␉fi␊ |
46 | ␊ |
47 | ␉# See if a Chameleon stage0 boot file already exists␊ |
48 | ␊ |
49 | ␉# Note: The checks for Boot0 and Boot0hfs assume the code stays the same.␊ |
50 | ␉# if the code changes then the hex values 0b807c and 0a803c used for matching␊ |
51 | ␉# need to be checked to see if they are the same or not.␊ |
52 | ␊ |
53 | ␉stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )␊ |
54 | ␉echo ${stage0type}␊ |
55 | ␉if [ "${stage0type}" == "0b807c" ]; then␊ |
56 | ␉␉echo "Found existing Chameleon stage 0 loader - Boot0hfs"␊ |
57 | ␊ |
58 | ␉␉# Script CheckDiskSignature.sh returned 0 if a Windows installation was NOT found␊ |
59 | ␉␉if [ "$diskSigCheck" == "0" ]; then␊ |
60 | ␉␉␉echo "Found no existing Windows installation so will replace stage 0 loader with Boot0"␊ |
61 | ␉␉fi␊ |
62 | ␉fi␊ |
63 | ␊ |
64 | ␉if [ "${stage0type}" == "0a803c" ]; then␊ |
65 | ␉␉echo "Found existing Chameleon stage 0 loader - Boot0"␊ |
66 | ␊ |
67 | ␉␉# Script CheckDiskSignature.sh returned 1 if a Windows installation was found␊ |
68 | ␉␉if [ "$diskSigCheck" = "1" ]; then␊ |
69 | ␉␉␉echo "Found existing Windows installation so will replace stage 0 loader with Boot0hfs"␊ |
70 | ␉␉fi␊ |
71 | ␉fi␊ |
72 | ␊ |
73 | ␉if [ "${stage0type}" != "0b807c" ] && [ "${stage0type}" != "0a803c" ] && [ "${windowsloader}" != "33c08ed0" ] ; then␊ |
74 | ␉␉echo "Something other than Chameleon or a Windows bootloader was found"␊ |
75 | ␉␉test=$(echo "${mbr437}" | awk -F0 '{print NF-1}' )␊ |
76 | ␉␉echo "Disk microcode found: ${test} - Preserving."␊ |
77 | ␉␉echo "diskupdate is set to false"␊ |
78 | ␉␉echo "-----------------------------------------------"␊ |
79 | ␉␉echo ""␊ |
80 | ␉␉exit 1␊ |
81 | ␉fi␊ |
82 | fi␊ |
83 | ␊ |
84 | echo "diskupdate is now set to true."␊ |
85 | echo "-----------------------------------------------"␊ |
86 | echo ""␊ |
87 | ␊ |
88 | exit 0␊ |
89 | |