Vekatech / SDHI_driver
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 #include "SDBlockDevice.h"
00052 #endif
00053 
00054 #if COMPONENT_RZ_SDHI
00055 #include "RZ_SDHIBlockDevice.hpp"
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 || COMPONENT_RZ_SDHI)
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     static SDBlockDevice bd(
00558         MBED_CONF_SD_SPI_MOSI,
00559         MBED_CONF_SD_SPI_MISO,
00560         MBED_CONF_SD_SPI_CLK,
00561         MBED_CONF_SD_SPI_CS
00562     );
00563 
00564     if (bd.init() != MBED_SUCCESS) {
00565         tr_error("KV Config: SDBlockDevice init fail");
00566         return NULL;
00567     }
00568 
00569     if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 ||
00570             strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) {
00571         //In TDBStore profile, we have a constraint of 4GByte
00572         if (start_address == 0 && size == 0  && bd.size() < (uint32_t)(-1)) {
00573             return &bd;
00574         }
00575 
00576         //If the size of external storage is bigger than 4G we need to slice it.
00577         size = size != 0 ? size : align_down(bd.size(), bd.get_erase_size(bd.size() - 1));
00578 
00579         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00580             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00581             return NULL;
00582         }
00583 
00584         if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) {
00585             aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only
00586         }
00587     } else {
00588         //For all other KVStore profiles beside TDBStore we take the entire external memory space.
00589         if (start_address == 0 && size == 0) {
00590             return &bd;
00591         }
00592 
00593         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00594             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00595             return NULL;
00596         }
00597     }
00598 
00599     aligned_end_address = align_down(aligned_end_address, bd.get_erase_size(aligned_end_address));
00600     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00601     return &sbd;
00602 
00603 #else
00604     return NULL;
00605 #endif
00606 }
00607 
00608 BlockDevice *_get_blockdevice_RZ_SDHI(bd_addr_t start_address, bd_size_t size)
00609 {
00610 #if COMPONENT_RZ_SDHI
00611 
00612     bd_addr_t aligned_end_address;
00613     bd_addr_t aligned_start_address;
00614 
00615     static SDHIBlockDevice bd(MBED_CONF_RZ_SDHI_CH);
00616 
00617     if (bd.init() != MBED_SUCCESS) {
00618         tr_error("KV Config: SDBlockDevice init fail");
00619         return NULL;
00620     }
00621 
00622     if (strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL_NO_RBP") == 0 ||
00623             strcmp(STR(MBED_CONF_STORAGE_STORAGE_TYPE), "TDB_EXTERNAL") == 0) {
00624         //In TDBStore profile, we have a constraint of 4GByte
00625         if (start_address == 0 && size == 0  && bd.size() < (uint32_t)(-1)) {
00626             return &bd;
00627         }
00628 
00629         //If the size of external storage is bigger than 4G we need to slice it.
00630         size = size != 0 ? size : align_down(bd.size(), bd.get_erase_size(bd.size() - 1));
00631 
00632         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00633             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00634             return NULL;
00635         }
00636 
00637         if (aligned_end_address - aligned_start_address != (uint32_t)(aligned_end_address - aligned_start_address)) {
00638             aligned_end_address = aligned_start_address + (uint32_t)(-1);//Support up to 4G only
00639         }
00640     } else {
00641         //For all other KVStore profiles beside TDBStore we take the entire external memory space.
00642         if (start_address == 0 && size == 0) {
00643             return &bd;
00644         }
00645 
00646         if (_get_addresses(&bd, start_address, size, &aligned_start_address, &aligned_end_address) != 0) {
00647             tr_error("KV Config: Fail to get addresses for SlicingBlockDevice.");
00648             return NULL;
00649         }
00650     }
00651 
00652     aligned_end_address = align_down(aligned_end_address, bd.get_erase_size(aligned_end_address));
00653     static SlicingBlockDevice sbd(&bd, aligned_start_address, aligned_end_address);
00654     return &sbd;
00655 
00656 #else
00657     return NULL;
00658 #endif
00659 }
00660 
00661 BlockDevice *_get_blockdevice_default(bd_addr_t start_address, bd_size_t size)
00662 {
00663 #if COMPONENT_QSPIF
00664     return _get_blockdevice_QSPIF(start_address, size);
00665 #elif COMPONENT_SPIF
00666     return _get_blockdevice_SPIF(start_address, size);
00667 #elif COMPONENT_DATAFLASH
00668     return _get_blockdevice_DATAFLASH(start_address, size);
00669 #elif COMPONENT_SD
00670     return _get_blockdevice_SD(start_address, size);
00671 #elif COMPONENT_RZ_SDHI
00672     return _get_blockdevice_RZ_SDHI(start_address, size);
00673 #else
00674     tr_error("KV Config: No default component define in target.json for this target.");
00675     return NULL;
00676 #endif
00677 }
00678 
00679 int _storage_config_TDB_INTERNAL()
00680 {
00681 #if COMPONENT_FLASHIAP
00682     bd_size_t internal_size = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_SIZE;
00683     bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_INTERNAL_INTERNAL_BASE_ADDRESS;
00684 
00685     if (internal_size == 0 && internal_start_address == 0) {
00686         //Calculate the block device size and start address in case default values are used.
00687         if (_get_flashiap_bd_default_addresses_tdb_internal(&internal_start_address, &internal_size) != MBED_SUCCESS) {
00688             return MBED_ERROR_FAILED_OPERATION;
00689         }
00690     }
00691 
00692     //Get internal memory FLASHIAP block device.
00693     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_size);
00694     if (kvstore_config.internal_bd == NULL) {
00695         tr_error("KV Config: Fail to get internal BlockDevice.");
00696         return MBED_ERROR_FAILED_OPERATION;
00697     }
00698 
00699 
00700     int ret = kvstore_config.internal_bd->init();
00701     if (ret != MBED_SUCCESS) {
00702         tr_error("KV Config: Fail to init internal BlockDevice.");
00703         return MBED_ERROR_FAILED_OPERATION;
00704     }
00705 
00706     //Check that internal flash has 2 or more sectors
00707     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00708         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00709         return MBED_ERROR_INVALID_ARGUMENT;
00710     }
00711 
00712     //Deinitialize internal block device and TDB will reinitialize and take control on it.
00713     ret = kvstore_config.internal_bd->deinit();
00714     if (ret != MBED_SUCCESS) {
00715         tr_error("KV Config: Fail to deinit internal BlockDevice.");
00716         return MBED_ERROR_FAILED_OPERATION;
00717     }
00718 
00719     //Create a TDBStore in the internal FLASHIAP block device.
00720     static TDBStore tdb_internal(kvstore_config.internal_bd);
00721     kvstore_config.internal_store = &tdb_internal;
00722 
00723     ret = kvstore_config.internal_store->init();
00724     if (ret != MBED_SUCCESS) {
00725         tr_error("KV Config: Fail to init internal TDBStore.");
00726         return ret;
00727     }
00728     kvstore_config.kvstore_main_instance =
00729         kvstore_config.internal_store;
00730 
00731     //Masking flag - Actually used to remove any KVStore flag which is not supported
00732     //in the chosen KVStore profile.
00733     kvstore_config.flags_mask = ~(KVStore::REQUIRE_CONFIDENTIALITY_FLAG |
00734                                   KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
00735 
00736     //Initialize kv_map and add the configuration struct to KVStore map.
00737     KVMap &kv_map = KVMap::get_instance();
00738     ret = kv_map.init();
00739     if (MBED_SUCCESS != ret) {
00740         tr_error("KV Config: Fail to init KVStore global API.");
00741         return ret;
00742     }
00743 
00744     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
00745     if (MBED_SUCCESS != ret) {
00746         tr_error("KV Config: Fail to attach KVStore main instance to KVStore global API.");
00747         return ret;
00748     }
00749     return MBED_SUCCESS;
00750 #else
00751     return MBED_ERROR_UNSUPPORTED;
00752 #endif
00753 
00754 }
00755 
00756 int _storage_config_TDB_EXTERNAL()
00757 {
00758 #if !SECURESTORE_ENABLED
00759     return MBED_ERROR_UNSUPPORTED;
00760 #endif
00761 
00762     bd_size_t internal_rbp_size = MBED_CONF_STORAGE_TDB_EXTERNAL_RBP_INTERNAL_SIZE;
00763     bd_addr_t internal_start_address = MBED_CONF_STORAGE_TDB_EXTERNAL_INTERNAL_BASE_ADDRESS;
00764 
00765     //Get the default address and size for internal rbp TDBStore
00766     if (internal_rbp_size == 0 && internal_start_address == 0) {
00767         //Calculate the block device size and start address in case default values are used.
00768         if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) {
00769             return MBED_ERROR_FAILED_OPERATION;
00770         }
00771     }
00772 
00773     //Create internal FLASHIAP block device
00774     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size);
00775     if (kvstore_config.internal_bd == NULL) {
00776         tr_error("KV Config: Fail to get internal BlockDevice.");
00777         return MBED_ERROR_FAILED_OPERATION ;
00778     }
00779 
00780     int ret = kvstore_config.internal_bd->init();
00781     if (ret != MBED_SUCCESS) {
00782         tr_error("KV Config: Fail to init internal BlockDevice.");
00783         return MBED_ERROR_FAILED_OPERATION ;
00784     }
00785 
00786     //Check if TDBStore has at least 2 sector.
00787     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00788         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00789         return MBED_ERROR_INVALID_ARGUMENT;
00790     }
00791 
00792     //Create internal TDBStore
00793     static TDBStore tdb_internal(kvstore_config.internal_bd);
00794     kvstore_config.internal_store = &tdb_internal;
00795 
00796     ret = kvstore_config.internal_store->init();
00797     if (ret != MBED_SUCCESS) {
00798         tr_error("KV Config: Fail to init internal TDBStore.");
00799         return ret;
00800     }
00801 
00802     bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_SIZE;
00803     bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_EXTERNAL_BASE_ADDRESS;
00804 
00805     //Get external BlockDevice for TDBStore
00806     BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE, address, size);
00807     if (bd == NULL) {
00808         tr_error("KV Config: Fail to get external BlockDevice.");
00809         return MBED_ERROR_FAILED_OPERATION ;
00810     }
00811 
00812     //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
00813     //add FlashSimBlockDevice on top of the SDBlockDevice
00814 #if defined(COMPONENT_SD) || defined(COMPONENT_RZ_SDHI)
00815     if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "SD") == 0
00816 #if (defined(COMPONENT_SD) || defined(COMPONENT_RZ_SDHI))&&  !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
00817             ||  strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_BLOCKDEVICE), "default") == 0) {
00818 #else
00819        ) {
00820 
00821 #endif
00822         //TDBStore need FlashSimBlockDevice when working with SD block device
00823         if (bd->init() != MBED_SUCCESS) {
00824             tr_error("KV Config: Fail to init external BlockDevice.");
00825             return MBED_ERROR_FAILED_OPERATION ;
00826         }
00827 
00828         static FlashSimBlockDevice flash_bd(bd);
00829         kvstore_config.external_bd = &flash_bd;
00830     } else {
00831         kvstore_config.external_bd = bd;
00832     }
00833 #else
00834     kvstore_config.external_bd = bd;
00835 #endif
00836 
00837     kvstore_config.flags_mask = ~(0);
00838 
00839     return _storage_config_tdb_external_common();
00840 }
00841 
00842 int _storage_config_TDB_EXTERNAL_NO_RBP()
00843 {
00844 #if !SECURESTORE_ENABLED
00845     return MBED_ERROR_UNSUPPORTED;
00846 #endif
00847     bd_size_t size = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_SIZE;
00848     bd_addr_t address = MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_EXTERNAL_BASE_ADDRESS;
00849 
00850     //Get external block device
00851     BlockDevice *bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE, address, size);
00852     if (bd == NULL) {
00853         tr_error("KV Config: Fail to get external BlockDevice.");
00854         return MBED_ERROR_FAILED_OPERATION ;
00855     }
00856 
00857     //TDBStore needs a block device base on flash. so if this is SD block device or the default block device is SD
00858     //add FlashSimBlockDevice on top of the SDBlockDevice
00859 #if defined(COMPONENT_SD) || defined(COMPONENT_RZ_SDHI)
00860     if (strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "SD") == 0
00861 #if (defined(COMPONENT_SD) || defined(COMPONENT_RZ_SDHI)) &&  !defined(COMPONENT_SPIF) && !defined(COMPONENT_QSPIF) && !defined(COMPONENT_DATAFLASH)
00862             ||  strcmp(STR(MBED_CONF_STORAGE_TDB_EXTERNAL_NO_RBP_BLOCKDEVICE), "default") == 0) {
00863 #else
00864        ) {
00865 
00866 #endif
00867         //TDBStore need FlashSimBlockDevice when working with SD block device
00868         if (bd->init() != MBED_SUCCESS) {
00869             tr_error("KV Config: Fail to init external BlockDevice.");
00870             return MBED_ERROR_FAILED_OPERATION ;
00871         }
00872 
00873         static FlashSimBlockDevice flash_bd(bd);
00874         kvstore_config.external_bd = &flash_bd;
00875     } else {
00876         kvstore_config.external_bd = bd;
00877     }
00878 #else
00879     kvstore_config.external_bd = bd;
00880 #endif
00881 
00882     //Masking flag - Actually used to remove any KVStore flag which is not supported
00883     //in the chosen KVStore profile.
00884     kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
00885 
00886     return _storage_config_tdb_external_common();
00887 }
00888 
00889 int _storage_config_tdb_external_common()
00890 {
00891 #if SECURESTORE_ENABLED
00892     //Initialize external block device
00893     int ret = kvstore_config.external_bd->init();
00894     if (ret != MBED_SUCCESS) {
00895         tr_error("KV Config: Fail to init external BlockDevice.");
00896         return MBED_ERROR_FAILED_OPERATION ;
00897     }
00898 
00899     //Check that there is at least 2 sector for the external TDBStore
00900     if (_calculate_blocksize_match_tdbstore(kvstore_config.external_bd) != MBED_SUCCESS) {
00901         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00902         return MBED_ERROR_INVALID_SIZE;
00903     }
00904 
00905     //Create external TDBStore
00906     static TDBStore tdb_external(kvstore_config.external_bd);
00907     kvstore_config.external_store = &tdb_external;
00908 
00909     //Create SecureStore and initialize it
00910     static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store);
00911 
00912     ret = secst.init();
00913     if (ret != MBED_SUCCESS) {
00914         tr_error("KV Config: Fail to init SecureStore.");
00915         return ret ;
00916     }
00917 
00918     kvstore_config.kvstore_main_instance = &secst;
00919 
00920     //Init kv_map and add the configuration struct to KVStore map.
00921     KVMap &kv_map = KVMap::get_instance();
00922     ret = kv_map.init();
00923     if (MBED_SUCCESS != ret) {
00924         tr_error("KV Config: Fail to init KVStore global API");
00925         return ret;
00926     }
00927 
00928     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
00929     if (MBED_SUCCESS != ret) {
00930         tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API");
00931         return ret;
00932     }
00933 
00934     return MBED_SUCCESS;
00935 #else
00936     return MBED_ERROR_UNSUPPORTED;
00937 #endif
00938 }
00939 
00940 int _storage_config_FILESYSTEM()
00941 {
00942 #if !SECURESTORE_ENABLED
00943     return MBED_ERROR_UNSUPPORTED;
00944 #endif
00945 
00946     filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_FOLDER_PATH);
00947 
00948     bd_size_t internal_rbp_size = MBED_CONF_STORAGE_FILESYSTEM_RBP_INTERNAL_SIZE;
00949     bd_addr_t internal_start_address = MBED_CONF_STORAGE_FILESYSTEM_INTERNAL_BASE_ADDRESS;
00950 
00951     //Get the default address and size for internal rbp TDBStore
00952     if (internal_rbp_size == 0 && internal_start_address == 0) {
00953         //Calculate the block device size and start address in case default values are used.
00954         if (_get_flashiap_bd_default_addresses_rbp(&internal_start_address, &internal_rbp_size) != MBED_SUCCESS) {
00955             return MBED_ERROR_FAILED_OPERATION;
00956         }
00957     }
00958 
00959     //Get internal FLASHIAP block device
00960     kvstore_config.internal_bd = GET_BLOCKDEVICE(INTERNAL_BLOCKDEVICE_NAME, internal_start_address, internal_rbp_size);
00961     if (kvstore_config.internal_bd == NULL) {
00962         tr_error("KV Config: Fail to get internal BlockDevice ");
00963         return MBED_ERROR_FAILED_OPERATION ;
00964     }
00965 
00966     int ret = kvstore_config.internal_bd->init();
00967     if (ret != MBED_SUCCESS) {
00968         tr_error("KV Config: Fail to init internal BlockDevice ");
00969         return MBED_ERROR_FAILED_OPERATION ;
00970     }
00971 
00972     //Check that internal flash has 2 or more sectors
00973     if (_calculate_blocksize_match_tdbstore(kvstore_config.internal_bd) != MBED_SUCCESS) {
00974         tr_error("KV Config: Can not create TDBStore with less then 2 sector.");
00975         return MBED_ERROR_INVALID_ARGUMENT;
00976     }
00977 
00978     //Deinitialize internal block device and TDB will reinitialize and take control on it.
00979     ret = kvstore_config.internal_bd->deinit();
00980     if (ret != MBED_SUCCESS) {
00981         tr_error("KV Config: Fail to deinit internal BlockDevice.");
00982         return MBED_ERROR_FAILED_OPERATION;
00983     }
00984 
00985     //Create internal TDBStore for rbp
00986     static TDBStore tdb_internal(kvstore_config.internal_bd);
00987     kvstore_config.internal_store = &tdb_internal;
00988 
00989     ret = kvstore_config.internal_store->init();
00990     if (ret != MBED_SUCCESS) {
00991         tr_error("KV Config: Fail to init internal TDBStore");
00992         return ret;
00993     }
00994 
00995     bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_SIZE;
00996     bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_EXTERNAL_BASE_ADDRESS;
00997     const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_MOUNT_POINT);
00998 
00999     //Get external block device for FileSystem.
01000     kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_BLOCKDEVICE, address, size);
01001     if (kvstore_config.external_bd == NULL) {
01002         tr_error("KV Config: Fail to get external BlockDevice ");
01003         return MBED_ERROR_FAILED_OPERATION ;
01004     }
01005 
01006     ret = kvstore_config.external_bd->init();
01007     if (MBED_SUCCESS != ret) {
01008         tr_error("KV Config: Fail to init external BlockDevice ");
01009         return MBED_ERROR_FAILED_OPERATION ;
01010     }
01011 
01012     //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default
01013     //component block device configured in the system. The priority is:
01014     //QSPI -> SPI -> DATAFLASH == LITTLE
01015     //SD == FAT
01016     kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_FILESYSTEM, mount_point);
01017     if (kvstore_config.external_fs == NULL) {
01018         tr_error("KV Config: Fail to get FileSystem");
01019         return MBED_ERROR_FAILED_OPERATION ;
01020     }
01021 
01022     kvstore_config.flags_mask = ~(0);
01023 
01024     return _storage_config_filesystem_common();
01025 }
01026 
01027 int _storage_config_FILESYSTEM_NO_RBP()
01028 {
01029 #if !SECURESTORE_ENABLED
01030     return MBED_ERROR_UNSUPPORTED;
01031 #endif
01032 
01033     filesystemstore_folder_path = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FOLDER_PATH);
01034 
01035     bd_size_t size = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_SIZE;
01036     bd_addr_t address = MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_EXTERNAL_BASE_ADDRESS;
01037     const char *mount_point = STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT);
01038 
01039     //Get external block device for FileSystem.
01040     kvstore_config.external_bd = GET_BLOCKDEVICE(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_BLOCKDEVICE, address, size);
01041     if (kvstore_config.external_bd == NULL) {
01042         tr_error("KV Config: Fail to get external BlockDevice ");
01043         return MBED_ERROR_FAILED_OPERATION ;
01044     }
01045 
01046     int ret = kvstore_config.external_bd->init();
01047     if (MBED_SUCCESS != ret) {
01048         tr_error("KV Config: Fail to init external BlockDevice ");
01049         return MBED_ERROR_FAILED_OPERATION ;
01050     }
01051 
01052     //Get FileSystem. Can be FAT, LITTLE or default. in case of default, the type will be decided base on the default
01053     //component block device configured in the system. The priority is:
01054     //QSPI -> SPI -> DATAFLASH == LITTLE
01055     //SD == FAT
01056     kvstore_config.external_fs = GET_FILESYSTEM(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_FILESYSTEM, mount_point);
01057     if (kvstore_config.external_fs == NULL) {
01058         tr_error("KV Config: Fail to get FileSystem");
01059         return MBED_ERROR_FAILED_OPERATION ;
01060     }
01061 
01062     //Masking flag - Actually used to remove any KVStore flag which is not supported
01063     //in the chosen KVStore profile.
01064     kvstore_config.flags_mask = ~(KVStore::REQUIRE_REPLAY_PROTECTION_FLAG);
01065 
01066     return _storage_config_filesystem_common();
01067 }
01068 
01069 int _storage_config_filesystem_common()
01070 {
01071 #if SECURESTORE_ENABLED
01072 
01073     //Mount file system. if it fails, try to reformat
01074     int ret = kvstore_config.external_fs->mount(kvstore_config.external_bd);
01075     if (ret != MBED_SUCCESS) {
01076         ret = kvstore_config.external_fs->reformat(kvstore_config.external_bd);
01077         if (ret != MBED_SUCCESS) {
01078             tr_error("KV Config: Fail to mount FileSystem to %s",
01079                      STR(MBED_CONF_STORAGE_FILESYSTEM_NO_RBP_MOUNT_POINT));
01080             return MBED_ERROR_FAILED_OPERATION ;
01081         }
01082     }
01083 
01084     //Create FileSystemStore
01085     kvstore_config.external_store = _get_file_system_store(kvstore_config.external_fs);
01086     if (kvstore_config.external_store == NULL) {
01087         tr_error("KV Config: Fail to get FileSystemStore");
01088         return MBED_ERROR_FAILED_OPERATION ;
01089     }
01090 
01091     //Create SecureStore and set it as main KVStore
01092     static SecureStore secst(kvstore_config.external_store, kvstore_config.internal_store);
01093 
01094     ret = secst.init();
01095     if (ret != MBED_SUCCESS) {
01096         tr_error("KV Config: Fail to init SecureStore.");
01097         return ret; ;
01098     }
01099 
01100     kvstore_config.kvstore_main_instance = &secst;
01101 
01102     //Init kv_map and add the configuration struct to KVStore map.
01103     KVMap &kv_map = KVMap::get_instance();
01104     ret = kv_map.init();
01105     if (MBED_SUCCESS != ret) {
01106         tr_error("KV Config: Fail to init KVStore global API");
01107         return ret;
01108     }
01109 
01110     ret = kv_map.attach(STR(MBED_CONF_STORAGE_DEFAULT_KV), &kvstore_config);
01111     if (MBED_SUCCESS != ret) {
01112         tr_error("KV Config: Fail to attach KvStore main instance to KVStore global API");
01113         return ret;
01114     }
01115 
01116     return MBED_SUCCESS;
01117 #else
01118     return MBED_ERROR_UNSUPPORTED;
01119 #endif
01120 }
01121 
01122 int _storage_config_default()
01123 {
01124 #if COMPONENT_QSPIF || COMPONENT_SPIF || COMPONENT_DATAFLASH
01125     return _storage_config_TDB_EXTERNAL();
01126 #elif (COMPONENT_SD || COMPONENT_RZ_SDHI)
01127     return _storage_config_FILESYSTEM();
01128 #elif COMPONENT_FLASHIAP
01129     return _storage_config_TDB_INTERNAL();
01130 #else
01131     return MBED_ERROR_UNSUPPORTED;
01132 #endif
01133 }
01134 
01135 const char *get_filesystemstore_folder_path()
01136 {
01137     return filesystemstore_folder_path;
01138 }
01139 
01140 MBED_WEAK int kv_init_storage_config()
01141 {
01142 
01143     int ret = MBED_SUCCESS;
01144 
01145     // We currently have no supported configuration without internal storage
01146 #ifndef COMPONENT_FLASHIAP
01147     return MBED_ERROR_UNSUPPORTED;
01148 #endif
01149 
01150     mutex->lock();
01151 
01152     if (is_kv_config_initialize) {
01153         goto exit;
01154     }
01155 
01156     memset(&kvstore_config, 0, sizeof(kvstore_config_t));
01157 
01158     ret = _STORAGE_CONFIG(MBED_CONF_STORAGE_STORAGE_TYPE);
01159 
01160     if (ret == MBED_SUCCESS) {
01161         is_kv_config_initialize = true;
01162     }
01163 
01164 exit:
01165     mutex->unlock();
01166     return ret;
01167 }