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 public header 15 */ 16 17 #ifndef UPG_PATCH_H 18 #define UPG_PATCH_H 19 20 #include <stdint.h> 21 #include <stdbool.h> 22 #include "errcode.h" 23 #include "upg_common_porting.h" 24 #include "upg.h" 25 #include "upg_definitions.h" 26 27 #ifdef __cplusplus 28 #if __cplusplus 29 extern "C" { 30 #endif 31 #endif 32 33 #define DIFF_STATUS_FLASH_SIZE 0x800000 34 #define FLASH_PAGES (DIFF_STATUS_FLASH_SIZE / UPG_FLASH_PAGE_SIZE) 35 36 #define NUM_BITS_PER_BYTE 8 37 38 typedef struct patch_description patch; 39 40 /** 41 * @brief The function to be called when a deliberate failure is injected. 42 * 43 * @param desc The patch to be applied 44 */ 45 typedef void (*failfunc)(patch *desc); 46 47 /** 48 * @brief The function to be called to obtain firmware image contents. 49 * 50 * @param desc The patch description. 51 * @param size The size of the contents. 52 * @param location The byte position within the image to get the contents from. 53 * @param dest The buffer to put the contents into. 54 */ 55 typedef errcode_t (*fetch_image_contents)(patch *desc, uint32_t size, int32_t location, uint8_t *dest); 56 57 /** 58 * @brief Update the image contents prior to writing to flash. 59 * 60 * @param desc The patch description. 61 * @param image_page_no The page to be written. 62 * @param page_contents The buffer containing the page contents that will be written. 63 */ 64 typedef errcode_t (*prep_image_contents_for_write)(patch *desc, uint32_t image_page_no, uint8_t *page_contents); 65 66 /** 67 * @brief Creates a plaintext copy of the recovered 4K flash. 68 * It assumes metadata pages are populated with the information about updated pages during FOTA 69 * It assumes FOTA Section is populated with the right key areas. 70 * @param desc Patch description 71 * @param flash_page Number of flash pages from base 72 * @param recover_buffer Buffer to decrypt and copy 73 * @return ERRCODE_SUCC errcode_t or an error code. 74 */ 75 typedef errcode_t (*copy_recovered_buffer_to_flash_cache)(patch *desc, int32_t flash_page, uint8_t *recover_buffer); 76 77 /** 78 * @brief Creates a plaintext version of the image flash in RAM 79 * It assumes metadata pages are populated with the information about updated pages during FOTA 80 * It assumes FOTA Section is populated with the right key areas. 81 * @param desc Patch description 82 * @return ERRCODE_SUCC errcode_t or an error code 83 */ 84 typedef errcode_t (*plaintext_flash_cache_init)(patch *desc); 85 86 /** 87 * Encrypt a UPG_FLASH_PAGE_SIZE RAM buffer and optionally specify a block of data at the start or at the end of the 88 * buffer that is to remain plaintext and not to be encrypted. It can encrypt in place if required where the the output 89 * encrypted buffer is set to the same as the plaintext source buffer. 90 * 91 * @param aes_ctrl aes control structure. 92 * @param src UPG_FLASH_PAGE_SIZE RAM buffer where the plaintext version of the page is located. 93 * @param dst UPG_FLASH_PAGE_SIZE RAM buffer where the encrypted version of the page will be produced. 94 * @param ptxt_len Number of bytes in the plaintext page (#src) that is to remain plaintext and not to be encrypted. 95 * Defined either from the start (>0) or from the end (<0) of the buffer #src. 96 * Its valid range of values is: -UPG_FLASH_PAGE_SIZE < ptxt_len < UPG_FLASH_PAGE_SIZE. 97 * If ptxt_len = 0 The whole page will be encrypted. 98 * If ptxt_len > 0 Everything except the first ptxt_len bytes will will be encrypted. 99 * If ptxt_len < 0 Everything except the last ptxt_len bytes will will be encrypted. 100 * 101 * @return ERRCODE_SUCC errcode_t or an error code. 102 */ 103 typedef errcode_t (*encrypt_one_flash_page)(upg_key_area_data_t *aes_ctrl, 104 uint8_t *src, uint8_t *dst, int32_t ptxt_len); 105 106 struct patch_description { 107 uint32_t image_id; 108 int32_t maxsize; 109 int32_t newsize; 110 int32_t oldsize; 111 uint32_t num_new_pages; 112 uint32_t num_old_pages; 113 uint32_t num_maxsize_pages; 114 uint32_t new_sig_page; 115 uint32_t old_sig_page; 116 117 /* If non zero, simulate a failure */ 118 int32_t failpoint; 119 failfunc failfn; 120 uint32_t patch_contents_ram_copy; 121 uint32_t patch_contents_flash_offset; 122 uint32_t patch_contents_len; 123 bool bottom_up; 124 /* Memory layout info */ 125 uint32_t image_flash_offset; 126 uint32_t image_flash_length; 127 uint32_t buffers_flash_offset; 128 uint32_t buffers_length; 129 fetch_image_contents fetch_image_contents_fn; 130 prep_image_contents_for_write prep_image_contents_for_write_fn; 131 copy_recovered_buffer_to_flash_cache copy_recovered_buffer_to_flash_cache_fn; 132 plaintext_flash_cache_init plaintext_flash_cache_init_fn; 133 encrypt_one_flash_page encrypt_flash_page_fn; 134 bool use_plain_text_cache; /* Use a plaintext RAM cache of the flash image for patching, e.g SEMAIN */ 135 uint8_t *image_cache; 136 bool image_encrypted; 137 uint16_t image_hdrs_len; /* Length of images key area + code area hdr. */ 138 uint16_t image_signature_size; /* Length of image signature */ 139 upg_key_area_data_t key_area_aes_params; 140 }; 141 142 /** 143 * @brief Process the patch provided. 144 * 145 * @param desc The patch description to apply. 146 * @return errcode_t ERRCODE_SUCC or an error code. 147 */ 148 errcode_t process_patch(patch *desc); 149 150 /** 151 * @brief Apply a FOTA core task for an image. 152 * 153 * @param task The FOTA code task header in SCPU RAM. 154 * @return errcode_t ERRCODE_SUCC or an error code. 155 */ 156 errcode_t fota_pkg_task_apply_code_diff(const upg_image_header_t *image); 157 158 #ifdef __cplusplus 159 #if __cplusplus 160 } 161 #endif 162 #endif 163 164 #endif /* UPG_PATCH_H */ 165