kernel: add a NAND_SUPPORT symbol
[openwrt.git] / scripts / ubinize-image.sh
1 #!/bin/sh
2
3 ubootenv=""
4 nokernel=""
5 ubinize_param=""
6 kernel=""
7 rootfs=""
8 outfile=""
9 err=""
10
11 get_magic_word() {
12         dd if=$1 bs=2 count=1 2>/dev/null | hexdump -v -n 2 -e '1/1 "%02x"'
13 }
14
15 is_ubifs() {
16         if [ "$( get_magic_word $1 )" = "3118" ]; then
17                 echo "1"
18         fi
19 }
20
21 ubivol() {
22         volid=$1
23         name=$2
24         image=$3
25         autoresize=$4
26         echo "[$name]"
27         echo "mode=ubi"
28         echo "vol_id=$volid"
29         echo "vol_type=dynamic"
30         echo "vol_name=$name"
31         if [ "$image" ]; then
32                 echo "image=$image"
33         else
34                 echo "vol_size=1MiB"
35         fi
36         if [ "$autoresize" ]; then
37                 echo "vol_flags=autoresize"
38         fi
39 }
40
41 ubilayout() {
42         local vol_id=0
43         local root_is_ubifs="$( is_ubifs "$2" )"
44         if [ "$1" = "ubootenv" ]; then
45                 ubivol $vol_id ubootenv
46                 vol_id=$(( $vol_id + 1 ))
47                 ubivol $vol_id ubootenv2
48                 vol_id=$(( $vol_id + 1 ))
49         fi
50         if [ "$3" ]; then
51                 ubivol $vol_id kernel "$3"
52                 vol_id=$(( $vol_id + 1 ))
53         fi
54         ubivol $vol_id rootfs "$2" $root_is_ubifs
55         vol_id=$(( $vol_id + 1 ))
56         [ "$root_is_ubifs" ] || ubivol $vol_id rootfs_data "" 1
57 }
58
59 while [ "$1" ]; do
60         if [ "$1" = "--uboot-env" ]; then
61                 ubootenv="ubootenv"
62                 shift
63                 continue
64         fi
65         if [ "$1" = "--no-kernel" ]; then
66                 nokernel="nokernel"
67                 shift
68                 continue
69         fi
70         if [ ! "$kernel" -a ! "$nokernel" ]; then
71                 [ "${1:0:1}" = "-" ] && break
72                 kernel=$1
73                 shift
74                 continue
75         fi
76         if [ ! "$rootfs" ]; then
77                 [ "${1:0:1}" = "-" ] && break
78                 rootfs=$1
79                 shift
80                 continue
81         fi
82         if [ ! "$outfile" ]; then
83                 [ "${1:0:1}" = "-" ] && break
84                 outfile=$1
85                 shift
86                 continue
87         fi
88         ubinize_param="$@"
89         break
90 done
91
92 if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$nokernel" -o ! "$outfile" ]; then
93         echo "syntax: $0 [--no-kernel] [--uboot-env] rootfs [kernel] out [ubinize opts]"
94         exit 1
95 fi
96
97 ubinize="$( which ubinize )"
98 if [ ! -x "$ubinize" ]; then
99         echo "ubinize tool not found or not usable"
100         exit 1
101 fi
102
103 ubinizecfg="$( mktemp )"
104 ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
105
106 cat "$ubinizecfg"
107 ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
108 err="$?"
109 [ ! -e "$outfile" ] && err=2
110 rm "$ubinizecfg"
111
112 exit $err
113