1 /* 2 // Copyright (C) 2022 Beken Corporation 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 */ 15 16 #ifndef __BEKENDRIVERFLASH_H__ 17 #define __BEKENDRIVERFLASH_H__ 18 19 #pragma once 20 21 #include <common/bk_include.h> 22 #include <os/os.h> 23 #include "bk_flash.h" 24 #ifdef __cplusplus 25 extern"C" { 26 #endif 27 28 /** @addtogroup BK_PLATFORM 29 * @{ 30 */ 31 32 /** @defgroup BK_FLASH _BK_ Flash Driver 33 * @brief Flash operation Functions 34 * @{ 35 */ 36 /****************************************************** 37 * Macros 38 ******************************************************/ 39 #define BK_PARTITION_USER_MAX ( 1 ) 40 #define PAR_OPT_READ_POS ( 0 ) 41 #define PAR_OPT_WRITE_POS ( 1 ) 42 43 #define PAR_OPT_READ_MASK ( 0x1u << PAR_OPT_READ_POS ) 44 #define PAR_OPT_WRITE_MASK ( 0x1u << PAR_OPT_WRITE_POS ) 45 46 #define PAR_OPT_READ_DIS ( 0x0u << PAR_OPT_READ_POS ) 47 #define PAR_OPT_READ_EN ( 0x1u << PAR_OPT_READ_POS ) 48 #define PAR_OPT_WRITE_DIS ( 0x0u << PAR_OPT_WRITE_POS ) 49 #define PAR_OPT_WRITE_EN ( 0x1u << PAR_OPT_WRITE_POS ) 50 51 /****************************************************** 52 * Enumerations 53 ******************************************************/ 54 typedef enum 55 { 56 BK_PARTITION_BOOTLOADER = 0, 57 BK_PARTITION_APPLICATION, 58 BK_PARTITION_OTA, 59 #if (CONFIG_SOC_BK7256XX) 60 BK_PARTITION_APPLICATION1, 61 #endif 62 BK_PARTITION_MATTER_FLASH, 63 BK_PARTITION_RF_FIRMWARE, 64 BK_PARTITION_NET_PARAM, 65 BK_PARTITION_USR_CONFIG, 66 BK_PARTITION_MAX, 67 } bk_partition_t; 68 69 /****************************************************** 70 * Type Definitions 71 ******************************************************/ 72 typedef enum 73 { 74 BK_FLASH_EMBEDDED, 75 BK_FLASH_SPI, 76 BK_FLASH_MAX, 77 BK_FLASH_NONE, 78 } bk_flash_t; 79 80 typedef struct 81 { 82 bk_flash_t partition_owner; 83 const char *partition_description; 84 uint32_t partition_start_addr; 85 uint32_t partition_length; 86 uint32_t partition_options; 87 } platform_logic_partition_t; 88 89 typedef platform_logic_partition_t bk_logic_partition_t; 90 91 92 /****************************************************** 93 * Global Variables 94 ******************************************************/ 95 96 extern const char *flash_name[]; /**< A name string of a Flash drive */ 97 98 /****************************************************** 99 * Function Declarations 100 ******************************************************/ 101 bk_err_t BkFlashInit(void); 102 bk_err_t BkFlashUninit(void); 103 104 /**@brief Get the infomation of the specified flash area 105 * 106 * @param inPartition: The target flash logical partition which should be erased 107 * 108 * @return bk_logi_partition struct 109 */ 110 bk_logic_partition_t *bk_flash_get_info( bk_partition_t inPartition ); 111 112 113 /**@brief Erase an area on a Flash logical partition 114 * 115 * @note Erase on an address will erase all data on a sector that the 116 * address is belonged to, this function does not save data that 117 * beyond the address area but in the affected sector, the data 118 * will be lost. 119 * 120 * @param inPartition : The target flash logical partition which should be erased 121 * @param off_set : Start address of the erased flash area 122 * @param size : Size of the erased flash area 123 * 124 * @return kNoErr : On success. 125 * @return kGeneralErr : If an error occurred with any step 126 */ 127 bk_err_t bk_flash_erase(bk_partition_t inPartition, uint32_t off_set, uint32_t size); 128 129 /**@brief Write data to an area on a Flash logical partition 130 * 131 * @param inPartition : The target flash logical partition which should be read which should be written 132 * @param off_set : 133 * @param inBuffer : point to the data buffer that will be written to flash 134 * @param inBufferLength : The length of the buffer 135 * 136 * @return kNoErr : On success. 137 * @return kGeneralErr : If an error occurred with any step 138 */ 139 bk_err_t bk_flash_write( bk_partition_t inPartition, volatile uint32_t off_set, uint8_t *inBuffer , uint32_t inBufferLength); 140 141 /**@brief Read data from an area on a Flash to data buffer in RAM 142 * 143 * @param inPartition : The target flash logical partition which should be read 144 * @param off_set : 145 * @param outBuffer : Point to the data buffer that stores the data read from flash 146 * @param inBufferLength : The length of the buffer 147 * 148 * @return kNoErr : On success. 149 * @return kGeneralErr : If an error occurred with any step 150 */ 151 bk_err_t bk_flash_read( bk_partition_t inPartition, volatile uint32_t off_set, uint8_t *outBuffer, uint32_t inBufferLength); 152 153 154 155 /**@brief Set security options on a logical partition 156 * 157 * @param partition : The target flash logical partition 158 * @param offset : Point to the start address that the data is read, and 159 * point to the last unread address after this function is 160 * returned, so you can call this function serval times without 161 * update this start address. 162 * @param size : Size of enabled flash area 163 * 164 * @return kNoErr : On success. 165 * @return kGeneralErr : If an error occurred with any step 166 */ 167 bk_err_t bk_flash_enable_security(PROTECT_TYPE type ); 168 /**@brief get current flash protect type 169 * 170 * @param protect_flag : teh protect type 171 * 172 * @return kNoErr : On success. 173 * @return kGeneralErr : If an error occurred with any step 174 */ 175 bk_err_t bk_flash_get_security(PROTECT_TYPE *protect_flag); 176 /**@brief Erase an area from an abosolute addres 177 * 178 * @note Erase on an address will erase all data on a sector that the 179 * address is belonged to, this function does not save data that 180 * beyond the address area but in the affected sector, the data 181 * will be lost. 182 * 183 * @param flashOffset : The aboslute address which is the start address of the erase operation 184 * @param size : Size of the erased flash area 185 * 186 * @return kNoErr : On success. 187 * @return kGeneralErr : If an error occurred with any step 188 */ 189 bk_err_t bk_flash_abs_addr_erase(unsigned int flashOffset, unsigned int size); 190 /**@brief Read data from an area on a Flash to data buffer in RAM 191 * 192 * @param off_set : 193 * @param outBuffer : Point to the data buffer that stores the data read from flash 194 * @param size : The length of the buffer 195 * 196 * @return kNoErr : On success. 197 * @return kGeneralErr : If an error occurred with any step 198 */ 199 bk_err_t bk_flash_abs_addr_read(unsigned int off_set, unsigned char *outBuffer, unsigned int size); 200 201 /**@brief write data to an area on a Flash from data buffer in RAM 202 * 203 * @param off_set : 204 * @param inBuffer : Point to the data buffer that stores the data write to flash 205 * @param size : The length of the buffer 206 *@param eraseflag: whehter erase or not before write 207 * @return kNoErr : On success. 208 * @return kGeneralErr : If an error occurred with any step 209 */ 210 bk_err_t bk_flash_abs_addr_write(unsigned int off_set, const unsigned char *inBuffer, unsigned int size, unsigned char eraseflag); 211 212 #ifdef BOOTLOADER 213 /**@brief Disable security options on a logical partition 214 * 215 * @param partition : The target flash logical partition 216 * @param offset : Point to the start address that the data is read, and 217 * point to the last unread address after this function is 218 * returned, so you can call this function serval times without 219 * update this start address. 220 * @param size : Size of disabled flash area 221 * 222 * @return kNoErr : On success. 223 * @return kGeneralErr : If an error occurred with any step 224 */ 225 bk_err_t BkFlashDisableSecurity( bk_partition_t partition, uint32_t off_set, uint32_t size ); 226 #endif 227 228 229 int hal_flash_init(void); 230 231 #ifdef __cplusplus 232 } 233 #endif 234 235 /** @} */ 236 /** @} */ 237 238 #endif 239 240 241