Chameleon

Chameleon Svn Source Tree

Root/branches/azimutz/trunkAutoResolution/package/Scripts/FAT/postinstall.sh

  • Property svn:executable set to *
1#!/bin/bash
2
3diskloader="/usr/standalone/i386/boot0"
4partitionloader="/usr/standalone/i386/boot1f32"
5filesystemloader="/usr/standalone/i386/boot"
6bootervolumename="EFI"
7booterextensions="Extra/Extensions"
8
9bootresources="${0%/*}"
10
11diskmicrocodetype[1]="GRUB,47525542"
12diskmicrocodetype[2]="LILO,4c494c4f"
13
14start ()
15{
16# $1 volume
17
18osxvolume="${@}"
19
20if [ -z "${osxvolume}" ]; then
21echo
22echo "Cannot find the volume. Exiting."
23exit
24fi
25
26bootdev=$( df "${osxvolume}" | sed -n '2p' | awk '{print $1}' )
27
28if [ "${bootdev}" = "${bootdev#*disk*s}" ]; then
29echo
30echo "ERROR Volume does not use slices."
31echo "Volume may be stored on a RAID array."
32echo
33exit
34fi
35
36bootuuid=$( diskutil info "$bootdev" | grep Volume\ UUID | awk {'print $3'} )
37partitiontable=$( diskutil list ${bootdev%s*} | sed -n '3p' | awk '{print $2}' )
38
39if [ ${partitiontable} != "GUID_partition_scheme" ]; then
40echo
41echo "ERROR Volume is not on a GPT partitioned disc."
42echo
43exit
44fi
45
46echo "GPT found."
47
48echo "OS X Volume is ${osxvolume}"
49echo "OX X Volume device is ${bootdev}"
50echo "OS X Volume UUID is ${bootuuid}"
51
52
53bootvolume="/Volumes/$bootervolumename"
54bootdev=${bootdev%s*}s1
55bootrdev=${bootdev/disk/rdisk}
56bootdisk=${bootdev%s*}
57bootrdisk=${bootdisk/disk/rdisk}
58bootslice=${bootdev#*disk*s}
59
60echo "EFI Volume device is ${bootdev}"
61echo "EFI Volume raw device is ${bootrdev}"
62echo "EFI Volume slice is ${bootslice}"
63echo "Disk device is ${bootdisk}"
64echo "Disk raw device is ${bootrdisk}"
65echo "Disk loader is ${diskloader}"
66echo "Partition loader is ${partitionloader}"
67echo "Filesystem loader is ${filesystemloader}"
68
69}
70
71checkdiskmicrocodetype ()
72{
73diskmicrocode=$( dd 2>/dev/null if=${bootdisk} count=1 | dd 2>/dev/null count=1 bs=437 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
74
75diskmicrocodetypecounter=0
76while [ ${diskmicrocodetypecounter} -lt ${#diskmicrocodetype[@]} ]; do
77 diskmicrocodetypecounter=$(( ${diskmicrocodetypecounter} + 1 ))
78 diskmicrocodetypeid=${diskmicrocodetype[${diskmicrocodetypecounter}]#*,}
79 if [ ! "${diskmicrocode}" = "${diskmicrocode/${diskmicrocodetypeid}/}" ]; then
80 echo "${diskmicrocodetype[${diskmicrocodetypecounter}]%,*} found."
81 fi
82done
83}
84
85checkdiskmicrocode ()
86{
87# 1 action ( check or set )
88
89diskmicrocode=$( dd 2>/dev/null if=${bootdisk} count=1 | dd 2>/dev/null count=1 bs=437 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
90diskmicrocodemd5=$( dd 2>/dev/null if=${bootdisk} count=1 | dd 2>/dev/null count=1 bs=437 | md5 )
91
92if [ $( echo "${diskmicrocode}" | awk -F0 '{print NF-1}' ) = 874 ]; then
93if [ "${1}" = "set" ]; then
94echo "No disk microcode found. Updating."
95diskupdate=true
96else
97echo "No disk microcode found."
98fi
99else
100if [ ${1} = set ]; then
101echo "Disk microcode found. Preserving."
102else
103echo "Disk microcode found."
104fi
105echo "Disk microcode MD5 is ${diskmicrocodemd5}"
106fi
107}
108
109checkdisksignature ()
110{
111disksignature=$( dd 2>/dev/null if=${bootdisk} count=1 | dd 2>/dev/null count=4 bs=1 skip=440 | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
112
113if [ $( echo "${disksignature}" | awk -F0 '{print NF-1}' ) = 8 ]; then
114echo "No disk signature found."
115else
116echo "Disk signature found."
117echo "Disk signature is 0x${disksignature}"
118fi
119}
120
121checkpartitionbootcode ()
122{
123# 1 action ( check or set )
124
125partitionbootcode=$( dd if=${bootrdev} count=1 2>/dev/null | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
126partitionbootcodeextended=$( dd if=${bootrdev} count=1 skip=1 2>/dev/null | perl -ne '@a=split"";for(@a){printf"%02x",ord}' )
127
128if [ $( echo "${partitionbootcode}" | awk -F0 '{print NF-1}' ) = 1024 ]; then
129if [ "${1}" = "set" ]; then
130echo "No partition bootcode found. Updating."
131else
132echo "No partition bootcode found."
133fi
134else
135if [ "${1}" = "set" ]; then
136echo "Partition bootcode found. Overwriting."
137else
138echo "Partition bootcode found."
139fi
140if [ $( echo "${partitionbootcodeextended}" | awk -F0 '{print NF-1}' ) = 1024 ]; then
141partitionbootcodemd5=$( dd 2>/dev/null if=${bootrdev} count=1 | md5 )
142else
143partitionbootcodemd5=$( dd 2>/dev/null if=${bootrdev} count=2 | md5 )
144echo "Partition bootcode is dual sector."
145fi
146echo "Partition bootcode MD5 is ${partitionbootcodemd5}"
147fi
148}
149
150checkpartitionactive ()
151{
152partitionactive=$( fdisk -d ${bootrdisk} | grep -n "*" | awk -F: '{print $1}')
153
154if [ -n "${partitionactive}" ]; then
155echo "Partition flagged active is ${partitionactive}"
156else
157echo "No partition flagged active."
158fi
159
160}
161
162start ${3}
163
164if [ "$( df | grep ${bootdev} )" ]; then
165 umount -f ${bootdev}
166fi
167
168#if [[ `dd if=/dev/disk${1}s1 count=8 bs=1 skip=82 | uuencode -m -|head -n 2|tail -n 1` != "RkFUMzIgICA=" ]]; then
169#echo "${bootdev} isn't a FAT32 partition"
170#newfs_msdos -v "${bootervolumename}" "${bootdev}"
171#fi
172
173if ! [ "$( fstyp ${bootdev} | grep msdos )" ]; then
174echo "${bootdev} isn't a FAT32 partition"
175echo "Executing command: newfs_msdos -F 32 ${bootdev}"
176newfs_msdos -F 32 "${bootdev}"
177else
178echo "${bootdev} is already a FAT32 partition (skipping)"
179fi
180
181diskupdate=false
182checkdiskmicrocodetype
183checkdiskmicrocode set
184checkdisksignature
185checkpartitionbootcode set
186checkpartitionactive
187
188if ${diskupdate}; then
189echo "Executing command: fdisk -u -f ${diskloader} -y ${bootdisk}"
190fdisk -u -f "${osxvolume}/${diskloader}" -y ${bootdisk}
191fi
192
193#echo "Executing command: dd if=${partitionloader} of=${bootrdev}"
194#dd if="${osxvolume}/${partitionloader}" of=${bootrdev}
195
196echo "Stage 1"
197
198echo "Executing command: dd if=${bootrdev} count=1 bs=512 of=/tmp/origbs"
199dd if=${bootrdev} count=1 bs=512 of=/tmp/origbs
200
201echo "Executing command: cp ${osxvolume}/${partitionloader} /tmp/newbs"
202cp "${osxvolume}/${partitionloader}" /tmp/newbs
203
204echo "Executing command: dd if=/tmp/origbs of=/tmp/newbs skip=3 seek=3 bs=1 count=87 conv=notrunc"
205dd if=/tmp/origbs of=/tmp/newbs skip=3 seek=3 bs=1 count=87 conv=notrunc
206
207echo "Executing command: dd of=${bootrdev} count=1 bs=512 if=/tmp/newbs"
208dd if=/tmp/newbs of=${bootrdev} count=1 bs=512
209
210# If table is MBR make the correct slice active. If table is GPT make the first partition active (BadAxe compatibility).
211[ "${partitiontable}" = "GUID_partition_scheme" ] && bootslice=1
212if [[ "${partitiontable}" = "FDisk_partition_scheme" || "${partitiontable}" = "GUID_partition_scheme" ]]; then
213fdisk -e ${bootdisk} <<-MAKEACTIVE
214print
215flag ${bootslice}
216write
217y
218quit
219MAKEACTIVE
220fi
221
222checkdiskmicrocode check
223checkdisksignature
224checkpartitionbootcode check
225checkpartitionactive
226
227[ -d "${bootvolume}" ] || mkdir -p "${bootvolume}"
228echo "Executing command: mount_hfs ${bootdev} ${bootvolume}"
229mount_msdos -u 0 -g 0 "${bootdev}" "${bootvolume}"
230
231echo "Executing command: cp ${osxvolume}${filesystemloader} ${bootvolume}/boot"
232cp "${osxvolume}${filesystemloader}" "${bootvolume}/boot"
233
234if ! [ -d "${bootvolume}/Extra/Extensions" ]; then
235echo "Executing command: mkdir -p ${bootvolume}/Extra/Extensions"
236mkdir -p "${bootvolume}/Extra/Extensions"
237fi
238# unpack any existing Extensions.mkext already on the booter volume
239if [ -e "${bootvolume}/Extra/Extensions.mkext" ]; then
240echo "Executing command: mkextunpack -d ${bootvolume}/Extra/Extensions ${bootvolume}/Extra/Extensions.mkext"
241mkextunpack -d "${bootvolume}/Extra/Extensions" "${bootvolume}/Extra/Extensions.mkext"
242echo "Executing command: rm -R -f ${bootvolume}/Extra/Extensions.mkext"
243rm -R -f "${bootvolume}/Extra/Extensions.mkext"
244fi
245
246# copy existing /Extra
247if [ -d "${2}/Extra" ]; then
248[ -d "${bootvolume}/Extra/Extensions" ] || mkdir -p "${bootvolume}/Extra/Extensions"
249echo "Executing command: find ${2}/Extra -name '*.plist' -depth 1 -exec cp -f {} ${bootvolume}/Extra \;"
250find "${2}/Extra" -name '*.plist' -depth 1 -exec cp -f {} "${bootvolume}/Extra/" \;
251if [ -f "${2}/Extra/Extensions.mkext" ]; then
252echo "Executing command: mkextunpack -d ${2}/Extra/Extensions ${2}/Extra/Extensions.mkext"
253mkextunpack -d "${bootvolume}/Extra/Extensions" "${2}/Extra/Extensions.mkext"
254fi
255if [ -d "${2}/Extra/Extensions" ]; then
256echo "Executing command: find ${2}/Extra/Extensions -name '*.kext' -depth 1 -exec cp -R {} ${bootvolume}/Extra/Extensions \;"
257find "${2}/Extra/Extensions" -name '*.kext' -depth 1 -exec cp -R {} "${bootvolume}/Extra/Extensions" \;
258fi
259fi
260
261# setup link for extras
262echo "Executing command: ln -s /Volumes/${bootervolumename} ${2}/.Chameleon"
263ln -s "/Volumes/${bootervolumename}" "${2}/.Chameleon"
264
265exit
266

Archive Download this file

Revision: 1015