lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* ========================================================================= |
| 2 | FILE NAME |
| 3 | libzte_dmapp.c |
| 4 | |
| 5 | GENERAL DESCRIPTION |
| 6 | This file include the dmapp lib's functions |
| 7 | |
| 8 | Copyright (c) 2012-2013 by ZTE Incorporated. All Rights Reserved. |
| 9 | ============================================================================ */ |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | #include <sys/types.h> |
| 15 | #include <sys/stat.h> |
| 16 | #include <dirent.h> |
| 17 | |
| 18 | #include "libzte_dmapp.h" |
| 19 | #include "fota_common.h" |
| 20 | |
| 21 | |
| 22 | stateflag_print_t stateflag_print[] = { |
| 23 | {RECOVERY, "RECOVERY"}, |
| 24 | {RECOVERY_KERNEL, "RECOVERY_KERNEL"}, |
| 25 | {CPUM0, "CPUM0"}, |
| 26 | {CPUZSP, "CPUZSP"}, |
| 27 | {CPUR7, "CPUR7"}, |
| 28 | {CPUAP, "CPUAP"}, |
| 29 | {CPFS, "CPFS"}, |
| 30 | {USERDATA, "USERDATA"}, |
| 31 | {SECUREFS, "SECUREFS"}, |
| 32 | |
| 33 | {STATEFLAG_NULL, 0} |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | static int zte_fota_get_fotaflag_partition_path(char *path) |
| 38 | { |
| 39 | int value = 0; |
| 40 | char fota_flag_part_name[256] = {'\0'}; |
| 41 | |
| 42 | if (path == NULL) { |
| 43 | printf("zte_fota_get_flag_partition_name fota_flag_part_name is NULL!\n"); |
| 44 | return -1; |
| 45 | } |
| 46 | |
| 47 | //open ZTE_DUA_PART_UPDATED_FLAG |
| 48 | //ÐÂÔö±éÀúfotaflag·ÖÇøÃû´úÂë------------------------------------------------------------------ |
| 49 | value = zte_fota_browse_fotaflag(ZTE_DUA_FOTAFLAG_BLOCK_PART, fota_flag_part_name, sizeof(fota_flag_part_name)); |
| 50 | if (1 == value) { |
| 51 | printf("[%s:%s]zte_fota_browse_fotaflag error!\n", __FILE__, __FUNCTION__); |
| 52 | return -1; |
| 53 | } |
| 54 | sprintf(path, "%s/%s", ZTE_DUA_PART_UPDATED_DEV, fota_flag_part_name); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | static int zte_fota_get_fotaflag(fotaflag_t *fotaflag) |
| 59 | { |
| 60 | #if 0 |
| 61 | char path[128] = {0}; |
| 62 | char fota_flag_temp[ZTE_FOTA_FLAG_LEN] = {0}; |
| 63 | int read_size = 0; |
| 64 | FILE *fota_flag_file; |
| 65 | |
| 66 | if (fotaflag == NULL) { |
| 67 | printf("zte_fota_get_fotaflag fotaflag is NULL!\n"); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | zte_fota_get_fotaflag_partition_path(path); |
| 72 | |
| 73 | fota_flag_file = fopen(path, "rb"); |
| 74 | //------------------------------------------------------------------------------------------ |
| 75 | |
| 76 | //fota_flag_file = fopen("/dev/zftl6", "wb+"); |
| 77 | if (fota_flag_file == NULL) { |
| 78 | printf("zte_fota_get_fotaflag Fota flag file does not exist!\n"); |
| 79 | return -1; |
| 80 | } |
| 81 | read_size = fread(fota_flag_temp, ZTE_FOTA_FLAG_LEN, 1, fota_flag_file); |
| 82 | if (read_size <= 0) { |
| 83 | printf("zte_fota_get_fotaflag read fota_flag_file failed!\n"); |
| 84 | return -1; |
| 85 | } |
| 86 | //fotaflag = (fotaflag_t *)fota_flag_temp; |
| 87 | memcpy(fotaflag, fota_flag_temp, sizeof(fotaflag_t)); |
| 88 | #endif |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | static int zte_fota_set_fotaflag(char *updateflag, stateflag_t stateflag, |
| 93 | int netdogflag_index, int netdogflag_value) |
| 94 | { |
| 95 | #if 0 |
| 96 | FILE *fota_flag_file; |
| 97 | int write_size = 0; |
| 98 | int read_size = 0; |
| 99 | |
| 100 | char path[128] = {0}; |
| 101 | |
| 102 | fotaflag_t *fotaflag; |
| 103 | char fota_flag_temp[ZTE_FOTA_FLAG_LEN] = {0}; |
| 104 | int write_back = 0; |
| 105 | int ret = -1; |
| 106 | |
| 107 | printf("zte_fota_set_stateflag start!\n"); |
| 108 | |
| 109 | zte_fota_get_fotaflag_partition_path(path); |
| 110 | |
| 111 | fota_flag_file = fopen(path, "wb+"); |
| 112 | //------------------------------------------------------------------------------------------ |
| 113 | |
| 114 | //fota_flag_file = fopen("/dev/zftl6", "wb+"); |
| 115 | if (fota_flag_file == NULL) { |
| 116 | printf("zte_fota_set_stateflag Fota flag file does not exist!\n"); |
| 117 | return -1; |
| 118 | } |
| 119 | read_size = fread(fota_flag_temp, ZTE_FOTA_FLAG_LEN, 1, fota_flag_file); |
| 120 | if (read_size <= 0) { |
| 121 | printf("zte_fota_set_stateflag read fota_flag_file failed!\n"); |
| 122 | return -1; |
| 123 | } |
| 124 | fotaflag = (fotaflag_t *)fota_flag_temp; |
| 125 | if (updateflag != NULL && strcmp(updateflag, "") != 0) { |
| 126 | memcpy(fotaflag->update_flag, updateflag, sizeof(fotaflag->update_flag)); |
| 127 | write_back = 1; |
| 128 | } |
| 129 | if (stateflag > 0) { |
| 130 | fotaflag->state_flag = stateflag; |
| 131 | write_back = 1; |
| 132 | } |
| 133 | if (netdogflag_index >= 0) { |
| 134 | fotaflag->netdog_flag[netdogflag_index] = netdogflag_value; |
| 135 | printf("zte_fota_set_fotaflag is %d!\n", fotaflag->netdog_flag[netdogflag_index]); |
| 136 | write_back = 1; |
| 137 | } |
| 138 | |
| 139 | fseek(fota_flag_file, 0, SEEK_SET); |
| 140 | |
| 141 | if (write_back) { |
| 142 | write_size = fwrite(fotaflag, 1, ZTE_FOTA_FLAG_LEN, fota_flag_file); |
| 143 | if (write_size <= 0) { |
| 144 | printf("zte_fota_set_stateflag Write fota_flag_file failed!\n"); |
| 145 | return -1; |
| 146 | } |
| 147 | printf("write_size = %d, write fota_flag_file success! End 20160624!\n", write_size); |
| 148 | fflush(fota_flag_file); |
| 149 | ret = fsync(fileno(fota_flag_file)); |
| 150 | printf("zte_fota_set_fotaflag fsync ret=%d\n", ret); |
| 151 | } |
| 152 | |
| 153 | fclose(fota_flag_file); |
| 154 | #endif |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | int zte_fota_set_updateflag(char *updateflag) |
| 159 | { |
| 160 | return zte_fota_set_fotaflag(updateflag, -1, -1, -1); |
| 161 | } |
| 162 | |
| 163 | char* zte_fota_get_updateflag(char *updateflag_buf) |
| 164 | { |
| 165 | fotaflag_t fotaflag = {0}; |
| 166 | |
| 167 | if (updateflag_buf == NULL) { |
| 168 | return NULL; |
| 169 | } |
| 170 | |
| 171 | if (zte_fota_get_fotaflag(&fotaflag) >= 0) { |
| 172 | memcpy(updateflag_buf, fotaflag.update_flag, sizeof(fotaflag.update_flag)); |
| 173 | return updateflag_buf; |
| 174 | } |
| 175 | return -1; |
| 176 | } |
| 177 | |
| 178 | |
| 179 | int zte_fota_set_stateflag(stateflag_t stateflag) |
| 180 | { |
| 181 | return zte_fota_set_fotaflag(NULL, stateflag, -1, -1); |
| 182 | } |
| 183 | |
| 184 | int zte_fota_get_stateflag() |
| 185 | { |
| 186 | #if 0 //klocwork |
| 187 | fotaflag_t fotaflag = {0}; |
| 188 | if (zte_fota_get_fotaflag(&fotaflag) >= 0) { |
| 189 | return fotaflag.state_flag; |
| 190 | } |
| 191 | return -1; |
| 192 | #endif |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | stateflag_t translate_stateflag_str_to_flag(char *stateflag_str) |
| 197 | { |
| 198 | if (stateflag_str == NULL) |
| 199 | return STATEFLAG_NULL; |
| 200 | |
| 201 | int i = 0; |
| 202 | |
| 203 | while (1) { |
| 204 | if (strcmp(stateflag_print[i].name, "") == 0) |
| 205 | break; |
| 206 | if (strcmp(stateflag_print[i].name, stateflag_str) == 0) { |
| 207 | return stateflag_print[i].stateflag; |
| 208 | } |
| 209 | i++; |
| 210 | } |
| 211 | return STATEFLAG_NULL; |
| 212 | } |
| 213 | |
| 214 | char* translate_stateflag_flag_to_str(stateflag_t stateflag) |
| 215 | { |
| 216 | int i = 0; |
| 217 | |
| 218 | while (1) { |
| 219 | if (stateflag_print[i].stateflag == STATEFLAG_NULL) |
| 220 | break; |
| 221 | if (stateflag_print[i].stateflag == stateflag) |
| 222 | return stateflag_print[i].name; |
| 223 | i++; |
| 224 | } |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | int zte_fota_set_netdogflag(int index, int value) |
| 230 | { |
| 231 | printf("zte_fota_set_netdogflag is %d %d!\n", index, value); |
| 232 | return zte_fota_set_fotaflag(NULL, -1, index, value); |
| 233 | } |
| 234 | |
| 235 | int zte_fota_get_netdogflag_single(int index) |
| 236 | { |
| 237 | #if 0 //klocwork |
| 238 | fotaflag_t fotaflag = {0}; |
| 239 | if (zte_fota_get_fotaflag(&fotaflag) >= 0) { |
| 240 | return fotaflag.netdog_flag[index]; |
| 241 | } |
| 242 | return -1; |
| 243 | #endif |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | char* zte_fota_get_netdogflag_all(char *netdogflag_buf) |
| 248 | { |
| 249 | fotaflag_t fotaflag = {0}; |
| 250 | |
| 251 | if (netdogflag_buf == NULL) { |
| 252 | return NULL; |
| 253 | } |
| 254 | if (zte_fota_get_fotaflag(&fotaflag) >= 0) { |
| 255 | memcpy(netdogflag_buf, fotaflag.netdog_flag, sizeof(fotaflag.netdog_flag)); |
| 256 | return netdogflag_buf; |
| 257 | } |
| 258 | return NULL; |
| 259 | } |
| 260 | |
| 261 | |
| 262 | /*********************************************************************** |
| 263 | * Description:²éÕÒfotflag·ÖÇøËù¶ÔÓ¦µÄÉ豸Ãû³Æ |
| 264 | * Function:zte_fota_browse_fotaflag() |
| 265 | * Input: |
| 266 | * Output: |
| 267 | * Return:ֵΪ0ΪÕÒµ½fotaflag·ÖÇø£¬ÖµÎª1ÕÒ·ÖÇøÊ§°Ü |
| 268 | *************************************************************************/ |
| 269 | int zte_fota_browse_fotaflag(char *pathname, char *fota_flag_part_name, unsigned int len) |
| 270 | { |
| 271 | FILE *fd = NULL; |
| 272 | DIR *pdir = NULL;//¶¨ÒåÒ»¸öĿ¼Á÷;¾ÍÏñÊǶ¨ÒåÎļþÁ÷Ò»Ñù |
| 273 | struct dirent *pentry = NULL;//¶¨ÒåĿ¼½á¹¹Ìå |
| 274 | struct stat statbuf; |
| 275 | char buffer[128] = {0}; |
| 276 | char fullpath[256] = {0}; |
| 277 | |
| 278 | if (NULL == pathname || NULL == fota_flag_part_name || len <= 0) { |
| 279 | printf("zte_fota_browse_fotaflag has invalid parameter!\n"); |
| 280 | return 1; |
| 281 | } |
| 282 | pdir = opendir(pathname);//´ò¿ªÒ»¸ö·¾¶²¢·µ»ØÒ»¸öĿ¼Á÷¡£ |
| 283 | if (pdir == NULL) { |
| 284 | printf("zte_fota_browse_fotaflag opendir %s error!\n", pathname); |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | while ((pentry = readdir(pdir)) != NULL) { |
| 289 | if (!strcmp(pentry->d_name, ".") || !strcmp(pentry->d_name, "..")) |
| 290 | continue; |
| 291 | |
| 292 | snprintf(fullpath, sizeof(fullpath), "%s/%s/%s", pathname, pentry->d_name, ZTE_DUA_FOTAFLAG_PART); |
| 293 | memset(&statbuf, 0, sizeof(struct stat)); |
| 294 | if (stat(fullpath, &statbuf)) { |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | fd = fopen(fullpath, "r"); |
| 299 | if (NULL == fd) { |
| 300 | printf("zte_fota_browse_fotaflag fopen %s failed!\n", fullpath); |
| 301 | closedir(pdir); |
| 302 | return 1; |
| 303 | } |
| 304 | memset(buffer, 0, sizeof(buffer)); |
| 305 | (void)fgets(buffer, sizeof(buffer), fd); |
| 306 | if (!strncmp(buffer, ZTE_DUA_FOTAFLAG, strlen(ZTE_DUA_FOTAFLAG))) { |
| 307 | if (strlen(pentry->d_name) <= (len - 1)) { |
| 308 | strncpy(fota_flag_part_name, pentry->d_name, strlen(pentry->d_name)); |
| 309 | fclose(fd); |
| 310 | closedir(pdir); |
| 311 | return 0; |
| 312 | } else { |
| 313 | printf("zte_fota_browse_fotaflag over_array_range error!pentry->d_name=%s, strlen(pentry->d_name)=%d\n", |
| 314 | pentry->d_name, strlen(pentry->d_name)); |
| 315 | fclose(fd); |
| 316 | closedir(pdir); |
| 317 | return 1; |
| 318 | } |
| 319 | } |
| 320 | fclose(fd); |
| 321 | } |
| 322 | closedir(pdir); |
| 323 | return 1; |
| 324 | } |