blob: e0d087705abd1a16279b8ddeadf23b0104be5711 [file] [log] [blame]
b.liu68a94c92025-05-24 12:53:41 +08001#include <stdio.h>
2#include <stdlib.h>
3#include <string.h>
4#include <fcntl.h>
5#include <unistd.h>
6#include <sys/ioctl.h>
7#include <mtd/mtd-user.h>
8#include <errno.h>
9#include <dlfcn.h>
10
11#include "gsw_oem_rw_interface.h"
12
13
14#ifndef LOG_ERR_LEVEL
15#define LOG_ERR_LEVEL 3 /* error conditions */
16#endif
17#ifndef LOG_WARN_LEVEL
18#define LOG_WARN_LEVEL 4 /* warning conditions */
19#endif
20#ifndef LOG_INFO_LEVEL
21#define LOG_INFO_LEVEL 6 /* informational */
22#endif
23#ifndef LOG_DEBUG_LEVEL
24#define LOG_DEBUG_LEVEL 7 /* debug-level messages */
25#endif
26#ifndef LOG_VERBOSE_LEVEL
27#define LOG_VERBOSE_LEVEL 8
28#endif
29
30#define ERASE_BLOCK_SIZE (256*1024)
31
32#define LIB_PATH "/lib/libmbtk_lib.so"
33
l.yang6a42e4d2025-05-28 01:04:20 -070034#define GSW_OEM_RW "[HAL][GSW_OEM_RW]"
b.liu68a94c92025-05-24 12:53:41 +080035
36#define StatFunc(x,y) stat(x,y)
37
38#define LOGV(fmt, args ...) \
39 do{ \
40 char *file_ptr_1001 = __FILE__; \
41 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
42 char line_1001[10] = {0}; \
43 sprintf(line_1001, "%d", __LINE__); \
44 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
45 if(*ptr_1001 == '/') \
46 break; \
47 ptr_1001--; \
48 } \
l.yang6a42e4d2025-05-28 01:04:20 -070049 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080050 } while(0)
51
52#define LOGI(fmt, args...) \
53 do{ \
54 char *file_ptr_1001 = __FILE__; \
55 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
56 char line_1001[10] = {0}; \
57 sprintf(line_1001, "%d", __LINE__); \
58 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
59 if(*ptr_1001 == '/') \
60 break; \
61 ptr_1001--; \
62 } \
l.yang6a42e4d2025-05-28 01:04:20 -070063 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080064 } while(0)
65
66#define LOGD(fmt, args...) \
67 do{ \
68 char *file_ptr_1001 = __FILE__; \
69 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
70 char line_1001[10] = {0}; \
71 sprintf(line_1001, "%d", __LINE__); \
72 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
73 if(*ptr_1001 == '/') \
74 break; \
75 ptr_1001--; \
76 } \
l.yang6a42e4d2025-05-28 01:04:20 -070077 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080078 } while(0)
79
80#define LOGW(fmt, args...) \
81 do{ \
82 char *file_ptr_1001 = __FILE__; \
83 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
84 char line_1001[10] = {0}; \
85 sprintf(line_1001, "%d", __LINE__); \
86 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
87 if(*ptr_1001 == '/') \
88 break; \
89 ptr_1001--; \
90 } \
l.yang6a42e4d2025-05-28 01:04:20 -070091 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +080092 } while(0)
93
94#define LOGE(fmt, args...) \
95 do{ \
96 char *file_ptr_1001 = __FILE__; \
97 char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \
98 char line_1001[10] = {0}; \
99 sprintf(line_1001, "%d", __LINE__); \
100 while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \
101 if(*ptr_1001 == '/') \
102 break; \
103 ptr_1001--; \
104 } \
l.yang6a42e4d2025-05-28 01:04:20 -0700105 fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
b.liu68a94c92025-05-24 12:53:41 +0800106 } while(0)
107
108typedef void (*mbtk_log)(int level, const char *format,...);
109
110typedef int (*mbtk_oem_read_data_ext)(unsigned int block_id, void *data, unsigned int *len);
111
112typedef int (*mbtk_oem_write_data_ext)(unsigned int block_id, void *data, unsigned int len);
113
114static mbtk_log fun_ptr_log = NULL;
115
116static mbtk_oem_read_data_ext mbtk_read_fun_ptr = NULL;
117static mbtk_oem_write_data_ext mbtk_write_fun_ptr = NULL;
118
119static void *handle = NULL;
120
121static int init_fun_handle()
122{
123 if(handle == NULL)
124 {
125 handle = dlopen(LIB_PATH, RTLD_NOW );
126 if(handle == NULL)
127 {
128 return -1;
129 }
130 if(fun_ptr_log == NULL)
131 {
132 fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log");
133 if(fun_ptr_log == NULL)
134 {
135 return -1;
136 }
137 }
138 if(mbtk_read_fun_ptr == NULL)
139 {
140 mbtk_read_fun_ptr = (mbtk_oem_read_data_ext)dlsym(handle, "mbtk_oem_read_data_ext");
141 if(mbtk_read_fun_ptr == NULL)
142 {
143 return -1;
144 }
145 }
146 if(mbtk_write_fun_ptr == NULL)
147 {
148 mbtk_write_fun_ptr = (mbtk_oem_write_data_ext)dlsym(handle, "mbtk_oem_write_data_ext");
149 if(mbtk_write_fun_ptr == NULL)
150 {
151 return -1;
152 }
153 }
154
155 }
156 return 0;
157}
158
159static void deinit_fun_handle()
160{
161 if(handle != NULL)
162 {
163 dlclose(handle);
164 handle = NULL;
165 }
166
167 fun_ptr_log = NULL;
168 mbtk_write_fun_ptr = NULL;
169 mbtk_read_fun_ptr = NULL;
170
171}
172
173/**
174 * @brief Read data from spec block of flash
175 * @param [in] unsigned int
176 * @param [out] void *
177 * @param [out] unsigned int *
178 * @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
179 */
180
181int gsw_oem_read_data_ext(unsigned int block_id, void *data, unsigned int *len)
182{
183
184 int ret = -1;
185 unsigned int actual_len = 0;
186 char *temp_buffer = NULL;
187 ret = init_fun_handle();
188 if(ret == -1)
189 {
190 printf(" init_fun_handle error ret %d\n",ret);
191 deinit_fun_handle();
192 return GSW_HAL_NORMAL_FAIL;
193 }
194
195 if (!data || !len)
196 {
197 LOGE("invalid :data or len us NULL\n");
198 deinit_fun_handle();
199 return GSW_HAL_ARG_INVALID;
200 }
201
202 temp_buffer = (char *)malloc(ERASE_BLOCK_SIZE);
203 if (temp_buffer == NULL)
204 {
205 LOGE("Failed to allocate read buffer ");
206 deinit_fun_handle();
207 return -1;
208 }
209
210 memset(temp_buffer, 0, ERASE_BLOCK_SIZE);
211
212 ret = mbtk_read_fun_ptr(block_id,temp_buffer,&actual_len);
213 if(ret != 0)
214 {
215 LOGE("mbtk_read_fun_ptr failed");
216 free(temp_buffer);
217 deinit_fun_handle();
218 return GSW_HAL_NORMAL_FAIL;
219 }
220 *len = actual_len;
221 memcpy(data, temp_buffer, *len);
222 free(temp_buffer);
223 deinit_fun_handle();
224
225 return GSW_HAL_SUCCESS;
226}
227
228/**
229 * @brief Write data to spec block of flash
230 * @param [in] unsigned int
231 * @param [in] void *
232 * @param [in] unsigned int
233 * @retval 0: success
234 * @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL
235 */
236
237int gsw_oem_write_data_ext(unsigned int block_id, void *data, unsigned int len)
238{
239 int ret = -1;
240 ret = init_fun_handle();
241 if(ret == -1)
242 {
243 printf("init log error ret %d\n",ret);
244 deinit_fun_handle();
245 return GSW_HAL_NORMAL_FAIL;
246 }
247 if (!data || len > ERASE_BLOCK_SIZE)
248 {
249 LOGE("invalid data is NULL ");
250 deinit_fun_handle();
251 return GSW_HAL_ARG_INVALID;
252 }
253
254 ret = mbtk_write_fun_ptr(block_id,data,len);
255 if(ret != 0)
256 {
257 LOGE("mbtk_write_fun_ptr failed");
258 deinit_fun_handle();
259 return GSW_HAL_NORMAL_FAIL;
260 }
261 deinit_fun_handle();
262 return GSW_HAL_SUCCESS;
263}