SDHI_driver patch (mbedOS 5.11.5)
mbed-os-program/mbed-os/features/storage/kvstore/conf/kv_config.cpp@0:e1f465d87307, 2019-03-18 (annotated)
- Committer:
- tvendov
- Date:
- Mon Mar 18 16:54:40 2019 +0000
- Revision:
- 0:e1f465d87307
Initial_II
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tvendov | 0:e1f465d87307 | 1 | /* |
| tvendov | 0:e1f465d87307 | 2 | * Copyright (c) 2018 ARM Limited. All rights reserved. |
| tvendov | 0:e1f465d87307 | 3 | * SPDX-License-Identifier: Apache-2.0 |
| tvendov | 0:e1f465d87307 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
| tvendov | 0:e1f465d87307 | 5 | * not use this file except in compliance with the License. |
| tvendov | 0:e1f465d87307 | 6 | * You may obtain a copy of the License at |
| tvendov | 0:e1f465d87307 | 7 | * |
| tvendov | 0:e1f465d87307 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| tvendov | 0:e1f465d87307 | 9 | * |
| tvendov | 0:e1f465d87307 | 10 | * Unless required by applicable law or agreed to in writing, software |
| tvendov | 0:e1f465d87307 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| tvendov | 0:e1f465d87307 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| tvendov | 0:e1f465d87307 | 13 | * See the License for the specific language governing permissions and |
| tvendov | 0:e1f465d87307 | 14 | * limitations under the License. |
| tvendov | 0:e1f465d87307 | 15 | */ |
| tvendov | 0:e1f465d87307 | 16 | |
| tvendov | 0:e1f465d87307 | 17 | #include "kv_config.h" |
| tvendov | 0:e1f465d87307 | 18 | #include "KVStore.h" |
| tvendov | 0:e1f465d87307 | 19 | #include "KVMap.h" |
| tvendov | 0:e1f465d87307 | 20 | #include "BlockDevice.h" |
| tvendov | 0:e1f465d87307 | 21 | #include "FileSystem.h" |
| tvendov | 0:e1f465d87307 | 22 | #include "FileSystemStore.h" |
| tvendov | 0:e1f465d87307 | 23 | #include "SlicingBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 24 | #include "FATFileSystem.h" |
| tvendov | 0:e1f465d87307 | 25 | #include "LittleFileSystem.h" |
| tvendov | 0:e1f465d87307 | 26 | #include "TDBStore.h" |
| tvendov | 0:e1f465d87307 | 27 | #include "mbed_error.h" |
| tvendov | 0:e1f465d87307 | 28 | #include "FlashIAP.h" |
| tvendov | 0:e1f465d87307 | 29 | #include "FlashSimBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 30 | #include "mbed_trace.h" |
| tvendov | 0:e1f465d87307 | 31 | #include "SecureStore.h" |
| tvendov | 0:e1f465d87307 | 32 | #define TRACE_GROUP "KVCFG" |
| tvendov | 0:e1f465d87307 | 33 | |
| tvendov | 0:e1f465d87307 | 34 | #if COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 35 | #include "FlashIAPBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 36 | #endif |
| tvendov | 0:e1f465d87307 | 37 | |
| tvendov | 0:e1f465d87307 | 38 | #if COMPONENT_QSPIF |
| tvendov | 0:e1f465d87307 | 39 | #include "QSPIFBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 40 | #endif |
| tvendov | 0:e1f465d87307 | 41 | |
| tvendov | 0:e1f465d87307 | 42 | #if COMPONENT_SPIF |
| tvendov | 0:e1f465d87307 | 43 | #include "SPIFBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 44 | #endif |
| tvendov | 0:e1f465d87307 | 45 | |
| tvendov | 0:e1f465d87307 | 46 | #if COMPONENT_DATAFLASH |
| tvendov | 0:e1f465d87307 | 47 | #include "DataFlashBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 48 | #endif |
| tvendov | 0:e1f465d87307 | 49 | |
| tvendov | 0:e1f465d87307 | 50 | #if COMPONENT_SD |
| tvendov | 0:e1f465d87307 | 51 | #if (defined(TARGET_VK_RZ_A1H) || defined(TARGET_VK_RZ_A1LU)) |
| tvendov | 0:e1f465d87307 | 52 | #include "SDHIBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 53 | #else |
| tvendov | 0:e1f465d87307 | 54 | #include "SDBlockDevice.h" |
| tvendov | 0:e1f465d87307 | 55 | #endif |
| tvendov | 0:e1f465d87307 | 56 | #endif |
| tvendov | 0:e1f465d87307 | 57 | |
| tvendov | 0:e1f465d87307 | 58 | /** |
| tvendov | 0:e1f465d87307 | 59 | * @brief This function initializes internal memory secure storage |
| tvendov | 0:e1f465d87307 | 60 | * This includes a TDBStore instance with a FlashIAPBlockdevice |
| tvendov | 0:e1f465d87307 | 61 | * as the supported storage. |
| tvendov | 0:e1f465d87307 | 62 | * The following is a list of configuration parameter |
| tvendov | 0:e1f465d87307 | 63 | * MBED_CONF_STORAGE_TDB_INTERNAL_SIZE - The size of the underlying FlashIAPBlockdevice |
| tvendov | 0:e1f465d87307 | 64 | * MBED_CONF_STORAGE_TDB_INTERNAL_BASE_ADDRESS - The start address of the underlying FlashIAPBlockdevice |
| tvendov | 0:e1f465d87307 | 65 | * @returns 0 on success or negative value on failure. |
| tvendov | 0:e1f465d87307 | 66 | */ |
| tvendov | 0:e1f465d87307 | 67 | int _storage_config_TDB_INTERNAL(); |
| tvendov | 0:e1f465d87307 | 68 | |
| tvendov | 0:e1f465d87307 | 69 | /** |
| tvendov | 0:e1f465d87307 | 70 | * @brief This function initialize external memory secure storage |
| tvendov | 0:e1f465d87307 | 71 | * This includes a SecureStore class with TDBStore over FlashIAPBlockdevice |
| tvendov | 0:e1f465d87307 | 72 | * and an external TDBStore over a default blockdevice unless configured differently. |
| tvendov | 0:e1f465d87307 | 73 | * The following is a list of configuration parameter: |
| tvendov | 0:e1f465d87307 | 74 | * MBED_CONF_STORAGE_TDB_EXTERNAL_RBP_INTERNAL_SIZE - Size of the internal FlashIAPBlockDevice and by |
| tvendov | 0:e1f465d87307 | 75 | * default is set to from start address to the end of the flash. |
| tvendov | 0:e1f465d87307 | 76 | * If start address is 0 the start address will be set to end of |
| tvendov | 0:e1f465d87307 | 77 | * flash - rbp_internal_size. |
| tvendov | 0:e1f465d87307 | 78 | * MBED_CONF_STORAGE_TDB_EXTERNAL_INTERNAL_BASE_ADDRESS - The satrt address of the internal FlashIAPBlockDevice. |
| tvendov | 0:e1f465d87307 | 79 | * MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_SIZE - Size of the external blockdevice in bytes or NULL for |
| tvendov | 0:e1f465d87307 | 80 | * max possible size. |
| tvendov | 0:e1f465d87307 | 81 | * MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BASE_ADDRESS - The block device start address. |
| tvendov | 0:e1f465d87307 | 82 | * MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BLOCK_DEVICE - Alowed vlaues are: default, SPIF, DATAFASH, QSPIF or SD |
| tvendov | 0:e1f465d87307 | 83 | * @returns 0 on success or negative value on failure. |
| tvendov | 0:e1f465d87307 | 84 | */ |
| tvendov | 0:e1f465d87307 | 85 | int _storage_config_TDB_EXTERNAL(); |
| tvendov | 0:e1f465d87307 | 86 | |
| tvendov | 0:e1f465d87307 | 87 | /** |
| tvendov | 0:e1f465d87307 | 88 | * @brief This function initialize a external memory secure storage |
| tvendov | 0:e1f465d87307 | 89 | * This includes a SecureStore class with external TDBStore over a blockdevice or, |
| tvendov | 0:e1f465d87307 | 90 | * if no blockdevice was set the default blockdevice will be used. |
| tvendov | 0:e1f465d87307 | 91 | * The following is a list of configuration parameter: |
| tvendov | 0:e1f465d87307 | 92 | * MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_SIZE - Size of the external blockdevice in bytes |
| tvendov | 0:e1f465d87307 | 93 | * or NULL for max possible size. |
| tvendov | 0:e1f465d87307 | 94 | * MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BASE_ADDRESS - The block device start address |
| tvendov | 0:e1f465d87307 | 95 | * MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BLOCK_DEVICE - Alowed vlaues are: default, SPIF, DATAFASH, QSPIF or SD |
| tvendov | 0:e1f465d87307 | 96 | * @returns 0 on success or negative value on failure. |
| tvendov | 0:e1f465d87307 | 97 | */ |
| tvendov | 0:e1f465d87307 | 98 | int _storage_config_TDB_EXTERNAL_NO_RBP(); |
| tvendov | 0:e1f465d87307 | 99 | |
| tvendov | 0:e1f465d87307 | 100 | /** |
| tvendov | 0:e1f465d87307 | 101 | * @brief This function initialize a FILESYSTEM memory secure storage |
| tvendov | 0:e1f465d87307 | 102 | * This includes a SecureStore class with TDBStore over FlashIAPBlockdevice |
| tvendov | 0:e1f465d87307 | 103 | * in the internal memory and an external FileSysteStore. If blockdevice and filesystem not set, |
| tvendov | 0:e1f465d87307 | 104 | * the system will use the default block device and default filesystem |
| tvendov | 0:e1f465d87307 | 105 | * The following is a list of configuration parameter: |
| tvendov | 0:e1f465d87307 | 106 | * MBED_CONF_STORAGE_FILESYSTEM_RBP_INTERNAL_SIZE - Size of the internal FlashIAPBlockDevice and by |
| tvendov | 0:e1f465d87307 | 107 | * default is set to from start address to the end of the flash. |
| tvendov | 0:e1f465d87307 | 108 | * If start address is 0 the start address will be set to end of |
| tvendov | 0:e1f465d87307 | 109 | * flash - rbp_internal_size. |
| tvendov | 0:e1f465d87307 | 110 | * MBED_CONF_STORAGE_FILESYSTEM_INTERNAL_BASE_ADDRESS - The satrt address of the internal FlashIAPBlockDevice. |
| tvendov | 0:e1f465d87307 | 111 | * MBED_CONF_STORAGE_FILESYSTEM_FILESYSTEM - Allowed values are: default, FAT or LITTLE |
| tvendov | 0:e1f465d87307 | 112 | * MBED_CONF_STORAGE_FILESYSTEM_BLOCKDEVICE - Allowed values are: default, SPIF, DATAFASH, QSPIF or SD |
| tvendov | 0:e1f465d87307 | 113 | * MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_SIZE - External Blockdevice size in bytes or NULL for max possible size. |
| tvendov | 0:e1f465d87307 | 114 | * MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_BASE_ADDRESS - The block device start address. |
| tvendov | 0:e1f465d87307 | 115 | * MBED_CONF_STORAGE_FILESYSTEM_MOUNT_POINT - Where to mount the filesystem |
| tvendov | 0:e1f465d87307 | 116 | * MBED_CONF_STORAGE_FILESYSTEM_FOLDER_PATH - The working folder paths |
| tvendov | 0:e1f465d87307 | 117 | * |
| tvendov | 0:e1f465d87307 | 118 | * @returns 0 on success or negative value on failure. |
| tvendov | 0:e1f465d87307 | 119 | */ |
| tvendov | 0:e1f465d87307 | 120 | int _storage_config_FILESYSTEM(); |
| tvendov | 0:e1f465d87307 | 121 | |
| tvendov | 0:e1f465d87307 | 122 | /** |
| tvendov | 0:e1f465d87307 | 123 | * @brief This function initialize a FILESYSTEM_NO_RBP memory secure storage with no |
| tvendov | 0:e1f465d87307 | 124 | * rollback protection. This includes a SecureStore class an external FileSysteStore over a default |
| tvendov | 0:e1f465d87307 | 125 | * filesystem with default blockdevice unless differently configured. |
| tvendov | 0:e1f465d87307 | 126 | * The following is a list of configuration parameter: |
| tvendov | 0:e1f465d87307 | 127 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FILESYSTEM - Allowed values are: default, FAT or LITTLE |
| tvendov | 0:e1f465d87307 | 128 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_BLOCKDEVICE - Allowed values are: default, SPIF, DATAFASH, QSPIF or SD |
| tvendov | 0:e1f465d87307 | 129 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_SIZE - Blockdevice size in bytes. or NULL for max possible size. |
| tvendov | 0:e1f465d87307 | 130 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_BASE_ADDRESS - The block device start address. |
| tvendov | 0:e1f465d87307 | 131 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT - Where to mount the filesystem |
| tvendov | 0:e1f465d87307 | 132 | * MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FOLDER_PATH - The working folder paths |
| tvendov | 0:e1f465d87307 | 133 | * |
| tvendov | 0:e1f465d87307 | 134 | * @returns 0 on success or negative value on failure. |
| tvendov | 0:e1f465d87307 | 135 | */ |
| tvendov | 0:e1f465d87307 | 136 | int _storage_config_FILESYSTEM_NO_RBP(); |
| tvendov | 0:e1f465d87307 | 137 | |
| tvendov | 0:e1f465d87307 | 138 | int _storage_config_tdb_external_common(); |
| tvendov | 0:e1f465d87307 | 139 | int _storage_config_filesystem_common(); |
| tvendov | 0:e1f465d87307 | 140 | |
| tvendov | 0:e1f465d87307 | 141 | static const char *filesystemstore_folder_path = NULL; |
| tvendov | 0:e1f465d87307 | 142 | |
| tvendov | 0:e1f465d87307 | 143 | using namespace mbed; |
| tvendov | 0:e1f465d87307 | 144 | |
| tvendov | 0:e1f465d87307 | 145 | |
| tvendov | 0:e1f465d87307 | 146 | static SingletonPtr<PlatformMutex> mutex; |
| tvendov | 0:e1f465d87307 | 147 | static bool is_kv_config_initialize = false; |
| tvendov | 0:e1f465d87307 | 148 | static kvstore_config_t kvstore_config; |
| tvendov | 0:e1f465d87307 | 149 | |
| tvendov | 0:e1f465d87307 | 150 | #define INTERNAL_BLOCKDEVICE_NAME FLASHIAP |
| tvendov | 0:e1f465d87307 | 151 | |
| tvendov | 0:e1f465d87307 | 152 | #define STR_EXPAND(tok) #tok |
| tvendov | 0:e1f465d87307 | 153 | #define STR(tok) STR_EXPAND(tok) |
| tvendov | 0:e1f465d87307 | 154 | |
| tvendov | 0:e1f465d87307 | 155 | #define _GET_FILESYSTEM_concat(dev, ...) _get_filesystem_##dev(__VA_ARGS__) |
| tvendov | 0:e1f465d87307 | 156 | #define GET_FILESYSTEM(dev, ...) _GET_FILESYSTEM_concat(dev, __VA_ARGS__) |
| tvendov | 0:e1f465d87307 | 157 | |
| tvendov | 0:e1f465d87307 | 158 | #define _GET_BLOCKDEVICE_concat(dev, ...) _get_blockdevice_##dev(__VA_ARGS__) |
| tvendov | 0:e1f465d87307 | 159 | #define GET_BLOCKDEVICE(dev, ...) _GET_BLOCKDEVICE_concat(dev, __VA_ARGS__) |
| tvendov | 0:e1f465d87307 | 160 | |
| tvendov | 0:e1f465d87307 | 161 | static inline uint32_t align_up(uint64_t val, uint64_t size) |
| tvendov | 0:e1f465d87307 | 162 | { |
| tvendov | 0:e1f465d87307 | 163 | return (((val - 1) / size) + 1) * size; |
| tvendov | 0:e1f465d87307 | 164 | } |
| tvendov | 0:e1f465d87307 | 165 | |
| tvendov | 0:e1f465d87307 | 166 | static inline uint32_t align_down(uint64_t val, uint64_t size) |
| tvendov | 0:e1f465d87307 | 167 | { |
| tvendov | 0:e1f465d87307 | 168 | return (((val) / size)) * size; |
| tvendov | 0:e1f465d87307 | 169 | } |
| tvendov | 0:e1f465d87307 | 170 | |
| tvendov | 0:e1f465d87307 | 171 | int _calculate_blocksize_match_tdbstore(BlockDevice *bd) |
| tvendov | 0:e1f465d87307 | 172 | { |
| tvendov | 0:e1f465d87307 | 173 | bd_size_t size = bd->size(); |
| tvendov | 0:e1f465d87307 | 174 | bd_size_t erase_size = bd->get_erase_size(); |
| tvendov | 0:e1f465d87307 | 175 | bd_size_t number_of_sector = size / erase_size; |
| tvendov | 0:e1f465d87307 | 176 | |
| tvendov | 0:e1f465d87307 | 177 | if (number_of_sector < 2) { |
| tvendov | 0:e1f465d87307 | 178 | tr_warning("KV Config: There are less than two sectors - TDBStore will not work."); |
| tvendov | 0:e1f465d87307 | 179 | return -1; |
| tvendov | 0:e1f465d87307 | 180 | } |
| tvendov | 0:e1f465d87307 | 181 | |
| tvendov | 0:e1f465d87307 | 182 | |
| tvendov | 0:e1f465d87307 | 183 | if (number_of_sector % 2 != 0) { |
| tvendov | 0:e1f465d87307 | 184 | tr_warning("KV Config: Number of sectors is not an even number. Consider changing the BlockDevice size"); |
| tvendov | 0:e1f465d87307 | 185 | } |
| tvendov | 0:e1f465d87307 | 186 | |
| tvendov | 0:e1f465d87307 | 187 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 188 | } |
| tvendov | 0:e1f465d87307 | 189 | |
| tvendov | 0:e1f465d87307 | 190 | int _get_addresses(BlockDevice *bd, bd_addr_t start_address, bd_size_t size, bd_addr_t *out_start_addr, |
| tvendov | 0:e1f465d87307 | 191 | bd_addr_t *out_end_addr) |
| tvendov | 0:e1f465d87307 | 192 | { |
| tvendov | 0:e1f465d87307 | 193 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 194 | bd_addr_t end_address; |
| tvendov | 0:e1f465d87307 | 195 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 196 | |
| tvendov | 0:e1f465d87307 | 197 | aligned_start_address = align_down(start_address, bd->get_erase_size(start_address)); |
| tvendov | 0:e1f465d87307 | 198 | if (aligned_start_address != start_address) { |
| tvendov | 0:e1f465d87307 | 199 | tr_error("KV Config: Start address is not aligned. Better use %02llx", aligned_start_address); |
| tvendov | 0:e1f465d87307 | 200 | return -1; |
| tvendov | 0:e1f465d87307 | 201 | } |
| tvendov | 0:e1f465d87307 | 202 | |
| tvendov | 0:e1f465d87307 | 203 | if (size == 0) { |
| tvendov | 0:e1f465d87307 | 204 | (*out_start_addr) = aligned_start_address; |
| tvendov | 0:e1f465d87307 | 205 | (*out_end_addr) = bd->size(); |
| tvendov | 0:e1f465d87307 | 206 | return 0; |
| tvendov | 0:e1f465d87307 | 207 | } |
| tvendov | 0:e1f465d87307 | 208 | |
| tvendov | 0:e1f465d87307 | 209 | end_address = start_address + size; |
| tvendov | 0:e1f465d87307 | 210 | aligned_end_address = align_up(end_address, bd->get_erase_size(end_address)); |
| tvendov | 0:e1f465d87307 | 211 | if (aligned_end_address != end_address) { |
| tvendov | 0:e1f465d87307 | 212 | tr_error("KV Config: End address is not aligned. Consider changing the size parameter."); |
| tvendov | 0:e1f465d87307 | 213 | return -1; |
| tvendov | 0:e1f465d87307 | 214 | } |
| tvendov | 0:e1f465d87307 | 215 | |
| tvendov | 0:e1f465d87307 | 216 | if (aligned_end_address > bd->size()) { |
| tvendov | 0:e1f465d87307 | 217 | tr_error("KV Config: End address is out of boundaries"); |
| tvendov | 0:e1f465d87307 | 218 | return -1; |
| tvendov | 0:e1f465d87307 | 219 | } |
| tvendov | 0:e1f465d87307 | 220 | |
| tvendov | 0:e1f465d87307 | 221 | (*out_start_addr) = aligned_start_address; |
| tvendov | 0:e1f465d87307 | 222 | (*out_end_addr) = aligned_end_address; |
| tvendov | 0:e1f465d87307 | 223 | return 0; |
| tvendov | 0:e1f465d87307 | 224 | } |
| tvendov | 0:e1f465d87307 | 225 | |
| tvendov | 0:e1f465d87307 | 226 | FileSystem *_get_filesystem_FAT(const char *mount) |
| tvendov | 0:e1f465d87307 | 227 | { |
| tvendov | 0:e1f465d87307 | 228 | static FATFileSystem sdcard(mount); |
| tvendov | 0:e1f465d87307 | 229 | return &sdcard; |
| tvendov | 0:e1f465d87307 | 230 | |
| tvendov | 0:e1f465d87307 | 231 | } |
| tvendov | 0:e1f465d87307 | 232 | |
| tvendov | 0:e1f465d87307 | 233 | FileSystem *_get_filesystem_LITTLE(const char *mount) |
| tvendov | 0:e1f465d87307 | 234 | { |
| tvendov | 0:e1f465d87307 | 235 | static LittleFileSystem flash(mount); |
| tvendov | 0:e1f465d87307 | 236 | return &flash; |
| tvendov | 0:e1f465d87307 | 237 | } |
| tvendov | 0:e1f465d87307 | 238 | |
| tvendov | 0:e1f465d87307 | 239 | FileSystemStore *_get_file_system_store(FileSystem *fs) |
| tvendov | 0:e1f465d87307 | 240 | { |
| tvendov | 0:e1f465d87307 | 241 | static FileSystemStore fss(fs); |
| tvendov | 0:e1f465d87307 | 242 | return &fss; |
| tvendov | 0:e1f465d87307 | 243 | } |
| tvendov | 0:e1f465d87307 | 244 | |
| tvendov | 0:e1f465d87307 | 245 | FileSystem *_get_filesystem_default(const char *mount) |
| tvendov | 0:e1f465d87307 | 246 | { |
| tvendov | 0:e1f465d87307 | 247 | #if COMPONENT_QSPIF || COMPONENT_SPIF || COMPONENT_DATAFLASH |
| tvendov | 0:e1f465d87307 | 248 | return _get_filesystem_LITTLE(mount); |
| tvendov | 0:e1f465d87307 | 249 | #elif COMPONENT_SD |
| tvendov | 0:e1f465d87307 | 250 | return _get_filesystem_FAT(mount); |
| tvendov | 0:e1f465d87307 | 251 | #else |
| tvendov | 0:e1f465d87307 | 252 | return NULL; |
| tvendov | 0:e1f465d87307 | 253 | #endif |
| tvendov | 0:e1f465d87307 | 254 | } |
| tvendov | 0:e1f465d87307 | 255 | |
| tvendov | 0:e1f465d87307 | 256 | //Calculates the start address of FLASHIAP block device for TDB_INTERNAL profile. |
| tvendov | 0:e1f465d87307 | 257 | //If possible, the address will start 2 sectors after the end of code sector allowing |
| tvendov | 0:e1f465d87307 | 258 | //some space for an application update. |
| tvendov | 0:e1f465d87307 | 259 | int _get_flashiap_bd_default_addresses_tdb_internal(bd_addr_t *start_address, bd_size_t *size) |
| tvendov | 0:e1f465d87307 | 260 | { |
| tvendov | 0:e1f465d87307 | 261 | #if COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 262 | |
| tvendov | 0:e1f465d87307 | 263 | FlashIAP flash; |
| tvendov | 0:e1f465d87307 | 264 | |
| tvendov | 0:e1f465d87307 | 265 | if (*start_address != 0 || *size != 0) { |
| tvendov | 0:e1f465d87307 | 266 | return MBED_ERROR_INVALID_ARGUMENT; |
| tvendov | 0:e1f465d87307 | 267 | } |
| tvendov | 0:e1f465d87307 | 268 | |
| tvendov | 0:e1f465d87307 | 269 | //If default values are set, we should get the maximum available size of internal bd. |
| tvendov | 0:e1f465d87307 | 270 | if (flash.init() != 0) { |
| tvendov | 0:e1f465d87307 | 271 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 272 | } |
| tvendov | 0:e1f465d87307 | 273 | |
| tvendov | 0:e1f465d87307 | 274 | *start_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)); |
| tvendov | 0:e1f465d87307 | 275 | |
| tvendov | 0:e1f465d87307 | 276 | // Give the application a couple of spare sectors to grow (if there are such) |
| tvendov | 0:e1f465d87307 | 277 | bd_size_t spare_size_for_app = 0; |
| tvendov | 0:e1f465d87307 | 278 | bd_addr_t curr_addr = *start_address; |
| tvendov | 0:e1f465d87307 | 279 | bd_addr_t flash_end_address = flash.get_flash_start() + flash.get_flash_size(); |
| tvendov | 0:e1f465d87307 | 280 | |
| tvendov | 0:e1f465d87307 | 281 | int spare_sectors_for_app = 2; |
| tvendov | 0:e1f465d87307 | 282 | int min_sectors_for_storage = 2; |
| tvendov | 0:e1f465d87307 | 283 | for (int i = 0; i < spare_sectors_for_app + min_sectors_for_storage - 1; i++) { |
| tvendov | 0:e1f465d87307 | 284 | bd_size_t sector_size = flash.get_sector_size(curr_addr); |
| tvendov | 0:e1f465d87307 | 285 | curr_addr += sector_size; |
| tvendov | 0:e1f465d87307 | 286 | if (curr_addr >= flash_end_address) { |
| tvendov | 0:e1f465d87307 | 287 | spare_size_for_app = 0; |
| tvendov | 0:e1f465d87307 | 288 | break; |
| tvendov | 0:e1f465d87307 | 289 | } |
| tvendov | 0:e1f465d87307 | 290 | |
| tvendov | 0:e1f465d87307 | 291 | if (i < spare_sectors_for_app) { |
| tvendov | 0:e1f465d87307 | 292 | spare_size_for_app += sector_size; |
| tvendov | 0:e1f465d87307 | 293 | } |
| tvendov | 0:e1f465d87307 | 294 | } |
| tvendov | 0:e1f465d87307 | 295 | *start_address += spare_size_for_app; |
| tvendov | 0:e1f465d87307 | 296 | |
| tvendov | 0:e1f465d87307 | 297 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 298 | |
| tvendov | 0:e1f465d87307 | 299 | #endif |
| tvendov | 0:e1f465d87307 | 300 | |
| tvendov | 0:e1f465d87307 | 301 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 302 | } |
| tvendov | 0:e1f465d87307 | 303 | |
| tvendov | 0:e1f465d87307 | 304 | //Calculates address and size for FLASHIAP block device in TDB_EXTERNAL and FILESYSTEM profiles. |
| tvendov | 0:e1f465d87307 | 305 | //The size of the block device will be 2 sectors at the ends of the internal flash for |
| tvendov | 0:e1f465d87307 | 306 | //the use of the rbp internal TDBStore. |
| tvendov | 0:e1f465d87307 | 307 | int _get_flashiap_bd_default_addresses_rbp(bd_addr_t *start_address, bd_size_t *size) |
| tvendov | 0:e1f465d87307 | 308 | { |
| tvendov | 0:e1f465d87307 | 309 | #if COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 310 | |
| tvendov | 0:e1f465d87307 | 311 | bd_addr_t flash_end_address; |
| tvendov | 0:e1f465d87307 | 312 | bd_addr_t flash_start_address; |
| tvendov | 0:e1f465d87307 | 313 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 314 | bd_addr_t flash_first_writable_sector_address; |
| tvendov | 0:e1f465d87307 | 315 | FlashIAP flash; |
| tvendov | 0:e1f465d87307 | 316 | |
| tvendov | 0:e1f465d87307 | 317 | if (*start_address != 0 || *size != 0) { |
| tvendov | 0:e1f465d87307 | 318 | return MBED_ERROR_INVALID_ARGUMENT; |
| tvendov | 0:e1f465d87307 | 319 | } |
| tvendov | 0:e1f465d87307 | 320 | |
| tvendov | 0:e1f465d87307 | 321 | int ret = flash.init(); |
| tvendov | 0:e1f465d87307 | 322 | if (ret != 0) { |
| tvendov | 0:e1f465d87307 | 323 | return MBED_ERROR_INITIALIZATION_FAILED; |
| tvendov | 0:e1f465d87307 | 324 | } |
| tvendov | 0:e1f465d87307 | 325 | |
| tvendov | 0:e1f465d87307 | 326 | flash_first_writable_sector_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)); |
| tvendov | 0:e1f465d87307 | 327 | flash_start_address = flash.get_flash_start(); |
| tvendov | 0:e1f465d87307 | 328 | flash_end_address = flash_start_address + flash.get_flash_size();; |
| tvendov | 0:e1f465d87307 | 329 | *start_address = flash_end_address - 1; |
| tvendov | 0:e1f465d87307 | 330 | aligned_start_address = align_down(*start_address, flash.get_sector_size(*start_address)); |
| tvendov | 0:e1f465d87307 | 331 | *size = (flash_end_address - aligned_start_address) * 2; |
| tvendov | 0:e1f465d87307 | 332 | *start_address = (flash_end_address - *size); |
| tvendov | 0:e1f465d87307 | 333 | aligned_start_address = align_down(*start_address, flash.get_sector_size(*start_address)); |
| tvendov | 0:e1f465d87307 | 334 | |
| tvendov | 0:e1f465d87307 | 335 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 336 | |
| tvendov | 0:e1f465d87307 | 337 | if (aligned_start_address < flash_first_writable_sector_address) { |
| tvendov | 0:e1f465d87307 | 338 | tr_error("KV Config: Internal block device start address overlapped ROM address "); |
| tvendov | 0:e1f465d87307 | 339 | return MBED_ERROR_INITIALIZATION_FAILED; |
| tvendov | 0:e1f465d87307 | 340 | } |
| tvendov | 0:e1f465d87307 | 341 | |
| tvendov | 0:e1f465d87307 | 342 | #endif |
| tvendov | 0:e1f465d87307 | 343 | |
| tvendov | 0:e1f465d87307 | 344 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 345 | |
| tvendov | 0:e1f465d87307 | 346 | } |
| tvendov | 0:e1f465d87307 | 347 | |
| tvendov | 0:e1f465d87307 | 348 | BlockDevice *_get_blockdevice_FLASHIAP(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 349 | { |
| tvendov | 0:e1f465d87307 | 350 | #if COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 351 | |
| tvendov | 0:e1f465d87307 | 352 | bd_addr_t flash_end_address; |
| tvendov | 0:e1f465d87307 | 353 | bd_addr_t flash_start_address; |
| tvendov | 0:e1f465d87307 | 354 | bd_addr_t flash_first_writable_sector_address; |
| tvendov | 0:e1f465d87307 | 355 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 356 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 357 | bd_addr_t end_address; |
| tvendov | 0:e1f465d87307 | 358 | FlashIAP flash; |
| tvendov | 0:e1f465d87307 | 359 | |
| tvendov | 0:e1f465d87307 | 360 | int ret = flash.init(); |
| tvendov | 0:e1f465d87307 | 361 | if (ret != 0) { |
| tvendov | 0:e1f465d87307 | 362 | return NULL; |
| tvendov | 0:e1f465d87307 | 363 | } |
| tvendov | 0:e1f465d87307 | 364 | |
| tvendov | 0:e1f465d87307 | 365 | //Get flash parameters before starting |
| tvendov | 0:e1f465d87307 | 366 | flash_first_writable_sector_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR)); |
| tvendov | 0:e1f465d87307 | 367 | flash_start_address = flash.get_flash_start(); |
| tvendov | 0:e1f465d87307 | 368 | flash_end_address = flash_start_address + flash.get_flash_size();; |
| tvendov | 0:e1f465d87307 | 369 | |
| tvendov | 0:e1f465d87307 | 370 | //Non default configuration |
| tvendov | 0:e1f465d87307 | 371 | if (start_address != 0) { |
| tvendov | 0:e1f465d87307 | 372 | |
| tvendov | 0:e1f465d87307 | 373 | aligned_start_address = align_down(start_address, flash.get_sector_size(start_address)); |
| tvendov | 0:e1f465d87307 | 374 | if (start_address != aligned_start_address) { |
| tvendov | 0:e1f465d87307 | 375 | tr_error("KV Config: Internal block device start address is not aligned. Better use %02llx", aligned_start_address); |
| tvendov | 0:e1f465d87307 | 376 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 377 | return NULL; |
| tvendov | 0:e1f465d87307 | 378 | } |
| tvendov | 0:e1f465d87307 | 379 | |
| tvendov | 0:e1f465d87307 | 380 | if (size == 0) { |
| tvendov | 0:e1f465d87307 | 381 | //The block device will have all space form start address to the end of the flash |
| tvendov | 0:e1f465d87307 | 382 | size = (flash_end_address - start_address); |
| tvendov | 0:e1f465d87307 | 383 | |
| tvendov | 0:e1f465d87307 | 384 | static FlashIAPBlockDevice bd(start_address, size); |
| tvendov | 0:e1f465d87307 | 385 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 386 | return &bd; |
| tvendov | 0:e1f465d87307 | 387 | } |
| tvendov | 0:e1f465d87307 | 388 | |
| tvendov | 0:e1f465d87307 | 389 | if (size != 0) { |
| tvendov | 0:e1f465d87307 | 390 | end_address = start_address + size; |
| tvendov | 0:e1f465d87307 | 391 | if (end_address > flash_end_address) { |
| tvendov | 0:e1f465d87307 | 392 | tr_error("KV Config: Internal block device end address is out of boundaries"); |
| tvendov | 0:e1f465d87307 | 393 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 394 | return NULL; |
| tvendov | 0:e1f465d87307 | 395 | } |
| tvendov | 0:e1f465d87307 | 396 | |
| tvendov | 0:e1f465d87307 | 397 | aligned_end_address = align_up(end_address, flash.get_sector_size(end_address - 1)); |
| tvendov | 0:e1f465d87307 | 398 | if (end_address != aligned_end_address) { |
| tvendov | 0:e1f465d87307 | 399 | tr_error("KV Config: Internal block device start address is not aligned. Consider changing the size parameter"); |
| tvendov | 0:e1f465d87307 | 400 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 401 | return NULL; |
| tvendov | 0:e1f465d87307 | 402 | } |
| tvendov | 0:e1f465d87307 | 403 | |
| tvendov | 0:e1f465d87307 | 404 | static FlashIAPBlockDevice bd(start_address, size); |
| tvendov | 0:e1f465d87307 | 405 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 406 | return &bd; |
| tvendov | 0:e1f465d87307 | 407 | } |
| tvendov | 0:e1f465d87307 | 408 | } |
| tvendov | 0:e1f465d87307 | 409 | |
| tvendov | 0:e1f465d87307 | 410 | //Non default configuration start_address = 0 |
| tvendov | 0:e1f465d87307 | 411 | start_address = flash_end_address - size; |
| tvendov | 0:e1f465d87307 | 412 | aligned_start_address = align_down(start_address, flash.get_sector_size(start_address)); |
| tvendov | 0:e1f465d87307 | 413 | if (start_address != aligned_start_address) { |
| tvendov | 0:e1f465d87307 | 414 | tr_error("KV Config: Internal block device start address is not aligned. Consider changing the size parameter"); |
| tvendov | 0:e1f465d87307 | 415 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 416 | return NULL; |
| tvendov | 0:e1f465d87307 | 417 | } |
| tvendov | 0:e1f465d87307 | 418 | |
| tvendov | 0:e1f465d87307 | 419 | flash.deinit(); |
| tvendov | 0:e1f465d87307 | 420 | |
| tvendov | 0:e1f465d87307 | 421 | if (aligned_start_address < flash_first_writable_sector_address) { |
| tvendov | 0:e1f465d87307 | 422 | tr_error("KV Config: Internal block device start address overlapped ROM address "); |
| tvendov | 0:e1f465d87307 | 423 | return NULL; |
| tvendov | 0:e1f465d87307 | 424 | } |
| tvendov | 0:e1f465d87307 | 425 | static FlashIAPBlockDevice bd(aligned_start_address, size); |
| tvendov | 0:e1f465d87307 | 426 | return &bd; |
| tvendov | 0:e1f465d87307 | 427 | |
| tvendov | 0:e1f465d87307 | 428 | #else |
| tvendov | 0:e1f465d87307 | 429 | return NULL; |
| tvendov | 0:e1f465d87307 | 430 | #endif |
| tvendov | 0:e1f465d87307 | 431 | } |
| tvendov | 0:e1f465d87307 | 432 | |
| tvendov | 0:e1f465d87307 | 433 | BlockDevice *_get_blockdevice_SPIF(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 434 | { |
| tvendov | 0:e1f465d87307 | 435 | #if COMPONENT_SPIF |
| tvendov | 0:e1f465d87307 | 436 | |
| tvendov | 0:e1f465d87307 | 437 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 438 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 439 | |
| tvendov | 0:e1f465d87307 | 440 | static SPIFBlockDevice bd( |
| tvendov | 0:e1f465d87307 | 441 | MBED_CONF_SPIF_DRIVER_SPI_MOSI, |
| tvendov | 0:e1f465d87307 | 442 | MBED_CONF_SPIF_DRIVER_SPI_MISO, |
| tvendov | 0:e1f465d87307 | 443 | MBED_CONF_SPIF_DRIVER_SPI_CLK, |
| tvendov | 0:e1f465d87307 | 444 | MBED_CONF_SPIF_DRIVER_SPI_CS, |
| tvendov | 0:e1f465d87307 | 445 | MBED_CONF_SPIF_DRIVER_SPI_FREQ |
| tvendov | 0:e1f465d87307 | 446 | ); |
| tvendov | 0:e1f465d87307 | 447 | |
| tvendov | 0:e1f465d87307 | 448 | if (bd.init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 449 | tr_error("KV Config: SPIFBlockDevice init fail"); |
| tvendov | 0:e1f465d87307 | 450 | return NULL; |
| tvendov | 0:e1f465d87307 | 451 | } |
| tvendov | 0:e1f465d87307 | 452 | |
| tvendov | 0:e1f465d87307 | 453 | if (start_address == 0 && size == 0) { |
| tvendov | 0:e1f465d87307 | 454 | return &bd; |
| tvendov | 0:e1f465d87307 | 455 | } |
| tvendov | 0:e1f465d87307 | 456 | |
| tvendov | 0:e1f465d87307 | 457 | //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address. |
| tvendov | 0:e1f465d87307 | 458 | if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) { |
| tvendov | 0:e1f465d87307 | 459 | tr_error("KV Config: Fail to get addresses for SlicingBlockDevice."); |
| tvendov | 0:e1f465d87307 | 460 | return NULL; |
| tvendov | 0:e1f465d87307 | 461 | } |
| tvendov | 0:e1f465d87307 | 462 | |
| tvendov | 0:e1f465d87307 | 463 | static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address); |
| tvendov | 0:e1f465d87307 | 464 | return &sbd; |
| tvendov | 0:e1f465d87307 | 465 | |
| tvendov | 0:e1f465d87307 | 466 | #else |
| tvendov | 0:e1f465d87307 | 467 | return NULL; |
| tvendov | 0:e1f465d87307 | 468 | #endif |
| tvendov | 0:e1f465d87307 | 469 | } |
| tvendov | 0:e1f465d87307 | 470 | |
| tvendov | 0:e1f465d87307 | 471 | BlockDevice *_get_blockdevice_QSPIF(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 472 | { |
| tvendov | 0:e1f465d87307 | 473 | #if COMPONENT_QSPIF |
| tvendov | 0:e1f465d87307 | 474 | |
| tvendov | 0:e1f465d87307 | 475 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 476 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 477 | |
| tvendov | 0:e1f465d87307 | 478 | static QSPIFBlockDevice bd( |
| tvendov | 0:e1f465d87307 | 479 | QSPI_FLASH1_IO0, |
| tvendov | 0:e1f465d87307 | 480 | QSPI_FLASH1_IO1, |
| tvendov | 0:e1f465d87307 | 481 | QSPI_FLASH1_IO2, |
| tvendov | 0:e1f465d87307 | 482 | QSPI_FLASH1_IO3, |
| tvendov | 0:e1f465d87307 | 483 | QSPI_FLASH1_SCK, |
| tvendov | 0:e1f465d87307 | 484 | QSPI_FLASH1_CSN, |
| tvendov | 0:e1f465d87307 | 485 | QSPIF_POLARITY_MODE_0, |
| tvendov | 0:e1f465d87307 | 486 | MBED_CONF_QSPIF_QSPI_FREQ |
| tvendov | 0:e1f465d87307 | 487 | ); |
| tvendov | 0:e1f465d87307 | 488 | |
| tvendov | 0:e1f465d87307 | 489 | if (bd.init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 490 | tr_error("KV Config: QSPIFBlockDevice init fail"); |
| tvendov | 0:e1f465d87307 | 491 | return NULL; |
| tvendov | 0:e1f465d87307 | 492 | } |
| tvendov | 0:e1f465d87307 | 493 | |
| tvendov | 0:e1f465d87307 | 494 | if (start_address == 0 && size == 0) { |
| tvendov | 0:e1f465d87307 | 495 | return &bd; |
| tvendov | 0:e1f465d87307 | 496 | } |
| tvendov | 0:e1f465d87307 | 497 | |
| tvendov | 0:e1f465d87307 | 498 | //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address. |
| tvendov | 0:e1f465d87307 | 499 | if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) { |
| tvendov | 0:e1f465d87307 | 500 | tr_error("KV Config: Fail to get addresses for SlicingBlockDevice."); |
| tvendov | 0:e1f465d87307 | 501 | return NULL; |
| tvendov | 0:e1f465d87307 | 502 | } |
| tvendov | 0:e1f465d87307 | 503 | |
| tvendov | 0:e1f465d87307 | 504 | static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address); |
| tvendov | 0:e1f465d87307 | 505 | return &sbd; |
| tvendov | 0:e1f465d87307 | 506 | |
| tvendov | 0:e1f465d87307 | 507 | #else |
| tvendov | 0:e1f465d87307 | 508 | return NULL; |
| tvendov | 0:e1f465d87307 | 509 | #endif |
| tvendov | 0:e1f465d87307 | 510 | } |
| tvendov | 0:e1f465d87307 | 511 | |
| tvendov | 0:e1f465d87307 | 512 | BlockDevice *_get_blockdevice_DATAFLASH(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 513 | { |
| tvendov | 0:e1f465d87307 | 514 | #if COMPONENT_DATAFLASH |
| tvendov | 0:e1f465d87307 | 515 | |
| tvendov | 0:e1f465d87307 | 516 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 517 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 518 | |
| tvendov | 0:e1f465d87307 | 519 | static DataFlashBlockDevice bd( |
| tvendov | 0:e1f465d87307 | 520 | MBED_CONF_DATAFLASH_SPI_MOSI, |
| tvendov | 0:e1f465d87307 | 521 | MBED_CONF_DATAFLASH_SPI_MISO, |
| tvendov | 0:e1f465d87307 | 522 | MBED_CONF_DATAFLASH_SPI_CLK, |
| tvendov | 0:e1f465d87307 | 523 | MBED_CONF_DATAFLASH_SPI_CS |
| tvendov | 0:e1f465d87307 | 524 | ); |
| tvendov | 0:e1f465d87307 | 525 | |
| tvendov | 0:e1f465d87307 | 526 | if (bd.init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 527 | tr_error("KV Config: DataFlashBlockDevice init fail"); |
| tvendov | 0:e1f465d87307 | 528 | return NULL; |
| tvendov | 0:e1f465d87307 | 529 | } |
| tvendov | 0:e1f465d87307 | 530 | |
| tvendov | 0:e1f465d87307 | 531 | if (start_address == 0 && size == 0) { |
| tvendov | 0:e1f465d87307 | 532 | return &bd; |
| tvendov | 0:e1f465d87307 | 533 | } |
| tvendov | 0:e1f465d87307 | 534 | |
| tvendov | 0:e1f465d87307 | 535 | //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address. |
| tvendov | 0:e1f465d87307 | 536 | if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) { |
| tvendov | 0:e1f465d87307 | 537 | tr_error("KV Config: Fail to get addresses for SlicingBlockDevice."); |
| tvendov | 0:e1f465d87307 | 538 | return NULL; |
| tvendov | 0:e1f465d87307 | 539 | } |
| tvendov | 0:e1f465d87307 | 540 | |
| tvendov | 0:e1f465d87307 | 541 | static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address); |
| tvendov | 0:e1f465d87307 | 542 | return &sbd; |
| tvendov | 0:e1f465d87307 | 543 | |
| tvendov | 0:e1f465d87307 | 544 | |
| tvendov | 0:e1f465d87307 | 545 | #else |
| tvendov | 0:e1f465d87307 | 546 | return NULL; |
| tvendov | 0:e1f465d87307 | 547 | #endif |
| tvendov | 0:e1f465d87307 | 548 | } |
| tvendov | 0:e1f465d87307 | 549 | |
| tvendov | 0:e1f465d87307 | 550 | BlockDevice *_get_blockdevice_SD(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 551 | { |
| tvendov | 0:e1f465d87307 | 552 | #if COMPONENT_SD |
| tvendov | 0:e1f465d87307 | 553 | |
| tvendov | 0:e1f465d87307 | 554 | bd_addr_t aligned_end_address; |
| tvendov | 0:e1f465d87307 | 555 | bd_addr_t aligned_start_address; |
| tvendov | 0:e1f465d87307 | 556 | |
| tvendov | 0:e1f465d87307 | 557 | #if (defined(TARGET_VK_RZ_A1H) || defined(TARGET_VK_RZ_A1LU)) |
| tvendov | 0:e1f465d87307 | 558 | static SDHIBlockDevice bd( |
| tvendov | 0:e1f465d87307 | 559 | MBED_CONF_SD_SDHI_CH |
| tvendov | 0:e1f465d87307 | 560 | ); |
| tvendov | 0:e1f465d87307 | 561 | #else |
| tvendov | 0:e1f465d87307 | 562 | static SDBlockDevice bd( |
| tvendov | 0:e1f465d87307 | 563 | MBED_CONF_SD_SPI_MOSI, |
| tvendov | 0:e1f465d87307 | 564 | MBED_CONF_SD_SPI_MISO, |
| tvendov | 0:e1f465d87307 | 565 | MBED_CONF_SD_SPI_CLK, |
| tvendov | 0:e1f465d87307 | 566 | MBED_CONF_SD_SPI_CS |
| tvendov | 0:e1f465d87307 | 567 | ); |
| tvendov | 0:e1f465d87307 | 568 | #endif |
| tvendov | 0:e1f465d87307 | 569 | |
| tvendov | 0:e1f465d87307 | 570 | if (bd.init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 571 | tr_error("KV Config: SDBlockDevice init fail"); |
| tvendov | 0:e1f465d87307 | 572 | return NULL; |
| tvendov | 0:e1f465d87307 | 573 | } |
| tvendov | 0:e1f465d87307 | 574 | |
| tvendov | 0:e1f465d87307 | 575 | if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 || |
| tvendov | 0:e1f465d87307 | 576 | strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) { |
| tvendov | 0:e1f465d87307 | 577 | //In TDBStore profile, we have a constraint of 4GByte |
| tvendov | 0:e1f465d87307 | 578 | if (start_address == 0 && size == 0 && bd.size() < (uint32_t)(-1)) { |
| tvendov | 0:e1f465d87307 | 579 | return &bd; |
| tvendov | 0:e1f465d87307 | 580 | } |
| tvendov | 0:e1f465d87307 | 581 | |
| tvendov | 0:e1f465d87307 | 582 | //If the size of external storage is bigger than 4G we need to slice it. |
| tvendov | 0:e1f465d87307 | 583 | size = size != 0 ? size : align_down(bd.size(), bd.get_erase_size(bd.size() - 1)); |
| tvendov | 0:e1f465d87307 | 584 | |
| tvendov | 0:e1f465d87307 | 585 | if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) { |
| tvendov | 0:e1f465d87307 | 586 | tr_error("KV Config: Fail to get addresses for SlicingBlockDevice."); |
| tvendov | 0:e1f465d87307 | 587 | return NULL; |
| tvendov | 0:e1f465d87307 | 588 | } |
| tvendov | 0:e1f465d87307 | 589 | |
| tvendov | 0:e1f465d87307 | 590 | if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) { |
| tvendov | 0:e1f465d87307 | 591 | aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only |
| tvendov | 0:e1f465d87307 | 592 | } |
| tvendov | 0:e1f465d87307 | 593 | } else { |
| tvendov | 0:e1f465d87307 | 594 | //For all other KVStore profiles beside TDBStore we take the entire external memory space. |
| tvendov | 0:e1f465d87307 | 595 | if (start_address == 0 && size == 0) { |
| tvendov | 0:e1f465d87307 | 596 | return &bd; |
| tvendov | 0:e1f465d87307 | 597 | } |
| tvendov | 0:e1f465d87307 | 598 | |
| tvendov | 0:e1f465d87307 | 599 | if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) { |
| tvendov | 0:e1f465d87307 | 600 | tr_error("KV Config: Fail to get addresses for SlicingBlockDevice."); |
| tvendov | 0:e1f465d87307 | 601 | return NULL; |
| tvendov | 0:e1f465d87307 | 602 | } |
| tvendov | 0:e1f465d87307 | 603 | } |
| tvendov | 0:e1f465d87307 | 604 | |
| tvendov | 0:e1f465d87307 | 605 | aligned_end_address = align_down(aligned_end_address, bd.get_erase_size(aligned_end_address)); |
| tvendov | 0:e1f465d87307 | 606 | static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address); |
| tvendov | 0:e1f465d87307 | 607 | return &sbd; |
| tvendov | 0:e1f465d87307 | 608 | |
| tvendov | 0:e1f465d87307 | 609 | #else |
| tvendov | 0:e1f465d87307 | 610 | return NULL; |
| tvendov | 0:e1f465d87307 | 611 | #endif |
| tvendov | 0:e1f465d87307 | 612 | } |
| tvendov | 0:e1f465d87307 | 613 | |
| tvendov | 0:e1f465d87307 | 614 | BlockDevice *_get_blockdevice_default(bd_addr_t start_address, bd_size_t size) |
| tvendov | 0:e1f465d87307 | 615 | { |
| tvendov | 0:e1f465d87307 | 616 | #if COMPONENT_QSPIF |
| tvendov | 0:e1f465d87307 | 617 | return _get_blockdevice_QSPIF(start_address, size); |
| tvendov | 0:e1f465d87307 | 618 | #elif COMPONENT_SPIF |
| tvendov | 0:e1f465d87307 | 619 | return _get_blockdevice_SPIF(start_address, size); |
| tvendov | 0:e1f465d87307 | 620 | #elif COMPONENT_DATAFLASH |
| tvendov | 0:e1f465d87307 | 621 | return _get_blockdevice_DATAFLASH(start_address, size); |
| tvendov | 0:e1f465d87307 | 622 | #elif COMPONENT_SD |
| tvendov | 0:e1f465d87307 | 623 | return _get_blockdevice_SD(start_address, size); |
| tvendov | 0:e1f465d87307 | 624 | #else |
| tvendov | 0:e1f465d87307 | 625 | tr_error("KV Config: No default component define in target.json for this target."); |
| tvendov | 0:e1f465d87307 | 626 | return NULL; |
| tvendov | 0:e1f465d87307 | 627 | #endif |
| tvendov | 0:e1f465d87307 | 628 | } |
| tvendov | 0:e1f465d87307 | 629 | |
| tvendov | 0:e1f465d87307 | 630 | int _storage_config_TDB_INTERNAL() |
| tvendov | 0:e1f465d87307 | 631 | { |
| tvendov | 0:e1f465d87307 | 632 | #if COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 633 | bd_size_t internal_size = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 634 | bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 635 | |
| tvendov | 0:e1f465d87307 | 636 | if (internal_size == 0 && internal_start_address == 0) { |
| tvendov | 0:e1f465d87307 | 637 | //Calculate the block device size and start address in case default values are used. |
| tvendov | 0:e1f465d87307 | 638 | if (_get_flashiap_bd_default_addresses_tdb_internal(&internal_start_address, &internal_size) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 639 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 640 | } |
| tvendov | 0:e1f465d87307 | 641 | } |
| tvendov | 0:e1f465d87307 | 642 | |
| tvendov | 0:e1f465d87307 | 643 | //Get internal memory FLASHIAP block device. |
| tvendov | 0:e1f465d87307 | 644 | kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_size); |
| tvendov | 0:e1f465d87307 | 645 | if (kvstore_config.internal_bd == NULL) { |
| tvendov | 0:e1f465d87307 | 646 | tr_error("KV Config: Fail to get internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 647 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 648 | } |
| tvendov | 0:e1f465d87307 | 649 | |
| tvendov | 0:e1f465d87307 | 650 | |
| tvendov | 0:e1f465d87307 | 651 | int ret = kvstore_config.internal_bd->init(); |
| tvendov | 0:e1f465d87307 | 652 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 653 | tr_error("KV Config: Fail to init internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 654 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 655 | } |
| tvendov | 0:e1f465d87307 | 656 | |
| tvendov | 0:e1f465d87307 | 657 | //Check that internal flash has 2 or more sectors |
| tvendov | 0:e1f465d87307 | 658 | if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 659 | tr_error("KV Config: Can not create TDBStore with less then 2 sector."); |
| tvendov | 0:e1f465d87307 | 660 | return MBED_ERROR_INVALID_ARGUMENT; |
| tvendov | 0:e1f465d87307 | 661 | } |
| tvendov | 0:e1f465d87307 | 662 | |
| tvendov | 0:e1f465d87307 | 663 | //Deinitialize internal block device and TDB will reinitialize and take control on it. |
| tvendov | 0:e1f465d87307 | 664 | ret = kvstore_config.internal_bd->deinit(); |
| tvendov | 0:e1f465d87307 | 665 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 666 | tr_error("KV Config: Fail to deinit internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 667 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 668 | } |
| tvendov | 0:e1f465d87307 | 669 | |
| tvendov | 0:e1f465d87307 | 670 | //Create a TDBStore in the internal FLASHIAP block device. |
| tvendov | 0:e1f465d87307 | 671 | static TDBStore tdb_internal(kvstore_config.internal_bd); |
| tvendov | 0:e1f465d87307 | 672 | kvstore_config.internal_store = &tdb_internal; |
| tvendov | 0:e1f465d87307 | 673 | |
| tvendov | 0:e1f465d87307 | 674 | ret = kvstore_config.internal_store->init(); |
| tvendov | 0:e1f465d87307 | 675 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 676 | tr_error("KV Config: Fail to init internal TDBStore."); |
| tvendov | 0:e1f465d87307 | 677 | return ret; |
| tvendov | 0:e1f465d87307 | 678 | } |
| tvendov | 0:e1f465d87307 | 679 | kvstore_config.kvstore_main_instance = |
| tvendov | 0:e1f465d87307 | 680 | kvstore_config.internal_store; |
| tvendov | 0:e1f465d87307 | 681 | |
| tvendov | 0:e1f465d87307 | 682 | //Masking flag - Actually used to remove any KVStore flag which is not supported |
| tvendov | 0:e1f465d87307 | 683 | //in the chosen KVStore profile. |
| tvendov | 0:e1f465d87307 | 684 | kvstore_config.flags_mask = ~(KVStore::REQUIRE_CONFIDENTIALITY_FLAG | |
| tvendov | 0:e1f465d87307 | 685 | KVStore::REQUIRE_REPLAY_PROTECTION_FLAG); |
| tvendov | 0:e1f465d87307 | 686 | |
| tvendov | 0:e1f465d87307 | 687 | //Initialize kv_map and add the configuration struct to KVStore map. |
| tvendov | 0:e1f465d87307 | 688 | KVMap &kv_map = KVMap::get_instance(); |
| tvendov | 0:e1f465d87307 | 689 | ret = kv_map.init(); |
| tvendov | 0:e1f465d87307 | 690 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 691 | tr_error("KV Config: Fail to init KVStore global API."); |
| tvendov | 0:e1f465d87307 | 692 | return ret; |
| tvendov | 0:e1f465d87307 | 693 | } |
| tvendov | 0:e1f465d87307 | 694 | |
| tvendov | 0:e1f465d87307 | 695 | ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config); |
| tvendov | 0:e1f465d87307 | 696 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 697 | tr_error("KV Config: Fail to attach KVStore main instance to KVStore global API."); |
| tvendov | 0:e1f465d87307 | 698 | return ret; |
| tvendov | 0:e1f465d87307 | 699 | } |
| tvendov | 0:e1f465d87307 | 700 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 701 | #else |
| tvendov | 0:e1f465d87307 | 702 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 703 | #endif |
| tvendov | 0:e1f465d87307 | 704 | |
| tvendov | 0:e1f465d87307 | 705 | } |
| tvendov | 0:e1f465d87307 | 706 | |
| tvendov | 0:e1f465d87307 | 707 | int _storage_config_TDB_EXTERNAL() |
| tvendov | 0:e1f465d87307 | 708 | { |
| tvendov | 0:e1f465d87307 | 709 | #if !SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 710 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 711 | #endif |
| tvendov | 0:e1f465d87307 | 712 | |
| tvendov | 0:e1f465d87307 | 713 | bd_size_t internal_rbp_size = MBED_CONF_STORAGE_TDB_EXTERNAL_RBP_INTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 714 | bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_EXTERNAL_INTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 715 | |
| tvendov | 0:e1f465d87307 | 716 | //Get the default address and size for internal rbp TDBStore |
| tvendov | 0:e1f465d87307 | 717 | if (internal_rbp_size == 0 && internal_start_address == 0) { |
| tvendov | 0:e1f465d87307 | 718 | //Calculate the block device size and start address in case default values are used. |
| tvendov | 0:e1f465d87307 | 719 | if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 720 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 721 | } |
| tvendov | 0:e1f465d87307 | 722 | } |
| tvendov | 0:e1f465d87307 | 723 | |
| tvendov | 0:e1f465d87307 | 724 | //Create internal FLASHIAP block device |
| tvendov | 0:e1f465d87307 | 725 | kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size); |
| tvendov | 0:e1f465d87307 | 726 | if (kvstore_config.internal_bd == NULL) { |
| tvendov | 0:e1f465d87307 | 727 | tr_error("KV Config: Fail to get internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 728 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 729 | } |
| tvendov | 0:e1f465d87307 | 730 | |
| tvendov | 0:e1f465d87307 | 731 | int ret = kvstore_config.internal_bd->init(); |
| tvendov | 0:e1f465d87307 | 732 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 733 | tr_error("KV Config: Fail to init internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 734 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 735 | } |
| tvendov | 0:e1f465d87307 | 736 | |
| tvendov | 0:e1f465d87307 | 737 | //Check if TDBStore has at least 2 sector. |
| tvendov | 0:e1f465d87307 | 738 | if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 739 | tr_error("KV Config: Can not create TDBStore with less then 2 sector."); |
| tvendov | 0:e1f465d87307 | 740 | return MBED_ERROR_INVALID_ARGUMENT; |
| tvendov | 0:e1f465d87307 | 741 | } |
| tvendov | 0:e1f465d87307 | 742 | |
| tvendov | 0:e1f465d87307 | 743 | //Create internal TDBStore |
| tvendov | 0:e1f465d87307 | 744 | static TDBStore tdb_internal(kvstore_config.internal_bd); |
| tvendov | 0:e1f465d87307 | 745 | kvstore_config.internal_store = &tdb_internal; |
| tvendov | 0:e1f465d87307 | 746 | |
| tvendov | 0:e1f465d87307 | 747 | ret = kvstore_config.internal_store->init(); |
| tvendov | 0:e1f465d87307 | 748 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 749 | tr_error("KV Config: Fail to init internal TDBStore."); |
| tvendov | 0:e1f465d87307 | 750 | return ret; |
| tvendov | 0:e1f465d87307 | 751 | } |
| tvendov | 0:e1f465d87307 | 752 | |
| tvendov | 0:e1f465d87307 | 753 | bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 754 | bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 755 | |
| tvendov | 0:e1f465d87307 | 756 | //Get external BlockDevice for TDBStore |
| tvendov | 0:e1f465d87307 | 757 | BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE, address, size); |
| tvendov | 0:e1f465d87307 | 758 | if (bd == NULL) { |
| tvendov | 0:e1f465d87307 | 759 | tr_error("KV Config: Fail to get external BlockDevice."); |
| tvendov | 0:e1f465d87307 | 760 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 761 | } |
| tvendov | 0:e1f465d87307 | 762 | |
| tvendov | 0:e1f465d87307 | 763 | //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD |
| tvendov | 0:e1f465d87307 | 764 | //add FlashSimBlockDevice on top of the SDBlockDevice |
| tvendov | 0:e1f465d87307 | 765 | #if defined(COMPONENT_SD) |
| tvendov | 0:e1f465d87307 | 766 | if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "SD") == 0 |
| tvendov | 0:e1f465d87307 | 767 | #if defined(COMPONENT_SD) && !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH) |
| tvendov | 0:e1f465d87307 | 768 | || strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "default") == 0) { |
| tvendov | 0:e1f465d87307 | 769 | #else |
| tvendov | 0:e1f465d87307 | 770 | ) { |
| tvendov | 0:e1f465d87307 | 771 | |
| tvendov | 0:e1f465d87307 | 772 | #endif |
| tvendov | 0:e1f465d87307 | 773 | //TDBStore need FlashSimBlockDevice when working with SD block device |
| tvendov | 0:e1f465d87307 | 774 | if (bd->init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 775 | tr_error("KV Config: Fail to init external BlockDevice."); |
| tvendov | 0:e1f465d87307 | 776 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 777 | } |
| tvendov | 0:e1f465d87307 | 778 | |
| tvendov | 0:e1f465d87307 | 779 | static FlashSimBlockDevice flash_bd(bd); |
| tvendov | 0:e1f465d87307 | 780 | kvstore_config.external_bd = &flash_bd; |
| tvendov | 0:e1f465d87307 | 781 | } else { |
| tvendov | 0:e1f465d87307 | 782 | kvstore_config.external_bd = bd; |
| tvendov | 0:e1f465d87307 | 783 | } |
| tvendov | 0:e1f465d87307 | 784 | #else |
| tvendov | 0:e1f465d87307 | 785 | kvstore_config.external_bd = bd; |
| tvendov | 0:e1f465d87307 | 786 | #endif |
| tvendov | 0:e1f465d87307 | 787 | |
| tvendov | 0:e1f465d87307 | 788 | kvstore_config.flags_mask = ~(0); |
| tvendov | 0:e1f465d87307 | 789 | |
| tvendov | 0:e1f465d87307 | 790 | return _storage_config_tdb_external_common(); |
| tvendov | 0:e1f465d87307 | 791 | } |
| tvendov | 0:e1f465d87307 | 792 | |
| tvendov | 0:e1f465d87307 | 793 | int _storage_config_TDB_EXTERNAL_NO_RBP() |
| tvendov | 0:e1f465d87307 | 794 | { |
| tvendov | 0:e1f465d87307 | 795 | #if !SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 796 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 797 | #endif |
| tvendov | 0:e1f465d87307 | 798 | bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 799 | bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 800 | |
| tvendov | 0:e1f465d87307 | 801 | //Get external block device |
| tvendov | 0:e1f465d87307 | 802 | BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE, address, size); |
| tvendov | 0:e1f465d87307 | 803 | if (bd == NULL) { |
| tvendov | 0:e1f465d87307 | 804 | tr_error("KV Config: Fail to get external BlockDevice."); |
| tvendov | 0:e1f465d87307 | 805 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 806 | } |
| tvendov | 0:e1f465d87307 | 807 | |
| tvendov | 0:e1f465d87307 | 808 | //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD |
| tvendov | 0:e1f465d87307 | 809 | //add FlashSimBlockDevice on top of the SDBlockDevice |
| tvendov | 0:e1f465d87307 | 810 | #if defined(COMPONENT_SD) |
| tvendov | 0:e1f465d87307 | 811 | if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "SD") == 0 |
| tvendov | 0:e1f465d87307 | 812 | #if defined(COMPONENT_SD) && !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH) |
| tvendov | 0:e1f465d87307 | 813 | || strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "default") == 0) { |
| tvendov | 0:e1f465d87307 | 814 | #else |
| tvendov | 0:e1f465d87307 | 815 | ) { |
| tvendov | 0:e1f465d87307 | 816 | |
| tvendov | 0:e1f465d87307 | 817 | #endif |
| tvendov | 0:e1f465d87307 | 818 | //TDBStore need FlashSimBlockDevice when working with SD block device |
| tvendov | 0:e1f465d87307 | 819 | if (bd->init() != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 820 | tr_error("KV Config: Fail to init external BlockDevice."); |
| tvendov | 0:e1f465d87307 | 821 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 822 | } |
| tvendov | 0:e1f465d87307 | 823 | |
| tvendov | 0:e1f465d87307 | 824 | static FlashSimBlockDevice flash_bd(bd); |
| tvendov | 0:e1f465d87307 | 825 | kvstore_config.external_bd = &flash_bd; |
| tvendov | 0:e1f465d87307 | 826 | } else { |
| tvendov | 0:e1f465d87307 | 827 | kvstore_config.external_bd = bd; |
| tvendov | 0:e1f465d87307 | 828 | } |
| tvendov | 0:e1f465d87307 | 829 | #else |
| tvendov | 0:e1f465d87307 | 830 | kvstore_config.external_bd = bd; |
| tvendov | 0:e1f465d87307 | 831 | #endif |
| tvendov | 0:e1f465d87307 | 832 | |
| tvendov | 0:e1f465d87307 | 833 | //Masking flag - Actually used to remove any KVStore flag which is not supported |
| tvendov | 0:e1f465d87307 | 834 | //in the chosen KVStore profile. |
| tvendov | 0:e1f465d87307 | 835 | kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG); |
| tvendov | 0:e1f465d87307 | 836 | |
| tvendov | 0:e1f465d87307 | 837 | return _storage_config_tdb_external_common(); |
| tvendov | 0:e1f465d87307 | 838 | } |
| tvendov | 0:e1f465d87307 | 839 | |
| tvendov | 0:e1f465d87307 | 840 | int _storage_config_tdb_external_common() |
| tvendov | 0:e1f465d87307 | 841 | { |
| tvendov | 0:e1f465d87307 | 842 | #if SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 843 | //Initialize external block device |
| tvendov | 0:e1f465d87307 | 844 | int ret = kvstore_config.external_bd->init(); |
| tvendov | 0:e1f465d87307 | 845 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 846 | tr_error("KV Config: Fail to init external BlockDevice."); |
| tvendov | 0:e1f465d87307 | 847 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 848 | } |
| tvendov | 0:e1f465d87307 | 849 | |
| tvendov | 0:e1f465d87307 | 850 | //Check that there is at least 2 sector for the external TDBStore |
| tvendov | 0:e1f465d87307 | 851 | if (_calculate_blocksize_match_tdbstore(kvstore_config.external_bd) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 852 | tr_error("KV Config: Can not create TDBStore with less then 2 sector."); |
| tvendov | 0:e1f465d87307 | 853 | return MBED_ERROR_INVALID_SIZE; |
| tvendov | 0:e1f465d87307 | 854 | } |
| tvendov | 0:e1f465d87307 | 855 | |
| tvendov | 0:e1f465d87307 | 856 | //Create external TDBStore |
| tvendov | 0:e1f465d87307 | 857 | static TDBStore tdb_external(kvstore_config.external_bd); |
| tvendov | 0:e1f465d87307 | 858 | kvstore_config.external_store = &tdb_external; |
| tvendov | 0:e1f465d87307 | 859 | |
| tvendov | 0:e1f465d87307 | 860 | //Create SecureStore and initialize it |
| tvendov | 0:e1f465d87307 | 861 | static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store); |
| tvendov | 0:e1f465d87307 | 862 | |
| tvendov | 0:e1f465d87307 | 863 | ret = secst.init(); |
| tvendov | 0:e1f465d87307 | 864 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 865 | tr_error("KV Config: Fail to init SecureStore."); |
| tvendov | 0:e1f465d87307 | 866 | return ret ; |
| tvendov | 0:e1f465d87307 | 867 | } |
| tvendov | 0:e1f465d87307 | 868 | |
| tvendov | 0:e1f465d87307 | 869 | kvstore_config.kvstore_main_instance = &secst; |
| tvendov | 0:e1f465d87307 | 870 | |
| tvendov | 0:e1f465d87307 | 871 | //Init kv_map and add the configuration struct to KVStore map. |
| tvendov | 0:e1f465d87307 | 872 | KVMap &kv_map = KVMap::get_instance(); |
| tvendov | 0:e1f465d87307 | 873 | ret = kv_map.init(); |
| tvendov | 0:e1f465d87307 | 874 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 875 | tr_error("KV Config: Fail to init KVStore global API"); |
| tvendov | 0:e1f465d87307 | 876 | return ret; |
| tvendov | 0:e1f465d87307 | 877 | } |
| tvendov | 0:e1f465d87307 | 878 | |
| tvendov | 0:e1f465d87307 | 879 | ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config); |
| tvendov | 0:e1f465d87307 | 880 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 881 | tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API"); |
| tvendov | 0:e1f465d87307 | 882 | return ret; |
| tvendov | 0:e1f465d87307 | 883 | } |
| tvendov | 0:e1f465d87307 | 884 | |
| tvendov | 0:e1f465d87307 | 885 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 886 | #else |
| tvendov | 0:e1f465d87307 | 887 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 888 | #endif |
| tvendov | 0:e1f465d87307 | 889 | } |
| tvendov | 0:e1f465d87307 | 890 | |
| tvendov | 0:e1f465d87307 | 891 | int _storage_config_FILESYSTEM() |
| tvendov | 0:e1f465d87307 | 892 | { |
| tvendov | 0:e1f465d87307 | 893 | #if !SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 894 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 895 | #endif |
| tvendov | 0:e1f465d87307 | 896 | |
| tvendov | 0:e1f465d87307 | 897 | filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_FOLDER_PATH); |
| tvendov | 0:e1f465d87307 | 898 | |
| tvendov | 0:e1f465d87307 | 899 | bd_size_t internal_rbp_size = MBED_CONF_STORAGE_FILESYSTEM_RBP_INTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 900 | bd_addr_t internal_start_address = MBED_CONF_STORAGE_FILESYSTEM_INTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 901 | |
| tvendov | 0:e1f465d87307 | 902 | //Get the default address and size for internal rbp TDBStore |
| tvendov | 0:e1f465d87307 | 903 | if (internal_rbp_size == 0 && internal_start_address == 0) { |
| tvendov | 0:e1f465d87307 | 904 | //Calculate the block device size and start address in case default values are used. |
| tvendov | 0:e1f465d87307 | 905 | if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 906 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 907 | } |
| tvendov | 0:e1f465d87307 | 908 | } |
| tvendov | 0:e1f465d87307 | 909 | |
| tvendov | 0:e1f465d87307 | 910 | //Get internal FLASHIAP block device |
| tvendov | 0:e1f465d87307 | 911 | kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size); |
| tvendov | 0:e1f465d87307 | 912 | if (kvstore_config.internal_bd == NULL) { |
| tvendov | 0:e1f465d87307 | 913 | tr_error("KV Config: Fail to get internal BlockDevice "); |
| tvendov | 0:e1f465d87307 | 914 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 915 | } |
| tvendov | 0:e1f465d87307 | 916 | |
| tvendov | 0:e1f465d87307 | 917 | int ret = kvstore_config.internal_bd->init(); |
| tvendov | 0:e1f465d87307 | 918 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 919 | tr_error("KV Config: Fail to init internal BlockDevice "); |
| tvendov | 0:e1f465d87307 | 920 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 921 | } |
| tvendov | 0:e1f465d87307 | 922 | |
| tvendov | 0:e1f465d87307 | 923 | //Check that internal flash has 2 or more sectors |
| tvendov | 0:e1f465d87307 | 924 | if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 925 | tr_error("KV Config: Can not create TDBStore with less then 2 sector."); |
| tvendov | 0:e1f465d87307 | 926 | return MBED_ERROR_INVALID_ARGUMENT; |
| tvendov | 0:e1f465d87307 | 927 | } |
| tvendov | 0:e1f465d87307 | 928 | |
| tvendov | 0:e1f465d87307 | 929 | //Deinitialize internal block device and TDB will reinitialize and take control on it. |
| tvendov | 0:e1f465d87307 | 930 | ret = kvstore_config.internal_bd->deinit(); |
| tvendov | 0:e1f465d87307 | 931 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 932 | tr_error("KV Config: Fail to deinit internal BlockDevice."); |
| tvendov | 0:e1f465d87307 | 933 | return MBED_ERROR_FAILED_OPERATION; |
| tvendov | 0:e1f465d87307 | 934 | } |
| tvendov | 0:e1f465d87307 | 935 | |
| tvendov | 0:e1f465d87307 | 936 | //Create internal TDBStore for rbp |
| tvendov | 0:e1f465d87307 | 937 | static TDBStore tdb_internal(kvstore_config.internal_bd); |
| tvendov | 0:e1f465d87307 | 938 | kvstore_config.internal_store = &tdb_internal; |
| tvendov | 0:e1f465d87307 | 939 | |
| tvendov | 0:e1f465d87307 | 940 | ret = kvstore_config.internal_store->init(); |
| tvendov | 0:e1f465d87307 | 941 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 942 | tr_error("KV Config: Fail to init internal TDBStore"); |
| tvendov | 0:e1f465d87307 | 943 | return ret; |
| tvendov | 0:e1f465d87307 | 944 | } |
| tvendov | 0:e1f465d87307 | 945 | |
| tvendov | 0:e1f465d87307 | 946 | bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 947 | bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 948 | const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_MOUNT_POINT); |
| tvendov | 0:e1f465d87307 | 949 | |
| tvendov | 0:e1f465d87307 | 950 | //Get external block device for FileSystem. |
| tvendov | 0:e1f465d87307 | 951 | kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_BLOCKDEVICE, address, size); |
| tvendov | 0:e1f465d87307 | 952 | if (kvstore_config.external_bd == NULL) { |
| tvendov | 0:e1f465d87307 | 953 | tr_error("KV Config: Fail to get external BlockDevice "); |
| tvendov | 0:e1f465d87307 | 954 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 955 | } |
| tvendov | 0:e1f465d87307 | 956 | |
| tvendov | 0:e1f465d87307 | 957 | ret = kvstore_config.external_bd->init(); |
| tvendov | 0:e1f465d87307 | 958 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 959 | tr_error("KV Config: Fail to init external BlockDevice "); |
| tvendov | 0:e1f465d87307 | 960 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 961 | } |
| tvendov | 0:e1f465d87307 | 962 | |
| tvendov | 0:e1f465d87307 | 963 | //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default |
| tvendov | 0:e1f465d87307 | 964 | //component block device configured in the system. The priority is: |
| tvendov | 0:e1f465d87307 | 965 | //QSPI -> SPI -> DATAFLASH == LITTLE |
| tvendov | 0:e1f465d87307 | 966 | //SD == FAT |
| tvendov | 0:e1f465d87307 | 967 | kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_FILESYSTEM, mount_point); |
| tvendov | 0:e1f465d87307 | 968 | if (kvstore_config.external_fs == NULL) { |
| tvendov | 0:e1f465d87307 | 969 | tr_error("KV Config: Fail to get FileSystem"); |
| tvendov | 0:e1f465d87307 | 970 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 971 | } |
| tvendov | 0:e1f465d87307 | 972 | |
| tvendov | 0:e1f465d87307 | 973 | kvstore_config.flags_mask = ~(0); |
| tvendov | 0:e1f465d87307 | 974 | |
| tvendov | 0:e1f465d87307 | 975 | return _storage_config_filesystem_common(); |
| tvendov | 0:e1f465d87307 | 976 | } |
| tvendov | 0:e1f465d87307 | 977 | |
| tvendov | 0:e1f465d87307 | 978 | int _storage_config_FILESYSTEM_NO_RBP() |
| tvendov | 0:e1f465d87307 | 979 | { |
| tvendov | 0:e1f465d87307 | 980 | #if !SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 981 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 982 | #endif |
| tvendov | 0:e1f465d87307 | 983 | |
| tvendov | 0:e1f465d87307 | 984 | filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FOLDER_PATH); |
| tvendov | 0:e1f465d87307 | 985 | |
| tvendov | 0:e1f465d87307 | 986 | bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_SIZE; |
| tvendov | 0:e1f465d87307 | 987 | bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_BASE_ADDRESS; |
| tvendov | 0:e1f465d87307 | 988 | const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT); |
| tvendov | 0:e1f465d87307 | 989 | |
| tvendov | 0:e1f465d87307 | 990 | //Get external block device for FileSystem. |
| tvendov | 0:e1f465d87307 | 991 | kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_BLOCKDEVICE, address, size); |
| tvendov | 0:e1f465d87307 | 992 | if (kvstore_config.external_bd == NULL) { |
| tvendov | 0:e1f465d87307 | 993 | tr_error("KV Config: Fail to get external BlockDevice "); |
| tvendov | 0:e1f465d87307 | 994 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 995 | } |
| tvendov | 0:e1f465d87307 | 996 | |
| tvendov | 0:e1f465d87307 | 997 | int ret = kvstore_config.external_bd->init(); |
| tvendov | 0:e1f465d87307 | 998 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 999 | tr_error("KV Config: Fail to init external BlockDevice "); |
| tvendov | 0:e1f465d87307 | 1000 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 1001 | } |
| tvendov | 0:e1f465d87307 | 1002 | |
| tvendov | 0:e1f465d87307 | 1003 | //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default |
| tvendov | 0:e1f465d87307 | 1004 | //component block device configured in the system. The priority is: |
| tvendov | 0:e1f465d87307 | 1005 | //QSPI -> SPI -> DATAFLASH == LITTLE |
| tvendov | 0:e1f465d87307 | 1006 | //SD == FAT |
| tvendov | 0:e1f465d87307 | 1007 | kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FILESYSTEM, mount_point); |
| tvendov | 0:e1f465d87307 | 1008 | if (kvstore_config.external_fs == NULL) { |
| tvendov | 0:e1f465d87307 | 1009 | tr_error("KV Config: Fail to get FileSystem"); |
| tvendov | 0:e1f465d87307 | 1010 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 1011 | } |
| tvendov | 0:e1f465d87307 | 1012 | |
| tvendov | 0:e1f465d87307 | 1013 | //Masking flag - Actually used to remove any KVStore flag which is not supported |
| tvendov | 0:e1f465d87307 | 1014 | //in the chosen KVStore profile. |
| tvendov | 0:e1f465d87307 | 1015 | kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG); |
| tvendov | 0:e1f465d87307 | 1016 | |
| tvendov | 0:e1f465d87307 | 1017 | return _storage_config_filesystem_common(); |
| tvendov | 0:e1f465d87307 | 1018 | } |
| tvendov | 0:e1f465d87307 | 1019 | |
| tvendov | 0:e1f465d87307 | 1020 | int _storage_config_filesystem_common() |
| tvendov | 0:e1f465d87307 | 1021 | { |
| tvendov | 0:e1f465d87307 | 1022 | #if SECURESTORE_ENABLED |
| tvendov | 0:e1f465d87307 | 1023 | |
| tvendov | 0:e1f465d87307 | 1024 | //Mount file system. if it fails, try to reformat |
| tvendov | 0:e1f465d87307 | 1025 | int ret = kvstore_config.external_fs->mount(kvstore_config.external_bd); |
| tvendov | 0:e1f465d87307 | 1026 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 1027 | ret = kvstore_config.external_fs->reformat(kvstore_config.external_bd); |
| tvendov | 0:e1f465d87307 | 1028 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 1029 | tr_error("KV Config: Fail to mount FileSystem to %s", |
| tvendov | 0:e1f465d87307 | 1030 | STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT)); |
| tvendov | 0:e1f465d87307 | 1031 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 1032 | } |
| tvendov | 0:e1f465d87307 | 1033 | } |
| tvendov | 0:e1f465d87307 | 1034 | |
| tvendov | 0:e1f465d87307 | 1035 | //Create FileSystemStore |
| tvendov | 0:e1f465d87307 | 1036 | kvstore_config.external_store = _get_file_system_store(kvstore_config.external_fs); |
| tvendov | 0:e1f465d87307 | 1037 | if (kvstore_config.external_store == NULL) { |
| tvendov | 0:e1f465d87307 | 1038 | tr_error("KV Config: Fail to get FileSystemStore"); |
| tvendov | 0:e1f465d87307 | 1039 | return MBED_ERROR_FAILED_OPERATION ; |
| tvendov | 0:e1f465d87307 | 1040 | } |
| tvendov | 0:e1f465d87307 | 1041 | |
| tvendov | 0:e1f465d87307 | 1042 | //Create SecureStore and set it as main KVStore |
| tvendov | 0:e1f465d87307 | 1043 | static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store); |
| tvendov | 0:e1f465d87307 | 1044 | |
| tvendov | 0:e1f465d87307 | 1045 | ret = secst.init(); |
| tvendov | 0:e1f465d87307 | 1046 | if (ret != MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 1047 | tr_error("KV Config: Fail to init SecureStore."); |
| tvendov | 0:e1f465d87307 | 1048 | return ret; ; |
| tvendov | 0:e1f465d87307 | 1049 | } |
| tvendov | 0:e1f465d87307 | 1050 | |
| tvendov | 0:e1f465d87307 | 1051 | kvstore_config.kvstore_main_instance = &secst; |
| tvendov | 0:e1f465d87307 | 1052 | |
| tvendov | 0:e1f465d87307 | 1053 | //Init kv_map and add the configuration struct to KVStore map. |
| tvendov | 0:e1f465d87307 | 1054 | KVMap &kv_map = KVMap::get_instance(); |
| tvendov | 0:e1f465d87307 | 1055 | ret = kv_map.init(); |
| tvendov | 0:e1f465d87307 | 1056 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 1057 | tr_error("KV Config: Fail to init KVStore global API"); |
| tvendov | 0:e1f465d87307 | 1058 | return ret; |
| tvendov | 0:e1f465d87307 | 1059 | } |
| tvendov | 0:e1f465d87307 | 1060 | |
| tvendov | 0:e1f465d87307 | 1061 | ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config); |
| tvendov | 0:e1f465d87307 | 1062 | if (MBED_SUCCESS != ret) { |
| tvendov | 0:e1f465d87307 | 1063 | tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API"); |
| tvendov | 0:e1f465d87307 | 1064 | return ret; |
| tvendov | 0:e1f465d87307 | 1065 | } |
| tvendov | 0:e1f465d87307 | 1066 | |
| tvendov | 0:e1f465d87307 | 1067 | return MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 1068 | #else |
| tvendov | 0:e1f465d87307 | 1069 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 1070 | #endif |
| tvendov | 0:e1f465d87307 | 1071 | } |
| tvendov | 0:e1f465d87307 | 1072 | |
| tvendov | 0:e1f465d87307 | 1073 | int _storage_config_default() |
| tvendov | 0:e1f465d87307 | 1074 | { |
| tvendov | 0:e1f465d87307 | 1075 | #if COMPONENT_QSPIF || COMPONENT_SPIF || COMPONENT_DATAFLASH |
| tvendov | 0:e1f465d87307 | 1076 | return _storage_config_TDB_EXTERNAL(); |
| tvendov | 0:e1f465d87307 | 1077 | #elif COMPONENT_SD |
| tvendov | 0:e1f465d87307 | 1078 | return _storage_config_FILESYSTEM(); |
| tvendov | 0:e1f465d87307 | 1079 | #elif COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 1080 | return _storage_config_TDB_INTERNAL(); |
| tvendov | 0:e1f465d87307 | 1081 | #else |
| tvendov | 0:e1f465d87307 | 1082 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 1083 | #endif |
| tvendov | 0:e1f465d87307 | 1084 | } |
| tvendov | 0:e1f465d87307 | 1085 | |
| tvendov | 0:e1f465d87307 | 1086 | const char *get_filesystemstore_folder_path() |
| tvendov | 0:e1f465d87307 | 1087 | { |
| tvendov | 0:e1f465d87307 | 1088 | return filesystemstore_folder_path; |
| tvendov | 0:e1f465d87307 | 1089 | } |
| tvendov | 0:e1f465d87307 | 1090 | |
| tvendov | 0:e1f465d87307 | 1091 | MBED_WEAK int kv_init_storage_config() |
| tvendov | 0:e1f465d87307 | 1092 | { |
| tvendov | 0:e1f465d87307 | 1093 | |
| tvendov | 0:e1f465d87307 | 1094 | int ret = MBED_SUCCESS; |
| tvendov | 0:e1f465d87307 | 1095 | |
| tvendov | 0:e1f465d87307 | 1096 | // We currently have no supported configuration without internal storage |
| tvendov | 0:e1f465d87307 | 1097 | #ifndef COMPONENT_FLASHIAP |
| tvendov | 0:e1f465d87307 | 1098 | return MBED_ERROR_UNSUPPORTED; |
| tvendov | 0:e1f465d87307 | 1099 | #endif |
| tvendov | 0:e1f465d87307 | 1100 | |
| tvendov | 0:e1f465d87307 | 1101 | mutex->lock(); |
| tvendov | 0:e1f465d87307 | 1102 | |
| tvendov | 0:e1f465d87307 | 1103 | if (is_kv_config_initialize) { |
| tvendov | 0:e1f465d87307 | 1104 | goto exit; |
| tvendov | 0:e1f465d87307 | 1105 | } |
| tvendov | 0:e1f465d87307 | 1106 | |
| tvendov | 0:e1f465d87307 | 1107 | memset(&kvstore_config, 0, sizeof(kvstore_config_t)); |
| tvendov | 0:e1f465d87307 | 1108 | |
| tvendov | 0:e1f465d87307 | 1109 | ret = _STORAGE_CONFIG(MBED_CONF_STORAGE_STORAGE_TYPE); |
| tvendov | 0:e1f465d87307 | 1110 | |
| tvendov | 0:e1f465d87307 | 1111 | if (ret == MBED_SUCCESS) { |
| tvendov | 0:e1f465d87307 | 1112 | is_kv_config_initialize = true; |
| tvendov | 0:e1f465d87307 | 1113 | } |
| tvendov | 0:e1f465d87307 | 1114 | |
| tvendov | 0:e1f465d87307 | 1115 | exit: |
| tvendov | 0:e1f465d87307 | 1116 | mutex->unlock(); |
| tvendov | 0:e1f465d87307 | 1117 | return ret; |
| tvendov | 0:e1f465d87307 | 1118 | } |
| tvendov | 0:e1f465d87307 | 1119 |