1 /* 2 * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 * @brief FOTA patch application library 15 */ 16 17 #ifndef UPG_PATCH_INFO_H 18 #define UPG_PATCH_INFO_H 19 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include "errcode.h" 23 #include "upg_patch.h" 24 #include "LzmaDec.h" 25 #include "upg_common_porting.h" 26 27 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif 31 #endif 32 33 #define upg_compile_assert(expr) typedef char upg_comp_assert[(expr) ? 1 : 0] 34 35 #define uapi_max(a, b) (((a) > (b)) ? (a) : (b)) 36 #define uapi_min(a, b) (((a) < (b)) ? (a) : (b)) 37 38 #define DECOMPRESSION_SIZE 1024 39 40 #define PAGE_WRITE_PARTIAL_START (1<<1) 41 #define PAGE_WRITE_STANDARD (1<<3) 42 #define PAGE_WRITE_PARTIAL_END (1<<5) 43 #define PAGE_WRITE_SIGNATURE_MASK 0xC0 /* Top 2 bits used for signature handling 1100 0000 */ 44 #define PAGE_WRITE_SIGNATURE_B 0x80 /* Signature page has been written to flash double buffer */ 45 #define PAGE_WRITE_SIGNATURE_W 0x40 /* Signature page has been written to flash image */ 46 47 #define HN_LZMA_SIZEOF_IMGSIZE 3 48 49 typedef struct fota_buffers { 50 uint8_t page_buffer[UPG_FLASH_PAGE_SIZE]; 51 uint8_t page_status[FLASH_PAGES]; 52 } fota_buffers_t; 53 54 typedef struct patch_state { 55 /* patch description */ 56 patch *desc; 57 /* patch contents moved into RAM */ 58 /* Error code */ 59 errcode_t err_code; 60 /* patch operating state */ 61 ISzAlloc lzma_alloc; 62 uint8_t *local_buffer; 63 int32_t local_buffer_page; 64 int32_t page_first_written; 65 int32_t page_last_written; 66 bool done_skipping; 67 } patch_state_t; 68 69 #pragma pack(push, 4) 70 typedef struct control_block { 71 uint32_t magic; 72 uint32_t copy; 73 uint32_t extra; 74 int32_t seek; 75 } control_block_t; // or could use attribute__((__packed)) instead of this pragma pack stuff 76 77 #pragma pack(pop) 78 upg_compile_assert(sizeof(control_block_t) == 16); 79 80 typedef struct zip_context { 81 /* The compression context */ 82 CLzmaDec *dec; 83 /* an offset into the source (why the hell this isn't stored within the context is beyond me) */ 84 int32_t offset; 85 /* the compressed data */ 86 unsigned char *cdata; 87 size_t unpacked_len; 88 size_t unpacked_so_far; 89 size_t cdata_len; 90 } zip_context_t; 91 92 /* Code style related process_patch(...) recovery state */ 93 typedef struct process_patch_state { 94 uint8_t *recovery_buffer; 95 bool recovery_found; 96 } process_patch_state_t; 97 98 /* Code style related apply_patch(...) state */ 99 typedef struct apply_patch_state { 100 zip_context_t *zcontext; 101 unsigned char *unpackedbuf; 102 control_block_t cb; 103 int32_t cerror; 104 uint32_t lenread; 105 int32_t oldpos; 106 int32_t newpos; 107 int32_t blockcount; 108 int32_t chunk_start; 109 int32_t chunk_size; 110 } apply_patch_state_t; 111 112 void write_image_block(patch_state_t *state, uint32_t size, int32_t location, const uint8_t *source); 113 void fota_patch_report_mem(void); 114 115 #ifdef __cplusplus 116 #if __cplusplus 117 } 118 #endif 119 #endif 120 121 #endif /* UPG_PATCH_INFO_H */ 122 123