-
Notifications
You must be signed in to change notification settings - Fork 116
/
old-extract.sh
executable file
·186 lines (182 loc) · 6.76 KB
/
old-extract.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#!/bin/sh
#
# $Id: extract_firmware.sh 336 2012-08-04 00:12:14Z [email protected] $
#
. "./shared.inc"
### 20110225-MCT The VERSION is set in the shared.inc file from a single external source now.
VERSION="${SHARED_VERSION}"
#
# Title: extract_firmware.sh
# Author: Jeremy Collake <[email protected]>
# Site: http://code.google.com/p/firmware-mod-kit/
#
# USAGE: old-extract.sh FIRMWARE_IMAGE.BIN WORKING_DIRECTORY/
#
# This scripts extacts the firmware image to [WORKING_DIRECTORY],
# with the following subdirectories:
#
# image_parts/ <- firmware seperated
# rootfs/ <- extracted filesystem
#
# Example:
#
# ./old-extract.sh dd-wrt.v23_generic.bin std_generic
#
#
EXIT_ON_FS_PROBLEM="0"
echo
echo " Firmware Mod Kit (extract) v$VERSION, (c)2010-2012 Jeremy Collake, - Newer NG edition by Craig Heffner"
#################################################################
#
# function: ExtractLinuxRawFirmwareType ()
#
# Extracts essentially 'raw' firmware images with kernel, filesystem, and hardware id
# Example is the TrendNET TEW-632BRP router.
#
ExtractLinuxRawFirmwareType ()
{
# $1 = input firmware
PARTS_PATH=$2
echo " Extracting $1 to $2 ..."
mkdir -p "$PARTS_PATH/image_parts"
if [ $? = 0 ]; then
dd "if=$1" "of=$PARTS_PATH/image_parts/vmlinuz" bs=1K count=1024 2>/dev/null >> extract.log
dd "if=$1" "of=$PARTS_PATH/image_parts/squashfs-3-lzma.img" bs=1K skip=1024 2>/dev/null >> extract.log
SYSNAME=`uname`
if [ `expr "$SYSNAME" : "Darwin"` = 6 ]; then
filesize=$(ls -la $1 | awk '{print $5}')
else
filesize=$(du --apparent-size --block-size=1 $1 | cut -f 1)
fi
filesize=$((filesize - 24))
dd "if=$1" "of=$PARTS_PATH/image_parts/hwid.txt" bs=1 skip=$filesize 2>/dev/null >> extract.log
"./src/squashfs-3.0/unsquashfs-lzma" -dest "$PARTS_PATH/rootfs" \
"$PARTS_PATH/image_parts/squashfs-3-lzma.img" 2>/dev/null >> extract.log
if [ -e "$PARTS_PATH/rootfs/" ]; then
# write a marker to indicate the firmware image type and filesystem type
touch "$PARTS_PATH/.linux_raw_type"
touch "$PARTS_PATH/.squashfs3_lzma_fs"
fi
else
echo " ERROR: Creating output directory.."
fi
}
#################################################################
#
# Main script entry
#
#################################################################
if [ $# = 2 ]; then
PlatformIdentify
#################################################################
TestFileSystemExit $1 $2
#################################################################
TestIsRoot
#################################################################
if [ -f "$1" ]; then
if [ ! -f "./old-extract.sh" ]; then
echo " ERROR - You must run this script from the same directory as it is in!"
exit 1
fi
#################################################################
# remove deprecated stuff
if [ -f "./src/mksquashfs.c" ] || [ -f "mksquashfs.c" ]; then
DeprecateOldVersion
fi
#################################################################
# Invoke BuildTools, which tries to build everything and then
# sets up appropriate symlinks.
#
BuildTools "extract.log"
#################################################################
echo " Preparing working directory ..."
echo " Removing any previous files ..."
rm -rf "$2/rootfs" >> extract.log 2>&1
rm -rf "$2/image_parts" >> extract.log 2>&1
rm -rf "$2/installed_packages" >> extract.log 2>&1
echo " Creating directories ..."
mkdir -p "$2/image_parts" >> extract.log 2>&1
mkdir -p "$2/installed_packages" >> extract.log 2>&1
echo " Extracting firmware"
"src/untrx" "$1" "$2/image_parts" >> extract.log 2>&1
if [ $? != 0 ]; then
echo "! untrx failed, trying splitter3";
"src/splitter3" "$1" "$2/image_parts" >> extract.log 2>&1
if [ $? != 0 ]; then
echo " Not recognized by splitter3";
# exit 1
else
touch "$2/.linux_raw_type3"
touch "$2/.squashfs3_lzma_fs"
fi
fi
# if unknown version, then we'll just try to use the latest UnSquashFS we have
if [ -f "$2/image_parts/squashfs-lzma-image-x_x" ]; then
ln -s "squashfs-lzma-image-x_x" "$2/image_parts/squashfs-lzma-image-3_0"
fi
# if squashfs 3.1 or 3.2, symlink it to 3.0 image, since they are compatible
if [ -f "$2/image_parts/squashfs-lzma-image-3_1" ]; then
ln -s "squashfs-lzma-image-3_1" "$2/image_parts/squashfs-lzma-image-3_0"
fi
if [ -f "$2/image_parts/squashfs-lzma-image-3_2" ]; then
ln -s "squashfs-lzma-image-3_2" "$2/image_parts/squashfs-lzma-image-3_0"
fi
if [ -f "$2/image_parts/squashfs-lzma-image-3_x" ]; then
ln -s "squashfs-lzma-image-3_x" "$2/image_parts/squashfs-lzma-image-3_0"
fi
if [ -f "$2/image_parts/squashfs-lzma-image-2_0" ]; then
ln -s "squashfs-lzma-image-2_0" "$2/image_parts/squashfs-lzma-image-2_x"
fi
if [ -f "$2/image_parts/squashfs-lzma-image-2_1" ]; then
ln -s "squashfs-lzma-image-2_1" "$2/image_parts/squashfs-lzma-image-2_x"
fi
# now unsquashfs, if filesystem is squashfs
if [ -f "$2/image_parts/squashfs-lzma-image-3_0" ]; then
echo " Attempting squashfs 3.0 lzma ..."
"src/squashfs-3.0/unsquashfs-lzma" \
-dest "$2/rootfs" "$2/image_parts/squashfs-lzma-image-3_0" 2>/dev/null >> extract.log
if [ ! -e "$2/rootfs" ]; then
echo " Trying 'damn small' variant - used by DD-WRT v24 ..."
"src/squashfs-3.0-lzma-damn-small-variant/unsquashfs-lzma" \
-dest "$2/rootfs" "$2/image_parts/squashfs-lzma-image-3_0" 2>/dev/null >> extract.log
if [ -e "$2/rootfs" ]; then
# if it worked, then write a tag so we know which squashfs variant to build the fs with
touch "$2/image_parts/.sq_lzma_damn_small_variant_marker"
touch "$2/image_parts/.trx-sqfs"
fi
else
touch "$2/image_parts/.trx-sqfs"
fi
elif [ -f "$2/image_parts/squashfs-lzma-image-2_x" ]; then
"src/squashfs-2.1-r2/unsquashfs-lzma" \
-dest "$2/rootfs" "$2/image_parts/squashfs-lzma-image-2_x" 2>/dev/null >>extract.log
if [ -e "$2/rootfs" ]; then
touch "$2/image_parts/.trx-sqfs"
else
echo " ERROR: extracting filesystem."
fi
elif [ -f "$2/image_parts/cramfs-image-x_x" ]; then
TestIsRoot
"src/cramfs-2.x/cramfsck" \
-v -x "$2/rootfs" "$2/image_parts/cramfs-image-x_x" >> extract.log 2>&1
else
echo " Attempting raw linux style firmware package (i.e. TEW-632BRP) ..."
ExtractLinuxRawFirmwareType "$1" "$2"
fi
if [ -e "$2/rootfs" ]; then
echo " Firmware appears extracted correctly!"
echo " Now make changes and run build_firmware.sh."
else
echo " Error: filesystem not extracted properly."
echo " firmware image format not compatible?"
exit 1
fi
else
echo " $1 does not exist.. give me something to work with man!"
fi
else
echo " Incorrect usage."
echo " USAGE: $0 FIRMWARE_IMAGE.BIN WORKING_DIR"
exit 1
fi
exit 0