blob: 2ca23821aa84baa1dee4cf64b9d108e4c8e51c64 [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
lichengzhangea38e902025-06-14 11:53:03 +080014#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
b.liu68a94c92025-05-24 12:53:41 +080028#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
lichengzhangea38e902025-06-14 11:53:03 +080038#define LOGV(fmt, args ...) \
39 do{ \
b.liu68a94c92025-05-24 12:53:41 +080040 char *file_ptr_1001 = __FILE__; \
lichengzhangea38e902025-06-14 11:53:03 +080041 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 } \
49 fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
50 } 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 } \
63 fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
64 } 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 } \
77 fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
78 } 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 } \
91 fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \
92 } 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
lichengzhangea38e902025-06-14 11:53:03 +0800159// static void deinit_fun_handle()
160// {
161// if(handle != NULL)
162// {
163// dlclose(handle);
164// handle = NULL;
165// }
b.liu68a94c92025-05-24 12:53:41 +0800166
lichengzhangea38e902025-06-14 11:53:03 +0800167// fun_ptr_log = NULL;
168// mbtk_write_fun_ptr = NULL;
169// mbtk_read_fun_ptr = NULL;
b.liu68a94c92025-05-24 12:53:41 +0800170
lichengzhangea38e902025-06-14 11:53:03 +0800171// }
b.liu68a94c92025-05-24 12:53:41 +0800172
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);
lichengzhangea38e902025-06-14 11:53:03 +0800191 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800192 return GSW_HAL_NORMAL_FAIL;
193 }
194
195 if (!data || !len)
196 {
197 LOGE("invalid :data or len us NULL\n");
lichengzhangea38e902025-06-14 11:53:03 +0800198 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800199 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 ");
lichengzhangea38e902025-06-14 11:53:03 +0800206 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800207 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);
lichengzhangea38e902025-06-14 11:53:03 +0800217 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800218 return GSW_HAL_NORMAL_FAIL;
219 }
220 *len = actual_len;
221 memcpy(data, temp_buffer, *len);
222 free(temp_buffer);
lichengzhangea38e902025-06-14 11:53:03 +0800223 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800224
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);
lichengzhangea38e902025-06-14 11:53:03 +0800244 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800245 return GSW_HAL_NORMAL_FAIL;
246 }
247 if (!data || len > ERASE_BLOCK_SIZE)
248 {
249 LOGE("invalid data is NULL ");
lichengzhangea38e902025-06-14 11:53:03 +0800250 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800251 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");
lichengzhangea38e902025-06-14 11:53:03 +0800258 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800259 return GSW_HAL_NORMAL_FAIL;
260 }
lichengzhangea38e902025-06-14 11:53:03 +0800261 //deinit_fun_handle();
b.liu68a94c92025-05-24 12:53:41 +0800262 return GSW_HAL_SUCCESS;
263}
hong.liud2417072025-06-27 07:10:37 -0700264/**
265 * @brief SDK interface to read data from spec page of flash
266 * @param [in] read data
267 * @param [in] size, it must be less than (64 * 4096 - 8)Byte
268 * @retval 0: success
269 * @retval other: fail
270 */
271int gsw_oem_read_data(void *data, unsigned int *len)
272{
273 int ret = 0;
274
275 return ret;
276}
277
278/**
279 * @brief SDK interface to write data to spec page of flash
280 * @param [in] write data
281 * @param [in] size, it must be less than (64 * 4096 - 8)Byte
282 * @retval 0: success
283 * @retval other: fail
284 */
285int gsw_oem_write_data(void *data, unsigned int len)
286{
287 int ret = 0;
288
289 return ret;
290}
291