From a798fccb1c91c903bc156f7e804ba3d6c8764761 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 22 May 2024 17:04:36 +0800
Subject: [PATCH] Add image build shell script

---
 images/build.sh |  236 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 236 insertions(+), 0 deletions(-)

diff --git a/images/build.sh b/images/build.sh
new file mode 100755
index 0000000..66ac829
--- /dev/null
+++ b/images/build.sh
@@ -0,0 +1,236 @@
+#!/bin/bash
+
+# this project absolute path
+PRJ_PATH=$(cd $(dirname "${BASH_SOURCE[0]}") && pwd)
+
+# top project absolute path
+TOP_PATH=$(realpath $PRJ_PATH/..)
+
+# binaries build prefix install path
+PRFX_PATH=$PRJ_PATH/install
+
+# binaries finally install path if needed
+#INST_PATH=/tftp
+
+# download taballs path
+TARBALL_PATH=$PRJ_PATH/tarballs
+
+# config file path
+CONF_FILE=$TOP_PATH/config.json
+
+# shell script will exit once get command error
+set -e
+
+#+-------------------------+
+#| Shell script functions  |
+#+-------------------------+
+
+function pr_error() {
+    echo -e "\033[40;31m $1 \033[0m"
+}
+
+function pr_warn() {
+    echo -e "\033[40;33m $1 \033[0m"
+}
+
+function pr_info() {
+    echo -e "\033[40;32m $1 \033[0m"
+}
+
+# decompress a packet to destination path
+function do_unpack()
+{
+    tarball=$1
+    dstpath=`pwd`
+
+    if [[ $# == 2 ]] ; then
+        dstpath=$2
+    fi
+
+    pr_info "decompress $tarball => $dstpath"
+
+    mkdir -p $dstpath
+    case $tarball in
+        *.tar.gz)
+            tar -xzf $tarball -C $dstpath
+            ;;
+
+        *.tar.bz2)
+            tar -xjf $tarball -C $dstpath
+            ;;
+
+        *.tar.xz)
+            tar -xJf $tarball -C $dstpath
+            ;;
+
+        *.tar.zst)
+            tar -I zstd -xf $tarball -C $dstpath
+            ;;
+
+        *.tar)
+            tar -xf $tarball -C $dstpath
+            ;;
+
+        *.zip)
+            unzip -qo $tarball -d $dstpath
+            ;;
+
+        *)
+            pr_error "decompress Unsupport packet: $tarball"
+            return 1;
+            ;;
+    esac
+}
+
+# parser configure file and export environment variable
+function export_env()
+{
+    export BOARD=`jq -r ".bsp.board" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export BSP_VER=`jq -r ".bsp.version" $CONF_FILE | tr 'A-Z' 'a-z'`
+    export BSP_URL=`jq -r ".bsp.giturl" $CONF_FILE`
+    export CROSS_COMPILE=`jq -r ".bsp.crosstool" $CONF_FILE`
+
+    export JOBS=`cat /proc/cpuinfo | grep processor | wc -l`
+    export ARCH=arm
+
+	ROOTFS_SRC=rootfs
+	ROOTFS_IMG=rootfs-${BOARD}.ubi
+
+    export SRCS="rootfs"
+}
+
+function do_fetch()
+{
+    cd $PRJ_PATH
+
+    for src in $SRCS
+    do
+        if [ -d $src ] ; then
+            pr_info "$src source code fetched already"
+            continue
+        fi
+
+		pr_info "start fetch $src source code"
+		mkdir -p $TARBALL_PATH
+
+		# Download source code packet
+		if [ ! -s $TARBALL_PATH/$src.tar.xz ] ; then
+			wget $BSP_URL/at91/bsp/$BSP_VER/$src.tar.xz -P $TARBALL_PATH
+		fi
+
+		# decompress source code packet
+		do_unpack $TARBALL_PATH/$src.tar.xz $ROOTFS_SRC
+    done
+}
+
+function build_ubifs()
+{
+    ubinize_cfg=ubinize.cfg
+    ubimg_tmp=ubi.img
+
+    # Rootfs partition size
+    partition_size=100  # MiB
+
+    # K9F2G08: 1 block=64 pages,  1 page = 2048
+    BLOCK_PAGES=64
+    PAGE_SIZE=2048
+
+    ROOTFS_IMG=${ROOTFS_IMG}-p`expr $PAGE_SIZE \/ 1024`k
+
+    # Logic Erase Block Size: UBI requires 2 minimum I/O units out of each Physical Erase Block (PEB) for overhead:
+    #   1 for maintaining erase count information, and 1 for maintaining the Volume ID information.
+
+    PEB_SIZE=`expr $PAGE_SIZE \* $BLOCK_PAGES`
+    LEB_SIZE=`expr $PEB_SIZE - 2 \* $PAGE_SIZE `
+
+    #UBI reserves 4 blocks space for management and bad PEB handling operations
+    # 2 PEBs are used to store the UBI volume table
+    # 1 PEB is reserved for wear-leveling purposes;
+    # 1 PEB is reserved for the atomic LEB change operation;
+    # a% of PEBs is reserved for handling bad EBs. The default for NAND is 1%
+
+    PEB_CNT=`expr $partition_size \* 1024 \* 1024 / ${PEB_SIZE}`
+    LEB_CNT=`expr $PEB_CNT - 4 - $PEB_CNT \/ 100 `
+
+    #echo "Parition size ${partition_size}MiB and LEB=$LEB_CNT"
+    set -x
+    sudo mkfs.ubifs -F -d ${ROOTFS_SRC} -m ${PAGE_SIZE} -e ${LEB_SIZE} -c $LEB_CNT -o ${ubimg_tmp}
+    set +x
+
+    # vol_size smaller than the actual size of the partition to leave room for Ubifs internal data.
+    # With "vol_type=dynamic", Ubifs will end up using the whole space automatically
+    vol_size=`expr ${partition_size} \- 10`
+
+    printf "[ubifs] \nmode=ubi \nimage=${ubimg_tmp} \nvol_id=0 \nvol_size=${vol_size}MiB\n" > $ubinize_cfg
+    printf "vol_type=dynamic \nvol_name=rootfs \nvol_flags=autoresize\n" >> $ubinize_cfg
+    #cat $ubinize_cfg
+
+    set -x
+    ubinize -o ${ROOTFS_IMG} -m ${PAGE_SIZE} -p ${PEB_SIZE} $ubinize_cfg
+    set +x
+
+    rm -f $ubimg_tmp $ubinize_cfg
+    chmod a+x ${ROOTFS_IMG}
+}
+
+
+function do_build()
+{
+    cd $PRJ_PATH
+    mkdir -p $PRFX_PATH
+
+    pr_info "start build rootfs image..."
+    build_ubifs
+
+	mv $ROOTFS_IMG $PRFX_PATH
+}
+
+function do_install()
+{
+    cd $PRJ_PATH
+
+    echo ""
+    pr_info "install all images to '$PRFX_PATH'"
+	cp $TOP_PATH/bootloader/install/* $PRFX_PATH
+	cp $TOP_PATH/kernel/install/* $PRFX_PATH
+
+    ls $PRFX_PATH && echo ""
+
+    if [[ -n "$INST_PATH" && -w $INST_PATH ]] ; then
+        pr_info "install rootfs to '$INST_PATH'"
+        cp $PRFX_PATH/* $INST_PATH
+    fi
+}
+
+function do_clean()
+{
+    for d in $SRCS
+    do
+        rm -rf $PRJ_PATH/$d
+    done
+
+    rm -rf $PRJ_PATH/tarballs
+    rm -rf $PRFX_PATH
+}
+
+#+-------------------------+
+#| Shell script body entry |
+#+-------------------------+
+
+cd $PRJ_PATH
+
+export_env
+
+if [[ $# == 1 && $1 == -c ]] ;then
+    pr_warn "start clean rootfs"
+    do_clean
+    exit;
+fi
+
+pr_warn "start build rootfs for ${BOARD}"
+
+do_fetch
+
+do_build
+
+do_install

--
Gitblit v1.9.1