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