15f9996aaSopenharmony_ci#!/bin/bash
25f9996aaSopenharmony_ci#
35f9996aaSopenharmony_ci# Copyright (c) 2021 Huawei Device Co., Ltd.
45f9996aaSopenharmony_ci# Licensed under the Apache License, Version 2.0 (the "License");
55f9996aaSopenharmony_ci# you may not use this file except in compliance with the License.
65f9996aaSopenharmony_ci# You may obtain a copy of the License at
75f9996aaSopenharmony_ci#
85f9996aaSopenharmony_ci#     http://www.apache.org/licenses/LICENSE-2.0
95f9996aaSopenharmony_ci#
105f9996aaSopenharmony_ci# Unless required by applicable law or agreed to in writing, software
115f9996aaSopenharmony_ci# distributed under the License is distributed on an "AS IS" BASIS,
125f9996aaSopenharmony_ci# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135f9996aaSopenharmony_ci# See the License for the specific language governing permissions and
145f9996aaSopenharmony_ci# limitations under the License.
155f9996aaSopenharmony_ciset -e
165f9996aaSopenharmony_ci
175f9996aaSopenharmony_cisystem=$(uname -s)
185f9996aaSopenharmony_ciROOTFS_DIR=$1
195f9996aaSopenharmony_ciFSTYPE=$2
205f9996aaSopenharmony_ciROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img"
215f9996aaSopenharmony_ciJFFS2_TOOL=mkfs.jffs2
225f9996aaSopenharmony_ciWIN_JFFS2_TOOL=mkfs.jffs2.exe
235f9996aaSopenharmony_ciVFAT_TOOL=mkfs.vfat
245f9996aaSopenharmony_ciMCOPY_TOOL=mcopy
255f9996aaSopenharmony_ciEXT4_TOOL=mkfs.ext4
265f9996aaSopenharmony_ciEXT4_SH=$(dirname $(readlink -f "$0"))/../../../third_party/e2fsprogs/e2fsprogs/contrib/populate-extfs.sh
275f9996aaSopenharmony_ciINSTALL_E2FSPROGS_SH=$(dirname $(readlink -f "$0"))/../../../third_party/e2fsprogs/install.sh
285f9996aaSopenharmony_ciINSTALL_E2FSPROGS_PATH=$(dirname $(readlink -f "$0"))/../../../third_party/e2fsprogs
295f9996aaSopenharmony_ci
305f9996aaSopenharmony_citool_check() {
315f9996aaSopenharmony_cilocal ret='0'
325f9996aaSopenharmony_cicommand -v "$1" >/dev/null 2>&1 || { local ret='1'; }
335f9996aaSopenharmony_ciif [ "$ret" -ne 0  ]; then
345f9996aaSopenharmony_ci    echo "$1 tool is not exit, please install it" >&2
355f9996aaSopenharmony_cifi
365f9996aaSopenharmony_cireturn 0
375f9996aaSopenharmony_ci}
385f9996aaSopenharmony_ci
395f9996aaSopenharmony_ciif [[ "${ROOTFS_DIR}" = *"rootfs" ]]; then
405f9996aaSopenharmony_ci    if [ -d "${ROOTFS_DIR}" ]; then
415f9996aaSopenharmony_ci        chmod -R 755 ${ROOTFS_DIR}
425f9996aaSopenharmony_ci    fi
435f9996aaSopenharmony_ci    if [ -f "${ROOTFS_DIR}/bin/init" ] && [ -f "${ROOTFS_DIR}/bin/shell" ]; then
445f9996aaSopenharmony_ci        chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null
455f9996aaSopenharmony_ci        chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
465f9996aaSopenharmony_ci    fi
475f9996aaSopenharmony_cifi
485f9996aaSopenharmony_ci
495f9996aaSopenharmony_ciCONFIG_PATH=$(dirname $(readlink -f "$0"))/rootfs_linux.config
505f9996aaSopenharmony_ci
515f9996aaSopenharmony_ciif [ "${FSTYPE}" = "jffs2" ]; then
525f9996aaSopenharmony_ci    if [ "${system}" != "Linux" ] ; then
535f9996aaSopenharmony_ci        tool_check ${WIN_JFFS2_TOOL}
545f9996aaSopenharmony_ci        ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
555f9996aaSopenharmony_ci    else
565f9996aaSopenharmony_ci        tool_check ${JFFS2_TOOL}
575f9996aaSopenharmony_ci        if [[ "${ROOTFS_DIR}" = *"rootfs" ]]; then
585f9996aaSopenharmony_ci            ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096 --devtable ${CONFIG_PATH}
595f9996aaSopenharmony_ci        else
605f9996aaSopenharmony_ci            ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
615f9996aaSopenharmony_ci        fi
625f9996aaSopenharmony_ci    fi
635f9996aaSopenharmony_cielif [ "${FSTYPE}" = "vfat" ]; then
645f9996aaSopenharmony_ci    if [ "${system}" != "Linux" ] ; then
655f9996aaSopenharmony_ci        echo "Unsupported fs type!" >&2
665f9996aaSopenharmony_ci    else
675f9996aaSopenharmony_ci        tool_check ${VFAT_TOOL}
685f9996aaSopenharmony_ci        tool_check ${MCOPY_TOOL}
695f9996aaSopenharmony_ci        BLK_SIZE=512
705f9996aaSopenharmony_ci        CLT_SIZE=2048
715f9996aaSopenharmony_ci        FAT_TAB_NUM=2
725f9996aaSopenharmony_ci        CLT_CNT=$(( ${CLT_SIZE} / ${BLK_SIZE} ))
735f9996aaSopenharmony_ci        if [ $# -eq 3 ]; then
745f9996aaSopenharmony_ci            IMG_SIZE=$3
755f9996aaSopenharmony_ci        else
765f9996aaSopenharmony_ci            FAT32_ITEM_SIZE=4
775f9996aaSopenharmony_ci            RESV_CNT=38
785f9996aaSopenharmony_ci            IMG_MIN_SIZE=1048576
795f9996aaSopenharmony_ci            DIR_SIZE=$(( $(echo $(du -s ${ROOTFS_DIR} | awk '{print $1}')) * 1024 ))
805f9996aaSopenharmony_ci            IMG_SIZE=$(( ${DIR_SIZE} / (1 - ${FAT_TAB_NUM} * ${FAT32_ITEM_SIZE} / ${CLT_SIZE}) + ${RESV_CNT} * ${BLK_SIZE}))
815f9996aaSopenharmony_ci            if [ ${IMG_SIZE} -le ${IMG_MIN_SIZE} ]; then
825f9996aaSopenharmony_ci                IMG_SIZE=${IMG_MIN_SIZE}
835f9996aaSopenharmony_ci            fi
845f9996aaSopenharmony_ci        fi
855f9996aaSopenharmony_ci        IMG_CNT=$(( (${IMG_SIZE} + ${BLK_SIZE} - 1) / ${BLK_SIZE} ))
865f9996aaSopenharmony_ci        echo mtools_skip_check=1 >> ~/.mtoolsrc
875f9996aaSopenharmony_ci        dd if=/dev/zero of=${ROOTFS_IMG} count=${IMG_CNT} bs=${BLK_SIZE}
885f9996aaSopenharmony_ci        ${VFAT_TOOL} ${ROOTFS_IMG} -s ${CLT_CNT} -f ${FAT_TAB_NUM} -S ${BLK_SIZE} > /dev/null
895f9996aaSopenharmony_ci        ${MCOPY_TOOL} -i ${ROOTFS_IMG} ${ROOTFS_DIR}/* -/ ::/
905f9996aaSopenharmony_ci    fi
915f9996aaSopenharmony_cielif [ "${FSTYPE}" = "ext4" ]; then
925f9996aaSopenharmony_ci    if [ "${system}" != "Linux" ] ; then
935f9996aaSopenharmony_ci        echo "Unsupported fs type!" >&2
945f9996aaSopenharmony_ci    else
955f9996aaSopenharmony_ci        if [ $# -eq 3 ]; then
965f9996aaSopenharmony_ci            EMMC_ROOTFS_SIZE=$3
975f9996aaSopenharmony_ci        else
985f9996aaSopenharmony_ci            if [[ "${ROOTFS_DIR}" = *"rootfs" ]]; then
995f9996aaSopenharmony_ci                EMMC_ROOTFS_SIZE=50
1005f9996aaSopenharmony_ci            else
1015f9996aaSopenharmony_ci                EMMC_ROOTFS_SIZE=50
1025f9996aaSopenharmony_ci            fi
1035f9996aaSopenharmony_ci        fi
1045f9996aaSopenharmony_ci        COUNT_SIZE="$(expr ${EMMC_ROOTFS_SIZE} \* 1024 \* 2)"
1055f9996aaSopenharmony_ci        dd if=/dev/zero of=${ROOTFS_IMG} bs=512 count=${COUNT_SIZE}
1065f9996aaSopenharmony_ci        ${EXT4_TOOL} ${ROOTFS_IMG}
1075f9996aaSopenharmony_ci        ${INSTALL_E2FSPROGS_SH} ${INSTALL_E2FSPROGS_PATH}
1085f9996aaSopenharmony_ci        ${EXT4_SH} ${ROOTFS_DIR} ${ROOTFS_IMG} ${CONFIG_PATH}
1095f9996aaSopenharmony_ci    fi
1105f9996aaSopenharmony_cielse
1115f9996aaSopenharmony_ci    echo "Unsupported fs type!" >&2
1125f9996aaSopenharmony_cifi
113