2020-09-08 10:21:39 +08:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2021-03-11 18:43:57 +08:00
|
|
|
# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
|
|
|
|
# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
|
2020-09-08 10:21:39 +08:00
|
|
|
#
|
|
|
|
# Redistribution and use in source and binary forms, with or without modification,
|
|
|
|
# are permitted provided that the following conditions are met:
|
|
|
|
#
|
|
|
|
# 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
# conditions and the following disclaimer.
|
|
|
|
#
|
|
|
|
# 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
# of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
# provided with the distribution.
|
|
|
|
#
|
|
|
|
# 3. Neither the name of the copyright holder nor the names of its contributors may be used
|
|
|
|
# to endorse or promote products derived from this software without specific prior written
|
|
|
|
# permission.
|
|
|
|
#
|
|
|
|
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
|
|
|
|
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
|
|
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
|
|
|
|
# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
|
|
# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
|
|
|
|
# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
set -e
|
|
|
|
|
|
|
|
system=$(uname -s)
|
|
|
|
ROOTFS_DIR=$1
|
|
|
|
FSTYPE=$2
|
2021-03-11 18:43:57 +08:00
|
|
|
ROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img"
|
2020-10-19 16:18:59 +08:00
|
|
|
JFFS2_TOOL=mkfs.jffs2
|
|
|
|
WIN_JFFS2_TOOL=mkfs.jffs2.exe
|
2021-03-11 18:43:57 +08:00
|
|
|
YAFFS2_TOOL=mkyaffs2image100
|
2020-10-19 16:18:59 +08:00
|
|
|
VFAT_TOOL=mkfs.vfat
|
|
|
|
MCOPY_TOOL=mcopy
|
2020-09-08 10:21:39 +08:00
|
|
|
|
2020-10-19 16:18:59 +08:00
|
|
|
tool_check() {
|
|
|
|
local ret='0'
|
|
|
|
command -v "$1" >/dev/null 2>&1 || { local ret='1'; }
|
|
|
|
if [ "$ret" -ne 0 ]; then
|
2021-03-11 18:43:57 +08:00
|
|
|
echo "$1 tool is not exit, please install it" >&2
|
2020-10-19 16:18:59 +08:00
|
|
|
fi
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
chmod -R 755 ${ROOTFS_DIR}
|
|
|
|
if [ -f "${ROOTFS_DIR}/bin/init" ]; then
|
2020-09-08 10:21:39 +08:00
|
|
|
chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null
|
2020-10-19 16:18:59 +08:00
|
|
|
fi
|
|
|
|
if [ -f "${ROOTFS_DIR}/bin/shell" ]; then
|
2020-09-08 10:21:39 +08:00
|
|
|
chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "${FSTYPE}" = "jffs2" ]; then
|
|
|
|
if [ "${system}" != "Linux" ] ; then
|
2020-10-19 16:18:59 +08:00
|
|
|
tool_check ${WIN_JFFS2_TOOL}
|
2020-09-08 10:21:39 +08:00
|
|
|
${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
|
|
|
|
else
|
2020-10-19 16:18:59 +08:00
|
|
|
tool_check ${JFFS2_TOOL}
|
2020-09-08 10:21:39 +08:00
|
|
|
${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
|
|
|
|
fi
|
2021-03-11 18:43:57 +08:00
|
|
|
elif [ "${FSTYPE}" = "yaffs2" ]; then
|
|
|
|
tool_check ${YAFFS2_TOOL}
|
|
|
|
${YAFFS2_TOOL} ${ROOTFS_DIR} ${ROOTFS_IMG} 2k 24bit
|
2020-09-08 10:21:39 +08:00
|
|
|
elif [ "${FSTYPE}" = "vfat" ]; then
|
|
|
|
if [ "${system}" != "Linux" ] ; then
|
2020-10-19 16:18:59 +08:00
|
|
|
echo "Unsupported fs type!" >&2
|
2020-09-08 10:21:39 +08:00
|
|
|
else
|
2020-10-19 16:18:59 +08:00
|
|
|
tool_check ${VFAT_TOOL}
|
|
|
|
tool_check ${MCOPY_TOOL}
|
2020-09-08 10:21:39 +08:00
|
|
|
BLK_SIZE=512
|
|
|
|
CLT_SIZE=2048
|
|
|
|
FAT_TAB_NUM=2
|
|
|
|
CLT_CNT=$(( ${CLT_SIZE} / ${BLK_SIZE} ))
|
|
|
|
if [ $# -eq 3 ]; then
|
|
|
|
IMG_SIZE=$3
|
|
|
|
else
|
|
|
|
FAT32_ITEM_SIZE=4
|
|
|
|
RESV_CNT=38
|
|
|
|
IMG_MIN_SIZE=1048576
|
|
|
|
DIR_SIZE=$(( $(echo $(du -s ${ROOTFS_DIR} | awk '{print $1}')) * 1024 ))
|
|
|
|
IMG_SIZE=$(( ${DIR_SIZE} / (1 - ${FAT_TAB_NUM} * ${FAT32_ITEM_SIZE} / ${CLT_SIZE}) + ${RESV_CNT} * ${BLK_SIZE}))
|
|
|
|
if [ ${IMG_SIZE} -le ${IMG_MIN_SIZE} ]; then
|
|
|
|
IMG_SIZE=${IMG_MIN_SIZE}
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
IMG_CNT=$(( (${IMG_SIZE} + ${BLK_SIZE} - 1) / ${BLK_SIZE} ))
|
|
|
|
echo mtools_skip_check=1 >> ~/.mtoolsrc
|
|
|
|
dd if=/dev/zero of=${ROOTFS_IMG} count=${IMG_CNT} bs=${BLK_SIZE}
|
2020-10-19 16:18:59 +08:00
|
|
|
${VFAT_TOOL} ${ROOTFS_IMG} -s ${CLT_CNT} -f ${FAT_TAB_NUM} -S ${BLK_SIZE} > /dev/null
|
|
|
|
${MCOPY_TOOL} -i ${ROOTFS_IMG} ${ROOTFS_DIR}/* -/ ::/
|
2020-09-08 10:21:39 +08:00
|
|
|
fi
|
|
|
|
else
|
2020-10-19 16:18:59 +08:00
|
|
|
echo "Unsupported fs type!" >&2
|
2020-09-08 10:21:39 +08:00
|
|
|
fi
|