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# Check for existing Chameleon installations on a different
38# partition of the same target disk.
39#echo "DEBUG: Checking for existing Chameleon installations on ${targetDisk#/dev/}..."
40
41sliceNumber=$( echo ${targetDeviceRaw#*disk*s} )
42# strip slice from end
43targetDiskRawNoSlice=$( echo ${targetDeviceRaw%$sliceNumber} )
44
45# Are there any other partitions on the disk?
46# How many actual partitions are there?
47numSlices=$(( $( diskutil list | grep $( echo ${targetDisk#/dev/} ) | sed -n '$=' ) -2 ))
48
49# Only check the disk for Chameleon installations if there is more than one partition.
50if [ $numSlices -gt 1 ]; then
51
52# If a GPT is used then we are going to have the check the EFI system partition
53# for the stage 2 'boot' file. As this script is called from the Main EFI
54# postinstall script, so lets mount the EFI system partition here instead.
55
56if [ "${partitiontable}" = "GUID_partition_scheme" ]; then
57
58# Unmount ALL mounted volumes named EFI
59# the following script returns 0 if it succeeds
60# the following script returns 1 if it fails to un-mount any EFI volume
61"$scriptDir"UnMountEFIvolumes.sh "${installerVolume}" "${scriptDir}"
62returnValue=$?
63if [ ${returnValue} = 0 ]; then
64# OK to proceed
65
66if [ ! -e "/Volumes/EFI" ]; then
67#echo "DEBUG: Executing Command: mkdir -p ${/Volumes/EFI}"
68mkdir -p "/Volumes/EFI"
69#else
70#echo "DEBUG: folder /Volumes/EFI already exists"
71fi
72
73# Mount '/Volumes/EFI' using the correct format type
74if [ "$( fstyp "${targetDisk}"s1 | grep hfs )" ]; then
75#echo "Executing command: mount_hfs ${targetDevice} ${targetVolume}"
76"$scriptDir"InstallLog.sh "${installerVolume}" "Mounting ${targetDisk}s1 as /Volumes/EFI"
77mount_hfs "${targetDisk}"s1 "/Volumes/EFI"
78fi
79if [ "$( fstyp "${targetDisk}"s1 | grep msdos )" ]; then
80#echo "Executing command: mount_msdos -u 0 -g 0 ${targetDevice} ${/Volumes/EFI}"
81"$scriptDir"InstallLog.sh "${installerVolume}" "Mounting ${targetDisk}s1 as /Volumes/EFI"
82mount_msdos -u 0 -g 0 "${targetDisk}"s1 "/Volumes/EFI"
83fi
84else
85# quit out and notify EFI post script not to write Chameleon files.
86exit 2
87fi
88fi
89
90#Scan all partitions for Chameleon code
91for (( i=1; i <= $numSlices; i++ ));
92do
93stageExistence=0
94stage0FirstBootable=0
95previousExistence="NONE"
96targetDiskRaw=$targetDiskRawNoSlice$i
97
98# Check for existing stage 0 boot file (same code as CheckDiskMicrocode.sh script)
99stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )
100if [ "${stage0type}" == "0b807c" ] || [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then
101#echo "DEBUG: stage 0 loader found on $targetDisk"
102(( stageExistence++ ))
103
104# While here, check just for either existing boot0hfs, boot0md or boot0md (dmazar's boot0workV2)
105if [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then
106stage0FirstBootable=1
107fi
108fi
109
110# Check for existence of a bootable partition boot sector containing either boot1h or boot1f32
111boot1Search=$( dd 2>/dev/null if="$targetDiskRaw" count=1 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
112if [ "${boot1Search:0:16}" == "fa31c08ed0bcf0ff" ] && [ "${boot1Search:1020:2}" == "55" ]; then
113#echo "DEBUG: boot1h found on "$targetDiskRaw
114(( stageExistence++ ))
115previousExistence="boot1"
116elif [ "${boot1Search:0:4}" == "e962" ] && [ "${boot1Search:180:12}" == "424f4f542020" ] && [ "${boot1Search:1020:2}" == "55" ]; then
117#echo "DEBUG: boot1f32 found on "$targetDiskRaw
118(( stageExistence++ ))
119previousExistence="boot1f32"
120fi
121
122# Check for existing stage 2 boot file.
123# Include checking the EFI system partition if it exists and is mounted.
124if [ -e "$( df | grep ${targetDisk}s${i} | awk '{ print $6 }' )"/boot ]; then
125#echo "DEBUG: boot found on $targetDiskRaw"
126(( stageExistence++ ))
127fi
128
129if [ $stageExistence -ge 2 ] && [ "$previousExistence" != "NONE" ] && [ $i -ne $sliceNumber ]; then
130# There is previous Chameleon stage 1 code on a partition boot sector,
131# and either a complete or incomplete installation (ie. boot0 or boot are missing).
132
133if [ $stageExistence == 3 ]; then
134"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: There is already an existing Chameleon installation on $targetDiskRaw."
135if [ $i -lt $sliceNumber ]; then
136"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: And that installation will still be used as it's on an earlier partition."
137else
138"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: but won't interfere as you're installing to an earlier partition."
139fi
140else
141"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: $previousExistence already exists at ${targetDisk}s${i}"
142
143# A b1f:error or boot0:error could result if the following conditions are true:
144# A) Boot0hfs, Boot0md or Boot0md (dmazar's Boot0workV2) is being used.
145# B) The previous stage 1 code is on a lower partiton than the one being installed to now.
146# C) boot is missing from that partition.
147
148# stage0FirstBootable=1 is used to know if 'A' is true.
149if [ $stage0FirstBootable == 1 ]; then
150# i = current slice we're checking, slicenumber = slice trying to install to.
151if [ $i -lt $sliceNumber ]; then
152"$scriptDir"InstallLog.sh "${installerVolume}" "WARN: Conditions point to the possibility of a boot failure"
153
154# Fix by making previous paritionboot sector un-bootable
155# Change Byte 01FExh to 00 (510 decimal)
156mesaageToPost="---
157FIX: Make ${targetDisk}s${i} boot sector un-bootable by changing byte 1FEh to 00.
158NOTE: Any Extra folder you had there will still be there. If you want to use
159NOTE: ${targetDisk}s${i} again as your boot partition then re-run this installer
160NOTE: selecting it as the target, ONLY choosing the 'Chameleon Bootloader' option
161NOTE: and NONE of the other options.
162---"
163"$scriptDir"InstallLog.sh "${installerVolume}" "${mesaageToPost}"
164dd if=${targetDisk}s${i} count=1 bs=512 of=/tmp/originalBootSector
165cp /tmp/originalBootSector /tmp/newBootSector
166dd if="$scriptDir/patch" of=/tmp/newBootSector bs=1 count=1 seek=510 conv=notrunc
167dd if=/tmp/newBootSector of=${targetDisk}s${i} count=1 bs=512
168else
169"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: but won't interfere as you're installing to an earlier partition."
170fi
171else
172"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: so select to boot that partition (if used) with active flag."
173fi
174fi
175fi
176done
177#else
178#echo "DEBUG: Just one slice"
179fi
180
181exit 0

Archive Download this file

Revision: 1634