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