10d163575Sopenharmony_ci#!/bin/bash
20d163575Sopenharmony_ci#
30d163575Sopenharmony_ci# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
50d163575Sopenharmony_ci#
60d163575Sopenharmony_ci# Redistribution and use in source and binary forms, with or without modification,
70d163575Sopenharmony_ci# are permitted provided that the following conditions are met:
80d163575Sopenharmony_ci#
90d163575Sopenharmony_ci# 1. Redistributions of source code must retain the above copyright notice, this list of
100d163575Sopenharmony_ci#    conditions and the following disclaimer.
110d163575Sopenharmony_ci#
120d163575Sopenharmony_ci# 2. Redistributions in binary form must reproduce the above copyright notice, this list
130d163575Sopenharmony_ci#    of conditions and the following disclaimer in the documentation and/or other materials
140d163575Sopenharmony_ci#    provided with the distribution.
150d163575Sopenharmony_ci#
160d163575Sopenharmony_ci# 3. Neither the name of the copyright holder nor the names of its contributors may be used
170d163575Sopenharmony_ci#    to endorse or promote products derived from this software without specific prior written
180d163575Sopenharmony_ci#    permission.
190d163575Sopenharmony_ci#
200d163575Sopenharmony_ci# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
210d163575Sopenharmony_ci# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
220d163575Sopenharmony_ci# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
230d163575Sopenharmony_ci# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
240d163575Sopenharmony_ci# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
250d163575Sopenharmony_ci# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
260d163575Sopenharmony_ci# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
270d163575Sopenharmony_ci# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
280d163575Sopenharmony_ci# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
290d163575Sopenharmony_ci# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
300d163575Sopenharmony_ci# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
310d163575Sopenharmony_ciset -e
320d163575Sopenharmony_ci
330d163575Sopenharmony_cisystem=$(uname -s)
340d163575Sopenharmony_ciROOTFS_DIR=$1
350d163575Sopenharmony_ciFSTYPE=$2
360d163575Sopenharmony_ciROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img"
370d163575Sopenharmony_ciJFFS2_TOOL=mkfs.jffs2
380d163575Sopenharmony_ciWIN_JFFS2_TOOL=mkfs.jffs2.exe
390d163575Sopenharmony_ciYAFFS2_TOOL=mkyaffs2image100
400d163575Sopenharmony_ciVFAT_TOOL=mkfs.vfat
410d163575Sopenharmony_ciROMFS_TOOL=genromfs
420d163575Sopenharmony_ciMCOPY_TOOL=mcopy
430d163575Sopenharmony_ci
440d163575Sopenharmony_citool_check() {
450d163575Sopenharmony_cilocal ret='0'
460d163575Sopenharmony_cicommand -v "$1" >/dev/null 2>&1 || { local ret='1'; }
470d163575Sopenharmony_ciif [ "$ret" -ne 0 ]; then
480d163575Sopenharmony_ci    echo "$1 tool is not exit, please install it" >&2
490d163575Sopenharmony_cifi
500d163575Sopenharmony_cireturn 0
510d163575Sopenharmony_ci}
520d163575Sopenharmony_ci
530d163575Sopenharmony_cichmod -R 755 ${ROOTFS_DIR}
540d163575Sopenharmony_ciif [ -f "${ROOTFS_DIR}/bin/init" ]; then
550d163575Sopenharmony_ci    chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null
560d163575Sopenharmony_cifi
570d163575Sopenharmony_ciif [ -f "${ROOTFS_DIR}/bin/shell" ]; then
580d163575Sopenharmony_ci    chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
590d163575Sopenharmony_cifi
600d163575Sopenharmony_ci
610d163575Sopenharmony_ciif [ "${FSTYPE}" = "jffs2" ]; then
620d163575Sopenharmony_ci    if [ "${system}" != "Linux" ] ; then
630d163575Sopenharmony_ci        tool_check ${WIN_JFFS2_TOOL}
640d163575Sopenharmony_ci        ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
650d163575Sopenharmony_ci    else
660d163575Sopenharmony_ci        tool_check ${JFFS2_TOOL}
670d163575Sopenharmony_ci        ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
680d163575Sopenharmony_ci    fi
690d163575Sopenharmony_cielif [ "${FSTYPE}" = "yaffs2" ]; then
700d163575Sopenharmony_ci    tool_check ${YAFFS2_TOOL}
710d163575Sopenharmony_ci    ${YAFFS2_TOOL}  ${ROOTFS_DIR} ${ROOTFS_IMG} 2k 24bit
720d163575Sopenharmony_cielif [ "${FSTYPE}" = "romfs" ]; then
730d163575Sopenharmony_ci    tool_check ${ROMFS_TOOL}
740d163575Sopenharmony_ci    ${ROMFS_TOOL}  -d ${ROOTFS_DIR} -f ${ROOTFS_IMG}
750d163575Sopenharmony_cielif [ "${FSTYPE}" = "vfat" ]; then
760d163575Sopenharmony_ci    if [ "${system}" != "Linux" ] ; then
770d163575Sopenharmony_ci        echo "Unsupported fs type!" >&2
780d163575Sopenharmony_ci    else
790d163575Sopenharmony_ci        tool_check ${VFAT_TOOL}
800d163575Sopenharmony_ci        tool_check ${MCOPY_TOOL}
810d163575Sopenharmony_ci        BLK_SIZE=512
820d163575Sopenharmony_ci        CLT_SIZE=2048
830d163575Sopenharmony_ci        FAT_TAB_NUM=2
840d163575Sopenharmony_ci        CLT_CNT=$(( ${CLT_SIZE} / ${BLK_SIZE} ))
850d163575Sopenharmony_ci        if [ $# -eq 3 ]; then
860d163575Sopenharmony_ci            IMG_SIZE=$3
870d163575Sopenharmony_ci        else
880d163575Sopenharmony_ci            FAT32_ITEM_SIZE=4
890d163575Sopenharmony_ci            RESV_CNT=38
900d163575Sopenharmony_ci            IMG_MIN_SIZE=1048576
910d163575Sopenharmony_ci            DU_DIR_SIZE=$(( $(echo $(du -s ${ROOTFS_DIR} | awk '{print $1}')) * 1024 ))
920d163575Sopenharmony_ci            DIR_NUM=$(( $(echo $(ls -lR ${ROOTFS_DIR} | grep "^d" | wc -l | awk '{print $1}')) + 1 ))
930d163575Sopenharmony_ci            DIR_SIZE=$(( ${DU_DIR_SIZE} + ${DIR_NUM} * 4096 ))
940d163575Sopenharmony_ci            IMG_SIZE=$(( ${DIR_SIZE} / (1 - ${FAT_TAB_NUM} * ${FAT32_ITEM_SIZE} / ${CLT_SIZE}) + ${RESV_CNT} * ${BLK_SIZE}))
950d163575Sopenharmony_ci            if [ ${IMG_SIZE} -le ${IMG_MIN_SIZE} ]; then
960d163575Sopenharmony_ci                IMG_SIZE=${IMG_MIN_SIZE}
970d163575Sopenharmony_ci            fi
980d163575Sopenharmony_ci        fi
990d163575Sopenharmony_ci        IMG_CNT=$(( (${IMG_SIZE} + ${BLK_SIZE} - 1) / ${BLK_SIZE} ))
1000d163575Sopenharmony_ci        echo mtools_skip_check=1 >> ~/.mtoolsrc
1010d163575Sopenharmony_ci        dd if=/dev/zero of=${ROOTFS_IMG} count=${IMG_CNT} bs=${BLK_SIZE}
1020d163575Sopenharmony_ci        ${VFAT_TOOL} ${ROOTFS_IMG} -s ${CLT_CNT} -f ${FAT_TAB_NUM} -S ${BLK_SIZE} > /dev/null
1030d163575Sopenharmony_ci        ${MCOPY_TOOL} -i ${ROOTFS_IMG} ${ROOTFS_DIR}/* -/ ::/
1040d163575Sopenharmony_ci    fi
1050d163575Sopenharmony_cielse
1060d163575Sopenharmony_ci    echo "Unsupported fs type!" >&2
1070d163575Sopenharmony_cifi
108