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_ciYAFFS2_TOOL=mkyaffs2image100
235f9996aaSopenharmony_ciWIN_JFFS2_TOOL=mkfs.jffs2.exe
245f9996aaSopenharmony_ciVFAT_TOOL=mkfs.vfat
255f9996aaSopenharmony_ciMCOPY_TOOL=mcopy
265f9996aaSopenharmony_ciCONFIG_PATH=$(dirname $(readlink -f "$0"))/rootfs_liteos.config
275f9996aaSopenharmony_ci
285f9996aaSopenharmony_citool_check() {
295f9996aaSopenharmony_cilocal ret='0'
305f9996aaSopenharmony_cicommand -v "$1" >/dev/null 2>&1 || { local ret='1'; }
315f9996aaSopenharmony_ciif [ "$ret" -ne 0  ]; then
325f9996aaSopenharmony_ci    echo "$1 tool is not exit, please install it" >&2
335f9996aaSopenharmony_cifi
345f9996aaSopenharmony_cireturn 0
355f9996aaSopenharmony_ci}
365f9996aaSopenharmony_ci
375f9996aaSopenharmony_ciif [[ "${ROOTFS_DIR}" = *"rootfs" ]]; then
385f9996aaSopenharmony_ci    if [ -d "${ROOTFS_DIR}" ]; then
395f9996aaSopenharmony_ci        chmod -R 755 ${ROOTFS_DIR}
405f9996aaSopenharmony_ci    fi
415f9996aaSopenharmony_ci    if [ -f "${ROOTFS_DIR}/bin/init" ] && [ -f "${ROOTFS_DIR}/bin/shell" ]; then
425f9996aaSopenharmony_ci        chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null
435f9996aaSopenharmony_ci        chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
445f9996aaSopenharmony_ci    fi
455f9996aaSopenharmony_cifi
465f9996aaSopenharmony_ci
475f9996aaSopenharmony_ciif [ "${FSTYPE}" = "jffs2" ]; then
485f9996aaSopenharmony_ci    if [ "${system}" != "Linux" ]; then
495f9996aaSopenharmony_ci        tool_check ${WIN_JFFS2_TOOL}
505f9996aaSopenharmony_ci        ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
515f9996aaSopenharmony_ci    else
525f9996aaSopenharmony_ci        tool_check ${JFFS2_TOOL}
535f9996aaSopenharmony_ci        if [[ "${ROOTFS_DIR}" = *"rootfs" ]]; then
545f9996aaSopenharmony_ci            if [ -f "${ROOTFS_DIR}/bin/foundation" ] && [ -f "${ROOTFS_DIR}/bin/apphilogcat" ]; then
555f9996aaSopenharmony_ci                ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096 --devtable ${CONFIG_PATH}
565f9996aaSopenharmony_ci            else
575f9996aaSopenharmony_ci                ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
585f9996aaSopenharmony_ci            fi
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}" = yaffs2 ]; then
925f9996aaSopenharmony_ci    tool_check ${YAFFS2_TOOL}
935f9996aaSopenharmony_ci    ${YAFFS2_TOOL}  ${ROOTFS_DIR} ${ROOTFS_IMG} 2k 4bit
945f9996aaSopenharmony_cielse
955f9996aaSopenharmony_ci    echo "Unsupported fs type!" >&2
965f9996aaSopenharmony_cifi
97