imagebuilder: properly escape single quotes in device titles
[openwrt/staging/yousong.git] / scripts / ubinize-image.sh
1 #!/bin/sh
2
3 part=""
4 ubootenv=""
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 size="$5"
27 echo "[$name]"
28 echo "mode=ubi"
29 echo "vol_id=$volid"
30 echo "vol_type=dynamic"
31 echo "vol_name=$name"
32 if [ "$image" ]; then
33 echo "image=$image"
34 [ -n "$size" ] && echo "vol_size=${size}MiB"
35 else
36 echo "vol_size=1MiB"
37 fi
38 if [ "$autoresize" ]; then
39 echo "vol_flags=autoresize"
40 fi
41 }
42
43 ubilayout() {
44 local vol_id=0
45 local root_is_ubifs="$( is_ubifs "$2" )"
46 if [ "$1" = "ubootenv" ]; then
47 ubivol $vol_id ubootenv
48 vol_id=$(( $vol_id + 1 ))
49 ubivol $vol_id ubootenv2
50 vol_id=$(( $vol_id + 1 ))
51 fi
52 for part in $parts; do
53 name="${part%%=*}"
54 prev="$part"
55 part="${part#*=}"
56 [ "$prev" = "$part" ] && part=
57
58 image="${part%%=*}"
59 prev="$part"
60 part="${part#*=}"
61 [ "$prev" = "$part" ] && part=
62
63 size="$part"
64
65 ubivol $vol_id "$name" "$image" "" "$size"
66 vol_id=$(( $vol_id + 1 ))
67 done
68 if [ "$3" ]; then
69 ubivol $vol_id kernel "$3"
70 vol_id=$(( $vol_id + 1 ))
71 fi
72 ubivol $vol_id rootfs "$2" $root_is_ubifs
73 vol_id=$(( $vol_id + 1 ))
74 [ "$root_is_ubifs" ] || ubivol $vol_id rootfs_data "" 1
75 }
76
77 while [ "$1" ]; do
78 case "$1" in
79 "--uboot-env")
80 ubootenv="ubootenv"
81 shift
82 continue
83 ;;
84 "--kernel")
85 kernel="$2"
86 shift
87 shift
88 continue
89 ;;
90 "--part")
91 parts="$parts $2"
92 shift
93 shift
94 continue
95 ;;
96 "-"*)
97 ubinize_param="$@"
98 break
99 ;;
100 *)
101 if [ ! "$rootfs" ]; then
102 rootfs=$1
103 shift
104 continue
105 fi
106 if [ ! "$outfile" ]; then
107 outfile=$1
108 shift
109 continue
110 fi
111 ;;
112 esac
113 done
114
115 if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$outfile" ]; then
116 echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] rootfs out [ubinize opts]"
117 exit 1
118 fi
119
120 ubinize="$( which ubinize )"
121 if [ ! -x "$ubinize" ]; then
122 echo "ubinize tool not found or not usable"
123 exit 1
124 fi
125
126 ubinizecfg="$( mktemp 2> /dev/null )"
127 if [ -z "$ubinizecfg" ]; then
128 # try OSX signature
129 ubinizecfg="$( mktemp -t 'ubitmp' )"
130 fi
131 ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
132
133 cat "$ubinizecfg"
134 ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
135 err="$?"
136 [ ! -e "$outfile" ] && err=2
137 rm "$ubinizecfg"
138
139 exit $err