blob: 4f1c2beaa3f7836fe5a35d47423ce56418a5fce3 [file] [log] [blame]
/*
* mbtk_mtd.c
*
* MBTK mtd partition utils API.
*
*/
/******************************************************************************
EDIT HISTORY FOR FILE
WHEN WHO WHAT,WHERE,WHY
-------- -------- -------------------------------------------------------
2024/2/26 LiuBin Initial version
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include "mbtk_mtd.h"
#include "mbtk_log.h"
#include "mbtk_str.h"
static mbtk_partition_info_t partition_list[MBTK_PARTITION_NUM_MAX];
static bool partition_inited = FALSE;
mbtk_partition_info_t* mbtk_partition_get()
{
if(partition_inited) {
return partition_list;
}
memset(partition_list, 0x0, sizeof(partition_list));
FILE *fp = fopen("/proc/mtd", "r");
if(fp == NULL) {
LOGE("fopen(/proc/mtd) fail:%d", errno);
return NULL;
}
char buff[64];
int index = 0;
char size_str[16];
char name[32];
memset(buff, 0x0, 64);
while(fgets(buff, 64, fp)) {
if(str_startwith(buff, "mtd")) {
memset(size_str, 0x0, 16);
memset(name, 0x0, 32);
if(3 == sscanf(buff, "%s %s %*s %s", partition_list[index].dev, size_str, name)) {
if(name[0] == '\"' && name[strlen(name) - 1] == '\"') {
memcpy(partition_list[index].name, name + 1, strlen(name) - 2); // No copy ""
} else {
LOGE("partition(%s) name error.", buff);
return NULL;
}
if(partition_list[index].dev[strlen(partition_list[index].dev) - 1] == ':') {
partition_list[index].dev[strlen(partition_list[index].dev) - 1] = '\0';
}
partition_list[index].partition_size = (uint32)strtoul(size_str, NULL, 16);
if(index > 0) {
partition_list[index].partition_start = partition_list[index - 1].partition_start + partition_list[index - 1].partition_size;
}
partition_list[index].used = TRUE;
} else {
LOGE("sscanf(%s) fail:%d", buff, errno);
return NULL;
}
index++;
}
memset(buff, 0x0, 64);
}
fclose(fp);
int i = 0;
while(i < MBTK_PARTITION_NUM_MAX) {
if(partition_list[i].used) {
LOGD("%s(%s) : %08x %08x", partition_list[i].name, partition_list[i].dev,
partition_list[i].partition_start, partition_list[i].partition_size);
}
i++;
}
partition_inited = TRUE;
return partition_list;
}