xref: /OK3568_Linux_fs/u-boot/scripts/android2distro.sh (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1#!/bin/bash
2#
3# Copyright (c) 2021 Rockchip Electronics Co., Ltd
4#
5# SPDX-License-Identifier: GPL-2.0
6#
7
8set -e
9
10function usage()
11{
12	echo
13	echo "usage:"
14	echo "    $0 -f [Android boot.img] -o [Distro(ext2) boot.img]"
15	echo
16}
17
18function args_process()
19{
20	if [ $# -ne 4 ]; then
21		usage
22		exit 1
23	fi
24
25	while [ $# -gt 0 ]; do
26		case $1 in
27			-f)
28				BOOT_IMG=$2
29				shift 2
30				;;
31			-o)
32				DISTRO_IMG=$2
33				shift 2
34				;;
35			*)
36				usage
37				exit 1
38				;;
39		esac
40	done
41
42	if [ ! -f ${BOOT_IMG} ]; then
43		echo "ERROR: No ${ITB}"
44		exit 1
45	fi
46}
47
48function android2distro()
49{
50	rm distro/ boot/ ${DISTRO_IMG} -rf
51	mkdir -p boot
52
53	./scripts/unpack_bootimg --boot_img ${BOOT_IMG} --out distro/
54	./scripts/unpack_resource.sh distro/second distro/
55	BOOTARGS=`fdtget -ts distro/rk-kernel.dtb /chosen bootargs`
56
57	cp distro/rk-kernel.dtb boot/rk-kernel.dtb
58	cp distro/kernel boot/kernel
59	cp distro/ramdisk boot/ramdisk
60	mkdir -p boot/extlinux
61	touch boot/extlinux/extlinux.conf
62	echo "label rockchip-linux-kernel" >> boot/extlinux/extlinux.conf
63	echo "    kernel /kernel" >> boot/extlinux/extlinux.conf
64	echo "    fdt /rk-kernel.dtb" >> boot/extlinux/extlinux.conf
65	echo "    initrd /ramdisk" >> boot/extlinux/extlinux.conf
66	echo "    append ${BOOTARGS}" >> boot/extlinux/extlinux.conf
67
68	SIZE_KB=`ls -lh ${BOOT_IMG} | awk '{ print $5 }' | tr -d 'M'`
69	SIZE_KB=`echo "scale=0;$SIZE_KB/1"|bc -l` # for align down integer
70	SIZE_KB=`expr ${SIZE_KB} + 2 + 1`
71	SIZE_BYTE=$((${SIZE_KB}*1024))
72	genext2fs -b ${SIZE_BYTE} -B 1024 -d boot/ -i 8192 -U ${DISTRO_IMG}
73
74	echo
75	echo "Successful: ${DISTRO_IMG} is ready."
76	echo
77}
78
79args_process $*
80android2distro
81