-
Notifications
You must be signed in to change notification settings - Fork 20
/
editor.sh
executable file
·177 lines (149 loc) · 8.45 KB
/
editor.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
#!/bin/sh
#################################################################################
# usage : ./editor.sh [kernel] [initramfs_directory / file] [gz/lzma/""] #
# example : ./editor.sh /home/zero/Desktop/test/zImage /home/zero/Desktop/ramdisk-dir lzma #
# 1. [kernel] : the place where your zImage file is #
# 2. [initramfs_directory / file] : the place where your initramfs folder or a compressed file of the initramfs is #
# (when you choose a compressed file of the initramfs, you don't need [gz or lzma], just leave it #
# 3. [gz/lzma/""] : When you choose a directory of the initramfs, you need to specify the way how you #
# want to compress your initramfs directory. ("" means just leave it!) #
# FROYO : supports both lzma and gzip compressions #
# ECLAIR : only supports gzip compression #
# you should let this editor where your cross-compiler is #
#################################################################################
COMPILER=/home/liliniser/ToolChains/arm-2009q3/bin/arm-none-linux-gnueabi
if test ! $COMPILER-gcc > /dev/null ; then
echo "****** You must let me know where the compiler is! ******"
exit
fi
cur_space=$PWD
zImage=$1
new_ramdisk=$2
determiner=0
Image_here="./out/Image"
if test -d out ; then
rm -r out && mkdir out
else
mkdir out
fi
echo "##### My name is $0 #####"
if [ $1 = "" ]; then
echo "****** You must let me know where the zImage file is ******"
exit
else
echo "##### The kernel is $1 #####"
fi
if [ "$2" = "" ]; then
echo "****** You must let me know where the ramdisk directory is ******"
exit
elif [ -d $2 ] ; then
echo "##### The directory of the ramdisk has been selected #####"
elif [ -f $2 ] ; then
echo "##### The compressed file of the ramdisk has been selected #####"
ramdisk_type="file"
fi
if [ "$3" = "gz" ]; then
echo "##### The ramdisk is $2 (will be gzipped) #####"
cd $2
find . -print0 | cpio -o0 -H newc | gzip -9 -f > $cur_space/out/initramfs_data.cpio.gz
new_ramdisk=out/initramfs_data.cpio.gz
cd $cur_space
elif [ "$3" = "lzma" ]; then
echo "##### The ramdisk is $2 (will be lzma'ed) #####"
/bin/bash resources/2.6.29/scripts/gen_initramfs_list.sh -o out/initramfs_data.cpio.lzma -u "squash" -g "squash" $2
new_ramdisk=out/initramfs_data.cpio.lzma
elif [ "$3" = "" ] ; then
if [ "$ramdisk_type" = "file" ] ; then
echo "##### The ramdisk is $2 (already compressed) #####"
new_ramdisk=$2
else
echo "****** You must let me know how you want to compress the ramdisk's directory ******"
exit
fi
fi
#=======================================================
# find start of gziped kernel object in the zImage file:
#=======================================================
pos=`grep -P -a -b --only-matching '\x1F\x8B\x08' $zImage | cut -f 1 -d : | grep '1' | awk '(NR==1)'`
echo "##### 01. Extracting kernel from $zImage (start = $pos)"
dd if=$zImage bs=1 skip=$pos | gunzip > $Image_here
#===========================================================================
# find start and end of the "cpio" initramfs inside the kernel object:
# ASCII cpio header starts with '070701'
# The end of the cpio archive is marked with an empty file named TRAILER!!!
#===========================================================================
start=`grep -a -b --only-matching '070701' $Image_here | head -1 | cut -f 1 -d :`
end=`grep -a -b --only-matching 'TRAILER!!!' $Image_here | head -1 | cut -f 1 -d :`
end=$((end + 10))
count=$((end - start))
if [ $count -lt $determiner ]; then
echo "##### ERROR : Couldn't match start/end of the initramfs ."
exit
fi
# Check the new ramdisk's size
ramdsize=`ls -l $new_ramdisk | awk '{print $5}'`
echo "##### 02. The size of the new ramdisk is = $ramdsize / original = $count"
if [ $ramdsize -gt $count ]; then
echo "****** Your initramfs needs to be smaller than the present!! ******"
exit
else
mv $new_ramdisk out/ramdisk.cpio
fi
# Check the Image's size
filesize=`ls -l $Image_here | awk '{print $5}'`
echo "##### 03. The size of the Image is $filesize"
# Split the Image #1 -> head.img
echo "##### 04. Making a head.img ( from 0 ~ $start )"
dd if=$Image_here bs=1 count=$start of=out/head.img
# Split the Image #2 -> tail.img
echo "##### 05. Making a tail.img ( from $end ~ $filesize )"
dd if=$Image_here bs=1 skip=$end of=out/tail.img
# FrankenStein is being made #1
echo "##### 06. Merging head + ramdisk"
cat out/head.img out/ramdisk.cpio > out/franken.img
echo "##### 07. Checking the size of [head+ramdisk]"
franksize=`ls -l out/franken.img | awk '{print $5}'`
# FrankenStein is being made #2
echo "##### 08. Merging [head+ramdisk] + padding + tail"
if [ $franksize -lt $end ]; then
tempnum=$((end - franksize))
dd if=resources/blankfile bs=1 count=$tempnum of=out/padding
cat out/padding out/tail.img > out/newtail.img
cat out/franken.img out/newtail.img > out/new_Image
else
echo "##### ERROR : Your initramfs is still BIGGER than the stock initramfs #####"
exit
fi
#============================================
# rebuild zImage
#============================================
echo "#=========================================="
echo "##### Now we are rebuilding the zImage #####"
echo "#=========================================="
cd resources/2.6.29
cp ../../out/new_Image arch/arm/boot/Image
#1. Image -> piggy.gz
echo "##### 09. Image ---> piggy.gz"
gzip -f -9 < arch/arm/boot/compressed/../Image > arch/arm/boot/compressed/piggy.gz
#2. piggy.gz -> piggy.o
echo "##### 10. piggy.gz ---> piggy.o"
$COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.piggy.o.d -nostdinc -isystem toolchain_resources/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -D__ASSEMBLY__ -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -gdwarf-2 -Wa,-march=all -c -o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/piggy.S
#3. head.o
echo "##### 11. Compiling head"
$COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.head.o.d -nostdinc -isystem toolchain_resources/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -D__ASSEMBLY__ -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -gdwarf-2 -Wa,-march=all -c -o arch/arm/boot/compressed/head.o arch/arm/boot/compressed/head.S
#4. misc.o
echo "##### 12. Compiling misc"
$COMPILER-gcc -Wp,-MD,arch/arm/boot/compressed/.misc.o.d -nostdinc -isystem toolchain_resources/include -Dlinux -Iinclude -Iarch/arm/include -include include/linux/autoconf.h -D__KERNEL__ -mlittle-endian -Iarch/arm/mach-s5pc110/include -Iarch/arm/plat-s5pc11x/include -Iarch/arm/plat-s3c/include -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs -fno-strict-aliasing -fno-common -Werror-implicit-function-declaration -Os -marm -fno-omit-frame-pointer -mapcs -mno-sched-prolog -mabi=aapcs-linux -mno-thumb-interwork -D__LINUX_ARM_ARCH__=7 -mcpu=cortex-a8 -msoft-float -Uarm -fno-stack-protector -I/modules/include -fno-omit-frame-pointer -fno-optimize-sibling-calls -g -Wdeclaration-after-statement -Wno-pointer-sign -fwrapv -fpic -fno-builtin -Dstatic= -D"KBUILD_STR(s)=\#s" -D"KBUILD_BASENAME=KBUILD_STR(misc)" -D"KBUILD_MODNAME=KBUILD_STR(misc)" -c -o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/misc.c
#5. head.o + misc.o + piggy.o --> vmlinux
echo "##### 13. head.o + misc.o + piggy.o ---> vmlinux"
$COMPILER-ld -EL --defsym zreladdr=0x30008000 --defsym params_phys=0x30000100 -p --no-undefined -X toolchain_resources/libgcc.a -T arch/arm/boot/compressed/vmlinux.lds arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/misc.o -o arch/arm/boot/compressed/vmlinux
#6. vmlinux -> zImage
echo "##### 14. vmlinux ---> zImage"
$COMPILER-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage
# finishing
echo "##### 15. Getting finished!!"
cp -f arch/arm/boot/zImage ../../new_zImage
rm -r arch/arm/boot/compressed/vmlinux arch/arm/boot/compressed/piggy.o arch/arm/boot/compressed/misc.o arch/arm/boot/compressed/head.o arch/arm/boot/compressed/piggy.gz arch/arm/boot/Image arch/arm/boot/zImage ../../out
echo "#=========================================="
echo "##### DONE!! #####"
echo "#=========================================="