Chameleon

Chameleon Svn Source Tree

Root/branches/blackosx/package/Scripts/Sub/CheckPreviousChameleon.sh

  • Property svn:executable set to *
1#!/bin/bash
2
3echo "==============================================="
4echo "Check Previous Chameleon: Will there be problems?"
5echo "***********************************************"
6
7# Checks for another existing Chameleon installation on the same disk
8# and tries to make sure the user doesn't end up with an un-bootable
9# system due to having installed Chameleon previously elsewhere.
10
11# Receives targetDisk: for example, /dev/disk3.
12# Receives targetDeviceRaw: for example, /dev/rdisk3s1.
13# Receives targetDevice: Stores device number, for example /dev/disk2s1.
14# Receives installerVolume: Volume to write the installer log to.
15# Receives partitiontable: for example, GUID_partition_scheme
16# Receives scriptDir: The location of the main script dir.
17
18if [ "$#" -eq 6 ]; then
19targetDisk="$1"
20targetDeviceRaw="$2"
21targetDevice="$3"
22installerVolume="$4"
23partitiontable="$5"
24scriptDir="$6"
25echo "DEBUG: passed argument for targetDisk = $targetDisk"
26echo "DEBUG: passed argument for targetDeviceRaw = $targetDeviceRaw"
27echo "DEBUG: passed argument for targetDevice = $targetDevice"
28echo "DEBUG: passed argument for installerVolume = $installerVolume"
29echo "DEBUG: passed argument for partitiontable = $partitiontable"
30echo "DEBUG: passed argument for scriptDir = $scriptDir"
31else
32echo "Error - wrong number of values passed"
33exit 9
34fi
35
36# ===============================================
37# Functions
38# ===============================================
39mountESP()
40{
41if [ "${partitiontable}" = "GUID_partition_scheme" ]; then
42
43# Unmount ALL mounted volumes named EFI
44# the following script returns 0 if it succeeds
45# the following script returns 1 if it fails to un-mount any EFI volume
46"$scriptDir"UnMountEFIvolumes.sh "${installerVolume}" "${scriptDir}"
47returnValue=$?
48if [ ${returnValue} = 0 ]; then
49# OK to proceed
50
51if [ ! -e "/Volumes/EFI" ]; then
52#echo "DEBUG: Executing Command: mkdir -p ${/Volumes/EFI}"
53mkdir -p "/Volumes/EFI"
54#else
55#echo "DEBUG: folder /Volumes/EFI already exists"
56fi
57
58# Mount '/Volumes/EFI' using the correct format type
59if [ "$( fstyp "${targetDisk}"s1 | grep hfs )" ]; then
60#echo "Executing command: mount_hfs ${targetDevice} ${targetVolume}"
61"$scriptDir"InstallLog.sh "${installerVolume}" "Mounting ${targetDisk}s1 as /Volumes/EFI"
62mount_hfs "${targetDisk}"s1 "/Volumes/EFI"
63fi
64if [ "$( fstyp "${targetDisk}"s1 | grep msdos )" ]; then
65#echo "Executing command: mount_msdos -u 0 -g 0 ${targetDevice} ${/Volumes/EFI}"
66"$scriptDir"InstallLog.sh "${installerVolume}" "Mounting ${targetDisk}s1 as /Volumes/EFI"
67mount_msdos -u 0 -g 0 "${targetDisk}"s1 "/Volumes/EFI"
68fi
69else
70# quit out and notify EFI post script not to write Chameleon files.
71exit 2
72fi
73fi
74}
75
76
77# ===============================================
78# Prepare some vars
79# ===============================================
80sliceNumber=$( echo ${targetDeviceRaw#*disk*s} )
81
82# strip slice from end
83targetDiskRawNoSlice=$( echo ${targetDeviceRaw%$sliceNumber} )
84
85# Are there any other partitions on the disk?
86# How many actual partitions are there?
87numSlices=$(( $( diskutil list | grep $( echo ${targetDisk#/dev/} ) | sed -n '$=' ) -2 ))
88
89
90# ===============================================
91# Checking the disk for existing Chameleon installations
92# if there is more than one partition on the disk.
93# ===============================================
94if [ $numSlices -gt 1 ]; then
95
96#echo "DEBUG: Checking for existing Chameleon installations on ${targetDisk#/dev/}..."
97"$scriptDir"InstallLog.sh "${installerVolume}" "LineBreak"
98"$scriptDir"InstallLog.sh "${installerVolume}" "Checking for previous chameleon installations on ${targetDisk#/dev/}"
99
100# If a GPT is used then we are going to have the check the EFI system partition
101# for the stage 2 'boot' file. As this script is called from the Main EFI
102# postinstall script, so lets mount the EFI system partition here instead.
103
104mountESP
105
106#Scan all partitions for Chameleon code
107for (( i=1; i <= $numSlices; i++ ));
108do
109stagesFound=0
110stage0type=0
111stage1Existence="NONE"
112stage2Existence=0
113targetDiskRaw=$targetDiskRawNoSlice$i
114
115# Check for existing stage 0 boot file (code from CheckDiskMicrocode.sh script)
116stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )
117if [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then
118#echo "DEBUG: stage 0 (hfs) loader found on $targetDisk"
119(( stagesFound++ ))
120stage0type=2
121elif [ "${stage0type}" == "0b807c" ]; then
122#echo "DEBUG: stage 0 (active) loader found on $targetDisk"
123(( stagesFound++ ))
124stage0type=1
125fi
126
127# Check for existence of a bootable partition boot sector containing either boot1h or boot1f32
128boot1Search=$( dd 2>/dev/null if="$targetDiskRaw" count=1 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
129if [ "${boot1Search:0:16}" == "fa31c08ed0bcf0ff" ] && [ "${boot1Search:1020:2}" == "55" ]; then
130#echo "DEBUG: boot1h found on "$targetDiskRaw
131(( stagesFound++ ))
132stage1Existence="boot1h"
133elif [ "${boot1Search:0:4}" == "e962" ] && [ "${boot1Search:180:12}" == "424f4f542020" ] && [ "${boot1Search:1020:2}" == "55" ]; then
134#echo "DEBUG: boot1f32 found on "$targetDiskRaw
135(( stagesFound++ ))
136stage1Existence="boot1f32"
137fi
138
139# Check for existing stage 2 boot file.
140# Include checking the EFI system partition if it exists and is mounted.
141if [ -e "$( df | grep ${targetDisk}s${i} | awk '{ print $6 }' )"/boot ]; then
142#echo "DEBUG: boot found on $targetDiskRaw"
143(( stagesFound++ ))
144stage2Existence=1
145fi
146
147if [ $stagesFound -ge 2 ] && [ "$stage1Existence" != "NONE" ] && [ $i -ne $sliceNumber ]; then
148# There is previous Chameleon stage 1 code on a partition boot sector,
149# and either a complete or incomplete installation (ie. boot0 or boot are missing).
150
151if [ $stagesFound == 3 ] && [ $i -lt $sliceNumber ]; then
152# Exisitng installation found which will still be default.
153message="************************** TAKE NOTE *****************************
154**** There is an existing Chameleon installation on $targetDiskRaw
155**** and that installation will still be the default loader as it's
156**** on an earlier partition. If you want this new installation on
157**** $installerVolume to be default then you will need to remove the
158**** boot file from $targetDiskRaw and re-run this installer.
159**************************************************************"
160"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
161fi
162if [ $stagesFound == 3 ] && [ $i -gt $sliceNumber ]; then
163# Exisitng installation found which will no longer be default.
164message="NOTE: There is an existing Chameleon installation on $targetDiskRaw
165NOTE: but this installation on $targetDevice will be the default loader
166NOTE: because you're installing to an earlier partition on the disk."
167"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
168fi
169
170# User could see a b1f:error or boot0:error if the following conditions are true:
171# A) Boot0hfs, Boot0md or Boot0md (dmazar's Boot0workV2) is being used.
172# B) The previous stage 1 code is on a lower partiton than the one being installed to now.
173# C) boot is missing from that partition.
174
175if [ $stagesFound == 2 ] && [ $stage2Existence == 0 ]; then
176# Exisitng boot0 and boot1 only found - missing boot
177"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: Found $stage1Existence installed to ${targetDisk}s${i}"
178
179# stage0type=2 is used to know if 'A' is true.
180if [ $stage0type == 2 ]; then
181# i = current slice we're checking, slicenumber = slice trying to install to.
182if [ $i -lt $sliceNumber ]; then
183"$scriptDir"InstallLog.sh "${installerVolume}" "WARN: Conditions point to the possibility of a boot failure"
184
185# Fix by making previous paritionboot sector un-bootable
186# Change Byte 01FExh to 00 (510 decimal)
187message="---
188FIX: Make ${targetDisk}s${i} boot sector un-bootable by changing byte 1FEh to 00.
189NOTE: Any Extra folder you had there will still be there. If you want to use
190NOTE: ${targetDisk}s${i} again as your boot partition then re-run this installer
191NOTE: selecting it as the target, ONLY choosing the 'Chameleon Bootloader' option
192NOTE: and NONE of the other options.
193---"
194"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
195
196if [ $i == 1 ]; then
197umount /Volumes/EFI
198fi
199
200if [ "$( fstyp "${targetDisk}"s1 | grep hfs )" ]; then
201#echo "DEBUG: HFS - changing byte 1FEh to 00"
202dd if=${targetDisk}s${i} count=2 bs=512 of=originalBootSector
203cp originalBootSector newBootSector
204dd if="patch" of=newBootSector bs=1 count=1 seek=510 conv=notrunc
205dd if=newBootSector of=${targetDisk}s${i} count=2 bs=510
206fi
207if [ "$( fstyp "${targetDisk}"s1 | grep msdos )" ]; then
208#echo "DEBUG: MSDOS - changing byte 1FEh to 00"
209dd if=${targetDisk}s${i} count=1 bs=512 of=/tmp/originalBootSector
210cp /tmp/originalBootSector /tmp/newBootSector
211dd if="$scriptDir/patch" of=/tmp/newBootSector bs=1 count=1 seek=510 conv=notrunc
212dd if=/tmp/newBootSector of=${targetDisk}s${i} count=1 bs=512
213fi
214
215mountESP
216
217else
218"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: but won't interfere as you're installing to an earlier partition."
219fi
220elif [ $stage0type == 1 ]; then
221# boot0 was found which looks for boot1 on the first active partition.
222"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: so select to boot that partition (if used) with active flag."
223#else
224#echo "DEBUG: Boot0 not found"
225fi
226fi
227fi
228
229done
230#else
231#echo "DEBUG: Just one slice"
232fi
233
234exit 0

Archive Download this file

Revision: 1636