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# Called from the Standard/postinstall and EFI/postinstall scripts
12# /Volumes/EFI should already be mounted before this is called.
13
14# Receives targetDisk: for example, /dev/disk3.
15# Receives targetDeviceRaw: for example, /dev/rdisk3s1.
16# Receives targetDevice: Stores device number, for example /dev/disk2s1.
17# Receives installerVolume: Volume to write the installer log to.
18# Receives scriptDir: The location of the main script dir.
19
20if [ "$#" -eq 5 ]; then
21targetDisk="$1"
22targetDeviceRaw="$2"
23targetDevice="$3"
24installerVolume="$4"
25scriptDir="$5"
26echo "DEBUG: passed argument for targetDisk = $targetDisk"
27echo "DEBUG: passed argument for targetDeviceRaw = $targetDeviceRaw"
28echo "DEBUG: passed argument for targetDevice = $targetDevice"
29echo "DEBUG: passed argument for installerVolume = $installerVolume"
30echo "DEBUG: passed argument for scriptDir = $scriptDir"
31else
32echo "Error - wrong number of values passed"
33exit 9
34fi
35
36
37# ===============================================
38# Prepare some vars
39# ===============================================
40sliceNumber=$( echo ${targetDeviceRaw#*disk*s} )
41
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
50# ===============================================
51# Checking the disk for existing Chameleon installations
52# if there is more than one partition on the disk.
53# ===============================================
54if [ $numSlices -gt 1 ]; then
55"$scriptDir"InstallLog.sh "${installerVolume}" "LineBreak"
56"$scriptDir"InstallLog.sh "${installerVolume}" "Checking for previous chameleon installations on ${targetDisk#/dev/}"
57
58# If a GPT is used then we are going to have the check the EFI system partition
59# for the stage 2 'boot' file so lets mount the EFI system partition.
60mountESP
61
62# Check the disk's MBR for existing stage 0 boot code (code from CheckDiskMicrocode.sh script)
63stage0type=$( dd 2>/dev/null if="$targetDisk" count=3 bs=1 skip=105 | xxd | awk '{print $2$3}' )
64if [ "${stage0type}" == "0a803c" ] || [ "${stage0type}" == "ee7505" ] || [ "${stage0type}" == "742b80" ]; then
65(( stagesFound++ ))
66stage0type=2
67elif [ "${stage0type}" == "0b807c" ]; then
68(( stagesFound++ ))
69stage0type=1
70fi
71
72#Scan all partitions for Chameleon code
73for (( i=1; i <= $numSlices; i++ ));
74do
75stagesFound=0
76stage1Existence="NONE"
77stage2Existence=0
78targetDiskRaw=$targetDiskRawNoSlice$i
79
80# Check for existence of a bootable partition boot sector containing either boot1h or boot1f32
81boot1Search=$( dd 2>/dev/null if="$targetDiskRaw" count=1 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
82if [ "${boot1Search:0:16}" == "fa31c08ed0bcf0ff" ] && [ "${boot1Search:1020:2}" == "55" ]; then
83(( stagesFound++ ))
84stage1Existence="boot1h"
85elif [ "${boot1Search:0:4}" == "e962" ] && [ "${boot1Search:180:12}" == "424f4f542020" ] && [ "${boot1Search:1020:2}" == "55" ]; then
86(( stagesFound++ ))
87stage1Existence="boot1f32"
88fi
89
90# Check for existing stage 2 boot file.
91# Include checking the EFI system partition if it exists and is mounted.
92if [ -e "$( df | grep ${targetDisk}s${i} | awk '{ print $6 }' )"/boot ]; then
93(( stagesFound++ ))
94stage2Existence=1
95fi
96
97if [ $stagesFound -ge 2 ] && [ "$stage1Existence" != "NONE" ] && [ $i -ne $sliceNumber ]; then
98# There is previous Chameleon stage 1 code on a partition boot sector,
99# and either a complete or incomplete installation (ie. boot0 or boot are missing).
100
101if [ $stagesFound == 3 ] && [ $i -lt $sliceNumber ]; then
102# Exisitng installation found which will still be default.
103message="************************** TAKE NOTE *****************************
104**** There is an existing Chameleon installation on $targetDiskRaw
105**** and that installation will still be the default loader as it's
106**** on an earlier partition. If you want this new installation on
107**** $installerVolume to be default then you will need to remove the
108**** boot file from $targetDiskRaw and re-run this installer.
109**************************************************************"
110"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
111fi
112if [ $stagesFound == 3 ] && [ $i -gt $sliceNumber ]; then
113# Exisitng installation found which will no longer be default.
114message="NOTE: There is an existing Chameleon installation on $targetDiskRaw
115NOTE: but this installation on $targetDevice will be the default loader
116NOTE: because you're installing to an earlier partition on the disk."
117"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
118fi
119
120
121# User could see a b1f:error or boot0:error if the following conditions are true:
122# A) Boot0hfs, Boot0md or Boot0md (dmazar's Boot0workV2) is being used.
123# B) The previous stage 1 code is on a lower partiton than the one being installed to now.
124# C) boot is missing from that partition.
125
126if [ $stagesFound == 2 ] && [ $stage2Existence == 0 ]; then
127# Exisitng boot0 and boot1 only found - missing boot
128"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: Found $stage1Existence installed to ${targetDisk}s${i}"
129
130# stage0type=2 is used to know if 'A' is true.
131if [ $stage0type == 2 ]; then
132# i = current slice we're checking, slicenumber = slice trying to install to.
133if [ $i -lt $sliceNumber ]; then
134"$scriptDir"InstallLog.sh "${installerVolume}" "WARN: Conditions point to the possibility of a boot failure"
135
136# Fix by making previous paritionboot sector un-bootable
137# Change Byte 01FExh to 00 (510 decimal)
138message="---
139FIX: Make ${targetDisk}s${i} boot sector un-bootable by changing byte 1FEh to 00.
140NOTE: Any Extra folder you had there will still be there. If you want to use
141NOTE: ${targetDisk}s${i} again as your boot partition then re-run this installer
142NOTE: selecting it as the target, ONLY choosing the 'Chameleon Bootloader' option
143NOTE: and NONE of the other options.
144---"
145"$scriptDir"InstallLog.sh "${installerVolume}" "${message}"
146
147# /Volumes/EFI needs unmounting before changing partition boot sector
148if [ $i == 1 ]; then
149umount /Volumes/EFI
150
151if [ "$( fstyp "${targetDisk}"s1 | grep hfs )" ]; then
152#echo "DEBUG: HFS - changing byte 1FEh to 00"
153dd if=${targetDisk}s${i} count=2 bs=512 of=originalBootSector
154cp originalBootSector newBootSector
155dd if="patch" of=newBootSector bs=1 count=1 seek=510 conv=notrunc
156dd if=newBootSector of=${targetDisk}s${i} count=2 bs=510
157fi
158if [ "$( fstyp "${targetDisk}"s1 | grep msdos )" ]; then
159#echo "DEBUG: MSDOS - changing byte 1FEh to 00"
160dd if=${targetDisk}s${i} count=1 bs=512 of=/tmp/originalBootSector
161cp /tmp/originalBootSector /tmp/newBootSector
162dd if="$scriptDir/patch" of=/tmp/newBootSector bs=1 count=1 seek=510 conv=notrunc
163dd if=/tmp/newBootSector of=${targetDisk}s${i} count=1 bs=512
164fi
165
166# /Volumes/EFI needs re-mounting so EFI/postinstall script can use it.
167# Don't check for a GPT as wouldn't have got here if it wasn't
168"$scriptDir"MountESP.sh "${targetDisk}" "${installerVolume}" "${scriptDir}"
169fi
170
171else
172"$scriptDir"InstallLog.sh "${installerVolume}" "INFO: but won't interfere as you're installing to an earlier partition."
173fi
174elif [ $stage0type == 1 ]; then
175# boot0 was found which looks for boot1 on the first active partition.
176"$scriptDir"InstallLog.sh "${installerVolume}" "NOTE: so select to boot that partition (if used) with active flag."
177#else
178#echo "DEBUG: Boot0 not found"
179fi
180fi
181fi
182
183done
184#else
185#echo "DEBUG: Just one slice"
186fi
187
188exit 0

Archive Download this file

Revision: 1637