images: fix boot failures on NAND with small sub pages
[oweals/openwrt.git] / scripts / ubinize-image.sh
1 #!/bin/sh
2
3 . $TOPDIR/scripts/functions.sh
4
5 part=""
6 ubootenv=""
7 ubinize_param=""
8 kernel=""
9 rootfs=""
10 outfile=""
11 err=""
12
13 ubivol() {
14         volid=$1
15         name=$2
16         image=$3
17         autoresize=$4
18         size="$5"
19         echo "[$name]"
20         echo "mode=ubi"
21         echo "vol_id=$volid"
22         echo "vol_type=dynamic"
23         echo "vol_name=$name"
24         if [ "$image" ]; then
25                 echo "image=$image"
26                 [ -n "$size" ] && echo "vol_size=${size}"
27         else
28                 echo "vol_size=1MiB"
29         fi
30         if [ "$autoresize" ]; then
31                 echo "vol_flags=autoresize"
32         fi
33 }
34
35 ubilayout() {
36         local vol_id=0
37         local rootsize=
38         local autoresize=
39         local rootfs_type="$( get_fs_type "$2" )"
40
41         if [ "$1" = "ubootenv" ]; then
42                 ubivol $vol_id ubootenv
43                 vol_id=$(( $vol_id + 1 ))
44                 ubivol $vol_id ubootenv2
45                 vol_id=$(( $vol_id + 1 ))
46         fi
47         for part in $parts; do
48                 name="${part%%=*}"
49                 prev="$part"
50                 part="${part#*=}"
51                 [ "$prev" = "$part" ] && part=
52
53                 image="${part%%=*}"
54                 prev="$part"
55                 part="${part#*=}"
56                 [ "$prev" = "$part" ] && part=
57
58                 size="$part"
59
60                 ubivol $vol_id "$name" "$image" "" "${size}MiB"
61                 vol_id=$(( $vol_id + 1 ))
62         done
63         if [ "$3" ]; then
64                 ubivol $vol_id kernel "$3"
65                 vol_id=$(( $vol_id + 1 ))
66         fi
67
68         case "$rootfs_type" in
69         "ubifs")
70                 autoresize=1
71                 ;;
72         "squashfs")
73                 # squashfs uses 1k block size, ensure we do not
74                 # violate that
75                 rootsize="$( round_up "$( stat -c%s "$2" )" 1024 )"
76                 ;;
77         esac
78         ubivol $vol_id rootfs "$2" "$autoresize" "$rootsize"
79
80         vol_id=$(( $vol_id + 1 ))
81         [ "$rootfs_type" = "ubifs" ] || ubivol $vol_id rootfs_data "" 1
82 }
83
84 while [ "$1" ]; do
85         case "$1" in
86         "--uboot-env")
87                 ubootenv="ubootenv"
88                 shift
89                 continue
90                 ;;
91         "--kernel")
92                 kernel="$2"
93                 shift
94                 shift
95                 continue
96                 ;;
97         "--part")
98                 parts="$parts $2"
99                 shift
100                 shift
101                 continue
102                 ;;
103         "-"*)
104                 ubinize_param="$@"
105                 break
106                 ;;
107         *)
108                 if [ ! "$rootfs" ]; then
109                         rootfs=$1
110                         shift
111                         continue
112                 fi
113                 if [ ! "$outfile" ]; then
114                         outfile=$1
115                         shift
116                         continue
117                 fi
118                 ;;
119         esac
120 done
121
122 if [ ! -r "$rootfs" -o ! -r "$kernel" -a ! "$outfile" ]; then
123         echo "syntax: $0 [--uboot-env] [--part <name>=<file>] [--kernel kernelimage] rootfs out [ubinize opts]"
124         exit 1
125 fi
126
127 ubinize="$( which ubinize )"
128 if [ ! -x "$ubinize" ]; then
129         echo "ubinize tool not found or not usable"
130         exit 1
131 fi
132
133 ubinizecfg="$( mktemp 2> /dev/null )"
134 if [ -z "$ubinizecfg" ]; then
135         # try OSX signature
136         ubinizecfg="$( mktemp -t 'ubitmp' )"
137 fi
138 ubilayout "$ubootenv" "$rootfs" "$kernel" > "$ubinizecfg"
139
140 cat "$ubinizecfg"
141 ubinize -o "$outfile" $ubinize_param "$ubinizecfg"
142 err="$?"
143 [ ! -e "$outfile" ] && err=2
144 rm "$ubinizecfg"
145
146 exit $err