1 | #!/bin/bash␊ |
2 | ␊ |
3 | echo "==============================================="␊ |
4 | echo "Check Previous Chameleon: Will there be problems?"␊ |
5 | echo "***********************************************"␊ |
6 | ␊ |
7 | # Checks the selected volume is present and the disk is partitioned␊ |
8 | # Now also check for another existing Chameleon installation on the same disk.␊ |
9 | ␊ |
10 | # Receives targetDisk: for example, /dev/disk3.␊ |
11 | # Receives targetDeviceRaw: for example, /dev/rdisk3s1.␊ |
12 | # Receives targetDevice: Stores device number, for example /dev/disk2s1.␊ |
13 | # Receives installerVolume: Volume to write the installer log to.␊ |
14 | # Receives scriptDir: The location of the main script dir.␊ |
15 | ␊ |
16 | if [ "$#" -eq 5 ]; then␊ |
17 | ␉targetDisk="$1"␊ |
18 | ␉targetDeviceRaw="$2"␊ |
19 | ␉targetDevice="$3"␊ |
20 | ␉installerVolume="$4"␊ |
21 | ␉scriptDir="$5"␊ |
22 | ␉echo "DEBUG: passed argument for targetDisk = $targetDisk"␊ |
23 | ␉echo "DEBUG: passed argument for targetDeviceRaw = $targetDeviceRaw"␊ |
24 | ␉echo "DEBUG: passed argument for targetDevice = $targetDevice"␊ |
25 | ␉echo "DEBUG: passed argument for installerVolume = $installerVolume"␊ |
26 | ␉echo "DEBUG: passed argument for scriptDir = $scriptDir"␊ |
27 | else␊ |
28 | ␉echo "Error - wrong number of values passed"␊ |
29 | ␉exit 9␊ |
30 | fi␊ |
31 | ␊ |
32 | ␊ |
33 | # Check for existing Chameleon installations on a different␊ |
34 | # partition of the same target disk.␊ |
35 | #echo "DEBUG: Checking for existing Chameleon installations on ${targetDisk#/dev/}..."␊ |
36 | ␊ |
37 | sliceNumber=$( echo ${targetDeviceRaw#*disk*s} )␊ |
38 | # strip slice from end␊ |
39 | targetDiskRawNoSlice=$( echo ${targetDeviceRaw%$sliceNumber} )␊ |
40 | ␊ |
41 | # Are there any other partitions on the disk?␊ |
42 | # How many actual partitions are there?␊ |
43 | numSlices=$(( $( diskutil list | grep $( echo ${targetDisk#/dev/} ) | sed -n '$=' ) -2 ))␊ |
44 | ␊ |
45 | # Only check the disk for Chameleon installations if there is more than one partition.␊ |
46 | if [ $numSlices -gt 1 ]; then ␊ |
47 | ␉␉␊ |
48 | ␉#Scan all partitions for Chameleon code␊ |
49 | ␉for (( i=1; i <= $numSlices; i++ ));␊ |
50 | ␉do␊ |
51 | ␉␉stageExistence=0␊ |
52 | ␉␉stage0FirstBootable=0␊ |
53 | ␉␉previousExistence="NONE"␊ |
54 | ␉␉targetDiskRaw=$targetDiskRawNoSlice$i␊ |
55 | ␊ |
56 | ␉␉# Check for existing stage 0 boot file (same code as CheckDiskMicrocode.sh script)␊ |
57 | ␉␉stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )␊ |
58 | ␉␉if [ "${stage0type}" == "0b807c" ] || [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then␊ |
59 | ␉␉␉#echo "DEBUG: boot0 found on $targetDisk"␊ |
60 | ␉␉␉(( stageExistence++ ))␊ |
61 | ␉␉␉␊ |
62 | ␉␉␉# While here, check just for either existing boot0hfs, boot0md or boot0md (dmazar's boot0workV2)␊ |
63 | ␉␉␉if [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then␊ |
64 | ␉␉␉␉stage0FirstBootable=1␊ |
65 | ␉␉␉fi␊ |
66 | ␉␉fi␊ |
67 | ␉␉␉␊ |
68 | ␉␉# Check for existence of a bootable partition boot sector containing either boot1h or boot1f32␊ |
69 | ␉␉boot1Search=$( dd 2>/dev/null if="$targetDiskRaw" count=1 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )␊ |
70 | ␉␉if [ "${boot1Search:0:16}" == "fa31c08ed0bcf0ff" ] && [ "${boot1Search:1020:2}" == "55" ]; then␊ |
71 | ␉␉␉#echo "DEBUG: boot1h found on "$targetDiskRaw␊ |
72 | ␉␉␉(( stageExistence++ ))␊ |
73 | ␉␉␉previousExistence="boot1"␊ |
74 | ␉␉elif [ "${boot1Search:0:4}" == "e962" ] && [ "${boot1Search:180:12}" == "424f4f542020" ] && [ "${boot1Search:1020:2}" == "55" ]; then␊ |
75 | ␉␉␉#echo "DEBUG: boot1f32 found on "$targetDiskRaw␊ |
76 | ␉␉␉(( stageExistence++ ))␊ |
77 | ␉␉␉previousExistence="boot1f32"␊ |
78 | ␉␉fi␊ |
79 | ␉␉␊ |
80 | ␉␉# Check for existing stage 2 boot file also␊ |
81 | ␉␉# NOTE: This will fail to find /boot on /Volumes/EFI as it won't be mounted!␊ |
82 | ␉␉if [ -e "$( df | grep ${targetDisk}s${i} | awk '{ print $6 }' )"/boot ]; then␊ |
83 | ␉␉␉#echo "DEBUG: boot found on $targetDiskRaw"␊ |
84 | ␉␉␉(( stageExistence++ ))␊ |
85 | ␉␉fi␊ |
86 | ␉␉␊ |
87 | ␉␉#if [ $stageExistence == 3 ] && [ $i -ne $sliceNumber ]; then␊ |
88 | ␉␉␉#echo "DEBUG: STOP: There is already an existing Chameleon installation on $targetDiskRaw"␊ |
89 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "STOP: There is already an existing Chameleon installation on $targetDiskRaw."␊ |
90 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: This is allowed and does work as long as you aren't dual booting Windows"␊ |
91 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: from the same disk and are happy to control which partition is used by"␊ |
92 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: flagging the required partition active. General use doesn't require two"␊ |
93 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: Chameleon installs on the same disk, though might be done by advanced users."␊ |
94 | ␉␉␉#"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: If you still want to do this then proceed by installing Chameleon manually."␊ |
95 | ␉␉␉#exit 1␊ |
96 | ␉␉#fi␊ |
97 | ␉␉␉␊ |
98 | ␉␉if [ $stageExistence -ge 2 ] && [ "$previousExistence" != "NONE" ] && [ $i -ne $sliceNumber ]; then␊ |
99 | ␉␉␉# There is previous Chameleon stage 1 code on a partition boot sector,␊ |
100 | ␉␉␉# and either a complete or incomplete installation (ie. boot0 or boot are missing).␊ |
101 | ␉␉␉if [ $stageExistence == 3 ]; then␊ |
102 | ␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "WARN: There is already an existing Chameleon installation on $targetDiskRaw."␊ |
103 | ␉␉␉else␊ |
104 | ␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: $previousExistence already exists at ${targetDisk}s${i}"␊ |
105 | ␉␉␉fi␊ |
106 | ␉␉␉␊ |
107 | ␉␉␉# This could prove problematic and result in either a b1f:error or boot0:error ␊ |
108 | ␉␉␉# if the following conditions are true:␊ |
109 | ␉␉␉# A) Boot0hfs, Boot0md or Boot0md (dmazar's Boot0workV2) is being used.␊ |
110 | ␉␉␉# B) The previous stage 1 code is on a lower partiton than the one being installed to now.␊ |
111 | ␉␉␉␉␉␉␊ |
112 | ␉␉␉# stage0FirstBootable=1 is used to know if 'A' is true.␊ |
113 | ␉␉␉if [ $stage0FirstBootable == 1 ]; then␊ |
114 | ␉␉␉␉# i = current slice we're checking, slicenumber = slice trying to install to.␊ |
115 | ␉␉␉␉if [ $i -lt $sliceNumber ]; then␊ |
116 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "WARN: Conditions point to you receiving a boot failure"␊ |
117 | ␉␉␉␉␉# Fix by making previous paritionboot sector un-bootable␊ |
118 | ␉␉␉␉␉# Change Byte 01FExh to 00 (510 decimal)␊ |
119 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "---"␊ |
120 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "FIX: Make ${targetDisk}s${i} boot sector un-bootable by changing byte 1FEh to 00."␊ |
121 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: Any Extra folder you had there will still be there. If you want to use"␊ |
122 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: ${targetDisk}s${i} again as your boot partition then re-run this installer"␊ |
123 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: selecting it as the target, ONLY choosing the 'Chameleon Bootloader' option"␊ |
124 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: and NONE of the other options."␊ |
125 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "---"␊ |
126 | ␉␉␉␉␉dd if=${targetDisk}s${i} count=1 bs=512 of=/tmp/originalBootSector␊ |
127 | ␉␉␉␉␉cp /tmp/originalBootSector /tmp/newBootSector␊ |
128 | ␉␉␉␉␉dd if="$scriptDir/patch" of=/tmp/newBootSector bs=1 count=1 seek=510 conv=notrunc␊ |
129 | ␉␉␉␉␉dd if=/tmp/newBootSector of=${targetDisk}s${i} count=1 bs=512␊ |
130 | ␉␉␉␉else␊ |
131 | ␉␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: but won't interfere as you're installing to an earlier partition."␊ |
132 | ␉␉␉␉fi␊ |
133 | ␉␉␉else␊ |
134 | ␉␉␉␉"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: so select to boot that partition (if used) with active flag."␊ |
135 | ␉␉␉fi␊ |
136 | ␉␉fi␊ |
137 | ␉done␊ |
138 | ␊ |
139 | #else␊ |
140 | ␉#echo "DEBUG: Just one slice"␊ |
141 | fi␊ |
142 | ␊ |
143 | exit 0 |