| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <fcntl.h> | |
| #include <unistd.h> | |
| #include <sys/ioctl.h> | |
| #include <mtd/mtd-user.h> | |
| #include <errno.h> | |
| #include <dlfcn.h> | |
| #include "gsw_oem_rw_interface.h" | |
| #ifndef LOG_ERR_LEVEL | |
| #define LOG_ERR_LEVEL 3 /* error conditions */ | |
| #endif | |
| #ifndef LOG_WARN_LEVEL | |
| #define LOG_WARN_LEVEL 4 /* warning conditions */ | |
| #endif | |
| #ifndef LOG_INFO_LEVEL | |
| #define LOG_INFO_LEVEL 6 /* informational */ | |
| #endif | |
| #ifndef LOG_DEBUG_LEVEL | |
| #define LOG_DEBUG_LEVEL 7 /* debug-level messages */ | |
| #endif | |
| #ifndef LOG_VERBOSE_LEVEL | |
| #define LOG_VERBOSE_LEVEL 8 | |
| #endif | |
| #define ERASE_BLOCK_SIZE (256*1024) | |
| #define LIB_PATH "/lib/libmbtk_lib.so" | |
| #define GSW_OEM_RW "[HAL][GSW_OEM_RW]" | |
| #define StatFunc(x,y) stat(x,y) | |
| #define LOGV(fmt, args ...) \ | |
| do{ \ | |
| char *file_ptr_1001 = __FILE__; \ | |
| char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
| char line_1001[10] = {0}; \ | |
| sprintf(line_1001, "%d", __LINE__); \ | |
| while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
| if(*ptr_1001 == '/') \ | |
| break; \ | |
| ptr_1001--; \ | |
| } \ | |
| fun_ptr_log(LOG_VERBOSE_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \ | |
| } while(0) | |
| #define LOGI(fmt, args...) \ | |
| do{ \ | |
| char *file_ptr_1001 = __FILE__; \ | |
| char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
| char line_1001[10] = {0}; \ | |
| sprintf(line_1001, "%d", __LINE__); \ | |
| while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
| if(*ptr_1001 == '/') \ | |
| break; \ | |
| ptr_1001--; \ | |
| } \ | |
| fun_ptr_log(LOG_INFO_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \ | |
| } while(0) | |
| #define LOGD(fmt, args...) \ | |
| do{ \ | |
| char *file_ptr_1001 = __FILE__; \ | |
| char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
| char line_1001[10] = {0}; \ | |
| sprintf(line_1001, "%d", __LINE__); \ | |
| while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
| if(*ptr_1001 == '/') \ | |
| break; \ | |
| ptr_1001--; \ | |
| } \ | |
| fun_ptr_log(LOG_DEBUG_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \ | |
| } while(0) | |
| #define LOGW(fmt, args...) \ | |
| do{ \ | |
| char *file_ptr_1001 = __FILE__; \ | |
| char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
| char line_1001[10] = {0}; \ | |
| sprintf(line_1001, "%d", __LINE__); \ | |
| while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
| if(*ptr_1001 == '/') \ | |
| break; \ | |
| ptr_1001--; \ | |
| } \ | |
| fun_ptr_log(LOG_WARN_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \ | |
| } while(0) | |
| #define LOGE(fmt, args...) \ | |
| do{ \ | |
| char *file_ptr_1001 = __FILE__; \ | |
| char *ptr_1001 = file_ptr_1001 + strlen(file_ptr_1001) - 1; \ | |
| char line_1001[10] = {0}; \ | |
| sprintf(line_1001, "%d", __LINE__); \ | |
| while(ptr_1001 >= file_ptr_1001 && *ptr_1001){ \ | |
| if(*ptr_1001 == '/') \ | |
| break; \ | |
| ptr_1001--; \ | |
| } \ | |
| fun_ptr_log(LOG_ERR_LEVEL, "%s#%s: "GSW_OEM_RW"" fmt, ptr_1001 + 1, line_1001, ##args); \ | |
| } while(0) | |
| typedef void (*mbtk_log)(int level, const char *format,...); | |
| typedef int (*mbtk_oem_read_data_ext)(unsigned int block_id, void *data, unsigned int *len); | |
| typedef int (*mbtk_oem_write_data_ext)(unsigned int block_id, void *data, unsigned int len); | |
| static mbtk_log fun_ptr_log = NULL; | |
| static mbtk_oem_read_data_ext mbtk_read_fun_ptr = NULL; | |
| static mbtk_oem_write_data_ext mbtk_write_fun_ptr = NULL; | |
| static void *handle = NULL; | |
| static int init_fun_handle() | |
| { | |
| if(handle == NULL) | |
| { | |
| handle = dlopen(LIB_PATH, RTLD_NOW ); | |
| if(handle == NULL) | |
| { | |
| return -1; | |
| } | |
| if(fun_ptr_log == NULL) | |
| { | |
| fun_ptr_log = (mbtk_log)dlsym(handle, "mbtk_log"); | |
| if(fun_ptr_log == NULL) | |
| { | |
| return -1; | |
| } | |
| } | |
| if(mbtk_read_fun_ptr == NULL) | |
| { | |
| mbtk_read_fun_ptr = (mbtk_oem_read_data_ext)dlsym(handle, "mbtk_oem_read_data_ext"); | |
| if(mbtk_read_fun_ptr == NULL) | |
| { | |
| return -1; | |
| } | |
| } | |
| if(mbtk_write_fun_ptr == NULL) | |
| { | |
| mbtk_write_fun_ptr = (mbtk_oem_write_data_ext)dlsym(handle, "mbtk_oem_write_data_ext"); | |
| if(mbtk_write_fun_ptr == NULL) | |
| { | |
| return -1; | |
| } | |
| } | |
| } | |
| return 0; | |
| } | |
| // static void deinit_fun_handle() | |
| // { | |
| // if(handle != NULL) | |
| // { | |
| // dlclose(handle); | |
| // handle = NULL; | |
| // } | |
| // fun_ptr_log = NULL; | |
| // mbtk_write_fun_ptr = NULL; | |
| // mbtk_read_fun_ptr = NULL; | |
| // } | |
| /** | |
| * @brief Read data from spec block of flash | |
| * @param [in] unsigned int | |
| * @param [out] void * | |
| * @param [out] unsigned int * | |
| * @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL | |
| */ | |
| int gsw_oem_read_data_ext(unsigned int block_id, void *data, unsigned int *len) | |
| { | |
| int ret = -1; | |
| unsigned int actual_len = 0; | |
| char *temp_buffer = NULL; | |
| ret = init_fun_handle(); | |
| if(ret == -1) | |
| { | |
| printf(" init_fun_handle error ret %d\n",ret); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| if (!data || !len) | |
| { | |
| LOGE("invalid :data or len us NULL\n"); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_ARG_INVALID; | |
| } | |
| temp_buffer = (char *)malloc(ERASE_BLOCK_SIZE); | |
| if (temp_buffer == NULL) | |
| { | |
| LOGE("Failed to allocate read buffer "); | |
| //deinit_fun_handle(); | |
| return -1; | |
| } | |
| memset(temp_buffer, 0, ERASE_BLOCK_SIZE); | |
| ret = mbtk_read_fun_ptr(block_id,temp_buffer,&actual_len); | |
| if(ret != 0) | |
| { | |
| LOGE("mbtk_read_fun_ptr failed"); | |
| free(temp_buffer); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| *len = actual_len; | |
| memcpy(data, temp_buffer, *len); | |
| free(temp_buffer); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_SUCCESS; | |
| } | |
| /** | |
| * @brief Write data to spec block of flash | |
| * @param [in] unsigned int | |
| * @param [in] void * | |
| * @param [in] unsigned int | |
| * @retval 0: success | |
| * @retval GSW_HAL_SUCCESS\GSW_HAL_FAIL | |
| */ | |
| int gsw_oem_write_data_ext(unsigned int block_id, void *data, unsigned int len) | |
| { | |
| int ret = -1; | |
| ret = init_fun_handle(); | |
| if(ret == -1) | |
| { | |
| printf("init log error ret %d\n",ret); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| if (!data || len > ERASE_BLOCK_SIZE) | |
| { | |
| LOGE("invalid data is NULL "); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_ARG_INVALID; | |
| } | |
| ret = mbtk_write_fun_ptr(block_id,data,len); | |
| if(ret != 0) | |
| { | |
| LOGE("mbtk_write_fun_ptr failed"); | |
| //deinit_fun_handle(); | |
| return GSW_HAL_NORMAL_FAIL; | |
| } | |
| //deinit_fun_handle(); | |
| return GSW_HAL_SUCCESS; | |
| } | |
| /** | |
| * @brief SDK interface to read data from spec page of flash | |
| * @param [in] read data | |
| * @param [in] size, it must be less than (64 * 4096 - 8)Byte | |
| * @retval 0: success | |
| * @retval other: fail | |
| */ | |
| int gsw_oem_read_data(void *data, unsigned int *len) | |
| { | |
| int ret = 0; | |
| return ret; | |
| } | |
| /** | |
| * @brief SDK interface to write data to spec page of flash | |
| * @param [in] write data | |
| * @param [in] size, it must be less than (64 * 4096 - 8)Byte | |
| * @retval 0: success | |
| * @retval other: fail | |
| */ | |
| int gsw_oem_write_data(void *data, unsigned int len) | |
| { | |
| int ret = 0; | |
| return ret; | |
| } | |