Code
·
156 lines
·
4034 bytes
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
154
155
156#!/bin/sh
#
ME="Bootsector Linux loader 1.3 (C) 2023 By Alphons van der Heijden"
#
# Making bootable disk file and an additional .vmdk file for booting in vmware
#
# For the .vmdk file see https://kb.vmware.com/s/article/1026266
# (this is handled in this script)
# HEADS=64, SECTORS=32 ; disksize < 1GB
# HEADS=128, SECTORS=32 ; disksize > 1GB && disksize < 2GB
# HEADS=255, SECTORS=63 ; disksize > 2GB
#
# Experimental disk decoding
# for example: ./build.sh disk /tmp
# creates kernel and initrd.gz in /tmp
# 2014- Dr Gareth Owen (www.ghowen.me). All rights reserved.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# INITRD can have multiple gz files, for example INITRD=rootfs.gz,modules.gz
WORK=/mnt/sdb1
KERNEL=$WORK/vmlinuz64
INITRD=$WORK/vmtux64.gz
#rootfs64.gz,$WORK/modules64.gz
CMDLINE="'loglevel=3 nozswap'"
OUTPUT=/tmp/vmtux
#
echo
echo $ME
echo
# some usefull constants
BOOT="bsect.asm"
TMPINITRD=/tmp/initrd.gz
SECTORSIZE=512
if [ "$#" -gt 0 ]; then
if [ "$#" != 2 ]; then
echo "Usage: $0 <disk> <outputdirectory>"
echo
exit 1
fi
INPUT=$1
DIR=$2
if [ ! -d $DIR ]; then
echo "Directory does not exist"
exit 1
fi
if [ ! -f $INPUT ]; then
echo "Input file $INPUT not found"
echo
exit 1
fi
TOTAL=$(stat -c %s $INPUT)
TOTALSECTORS=$(($TOTAL / $SECTORSIZE))
echo "Decoding $INPUT to directory $DIR processing..."
HEXADDRESS=$(hexdump $INPUT | grep '8b1f 0008 0000' | sed 's/ .*//' | tr '[a-z]' '[A-Z]')
if [[ $HEXADDRESS == "" ]]; then
echo "No initrd.gz found"
exit 1
fi
echo
ADDRESS=$(echo "obase=10; ibase=16; $HEXADDRESS" | bc)
SECTOR=$(($ADDRESS / SECTORSIZE))
echo "writing: $DIR/kernel"
dd if=$INPUT count=$(($SECTOR - 1)) skip=1 2>/dev/null > $DIR/kernel
echo "writing: $DIR/initrd.gz"
dd if=$INPUT count=$(($TOTALSECTORS - $SECTOR)) skip=$SECTOR 2>/dev/null | gunzip | gzip > $DIR/initrd.gz
echo
exit 0
fi
if [ ! -f $KERNEL ]; then
echo "Error: $KERNEL not found"
exit 1
fi
# preprocessing INITRD for multiple entries
echo -n > $TMPINITRD
for gz in ${INITRD//,/ }
do
if [ -f $gz ]; then
cat $gz >> $TMPINITRD
else
echo "Error: $gz not found"
echo
exit 1
fi
done
# size of kernel + ramdisk
K_SZ=$(stat -c %s $KERNEL)
R_SZ=$(stat -c %s $TMPINITRD)
# Padding to make it up to a sector
# Always use padding even when exact on sector boundary
K_PAD=$(($SECTORSIZE - $K_SZ % $SECTORSIZE))
R_PAD=$(($SECTORSIZE - $R_SZ % $SECTORSIZE))
nasm -o $OUTPUT -D initRdSizeDef=$R_SZ -D cmdLineDef="$CMDLINE" $BOOT
cat $KERNEL >> $OUTPUT
dd if=/dev/zero bs=1 count=$K_PAD status=none >> $OUTPUT
cat $TMPINITRD >> $OUTPUT
dd if=/dev/zero bs=1 count=$R_PAD status=none >> $OUTPUT
rm -f $TMPINITRD
TOTAL=$(stat -c %s $OUTPUT)
if [ $TOTAL -gt 2000000000 ]; then
SECTORS=63
HEADS=255
else
SECTORS=32
if [ $TOTAL -gt 1000000000 ]; then
HEADS=128
else
HEADS=64
fi
fi
TOTALSECTORS=$(($TOTAL / $SECTORSIZE))
CYLINDERS=$(($TOTALSECTORS / ($HEADS * $SECTORS)))
OUTPUTNAME=$(echo $OUTPUT | sed 's/.*\///' )
cat > $OUTPUT.vmdk <<EOF
version=1
encoding="UTF-8"
CID=123456789
parentCID=ffffffff
createType="vmfs"
RW $TOTALSECTORS VMFS "$OUTPUTNAME"
ddb.virtualHWVersion = "8"
ddb.geometry.cylinders = "$CYLINDERS"
ddb.geometry.heads = "$HEADS"
ddb.geometry.sectors = "$SECTORS"
ddb.adapterType = "lsilogic"
EOF
echo "Ready $OUTPUT ($TOTAL bytes) and $OUTPUT.vmdk"
echo