Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.95 KB | None | 0 0
  1. #!/bin/sh
  2. ##############################################################################
  3. # usage : ./editor.sh [kernel] [initramfs.cpio] #
  4. # example : ./editor.sh /home/zero/Desktop/test/zImage /home/zero/Desktop/test/ramdisk.cpio #
  5. ##############################################################################
  6. # you should point where your cross-compiler is #
  7. COMPILER=/media/Library/Library/kernel_stuff/arm-none-eabi-4.3.4/bin/arm-none-eabi #
  8. ##############################################################################
  9.  
  10. zImage=$1
  11. new_ramdisk=$2
  12. determiner=0
  13. Image_here="./out/Image"
  14.  
  15. echo "##### My name is $0 #####"
  16. echo "##### The kernel is $1 #####"
  17. echo "##### The ramdisk is $2 #####"
  18. if [ $1 = "" ]; then
  19. echo "##### You should point where the zImage file is #####"
  20. exit
  21. elif [ $2 = "" ]; then
  22. echo "##### You should point where your new initramfs is #####"
  23. exit
  24. fi
  25.  
  26. #=======================================================
  27. # find start of gziped kernel object in the zImage file:
  28. #=======================================================
  29.  
  30. pos=`grep -P -a -b --only-matching '\x1F\x8B\x08' $zImage | cut -f 1 -d :`
  31. echo "##### 01. Extracting kernel from $zImage (start = $pos)"
  32. mkdir out
  33. dd if=$zImage bs=1 skip=$pos | gunzip > $Image_here
  34.  
  35. #===========================================================================
  36. # find start and end of the "cpio" initramfs inside the kernel object:
  37. # ASCII cpio header starts with '070701'
  38. # The end of the cpio archive is marked with an empty file named TRAILER!!!
  39. #===========================================================================
  40. start=`grep -a -b --only-matching '070701' $Image_here | head -1 | cut -f 1 -d :`
  41. end=`grep -a -b --only-matching 'TRAILER!!!' $Image_here | head -1 | cut -f 1 -d :`
  42.  
  43. end=$((end + 10))
  44. count=$((end - start))
  45.  
  46. if [ $count -lt $determiner ]; then
  47. echo "##### ERROR : Couldn't match start/end of the initramfs ."
  48. exit
  49. fi
  50.  
  51. # Check the Image's size
  52. filesize=`ls -l $Image_here | awk '{print $5}'`
  53. echo "##### 02. The size of the Image is $filesize"
  54.  
  55. # Split the Image #1 -> head.img
  56. echo "##### 03. Making a head.img ( from 0 ~ $start )"
  57. dd if=$Image_here bs=1 count=$start of=out/head.img
  58.  
  59. # Split the Image #2 -> tail.img
  60. echo "##### 04. Making a tail.img ( from $end ~ $filesize )"
  61. dd if=$Image_here bs=1 skip=$end of=out/tail.img
  62.  
  63. # Check the new ramdisk's size
  64. ramdsize=`ls -l $new_ramdisk | awk '{print $5}'`
  65. echo "##### 05. The size of the new ramdisk is = $ramdsize"
  66.  
  67. echo "##### 06. Checking if ramdsize is bigger than the stock one"
  68. if [ $ramdsize -gt $count ]; then
  69. cp $new_ramdisk out/ramdisk.backup
  70. cat out/ramdisk.backup | gzip -f -9 > out/ramdisk.cpio
  71. echo "##### Your initramfs needs to be gzipped!! ###"
  72. else
  73. cp $new_ramdisk out/ramdisk.cpio
  74. fi
  75.  
  76. # FrankenStein is being made #1
  77. echo "##### 07. Merging head + ramdisk"
  78. cat out/head.img out/ramdisk.cpio > out/franken.img
  79.  
  80. echo "##### 08. Checking the size of [head+ramdisk]"
  81. franksize=`ls -l out/franken.img | awk '{print $5}'`
  82.  
  83. # FrankenStein is being made #2
  84. echo "##### 09. Merging [head+ramdisk] + padding + tail"
  85. if [ $franksize -lt $end ]; then
  86. tempnum=$((end - franksize))
  87. dd if=resources/blankfile bs=1 count=$tempnum of=out/padding
  88. cat out/padding out/tail.img > out/newtail.img
  89. cat out/franken.img out/newtail.img > out/new_Image
  90. else
  91. echo "##### ERROR : Your initramfs is still BIGGER than the stock initramfs #####"
  92. exit
  93. fi
  94.  
  95.  
  96. #============================================
  97. # rebuild zImage
  98. #============================================
  99. echo "##### Now we are rebuilding the zImage #####"
  100.  
  101. cd resources/2.6.29
  102. cp ../../out/new_Image arch/arm/boot/Image
  103.  
  104. #1. Image -> piggy.gz
  105. echo "##### 10. Image ---> piggy.gz"
  106. gzip -f -9 < arch/arm/boot/compressed/../Image > arch/arm/boot/compressed/piggy.gz
  107.  
  108. #2. piggy.gz -> piggy.o
  109. echo "##### 11. piggy.gz ---> piggy.o"
  110. $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
  111.  
  112. #3. head.o
  113. echo "##### 12. Compiling head"
  114. $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
  115.  
  116. #4. misc.o
  117. echo "##### 13. Compiling misc"
  118. $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
  119.  
  120. #5. head.o + misc.o + piggy.o --> vmlinux
  121. echo "##### 14. head.o + misc.o + piggy.o ---> vmlinux"
  122. $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
  123.  
  124. #6. vmlinux -> zImage
  125. echo "##### 15. vmlinux ---> zImage"
  126. $COMPILER-objcopy -O binary -R .note -R .note.gnu.build-id -R .comment -S arch/arm/boot/compressed/vmlinux arch/arm/boot/zImage
  127.  
  128. # finishing
  129. echo "##### 16. Getting finished!!"
  130. mv arch/arm/boot/zImage ../../new_zImage
  131. rm 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
  132. rm -r ../../out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement