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