6ffedc5f32620deec6c9de494001fa59a32638be
[oweals/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         case "$1" in
61         "--uboot-env")
62                 ubootenv="ubootenv"
63                 shift
64                 continue
65                 ;;
66         "--no-kernel")
67                 nokernel="nokernel"
68                 shift
69                 continue
70                 ;;
71         "-"*)
72                 ubinize_param="$@"
73                 break
74                 ;;
75         *)
76                 if [ ! "$kernel" -a ! "$nokernel" ]; then
77                         kernel=$1
78                         shift
79                         continue
80                 fi
81                 if [ ! "$rootfs" ]; then
82                         rootfs=$1
83                         shift
84                         continue
85                 fi
86                 if [ ! "$outfile" ]; then
87                         outfile=$1
88                         shift
89                         continue
90                 fi
91                 ;;
92         esac
93 done
94
95 if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$nokernel" -o ! "$outfile" ]; then
96         echo "syntax: $0 [--no-kernel] [--uboot-env] rootfs [kernel] out [ubinize opts]"
97         exit 1
98 fi
99
100 ubinize="$( which ubinize )"
101 if [ ! -x "$ubinize" ]; then
102         echo "ubinize tool not found or not usable"
103         exit 1
104 fi
105
106 ubinizecfg="$( mktemp )"
107 ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
108
109 cat "$ubinizecfg"
110 ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
111 err="$?"
112 [ ! -e "$outfile" ] && err=2
113 rm "$ubinizecfg"
114
115 exit $err
116