blob: b17b27f0bfe83b39aab14a5e25adf5af3a593d69 [file] [log] [blame]
b.liu3a41a312024-02-28 09:57:39 +08001/*
2* mbtk_mtd.c
3*
4* MBTK mtd partition utils API.
5*
6*/
7/******************************************************************************
8
9 EDIT HISTORY FOR FILE
10
11 WHEN WHO WHAT,WHERE,WHY
12-------- -------- -------------------------------------------------------
132024/2/26 LiuBin Initial version
14
15******************************************************************************/
16#include <stdio.h>
17#include <stdlib.h>
18#include <unistd.h>
19#include <errno.h>
20
21#include "mbtk_mtd.h"
22#include "mbtk_log.h"
23#include "mbtk_str.h"
24
25static mbtk_partition_info_t partition_list[MBTK_PARTITION_NUM_MAX];
26static bool partition_inited = FALSE;
27
28mbtk_partition_info_t* mbtk_partition_get()
29{
30 if(partition_inited) {
31 return partition_list;
32 }
33
34 memset(partition_list, 0x0, sizeof(partition_list));
35 FILE *fp = fopen("/proc/mtd", "r");
36 if(fp == NULL) {
37 LOGE("fopen(/proc/mtd) fail:%d", errno);
38 return NULL;
39 }
40
41 char buff[64];
42 int index = 0;
43 char size_str[16];
44 char name[32];
b.liuf678f992024-05-08 15:23:10 +080045 char erase_size_str[16];
b.liu45bfb532024-05-11 15:37:22 +080046 char name_temp[32];
b.liu3a41a312024-02-28 09:57:39 +080047 memset(buff, 0x0, 64);
b.liu45bfb532024-05-11 15:37:22 +080048 memset(name_temp, 0x0, 32);
b.liu3a41a312024-02-28 09:57:39 +080049 while(fgets(buff, 64, fp)) {
50 if(str_startwith(buff, "mtd")) {
51 memset(size_str, 0x0, 16);
b.liuf678f992024-05-08 15:23:10 +080052 memset(erase_size_str, 0x0, sizeof(erase_size_str));
b.liu3a41a312024-02-28 09:57:39 +080053 memset(name, 0x0, 32);
b.liuf678f992024-05-08 15:23:10 +080054 if(4 == sscanf(buff, "%s %s %s %s", partition_list[index].dev, size_str, erase_size_str, name)) {
b.liu3a41a312024-02-28 09:57:39 +080055 if(name[0] == '\"' && name[strlen(name) - 1] == '\"') {
56 memcpy(partition_list[index].name, name + 1, strlen(name) - 2); // No copy ""
57 } else {
58 LOGE("partition(%s) name error.", buff);
59 return NULL;
60 }
61
62 if(partition_list[index].dev[strlen(partition_list[index].dev) - 1] == ':') {
63 partition_list[index].dev[strlen(partition_list[index].dev) - 1] = '\0';
64 }
65
66 partition_list[index].partition_size = (uint32)strtoul(size_str, NULL, 16);
b.liuf678f992024-05-08 15:23:10 +080067 partition_list[index].erase_size = (uint32)strtoul(erase_size_str, NULL, 16);
b.liu45bfb532024-05-11 15:37:22 +080068 // XXX-sdtim or XXX-mount
69 ssize_t index_sdtim = str_indexof(partition_list[index].name, "-sdtim");
70 ssize_t index_mount = str_indexof(partition_list[index].name, "-mount");
71 if(index_sdtim > 0 || index_mount > 0) {
72 if(index_sdtim > 0) {
73 memcpy(name_temp, partition_list[index].name, index_sdtim);
74 } else {
75 memcpy(name_temp, partition_list[index].name, index_mount);
76 }
77 if(index > 0) {
78 partition_list[index].partition_start = partition_list[index - 1].partition_start + partition_list[index - 1].partition_size;
79 }
80 } else {
81 if(strlen(name_temp) > 0) {
82 memset(name_temp, 0x0, 32);
83 if(index > 0) {
84 partition_list[index].partition_start = partition_list[index - 2].partition_start;
85 }
86 } else {
87 if(index > 0) {
88 partition_list[index].partition_start = partition_list[index - 1].partition_start + partition_list[index - 1].partition_size;
89 }
90 }
b.liu3a41a312024-02-28 09:57:39 +080091 }
92 partition_list[index].used = TRUE;
93 } else {
94 LOGE("sscanf(%s) fail:%d", buff, errno);
95 return NULL;
96 }
97 index++;
98 }
99 memset(buff, 0x0, 64);
100 }
101 fclose(fp);
102
103 int i = 0;
104 while(i < MBTK_PARTITION_NUM_MAX) {
105 if(partition_list[i].used) {
106 LOGD("%s(%s) : %08x %08x", partition_list[i].name, partition_list[i].dev,
107 partition_list[i].partition_start, partition_list[i].partition_size);
108 }
109 i++;
110 }
111
112 partition_inited = TRUE;
113 return partition_list;
114}
115
b.liuf678f992024-05-08 15:23:10 +0800116int mbtk_partition_get_by_name(char *partition_name, mbtk_partition_info_t *info)
117{
118 FILE *fp = fopen("/proc/mtd", "r");
119 if(fp == NULL) {
120 LOGE("fopen(/proc/mtd) fail:%d", errno);
121 return -1;
122 }
123
124 char buff[64];
125 char size_str[16];
126 char erase_size_str[16];
127 char name[32];
128 memset(buff, 0x0, sizeof(buff));
129 while(fgets(buff, sizeof(buff), fp)) {
130 if(strstr(buff, partition_name)) {
131 memset(size_str, 0x0, sizeof(size_str));
132 memset(erase_size_str, 0x0, sizeof(erase_size_str));
133 memset(name, 0x0, sizeof(name));
134 memcpy(info->dev, "/dev/", 5);
135 if(4 == sscanf(buff, "%s %s %s %s", info->dev + 5, size_str, erase_size_str, name)) {
136 if(name[0] == '\"' && name[strlen(name) - 1] == '\"') {
137 memcpy(info->name, name + 1, strlen(name) - 2); // No copy ""
138 } else {
139 LOGE("partition(%s) name error.", buff);
140 return -1;
141 }
142
143 if(info->dev[strlen(info->dev) - 1] == ':') {
144 info->dev[strlen(info->dev) - 1] = '\0';
145 }
146
147 info->partition_size = (uint32)strtoul(size_str, NULL, 16);
148 info->erase_size = (uint32)strtoul(erase_size_str, NULL, 16);
149 //info->partition_start += info->partition_size;
150 break;
151 } else {
152 LOGE("sscanf(%s) fail:%d", buff, errno);
153 return -1;
154 }
155 }
156 memset(buff, 0x0, sizeof(buff));
157 }
158 fclose(fp);
159
160 return 0;
161}
162
b.liu3a41a312024-02-28 09:57:39 +0800163