2
0

pad_image 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #!/usr/bin/env bash
  2. function usage {
  3. echo "Usage: prepare_image image_type kernel_image rootfs_image header_size"
  4. echo "Padd root and kernel image to the correct size and append the jffs2 start marker as needed"
  5. exit 1
  6. }
  7. function pad_file {
  8. echo "Padding $1 to size $2"
  9. dd if=$1 of=$1.paddingtempfile bs=$2 count=1 conv=sync &> /dev/null
  10. mv $1.paddingtempfile $1
  11. }
  12. #filesize filestart padding
  13. function calc_pad {
  14. [ $((($1 + $2) & ($3 - 1))) == 0 ] && {
  15. echo $1
  16. return 0
  17. }
  18. echo $(((($1 + $2) | ($3 - 1)) + 1 - $2))
  19. }
  20. function prep_squash {
  21. echo "kernel_size: $kernel_size"
  22. echo "header_size: $header_size"
  23. kernel_pad_size=$(calc_pad $kernel_size $header_size 32)
  24. kernel_end=$(($header_size + $kernel_pad_size))
  25. pad_file $kernel_image $kernel_pad_size
  26. #4k
  27. rootfs_pad_size=$(calc_pad $rootfs_size $kernel_end 4096)
  28. pad_file $rootfs_image $rootfs_pad_size
  29. echo -ne '\xde\xad\xc0\xde' >> $rootfs_image
  30. #8k
  31. rootfs_pad_size=$(calc_pad $rootfs_size $kernel_end 8192)
  32. [ $rootfs_pad_size == rootfs_old_padsize ] || {
  33. pad_file $rootfs_image $rootfs_pad_size
  34. rootfs_old_padsize=$rootfs_pad_size
  35. echo -ne '\xde\xad\xc0\xde' >> $rootfs_image
  36. }
  37. #64k
  38. rootfs_pad_size=$(calc_pad $rootfs_size $kernel_end 65536)
  39. [ $rootfs_pad_size == rootfs_old_padsize ] || {
  40. pad_file $rootfs_image $rootfs_pad_size
  41. rootfs_old_padsize=$rootfs_pad_size
  42. echo -ne '\xde\xad\xc0\xde' >> $rootfs_image
  43. }
  44. #128k
  45. rootfs_pad_size=$(calc_pad $rootfs_size $kernel_end 131072)
  46. [ $rootfs_pad_size == rootfs_old_padsize ] || {
  47. pad_file $rootfs_image $rootfs_pad_size
  48. rootfs_old_padsize=$rootfs_pad_size
  49. echo -ne '\xde\xad\xc0\xde' >> $rootfs_image
  50. }
  51. }
  52. function prep_jffs2 {
  53. kernel_pad_size=$(calc_pad $kernel_size $header_size $1)
  54. pad_file $kernel_image $kernel_pad_size
  55. }
  56. image_type=$1
  57. kernel_image=$2
  58. rootfs_image=$3
  59. header_size=$4
  60. if [ -z "$image_type" ] || [ -z "$rootfs_image" ] || [ -z "$kernel_image" ] || [ -z "$header_size" ]; then
  61. usage
  62. fi
  63. if [ ! -e "$rootfs_image" ] || [ -z "$kernel_image" ]; then
  64. echo "input file not found"
  65. exit 1
  66. fi
  67. kernel_size=$(stat -c "%s" "$kernel_image")
  68. rootfs_size=$(stat -c "%s" "$rootfs_image")
  69. if [ $kernel_size == 0 ] || [ $rootfs_size == 0 ]; then
  70. echo "kernel or rootfs empty"
  71. exit 1
  72. fi
  73. case $image_type in
  74. squashfs )
  75. prep_squash ;;
  76. jffs2-64k )
  77. prep_jffs2 65536 ;;
  78. jffs2-128k )
  79. prep_jffs2 131072 ;;
  80. * )
  81. echo "Unknown image type"
  82. exit 1 ;;
  83. esac