Modify to copy "root hash verifying script" to initrd-recovery
[platform/core/system/initrd-recovery.git] / src / initrd-recovery / mkinitrd-recovery.sh.in
1 #!/bin/sh
2
3 PATH=/bin:/usr/bin:/sbin:/usr/sbin
4
5 CP="/bin/cp"
6 LN="/bin/ln"
7 SED="/bin/sed"
8 MKDIR="/bin/mkdir"
9 PARTX="/usr/sbin/partx"
10 BLKID="/usr/sbin/blkid"
11 DIRNAME="/usr/bin/dirname"
12 MKDIR="/bin/mkdir"
13 RM="/bin/rm"
14 TR="/bin/tr"
15 GREP="/bin/grep"
16 CUT="/bin/cut"
17
18 UNAME="/bin/uname"
19 ARCH=`$UNAME -m`
20
21 if [ "z$ARCH" == "zarmv7l" ]; then
22         LD_LINUX="/lib/ld-linux.so.3"
23 elif [ "z$ARCH" == "zaarch64" ]; then
24         LD_LINUX="/lib64/ld-linux-aarch64.so.1"
25 elif [ "z$ARCH" == "zi686" ]; then
26         LD_LINUX="/lib/ld-linux.so.2"
27 elif [ "z$ARCH" == "zx86-64" ]; then
28         LD_LINUX="/lib64/ld-linux-x86-64.so.2"
29 else
30         # set as armv7l, need to assign properly
31         LD_LINUX="/lib/ld-linux.so.3"
32 fi
33
34 DMVERITY_ROOTFS_VERIFY_HASH="/usr/lib/initrd/bin/dmverity-rootfs-verify-hash.sh"
35
36 INITRD_ROOT="/mnt/initrd-recovery"
37 HAL_LIST="${INITRD_ROOT}/hal/.hal_list"
38
39 OBJECTS_SPECIFY_DIR="@INITRD_RECOVERY_INSTALL_DROPIN_DIR@"
40 OBJECTS_DIRECTORY=
41 OBJECTS_VERBATIM=
42 OBJECTS_WITHLIB=
43 OBJECTS_LIBONLY=
44 OBJECTS_SYMLINK=
45 OBJECTS_MVWITHLIB=
46
47 BASE_DIRECTORIES="
48 /dev
49 /etc
50 /proc
51 /sdcard
52 /smack
53 /sys
54 /system-ro
55 /run
56 /run/upgrade-sysroot
57 /tmp
58 /usr/bin
59 /usr/sbin
60 /usr/lib
61 /var/log
62 /hal
63 "
64
65 BASE_DIR_SYMLINKS="
66 /bin:usr/bin
67 /sbin:usr/sbin
68 /lib:usr/lib
69 /opt:run/upgrade-sysroot/opt
70 "
71
72 OPT="default"
73
74 #-----------------------------------------------------------------------------
75 #       help
76 #-----------------------------------------------------------------------------
77 show_help() {
78     echo
79     echo "usage: `basename $0` [OPTION]"
80     echo "make initrd image"
81     echo
82     echo "  -h, --help          show help"
83     echo "  -p, --post          find initrd partition and format and make"
84     echo "                       initrd image to there"
85     echo
86 }
87
88 #-----------------------------------------------------------------------------
89 #       find initrd-recovery partition
90 #-----------------------------------------------------------------------------
91 find_initrd_recovery_partition() {
92     EMMC_DEVICE="/dev/mmcblk0"
93     RET_PARTX=$("$PARTX" -s ${EMMC_DEVICE})
94     TEST=$(echo "$RET_PARTX" | "$TR" -s ' ' | "$SED" -e '1d' -e 's/^ //' | "$CUT" -d ' ' -f 6)
95     if [ "z$TEST" == "z" ]; then
96         PART_INITRD=$("$BLKID" -L "ramdisk-recovery" -o device)
97         if [ "z$PART_INITRD" == "z" ]; then
98             PART_INITRD=$("$BLKID" -L "ramdisk" -o device)
99         fi
100     else
101         PART_INITRD=${EMMC_DEVICE}p$(
102             echo "$RET_PARTX" |
103             "$TR" -s ' ' | "$TR" '[:upper:]' '[:lower:]' |
104             "$GREP" "ramdisk2" | "$SED" 's/^ //' | "$CUT" -d ' ' -f 1)
105         if [ "z$PART_INITRD" == "z/dev/mmcblk0p" ]; then
106             PART_INITRD=${EMMC_DEVICE}p$(
107                 echo "$RET_PARTX" |
108                 "$TR" -s ' ' | "$TR" '[:upper:]' '[:lower:]' |
109                 "$GREP" "ramdisk" | "$SED" 's/^ //' | "$CUT" -d ' ' -f 1)
110         fi
111     fi
112 }
113
114 #-----------------------------------------------------------------------------
115 #       Prepare parent directory
116 #-----------------------------------------------------------------------------
117 mkdir_p_parent() {
118     dst_dir=`"$DIRNAME" "$1"`
119     if [ ! -d "$dst_dir" -a ! -L "$dst_dir" ]; then
120         "$MKDIR" -p "$dst_dir"
121     fi
122 }
123
124 #-----------------------------------------------------------------------------
125 #       Copy content to root of mounted image
126 #-----------------------------------------------------------------------------
127 do_copy() {
128     src=$1
129     dst="$INITRD_ROOT/$src"
130
131     path_prefix=$(echo $src | "$CUT" -d '/' -f 2)
132
133     if [ "$path_prefix" == "hal" ]; then
134         remain=$(echo $src | "$SED" 's/\/hal\///')
135         echo "$remain" >> $HAL_LIST
136     fi
137
138     if [ ! -e "$src" -o -e "$dst" ]; then
139         return
140     fi
141
142     mkdir_p_parent $dst
143
144     "$CP" -f "$src" "$dst"
145 }
146
147 #-----------------------------------------------------------------------------
148 #       Get dependency libraries
149 #-----------------------------------------------------------------------------
150 get_dep_libs() {
151     "$LD_LINUX" --list $1 | "$SED" -r 's|^[^/]+([^ \(]+).*$|\1|'
152 }
153
154 #-----------------------------------------------------------------------------
155 #       Gather initrd objects
156 #-----------------------------------------------------------------------------
157 get_initrd_objects() {
158     for f in $(ls ${OBJECTS_SPECIFY_DIR}); do
159         DIRECTORIES=
160         DIR_SYMLINKS=
161         VERBATIMS=
162         WITHLIBS=
163         LIBONLYS=
164         SYMLINKS=
165         MVWITHLIBS=
166         source "${OBJECTS_SPECIFY_DIR}"/$f
167         OBJECTS_DIRECTORY="$OBJECTS_DIRECTORY $DIRECTORIES"
168         OBJECTS_DIR_SYMLINK="$OBJECTS_DIR_SYMLINK $DIR_SYMLINKS"
169         OBJECTS_VERBATIM="$OBJECTS_VERBATIM $VERBATIMS"
170         OBJECTS_WITHLIB="$OBJECTS_WITHLIB $WITHLIBS"
171         OBJECTS_LIBONLY="$OBJECTS_LIBONLY $LIBONLYS"
172         OBJECTS_SYMLINK="$OBJECTS_SYMLINK $SYMLINKS"
173         OBJECTS_MVWITHLIB="$OBJECTS_MVWITHLIB $MVWITHLIBS"
174     done
175
176     OBJECTS_DIRECTORY=$(echo "$OBJECTS_DIRECTORY" | sort | uniq)
177     OBJECTS_DIR_SYMLINK=$(echo "$OBJECTS_DIR_SYMLINK" | sort | uniq)
178     OBJECTS_VERBATIM=$(echo "$OBJECTS_VERBATIM" | sort | uniq)
179     OBJECTS_WITHLIB=$(echo "$OBJECTS_WITHLIB" | sort | uniq)
180     OBJECTS_LIBONLY=$(echo "$OBJECTS_LIBONLY" | sort | uniq)
181     OBJECTS_SYMLINK=$(echo "$OBJECTS_SYMLINK" | sort | uniq)
182     OBJECTS_MVWITHLIB=$(echo "$OBJECTS_MVWITHLIB" | sort | uniq)
183 }
184
185 #-----------------------------------------------------------------------------
186 #       Prepare directory objects
187 #-----------------------------------------------------------------------------
188 prepare_directory_objects() {
189     for dir in $@; do
190         "$MKDIR" -p "$INITRD_ROOT$dir"
191     done
192 }
193
194 #-----------------------------------------------------------------------------
195 #       Copy verbatim objects
196 #-----------------------------------------------------------------------------
197 verbatim_objects() {
198     for obj in $@; do
199         do_copy $obj
200     done
201 }
202
203 #-----------------------------------------------------------------------------
204 #       Copy withlib objects
205 #-----------------------------------------------------------------------------
206 withlib_objects() {
207     for content in $@; do
208         do_copy $content
209
210         DEP_LIBS=$(get_dep_libs $content)
211         for lib in $DEP_LIBS; do
212             do_copy $lib
213         done
214     done
215 }
216
217 mvwithlib_objects() {
218     for content in $@; do
219
220         do_copy $content
221
222         "$LD_LINUX" --verify $content
223         if [ $? -eq 0 ]; then
224             DEP_LIBS=$(get_dep_libs $content)
225             for lib in $DEP_LIBS; do
226                 do_copy $lib
227             done
228         fi
229
230         "$RM" -rf $content
231     done
232 }
233
234 #-----------------------------------------------------------------------------
235 #       Copy libonly objects
236 #-----------------------------------------------------------------------------
237 libonly_objects() {
238     for content in $@; do
239         DEP_LIBS=$(get_dep_libs $content)
240         for lib in $DEP_LIBS; do
241             do_copy $lib
242         done
243     done
244 }
245
246 #-----------------------------------------------------------------------------
247 #       Copy symlink objects
248 #-----------------------------------------------------------------------------
249 symlink_objects() {
250     for i in $@; do
251         if [ "z$i" == "z" ]; then
252             continue
253         fi
254
255         link=${i%:*}
256         target=${i#*:}
257         mkdir_p_parent "$INITRD_ROOT$link"
258         "$LN" -s "$target" "$INITRD_ROOT$link"
259     done
260 }
261
262 #-----------------------------------------------------------------------------
263 #       Copy content
264 #-----------------------------------------------------------------------------
265 make_initrd_recovery() {
266     echo "copy initrd objects to $INITRD_ROOT"
267     get_initrd_objects
268
269     prepare_directory_objects $BASE_DIRECTORIES
270     symlink_objects $BASE_DIR_SYMLINKS
271     prepare_directory_objects $OBJECTS_DIRECTORY
272     symlink_objects $OBJECTS_DIR_SYMLINK
273     verbatim_objects $OBJECTS_VERBATIM
274     withlib_objects $OBJECTS_WITHLIB
275     libonly_objects $OBJECTS_LIBONLY
276     symlink_objects $OBJECTS_SYMLINK
277     mvwithlib_objects $OBJECTS_MVWITHLIB
278
279     "$CP" "$DMVERITY_ROOTFS_VERIFY_HASH" "$INITRD_ROOT/usr/bin/dmverity-rootfs-verify-hash.sh"
280
281     /usr/sbin/ldconfig -r $INITRD_ROOT
282 }
283
284 #-----------------------------------------------------------------------------
285 #       Check given parameter is mount point
286 #-----------------------------------------------------------------------------
287 check_mount_point() {
288     grep " $1 " /etc/mtab > /dev/null
289     if [ $? -ne 0 ]; then
290         echo "$1 is not mount point"
291         exit 0
292     fi
293 }
294
295 #-----------------------------------------------------------------------------
296 #       Main
297 #-----------------------------------------------------------------------------
298 ARGS=$(getopt -o hp -l "help,post" -n "$0" -- "$@");
299
300 eval set -- "$ARGS";
301
302 while true; do
303     case "$1" in
304         -h|--help)
305             show_help >&2
306             exit 0
307             ;;
308         -p|--post)
309             OPT="post"
310             shift
311             ;;
312         --)
313             shift;
314             break;
315             ;;
316     esac
317 done
318
319 if [ $# -gt 1 ]; then
320     echo "Error: too many argument was given."
321     show_help >&2
322     exit 1
323 fi
324
325 case $OPT in
326     post)
327         find_initrd_recovery_partition
328         if [ "z$PART_INITRD" == "z/dev/mmcblk0p" ]; then
329             echo "Error: failed to find initrd partition"
330             exit 1
331         else
332             echo "Info: initrd partition: $PART_INITRD"
333         fi
334         mke2fs $PART_INITRD
335         e2fsck $PART_INITRD
336         mount $PART_INITRD $INITRD_ROOT
337         make_initrd_recovery
338         umount $INITRD_ROOT
339         ;;
340     *)
341         check_mount_point $INITRD_ROOT
342         make_initrd_recovery
343         ;;
344 esac