10d163575Sopenharmony_ci/*
20d163575Sopenharmony_ci * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
30d163575Sopenharmony_ci * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
40d163575Sopenharmony_ci *
50d163575Sopenharmony_ci * Redistribution and use in source and binary forms, with or without modification,
60d163575Sopenharmony_ci * are permitted provided that the following conditions are met:
70d163575Sopenharmony_ci *
80d163575Sopenharmony_ci * 1. Redistributions of source code must retain the above copyright notice, this list of
90d163575Sopenharmony_ci *    conditions and the following disclaimer.
100d163575Sopenharmony_ci *
110d163575Sopenharmony_ci * 2. Redistributions in binary form must reproduce the above copyright notice, this list
120d163575Sopenharmony_ci *    of conditions and the following disclaimer in the documentation and/or other materials
130d163575Sopenharmony_ci *    provided with the distribution.
140d163575Sopenharmony_ci *
150d163575Sopenharmony_ci * 3. Neither the name of the copyright holder nor the names of its contributors may be used
160d163575Sopenharmony_ci *    to endorse or promote products derived from this software without specific prior written
170d163575Sopenharmony_ci *    permission.
180d163575Sopenharmony_ci *
190d163575Sopenharmony_ci * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
200d163575Sopenharmony_ci * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
210d163575Sopenharmony_ci * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
220d163575Sopenharmony_ci * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
230d163575Sopenharmony_ci * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
240d163575Sopenharmony_ci * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
250d163575Sopenharmony_ci * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
260d163575Sopenharmony_ci * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
270d163575Sopenharmony_ci * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
280d163575Sopenharmony_ci * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
290d163575Sopenharmony_ci * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
300d163575Sopenharmony_ci */
310d163575Sopenharmony_ci
320d163575Sopenharmony_ci/**
330d163575Sopenharmony_ci * @defgroup filesystem FileSystem
340d163575Sopenharmony_ci * @defgroup mtd_partition Multi Partition
350d163575Sopenharmony_ci * @ingroup filesystem
360d163575Sopenharmony_ci */
370d163575Sopenharmony_ci#ifndef _MTD_PARTITION_H
380d163575Sopenharmony_ci#define _MTD_PARTITION_H
390d163575Sopenharmony_ci
400d163575Sopenharmony_ci#include "sys/types.h"
410d163575Sopenharmony_ci#include "los_mux.h"
420d163575Sopenharmony_ci#include "mtd_list.h"
430d163575Sopenharmony_ci
440d163575Sopenharmony_ci#ifdef __cplusplus
450d163575Sopenharmony_ci#if __cplusplus
460d163575Sopenharmony_ciextern "C" {
470d163575Sopenharmony_ci#endif /* __cplusplus */
480d163575Sopenharmony_ci#endif /* __cplusplus */
490d163575Sopenharmony_ci
500d163575Sopenharmony_ci#define SPIBLK_NAME  "/dev/spinorblk"
510d163575Sopenharmony_ci#define SPICHR_NAME  "/dev/spinorchr"
520d163575Sopenharmony_ci
530d163575Sopenharmony_ci#define NANDBLK_NAME "/dev/nandblk"
540d163575Sopenharmony_ci#define NANDCHR_NAME "/dev/nandchr"
550d163575Sopenharmony_ci
560d163575Sopenharmony_citypedef struct mtd_node {
570d163575Sopenharmony_ci    UINT32 start_block;
580d163575Sopenharmony_ci    UINT32 end_block;
590d163575Sopenharmony_ci    UINT32 patitionnum;
600d163575Sopenharmony_ci    CHAR *blockdriver_name;
610d163575Sopenharmony_ci    CHAR *chardriver_name;
620d163575Sopenharmony_ci    CHAR *mountpoint_name;
630d163575Sopenharmony_ci    VOID *mtd_info; /* Driver used by a partition */
640d163575Sopenharmony_ci    LOS_DL_LIST node_info;
650d163575Sopenharmony_ci    LosMux lock;
660d163575Sopenharmony_ci    UINT32 user_num;
670d163575Sopenharmony_ci} mtd_partition;
680d163575Sopenharmony_ci
690d163575Sopenharmony_citypedef struct par_param {
700d163575Sopenharmony_ci    mtd_partition *partition_head;
710d163575Sopenharmony_ci    struct MtdDev *flash_mtd;
720d163575Sopenharmony_ci    const struct block_operations *flash_ops;
730d163575Sopenharmony_ci    const struct file_operations_vfs *char_ops;
740d163575Sopenharmony_ci    CHAR *blockname;
750d163575Sopenharmony_ci    CHAR *charname;
760d163575Sopenharmony_ci    UINT32 block_size;
770d163575Sopenharmony_ci} partition_param;
780d163575Sopenharmony_ci
790d163575Sopenharmony_ci#define CONFIG_MTD_PATTITION_NUM 20
800d163575Sopenharmony_ci
810d163575Sopenharmony_ci#define ALIGN_ASSIGN(len, startAddr, startBlk, endBlk, blkSize) do {    \
820d163575Sopenharmony_ci    (len) = (((len) + ((blkSize) - 1)) & ~((blkSize) - 1));             \
830d163575Sopenharmony_ci    (startAddr) = ((startAddr) & ~((blkSize) - 1));                     \
840d163575Sopenharmony_ci    (startBlk) = (startAddr) / (blkSize);                               \
850d163575Sopenharmony_ci    (endBlk) = (len) / (blkSize) + ((startBlk) - 1);                    \
860d163575Sopenharmony_ci} while (0)
870d163575Sopenharmony_ci
880d163575Sopenharmony_ci#define PAR_ASSIGNMENT(node, len, startAddr, num, mtd, blkSize) do {    \
890d163575Sopenharmony_ci    (node)->start_block = (startAddr) / (blkSize);                      \
900d163575Sopenharmony_ci    (node)->end_block = (len) / (blkSize) + ((node)->start_block - 1);  \
910d163575Sopenharmony_ci    (node)->patitionnum = (num);                                        \
920d163575Sopenharmony_ci    (node)->mtd_info = (mtd);                                           \
930d163575Sopenharmony_ci    (node)->mountpoint_name = NULL;                                     \
940d163575Sopenharmony_ci} while (0)
950d163575Sopenharmony_ci
960d163575Sopenharmony_cipartition_param *GetNandPartParam(VOID);
970d163575Sopenharmony_cipartition_param *GetSpinorPartParam(VOID);
980d163575Sopenharmony_cimtd_partition *GetSpinorPartitionHead(VOID);
990d163575Sopenharmony_ci
1000d163575Sopenharmony_ci/**
1010d163575Sopenharmony_ci * @ingroup mtd_partition
1020d163575Sopenharmony_ci * @brief Add a partition.
1030d163575Sopenharmony_ci *
1040d163575Sopenharmony_ci * @par Description:
1050d163575Sopenharmony_ci * <ul>
1060d163575Sopenharmony_ci * <li>This API is used to add a partition according to the passed-in parameters.</li>
1070d163575Sopenharmony_ci * </ul>
1080d163575Sopenharmony_ci * @attention
1090d163575Sopenharmony_ci * <ul>
1100d163575Sopenharmony_ci * <li>None.</li>
1110d163575Sopenharmony_ci * </ul>
1120d163575Sopenharmony_ci *
1130d163575Sopenharmony_ci * @param type           [IN] Storage medium type, support "nand" and "spinor" currently.
1140d163575Sopenharmony_ci * @param startAddr      [IN] Starting address of a partition.
1150d163575Sopenharmony_ci * @param length         [IN] Partition size.
1160d163575Sopenharmony_ci * @param partitionNum   [IN] Partition number, less than the value defined by CONFIG_MTD_PATTITION_NUM.
1170d163575Sopenharmony_ci *
1180d163575Sopenharmony_ci * @retval #-ENODEV      The driver is not found.
1190d163575Sopenharmony_ci * @retval #-EINVAL      Invalid parameter.
1200d163575Sopenharmony_ci * @retval #-ENOMEM      Insufficient memory.
1210d163575Sopenharmony_ci * @retval #ENOERR       The partition is successfully created.
1220d163575Sopenharmony_ci *
1230d163575Sopenharmony_ci * @par Dependency:
1240d163575Sopenharmony_ci * <ul><li>mtd_partition.h: the header file that contains the API declaration.</li></ul>
1250d163575Sopenharmony_ci * @see delete_mtd_partition
1260d163575Sopenharmony_ci */
1270d163575Sopenharmony_ciextern INT32 add_mtd_partition(const CHAR *type, UINT32 startAddr, UINT32 length, UINT32 partitionNum);
1280d163575Sopenharmony_ci
1290d163575Sopenharmony_ci/**
1300d163575Sopenharmony_ci * @ingroup mtd_partition
1310d163575Sopenharmony_ci * @brief Delete a partition.
1320d163575Sopenharmony_ci *
1330d163575Sopenharmony_ci * @par Description:
1340d163575Sopenharmony_ci * <ul>
1350d163575Sopenharmony_ci * <li>This API is used to delete a partition according to its partition number and storage medium type.</li>
1360d163575Sopenharmony_ci * </ul>
1370d163575Sopenharmony_ci * @attention
1380d163575Sopenharmony_ci * <ul>
1390d163575Sopenharmony_ci * <li>None.</li>
1400d163575Sopenharmony_ci * </ul>
1410d163575Sopenharmony_ci *
1420d163575Sopenharmony_ci * @param partitionNum   [IN] Partition number, less than the value defined by CONFIG_MTD_PATTITION_NUM.
1430d163575Sopenharmony_ci * @param type           [IN] Storage medium type, support "nand" and "spinor" currently.
1440d163575Sopenharmony_ci *
1450d163575Sopenharmony_ci * @retval #-EINVAL    Invalid parameter.
1460d163575Sopenharmony_ci * @retval #ENOERR     The partition is successfully deleted.
1470d163575Sopenharmony_ci *
1480d163575Sopenharmony_ci * @par Dependency:
1490d163575Sopenharmony_ci * <ul><li>mtd_partition.h: the header file that contains the API declaration.</li></ul>
1500d163575Sopenharmony_ci * @see add_mtd_partition
1510d163575Sopenharmony_ci */
1520d163575Sopenharmony_ciextern INT32 delete_mtd_partition(UINT32 partitionNum, const CHAR *type);
1530d163575Sopenharmony_ci
1540d163575Sopenharmony_ci#ifdef __cplusplus
1550d163575Sopenharmony_ci#if __cplusplus
1560d163575Sopenharmony_ci}
1570d163575Sopenharmony_ci#endif /* __cplusplus */
1580d163575Sopenharmony_ci#endif /* __cplusplus */
1590d163575Sopenharmony_ci
1600d163575Sopenharmony_ci#endif /* _MTD_PARTITION_H */
161