• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/bash
2#
3# Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
4# Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without modification,
7# are permitted provided that the following conditions are met:
8#
9# 1. Redistributions of source code must retain the above copyright notice, this list of
10#    conditions and the following disclaimer.
11#
12# 2. Redistributions in binary form must reproduce the above copyright notice, this list
13#    of conditions and the following disclaimer in the documentation and/or other materials
14#    provided with the distribution.
15#
16# 3. Neither the name of the copyright holder nor the names of its contributors may be used
17#    to endorse or promote products derived from this software without specific prior written
18#    permission.
19#
20# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22# THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
24# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27# OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29# OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30# ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31set -e
32
33system=$(uname -s)
34ROOTFS_DIR=$1
35FSTYPE=$2
36ROOTFS_IMG=${ROOTFS_DIR}"_"${FSTYPE}".img"
37JFFS2_TOOL=mkfs.jffs2
38WIN_JFFS2_TOOL=mkfs.jffs2.exe
39YAFFS2_TOOL=mkyaffs2image100
40VFAT_TOOL=mkfs.vfat
41ROMFS_TOOL=genromfs
42MCOPY_TOOL=mcopy
43
44tool_check() {
45local ret='0'
46command -v "$1" >/dev/null 2>&1 || { local ret='1'; }
47if [ "$ret" -ne 0 ]; then
48    echo "$1 tool is not exit, please install it" >&2
49fi
50return 0
51}
52
53chmod -R 755 ${ROOTFS_DIR}
54if [ -f "${ROOTFS_DIR}/bin/init" ]; then
55    chmod 700 ${ROOTFS_DIR}/bin/init 2> /dev/null
56fi
57if [ -f "${ROOTFS_DIR}/bin/shell" ]; then
58    chmod 700 ${ROOTFS_DIR}/bin/shell 2> /dev/null
59fi
60
61if [ "${FSTYPE}" = "jffs2" ]; then
62    if [ "${system}" != "Linux" ] ; then
63        tool_check ${WIN_JFFS2_TOOL}
64        ${WIN_JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
65    else
66        tool_check ${JFFS2_TOOL}
67        ${JFFS2_TOOL} -q -o ${ROOTFS_IMG} -d ${ROOTFS_DIR} --pagesize=4096
68    fi
69elif [ "${FSTYPE}" = "yaffs2" ]; then
70    tool_check ${YAFFS2_TOOL}
71    ${YAFFS2_TOOL}  ${ROOTFS_DIR} ${ROOTFS_IMG} 2k 24bit
72elif [ "${FSTYPE}" = "romfs" ]; then
73    tool_check ${ROMFS_TOOL}
74    ${ROMFS_TOOL}  -d ${ROOTFS_DIR} -f ${ROOTFS_IMG}
75elif [ "${FSTYPE}" = "vfat" ]; then
76    if [ "${system}" != "Linux" ] ; then
77        echo "Unsupported fs type!" >&2
78    else
79        tool_check ${VFAT_TOOL}
80        tool_check ${MCOPY_TOOL}
81        BLK_SIZE=512
82        CLT_SIZE=2048
83        FAT_TAB_NUM=2
84        CLT_CNT=$(( ${CLT_SIZE} / ${BLK_SIZE} ))
85        if [ $# -eq 3 ]; then
86            IMG_SIZE=$3
87        else
88            FAT32_ITEM_SIZE=4
89            RESV_CNT=38
90            IMG_MIN_SIZE=1048576
91            DU_DIR_SIZE=$(( $(echo $(du -s ${ROOTFS_DIR} | awk '{print $1}')) * 1024 ))
92            DIR_NUM=$(( $(echo $(ls -lR ${ROOTFS_DIR} | grep "^d" | wc -l | awk '{print $1}')) + 1 ))
93            DIR_SIZE=$(( ${DU_DIR_SIZE} + ${DIR_NUM} * 4096 ))
94            IMG_SIZE=$(( ${DIR_SIZE} / (1 - ${FAT_TAB_NUM} * ${FAT32_ITEM_SIZE} / ${CLT_SIZE}) + ${RESV_CNT} * ${BLK_SIZE}))
95            if [ ${IMG_SIZE} -le ${IMG_MIN_SIZE} ]; then
96                IMG_SIZE=${IMG_MIN_SIZE}
97            fi
98        fi
99        IMG_CNT=$(( (${IMG_SIZE} + ${BLK_SIZE} - 1) / ${BLK_SIZE} ))
100        echo mtools_skip_check=1 >> ~/.mtoolsrc
101        dd if=/dev/zero of=${ROOTFS_IMG} count=${IMG_CNT} bs=${BLK_SIZE}
102        ${VFAT_TOOL} ${ROOTFS_IMG} -s ${CLT_CNT} -f ${FAT_TAB_NUM} -S ${BLK_SIZE} > /dev/null
103        ${MCOPY_TOOL} -i ${ROOTFS_IMG} ${ROOTFS_DIR}/* -/ ::/
104    fi
105else
106    echo "Unsupported fs type!" >&2
107fi
108