-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskmaker.sh
executable file
·153 lines (109 loc) · 3.53 KB
/
diskmaker.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/bin/bash
# Diskmaker.sh : generates a disk image
# diskmaker.sh <bootloader> <stage1> <output
if [[ $# -ne 4 ]]; then
echo -e "Error Bad usage"
echo -e "Usage : $0 <bootloader> <chainloader> <stage1> <output>\n"
echo -e "Generates a disk image in <output>, and create a partition table"
echo -e "table on it, containg a single FAT32 partition. Then writes the"
echo -e "<bootloader> binary on the Master Boot Record of <output>, and "
echo -e "<stage1> on the reserved sectors of the FAT 32 partition, using "
echo -e "<chainloader> on the boot section of the VBR to chain."
exit 1
fi
# check the running OS
OS=`uname`
if [[ -f $4 ]]; then
echo -e "Warning : output file $4 exists, override\n"
fi
echo -e "\\033[1;32mCreating the disk image... \\033[0;39m"
echo -e "(Command:\\033[1;33m\tdd if=/dev/zero of=$4 bs=1440 count=8192\\033[0;39m)"
dd if=/dev/zero of=$4 bs=512 count=1440
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error creating disk image. (Check command output) \\033[0;39m"
exit 1
fi
echo -e "\\033[1;32mPartitionning disk... \\033[0;39m"
if [[ $OS = "Darwin" ]]; then
echo -e "(Command:\\033[1;33m\tfdisk -i -a dos $4 \\033[0;39m)"
fdisk -i -a dos $4
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error partitionning disk image. (Check command output) \\033[0;39m"
exit 1
fi
else
# 16065
# quite dirty but...
echo " o
x
c
16065
r
n
p
1
1
1439
t
c
a
1
w
" | fdisk disk.img
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error partitionning disk image. (Check command output) \\033[0;39m"
exit 1
fi
fi
echo -e "\\033[1;32mCreating FAT filesystem on first partition... \\033[0;39m"
if [[ $OS = "Darwin" ]]; then
echo -e "(Command:\\033[1;33m\thdiutil attach $4\\033[0;39m)"
device=`hdiutil attach -nomount $4 | head -n 2 | tail -n 1 | cut -d ' ' -f 1`
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error mounting disk image (Check command output) \\033[0;39m"
exit 1
fi
echo -e "(Command:\\033[1;33m\tnewfs_msdos -F 12 -O \"QUASAR 0\" -S 512 -c 1 -r 32 $device\\033[0;39m)"
newfs_msdos -F 12 -O "QUASAR 0" -S 512 -c 1 -r 32 $device
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error creating FS (Check command output) \\033[0;39m"
exit 1
fi
hdiutil detach $device
else
# This is _VERY_ dirty... one should find a cleaner way
offset=63
offset=$(($offset*512))
losetup -o $offset /dev/loop0 $4
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error mounting disk image (Check command output) \\033[0;39m"
exit 1
fi
# not sure if -n is really the equivalent of Mac OS's -O switch...
mkdosfs -F 12 -n "QUASAR 0" -S 512 -s 1 -R 32 /dev/loop0
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error creating FS (Check command output) \\033[0;39m"
exit 1
fi
losetup -d /dev/loop0
fi
if [[ !( -f $1) ]]; then
echo -e "\\033[1;31mFAILED: $1: No such file \\033[0;39m"
exit 1
fi
if [[ !( -f $2) ]]; then
echo -e "\\033[1;31mFAILED: $2: No such file \\033[0;39m"
exit 1
fi
if [[ !( -f $3) ]]; then
echo -e "\\033[1;31mFAILED: $3: No such file \\033[0;39m"
exit 1
fi
echo -e "\\033[1;32mWriting binaries on disk... \\033[0;39m"
echo -e "(Command:\\033[1;33m\t./writeloader/writeloader $1 $2 $3 $4\\033[0;39m)"
./writeloader/writeloader $1 $2 $3 $4
if [[ $? -ne 0 ]]; then
echo -e "\\033[1;31mFAILED: Error writing programs. (Check command output) \\033[0;39m"
exit 1
fi
echo -e "\\033[1;32mAll operations successful\\033[0;39m"