Skip to content

Commit e5888fe

Browse files
Yossi Levyadbridge
authored andcommitted
Create runtime error if TDBStore and NVStore are created in internal flash
1 parent b1837c6 commit e5888fe

File tree

4 files changed

+83
-0
lines changed

4 files changed

+83
-0
lines changed

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mbed_error.h"
2525
#include "mbed_wait_api.h"
2626
#include "MbedCRC.h"
27+
#include "SystemStorage.h"
2728

2829
using namespace mbed;
2930

@@ -982,13 +983,21 @@ int TDBStore::init()
982983
uint32_t actual_data_size;
983984
int os_ret, ret = MBED_SUCCESS, reserved_ret;
984985
uint16_t versions[_num_areas];
986+
internal_mem_resident_type_e out_in_mem_res;
985987

986988
_mutex.lock();
987989

988990
if (_is_initialized) {
989991
goto end;
990992
}
991993

994+
//Check if we are on internal memory && try to set the internal memory for TDBStore use.
995+
if (strcmp(_bd->get_type(), "FLASHIAP")==0 &&
996+
set_internal_storage_ownership(TDBSTORE, &out_in_mem_res) == MBED_ERROR_ALREADY_INITIALIZED) {
997+
998+
MBED_ERROR(MBED_ERROR_ALREADY_INITIALIZED, "TDBStore in internal memory can not be initialize when NVStore is in use");
999+
}
1000+
9921001
_max_keys = initial_max_keys;
9931002

9941003
ram_table = new ram_table_entry_t[_max_keys];

features/storage/nvstore/source/nvstore.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121
#if NVSTORE_ENABLED
2222

2323
#include "FlashIAP.h"
24+
#include "SystemStorage.h"
2425
#include "mbed_critical.h"
2526
#include "mbed_assert.h"
27+
#include "mbed_error.h"
2628
#include "mbed_wait_api.h"
2729
#include <algorithm>
2830
#include <string.h>
@@ -849,11 +851,18 @@ int NVStore::init()
849851
uint16_t keys[NVSTORE_NUM_AREAS];
850852
uint16_t actual_size;
851853
uint8_t owner;
854+
internal_mem_resident_type_e out_in_mem_res;
852855

853856
if (_init_done) {
854857
return NVSTORE_SUCCESS;
855858
}
856859

860+
//Check if we are on internal memory && try to set the internal memory for TDBStore use.
861+
ret = set_internal_storage_ownership(NVSTORE, &out_in_mem_res);
862+
//NVstore in internal memory can not be initialize when TDBStore is in use
863+
MBED_ASSERT(ret != MBED_ERROR_ALREADY_INITIALIZED);
864+
865+
857866
// This handles the case that init function is called by more than one thread concurrently.
858867
// Only the one who gets the value of 1 in _init_attempts_val will proceed, while others will
859868
// wait until init is finished.

features/storage/system_storage/SystemStorage.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
#include "SystemStorage.h"
1617
#include "BlockDevice.h"
1718
#include "FileSystem.h"
1819
#include "FATFileSystem.h"
1920
#include "LittleFileSystem.h"
21+
#include "mbed_error.h"
22+
2023

2124
#if COMPONENT_SPIF
2225
#include "SPIFBlockDevice.h"
@@ -40,6 +43,31 @@
4043

4144
using namespace mbed;
4245

46+
static internal_mem_resident_type_e internal_memory_residency = NONE;
47+
static SingletonPtr<PlatformMutex> system_storage_mutex;
48+
49+
MBED_WEAK int set_internal_storage_ownership(internal_mem_resident_type_e in_mem_res, internal_mem_resident_type_e *out_mem_res)
50+
{
51+
int status = MBED_SUCCESS;
52+
53+
system_storage_mutex->lock();
54+
55+
if (internal_memory_residency != NONE &&
56+
internal_memory_residency != in_mem_res) {
57+
58+
status = MBED_ERROR_ALREADY_INITIALIZED;
59+
60+
} else {
61+
62+
internal_memory_residency = in_mem_res;
63+
}
64+
65+
*out_mem_res = internal_memory_residency;
66+
system_storage_mutex->unlock();
67+
68+
return status;
69+
}
70+
4371
// Align a value to a specified size.
4472
// Parameters :
4573
// val - [IN] Value.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
* Licensed under the Apache License, Version 2.0 (the License); you may
5+
* not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an AS IS BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#ifndef MBED_SYSTEM_STORAGE_H
17+
#define MBED_SYSTEM_STORAGE_H
18+
19+
#include "mbed_error.h"
20+
21+
typedef enum {
22+
NONE = 0,
23+
NVSTORE,
24+
TDBSTORE
25+
} internal_mem_resident_type_e;
26+
27+
/**
28+
* @brief Try to get an ownership for the internal flash memory storage type.
29+
* KVSTORE or NVSTORE is the current option and once the ownership is taken by one
30+
* second one can not be initialize.
31+
* @param[in] in_mem_res Enum parameter to specify NVSTORE or KVSTORE as the storage owner
32+
* @param[in] out_mem_res Enum parameter which specify who is the current owner of the storage.
33+
* @returns MBED_SUCCESS if succeeded or MBED_ERROR_ALREADY_INITIALIZED if fails.
34+
*/
35+
int set_internal_storage_ownership(internal_mem_resident_type_e in_mem_res, internal_mem_resident_type_e *out_mem_res);
36+
37+
#endif

0 commit comments

Comments
 (0)