更新日期: 2022/06/01 来源: https://gitee.com/weharmony/kernel_liteos_a_note
Multi Partition
Multi Partition 的协作图:

函数

INT32 add_mtd_partition (const CHAR *type, UINT32 startAddr, UINT32 length, UINT32 partitionNum)
 Add a partition. 更多...
 
INT32 delete_mtd_partition (UINT32 partitionNum, const CHAR *type)
 Delete a partition. 更多...
 

详细描述

函数说明

◆ add_mtd_partition()

INT32 add_mtd_partition ( const CHAR type,
UINT32  startAddr,
UINT32  length,
UINT32  partitionNum 
)

Add a partition.

Description:
  • This API is used to add a partition according to the passed-in parameters.
注意
  • None.
参数
type[IN] Storage medium type, support "nand" and "spinor" currently.
startAddr[IN] Starting address of a partition.
length[IN] Partition size.
partitionNum[IN] Partition number, less than the value defined by CONFIG_MTD_PATTITION_NUM.
返回值
#-ENODEVThe driver is not found.
#-EINVALInvalid parameter.
#-ENOMEMInsufficient memory.
#ENOERRThe partition is successfully created.
Dependency:
参见
delete_mtd_partition

在文件 mtd_partition.c394 行定义.

396{
397 INT32 ret;
398 mtd_partition *newNode = NULL;
399 partition_param *param = NULL;
400
401 if ((partitionNum >= CONFIG_MTD_PATTITION_NUM) || (type == NULL)) {
402 return -EINVAL;
403 }
404
406 if (ret != ENOERR) {
407 PRINT_ERR("%s %d, mutex lock failed, error:%d\n", __FUNCTION__, __LINE__, ret);
408 }
409
410 ret = MtdInitFsparParam(type, &param);//初始化 flash 分区参数
411 if (ret != ENOERR) {
412 goto ERROR_OUT;
413 }
414 //参数检查
415 ret = AddParamCheck(startAddr, param, partitionNum, length);
416 if (ret != ENOERR) {
417 goto ERROR_OUT;
418 }
419
420 newNode = (mtd_partition *)zalloc(sizeof(mtd_partition));//分配一个分区节点
421 if (newNode == NULL) {
423 return -ENOMEM;
424 }
425 //分区对齐
426 PAR_ASSIGNMENT(newNode, length, startAddr, partitionNum, param->flash_mtd, param->block_size);
427 //注册块设备驱动程序
428 ret = BlockDriverRegisterOperate(newNode, param, partitionNum);
429 if (ret) {
430 goto ERROR_OUT1;
431 }
432 //注册字符设备驱动程序
433 ret = CharDriverRegisterOperate(newNode, param, partitionNum);
434 if (ret) {
435 goto ERROR_OUT2;
436 }
437
438 LOS_ListTailInsert(&param->partition_head->node_info, &newNode->node_info);//挂到全局分区链表上
439 (VOID)LOS_MuxInit(&newNode->lock, NULL);//初始化锁,每个分区节点都有自己的互斥锁
440
442 if (ret != ENOERR) {
443 PRINT_ERR("%s %d, mutex unlock failed, error:%d\n", __FUNCTION__, __LINE__, ret);
444 }
445
446 return ENOERR;
447ERROR_OUT2:
448 (VOID)BlockDriverUnregister(newNode);
449ERROR_OUT1:
450 free(newNode);
451ERROR_OUT:
453 return ret;
454}
LITE_OS_SEC_ALW_INLINE STATIC INLINE VOID LOS_ListTailInsert(LOS_DL_LIST *list, LOS_DL_LIST *node)
Insert a node to the tail of a doubly linked list.
Definition: los_list.h:244
LITE_OS_SEC_TEXT UINT32 LOS_MuxInit(LosMux *mutex, const LosMuxAttr *attr)
初始化互斥锁
Definition: los_mux.c:262
signed int INT32
Definition: los_typedef.h:60
void * zalloc(size_t size)
Definition: malloc.c:91
void free(void *ptr)
释放ptr所指向的内存空间
Definition: malloc.c:66
static INT32 BlockDriverUnregister(mtd_partition *node)
注销块设备驱动
static INT32 AddParamCheck(UINT32 startAddr, const partition_param *param, UINT32 partitionNum, UINT32 length)
增加MTD分区参数检查
pthread_mutex_t g_mtdPartitionLock
Definition: mtd_partition.c:48
static INT32 BlockDriverRegisterOperate(mtd_partition *newNode, const partition_param *param, UINT32 partitionNum)
注册块设备,此函数之后设备将支持VFS访问
static INT32 MtdInitFsparParam(const CHAR *type, partition_param **fsparParam)
根据 flash-type 来初始化分区的参数, Fspar 是不是 flash partition 的意思? 这名字取得有点费解 @note_thinking
static INT32 CharDriverRegisterOperate(mtd_partition *newNode, const partition_param *param, UINT32 partitionNum)
注册字符设备,此函数之后设备将支持VFS访问
int pthread_mutex_lock(pthread_mutex_t *mutex)
互斥锁加锁操作
int pthread_mutex_unlock(pthread_mutex_t *mutex)
解锁互斥锁
通过mknod在/dev子目录下建立MTD块设备节点(主设备号为31)和MTD字符设备节点(主设备号为90)
LosMux lock
每个分区都有自己的互斥锁
LOS_DL_LIST node_info
双循环节点,挂在首个分区节点上
分区参数描述符,一个分区既可支持按块访问也可以支持按字符访问,只要有驱动程序就阔以
mtd_partition * partition_head
首个分区,其他分区都挂在.node_info节点上
struct MtdDev * flash_mtd
flash设备描述符,属于硬件驱动层
UINT32 block_size
块单位(4K),对文件系统而言是按块读取数据,方便和内存页置换
函数调用图:
这是这个函数的调用关系图:

◆ delete_mtd_partition()

INT32 delete_mtd_partition ( UINT32  partitionNum,
const CHAR type 
)

Delete a partition.

Description:
  • This API is used to delete a partition according to its partition number and storage medium type.
注意
  • None.
参数
partitionNum[IN] Partition number, less than the value defined by CONFIG_MTD_PATTITION_NUM.
type[IN] Storage medium type, support "nand" and "spinor" currently.
返回值
#-EINVALInvalid parameter.
#ENOERRThe partition is successfully deleted.
Dependency:
参见
add_mtd_partition

Delete a partition.

在文件 mtd_partition.c527 行定义.

528{
529 INT32 ret;
530 mtd_partition *node = NULL;
531 partition_param *param = NULL;
532
533 if (type == NULL) {
534 return -EINVAL;
535 }
536
538 if (ret != ENOERR) {
539 PRINT_ERR("%s %d, mutex lock failed, error:%d\n", __FUNCTION__, __LINE__, ret);
540 }
541
542 ret = DeleteParamCheck(partitionNum, type, &param);//删除操作时的参数检查
543 if (ret) {
544 PRINT_ERR("delete_mtd_partition param invalid\n");
546 return ret;
547 }
548
549 ret = OsNodeGet(&node, partitionNum, param);
550 if (ret) {
552 return ret;
553 }
554
555 ret = DeletePartitionUnregister(node);
556 if (ret) {
557 PRINT_ERR("DeletePartitionUnregister error:%d\n", ret);
559 return ret;
560 }
561
562 ret = OsResourceRelease(node, type, param);
563 if (ret) {
564 PRINT_ERR("DeletePartitionUnregister error:%d\n", ret);
566 return ret;
567 }
568
570 if (ret != ENOERR) {
571 PRINT_ERR("%s %d, mutex unlock failed, error:%d\n", __FUNCTION__, __LINE__, ret);
572 }
573 return ENOERR;
574}
static INT32 DeleteParamCheck(UINT32 partitionNum, const CHAR *type, partition_param **param)
检查分区参数
static INT32 DeletePartitionUnregister(mtd_partition *node)
删除分区驱动程序,注意每个分区的文件系统都可以不一样,驱动程序也都不同
static INT32 OsResourceRelease(mtd_partition *node, const CHAR *type, partition_param *param)
释放分区链表节点所占内存 sizeof(mtd_partition)
static INT32 OsNodeGet(mtd_partition **node, UINT32 partitionNum, const partition_param *param)
获取分区链表节点
函数调用图:
这是这个函数的调用关系图: