Vekatech / SDHI_driver_variant_II
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers kv_config.cpp Source File

kv_config.cpp

00001 /*
00002  * Copyright (c) 2018 ARM Limited. All rights reserved.
00003  * SPDX-License-Identifier: Apache-2.0
00004  * Licensed under the Apache License, Version 2.0 (the License); you may
00005  * not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
00012  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "kv_config.h"
00018 #include "KVStore.h"
00019 #include "KVMap.h"
00020 #include "BlockDevice.h"
00021 #include "FileSystem.h"
00022 #include "FileSystemStore.h"
00023 #include "SlicingBlockDevice.h"
00024 #include "FATFileSystem.h"
00025 #include "LittleFileSystem.h"
00026 #include "TDBStore.h"
00027 #include "mbed_error.h"
00028 #include "FlashIAP.h"
00029 #include "FlashSimBlockDevice.h"
00030 #include "mbed_trace.h"
00031 #include "SecureStore.h"
00032 #define TRACE_GROUP "KVCFG"
00033 
00034 #if COMPONENT_FLASHIAP
00035 #include "FlashIAPBlockDevice.h"
00036 #endif
00037 
00038 #if COMPONENT_QSPIF
00039 #include "QSPIFBlockDevice.h"
00040 #endif
00041 
00042 #if COMPONENT_SPIF
00043 #include "SPIFBlockDevice.h"
00044 #endif
00045 
00046 #if COMPONENT_DATAFLASH
00047 #include "DataFlashBlockDevice.h"
00048 #endif
00049 
00050 #if COMPONENT_SD
00051  #if (defined(TARGET_VK_RZ_A1H) || defined(TARGET_VK_RZ_A1LU))
00052   #include "SDHIBlockDevice.h"
00053  #else
00054   #include "SDBlockDevice.h"
00055  #endif
00056 #endif
00057 
00058 /**
00059  * @brief This function initializes internal memory secure storage
00060  *        This includes a TDBStore instance with a FlashIAPBlockdevice
00061  *        as the supported storage.
00062  *        The following is a list of configuration parameter
00063  *        MBED_CONF_STORAGE_TDB_INTERNAL_SIZE - The size of the underlying FlashIAPBlockdevice
00064  *        MBED_CONF_STORAGE_TDB_INTERNAL_BASE_ADDRESS - The start address of the underlying FlashIAPBlockdevice
00065  * @returns 0 on success or negative value on failure.
00066  */
00067 int _storage_config_TDB_INTERNAL();
00068 
00069 /**
00070  * @brief This function initialize external memory secure storage
00071  *        This includes a SecureStore class with TDBStore over FlashIAPBlockdevice
00072  *        and an external TDBStore over a default blockdevice unless configured differently.
00073  *        The following is a list of configuration parameter:
00074  *        MBED_CONF_STORAGE_TDB_EXTERNAL_RBP_INTERNAL_SIZE - Size of the internal FlashIAPBlockDevice and by
00075  *                                                           default is set to from start address to the end of the flash.
00076  *                                                           If start address is 0 the start address will be set to end of
00077  *                                                           flash - rbp_internal_size.
00078  *        MBED_CONF_STORAGE_TDB_EXTERNAL_INTERNAL_BASE_ADDRESS - The satrt address of the internal FlashIAPBlockDevice.
00079  *        MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_SIZE - Size of the external blockdevice in bytes or NULL for
00080  *                                                       max possible size.
00081  *        MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BASE_ADDRESS - The block device start address.
00082  *        MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BLOCK_DEVICE - Alowed vlaues are: default, SPIF, DATAFASH, QSPIF or SD
00083  * @returns 0 on success or negative value on failure.
00084  */
00085 int _storage_config_TDB_EXTERNAL();
00086 
00087 /**
00088  * @brief This function initialize a external memory secure storage
00089  *        This includes a SecureStore class with external TDBStore over a blockdevice or,
00090  *        if no blockdevice was set the default blockdevice will be used.
00091  *        The following is a list of configuration parameter:
00092  *        MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_SIZE - Size of the external blockdevice in bytes
00093  *                                                              or NULL for max possible size.
00094  *        MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BASE_ADDRESS - The block device start address
00095  *        MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BLOCK_DEVICE - Alowed vlaues are: default, SPIF, DATAFASH, QSPIF or SD
00096  * @returns 0 on success or negative value on failure.
00097  */
00098 int _storage_config_TDB_EXTERNAL_NO_RBP();
00099 
00100 /**
00101  * @brief This function initialize a FILESYSTEM memory secure storage
00102  *        This includes a SecureStore class with TDBStore over FlashIAPBlockdevice
00103  *        in the internal memory and an external FileSysteStore. If blockdevice and filesystem not set,
00104  *        the system will use the default block device and default filesystem
00105  *        The following is a list of configuration parameter:
00106  *        MBED_CONF_STORAGE_FILESYSTEM_RBP_INTERNAL_SIZE - Size of the internal FlashIAPBlockDevice and by
00107  *                                                         default is set to from start address to the end of the flash.
00108  *                                                         If start address is 0 the start address will be set to end of
00109  *                                                         flash - rbp_internal_size.
00110  *        MBED_CONF_STORAGE_FILESYSTEM_INTERNAL_BASE_ADDRESS - The satrt address of the internal FlashIAPBlockDevice.
00111  *        MBED_CONF_STORAGE_FILESYSTEM_FILESYSTEM - Allowed values are: default, FAT or LITTLE
00112  *        MBED_CONF_STORAGE_FILESYSTEM_BLOCKDEVICE - Allowed values are: default, SPIF, DATAFASH, QSPIF or SD
00113  *        MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_SIZE - External Blockdevice size in bytes or NULL for max possible size.
00114  *        MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_BASE_ADDRESS - The block device start address.
00115  *        MBED_CONF_STORAGE_FILESYSTEM_MOUNT_POINT - Where to mount the filesystem
00116  *        MBED_CONF_STORAGE_FILESYSTEM_FOLDER_PATH - The working folder paths
00117  *
00118  * @returns 0 on success or negative value on failure.
00119  */
00120 int _storage_config_FILESYSTEM();
00121 
00122 /**
00123  * @brief This function initialize a FILESYSTEM_NO_RBP memory secure storage with no
00124  *        rollback protection. This includes a SecureStore class an external FileSysteStore over a default
00125  *        filesystem with default blockdevice unless differently configured.
00126  *        The following is a list of configuration parameter:
00127  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FILESYSTEM - Allowed values are: default, FAT or LITTLE
00128  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_BLOCKDEVICE - Allowed values are: default, SPIF, DATAFASH, QSPIF or SD
00129  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_SIZE - Blockdevice size in bytes. or NULL for max possible size.
00130  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_BASE_ADDRESS - The block device start address.
00131  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT - Where to mount the filesystem
00132  *        MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FOLDER_PATH - The working folder paths
00133  *
00134  * @returns 0 on success or negative value on failure.
00135  */
00136 int _storage_config_FILESYSTEM_NO_RBP();
00137 
00138 int _storage_config_tdb_external_common();
00139 int _storage_config_filesystem_common();
00140 
00141 static const char *filesystemstore_folder_path = NULL;
00142 
00143 using namespace mbed;
00144 
00145 
00146 static SingletonPtr<PlatformMutex> mutex;
00147 static bool is_kv_config_initialize = false;
00148 static kvstore_config_t kvstore_config;
00149 
00150 #define INTERNAL_BLOCKDEVICE_NAME FLASHIAP
00151 
00152 #define STR_EXPAND(tok) #tok
00153 #define STR(tok) STR_EXPAND(tok)
00154 
00155 #define _GET_FILESYSTEM_concat(dev, ...) _get_filesystem_##dev(__VA_ARGS__)
00156 #define GET_FILESYSTEM(dev, ...) _GET_FILESYSTEM_concat(dev, __VA_ARGS__)
00157 
00158 #define _GET_BLOCKDEVICE_concat(dev, ...) _get_blockdevice_##dev(__VA_ARGS__)
00159 #define GET_BLOCKDEVICE(dev, ...) _GET_BLOCKDEVICE_concat(dev, __VA_ARGS__)
00160 
00161 static inline uint32_t align_up(uint64_t val, uint64_t size)
00162 {
00163     return (((val - 1) / size) + 1) * size;
00164 }
00165 
00166 static inline uint32_t align_down(uint64_t val, uint64_t size)
00167 {
00168     return (((val) / size)) * size;
00169 }
00170 
00171 int _calculate_blocksize_match_tdbstore(BlockDevice *bd)
00172 {
00173     bd_size_t size = bd->size();
00174     bd_size_t erase_size = bd->get_erase_size();
00175     bd_size_t number_of_sector = size / erase_size;
00176 
00177     if (number_of_sector < 2) {
00178         tr_warning("KV Config: There are less than two sectors - TDBStore will not work.");
00179         return -1;
00180     }
00181 
00182 
00183     if (number_of_sector % 2 != 0) {
00184         tr_warning("KV Config: Number of sectors is not an even number. Consider changing the BlockDevice size");
00185     }
00186 
00187     return MBED_SUCCESS;
00188 }
00189 
00190 int _get_addresses(BlockDevice *bd, bd_addr_t start_address, bd_size_t size, bd_addr_t *out_start_addr,
00191                    bd_addr_t *out_end_addr)
00192 {
00193     bd_addr_t aligned_end_address;
00194     bd_addr_t end_address;
00195     bd_addr_t aligned_start_address;
00196 
00197     aligned_start_address = align_down(start_address, bd->get_erase_size(start_address));
00198     if (aligned_start_address != start_address) {
00199         tr_error("KV Config: Start address is not aligned. Better use %02llx", aligned_start_address);
00200         return -1;
00201     }
00202 
00203     if (size == 0) {
00204         (*out_start_addr) = aligned_start_address;
00205         (*out_end_addr) = bd->size();
00206         return 0;
00207     }
00208 
00209     end_address = start_address + size;
00210     aligned_end_address = align_up(end_address, bd->get_erase_size(end_address));
00211     if (aligned_end_address != end_address) {
00212         tr_error("KV Config: End address is not aligned. Consider changing the size parameter.");
00213         return -1;
00214     }
00215 
00216     if (aligned_end_address > bd->size()) {
00217         tr_error("KV Config: End address is out of boundaries");
00218         return -1;
00219     }
00220 
00221     (*out_start_addr) = aligned_start_address;
00222     (*out_end_addr) = aligned_end_address;
00223     return 0;
00224 }
00225 
00226 FileSystem *_get_filesystem_FAT(const char *mount)
00227 {
00228     static FATFileSystem sdcard(mount);
00229     return &sdcard;
00230 
00231 }
00232 
00233 FileSystem *_get_filesystem_LITTLE(const char *mount)
00234 {
00235     static LittleFileSystem flash(mount);
00236     return &flash;
00237 }
00238 
00239 FileSystemStore *_get_file_system_store(FileSystem *fs)
00240 {
00241     static FileSystemStore fss(fs);
00242     return &fss;
00243 }
00244 
00245 FileSystem *_get_filesystem_default(const char *mount)
00246 {
00247 #if COMPONENT_QSPIF || COMPONENT_SPIF || COMPONENT_DATAFLASH
00248     return _get_filesystem_LITTLE(mount);
00249 #elif COMPONENT_SD
00250     return _get_filesystem_FAT(mount);
00251 #else
00252     return NULL;
00253 #endif
00254 }
00255 
00256 //Calculates the start address of FLASHIAP block device for TDB_INTERNAL profile.
00257 //If possible, the address will start 2 sectors after the end of code sector allowing
00258 //some space for an application update.
00259 int _get_flashiap_bd_default_addresses_tdb_internal(bd_addr_t *start_address, bd_size_t *size)
00260 {
00261 #if COMPONENT_FLASHIAP
00262 
00263     FlashIAP flash;
00264 
00265     if (*start_address != 0 || *size != 0) {
00266         return MBED_ERROR_INVALID_ARGUMENT;
00267     }
00268 
00269     //If default values are set, we should get the maximum available size of internal bd.
00270     if (flash.init() != 0) {
00271         return MBED_ERROR_FAILED_OPERATION;
00272     }
00273 
00274     *start_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR));
00275 
00276     // Give the application a couple of spare sectors to grow (if there are such)
00277     bd_size_t spare_size_for_app = 0;
00278     bd_addr_t curr_addr = *start_address;
00279     bd_addr_t flash_end_address = flash.get_flash_start() + flash.get_flash_size();
00280 
00281     int spare_sectors_for_app = 2;
00282     int min_sectors_for_storage = 2;
00283     for (int i = 0; i < spare_sectors_for_app + min_sectors_for_storage - 1; i++) {
00284         bd_size_t sector_size = flash.get_sector_size(curr_addr);
00285         curr_addr += sector_size;
00286         if (curr_addr >= flash_end_address) {
00287             spare_size_for_app = 0;
00288             break;
00289         }
00290 
00291         if (i < spare_sectors_for_app) {
00292             spare_size_for_app += sector_size;
00293         }
00294     }
00295     *start_address += spare_size_for_app;
00296 
00297     flash.deinit();
00298 
00299 #endif
00300 
00301     return MBED_SUCCESS;
00302 }
00303 
00304 //Calculates address and size for FLASHIAP block device in TDB_EXTERNAL and FILESYSTEM profiles.
00305 //The size of the block device will be 2 sectors at the ends of the internal flash for
00306 //the use of the rbp internal TDBStore.
00307 int _get_flashiap_bd_default_addresses_rbp(bd_addr_t *start_address, bd_size_t *size)
00308 {
00309 #if COMPONENT_FLASHIAP
00310 
00311     bd_addr_t flash_end_address;
00312     bd_addr_t flash_start_address;
00313     bd_addr_t aligned_start_address;
00314     bd_addr_t flash_first_writable_sector_address;
00315     FlashIAP flash;
00316 
00317     if (*start_address != 0 || *size != 0) {
00318         return MBED_ERROR_INVALID_ARGUMENT;
00319     }
00320 
00321     int ret = flash.init();
00322     if (ret != 0) {
00323         return MBED_ERROR_INITIALIZATION_FAILED;
00324     }
00325 
00326     flash_first_writable_sector_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR));
00327     flash_start_address = flash.get_flash_start();
00328     flash_end_address = flash_start_address + flash.get_flash_size();;
00329     *start_address = flash_end_address - 1;
00330     aligned_start_address = align_down(*start_address, flash.get_sector_size(*start_address));
00331     *size = (flash_end_address - aligned_start_address) * 2;
00332     *start_address = (flash_end_address - *size);
00333     aligned_start_address = align_down(*start_address, flash.get_sector_size(*start_address));
00334 
00335     flash.deinit();
00336 
00337     if (aligned_start_address < flash_first_writable_sector_address) {
00338         tr_error("KV Config: Internal block device start address overlapped ROM address ");
00339         return MBED_ERROR_INITIALIZATION_FAILED;
00340     }
00341 
00342 #endif
00343 
00344     return MBED_SUCCESS;
00345 
00346 }
00347 
00348 BlockDevice *_get_blockdevice_FLASHIAP(bd_addr_t start_address, bd_size_t size)
00349 {
00350 #if COMPONENT_FLASHIAP
00351 
00352     bd_addr_t flash_end_address;
00353     bd_addr_t flash_start_address;
00354     bd_addr_t flash_first_writable_sector_address;
00355     bd_addr_t aligned_start_address;
00356     bd_addr_t aligned_end_address;
00357     bd_addr_t end_address;
00358     FlashIAP flash;
00359 
00360     int ret = flash.init();
00361     if (ret != 0) {
00362         return NULL;
00363     }
00364 
00365     //Get flash parameters before starting
00366     flash_first_writable_sector_address = align_up(FLASHIAP_APP_ROM_END_ADDR, flash.get_sector_size(FLASHIAP_APP_ROM_END_ADDR));
00367     flash_start_address = flash.get_flash_start();
00368     flash_end_address = flash_start_address + flash.get_flash_size();;
00369 
00370     //Non default configuration
00371     if (start_address != 0) {
00372 
00373         aligned_start_address = align_down(start_address, flash.get_sector_size(start_address));
00374         if (start_address != aligned_start_address) {
00375             tr_error("KV Config: Internal block device start address is not aligned. Better use %02llx", aligned_start_address);
00376             flash.deinit();
00377             return NULL;
00378         }
00379 
00380         if (size == 0) {
00381             //The block device will have all space form start address to the end of the flash
00382             size = (flash_end_address - start_address);
00383 
00384             static FlashIAPBlockDevice bd(start_address, size);
00385             flash.deinit();
00386             return &bd;
00387         }
00388 
00389         if (size != 0) {
00390             end_address = start_address + size;
00391             if (end_address > flash_end_address) {
00392                 tr_error("KV Config: Internal block device end address is out of boundaries");
00393                 flash.deinit();
00394                 return NULL;
00395             }
00396 
00397             aligned_end_address = align_up(end_address, flash.get_sector_size(end_address - 1));
00398             if (end_address != aligned_end_address) {
00399                 tr_error("KV Config: Internal block device start address is not aligned. Consider changing the size parameter");
00400                 flash.deinit();
00401                 return NULL;
00402             }
00403 
00404             static FlashIAPBlockDevice bd(start_address, size);
00405             flash.deinit();
00406             return &bd;
00407         }
00408     }
00409 
00410     //Non default configuration start_address = 0
00411     start_address = flash_end_address - size;
00412     aligned_start_address = align_down(start_address, flash.get_sector_size(start_address));
00413     if (start_address != aligned_start_address) {
00414         tr_error("KV Config: Internal block device start address is not aligned. Consider changing the size parameter");
00415         flash.deinit();
00416         return NULL;
00417     }
00418 
00419     flash.deinit();
00420 
00421     if (aligned_start_address < flash_first_writable_sector_address) {
00422         tr_error("KV Config: Internal block device start address overlapped ROM address ");
00423         return NULL;
00424     }
00425     static FlashIAPBlockDevice bd(aligned_start_address, size);
00426     return &bd;
00427 
00428 #else
00429     return NULL;
00430 #endif
00431 }
00432 
00433 BlockDevice *_get_blockdevice_SPIF(bd_addr_t start_address, bd_size_t size)
00434 {
00435 #if COMPONENT_SPIF
00436 
00437     bd_addr_t aligned_end_address;
00438     bd_addr_t aligned_start_address;
00439 
00440     static SPIFBlockDevice bd(
00441         MBED_CONF_SPIF_DRIVER_SPI_MOSI,
00442         MBED_CONF_SPIF_DRIVER_SPI_MISO,
00443         MBED_CONF_SPIF_DRIVER_SPI_CLK,
00444         MBED_CONF_SPIF_DRIVER_SPI_CS,
00445         MBED_CONF_SPIF_DRIVER_SPI_FREQ
00446     );
00447 
00448     if (bd.init() != MBED_SUCCESS) {
00449         tr_error("KV Config: SPIFBlockDevice init fail");
00450         return NULL;
00451     }
00452 
00453     if (start_address == 0 && size == 0) {
00454         return &bd;
00455     }
00456 
00457     //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address.
00458     if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00459         tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00460         return NULL;
00461     }
00462 
00463     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00464     return &sbd;
00465 
00466 #else
00467     return NULL;
00468 #endif
00469 }
00470 
00471 BlockDevice *_get_blockdevice_QSPIF(bd_addr_t start_address, bd_size_t size)
00472 {
00473 #if COMPONENT_QSPIF
00474 
00475     bd_addr_t aligned_end_address;
00476     bd_addr_t aligned_start_address;
00477 
00478     static QSPIFBlockDevice bd(
00479         QSPI_FLASH1_IO0,
00480         QSPI_FLASH1_IO1,
00481         QSPI_FLASH1_IO2,
00482         QSPI_FLASH1_IO3,
00483         QSPI_FLASH1_SCK,
00484         QSPI_FLASH1_CSN,
00485         QSPIF_POLARITY_MODE_0,
00486         MBED_CONF_QSPIF_QSPI_FREQ
00487     );
00488 
00489     if (bd.init() != MBED_SUCCESS) {
00490         tr_error("KV Config: QSPIFBlockDevice init fail");
00491         return NULL;
00492     }
00493 
00494     if (start_address == 0 && size == 0) {
00495         return &bd;
00496     }
00497 
00498     //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address.
00499     if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00500         tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00501         return NULL;
00502     }
00503 
00504     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00505     return &sbd;
00506 
00507 #else
00508     return NULL;
00509 #endif
00510 }
00511 
00512 BlockDevice *_get_blockdevice_DATAFLASH(bd_addr_t start_address, bd_size_t size)
00513 {
00514 #if COMPONENT_DATAFLASH
00515 
00516     bd_addr_t aligned_end_address;
00517     bd_addr_t aligned_start_address;
00518 
00519     static DataFlashBlockDevice bd(
00520         MBED_CONF_DATAFLASH_SPI_MOSI,
00521         MBED_CONF_DATAFLASH_SPI_MISO,
00522         MBED_CONF_DATAFLASH_SPI_CLK,
00523         MBED_CONF_DATAFLASH_SPI_CS
00524     );
00525 
00526     if (bd.init() != MBED_SUCCESS) {
00527         tr_error("KV Config: DataFlashBlockDevice init fail");
00528         return NULL;
00529     }
00530 
00531     if (start_address == 0 && size == 0) {
00532         return &bd;
00533     }
00534 
00535     //If address and size were specified use SlicingBlockDevice to get the correct block device size and start address.
00536     if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00537         tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00538         return NULL;
00539     }
00540 
00541     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00542     return &sbd;
00543 
00544 
00545 #else
00546     return NULL;
00547 #endif
00548 }
00549 
00550 BlockDevice *_get_blockdevice_SD(bd_addr_t start_address, bd_size_t size)
00551 {
00552 #if COMPONENT_SD
00553 
00554     bd_addr_t aligned_end_address;
00555     bd_addr_t aligned_start_address;
00556 
00557   #if (defined(TARGET_VK_RZ_A1H) || defined(TARGET_VK_RZ_A1LU))
00558     static SDHIBlockDevice bd(
00559         MBED_CONF_SD_SDHI_CH
00560     );
00561   #else
00562     static SDBlockDevice bd(
00563         MBED_CONF_SD_SPI_MOSI,
00564         MBED_CONF_SD_SPI_MISO,
00565         MBED_CONF_SD_SPI_CLK,
00566         MBED_CONF_SD_SPI_CS
00567     );
00568   #endif
00569 
00570     if (bd.init() != MBED_SUCCESS) {
00571         tr_error("KV Config: SDBlockDevice init fail");
00572         return NULL;
00573     }
00574 
00575     if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 ||
00576             strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) {
00577         //In TDBStore profile, we have a constraint of 4GByte
00578         if (start_address == 0 && size == 0  && bd.size() < (uint32_t)(-1)) {
00579             return &bd;
00580         }
00581 
00582         //If the size of external storage is bigger than 4G we need to slice it.
00583         size = size != 0 ? size : align_down(bd.size(), bd.get_erase_size(bd.size() - 1));
00584 
00585         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00586             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00587             return NULL;
00588         }
00589 
00590         if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) {
00591             aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only
00592         }
00593     } else {
00594         //For all other KVStore profiles beside TDBStore we take the entire external memory space.
00595         if (start_address == 0 && size == 0) {
00596             return &bd;
00597         }
00598 
00599         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00600             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00601             return NULL;
00602         }
00603     }
00604 
00605     aligned_end_address = align_down(aligned_end_address, bd.get_erase_size(aligned_end_address));
00606     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00607     return &sbd;
00608 
00609 #else
00610     return NULL;
00611 #endif
00612 }
00613 
00614 BlockDevice *_get_blockdevice_default(bd_addr_t start_address, bd_size_t size)
00615 {
00616 #if COMPONENT_QSPIF
00617     return _get_blockdevice_QSPIF(start_address, size);
00618 #elif COMPONENT_SPIF
00619     return _get_blockdevice_SPIF(start_address, size);
00620 #elif COMPONENT_DATAFLASH
00621     return _get_blockdevice_DATAFLASH(start_address, size);
00622 #elif COMPONENT_SD
00623     return _get_blockdevice_SD(start_address, size);
00624 #else
00625     tr_error("KV Config: No default component define in target.json for this target.");
00626     return NULL;
00627 #endif
00628 }
00629 
00630 int _storage_config_TDB_INTERNAL()
00631 {
00632 #if COMPONENT_FLASHIAP
00633     bd_size_t internal_size = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE;
00634     bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS;
00635 
00636     if (internal_size == 0 && internal_start_address == 0) {
00637         //Calculate the block device size and start address in case default values are used.
00638         if (_get_flashiap_bd_default_addresses_tdb_internal(&internal_start_address, &internal_size) != MBED_SUCCESS) {
00639             return MBED_ERROR_FAILED_OPERATION;
00640         }
00641     }
00642 
00643     //Get internal memory FLASHIAP block device.
00644     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_size);
00645     if (kvstore_config.internal_bd == NULL) {
00646         tr_error("KV Config: Fail to get internal BlockDevice.");
00647         return MBED_ERROR_FAILED_OPERATION;
00648     }
00649 
00650 
00651     int ret = kvstore_config.internal_bd->init();
00652     if (ret != MBED_SUCCESS) {
00653         tr_error("KV Config: Fail to init internal BlockDevice.");
00654         return MBED_ERROR_FAILED_OPERATION;
00655     }
00656 
00657     //Check that internal flash has 2 or more sectors
00658     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00659         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00660         return MBED_ERROR_INVALID_ARGUMENT;
00661     }
00662 
00663     //Deinitialize internal block device and TDB will reinitialize and take control on it.
00664     ret = kvstore_config.internal_bd->deinit();
00665     if (ret != MBED_SUCCESS) {
00666         tr_error("KV Config: Fail to deinit internal BlockDevice.");
00667         return MBED_ERROR_FAILED_OPERATION;
00668     }
00669 
00670     //Create a TDBStore in the internal FLASHIAP block device.
00671     static TDBStore tdb_internal(kvstore_config.internal_bd);
00672     kvstore_config.internal_store = &tdb_internal;
00673 
00674     ret = kvstore_config.internal_store->init();
00675     if (ret != MBED_SUCCESS) {
00676         tr_error("KV Config: Fail to init internal TDBStore.");
00677         return ret;
00678     }
00679     kvstore_config.kvstore_main_instance =
00680         kvstore_config.internal_store;
00681 
00682     //Masking flag - Actually used to remove any KVStore flag which is not supported
00683     //in the chosen KVStore profile.
00684     kvstore_config.flags_mask = ~(KVStore::REQUIRE_CONFIDENTIALITY_FLAG |
00685                                   KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
00686 
00687     //Initialize kv_map and add the configuration struct to KVStore map.
00688     KVMap &kv_map = KVMap::get_instance();
00689     ret = kv_map.init();
00690     if (MBED_SUCCESS != ret) {
00691         tr_error("KV Config: Fail to init KVStore global API.");
00692         return ret;
00693     }
00694 
00695     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
00696     if (MBED_SUCCESS != ret) {
00697         tr_error("KV Config: Fail to attach KVStore main instance to KVStore global API.");
00698         return ret;
00699     }
00700     return MBED_SUCCESS;
00701 #else
00702     return MBED_ERROR_UNSUPPORTED;
00703 #endif
00704 
00705 }
00706 
00707 int _storage_config_TDB_EXTERNAL()
00708 {
00709 #if !SECURESTORE_ENABLED
00710     return MBED_ERROR_UNSUPPORTED;
00711 #endif
00712 
00713     bd_size_t internal_rbp_size = MBED_CONF_STORAGE_TDB_EXTERNAL_RBP_INTERNAL_SIZE;
00714     bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_EXTERNAL_INTERNAL_BASE_ADDRESS;
00715 
00716     //Get the default address and size for internal rbp TDBStore
00717     if (internal_rbp_size == 0 && internal_start_address == 0) {
00718         //Calculate the block device size and start address in case default values are used.
00719         if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) {
00720             return MBED_ERROR_FAILED_OPERATION;
00721         }
00722     }
00723 
00724     //Create internal FLASHIAP block device
00725     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size);
00726     if (kvstore_config.internal_bd == NULL) {
00727         tr_error("KV Config: Fail to get internal BlockDevice.");
00728         return MBED_ERROR_FAILED_OPERATION ;
00729     }
00730 
00731     int ret = kvstore_config.internal_bd->init();
00732     if (ret != MBED_SUCCESS) {
00733         tr_error("KV Config: Fail to init internal BlockDevice.");
00734         return MBED_ERROR_FAILED_OPERATION ;
00735     }
00736 
00737     //Check if TDBStore has at least 2 sector.
00738     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00739         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00740         return MBED_ERROR_INVALID_ARGUMENT;
00741     }
00742 
00743     //Create internal TDBStore
00744     static TDBStore tdb_internal(kvstore_config.internal_bd);
00745     kvstore_config.internal_store = &tdb_internal;
00746 
00747     ret = kvstore_config.internal_store->init();
00748     if (ret != MBED_SUCCESS) {
00749         tr_error("KV Config: Fail to init internal TDBStore.");
00750         return ret;
00751     }
00752 
00753     bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_SIZE;
00754     bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BASE_ADDRESS;
00755 
00756     //Get external BlockDevice for TDBStore
00757     BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE, address, size);
00758     if (bd == NULL) {
00759         tr_error("KV Config: Fail to get external BlockDevice.");
00760         return MBED_ERROR_FAILED_OPERATION ;
00761     }
00762 
00763     //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
00764     //add FlashSimBlockDevice on top of the SDBlockDevice
00765 #if defined(COMPONENT_SD)
00766     if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "SD") == 0
00767 #if defined(COMPONENT_SD) &&  !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
00768             ||  strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "default") == 0) {
00769 #else
00770        ) {
00771 
00772 #endif
00773         //TDBStore need FlashSimBlockDevice when working with SD block device
00774         if (bd->init() != MBED_SUCCESS) {
00775             tr_error("KV Config: Fail to init external BlockDevice.");
00776             return MBED_ERROR_FAILED_OPERATION ;
00777         }
00778 
00779         static FlashSimBlockDevice flash_bd(bd);
00780         kvstore_config.external_bd = &flash_bd;
00781     } else {
00782         kvstore_config.external_bd = bd;
00783     }
00784 #else
00785     kvstore_config.external_bd = bd;
00786 #endif
00787 
00788     kvstore_config.flags_mask = ~(0);
00789 
00790     return _storage_config_tdb_external_common();
00791 }
00792 
00793 int _storage_config_TDB_EXTERNAL_NO_RBP()
00794 {
00795 #if !SECURESTORE_ENABLED
00796     return MBED_ERROR_UNSUPPORTED;
00797 #endif
00798     bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_SIZE;
00799     bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BASE_ADDRESS;
00800 
00801     //Get external block device
00802     BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE, address, size);
00803     if (bd == NULL) {
00804         tr_error("KV Config: Fail to get external BlockDevice.");
00805         return MBED_ERROR_FAILED_OPERATION ;
00806     }
00807 
00808     //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
00809     //add FlashSimBlockDevice on top of the SDBlockDevice
00810 #if defined(COMPONENT_SD)
00811     if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "SD") == 0
00812 #if defined(COMPONENT_SD) &&  !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
00813             ||  strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "default") == 0) {
00814 #else
00815        ) {
00816 
00817 #endif
00818         //TDBStore need FlashSimBlockDevice when working with SD block device
00819         if (bd->init() != MBED_SUCCESS) {
00820             tr_error("KV Config: Fail to init external BlockDevice.");
00821             return MBED_ERROR_FAILED_OPERATION ;
00822         }
00823 
00824         static FlashSimBlockDevice flash_bd(bd);
00825         kvstore_config.external_bd = &flash_bd;
00826     } else {
00827         kvstore_config.external_bd = bd;
00828     }
00829 #else
00830     kvstore_config.external_bd = bd;
00831 #endif
00832 
00833     //Masking flag - Actually used to remove any KVStore flag which is not supported
00834     //in the chosen KVStore profile.
00835     kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
00836 
00837     return _storage_config_tdb_external_common();
00838 }
00839 
00840 int _storage_config_tdb_external_common()
00841 {
00842 #if SECURESTORE_ENABLED
00843     //Initialize external block device
00844     int ret = kvstore_config.external_bd->init();
00845     if (ret != MBED_SUCCESS) {
00846         tr_error("KV Config: Fail to init external BlockDevice.");
00847         return MBED_ERROR_FAILED_OPERATION ;
00848     }
00849 
00850     //Check that there is at least 2 sector for the external TDBStore
00851     if (_calculate_blocksize_match_tdbstore(kvstore_config.external_bd) != MBED_SUCCESS) {
00852         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00853         return MBED_ERROR_INVALID_SIZE;
00854     }
00855 
00856     //Create external TDBStore
00857     static TDBStore tdb_external(kvstore_config.external_bd);
00858     kvstore_config.external_store = &tdb_external;
00859 
00860     //Create SecureStore and initialize it
00861     static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store);
00862 
00863     ret = secst.init();
00864     if (ret != MBED_SUCCESS) {
00865         tr_error("KV Config: Fail to init SecureStore.");
00866         return ret ;
00867     }
00868 
00869     kvstore_config.kvstore_main_instance = &secst;
00870 
00871     //Init kv_map and add the configuration struct to KVStore map.
00872     KVMap &kv_map = KVMap::get_instance();
00873     ret = kv_map.init();
00874     if (MBED_SUCCESS != ret) {
00875         tr_error("KV Config: Fail to init KVStore global API");
00876         return ret;
00877     }
00878 
00879     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
00880     if (MBED_SUCCESS != ret) {
00881         tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API");
00882         return ret;
00883     }
00884 
00885     return MBED_SUCCESS;
00886 #else
00887     return MBED_ERROR_UNSUPPORTED;
00888 #endif
00889 }
00890 
00891 int _storage_config_FILESYSTEM()
00892 {
00893 #if !SECURESTORE_ENABLED
00894     return MBED_ERROR_UNSUPPORTED;
00895 #endif
00896 
00897     filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_FOLDER_PATH);
00898 
00899     bd_size_t internal_rbp_size = MBED_CONF_STORAGE_FILESYSTEM_RBP_INTERNAL_SIZE;
00900     bd_addr_t internal_start_address = MBED_CONF_STORAGE_FILESYSTEM_INTERNAL_BASE_ADDRESS;
00901 
00902     //Get the default address and size for internal rbp TDBStore
00903     if (internal_rbp_size == 0 && internal_start_address == 0) {
00904         //Calculate the block device size and start address in case default values are used.
00905         if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) {
00906             return MBED_ERROR_FAILED_OPERATION;
00907         }
00908     }
00909 
00910     //Get internal FLASHIAP block device
00911     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size);
00912     if (kvstore_config.internal_bd == NULL) {
00913         tr_error("KV Config: Fail to get internal BlockDevice ");
00914         return MBED_ERROR_FAILED_OPERATION ;
00915     }
00916 
00917     int ret = kvstore_config.internal_bd->init();
00918     if (ret != MBED_SUCCESS) {
00919         tr_error("KV Config: Fail to init internal BlockDevice ");
00920         return MBED_ERROR_FAILED_OPERATION ;
00921     }
00922 
00923     //Check that internal flash has 2 or more sectors
00924     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00925         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00926         return MBED_ERROR_INVALID_ARGUMENT;
00927     }
00928 
00929     //Deinitialize internal block device and TDB will reinitialize and take control on it.
00930     ret = kvstore_config.internal_bd->deinit();
00931     if (ret != MBED_SUCCESS) {
00932         tr_error("KV Config: Fail to deinit internal BlockDevice.");
00933         return MBED_ERROR_FAILED_OPERATION;
00934     }
00935 
00936     //Create internal TDBStore for rbp
00937     static TDBStore tdb_internal(kvstore_config.internal_bd);
00938     kvstore_config.internal_store = &tdb_internal;
00939 
00940     ret = kvstore_config.internal_store->init();
00941     if (ret != MBED_SUCCESS) {
00942         tr_error("KV Config: Fail to init internal TDBStore");
00943         return ret;
00944     }
00945 
00946     bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_SIZE;
00947     bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_BASE_ADDRESS;
00948     const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_MOUNT_POINT);
00949 
00950     //Get external block device for FileSystem.
00951     kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_BLOCKDEVICE, address, size);
00952     if (kvstore_config.external_bd == NULL) {
00953         tr_error("KV Config: Fail to get external BlockDevice ");
00954         return MBED_ERROR_FAILED_OPERATION ;
00955     }
00956 
00957     ret = kvstore_config.external_bd->init();
00958     if (MBED_SUCCESS != ret) {
00959         tr_error("KV Config: Fail to init external BlockDevice ");
00960         return MBED_ERROR_FAILED_OPERATION ;
00961     }
00962 
00963     //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default
00964     //component block device configured in the system. The priority is:
00965     //QSPI -> SPI -> DATAFLASH == LITTLE
00966     //SD == FAT
00967     kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_FILESYSTEM, mount_point);
00968     if (kvstore_config.external_fs == NULL) {
00969         tr_error("KV Config: Fail to get FileSystem");
00970         return MBED_ERROR_FAILED_OPERATION ;
00971     }
00972 
00973     kvstore_config.flags_mask = ~(0);
00974 
00975     return _storage_config_filesystem_common();
00976 }
00977 
00978 int _storage_config_FILESYSTEM_NO_RBP()
00979 {
00980 #if !SECURESTORE_ENABLED
00981     return MBED_ERROR_UNSUPPORTED;
00982 #endif
00983 
00984     filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FOLDER_PATH);
00985 
00986     bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_SIZE;
00987     bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_BASE_ADDRESS;
00988     const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT);
00989 
00990     //Get external block device for FileSystem.
00991     kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_BLOCKDEVICE, address, size);
00992     if (kvstore_config.external_bd == NULL) {
00993         tr_error("KV Config: Fail to get external BlockDevice ");
00994         return MBED_ERROR_FAILED_OPERATION ;
00995     }
00996 
00997     int ret = kvstore_config.external_bd->init();
00998     if (MBED_SUCCESS != ret) {
00999         tr_error("KV Config: Fail to init external BlockDevice ");
01000         return MBED_ERROR_FAILED_OPERATION ;
01001     }
01002 
01003     //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default
01004     //component block device configured in the system. The priority is:
01005     //QSPI -> SPI -> DATAFLASH == LITTLE
01006     //SD == FAT
01007     kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FILESYSTEM, mount_point);
01008     if (kvstore_config.external_fs == NULL) {
01009         tr_error("KV Config: Fail to get FileSystem");
01010         return MBED_ERROR_FAILED_OPERATION ;
01011     }
01012 
01013     //Masking flag - Actually used to remove any KVStore flag which is not supported
01014     //in the chosen KVStore profile.
01015     kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
01016 
01017     return _storage_config_filesystem_common();
01018 }
01019 
01020 int _storage_config_filesystem_common()
01021 {
01022 #if SECURESTORE_ENABLED
01023 
01024     //Mount file system. if it fails, try to reformat
01025     int ret = kvstore_config.external_fs->mount(kvstore_config.external_bd);
01026     if (ret != MBED_SUCCESS) {
01027         ret = kvstore_config.external_fs->reformat(kvstore_config.external_bd);
01028         if (ret != MBED_SUCCESS) {
01029             tr_error("KV Config: Fail to mount FileSystem to %s",
01030                      STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT));
01031             return MBED_ERROR_FAILED_OPERATION ;
01032         }
01033     }
01034 
01035     //Create FileSystemStore
01036     kvstore_config.external_store = _get_file_system_store(kvstore_config.external_fs);
01037     if (kvstore_config.external_store == NULL) {
01038         tr_error("KV Config: Fail to get FileSystemStore");
01039         return MBED_ERROR_FAILED_OPERATION ;
01040     }
01041 
01042     //Create SecureStore and set it as main KVStore
01043     static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store);
01044 
01045     ret = secst.init();
01046     if (ret != MBED_SUCCESS) {
01047         tr_error("KV Config: Fail to init SecureStore.");
01048         return ret; ;
01049     }
01050 
01051     kvstore_config.kvstore_main_instance = &secst;
01052 
01053     //Init kv_map and add the configuration struct to KVStore map.
01054     KVMap &kv_map = KVMap::get_instance();
01055     ret = kv_map.init();
01056     if (MBED_SUCCESS != ret) {
01057         tr_error("KV Config: Fail to init KVStore global API");
01058         return ret;
01059     }
01060 
01061     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
01062     if (MBED_SUCCESS != ret) {
01063         tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API");
01064         return ret;
01065     }
01066 
01067     return MBED_SUCCESS;
01068 #else
01069     return MBED_ERROR_UNSUPPORTED;
01070 #endif
01071 }
01072 
01073 int _storage_config_default()
01074 {
01075 #if COMPONENT_QSPIF || COMPONENT_SPIF || COMPONENT_DATAFLASH
01076     return _storage_config_TDB_EXTERNAL();
01077 #elif COMPONENT_SD
01078     return _storage_config_FILESYSTEM();
01079 #elif COMPONENT_FLASHIAP
01080     return _storage_config_TDB_INTERNAL();
01081 #else
01082     return MBED_ERROR_UNSUPPORTED;
01083 #endif
01084 }
01085 
01086 const char *get_filesystemstore_folder_path()
01087 {
01088     return filesystemstore_folder_path;
01089 }
01090 
01091 MBED_WEAK int kv_init_storage_config()
01092 {
01093 
01094     int ret = MBED_SUCCESS;
01095 
01096     // We currently have no supported configuration without internal storage
01097 #ifndef COMPONENT_FLASHIAP
01098     return MBED_ERROR_UNSUPPORTED;
01099 #endif
01100 
01101     mutex->lock();
01102 
01103     if (is_kv_config_initialize) {
01104         goto exit;
01105     }
01106 
01107     memset(&kvstore_config, 0, sizeof(kvstore_config_t));
01108 
01109     ret = _STORAGE_CONFIG(MBED_CONF_STORAGE_STORAGE_TYPE);
01110 
01111     if (ret == MBED_SUCCESS) {
01112         is_kv_config_initialize = true;
01113     }
01114 
01115 exit:
01116     mutex->unlock();
01117     return ret;
01118 }
01119