From 11f18650e86054dcea6f8a69025a5e33d431b79d Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Thu, 21 Sep 2017 15:13:45 +0900 Subject: [PATCH 01/16] Fix strtok_r bug In previous code the strtok and strtok_r were mixed in order to preserve each context. When fixed svace issues, this intention was not considered. Fix the bug about it. Change-Id: I76d838370fa5d3d0ebcf16b0e33e3055f5b869f8 Signed-off-by: Sunmin Lee --- ss_engine/SS_UPI.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index b974c18..be6bbcd 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -1158,7 +1158,8 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) } while (pline) { - pfilePath = strtok_r(pline, "\"", &psaveptr); + char *saveptr_pline; + pfilePath = strtok_r(pline, "\"", &saveptr_pline); if (pfilePath && strcmp(pfilePath, SS_FWSLASH) == 0) { LOGE("\n skip root: it is RO"); @@ -1166,9 +1167,9 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) continue; } - pfiletype = strtok_r(NULL, SS_TOKEN_SPACE, &psaveptr); - attributSize = strtok_r(NULL, SS_TOKEN_SPACE, &psaveptr); - pattribs = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); + pfiletype = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); + attributSize = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); + pattribs = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr_pline); LOG("\nSS_FSSetAttributes [%s][%s][%s]", pfiletype, attributSize, pattribs); if (pattribs && pfilePath && pfiletype) { ulAttribSize = strlen(pattribs); -- 2.7.4 From 845368afe6a37474fb2ceb4ba74f3e5249c38926 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Fri, 22 Sep 2017 13:52:01 +0900 Subject: [PATCH 02/16] Apply Tizen coding rule Change-Id: Ic2e65905e90fa40b0dd6e637f66b46a6a41059b2 Signed-off-by: Sunmin Lee --- bsdiff/ss_bsdiff.c | 16 +- bsdiff/ss_bspatch.c | 7 +- bsdiff/ss_bspatch_common.c | 13 +- ss_engine/SS_ApplyPatch.c | 133 +- ss_engine/SS_Common.c | 109 +- ss_engine/SS_Common.h | 34 +- ss_engine/SS_Engine_Errors.h | 78 +- ss_engine/SS_Engine_Update.h | 122 +- ss_engine/SS_FSUpdate.c | 222 ++- ss_engine/SS_FSUpdate.h | 57 +- ss_engine/SS_ImageUpdate.h | 22 +- ss_engine/SS_MultiProcessUpdate.h | 32 +- ss_engine/SS_PatchDelta.c | 2187 +++++++++++++------------ ss_engine/SS_PatchDelta.h | 26 +- ss_engine/SS_UPI.c | 3156 ++++++++++++++++++------------------- ss_engine/SS_UPI.h | 56 +- ss_engine/fota_common.h | 142 +- ss_engine/fota_log.c | 32 +- ss_engine/fota_log.h | 28 +- ss_engine/fota_tar.c | 1904 +++++++++++----------- ss_engine/ua.h | 106 +- 21 files changed, 4210 insertions(+), 4272 deletions(-) diff --git a/bsdiff/ss_bsdiff.c b/bsdiff/ss_bsdiff.c index 7415c6b..68967ee 100755 --- a/bsdiff/ss_bsdiff.c +++ b/bsdiff/ss_bsdiff.c @@ -77,12 +77,11 @@ void get_time_stamp(void) #include #endif -#define MIN(x,y) (((x)<(y)) ? (x) : (y)) +#define MIN(x, y) (((x) < (y)) ? (x) : (y)) #ifdef MULTI_THREADING -struct data_thread -{ +struct data_thread { unsigned num_threads; off_t size_thread; @@ -113,7 +112,7 @@ int Function(int); #endif -static off_t matchlen(u_char *old, off_t oldsize,u_char *new, off_t newsize) +static off_t matchlen(u_char *old, off_t oldsize, u_char *new, off_t newsize) { off_t i; for (i = 0; (i < oldsize) && (i < newsize); i++) @@ -128,11 +127,10 @@ static off_t search(saidx_t *I, u_char *old, off_t oldsize, off_t x, y; while (en - st >= 2) { x = st + (en - st) / 2; - if (memcmp(old + I[x], new, MIN(oldsize - I[x], newsize)) < 0) { + if (memcmp(old + I[x], new, MIN(oldsize - I[x], newsize)) < 0) st = x; - } else { + else en = x; - } } x = matchlen(old + I[st], oldsize - I[st], new, newsize); @@ -286,7 +284,7 @@ int Function(int offset_oldscore) { unsigned int thread_num = 0; off_t end; - off_t scan=0; + off_t scan = 0; end = data.newsize; int t1 = 0, t2 = 0; get_time_stamp(); //total time capturing @@ -561,7 +559,7 @@ int MY_CDECL main(int numArgs, const char *args[]) { char rs[800] = { 0 }; if (numArgs != 4) - errx(1,"ss_bsdiff Version 5.0\nUsage: ss_bsdiff oldfile newfile patchfile\n"); + errx(1, "ss_bsdiff Version 5.0\nUsage: ss_bsdiff oldfile newfile patchfile\n"); int ret = create_patch(numArgs, args, 8); #ifdef TIME_LIMIT_CHECK diff --git a/bsdiff/ss_bspatch.c b/bsdiff/ss_bspatch.c index 468bb11..1915223 100755 --- a/bsdiff/ss_bspatch.c +++ b/bsdiff/ss_bspatch.c @@ -115,14 +115,13 @@ int main2(int numArgs, const char *args[], char *rs, int rs_size) LzmaDec_Free(&state, &g_Alloc); File_Close(&inStream.file); if (apply_patch(args[1], buf_res, &new_data, &new_size) != 0) { - if (new_data){ + if (new_data) free(new_data); - } return 1; } if (((fd = open(args[2], O_CREAT | O_TRUNC | O_WRONLY, 0666)) < 0) || (write(fd, new_data, new_size) != new_size) || (close(fd) == -1)) - err(1,"%s",args[2]); + err(1, "%s", args[2]); if (res != SZ_OK) { free(new_data); free(buf_res); @@ -147,7 +146,7 @@ int main(int numArgs, const char *args[]) { char rs[800] = { 0 }; if (numArgs != 4) - errx(1,"ss_bspatch Version 1.0\nUsage: ss_bspatch oldfile newfile patchfile\n"); + errx(1, "ss_bspatch Version 1.0\nUsage: ss_bspatch oldfile newfile patchfile\n"); int res = main2(numArgs, args, rs, sizeof(rs)); fputs(rs, stdout); return res; diff --git a/bsdiff/ss_bspatch_common.c b/bsdiff/ss_bspatch_common.c index ffb5a7e..e7bd45e 100755 --- a/bsdiff/ss_bspatch_common.c +++ b/bsdiff/ss_bspatch_common.c @@ -181,7 +181,7 @@ int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char /* Check for appropriate magic */ if (memcmp(header, BSDIFF_HEADER, 8) != 0 && memcmp(header, SSDIFF_HEADER, 8) != 0) { - printf("%s().%d Patch buffer header corrupt\n", __FUNCTION__, __LINE__ ); + printf("%s().%d Patch buffer header corrupt\n", __FUNCTION__, __LINE__); return 1; } @@ -189,7 +189,7 @@ int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char newsize = offtin(header + 8); if ((newsize < 0)) { - printf("%s().%d Patch buffer corrupt\n", __FUNCTION__, __LINE__ ); + printf("%s().%d Patch buffer corrupt\n", __FUNCTION__, __LINE__); return 1; } @@ -238,15 +238,14 @@ int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char goto Cleanup; } if (newpos + match_size > newsize) { - printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__ ); + printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__); result = 1; goto Cleanup; } memcpy((*dest_buf) + newpos, patch_buffer + patch_buffer_offset, match_size); patch_buffer_offset += match_size; - for (j = 0; j < match_size; j++) { + for (j = 0; j < match_size; j++) (*dest_buf)[newpos + j] += old[j]; - } newpos += match_size; flag = false; } else { @@ -256,7 +255,7 @@ int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char goto Cleanup; } if (newpos + memory_usage > newsize) { - printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__ ); + printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__); result = 1; goto Cleanup; } @@ -273,7 +272,7 @@ int apply_patch(const char *oldfile, unsigned char *patch_buffer, unsigned char //////////////////////////////////////////////////////////////////////////////////////// /* Sanity-check */ if (newpos + ctrl[1] > newsize) { - printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__ ); + printf("%s().%d Corrupt patch\n", __FUNCTION__, __LINE__); result = 1; goto Cleanup; } diff --git a/ss_engine/SS_ApplyPatch.c b/ss_engine/SS_ApplyPatch.c index 9516085..2057ac3 100755 --- a/ss_engine/SS_ApplyPatch.c +++ b/ss_engine/SS_ApplyPatch.c @@ -46,73 +46,70 @@ int SS_ApplyBsdiff(char *oldfile, char *newfile, char *patch, SinkFn sink, void *token, sha1_ctx_t * ctx1) { - - UInt64 unpackSize = 0; - CFileSeqInStream inStream; - ISeqOutStream outStream; - unsigned char *buf_res = NULL; - unsigned char *new_data = NULL; - char buf[256]; - ssize_t new_size = 0; - int result = E_SS_FAILURE; - - FileSeqInStream_CreateVTable(&inStream); - File_Construct(&inStream.file); - FileOutStream_CreateVTable((CFileOutStream *) & outStream); - - if (InFile_Open(&inStream.file, patch) != 0) - return E_SS_FAILURE; - - UInt64 i; - CLzmaDec state; - unsigned char header[LZMA_PROPS_SIZE + 8]; - - RINOK(SeqInStream_Read(&inStream.s, header, sizeof(header))); - - unpackSize = 0; - for (i = 0; i < 8; i++) - unpackSize += (UInt64) header[LZMA_PROPS_SIZE + i] << (i * 8); - - LzmaDec_Construct(&state); - RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); - - //decompress the patch file into buf_res - buf_res = (unsigned char *)SS_Malloc(unpackSize); - if (!buf_res) { - LOGE("Bad memory allocation\n"); - goto Cleanup; - } - result = Decode2(&state, &outStream, &inStream.s, &unpackSize, buf_res); - - LzmaDec_Free(&state, &g_Alloc); - File_Close(&inStream.file); - - if (result != S_SS_SUCCESS) { - LOGE("Error decompression failed with code : [%d]\n", result); - goto Cleanup; - } - //apply patch using buffer decoded by Decode2 - result = apply_patch(oldfile, buf_res, &new_data, &new_size); - if (result != S_SS_SUCCESS) { - goto Cleanup; - } - - result = (sink(new_data, new_size, token) < new_size) ? E_SS_FAILURE : S_SS_SUCCESS; - if (result != S_SS_SUCCESS) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("short write of output: %d (%s)\n", errno, buf); - goto Cleanup; - } - - if (ctx1) { - sha1_update(ctx1, new_data, new_size); - } - Cleanup: - if (new_data) - SS_Free(new_data); - if (buf_res) - SS_Free(buf_res); - File_Close(&inStream.file);//wgid: 27007 - return result; + UInt64 unpackSize = 0; + CFileSeqInStream inStream; + ISeqOutStream outStream; + unsigned char *buf_res = NULL; + unsigned char *new_data = NULL; + char buf[256]; + ssize_t new_size = 0; + int result = E_SS_FAILURE; + + FileSeqInStream_CreateVTable(&inStream); + File_Construct(&inStream.file); + FileOutStream_CreateVTable((CFileOutStream *) & outStream); + + if (InFile_Open(&inStream.file, patch) != 0) + return E_SS_FAILURE; + + UInt64 i; + CLzmaDec state; + unsigned char header[LZMA_PROPS_SIZE + 8]; + + RINOK(SeqInStream_Read(&inStream.s, header, sizeof(header))); + + unpackSize = 0; + for (i = 0; i < 8; i++) + unpackSize += (UInt64) header[LZMA_PROPS_SIZE + i] << (i * 8); + + LzmaDec_Construct(&state); + RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); + + //decompress the patch file into buf_res + buf_res = (unsigned char *)SS_Malloc(unpackSize); + if (!buf_res) { + LOGE("Bad memory allocation\n"); + goto Cleanup; + } + result = Decode2(&state, &outStream, &inStream.s, &unpackSize, buf_res); + + LzmaDec_Free(&state, &g_Alloc); + File_Close(&inStream.file); + + if (result != S_SS_SUCCESS) { + LOGE("Error decompression failed with code : [%d]\n", result); + goto Cleanup; + } + //apply patch using buffer decoded by Decode2 + result = apply_patch(oldfile, buf_res, &new_data, &new_size); + if (result != S_SS_SUCCESS) + goto Cleanup; + + result = (sink(new_data, new_size, token) < new_size) ? E_SS_FAILURE : S_SS_SUCCESS; + if (result != S_SS_SUCCESS) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("short write of output: %d (%s)\n", errno, buf); + goto Cleanup; + } + + if (ctx1) + sha1_update(ctx1, new_data, new_size); +Cleanup: + if (new_data) + SS_Free(new_data); + if (buf_res) + SS_Free(buf_res); + File_Close(&inStream.file);//wgid: 27007 + return result; } diff --git a/ss_engine/SS_Common.c b/ss_engine/SS_Common.c index 6a38d22..6722938 100755 --- a/ss_engine/SS_Common.c +++ b/ss_engine/SS_Common.c @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -35,83 +35,82 @@ void SS_Progress(void *pbUserData, SS_UINT32 uPercent) { - LOG("Progress before\n"); - LOGL(LOG_SSENGINE, "Progress.. (%u %%)\n", uPercent); - LOG("Progress after:\n"); - ((ua_data_t *) pbUserData)->ui_progress(pbUserData, uPercent); + LOG("Progress before\n"); + LOGL(LOG_SSENGINE, "Progress.. (%u %%)\n", uPercent); + LOG("Progress after:\n"); + ((ua_data_t *) pbUserData)->ui_progress(pbUserData, uPercent); } /* Prints a string like the C printf() function */ SS_UINT32 SS_Trace(void *pUser, const char *aFormat, ...) { #if 0 - LOGL(LOG_SSENGINE, aFormat); + LOGL(LOG_SSENGINE, aFormat); #else - char temp[4096]; - va_list list; + char temp[4096]; + va_list list; - va_start(list, aFormat); - vsnprintf(temp, sizeof(temp), aFormat, list); - va_end(list); + va_start(list, aFormat); + vsnprintf(temp, sizeof(temp), aFormat, list); + va_end(list); - LOGL(LOG_SSENGINE, "%s", temp); + LOGL(LOG_SSENGINE, "%s", temp); #endif - return S_SS_SUCCESS; + return S_SS_SUCCESS; } long SS_FSTrace(void *pbUserData, const unsigned short *aFormat, ...) { - va_list list; + va_list list; - va_start(list, aFormat); - vprintf((const char *)aFormat, list); - va_end(list); + va_start(list, aFormat); + vprintf((const char *)aFormat, list); + va_end(list); - return S_SS_SUCCESS; + return S_SS_SUCCESS; } long SS_ResetTimerA(void) { - //LOG("%s \n", __func__); + //LOG("%s \n", __func__); - return S_SS_SUCCESS; + return S_SS_SUCCESS; } long SS_GetDelta(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddressOffset, SS_UINT32 dwSize) { - int ret = S_SS_SUCCESS; - int readCount = 0; - FILE *fp; - long itemOffset = 0; - - ua_data_t *ua_data = (ua_data_t *) pbUserData; - ua_part_info_t *ua_partition = ua_data->parti_info; - ua_update_data_t *ua_update_data = ua_data->update_data; - - LOGL(LOG_SSENGINE, "SS_GetDelta offset 0x%x(%u), size 0x%x(%u)\n", - dwStartAddressOffset, dwStartAddressOffset, dwSize, dwSize); - - itemOffset = tar_get_item_offset(ua_update_data->ua_delta_path, ua_partition->ua_subject_name); - if (itemOffset < 0) { - return E_SS_OPENFILE_ONLYR; - } - - fp = fopen(ua_update_data->ua_delta_path, "r"); - if (!fp) { - LOGL(LOG_SSENGINE, "open file %s failed.\n", ua_update_data->ua_delta_path); - return E_SS_OPENFILE_ONLYR; - } - - if (fseek(fp, itemOffset + dwStartAddressOffset, 0) == -1) - ret = E_SS_READ_ERROR; - else { - readCount = fread(pbBuffer, 1, dwSize, fp); - if (readCount != dwSize) { - LOGL(LOG_SSENGINE, "error in read size\n"); - ret = E_SS_READ_ERROR; - } - } - fclose(fp); - - return ret; + int ret = S_SS_SUCCESS; + int readCount = 0; + FILE *fp; + long itemOffset = 0; + + ua_data_t *ua_data = (ua_data_t *) pbUserData; + ua_part_info_t *ua_partition = ua_data->parti_info; + ua_update_data_t *ua_update_data = ua_data->update_data; + + LOGL(LOG_SSENGINE, "SS_GetDelta offset 0x%x(%u), size 0x%x(%u)\n", + dwStartAddressOffset, dwStartAddressOffset, dwSize, dwSize); + + itemOffset = tar_get_item_offset(ua_update_data->ua_delta_path, ua_partition->ua_subject_name); + if (itemOffset < 0) + return E_SS_OPENFILE_ONLYR; + + fp = fopen(ua_update_data->ua_delta_path, "r"); + if (!fp) { + LOGL(LOG_SSENGINE, "open file %s failed.\n", ua_update_data->ua_delta_path); + return E_SS_OPENFILE_ONLYR; + } + + if (fseek(fp, itemOffset + dwStartAddressOffset, 0) == -1) + ret = E_SS_READ_ERROR; + else { + readCount = fread(pbBuffer, 1, dwSize, fp); + if (readCount != dwSize) { + LOGL(LOG_SSENGINE, "error in read size\n"); + ret = E_SS_READ_ERROR; + } + } + fclose(fp); + + return ret; } diff --git a/ss_engine/SS_Common.h b/ss_engine/SS_Common.h index 5d92020..d7175ba 100755 --- a/ss_engine/SS_Common.h +++ b/ss_engine/SS_Common.h @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -28,20 +28,20 @@ #define MAX_PATH 256 -#define NAND_BLOCK_BITS 18 // 0x40000 -#define NAND_PAGE_BITS 12 // 0x1000 +#define NAND_BLOCK_BITS 18 // 0x40000 +#define NAND_PAGE_BITS 12 // 0x1000 #if defined(PART_W) || defined(PART_WB1) || defined(PART_W3G) || defined(PART_TINY) \ || defined(PART_HIGGS) || defined(PART_KIRAN) || defined(PART_KIRANLTE) || defined(PART_ORBIS) -#define UA_RAM_SIZE 64*1024*1024 // it sould be same 0x4000000 in xml. +#define UA_RAM_SIZE 64*1024*1024 // it sould be same 0x4000000 in xml. #else #define UA_RAM_SIZE 512*1024*1024 // it sould be same 0x20000000 in xml. #endif -#define FS_ID_MAX_LEN 4 -#define DP_HEADER_SIZE 36 -#define DP_START_OFFSET 0 -#define BACKUPBUFFER_NUM 4 +#define FS_ID_MAX_LEN 4 +#define DP_HEADER_SIZE 36 +#define DP_START_OFFSET 0 +#define BACKUPBUFFER_NUM 4 /*******[ Multiprocess API sample implementation ]******/ #define _NOEXEC_ @@ -50,15 +50,15 @@ #define HANDLE_RUN_PROC "handle_run_process" struct status_header_page { - unsigned char preamble[5]; // state·Î »ç¿ëÇÒ ¼ö ÀÖÀ½, FOTA, SBLB, ZIMG, PLRM, MDEM, DONE, FAILµî - unsigned long format; //ÃÖÁ¾ return °ª, ¾Æ·¡ °¢ image¿¡ ´ëÇÑ engineÀÇ return°ªÀ» DM error table·Î º¯°æµÈ °ª - unsigned long reserved; // Not used - unsigned long flexStatus; // Not used - unsigned long apfwStatus; //zImage update¿¡ ´ëÇÑ engineÀÇ return °ª - unsigned long apffsStatus; //platform update¿¡ ´ëÇÑengineÀÇreturn °ª - unsigned long apuaStatus; //sbl update¿¡ ´ëÇÑengineÀÇreturn °ª - unsigned long bpfwStatus; //modem update¿¡ ´ëÇÑengineÀÇreturn °ª - unsigned long bpuaStatus; + unsigned char preamble[5]; // state·Î »ç¿ëÇÒ ¼ö ÀÖÀ½, FOTA, SBLB, ZIMG, PLRM, MDEM, DONE, FAILµî + unsigned long format; //ÃÖÁ¾ return °ª, ¾Æ·¡ °¢ image¿¡ ´ëÇÑ engineÀÇ return°ªÀ» DM error table·Î º¯°æµÈ °ª + unsigned long reserved; // Not used + unsigned long flexStatus; // Not used + unsigned long apfwStatus; //zImage update¿¡ ´ëÇÑ engineÀÇ return °ª + unsigned long apffsStatus; //platform update¿¡ ´ëÇÑengineÀÇreturn °ª + unsigned long apuaStatus; //sbl update¿¡ ´ëÇÑengineÀÇreturn °ª + unsigned long bpfwStatus; //modem update¿¡ ´ëÇÑengineÀÇreturn °ª + unsigned long bpuaStatus; }; void SS_unicode_to_char(const char *src, char *dest, int size); diff --git a/ss_engine/SS_Engine_Errors.h b/ss_engine/SS_Engine_Errors.h index f116c14..c9e2daa 100755 --- a/ss_engine/SS_Engine_Errors.h +++ b/ss_engine/SS_Engine_Errors.h @@ -34,32 +34,32 @@ #ifndef __SS_Engine_ERRORS__ #define __SS_Engine_ERRORS__ -#define S_SS_SUCCESS (0) /*! Success Code */ -#define E_SS_FAILURE (1) /*! Failure Code */ +#define S_SS_SUCCESS (0) /*! Success Code */ +#define E_SS_FAILURE (1) /*! Failure Code */ /* INPUT Processing errors */ /* invocation errors */ -#define E_SS_BAD_PARAMS (0xA00) /**< error in a run parameter */ +#define E_SS_BAD_PARAMS (0xA00) /**< error in a run parameter */ #define E_SS_FSINVALIDNODEPARAMS (0xA01) /* Failed to Parse the Params to verify NODE */ -#define E_SS_FSBADNODES (0xA02) /* Failed to verify NODE for FS */ -#define E_SS_FSBADDELTA (0xA03) /* Delta File does NOT contain required information on FS */ -#define E_SS_FSBADATTRIBUTES (0xA04) /* Failed to parse attribute data */ -#define E_SS_FSFAILEDTOOPENPATCHINFO (0xA05) /*Failed to open patch list file having details of PATCH info */ -#define E_SS_FSFAILEDTOPARSEDELTACNT (0xA06) /* Failed to parse the PATCH count information */ -#define E_SS_FSFAILEDTOPARSEDELTAINFO (0xA07) /* Failed to parse the Delta patch information */ +#define E_SS_FSBADNODES (0xA02) /* Failed to verify NODE for FS */ +#define E_SS_FSBADDELTA (0xA03) /* Delta File does NOT contain required information on FS */ +#define E_SS_FSBADATTRIBUTES (0xA04) /* Failed to parse attribute data */ +#define E_SS_FSFAILEDTOOPENPATCHINFO (0xA05) /*Failed to open patch list file having details of PATCH info */ +#define E_SS_FSFAILEDTOPARSEDELTACNT (0xA06) /* Failed to parse the PATCH count information */ +#define E_SS_FSFAILEDTOPARSEDELTAINFO (0xA07) /* Failed to parse the Delta patch information */ /* update package errors */ -#define E_SS_PKG_TOO_LONG (0xA08) /**< expected length error */ -#define E_SS_PKG_CORRUPTED (0xA09) /**< structural error */ +#define E_SS_PKG_TOO_LONG (0xA08) /**< expected length error */ +#define E_SS_PKG_CORRUPTED (0xA09) /**< structural error */ #define E_SS_SOURCE_CORRUPTED (0xA10) /**< signature error */ /*Delta Errors*/ -#define E_SS_FSBADPATCH (0xB00) /*File Patch Does NOT match the signature */ +#define E_SS_FSBADPATCH (0xB00) /*File Patch Does NOT match the signature */ #define E_SS_FSSHA_MISMATCH (0xB01) /*Could NOT produce expected Target SHA for file */ -#define E_SS_IMGBADDELTA (0xB02) /* Failed to parse attribute data */ -#define E_SS_IMGBADPATCH (0xB03) /*Image Patch Does NOT match the signature */ +#define E_SS_IMGBADDELTA (0xB02) /* Failed to parse attribute data */ +#define E_SS_IMGBADPATCH (0xB03) /*Image Patch Does NOT match the signature */ #define E_SS_IMGSHA_MISMATCH (0xB04) /*Could NOT produce expected Target SHA for Image */ #define E_SS_SHAPRASE_FAILED (0xB05) /*Could NOT Parse SHA */ @@ -67,50 +67,50 @@ /* Resources errors */ #define E_SS_NOT_ENOUGH_RAM (0xC00) /**< given RAM is not enough */ -#define E_SS_BAD_RAM (0xC01) /**< does not behave as RAM */ -#define E_SS_MALLOC_ERROR (0xC02) /**< memory allocation failure */ +#define E_SS_BAD_RAM (0xC01) /**< does not behave as RAM */ +#define E_SS_MALLOC_ERROR (0xC02) /**< memory allocation failure */ /* Image update Error codes */ -#define E_SS_WRITE_ERROR (0xC03) /**< flash writing failure */ -#define E_SS_ERASE_ERROR (0xC04) /**< flash erasing failure */ -#define E_SS_READ_ERROR (0xC05) /**< flash reading failure */ +#define E_SS_WRITE_ERROR (0xC03) /**< flash writing failure */ +#define E_SS_ERASE_ERROR (0xC04) /**< flash erasing failure */ +#define E_SS_READ_ERROR (0xC05) /**< flash reading failure */ /*File System Error codes */ #define E_SS_OPENFILE_ONLYR (0xC06) /**< file does not exist */ -#define E_SS_OPENFILE_WRITE (0xC07) /**< RO or no access rights */ +#define E_SS_OPENFILE_WRITE (0xC07) /**< RO or no access rights */ #define E_SS_DELETEFILE_NOFILE (0xC08) /**< file does not exist */ -#define E_SS_DELETEFILE (0xC09) /**< no access rights */ -#define E_SS_RESIZEFILE (0xC10) /**< cannot resize file */ -#define E_SS_READFILE_SIZE (0xC11) /**< cannot read specified size*/ +#define E_SS_DELETEFILE (0xC09) /**< no access rights */ +#define E_SS_RESIZEFILE (0xC10) /**< cannot resize file */ +#define E_SS_READFILE_SIZE (0xC11) /**< cannot read specified size*/ #define E_SS_CLOSEFILE_ERROR (0xC12) /**< cannot close file handle */ -#define E_SS_FAILED_CREATING_SYMBOLIC_LINK (0xC13) /**< Failed creating symbolic link */ -#define E_SS_CANNOT_CREATE_DIRECTORY (0xC14) /**< Failed creating directory */ +#define E_SS_FAILED_CREATING_SYMBOLIC_LINK (0xC13) /**< Failed creating symbolic link */ +#define E_SS_CANNOT_CREATE_DIRECTORY (0xC14) /**< Failed creating directory */ #define E_SS_FSMEMORYERROR (0xC15) /* Failed to allocate Memory */ #define E_SS_FILENAMELENERROR (0xC16) /* Failed to serve filename length */ /*Engine errors */ -#define E_SS_NOT_ENOUGH_RAM_FOR_OPERATION2 (0xD00) /**< There is not enough RAM to run with operation=2 (Dry update) */ +#define E_SS_NOT_ENOUGH_RAM_FOR_OPERATION2 (0xD00) /**< There is not enough RAM to run with operation=2 (Dry update) */ #define E_SS_DELTA_FILE_TOO_LONG (0xD01) /**< Delta file too long - curropted */ #define E_SS_ERROR_IN_DELETES_SIG (0xD02) /**< Mismatch between deletes sig and delta deletes buffers signature */ #define E_SS_DELTA_IS_CORRUPT (0xD03) /**< Delta file is corrupt: signature mismatch between delta header signature and calculated signature */ -#define E_SS_SOURCE_FILE_SIG_MISMATCH (0xD04) /**< File signature does not match signature */ +#define E_SS_SOURCE_FILE_SIG_MISMATCH (0xD04) /**< File signature does not match signature */ #define E_SS_TARGET_SIG_MISMATCH (0xD05) /**< Signature for the target buffer does not match the one stored in the delta file */ -#define E_SS_INVALID_BACKUP (0xD06) /**< Too many dirty buffers */ +#define E_SS_INVALID_BACKUP (0xD06) /**< Too many dirty buffers */ #define E_SS_UPI_VERSION_MISMATCH (0xD07) /**< UPI version mismatch between UPI and delta */ -#define E_SS_PARTITION_NAME_NOT_FOUND (0xD08) /**< Partition name is different in delta and in UPI data */ -#define E_SS_NO_SPACE_LEFT (0xD09) /**< There is not enough flash to update or install the files */ -#define E_SS_INVALID_DP_HEADER (0xD10) /**< Deployment Package header is invalid */ -#define E_SS_INVALID_DP_WRONG_SIGNATURE (0xD11) /**< Deployment Package signature is invalid */ -#define E_SS_FSFAILEDTOBACKUPPATCHINFO (0xD12) /* Failed to create backup file to write Delta patch info data */ -#define E_SS_FSUPDATEFAILED (0xD13) /*FS Failed during UPGRADE */ -#define E_SS_FSSRCBACKUPFAILED (0xD14) /*Failed to backup FS */ -#define E_SS_FSSRCCURRUPTED (0xD15) /*Could NOT update FS as SRC seems to be corrupted */ +#define E_SS_PARTITION_NAME_NOT_FOUND (0xD08) /**< Partition name is different in delta and in UPI data */ +#define E_SS_NO_SPACE_LEFT (0xD09) /**< There is not enough flash to update or install the files */ +#define E_SS_INVALID_DP_HEADER (0xD10) /**< Deployment Package header is invalid */ +#define E_SS_INVALID_DP_WRONG_SIGNATURE (0xD11) /**< Deployment Package signature is invalid */ +#define E_SS_FSFAILEDTOBACKUPPATCHINFO (0xD12) /* Failed to create backup file to write Delta patch info data */ +#define E_SS_FSUPDATEFAILED (0xD13) /*FS Failed during UPGRADE */ +#define E_SS_FSSRCBACKUPFAILED (0xD14) /*Failed to backup FS */ +#define E_SS_FSSRCCURRUPTED (0xD15) /*Could NOT update FS as SRC seems to be corrupted */ #define E_SS_IMGUPDATEFAILED (0xD16) /*IMG Failed during UPGRADE */ -#define E_SS_IMGSRCBACKUPFAILED (0xD17) /*Failed to backup IMG */ -#define E_SS_IMGRECOVERYWRITEFAILED (0xD18) /*Failed to write patched Recovery IMG */ +#define E_SS_IMGSRCBACKUPFAILED (0xD17) /*Failed to backup IMG */ +#define E_SS_IMGRECOVERYWRITEFAILED (0xD18) /*Failed to write patched Recovery IMG */ #define E_SS_IMGSRCCURRUPTED (0xD19) /*Could NOT update IMG as SRC seems to be corrupted */ #define E_SS_IMGFLASHWRITEFAIL (0xD20) /*Failed to write Patched IMG data to flash */ -#define E_SS_PATCHFILE_DEL_ERROR (0xD21) /*Failed to Clear/Del Patched SRC file */ +#define E_SS_PATCHFILE_DEL_ERROR (0xD21) /*Failed to Clear/Del Patched SRC file */ #endif diff --git a/ss_engine/SS_Engine_Update.h b/ss_engine/SS_Engine_Update.h index 72d6f8e..2f2ccc8 100755 --- a/ss_engine/SS_Engine_Update.h +++ b/ss_engine/SS_Engine_Update.h @@ -29,26 +29,26 @@ * Partition type */ typedef enum { - PT_FOTA, //!< Image - PT_FS //!< File system + PT_FOTA, //!< Image + PT_FS //!< File system } PartitionType; /** * In-place update */ typedef enum { - UT_SELF_UPDATE = 0, //!< Don't update in place - UT_NO_SELF_UPDATE, //!< Update in place - UT_PRIVATE, //!< For internal usage + UT_SELF_UPDATE = 0, //!< Don't update in place + UT_NO_SELF_UPDATE, //!< Update in place + UT_PRIVATE, //!< For internal usage } UpdateType; typedef unsigned int SS_UINT32; typedef enum { - FT_REGULAR_FILE, - FT_SYMBOLIC_LINK, - FT_FOLDER, - FT_MISSING + FT_REGULAR_FILE, + FT_SYMBOLIC_LINK, + FT_FOLDER, + FT_MISSING } enumFileType; #ifdef __cplusplus @@ -58,53 +58,53 @@ extern "C" { /** * Partition data */ - typedef struct tagCustomerPartitionData { + typedef struct tagCustomerPartitionData { /** * Partition name. Maximum 256 characters. Must match exactly the name used in * the UPG. */ - const char *partition_name; + const char *partition_name; /** * Partition flash address. Address must be sector aligned. Relevant only for * R/O partitions of any type; for R/W FS updates, set to 0. */ - SS_UINT32 rom_start_address; + SS_UINT32 rom_start_address; /** * Mount point or drive letter containing the partition. Maximum size is 256 * characters. Relevent only for R/W FS updates; otherwise set to 0.

*/ - const char *mount_point; + const char *mount_point; /** * Source path (input) partition if the update will not be done in place. * Maximum 25 characters. For Image updates, set to 0. */ - const char *strSourcePath; + const char *strSourcePath; /** * Target path (output) partition if the update will not be done in place. * Maximum 25 characters. For Image updates, set to 0. */ - const char *strTargetPath; + const char *strTargetPath; /** * Internal use; leave null. */ - const void *priv; + const void *priv; /** * Partition type, a \ref PartitionType value. */ - PartitionType partition_type; + PartitionType partition_type; - } CustomerPartitionData; + } CustomerPartitionData; /** * Device data */ - typedef struct tag_Engine_DeviceData { + typedef struct tag_Engine_DeviceData { /** * UPI Mode. One of: * \li 0: scout and update. Verify that the update is applicable to the device @@ -125,22 +125,22 @@ extern "C" { * (post-installation). This mode applies only to FS updates. Does not verify * attributes or empty folders. */ - SS_UINT32 ui32Operation; + SS_UINT32 ui32Operation; /** * Pre-allocated RAM space. */ - unsigned char *pRam; + unsigned char *pRam; /** * Size of pRam in bytes. */ - SS_UINT32 ui32RamSize; + SS_UINT32 ui32RamSize; /** * Number of backup sectors listed in pBufferBlocks. */ - SS_UINT32 ui32NumberOfBuffers; + SS_UINT32 ui32NumberOfBuffers; /** * List of backup buffer sector addresses. Addresses must be @@ -148,18 +148,18 @@ extern "C" { * is used instead of pTempPath. Otherwise pBufferBlocks should be set to 0 * and pTempPath (see below) will be used to allocate the backup as a file. */ - SS_UINT32 *pBufferBlocks; + SS_UINT32 *pBufferBlocks; /** * Number of partitions listed in pFirstPartitionData. */ - SS_UINT32 ui32NumberOfPartitions; + SS_UINT32 ui32NumberOfPartitions; /** * List of partition data structures, a list of \ref CustomerPartitionData * values. */ - CustomerPartitionData *pFirstPartitionData; + CustomerPartitionData *pFirstPartitionData; /** * Path to temporary storage. If the update contains FS updates, pTempPath is @@ -169,13 +169,13 @@ extern "C" { * The maximum file size will not exceed the sector size x number of backup * sectors. */ - char *pTempPath; + char *pTempPath; /** * Whether or not there is a UPI self-update in the update, an * \ref UpdateType value. */ - UpdateType enmUpdateType; + UpdateType enmUpdateType; /** * List of customer-defined installer types. For an Image only update, use a @@ -183,12 +183,12 @@ extern "C" { * * For the list of installer types, see the SWM Center documentation. */ - SS_UINT32 *pComponentInstallerTypes; + SS_UINT32 *pComponentInstallerTypes; /** * Number of installer types in pComponentInstallerTypes. */ - SS_UINT32 ui32ComponentInstallerTypesNum; + SS_UINT32 ui32ComponentInstallerTypesNum; /** * Update flags, if any. Used by the SWM Center.

@@ -196,23 +196,23 @@ extern "C" { * For non SWM Center update, set to 0xFFFFFFFF * For the list of update flags, see the SWM Center documentation. */ - SS_UINT32 ui32ComponentUpdateFlags; + SS_UINT32 ui32ComponentUpdateFlags; /** * Update number within the DP. */ - SS_UINT32 ui32OrdinalToUpdate; + SS_UINT32 ui32OrdinalToUpdate; /** * Deprecated. */ - char *pDeltaPath; + char *pDeltaPath; /** * Additional data to pass to APIs, if any. Set to null if not used. */ - void *pbUserData; - } Engine_DeviceData; + void *pbUserData; + } Engine_DeviceData; /** ******************************************************************************* @@ -224,7 +224,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_CheckDPStructure(void *pbUserData); + long SS_CheckDPStructure(void *pbUserData); /** ******************************************************************************* @@ -245,8 +245,8 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetNumberOfDeltas(void *pbUserData, SS_UINT32 * num_deltas, SS_UINT32 * installer_types, - SS_UINT32 installer_types_num, SS_UINT32 component_flags); + long SS_GetNumberOfDeltas(void *pbUserData, SS_UINT32 * num_deltas, SS_UINT32 * installer_types, + SS_UINT32 installer_types_num, SS_UINT32 component_flags); /** ******************************************************************************* @@ -269,8 +269,8 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetSignedDeltaOffset(void *pbUserData, SS_UINT32 delta_ordinal, SS_UINT32 * offset, SS_UINT32 * size, - SS_UINT32 * installer_types, SS_UINT32 installer_types_num, SS_UINT32 component_flags); + long SS_GetSignedDeltaOffset(void *pbUserData, SS_UINT32 delta_ordinal, SS_UINT32 * offset, SS_UINT32 * size, + SS_UINT32 * installer_types, SS_UINT32 installer_types_num, SS_UINT32 component_flags); /** ******************************************************************************* @@ -293,9 +293,9 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetUnsignedDeltaOffset(void *pbUserData, SS_UINT32 delta_ordinal, SS_UINT32 * offset, SS_UINT32 * size, - SS_UINT32 * installer_types, SS_UINT32 installer_types_num, - SS_UINT32 component_flags); + long SS_GetUnsignedDeltaOffset(void *pbUserData, SS_UINT32 delta_ordinal, SS_UINT32 * offset, SS_UINT32 * size, + SS_UINT32 * installer_types, SS_UINT32 installer_types_num, + SS_UINT32 component_flags); /** ******************************************************************************* @@ -309,7 +309,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetRBDeltaOffset(void *pbUserData, SS_UINT32 signed_delta_offset, SS_UINT32 * delta_offset); + long SS_GetRBDeltaOffset(void *pbUserData, SS_UINT32 signed_delta_offset, SS_UINT32 * delta_offset); /** ******************************************************************************* @@ -327,7 +327,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_Engine_Update(Engine_DeviceData * pDeviceData); + long SS_Engine_Update(Engine_DeviceData * pDeviceData); /** ******************************************************************************* @@ -342,7 +342,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_Engine_GetDpRamUse(SS_UINT32 * ui32pRamUse, Engine_DeviceData * pDeviceData); + long SS_Engine_GetDpRamUse(SS_UINT32 * ui32pRamUse, Engine_DeviceData * pDeviceData); /** ******************************************************************************* @@ -369,9 +369,9 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetDPProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize, - SS_UINT32 * installer_types, SS_UINT32 installer_types_num, SS_UINT32 component_flags, - SS_UINT32 * dpProtocolVersion); + long SS_GetDPProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize, + SS_UINT32 * installer_types, SS_UINT32 installer_types_num, SS_UINT32 component_flags, + SS_UINT32 * dpProtocolVersion); /** ******************************************************************************* @@ -399,9 +399,9 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetDPScoutProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize, - SS_UINT32 * installer_types, SS_UINT32 installer_types_num, - SS_UINT32 component_flags, SS_UINT32 * dpScoutProtocolVersion); + long SS_GetDPScoutProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize, + SS_UINT32 * installer_types, SS_UINT32 installer_types_num, + SS_UINT32 component_flags, SS_UINT32 * dpScoutProtocolVersion); /** ******************************************************************************* @@ -414,7 +414,7 @@ extern "C" { * \return S_SS_SUCCESS ******************************************************************************* */ - long SS_GetUPIVersion(unsigned char *pbVersion); + long SS_GetUPIVersion(unsigned char *pbVersion); /** ******************************************************************************* @@ -428,7 +428,7 @@ extern "C" { * this returns 50140. ******************************************************************************* */ - SS_UINT32 SS_GetUPIProtocolVersion(void); + SS_UINT32 SS_GetUPIProtocolVersion(void); /** ******************************************************************************* @@ -442,7 +442,7 @@ extern "C" { * 5.0.14.33, this returns 50140. ******************************************************************************* */ - SS_UINT32 SS_GetUPIScoutProtocolVersion(void); + SS_UINT32 SS_GetUPIScoutProtocolVersion(void); /** ******************************************************************************* @@ -457,7 +457,7 @@ extern "C" { ******************************************************************************* */ - SS_UINT32 SS_GetDeltaProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize); + SS_UINT32 SS_GetDeltaProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize); /** ******************************************************************************* * Get scout protocol version of an update.

@@ -471,7 +471,7 @@ extern "C" { ******************************************************************************* */ - SS_UINT32 SS_GetDeltaScoutProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize); /* User data passed to all porting routines, pointer for the ram to use, size of the ram */ + SS_UINT32 SS_GetDeltaScoutProtocolVersion(void *pbUserData, void *pbyRAM, SS_UINT32 dwRAMSize); /* User data passed to all porting routines, pointer for the ram to use, size of the ram */ /** ******************************************************************************* @@ -485,7 +485,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_ResetTimerA(void); + long SS_ResetTimerA(void); /** ******************************************************************************* @@ -502,7 +502,7 @@ extern "C" { * \return None ******************************************************************************* */ - void SS_Progress(void *pbUserData, SS_UINT32 uPercent); + void SS_Progress(void *pbUserData, SS_UINT32 uPercent); /** ******************************************************************************* @@ -525,7 +525,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ...); + SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ...); /** ******************************************************************************* @@ -543,7 +543,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetDelta(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddressOffset, SS_UINT32 dwSize); + long SS_GetDelta(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddressOffset, SS_UINT32 dwSize); #ifdef __cplusplus } diff --git a/ss_engine/SS_FSUpdate.c b/ss_engine/SS_FSUpdate.c index f0dadd1..187d209 100755 --- a/ss_engine/SS_FSUpdate.c +++ b/ss_engine/SS_FSUpdate.c @@ -45,47 +45,45 @@ static int feature_support_capability; ************************************************************/ void SS_create_dir(char *pathname, int mode) { - char *p; - int r; - - /* Strip trailing '/' */ - if (pathname[strlen(pathname) - 1] == '/') - pathname[strlen(pathname) - 1] = '\0'; - - /* Try creating the directory. */ - r = mkdir(pathname, mode); - - if (r != 0) { - /* On failure, try creating parent directory. */ - p = strrchr(pathname, '/'); - if (p != NULL) { - *p = '\0'; - SS_create_dir(pathname, 0755); - *p = '/'; - r = mkdir(pathname, mode); - } - } - if (r != 0) { - if (r != EEXIST && r != -1) - LOG("Could not create directory [%s] Error[%d]\n", pathname, r); - } + char *p; + int r; + + /* Strip trailing '/' */ + if (pathname[strlen(pathname) - 1] == '/') + pathname[strlen(pathname) - 1] = '\0'; + + /* Try creating the directory. */ + r = mkdir(pathname, mode); + + if (r != 0) { + /* On failure, try creating parent directory. */ + p = strrchr(pathname, '/'); + if (p != NULL) { + *p = '\0'; + SS_create_dir(pathname, 0755); + *p = '/'; + r = mkdir(pathname, mode); + } + } + if (r != 0) { + if (r != EEXIST && r != -1) + LOG("Could not create directory [%s] Error[%d]\n", pathname, r); + } } void SS_unicode_to_char(const char *src, char *dest, int size) { - if (src == NULL) { + if (src == NULL) return; - } strncpy(dest, src, size); } void SS_char_to_unicode(const char *src, char *dest, int size) { - if (src == NULL) { + if (src == NULL) return; - } strncpy(dest, src, size); } @@ -156,7 +154,7 @@ SS_CopyFile(void *pbUserData, writeCount = write(fd2, buf, readCount); if (writeCount != readCount) { LOGE(" read %d, but write %d, abort.\n", readCount, - writeCount); + writeCount); ret = E_SS_WRITE_ERROR; break; } @@ -191,9 +189,9 @@ int SS_unlink_cbf(const char *fpath, const struct stat *sb, int typeflag, struct { int rv = remove(fpath);//returns zero on success and -1 on failure - if (rv){ + if (rv) { perror(fpath); - LOGE("path : %s, remove value: %d, errno: %d\n",fpath, rv, errno); + LOGE("path : %s, remove value: %d, errno: %d\n", fpath, rv, errno); } return rv; } @@ -230,14 +228,14 @@ long SS_CreateFolder(void *pbUserData, const char *strPath) SS_unicode_to_char((const char *)strPath, (char *)path, MAX_PATH - 1); mode = S_IRUSR /*Read by owner */ | - S_IWUSR /*Write by owner */ | - S_IXUSR /*Execute by owner */ | - S_IRGRP /*Read by group */ | - S_IWGRP /*Write by group */ | - S_IXGRP /*Execute by group */ | - S_IROTH /*Read by others */ | - S_IWOTH /*Write by others */ | - S_IXOTH /*Execute by others */ ; + S_IWUSR /*Write by owner */ | + S_IXUSR /*Execute by owner */ | + S_IRGRP /*Read by group */ | + S_IWGRP /*Write by group */ | + S_IXGRP /*Execute by group */ | + S_IROTH /*Read by others */ | + S_IWOTH /*Write by others */ | + S_IXOTH /*Execute by others */ ; LOGL(LOG_SSENGINE, "%s, mode:0x%x\n", path, mode); @@ -245,8 +243,7 @@ long SS_CreateFolder(void *pbUserData, const char *strPath) if (ret == 0 || ((ret == -1) && (errno == EEXIST))) { return S_SS_SUCCESS; - } else if ((ret == -1) && (errno == ENOENT)) //maybe multi directory problem - { + } else if ((ret == -1) && (errno == ENOENT)) { //maybe multi directory problem //do//Recursive Function //{ ret = SS_recursive_folder_creater(path, mode); @@ -297,7 +294,7 @@ SS_OpenFile(void *pbUserData, mode = SS_get_mode(wFlag); //LOGL(LOG_SSENGINE, "Path:%s wFlag:%d Mode:%d\n", path, wFlag, mode); - if(mode & O_CREAT) { + if (mode & O_CREAT) { //LOGL(LOG_SSENGINE, " open() S_IRWXU\n"); *pwHandle = open(path, mode, S_IRWXU); } else { @@ -391,14 +388,14 @@ long SS_CloseFile(void *pbUserData, long wHandle) long SS_WriteFile(void *pbUserData, - long wHandle, - SS_UINT32 dwPosition, - unsigned char *pbBuffer, SS_UINT32 dwSize) + long wHandle, + SS_UINT32 dwPosition, + unsigned char *pbBuffer, SS_UINT32 dwSize) { int ret = 0; LOGL(LOG_SSENGINE, "Handle:%ld , Pos:%u , Size: %u\n", wHandle, - dwPosition, dwSize); + dwPosition, dwSize); ret = lseek(wHandle, dwPosition, SEEK_SET); if (ret < 0) { @@ -427,9 +424,8 @@ long SS_MoveFile(void *pbUserData, const char *strFromPath, char path1[MAX_PATH] = { '\0' }; char path2[MAX_PATH] = { '\0' }; - if (!strFromPath || !strToPath) { + if (!strFromPath || !strToPath) return -1; //should never happen - } SS_unicode_to_char(strFromPath, (char *)path1, MAX_PATH - 1); SS_unicode_to_char(strToPath, (char *)path2, MAX_PATH - 1); @@ -444,17 +440,16 @@ long SS_MoveFile(void *pbUserData, const char *strFromPath, if (ret < 0) { LOGL(LOG_INFO, "rename fail with code [%d], try to create dir if errno is 2\n", errno); if (errno == 2) { - char * file_name = strrchr(path2,'/'); + char * file_name = strrchr(path2, '/'); *file_name = '\0'; - SS_create_dir(path2,0755); + SS_create_dir(path2, 0755); *file_name = '/'; ret = rename(path1, path2); if (ret < 0) { - LOGE("Move failed, error code [%d]",errno); + LOGE("Move failed, error code [%d]", errno); return E_SS_WRITE_ERROR; - } - } - else if (errno == 18){ //EXDEV 18 /* Cross-device link */ + } + } else if (errno == 18) { //EXDEV 18 /* Cross-device link */ //Moving file across partitions if mount point is different (Extremely rare) ret = (int)SS_CopyFile(NULL, path1, path2); if (ret != S_SS_SUCCESS) { @@ -466,9 +461,8 @@ long SS_MoveFile(void *pbUserData, const char *strFromPath, LOGE("failed to unlink [%s] code [%d]\n", path1, errno); return E_SS_WRITE_ERROR; } - } - else{ - LOGE("Move failed, error code [%d]",errno); + } else { + LOGE("Move failed, error code [%d]", errno); return E_SS_WRITE_ERROR; } } @@ -595,7 +589,7 @@ SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName) ret = symlink(refpath, sympath); if (ret != 0) { - LOGE(" symlink failed with return value: %d, errno: %d\n", ret,errno); + LOGE(" symlink failed with return value: %d, errno: %d\n", ret, errno); if (errno == EEXIST) { ret = lstat(sympath, &sbuf); @@ -603,21 +597,21 @@ SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName) if (ret >= 0) { if (S_ISREG(sbuf.st_mode)) { LOGL(LOG_SSENGINE, " stat->st_mode = regular file, To be deleted and create a LINK \n"); - SS_DeleteFile(pbUserData,sympath); + SS_DeleteFile(pbUserData, sympath); SS_Link(pbUserData, pLinkName, pReferenceFileName); } } if (SS_VerifyLinkReference(pbUserData, pLinkName , pReferenceFileName) == S_SS_SUCCESS) { return S_SS_SUCCESS; - } - else + } else { return E_SS_FAILURE; - } - else if(errno == ENOENT )//to handle cases where new symlink points to a new symlink yet to be created + } + } else if (errno == ENOENT) {//to handle cases where new symlink points to a new symlink yet to be created return errno; - else + } else { return E_SS_FAILURE; + } } //LOGL(LOG_SSENGINE, "symlink with return value: %d\n", ret); @@ -829,12 +823,10 @@ long SS_SetFileAttributes(const char *ui16pFilePath, LOGL(LOG_SSENGINE, " stat->st_mode = symbolic link file\n"); // return S_RB_SUCCESS; // sybolic link should be set mode. } - if (S_ISREG(sbuf.st_mode)) { + if (S_ISREG(sbuf.st_mode)) LOGL(LOG_SSENGINE, " stat->st_mode = regular file\n"); - } - if (S_ISDIR(sbuf.st_mode)) { + if (S_ISDIR(sbuf.st_mode)) LOGL(LOG_SSENGINE, " stat->st_mode = directory\n"); - } } LOGL(LOG_SSENGINE, "ui16pFilePath = %s\n", setFilePath); @@ -875,7 +867,7 @@ long SS_SetFileAttributes(const char *ui16pFilePath, char *cap_mark = "capability=0x"; int cap_mark_len = strlen(cap_mark); psmack = strstr(smack_attr_pos, cap_mark); - if(psmack) { + if (psmack) { int cap_hex_len; int i; char ch1, ch2; @@ -891,16 +883,16 @@ long SS_SetFileAttributes(const char *ui16pFilePath, cap_hex_len = strlen(tmpSmackAttribs); cap_len = cap_hex_len/2; memset(cap_raw, 0x00, sizeof(cap_raw)); - for (i=0; i= '0')&&(ch1 <= '9')) raw1 = ch1 - '0'; - else if ((ch1 >= 'a')&&(ch1 <= 'f')) raw1 = ch1 - 'a' + 10; - else if ((ch1 >= 'A')&&(ch1 <= 'F')) raw1 = ch1 - 'A' + 10; + if ((ch1 >= '0') && (ch1 <= '9')) raw1 = ch1 - '0'; + else if ((ch1 >= 'a') && (ch1 <= 'f')) raw1 = ch1 - 'a' + 10; + else if ((ch1 >= 'A') && (ch1 <= 'F')) raw1 = ch1 - 'A' + 10; else raw1 = 0; - if ((ch2 >= '0')&&(ch2 <= '9')) raw2 = ch2 - '0'; - else if ((ch2 >= 'a')&&(ch2 <= 'f')) raw2 = ch2 - 'a' + 10; - else if ((ch2 >= 'A')&&(ch2 <= 'F')) raw2 = ch2 - 'A' + 10; + if ((ch2 >= '0') && (ch2 <= '9')) raw2 = ch2 - '0'; + else if ((ch2 >= 'a') && (ch2 <= 'f')) raw2 = ch2 - 'a' + 10; + else if ((ch2 >= 'A') && (ch2 <= 'F')) raw2 = ch2 - 'A' + 10; else raw2 = 0; cap_raw[i] = raw1*16 + raw2; @@ -916,7 +908,7 @@ long SS_SetFileAttributes(const char *ui16pFilePath, char *acl_mark = "acl_access=0x"; int acl_mark_len = strlen(acl_mark); psmack = strstr(smack_attr_pos, acl_mark); - if(psmack) { + if (psmack) { int acl_hex_len; int i; char ch1, ch2; @@ -932,16 +924,16 @@ long SS_SetFileAttributes(const char *ui16pFilePath, acl_hex_len = strlen(tmpSmackAttribs); acl_len = acl_hex_len/2; memset(acl_raw, 0x00, sizeof(acl_raw)); - for (i=0; i= '0')&&(ch1 <= '9')) raw1 = ch1 - '0'; - else if ((ch1 >= 'a')&&(ch1 <= 'f')) raw1 = ch1 - 'a' + 10; - else if ((ch1 >= 'A')&&(ch1 <= 'F')) raw1 = ch1 - 'A' + 10; + if ((ch1 >= '0') && (ch1 <= '9')) raw1 = ch1 - '0'; + else if ((ch1 >= 'a') && (ch1 <= 'f')) raw1 = ch1 - 'a' + 10; + else if ((ch1 >= 'A') && (ch1 <= 'F')) raw1 = ch1 - 'A' + 10; else raw1 = 0; - if ((ch2 >= '0')&&(ch2 <= '9')) raw2 = ch2 - '0'; - else if ((ch2 >= 'a')&&(ch2 <= 'f')) raw2 = ch2 - 'a' + 10; - else if ((ch2 >= 'A')&&(ch2 <= 'F')) raw2 = ch2 - 'A' + 10; + if ((ch2 >= '0') && (ch2 <= '9')) raw2 = ch2 - '0'; + else if ((ch2 >= 'a') && (ch2 <= 'f')) raw2 = ch2 - 'a' + 10; + else if ((ch2 >= 'A') && (ch2 <= 'F')) raw2 = ch2 - 'A' + 10; else raw2 = 0; acl_raw[i] = raw1*16 + raw2; @@ -961,14 +953,14 @@ long SS_SetFileAttributes(const char *ui16pFilePath, smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_TRANSMUTE); psmack = strstr(smack_attr_pos, "access=\""); - if(psmack) { + if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); memcpy(tmpSmackAttribs, psmack, strlen(psmack)); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_ACCESS] smack_value=%s\n", smack_value); - if(smack_value) { + if (smack_value) { ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_ACCESS); if (ret < 0) LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); @@ -976,14 +968,14 @@ long SS_SetFileAttributes(const char *ui16pFilePath, } } psmack = strstr(smack_attr_pos, "execute=\""); - if(psmack) { + if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); memcpy(tmpSmackAttribs, psmack, strlen(psmack)); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_EXEC] smack_value=%s\n", smack_value); - if(smack_value) { + if (smack_value) { ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_EXEC); if (ret < 0) LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); @@ -991,14 +983,14 @@ long SS_SetFileAttributes(const char *ui16pFilePath, } } psmack = strstr(smack_attr_pos, "mmap=\""); - if(psmack) { + if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); memcpy(tmpSmackAttribs, psmack, strlen(psmack)); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_MMAP] smack_value=%s\n", smack_value); - if(smack_value) { + if (smack_value) { ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_MMAP); if (ret < 0) LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); @@ -1006,19 +998,18 @@ long SS_SetFileAttributes(const char *ui16pFilePath, } } psmack = strstr(smack_attr_pos, "transmute=\""); - if(psmack) { + if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); memcpy(tmpSmackAttribs, psmack, strlen(psmack)); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_TRANSMUTE] smack_value=%s\n", smack_value); - if(smack_value) { - if (strcasecmp(smack_value, "TRUE")==0) { + if (smack_value) { + if (strcasecmp(smack_value, "TRUE") == 0) ret = smack_lsetlabel(setFilePath, "1", SMACK_LABEL_TRANSMUTE); - } else { + else ret = smack_lsetlabel(setFilePath, "0", SMACK_LABEL_TRANSMUTE); - } if (ret < 0) LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); } @@ -1105,20 +1096,18 @@ SS_GetAvailableFreeSpace(void *pbUserData, const char *partition_name, //LOGL(LOG_SSENGINE, "path=%s\n", path); result = statfs(path, &vfs); - if (result < 0 ) - { + if (result < 0) { LOGE("failed to fstatfs, err : %d errno: %d\n", result, errno); return -1; } *available_flash_size = vfs.f_bsize * vfs.f_bavail; - if(*available_flash_size == 0) - { + if (*available_flash_size == 0) { *available_flash_size = 0x80000; //Same as Legecy RB - LOGE("available_flash_size=%u(vfs.f_bsize=%d vfs.f_bavail=%d\n", - (unsigned int)*available_flash_size, (int)vfs.f_bsize, (int)vfs.f_bavail); - return 0; // Same as Legecy RB - } + LOGE("available_flash_size=%u(vfs.f_bsize=%d vfs.f_bavail=%d\n", + (unsigned int)*available_flash_size, (int)vfs.f_bsize, (int)vfs.f_bavail); + return 0; // Same as Legecy RB + } return 0; } @@ -1135,9 +1124,9 @@ void *SS_Malloc(SS_UINT32 size) memset(p, 0, size); #ifdef HEAP_PROFILING cur_mem += size; - if(cur_mem > max_mem ){ + if (cur_mem > max_mem) { max_mem = cur_mem; - LOGL(LOG_SSENGINE,"New chunk [%d] assigned making heap [%d]\n",size,cur_mem); + LOGL(LOG_SSENGINE, "New chunk [%d] assigned making heap [%d]\n", size, cur_mem); } #endif @@ -1148,9 +1137,9 @@ void SS_Free(void *pMemBlock) { #ifdef HEAP_PROFILING cur_mem -= malloc_usable_size(pMemBlock); - LOGL(LOG_SSENGINE,"Old chunk [%d] removed making heap [%d]\n",malloc_usable_size(pMemBlock),cur_mem); + LOGL(LOG_SSENGINE, "Old chunk [%d] removed making heap [%d]\n", malloc_usable_size(pMemBlock), cur_mem); #endif - if(pMemBlock) + if (pMemBlock) free(pMemBlock); } @@ -1178,13 +1167,12 @@ void* SS_WaitForProcess(const void *handle, SS_UINT32* process_exit_code) if (pid < 0) return NULL; - if (!WIFEXITED(*process_exit_code)) - { + if (!WIFEXITED(*process_exit_code)) { *process_exit_code = (char)WTERMSIG(*process_exit_code); LOG("Wait Error\n"); - } - else + } else { *process_exit_code = (char)WEXITSTATUS(*process_exit_code); + } return (void*)pid; } @@ -1209,8 +1197,7 @@ void* SS_RunProcess(void *pbUserData, int argc, char* argv[]) } // This is the child - if (child_pid == 0) - { + if (child_pid == 0) { #ifdef _NOEXEC_ #ifdef _TIZEN_SSENGINE//bota SS_HandleProcessRequest(pbUserData, argc, argv); @@ -1223,8 +1210,7 @@ void* SS_RunProcess(void *pbUserData, int argc, char* argv[]) int i; params = (char **)SS_Malloc((argc+EXTRA_ARGS) *sizeof(char*)); - if (!params) - { + if (!params) { LOG("params allocation failed\n"); return NULL; } @@ -1233,16 +1219,14 @@ void* SS_RunProcess(void *pbUserData, int argc, char* argv[]) params[1] = strdup("handle_run_process"); params[2] = strdup(((sample_userdata*)user)->delta_path); - for (i=0; i < argc; i++) + for (i = 0; i < argc; i++) params[i+EXTRA_ARGS] = strdup(argv[i]); // no need to free allocated memory - execv takes care of it execv(params[0], (char**)params); _exit(-1); // if we're here, execv has failed #endif - } - else - { + } else { return (void *)child_pid; } } diff --git a/ss_engine/SS_FSUpdate.h b/ss_engine/SS_FSUpdate.h index 3bc4e88..49eda03 100755 --- a/ss_engine/SS_FSUpdate.h +++ b/ss_engine/SS_FSUpdate.h @@ -36,11 +36,11 @@ extern "C" { /*! * File access modes */ - typedef enum tag_RW_TYPE { - ONLY_R, //!< Read-only - ONLY_W, //!< Write-only - BOTH_RW //!< Read-write - } E_RW_TYPE; + typedef enum tag_RW_TYPE { + ONLY_R, //!< Read-only + ONLY_W, //!< Write-only + BOTH_RW //!< Read-write + } E_RW_TYPE; /*! ******************************************************************************* @@ -59,7 +59,7 @@ extern "C" { ******************************************************************************* */ - long SS_CopyFile(void *pbUserData, const char *strFromPath, const char *strToPath); + long SS_CopyFile(void *pbUserData, const char *strFromPath, const char *strToPath); /*! ******************************************************************************* @@ -74,7 +74,7 @@ extern "C" { ******************************************************************************* */ - long SS_MoveFile(void *pbUserData, const char *strFromPath, const char *strToPath); + long SS_MoveFile(void *pbUserData, const char *strFromPath, const char *strToPath); /*! ******************************************************************************* @@ -91,7 +91,7 @@ extern "C" { ******************************************************************************* */ - long SS_DeleteFile(void *pbUserData, const char *strPath); + long SS_DeleteFile(void *pbUserData, const char *strPath); /*! ******************************************************************************* @@ -107,7 +107,7 @@ extern "C" { ******************************************************************************* */ - long SS_DeleteFolder(void *pbUserData, const char *strPath); + long SS_DeleteFolder(void *pbUserData, const char *strPath); /*! ******************************************************************************* @@ -125,7 +125,7 @@ extern "C" { ******************************************************************************* */ - long SS_CreateFolder(void *pbUserData, const char *strPath); + long SS_CreateFolder(void *pbUserData, const char *strPath); /*! ******************************************************************************* @@ -146,7 +146,7 @@ extern "C" { ******************************************************************************* */ - long SS_OpenFile(void *pbUserData, const char *strPath, E_RW_TYPE wFlag, long *pwHandle); + long SS_OpenFile(void *pbUserData, const char *strPath, E_RW_TYPE wFlag, long *pwHandle); /*! ******************************************************************************* @@ -161,7 +161,7 @@ extern "C" { ******************************************************************************* */ - long SS_ResizeFile(void *pbUserData, long wHandle, SS_UINT32 dwSize); + long SS_ResizeFile(void *pbUserData, long wHandle, SS_UINT32 dwSize); /*! ******************************************************************************* @@ -175,7 +175,7 @@ extern "C" { ******************************************************************************* */ - long SS_CloseFile(void *pbUserData, long wHandle); + long SS_CloseFile(void *pbUserData, long wHandle); /*! ******************************************************************************* @@ -195,7 +195,7 @@ extern "C" { ******************************************************************************* */ - long SS_WriteFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); + long SS_WriteFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); /*! ******************************************************************************* @@ -210,7 +210,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_SyncFile(void *pbUserData, long wHandle); + long SS_SyncFile(void *pbUserData, long wHandle); /*! ******************************************************************************* @@ -229,7 +229,7 @@ extern "C" { ******************************************************************************* */ - long SS_ReadFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); + long SS_ReadFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); /*! ******************************************************************************* @@ -242,7 +242,7 @@ extern "C" { * \return File size, -1 if file not found, or < -1 on error ******************************************************************************* */ - long SS_GetFileSize(void *pbUserData, long wHandle); + long SS_GetFileSize(void *pbUserData, long wHandle); /*! ******************************************************************************* @@ -257,7 +257,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetAvailableFreeSpace(void *pbUserData, const char *path, SS_UINT32 * available_flash_size); + long SS_GetAvailableFreeSpace(void *pbUserData, const char *path, SS_UINT32 * available_flash_size); /*! ******************************************************************************* @@ -275,7 +275,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_Unlink(void *pbUserData, char *pLinkName); + long SS_Unlink(void *pbUserData, char *pLinkName); /*! ******************************************************************************* @@ -297,7 +297,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName); + long SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName); /*! ******************************************************************************* @@ -320,8 +320,8 @@ extern "C" { ******************************************************************************* */ - long SS_SetFileAttributes(const char *ui16pFilePath, - const SS_UINT32 ui32AttribSize, const unsigned char *ui8pAttribs); + long SS_SetFileAttributes(const char *ui16pFilePath, + const SS_UINT32 ui32AttribSize, const unsigned char *ui8pAttribs); /*! ******************************************************************************* @@ -340,8 +340,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ... - ); + SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ...); /** ******************************************************************************* @@ -352,16 +351,16 @@ extern "C" { * \return A pointer to the memory block on success, NULL on failure ******************************************************************************* */ - void *SS_Malloc(SS_UINT32 size); + void *SS_Malloc(SS_UINT32 size); /** ******************************************************************************* * Free a memory block. * - * \param pMemBlock Pointer to the memory block + * \param pMemBlock Pointer to the memory block ******************************************************************************* */ - void SS_Free(void *pMemBlock); + void SS_Free(void *pMemBlock); /** ******************************************************************************* @@ -370,7 +369,7 @@ extern "C" { * \return The value of feature_support_capability ******************************************************************************* */ - int SS_get_feature_support_capability(void); + int SS_get_feature_support_capability(void); /** ******************************************************************************* @@ -379,7 +378,7 @@ extern "C" { * \param val The value to set feature_support_capability ******************************************************************************* */ - void SS_set_feature_support_capability(int val); + void SS_set_feature_support_capability(int val); #ifdef __cplusplus } diff --git a/ss_engine/SS_ImageUpdate.h b/ss_engine/SS_ImageUpdate.h index 2923b71..2b9b773 100755 --- a/ss_engine/SS_ImageUpdate.h +++ b/ss_engine/SS_ImageUpdate.h @@ -45,7 +45,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_ReadBackupBlock(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwBlockAddress, SS_UINT32 dwSize); + long SS_ReadBackupBlock(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwBlockAddress, SS_UINT32 dwSize); /** ******************************************************************************* @@ -65,7 +65,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_EraseBackupBlock(void *pbUserData, SS_UINT32 dwStartAddress); + long SS_EraseBackupBlock(void *pbUserData, SS_UINT32 dwStartAddress); /** ******************************************************************************* @@ -82,7 +82,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_WriteBackupBlock(void *pbUserData, SS_UINT32 dwBlockStartAddress, unsigned char *pbBuffer); + long SS_WriteBackupBlock(void *pbUserData, SS_UINT32 dwBlockStartAddress, unsigned char *pbBuffer); /** ******************************************************************************* @@ -106,8 +106,8 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_WriteBackupPartOfBlock(void *pbUserData, - SS_UINT32 dwStartAddress, SS_UINT32 dwSize, unsigned char *pbBuffer); + long SS_WriteBackupPartOfBlock(void *pbUserData, + SS_UINT32 dwStartAddress, SS_UINT32 dwSize, unsigned char *pbBuffer); /** ******************************************************************************* @@ -124,7 +124,7 @@ extern "C" { * or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_WriteBlock(void *pbUserData, SS_UINT32 dwBlockAddress, unsigned char *pbBuffer); + long SS_WriteBlock(void *pbUserData, SS_UINT32 dwBlockAddress, unsigned char *pbBuffer); /** ******************************************************************************* @@ -142,9 +142,9 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_ReadImage(void *pbUserData, unsigned char *pbBuffer, /* pointer to user buffer */ - SS_UINT32 dwStartAddress, /* memory address to read from */ - SS_UINT32 dwSize); /* number of bytes to copy */ + long SS_ReadImage(void *pbUserData, unsigned char *pbBuffer, /* pointer to user buffer */ + SS_UINT32 dwStartAddress, /* memory address to read from */ + SS_UINT32 dwSize); /* number of bytes to copy */ /** ******************************************************************************* @@ -166,7 +166,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_ReadImageNewKey(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddress, SS_UINT32 dwSize); + long SS_ReadImageNewKey(void *pbUserData, unsigned char *pbBuffer, SS_UINT32 dwStartAddress, SS_UINT32 dwSize); /** ******************************************************************************* @@ -178,7 +178,7 @@ extern "C" { * \return Sector size, in bytes ******************************************************************************* */ - long SS_GetBlockSize(void *pbUserData); + long SS_GetBlockSize(void *pbUserData); #ifdef __cplusplus } diff --git a/ss_engine/SS_MultiProcessUpdate.h b/ss_engine/SS_MultiProcessUpdate.h index 73a0d75..e0cfa12 100755 --- a/ss_engine/SS_MultiProcessUpdate.h +++ b/ss_engine/SS_MultiProcessUpdate.h @@ -26,11 +26,11 @@ * To use the UPI with multi-process support you should implement the following * Porting Layer functions: * SS_Malloc - * SS_Free - * SS_WaitForProcess - * SS_RunProcess - * SS_GetMaxNumProcess - * SS_GetMaxProcRamSize + * SS_Free + * SS_WaitForProcess + * SS_RunProcess + * SS_GetMaxNumProcess + * SS_GetMaxProcRamSize * * The function SS_RunProcess must run a sub-process (optionally the same * executable running the main process) that is also integrated with the UPI. @@ -58,9 +58,9 @@ * for SS_Trace, these preparations must be done before calling * SS_HandleProcessRequests. * \li To distinguish between processes when running in Multi-Process Update mode, - * it is recommended that you add the Process ID (i.e. getpid()) to the log - * output in SS_Trace. Alternatively, create a log file for each sub-process - * named log_file.[pid] + * it is recommended that you add the Process ID (i.e. getpid()) to the log + * output in SS_Trace. Alternatively, create a log file for each sub-process + * named log_file.[pid] * \li If your implementation uses the optional opaque data structure ('user' * in the functions below), you may also have to pass it to sub-processes. * You must implement this functionality. @@ -85,16 +85,16 @@ extern "C" { * \return A pointer to the memory block on success, NULL on failure ******************************************************************************* */ - void *SS_Malloc(SS_UINT32 size); + void *SS_Malloc(SS_UINT32 size); /** ******************************************************************************* * Free a memory block. * - * \param pMemBlock Pointer to the memory block + * \param pMemBlock Pointer to the memory block ******************************************************************************* */ - void SS_Free(void *pMemBlock); + void SS_Free(void *pMemBlock); /** ******************************************************************************* @@ -107,7 +107,7 @@ extern "C" { * \return Handle of the completed process or NULL on error ******************************************************************************* */ - void *SS_WaitForProcess(const void *handle, SS_UINT32 * process_exit_code); + void *SS_WaitForProcess(const void *handle, SS_UINT32 * process_exit_code); /** ******************************************************************************* @@ -124,7 +124,7 @@ extern "C" { * \return Handle to the new process or NULL on error ******************************************************************************* */ - void *SS_RunProcess(void *user, int argc, char *argv[]); + void *SS_RunProcess(void *user, int argc, char *argv[]); /** ******************************************************************************* @@ -137,7 +137,7 @@ extern "C" { * allowed ******************************************************************************* */ - SS_UINT32 SS_GetMaxNumProcess(void *user); + SS_UINT32 SS_GetMaxNumProcess(void *user); /** ******************************************************************************* * Get the maximum available memory for processes to use. @@ -148,7 +148,7 @@ extern "C" { * \return The memory amount available for processes or 0 if there is no available memory ******************************************************************************* */ - unsigned long SS_GetMaxProcRamSize(void *user); + unsigned long SS_GetMaxProcRamSize(void *user); /** ******************************************************************************* @@ -166,7 +166,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an error code ******************************************************************************* */ - long SS_HandleProcessRequest(void *user, int argc, char *argv[]); + long SS_HandleProcessRequest(void *user, int argc, char *argv[]); #ifdef __cplusplus } diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index 49180a4..3395881 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -35,40 +35,39 @@ extern void *SS_Malloc(unsigned int size); typedef struct { - unsigned char *buffer; - ssize_t size; - ssize_t pos; + unsigned char *buffer; + ssize_t size; + ssize_t pos; } MemorySinkInfo; ssize_t ss_memorySink(unsigned char *data, ssize_t len, void *token) { - MemorySinkInfo *msi = (MemorySinkInfo *) token; - if (msi->size - msi->pos < len) { - return -1; - } - memcpy(msi->buffer + msi->pos, data, len); - msi->pos += len; - return len; + MemorySinkInfo *msi = (MemorySinkInfo *) token; + if (msi->size - msi->pos < len) + return -1; + memcpy(msi->buffer + msi->pos, data, len); + msi->pos += len; + return len; } ssize_t ss_fileSink(unsigned char *data, ssize_t len, void *token) { - int ss_fd = *(int *)token; - char buf[256]; - ssize_t done = 0; - ssize_t wrote; - while (done < (ssize_t) len) { - wrote = write(ss_fd, data + done, len - done); - if (wrote <= 0) { - if (errno == EINTR || errno == EAGAIN) - continue; // try again - strerror_r(errno, buf, sizeof(buf)); - LOGE("error writing %d bytes: %s\n", (int)(len - done), buf); - return done; - } - done += wrote; - } - return done; + int ss_fd = *(int *)token; + char buf[256]; + ssize_t done = 0; + ssize_t wrote; + while (done < (ssize_t) len) { + wrote = write(ss_fd, data + done, len - done); + if (wrote <= 0) { + if (errno == EINTR || errno == EAGAIN) + continue; // try again + strerror_r(errno, buf, sizeof(buf)); + LOGE("error writing %d bytes: %s\n", (int)(len - done), buf); + return done; + } + done += wrote; + } + return done; } // Take a string 'str' of 40 hex digits and parse it into the 20 @@ -77,142 +76,140 @@ ssize_t ss_fileSink(unsigned char *data, ssize_t len, void *token) // error. int ParseSha1(const char *str, uint8_t * digest) { - int i; - const char *ps = str; - uint8_t *pd = digest; - for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { - int digit; - if (*ps >= '0' && *ps <= '9') { - digit = *ps - '0'; - } else if (*ps >= 'a' && *ps <= 'f') { - digit = *ps - 'a' + 10; - } else if (*ps >= 'A' && *ps <= 'F') { - digit = *ps - 'A' + 10; - } else { - return -1; - } - if (i % 2 == 0) { - *pd = digit << 4; - } else { - *pd |= digit; - ++pd; - } - } - if (*ps != '\0') - return -1; - return 0; + int i; + const char *ps = str; + uint8_t *pd = digest; + for (i = 0; i < SHA_DIGEST_SIZE * 2; ++i, ++ps) { + int digit; + if (*ps >= '0' && *ps <= '9') + digit = *ps - '0'; + else if (*ps >= 'a' && *ps <= 'f') + digit = *ps - 'a' + 10; + else if (*ps >= 'A' && *ps <= 'F') + digit = *ps - 'A' + 10; + else + return -1; + if (i % 2 == 0) { + *pd = digit << 4; + } else { + *pd |= digit; + ++pd; + } + } + if (*ps != '\0') + return -1; + return 0; } //Function to find the start of gzipped part in compressed kernel int getOffset(char *zimage_path) { - char gzip_header[] = { 31, -117, 8 }; //header value for gzip which needs to be checked - char buf[4] = { 0, }; - int offset = 0;//wgid:14074 - - FILE *f = fopen(zimage_path, "r"); - if(!f){ - LOGE("Fopen failed for path %s\n", zimage_path); - SS_SetUpgradeState(E_SS_OPENFILE_ONLYR); - return -1; - } - fseek(f, 0, SEEK_SET); - while (fread(buf, 1, 3, f) > 0) { - if (gzip_header[0] == buf[0] && gzip_header[1] == buf[1] && gzip_header[2] == buf[2]) { - LOGL(LOG_SSENGINE, "match for %d %d %d found at %d\n", buf[0], buf[1], buf[2], ftell(f) - 3); - break; - } else { - fseek(f, -2, SEEK_CUR); - } - } - offset = ftell(f) - 3; - fclose(f); - return offset; + char gzip_header[] = { 31, -117, 8 }; //header value for gzip which needs to be checked + char buf[4] = { 0, }; + int offset = 0;//wgid:14074 + + FILE *f = fopen(zimage_path, "r"); + if (!f) { + LOGE("Fopen failed for path %s\n", zimage_path); + SS_SetUpgradeState(E_SS_OPENFILE_ONLYR); + return -1; + } + fseek(f, 0, SEEK_SET); + while (fread(buf, 1, 3, f) > 0) { + if (gzip_header[0] == buf[0] && gzip_header[1] == buf[1] && gzip_header[2] == buf[2]) { + LOGL(LOG_SSENGINE, "match for %d %d %d found at %d\n", buf[0], buf[1], buf[2], ftell(f) - 3); + break; + } else { + fseek(f, -2, SEEK_CUR); + } + } + offset = ftell(f) - 3; + fclose(f); + return offset; } int SS_LoadPartition(const char *filename, FileInfo * file) { - size_t read = 0; - FILE *dev = NULL; - int i; - char buf[256]; - - dev = fopen(filename, "rb"); - if (dev == NULL) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to open partition \"%s\": %s\n", filename, buf); - return -1; - } - - sha1_ctx_t sha_ctx; - sha1_init(&sha_ctx); - - file->data = SS_Malloc(file->size); - if (file->data) { - read = fread(file->data, 1, file->size, dev); - LOGL(LOG_SSENGINE, "Partition size read %d\n", read); - sha1_update(&sha_ctx, file->data, read); - file->size = read; - } - - const uint8_t sha_final[SHA_DIGEST_SIZE] = { 0, }; - sha1_final(&sha_ctx, (uint32_t *) & sha_final); - for (i = 0; i < SHA_DIGEST_SIZE; ++i) { - file->sha1[i] = sha_final[i]; - } - //LOGL(LOG_SSENGINE, "Final SHA of Source (%s)\n", sha_final); - - file->st.st_mode = 0644; - file->st.st_uid = 0; - file->st.st_gid = 0; - fclose(dev); - return 0; + size_t read = 0; + FILE *dev = NULL; + int i; + char buf[256]; + + dev = fopen(filename, "rb"); + if (dev == NULL) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to open partition \"%s\": %s\n", filename, buf); + return -1; + } + + sha1_ctx_t sha_ctx; + sha1_init(&sha_ctx); + + file->data = SS_Malloc(file->size); + if (file->data) { + read = fread(file->data, 1, file->size, dev); + LOGL(LOG_SSENGINE, "Partition size read %d\n", read); + sha1_update(&sha_ctx, file->data, read); + file->size = read; + } + + const uint8_t sha_final[SHA_DIGEST_SIZE] = { 0, }; + sha1_final(&sha_ctx, (uint32_t *) & sha_final); + for (i = 0; i < SHA_DIGEST_SIZE; ++i) + file->sha1[i] = sha_final[i]; + //LOGL(LOG_SSENGINE, "Final SHA of Source (%s)\n", sha_final); + + file->st.st_mode = 0644; + file->st.st_uid = 0; + file->st.st_gid = 0; + fclose(dev); + return 0; } //extern int write_data_to_blkdev(char* dev_name, int blk_start, int blk_cnt, char* data); int SS_LoadFile(const char *filename, FileInfo * file) { - char buf[256]; - - file->data = NULL; - //LOGL(LOG_SSENGINE,"SS_LoadFile --- [File name %s]\n",filename); - - if (stat(filename, &file->st) != 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to stat \"%s\": %s\n", filename, buf); - return -1; - } - - file->size = file->st.st_size; - file->data = SS_Malloc(file->size); - if (!file->data) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to allocate memory for \"%s\": %s\n", filename, buf); - return -1; - } - - FILE *f = fopen(filename, "rb"); - if (f == NULL) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to open \"%s\": %s\n", filename, buf); - SS_Free(file->data); - file->data = NULL; - return -1; - } - - ssize_t bytes_read = fread(file->data, 1, file->size, f); - if (bytes_read != file->size) { - LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); - SS_Free(file->data); - file->data = NULL; - fclose(f); - return -1; - } - fclose(f); - //LOGL(LOG_SSENGINE,"SS_LoadFile --- [bytes_read %d]\n",bytes_read); - sha1(file->data, file->size, (uint32_t *) file->sha1); - return 0; + char buf[256]; + + file->data = NULL; + //LOGL(LOG_SSENGINE,"SS_LoadFile --- [File name %s]\n",filename); + + if (stat(filename, &file->st) != 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to stat \"%s\": %s\n", filename, buf); + return -1; + } + + file->size = file->st.st_size; + file->data = SS_Malloc(file->size); + if (!file->data) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to allocate memory for \"%s\": %s\n", filename, buf); + return -1; + } + + FILE *f = fopen(filename, "rb"); + if (f == NULL) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to open \"%s\": %s\n", filename, buf); + SS_Free(file->data); + file->data = NULL; + return -1; + } + + ssize_t bytes_read = fread(file->data, 1, file->size, f); + if (bytes_read != file->size) { + LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); + SS_Free(file->data); + file->data = NULL; + fclose(f); + return -1; + } + fclose(f); + //LOGL(LOG_SSENGINE,"SS_LoadFile --- [bytes_read %d]\n",bytes_read); + sha1(file->data, file->size, (uint32_t *) file->sha1); + return 0; } extern int gvalid_session; @@ -220,317 +217,314 @@ extern void create_dir(char *pathname, int mode); #ifdef SUPPORT_CONTAINER //unzip source archive , apply patch from extracted delta folder and repack the source archive int SS_UpdateArchive(ua_dataSS_t * ua_dataSS, const char *source_filename, const char *target_filename, - const char *source_sha1_str, const char *target_sha1_str) + const char *source_sha1_str, const char *target_sha1_str) { - FILE *fp; - char *line = NULL, *token = NULL, *source_file = NULL, *new_file = NULL, *dir_to_create = NULL, *patch_file = NULL; - char patch_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for patches - char source_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for uncompressed source files - char target_path_full[MAX_FILE_PATH] = { 0, }; - char patchlist[MAX_FILE_PATH] = { 0, }; - char cmd[2 * MAX_FILE_PATH] = { 0, }; - size_t len = 0, read = 0; - int result = S_SS_SUCCESS; - uint8_t target_sha1[SHA_DIGEST_SIZE]; - uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; - FileInfo source_data = { 0, }; - int backupsrc = -1; - SinkFn sink = NULL; - void *tok = NULL; - int output = -1; - char *outname = NULL; - - if (ParseSha1(target_sha1_str, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); - return E_SS_FAILURE; - } - - if (0 == gvalid_session) { - if (ParseSha1(source_sha1_str, source_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", source_sha1_str); - return E_SS_FAILURE; - } - if (SS_LoadFile(source_filename, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); - if (source_data.data) - SS_Free(source_data.data); - } else if (memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); - if (source_data.data) - SS_Free(source_data.data); - return S_SS_SUCCESS; - } else { - //Check for backup file SHA - SS_Free(source_data.data); - source_data.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { - LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, - "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } - } else { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { - LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } - } + FILE *fp; + char *line = NULL, *token = NULL, *source_file = NULL, *new_file = NULL, *dir_to_create = NULL, *patch_file = NULL; + char patch_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for patches + char source_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for uncompressed source files + char target_path_full[MAX_FILE_PATH] = { 0, }; + char patchlist[MAX_FILE_PATH] = { 0, }; + char cmd[2 * MAX_FILE_PATH] = { 0, }; + size_t len = 0, read = 0; + int result = S_SS_SUCCESS; + uint8_t target_sha1[SHA_DIGEST_SIZE]; + uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; + FileInfo source_data = { 0, }; + int backupsrc = -1; + SinkFn sink = NULL; + void *tok = NULL; + int output = -1; + char *outname = NULL; + + if (ParseSha1(target_sha1_str, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); + return E_SS_FAILURE; + } + + if (0 == gvalid_session) { + if (ParseSha1(source_sha1_str, source_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", source_sha1_str); + return E_SS_FAILURE; + } + if (SS_LoadFile(source_filename, &source_data) == 0) { + if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); + if (source_data.data) + SS_Free(source_data.data); + } else if (memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); + if (source_data.data) + SS_Free(source_data.data); + return S_SS_SUCCESS; + } else { + //Check for backup file SHA + SS_Free(source_data.data); + source_data.data = NULL; + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); + if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { + if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { + LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + LOGL(LOG_SSENGINE, + "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + } + } else { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); + if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { + if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { + LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_data.data) + SS_Free(source_data.data); + return E_SS_FAILURE; + } + } + } #ifndef ENHANCED_BSDIFF - backupsrc = SS_BackupSource(source_filename); - if (backupsrc != 0) { - LOGE("failed to Backup source File:[%s] \n", source_filename); - SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); - return E_SS_FAILURE; - } + backupsrc = SS_BackupSource(source_filename); + if (backupsrc != 0) { + LOGE("failed to Backup source File:[%s] \n", source_filename); + SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); + return E_SS_FAILURE; + } #endif - //create workspace for processing container upgrade - SS_CreateFolder(NULL, SS_ARCHIVE_WORK_FOLDER); - SS_CreateFolder(NULL, SS_ARCHIVE_UNPACK_FOLDER); - - //unpack the source container to the unpack workspace - snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, source_filename, SS_ARCHIVE_UNPACK_FOLDER); - result = _system_cmd_wait(cmd); - if (result != S_SS_SUCCESS) { - LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); - return E_SS_FAILURE; - } - //extract unpack scipt from delta.tar to process containers later - if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) - if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > - 0) - LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); - else { - LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - result = E_SS_FAILURE; - } - //move new tpk extracted in the delta folder to the work folder - new_file = strrchr(target_filename, '/'); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); - - result = rename(source_path_full, target_path_full); - if (result != 0) { - LOGE("fatal error in moving %s to %s\n", source_path_full, target_path_full); - return E_SS_FAILURE; - } - snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, target_path_full, SS_ARCHIVE_WORK_FOLDER); - result = _system_cmd_wait(cmd); - if (result != S_SS_SUCCESS) { - LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); - return E_SS_FAILURE; - } else - LOGL(LOG_SSENGINE, "Thin zip extracted successfully\n"); - // open the patch list and start iterating through the changes and the same files - snprintf(patchlist, MAX_FILE_PATH, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, SS_CONTAINER_INFO_FILE); - fp = fopen(patchlist, "r"); - if (!fp) { - LOGE("file open error [%s]\n", patchlist); - return E_SS_FAILURE; - } - - while ((read = getline(&line, &len, fp)) != -1) { - - switch (line[0]) { // '-' == Delete File, 's' == same File, 'c' == Changed File - case 's': //for same files case, just extract from old tpk to the work folder, update new tpk in the end - token = strtok(line, SS_SEPARATOR_TOKEN); - - source_file = strtok(NULL, SS_NEWLINE_TOKEN); - - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - LOGL(LOG_SSENGINE, "copy %s\n", source_file); - result = SS_MoveFile(NULL, source_path_full, target_path_full); - if (result != S_SS_SUCCESS) { - LOGE("fatal error [%d]\n", errno); - goto Cleanup; - } - break; - case 'c': - token = strtok(line, SS_SEPARATOR_TOKEN); - - source_file = strtok(NULL, SS_SEPARATOR_TOKEN); - patch_file = strtok(NULL, SS_NEWLINE_TOKEN); - - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - LOGL(LOG_SSENGINE, "copy %s\n", source_file); - result = SS_MoveFile(NULL, source_path_full, target_path_full); - if (result != S_SS_SUCCESS) { - LOGE("fatal error [%d]\n", errno); - } - - snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, patch_file); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(source_path_full) + 10); - if (outname == NULL) - goto Cleanup; - strcpy(outname, source_path_full); - strcat(outname, ".patch"); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); - SS_Free(outname); - goto Cleanup; - } - sink = ss_fileSink; - tok = &output; - } - result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); - LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != 0) { - LOGE("applying patch failed %s\n", source_path_full); - if (outname != NULL) { - unlink(outname); - } - goto Cleanup; - } - result = rename(outname, source_path_full); - if (result != 0) { - LOGE("fatal error %s\n", source_path_full); - goto Cleanup; - } else { - LOGL(LOG_SSENGINE, "Successfully applied patch for [%s]\n", source_path_full); - } - break; - default: - break; - } - } - - new_file = strrchr(target_filename, '/'); - snprintf(cmd, sizeof(cmd) - 1, "%s -p %s %s /opt/data/fota", SS_KERN_UNPK_SCRIPT_PATH, SS_ARCHIVE_WORK_FOLDER, - new_file + 1); - int ret = _system_cmd_wait(cmd); - LOGL(LOG_SSENGINE, "ret for %s is %d\n", cmd, ret); - - //Apply diff between intermediate new tpk and the new tpk which contains central dir changes only - snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/New_%s.delta", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(source_path_full) + 10); - if (outname == NULL) - return E_SS_FAILURE; - strcpy(outname, source_path_full); - strcat(outname, ".patch"); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); - SS_Free(outname); - goto Cleanup; - } - sink = ss_fileSink; - tok = &output; - } - result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); - LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != 0) { - LOGE("applying patch failed %s\n", source_path_full); - if (outname != NULL) { - unlink(outname); - } - goto Cleanup; - } - - if (SS_LoadFile(outname, &source_data) == 0) - result = memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE); - if (result != S_SS_SUCCESS) { - LOGE("patch did not produce expected sha1 \n"); - SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); - goto Cleanup; - } - - result = rename(outname, source_path_full); - if (result != 0) { - LOGE("fatal error %s\n", source_path_full); - goto Cleanup; - } - //Delete old file and copy patched archive, cant use rename as partitions may be different - unlink(source_filename); - if (result != 0) { - LOGE("failed to unlink [%s] code [%d]\n", source_filename, errno); - goto Cleanup; - } - result = (int)SS_CopyFile(NULL, source_path_full, target_filename); - if (result != S_SS_SUCCESS) { - LOGE("failed to copy file [%s] result [%d]\n", source_path_full, result); - goto Cleanup; - } + //create workspace for processing container upgrade + SS_CreateFolder(NULL, SS_ARCHIVE_WORK_FOLDER); + SS_CreateFolder(NULL, SS_ARCHIVE_UNPACK_FOLDER); + + //unpack the source container to the unpack workspace + snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, source_filename, SS_ARCHIVE_UNPACK_FOLDER); + result = _system_cmd_wait(cmd); + if (result != S_SS_SUCCESS) { + LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); + return E_SS_FAILURE; + } + //extract unpack scipt from delta.tar to process containers later + if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) + if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > + 0) + LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); + else { + LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); + SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); + result = E_SS_FAILURE; + } + //move new tpk extracted in the delta folder to the work folder + new_file = strrchr(target_filename, '/'); + snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); + snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); + + result = rename(source_path_full, target_path_full); + if (result != 0) { + LOGE("fatal error in moving %s to %s\n", source_path_full, target_path_full); + return E_SS_FAILURE; + } + snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, target_path_full, SS_ARCHIVE_WORK_FOLDER); + result = _system_cmd_wait(cmd); + if (result != S_SS_SUCCESS) { + LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); + return E_SS_FAILURE; + } else + LOGL(LOG_SSENGINE, "Thin zip extracted successfully\n"); + // open the patch list and start iterating through the changes and the same files + snprintf(patchlist, MAX_FILE_PATH, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, SS_CONTAINER_INFO_FILE); + fp = fopen(patchlist, "r"); + if (!fp) { + LOGE("file open error [%s]\n", patchlist); + return E_SS_FAILURE; + } + + while ((read = getline(&line, &len, fp)) != -1) { + + switch (line[0]) { // '-' == Delete File, 's' == same File, 'c' == Changed File + case 's': //for same files case, just extract from old tpk to the work folder, update new tpk in the end + token = strtok(line, SS_SEPARATOR_TOKEN); + + source_file = strtok(NULL, SS_NEWLINE_TOKEN); + + snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); + snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); + LOGL(LOG_SSENGINE, "copy %s\n", source_file); + result = SS_MoveFile(NULL, source_path_full, target_path_full); + if (result != S_SS_SUCCESS) { + LOGE("fatal error [%d]\n", errno); + goto Cleanup; + } + break; + case 'c': + token = strtok(line, SS_SEPARATOR_TOKEN); + + source_file = strtok(NULL, SS_SEPARATOR_TOKEN); + patch_file = strtok(NULL, SS_NEWLINE_TOKEN); + + snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); + snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); + LOGL(LOG_SSENGINE, "copy %s\n", source_file); + result = SS_MoveFile(NULL, source_path_full, target_path_full); + if (result != S_SS_SUCCESS) + LOGE("fatal error [%d]\n", errno); + + snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, patch_file); + snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); + + { + // We write the decoded output to ".patch". + //allocate some extra space to allow for concatinating ".patch" with the name + outname = (char *)SS_Malloc(strlen(source_path_full) + 10); + if (outname == NULL) + goto Cleanup; + strcpy(outname, source_path_full); + strcat(outname, ".patch"); + + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (output < 0) { + LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); + SS_Free(outname); + goto Cleanup; + } + sink = ss_fileSink; + tok = &output; + } + result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); + LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); + if (output >= 0) { + fsync(output); + close(output); + } + + if (result != 0) { + LOGE("applying patch failed %s\n", source_path_full); + if (outname != NULL) + unlink(outname); + goto Cleanup; + } + result = rename(outname, source_path_full); + if (result != 0) { + LOGE("fatal error %s\n", source_path_full); + goto Cleanup; + } else { + LOGL(LOG_SSENGINE, "Successfully applied patch for [%s]\n", source_path_full); + } + break; + default: + break; + } + } + + new_file = strrchr(target_filename, '/'); + snprintf(cmd, sizeof(cmd) - 1, "%s -p %s %s /opt/data/fota", SS_KERN_UNPK_SCRIPT_PATH, SS_ARCHIVE_WORK_FOLDER, + new_file + 1); + int ret = _system_cmd_wait(cmd); + LOGL(LOG_SSENGINE, "ret for %s is %d\n", cmd, ret); + + //Apply diff between intermediate new tpk and the new tpk which contains central dir changes only + snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/New_%s.delta", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); + snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); + + { + // We write the decoded output to ".patch". + //allocate some extra space to allow for concatinating ".patch" with the name + outname = (char *)SS_Malloc(strlen(source_path_full) + 10); + if (outname == NULL) + return E_SS_FAILURE; + strcpy(outname, source_path_full); + strcat(outname, ".patch"); + + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (output < 0) { + LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); + SS_Free(outname); + goto Cleanup; + } + sink = ss_fileSink; + tok = &output; + } + result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); + LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); + if (output >= 0) { + fsync(output); + close(output); + } + + if (result != 0) { + LOGE("applying patch failed %s\n", source_path_full); + if (outname != NULL) + unlink(outname); + goto Cleanup; + } + + if (SS_LoadFile(outname, &source_data) == 0) + result = memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE); + if (result != S_SS_SUCCESS) { + LOGE("patch did not produce expected sha1 \n"); + SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); + goto Cleanup; + } + + result = rename(outname, source_path_full); + if (result != 0) { + LOGE("fatal error %s\n", source_path_full); + goto Cleanup; + } + //Delete old file and copy patched archive, cant use rename as partitions may be different + unlink(source_filename); + if (result != 0) { + LOGE("failed to unlink [%s] code [%d]\n", source_filename, errno); + goto Cleanup; + } + result = (int)SS_CopyFile(NULL, source_path_full, target_filename); + if (result != S_SS_SUCCESS) { + LOGE("failed to copy file [%s] result [%d]\n", source_path_full, result); + goto Cleanup; + } Cleanup: - fclose(fp); - if (line) - SS_Free(line); - if (outname) - SS_Free(outname); - SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); - SS_DeleteFile(NULL, SS_FIND_CMD_TARGET); - SS_DeleteFolder(NULL, SS_CONTAINER_WORKSPACE); - return result; + fclose(fp); + if (line) + SS_Free(line); + if (outname) + SS_Free(outname); + SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); + SS_DeleteFile(NULL, SS_FIND_CMD_TARGET); + SS_DeleteFolder(NULL, SS_CONTAINER_WORKSPACE); + return result; } #endif /*! ********************************************************************************* - * SS_UpdateDeltaFS + * SS_UpdateDeltaFS ********************************************************************************* * * @brief @@ -546,238 +540,235 @@ int SS_UpdateArchive(ua_dataSS_t * ua_dataSS, const char *source_filename, const */ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, - const char *source_sha1_str, const char *target_sha1_str, int patch_data_size) + const char *source_sha1_str, const char *target_sha1_str, int patch_data_size) { - uint8_t target_sha1[SHA_DIGEST_SIZE]; - sha1_ctx_t ctx1; - int output; - int retry = 1; - int use_backup = 0; - char *outname = NULL; - int backupsrc = -1; - int result = 0; - char buf[256]; - FileInfo source_file; - uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; - - if (ParseSha1(target_sha1_str, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); - return E_SS_FAILURE; - } - - /* - if battery removed in between update gvalid_session becomes 0 - need to check file integrity in that case - */ - if (0 == gvalid_session) { - if (ParseSha1(source_sha1_str, source_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", source_sha1_str); - return E_SS_FAILURE; - } - if (SS_LoadFile(source_filename, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); - if (source_file.data) - SS_Free(source_file.data); - } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); - if (source_file.data) - SS_Free(source_file.data); - return S_SS_SUCCESS; - } else { - //Check for backup file SHA - SS_Free(source_file.data); - source_file.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != S_SS_SUCCESS) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, buf); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (source_file.data) - SS_Free(source_file.data); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, - "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_file.data) - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_file.data) - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } - } else { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - use_backup = 1; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_file.data) - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_file.data) - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } - } - //Now proceed wit patch application since patch can be applied - do { - int enough_space = 0; - size_t free_space; - char *tok; - - if (retry > 0) { - if (use_backup) { - tok = strrchr(source_filename, '/'); - *tok = '\0'; - } - SS_GetAvailableFreeSpace(NULL, source_filename, &free_space); - enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum - (free_space > (patch_data_size * 3 / 2)); // 50% margin of error - if (use_backup) - *tok = '/'; - } - - if (!use_backup) { + uint8_t target_sha1[SHA_DIGEST_SIZE]; + sha1_ctx_t ctx1; + int output; + int retry = 1; + int use_backup = 0; + char *outname = NULL; + int backupsrc = -1; + int result = 0; + char buf[256]; + FileInfo source_file; + uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; + + if (ParseSha1(target_sha1_str, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); + return E_SS_FAILURE; + } + + /* + if battery removed in between update gvalid_session becomes 0 + need to check file integrity in that case + */ + if (0 == gvalid_session) { + if (ParseSha1(source_sha1_str, source_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", source_sha1_str); + return E_SS_FAILURE; + } + if (SS_LoadFile(source_filename, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); + if (source_file.data) + SS_Free(source_file.data); + } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); + if (source_file.data) + SS_Free(source_file.data); + return S_SS_SUCCESS; + } else { + //Check for backup file SHA + SS_Free(source_file.data); + source_file.data = NULL; + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); + if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != S_SS_SUCCESS) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, buf); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (source_file.data) + SS_Free(source_file.data); + return E_SS_FAILURE; + } + LOGL(LOG_SSENGINE, + "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_file.data) + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_file.data) + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } + } else { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); + if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + use_backup = 1; + LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_file.data) + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } else { + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); + if (source_file.data) + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } + } + //Now proceed wit patch application since patch can be applied + do { + int enough_space = 0; + size_t free_space; + char *tok; + + if (retry > 0) { + if (use_backup) { + tok = strrchr(source_filename, '/'); + *tok = '\0'; + } + SS_GetAvailableFreeSpace(NULL, source_filename, &free_space); + enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum + (free_space > (patch_data_size * 3 / 2)); // 50% margin of error + if (use_backup) + *tok = '/'; + } + + if (!use_backup) { #ifndef ENHANCED_BSDIFF - backupsrc = SS_BackupSource(source_filename); - if (backupsrc != 0) { - LOGE("failed to Backup source File:[%s] \n", source_filename); - SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); - return E_SS_FAILURE; - } + backupsrc = SS_BackupSource(source_filename); + if (backupsrc != 0) { + LOGE("failed to Backup source File:[%s] \n", source_filename); + SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); + return E_SS_FAILURE; + } #endif - } - if (!enough_space) { - LOGL(LOG_SSENGINE, "For %s: free space %ld bytes; enough %d\n", source_filename, (long)free_space, - enough_space); - retry = 0; - use_backup = 1; - unlink(source_filename); - } - //LOGL(LOG_SSENGINE, "For %s: target %ld bytes; free space %ld bytes; enough %d\n", - // source_filename, (long)patch_data_size, (long)free_space, enough_space); - //LOGL(LOG_SSENGINE,"Generate Target Space availabitiy [%d]\n", enough_space); - - SinkFn sink = NULL; - void *token = NULL; - output = -1; - outname = NULL; - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(target_filename) + 10); - if (outname == NULL) - return -1; - snprintf(outname, strlen(target_filename) + 10, - "%s.patch", target_filename); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - if (errno == 2) { - char *dir_path = strrchr(outname, '/'); - *dir_path = '\0'; - // need to create directory as the target may be different from source - LOGL(LOG_SSENGINE, "need to create directory [%s]\n", outname); - create_dir(outname, 0755); - *dir_path = '/'; - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to open output file %s: %s\n", outname, buf); - SS_Free(outname); - return E_SS_FAILURE; - } - } - } - sink = ss_fileSink; - token = &output; - } - sha1_init(&ctx1); - if (use_backup) - result = SS_ApplyBsdiff(SS_BACKUP_SOURCE, outname, SS_PATCHFILE_SOURCE, sink, token, &ctx1); - else - result = SS_ApplyBsdiff((char *)source_filename, outname, SS_PATCHFILE_SOURCE, sink, token, &ctx1); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != 0) { - if (retry == 0) { - LOGE("applying patch failed result : [%d]\n", result); - SS_Free(outname);//wgid: 20739 - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - return E_SS_FAILURE; - } else { - LOGE("applying patch failed; retrying\n"); - SS_Free(outname);//wgid: 20739 - } - } else { - // succeeded; no need to retry - break; - } - } while (retry-- > 0); - const uint8_t current_target_sha1[SHA_DIGEST_SIZE] = { 0, }; - sha1_final(&ctx1, (uint32_t *) & current_target_sha1); - if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { - LOGE("patch did not produce expected sha1\n"); - SS_SetUpgradeState(E_SS_FSSHA_MISMATCH); - if (outname != NULL) { - SS_Free(outname); - } - return E_SS_FAILURE; - } - // Finally, rename the .patch file to replace the target file. + } + if (!enough_space) { + LOGL(LOG_SSENGINE, "For %s: free space %ld bytes; enough %d\n", source_filename, (long)free_space, + enough_space); + retry = 0; + use_backup = 1; + unlink(source_filename); + } + //LOGL(LOG_SSENGINE, "For %s: target %ld bytes; free space %ld bytes; enough %d\n", + // source_filename, (long)patch_data_size, (long)free_space, enough_space); + //LOGL(LOG_SSENGINE,"Generate Target Space availabitiy [%d]\n", enough_space); + + SinkFn sink = NULL; + void *token = NULL; + output = -1; + outname = NULL; + + { + // We write the decoded output to ".patch". + //allocate some extra space to allow for concatinating ".patch" with the name + outname = (char *)SS_Malloc(strlen(target_filename) + 10); + if (outname == NULL) + return -1; + snprintf(outname, strlen(target_filename) + 10, + "%s.patch", target_filename); + + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (output < 0) { + if (errno == 2) { + char *dir_path = strrchr(outname, '/'); + *dir_path = '\0'; + // need to create directory as the target may be different from source + LOGL(LOG_SSENGINE, "need to create directory [%s]\n", outname); + create_dir(outname, 0755); + *dir_path = '/'; + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (output < 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to open output file %s: %s\n", outname, buf); + SS_Free(outname); + return E_SS_FAILURE; + } + } + } + sink = ss_fileSink; + token = &output; + } + sha1_init(&ctx1); + if (use_backup) + result = SS_ApplyBsdiff(SS_BACKUP_SOURCE, outname, SS_PATCHFILE_SOURCE, sink, token, &ctx1); + else + result = SS_ApplyBsdiff((char *)source_filename, outname, SS_PATCHFILE_SOURCE, sink, token, &ctx1); + if (output >= 0) { + fsync(output); + close(output); + } + + if (result != 0) { + if (retry == 0) { + LOGE("applying patch failed result : [%d]\n", result); + SS_Free(outname);//wgid: 20739 + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + return E_SS_FAILURE; + } else { + LOGE("applying patch failed; retrying\n"); + SS_Free(outname);//wgid: 20739 + } + } else { + // succeeded; no need to retry + break; + } + } while (retry-- > 0); + const uint8_t current_target_sha1[SHA_DIGEST_SIZE] = { 0, }; + sha1_final(&ctx1, (uint32_t *) & current_target_sha1); + if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { + LOGE("patch did not produce expected sha1\n"); + SS_SetUpgradeState(E_SS_FSSHA_MISMATCH); + if (outname != NULL) + SS_Free(outname); + return E_SS_FAILURE; + } + // Finally, rename the .patch file to replace the target file. #ifdef ENHANCED_BSDIFF - if (SS_rename1(outname, target_filename) != 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (outname != NULL) { - SS_Free(outname); - } - return E_SS_FAILURE; - } + if (SS_rename1(outname, target_filename) != 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (outname != NULL) + SS_Free(outname); + return E_SS_FAILURE; + } #else - if (rename(outname, target_filename) != 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (outname != NULL) { - SS_Free(outname); - } - return E_SS_FAILURE; - } - //remove source file if target is not same - if (strcmp(source_filename, target_filename) != 0) - unlink(source_filename); - SS_BackupSourceClear(); + if (rename(outname, target_filename) != 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("rename of .patch to \"%s\" failed: %s\n", target_filename, buf); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (outname != NULL) + SS_Free(outname); + return E_SS_FAILURE; + } + //remove source file if target is not same + if (strcmp(source_filename, target_filename) != 0) + unlink(source_filename); + SS_BackupSourceClear(); #endif - SS_PatchSourceClear(); - SS_Free(outname); + SS_PatchSourceClear(); + SS_Free(outname); - return result; + return result; } /*! ********************************************************************************* - * SS_UpdateDeltaKernel + * SS_UpdateDeltaKernel ********************************************************************************* * * @brief @@ -794,302 +785,301 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char *, int, int, char *)) { - uint8_t target_sha1[SHA_DIGEST_SIZE]; - uint8_t source_sha1[SHA_DIGEST_SIZE]; - FileInfo source_file; - int result = S_SS_SUCCESS; - int blk_cnt, read_count = 0; - int blk_start = 0; - int backupsrc = -1; - int use_backup_img = -1; - FILE *fp = NULL, *wp = NULL, *kp = NULL; - int i = 0, j = 0, file_len = 0; - char *magic = NULL, *file_name = NULL, *tok_buf = NULL, *buf = NULL, a = '0'; - char *saveptr; - char err_buf[256]; - char cmd[1024] = { 0, }; - char source_filename[MAX_FILE_PATH] = { 0, }; - char part_filename[MAX_FILE_PATH] = { 0, }; - int file_num = 0; - SinkFn sink = NULL; - void *tok = NULL; - int output = -1; - char *outname = NULL; - //Kernel Parts are created on unpacking kernel which is then used to apply delta - char *kernel_parts[] = { "decompression_code", - "piggy.gz", - "padding_piggy", - "piggy_trailer" - }; - - if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); - return E_SS_FAILURE; - } - - source_file.size = ua_dataSS->update_cfg->soure_img_size; - source_file.data = NULL; - if (0 == gvalid_session) { - - if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { - LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); - return E_SS_FAILURE; - } - - if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Can be applied\n"); - - } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Already applied\n"); - SS_Free(source_file.data); - return S_SS_SUCCESS; - } else { - SS_Free(source_file.data); - source_file.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Source was currupted, Try loading from backup source\n"); - if (SS_LoadPartition(SS_BACKUP_SOURCE, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - use_backup_img = 1; - - LOGL(LOG_SSENGINE, - "SS_UpdateDeltaIMG - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } - } - } - } else { //in case of kernel delta need to copy kernel data from blk to buffer - if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) != 0) { - SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); - LOGE("Fatal Error : Kernel block is corrupted\n"); - return E_SS_FAILURE; - } - } - if (use_backup_img == -1) { - backupsrc = SS_BackupSource(ua_dataSS->parti_info->ua_blk_name); - if (backupsrc != 0) { - LOGE("failed to Backup source File:[%s] \n", ua_dataSS->parti_info->ua_blk_name); - SS_SetUpgradeState(E_SS_IMGSRCBACKUPFAILED); - return E_SS_FAILURE; - } - } - //Cleanup workspace and copy helper executables to it before proceeding - SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); - create_dir(SS_KERNEL_WORKSPACE, 0755); - SS_CopyFile(NULL, SS_GZIP_SOURCE, SS_GZIP_TARGET); - SS_CopyFile(NULL, SS_STAT_SOURCE, SS_STAT_TARGET); - SS_CopyFile(NULL, SS_DD_SOURCE, SS_DD_TARGET); - - if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) - if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > - 0) - LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); - else { - LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - result = E_SS_FAILURE; - goto Cleanup; - } else { - LOGE("Error size is not positive for item %s", SS_KERNEL_UNPACK_SCRIPT); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - result = E_SS_FAILURE; - goto Cleanup; - } - //Now write the kernel data to the workplace and start applying patch - snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_NAME); - fp = fopen(source_filename, "w"); - if (!fp) { - LOGE("file open error [%s] code [%d]\n", source_filename, errno); - SS_Free(source_file.data); - result = E_SS_FAILURE; - goto Cleanup; - } - //write source kernel data to workspace - read_count = fwrite(source_file.data, 1, source_file.size, fp); - if (read_count != source_file.size) { - LOGE("file write error read_count = %d for [%s]\n", read_count, source_filename); - SS_Free(source_file.data); - result = E_SS_FAILURE; - goto Cleanup; - } - SS_Free(source_file.data); - fclose(fp); - fp = NULL;//wgid: 59313 - - //Unpack source kernel - int offset = getOffset(source_filename); - if(offset < 0){ - LOGE("Failed to get offset\n"); - result = E_SS_FAILURE; - goto Cleanup; - } - snprintf(cmd, sizeof(cmd) - 1, "%s -u %s %s %d", SS_KERN_UNPK_SCRIPT_PATH, SS_KERNEL_WORKSPACE, SS_KERNEL_NAME, - offset); - int ret = _system_cmd_wait(cmd); - LOGL(LOG_SSENGINE, "ret for %s is %d\n", cmd, ret); - - //open delta file, extract kernel delta parts and apply patch to previously unpacked kernel - fp = fopen(SS_PATCHFILE_SOURCE, "r"); - if (fp == NULL) { - LOGE("Failed to open kernel delta patch\n"); - result = E_SS_FAILURE; - goto Cleanup; - } - //read kernel delta header for delta names and size - buf = SS_Malloc(SS_KERNEL_DELTA_HEADER); - if(!buf){//wgid: 13099 - LOGE("Failed to allocate memory\n"); - result = E_SS_MALLOC_ERROR; - goto Cleanup; - } - ssize_t bytes_read = fread(buf, 1, SS_KERNEL_DELTA_HEADER, fp); - if (bytes_read != SS_KERNEL_DELTA_HEADER) - LOGL(LOG_SSENGINE, "short read of \"%s\" (%ld bytes of %ld)\n", SS_PATCHFILE_SOURCE, (long)bytes_read, (long)SS_KERNEL_DELTA_HEADER); - magic = strtok_r(buf, ":", &saveptr); - LOGL(LOG_SSENGINE, "magic: %s\n", magic); - tok_buf = strtok_r(NULL, ":", &saveptr); - if (tok_buf) - file_num = atoi(tok_buf); - - //adjust offset to start of data section before proceeding - fseek(fp, SS_KERNEL_DELTA_HEADER, SEEK_SET); - - while (file_num-- > 0) { - file_name = strtok_r(NULL, ":", &saveptr); + uint8_t target_sha1[SHA_DIGEST_SIZE]; + uint8_t source_sha1[SHA_DIGEST_SIZE]; + FileInfo source_file; + int result = S_SS_SUCCESS; + int blk_cnt, read_count = 0; + int blk_start = 0; + int backupsrc = -1; + int use_backup_img = -1; + FILE *fp = NULL, *wp = NULL, *kp = NULL; + int i = 0, j = 0, file_len = 0; + char *magic = NULL, *file_name = NULL, *tok_buf = NULL, *buf = NULL, a = '0'; + char *saveptr; + char err_buf[256]; + char cmd[1024] = { 0, }; + char source_filename[MAX_FILE_PATH] = { 0, }; + char part_filename[MAX_FILE_PATH] = { 0, }; + int file_num = 0; + SinkFn sink = NULL; + void *tok = NULL; + int output = -1; + char *outname = NULL; + //Kernel Parts are created on unpacking kernel which is then used to apply delta + char *kernel_parts[] = { "decompression_code", + "piggy.gz", + "padding_piggy", + "piggy_trailer" + }; + + if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); + return E_SS_FAILURE; + } + + source_file.size = ua_dataSS->update_cfg->soure_img_size; + source_file.data = NULL; + if (0 == gvalid_session) { + + if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { + LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); + return E_SS_FAILURE; + } + + if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Can be applied\n"); + + } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Already applied\n"); + SS_Free(source_file.data); + return S_SS_SUCCESS; + } else { + SS_Free(source_file.data); + source_file.data = NULL; + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Source was currupted, Try loading from backup source\n"); + if (SS_LoadPartition(SS_BACKUP_SOURCE, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + use_backup_img = 1; + + LOGL(LOG_SSENGINE, + "SS_UpdateDeltaIMG - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } + } + } + } else { //in case of kernel delta need to copy kernel data from blk to buffer + if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) != 0) { + SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); + LOGE("Fatal Error : Kernel block is corrupted\n"); + return E_SS_FAILURE; + } + } + if (use_backup_img == -1) { + backupsrc = SS_BackupSource(ua_dataSS->parti_info->ua_blk_name); + if (backupsrc != 0) { + LOGE("failed to Backup source File:[%s] \n", ua_dataSS->parti_info->ua_blk_name); + SS_SetUpgradeState(E_SS_IMGSRCBACKUPFAILED); + return E_SS_FAILURE; + } + } + //Cleanup workspace and copy helper executables to it before proceeding + SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); + create_dir(SS_KERNEL_WORKSPACE, 0755); + SS_CopyFile(NULL, SS_GZIP_SOURCE, SS_GZIP_TARGET); + SS_CopyFile(NULL, SS_STAT_SOURCE, SS_STAT_TARGET); + SS_CopyFile(NULL, SS_DD_SOURCE, SS_DD_TARGET); + + if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) + if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > + 0) + LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); + else { + LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); + SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); + result = E_SS_FAILURE; + goto Cleanup; + } else { + LOGE("Error size is not positive for item %s", SS_KERNEL_UNPACK_SCRIPT); + SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); + result = E_SS_FAILURE; + goto Cleanup; + } + //Now write the kernel data to the workplace and start applying patch + snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_NAME); + fp = fopen(source_filename, "w"); + if (!fp) { + LOGE("file open error [%s] code [%d]\n", source_filename, errno); + SS_Free(source_file.data); + result = E_SS_FAILURE; + goto Cleanup; + } + //write source kernel data to workspace + read_count = fwrite(source_file.data, 1, source_file.size, fp); + if (read_count != source_file.size) { + LOGE("file write error read_count = %d for [%s]\n", read_count, source_filename); + SS_Free(source_file.data); + result = E_SS_FAILURE; + goto Cleanup; + } + SS_Free(source_file.data); + fclose(fp); + fp = NULL;//wgid: 59313 + + //Unpack source kernel + int offset = getOffset(source_filename); + if (offset < 0) { + LOGE("Failed to get offset\n"); + result = E_SS_FAILURE; + goto Cleanup; + } + snprintf(cmd, sizeof(cmd) - 1, "%s -u %s %s %d", SS_KERN_UNPK_SCRIPT_PATH, SS_KERNEL_WORKSPACE, SS_KERNEL_NAME, + offset); + int ret = _system_cmd_wait(cmd); + LOGL(LOG_SSENGINE, "ret for %s is %d\n", cmd, ret); + + //open delta file, extract kernel delta parts and apply patch to previously unpacked kernel + fp = fopen(SS_PATCHFILE_SOURCE, "r"); + if (fp == NULL) { + LOGE("Failed to open kernel delta patch\n"); + result = E_SS_FAILURE; + goto Cleanup; + } + //read kernel delta header for delta names and size + buf = SS_Malloc(SS_KERNEL_DELTA_HEADER); + if (!buf) {//wgid: 13099 + LOGE("Failed to allocate memory\n"); + result = E_SS_MALLOC_ERROR; + goto Cleanup; + } + ssize_t bytes_read = fread(buf, 1, SS_KERNEL_DELTA_HEADER, fp); + if (bytes_read != SS_KERNEL_DELTA_HEADER) + LOGL(LOG_SSENGINE, "short read of \"%s\" (%ld bytes of %ld)\n", SS_PATCHFILE_SOURCE, (long)bytes_read, (long)SS_KERNEL_DELTA_HEADER); + magic = strtok_r(buf, ":", &saveptr); + LOGL(LOG_SSENGINE, "magic: %s\n", magic); tok_buf = strtok_r(NULL, ":", &saveptr); if (tok_buf) - file_len = atoi(tok_buf); - j = file_len; - snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s_unpacked/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_NAME, - file_name); - snprintf(part_filename, sizeof(part_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, file_name); - wp = fopen(part_filename, "w"); - while (j-- > 0) { - a = fgetc(fp); - fputc(a, wp); - } - fclose(wp); - - //apply bspatch to the unpacked kernel parts - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(source_filename) + 10); - if (outname == NULL) { - SS_SetUpgradeState(E_SS_MALLOC_ERROR); - result = E_SS_FAILURE; - goto Cleanup; - } - snprintf(outname, strlen(source_filename) + 10, "%s.patch", - source_filename); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - strerror_r(errno, err_buf, sizeof(err_buf)); - LOGE("failed to open output file %s: %s\n", outname, err_buf); - result = E_SS_FAILURE; - goto Cleanup; - } - sink = ss_fileSink; - tok = &output; - } - result = SS_ApplyBsdiff(source_filename, outname, part_filename, sink, tok, NULL); - LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != S_SS_SUCCESS) { - LOGE("applying patch failed %s\n", source_filename); - if (outname != NULL) { - unlink(outname); - } - goto Cleanup; - } - result = rename(outname, source_filename); - if (result != S_SS_SUCCESS) { - LOGE("fatal error %s\n", source_filename); - goto Cleanup; - } - if (file_name && strcmp(file_name, "piggy") == 0) { - snprintf(cmd, sizeof(cmd) - 1, - "%s/gzip -n -9 -c %s/%s/%s > %s/%s/%s.gz", - SS_KERNEL_WORKSPACE, SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, file_name, - SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, file_name); - result = _system_cmd_wait(cmd); - LOGL(LOG_SSENGINE, "ret for %s = %d\n", cmd, result); - unlink(source_filename); - } - unlink(part_filename); - } - //open new kernel file and append kernel parts to it in - snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_TARGET_NAME); - kp = fopen(source_filename, "w"); - for (i = 0; i < 4; i++) { - snprintf(part_filename, sizeof(part_filename) - 1, "%s/%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, - kernel_parts[i]); - wp = fopen(part_filename, "r"); - fseek(wp, SEEK_SET, SEEK_END); - j = ftell(wp); - fseek(wp, SEEK_SET, SEEK_SET); - while (j-- > 0) { - a = fgetc(wp); - if(a != EOF) - fputc(a, kp); - else - break; - } - fclose(wp); - unlink(part_filename); - } - fclose(fp); - fp = NULL; - fclose(kp); - - if (SS_LoadFile(source_filename, &source_file) == 0) - result = memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE); - if (result != S_SS_SUCCESS) { - LOGE("patch did not produce expected sha1 \n"); - SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); - goto Cleanup; - } - //Considering EMMC partition by deafult - - blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; - result = write_to_blkdev((char *)ua_dataSS->parti_info->ua_blk_name, blk_start, blk_cnt, (char *)source_file.data); - if (result != S_SS_SUCCESS) { - LOGE("write of patched data to %s failed\n", ua_dataSS->parti_info->ua_blk_name); // All returns should go to CLEAN UP. - SS_SetUpgradeState(E_SS_IMGFLASHWRITEFAIL); - goto Cleanup; - } + file_num = atoi(tok_buf); + + //adjust offset to start of data section before proceeding + fseek(fp, SS_KERNEL_DELTA_HEADER, SEEK_SET); + + while (file_num-- > 0) { + file_name = strtok_r(NULL, ":", &saveptr); + tok_buf = strtok_r(NULL, ":", &saveptr); + if (tok_buf) + file_len = atoi(tok_buf); + j = file_len; + snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s_unpacked/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_NAME, + file_name); + snprintf(part_filename, sizeof(part_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, file_name); + wp = fopen(part_filename, "w"); + while (j-- > 0) { + a = fgetc(fp); + fputc(a, wp); + } + fclose(wp); + + //apply bspatch to the unpacked kernel parts + + { + // We write the decoded output to ".patch". + //allocate some extra space to allow for concatinating ".patch" with the name + outname = (char *)SS_Malloc(strlen(source_filename) + 10); + if (outname == NULL) { + SS_SetUpgradeState(E_SS_MALLOC_ERROR); + result = E_SS_FAILURE; + goto Cleanup; + } + snprintf(outname, strlen(source_filename) + 10, "%s.patch", + source_filename); + + output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (output < 0) { + strerror_r(errno, err_buf, sizeof(err_buf)); + LOGE("failed to open output file %s: %s\n", outname, err_buf); + result = E_SS_FAILURE; + goto Cleanup; + } + sink = ss_fileSink; + tok = &output; + } + result = SS_ApplyBsdiff(source_filename, outname, part_filename, sink, tok, NULL); + LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); + if (output >= 0) { + fsync(output); + close(output); + } + + if (result != S_SS_SUCCESS) { + LOGE("applying patch failed %s\n", source_filename); + if (outname != NULL) + unlink(outname); + goto Cleanup; + } + result = rename(outname, source_filename); + if (result != S_SS_SUCCESS) { + LOGE("fatal error %s\n", source_filename); + goto Cleanup; + } + if (file_name && strcmp(file_name, "piggy") == 0) { + snprintf(cmd, sizeof(cmd) - 1, + "%s/gzip -n -9 -c %s/%s/%s > %s/%s/%s.gz", + SS_KERNEL_WORKSPACE, SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, file_name, + SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, file_name); + result = _system_cmd_wait(cmd); + LOGL(LOG_SSENGINE, "ret for %s = %d\n", cmd, result); + unlink(source_filename); + } + unlink(part_filename); + } + //open new kernel file and append kernel parts to it in + snprintf(source_filename, sizeof(source_filename) - 1, "%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_TARGET_NAME); + kp = fopen(source_filename, "w"); + for (i = 0; i < 4; i++) { + snprintf(part_filename, sizeof(part_filename) - 1, "%s/%s/%s", SS_KERNEL_WORKSPACE, SS_KERNEL_UNPACK_DIR, + kernel_parts[i]); + wp = fopen(part_filename, "r"); + fseek(wp, SEEK_SET, SEEK_END); + j = ftell(wp); + fseek(wp, SEEK_SET, SEEK_SET); + while (j-- > 0) { + a = fgetc(wp); + if (a != EOF) + fputc(a, kp); + else + break; + } + fclose(wp); + unlink(part_filename); + } + fclose(fp); + fp = NULL; + fclose(kp); + + if (SS_LoadFile(source_filename, &source_file) == 0) + result = memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE); + if (result != S_SS_SUCCESS) { + LOGE("patch did not produce expected sha1 \n"); + SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); + goto Cleanup; + } + //Considering EMMC partition by deafult + + blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; + result = write_to_blkdev((char *)ua_dataSS->parti_info->ua_blk_name, blk_start, blk_cnt, (char *)source_file.data); + if (result != S_SS_SUCCESS) { + LOGE("write of patched data to %s failed\n", ua_dataSS->parti_info->ua_blk_name); // All returns should go to CLEAN UP. + SS_SetUpgradeState(E_SS_IMGFLASHWRITEFAIL); + goto Cleanup; + } Cleanup: - SS_BackupSourceClear(); - SS_PatchSourceClear(); - SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); - SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); - SS_Free(buf); - SS_Free(outname);//wgid: 20740 - if (result == S_SS_SUCCESS) - LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaKernel SUCCESS *****************\n"); - else{ - LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaKernel FAILED *****************\n"); - if(fp) - fclose(fp);//wgid:14711 - } - return result; + SS_BackupSourceClear(); + SS_PatchSourceClear(); + SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); + SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); + SS_Free(buf); + SS_Free(outname);//wgid: 20740 + if (result == S_SS_SUCCESS) + LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaKernel SUCCESS *****************\n"); + else{ + LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaKernel FAILED *****************\n"); + if (fp) + fclose(fp);//wgid:14711 + } + return result; } /*! ********************************************************************************* - * SS_UpdateDeltaIMG + * SS_UpdateDeltaIMG ********************************************************************************* * * @brief @@ -1106,146 +1096,145 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * int SS_UpdateDeltaIMG(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char *, int, int, char *)) { - uint8_t target_sha1[SHA_DIGEST_SIZE]; - uint8_t source_sha1[SHA_DIGEST_SIZE]; - const uint8_t current_target_sha1[SHA_DIGEST_SIZE]; - FileInfo source_file; - sha1_ctx_t ctx1; - MemorySinkInfo msi; - int result = S_SS_SUCCESS; - int blk_cnt; - int blk_start = 0; - int backupsrc = -1; - int use_backup_img = -1; - int fd = -1; - char buf[256]; - if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); - return E_SS_FAILURE; - } - - source_file.size = ua_dataSS->update_cfg->soure_img_size; - source_file.data = NULL; - if (0 == gvalid_session) { - - if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { - LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); - return E_SS_FAILURE; - } - - if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Can be applied\n"); - SS_Free(source_file.data); - } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Already applied\n"); - SS_Free(source_file.data); - return S_SS_SUCCESS; - } else { - SS_Free(source_file.data); - source_file.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Source was currupted, Try loading from backup source\n"); - if (SS_LoadPartition(SS_BACKUP_SOURCE, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - use_backup_img = 1; - SS_Free(source_file.data); - LOGL(LOG_SSENGINE, - "SS_UpdateDeltaIMG - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); - SS_Free(source_file.data); - return E_SS_FAILURE; - } - } - } - } - } - if (use_backup_img == -1) { - backupsrc = SS_BackupSource(ua_dataSS->parti_info->ua_blk_name); - if (backupsrc != 0) { - LOGE("failed to Backup source File:[%s] \n", ua_dataSS->parti_info->ua_blk_name); - SS_SetUpgradeState(E_SS_IMGSRCBACKUPFAILED); - return E_SS_FAILURE; - } - } - SinkFn sink = NULL; - void *token = NULL; - - blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; - - msi.buffer = SS_Malloc(blk_cnt * SECTOR_SIZE); - if (msi.buffer == NULL) { - LOGE("failed to alloc %ld bytes for output\n", (long)ua_dataSS->update_cfg->target_img_size); - SS_SetUpgradeState(E_SS_MALLOC_ERROR); - return E_SS_FAILURE; - } - msi.pos = 0; - msi.size = ua_dataSS->update_cfg->target_img_size; - sink = ss_memorySink; - token = &msi; - - sha1_init(&ctx1); - //if souce was corrupted, use backup to apply diff - if (use_backup_img == -1) - result = - SS_ApplyBsdiff((char *)ua_dataSS->parti_info->ua_blk_name, NULL, SS_PATCHFILE_SOURCE, sink, token, &ctx1); - else - result = SS_ApplyBsdiff(SS_BACKUP_SOURCE, NULL, SS_PATCHFILE_SOURCE, sink, token, &ctx1); - if (result != S_SS_SUCCESS) { - LOGE("failed to SS_ApplyBsdiff\n"); - SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); - goto Cleanup; - } - - sha1_final(&ctx1, (uint32_t *) & current_target_sha1); - result = memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE); - if (result != S_SS_SUCCESS) { - LOGE("patch did not produce expected sha1 \n"); - SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); - goto Cleanup; - } - //Considering EMMC partition by deafult - - if (ua_dataSS->update_cfg->update_type == DELTA_IMG) { - blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; - result = write_to_blkdev((char *)ua_dataSS->parti_info->ua_blk_name, blk_start, blk_cnt, (char *)msi.buffer); - if (result != S_SS_SUCCESS) { - LOGE("write of patched data to %s failed\n", ua_dataSS->parti_info->ua_blk_name); // All returns should go to CLEAN UP. - SS_SetUpgradeState(E_SS_IMGFLASHWRITEFAIL); - goto Cleanup; - } - } else if (ua_dataSS->update_cfg->update_type == EXTRA && ua_dataSS->update_data->ua_temp_path) { - fd = open(ua_dataSS->update_data->ua_temp_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (fd < 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to open %s for write: %s\n", ua_dataSS->update_data->ua_temp_path, buf); - SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); - result = E_SS_FAILURE; - goto Cleanup; - } - result = SS_WriteFile(NULL, fd, 0, msi.buffer, msi.size); - if (result != S_SS_SUCCESS) { - LOGE("failed to write\n"); - SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); - goto Cleanup; - } - fsync(fd); - } - else { - SS_SetUpgradeState(E_SS_IMGUPDATEFAILED); - result = E_SS_FAILURE; - LOGE("failed to apply patch - Invalid Update type params \n"); - } - - Cleanup: - SS_BackupSourceClear(); - SS_PatchSourceClear(); - if (msi.buffer) - SS_Free(msi.buffer); - if (fd >= 0) - close(fd); - if (result == S_SS_SUCCESS) - LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaIMG SUCCESS *****************\n"); - return result; + uint8_t target_sha1[SHA_DIGEST_SIZE]; + uint8_t source_sha1[SHA_DIGEST_SIZE]; + const uint8_t current_target_sha1[SHA_DIGEST_SIZE]; + FileInfo source_file; + sha1_ctx_t ctx1; + MemorySinkInfo msi; + int result = S_SS_SUCCESS; + int blk_cnt; + int blk_start = 0; + int backupsrc = -1; + int use_backup_img = -1; + int fd = -1; + char buf[256]; + if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); + return E_SS_FAILURE; + } + + source_file.size = ua_dataSS->update_cfg->soure_img_size; + source_file.data = NULL; + if (0 == gvalid_session) { + + if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { + LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); + return E_SS_FAILURE; + } + + if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Can be applied\n"); + SS_Free(source_file.data); + } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Patch Already applied\n"); + SS_Free(source_file.data); + return S_SS_SUCCESS; + } else { + SS_Free(source_file.data); + source_file.data = NULL; + LOGL(LOG_SSENGINE, "SS_UpdateDeltaIMG - Source was currupted, Try loading from backup source\n"); + if (SS_LoadPartition(SS_BACKUP_SOURCE, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + use_backup_img = 1; + SS_Free(source_file.data); + LOGL(LOG_SSENGINE, + "SS_UpdateDeltaIMG - Patch Can be applied from using backup file as source\n"); + } else { + SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); + SS_Free(source_file.data); + return E_SS_FAILURE; + } + } + } + } + } + if (use_backup_img == -1) { + backupsrc = SS_BackupSource(ua_dataSS->parti_info->ua_blk_name); + if (backupsrc != 0) { + LOGE("failed to Backup source File:[%s] \n", ua_dataSS->parti_info->ua_blk_name); + SS_SetUpgradeState(E_SS_IMGSRCBACKUPFAILED); + return E_SS_FAILURE; + } + } + SinkFn sink = NULL; + void *token = NULL; + + blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; + + msi.buffer = SS_Malloc(blk_cnt * SECTOR_SIZE); + if (msi.buffer == NULL) { + LOGE("failed to alloc %ld bytes for output\n", (long)ua_dataSS->update_cfg->target_img_size); + SS_SetUpgradeState(E_SS_MALLOC_ERROR); + return E_SS_FAILURE; + } + msi.pos = 0; + msi.size = ua_dataSS->update_cfg->target_img_size; + sink = ss_memorySink; + token = &msi; + + sha1_init(&ctx1); + //if souce was corrupted, use backup to apply diff + if (use_backup_img == -1) + result = + SS_ApplyBsdiff((char *)ua_dataSS->parti_info->ua_blk_name, NULL, SS_PATCHFILE_SOURCE, sink, token, &ctx1); + else + result = SS_ApplyBsdiff(SS_BACKUP_SOURCE, NULL, SS_PATCHFILE_SOURCE, sink, token, &ctx1); + if (result != S_SS_SUCCESS) { + LOGE("failed to SS_ApplyBsdiff\n"); + SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); + goto Cleanup; + } + + sha1_final(&ctx1, (uint32_t *) & current_target_sha1); + result = memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE); + if (result != S_SS_SUCCESS) { + LOGE("patch did not produce expected sha1 \n"); + SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); + goto Cleanup; + } + //Considering EMMC partition by deafult + + if (ua_dataSS->update_cfg->update_type == DELTA_IMG) { + blk_cnt = ((ua_dataSS->update_cfg->target_img_size - 1) / SECTOR_SIZE) + 1; + result = write_to_blkdev((char *)ua_dataSS->parti_info->ua_blk_name, blk_start, blk_cnt, (char *)msi.buffer); + if (result != S_SS_SUCCESS) { + LOGE("write of patched data to %s failed\n", ua_dataSS->parti_info->ua_blk_name); // All returns should go to CLEAN UP. + SS_SetUpgradeState(E_SS_IMGFLASHWRITEFAIL); + goto Cleanup; + } + } else if (ua_dataSS->update_cfg->update_type == EXTRA && ua_dataSS->update_data->ua_temp_path) { + fd = open(ua_dataSS->update_data->ua_temp_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); + if (fd < 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to open %s for write: %s\n", ua_dataSS->update_data->ua_temp_path, buf); + SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); + result = E_SS_FAILURE; + goto Cleanup; + } + result = SS_WriteFile(NULL, fd, 0, msi.buffer, msi.size); + if (result != S_SS_SUCCESS) { + LOGE("failed to write\n"); + SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); + goto Cleanup; + } + fsync(fd); + } else { + SS_SetUpgradeState(E_SS_IMGUPDATEFAILED); + result = E_SS_FAILURE; + LOGE("failed to apply patch - Invalid Update type params \n"); + } + +Cleanup: + SS_BackupSourceClear(); + SS_PatchSourceClear(); + if (msi.buffer) + SS_Free(msi.buffer); + if (fd >= 0) + close(fd); + if (result == S_SS_SUCCESS) + LOGL(LOG_SSENGINE, "************* SS_UpdateDeltaIMG SUCCESS *****************\n"); + return result; } diff --git a/ss_engine/SS_PatchDelta.h b/ss_engine/SS_PatchDelta.h index db54078..fd3bb66 100755 --- a/ss_engine/SS_PatchDelta.h +++ b/ss_engine/SS_PatchDelta.h @@ -31,20 +31,20 @@ #define SS_UPDATE_IMG 1 //#define SHA_DIGEST_SIZE 20 typedef struct { - int type; - ssize_t size; - char *data; + int type; + ssize_t size; + char *data; } Value; typedef struct _Patch { - uint8_t sha1[SHA_DIGEST_SIZE]; - const char *patch_filename; + uint8_t sha1[SHA_DIGEST_SIZE]; + const char *patch_filename; } Patch; typedef struct _FileInfo { - unsigned char sha1[20]; //SHA_DIGEST_SIZE 20 - unsigned char *data; - int size; - struct stat st; + unsigned char sha1[20]; //SHA_DIGEST_SIZE 20 + unsigned char *data; + int size; + struct stat st; } FileInfo; typedef ssize_t(*SinkFn) (unsigned char *, ssize_t, void *); @@ -53,11 +53,11 @@ int ParseSha1(const char *str, uint8_t * digest); void ShowBSDiffLicense(); int ApplyBSDiffPatch(const unsigned char *old_data, ssize_t old_size, - const Value * patch, ssize_t patch_offset, SinkFn sink, void *token, sha1_ctx_t * ctx1); + const Value * patch, ssize_t patch_offset, SinkFn sink, void *token, sha1_ctx_t * ctx1); int ApplyBSDiffPatchMem(const unsigned char *old_data, ssize_t old_size, - const Value * patch, ssize_t patch_offset, unsigned char **new_data, ssize_t * new_size); + const Value * patch, ssize_t patch_offset, unsigned char **new_data, ssize_t * new_size); //int ApplyOptimizedBSDiffPatch(const unsigned char* old_data,void* token, -// const Value* patch, SinkFn sink,sha1_ctx_t* ctx1); +// const Value* patch, SinkFn sink,sha1_ctx_t* ctx1); int SS_LoadPartition(const char *filename, FileInfo * file); int SS_LoadFile(const char *filename, FileInfo * file); @@ -68,7 +68,7 @@ extern int SS_ApplyBsdiff(char *oldfile, char *newfile, char *patch, SinkFn sink extern int SS_BackupSourceClear(); extern int SS_PatchSourceClear(); extern long SS_WriteFile(void *pbUserData, - long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); + long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); extern void SS_Free(void * pMemBlock); extern long SS_CopyFile(void * pbUserData, const char * strFromPath, const char * strToPath); extern long SS_DeleteFolder(void * pbUserData, const char * strPath); diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index be6bbcd..aef89db 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -42,7 +42,7 @@ Function Prototypes Mandatory int gtotalFSCnt = 0; int FS_UpgradeState = E_SS_FAILURE; -int gvalid_session = 0; //used as fail-safe in case device is turmed off or battery removed during update +int gvalid_session = 0; //used as fail-safe in case device is turmed off or battery removed during update fs_list *fs_headptr_main = NULL; fs_list *headptr_list[UA_PARTI_MAX]; tar_Data_t *tar_cfg_data = NULL; @@ -60,38 +60,38 @@ tar_Data_t *tar_cfg_data = NULL; int mem_profiling_start = 0; int SS_Do_Memory_Profiling() { - int ret = -1; - pid_t pid; - char memory_usage_script[1024] = "#!/bin/bash\nlog_file=$1\npid=$2\nmaxmem=0\nwhile [[ -d \"/proc/${pid}\" ]]; do\n\ - mem=`cat /proc/${pid}/smaps | grep Pss | grep -v Swap|awk '{print $2}'|awk '{s+=$1} END {print s}'`\n\ - if [[ ${mem} -gt ${maxmem} ]]; then\n maxmem=${mem}\n\ - echo -e \"Memory usage till now is: ${maxmem} KB.\" >> $log_file\n fi\n sleep 0.01\ndone\n\ - echo -e \"Max was : ${maxmem} KB.\" >> $log_file\n"; - char cmd[1024] = { 0, }; - - //Open a file and write the script contents in it - FILE *fp = fopen(SS_MEMORY_PROFILING_SCRIPT, "w+"); - fwrite(memory_usage_script, strlen(memory_usage_script), 1, fp); - fclose(fp); - //make the file executable - Octal 495 is 757 decimal - if (chmod(SS_MEMORY_PROFILING_SCRIPT, 495) < 0) { - LOGE("Error in chmod(%s, 495) - %d (%s)\n", SS_MEMORY_PROFILING_SCRIPT, errno, strerror(errno)); - return E_SS_FAILURE; - } - //calling mem_use.sh - //Usage : - pid = getpid(); - snprintf(cmd, sizeof(cmd) - 1, "%s %s %d", SS_MEMORY_PROFILING_SCRIPT, SS_MEMORY_USAGE_LOG, pid); - ret = _system_cmd_nowait(cmd); - sleep(1); - LOG("ret for memory profiling cmd is %d\n", ret); - if (ret == 0) { - mem_profiling_start = 1; - return S_SS_SUCCESS; - } else { - LOGE("Could not start Memory Profiling\n"); - return E_SS_FAILURE; - } + int ret = -1; + pid_t pid; + char memory_usage_script[1024] = "#!/bin/bash\nlog_file=$1\npid=$2\nmaxmem=0\nwhile [[ -d \"/proc/${pid}\" ]]; do\n\ + mem=`cat /proc/${pid}/smaps | grep Pss | grep -v Swap|awk '{print $2}'|awk '{s+=$1} END {print s}'`\n\ + if [[ ${mem} -gt ${maxmem} ]]; then\nmaxmem=${mem}\n\ + echo -e \"Memory usage till now is: ${maxmem} KB.\" >> $log_file\n fi\n sleep 0.01\ndone\n\ + echo -e \"Max was : ${maxmem} KB.\" >> $log_file\n"; + char cmd[1024] = { 0, }; + + //Open a file and write the script contents in it + FILE *fp = fopen(SS_MEMORY_PROFILING_SCRIPT, "w+"); + fwrite(memory_usage_script, strlen(memory_usage_script), 1, fp); + fclose(fp); + //make the file executable - Octal 495 is 757 decimal + if (chmod(SS_MEMORY_PROFILING_SCRIPT, 495) < 0) { + LOGE("Error in chmod(%s, 495) - %d (%s)\n", SS_MEMORY_PROFILING_SCRIPT, errno, strerror(errno)); + return E_SS_FAILURE; + } + //calling mem_use.sh + //Usage : + pid = getpid(); + snprintf(cmd, sizeof(cmd) - 1, "%s %s %d", SS_MEMORY_PROFILING_SCRIPT, SS_MEMORY_USAGE_LOG, pid); + ret = _system_cmd_nowait(cmd); + sleep(1); + LOG("ret for memory profiling cmd is %d\n", ret); + if (ret == 0) { + mem_profiling_start = 1; + return S_SS_SUCCESS; + } else { + LOGE("Could not start Memory Profiling\n"); + return E_SS_FAILURE; + } } #endif @@ -104,264 +104,261 @@ double SS_FSBuildNodes_time = 0.0; static void get_time_stamp1(void) { - struct timeval tv; - int sec, msec; + struct timeval tv; + int sec, msec; - gettimeofday(&tv, NULL); - sec = (int)tv.tv_sec; - msec = (int)(tv.tv_usec / 1000); - snprintf(ts1, 256, "%06d.%03d", sec % 100000, msec); + gettimeofday(&tv, NULL); + sec = (int)tv.tv_sec; + msec = (int)(tv.tv_usec / 1000); + snprintf(ts1, 256, "%06d.%03d", sec % 100000, msec); } static double get_time_stamp2(void) { - struct timeval tv; + struct timeval tv; - gettimeofday(&tv, NULL); - ts2 = (double)tv.tv_sec + (double)(tv.tv_usec / 1000000.0); - return ts2; + gettimeofday(&tv, NULL); + ts2 = (double)tv.tv_sec + (double)(tv.tv_usec / 1000000.0); + return ts2; } #endif //Check SS function if available int file_exist(char *filename, int type) { - struct stat buf; - int ret = 0; - - ret = lstat(filename, &buf); - if (ret < 0) { - ret = stat(filename, &buf); - } - return (ret >= 0) ? (1) : (0); + struct stat buf; + int ret = 0; + + ret = lstat(filename, &buf); + if (ret < 0) + ret = stat(filename, &buf); + return (ret >= 0) ? (1) : (0); } long SS_GetUPIVersion(unsigned char *ver_str) { - if (ver_str) { - strncpy((char *)ver_str, SS_TOTA_VERSION, MAX_PATH); + if (ver_str) { + strncpy((char *)ver_str, SS_TOTA_VERSION, MAX_PATH); #ifdef MEM_PROFILING - if (!mem_profiling_start) - if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling())) - LOGE("Unable to start Memory_Profiling"); + if (!mem_profiling_start) + if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling())) + LOGE("Unable to start Memory_Profiling"); #endif - return S_SS_SUCCESS;//wgid: 2456 - } else - return E_SS_FAILURE; + return S_SS_SUCCESS;//wgid: 2456 + } else + return E_SS_FAILURE; } int SS_CalculateFileSha(char *filename, int filesize, FileInfo * file) { - FILE *fp = NULL; - int ulResult = S_SS_SUCCESS; - int chunk = 20*1024*1024; - char buf[256]; - - fp = fopen(filename, "rb"); - if (fp == NULL) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to open \"%s\": %s\n", filename, buf); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - file->data = SS_Malloc(chunk); - if (!file->data) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to allocate memory for \"%s\": %s\n", filename, buf); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - ssize_t bytes_read = 0; - sha1_ctx_t sha_ctx; - sha1_init(&sha_ctx); - - while( filesize > 0){ - if(filesize < chunk){ - bytes_read = fread(file->data, 1, filesize, fp); - if (bytes_read != filesize) { - LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - sha1_update(&sha_ctx, file->data, filesize); - break; - } - else { - bytes_read = fread(file->data, 1, chunk, fp); - if (bytes_read != chunk) { - LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - sha1_update(&sha_ctx, file->data, chunk); - filesize -= chunk; - } - } - - sha1_final(&sha_ctx, (uint32_t *) &file->sha1); + FILE *fp = NULL; + int ulResult = S_SS_SUCCESS; + int chunk = 20*1024*1024; + char buf[256]; + + fp = fopen(filename, "rb"); + if (fp == NULL) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to open \"%s\": %s\n", filename, buf); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + file->data = SS_Malloc(chunk); + if (!file->data) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to allocate memory for \"%s\": %s\n", filename, buf); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + ssize_t bytes_read = 0; + sha1_ctx_t sha_ctx; + sha1_init(&sha_ctx); + + while (filesize > 0) { + if (filesize < chunk) { + bytes_read = fread(file->data, 1, filesize, fp); + if (bytes_read != filesize) { + LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + sha1_update(&sha_ctx, file->data, filesize); + break; + } else { + bytes_read = fread(file->data, 1, chunk, fp); + if (bytes_read != chunk) { + LOGE("short read of \"%s\" (%ld bytes of %ld)\n", filename, (long)bytes_read, (long)file->size); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + sha1_update(&sha_ctx, file->data, chunk); + filesize -= chunk; + } + } + + sha1_final(&sha_ctx, (uint32_t *) &file->sha1); Cleanup: - if(fp) - fclose(fp); - if(file->data) - SS_Free(file->data); - return ulResult; + if (fp) + fclose(fp); + if (file->data) + SS_Free(file->data); + return ulResult; } int SS_verify_DELTA_image(char *filename) { - FileInfo file; - FILE *fp = NULL; - char line[SS_TOKEN_MAXLINE_LEN] = { '\0' }; - char * delta_size = NULL; - char *signature = NULL; - char * sha1trg = NULL; - char *saveptr; - uint8_t target_sha1[SHA_DIGEST_SIZE] = { 0, }; - char cmd[512] = { 0, }; - char buf[256]; - int udelta_size = 0; - int ulResult = S_SS_SUCCESS; - - if (stat(filename, &file.st) != 0) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("failed to stat \"%s\": %s\n", filename, buf); - return -1; - } - - snprintf(cmd, sizeof(cmd) - 1, "grep -o -P '%s' --binary-files=text system/%s > %s", - SS_IMAGE_MAGIC_KEY,filename, SS_IMAGE_MAGIC_KEY_VAL); - - ulResult = _system_cmd_wait(cmd); - if (ulResult != S_SS_SUCCESS) { - LOGE("Grep extraction for [%s] failed, code [%d]\n", cmd, ulResult); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - fp = fopen(SS_IMAGE_MAGIC_KEY_VAL, "r"); - if (!fp) { - LOGE("Grep extraction for [%s] failed, code [%d]\n", cmd, ulResult); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL){ - ulResult = E_SS_FAILURE; - goto Cleanup; - } - fclose(fp); - fp = NULL; - - signature = strtok_r(line, SS_TOEKN_COLON, &saveptr); - delta_size = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - sha1trg = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - - if (signature && sha1trg && delta_size) { - udelta_size = atoi(delta_size); + FileInfo file; + FILE *fp = NULL; + char line[SS_TOKEN_MAXLINE_LEN] = { '\0' }; + char * delta_size = NULL; + char *signature = NULL; + char * sha1trg = NULL; + char *saveptr; + uint8_t target_sha1[SHA_DIGEST_SIZE] = { 0, }; + char cmd[512] = { 0, }; + char buf[256]; + int udelta_size = 0; + int ulResult = S_SS_SUCCESS; + + if (stat(filename, &file.st) != 0) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("failed to stat \"%s\": %s\n", filename, buf); + return -1; + } + + snprintf(cmd, sizeof(cmd) - 1, "grep -o -P '%s' --binary-files=text system/%s > %s", + SS_IMAGE_MAGIC_KEY, filename, SS_IMAGE_MAGIC_KEY_VAL); + + ulResult = _system_cmd_wait(cmd); + if (ulResult != S_SS_SUCCESS) { + LOGE("Grep extraction for [%s] failed, code [%d]\n", cmd, ulResult); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + fp = fopen(SS_IMAGE_MAGIC_KEY_VAL, "r"); + if (!fp) { + LOGE("Grep extraction for [%s] failed, code [%d]\n", cmd, ulResult); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { + ulResult = E_SS_FAILURE; + goto Cleanup; + } + fclose(fp); + fp = NULL; + + signature = strtok_r(line, SS_TOEKN_COLON, &saveptr); + delta_size = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + sha1trg = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + + if (signature && sha1trg && delta_size) { + udelta_size = atoi(delta_size); if (udelta_size < 0) - LOGE("Invalid udelta_size %d (%s)\n", udelta_size, delta_size); - LOGL(LOG_SSENGINE, "delta_size %d sha1trg %s\n", udelta_size, sha1trg); - } - else { - LOGE("Could not parse signature [%s]\n", line); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - if (ParseSha1(sha1trg, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", sha1trg); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - ulResult = SS_CalculateFileSha(filename, udelta_size, &file); - if (ulResult != S_SS_SUCCESS) - goto Cleanup; - - if(memcmp(file.sha1, target_sha1, SHA_DIGEST_SIZE) != 0){ - LOGE("SHA mismatch -[%s] actual [%x] target [%x]\n", filename, file.sha1, target_sha1); - SS_SetUpgradeState(E_SS_FSBADDELTA); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - else - LOGL(LOG_SSENGINE, "DELTA verified %s \n", sha1trg); + LOGE("Invalid udelta_size %d (%s)\n", udelta_size, delta_size); + LOGL(LOG_SSENGINE, "delta_size %d sha1trg %s\n", udelta_size, sha1trg); + } else { + LOGE("Could not parse signature [%s]\n", line); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + if (ParseSha1(sha1trg, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", sha1trg); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + ulResult = SS_CalculateFileSha(filename, udelta_size, &file); + if (ulResult != S_SS_SUCCESS) + goto Cleanup; + + if (memcmp(file.sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { + LOGE("SHA mismatch -[%s] actual [%x] target [%x]\n", filename, file.sha1, target_sha1); + SS_SetUpgradeState(E_SS_FSBADDELTA); + ulResult = E_SS_FAILURE; + goto Cleanup; + } else { + LOGL(LOG_SSENGINE, "DELTA verified %s \n", sha1trg); + } Cleanup: - if(fp) - fclose(fp); - if(file_exist(SS_IMAGE_MAGIC_KEY_VAL, 0)) - SS_DeleteFile(NULL, SS_IMAGE_MAGIC_KEY_VAL); - return ulResult; + if (fp) + fclose(fp); + if (file_exist(SS_IMAGE_MAGIC_KEY_VAL, 0)) + SS_DeleteFile(NULL, SS_IMAGE_MAGIC_KEY_VAL); + return ulResult; } int SS_GetProgressResolution(int ultotalFSCnt) { - if (ultotalFSCnt < DISPLAYRESOLUTION_SIZE) - return 1; - else - return (ultotalFSCnt / DISPLAYRESOLUTION_SIZE); + if (ultotalFSCnt < DISPLAYRESOLUTION_SIZE) + return 1; + else + return (ultotalFSCnt / DISPLAYRESOLUTION_SIZE); } void SS_SetUpgradeState(int Val) { - LOGE("FAILED to upgrade Cause:[0x%x]\n", Val); - FS_UpgradeState = Val; - return; + LOGE("FAILED to upgrade Cause:[0x%x]\n", Val); + FS_UpgradeState = Val; + return; } int SS_GetUpgradeState() { - return FS_UpgradeState; + return FS_UpgradeState; } int SS_rename(const char *old_file_name, const char *new_file_name) { - if (link(old_file_name, new_file_name) < 0) { - if (errno == EEXIST) { - if (unlink(new_file_name) < 0 || link(old_file_name, new_file_name) < 0) - return -1; - } else - return -1; - } - if (unlink(old_file_name) < 0) { - if (unlink(new_file_name) == 0) - return -1; - } - return 0; + if (link(old_file_name, new_file_name) < 0) { + if (errno == EEXIST) { + if (unlink(new_file_name) < 0 || link(old_file_name, new_file_name) < 0) + return -1; + } else + return -1; + } + if (unlink(old_file_name) < 0) { + if (unlink(new_file_name) == 0) + return -1; + } + return 0; } int SS_rename1(const char *old_file_name, const char *new_file_name) { - int result = E_SS_FAILURE; - char *temp_name = NULL; - temp_name = (char *)SS_Malloc(strlen(new_file_name) + 10); - if (temp_name == NULL) - return E_SS_FAILURE; - snprintf(temp_name, strlen(new_file_name) + 10, "%s.temp", new_file_name); - result = rename(new_file_name, temp_name); - if (result != 0) - goto Cleanup; - result = rename(old_file_name, new_file_name); - if (result != 0) - goto Cleanup; - result = unlink(temp_name); + int result = E_SS_FAILURE; + char *temp_name = NULL; + temp_name = (char *)SS_Malloc(strlen(new_file_name) + 10); + if (temp_name == NULL) + return E_SS_FAILURE; + snprintf(temp_name, strlen(new_file_name) + 10, "%s.temp", new_file_name); + result = rename(new_file_name, temp_name); + if (result != 0) + goto Cleanup; + result = rename(old_file_name, new_file_name); + if (result != 0) + goto Cleanup; + result = unlink(temp_name); Cleanup: - if (temp_name) - SS_Free(temp_name); - return result; + if (temp_name) + SS_Free(temp_name); + return result; } /*! ********************************************************************************* - * SS_FSVerifyNode + * SS_FSVerifyNode ********************************************************************************* * * @brief @@ -377,71 +374,72 @@ int SS_rename1(const char *old_file_name, const char *new_file_name) ********************************************************************************* */ int SS_FSVerifyNode(const char *ubDeltaPath, const char *path, const char *patchname, const char *sha1src, int type, - char *patchpath_name, int *data_size, int *data_offset) + char *patchpath_name, int *data_size, int *data_offset) { - char patch[MAX_FILE_PATH] = { 0 }; - FileInfo source_file; - uint8_t source_sha1[SHA_DIGEST_SIZE]; - - if (gvalid_session) { - if ((type == DIFFS || type == DELETES || type == MOVES) && !file_exist((char *)path, type)) { - LOGE("failed to verifyNodes [does not exist], Path : [%s] Type[%d]\n", path, type); - SS_SetUpgradeState(E_SS_FSBADNODES); - return E_SS_FAILURE; - } - snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname); - if ((type == DIFFS /*||type == NEWFILES */ )) // allowing size 0 also for folders - { - if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset) != S_SS_SUCCESS) { - LOGE("failed to get item size from struct, Patch : [%s]\n", patchname); - SS_SetUpgradeState(E_SS_FSBADNODES); - return E_SS_FAILURE; - } - if (*data_size < 0) { - LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type); - SS_SetUpgradeState(E_SS_FSBADNODES); - return E_SS_FAILURE; - } - } - //For other types (NEWs, SYMs, Folders), SHA check not required - if ((type == DIFFS || type == MOVES /* ||type == DELETES */ ) && (strcmp(sha1src, SS_NULLENTRY) != 0)) { - if (ParseSha1(sha1src, source_sha1) != 0) { - LOGE("Failed to parse Src-sha1 \"%s\"\n", sha1src); - return E_SS_FAILURE; - } - if (SS_LoadFile(path, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) != 0) { - SS_Free(source_file.data); - LOGE("SS_FSVerifyNode - SHA mismatch with SRC - PATH [%s]\n", path); - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); // Define other error - return E_SS_FAILURE; - } - } - SS_Free(source_file.data); - } - } else { - LOGL(LOG_SSENGINE, "FS partition Already verified - Filling only size and offset \n"); - snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname); - if ((type == DIFFS /*||type == NEWFILES */ )) // allowing size 0 also for folders - { - if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset) != S_SS_SUCCESS) { - LOGE("failed to get item size from struct, Patch : [%s] \n", patchname); - SS_SetUpgradeState(E_SS_FSBADNODES); - return E_SS_FAILURE; - } - if (*data_size < 0) { - LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type); - SS_SetUpgradeState(E_SS_FSBADNODES); - return E_SS_FAILURE; - } - } - } - return S_SS_SUCCESS; + char patch[MAX_FILE_PATH] = { 0 }; + FileInfo source_file; + uint8_t source_sha1[SHA_DIGEST_SIZE]; + + if (gvalid_session) { + if ((type == DIFFS || type == DELETES || type == MOVES) && !file_exist((char *)path, type)) { + LOGE("failed to verifyNodes [does not exist], Path : [%s] Type[%d]\n", path, type); + SS_SetUpgradeState(E_SS_FSBADNODES); + return E_SS_FAILURE; + } + snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname); + if ((type == DIFFS/*||type == NEWFILES */)) { // allowing size 0 also for folders + if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset) + != S_SS_SUCCESS) { + LOGE("failed to get item size from struct, Patch : [%s]\n", patchname); + SS_SetUpgradeState(E_SS_FSBADNODES); + return E_SS_FAILURE; + } + if (*data_size < 0) { + LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type); + SS_SetUpgradeState(E_SS_FSBADNODES); + return E_SS_FAILURE; + } + } + //For other types (NEWs, SYMs, Folders), SHA check not required + if ((type == DIFFS || type == MOVES/* ||type == DELETES */) && + (strcmp(sha1src, SS_NULLENTRY) != 0)) { + if (ParseSha1(sha1src, source_sha1) != 0) { + LOGE("Failed to parse Src-sha1 \"%s\"\n", sha1src); + return E_SS_FAILURE; + } + if (SS_LoadFile(path, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) != 0) { + SS_Free(source_file.data); + LOGE("SS_FSVerifyNode - SHA mismatch with SRC - PATH [%s]\n", path); + SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); // Define other error + return E_SS_FAILURE; + } + } + SS_Free(source_file.data); + } + } else { + LOGL(LOG_SSENGINE, "FS partition Already verified - Filling only size and offset \n"); + snprintf(patch, MAX_FILE_PATH, "%s%s%s", patchpath_name, "/", patchname); + if ((type == DIFFS/* ||type == NEWFILES */)) { // allowing size 0 also for folders + if (tar_get_item_size_from_struct(&tar_cfg_data, patchname, data_size, data_offset) + != S_SS_SUCCESS) { + LOGE("failed to get item size from struct, Patch : [%s] \n", patchname); + SS_SetUpgradeState(E_SS_FSBADNODES); + return E_SS_FAILURE; + } + if (*data_size < 0) { + LOGE("failed to verifyNodes [delta absent], Patch : [%s] Type[%d]\n", patch, type); + SS_SetUpgradeState(E_SS_FSBADNODES); + return E_SS_FAILURE; + } + } + } + return S_SS_SUCCESS; } /*! ********************************************************************************* - * SS_AppendNode + * SS_AppendNode ********************************************************************************* * * @brief @@ -456,84 +454,81 @@ int SS_FSVerifyNode(const char *ubDeltaPath, const char *path, const char *patch ********************************************************************************* */ int SS_AppendNode(const char *ubDeltaPath, fs_params ** headparam, fs_params ** tailparam, const char *old_path, - const char *new_path, const char *patchname, const char *sha1src, const char *sha1trg, int type, - char *patchpath_name) + const char *new_path, const char *patchname, const char *sha1src, const char *sha1trg, int type, + char *patchpath_name) { - fs_params *newnode = NULL; - int data_size = 0; - int data_offset = 0; - - if (!ubDeltaPath || !old_path || !new_path || !patchname || !sha1src || !sha1trg || !patchpath_name) { - LOGE("Bad Nodes, NULL params passed for Appending Nodes \n"); - SS_SetUpgradeState(E_SS_FSINVALIDNODEPARAMS); - return E_SS_FAILURE; - } - if ((E_SS_FAILURE == - SS_FSVerifyNode(ubDeltaPath, old_path, patchname, sha1src, type, patchpath_name, &data_size, &data_offset))) { - LOGE("Bad Nodes, Failed to pass verification - [Delta Path - %s][OldPath - %s] [NewPath - %s] \n", ubDeltaPath, - old_path, new_path); - return E_SS_FAILURE; - } - newnode = (fs_params *) SS_Malloc(sizeof(fs_params)); - if (!newnode) - return E_SS_FAILURE; - strncpy(newnode->file_old_path, old_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29483 - strncpy(newnode->file_new_path, new_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29482 - strncpy(newnode->patch_name, patchname, SS_MAX_NAMELENSUPPORTED);//wgid: 28033 - strncpy(newnode->sha1src, sha1src, sizeof(newnode->sha1src) -1);//wgid: 25282 - strncpy(newnode->sha1trg, sha1trg, sizeof(newnode->sha1trg) - 1);//wgid: 25283 - newnode->type = type; - newnode->data_size = data_size; - newnode->data_offset = data_offset; - newnode->nextnode = NULL; - - //LOG("%s %s %d %s %s \n",newnode->file_path,newnode->patch_name,newnode->type, newnode->sha1src, newnode->sha1trg); - - if (*headparam == NULL) { - *headparam = newnode; - *tailparam = newnode; - } else { - (*tailparam)->nextnode = newnode; - (*tailparam) = (*tailparam)->nextnode; - } - return S_SS_SUCCESS; + fs_params *newnode = NULL; + int data_size = 0; + int data_offset = 0; + + if (!ubDeltaPath || !old_path || !new_path || !patchname || !sha1src || !sha1trg || !patchpath_name) { + LOGE("Bad Nodes, NULL params passed for Appending Nodes \n"); + SS_SetUpgradeState(E_SS_FSINVALIDNODEPARAMS); + return E_SS_FAILURE; + } + if ((E_SS_FAILURE == + SS_FSVerifyNode(ubDeltaPath, old_path, patchname, sha1src, type, patchpath_name, &data_size, &data_offset))) { + LOGE("Bad Nodes, Failed to pass verification - [Delta Path - %s][OldPath - %s] [NewPath - %s] \n", ubDeltaPath, + old_path, new_path); + return E_SS_FAILURE; + } + newnode = (fs_params *) SS_Malloc(sizeof(fs_params)); + if (!newnode) + return E_SS_FAILURE; + strncpy(newnode->file_old_path, old_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29483 + strncpy(newnode->file_new_path, new_path, SS_MAX_NAMELENSUPPORTED);//wgid: 29482 + strncpy(newnode->patch_name, patchname, SS_MAX_NAMELENSUPPORTED);//wgid: 28033 + strncpy(newnode->sha1src, sha1src, sizeof(newnode->sha1src) -1);//wgid: 25282 + strncpy(newnode->sha1trg, sha1trg, sizeof(newnode->sha1trg) - 1);//wgid: 25283 + newnode->type = type; + newnode->data_size = data_size; + newnode->data_offset = data_offset; + newnode->nextnode = NULL; + + //LOG("%s %s %d %s %s \n",newnode->file_path,newnode->patch_name,newnode->type, newnode->sha1src, newnode->sha1trg); + + if (*headparam == NULL) { + *headparam = newnode; + *tailparam = newnode; + } else { + (*tailparam)->nextnode = newnode; + (*tailparam) = (*tailparam)->nextnode; + } + return S_SS_SUCCESS; } void SS_UpdateUIProgress(ua_dataSS_t * ua_dataSS, int ulTotalFsCnt, int ulDone) { - - static int ss_count = 1; - int res_val = SS_GetProgressResolution(ulTotalFsCnt); - if (!ua_dataSS) { - LOGE("Error ua_dataSS\n"); - return; - } + static int ss_count = 1; + int res_val = SS_GetProgressResolution(ulTotalFsCnt); + if (!ua_dataSS) { + LOGE("Error ua_dataSS\n"); + return; + } //LOG("\nvalues are ss_count[%d] total_file_cnt[%d]",ss_count,ulTotalFsCnt); - if (ulDone == 1) { - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, 100); - ss_count = 1; - } else if (ss_count < ulTotalFsCnt) { - - if (ss_count % res_val == 0 /* && ss_iterator <= 2 */ ) //Max 50 times display - { - double data = (double)ss_count / (double)ulTotalFsCnt; - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, data * 100); - } - ss_count++; - } else { - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, 100); - ss_count = 1; - } + if (ulDone == 1) { + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, 100); + ss_count = 1; + } else if (ss_count < ulTotalFsCnt) { + if (ss_count % res_val == 0) { //Max 50 times display + double data = (double)ss_count / (double)ulTotalFsCnt; + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, data * 100); + } + ss_count++; + } else { + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, 100); + ss_count = 1; + } } /*! ********************************************************************************* - * SS_FSClearNodes + * SS_FSClearNodes ********************************************************************************* * * @brief @@ -548,66 +543,66 @@ void SS_UpdateUIProgress(ua_dataSS_t * ua_dataSS, int ulTotalFsCnt, int ulDone) */ void SS_FSClearNodes(int idx) { - fs_params *local_temp = NULL; - fs_params *local_next = NULL; - LOGL(LOG_SSENGINE, "Free Nodes idx=%d \n", idx); - if (headptr_list[idx]) { - if (headptr_list[idx]->del_ref) { - local_temp = headptr_list[idx]->del_ref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - if (headptr_list[idx]->dif_ref) { - local_temp = headptr_list[idx]->dif_ref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - if (headptr_list[idx]->move_ref) { - local_temp = headptr_list[idx]->move_ref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - if (headptr_list[idx]->new_ref) { - local_temp = headptr_list[idx]->new_ref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - if (headptr_list[idx]->sym_difref) { - local_temp = headptr_list[idx]->sym_difref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - if (headptr_list[idx]->sym_newref) { - local_temp = headptr_list[idx]->sym_newref; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - SS_Free(headptr_list[idx]); - headptr_list[idx] = NULL; - } + fs_params *local_temp = NULL; + fs_params *local_next = NULL; + LOGL(LOG_SSENGINE, "Free Nodes idx=%d \n", idx); + if (headptr_list[idx]) { + if (headptr_list[idx]->del_ref) { + local_temp = headptr_list[idx]->del_ref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + if (headptr_list[idx]->dif_ref) { + local_temp = headptr_list[idx]->dif_ref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + if (headptr_list[idx]->move_ref) { + local_temp = headptr_list[idx]->move_ref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + if (headptr_list[idx]->new_ref) { + local_temp = headptr_list[idx]->new_ref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + if (headptr_list[idx]->sym_difref) { + local_temp = headptr_list[idx]->sym_difref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + if (headptr_list[idx]->sym_newref) { + local_temp = headptr_list[idx]->sym_newref; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + SS_Free(headptr_list[idx]); + headptr_list[idx] = NULL; + } } /*! ********************************************************************************* - * SS_FSGetDeltaCount + * SS_FSGetDeltaCount ********************************************************************************* * * @brief @@ -624,160 +619,160 @@ void SS_FSClearNodes(int idx) struct details *SS_FSGetDeltaCount(char *ubDeltaPath, char *ubDeltaInfoFile) { - int size = 0, bckupsize = 0, ret = 1; - char *token = NULL; - char *FileData = NULL; - int data_size = -1; - char *line = NULL; - char *saveptr; - char buf[256]; - struct details *refer_copy = NULL; - FILE *filename_bkup = NULL; - - if (!(ubDeltaPath && ubDeltaInfoFile)) { - LOGE("failed to Parse DELTA count information: \n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return NULL; - } - refer_copy = (struct details *)SS_Malloc(sizeof(struct details)); - - if (refer_copy == NULL) { - LOGE("failed to allocate memory\n"); - return NULL; - } - - LOGL(LOG_SSENGINE, "Delta File Info =%s\n", ubDeltaInfoFile); - - size = tar_get_item_size(ubDeltaPath, ubDeltaInfoFile); - if (size < 0) { - LOGE("failed to Access DELTA info file DPath:[%s] File: [%s]\n", ubDeltaPath, ubDeltaInfoFile); - SS_SetUpgradeState(E_SS_FSBADDELTA); - ret = 0; - goto cleanup; - } - - FileData = SS_Malloc(size + 1); - if (FileData == NULL) { - LOGE("Failed to Allocate Memory\n"); - SS_SetUpgradeState(E_SS_MALLOC_ERROR); - ret = 0; - goto cleanup; - } - - data_size = tar_get_cfg_data(ubDeltaPath, ubDeltaInfoFile, FileData, size); - if (data_size <= 0) { // == 0 is NOT okay?? - LOGE("Failed to read cfg data from Delta\n"); - SS_SetUpgradeState(E_SS_FSBADDELTA); - ret = 0; - goto cleanup; - } - filename_bkup = fopen(SS_PATCHLIST_BKUPLOC, "wb+"); - if (filename_bkup == NULL) { - strerror_r(errno, buf, sizeof(buf)); - LOGE("Failed to create BACKUP file Error:[%s]\n", buf); - SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO); - ret = 0; - goto cleanup; - } - - bckupsize = fwrite(FileData, 1, data_size, filename_bkup); //RECHECK SIZE 1 - if (bckupsize <= 0) { - SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO); - ret = 0; - goto cleanup; - } - LOGL(LOG_SSENGINE, " Size [%d] DataSize [%d] BakUpSize [%d]\n", size, data_size, bckupsize); - - line = strstr(FileData, SS_FSCOUNT_MAGIC_KEY); - if (line) { - LOGL(LOG_SSENGINE, "SS_FSGetDeltaCount() last line %s \n", line); - - token = strtok_r(&line[SS_FSCOUNT_MAGIG_KEYLEN], SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->diffs = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->moves = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - - token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->news = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - - token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->deletes = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - - token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->symdiffs = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - - token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); - if (token) - refer_copy->symnews = atoi(token); - else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - ret = 0; - goto cleanup; - } - - gtotalFSCnt = - refer_copy->diffs + refer_copy->moves + refer_copy->news + refer_copy->deletes + refer_copy->symdiffs + - refer_copy->symnews; - LOG("SS_FSGetDeltaCount() total no of file %d\n", gtotalFSCnt); - - } else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); - LOG("SS_FSGetDeltaCount() Failed to read last line\n"); - } - if (gtotalFSCnt < 0) { - ret = 0; - goto cleanup; - } + int size = 0, bckupsize = 0, ret = 1; + char *token = NULL; + char *FileData = NULL; + int data_size = -1; + char *line = NULL; + char *saveptr; + char buf[256]; + struct details *refer_copy = NULL; + FILE *filename_bkup = NULL; + + if (!(ubDeltaPath && ubDeltaInfoFile)) { + LOGE("failed to Parse DELTA count information: \n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return NULL; + } + refer_copy = (struct details *)SS_Malloc(sizeof(struct details)); + + if (refer_copy == NULL) { + LOGE("failed to allocate memory\n"); + return NULL; + } + + LOGL(LOG_SSENGINE, "Delta File Info =%s\n", ubDeltaInfoFile); + + size = tar_get_item_size(ubDeltaPath, ubDeltaInfoFile); + if (size < 0) { + LOGE("failed to Access DELTA info file DPath:[%s] File: [%s]\n", ubDeltaPath, ubDeltaInfoFile); + SS_SetUpgradeState(E_SS_FSBADDELTA); + ret = 0; + goto cleanup; + } + + FileData = SS_Malloc(size + 1); + if (FileData == NULL) { + LOGE("Failed to Allocate Memory\n"); + SS_SetUpgradeState(E_SS_MALLOC_ERROR); + ret = 0; + goto cleanup; + } + + data_size = tar_get_cfg_data(ubDeltaPath, ubDeltaInfoFile, FileData, size); + if (data_size <= 0) { // == 0 is NOT okay?? + LOGE("Failed to read cfg data from Delta\n"); + SS_SetUpgradeState(E_SS_FSBADDELTA); + ret = 0; + goto cleanup; + } + filename_bkup = fopen(SS_PATCHLIST_BKUPLOC, "wb+"); + if (filename_bkup == NULL) { + strerror_r(errno, buf, sizeof(buf)); + LOGE("Failed to create BACKUP file Error:[%s]\n", buf); + SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO); + ret = 0; + goto cleanup; + } + + bckupsize = fwrite(FileData, 1, data_size, filename_bkup); //RECHECK SIZE 1 + if (bckupsize <= 0) { + SS_SetUpgradeState(E_SS_FSFAILEDTOBACKUPPATCHINFO); + ret = 0; + goto cleanup; + } + LOGL(LOG_SSENGINE, " Size [%d] DataSize [%d] BakUpSize [%d]\n", size, data_size, bckupsize); + + line = strstr(FileData, SS_FSCOUNT_MAGIC_KEY); + if (line) { + LOGL(LOG_SSENGINE, "SS_FSGetDeltaCount() last line %s \n", line); + + token = strtok_r(&line[SS_FSCOUNT_MAGIG_KEYLEN], SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->diffs = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->moves = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + + token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->news = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + + token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->deletes = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + + token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->symdiffs = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + + token = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr); + if (token) + refer_copy->symnews = atoi(token); + else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + ret = 0; + goto cleanup; + } + + gtotalFSCnt = + refer_copy->diffs + refer_copy->moves + refer_copy->news + refer_copy->deletes + refer_copy->symdiffs + + refer_copy->symnews; + LOG("SS_FSGetDeltaCount() total no of file %d\n", gtotalFSCnt); + + } else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTACNT); + LOG("SS_FSGetDeltaCount() Failed to read last line\n"); + } + if (gtotalFSCnt < 0) { + ret = 0; + goto cleanup; + } cleanup: - if (ret) { - SS_Free(FileData); - if (filename_bkup) - fclose(filename_bkup); - return refer_copy; - } else { - SS_Free(FileData); - SS_Free(refer_copy); - if (filename_bkup) - fclose(filename_bkup); - return NULL; - } + if (ret) { + SS_Free(FileData); + if (filename_bkup) + fclose(filename_bkup); + return refer_copy; + } else { + SS_Free(FileData); + SS_Free(refer_copy); + if (filename_bkup) + fclose(filename_bkup); + return NULL; + } } /*! ********************************************************************************* - * SS_FSBuildNodes + * SS_FSBuildNodes ********************************************************************************* * * @brief @@ -794,66 +789,66 @@ struct details *SS_FSGetDeltaCount(char *ubDeltaPath, char *ubDeltaInfoFile) */ fs_list *SS_FSBuildNodes(ua_dataSS_t * ua_dataSS) { - FILE *fp = NULL; - char line[SS_TOKEN_MAXLINE_LEN] = { '\0' }; - char string_na[] = "NA"; - char *patch_name = NULL; - char *source_name = NULL; - char *target_name = NULL; - char *sha1src = NULL; - char *sha1trg = NULL; - char *change_type = NULL; - char *file_type = NULL; - char *saveptr; - uint32_t ulPatchCount = 0, del_type = DELETES; - fs_params *fs_diffhead = NULL; - fs_params *fs_difftail = NULL; - fs_params *fs_movehead = NULL; - fs_params *fs_movetail = NULL; - fs_params *fs_newhead = NULL; - fs_params *fs_delhead = NULL; - fs_params *fs_deltail = NULL; - fs_params *fs_symlinkdiffhead = NULL; - fs_params *fs_symlinkdifftail = NULL; - fs_params *fs_symlinknewhead = NULL; - fs_params *fs_symlinknewtail = NULL; - - struct details *local = NULL; - fs_list *fs_head_node = NULL; - int i = 0, retval = 0; - if (!ua_dataSS) { - LOGE("Bad structure ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return NULL; - } - LOGL(LOG_SSENGINE, " Build Nodes Entry \n"); - if (tar_cfg_data == NULL) - tar_cfg_data = tar_build_cfg_table(ua_dataSS->update_data->ua_delta_path); - if (!tar_cfg_data) { - LOGE(" tar_build_cfg_table Failed \n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return NULL; - } - local = SS_FSGetDeltaCount(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_patch_info); - if (local == NULL) { - LOGE(" Build Nodes Failed \n"); - if (tar_cfg_data) - tar_free_cfg_table(&tar_cfg_data); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return NULL; - } - - fp = fopen(SS_PATCHLIST_BKUPLOC, "r"); - if (!fp) { - SS_SetUpgradeState(E_SS_FSFAILEDTOOPENPATCHINFO); - if (tar_cfg_data) - tar_free_cfg_table(&tar_cfg_data); - SS_Free(local); - return NULL; - } - - ulPatchCount = local->diffs + local->deletes + local->news + local->moves + local->symdiffs + local->symnews; - LOG("Total FS count [%d].\n", ulPatchCount); + FILE *fp = NULL; + char line[SS_TOKEN_MAXLINE_LEN] = { '\0' }; + char string_na[] = "NA"; + char *patch_name = NULL; + char *source_name = NULL; + char *target_name = NULL; + char *sha1src = NULL; + char *sha1trg = NULL; + char *change_type = NULL; + char *file_type = NULL; + char *saveptr; + uint32_t ulPatchCount = 0, del_type = DELETES; + fs_params *fs_diffhead = NULL; + fs_params *fs_difftail = NULL; + fs_params *fs_movehead = NULL; + fs_params *fs_movetail = NULL; + fs_params *fs_newhead = NULL; + fs_params *fs_delhead = NULL; + fs_params *fs_deltail = NULL; + fs_params *fs_symlinkdiffhead = NULL; + fs_params *fs_symlinkdifftail = NULL; + fs_params *fs_symlinknewhead = NULL; + fs_params *fs_symlinknewtail = NULL; + + struct details *local = NULL; + fs_list *fs_head_node = NULL; + int i = 0, retval = 0; + if (!ua_dataSS) { + LOGE("Bad structure ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return NULL; + } + LOGL(LOG_SSENGINE, " Build Nodes Entry \n"); + if (tar_cfg_data == NULL) + tar_cfg_data = tar_build_cfg_table(ua_dataSS->update_data->ua_delta_path); + if (!tar_cfg_data) { + LOGE(" tar_build_cfg_table Failed \n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return NULL; + } + local = SS_FSGetDeltaCount(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_patch_info); + if (local == NULL) { + LOGE(" Build Nodes Failed \n"); + if (tar_cfg_data) + tar_free_cfg_table(&tar_cfg_data); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return NULL; + } + + fp = fopen(SS_PATCHLIST_BKUPLOC, "r"); + if (!fp) { + SS_SetUpgradeState(E_SS_FSFAILEDTOOPENPATCHINFO); + if (tar_cfg_data) + tar_free_cfg_table(&tar_cfg_data); + SS_Free(local); + return NULL; + } + + ulPatchCount = local->diffs + local->deletes + local->news + local->moves + local->symdiffs + local->symnews; + LOG("Total FS count [%d].\n", ulPatchCount); /* ************************************************************************ Parsing logic implemented for patchlist @@ -872,335 +867,334 @@ SYM:DIFF:system/usr/lib/sync-agent/kies-private/libplugin-na-mobex.so.0:system/u SYM:NEW:system/lib/firmware/vbc_eq:/opt/system/vbc_eq *********************************************************************** */ - if (local && ((local->diffs) > 0 || (local->moves > 0))) { - LOGL(LOG_SSENGINE, "%ss [%d] %ss [%d]\n", SS_STRING_DIFF, local->diffs, SS_STRING_MOVE, local->moves); - for (i = 0; i < (local->diffs + local->moves); i++) { - if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - break; - } - //LOGL(LOG_SSENGINE, "DIFF LINE:[%d] [%s] \n",i+1,line); - - change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); - file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - - if (change_type && strcmp(change_type, SS_STRING_MOVE) == 0) { // && strcmp(file_type,"TPK") == 0){ - source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_MOVE, i); - - if (!source_name || !target_name || !sha1src) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - //LOGE("Failed to extract Patch Info Type:DELETES \n"); - LOGE("Failed to parse DIFFS - LINE:[%d] [%s] \n", i + 1, line); - goto CleanUp; - } - retval = - SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_movehead, &fs_movetail, source_name, - target_name, string_na, sha1src, string_na, MOVES, - ua_dataSS->update_delta->ua_patch_path); - if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this - goto CleanUp; - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - } else if (change_type && strcmp(change_type, SS_STRING_DIFF) == 0) { // && strcmp(file_type,"TPK") == 0){ - source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - sha1src = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - sha1trg = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DIFF, i); - - if (patch_name && (strlen(patch_name) <= SS_MAX_NAMELENSUPPORTED)) { - retval = - SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_diffhead, &fs_difftail, - source_name, target_name, patch_name, sha1src, sha1trg, DIFFS, - ua_dataSS->update_delta->ua_patch_path); - if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this - goto CleanUp; - } else { - SS_SetUpgradeState(E_SS_FILENAMELENERROR); - LOGE("File Name length Limitation Error File:[%s]\n", patch_name); - goto CleanUp; - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - } else { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - LOGE("Patch Name format Error File\n"); - goto CleanUp; - } - } - } - if (local && (local->news) > 0) { //check if new files archive is present in the delta - char new_patch_path[MAX_FILE_PATH] = { 0, }; - snprintf(new_patch_path, MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE); - if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, new_patch_path) <= 0) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - LOGE("New files not present in Patch\n"); - goto CleanUp; - } - } - if (local && (local->deletes) > 0) { //this is to group to delete list - LOGL(LOG_SSENGINE, "%ss [%d]\n", SS_STRING_DEL, local->deletes); - for (i = 0; i < (local->deletes); i++) { - if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - break; - } - - change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); - file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - - if (file_type && strcmp(file_type, SS_STRING_REG) == 0) { - source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - del_type = DELETES; - } else if (file_type && strcmp(file_type, SS_STRING_SYM) == 0) { - source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - sha1src = string_na; - del_type = DELETES; - } else if (file_type && strcmp(file_type, SS_STRING_END) == 0) { - source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - sha1src = string_na; - del_type = DELETE_END; - } - else { - LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line); - goto CleanUp; - } - - if (!source_name || !sha1src) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - //LOGE("Failed to extract Patch Info Type:DELETES \n"); - LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line); - goto CleanUp; - } - //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DEL, i); - retval = - SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_delhead, &fs_deltail, source_name, - string_na, string_na, sha1src, string_na, del_type, - ua_dataSS->update_delta->ua_patch_path); - if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this - goto CleanUp; - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - - } - } //For symlink files - - if (local && (local->symdiffs) > 0) { - LOGL(LOG_SSENGINE, "%s %ss [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, local->symdiffs); - for (i = 0; i < (local->symdiffs); i++) { //get the count from below function - if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - break; - } - //LOGL(LOG_SSENGINE, "SYMDIFF LINE:[%d] [%s] \n",i+1,line); - - change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); - file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - - if ((change_type && file_type) && - strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_DIFF) == 0) { // && strcmp(file_type,"TPK") == 0){ - source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, i); - - if (!source_name || !target_name || !patch_name) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - //LOGE("Failed to extract Patch Info Type:DELETES \n"); - LOGE("Failed to parse SymDiffs - LINE:[%d] [%s] \n", i + 1, line); - goto CleanUp; - } - retval = - SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinkdiffhead, &fs_symlinkdifftail, - source_name, target_name, patch_name, string_na, string_na, SYMDIFFS, - ua_dataSS->update_delta->ua_patch_path); - if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this - goto CleanUp; - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - } - } - } - if (local && (local->symnews) > 0) { - LOGL(LOG_SSENGINE, "%s %ss [%d]n", SS_STRING_SYM, SS_STRING_NEW, local->symnews); - for (i = 0; i < (local->symnews); i++) { - if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - break; - } - //LOGL(LOG_SSENGINE, "SYMNEWS LINE:[%d] [%s] \n",i+1,line); - - change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); - file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - - if ((change_type && file_type) && - (strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_NEW) == 0)) { - source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); - patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); - //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_NEW, i); - - if (!source_name || !patch_name) { - SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); - //LOGE("Failed to extract Patch Info Type:DELETES \n"); - LOGE("Failed to parse SymNews - LINE:[%d] [%s] \n", i + 1, line); - goto CleanUp; - } - retval = - SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinknewhead, &fs_symlinknewtail, - source_name, string_na, patch_name, string_na, string_na, SYMNEWFILES, - ua_dataSS->update_delta->ua_patch_path); - if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this - goto CleanUp; - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - } - } - } - - fs_head_node = (fs_list *) SS_Malloc(sizeof(fs_list)); - if (!fs_head_node) { - SS_SetUpgradeState(E_SS_MALLOC_ERROR); - goto CleanUp; - } - fs_head_node->dif_ref = fs_diffhead; - fs_head_node->move_ref = fs_movehead; - fs_head_node->new_ref = fs_newhead; - fs_head_node->del_ref = fs_delhead; - fs_head_node->sym_difref = fs_symlinkdiffhead; - fs_head_node->sym_newref = fs_symlinknewhead; - fs_head_node->ulPatchCount = ulPatchCount; - - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 1); + if (local && ((local->diffs) > 0 || (local->moves > 0))) { + LOGL(LOG_SSENGINE, "%ss [%d] %ss [%d]\n", SS_STRING_DIFF, local->diffs, SS_STRING_MOVE, local->moves); + for (i = 0; i < (local->diffs + local->moves); i++) { + if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + break; + } + //LOGL(LOG_SSENGINE, "DIFF LINE:[%d] [%s] \n",i+1,line); + + change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); + file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + + if (change_type && strcmp(change_type, SS_STRING_MOVE) == 0) { // && strcmp(file_type,"TPK") == 0){ + source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_MOVE, i); + + if (!source_name || !target_name || !sha1src) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + //LOGE("Failed to extract Patch Info Type:DELETES \n"); + LOGE("Failed to parse DIFFS - LINE:[%d] [%s] \n", i + 1, line); + goto CleanUp; + } + retval = + SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_movehead, &fs_movetail, source_name, + target_name, string_na, sha1src, string_na, MOVES, + ua_dataSS->update_delta->ua_patch_path); + if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this + goto CleanUp; + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + } else if (change_type && strcmp(change_type, SS_STRING_DIFF) == 0) { // && strcmp(file_type,"TPK") == 0){ + source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + sha1src = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + sha1trg = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DIFF, i); + + if (patch_name && (strlen(patch_name) <= SS_MAX_NAMELENSUPPORTED)) { + retval = + SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_diffhead, &fs_difftail, + source_name, target_name, patch_name, sha1src, sha1trg, DIFFS, + ua_dataSS->update_delta->ua_patch_path); + if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this + goto CleanUp; + } else { + SS_SetUpgradeState(E_SS_FILENAMELENERROR); + LOGE("File Name length Limitation Error File:[%s]\n", patch_name); + goto CleanUp; + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + } else { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + LOGE("Patch Name format Error File\n"); + goto CleanUp; + } + } + } + if (local && (local->news) > 0) { //check if new files archive is present in the delta + char new_patch_path[MAX_FILE_PATH] = { 0, }; + snprintf(new_patch_path, MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE); + if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, new_patch_path) <= 0) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + LOGE("New files not present in Patch\n"); + goto CleanUp; + } + } + if (local && (local->deletes) > 0) { //this is to group to delete list + LOGL(LOG_SSENGINE, "%ss [%d]\n", SS_STRING_DEL, local->deletes); + for (i = 0; i < (local->deletes); i++) { + if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + break; + } + + change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); + file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + + if (file_type && strcmp(file_type, SS_STRING_REG) == 0) { + source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + sha1src = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + del_type = DELETES; + } else if (file_type && strcmp(file_type, SS_STRING_SYM) == 0) { + source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + sha1src = string_na; + del_type = DELETES; + } else if (file_type && strcmp(file_type, SS_STRING_END) == 0) { + source_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + sha1src = string_na; + del_type = DELETE_END; + } else { + LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line); + goto CleanUp; + } + + if (!source_name || !sha1src) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + //LOGE("Failed to extract Patch Info Type:DELETES \n"); + LOGE("Failed to parse DELETES - LINE:[%d] [%s] \n", i + 1, line); + goto CleanUp; + } + //LOGL(LOG_SSENGINE, "%s Index [%d]\n", SS_STRING_DEL, i); + retval = + SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_delhead, &fs_deltail, source_name, + string_na, string_na, sha1src, string_na, del_type, + ua_dataSS->update_delta->ua_patch_path); + if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this + goto CleanUp; + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + + } + } //For symlink files + + if (local && (local->symdiffs) > 0) { + LOGL(LOG_SSENGINE, "%s %ss [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, local->symdiffs); + for (i = 0; i < (local->symdiffs); i++) { //get the count from below function + if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + break; + } + //LOGL(LOG_SSENGINE, "SYMDIFF LINE:[%d] [%s] \n",i+1,line); + + change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); + file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + + if ((change_type && file_type) && + strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_DIFF) == 0) { // && strcmp(file_type,"TPK") == 0){ + source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + target_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_DIFF, i); + + if (!source_name || !target_name || !patch_name) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + //LOGE("Failed to extract Patch Info Type:DELETES \n"); + LOGE("Failed to parse SymDiffs - LINE:[%d] [%s] \n", i + 1, line); + goto CleanUp; + } + retval = + SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinkdiffhead, &fs_symlinkdifftail, + source_name, target_name, patch_name, string_na, string_na, SYMDIFFS, + ua_dataSS->update_delta->ua_patch_path); + if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this + goto CleanUp; + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + } + } + } + if (local && (local->symnews) > 0) { + LOGL(LOG_SSENGINE, "%s %ss [%d]n", SS_STRING_SYM, SS_STRING_NEW, local->symnews); + for (i = 0; i < (local->symnews); i++) { + if (fgets(line, SS_TOKEN_MAXLINE_LEN, fp) == NULL) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + break; + } + //LOGL(LOG_SSENGINE, "SYMNEWS LINE:[%d] [%s] \n",i+1,line); + + change_type = strtok_r(line, SS_TOEKN_COLON, &saveptr); + file_type = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + + if ((change_type && file_type) && + (strcmp(change_type, SS_STRING_SYM) == 0 && strcmp(file_type, SS_STRING_NEW) == 0)) { + source_name = strtok_r(NULL, SS_TOEKN_COLON, &saveptr); + patch_name = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr); + //LOGL(LOG_SSENGINE, "%s %s Index [%d]\n", SS_STRING_SYM, SS_STRING_NEW, i); + + if (!source_name || !patch_name) { + SS_SetUpgradeState(E_SS_FSFAILEDTOPARSEDELTAINFO); + //LOGE("Failed to extract Patch Info Type:DELETES \n"); + LOGE("Failed to parse SymNews - LINE:[%d] [%s] \n", i + 1, line); + goto CleanUp; + } + retval = + SS_AppendNode(ua_dataSS->update_data->ua_delta_path, &fs_symlinknewhead, &fs_symlinknewtail, + source_name, string_na, patch_name, string_na, string_na, SYMNEWFILES, + ua_dataSS->update_delta->ua_patch_path); + if (retval == E_SS_FAILURE) // ONLY test purpose, should enable this + goto CleanUp; + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + } + } + } + + fs_head_node = (fs_list *) SS_Malloc(sizeof(fs_list)); + if (!fs_head_node) { + SS_SetUpgradeState(E_SS_MALLOC_ERROR); + goto CleanUp; + } + fs_head_node->dif_ref = fs_diffhead; + fs_head_node->move_ref = fs_movehead; + fs_head_node->new_ref = fs_newhead; + fs_head_node->del_ref = fs_delhead; + fs_head_node->sym_difref = fs_symlinkdiffhead; + fs_head_node->sym_newref = fs_symlinknewhead; + fs_head_node->ulPatchCount = ulPatchCount; + + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 1); CleanUp: - fclose(fp); - SS_Free(local); - unlink(SS_PATCHLIST_BKUPLOC); - if (retval == E_SS_FAILURE) - if (tar_cfg_data) - tar_free_cfg_table(&tar_cfg_data); - return fs_head_node; + fclose(fp); + SS_Free(local); + unlink(SS_PATCHLIST_BKUPLOC); + if (retval == E_SS_FAILURE) + if (tar_cfg_data) + tar_free_cfg_table(&tar_cfg_data); + return fs_head_node; } void SS_GetPartition_LocDetails(ua_dataSS_t * ua_dataSS, int part_idx) { - LOGL(LOG_SSENGINE, "PART NAME: [%s] \n", ua_dataSS->parti_info->ua_parti_name); - snprintf(ua_dataSS->update_delta->ua_patch_path, MAX_FILE_PATH, "%s", ua_dataSS->parti_info->ua_subject_name); - snprintf(ua_dataSS->update_delta->ua_patch_info, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name, - ua_dataSS->parti_info->ua_parti_name, SS_PATCHLISTFORMAT); - snprintf(ua_dataSS->update_delta->ua_attrib_path, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name, - ua_dataSS->parti_info->ua_parti_name, SS_PATCH_ATTR_FORMAT); - LOGL(LOG_SSENGINE, "PatchPath[%s] PatchInfo [%s] Attributes [%s]\n", ua_dataSS->update_delta->ua_patch_path, - ua_dataSS->update_delta->ua_patch_info, ua_dataSS->update_delta->ua_attrib_path); - - return; + LOGL(LOG_SSENGINE, "PART NAME: [%s] \n", ua_dataSS->parti_info->ua_parti_name); + snprintf(ua_dataSS->update_delta->ua_patch_path, MAX_FILE_PATH, "%s", ua_dataSS->parti_info->ua_subject_name); + snprintf(ua_dataSS->update_delta->ua_patch_info, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name, + ua_dataSS->parti_info->ua_parti_name, SS_PATCHLISTFORMAT); + snprintf(ua_dataSS->update_delta->ua_attrib_path, MAX_FILE_PATH, "%s%s%s", ua_dataSS->parti_info->ua_subject_name, + ua_dataSS->parti_info->ua_parti_name, SS_PATCH_ATTR_FORMAT); + LOGL(LOG_SSENGINE, "PatchPath[%s] PatchInfo [%s] Attributes [%s]\n", ua_dataSS->update_delta->ua_patch_path, + ua_dataSS->update_delta->ua_patch_info, ua_dataSS->update_delta->ua_attrib_path); + + return; } //Support functions//Change Struct format details (Can include total file count also???)/*! /* ******************************************************************************** * SS_FSSetAttributes - ********************************************************************************* - * *@brief - * This is used to set the file attributes at the end of application of patches in FS - * - * *@param - * - *@return returns S_SS_SUCCESS - * E_SS_FAILURE in case of error - * - ********************************************************************************* + ********************************************************************************* + * *@brief + * This is used to set the file attributes at the end of application of patches in FS + * + * *@param + * + *@return returns S_SS_SUCCESS + * E_SS_FAILURE in case of error + * + ********************************************************************************* */ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) { - char *pline = NULL; - char *psaveptr = NULL; - char *pfilePath = NULL; - char *pfiletype = NULL; - char *attributSize = NULL; - char *pattribs = NULL; - int ulAttribSize = 0; - int result = S_SS_SUCCESS; - - if (!(ua_dataSS && ua_dataSS->update_delta && ua_dataSS->update_data->ua_delta_path)) { - LOGE("Bad params for SS_FSSetAttributes\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, "ATTRIB PATH: [%s] \n", ua_dataSS->update_delta->ua_attrib_path); - - int item_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path); - - if (item_size <= 0) { - LOGL(LOG_SSENGINE, "No Attributes to SET\n"); - return S_SS_SUCCESS; // Delta with ONLY deletes - } - char *item_data = SS_Malloc(item_size + 1); - if (item_data == NULL) { - SS_SetUpgradeState(E_SS_MALLOC_ERROR); - return E_SS_FAILURE; - } - int read_data = - tar_get_cfg_data(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path, item_data, - item_size); - if (read_data <= 0) { - SS_SetUpgradeState(E_SS_FSBADDELTA); - if (item_data != NULL) - SS_Free(item_data); - return E_SS_FAILURE; - } - pline = strtok_r(item_data, "\n", &psaveptr); - if (pline == NULL) { - LOGL(LOG_SSENGINE, "No Attributes to SET as no lines in file\n"); - if (item_data != NULL) - SS_Free(item_data); - return E_SS_FAILURE; - } - - while (pline) { - char *saveptr_pline; - pfilePath = strtok_r(pline, "\"", &saveptr_pline); - - if (pfilePath && strcmp(pfilePath, SS_FWSLASH) == 0) { - LOGE("\n skip root: it is RO"); - pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); - continue; - } - - pfiletype = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); - attributSize = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); - pattribs = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr_pline); - LOG("\nSS_FSSetAttributes [%s][%s][%s]", pfiletype, attributSize, pattribs); - if (pattribs && pfilePath && pfiletype) { - ulAttribSize = strlen(pattribs); - //LOG("\nSS_SetFileAttributes [%s][%s][%d][%s]",pfilePath,pfiletype,ulAttribSize, pattribs ); - //LOG("SS_SetFileAttributes [%s]\n", pfilePath); - - result = SS_SetFileAttributes(pfilePath, ulAttribSize, (const unsigned char *)pattribs); - if (result != S_SS_SUCCESS) { - LOGE("\n Failed to set Attributes %s", pfilePath); - SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); - if (item_data) - SS_Free(item_data); - return E_SS_FAILURE; - } - } else { - LOGE("\n Failed to Parse Attributes - LINE %s", pline); - SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); - if (item_data) - SS_Free(item_data); - return E_SS_FAILURE; - } - pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); - } - SS_Free(item_data); - - return S_SS_SUCCESS; + char *pline = NULL; + char *psaveptr = NULL; + char *pfilePath = NULL; + char *pfiletype = NULL; + char *attributSize = NULL; + char *pattribs = NULL; + int ulAttribSize = 0; + int result = S_SS_SUCCESS; + + if (!(ua_dataSS && ua_dataSS->update_delta && ua_dataSS->update_data->ua_delta_path)) { + LOGE("Bad params for SS_FSSetAttributes\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + LOGL(LOG_SSENGINE, "ATTRIB PATH: [%s] \n", ua_dataSS->update_delta->ua_attrib_path); + + int item_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path); + + if (item_size <= 0) { + LOGL(LOG_SSENGINE, "No Attributes to SET\n"); + return S_SS_SUCCESS; // Delta with ONLY deletes + } + char *item_data = SS_Malloc(item_size + 1); + if (item_data == NULL) { + SS_SetUpgradeState(E_SS_MALLOC_ERROR); + return E_SS_FAILURE; + } + int read_data = + tar_get_cfg_data(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path, item_data, + item_size); + if (read_data <= 0) { + SS_SetUpgradeState(E_SS_FSBADDELTA); + if (item_data != NULL) + SS_Free(item_data); + return E_SS_FAILURE; + } + pline = strtok_r(item_data, "\n", &psaveptr); + if (pline == NULL) { + LOGL(LOG_SSENGINE, "No Attributes to SET as no lines in file\n"); + if (item_data != NULL) + SS_Free(item_data); + return E_SS_FAILURE; + } + + while (pline) { + char *saveptr_pline; + pfilePath = strtok_r(pline, "\"", &saveptr_pline); + + if (pfilePath && strcmp(pfilePath, SS_FWSLASH) == 0) { + LOGE("\n skip root: it is RO"); + pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); + continue; + } + + pfiletype = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); + attributSize = strtok_r(NULL, SS_TOKEN_SPACE, &saveptr_pline); + pattribs = strtok_r(NULL, SS_TOKEN_NEWLINE, &saveptr_pline); + LOG("\nSS_FSSetAttributes [%s][%s][%s]", pfiletype, attributSize, pattribs); + if (pattribs && pfilePath && pfiletype) { + ulAttribSize = strlen(pattribs); + //LOG("\nSS_SetFileAttributes [%s][%s][%d][%s]",pfilePath,pfiletype,ulAttribSize, pattribs ); + //LOG("SS_SetFileAttributes [%s]\n", pfilePath); + + result = SS_SetFileAttributes(pfilePath, ulAttribSize, (const unsigned char *)pattribs); + if (result != S_SS_SUCCESS) { + LOGE("\n Failed to set Attributes %s", pfilePath); + SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); + if (item_data) + SS_Free(item_data); + return E_SS_FAILURE; + } + } else { + LOGE("\n Failed to Parse Attributes - LINE %s", pline); + SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); + if (item_data) + SS_Free(item_data); + return E_SS_FAILURE; + } + pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); + } + SS_Free(item_data); + + return S_SS_SUCCESS; } /*! ********************************************************************************* - * SS_FSUpdateFile + * SS_FSUpdateFile ********************************************************************************* * * @brief @@ -1215,307 +1209,304 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) ********************************************************************************* */ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, fs_params * pFsNode, - const char *patch_path) + const char *patch_path) { - int ulFileIndex = 1; - char ubPatch[SS_MAX_FILE_PATH] = { - 0 - }; - int ulReadCnt = 0; - int ulResult = S_SS_SUCCESS; - - if (!patch_path) { - LOGE("Bad patch_path name\n"); - return E_SS_FAILURE; - } - switch (ubFileType) { - case DIFFS: - { - tar_open(ua_dataSS->update_data->ua_delta_path); + int ulFileIndex = 1; + char ubPatch[SS_MAX_FILE_PATH] = { + 0 + }; + int ulReadCnt = 0; + int ulResult = S_SS_SUCCESS; + + if (!patch_path) { + LOGE("Bad patch_path name\n"); + return E_SS_FAILURE; + } + switch (ubFileType) { + case DIFFS: + { + tar_open(ua_dataSS->update_data->ua_delta_path); #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - while (pFsNode) { - LOGL(LOG_SSENGINE, "DIFFS update Index: [%d] \n", ulFileIndex++); - snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s", patch_path, pFsNode->patch_name); - //LOGL(LOG_SSENGINE, "DIFF list --- [File Name %s]\n [Patch Name %s]",pFsNode->file_path, ubPatch); - if (pFsNode->data_size > 0) { - ulReadCnt = - fast_tar_extract_file(ua_dataSS->update_data->ua_delta_path, ubPatch, SS_PATCHFILE_SOURCE, - pFsNode->data_size, pFsNode->data_offset); - if (ulReadCnt < 0) { - ulResult = E_SS_FAILURE; - tar_close(); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - LOGE("Delta Read Failed\n"); - break; - } - //LOGL(LOG_SSENGINE,"Updating [Item - %s]and size is[%d] Read Count[%d]\n",ubPatch, pFsNode->data_size, ulReadCnt); - - if (ulReadCnt > 0) - ulResult = - SS_UpdateDeltaFS(pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src, - pFsNode->sha1trg, pFsNode->data_size); - if (ulResult != S_SS_SUCCESS) { - LOGE("FS update Failed Result : [%d], [Item - %s]and size is[%d] Read Count[%d]\n", ulResult, - ubPatch, pFsNode->data_size, ulReadCnt); - tar_close(); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - break; - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } + while (pFsNode) { + LOGL(LOG_SSENGINE, "DIFFS update Index: [%d] \n", ulFileIndex++); + snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s", patch_path, pFsNode->patch_name); + //LOGL(LOG_SSENGINE, "DIFF list --- [File Name %s]\n [Patch Name %s]",pFsNode->file_path, ubPatch); + if (pFsNode->data_size > 0) { + ulReadCnt = + fast_tar_extract_file(ua_dataSS->update_data->ua_delta_path, ubPatch, SS_PATCHFILE_SOURCE, + pFsNode->data_size, pFsNode->data_offset); + if (ulReadCnt < 0) { + ulResult = E_SS_FAILURE; + tar_close(); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + LOGE("Delta Read Failed\n"); + break; + } + //LOGL(LOG_SSENGINE,"Updating [Item - %s]and size is[%d] Read Count[%d]\n",ubPatch, pFsNode->data_size, ulReadCnt); + + if (ulReadCnt > 0) + ulResult = + SS_UpdateDeltaFS(pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src, + pFsNode->sha1trg, pFsNode->data_size); + if (ulResult != S_SS_SUCCESS) { + LOGE("FS update Failed Result : [%d], [Item - %s]and size is[%d] Read Count[%d]\n", ulResult, + ubPatch, pFsNode->data_size, ulReadCnt); + tar_close(); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + break; + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + pFsNode = pFsNode->nextnode; + } #ifdef SUPPORT_CONTAINER - else if (pFsNode->data_size == 0) { //need to add logic to identify archive update - ulResult = tar_extract_folder(ua_dataSS->update_data->ua_delta_path, ubPatch, - SS_ARCHIVE_DELTA_FOLDER); - if (ulResult != S_SS_SUCCESS) { - LOGE("extraction failed for [%s] result [%d]\n", ubPatch, ulResult); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - break; - } - //Copy exe's for zip , unzip and find operations - code to be removed after ramdisk upgrade - ulResult = (int)SS_CopyFile(NULL, SS_ZIP_SOURCE, SS_ZIP_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy unzip [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = (int)SS_CopyFile(NULL, SS_UNZIP_SOURCE, SS_UNZIP_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy unzip [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = (int)SS_CopyFile(NULL, SS_FIND_CMD_SOURCE, SS_FIND_CMD_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy find [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = - SS_UpdateArchive(ua_dataSS, pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src, - pFsNode->sha1trg); - if (ulResult != S_SS_SUCCESS) { - LOGE("FS update Failed, Unable to extact archive\n"); - break; - } - - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } + else if (pFsNode->data_size == 0) { //need to add logic to identify archive update + ulResult = tar_extract_folder(ua_dataSS->update_data->ua_delta_path, ubPatch, + SS_ARCHIVE_DELTA_FOLDER); + if (ulResult != S_SS_SUCCESS) { + LOGE("extraction failed for [%s] result [%d]\n", ubPatch, ulResult); + SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); + break; + } + //Copy exe's for zip , unzip and find operations - code to be removed after ramdisk upgrade + ulResult = (int)SS_CopyFile(NULL, SS_ZIP_SOURCE, SS_ZIP_TARGET); + if (ulResult != S_SS_SUCCESS) { + LOGE("failed to copy unzip [%d]\n", ulResult); + SS_SetUpgradeState(E_SS_WRITE_ERROR); + break; + } + ulResult = (int)SS_CopyFile(NULL, SS_UNZIP_SOURCE, SS_UNZIP_TARGET); + if (ulResult != S_SS_SUCCESS) { + LOGE("failed to copy unzip [%d]\n", ulResult); + SS_SetUpgradeState(E_SS_WRITE_ERROR); + break; + } + ulResult = (int)SS_CopyFile(NULL, SS_FIND_CMD_SOURCE, SS_FIND_CMD_TARGET); + if (ulResult != S_SS_SUCCESS) { + LOGE("failed to copy find [%d]\n", ulResult); + SS_SetUpgradeState(E_SS_WRITE_ERROR); + break; + } + ulResult = + SS_UpdateArchive(ua_dataSS, pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src, + pFsNode->sha1trg); + if (ulResult != S_SS_SUCCESS) { + LOGE("FS update Failed, Unable to extact archive\n"); + break; + } + + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + pFsNode = pFsNode->nextnode; + } #endif - else { - ulResult = E_SS_FAILURE; - tar_close(); - LOGE("size is invalid\n"); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - break; - } - } + else { + ulResult = E_SS_FAILURE; + tar_close(); + LOGE("size is invalid\n"); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + break; + } + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for DIFFS - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for DIFFS - [%d] \n", (t2 - t1)); #endif #ifdef SUPPORT_CONTAINER - SS_DeleteFile(NULL, SS_ZIP_TARGET); - SS_DeleteFile(NULL, SS_UNZIP_TARGET); + SS_DeleteFile(NULL, SS_ZIP_TARGET); + SS_DeleteFile(NULL, SS_UNZIP_TARGET); #endif - tar_close(); - } - break; - case MOVES: - { + tar_close(); + } + break; + case MOVES: + { #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - while (pFsNode) { - LOGL(LOG_SSENGINE, "MOVES update Index: [%d] \n", ulFileIndex++); - ulResult = SS_MoveFile(NULL, pFsNode->file_old_path, pFsNode->file_new_path); - if (ulResult != S_SS_SUCCESS) { - LOGE("Move Failed for [%s] to [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); - SS_SetUpgradeState(ulResult); - break; - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } + while (pFsNode) { + LOGL(LOG_SSENGINE, "MOVES update Index: [%d] \n", ulFileIndex++); + ulResult = SS_MoveFile(NULL, pFsNode->file_old_path, pFsNode->file_new_path); + if (ulResult != S_SS_SUCCESS) { + LOGE("Move Failed for [%s] to [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); + SS_SetUpgradeState(ulResult); + break; + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + pFsNode = pFsNode->nextnode; + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); #endif - } - break; - case DELETES: - { - int ulFiletype = 0; + } + break; + case DELETES: + { + int ulFiletype = 0; #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - while (pFsNode) { - if (pFsNode->type == DELETES) { - LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); - SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); - if (ulFiletype == 2) //FT_FOLDER - ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); - else - ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); - if (ulResult != S_SS_SUCCESS) { - LOGE("Delete Failed\n"); - SS_SetUpgradeState(ulResult); - break; - } - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } + while (pFsNode) { + if (pFsNode->type == DELETES) { + LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); + SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); + if (ulFiletype == 2) //FT_FOLDER + ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); + else + ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); + if (ulResult != S_SS_SUCCESS) { + LOGE("Delete Failed\n"); + SS_SetUpgradeState(ulResult); + break; + } + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + pFsNode = pFsNode->nextnode; + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); #endif - } - break; - case DELETE_END: - { - int ulFiletype = 0; + } + break; + case DELETE_END: + { + int ulFiletype = 0; #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - while (pFsNode) { - if (pFsNode->type == DELETE_END) { - LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); - SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); - if (ulFiletype == 2) //FT_FOLDER - ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); - else - ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); - if (ulResult != S_SS_SUCCESS) { - LOGE("Delete Failed\n"); - SS_SetUpgradeState(ulResult); - break; - } - } - pFsNode = pFsNode->nextnode; - } + while (pFsNode) { + if (pFsNode->type == DELETE_END) { + LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); + SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); + if (ulFiletype == 2) //FT_FOLDER + ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); + else + ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); + if (ulResult != S_SS_SUCCESS) { + LOGE("Delete Failed\n"); + SS_SetUpgradeState(ulResult); + break; + } + } + pFsNode = pFsNode->nextnode; + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); #endif - } - break; + } + break; - case NEWFILES: - { + case NEWFILES: + { #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - LOGL(LOG_SSENGINE, "Starting New file upgrade for [%s]\n", patch_path); - if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, (char *)patch_path, SS_NEW_COMPRESSED_FILE) >= - 0) - if (_7zdecompress(SS_NEW_COMPRESSED_FILE) == 0) - LOGL(LOG_SSENGINE, "7zip extracted successfully %s\n", ua_dataSS->parti_info->ua_parti_name); - else - LOGL(LOG_SSENGINE, "7zip extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); - else - LOGL(LOG_SSENGINE, "tar extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); - SS_DeleteFile(NULL, SS_NEW_COMPRESSED_FILE); + LOGL(LOG_SSENGINE, "Starting New file upgrade for [%s]\n", patch_path); + if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, (char *)patch_path, SS_NEW_COMPRESSED_FILE) >= + 0) + if (_7zdecompress(SS_NEW_COMPRESSED_FILE) == 0) + LOGL(LOG_SSENGINE, "7zip extracted successfully %s\n", ua_dataSS->parti_info->ua_parti_name); + else + LOGL(LOG_SSENGINE, "7zip extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); + else + LOGL(LOG_SSENGINE, "tar extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); + SS_DeleteFile(NULL, SS_NEW_COMPRESSED_FILE); #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for NEWFILES - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for NEWFILES - [%d] \n", (t2 - t1)); #endif - } - break; - case SYMDIFFS: - { + } + break; + case SYMDIFFS: + { #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif - while (pFsNode) { - LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++); - //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name); - ulResult = SS_Unlink(NULL, pFsNode->file_old_path); - if (ulResult == S_SS_SUCCESS) - ulResult = SS_Link(NULL, pFsNode->file_new_path, pFsNode->patch_name); - else { - LOGE("Unlink Failed\n"); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - break; - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } + while (pFsNode) { + LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++); + //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name); + ulResult = SS_Unlink(NULL, pFsNode->file_old_path); + if (ulResult == S_SS_SUCCESS) + ulResult = SS_Link(NULL, pFsNode->file_new_path, pFsNode->patch_name); + else { + LOGE("Unlink Failed\n"); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + break; + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + pFsNode = pFsNode->nextnode; + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for SYMDIFFS - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for SYMDIFFS - [%d] \n", (t2 - t1)); #endif - } - break; - case SYMNEWFILES: - { - fs_params *head_node; - int retry_count = 0, do_retry = 0; + } + break; + case SYMNEWFILES: + { + fs_params *head_node; + int retry_count = 0, do_retry = 0; #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t1 = atoi(ts1); + get_time_stamp1(); //total time capturing + t1 = atoi(ts1); #endif SYMLINK_CREATE: - head_node = pFsNode; - while (head_node) { - LOGL(LOG_SSENGINE, "SYMNEWS update Index: [%d] \n", ulFileIndex++); - snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s%s", patch_path, "/", head_node->patch_name); - LOGL(LOG_SSENGINE, "Sym New file paths: [Linkname - %s] [reference file name- %s][]\n", - head_node->file_old_path, head_node->patch_name); - ulResult = SS_Link(NULL, head_node->file_old_path, head_node->patch_name); - if (ulResult == E_SS_FAILURE) { - LOGE("Link Failed\n"); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - break; - } else if (ulResult == ENOENT) //to handle cases where new symlink points to a new symlink yet to be created - { - do_retry = 1; //we will retry the failed symlinks with error 2 (no file or dir) again after this cycle - //SS_UpdateUIProgress(ua_dataSS,ulPatchCount); - head_node = head_node->nextnode; - continue; - - } - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - head_node = head_node->nextnode; - } - if (do_retry && (retry_count < 4)) { - retry_count++; - ulFileIndex = 0; - do_retry = 0; - goto SYMLINK_CREATE; - } else if (do_retry && (retry_count >= 4)) //retry to be done maximum 4 times - { - LOGE("Link Failed after %d retrys\n", retry_count); - //SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - break; - } + head_node = pFsNode; + while (head_node) { + LOGL(LOG_SSENGINE, "SYMNEWS update Index: [%d] \n", ulFileIndex++); + snprintf(ubPatch, SS_MAX_FILE_PATH, "%s%s%s", patch_path, "/", head_node->patch_name); + LOGL(LOG_SSENGINE, "Sym New file paths: [Linkname - %s] [reference file name- %s][]\n", + head_node->file_old_path, head_node->patch_name); + ulResult = SS_Link(NULL, head_node->file_old_path, head_node->patch_name); + if (ulResult == E_SS_FAILURE) { + LOGE("Link Failed\n"); + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + break; + } else if (ulResult == ENOENT) { //to handle cases where new symlink points to a new symlink yet to be created + do_retry = 1; //we will retry the failed symlinks with error 2 (no file or dir) again after this cycle + //SS_UpdateUIProgress(ua_dataSS,ulPatchCount); + head_node = head_node->nextnode; + continue; + } + SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); + head_node = head_node->nextnode; + } + if (do_retry && (retry_count < 4)) { + retry_count++; + ulFileIndex = 0; + do_retry = 0; + goto SYMLINK_CREATE; + } else if (do_retry && (retry_count >= 4)) { //retry to be done maximum 4 times + LOGE("Link Failed after %d retrys\n", retry_count); + //SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + break; + } #ifdef TIME_PROFILING - get_time_stamp1(); //total time capturing - t2 = atoi(ts1); - LOG("Shirsh time for SYMNEWS - [%d] \n", (t2 - t1)); + get_time_stamp1(); //total time capturing + t2 = atoi(ts1); + LOG("Shirsh time for SYMNEWS - [%d] \n", (t2 - t1)); #endif - } - break; - default: - break; - } - return ulResult; + } + break; + default: + break; + } + return ulResult; } #ifdef MEM_PROFILING @@ -1524,7 +1515,7 @@ extern int cur_mem; #endif /*! ********************************************************************************* - * SS_FSUpdatemain + * SS_FSUpdatemain ********************************************************************************* * * @brief @@ -1541,112 +1532,112 @@ extern int cur_mem; int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) { - int ulResult = S_SS_SUCCESS; - fs_list *head_ptr_node = NULL; - char new_patch_path[SS_MAX_FILE_PATH] = { - 0 - }; - - if (!ua_dataSS) - return E_SS_BAD_PARAMS; // Set error ?? - head_ptr_node = headptr_list[part_idx]; - - if (!head_ptr_node) { //in case of power failure, try rebilding nodes again - SS_FSVerifyPartition(ua_dataSS, part_idx); - head_ptr_node = headptr_list[part_idx]; - } - - if (!head_ptr_node) - return E_SS_FSBADNODES; - - SS_GetPartition_LocDetails(ua_dataSS, part_idx); - - LOGL(LOG_SSENGINE, "FS Update Entry PartIndex: [%d]\n", part_idx); - - if (head_ptr_node->del_ref == NULL) { - LOGL(LOG_SSENGINE, "No DEL header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(DELETES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (head_ptr_node->dif_ref == NULL) { - LOGL(LOG_SSENGINE, "No DIFF header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(DIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->dif_ref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (head_ptr_node->move_ref == NULL) { - LOGL(LOG_SSENGINE, "No MOVE header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(MOVES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->move_ref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (head_ptr_node->del_ref == NULL) { - LOGL(LOG_SSENGINE, "No DEL header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(DELETE_END, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (ulResult == S_SS_SUCCESS) { - //new file extraction start - snprintf(new_patch_path, SS_MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE); // subject name wil have fw slash as part of cfg file - LOGL(LOG_SSENGINE, "File path created to extract new files : [%s]\n", new_patch_path); - ulResult = - SS_FSUpdateFile(NEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->new_ref, new_patch_path); - //new file extraction end - } - - if (head_ptr_node->sym_difref == NULL) { - LOGL(LOG_SSENGINE, "No SYMDIFF header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(SYMDIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_difref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (head_ptr_node->sym_newref == NULL) { - LOGL(LOG_SSENGINE, "No SYMNEW header\n"); - } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(SYMNEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_newref, - ua_dataSS->update_delta->ua_patch_path); - } - - if (ulResult == S_SS_SUCCESS) - ulResult = SS_FSSetAttributes(ua_dataSS); - sync(); - sleep(1); - SS_FSClearNodes(part_idx); - - if (ulResult == S_SS_SUCCESS) - SS_UpdateUIProgress(ua_dataSS, 0, 1); //fix WGID : 51963, When all updates are done to FS , patchcount is not needed, passing 1 to 3rd arg is enough - - LOGL(LOG_SSENGINE, "FS update Complete PartIndex: [%d]\n", part_idx); + int ulResult = S_SS_SUCCESS; + fs_list *head_ptr_node = NULL; + char new_patch_path[SS_MAX_FILE_PATH] = { + 0 + }; + + if (!ua_dataSS) + return E_SS_BAD_PARAMS; // Set error ?? + head_ptr_node = headptr_list[part_idx]; + + if (!head_ptr_node) { //in case of power failure, try rebilding nodes again + SS_FSVerifyPartition(ua_dataSS, part_idx); + head_ptr_node = headptr_list[part_idx]; + } + + if (!head_ptr_node) + return E_SS_FSBADNODES; + + SS_GetPartition_LocDetails(ua_dataSS, part_idx); + + LOGL(LOG_SSENGINE, "FS Update Entry PartIndex: [%d]\n", part_idx); + + if (head_ptr_node->del_ref == NULL) { + LOGL(LOG_SSENGINE, "No DEL header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(DELETES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (head_ptr_node->dif_ref == NULL) { + LOGL(LOG_SSENGINE, "No DIFF header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(DIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->dif_ref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (head_ptr_node->move_ref == NULL) { + LOGL(LOG_SSENGINE, "No MOVE header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(MOVES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->move_ref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (head_ptr_node->del_ref == NULL) { + LOGL(LOG_SSENGINE, "No DEL header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(DELETE_END, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (ulResult == S_SS_SUCCESS) { + //new file extraction start + snprintf(new_patch_path, SS_MAX_FILE_PATH, "%s%s", ua_dataSS->parti_info->ua_subject_name, SS_COMPRESSED_FILE); // subject name wil have fw slash as part of cfg file + LOGL(LOG_SSENGINE, "File path created to extract new files : [%s]\n", new_patch_path); + ulResult = + SS_FSUpdateFile(NEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->new_ref, new_patch_path); + //new file extraction end + } + + if (head_ptr_node->sym_difref == NULL) { + LOGL(LOG_SSENGINE, "No SYMDIFF header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(SYMDIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_difref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (head_ptr_node->sym_newref == NULL) { + LOGL(LOG_SSENGINE, "No SYMNEW header\n"); + } else if (ulResult == S_SS_SUCCESS) { + ulResult = + SS_FSUpdateFile(SYMNEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_newref, + ua_dataSS->update_delta->ua_patch_path); + } + + if (ulResult == S_SS_SUCCESS) + ulResult = SS_FSSetAttributes(ua_dataSS); + sync(); + sleep(1); + SS_FSClearNodes(part_idx); + + if (ulResult == S_SS_SUCCESS) + SS_UpdateUIProgress(ua_dataSS, 0, 1); //fix WGID : 51963, When all updates are done to FS , patchcount is not needed, passing 1 to 3rd arg is enough + + LOGL(LOG_SSENGINE, "FS update Complete PartIndex: [%d]\n", part_idx); #ifdef MEM_PROFILING - LOGL(LOG_SSENGINE, "Stats are : Cur Max : [%d] Global Max : [%d]\n", cur_mem, max_mem); + LOGL(LOG_SSENGINE, "Stats are : Cur Max : [%d] Global Max : [%d]\n", cur_mem, max_mem); #endif - if (ulResult == S_SS_SUCCESS) - return ulResult; - else - return SS_GetUpgradeState(); + if (ulResult == S_SS_SUCCESS) + return ulResult; + else + return SS_GetUpgradeState(); } /*! ********************************************************************************* - * SS_FSUpdatemain + * SS_FSUpdatemain ********************************************************************************* * * @brief * This is the API exposed from the engine to update FS. - * FS entry function for updating FS partition. Should be invoked only after verification of the partition + * FS entry function for updating FS partition. Should be invoked only after verification of the partition * * * @param Requires common data structure having all details & Partition Index. @@ -1661,257 +1652,253 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) size_t SS_FSAvailiableFreeSpace(char *block_name) { - struct mntent *ent; - FILE *aFile; - struct statfs sb; - aFile = setmntent("/proc/mounts", "r"); - if (aFile == NULL) { - LOGE("setmntent error"); - return E_SS_FAILURE; - } - while (NULL != (ent = getmntent(aFile))) { - if (strcmp(ent->mnt_fsname, block_name) == 0) { - if (statfs(ent->mnt_dir, &sb) == 0) { - LOGL(LOG_SSENGINE, "Total free space = %d, blocks free = %d\n", sb.f_bsize * sb.f_bavail, sb.f_bfree); - } - } - } - endmntent(aFile); - return sb.f_bsize * sb.f_bavail; + struct mntent *ent; + FILE *aFile; + struct statfs sb; + aFile = setmntent("/proc/mounts", "r"); + if (aFile == NULL) { + LOGE("setmntent error"); + return E_SS_FAILURE; + } + while (NULL != (ent = getmntent(aFile))) { + if (strcmp(ent->mnt_fsname, block_name) == 0) { + if (statfs(ent->mnt_dir, &sb) == 0) + LOGL(LOG_SSENGINE, "Total free space = %d, blocks free = %d\n", sb.f_bsize * sb.f_bavail, sb.f_bfree); + } + } + endmntent(aFile); + return sb.f_bsize * sb.f_bavail; } int SS_FSVerifyPartition(ua_dataSS_t * ua_dataSS, int part_idx) { - int ulResult = S_SS_SUCCESS; - size_t free_space = 0; - if (!ua_dataSS) { - LOGE("Wrong Param for fs verification\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - - LOGL(LOG_SSENGINE, "FS max free mem reqired : [%d]\n", ua_dataSS->update_cfg->soure_img_size); - free_space = SS_FSAvailiableFreeSpace(ua_dataSS->parti_info->ua_blk_name); - if (free_space != E_SS_FAILURE) { - //Source img size is max single file size for a file system under upgrade, which is updated in CFG file by UPG - if ((free_space) < (ua_dataSS->update_cfg->soure_img_size + ua_dataSS->update_cfg->soure_img_size / 10)) { - LOGE("Not enough free space [%d] for max size [%d]\n", free_space, - (ua_dataSS->update_cfg->soure_img_size + ua_dataSS->update_cfg->soure_img_size / 10)); - //SS_SetUpgradeState(E_SS_FSMEMORYERROR); - //return E_SS_FAILURE; - } else - LOGL(LOG_SSENGINE, "Enough space for Partition [%s]\n", ua_dataSS->parti_info->ua_parti_name); - } - - SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); - //Checking for 2 times the file size free space , as delta can be worst case size of file. - if ((free_space) < (2 * ua_dataSS->update_cfg->soure_img_size)) { - LOGE("Not enough free space [%d] for max size [%d]\n", free_space, (2 * ua_dataSS->update_cfg->soure_img_size)); - SS_SetUpgradeState(E_SS_FSMEMORYERROR); - return E_SS_FAILURE; - } + int ulResult = S_SS_SUCCESS; + size_t free_space = 0; + if (!ua_dataSS) { + LOGE("Wrong Param for fs verification\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + + LOGL(LOG_SSENGINE, "FS max free mem reqired : [%d]\n", ua_dataSS->update_cfg->soure_img_size); + free_space = SS_FSAvailiableFreeSpace(ua_dataSS->parti_info->ua_blk_name); + if (free_space != E_SS_FAILURE) { + //Source img size is max single file size for a file system under upgrade, which is updated in CFG file by UPG + if ((free_space) < (ua_dataSS->update_cfg->soure_img_size + ua_dataSS->update_cfg->soure_img_size / 10)) { + LOGE("Not enough free space [%d] for max size [%d]\n", free_space, + (ua_dataSS->update_cfg->soure_img_size + ua_dataSS->update_cfg->soure_img_size / 10)); + //SS_SetUpgradeState(E_SS_FSMEMORYERROR); + //return E_SS_FAILURE; + } else + LOGL(LOG_SSENGINE, "Enough space for Partition [%s]\n", ua_dataSS->parti_info->ua_parti_name); + } + + SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); + //Checking for 2 times the file size free space , as delta can be worst case size of file. + if ((free_space) < (2 * ua_dataSS->update_cfg->soure_img_size)) { + LOGE("Not enough free space [%d] for max size [%d]\n", free_space, (2 * ua_dataSS->update_cfg->soure_img_size)); + SS_SetUpgradeState(E_SS_FSMEMORYERROR); + return E_SS_FAILURE; + } #ifdef MEM_PROFILING - if (!mem_profiling_start) - if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling())) - return E_SS_FAILURE; + if (!mem_profiling_start) + if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling())) + return E_SS_FAILURE; #endif - SS_GetPartition_LocDetails(ua_dataSS, part_idx); - LOGL(LOG_SSENGINE, "FS Verification Start PartIndex:[%d]\n", part_idx); - if (ua_dataSS->ua_operation == UI_OP_SCOUT) - gvalid_session = TRUE; // (shd b true if called during verification) - headptr_list[part_idx] = SS_FSBuildNodes(ua_dataSS); + SS_GetPartition_LocDetails(ua_dataSS, part_idx); + LOGL(LOG_SSENGINE, "FS Verification Start PartIndex:[%d]\n", part_idx); + if (ua_dataSS->ua_operation == UI_OP_SCOUT) + gvalid_session = TRUE; // (shd b true if called during verification) + headptr_list[part_idx] = SS_FSBuildNodes(ua_dataSS); #ifdef TIME_PROFILING - LOGL(LOG_SSENGINE, "fast_tar_get_item_size_time :[%lf]\n", fast_tar_get_item_size_time); - LOGL(LOG_SSENGINE, "SS_LoadFile_time :[%lf]\n", SS_LoadFile_time); - LOGL(LOG_SSENGINE, "SS_FSBuildNodes_time :[%lf]\n", SS_FSBuildNodes_time); + LOGL(LOG_SSENGINE, "fast_tar_get_item_size_time :[%lf]\n", fast_tar_get_item_size_time); + LOGL(LOG_SSENGINE, "SS_LoadFile_time :[%lf]\n", SS_LoadFile_time); + LOGL(LOG_SSENGINE, "SS_FSBuildNodes_time :[%lf]\n", SS_FSBuildNodes_time); #endif - if (!headptr_list[part_idx]) { - LOGE("FS Verification Failed PartIndex: [%d]\n", part_idx); - SS_FSClearNodes(part_idx); - ulResult = E_SS_FAILURE; - } - - if (ulResult == S_SS_SUCCESS) - return ulResult; - else - return SS_GetUpgradeState(); + if (!headptr_list[part_idx]) { + LOGE("FS Verification Failed PartIndex: [%d]\n", part_idx); + SS_FSClearNodes(part_idx); + ulResult = E_SS_FAILURE; + } + + if (ulResult == S_SS_SUCCESS) + return ulResult; + else + return SS_GetUpgradeState(); } //Should check if space is available???? int SS_BackupSource(const char *source_filename) { - int ret = E_SS_FAILURE; - - if (source_filename) { - ret = (int)SS_CopyFile(NULL, source_filename, SS_BACKUP_SOURCE); - if (ret != S_SS_SUCCESS) { - LOGE("failed to back up source file Error [%d]\n", ret); - SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); - } - } - return ret; + int ret = E_SS_FAILURE; + + if (source_filename) { + ret = (int)SS_CopyFile(NULL, source_filename, SS_BACKUP_SOURCE); + if (ret != S_SS_SUCCESS) { + LOGE("failed to back up source file Error [%d]\n", ret); + SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); + } + } + return ret; } int SS_BackupSourceClear(void) { - int ret = E_SS_FAILURE; - ret = (int)SS_DeleteFile(NULL, SS_BACKUP_SOURCE); - if (ret != S_SS_SUCCESS) { - LOGE("failed to delete BACKUP file\n"); - SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); - } - return ret; + int ret = E_SS_FAILURE; + ret = (int)SS_DeleteFile(NULL, SS_BACKUP_SOURCE); + if (ret != S_SS_SUCCESS) { + LOGE("failed to delete BACKUP file\n"); + SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); + } + return ret; } int SS_PatchSourceClear(void) { - int ret = E_SS_FAILURE; - ret = (int)SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); - if (ret != S_SS_SUCCESS) { - LOGE("failed to delete PATCHFILE file\n"); - SS_SetUpgradeState(E_SS_PATCHFILE_DEL_ERROR); - } - return ret; + int ret = E_SS_FAILURE; + ret = (int)SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); + if (ret != S_SS_SUCCESS) { + LOGE("failed to delete PATCHFILE file\n"); + SS_SetUpgradeState(E_SS_PATCHFILE_DEL_ERROR); + } + return ret; } int SS_IMGVerifyFullImage(ua_dataSS_t * ua_dataSS) { - int read_cnt = 0, patch_size = 0; - FileInfo source_file; - uint8_t target_sha1[SHA_DIGEST_SIZE]; - int ulResult = S_SS_SUCCESS; - - if (!(ua_dataSS && ua_dataSS->update_cfg && ua_dataSS->parti_info && ua_dataSS->parti_info->ua_blk_name - && !(ua_dataSS->update_cfg->skip_verify == 1))) { - LOGE("Bad structure or members\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - - LOGL(LOG_SSENGINE, "FULL IMG Verification Entry BlkName:[%s]\n", ua_dataSS->parti_info->ua_blk_name); - - if (ua_dataSS->update_data && ua_dataSS->parti_info && ua_dataSS->update_data->ua_delta_path - && ua_dataSS->parti_info->ua_subject_name) - patch_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name); - else { - LOGE("Bad structure members in ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - if (ua_dataSS->update_cfg && ua_dataSS->update_cfg->soure_img_size && ua_dataSS->update_cfg->target_sha1) - LOGL(LOG_SSENGINE, "\nParams -image size [%d] sha1 [%s]\n", - ua_dataSS->update_cfg->soure_img_size, ua_dataSS->update_cfg->target_sha1); - else { - LOGE("Bad structure member update_cfg in ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); - SS_SetUpgradeState(E_SS_IMGBADDELTA); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - - if ((patch_size) > 0) - read_cnt = - tar_extract_file(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name, - SS_PATCHFILE_SOURCE); - - if (read_cnt <= 0) { - LOGL(LOG_SSENGINE, "Failed to read delta\n"); - SS_SetUpgradeState(E_SS_IMGBADDELTA); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - if (SS_LoadFile(SS_PATCHFILE_SOURCE, &source_file) == 0) { - if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "Patch Can be applied\n"); - SS_Free(source_file.data); - ulResult = S_SS_SUCCESS; - } - else{ - LOGL(LOG_SSENGINE, "Patch Cannot be applied\n"); - SS_Free(source_file.data); - SS_SetUpgradeState(E_SS_IMGBADDELTA); - ulResult = E_SS_FAILURE; - goto Cleanup; - } - } - else{ - LOGL(LOG_SSENGINE, "Failed to LoadFile\n"); - SS_SetUpgradeState(E_SS_IMGBADDELTA); - ulResult = E_SS_FAILURE; - } + int read_cnt = 0, patch_size = 0; + FileInfo source_file; + uint8_t target_sha1[SHA_DIGEST_SIZE]; + int ulResult = S_SS_SUCCESS; + + if (!(ua_dataSS && ua_dataSS->update_cfg && ua_dataSS->parti_info && + ua_dataSS->parti_info->ua_blk_name && + !(ua_dataSS->update_cfg->skip_verify == 1))) { + LOGE("Bad structure or members\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + + LOGL(LOG_SSENGINE, "FULL IMG Verification Entry BlkName:[%s]\n", ua_dataSS->parti_info->ua_blk_name); + + if (ua_dataSS->update_data && ua_dataSS->parti_info && ua_dataSS->update_data->ua_delta_path + && ua_dataSS->parti_info->ua_subject_name) { + patch_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name); + } else { + LOGE("Bad structure members in ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + if (ua_dataSS->update_cfg && ua_dataSS->update_cfg->soure_img_size && ua_dataSS->update_cfg->target_sha1) { + LOGL(LOG_SSENGINE, "\nParams -image size [%d] sha1 [%s]\n", + ua_dataSS->update_cfg->soure_img_size, ua_dataSS->update_cfg->target_sha1); + } else { + LOGE("Bad structure member update_cfg in ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { + LOGE("failed to parse tgt-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); + SS_SetUpgradeState(E_SS_IMGBADDELTA); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + + if ((patch_size) > 0) + read_cnt = tar_extract_file(ua_dataSS->update_data->ua_delta_path, + ua_dataSS->parti_info->ua_subject_name, SS_PATCHFILE_SOURCE); + + if (read_cnt <= 0) { + LOGL(LOG_SSENGINE, "Failed to read delta\n"); + SS_SetUpgradeState(E_SS_IMGBADDELTA); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + if (SS_LoadFile(SS_PATCHFILE_SOURCE, &source_file) == 0) { + if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "Patch Can be applied\n"); + SS_Free(source_file.data); + ulResult = S_SS_SUCCESS; + } else{ + LOGL(LOG_SSENGINE, "Patch Cannot be applied\n"); + SS_Free(source_file.data); + SS_SetUpgradeState(E_SS_IMGBADDELTA); + ulResult = E_SS_FAILURE; + goto Cleanup; + } + } else { + LOGL(LOG_SSENGINE, "Failed to LoadFile\n"); + SS_SetUpgradeState(E_SS_IMGBADDELTA); + ulResult = E_SS_FAILURE; + } Cleanup: - if(file_exist(SS_PATCHFILE_SOURCE, 0)) - SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); - return ulResult; + if (file_exist(SS_PATCHFILE_SOURCE, 0)) + SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); + return ulResult; } int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS) { - FileInfo source_file; - int ulResult = S_SS_SUCCESS; - uint8_t source_sha1[SHA_DIGEST_SIZE]; - uint8_t target_sha1[SHA_DIGEST_SIZE]; - size_t free_space = 0; - - if (!(ua_dataSS && ua_dataSS->update_cfg && ua_dataSS->parti_info && ua_dataSS->parti_info->ua_blk_name)) { - LOGE("Bad structure or members\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - - //We verify twice the image size for BACKUP source, not on Partition. As Patch will be created on RAM - SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); - if ((free_space) < (2 * ua_dataSS->update_cfg->target_img_size)) { - LOGE("Not enough free space [%d] for twice max size [%d]\n", free_space, - (2 * ua_dataSS->update_cfg->target_img_size)); - SS_SetUpgradeState(E_SS_FSMEMORYERROR); - return E_SS_FAILURE; - } - - if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { - LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); - SS_SetUpgradeState(E_SS_SHAPRASE_FAILED); - return E_SS_FAILURE; - } - // corner case, Parsing sha can fail if update.cfg is wrong/manually edited - if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { - LOGE("failed to parse Target-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); - SS_SetUpgradeState(E_SS_SHAPRASE_FAILED); - return E_SS_FAILURE; - } - - source_file.size = ua_dataSS->update_cfg->soure_img_size; - source_file.data = NULL; - if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { - if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_IMGVerfiyPartition - SHA matches with source [%s] \n", - ua_dataSS->parti_info->ua_blk_name); - } else // Need not compare with Target sha as once upgraded, it should NOT verify same partition again. - { - SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); - ulResult = E_SS_FAILURE; - } - } - SS_Free(source_file.data); - if (ulResult == S_SS_SUCCESS) { - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, 100); - return ulResult; - } else - return SS_GetUpgradeState(); + FileInfo source_file; + int ulResult = S_SS_SUCCESS; + uint8_t source_sha1[SHA_DIGEST_SIZE]; + uint8_t target_sha1[SHA_DIGEST_SIZE]; + size_t free_space = 0; + + if (!(ua_dataSS && ua_dataSS->update_cfg && ua_dataSS->parti_info && ua_dataSS->parti_info->ua_blk_name)) { + LOGE("Bad structure or members\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + + //We verify twice the image size for BACKUP source, not on Partition. As Patch will be created on RAM + SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); + if ((free_space) < (2 * ua_dataSS->update_cfg->target_img_size)) { + LOGE("Not enough free space [%d] for twice max size [%d]\n", free_space, + (2 * ua_dataSS->update_cfg->target_img_size)); + SS_SetUpgradeState(E_SS_FSMEMORYERROR); + return E_SS_FAILURE; + } + + if (ParseSha1(ua_dataSS->update_cfg->soure_sha1, source_sha1) != 0) { + LOGE("failed to parse Src-sha1 \"%s\"\n", ua_dataSS->update_cfg->soure_sha1); + SS_SetUpgradeState(E_SS_SHAPRASE_FAILED); + return E_SS_FAILURE; + } + // corner case, Parsing sha can fail if update.cfg is wrong/manually edited + if (ParseSha1(ua_dataSS->update_cfg->target_sha1, target_sha1) != 0) { + LOGE("failed to parse Target-sha1 \"%s\"\n", ua_dataSS->update_cfg->target_sha1); + SS_SetUpgradeState(E_SS_SHAPRASE_FAILED); + return E_SS_FAILURE; + } + + source_file.size = ua_dataSS->update_cfg->soure_img_size; + source_file.data = NULL; + if (SS_LoadPartition(ua_dataSS->parti_info->ua_blk_name, &source_file) == 0) { + if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "SS_IMGVerfiyPartition - SHA matches with source [%s] \n", + ua_dataSS->parti_info->ua_blk_name); + } else { // Need not compare with Target sha as once upgraded, it should NOT verify same partition again. + SS_SetUpgradeState(E_SS_IMGSRCCURRUPTED); + ulResult = E_SS_FAILURE; + } + } + SS_Free(source_file.data); + if (ulResult == S_SS_SUCCESS) { + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, 100); + return ulResult; + } else + return SS_GetUpgradeState(); } /*! ********************************************************************************* - * SS_IMGUpdatemain + * SS_IMGUpdatemain ********************************************************************************* * * @brief @@ -1928,78 +1915,77 @@ int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS) int SS_IMGUpdatemain(ua_dataSS_t * ua_dataSS, int update_type) //SS_FSUpdatePartition { - int read_cnt = 0, patch_size; - int ulResult = S_SS_SUCCESS; - - //sprintf(Blk_name, "%s%s%s","EMMC",":", ua_dataSS->parti_info->ua_blk_name); - //LOGL(LOG_SSENGINE, "IMG Upgrade Entry BlkName:[%s]\n",Blk_name); - if (!ua_dataSS) { - LOGE("Bad structure ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, "IMG Upgrade Entry BlkName:[%s]\n", ua_dataSS->parti_info->ua_blk_name); - - if (ua_dataSS->update_data && ua_dataSS->parti_info && ua_dataSS->update_data->ua_delta_path - && ua_dataSS->parti_info->ua_subject_name) - patch_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name); - else { - LOGE("Bad structure members in ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - if (ua_dataSS->update_cfg && ua_dataSS->update_cfg->soure_img_size && ua_dataSS->update_cfg->target_sha1) - LOGL(LOG_SSENGINE, "\n SS_IMGUpdatemain Params -source size [%d] sha1 [%s]", - ua_dataSS->update_cfg->soure_img_size, ua_dataSS->update_cfg->target_sha1); - else { - LOGE("Bad structure member update_cfg in ua_dataSS\n"); - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - - if ((patch_size) > 0) - read_cnt = - tar_extract_file(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name, - SS_PATCHFILE_SOURCE); - - if (read_cnt <= 0) { - ulResult = E_SS_FAILURE; - SS_SetUpgradeState(E_SS_IMGBADDELTA); - return E_SS_FAILURE; //ulResult; - } - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, 40); - - if (update_type == FULL_IMG && ua_dataSS->update_data->ua_temp_path) - ulResult = SS_MoveFile(ua_dataSS, SS_PATCHFILE_SOURCE, ua_dataSS->update_data->ua_temp_path); - else if ((ua_dataSS->update_cfg->update_type == DELTA_IMG && ua_dataSS->write_data_to_blkdev) - || ua_dataSS->update_cfg->update_type == EXTRA) { - - FILE *fp = NULL; - char buf[14] = { 0, }; //to store zImage-delta magic keyword - ssize_t bytes_read; - fp = fopen(SS_PATCHFILE_SOURCE, "r"); - bytes_read = fread(buf, 1, 13, fp); //error check not required as any delta corruption will be caught in SS_UpdateDeltaIMG - if (bytes_read != 13) - LOGL(LOG_SSENGINE, "short read of \"%s\" (%ld bytes of %ld)\n", SS_PATCHFILE_SOURCE, (long)bytes_read, (long)13); - fclose(fp); - - if (strcmp(buf, SS_KERNEL_MAGIC) == 0) - ulResult = SS_UpdateDeltaKernel(ua_dataSS, ua_dataSS->write_data_to_blkdev); - else - ulResult = SS_UpdateDeltaIMG(ua_dataSS, ua_dataSS->write_data_to_blkdev); - } - else { - LOGE("Update type is INVALID - Exit \n"); - ulResult = E_SS_FAILURE; - SS_SetUpgradeState(E_SS_BAD_PARAMS); - return E_SS_FAILURE; - } - - if (ulResult == S_SS_SUCCESS) { - if (ua_dataSS->ui_progress) - ua_dataSS->ui_progress(ua_dataSS, 100); - return ulResult; - } else - return SS_GetUpgradeState(); + int read_cnt = 0, patch_size; + int ulResult = S_SS_SUCCESS; + + //sprintf(Blk_name, "%s%s%s","EMMC",":", ua_dataSS->parti_info->ua_blk_name); + //LOGL(LOG_SSENGINE, "IMG Upgrade Entry BlkName:[%s]\n",Blk_name); + if (!ua_dataSS) { + LOGE("Bad structure ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + LOGL(LOG_SSENGINE, "IMG Upgrade Entry BlkName:[%s]\n", ua_dataSS->parti_info->ua_blk_name); + + if (ua_dataSS->update_data && ua_dataSS->parti_info && ua_dataSS->update_data->ua_delta_path + && ua_dataSS->parti_info->ua_subject_name) + patch_size = tar_get_item_size(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name); + else { + LOGE("Bad structure members in ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + if (ua_dataSS->update_cfg && ua_dataSS->update_cfg->soure_img_size && ua_dataSS->update_cfg->target_sha1) + LOGL(LOG_SSENGINE, "\n SS_IMGUpdatemain Params -source size [%d] sha1 [%s]", + ua_dataSS->update_cfg->soure_img_size, ua_dataSS->update_cfg->target_sha1); + else { + LOGE("Bad structure member update_cfg in ua_dataSS\n"); + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + + if ((patch_size) > 0) + read_cnt = + tar_extract_file(ua_dataSS->update_data->ua_delta_path, ua_dataSS->parti_info->ua_subject_name, + SS_PATCHFILE_SOURCE); + + if (read_cnt <= 0) { + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_IMGBADDELTA); + return E_SS_FAILURE; //ulResult; + } + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, 40); + + if (update_type == FULL_IMG && ua_dataSS->update_data->ua_temp_path) + ulResult = SS_MoveFile(ua_dataSS, SS_PATCHFILE_SOURCE, ua_dataSS->update_data->ua_temp_path); + else if ((ua_dataSS->update_cfg->update_type == DELTA_IMG && ua_dataSS->write_data_to_blkdev) + || ua_dataSS->update_cfg->update_type == EXTRA) { + + FILE *fp = NULL; + char buf[14] = { 0, }; //to store zImage-delta magic keyword + ssize_t bytes_read; + fp = fopen(SS_PATCHFILE_SOURCE, "r"); + bytes_read = fread(buf, 1, 13, fp); //error check not required as any delta corruption will be caught in SS_UpdateDeltaIMG + if (bytes_read != 13) + LOGL(LOG_SSENGINE, "short read of \"%s\" (%ld bytes of %ld)\n", SS_PATCHFILE_SOURCE, (long)bytes_read, (long)13); + fclose(fp); + + if (strcmp(buf, SS_KERNEL_MAGIC) == 0) + ulResult = SS_UpdateDeltaKernel(ua_dataSS, ua_dataSS->write_data_to_blkdev); + else + ulResult = SS_UpdateDeltaIMG(ua_dataSS, ua_dataSS->write_data_to_blkdev); + } else { + LOGE("Update type is INVALID - Exit \n"); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_BAD_PARAMS); + return E_SS_FAILURE; + } + + if (ulResult == S_SS_SUCCESS) { + if (ua_dataSS->ui_progress) + ua_dataSS->ui_progress(ua_dataSS, 100); + return ulResult; + } else + return SS_GetUpgradeState(); } diff --git a/ss_engine/SS_UPI.h b/ss_engine/SS_UPI.h index 8d3fabd..8a4e351 100755 --- a/ss_engine/SS_UPI.h +++ b/ss_engine/SS_UPI.h @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -22,43 +22,43 @@ #define DISPLAYRESOLUTION_SIZE 50 struct details { - int diffs; - int moves; - int news; - int deletes; - int symdiffs; - int symnews; + int diffs; + int moves; + int news; + int deletes; + int symdiffs; + int symnews; }; enum DEL_TYPE { DIFFS, MOVES, NEWFILES, DELETES, SYMDIFFS, SYMNEWFILES, DELETE_END }; -struct fs_params { // Use Macros - char file_old_path[512]; - char file_new_path[512]; - char patch_name[256]; - char sha1src[64]; - char sha1trg[64]; - int data_size; - int data_offset; - int type; //0 is for diff and 1 is for verbatim - struct fs_params *nextnode; +struct fs_params { // Use Macros + char file_old_path[512]; + char file_new_path[512]; + char patch_name[256]; + char sha1src[64]; + char sha1trg[64]; + int data_size; + int data_offset; + int type; //0 is for diff and 1 is for verbatim + struct fs_params *nextnode; }; typedef struct fs_params fs_params; struct fs_list { - fs_params *dif_ref; - fs_params *move_ref; - fs_params *new_ref; - fs_params *del_ref; - fs_params *sym_difref; - fs_params *sym_newref; - int ulPatchCount; + fs_params *dif_ref; + fs_params *move_ref; + fs_params *new_ref; + fs_params *del_ref; + fs_params *sym_difref; + fs_params *sym_newref; + int ulPatchCount; }; typedef struct fs_list fs_list; struct details *get_fs_details(char *filename); int SS_AppendNode(const char *ubDeltaPath, fs_params ** headparam, fs_params ** tailparam, const char *old_path, - const char *new_path, const char *patchname, const char *sha1src, const char *sha1trg, int type, - char *patchpath_name); + const char *new_path, const char *patchname, const char *sha1src, const char *sha1trg, int type, + char *patchpath_name); extern int SS_IMGUpdatemain(ua_dataSS_t * ua_dataSS, int update_type); extern int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS); extern int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx); @@ -77,10 +77,10 @@ extern int tar_open(char *tar); extern int fast_tar_extract_file(char *tar, char *item, char *pathname, int size, int offset); extern int tar_close(); extern int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, - const char *source_sha1_str, const char *target_sha1_str, int patch_data_size); + const char *source_sha1_str, const char *target_sha1_str, int patch_data_size); extern int tar_extract_file(char *tar, char *item, char *pathname); extern int _7zdecompress(char *path); extern void tar_free_cfg_table(tar_Data_t ** delta_tar); extern long SS_GetFileType(void *pbUserData, char *pLinkName, enumFileType * fileType); -#endif //_SS_UPI_H_ +#endif //_SS_UPI_H_ diff --git a/ss_engine/fota_common.h b/ss_engine/fota_common.h index 91af6fa..6dbe909 100755 --- a/ss_engine/fota_common.h +++ b/ss_engine/fota_common.h @@ -58,92 +58,92 @@ typedef unsigned long long u64; #define UNUSED(x) (void)(x) -#define SS_TOTA_VERSION "1.0.18" -#define BSDIFF "BSDIFF40" -#define IMGDIFF "IMGDIFF2" -#define SECTOR_SIZE 512 +#define SS_TOTA_VERSION "1.0.18" +#define BSDIFF "BSDIFF40" +#define IMGDIFF "IMGDIFF2" +#define SECTOR_SIZE 512 #define SS_KERNEL_DELTA_HEADER 128 #define SS_COMMON_WORKSPACE "/system/opt/usr/data/fota" #define SS_KERNEL_UNPACK_SCRIPT "unpack.sh" #define SS_KERN_UNPK_SCRIPT_PATH SS_COMMON_WORKSPACE "/" SS_KERNEL_UNPACK_SCRIPT -#define SS_BACKUP_SOURCE SS_COMMON_WORKSPACE "/saved.file" //How to make sure there is SPACE -#define SS_PATCHFILE_SOURCE SS_COMMON_WORKSPACE "/patchfile.file" //define in common place -#define SS_PATCHLIST_BKUPLOC SS_COMMON_WORKSPACE "/patchlist.txt" -#define SS_NEW_COMPRESSED_FILE SS_COMMON_WORKSPACE "/system.7z" -#define SS_KERNEL_WORKSPACE SS_COMMON_WORKSPACE "/kernel-work" -#define SS_GZIP_TARGET SS_KERNEL_WORKSPACE "/gzip" -#define SS_STAT_TARGET SS_KERNEL_WORKSPACE "/stat" -#define SS_DD_TARGET SS_KERNEL_WORKSPACE "/dd" - -#define SS_GZIP_SOURCE "system/bin/gzip" -#define SS_STAT_SOURCE "system/usr/bin/stat" -#define SS_DD_SOURCE "system/bin/dd" - -#define SS_KERNEL_MAGIC "UnpackdzImage" -#define SS_KERNEL_NAME "zImage" -#define SS_KERNEL_TARGET_NAME "dzImage_final" -#define SS_KERNEL_UNPACK_DIR SS_KERNEL_NAME "_unpacked" -#define SS_PATCHLISTFOLDER "/p" -#define SS_NEWPATCHFOLDER "/n" -#define SS_PATCHLISTFORMAT ".txt" -#define SS_PATCH_ATTR_FORMAT "_attr.txt" -#define SS_FSCOUNT_MAGIC_KEY "PaTcHCoUnT:" -#define SS_FSCOUNT_MAGIG_KEYLEN (11) //length of SS_FSCOUNT_MAGIC_KEY -#define SS_IMAGE_MAGIC_KEY "TiZSiG@tOtA_00_:.{0,64}" -#define SS_IMAGE_MAGIC_KEY_VAL SS_COMMON_WORKSPACE "/delta_sha.txt" - -#define SS_TOKEN_SPACE " " -#define SS_TOKEN_NEWLINE "\n" -#define SS_TOEKN_COLON ":" -#define SS_CHAR_FWSLASH '/' -#define SS_FWSLASH "/" -#define SS_NULLENTRY "0" -#define SS_MAX_NAMELENSUPPORTED (200) //(Tar supports 256, But extra space is used for PartitionName, .delta, /p, so restricting filename max to 200) -#define SS_MAX_FILE_PATH (512) -#define SS_TOKEN_MAXLINE_LEN (1024) -#define SS_COMPRESSED_FILE "system.7z" - -#define SS_STRING_DIFF "DIFF" -#define SS_STRING_MOVE "MOVE" -#define SS_STRING_DEL "DEL" -#define SS_STRING_SYM "SYM" -#define SS_STRING_NEW "NEW" -#define SS_STRING_REG "REG" -#define SS_STRING_TPK "TPK" -#define SS_STRING_ZIP "ZIP" -#define SS_STRING_END "END" +#define SS_BACKUP_SOURCE SS_COMMON_WORKSPACE "/saved.file" //How to make sure there is SPACE +#define SS_PATCHFILE_SOURCE SS_COMMON_WORKSPACE "/patchfile.file" //define in common place +#define SS_PATCHLIST_BKUPLOC SS_COMMON_WORKSPACE "/patchlist.txt" +#define SS_NEW_COMPRESSED_FILE SS_COMMON_WORKSPACE "/system.7z" +#define SS_KERNEL_WORKSPACE SS_COMMON_WORKSPACE "/kernel-work" +#define SS_GZIP_TARGET SS_KERNEL_WORKSPACE "/gzip" +#define SS_STAT_TARGET SS_KERNEL_WORKSPACE "/stat" +#define SS_DD_TARGET SS_KERNEL_WORKSPACE "/dd" + +#define SS_GZIP_SOURCE "system/bin/gzip" +#define SS_STAT_SOURCE "system/usr/bin/stat" +#define SS_DD_SOURCE "system/bin/dd" + +#define SS_KERNEL_MAGIC "UnpackdzImage" +#define SS_KERNEL_NAME "zImage" +#define SS_KERNEL_TARGET_NAME "dzImage_final" +#define SS_KERNEL_UNPACK_DIR SS_KERNEL_NAME "_unpacked" +#define SS_PATCHLISTFOLDER "/p" +#define SS_NEWPATCHFOLDER "/n" +#define SS_PATCHLISTFORMAT ".txt" +#define SS_PATCH_ATTR_FORMAT "_attr.txt" +#define SS_FSCOUNT_MAGIC_KEY "PaTcHCoUnT:" +#define SS_FSCOUNT_MAGIG_KEYLEN (11) //length of SS_FSCOUNT_MAGIC_KEY +#define SS_IMAGE_MAGIC_KEY "TiZSiG@tOtA_00_:.{0,64}" +#define SS_IMAGE_MAGIC_KEY_VAL SS_COMMON_WORKSPACE "/delta_sha.txt" + +#define SS_TOKEN_SPACE " " +#define SS_TOKEN_NEWLINE "\n" +#define SS_TOEKN_COLON ":" +#define SS_CHAR_FWSLASH '/' +#define SS_FWSLASH "/" +#define SS_NULLENTRY "0" +#define SS_MAX_NAMELENSUPPORTED (200) //(Tar supports 256, But extra space is used for PartitionName, .delta, /p, so restricting filename max to 200) +#define SS_MAX_FILE_PATH (512) +#define SS_TOKEN_MAXLINE_LEN (1024) +#define SS_COMPRESSED_FILE "system.7z" + +#define SS_STRING_DIFF "DIFF" +#define SS_STRING_MOVE "MOVE" +#define SS_STRING_DEL "DEL" +#define SS_STRING_SYM "SYM" +#define SS_STRING_NEW "NEW" +#define SS_STRING_REG "REG" +#define SS_STRING_TPK "TPK" +#define SS_STRING_ZIP "ZIP" +#define SS_STRING_END "END" #ifdef SUPPORT_CONTAINER -#define SS_ZIP_SOURCE "system/usr/bin/zip" -#define SS_ZIP_TARGET "system/opt/data/fota/zip" -#define SS_UNZIP_SOURCE "system/usr/bin/unzip" -#define SS_UNZIP_TARGET "system/opt/data/fota/unzip" -#define SS_FIND_CMD_SOURCE "system/usr/bin/find" -#define SS_FIND_CMD_TARGET "system/opt/data/fota/find" -#define SS_ZIP_COMMAND "/opt/data/fota/zip" -#define SS_UNZIP_COMMAND "/opt/data/fota/unzip" -#define SS_FIND_COMMAND "/opt/data/fota/find" -#define SS_SEPARATOR_TOKEN "|" -#define SS_NEWLINE_TOKEN "\n" -#define SS_FW_SLASH_TOKEN '/' -#define SS_CONTAINER_INFO_FILE "PATCH.txt" +#define SS_ZIP_SOURCE "system/usr/bin/zip" +#define SS_ZIP_TARGET "system/opt/data/fota/zip" +#define SS_UNZIP_SOURCE "system/usr/bin/unzip" +#define SS_UNZIP_TARGET "system/opt/data/fota/unzip" +#define SS_FIND_CMD_SOURCE "system/usr/bin/find" +#define SS_FIND_CMD_TARGET "system/opt/data/fota/find" +#define SS_ZIP_COMMAND "/opt/data/fota/zip" +#define SS_UNZIP_COMMAND "/opt/data/fota/unzip" +#define SS_FIND_COMMAND "/opt/data/fota/find" +#define SS_SEPARATOR_TOKEN "|" +#define SS_NEWLINE_TOKEN "\n" +#define SS_FW_SLASH_TOKEN '/' +#define SS_CONTAINER_INFO_FILE "PATCH.txt" #define SS_CONTAINER_WORKSPACE SS_COMMON_WORKSPACE "/archive" -#define SS_ARCHIVE_WORK_FOLDER SS_CONTAINER_WORKSPACE "/tpk" -#define SS_ARCHIVE_DELTA_FOLDER SS_CONTAINER_WORKSPACE "/delta" -#define SS_ARCHIVE_UNPACK_FOLDER SS_CONTAINER_WORKSPACE "/unpack" +#define SS_ARCHIVE_WORK_FOLDER SS_CONTAINER_WORKSPACE "/tpk" +#define SS_ARCHIVE_DELTA_FOLDER SS_CONTAINER_WORKSPACE "/delta" +#define SS_ARCHIVE_UNPACK_FOLDER SS_CONTAINER_WORKSPACE "/unpack" #endif #ifdef MEM_PROFILING -#define SS_MEMORY_USAGE_LOG SS_COMMON_WORKSPACE "/log_memory" +#define SS_MEMORY_USAGE_LOG SS_COMMON_WORKSPACE "/log_memory" #define SS_MEMORY_PROFILING_SCRIPT SS_COMMON_WORKSPACE "/mem_use.sh" #endif struct tar_Data { - int itemSize; - int itemOffset; - int itemName[256]; - struct tar_Data *nextnode; + int itemSize; + int itemOffset; + int itemName[256]; + struct tar_Data *nextnode; }; typedef struct tar_Data tar_Data_t; diff --git a/ss_engine/fota_log.c b/ss_engine/fota_log.c index 5fa3220..348dba0 100755 --- a/ss_engine/fota_log.c +++ b/ss_engine/fota_log.c @@ -43,15 +43,13 @@ static long __check_existence(const char *file_path) struct stat statbuf; char filename[MAX_FILE_PATH]; - if (strncpy(filename, file_path, strlen(file_path) + 1) == NULL) { - return 0; - } + if (strncpy(filename, file_path, strlen(file_path) + 1) == NULL) + return 0; if (stat(filename, &statbuf)) { - if (ENOENT == errno) { - return 0; - } + if (ENOENT == errno) + return 0; } - return statbuf.st_size; + return statbuf.st_size; } /*----------------------------------------------------------------------------- @@ -65,14 +63,12 @@ static int __read_from_file(const char *path, char *buf, size_t size) if (!path) return -1; - if (size == 0) { + if (size == 0) return 0; - } fd = open(path, O_RDONLY, 0); - if (fd == -1) { + if (fd == -1) return -1; - } count = read(fd, buf, size); if (count > 0) { @@ -100,13 +96,11 @@ static int get_opt_logfile_size(void) */ char buf[256]; - if (__check_existence(LOG_SIZE_OPT_PATH) == 0) { + if (__check_existence(LOG_SIZE_OPT_PATH) == 0) return -1; - } - if (__read_from_file(LOG_SIZE_OPT_PATH, buf, sizeof(buf)) < 0) { + if (__read_from_file(LOG_SIZE_OPT_PATH, buf, sizeof(buf)) < 0) return -1; - } return atoi(buf); } @@ -118,9 +112,8 @@ void set_max_logfile_size(void) { int size = get_opt_logfile_size(); - if (size <= 0) { + if (size <= 0) size = DEF_MAX_LOG_SIZE; - } max_logfile_size = size; } @@ -188,11 +181,10 @@ void truncate_log_file(char *log_path, int size_kb) if (size_kb == 0) { log_fp = fopen(log_path, "w"); - if (log_fp == NULL) { + if (log_fp == NULL) perror("file open error\n"); - } else { + else fclose(log_fp); - } } sync(); diff --git a/ss_engine/fota_log.h b/ss_engine/fota_log.h index 6c9aca5..231af30 100755 --- a/ss_engine/fota_log.h +++ b/ss_engine/fota_log.h @@ -33,37 +33,37 @@ extern void set_max_logfile_size(void); #define LOG_INFO (1<<8) #define LOG_ENGINE (1<<7) -#define LOG_FUNCS (1<<6) +#define LOG_FUNCS (1<<6) #define LOG_GUI (1<<5) -#define LOG_DEBUG (1<<4) -#define LOG_FILE (1<<3) -#define LOG_FLASH (1<<2) -#define LOG_SSENGINE LOG_ENGINE +#define LOG_DEBUG (1<<4) +#define LOG_FILE (1<<3) +#define LOG_FLASH (1<<2) +#define LOG_SSENGINE LOG_ENGINE //#define DEBUG_STDOUT #define DEBUG_FILE #ifdef DEBUG_STDOUT #define LOGE(s, args...) printf("UA/ERROR(%s) " s, __func__, ##args) // Error log -#define LOGL(mask, s, args...) do{if((mask) & __log_level__) printf("UA/(%s): " s,__func__, ##args);}while(0) +#define LOGL(mask, s, args...) do { if ((mask) & __log_level__) printf("UA/(%s): " s, __func__, ##args); } while (0) #define LOG(s, args...) LOGL(LOG_DEBUG, s, ##args) #elif defined(DEBUG_FILE) #define LOGE(s, args...) (void)log_printf(__log_out_file__, "UA/ERROR(%s) " s, __func__, ##args) -#define LOGL(mask, s, args...) do{if((mask) & __log_level__) (void)log_printf(__log_out_file__, "UA/(%s): " s ,__func__, ##args);}while(0) +#define LOGL(mask, s, args...) do { if ((mask) & __log_level__) (void)log_printf(__log_out_file__, "UA/(%s): " s , __func__, ##args); } while (0) #define LOG(s, args...) LOGL(LOG_DEBUG, s, ##args) #elif defined(DEBUG_STDOUT_FILE) // debug printf #define LOGE(s, args...) do {\ printf("UA/ERROR(%s) " s, __func__, ##args);\ (void)log_printf(__log_out_file__, "UA/ERROR(%s) " s, __func__, ##args);\ - }while(0) -#define LOGL(mask, s, args...) do{ \ - if((mask) & __log_level__){\ - printf("UA/(%s): " s ,__func__, ##args);\ - (void)log_printf(__log_out_file__, "UA/(%s): " s,__func__, ##args);\ - }\ - }while(0) + } while (0) +#define LOGL(mask, s, args...) do { \ + if ((mask) & __log_level__) {\ + printf("UA/(%s): " s , __func__, ##args);\ + (void)log_printf(__log_out_file__, "UA/(%s): " s, __func__, ##args);\ + } \ + } while (0) #define LOG(s, args...) LOGL(LOG_DEBUG, s, ##args) #else diff --git a/ss_engine/fota_tar.c b/ss_engine/fota_tar.c index 5e10a88..9c63c82 100755 --- a/ss_engine/fota_tar.c +++ b/ss_engine/fota_tar.c @@ -7,7 +7,7 @@ * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, @@ -32,26 +32,26 @@ /* tar Header Block, from POSIX 1003.1-1990. for reference */ #if 0 - /* POSIX header. */ - -struct posix_header { /* byte offset */ - char name[100]; /* 0 */ - char mode[8]; /* 100 */ - char uid[8]; /* 108 */ - char gid[8]; /* 116 */ - char size[12]; /* 124 */ - char mtime[12]; /* 136 */ - char chksum[8]; /* 148 */ - char typeflag; /* 156 */ - char linkname[100]; /* 157 */ - char magic[6]; /* 257 */ - char version[2]; /* 263 */ - char uname[32]; /* 265 */ - char gname[32]; /* 297 */ - char devmajor[8]; /* 329 */ - char devminor[8]; /* 337 */ - char prefix[155]; /* 345 */ - /* 500 */ + /* POSIX header. */ + +struct posix_header { /* byte offset */ + char name[100]; /* 0 */ + char mode[8]; /* 100 */ + char uid[8]; /* 108 */ + char gid[8]; /* 116 */ + char size[12]; /* 124 */ + char mtime[12]; /* 136 */ + char chksum[8]; /* 148 */ + char typeflag; /* 156 */ + char linkname[100]; /* 157 */ + char magic[6]; /* 257 */ + char version[2]; /* 263 */ + char uname[32]; /* 265 */ + char gname[32]; /* 297 */ + char devmajor[8]; /* 329 */ + char devminor[8]; /* 337 */ + char prefix[155]; /* 345 */ + /* 500 */ }; #endif @@ -64,7 +64,7 @@ struct posix_header { /* byte offset */ #define TAR_LONG_NAME_SIZE 256 #define TAR_ITEM_TYPE_FLAG_POS 156 - /*** The byte that indicates whether the prefix is present or not */ +/*** The byte that indicates whether the prefix is present or not */ #define PREFIX_INDICATOR_BYTE 345 #define PREFIX_LEN 155 @@ -72,118 +72,118 @@ struct posix_header { /* byte offset */ /* Values used in typeflag field. */ -#define REGTYPE '0' /* regular file */ -#define AREGTYPE '\0' /* regular file */ -#define LNKTYPE '1' /* link */ -#define SYMTYPE '2' /* reserved */ -#define CHRTYPE '3' /* character special */ -#define BLKTYPE '4' /* block special */ -#define DIRTYPE '5' /* directory */ -#define FIFOTYPE '6' /* FIFO special */ -#define CONTTYPE '7' /* reserved, for compatibility with gnu tar, - treat as regular file, where it represents - a regular file, but saved contiguously on disk */ +#define REGTYPE '0' /* regular file */ +#define AREGTYPE '\0' /* regular file */ +#define LNKTYPE '1' /* link */ +#define SYMTYPE '2' /* reserved */ +#define CHRTYPE '3' /* character special */ +#define BLKTYPE '4' /* block special */ +#define DIRTYPE '5' /* directory */ +#define FIFOTYPE '6' /* FIFO special */ +#define CONTTYPE '7' /* reserved, for compatibility with gnu tar, + treat as regular file, where it represents + a regular file, but saved contiguously on disk */ /* GNU tar extensions */ -#define GNUTYPE_DUMPDIR 'D' /* file names from dumped directory */ -#define GNUTYPE_LONGLINK 'K' /* long link name */ -#define GNUTYPE_LONGNAME 'L' /* long file name */ -#define GNUTYPE_MULTIVOL 'M' /* continuation of file from another volume */ -#define GNUTYPE_NAMES 'N' /* file name that does not fit into main hdr */ -#define GNUTYPE_SPARSE 'S' /* sparse file */ -#define GNUTYPE_VOLHDR 'V' /* tape/volume header */ +#define GNUTYPE_DUMPDIR 'D' /* file names from dumped directory */ +#define GNUTYPE_LONGLINK 'K' /* long link name */ +#define GNUTYPE_LONGNAME 'L' /* long file name */ +#define GNUTYPE_MULTIVOL 'M' /* continuation of file from another volume */ +#define GNUTYPE_NAMES 'N' /* file name that does not fit into main hdr */ +#define GNUTYPE_SPARSE 'S' /* sparse file */ +#define GNUTYPE_VOLHDR 'V' /* tape/volume header */ extern void *SS_Malloc(SS_UINT32 size); -int gTarFd = -1; // Currenlty this logic supports only one tar file +int gTarFd = -1; // Currenlty this logic supports only one tar file /* Parse an octal number, ignoring leading and trailing nonsense. */ static int parseoct(const char *p, size_t n) { - int i = 0; - - while (*p < '0' || *p > '7') { - ++p; - --n; - } - while (*p >= '0' && *p <= '7' && n > 0) { - i *= 8; - i += *p - '0'; - ++p; - --n; - } - return (i); + int i = 0; + + while (*p < '0' || *p > '7') { + ++p; + --n; + } + while (*p >= '0' && *p <= '7' && n > 0) { + i *= 8; + i += *p - '0'; + ++p; + --n; + } + return (i); } /* Verify the tar checksum. */ static int verify_checksum(const char *p) { - int n, u = 0; - for (n = 0; n < 512; ++n) { - if (n < 148 || n > 155) - /* Standard tar checksum adds unsigned bytes. */ - u += ((unsigned char *)p)[n]; - else - u += 0x20; - - } - return (u == parseoct(p + 148, 8)); + int n, u = 0; + for (n = 0; n < 512; ++n) { + if (n < 148 || n > 155) + /* Standard tar checksum adds unsigned bytes. */ + u += ((unsigned char *)p)[n]; + else + u += 0x20; + + } + return (u == parseoct(p + 148, 8)); } static int is_end_of_archive(const char *p) { - int n; - for (n = 511; n >= 0; --n) - if (p[n] != '\0') - return (0); - return (1); + int n; + for (n = 511; n >= 0; --n) + if (p[n] != '\0') + return (0); + return (1); } void create_dir(char *pathname, int mode) { - char *p; - int r; - - /* Strip trailing '/' */ - if (pathname[strlen(pathname) - 1] == '/') - pathname[strlen(pathname) - 1] = '\0'; - - /* Try creating the directory. */ - r = mkdir(pathname, mode); - - if (r != 0) { - /* On failure, try creating parent directory. */ - p = strrchr(pathname, '/'); - if (p != NULL) { - *p = '\0'; - create_dir(pathname, 0755); - *p = '/'; - r = mkdir(pathname, mode); - } - } - if (r != 0) { - if (r != EEXIST && r != -1) - LOG("Could not create directory [%s] Error[%d]\n", pathname, r); - } + char *p; + int r; + + /* Strip trailing '/' */ + if (pathname[strlen(pathname) - 1] == '/') + pathname[strlen(pathname) - 1] = '\0'; + + /* Try creating the directory. */ + r = mkdir(pathname, mode); + + if (r != 0) { + /* On failure, try creating parent directory. */ + p = strrchr(pathname, '/'); + if (p != NULL) { + *p = '\0'; + create_dir(pathname, 0755); + *p = '/'; + r = mkdir(pathname, mode); + } + } + if (r != 0) { + if (r != EEXIST && r != -1) + LOG("Could not create directory [%s] Error[%d]\n", pathname, r); + } } /* Create a file, including parent directory as necessary. */ static FILE *create_file(char *pathname, int mode) { - FILE *f; - f = fopen(pathname, "w+"); - if (f == NULL) { - /* Try creating parent dir and then creating file. */ - char *p = strrchr(pathname, '/'); - if (p != NULL) { - *p = '\0'; - create_dir(pathname, 0755); - *p = '/'; - f = fopen(pathname, "w+"); - } - } - return (f); + FILE *f; + f = fopen(pathname, "w+"); + if (f == NULL) { + /* Try creating parent dir and then creating file. */ + char *p = strrchr(pathname, '/'); + if (p != NULL) { + *p = '\0'; + create_dir(pathname, 0755); + *p = '/'; + f = fopen(pathname, "w+"); + } + } + return (f); } /*----------------------------------------------------------------------------- @@ -191,79 +191,79 @@ static FILE *create_file(char *pathname, int mode) ----------------------------------------------------------------------------*/ int tar_get_item_offset(char *tar, char *item) { - int ret = -1; - int fd = -1; - char header[TAR_BLOCK_SIZE] = { 0, }; - char name[TAR_ITEM_NAME_SIZE + 1] = { 0, }; - char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; - int size_dec = 0; - int blknum = 0; - off_t pos = 0; - off_t tar_len = 0; - ssize_t rdcnt = 0; - - //check if gTarFd was opened by tar_open during SS_FSUpdateFile then use it - if (gTarFd > 0) - fd = gTarFd; - if (fd < 0) { - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOGE("can't open file(%s).\n", tar); - return -1; - } - } - - tar_len = lseek(fd, 0, SEEK_END); - if (tar_len < 0) { - LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); - goto Cleanup; - } - pos = lseek(fd, 0, SEEK_SET); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); - goto Cleanup; - } - while (pos < tar_len) { - /* read file header */ - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - break; - } - - /* get file name and file size */ - memcpy(name, header, sizeof(name) - 1); - memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); - size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); - if (size_dec > MAX_ITEM_SIZE) { - LOG("size too big. (size_dec=0x%08X)\n", size_dec); - break; - } - - /* check if the file is what we are looking for */ - if (strncmp(name, item, TAR_ITEM_NAME_SIZE) == 0) { - ret = (int)lseek(fd, 0, SEEK_CUR); - break; - } - - /* move file pointer to next file header */ - blknum = size_dec / TAR_BLOCK_SIZE; - if (size_dec % TAR_BLOCK_SIZE) - blknum++; - - pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); - if (pos < 0) { - LOGE("can't read next block (%s).\n", tar); - close(fd); - return -1; - } - } + int ret = -1; + int fd = -1; + char header[TAR_BLOCK_SIZE] = { 0, }; + char name[TAR_ITEM_NAME_SIZE + 1] = { 0, }; + char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; + int size_dec = 0; + int blknum = 0; + off_t pos = 0; + off_t tar_len = 0; + ssize_t rdcnt = 0; + + //check if gTarFd was opened by tar_open during SS_FSUpdateFile then use it + if (gTarFd > 0) + fd = gTarFd; + if (fd < 0) { + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOGE("can't open file(%s).\n", tar); + return -1; + } + } + + tar_len = lseek(fd, 0, SEEK_END); + if (tar_len < 0) { + LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); + goto Cleanup; + } + pos = lseek(fd, 0, SEEK_SET); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); + goto Cleanup; + } + while (pos < tar_len) { + /* read file header */ + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + break; + } + + /* get file name and file size */ + memcpy(name, header, sizeof(name) - 1); + memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); + size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); + if (size_dec > MAX_ITEM_SIZE) { + LOG("size too big. (size_dec=0x%08X)\n", size_dec); + break; + } + + /* check if the file is what we are looking for */ + if (strncmp(name, item, TAR_ITEM_NAME_SIZE) == 0) { + ret = (int)lseek(fd, 0, SEEK_CUR); + break; + } + + /* move file pointer to next file header */ + blknum = size_dec / TAR_BLOCK_SIZE; + if (size_dec % TAR_BLOCK_SIZE) + blknum++; + + pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); + if (pos < 0) { + LOGE("can't read next block (%s).\n", tar); + close(fd); + return -1; + } + } Cleanup: - if (gTarFd < 0) - close(fd); + if (gTarFd < 0) + close(fd); - return ret; + return ret; } /*----------------------------------------------------------------------------- @@ -271,93 +271,93 @@ int tar_get_item_offset(char *tar, char *item) ----------------------------------------------------------------------------*/ int tar_get_item_size(char *tar, char *item) { - int ret = -1; - int fd = -1; - char header[TAR_BLOCK_SIZE] = { 0, }; - char uExtendedName[MAX_FILE_PATH] = { 0, }; - char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; - unsigned long size_dec = 0; - int blknum = 0; - off_t pos = 0; - off_t tar_len = 0; - ssize_t rdcnt = 0; - LOGL(LOG_SSENGINE, "Tar file Looking for (%s)\n", item); - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - - tar_len = lseek(fd, 0, SEEK_END); - if (tar_len < 0) { - LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); - goto Cleanup; - } - pos = lseek(fd, 0, SEEK_SET); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); - goto Cleanup; - } - - while (pos < tar_len) { - /* read file header */ - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - break; - } - - /* get file name and file size */ - if (header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGNAME || header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGLINK) { - //rdcnt = read(fd, header, sizeof(header)); - memset(uExtendedName, 0, sizeof(uExtendedName)); - rdcnt = read(fd, uExtendedName, sizeof(uExtendedName)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - break; - } - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - break; - } - } else { - memset(uExtendedName, 0, sizeof(uExtendedName)); - memcpy(uExtendedName, header, TAR_ITEM_NAME_SIZE); - } - //memcpy(name, header, sizeof(name)); - memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); - size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); - if (size_dec > MAX_ITEM_SIZE) { - LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); - break; - } - - /* check if the file is what we are looking for */ - if (strcmp(uExtendedName, item) == 0) { - ret = (int)size_dec; - if ((ret == 0) && (header[TAR_ITEM_TYPE_FLAG_POS] == DIRTYPE)) - ret = tar_get_folder_size(tar, item); - break; - } - /* move file pointer to next file header */ - //LOGL(LOG_SSENGINE,"Item in Tar (%s)\n", uExtendedName); - blknum = size_dec / TAR_BLOCK_SIZE; - if (size_dec % TAR_BLOCK_SIZE) - blknum++; - - pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); - close(fd); - return -1; - } - } + int ret = -1; + int fd = -1; + char header[TAR_BLOCK_SIZE] = { 0, }; + char uExtendedName[MAX_FILE_PATH] = { 0, }; + char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; + unsigned long size_dec = 0; + int blknum = 0; + off_t pos = 0; + off_t tar_len = 0; + ssize_t rdcnt = 0; + LOGL(LOG_SSENGINE, "Tar file Looking for (%s)\n", item); + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + + tar_len = lseek(fd, 0, SEEK_END); + if (tar_len < 0) { + LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); + goto Cleanup; + } + pos = lseek(fd, 0, SEEK_SET); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); + goto Cleanup; + } + + while (pos < tar_len) { + /* read file header */ + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + break; + } + + /* get file name and file size */ + if (header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGNAME || header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGLINK) { + //rdcnt = read(fd, header, sizeof(header)); + memset(uExtendedName, 0, sizeof(uExtendedName)); + rdcnt = read(fd, uExtendedName, sizeof(uExtendedName)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + break; + } + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + break; + } + } else { + memset(uExtendedName, 0, sizeof(uExtendedName)); + memcpy(uExtendedName, header, TAR_ITEM_NAME_SIZE); + } + //memcpy(name, header, sizeof(name)); + memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); + size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); + if (size_dec > MAX_ITEM_SIZE) { + LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); + break; + } + + /* check if the file is what we are looking for */ + if (strcmp(uExtendedName, item) == 0) { + ret = (int)size_dec; + if ((ret == 0) && (header[TAR_ITEM_TYPE_FLAG_POS] == DIRTYPE)) + ret = tar_get_folder_size(tar, item); + break; + } + /* move file pointer to next file header */ + //LOGL(LOG_SSENGINE,"Item in Tar (%s)\n", uExtendedName); + blknum = size_dec / TAR_BLOCK_SIZE; + if (size_dec % TAR_BLOCK_SIZE) + blknum++; + + pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); + close(fd); + return -1; + } + } Cleanup: - close(fd); + close(fd); - return ret; + return ret; } /*----------------------------------------------------------------------------- @@ -366,76 +366,76 @@ int tar_get_item_size(char *tar, char *item) char tar_get_item_type(char *tar, char *item) { - char ret = '0'; - int fd = -1; - char header[TAR_BLOCK_SIZE] = { 0, }; - char name[TAR_ITEM_NAME_SIZE + 1] = { 0, }; - char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; - unsigned long size_dec = 0; - int blknum = 0; - off_t pos = 0; - off_t tar_len = 0; - ssize_t rdcnt = 0; - //LOG("Tar file Looking for (%s)\n", item); - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - - tar_len = lseek(fd, 0, SEEK_END); - if (tar_len < 0) { - LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); - goto Cleanup; - } - pos = lseek(fd, 0, SEEK_SET); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); - goto Cleanup; - } - - while (pos < tar_len) { - /* read file header */ - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - ret = -1; - break; - } - - /* get file name and file size */ - memcpy(name, header, sizeof(name) - 1); - memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); - size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); - if (size_dec > MAX_ITEM_SIZE) { - LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); - ret = -1; - break; - } - - /* check if the file is what we are looking for */ - if (strncmp(name, item, TAR_ITEM_NAME_SIZE) == 0) { - ret = header[TAR_ITEM_TYPE_FLAG_POS]; - break; - } - - /* move file pointer to next file header */ - blknum = size_dec / TAR_BLOCK_SIZE; - if (size_dec % TAR_BLOCK_SIZE) - blknum++; - - pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); - close(fd); - return -1; - } - } + char ret = '0'; + int fd = -1; + char header[TAR_BLOCK_SIZE] = { 0, }; + char name[TAR_ITEM_NAME_SIZE + 1] = { 0, }; + char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; + unsigned long size_dec = 0; + int blknum = 0; + off_t pos = 0; + off_t tar_len = 0; + ssize_t rdcnt = 0; + //LOG("Tar file Looking for (%s)\n", item); + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + + tar_len = lseek(fd, 0, SEEK_END); + if (tar_len < 0) { + LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); + goto Cleanup; + } + pos = lseek(fd, 0, SEEK_SET); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); + goto Cleanup; + } + + while (pos < tar_len) { + /* read file header */ + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + ret = -1; + break; + } + + /* get file name and file size */ + memcpy(name, header, sizeof(name) - 1); + memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); + size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); + if (size_dec > MAX_ITEM_SIZE) { + LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); + ret = -1; + break; + } + + /* check if the file is what we are looking for */ + if (strncmp(name, item, TAR_ITEM_NAME_SIZE) == 0) { + ret = header[TAR_ITEM_TYPE_FLAG_POS]; + break; + } + + /* move file pointer to next file header */ + blknum = size_dec / TAR_BLOCK_SIZE; + if (size_dec % TAR_BLOCK_SIZE) + blknum++; + + pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); + close(fd); + return -1; + } + } Cleanup: - close(fd); + close(fd); - return ret; + return ret; } /*----------------------------------------------------------------------------- @@ -443,681 +443,677 @@ char tar_get_item_type(char *tar, char *item) ----------------------------------------------------------------------------*/ int tar_get_cfg_data(char *tar, char *item, char *buf, int buflen) { - int fd = -1; - int data_size = -1; - int data_offset = -1; - off_t pos = 0; - ssize_t rdcnt = 0; - - data_size = tar_get_item_size(tar, item); - if (data_size <= 0) { - return -1; - } - - if (data_size > buflen) { - data_size = buflen; - } - - data_offset = tar_get_item_offset(tar, item); - if (data_offset < 0) { - return -1; - } - - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - - pos = lseek(fd, data_offset, SEEK_SET); - if (pos < 0) { - LOG("lseek fail (%s offset %d).\n", tar, data_offset); - close(fd); - return -1; - } - - rdcnt = read(fd, buf, data_size); - if (rdcnt != (ssize_t) data_size) { - LOG("read fail(%s from %s).\n", item, tar); - close(fd); - return -1; - } - - close(fd); - - return rdcnt; + int fd = -1; + int data_size = -1; + int data_offset = -1; + off_t pos = 0; + ssize_t rdcnt = 0; + + data_size = tar_get_item_size(tar, item); + if (data_size <= 0) + return -1; + + if (data_size > buflen) + data_size = buflen; + + data_offset = tar_get_item_offset(tar, item); + if (data_offset < 0) + return -1; + + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + + pos = lseek(fd, data_offset, SEEK_SET); + if (pos < 0) { + LOG("lseek fail (%s offset %d).\n", tar, data_offset); + close(fd); + return -1; + } + + rdcnt = read(fd, buf, data_size); + if (rdcnt != (ssize_t) data_size) { + LOG("read fail(%s from %s).\n", item, tar); + close(fd); + return -1; + } + + close(fd); + + return rdcnt; } tar_Data_t *tar_build_cfg_table(char *tar) { - int fd = -1; - int ret = 0; - char header[TAR_BLOCK_SIZE] = { 0, }; - char uExtendedName[MAX_FILE_PATH] = { 0, }; - char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; - unsigned long size_dec = 0; - int blknum = 0; - off_t pos = 0; - off_t tar_len = 0; - ssize_t rdcnt = 0; - - int itemSize; - int itemOffset; - tar_Data_t *headparam = NULL, *tailparam = NULL, *newnode = NULL; - tar_Data_t *local_temp = NULL; - tar_Data_t *local_next = NULL; - if (!tar) { - LOGE("Bad param tar\n"); - return NULL; - } - //check if gTarFd was opened by tar_open during SS_FSUpdateFile then use it - if (gTarFd > 0) - fd = gTarFd; - if (fd < 0) { - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return NULL; - } - } - tar_len = lseek(fd, 0, SEEK_END); - if (tar_len < 0) { - LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); - goto Cleanup; - } - pos = lseek(fd, 0, SEEK_SET); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); - goto Cleanup; - } - while (pos < tar_len) { - /* read file header */ - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - ret = -1; - break; - } - /* get file name and file size */ - //memcpy(name, header, sizeof(name)); - if (header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGNAME || header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGLINK) { - //rdcnt = read(fd, header, sizeof(header)); - memset(uExtendedName, 0, sizeof(uExtendedName)); - rdcnt = read(fd, uExtendedName, sizeof(uExtendedName)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - ret = -1; - break; - } - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - ret = -1; - break; - } - } else { - memset(uExtendedName, 0, sizeof(uExtendedName)); - memcpy(uExtendedName, header, TAR_ITEM_NAME_SIZE); - } - memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); - size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); - if (size_dec > MAX_ITEM_SIZE) { - LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); - ret = -1; - break; - } - //fix WGID : 51254 , size_dec comparison is not required - if ((strstr(uExtendedName, "/diff") != NULL)) //add only delta files from rootfs and csc, hardcoding shd b removed.. - { - - /* check if the file is what we are looking for */ - //strncpy(itemName, name,100); - itemSize = (int)size_dec; - itemOffset = (int)lseek(fd, 0, SEEK_CUR); - newnode = (tar_Data_t *) SS_Malloc(sizeof(tar_Data_t)); - if (!newnode) { - ret = -1; - break; - } - strncpy((char *)newnode->itemName, uExtendedName, sizeof(newnode->itemName)); - newnode->itemOffset = itemOffset; - newnode->itemSize = itemSize; - newnode->nextnode = NULL; - if (headparam == NULL) { - headparam = newnode; - tailparam = newnode; - } else { - (tailparam)->nextnode = newnode; - (tailparam) = (tailparam)->nextnode; - } - } - - /* move file pointer to next file header */ - blknum = size_dec / TAR_BLOCK_SIZE; - if (size_dec % TAR_BLOCK_SIZE) - blknum++; - - pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); - ret = -1; - break; - } - } + int fd = -1; + int ret = 0; + char header[TAR_BLOCK_SIZE] = { 0, }; + char uExtendedName[MAX_FILE_PATH] = { 0, }; + char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; + unsigned long size_dec = 0; + int blknum = 0; + off_t pos = 0; + off_t tar_len = 0; + ssize_t rdcnt = 0; + + int itemSize; + int itemOffset; + tar_Data_t *headparam = NULL, *tailparam = NULL, *newnode = NULL; + tar_Data_t *local_temp = NULL; + tar_Data_t *local_next = NULL; + if (!tar) { + LOGE("Bad param tar\n"); + return NULL; + } + //check if gTarFd was opened by tar_open during SS_FSUpdateFile then use it + if (gTarFd > 0) + fd = gTarFd; + if (fd < 0) { + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return NULL; + } + } + tar_len = lseek(fd, 0, SEEK_END); + if (tar_len < 0) { + LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); + goto Cleanup; + } + pos = lseek(fd, 0, SEEK_SET); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); + goto Cleanup; + } + while (pos < tar_len) { + /* read file header */ + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + ret = -1; + break; + } + /* get file name and file size */ + //memcpy(name, header, sizeof(name)); + if (header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGNAME || header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGLINK) { + //rdcnt = read(fd, header, sizeof(header)); + memset(uExtendedName, 0, sizeof(uExtendedName)); + rdcnt = read(fd, uExtendedName, sizeof(uExtendedName)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + ret = -1; + break; + } + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + ret = -1; + break; + } + } else { + memset(uExtendedName, 0, sizeof(uExtendedName)); + memcpy(uExtendedName, header, TAR_ITEM_NAME_SIZE); + } + memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); + size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); + if (size_dec > MAX_ITEM_SIZE) { + LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); + ret = -1; + break; + } + //fix WGID : 51254 , size_dec comparison is not required + if ((strstr(uExtendedName, "/diff") != NULL)) { //add only delta files from rootfs and csc, hardcoding shd b removed.. + + /* check if the file is what we are looking for */ + //strncpy(itemName, name,100); + itemSize = (int)size_dec; + itemOffset = (int)lseek(fd, 0, SEEK_CUR); + newnode = (tar_Data_t *) SS_Malloc(sizeof(tar_Data_t)); + if (!newnode) { + ret = -1; + break; + } + strncpy((char *)newnode->itemName, uExtendedName, sizeof(newnode->itemName)); + newnode->itemOffset = itemOffset; + newnode->itemSize = itemSize; + newnode->nextnode = NULL; + if (headparam == NULL) { + headparam = newnode; + tailparam = newnode; + } else { + (tailparam)->nextnode = newnode; + (tailparam) = (tailparam)->nextnode; + } + } + + /* move file pointer to next file header */ + blknum = size_dec / TAR_BLOCK_SIZE; + if (size_dec % TAR_BLOCK_SIZE) + blknum++; + + pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); + ret = -1; + break; + } + } Cleanup: - //if gTarFd was opened by tar_open during SS_FSUpdateFile we do not close it - if (gTarFd < 0) - close(fd); - if (ret != -1) - return headparam; - else { - if (newnode) - SS_Free(newnode); - if (headparam) { - local_temp = headparam; - while (local_temp) { - local_next = local_temp->nextnode; - SS_Free(local_temp); - local_temp = local_next; - } - } - return NULL; - } + //if gTarFd was opened by tar_open during SS_FSUpdateFile we do not close it + if (gTarFd < 0) + close(fd); + if (ret != -1) + return headparam; + else { + if (newnode) + SS_Free(newnode); + if (headparam) { + local_temp = headparam; + while (local_temp) { + local_next = local_temp->nextnode; + SS_Free(local_temp); + local_temp = local_next; + } + } + return NULL; + } } void tar_free_cfg_table(tar_Data_t ** delta_tar) { - tar_Data_t *local_temp = NULL; - tar_Data_t *local_next = NULL; - LOGL(LOG_SSENGINE, "Free TAR CFG TABLE\n"); - if (*delta_tar) { - local_temp = *delta_tar; - while (local_temp) { - local_next = local_temp->nextnode; - //LOGL(LOG_SSENGINE,"freeing [%s]\n",local_temp->itemName); - SS_Free(local_temp); - local_temp = local_next; - } - } + tar_Data_t *local_temp = NULL; + tar_Data_t *local_next = NULL; + LOGL(LOG_SSENGINE, "Free TAR CFG TABLE\n"); + if (*delta_tar) { + local_temp = *delta_tar; + while (local_temp) { + local_next = local_temp->nextnode; + //LOGL(LOG_SSENGINE,"freeing [%s]\n",local_temp->itemName); + SS_Free(local_temp); + local_temp = local_next; + } + } } void deleteNode(tar_Data_t * head, tar_Data_t * n) { - tar_Data_t *prev = head; - if (head == n) { - if (head->nextnode == NULL) { - LOG("There is only one node. The list can't be made empty "); - return; - } - strncpy((char *)head->itemName, (const char *)head->nextnode->itemName, TAR_ITEM_NAME_SIZE); //head->itemName = head->nextnode->itemName; - head->itemSize = head->nextnode->itemSize; - head->itemOffset = head->nextnode->itemOffset; - n = head->nextnode; - head->nextnode = head->nextnode->nextnode; - SS_Free(n); - return; - } - while (prev->nextnode != NULL && prev->nextnode != n) - prev = prev->nextnode; - if (prev->nextnode == NULL) { - LOG("\n Given node is not present in Linked List"); - return; - } - prev->nextnode = prev->nextnode->nextnode; - SS_Free(n); - return; + tar_Data_t *prev = head; + if (head == n) { + if (head->nextnode == NULL) { + LOG("There is only one node. The list can't be made empty "); + return; + } + strncpy((char *)head->itemName, (const char *)head->nextnode->itemName, TAR_ITEM_NAME_SIZE); //head->itemName = head->nextnode->itemName; + head->itemSize = head->nextnode->itemSize; + head->itemOffset = head->nextnode->itemOffset; + n = head->nextnode; + head->nextnode = head->nextnode->nextnode; + SS_Free(n); + return; + } + while (prev->nextnode != NULL && prev->nextnode != n) + prev = prev->nextnode; + if (prev->nextnode == NULL) { + LOG("\n Given node is not present in Linked List"); + return; + } + prev->nextnode = prev->nextnode->nextnode; + SS_Free(n); + return; } int tar_get_item_size_from_struct(tar_Data_t ** delta_tar, const char *patchname, int *data_size, int *data_offset) { - tar_Data_t *head = *delta_tar; - tar_Data_t *base = *delta_tar; - if (head == NULL) - return 1; - else { - //LOG("fast_tar_get_item_size- looking for [%s] [%s]\n",patchname,head->itemName); - while (1) { - if (strstr((const char *)head->itemName, patchname) != 0) { - //LOG("fast_tar_get_item_size found [%s] in [%s]\n",patchname, head->itemName); - *data_size = head->itemSize; - *data_offset = head->itemOffset; - deleteNode(base, head); - return 0; - - } else if (head->nextnode != NULL) { - head = head->nextnode; - //LOG("fast_tar_get_item_size current node [%s] \n",head->itemName); - } else { - LOGE("fast_tar_get_item_size FAILED TO GET [%s] in [%s]\n", patchname, (char *)head->itemName); - break; - } - } - return 1; - } + tar_Data_t *head = *delta_tar; + tar_Data_t *base = *delta_tar; + if (head == NULL) + return 1; + else { + //LOG("fast_tar_get_item_size- looking for [%s] [%s]\n",patchname,head->itemName); + while (1) { + if (strstr((const char *)head->itemName, patchname) != 0) { + //LOG("fast_tar_get_item_size found [%s] in [%s]\n",patchname, head->itemName); + *data_size = head->itemSize; + *data_offset = head->itemOffset; + deleteNode(base, head); + return 0; + + } else if (head->nextnode != NULL) { + head = head->nextnode; + //LOG("fast_tar_get_item_size current node [%s] \n",head->itemName); + } else { + LOGE("fast_tar_get_item_size FAILED TO GET [%s] in [%s]\n", patchname, (char *)head->itemName); + break; + } + } + return 1; + } } tar_Data_t *tar_cfg_clear_nodes(tar_Data_t * head) { - tar_Data_t *local_temp = NULL; - while (head) { - LOGL(LOG_SSENGINE, "tar_cfg_delete_node [%s]", (char *)head->itemName); - local_temp = head->nextnode; - SS_Free(head); - head = local_temp; - } - return 0; + tar_Data_t *local_temp = NULL; + while (head) { + LOGL(LOG_SSENGINE, "tar_cfg_delete_node [%s]", (char *)head->itemName); + local_temp = head->nextnode; + SS_Free(head); + head = local_temp; + } + return 0; } int tar_open(char *tar) { - if (gTarFd) - close(gTarFd); - gTarFd = open(tar, O_RDONLY); - if (gTarFd < 0) { - LOG("can't open TAR file(%s).\n", tar); - return -1; - } - return 0; + if (gTarFd) + close(gTarFd); + gTarFd = open(tar, O_RDONLY); + if (gTarFd < 0) { + LOG("can't open TAR file(%s).\n", tar); + return -1; + } + return 0; } int tar_close() { - if (gTarFd) - close(gTarFd); - gTarFd = -1; - return 0; + if (gTarFd) + close(gTarFd); + gTarFd = -1; + return 0; } int tar_get_folder_size(char *tar, char *item) { - int ret = -1; - int fd = -1; - char header[TAR_BLOCK_SIZE] = { 0, }; - char name[TAR_LONG_NAME_SIZE + 1] = { 0, }; - char *lastfolder = NULL; - int folderpathlen = 0; - char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; - unsigned long size_dec = 0; - int blknum = 0; - off_t pos = 0; - off_t tar_len = 0; - ssize_t rdcnt = 0; - LOG("Tar folder Looking for (%s)\n", item); - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - - tar_len = lseek(fd, 0, SEEK_END); - if (tar_len < 0) { - LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); - goto Cleanup; - } - pos = lseek(fd, 0, SEEK_SET); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); - goto Cleanup; - } - - while (pos < tar_len) { - /* read file header */ - rdcnt = read(fd, header, sizeof(header)); - if (rdcnt <= 0) { - LOG("read failed. (rdcnt=%d)\n", rdcnt); - ret = -1; - break; - } - - /* get file name and file size */ - memcpy(name, header, sizeof(name) - 1); - memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); - size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); - if (size_dec > MAX_ITEM_SIZE) { - LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); - ret = -1; - break; - } - - /* check if the file is what we are looking for */ - //Get until folder name - - lastfolder = strrchr(name, '/'); - if (lastfolder) - folderpathlen = strlen(name) - strlen(lastfolder); - - if (strncmp(name, item, folderpathlen) == 0) { - ret += (int)size_dec; - //LOG("Tar Files under folder [%s]\n", name); - //break; - } - - /* move file pointer to next file header */ - blknum = size_dec / TAR_BLOCK_SIZE; - if (size_dec % TAR_BLOCK_SIZE) - blknum++; - - pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); - if (pos < 0) { - LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); - close(fd); - return -1; - } - } + int ret = -1; + int fd = -1; + char header[TAR_BLOCK_SIZE] = { 0, }; + char name[TAR_LONG_NAME_SIZE + 1] = { 0, }; + char *lastfolder = NULL; + int folderpathlen = 0; + char size_oct[TAR_SIZE_OF_HEADER] = { 0, }; + unsigned long size_dec = 0; + int blknum = 0; + off_t pos = 0; + off_t tar_len = 0; + ssize_t rdcnt = 0; + LOG("Tar folder Looking for (%s)\n", item); + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + + tar_len = lseek(fd, 0, SEEK_END); + if (tar_len < 0) { + LOGL(LOG_SSENGINE, "can't read tar_len (%s).\n", tar); + goto Cleanup; + } + pos = lseek(fd, 0, SEEK_SET); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read pos (%s).\n", tar); + goto Cleanup; + } + + while (pos < tar_len) { + /* read file header */ + rdcnt = read(fd, header, sizeof(header)); + if (rdcnt <= 0) { + LOG("read failed. (rdcnt=%d)\n", rdcnt); + ret = -1; + break; + } + + /* get file name and file size */ + memcpy(name, header, sizeof(name) - 1); + memcpy(size_oct, header + TAR_ITEM_SIZE_POSITION, sizeof(size_oct)); + size_dec = strtoul(size_oct, NULL, TAR_SIZE_OF_ITEM_SIZE); + if (size_dec > MAX_ITEM_SIZE) { + LOG("size too big. (size_dec=0x%08X)\n", (unsigned int)size_dec); + ret = -1; + break; + } + + /* check if the file is what we are looking for */ + //Get until folder name + + lastfolder = strrchr(name, '/'); + if (lastfolder) + folderpathlen = strlen(name) - strlen(lastfolder); + + if (strncmp(name, item, folderpathlen) == 0) { + ret += (int)size_dec; + //LOG("Tar Files under folder [%s]\n", name); + //break; + } + + /* move file pointer to next file header */ + blknum = size_dec / TAR_BLOCK_SIZE; + if (size_dec % TAR_BLOCK_SIZE) + blknum++; + + pos = lseek(fd, (off_t) (blknum * TAR_BLOCK_SIZE), SEEK_CUR); + if (pos < 0) { + LOGL(LOG_SSENGINE, "can't read next block (%s).\n", tar); + close(fd); + return -1; + } + } Cleanup: - close(fd); - LOG("ret=%d\n", ret); + close(fd); + LOG("ret=%d\n", ret); - return ret; //Should return +1?? or Ignore?? + return ret; //Should return +1?? or Ignore?? } /*Extract Specific Folder from tar, Taken from Untar.c */ int tar_extract_folder(char *tar, char *item, char *path) { - char buff[MAX_FILE_PATH]; - FILE *f = NULL; - size_t bytes_read; - int filesize; - int data_offset = -1; - int fd = -1; - char name[512] = { 0, }; - int folderpathlen = 0; - char dirPath[512] = { 0 }; - int getheader = 1; // Asuming initial header is TAR header - char fullname[512] = { 0 }; - int ret; - LOG("Extracting Folder from %s %s to %s\n", tar, item, path); - - data_offset = tar_get_item_offset(tar, item); - if (data_offset < 0) { - LOGE("data offset for [%s] is [%d]\n", item, data_offset); - return -1; - } - - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOGE("can't open file(%s).\n", tar); - return -1; - } - - folderpathlen = strlen(item); - - for (;;) { - bytes_read = read(fd, buff, sizeof(buff)); - if (bytes_read < 512) { - LOGE("Short read on %s: expected 512, got %d\n", tar, bytes_read); - close(fd); - return -1; - } - if (is_end_of_archive(buff)) { - close(fd); - LOG("End of %s\n", tar); //Can stop at end of folder. - return S_SS_SUCCESS; - } - if (!verify_checksum(buff)) { - close(fd); - LOGE("Checksum failure\n"); - return -1; - } - filesize = parseoct(buff + 124, 12); - if (getheader == 2) { - getheader = 1; - //LOG(" Working on LONG FILE NAME CASE [%s]\n", fullname); - } else { - memset(fullname, 0, sizeof(fullname)); - strncpy(fullname, buff, 100); - //LOG(" Working on Normal FILE NAME CASE [%s]\n", fullname); - } - - switch (buff[156]) { - case '1': - LOG(" Ignoring hardlink %s\n", fullname); - break; - case '2': - - //LOG(" Creating symlink %s\n", buff); - if (strncmp(fullname, item, folderpathlen) == 0) { - //LOG("Printing Buffer \n"); - //for(i=157; buff[i] !='\0' ;i++) - //{ - //LOG("%c", buff[i]) ; - //} - //LOG("\nEnd buffer\n"); - memset(name, 0, sizeof(name)); - strncpy(name, buff + 157, 100); //157, target link name will be present - memset(dirPath, 0, sizeof(dirPath)); - snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); - LOG(" Creating Symlink [%s][%s]\n", name, dirPath); - ret = symlink(name, dirPath); // use ss_link - if (ret < 0) - LOG("Error with symlink: %d\n", errno); - } - break; - case '3': - LOG(" Ignoring character device %s\n", fullname); - break; - case '4': - LOG(" Ignoring block device %s\n", fullname); - break; - case '5': - //break;//delete - //LOG(" Dir [%s] Item [%s] Length [%d]\n", fullname, item, folderpathlen); - if (strncmp(fullname, item, folderpathlen) == 0) { - //LOG(" Extracting dir %s\n", fullname); - memset(dirPath, 0, sizeof(dirPath)); - snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); - create_dir(dirPath, parseoct(fullname + 100, 8)); - } - - filesize = 0; - break; - case '6': - LOG(" Ignoring FIFO %s\n", fullname); - break; - case GNUTYPE_LONGLINK: - case GNUTYPE_LONGNAME: - { - getheader = 2; - memset(fullname, 0, sizeof(fullname)); - bytes_read = read(fd, fullname, sizeof(fullname)); - if (bytes_read < 512) { - LOGE("Short read on %s: expected 512, got %d\n", tar, bytes_read); - close(fd); - return -1; - } - filesize = 0; - //LOG("Entered LONG FILE NAME CASE new NAME is [%s]\n", fullname); - break; - } - - default: - //break; - //LOG(" File [%s] Item [%s] Length [%d]\n", fullname, item, folderpathlen); - if (strncmp(fullname, item, folderpathlen) == 0) { - if (buff[PREFIX_INDICATOR_BYTE] != 0) { - memset(name, 0, sizeof(name)); - memset(dirPath, 0, sizeof(dirPath)); - strncpy(name, buff, 100); - snprintf(name + strlen(name), sizeof(name) - strlen(name), - "%s", buff + PREFIX_INDICATOR_BYTE); - snprintf(dirPath, sizeof(dirPath), "%s/%s", path, name + folderpathlen); - LOG(" File Name is longer than 100 bytes -Remaining Str [%s]\n Full Str[%s]", dirPath); - } else { - //LOG(" Extracting file %s\n", fullname); - memset(dirPath, 0, sizeof(dirPath)); - snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); - f = create_file(dirPath, parseoct(fullname + 100, 8)); - } - } - - break; - } - - while (filesize > 0) { - bytes_read = read(fd, buff, sizeof(buff)); - if (bytes_read < 512) { - LOGE("Short read on %s: Expected 512, got %d\n", tar, bytes_read); - close(fd); - if(f){ - fclose(f);//wgid: 16892 - f = NULL; - } - return -1; - } - if (filesize < 512) - bytes_read = filesize; - if (f != NULL) { - if (fwrite(buff, 1, bytes_read, f) - != bytes_read) { - LOG("Failed write\n"); - fclose(f); - f = NULL; - close(fd);//wgid: 59268 - return -1;//wgid: 16892 - } - } - filesize -= bytes_read; - } - if (f != NULL) { - fclose(f); - f = NULL; - } - - } - close(fd); - return S_SS_SUCCESS; + char buff[MAX_FILE_PATH]; + FILE *f = NULL; + size_t bytes_read; + int filesize; + int data_offset = -1; + int fd = -1; + char name[512] = { 0, }; + int folderpathlen = 0; + char dirPath[512] = { 0 }; + int getheader = 1; // Asuming initial header is TAR header + char fullname[512] = { 0 }; + int ret; + LOG("Extracting Folder from %s %s to %s\n", tar, item, path); + + data_offset = tar_get_item_offset(tar, item); + if (data_offset < 0) { + LOGE("data offset for [%s] is [%d]\n", item, data_offset); + return -1; + } + + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOGE("can't open file(%s).\n", tar); + return -1; + } + + folderpathlen = strlen(item); + + for (;;) { + bytes_read = read(fd, buff, sizeof(buff)); + if (bytes_read < 512) { + LOGE("Short read on %s: expected 512, got %d\n", tar, bytes_read); + close(fd); + return -1; + } + if (is_end_of_archive(buff)) { + close(fd); + LOG("End of %s\n", tar); //Can stop at end of folder. + return S_SS_SUCCESS; + } + if (!verify_checksum(buff)) { + close(fd); + LOGE("Checksum failure\n"); + return -1; + } + filesize = parseoct(buff + 124, 12); + if (getheader == 2) { + getheader = 1; + //LOG(" Working on LONG FILE NAME CASE [%s]\n", fullname); + } else { + memset(fullname, 0, sizeof(fullname)); + strncpy(fullname, buff, 100); + //LOG(" Working on Normal FILE NAME CASE [%s]\n", fullname); + } + + switch (buff[156]) { + case '1': + LOG(" Ignoring hardlink %s\n", fullname); + break; + case '2': + + //LOG(" Creating symlink %s\n", buff); + if (strncmp(fullname, item, folderpathlen) == 0) { + //LOG("Printing Buffer \n"); + //for(i=157; buff[i] !='\0' ;i++) + //{ + //LOG("%c", buff[i]) ; + //} + //LOG("\nEnd buffer\n"); + memset(name, 0, sizeof(name)); + strncpy(name, buff + 157, 100); //157, target link name will be present + memset(dirPath, 0, sizeof(dirPath)); + snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); + LOG(" Creating Symlink [%s][%s]\n", name, dirPath); + ret = symlink(name, dirPath); // use ss_link + if (ret < 0) + LOG("Error with symlink: %d\n", errno); + } + break; + case '3': + LOG(" Ignoring character device %s\n", fullname); + break; + case '4': + LOG(" Ignoring block device %s\n", fullname); + break; + case '5': + //break;//delete + //LOG(" Dir [%s] Item [%s] Length [%d]\n", fullname, item, folderpathlen); + if (strncmp(fullname, item, folderpathlen) == 0) { + //LOG(" Extracting dir %s\n", fullname); + memset(dirPath, 0, sizeof(dirPath)); + snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); + create_dir(dirPath, parseoct(fullname + 100, 8)); + } + + filesize = 0; + break; + case '6': + LOG(" Ignoring FIFO %s\n", fullname); + break; + case GNUTYPE_LONGLINK: + case GNUTYPE_LONGNAME: + { + getheader = 2; + memset(fullname, 0, sizeof(fullname)); + bytes_read = read(fd, fullname, sizeof(fullname)); + if (bytes_read < 512) { + LOGE("Short read on %s: expected 512, got %d\n", tar, bytes_read); + close(fd); + return -1; + } + filesize = 0; + //LOG("Entered LONG FILE NAME CASE new NAME is [%s]\n", fullname); + break; + } + + default: + //break; + //LOG(" File [%s] Item [%s] Length [%d]\n", fullname, item, folderpathlen); + if (strncmp(fullname, item, folderpathlen) == 0) { + if (buff[PREFIX_INDICATOR_BYTE] != 0) { + memset(name, 0, sizeof(name)); + memset(dirPath, 0, sizeof(dirPath)); + strncpy(name, buff, 100); + snprintf(name + strlen(name), sizeof(name) - strlen(name), + "%s", buff + PREFIX_INDICATOR_BYTE); + snprintf(dirPath, sizeof(dirPath), "%s/%s", path, name + folderpathlen); + LOG(" File Name is longer than 100 bytes -Remaining Str [%s]\n Full Str[%s]", dirPath); + } else { + //LOG(" Extracting file %s\n", fullname); + memset(dirPath, 0, sizeof(dirPath)); + snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); + f = create_file(dirPath, parseoct(fullname + 100, 8)); + } + } + + break; + } + + while (filesize > 0) { + bytes_read = read(fd, buff, sizeof(buff)); + if (bytes_read < 512) { + LOGE("Short read on %s: Expected 512, got %d\n", tar, bytes_read); + close(fd); + if (f) { + fclose(f);//wgid: 16892 + f = NULL; + } + return -1; + } + if (filesize < 512) + bytes_read = filesize; + if (f != NULL) { + if (fwrite(buff, 1, bytes_read, f) + != bytes_read) { + LOG("Failed write\n"); + fclose(f); + f = NULL; + close(fd);//wgid: 59268 + return -1;//wgid: 16892 + } + } + filesize -= bytes_read; + } + if (f != NULL) { + fclose(f); + f = NULL; + } + + } + close(fd); + return S_SS_SUCCESS; } int fast_tar_extract_file(char *tar, char *item, char *pathname, int size, int offset) { - int fd = -1; - int data_size = size; - int data_offset = offset; - off_t pos = 0; - ssize_t rdcnt = 0; - ssize_t writeCount = 0; - char *buf = NULL; - char err_buf[256]; - int fd2; - - if (gTarFd > 0) - fd = gTarFd; - if (fd < 0) { - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - } - pos = lseek(fd, data_offset, SEEK_SET); - if (pos < 0) { - LOG("lseek fail (%s offset %d).\n", tar, data_offset); - close(fd); - return -1; - } - buf = SS_Malloc(data_size + 1); - if (buf == NULL) { - close(fd); - LOGE("Failed to Allocate Memory\n"); - return -1; - } - rdcnt = read(fd, buf, data_size); - if (rdcnt != (ssize_t) data_size) { - LOG(" rdcnt read fail(%s from %s).\n", item, tar); - SS_Free(buf); - close(fd); - return -1; - } - fd2 = open(pathname, O_CREAT | O_WRONLY, 0777); // Directory where file is required should be created already. - if (fd2 < 0) { - LOG("can't open file(%s).\n", pathname); - SS_Free(buf); - close(fd); - return -1; - } - writeCount = write(fd2, buf, rdcnt); - if (writeCount != rdcnt) { - LOG("writeCount write fail(%s from %s).\n", item, tar); - strerror_r(errno, err_buf, sizeof(err_buf)); - LOG("Oh dear, something went wrong with read()! %s\n", err_buf); - close(fd); - close(fd2); - SS_Free(buf); - return -1; - } - SS_Free(buf); - if (gTarFd < 0) - close(fd); - fsync(fd2); - close(fd2); - return rdcnt; // or jus return success? + int fd = -1; + int data_size = size; + int data_offset = offset; + off_t pos = 0; + ssize_t rdcnt = 0; + ssize_t writeCount = 0; + char *buf = NULL; + char err_buf[256]; + int fd2; + + if (gTarFd > 0) + fd = gTarFd; + if (fd < 0) { + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + } + pos = lseek(fd, data_offset, SEEK_SET); + if (pos < 0) { + LOG("lseek fail (%s offset %d).\n", tar, data_offset); + close(fd); + return -1; + } + buf = SS_Malloc(data_size + 1); + if (buf == NULL) { + close(fd); + LOGE("Failed to Allocate Memory\n"); + return -1; + } + rdcnt = read(fd, buf, data_size); + if (rdcnt != (ssize_t) data_size) { + LOG(" rdcnt read fail(%s from %s).\n", item, tar); + SS_Free(buf); + close(fd); + return -1; + } + fd2 = open(pathname, O_CREAT | O_WRONLY, 0777); // Directory where file is required should be created already. + if (fd2 < 0) { + LOG("can't open file(%s).\n", pathname); + SS_Free(buf); + close(fd); + return -1; + } + writeCount = write(fd2, buf, rdcnt); + if (writeCount != rdcnt) { + LOG("writeCount write fail(%s from %s).\n", item, tar); + strerror_r(errno, err_buf, sizeof(err_buf)); + LOG("Oh dear, something went wrong with read()! %s\n", err_buf); + close(fd); + close(fd2); + SS_Free(buf); + return -1; + } + SS_Free(buf); + if (gTarFd < 0) + close(fd); + fsync(fd2); + close(fd2); + return rdcnt; // or jus return success? } int tar_extract_file(char *tar, char *item, char *pathname) { - int fd = -1; - int data_size = -1; - int data_offset = -1; - off_t pos = 0; - ssize_t rdcnt = 0; - ssize_t writeCount = 0; - char *buf = NULL; - char err_buf[256]; - int fd2; - data_size = tar_get_item_size(tar, item); - data_offset = tar_get_item_offset(tar, item); - - if (data_size <= 0 || data_offset < 0) { - LOGE("Error Not a file , size is [%d], offset [%d] for item [%s]\n", data_size, data_offset, item); - return -1; - } else - LOGL(LOG_SSENGINE, "extracting file [%s] size [%d]\n", item, data_size); - fd = open(tar, O_RDONLY); - if (fd < 0) { - LOG("can't open file(%s).\n", tar); - return -1; - } - pos = lseek(fd, data_offset, SEEK_SET); - if (pos < 0) { - LOG("lseek fail (%s offset %d).\n", tar, data_offset); - close(fd); - return -1; - } - buf = SS_Malloc(data_size + 1); - if (buf == NULL) { - close(fd); - LOGE("Failed to Allocate Memory\n"); - return -1; - } - rdcnt = read(fd, buf, data_size); - if (rdcnt != (ssize_t) data_size) { - LOG(" rdcnt read fail(%s from %s).\n", item, tar); - SS_Free(buf); - close(fd); - return -1; - } - fd2 = open(pathname, O_CREAT | O_WRONLY, 0777); // Directory where file is required should be created already. - if (fd2 < 0) { - LOG("can't open file(%s).\n", pathname); - SS_Free(buf); - close(fd); - return -1; - } - writeCount = write(fd2, buf, rdcnt); - if (writeCount != rdcnt) { - LOG("writeCount write fail(%s from %s).\n", item, tar); - strerror_r(errno, err_buf, sizeof(err_buf)); - LOG("Oh dear, something went wrong with read()! %s\n", err_buf); - close(fd); - close(fd2); - SS_Free(buf); - return -1; - } - SS_Free(buf); - close(fd); - fsync(fd2); - close(fd2); - return rdcnt; // or jus return success? + int fd = -1; + int data_size = -1; + int data_offset = -1; + off_t pos = 0; + ssize_t rdcnt = 0; + ssize_t writeCount = 0; + char *buf = NULL; + char err_buf[256]; + int fd2; + data_size = tar_get_item_size(tar, item); + data_offset = tar_get_item_offset(tar, item); + + if (data_size <= 0 || data_offset < 0) { + LOGE("Error Not a file , size is [%d], offset [%d] for item [%s]\n", data_size, data_offset, item); + return -1; + } else + LOGL(LOG_SSENGINE, "extracting file [%s] size [%d]\n", item, data_size); + fd = open(tar, O_RDONLY); + if (fd < 0) { + LOG("can't open file(%s).\n", tar); + return -1; + } + pos = lseek(fd, data_offset, SEEK_SET); + if (pos < 0) { + LOG("lseek fail (%s offset %d).\n", tar, data_offset); + close(fd); + return -1; + } + buf = SS_Malloc(data_size + 1); + if (buf == NULL) { + close(fd); + LOGE("Failed to Allocate Memory\n"); + return -1; + } + rdcnt = read(fd, buf, data_size); + if (rdcnt != (ssize_t) data_size) { + LOG(" rdcnt read fail(%s from %s).\n", item, tar); + SS_Free(buf); + close(fd); + return -1; + } + fd2 = open(pathname, O_CREAT | O_WRONLY, 0777); // Directory where file is required should be created already. + if (fd2 < 0) { + LOG("can't open file(%s).\n", pathname); + SS_Free(buf); + close(fd); + return -1; + } + writeCount = write(fd2, buf, rdcnt); + if (writeCount != rdcnt) { + LOG("writeCount write fail(%s from %s).\n", item, tar); + strerror_r(errno, err_buf, sizeof(err_buf)); + LOG("Oh dear, something went wrong with read()! %s\n", err_buf); + close(fd); + close(fd2); + SS_Free(buf); + return -1; + } + SS_Free(buf); + close(fd); + fsync(fd2); + close(fd2); + return rdcnt; // or jus return success? } diff --git a/ss_engine/ua.h b/ss_engine/ua.h index 7d1e73c..55eae02 100755 --- a/ss_engine/ua.h +++ b/ss_engine/ua.h @@ -21,8 +21,8 @@ #include -#define MAX_FILE_PATH 512 -#define MAX_PART_NAME 32 +#define MAX_FILE_PATH 512 +#define MAX_PART_NAME 32 /* * FOTA Adaptaion header @@ -30,15 +30,15 @@ #define ERROR -1 // 0XFFFFFFFF -#define INVALID 0 -#define VALID 1 +#define INVALID 0 +#define VALID 1 #define TRUE 1 #define FALSE 0 #define OK 0 #define SUCCESS 0 #define FAIL 1 #define RESTORING 2 -#define UA_PARTI_MAX 20 //currently supporting max 20 partitions +#define UA_PARTI_MAX 20 //currently supporting max 20 partitions #define DEFAULT_DELTA_NAME "delta.tar" #define UPDATTE_CFG_FILE "update.cfg" @@ -50,76 +50,76 @@ typedef enum { - FULL_IMG, - DELTA_IMG, - DELTA_FS, - EXTRA + FULL_IMG, + DELTA_IMG, + DELTA_FS, + EXTRA } UA_DATA_FORMAT; typedef enum { - UA_MODE_SCOUT_UPDATE, - UA_MODE_SCOUT, - UA_MODE_VERIFYTARGET, - UA_MODE_UPDATE, - UA_MODE_SUPPLYIMFOM = 200 + UA_MODE_SCOUT_UPDATE, + UA_MODE_SCOUT, + UA_MODE_VERIFYTARGET, + UA_MODE_UPDATE, + UA_MODE_SUPPLYIMFOM = 200 } UA_OPERATION_MODE; typedef struct _ua_update_data_t { - unsigned int exist_check; - unsigned int verify_check; - unsigned int update_check; - unsigned int weight; // the sum of weight should be 100 - unsigned int weight_offset; // start offset - unsigned int data_size; // byte - char *ua_delta_path; // it will be allocated to copy delta file path, need to free memory - char *ua_temp_path; // it will be allocated to copy delta file path, need to free memory + unsigned int exist_check; + unsigned int verify_check; + unsigned int update_check; + unsigned int weight; // the sum of weight should be 100 + unsigned int weight_offset; // start offset + unsigned int data_size; // byte + char *ua_delta_path; // it will be allocated to copy delta file path, need to free memory + char *ua_temp_path; // it will be allocated to copy delta file path, need to free memory } ua_update_data_t; typedef struct _ua_update_cfg_t { - unsigned int update_type; - unsigned int part_idx; - int skip_verify; - int skip_update; - int soure_img_size; //TOTA - int target_img_size; - char *soure_sha1; - char *target_sha1; + unsigned int update_type; + unsigned int part_idx; + int skip_verify; + int skip_update; + int soure_img_size; //TOTA + int target_img_size; + char *soure_sha1; + char *target_sha1; } ua_update_cfg_t; typedef struct _ua_part_info_t { - char *ua_parti_name; - char *ua_subject_name; - char *ua_blk_name; - int ua_blk_offset; + char *ua_parti_name; + char *ua_subject_name; + char *ua_blk_name; + int ua_blk_offset; } ua_part_info_t; // User data structure typedef struct _ua_data_t { // partition operations - ua_part_info_t *parti_info; - ua_update_cfg_t *update_cfg; - ua_update_data_t *update_data; - unsigned long ua_operation; - - int (*ua_op_read_block) (void *, unsigned char *, unsigned long, unsigned long); - int (*ua_op_write_block) (void *, unsigned char *, unsigned long); - void (*ui_progress) (void *, unsigned long); + ua_part_info_t *parti_info; + ua_update_cfg_t *update_cfg; + ua_update_data_t *update_data; + unsigned long ua_operation; + + int (*ua_op_read_block) (void *, unsigned char *, unsigned long, unsigned long); + int (*ua_op_write_block) (void *, unsigned char *, unsigned long); + void (*ui_progress) (void *, unsigned long); } ua_data_t; typedef struct _ua_delta_info_t { - char ua_patch_path[MAX_FILE_PATH]; - char ua_patch_info[MAX_FILE_PATH]; - char ua_delta_path[MAX_FILE_PATH]; - char ua_attrib_path[MAX_FILE_PATH]; + char ua_patch_path[MAX_FILE_PATH]; + char ua_patch_info[MAX_FILE_PATH]; + char ua_delta_path[MAX_FILE_PATH]; + char ua_attrib_path[MAX_FILE_PATH]; } ua_delta_info_t; typedef struct _ua_dataSS_t { // partition operations - ua_part_info_t *parti_info; - ua_update_cfg_t *update_cfg; - ua_update_data_t *update_data; - ua_delta_info_t *update_delta; - unsigned long ua_operation; - void (*ui_progress) (void *, unsigned long); - int (*write_data_to_blkdev) (char *, int, int, char *); + ua_part_info_t *parti_info; + ua_update_cfg_t *update_cfg; + ua_update_data_t *update_data; + ua_delta_info_t *update_delta; + unsigned long ua_operation; + void (*ui_progress) (void *, unsigned long); + int (*write_data_to_blkdev) (char *, int, int, char *); } ua_dataSS_t; #endif -- 2.7.4 From 2b92c7dbadc57ddd457f8395656d48f99d3dc401 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Tue, 26 Sep 2017 11:25:40 +0900 Subject: [PATCH 03/16] Fix svace issue Change-Id: I3b7737174837bbc696bf4513d15367d818ad2934 Signed-off-by: Sunmin Lee --- ss_engine/SS_PatchDelta.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index 3395881..4a527d6 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -864,6 +864,7 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * backupsrc = SS_BackupSource(ua_dataSS->parti_info->ua_blk_name); if (backupsrc != 0) { LOGE("failed to Backup source File:[%s] \n", ua_dataSS->parti_info->ua_blk_name); + SS_Free(source_file.data); SS_SetUpgradeState(E_SS_IMGSRCBACKUPFAILED); return E_SS_FAILURE; } @@ -876,8 +877,7 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * SS_CopyFile(NULL, SS_DD_SOURCE, SS_DD_TARGET); if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) - if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > - 0) + if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > 0) LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); else { LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); @@ -895,7 +895,6 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * fp = fopen(source_filename, "w"); if (!fp) { LOGE("file open error [%s] code [%d]\n", source_filename, errno); - SS_Free(source_file.data); result = E_SS_FAILURE; goto Cleanup; } @@ -903,7 +902,6 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * read_count = fwrite(source_file.data, 1, source_file.size, fp); if (read_count != source_file.size) { LOGE("file write error read_count = %d for [%s]\n", read_count, source_filename); - SS_Free(source_file.data); result = E_SS_FAILURE; goto Cleanup; } @@ -976,8 +974,8 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * result = E_SS_FAILURE; goto Cleanup; } - snprintf(outname, strlen(source_filename) + 10, "%s.patch", - source_filename); + snprintf(outname, strlen(source_filename) + 10, "%s.patch", + source_filename); output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); if (output < 0) { @@ -1064,6 +1062,7 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * SS_PatchSourceClear(); SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); + SS_Free(source_file.data); SS_Free(buf); SS_Free(outname);//wgid: 20740 if (result == S_SS_SUCCESS) -- 2.7.4 From ecc4e41e1aef72d36b9edf6ab28bafcdef165266 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Tue, 10 Oct 2017 11:17:22 +0900 Subject: [PATCH 04/16] Fix svace issue Change-Id: I91e2715d2e7c04e7141005322716e9806cea8d99 Signed-off-by: Sunmin Lee --- ss_engine/SS_PatchDelta.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index 4a527d6..f3ebdc3 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -905,7 +905,6 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * result = E_SS_FAILURE; goto Cleanup; } - SS_Free(source_file.data); fclose(fp); fp = NULL;//wgid: 59313 @@ -1040,6 +1039,8 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * fp = NULL; fclose(kp); + SS_Free(source_file.data); + source_file.data = NULL; if (SS_LoadFile(source_filename, &source_file) == 0) result = memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE); if (result != S_SS_SUCCESS) { -- 2.7.4 From 0b2316dc3126b37865a13d359ce1048ee52c601c Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Fri, 13 Oct 2017 10:09:56 +0900 Subject: [PATCH 05/16] Fix vulnerabilities Change-Id: I73965454cc282be6e373805931b9d5790f74ef21 Signed-off-by: Sunmin Lee --- bsdiff/ss_bsdiff.c | 3 ++- bsdiff/ss_bspatch.c | 2 ++ ss_engine/SS_PatchDelta.c | 5 +++-- ss_engine/SS_UPI.c | 1 + ss_engine/fota_tar.c | 8 ++++---- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/bsdiff/ss_bsdiff.c b/bsdiff/ss_bsdiff.c index 68967ee..848e7ff 100755 --- a/bsdiff/ss_bsdiff.c +++ b/bsdiff/ss_bsdiff.c @@ -575,7 +575,8 @@ int MY_CDECL main(int numArgs, const char *args[]) err(1, "bsdiff fails to create delta within timelimit"); #endif int res = main2(numArgs, args, rs, sizeof(rs)); - remove("temp_patch"); + if (remove("temp_patch") < 0) + printf("Failed to remove temp_patch\n"); fputs(rs, stdout); return res; } diff --git a/bsdiff/ss_bspatch.c b/bsdiff/ss_bspatch.c index 1915223..8b6117f 100755 --- a/bsdiff/ss_bspatch.c +++ b/bsdiff/ss_bspatch.c @@ -108,6 +108,8 @@ int main2(int numArgs, const char *args[], char *rs, int rs_size) for (i = 0; i < 8; i++) unpackSize += (UInt64)header[LZMA_PROPS_SIZE + i] << (i * 8); buf_res = (unsigned char *)malloc(unpackSize); + if (!buf_res) + return PrintError(rs, "Failed to allocate memory", rs_size); memset(buf_res, 0x0, unpackSize); LzmaDec_Construct(&state); RINOK(LzmaDec_Allocate(&state, header, LZMA_PROPS_SIZE, &g_Alloc)); diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index f3ebdc3..ad906cb 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -120,7 +120,8 @@ int getOffset(char *zimage_path) LOGL(LOG_SSENGINE, "match for %d %d %d found at %d\n", buf[0], buf[1], buf[2], ftell(f) - 3); break; } else { - fseek(f, -2, SEEK_CUR); + if (fseek(f, -2, SEEK_CUR) < 0) + LOGE("Failed to fseek\n"); } } offset = ftell(f) - 3; @@ -631,7 +632,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, //Now proceed wit patch application since patch can be applied do { int enough_space = 0; - size_t free_space; + size_t free_space = 0; char *tok; if (retry > 0) { diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index aef89db..6c32229 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -659,6 +659,7 @@ struct details *SS_FSGetDeltaCount(char *ubDeltaPath, char *ubDeltaInfoFile) goto cleanup; } + memset(FileData, 0, size + 1); data_size = tar_get_cfg_data(ubDeltaPath, ubDeltaInfoFile, FileData, size); if (data_size <= 0) { // == 0 is NOT okay?? LOGE("Failed to read cfg data from Delta\n"); diff --git a/ss_engine/fota_tar.c b/ss_engine/fota_tar.c index 9c63c82..362dec5 100755 --- a/ss_engine/fota_tar.c +++ b/ss_engine/fota_tar.c @@ -541,7 +541,7 @@ tar_Data_t *tar_build_cfg_table(char *tar) if (header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGNAME || header[TAR_ITEM_TYPE_FLAG_POS] == GNUTYPE_LONGLINK) { //rdcnt = read(fd, header, sizeof(header)); memset(uExtendedName, 0, sizeof(uExtendedName)); - rdcnt = read(fd, uExtendedName, sizeof(uExtendedName)); + rdcnt = read(fd, uExtendedName, sizeof(uExtendedName) - 1); if (rdcnt <= 0) { LOG("read failed. (rdcnt=%d)\n", rdcnt); ret = -1; @@ -608,7 +608,7 @@ tar_Data_t *tar_build_cfg_table(char *tar) if (ret != -1) return headparam; else { - if (newnode) + if (headparam != newnode && newnode) SS_Free(newnode); if (headparam) { local_temp = headparam; @@ -936,8 +936,8 @@ int tar_extract_folder(char *tar, char *item, char *path) memset(name, 0, sizeof(name)); memset(dirPath, 0, sizeof(dirPath)); strncpy(name, buff, 100); - snprintf(name + strlen(name), sizeof(name) - strlen(name), - "%s", buff + PREFIX_INDICATOR_BYTE); + snprintf(name + strlen(name), sizeof(name) - strlen(name), + "%s", buff + PREFIX_INDICATOR_BYTE); snprintf(dirPath, sizeof(dirPath), "%s/%s", path, name + folderpathlen); LOG(" File Name is longer than 100 bytes -Remaining Str [%s]\n Full Str[%s]", dirPath); } else { -- 2.7.4 From e07c98b8e6a68c847d4fedc7a08ed2d5d2466191 Mon Sep 17 00:00:00 2001 From: shirshkumar Date: Tue, 17 Oct 2017 18:28:56 +0530 Subject: [PATCH 06/16] Remove experimental code from libtota Change-Id: I1dd350dbcd2df13dd7a0109b02fcefa7d784b072 Signed-off-by: shirshkumar --- ss_engine/SS_PatchDelta.c | 307 ---------------------------------------------- ss_engine/SS_UPI.c | 44 ------- ss_engine/fota_common.h | 23 ---- 3 files changed, 374 deletions(-) diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index ad906cb..d312d06 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -215,314 +215,7 @@ int SS_LoadFile(const char *filename, FileInfo * file) extern int gvalid_session; extern void create_dir(char *pathname, int mode); -#ifdef SUPPORT_CONTAINER -//unzip source archive , apply patch from extracted delta folder and repack the source archive -int SS_UpdateArchive(ua_dataSS_t * ua_dataSS, const char *source_filename, const char *target_filename, - const char *source_sha1_str, const char *target_sha1_str) -{ - FILE *fp; - char *line = NULL, *token = NULL, *source_file = NULL, *new_file = NULL, *dir_to_create = NULL, *patch_file = NULL; - char patch_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for patches - char source_path_full[MAX_FILE_PATH] = { 0, }; //absolute path for uncompressed source files - char target_path_full[MAX_FILE_PATH] = { 0, }; - char patchlist[MAX_FILE_PATH] = { 0, }; - char cmd[2 * MAX_FILE_PATH] = { 0, }; - size_t len = 0, read = 0; - int result = S_SS_SUCCESS; - uint8_t target_sha1[SHA_DIGEST_SIZE]; - uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; - FileInfo source_data = { 0, }; - int backupsrc = -1; - SinkFn sink = NULL; - void *tok = NULL; - int output = -1; - char *outname = NULL; - if (ParseSha1(target_sha1_str, target_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); - return E_SS_FAILURE; - } - - if (0 == gvalid_session) { - if (ParseSha1(source_sha1_str, source_sha1) != 0) { - LOGE("failed to parse tgt-sha1 \"%s\"\n", source_sha1_str); - return E_SS_FAILURE; - } - if (SS_LoadFile(source_filename, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); - if (source_data.data) - SS_Free(source_data.data); - } else if (memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); - if (source_data.data) - SS_Free(source_data.data); - return S_SS_SUCCESS; - } else { - //Check for backup file SHA - SS_Free(source_data.data); - source_data.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { - LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, - "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } - } else { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); - if (SS_LoadFile(SS_BACKUP_SOURCE, &source_data) == 0) { - if (memcmp(source_data.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != 0) { - LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, strerror(errno)); - SS_SetUpgradeState(E_SS_FSUPDATEFAILED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } else { - SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); - if (source_data.data) - SS_Free(source_data.data); - return E_SS_FAILURE; - } - } - } -#ifndef ENHANCED_BSDIFF - backupsrc = SS_BackupSource(source_filename); - if (backupsrc != 0) { - LOGE("failed to Backup source File:[%s] \n", source_filename); - SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); - return E_SS_FAILURE; - } -#endif - //create workspace for processing container upgrade - SS_CreateFolder(NULL, SS_ARCHIVE_WORK_FOLDER); - SS_CreateFolder(NULL, SS_ARCHIVE_UNPACK_FOLDER); - - //unpack the source container to the unpack workspace - snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, source_filename, SS_ARCHIVE_UNPACK_FOLDER); - result = _system_cmd_wait(cmd); - if (result != S_SS_SUCCESS) { - LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); - return E_SS_FAILURE; - } - //extract unpack scipt from delta.tar to process containers later - if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) - if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > - 0) - LOGL(LOG_SSENGINE, "Extracted %s successfully\n", SS_KERNEL_UNPACK_SCRIPT); - else { - LOGE("Error in fn tar_extract_file for item %s", SS_KERNEL_UNPACK_SCRIPT); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - result = E_SS_FAILURE; - } - //move new tpk extracted in the delta folder to the work folder - new_file = strrchr(target_filename, '/'); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); - - result = rename(source_path_full, target_path_full); - if (result != 0) { - LOGE("fatal error in moving %s to %s\n", source_path_full, target_path_full); - return E_SS_FAILURE; - } - snprintf(cmd, sizeof(cmd) - 1, "%s -qo %s -d %s", SS_UNZIP_COMMAND, target_path_full, SS_ARCHIVE_WORK_FOLDER); - result = _system_cmd_wait(cmd); - if (result != S_SS_SUCCESS) { - LOGE("zip extraction for [%s] failed, code [%d]\n", cmd, result); - return E_SS_FAILURE; - } else - LOGL(LOG_SSENGINE, "Thin zip extracted successfully\n"); - // open the patch list and start iterating through the changes and the same files - snprintf(patchlist, MAX_FILE_PATH, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, SS_CONTAINER_INFO_FILE); - fp = fopen(patchlist, "r"); - if (!fp) { - LOGE("file open error [%s]\n", patchlist); - return E_SS_FAILURE; - } - - while ((read = getline(&line, &len, fp)) != -1) { - - switch (line[0]) { // '-' == Delete File, 's' == same File, 'c' == Changed File - case 's': //for same files case, just extract from old tpk to the work folder, update new tpk in the end - token = strtok(line, SS_SEPARATOR_TOKEN); - - source_file = strtok(NULL, SS_NEWLINE_TOKEN); - - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - LOGL(LOG_SSENGINE, "copy %s\n", source_file); - result = SS_MoveFile(NULL, source_path_full, target_path_full); - if (result != S_SS_SUCCESS) { - LOGE("fatal error [%d]\n", errno); - goto Cleanup; - } - break; - case 'c': - token = strtok(line, SS_SEPARATOR_TOKEN); - - source_file = strtok(NULL, SS_SEPARATOR_TOKEN); - patch_file = strtok(NULL, SS_NEWLINE_TOKEN); - - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_UNPACK_FOLDER, source_file); - snprintf(target_path_full, sizeof(target_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - LOGL(LOG_SSENGINE, "copy %s\n", source_file); - result = SS_MoveFile(NULL, source_path_full, target_path_full); - if (result != S_SS_SUCCESS) - LOGE("fatal error [%d]\n", errno); - - snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/%s", SS_ARCHIVE_DELTA_FOLDER, patch_file); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, source_file); - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(source_path_full) + 10); - if (outname == NULL) - goto Cleanup; - strcpy(outname, source_path_full); - strcat(outname, ".patch"); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); - SS_Free(outname); - goto Cleanup; - } - sink = ss_fileSink; - tok = &output; - } - result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); - LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != 0) { - LOGE("applying patch failed %s\n", source_path_full); - if (outname != NULL) - unlink(outname); - goto Cleanup; - } - result = rename(outname, source_path_full); - if (result != 0) { - LOGE("fatal error %s\n", source_path_full); - goto Cleanup; - } else { - LOGL(LOG_SSENGINE, "Successfully applied patch for [%s]\n", source_path_full); - } - break; - default: - break; - } - } - - new_file = strrchr(target_filename, '/'); - snprintf(cmd, sizeof(cmd) - 1, "%s -p %s %s /opt/data/fota", SS_KERN_UNPK_SCRIPT_PATH, SS_ARCHIVE_WORK_FOLDER, - new_file + 1); - int ret = _system_cmd_wait(cmd); - LOGL(LOG_SSENGINE, "ret for %s is %d\n", cmd, ret); - - //Apply diff between intermediate new tpk and the new tpk which contains central dir changes only - snprintf(patch_path_full, sizeof(patch_path_full) - 1, "%s/New_%s.delta", SS_ARCHIVE_DELTA_FOLDER, new_file + 1); - snprintf(source_path_full, sizeof(source_path_full) - 1, "%s/%s", SS_ARCHIVE_WORK_FOLDER, new_file + 1); - - { - // We write the decoded output to ".patch". - //allocate some extra space to allow for concatinating ".patch" with the name - outname = (char *)SS_Malloc(strlen(source_path_full) + 10); - if (outname == NULL) - return E_SS_FAILURE; - strcpy(outname, source_path_full); - strcat(outname, ".patch"); - - output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); - if (output < 0) { - LOGE("failed to open output file %s: %s\n", outname, strerror(errno)); - SS_Free(outname); - goto Cleanup; - } - sink = ss_fileSink; - tok = &output; - } - result = SS_ApplyBsdiff((char *)source_path_full, outname, patch_path_full, sink, tok, NULL); - LOGL(LOG_SSENGINE, "GenerateTarget Output is %d and result is %d\n", output, result); - if (output >= 0) { - fsync(output); - close(output); - } - - if (result != 0) { - LOGE("applying patch failed %s\n", source_path_full); - if (outname != NULL) - unlink(outname); - goto Cleanup; - } - - if (SS_LoadFile(outname, &source_data) == 0) - result = memcmp(source_data.sha1, target_sha1, SHA_DIGEST_SIZE); - if (result != S_SS_SUCCESS) { - LOGE("patch did not produce expected sha1 \n"); - SS_SetUpgradeState(E_SS_IMGSHA_MISMATCH); - goto Cleanup; - } - - result = rename(outname, source_path_full); - if (result != 0) { - LOGE("fatal error %s\n", source_path_full); - goto Cleanup; - } - //Delete old file and copy patched archive, cant use rename as partitions may be different - unlink(source_filename); - if (result != 0) { - LOGE("failed to unlink [%s] code [%d]\n", source_filename, errno); - goto Cleanup; - } - result = (int)SS_CopyFile(NULL, source_path_full, target_filename); - if (result != S_SS_SUCCESS) { - LOGE("failed to copy file [%s] result [%d]\n", source_path_full, result); - goto Cleanup; - } - - Cleanup: - fclose(fp); - if (line) - SS_Free(line); - if (outname) - SS_Free(outname); - SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); - SS_DeleteFile(NULL, SS_FIND_CMD_TARGET); - SS_DeleteFolder(NULL, SS_CONTAINER_WORKSPACE); - return result; -} -#endif /*! ********************************************************************************* * SS_UpdateDeltaFS diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index 6c32229..9dddd56 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -1262,46 +1262,6 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); pFsNode = pFsNode->nextnode; } -#ifdef SUPPORT_CONTAINER - else if (pFsNode->data_size == 0) { //need to add logic to identify archive update - ulResult = tar_extract_folder(ua_dataSS->update_data->ua_delta_path, ubPatch, - SS_ARCHIVE_DELTA_FOLDER); - if (ulResult != S_SS_SUCCESS) { - LOGE("extraction failed for [%s] result [%d]\n", ubPatch, ulResult); - SS_SetUpgradeState(E_SS_DELTA_IS_CORRUPT); - break; - } - //Copy exe's for zip , unzip and find operations - code to be removed after ramdisk upgrade - ulResult = (int)SS_CopyFile(NULL, SS_ZIP_SOURCE, SS_ZIP_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy unzip [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = (int)SS_CopyFile(NULL, SS_UNZIP_SOURCE, SS_UNZIP_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy unzip [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = (int)SS_CopyFile(NULL, SS_FIND_CMD_SOURCE, SS_FIND_CMD_TARGET); - if (ulResult != S_SS_SUCCESS) { - LOGE("failed to copy find [%d]\n", ulResult); - SS_SetUpgradeState(E_SS_WRITE_ERROR); - break; - } - ulResult = - SS_UpdateArchive(ua_dataSS, pFsNode->file_old_path, pFsNode->file_new_path, pFsNode->sha1src, - pFsNode->sha1trg); - if (ulResult != S_SS_SUCCESS) { - LOGE("FS update Failed, Unable to extact archive\n"); - break; - } - - SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); - pFsNode = pFsNode->nextnode; - } -#endif else { ulResult = E_SS_FAILURE; tar_close(); @@ -1315,10 +1275,6 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f t2 = atoi(ts1); LOG("Shirsh time for DIFFS - [%d] \n", (t2 - t1)); #endif -#ifdef SUPPORT_CONTAINER - SS_DeleteFile(NULL, SS_ZIP_TARGET); - SS_DeleteFile(NULL, SS_UNZIP_TARGET); -#endif tar_close(); } break; diff --git a/ss_engine/fota_common.h b/ss_engine/fota_common.h index 6dbe909..0d2720d 100755 --- a/ss_engine/fota_common.h +++ b/ss_engine/fota_common.h @@ -52,9 +52,6 @@ typedef unsigned long long u64; #ifndef MEM_PROFILING //#define MEM_PROFILING #endif -#ifndef SUPPORT_CONTAINER - //#define SUPPORT_CONTAINER -#endif #define UNUSED(x) (void)(x) @@ -114,26 +111,6 @@ typedef unsigned long long u64; #define SS_STRING_ZIP "ZIP" #define SS_STRING_END "END" -#ifdef SUPPORT_CONTAINER -#define SS_ZIP_SOURCE "system/usr/bin/zip" -#define SS_ZIP_TARGET "system/opt/data/fota/zip" -#define SS_UNZIP_SOURCE "system/usr/bin/unzip" -#define SS_UNZIP_TARGET "system/opt/data/fota/unzip" -#define SS_FIND_CMD_SOURCE "system/usr/bin/find" -#define SS_FIND_CMD_TARGET "system/opt/data/fota/find" -#define SS_ZIP_COMMAND "/opt/data/fota/zip" -#define SS_UNZIP_COMMAND "/opt/data/fota/unzip" -#define SS_FIND_COMMAND "/opt/data/fota/find" -#define SS_SEPARATOR_TOKEN "|" -#define SS_NEWLINE_TOKEN "\n" -#define SS_FW_SLASH_TOKEN '/' -#define SS_CONTAINER_INFO_FILE "PATCH.txt" -#define SS_CONTAINER_WORKSPACE SS_COMMON_WORKSPACE "/archive" -#define SS_ARCHIVE_WORK_FOLDER SS_CONTAINER_WORKSPACE "/tpk" -#define SS_ARCHIVE_DELTA_FOLDER SS_CONTAINER_WORKSPACE "/delta" -#define SS_ARCHIVE_UNPACK_FOLDER SS_CONTAINER_WORKSPACE "/unpack" -#endif - #ifdef MEM_PROFILING #define SS_MEMORY_USAGE_LOG SS_COMMON_WORKSPACE "/log_memory" #define SS_MEMORY_PROFILING_SCRIPT SS_COMMON_WORKSPACE "/mem_use.sh" -- 2.7.4 From d2bf25e3a64e9ee3ba144b9c134e81b7565d556b Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Wed, 25 Oct 2017 17:01:19 +0900 Subject: [PATCH 07/16] Release 1.0.0 libtota for Tizen 4.0 is released (accepted/tizen/4.0/unified/20171017.211102) Change-Id: Icbdcb99d674c2dd76d4cd4f661f8fb5740a21ca6 Signed-off-by: Sunmin Lee --- packaging/libtota.spec | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index 5bec19f..a277a09 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -1,7 +1,7 @@ Name: libtota Summary: fota update library -Version: 0.2.3 -Release: 4 +Version: 1.0.0 +Release: 0 Group: System License: Apache-2.0 and BSD-2-Clause and BSD-3-Clause and PD Source0: %{name}-%{version}.tar.gz -- 2.7.4 From 19efac96407b671ac34b1a5186576c66e6213e62 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Mon, 30 Oct 2017 16:28:49 +0900 Subject: [PATCH 08/16] Remove deprecated member variable - NOTICE : it must be merged along with the tota-ua patch - https://review.tizen.org/gerrit/#/c/158032/ Change-Id: Ibd14a0f92a6270f450a90eecd2c6a6e06f624baa Signed-off-by: Kichan Kwon --- ss_engine/ua.h | 1 - 1 file changed, 1 deletion(-) diff --git a/ss_engine/ua.h b/ss_engine/ua.h index 55eae02..7d2971f 100755 --- a/ss_engine/ua.h +++ b/ss_engine/ua.h @@ -77,7 +77,6 @@ typedef struct _ua_update_data_t { typedef struct _ua_update_cfg_t { unsigned int update_type; - unsigned int part_idx; int skip_verify; int skip_update; int soure_img_size; //TOTA -- 2.7.4 From 3099f1751574eedefa237505a1504285670d7af8 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Mon, 6 Nov 2017 11:52:48 +0900 Subject: [PATCH 09/16] Remove unused parameter Change-Id: I23baef728f4d259a1c1df402240452efb9cc8974 Signed-off-by: Kichan Kwon --- ss_engine/SS_Common.c | 4 +-- ss_engine/SS_Engine_Update.h | 4 +-- ss_engine/SS_FSUpdate.c | 66 ++++++++++++++++----------------------- ss_engine/SS_FSUpdate.h | 60 +++++++++-------------------------- ss_engine/SS_MultiProcessUpdate.h | 17 ++-------- ss_engine/SS_PatchDelta.c | 18 +++++------ ss_engine/SS_PatchDelta.h | 11 +++---- ss_engine/SS_UPI.c | 52 +++++++++++++++--------------- ss_engine/SS_UPI.h | 2 +- ss_engine/fota_tar.c | 4 +-- 10 files changed, 91 insertions(+), 147 deletions(-) diff --git a/ss_engine/SS_Common.c b/ss_engine/SS_Common.c index 6722938..a169dd6 100755 --- a/ss_engine/SS_Common.c +++ b/ss_engine/SS_Common.c @@ -42,7 +42,7 @@ void SS_Progress(void *pbUserData, SS_UINT32 uPercent) } /* Prints a string like the C printf() function */ -SS_UINT32 SS_Trace(void *pUser, const char *aFormat, ...) +SS_UINT32 SS_Trace(const char *aFormat, ...) { #if 0 LOGL(LOG_SSENGINE, aFormat); @@ -59,7 +59,7 @@ SS_UINT32 SS_Trace(void *pUser, const char *aFormat, ...) return S_SS_SUCCESS; } -long SS_FSTrace(void *pbUserData, const unsigned short *aFormat, ...) +long SS_FSTrace(const unsigned short *aFormat, ...) { va_list list; diff --git a/ss_engine/SS_Engine_Update.h b/ss_engine/SS_Engine_Update.h index 2f2ccc8..7c7fde9 100755 --- a/ss_engine/SS_Engine_Update.h +++ b/ss_engine/SS_Engine_Update.h @@ -517,15 +517,13 @@ extern "C" { * \li %s: Null-terminated string * \li %d: Signed decimal integer * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param aFormat Printf-like format string * \param ... Items to insert in \a aFormat * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ...); + SS_UINT32 SS_Trace(const char *aFormat, ...); /** ******************************************************************************* diff --git a/ss_engine/SS_FSUpdate.c b/ss_engine/SS_FSUpdate.c index 187d209..fd0b52a 100755 --- a/ss_engine/SS_FSUpdate.c +++ b/ss_engine/SS_FSUpdate.c @@ -119,8 +119,7 @@ long SS_recursive_folder_creater(const char *path, const mode_t mode) } long -SS_CopyFile(void *pbUserData, - const char *strFromPath, const char *strToPath) +SS_CopyFile(const char *strFromPath, const char *strToPath) { int fd1, fd2; int readCount = 0, writeCount = 0; @@ -143,7 +142,7 @@ SS_CopyFile(void *pbUserData, fd1 = open(path1, O_RDONLY); if (fd1 < 0) return E_SS_OPENFILE_ONLYR; - ret = SS_OpenFile(pbUserData, strToPath, ONLY_W, (long *)&fd2); + ret = SS_OpenFile(strToPath, ONLY_W, (long *)&fd2); if (ret != S_SS_SUCCESS || fd2 < 0) { close(fd1); LOGE(" SS_OpenFile fail leaved path1:%s | path2:%s\n", path1, path1); @@ -168,7 +167,7 @@ SS_CopyFile(void *pbUserData, return ret; } -long SS_DeleteFile(void *pbUserData, const char *strPath) +long SS_DeleteFile(const char *strPath) { char path[MAX_PATH] = { '\0' }; int ret = 0; @@ -195,14 +194,13 @@ int SS_unlink_cbf(const char *fpath, const struct stat *sb, int typeflag, struct } return rv; } -long SS_DeleteFolder(void *pbUserData, const char *strPath) +long SS_DeleteFolder(const char *strPath) { //runs till either tree is exhausted or when unlink_cbf returns non zero value. return ((long)nftw(strPath, SS_unlink_cbf, 64, FTW_DEPTH | FTW_PHYS) == 0) ? S_SS_SUCCESS : E_SS_FAILURE; - } -long SS_DeleteFolderEmpty(void *pbUserData, const char *strPath) +long SS_DeleteFolderEmpty(const char *strPath) { int ret = 0; @@ -220,7 +218,7 @@ long SS_DeleteFolderEmpty(void *pbUserData, const char *strPath) return E_SS_FAILURE; } -long SS_CreateFolder(void *pbUserData, const char *strPath) +long SS_CreateFolder(const char *strPath) { mode_t mode = 0; int ret = 0; @@ -283,8 +281,7 @@ mode_t SS_get_mode(E_RW_TYPE wFlag) } long -SS_OpenFile(void *pbUserData, - const char *strPath, E_RW_TYPE wFlag, long *pwHandle) +SS_OpenFile(const char *strPath, E_RW_TYPE wFlag, long *pwHandle) { mode_t mode; char path[MAX_PATH] = { '\0' }; @@ -327,7 +324,7 @@ SS_OpenFile(void *pbUserData, SS_char_to_unicode((const char *)dir, (char *)dirShort, MAX_PATH - 1); - if (SS_CreateFolder(pbUserData, dirShort)) { + if (SS_CreateFolder(dirShort)) { LOGE(" Fail create folder, Leave SS_OpenFile\n"); return E_SS_OPENFILE_WRITE; } @@ -349,7 +346,7 @@ SS_OpenFile(void *pbUserData, return S_SS_SUCCESS; } -long SS_ResizeFile(void *pbUserData, long wHandle, SS_UINT32 dwSize) +long SS_ResizeFile(long wHandle, SS_UINT32 dwSize) { int ret = 0; @@ -365,7 +362,7 @@ long SS_ResizeFile(void *pbUserData, long wHandle, SS_UINT32 dwSize) return ret; } -long SS_CloseFile(void *pbUserData, long wHandle) +long SS_CloseFile(long wHandle) { int ret = 0; LOGL(LOG_SSENGINE, "wHandle = %ld\n", wHandle); @@ -387,8 +384,7 @@ long SS_CloseFile(void *pbUserData, long wHandle) } long -SS_WriteFile(void *pbUserData, - long wHandle, +SS_WriteFile(long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize) { @@ -416,8 +412,7 @@ SS_WriteFile(void *pbUserData, return S_SS_SUCCESS; } -long SS_MoveFile(void *pbUserData, const char *strFromPath, - const char *strToPath) +long SS_MoveFile(const char *strFromPath, const char *strToPath) { int ret = 0; struct stat sbuf; @@ -451,7 +446,7 @@ long SS_MoveFile(void *pbUserData, const char *strFromPath, } } else if (errno == 18) { //EXDEV 18 /* Cross-device link */ //Moving file across partitions if mount point is different (Extremely rare) - ret = (int)SS_CopyFile(NULL, path1, path2); + ret = (int)SS_CopyFile(path1, path2); if (ret != S_SS_SUCCESS) { LOGE("failed to copy file [%s] result [%d]\n", path1, ret); return E_SS_WRITE_ERROR; @@ -469,14 +464,13 @@ long SS_MoveFile(void *pbUserData, const char *strFromPath, LOGL(LOG_INFO, "leaved path1:%s | path2:%s\n", path1, path2); return S_SS_SUCCESS; } -long SS_SyncFile(void *pbUserData, long wHandle) +long SS_SyncFile(long wHandle) { return (-1 == fsync(wHandle)) ? E_SS_FAILURE : S_SS_SUCCESS; } long -SS_ReadFile(void *pbUserData, - long wHandle, +SS_ReadFile(long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize) { @@ -501,7 +495,7 @@ SS_ReadFile(void *pbUserData, return E_SS_READ_ERROR; } - if (ret != dwSize && ((ret + dwPosition) != (unsigned long)SS_GetFileSize(pbUserData, wHandle))) + if (ret != dwSize && ((ret + dwPosition) != (unsigned long)SS_GetFileSize(wHandle))) return E_SS_READ_ERROR; #if 0 @@ -510,7 +504,7 @@ SS_ReadFile(void *pbUserData, return S_SS_SUCCESS; } -long SS_GetFileSize(void *pbUserData, long wHandle) +long SS_GetFileSize(long wHandle) { int ret = 0; @@ -527,7 +521,7 @@ long SS_GetFileSize(void *pbUserData, long wHandle) return ret; } -long SS_Unlink(void *pbUserData, char *pLinkName) +long SS_Unlink(char *pLinkName) { int ret = 0; char path[MAX_PATH] = { '\0' }; @@ -547,9 +541,7 @@ long SS_Unlink(void *pbUserData, char *pLinkName) } long -SS_VerifyLinkReference(void *pbUserData, - char *pLinkName, - char *pReferenceFileName) +SS_VerifyLinkReference(char *pLinkName, char *pReferenceFileName) { int ret = 0; char path[MAX_PATH] = { '\0' }; @@ -597,11 +589,11 @@ SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName) if (ret >= 0) { if (S_ISREG(sbuf.st_mode)) { LOGL(LOG_SSENGINE, " stat->st_mode = regular file, To be deleted and create a LINK \n"); - SS_DeleteFile(pbUserData, sympath); + SS_DeleteFile(sympath); SS_Link(pbUserData, pLinkName, pReferenceFileName); } } - if (SS_VerifyLinkReference(pbUserData, pLinkName + if (SS_VerifyLinkReference(pLinkName , pReferenceFileName) == S_SS_SUCCESS) { return S_SS_SUCCESS; } else { @@ -618,8 +610,7 @@ SS_Link(void *pbUserData, char *pLinkName, char *pReferenceFileName) return S_SS_SUCCESS; } -long SS_GetFileType(void *pbUserData, - char *pLinkName, enumFileType * fileType) +long SS_GetFileType(char *pLinkName, enumFileType * fileType) { int ret = 0; char path[MAX_PATH] = { '\0' }; @@ -1064,10 +1055,7 @@ long SS_SetFileAttributes(const char *ui16pFilePath, } -long SS_CompareFileAttributes(void *pbUserData, - char *pFilePath, - unsigned char *pAdditionalAttribs, - unsigned long iAddiInfoSize) +long SS_CompareFileAttributes(void) { return S_SS_SUCCESS; } @@ -1078,7 +1066,7 @@ long SS_CompareFileAttributes(void *pbUserData, /* vrm functions */ long -SS_GetAvailableFreeSpace(void *pbUserData, const char *partition_name, +SS_GetAvailableFreeSpace(const char *partition_name, SS_UINT32 *available_flash_size) { // *available_flash_size = MAX_INT; @@ -1143,7 +1131,7 @@ void SS_Free(void *pMemBlock) free(pMemBlock); } -SS_UINT32 SS_GetMaxNumProcess(void *user) +SS_UINT32 SS_GetMaxNumProcess(void) { return SAMPLE_PROCS_NUM; } @@ -1176,12 +1164,12 @@ void* SS_WaitForProcess(const void *handle, SS_UINT32* process_exit_code) return (void*)pid; } -unsigned long SS_GetMaxProcRamSize(void *user) +unsigned long SS_GetMaxProcRamSize(void) { return UA_RAM_SIZE; } -void* SS_RunProcess(void *pbUserData, int argc, char* argv[]) +void* SS_RunProcess(void) { pid_t child_pid; diff --git a/ss_engine/SS_FSUpdate.h b/ss_engine/SS_FSUpdate.h index 49eda03..0cc839b 100755 --- a/ss_engine/SS_FSUpdate.h +++ b/ss_engine/SS_FSUpdate.h @@ -50,8 +50,6 @@ extern "C" { * in the old file, if any. Must check if the source file is a symbolic link. * If it is, instead create a new symbolic link using \ref SS_Link. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strFromPath Path to old file * \param strToPath Path to new file * @@ -59,14 +57,12 @@ extern "C" { ******************************************************************************* */ - long SS_CopyFile(void *pbUserData, const char *strFromPath, const char *strToPath); + long SS_CopyFile(const char *strFromPath, const char *strToPath); /*! ******************************************************************************* * Move (rename) file.

* - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strFromPath Path to old file location * \param strToPath Path to new file location * @@ -74,7 +70,7 @@ extern "C" { ******************************************************************************* */ - long SS_MoveFile(void *pbUserData, const char *strFromPath, const char *strToPath); + long SS_MoveFile(const char *strFromPath, const char *strToPath); /*! ******************************************************************************* @@ -82,8 +78,6 @@ extern "C" { * * Must return success if the file is deleted or didn't exist. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strPath Path to file * * \return S_SS_SUCCESS on success, E_SS_DELETEFILE if the file cannot be @@ -91,7 +85,7 @@ extern "C" { ******************************************************************************* */ - long SS_DeleteFile(void *pbUserData, const char *strPath); + long SS_DeleteFile(const char *strPath); /*! ******************************************************************************* @@ -99,15 +93,13 @@ extern "C" { * * Must return success if the folder is now deleted or didn't exist. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strPath Path to folder * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_DeleteFolder(void *pbUserData, const char *strPath); + long SS_DeleteFolder(const char *strPath); /*! ******************************************************************************* @@ -117,15 +109,13 @@ extern "C" { * recommended that the new folder's attributes are a copy of its parent's * attributes. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strPath Path to folder * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_CreateFolder(void *pbUserData, const char *strPath); + long SS_CreateFolder(const char *strPath); /*! ******************************************************************************* @@ -134,8 +124,6 @@ extern "C" { * Must create the the file (and the path to the file) if it doesn't exist. Must * open in binary mode. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param strPath Path to file * \param wFlag Read/write mode, an \ref E_RW_TYPE value * \param pwHandle (out) File handle @@ -146,14 +134,12 @@ extern "C" { ******************************************************************************* */ - long SS_OpenFile(void *pbUserData, const char *strPath, E_RW_TYPE wFlag, long *pwHandle); + long SS_OpenFile(const char *strPath, E_RW_TYPE wFlag, long *pwHandle); /*! ******************************************************************************* * Set file size. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * \param dwSize New file size * @@ -161,21 +147,19 @@ extern "C" { ******************************************************************************* */ - long SS_ResizeFile(void *pbUserData, long wHandle, SS_UINT32 dwSize); + long SS_ResizeFile(long wHandle, SS_UINT32 dwSize); /*! ******************************************************************************* * Close file. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_CloseFile(void *pbUserData, long wHandle); + long SS_CloseFile(long wHandle); /*! ******************************************************************************* @@ -184,8 +168,6 @@ extern "C" { * Must return success if the block is written or at least resides in * non-volatile memory. Use \ref SS_SyncFile to commit the file to storage. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * \param dwPosition Position within the file to which to write * \param pbBuffer Data to write @@ -195,7 +177,7 @@ extern "C" { ******************************************************************************* */ - long SS_WriteFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); + long SS_WriteFile(long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); /*! ******************************************************************************* @@ -203,14 +185,12 @@ extern "C" { * * Generally called after \ref SS_WriteFile. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_SyncFile(void *pbUserData, long wHandle); + long SS_SyncFile(long wHandle); /*! ******************************************************************************* @@ -218,8 +198,6 @@ extern "C" { * If fewer bytes than requested are available in the specified position, this * function should read up to the end of file and return S_SS_SUCCESS. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * \param dwPosition Position within the file from which to read * \param pbBuffer Buffer to contain data @@ -229,27 +207,23 @@ extern "C" { ******************************************************************************* */ - long SS_ReadFile(void *pbUserData, long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); + long SS_ReadFile(long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); /*! ******************************************************************************* * Get file size. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param wHandle File handle * * \return File size, -1 if file not found, or < -1 on error ******************************************************************************* */ - long SS_GetFileSize(void *pbUserData, long wHandle); + long SS_GetFileSize(long wHandle); /*! ******************************************************************************* * Get free space of a mounted file system. * - * \param pbUserData Optional opaque data-structure to pass to - * IPL functions * \param path Name of any directory within the mounted * file system * \param available_flash_size (out) Available space @@ -257,7 +231,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_GetAvailableFreeSpace(void *pbUserData, const char *path, SS_UINT32 * available_flash_size); + long SS_GetAvailableFreeSpace(const char *path, SS_UINT32 * available_flash_size); /*! ******************************************************************************* @@ -268,14 +242,12 @@ extern "C" { * If your platform does not support symbolic links, you do not need to * implement this. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param pLinkName Link * * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - long SS_Unlink(void *pbUserData, char *pLinkName); + long SS_Unlink(char *pLinkName); /*! ******************************************************************************* @@ -327,8 +299,6 @@ extern "C" { ******************************************************************************* * Print status and debug information. * - * \param pbUserData Optional opaque data-structure to pass to IPL - * functions * \param aFormat A NULL-terminated printf-like string with support for * the following tags: * \li %x: Hex number @@ -340,7 +310,7 @@ extern "C" { * \return S_SS_SUCCESS on success or an \ref SS_vRM_Errors.h error code ******************************************************************************* */ - SS_UINT32 SS_Trace(void *pbUserData, const char *aFormat, ...); + SS_UINT32 SS_Trace(const char *aFormat, ...); /** ******************************************************************************* diff --git a/ss_engine/SS_MultiProcessUpdate.h b/ss_engine/SS_MultiProcessUpdate.h index e0cfa12..825f337 100755 --- a/ss_engine/SS_MultiProcessUpdate.h +++ b/ss_engine/SS_MultiProcessUpdate.h @@ -116,39 +116,28 @@ extern "C" { * The new process must call \ref SS_HandleProcessRequest with the same * arguments. * - * \param user Optional opaque data-structure passed in during - * initialization - * \param argc Number of \a argv arguments - * \param argv Arguments to pass to the new process - * * \return Handle to the new process or NULL on error ******************************************************************************* */ - void *SS_RunProcess(void *user, int argc, char *argv[]); + void *SS_RunProcess(void); /** ******************************************************************************* * Get the maximum number of processes that are allowed to run. * - * \param user Optional opaque data-structure passed in during - * initialization - * * \return The number of allowed processes or 0 if multiple processes are not * allowed ******************************************************************************* */ - SS_UINT32 SS_GetMaxNumProcess(void *user); + SS_UINT32 SS_GetMaxNumProcess(void); /** ******************************************************************************* * Get the maximum available memory for processes to use. * - * \param user Optional opaque data-structure passed in during - * initialization - * * \return The memory amount available for processes or 0 if there is no available memory ******************************************************************************* */ - unsigned long SS_GetMaxProcRamSize(void *user); + unsigned long SS_GetMaxProcRamSize(void); /** ******************************************************************************* diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index d312d06..1294a09 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -279,7 +279,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - if (SS_CopyFile(NULL, SS_BACKUP_SOURCE, source_filename) != S_SS_SUCCESS) { + if (SS_CopyFile(SS_BACKUP_SOURCE, source_filename) != S_SS_SUCCESS) { strerror_r(errno, buf, sizeof(buf)); LOGE("copy of backup to \"%s\" failed: %s\n", source_filename, buf); SS_SetUpgradeState(E_SS_FSUPDATEFAILED); @@ -333,7 +333,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, tok = strrchr(source_filename, '/'); *tok = '\0'; } - SS_GetAvailableFreeSpace(NULL, source_filename, &free_space); + SS_GetAvailableFreeSpace(source_filename, &free_space); enough_space = (free_space > (256 << 10)) && // 256k (two-block) minimum (free_space > (patch_data_size * 3 / 2)); // 50% margin of error if (use_backup) @@ -564,11 +564,11 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * } } //Cleanup workspace and copy helper executables to it before proceeding - SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); + SS_DeleteFolder(SS_KERNEL_WORKSPACE); create_dir(SS_KERNEL_WORKSPACE, 0755); - SS_CopyFile(NULL, SS_GZIP_SOURCE, SS_GZIP_TARGET); - SS_CopyFile(NULL, SS_STAT_SOURCE, SS_STAT_TARGET); - SS_CopyFile(NULL, SS_DD_SOURCE, SS_DD_TARGET); + SS_CopyFile(SS_GZIP_SOURCE, SS_GZIP_TARGET); + SS_CopyFile(SS_STAT_SOURCE, SS_STAT_TARGET); + SS_CopyFile(SS_DD_SOURCE, SS_DD_TARGET); if (tar_get_item_size(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT) > 0) if (tar_extract_file(ua_dataSS->update_data->ua_delta_path, SS_KERNEL_UNPACK_SCRIPT, SS_KERN_UNPK_SCRIPT_PATH) > 0) @@ -755,8 +755,8 @@ int SS_UpdateDeltaKernel(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char * Cleanup: SS_BackupSourceClear(); SS_PatchSourceClear(); - SS_DeleteFile(NULL, SS_KERN_UNPK_SCRIPT_PATH); - SS_DeleteFolder(NULL, SS_KERNEL_WORKSPACE); + SS_DeleteFile(SS_KERN_UNPK_SCRIPT_PATH); + SS_DeleteFolder(SS_KERNEL_WORKSPACE); SS_Free(source_file.data); SS_Free(buf); SS_Free(outname);//wgid: 20740 @@ -907,7 +907,7 @@ int SS_UpdateDeltaIMG(ua_dataSS_t * ua_dataSS, int (*write_to_blkdev) (char *, i result = E_SS_FAILURE; goto Cleanup; } - result = SS_WriteFile(NULL, fd, 0, msi.buffer, msi.size); + result = SS_WriteFile(fd, 0, msi.buffer, msi.size); if (result != S_SS_SUCCESS) { LOGE("failed to write\n"); SS_SetUpgradeState(E_SS_IMGRECOVERYWRITEFAILED); diff --git a/ss_engine/SS_PatchDelta.h b/ss_engine/SS_PatchDelta.h index fd3bb66..d789419 100755 --- a/ss_engine/SS_PatchDelta.h +++ b/ss_engine/SS_PatchDelta.h @@ -62,17 +62,16 @@ int ApplyBSDiffPatchMem(const unsigned char *old_data, ssize_t old_size, int SS_LoadPartition(const char *filename, FileInfo * file); int SS_LoadFile(const char *filename, FileInfo * file); extern void SS_SetUpgradeState(int Val); -extern long SS_GetAvailableFreeSpace(void *pbUserData, const char *partition_name, SS_UINT32 * available_flash_size); +extern long SS_GetAvailableFreeSpace(const char *partition_name, SS_UINT32 * available_flash_size); extern int SS_BackupSource(const char *source_filename); extern int SS_ApplyBsdiff(char *oldfile, char *newfile, char *patch, SinkFn sink, void *token, sha1_ctx_t * ctx1); extern int SS_BackupSourceClear(); extern int SS_PatchSourceClear(); -extern long SS_WriteFile(void *pbUserData, - long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); +extern long SS_WriteFile(long wHandle, SS_UINT32 dwPosition, unsigned char *pbBuffer, SS_UINT32 dwSize); extern void SS_Free(void * pMemBlock); -extern long SS_CopyFile(void * pbUserData, const char * strFromPath, const char * strToPath); -extern long SS_DeleteFolder(void * pbUserData, const char * strPath); -extern long SS_DeleteFile(void * pbUserData, const char * strPath); +extern long SS_CopyFile(const char * strFromPath, const char * strToPath); +extern long SS_DeleteFolder(const char * strPath); +extern long SS_DeleteFile(const char * strPath); extern int tar_get_item_size(char * tar, char * item); extern int tar_extract_file(char * tar, char * item, char * pathname); extern int _system_cmd_wait(const char * command); diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index 9dddd56..db19b99 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -124,7 +124,7 @@ static double get_time_stamp2(void) #endif //Check SS function if available -int file_exist(char *filename, int type) +int file_exist(char *filename) { struct stat buf; int ret = 0; @@ -292,8 +292,8 @@ int SS_verify_DELTA_image(char *filename) Cleanup: if (fp) fclose(fp); - if (file_exist(SS_IMAGE_MAGIC_KEY_VAL, 0)) - SS_DeleteFile(NULL, SS_IMAGE_MAGIC_KEY_VAL); + if (file_exist(SS_IMAGE_MAGIC_KEY_VAL)) + SS_DeleteFile(SS_IMAGE_MAGIC_KEY_VAL); return ulResult; } @@ -373,7 +373,7 @@ int SS_rename1(const char *old_file_name, const char *new_file_name) * ********************************************************************************* */ -int SS_FSVerifyNode(const char *ubDeltaPath, const char *path, const char *patchname, const char *sha1src, int type, +int SS_FSVerifyNode(const char *path, const char *patchname, const char *sha1src, int type, char *patchpath_name, int *data_size, int *data_offset) { char patch[MAX_FILE_PATH] = { 0 }; @@ -381,7 +381,7 @@ int SS_FSVerifyNode(const char *ubDeltaPath, const char *path, const char *patch uint8_t source_sha1[SHA_DIGEST_SIZE]; if (gvalid_session) { - if ((type == DIFFS || type == DELETES || type == MOVES) && !file_exist((char *)path, type)) { + if ((type == DIFFS || type == DELETES || type == MOVES) && !file_exist((char *)path)) { LOGE("failed to verifyNodes [does not exist], Path : [%s] Type[%d]\n", path, type); SS_SetUpgradeState(E_SS_FSBADNODES); return E_SS_FAILURE; @@ -467,7 +467,7 @@ int SS_AppendNode(const char *ubDeltaPath, fs_params ** headparam, fs_params ** return E_SS_FAILURE; } if ((E_SS_FAILURE == - SS_FSVerifyNode(ubDeltaPath, old_path, patchname, sha1src, type, patchpath_name, &data_size, &data_offset))) { + SS_FSVerifyNode(old_path, patchname, sha1src, type, patchpath_name, &data_size, &data_offset))) { LOGE("Bad Nodes, Failed to pass verification - [Delta Path - %s][OldPath - %s] [NewPath - %s] \n", ubDeltaPath, old_path, new_path); return E_SS_FAILURE; @@ -1077,7 +1077,7 @@ SYM:NEW:system/lib/firmware/vbc_eq:/opt/system/vbc_eq return fs_head_node; } -void SS_GetPartition_LocDetails(ua_dataSS_t * ua_dataSS, int part_idx) +void SS_GetPartition_LocDetails(ua_dataSS_t * ua_dataSS) { LOGL(LOG_SSENGINE, "PART NAME: [%s] \n", ua_dataSS->parti_info->ua_parti_name); snprintf(ua_dataSS->update_delta->ua_patch_path, MAX_FILE_PATH, "%s", ua_dataSS->parti_info->ua_subject_name); @@ -1286,7 +1286,7 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f #endif while (pFsNode) { LOGL(LOG_SSENGINE, "MOVES update Index: [%d] \n", ulFileIndex++); - ulResult = SS_MoveFile(NULL, pFsNode->file_old_path, pFsNode->file_new_path); + ulResult = SS_MoveFile(pFsNode->file_old_path, pFsNode->file_new_path); if (ulResult != S_SS_SUCCESS) { LOGE("Move Failed for [%s] to [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); SS_SetUpgradeState(ulResult); @@ -1312,11 +1312,11 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f while (pFsNode) { if (pFsNode->type == DELETES) { LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); - SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); + SS_GetFileType(pFsNode->file_old_path, (enumFileType *) & ulFiletype); if (ulFiletype == 2) //FT_FOLDER - ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); + ulResult = SS_DeleteFolder(pFsNode->file_old_path); else - ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); + ulResult = SS_DeleteFile(pFsNode->file_old_path); if (ulResult != S_SS_SUCCESS) { LOGE("Delete Failed\n"); SS_SetUpgradeState(ulResult); @@ -1343,11 +1343,11 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f while (pFsNode) { if (pFsNode->type == DELETE_END) { LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); - SS_GetFileType(NULL, pFsNode->file_old_path, (enumFileType *) & ulFiletype); + SS_GetFileType(pFsNode->file_old_path, (enumFileType *) & ulFiletype); if (ulFiletype == 2) //FT_FOLDER - ulResult = SS_DeleteFolder(NULL, pFsNode->file_old_path); + ulResult = SS_DeleteFolder(pFsNode->file_old_path); else - ulResult = SS_DeleteFile(NULL, pFsNode->file_old_path); + ulResult = SS_DeleteFile(pFsNode->file_old_path); if (ulResult != S_SS_SUCCESS) { LOGE("Delete Failed\n"); SS_SetUpgradeState(ulResult); @@ -1379,7 +1379,7 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f LOGL(LOG_SSENGINE, "7zip extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); else LOGL(LOG_SSENGINE, "tar extraction error for %s\n", ua_dataSS->parti_info->ua_parti_name); - SS_DeleteFile(NULL, SS_NEW_COMPRESSED_FILE); + SS_DeleteFile(SS_NEW_COMPRESSED_FILE); #ifdef TIME_PROFILING get_time_stamp1(); //total time capturing t2 = atoi(ts1); @@ -1396,7 +1396,7 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f while (pFsNode) { LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++); //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name); - ulResult = SS_Unlink(NULL, pFsNode->file_old_path); + ulResult = SS_Unlink(pFsNode->file_old_path); if (ulResult == S_SS_SUCCESS) ulResult = SS_Link(NULL, pFsNode->file_new_path, pFsNode->patch_name); else { @@ -1507,7 +1507,7 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) if (!head_ptr_node) return E_SS_FSBADNODES; - SS_GetPartition_LocDetails(ua_dataSS, part_idx); + SS_GetPartition_LocDetails(ua_dataSS); LOGL(LOG_SSENGINE, "FS Update Entry PartIndex: [%d]\n", part_idx); @@ -1650,7 +1650,7 @@ int SS_FSVerifyPartition(ua_dataSS_t * ua_dataSS, int part_idx) LOGL(LOG_SSENGINE, "Enough space for Partition [%s]\n", ua_dataSS->parti_info->ua_parti_name); } - SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); + SS_GetAvailableFreeSpace(SS_COMMON_WORKSPACE, &free_space); //Checking for 2 times the file size free space , as delta can be worst case size of file. if ((free_space) < (2 * ua_dataSS->update_cfg->soure_img_size)) { LOGE("Not enough free space [%d] for max size [%d]\n", free_space, (2 * ua_dataSS->update_cfg->soure_img_size)); @@ -1662,7 +1662,7 @@ int SS_FSVerifyPartition(ua_dataSS_t * ua_dataSS, int part_idx) if (!(S_SS_SUCCESS == SS_Do_Memory_Profiling())) return E_SS_FAILURE; #endif - SS_GetPartition_LocDetails(ua_dataSS, part_idx); + SS_GetPartition_LocDetails(ua_dataSS); LOGL(LOG_SSENGINE, "FS Verification Start PartIndex:[%d]\n", part_idx); if (ua_dataSS->ua_operation == UI_OP_SCOUT) gvalid_session = TRUE; // (shd b true if called during verification) @@ -1690,7 +1690,7 @@ int SS_BackupSource(const char *source_filename) int ret = E_SS_FAILURE; if (source_filename) { - ret = (int)SS_CopyFile(NULL, source_filename, SS_BACKUP_SOURCE); + ret = (int)SS_CopyFile(source_filename, SS_BACKUP_SOURCE); if (ret != S_SS_SUCCESS) { LOGE("failed to back up source file Error [%d]\n", ret); SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); @@ -1702,7 +1702,7 @@ int SS_BackupSource(const char *source_filename) int SS_BackupSourceClear(void) { int ret = E_SS_FAILURE; - ret = (int)SS_DeleteFile(NULL, SS_BACKUP_SOURCE); + ret = (int)SS_DeleteFile(SS_BACKUP_SOURCE); if (ret != S_SS_SUCCESS) { LOGE("failed to delete BACKUP file\n"); SS_SetUpgradeState(E_SS_FSSRCBACKUPFAILED); @@ -1713,7 +1713,7 @@ int SS_BackupSourceClear(void) int SS_PatchSourceClear(void) { int ret = E_SS_FAILURE; - ret = (int)SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); + ret = (int)SS_DeleteFile(SS_PATCHFILE_SOURCE); if (ret != S_SS_SUCCESS) { LOGE("failed to delete PATCHFILE file\n"); SS_SetUpgradeState(E_SS_PATCHFILE_DEL_ERROR); @@ -1793,8 +1793,8 @@ int SS_IMGVerifyFullImage(ua_dataSS_t * ua_dataSS) } Cleanup: - if (file_exist(SS_PATCHFILE_SOURCE, 0)) - SS_DeleteFile(NULL, SS_PATCHFILE_SOURCE); + if (file_exist(SS_PATCHFILE_SOURCE)) + SS_DeleteFile(SS_PATCHFILE_SOURCE); return ulResult; } @@ -1813,7 +1813,7 @@ int SS_IMGVerfiyPartition(ua_dataSS_t * ua_dataSS) } //We verify twice the image size for BACKUP source, not on Partition. As Patch will be created on RAM - SS_GetAvailableFreeSpace(NULL, SS_COMMON_WORKSPACE, &free_space); + SS_GetAvailableFreeSpace(SS_COMMON_WORKSPACE, &free_space); if ((free_space) < (2 * ua_dataSS->update_cfg->target_img_size)) { LOGE("Not enough free space [%d] for twice max size [%d]\n", free_space, (2 * ua_dataSS->update_cfg->target_img_size)); @@ -1915,7 +1915,7 @@ int SS_IMGUpdatemain(ua_dataSS_t * ua_dataSS, int update_type) //SS_FSUpdatePar ua_dataSS->ui_progress(ua_dataSS, 40); if (update_type == FULL_IMG && ua_dataSS->update_data->ua_temp_path) - ulResult = SS_MoveFile(ua_dataSS, SS_PATCHFILE_SOURCE, ua_dataSS->update_data->ua_temp_path); + ulResult = SS_MoveFile(SS_PATCHFILE_SOURCE, ua_dataSS->update_data->ua_temp_path); else if ((ua_dataSS->update_cfg->update_type == DELTA_IMG && ua_dataSS->write_data_to_blkdev) || ua_dataSS->update_cfg->update_type == EXTRA) { diff --git a/ss_engine/SS_UPI.h b/ss_engine/SS_UPI.h index 8a4e351..ce91f70 100755 --- a/ss_engine/SS_UPI.h +++ b/ss_engine/SS_UPI.h @@ -81,6 +81,6 @@ extern int SS_UpdateDeltaFS(const char *source_filename, const char *target_file extern int tar_extract_file(char *tar, char *item, char *pathname); extern int _7zdecompress(char *path); extern void tar_free_cfg_table(tar_Data_t ** delta_tar); -extern long SS_GetFileType(void *pbUserData, char *pLinkName, enumFileType * fileType); +extern long SS_GetFileType(char *pLinkName, enumFileType * fileType); #endif //_SS_UPI_H_ diff --git a/ss_engine/fota_tar.c b/ss_engine/fota_tar.c index 362dec5..6f82cec 100755 --- a/ss_engine/fota_tar.c +++ b/ss_engine/fota_tar.c @@ -169,7 +169,7 @@ void create_dir(char *pathname, int mode) } /* Create a file, including parent directory as necessary. */ -static FILE *create_file(char *pathname, int mode) +static FILE *create_file(char *pathname) { FILE *f; f = fopen(pathname, "w+"); @@ -944,7 +944,7 @@ int tar_extract_folder(char *tar, char *item, char *path) //LOG(" Extracting file %s\n", fullname); memset(dirPath, 0, sizeof(dirPath)); snprintf(dirPath, sizeof(dirPath), "%s/%s", path, fullname + folderpathlen); - f = create_file(dirPath, parseoct(fullname + 100, 8)); + f = create_file(dirPath); } } -- 2.7.4 From 461dc0645d19f388966a854273493b5291c0490d Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Fri, 3 Nov 2017 19:02:50 +0900 Subject: [PATCH 10/16] Modify ua.h to ua_types.h for integrating header with tota-ua - ua_types.h : structure (both packages use) - ua.h : macro, error code (only tota-ua uses) Change-Id: Id7fa9900236ade160c0e9a2bfbbb63f851bb55c4 Signed-off-by: Kichan Kwon --- CMakeLists.txt | 1 + packaging/libtota.spec | 1 + ss_engine/SS_Common.c | 2 +- ss_engine/SS_Common.h | 2 +- ss_engine/SS_FSUpdate.c | 2 +- ss_engine/SS_Nand.c | 2 +- ss_engine/SS_PatchDelta.c | 2 +- ss_engine/SS_UPI.c | 4 ++-- ss_engine/fota_tar.c | 2 +- ss_engine/{ua.h => ua_types.h} | 44 ++++++++++++++---------------------------- 10 files changed, 24 insertions(+), 38 deletions(-) rename ss_engine/{ua.h => ua_types.h} (65%) diff --git a/CMakeLists.txt b/CMakeLists.txt index fe4c89d..d437011 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ SET(HEADERS ss_engine/SS_MultiProcessUpdate.h ss_engine/SS_Nand.h ss_engine/SS_UPI.h + ss_engine/ua_types.h ) #SET(EXEC_PREFIX "\${prefix}") SET(PREFIX ${CMAKE_INSTALL_PREFIX}) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index a277a09..a79c8f2 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -76,6 +76,7 @@ cp libtota.a %{buildroot}%{_libdir}/libtota.a %{_includedir}/SS_MultiProcessUpdate.h %{_includedir}/SS_Nand.h %{_includedir}/SS_UPI.h +%{_includedir}/ua_types.h %files -n tota-bsdiff %{_bindir}/ss_bsdiff diff --git a/ss_engine/SS_Common.c b/ss_engine/SS_Common.c index a169dd6..cf7714d 100755 --- a/ss_engine/SS_Common.c +++ b/ss_engine/SS_Common.c @@ -29,7 +29,7 @@ #include "SS_Engine_Errors.h" #include "SS_Common.h" -#include "ua.h" +#include "ua_types.h" #include "fota_tar.h" #include "fota_common.h" diff --git a/ss_engine/SS_Common.h b/ss_engine/SS_Common.h index d7175ba..453b61b 100755 --- a/ss_engine/SS_Common.h +++ b/ss_engine/SS_Common.h @@ -24,7 +24,7 @@ #include "SS_Engine_Errors.h" #include "SS_Engine_Update.h" -#include "ua.h" +#include "ua_types.h" #define MAX_PATH 256 diff --git a/ss_engine/SS_FSUpdate.c b/ss_engine/SS_FSUpdate.c index fd0b52a..0e004c8 100755 --- a/ss_engine/SS_FSUpdate.c +++ b/ss_engine/SS_FSUpdate.c @@ -36,7 +36,7 @@ #include "SS_Common.h" #include "fota_common.h" -#include "ua.h" +#include "ua_types.h" static int feature_support_capability; diff --git a/ss_engine/SS_Nand.c b/ss_engine/SS_Nand.c index 21d1e65..9fcb51d 100755 --- a/ss_engine/SS_Nand.c +++ b/ss_engine/SS_Nand.c @@ -32,7 +32,7 @@ #include "SS_ImageUpdate.h" #include "SS_Engine_Errors.h" #include "SS_Common.h" -#include "ua.h" +#include "ua_types.h" #include "fota_common.h" static int local_pid; diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index 1294a09..d0f5eab 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -26,7 +26,7 @@ #include #include #include -#include "ua.h" +#include "ua_types.h" #include "sha1.h" #include "SS_PatchDelta.h" #include "fota_common.h" diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index db19b99..290ba58 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -32,7 +32,7 @@ Function Prototypes Mandatory #include #include #include -#include "ua.h" +#include "ua_types.h" #include "SS_Common.h" #include "fota_common.h" #include "SS_UPI.h" @@ -1665,7 +1665,7 @@ int SS_FSVerifyPartition(ua_dataSS_t * ua_dataSS, int part_idx) SS_GetPartition_LocDetails(ua_dataSS); LOGL(LOG_SSENGINE, "FS Verification Start PartIndex:[%d]\n", part_idx); if (ua_dataSS->ua_operation == UI_OP_SCOUT) - gvalid_session = TRUE; // (shd b true if called during verification) + gvalid_session = 1; // (shd b true if called during verification) headptr_list[part_idx] = SS_FSBuildNodes(ua_dataSS); #ifdef TIME_PROFILING LOGL(LOG_SSENGINE, "fast_tar_get_item_size_time :[%lf]\n", fast_tar_get_item_size_time); diff --git a/ss_engine/fota_tar.c b/ss_engine/fota_tar.c index 6f82cec..7f8ba30 100755 --- a/ss_engine/fota_tar.c +++ b/ss_engine/fota_tar.c @@ -28,7 +28,7 @@ #include "SS_FSUpdate.h" #include "fota_common.h" #include "fota_tar.h" -#include "ua.h" +#include "ua_types.h" /* tar Header Block, from POSIX 1003.1-1990. for reference */ #if 0 diff --git a/ss_engine/ua.h b/ss_engine/ua_types.h similarity index 65% rename from ss_engine/ua.h rename to ss_engine/ua_types.h index 7d2971f..c7d70b1 100755 --- a/ss_engine/ua.h +++ b/ss_engine/ua_types.h @@ -16,38 +16,22 @@ * limitations under the License. */ -#ifndef __UA_H__ -#define __UA_H__ +#ifndef __UA_TYPES_H__ +#define __UA_TYPES_H__ #include #define MAX_FILE_PATH 512 -#define MAX_PART_NAME 32 /* * FOTA Adaptaion header */ -#define ERROR -1 // 0XFFFFFFFF - -#define INVALID 0 -#define VALID 1 -#define TRUE 1 -#define FALSE 0 -#define OK 0 -#define SUCCESS 0 -#define FAIL 1 -#define RESTORING 2 #define UA_PARTI_MAX 20 //currently supporting max 20 partitions -#define DEFAULT_DELTA_NAME "delta.tar" -#define UPDATTE_CFG_FILE "update.cfg" #define UI_OP_SCOUT_UPDATE 0 #define UI_OP_SCOUT 1 #define UI_OP_UPDATE 3 -//SS_ENGINE && BSDIFF -#define SS_RECOVERYRAMDISK - typedef enum { FULL_IMG, @@ -68,18 +52,18 @@ typedef struct _ua_update_data_t { unsigned int exist_check; unsigned int verify_check; unsigned int update_check; - unsigned int weight; // the sum of weight should be 100 + unsigned int weight; // the sum of weight should be 100 unsigned int weight_offset; // start offset - unsigned int data_size; // byte - char *ua_delta_path; // it will be allocated to copy delta file path, need to free memory - char *ua_temp_path; // it will be allocated to copy delta file path, need to free memory + unsigned int data_size; // byte + char *ua_delta_path; // it will be allocated to copy delta file path, need to free memory + char *ua_temp_path; // it will be allocated to copy delta file path, need to free memory } ua_update_data_t; typedef struct _ua_update_cfg_t { unsigned int update_type; int skip_verify; int skip_update; - int soure_img_size; //TOTA + int soure_img_size; //TOTA int target_img_size; char *soure_sha1; char *target_sha1; @@ -93,15 +77,15 @@ typedef struct _ua_part_info_t { } ua_part_info_t; // User data structure -typedef struct _ua_data_t { // partition operations +typedef struct _ua_data_t { // partition operations ua_part_info_t *parti_info; ua_update_cfg_t *update_cfg; ua_update_data_t *update_data; unsigned long ua_operation; - int (*ua_op_read_block) (void *, unsigned char *, unsigned long, unsigned long); - int (*ua_op_write_block) (void *, unsigned char *, unsigned long); - void (*ui_progress) (void *, unsigned long); + int (*ua_op_read_block)(void *, unsigned char *, unsigned long, unsigned long); + int (*ua_op_write_block)(void *, unsigned char *, unsigned long); + void (*ui_progress)(void *, unsigned long); } ua_data_t; typedef struct _ua_delta_info_t { @@ -111,14 +95,14 @@ typedef struct _ua_delta_info_t { char ua_attrib_path[MAX_FILE_PATH]; } ua_delta_info_t; -typedef struct _ua_dataSS_t { // partition operations +typedef struct _ua_dataSS_t { // partition operations ua_part_info_t *parti_info; ua_update_cfg_t *update_cfg; ua_update_data_t *update_data; ua_delta_info_t *update_delta; unsigned long ua_operation; - void (*ui_progress) (void *, unsigned long); - int (*write_data_to_blkdev) (char *, int, int, char *); + void (*ui_progress)(void *, unsigned long); + int (*write_data_to_blkdev)(char *, int, int, char *); } ua_dataSS_t; #endif -- 2.7.4 From 036e953d52b0e5c77257efd31df5645106704640 Mon Sep 17 00:00:00 2001 From: Kichan Kwon Date: Mon, 30 Oct 2017 16:28:49 +0900 Subject: [PATCH 11/16] Remove deprecated member variable - NOTICE : it must be merged along with the tota-ua patch - https://review.tizen.org/gerrit/#/c/161213/ Change-Id: Ie04ad91fe88a1a703a7d135e349493352022cc74 Signed-off-by: Kichan Kwon --- packaging/libtota.spec | 2 +- ss_engine/ua.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index a277a09..a7699df 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -1,6 +1,6 @@ Name: libtota Summary: fota update library -Version: 1.0.0 +Version: 1.1.0 Release: 0 Group: System License: Apache-2.0 and BSD-2-Clause and BSD-3-Clause and PD diff --git a/ss_engine/ua.h b/ss_engine/ua.h index 55eae02..7d2971f 100755 --- a/ss_engine/ua.h +++ b/ss_engine/ua.h @@ -77,7 +77,6 @@ typedef struct _ua_update_data_t { typedef struct _ua_update_cfg_t { unsigned int update_type; - unsigned int part_idx; int skip_verify; int skip_update; int soure_img_size; //TOTA -- 2.7.4 From 018a225b622fdd94bab51ed3e10cd52a1ba9dd09 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Wed, 31 Jan 2018 10:36:04 +0900 Subject: [PATCH 12/16] Apply PIC to libtota Make libtota into position-independent code Change-Id: I9a482dddd0ad39533adf8e9890c69ab8afae3450 Signed-off-by: Sunmin Lee --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d437011..a1e41e2 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,7 +52,7 @@ ENDFOREACH(flag) SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -Wall") SET(EXTRA_CFLAGS "${EXTRA_CFLAGS} -g ") -SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}") +SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS} -fPIC") FIND_PROGRAM(UNAME NAMES uname) EXEC_PROGRAM("${UNAME}" ARGS "-m" OUTPUT_VARIABLE "ARCH") -- 2.7.4 From 70cce15e4031cea69929d96d9248af9a531a896a Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Thu, 1 Feb 2018 15:26:54 +0900 Subject: [PATCH 13/16] Stop building diff tools by gbs The diff tools package is not being used. Instead of it, local_build script is available to install it directly. Change-Id: I640e537ca4f6f27975be83b25b124a800a2dfea6 Signed-off-by: Sunmin Lee --- CMakeLists.txt | 2 -- packaging/libtota.spec | 14 +------------- 2 files changed, 1 insertion(+), 15 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a1e41e2..8df6b0f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -82,5 +82,3 @@ INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/tota.pc DESTINATION ${LIB_INSTALL_DIR} FOREACH(hfile ${HEADERS}) INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/${hfile} DESTINATION include) ENDFOREACH(hfile) - -ADD_SUBDIRECTORY(bsdiff) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index 1e35a00..9dccfbd 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -1,6 +1,6 @@ Name: libtota Summary: fota update library -Version: 1.1.0 +Version: 1.2.0 Release: 0 Group: System License: Apache-2.0 and BSD-2-Clause and BSD-3-Clause and PD @@ -9,7 +9,6 @@ Source0: %{name}-%{version}.tar.gz BuildRequires: cmake BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(lib7zip) -BuildRequires: pkgconfig(libdivsufsort) %description Fota update agent which update firmware using delta files @@ -23,13 +22,6 @@ Group: Development/Libraries %description devel Description: libfactory library (development) -%package -n tota-bsdiff -Summary: bsdiff / bspatch tools for TOTA - -%description -n tota-bsdiff -bsdiff / bspatch are tools for building and applying patches to binary files. -This package offers these tools for TOTA. - %prep %setup -q @@ -77,7 +69,3 @@ cp libtota.a %{buildroot}%{_libdir}/libtota.a %{_includedir}/SS_Nand.h %{_includedir}/SS_UPI.h %{_includedir}/ua_types.h - -%files -n tota-bsdiff -%{_bindir}/ss_bsdiff -%{_bindir}/ss_bspatch -- 2.7.4 From 9796fcd3921bc655ee95ca0cb22ba333b3395832 Mon Sep 17 00:00:00 2001 From: Sunmin Lee Date: Thu, 1 Feb 2018 15:55:12 +0900 Subject: [PATCH 14/16] Eliminate libsmack dependency For independence of libtota, remove libsmack dependency. Change-Id: Ibf909e32373a8e7814e4196bfff9cffa5055eab4 Signed-off-by: Sunmin Lee --- CMakeLists.txt | 1 - packaging/libtota.spec | 1 - ss_engine/SS_FSUpdate.c | 102 +++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 84 insertions(+), 20 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8df6b0f..e403e62 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,7 +41,6 @@ INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR}/bsdiff) INCLUDE(FindPkgConfig) pkg_check_modules(packages REQUIRED - libsmack lib7zip ) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index 9dccfbd..48c629a 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -7,7 +7,6 @@ License: Apache-2.0 and BSD-2-Clause and BSD-3-Clause and PD Source0: %{name}-%{version}.tar.gz BuildRequires: cmake -BuildRequires: pkgconfig(libsmack) BuildRequires: pkgconfig(lib7zip) %description diff --git a/ss_engine/SS_FSUpdate.c b/ss_engine/SS_FSUpdate.c index 0e004c8..577e698 100755 --- a/ss_engine/SS_FSUpdate.c +++ b/ss_engine/SS_FSUpdate.c @@ -28,7 +28,6 @@ #include #include #include -#include #include #include @@ -40,6 +39,17 @@ static int feature_support_capability; +enum smack_label_type { + SMACK_LABEL_ACCESS, + SMACK_LABEL_EXEC, + SMACK_LABEL_MMAP, + SMACK_LABEL_TRANSMUTE, + SMACK_LABEL_IPIN, + SMACK_LABEL_IPOUT, +}; + +#define SMACK_LABEL_LEN 255 + /************************************************************ * common functions ************************************************************/ @@ -742,6 +752,62 @@ void SS_chtoa(int value, char *str) } } +static inline char *SS_get_xattr_name(enum smack_label_type type) +{ + switch (type) { + case SMACK_LABEL_ACCESS: + return "security.SMACK64"; + case SMACK_LABEL_EXEC: + return "security.SMACK64EXEC"; + case SMACK_LABEL_MMAP: + return "security.SMACK64MMAP"; + case SMACK_LABEL_TRANSMUTE: + return "security.SMACK64TRANSMUTE"; + case SMACK_LABEL_IPIN: + return "security.SMACK64IPIN"; + case SMACK_LABEL_IPOUT: + return "security.SMACK64IPOUT"; + default: + /* Should not reach this point */ + return NULL; + } +} + +int SS_smack_lsetlabel(const char *path, const char *label, enum smack_label_type type) +{ + if (path == NULL) { + LOGE("Path is NULL\n"); + return -1; + } + + char *xattr_name = SS_get_xattr_name(type); + if (xattr_name == NULL) { + LOGE("Failed get xattr name\n"); + return -1; + } + + /* Check validity of labels for LABEL_TRANSMUTE */ + if (type == SMACK_LABEL_TRANSMUTE && label != NULL) { + if (!strncmp(label, "0", strlen("0"))) { + label = NULL; + } else if (!strncmp(label, "1", strlen("0"))) { + label = "TRUE"; + } else { + return -1; + } + } + + if (label == NULL || label[0] == '\0') { + return lremovexattr(path, xattr_name); + } else { + int len = strnlen(label, SMACK_LABEL_LEN + 1); + if (len > SMACK_LABEL_LEN) { + return -1; + } + return lsetxattr(path, xattr_name, label, len, 0); + } +} + /*! ******************************************************************************* * Set file attributes.

@@ -938,71 +1004,71 @@ long SS_SetFileAttributes(const char *ui16pFilePath, // Get Smack value -> Set Smack value if (*smack_attr_pos != '\0') { - smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_ACCESS); - smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_EXEC); - smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_MMAP); - smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_TRANSMUTE); + SS_smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_ACCESS); + SS_smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_EXEC); + SS_smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_MMAP); + SS_smack_lsetlabel(setFilePath, NULL, SMACK_LABEL_TRANSMUTE); psmack = strstr(smack_attr_pos, "access=\""); if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); - memcpy(tmpSmackAttribs, psmack, strlen(psmack)); + memcpy(tmpSmackAttribs, psmack, sizeof(tmpSmackAttribs) - 1); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_ACCESS] smack_value=%s\n", smack_value); if (smack_value) { - ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_ACCESS); + ret = SS_smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_ACCESS); if (ret < 0) - LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); + LOGL(LOG_SSENGINE, "SS_smack_lsetlabel() failed\n"); } } } psmack = strstr(smack_attr_pos, "execute=\""); if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); - memcpy(tmpSmackAttribs, psmack, strlen(psmack)); + memcpy(tmpSmackAttribs, psmack, sizeof(tmpSmackAttribs) - 1); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_EXEC] smack_value=%s\n", smack_value); if (smack_value) { - ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_EXEC); + ret = SS_smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_EXEC); if (ret < 0) - LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); + LOGL(LOG_SSENGINE, "SS_smack_lsetlabel() failed\n"); } } } psmack = strstr(smack_attr_pos, "mmap=\""); if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); - memcpy(tmpSmackAttribs, psmack, strlen(psmack)); + memcpy(tmpSmackAttribs, psmack, sizeof(tmpSmackAttribs) - 1); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_MMAP] smack_value=%s\n", smack_value); if (smack_value) { - ret = smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_MMAP); + ret = SS_smack_lsetlabel(setFilePath, smack_value, SMACK_LABEL_MMAP); if (ret < 0) - LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); + LOGL(LOG_SSENGINE, "SS_smack_lsetlabel() failed\n"); } } } psmack = strstr(smack_attr_pos, "transmute=\""); if (psmack) { memset(tmpSmackAttribs, 0x0, sizeof(tmpSmackAttribs)); - memcpy(tmpSmackAttribs, psmack, strlen(psmack)); + memcpy(tmpSmackAttribs, psmack, sizeof(tmpSmackAttribs) - 1); smack_value = strtok_r(tmpSmackAttribs, "\"", &saveptr); if (smack_value) { smack_value = strtok_r(NULL, "\"", &saveptr); //LOGL(LOG_SSENGINE, "[SMACK_LABEL_TRANSMUTE] smack_value=%s\n", smack_value); if (smack_value) { if (strcasecmp(smack_value, "TRUE") == 0) - ret = smack_lsetlabel(setFilePath, "1", SMACK_LABEL_TRANSMUTE); + ret = SS_smack_lsetlabel(setFilePath, "1", SMACK_LABEL_TRANSMUTE); else - ret = smack_lsetlabel(setFilePath, "0", SMACK_LABEL_TRANSMUTE); + ret = SS_smack_lsetlabel(setFilePath, "0", SMACK_LABEL_TRANSMUTE); if (ret < 0) - LOGL(LOG_SSENGINE, "smack_lsetlabel() failed\n"); + LOGL(LOG_SSENGINE, "SS_smack_lsetlabel() failed\n"); } } } -- 2.7.4 From 4db0f70846f8345c1b28bf95a123f4d0b4ff9a51 Mon Sep 17 00:00:00 2001 From: sanghyuk Ko Date: Mon, 4 Dec 2017 21:53:57 +0900 Subject: [PATCH 15/16] Tried to update the applied file when second update Files that are applied the patch during the update are skipped during the second udpate. Files that are deleted during the update are skipped during the second udpate. Change-Id: Id7366ce042d361dad4913c0a31aa22fe1b440778 --- packaging/libtota.spec | 5 ++ ss_engine/SS_PatchDelta.c | 52 +++++++++--- ss_engine/SS_UPI.c | 201 +++++++++++++++++++++++++++++++++++++++++++--- ss_engine/SS_UPI.h | 6 +- ss_engine/fota_common.h | 2 + 5 files changed, 239 insertions(+), 27 deletions(-) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index 48c629a..e6218ef 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -42,6 +42,11 @@ cd cmake_tmp cp libtota.a %{buildroot}%{_libdir}/libtota.a %post +%posttrans +if [ ! -e /opt/data/recovery ]; then + ln -sf /opt/data/update /opt/data/recovery +fi + %files %defattr(-,root,root,-) diff --git a/ss_engine/SS_PatchDelta.c b/ss_engine/SS_PatchDelta.c index d0f5eab..cc55fea 100755 --- a/ss_engine/SS_PatchDelta.c +++ b/ss_engine/SS_PatchDelta.c @@ -236,7 +236,8 @@ extern void create_dir(char *pathname, int mode); int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, const char *source_sha1_str, const char *target_sha1_str, int patch_data_size) { - uint8_t target_sha1[SHA_DIGEST_SIZE]; + uint8_t target_sha1[SHA_DIGEST_SIZE] = { 0, }; + uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; sha1_ctx_t ctx1; int output; int retry = 1; @@ -246,7 +247,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, int result = 0; char buf[256]; FileInfo source_file; - uint8_t source_sha1[SHA_DIGEST_SIZE] = { 0, }; + FileInfo target_file; if (ParseSha1(target_sha1_str, target_sha1) != 0) { LOGE("failed to parse tgt-sha1 \"%s\"\n", target_sha1_str); @@ -264,11 +265,11 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, } if (SS_LoadFile(source_filename, &source_file) == 0) { if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied\n"); + LOGL(LOG_SSENGINE, "Patch Can be applied\n"); if (source_file.data) SS_Free(source_file.data); } else if (memcmp(source_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Already applied\n"); + LOGL(LOG_SSENGINE, "Patch Already applied\n"); if (source_file.data) SS_Free(source_file.data); return S_SS_SUCCESS; @@ -276,7 +277,17 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, //Check for backup file SHA SS_Free(source_file.data); source_file.data = NULL; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was currupted, Try loading from backup source\n"); + LOGL(LOG_SSENGINE, "Source was currupted, Try loading from backup source\n"); + + if (SS_LoadFile(target_filename, &target_file) == 0) { + if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "Patch Already applied\n"); + if (target_file.data) + SS_Free(target_file.data); + return S_SS_SUCCESS; + } + } + if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { if (SS_CopyFile(SS_BACKUP_SOURCE, source_filename) != S_SS_SUCCESS) { @@ -288,7 +299,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, return E_SS_FAILURE; } LOGL(LOG_SSENGINE, - "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + "Patch Can be applied from using backup file as source\n"); } else { SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); if (source_file.data) @@ -303,11 +314,21 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, } } } else { - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Source was deleted, Try loading from backup source\n"); + LOGL(LOG_SSENGINE, "Source was deleted!!\n"); + if (SS_LoadFile(target_filename, &target_file) == 0) { + if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "Patch Already applied\n"); + if (target_file.data) + SS_Free(target_file.data); + return S_SS_SUCCESS; + } + } + + LOGL(LOG_SSENGINE, "Try loading from backup source\n"); if (SS_LoadFile(SS_BACKUP_SOURCE, &source_file) == 0) { if (memcmp(source_file.sha1, source_sha1, SHA_DIGEST_SIZE) == 0) { use_backup = 1; - LOGL(LOG_SSENGINE, "SS_UpdateDeltaFS - Patch Can be applied from using backup file as source\n"); + LOGL(LOG_SSENGINE, "Patch Can be applied from using backup file as source\n"); } else { SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); if (source_file.data) @@ -315,6 +336,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, return E_SS_FAILURE; } } else { + LOGE(" SS_LoadFile from backup source failed!!\n"); SS_SetUpgradeState(E_SS_FSSRCCURRUPTED); if (source_file.data) SS_Free(source_file.data); @@ -370,10 +392,12 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, // We write the decoded output to ".patch". //allocate some extra space to allow for concatinating ".patch" with the name outname = (char *)SS_Malloc(strlen(target_filename) + 10); - if (outname == NULL) - return -1; - snprintf(outname, strlen(target_filename) + 10, - "%s.patch", target_filename); + if (outname == NULL) { + LOGE("SS_Malloc failed!!\n"); + return E_SS_FAILURE; + } + snprintf(outname, strlen(target_filename) + 10, + "%s.patch", target_filename); output = open(outname, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU); if (output < 0) { @@ -406,7 +430,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, close(output); } - if (result != 0) { + if (result != S_SS_SUCCESS) { if (retry == 0) { LOGE("applying patch failed result : [%d]\n", result); SS_Free(outname);//wgid: 20739 @@ -421,6 +445,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, break; } } while (retry-- > 0); + const uint8_t current_target_sha1[SHA_DIGEST_SIZE] = { 0, }; sha1_final(&ctx1, (uint32_t *) & current_target_sha1); if (memcmp(current_target_sha1, target_sha1, SHA_DIGEST_SIZE) != 0) { @@ -430,6 +455,7 @@ int SS_UpdateDeltaFS(const char *source_filename, const char *target_filename, SS_Free(outname); return E_SS_FAILURE; } + // Finally, rename the .patch file to replace the target file. #ifdef ENHANCED_BSDIFF if (SS_rename1(outname, target_filename) != 0) { diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index 290ba58..9eca8a8 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -318,6 +318,127 @@ int SS_GetUpgradeState() return FS_UpgradeState; } +int SS_Get_last_update_del_type(void) +{ + int fd; + unsigned char buf[256]; + char *ptr = NULL; + int status = -1; + int del_type = 0; + int result = 0; + + if (file_exist(SS_UPDATE_STATUR_PATH) == 0) { + LOG("No exist file!!\n"); + return 0; + } + + fd = open(SS_UPDATE_STATUR_PATH, O_RDWR, S_IRWXU); + if (fd == -1) { + LOG("Could not open status file!!\n"); + return 0; + } + + result = SS_ReadFile(fd, 0, buf, sizeof(buf)); + if (result != 0) { + LOG("SS_ReadFile failed!!\n"); + return 0; + } + + ptr = strtok((char *)buf, " "); + + if (ptr != NULL) { + status = atoi(ptr); + ptr = strtok(NULL, " "); + } + + if (ptr != NULL) { + del_type = atoi(ptr); + } + + result = SS_CloseFile(fd); + if (result != 0) + LOG("SS_CloseFile failed!!\n"); + + LOG("del_type:[%d] (status: [%d])\n", del_type, status); + + return del_type; +} + +int SS_Get_last_update_status(void) +{ + int fd; + unsigned char buf[256]; + char *ptr = NULL; + int status = -1; + int del_type = 0; + int result = 0; + + if (file_exist(SS_UPDATE_STATUR_PATH) == 0) { + LOG("No exist file!!\n"); + return 0; + } + + fd = open(SS_UPDATE_STATUR_PATH, O_RDWR, S_IRWXU); + if (fd == -1) { + LOG("Could not open status file!!\n"); + return 0; + } + + result = SS_ReadFile(fd, 0, buf, sizeof(buf)); + if (result != 0) { + LOG("SS_ReadFile failed!!\n"); + return 0; + } + + LOG("buf:[%s]\n", buf); + + ptr = strtok((char *)buf, " "); + + if (ptr != NULL) { + status = atoi(ptr); + ptr = strtok(NULL, " "); + } + + if (ptr != NULL) { + del_type = atoi(ptr); + } + + result = SS_CloseFile(fd); + if (result != 0) + LOG("SS_CloseFile failed!!\n"); + + LOG("status:[%d] (del_type: [%d])\n", status, del_type); + + return status; +} + + +void SS_Set_last_update_status(int status, int del_type) +{ + int fd; + int result = 0; + char num_str[16]; + LOG("status:[%d], del_type:[%d]\n", status, del_type); + + fd = open(SS_UPDATE_STATUR_PATH, O_RDWR | O_CREAT, S_IRWXU); + if (fd == -1) { + LOG("Could not open status file!!\n"); + return; + } + + sprintf(num_str, "%d %d", status, del_type); + result = SS_WriteFile(fd, 0, (unsigned char *)num_str, strlen(num_str)); + if (result != 0) + LOG("SS_WriteFile failed!!\n"); + + result = SS_CloseFile(fd); + if (result != 0) + LOG("SS_CloseFile failed!!\n"); + + sync(); + +} + int SS_rename(const char *old_file_name, const char *new_file_name) { if (link(old_file_name, new_file_name) < 0) { @@ -1116,6 +1237,7 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) char *pattribs = NULL; int ulAttribSize = 0; int result = S_SS_SUCCESS; + int fail_cnt = 0; if (!(ua_dataSS && ua_dataSS->update_delta && ua_dataSS->update_data->ua_delta_path)) { LOGE("Bad params for SS_FSSetAttributes\n"); @@ -1132,6 +1254,7 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) } char *item_data = SS_Malloc(item_size + 1); if (item_data == NULL) { + LOGE("SS_Malloc failed!! - item_data\n"); SS_SetUpgradeState(E_SS_MALLOC_ERROR); return E_SS_FAILURE; } @@ -1139,6 +1262,7 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) tar_get_cfg_data(ua_dataSS->update_data->ua_delta_path, ua_dataSS->update_delta->ua_attrib_path, item_data, item_size); if (read_data <= 0) { + LOGE("read_data failed!!\n"); SS_SetUpgradeState(E_SS_FSBADDELTA); if (item_data != NULL) SS_Free(item_data); @@ -1177,20 +1301,26 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); if (item_data) SS_Free(item_data); - return E_SS_FAILURE; + fail_cnt++; } } else { LOGE("\n Failed to Parse Attributes - LINE %s", pline); SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); if (item_data) SS_Free(item_data); - return E_SS_FAILURE; + fail_cnt++; } pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); } + SS_Free(item_data); - return S_SS_SUCCESS; + if (fail_cnt > 0) + result = E_SS_FAILURE; + else + result = S_SS_SUCCESS; + + return result; } /*! @@ -1342,7 +1472,7 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f #endif while (pFsNode) { if (pFsNode->type == DELETE_END) { - LOGL(LOG_SSENGINE, "DELETES update Index: [%d] \n", ulFileIndex++); + LOGL(LOG_SSENGINE, "DELETE_END update Index: [%d] \n", ulFileIndex++); SS_GetFileType(pFsNode->file_old_path, (enumFileType *) & ulFiletype); if (ulFiletype == 2) //FT_FOLDER ulResult = SS_DeleteFolder(pFsNode->file_old_path); @@ -1359,7 +1489,7 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f #ifdef TIME_PROFILING get_time_stamp1(); //total time capturing t2 = atoi(ts1); - LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); + LOG("Shirsh time for DELETE_END - [%d] \n", (t2 - t1)); #endif } break; @@ -1490,6 +1620,7 @@ extern int cur_mem; int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) { int ulResult = S_SS_SUCCESS; + int del_type = 0; fs_list *head_ptr_node = NULL; char new_patch_path[SS_MAX_FILE_PATH] = { 0 @@ -1511,12 +1642,22 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) LOGL(LOG_SSENGINE, "FS Update Entry PartIndex: [%d]\n", part_idx); + del_type = SS_Get_last_update_del_type(); + LOGL(LOG_SSENGINE, "del_type: [%d]\n", del_type); + if (head_ptr_node->del_ref == NULL) { LOGL(LOG_SSENGINE, "No DEL header\n"); } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(DELETES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, - ua_dataSS->update_delta->ua_patch_path); + if (del_type < DELETES) { + ulResult = SS_FSUpdateFile(DELETES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, + ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETES success!!\n"); + SS_Set_last_update_status(part_idx, DELETES); + } + } else { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETES already applied!!\n"); + } } if (head_ptr_node->dif_ref == NULL) { @@ -1525,22 +1666,40 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ulResult = SS_FSUpdateFile(DIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->dif_ref, ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DIFFS success!!\n"); + } } if (head_ptr_node->move_ref == NULL) { LOGL(LOG_SSENGINE, "No MOVE header\n"); } else if (ulResult == S_SS_SUCCESS) { + if (del_type < MOVES) { ulResult = SS_FSUpdateFile(MOVES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->move_ref, ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - MOVES success!!\n"); + SS_Set_last_update_status(part_idx, MOVES); + } + } else { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - MOVES already applied!!\n"); + } } if (head_ptr_node->del_ref == NULL) { LOGL(LOG_SSENGINE, "No DEL header\n"); } else if (ulResult == S_SS_SUCCESS) { - ulResult = - SS_FSUpdateFile(DELETE_END, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, - ua_dataSS->update_delta->ua_patch_path); + if (del_type < DELETE_END) { + ulResult = SS_FSUpdateFile(DELETE_END, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->del_ref, + ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETE_END success!!\n"); + SS_Set_last_update_status(part_idx, DELETE_END); + } + } else { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETE_END already applied!!\n"); + } } if (ulResult == S_SS_SUCCESS) { @@ -1550,6 +1709,9 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ulResult = SS_FSUpdateFile(NEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->new_ref, new_patch_path); //new file extraction end + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - NEWFILES success!!\n"); + } } if (head_ptr_node->sym_difref == NULL) { @@ -1558,6 +1720,9 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ulResult = SS_FSUpdateFile(SYMDIFFS, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_difref, ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMDIFFS success!!\n"); + } } if (head_ptr_node->sym_newref == NULL) { @@ -1566,16 +1731,26 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ulResult = SS_FSUpdateFile(SYMNEWFILES, ua_dataSS, head_ptr_node->ulPatchCount, head_ptr_node->sym_newref, ua_dataSS->update_delta->ua_patch_path); + if (ulResult == S_SS_SUCCESS) { + LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMNEWFILES success!!\n"); + } } - if (ulResult == S_SS_SUCCESS) + + if (ulResult == S_SS_SUCCESS) { ulResult = SS_FSSetAttributes(ua_dataSS); + } else { + SS_FSSetAttributes(ua_dataSS); // To prevent boot failures by smack. + } + sync(); sleep(1); SS_FSClearNodes(part_idx); - if (ulResult == S_SS_SUCCESS) + if (ulResult == S_SS_SUCCESS) { SS_UpdateUIProgress(ua_dataSS, 0, 1); //fix WGID : 51963, When all updates are done to FS , patchcount is not needed, passing 1 to 3rd arg is enough + SS_Set_last_update_status(part_idx, DEL_TYPE_MAX); + } LOGL(LOG_SSENGINE, "FS update Complete PartIndex: [%d]\n", part_idx); #ifdef MEM_PROFILING diff --git a/ss_engine/SS_UPI.h b/ss_engine/SS_UPI.h index ce91f70..4c9edc8 100755 --- a/ss_engine/SS_UPI.h +++ b/ss_engine/SS_UPI.h @@ -30,7 +30,7 @@ struct details { int symnews; }; -enum DEL_TYPE { DIFFS, MOVES, NEWFILES, DELETES, SYMDIFFS, SYMNEWFILES, DELETE_END }; +enum DEL_TYPE { DELETES = 1, DIFFS, MOVES, DELETE_END, NEWFILES, SYMDIFFS, SYMNEWFILES, DEL_TYPE_MAX }; struct fs_params { // Use Macros char file_old_path[512]; char file_new_path[512]; @@ -83,4 +83,8 @@ extern int _7zdecompress(char *path); extern void tar_free_cfg_table(tar_Data_t ** delta_tar); extern long SS_GetFileType(char *pLinkName, enumFileType * fileType); +extern int SS_Get_last_update_del_type(void); +extern int SS_Get_last_update_status(void); +extern void SS_Set_last_update_status(int status, int del_type); + #endif //_SS_UPI_H_ diff --git a/ss_engine/fota_common.h b/ss_engine/fota_common.h index 0d2720d..6b538eb 100755 --- a/ss_engine/fota_common.h +++ b/ss_engine/fota_common.h @@ -72,6 +72,8 @@ typedef unsigned long long u64; #define SS_GZIP_TARGET SS_KERNEL_WORKSPACE "/gzip" #define SS_STAT_TARGET SS_KERNEL_WORKSPACE "/stat" #define SS_DD_TARGET SS_KERNEL_WORKSPACE "/dd" +//#define SS_UPDATE_STATUR_PATH SS_COMMON_WORKSPACE "/UP.STATUS" +#define SS_UPDATE_STATUR_PATH "/system/opt/data/recovery/UP.STATUS" #define SS_GZIP_SOURCE "system/bin/gzip" #define SS_STAT_SOURCE "system/usr/bin/stat" -- 2.7.4 From 841433013f0d6d99b1c4b5bbc208f39eaf8e771b Mon Sep 17 00:00:00 2001 From: sanghyuk Ko Date: Thu, 7 Dec 2017 14:56:19 +0900 Subject: [PATCH 16/16] In case of MOVES, added code to check validation. In addition, Remove duplicated code. Change-Id: I5162a65001dce5fbef899b5021367edb358cca84 --- packaging/libtota.spec | 2 +- ss_engine/SS_UPI.c | 336 ++++++++++++++++++++++++++++++++++++------------ ss_engine/SS_UPI.h | 5 +- ss_engine/fota_common.h | 7 + 4 files changed, 261 insertions(+), 89 deletions(-) diff --git a/packaging/libtota.spec b/packaging/libtota.spec index e6218ef..3a8408c 100755 --- a/packaging/libtota.spec +++ b/packaging/libtota.spec @@ -1,6 +1,6 @@ Name: libtota Summary: fota update library -Version: 1.2.0 +Version: 1.2.1 Release: 0 Group: System License: Apache-2.0 and BSD-2-Clause and BSD-3-Clause and PD diff --git a/ss_engine/SS_UPI.c b/ss_engine/SS_UPI.c index 9eca8a8..82072b7 100755 --- a/ss_engine/SS_UPI.c +++ b/ss_engine/SS_UPI.c @@ -135,6 +135,88 @@ int file_exist(char *filename) return (ret >= 0) ? (1) : (0); } +#ifdef POWER_FAIL_TEST + +static int SS_Get_power_fail_flag(void) +{ + int fd; + char buf[256]; + char *ptr = NULL; + int result = 0; + int ret_val = 0; + + if (file_exist(SS_POWER_FAIL_TEST_FLAG) == 0) { + LOGE("No exist file!!\n"); + return -1; + } + + fd = open(SS_POWER_FAIL_TEST_FLAG, O_RDWR, S_IRWXU); + if (fd == -1) { + LOGE("Could not open status file!!\n"); + return -1; + } + + result = SS_ReadFile(fd, 0, buf, sizeof(buf)); + if (result != 0) { + LOGE("SS_ReadFile failed!!\n"); + return -1; + } + + ret_val = atoi(buf); + + result = SS_CloseFile(fd); + if (result != 0) + LOGE("SS_CloseFile failed!!\n"); + + return ret_val; +} + +static void SS_Set_power_fail_flag(int del_type) +{ + int fd; + int result = 0; + char num_str[16]; + LOG("del_type:[%d]\n", del_type); + + fd = open(SS_POWER_FAIL_TEST_FLAG, O_RDWR | O_CREAT, S_IRWXU); + if (fd == -1) { + LOGE("Could not open status file!!\n"); + return; + } + + sprintf(num_str, "%d", del_type); + result = SS_WriteFile(fd, 0, num_str, strlen(num_str)); + if (result != 0) + LOGE("SS_WriteFile failed!!\n"); + + result = SS_CloseFile(fd); + if (result != 0) + LOGE("SS_CloseFile failed!!\n"); + + sync(); + +} + +int SS_Do_Power_fail_test(int del_type) +{ + int ret = -1; + char cmd[1024] = { 0, }; + + SS_Set_power_fail_flag(del_type); + + snprintf(cmd, sizeof(cmd) - 1, "%s", "/usr/sbin/reboot fota"); + ret = _system_cmd_nowait(cmd); + sleep(1); + LOG("ret for SS_Do_Power_fail_test cmd is %d\n", ret); + if (ret == 0) { + return S_SS_SUCCESS; + } else { + LOGE("Could not start Memory Profiling\n"); + return E_SS_FAILURE; + } +} +#endif + long SS_GetUPIVersion(unsigned char *ver_str) { if (ver_str) { @@ -318,122 +400,72 @@ int SS_GetUpgradeState() return FS_UpgradeState; } -int SS_Get_last_update_del_type(void) -{ - int fd; - unsigned char buf[256]; - char *ptr = NULL; - int status = -1; - int del_type = 0; - int result = 0; - - if (file_exist(SS_UPDATE_STATUR_PATH) == 0) { - LOG("No exist file!!\n"); - return 0; - } - - fd = open(SS_UPDATE_STATUR_PATH, O_RDWR, S_IRWXU); - if (fd == -1) { - LOG("Could not open status file!!\n"); - return 0; - } - - result = SS_ReadFile(fd, 0, buf, sizeof(buf)); - if (result != 0) { - LOG("SS_ReadFile failed!!\n"); - return 0; - } - - ptr = strtok((char *)buf, " "); - - if (ptr != NULL) { - status = atoi(ptr); - ptr = strtok(NULL, " "); - } - - if (ptr != NULL) { - del_type = atoi(ptr); - } - - result = SS_CloseFile(fd); - if (result != 0) - LOG("SS_CloseFile failed!!\n"); - - LOG("del_type:[%d] (status: [%d])\n", del_type, status); - - return del_type; -} - -int SS_Get_last_update_status(void) +int SS_Get_last_update_status(int* last_update_status, int* del_type) { int fd; unsigned char buf[256]; char *ptr = NULL; - int status = -1; - int del_type = 0; int result = 0; if (file_exist(SS_UPDATE_STATUR_PATH) == 0) { - LOG("No exist file!!\n"); - return 0; + LOG("No exist file!! - File_path:[%s]\n", SS_UPDATE_STATUR_PATH); + return -1; } fd = open(SS_UPDATE_STATUR_PATH, O_RDWR, S_IRWXU); if (fd == -1) { - LOG("Could not open status file!!\n"); - return 0; + LOGE("Could not open status file!!, File_path:[%s]\n", SS_UPDATE_STATUR_PATH); + return -1; } result = SS_ReadFile(fd, 0, buf, sizeof(buf)); if (result != 0) { - LOG("SS_ReadFile failed!!\n"); - return 0; + LOGE("SS_ReadFile failed!!\n"); + result = SS_CloseFile(fd); + if (result != 0) + LOGE("SS_CloseFile failed!!\n"); + return -1; } - LOG("buf:[%s]\n", buf); - ptr = strtok((char *)buf, " "); if (ptr != NULL) { - status = atoi(ptr); + *last_update_status = atoi(ptr); ptr = strtok(NULL, " "); } if (ptr != NULL) { - del_type = atoi(ptr); + *del_type = atoi(ptr); } result = SS_CloseFile(fd); if (result != 0) - LOG("SS_CloseFile failed!!\n"); - - LOG("status:[%d] (del_type: [%d])\n", status, del_type); + LOGE("SS_CloseFile failed!!\n"); - return status; + return 0; } - -void SS_Set_last_update_status(int status, int del_type) +void SS_Set_last_update_status(int last_update_status, int del_type) { int fd; int result = 0; char num_str[16]; - LOG("status:[%d], del_type:[%d]\n", status, del_type); + LOG("last_update_status:[%d], del_type:[%d]\n", last_update_status, del_type); fd = open(SS_UPDATE_STATUR_PATH, O_RDWR | O_CREAT, S_IRWXU); if (fd == -1) { - LOG("Could not open status file!!\n"); + LOGE("Could not open status file!!, File_path:[%s]\n", SS_UPDATE_STATUR_PATH); return; } - sprintf(num_str, "%d %d", status, del_type); + sprintf(num_str, "%d %d", last_update_status, del_type); result = SS_WriteFile(fd, 0, (unsigned char *)num_str, strlen(num_str)); if (result != 0) - LOG("SS_WriteFile failed!!\n"); + LOGE("SS_WriteFile failed!!\n"); result = SS_CloseFile(fd); if (result != 0) - LOG("SS_CloseFile failed!!\n"); + LOGE("SS_CloseFile failed!!\n"); sync(); @@ -1264,15 +1296,19 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) if (read_data <= 0) { LOGE("read_data failed!!\n"); SS_SetUpgradeState(E_SS_FSBADDELTA); - if (item_data != NULL) + if (item_data != NULL) { SS_Free(item_data); + item_data = NULL; + } return E_SS_FAILURE; } pline = strtok_r(item_data, "\n", &psaveptr); if (pline == NULL) { LOGL(LOG_SSENGINE, "No Attributes to SET as no lines in file\n"); - if (item_data != NULL) + if (item_data != NULL) { SS_Free(item_data); + item_data = NULL; + } return E_SS_FAILURE; } @@ -1299,21 +1335,28 @@ int SS_FSSetAttributes(ua_dataSS_t * ua_dataSS) if (result != S_SS_SUCCESS) { LOGE("\n Failed to set Attributes %s", pfilePath); SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); - if (item_data) + if (item_data) { SS_Free(item_data); + item_data = NULL; + } fail_cnt++; } } else { LOGE("\n Failed to Parse Attributes - LINE %s", pline); SS_SetUpgradeState(E_SS_FSBADATTRIBUTES); - if (item_data) + if (item_data) { SS_Free(item_data); + item_data = NULL; + } fail_cnt++; } pline = strtok_r(NULL, SS_TOKEN_NEWLINE, &psaveptr); } - SS_Free(item_data); + if (item_data != NULL) { + SS_Free(item_data); + item_data = NULL; + } if (fail_cnt > 0) result = E_SS_FAILURE; @@ -1348,6 +1391,9 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f }; int ulReadCnt = 0; int ulResult = S_SS_SUCCESS; + FileInfo source_file; + FileInfo target_file; + uint8_t target_sha1[SHA_DIGEST_SIZE] = { 0, }; if (!patch_path) { LOGE("Bad patch_path name\n"); @@ -1416,19 +1462,80 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f #endif while (pFsNode) { LOGL(LOG_SSENGINE, "MOVES update Index: [%d] \n", ulFileIndex++); - ulResult = SS_MoveFile(pFsNode->file_old_path, pFsNode->file_new_path); - if (ulResult != S_SS_SUCCESS) { - LOGE("Move Failed for [%s] to [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); - SS_SetUpgradeState(ulResult); + int skip_flag = 0; + if (SS_LoadFile(pFsNode->file_old_path, &source_file) == 0) { + LOGL(LOG_SSENGINE, "Patch Can be applied\n"); + if (source_file.data) + SS_Free(source_file.data); + } else if (SS_LoadFile(pFsNode->file_new_path, &target_file) == 0) { + LOGL(LOG_SSENGINE, "source deleted!!, file_old_path: [%s]\n", pFsNode->file_old_path); + if (ParseSha1(pFsNode->sha1src, target_sha1) != 0) { + LOGE("failed to parse sha1 \"%s\"\n", pFsNode->sha1src); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (target_file.data) + SS_Free(target_file.data); + break; + } + if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "Patch already applied\n"); + skip_flag = 1; + if (target_file.data) + SS_Free(target_file.data); + } else { + LOGL(LOG_SSENGINE, "target_sha1 diff!!: [%s]\n", target_sha1); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (target_file.data) + SS_Free(target_file.data); + break; + } + } else { + LOGE("No exist files. - file_old_path: [%s] file_new_path: [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); break; } + + if (skip_flag == 0) { + ulResult = SS_MoveFile(pFsNode->file_old_path, pFsNode->file_new_path); + if (ulResult != S_SS_SUCCESS) { + LOGE("Move Failed for [%s] to [%s]\n", pFsNode->file_old_path, pFsNode->file_new_path); + SS_SetUpgradeState(ulResult); + break; + } else { + // Verification + if (SS_LoadFile(pFsNode->file_new_path, &target_file) == 0) { + if (ParseSha1(pFsNode->sha1src, target_sha1) != 0) { + LOGE("failed to parse sha1 \"%s\"\n", pFsNode->sha1src); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (target_file.data) + SS_Free(target_file.data); + break; + } + if (memcmp(target_file.sha1, target_sha1, SHA_DIGEST_SIZE) == 0) { + LOGL(LOG_SSENGINE, "Patch success!!\n"); + if (target_file.data) + SS_Free(target_file.data); + } else { + LOGL(LOG_SSENGINE, "target_sha1 diff!!: [%s]\n", target_sha1); + ulResult = E_SS_FAILURE; + SS_SetUpgradeState(E_SS_FSUPDATEFAILED); + if (target_file.data) + SS_Free(target_file.data); + break; + } + } + } + } SS_UpdateUIProgress(ua_dataSS, ulPatchCount, 0); pFsNode = pFsNode->nextnode; } #ifdef TIME_PROFILING get_time_stamp1(); //total time capturing t2 = atoi(ts1); - LOG("Shirsh time for DELETES - [%d] \n", (t2 - t1)); + LOG("Shirsh time for MOVES - [%d] \n", (t2 - t1)); #endif } break; @@ -1527,10 +1634,14 @@ int SS_FSUpdateFile(int ubFileType, ua_dataSS_t * ua_dataSS, int ulPatchCount, f LOGL(LOG_SSENGINE, "SYMDIFFS update Index: [%d] \n", ulFileIndex++); //LOG("Sym Diff file paths: [Linkname - %s] [reference file name- %s][]\n", pFsNode->file_path,pFsNode->patch_name); ulResult = SS_Unlink(pFsNode->file_old_path); - if (ulResult == S_SS_SUCCESS) + if (ulResult == S_SS_SUCCESS) { ulResult = SS_Link(NULL, pFsNode->file_new_path, pFsNode->patch_name); - else { - LOGE("Unlink Failed\n"); + if (ulResult != S_SS_SUCCESS) { + LOGE("SS_Link Failed, Linkname:[%s], reference file name:[%s]\n", + pFsNode->file_new_path, pFsNode->patch_name); + } + } else { + LOGE("SS_Unlink Failed\n"); SS_SetUpgradeState(E_SS_FSUPDATEFAILED); break; } @@ -1620,6 +1731,7 @@ extern int cur_mem; int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) { int ulResult = S_SS_SUCCESS; + int last_update_status = 0; int del_type = 0; fs_list *head_ptr_node = NULL; char new_patch_path[SS_MAX_FILE_PATH] = { @@ -1642,8 +1754,16 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) LOGL(LOG_SSENGINE, "FS Update Entry PartIndex: [%d]\n", part_idx); - del_type = SS_Get_last_update_del_type(); - LOGL(LOG_SSENGINE, "del_type: [%d]\n", del_type); +#ifdef POWER_FAIL_TEST + int fail_test_flag = 0; + fail_test_flag = SS_Get_power_fail_flag(); + LOGL(LOG_SSENGINE, "fail_test_flag: [%d]\n", fail_test_flag); +#endif + + if(SS_Get_last_update_status(&last_update_status, &del_type) == -1) + LOGE("SS_Get_last_update_status failed!!\n"); + + LOGL(LOG_SSENGINE, "last_update_status: [%d], del_type: [%d]\n", last_update_status, del_type); if (head_ptr_node->del_ref == NULL) { LOGL(LOG_SSENGINE, "No DEL header\n"); @@ -1653,6 +1773,14 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETES success!!\n"); + +#ifdef POWER_FAIL_TEST + if (fail_test_flag < DELETES) { + if (SS_Do_Power_fail_test(DELETES) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif SS_Set_last_update_status(part_idx, DELETES); } } else { @@ -1668,6 +1796,13 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DIFFS success!!\n"); +#ifdef POWER_FAIL_TEST + if (fail_test_flag < DIFFS) { + if (SS_Do_Power_fail_test(DIFFS) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif } } @@ -1680,6 +1815,13 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - MOVES success!!\n"); +#ifdef POWER_FAIL_TEST + if (fail_test_flag < MOVES) { + if (SS_Do_Power_fail_test(MOVES) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif SS_Set_last_update_status(part_idx, MOVES); } } else { @@ -1695,6 +1837,13 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - DELETE_END success!!\n"); +#ifdef POWER_FAIL_TEST + if (fail_test_flag < DELETE_END) { + if (SS_Do_Power_fail_test(DELETE_END) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif SS_Set_last_update_status(part_idx, DELETE_END); } } else { @@ -1722,6 +1871,13 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMDIFFS success!!\n"); +#ifdef POWER_FAIL_TEST + if (fail_test_flag < SYMDIFFS) { + if (SS_Do_Power_fail_test(SYMDIFFS) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif } } @@ -1733,6 +1889,13 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) ua_dataSS->update_delta->ua_patch_path); if (ulResult == S_SS_SUCCESS) { LOGL(LOG_SSENGINE, "SS_FSUpdateFile - SYMNEWFILES success!!\n"); +#ifdef POWER_FAIL_TEST + if (fail_test_flag < SYMNEWFILES) { + if (SS_Do_Power_fail_test(SYMNEWFILES) == E_SS_FAILURE) { + LOGE("SS_Do_Power_fail_test failed!!\n"); + } + } +#endif } } @@ -1756,6 +1919,9 @@ int SS_FSUpdatemain(ua_dataSS_t * ua_dataSS, int part_idx) #ifdef MEM_PROFILING LOGL(LOG_SSENGINE, "Stats are : Cur Max : [%d] Global Max : [%d]\n", cur_mem, max_mem); #endif +#ifdef POWER_FAIL_TEST + unlink(SS_POWER_FAIL_TEST_FLAG); +#endif if (ulResult == S_SS_SUCCESS) return ulResult; else diff --git a/ss_engine/SS_UPI.h b/ss_engine/SS_UPI.h index 4c9edc8..d82c8e6 100755 --- a/ss_engine/SS_UPI.h +++ b/ss_engine/SS_UPI.h @@ -83,8 +83,7 @@ extern int _7zdecompress(char *path); extern void tar_free_cfg_table(tar_Data_t ** delta_tar); extern long SS_GetFileType(char *pLinkName, enumFileType * fileType); -extern int SS_Get_last_update_del_type(void); -extern int SS_Get_last_update_status(void); -extern void SS_Set_last_update_status(int status, int del_type); +extern int SS_Get_last_update_status(int* last_update_status, int* del_type); +extern void SS_Set_last_update_status(int last_update_status, int del_type); #endif //_SS_UPI_H_ diff --git a/ss_engine/fota_common.h b/ss_engine/fota_common.h index 6b538eb..8213546 100755 --- a/ss_engine/fota_common.h +++ b/ss_engine/fota_common.h @@ -52,6 +52,9 @@ typedef unsigned long long u64; #ifndef MEM_PROFILING //#define MEM_PROFILING #endif +#ifndef POWER_FAIL_TEST + //#define POWER_FAIL_TEST +#endif #define UNUSED(x) (void)(x) @@ -118,6 +121,10 @@ typedef unsigned long long u64; #define SS_MEMORY_PROFILING_SCRIPT SS_COMMON_WORKSPACE "/mem_use.sh" #endif +#ifdef POWER_FAIL_TEST +#define SS_POWER_FAIL_TEST_FLAG SS_COMMON_WORKSPACE "/power_fail_test_flag" +#endif + struct tar_Data { int itemSize; int itemOffset; -- 2.7.4