-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch
executable file
·99 lines (91 loc) · 3.29 KB
/
patch
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
#!/usr/bin/env bash
## NONE/RED/GREEN
COLORS=("\033[0m" "\033[0;31m" "\033[0;32m")
function cleanup {
if mount | grep -q ${FIRMWARE_DIR}/image; then
sudo umount ${FIRMWARE_DIR}/image
fi
if [ ! -z "${FIRMWARE_DIR}" ]; then
test -d ${FIRMWARE_DIR} && rm -rf ${FIRMWARE_DIR}
fi
};
function select_option {
echo $1
shift
options=($@ "Quit")
select value in "${options[@]}"
do
if [ "${value}" == "Quit" ]; then
cleanup
exit 1;
fi
if [[ " ${options[@]} " =~ " ${value} " ]]; then
SELECTION=${value}
break;
fi
done
};
function execute_step {
MSG=$1
shift
if eval "$@"; then
echo -e "[${COLORS[2]}Done${COLORS[0]}]\t${MSG}"
else
echo -e "[${COLORS[1]}Error${COLORS[0]}]\t${MSG}"
cleanup
exit 1
fi
};
shrinker () {
if [ $# -eq 2 ]; then
for f in $1/* ;do
if [ -d ${f} ]; then
shrinker ${f} $2
else
case "$2" in
-c | --compress )
test -f $f && echo $f | grep -q -v ".lzma" && sudo lzma $f
;;
-d | --decompress )
test -f $f && echo $f | grep -q ".lzma" && sudo unlzma $f
;;
*)
echo "usage: $0 { -c | --compress | -d | --decompress } path" >&2
exit 1
;;
esac
fi
done
fi
};
sudo echo "Sudo permissions granted" || exit 1
select_option "Select firmware:" "TW03" "AW03"
FIRMWARE="./resources/SE-208BW_${SELECTION}.tar.gz"
FIRMWARE_DIR=${FIRMWARE%*.*.*}
if [ -d ${FIRMWARE_DIR} ]; then
cleanup
echo "Old images removed."
fi
mkdir -p ${FIRMWARE_DIR}/firmware
mkdir -p ${FIRMWARE_DIR}/image
tar -zxf ${FIRMWARE} -C ./${FIRMWARE_DIR}/firmware
execute_step "Image extraction" "gunzip -c ${FIRMWARE_DIR}/firmware/rd.gz > ${FIRMWARE_DIR}/rd"
rm ${FIRMWARE_DIR}/firmware/rd.gz
execute_step "Image mount" sudo mount -o loop ${FIRMWARE_DIR}/rd ${FIRMWARE_DIR}/image
execute_step "Patches copied" sudo cp -rf ./patches/* ${FIRMWARE_DIR}/image
execute_step "RC.begin patched" sudo sed -i "20r${FIRMWARE_DIR}/image/etc/rc.begin" ${FIRMWARE_DIR}/image/etc/rc && sudo rm ${FIRMWARE_DIR}/image/etc/rc.begin
RC_END=$(( $(cat ${FIRMWARE_DIR}/image/etc/rc | wc -l) - 1 ))
execute_step "RC.end patched" sudo sed -i "${RC_END}r${FIRMWARE_DIR}/image/etc/rc.end" ${FIRMWARE_DIR}/image/etc/rc && sudo rm ${FIRMWARE_DIR}/image/etc/rc.end
execute_step "Comprimed /usr/local" shrinker ${FIRMWARE_DIR}/image/usr/local -c
mkdir -p ${FIRMWARE_DIR}/tmp
cp -r ./image/* ${FIRMWARE_DIR}/tmp
execute_step "Comprimed /bin" shrinker ${FIRMWARE_DIR}/tmp/bin -c
execute_step "Comprimed /usr" shrinker ${FIRMWARE_DIR}/tmp/usr -c
execute_step "Binaries copied" sudo cp -rf ${FIRMWARE_DIR}/tmp/* ${FIRMWARE_DIR}/image/
rm -rf ${FIRMWARE_DIR}/tmp/
execute_step "Image un-mount" sudo umount ${FIRMWARE_DIR}/image
## Zeroing empty blocks for better compression rate
zerofree ${FIRMWARE_DIR}/rd
execute_step "Image build" "gzip -9 -c ${FIRMWARE_DIR}/rd > ${FIRMWARE_DIR}/firmware/rd.gz "
execute_step "Firmware patched" tar -czf SE-208BW_${VERSION:-TW03}.tar.gz -C ./${FIRMWARE_DIR}/firmware zImage ImageInfo rd.gz
cleanup