Skip to content

Commit 981a1a7

Browse files
author
Seppo Takalo
committed
Move some storage functions to internal/utils.cpp
1 parent 0ec410e commit 981a1a7

File tree

12 files changed

+105
-123
lines changed

12 files changed

+105
-123
lines changed

UNITTESTS/moduletests/storage/kvstore/TDBStore/unittest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ set(unittest-sources
1414
../features/storage/blockdevice/HeapBlockDevice.cpp
1515
../features/storage/blockdevice/BufferedBlockDevice.cpp
1616
../features/storage/kvstore/tdbstore/TDBStore.cpp
17+
../features/storage/internal/utils.cpp
1718
../features/frameworks/mbed-trace/source/mbed_trace.c
1819
stubs/mbed_atomic_stub.c
1920
stubs/mbed_assert_stub.cpp

features/storage/TESTS/blockdevice/general_block_device/main.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "BufferedBlockDevice.h"
2828
#include "BlockDevice.h"
2929
#include <algorithm>
30+
#include "features/storage/internal/utils.h"
3031

3132
#if COMPONENT_SPIF
3233
#include "SPIFBlockDevice.h"
@@ -96,13 +97,6 @@ static SingletonPtr<PlatformMutex> _mutex;
9697

9798
BlockDevice *block_device = NULL;
9899

99-
#if COMPONENT_FLASHIAP
100-
static inline uint32_t align_up(uint32_t val, uint32_t size)
101-
{
102-
return (((val - 1) / size) + 1) * size;
103-
}
104-
#endif
105-
106100
static BlockDevice *get_bd_instance(uint8_t bd_type)
107101
{
108102
switch (bd_arr[bd_type]) {

features/storage/TESTS/kvstore/direct_access_devicekey_test/main.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "FlashIAPBlockDevice.h"
3232
#include "DirectAccessDevicekey.h"
3333
#include "greentea-client/test_env.h"
34+
#include "features/storage/internal/utils.h"
3435
#include "unity.h"
3536
#include "utest.h"
3637
#include <stdlib.h>
@@ -40,16 +41,6 @@ using namespace mbed;
4041

4142
#define TEST_DEVICEKEY_LENGTH 32
4243

43-
static inline uint32_t align_up(uint64_t val, uint64_t size)
44-
{
45-
return (((val - 1) / size) + 1) * size;
46-
}
47-
48-
static inline uint32_t align_down(uint64_t val, uint64_t size)
49-
{
50-
return (((val) / size)) * size;
51-
}
52-
5344
int get_virtual_TDBStore_position(uint32_t conf_start_address, uint32_t conf_size,
5445
uint32_t *tdb_start_address, uint32_t *tdb_end_address)
5546
{

features/storage/blockdevice/FlashSimBlockDevice.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "FlashSimBlockDevice.h"
1818
#include "platform/mbed_assert.h"
1919
#include "platform/mbed_atomic.h"
20+
#include "features/storage/internal/utils.h"
2021
#include <algorithm>
2122
#include <stdlib.h>
2223
#include <string.h>
@@ -25,11 +26,6 @@ namespace mbed {
2526

2627
static const bd_size_t min_blank_buf_size = 32;
2728

28-
static inline uint32_t align_up(bd_size_t val, bd_size_t size)
29-
{
30-
return (((val - 1) / size) + 1) * size;
31-
}
32-
3329
FlashSimBlockDevice::FlashSimBlockDevice(BlockDevice *bd, uint8_t erase_value) :
3430
_erase_value(erase_value), _blank_buf_size(0),
3531
_blank_buf(0), _bd(bd), _init_ref_count(0), _is_initialized(false)
@@ -222,4 +218,3 @@ const char *FlashSimBlockDevice::get_type() const
222218
}
223219

224220
} // namespace mbed
225-

features/storage/internal/utils.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
17+
#include "utils.h"
18+
#include "MbedCRC.h"
19+
20+
using namespace mbed;
21+
22+
uint32_t align_up(uint64_t val, uint64_t size)
23+
{
24+
return (((val - 1) / size) + 1) * size;
25+
}
26+
27+
uint32_t align_down(uint64_t val, uint64_t size)
28+
{
29+
return (((val) / size)) * size;
30+
}
31+
32+
uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf)
33+
{
34+
uint32_t crc;
35+
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
36+
ct.compute(data_buf, data_size, &crc);
37+
return crc;
38+
}

features/storage/internal/utils.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright (c) 2019 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+
17+
#ifndef STORAGE_UTILS_H
18+
#define STORAGE_UTILS_H
19+
20+
#include <stdint.h>
21+
22+
/** \defgroup storage-internal-api Storage-internal
23+
* @{
24+
* \ingroup mbed-os-internal
25+
*/
26+
27+
/** Align an address to next border of given block size.
28+
* @param val unaligned value.
29+
* @param size block size to align for.
30+
* @return Aligned value
31+
*/
32+
uint32_t align_up(uint64_t val, uint64_t size);
33+
34+
/** Align an address to previous border of given block size.
35+
* @param val unaligned value.
36+
* @param size block size to align for.
37+
* @return Aligned value
38+
*/
39+
uint32_t align_down(uint64_t val, uint64_t size);
40+
41+
/** Calculate CRC32 of given buffer.
42+
* @param init_crc Initial CRC or zero.
43+
* @param data_size Size of given buffer
44+
* @param data_buf Pointer to buffer
45+
* @return value of calculated CRC32
46+
*/
47+
uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf);
48+
49+
/** @}*/
50+
51+
#endif

features/storage/kvstore/conf/kv_config.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@
2727
#include "mbed_error.h"
2828
#include "drivers/FlashIAP.h"
2929
#include "features/storage/blockdevice/FlashSimBlockDevice.h"
30-
#include "mbed_trace.h"
3130
#include "features/storage/kvstore/securestore/SecureStore.h"
31+
#include "features/storage/internal/utils.h"
32+
#include "mbed_trace.h"
3233
#define TRACE_GROUP "KVCFG"
3334

3435
#if COMPONENT_FLASHIAP
@@ -162,16 +163,6 @@ static kvstore_config_t kvstore_config;
162163
#define _GET_BLOCKDEVICE_concat(dev, ...) _get_blockdevice_##dev(__VA_ARGS__)
163164
#define GET_BLOCKDEVICE(dev, ...) _GET_BLOCKDEVICE_concat(dev, __VA_ARGS__)
164165

165-
static inline uint32_t align_up(uint64_t val, uint64_t size)
166-
{
167-
return (((val - 1) / size) + 1) * size;
168-
}
169-
170-
static inline uint32_t align_down(uint64_t val, uint64_t size)
171-
{
172-
return (((val) / size)) * size;
173-
}
174-
175166
int _calculate_blocksize_match_tdbstore(BlockDevice *bd)
176167
{
177168
bd_size_t size = bd->size();

features/storage/kvstore/direct_access_devicekey/DirectAccessDevicekey.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <stdio.h>
2323
#include "mbed_error.h"
2424
#include "MbedCRC.h"
25+
#include "features/storage/internal/utils.h"
2526
#include "mbed_trace.h"
2627
#define TRACE_GROUP "DADK"
2728

@@ -51,9 +52,6 @@ static int calc_area_params(FlashIAP *flash, uint32_t out_tdb_start_offset, uint
5152
tdbstore_area_data_t *area_params);
5253
static int reserved_data_get(FlashIAP *flash, tdbstore_area_data_t *area_params, void *reserved_data_buf,
5354
size_t reserved_data_buf_size, size_t *actual_data_size_ptr);
54-
static inline uint32_t align_up(uint64_t val, uint64_t size);
55-
static inline uint32_t align_down(uint64_t val, uint64_t size);
56-
static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf);
5755

5856
// -------------------------------------------------- API Functions Implementation ----------------------------------------------------
5957
int direct_access_to_devicekey(uint32_t tdb_start_offset, uint32_t tdb_end_offset, void *data_buf,
@@ -295,21 +293,4 @@ static int reserved_data_get(FlashIAP *flash, tdbstore_area_data_t *area_params,
295293
return status;
296294
}
297295

298-
static inline uint32_t align_up(uint64_t val, uint64_t size)
299-
{
300-
return (((val - 1) / size) + 1) * size;
301-
}
302-
303-
static inline uint32_t align_down(uint64_t val, uint64_t size)
304-
{
305-
return (((val) / size)) * size;
306-
}
307-
308-
static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf)
309-
{
310-
uint32_t crc;
311-
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
312-
ct.compute(data_buf, data_size, &crc);
313-
return crc;
314-
}
315296
#endif // DEVICE_FLASH

features/storage/kvstore/direct_access_devicekey/DirectAccessDevicekey.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
*
2525
* @param[in] tdb_start_offset FlashIAP Start address offset of tdb store
2626
* @param[in] tdb_end_offset FlashIAP End address offset of tdb store
27-
* @param[in] data_buf Buffer to store devicekey data
27+
* @param[out] data_buf Buffer to store devicekey data
2828
* @param[in] data_buf_size Max expected size of buffer to store the devicekey data
2929
* @param[out] actual_data_size_ptr Actual retrieved devicekey size
3030
*

features/storage/kvstore/tdbstore/TDBStore.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@
2323
#include <stdio.h>
2424
#include "mbed_error.h"
2525
#include "mbed_wait_api.h"
26-
#include "MbedCRC.h"
2726
//Bypass the check of NVStore co existance if compiled for TARGET_TFM
2827
#if !(BYPASS_NVSTORE_CHECK)
2928
#include "features/storage/system_storage/SystemStorage.h"
3029
#endif
30+
#include "features/storage/internal/utils.h"
3131

3232
using namespace mbed;
3333

@@ -103,25 +103,6 @@ typedef struct {
103103

104104
} // anonymous namespace
105105

106-
107-
// -------------------------------------------------- Local Functions Declaration ----------------------------------------------------
108-
109-
// -------------------------------------------------- Functions Implementation ----------------------------------------------------
110-
111-
static inline uint32_t align_up(uint32_t val, uint32_t size)
112-
{
113-
return (((val - 1) / size) + 1) * size;
114-
}
115-
116-
117-
static uint32_t calc_crc(uint32_t init_crc, uint32_t data_size, const void *data_buf)
118-
{
119-
uint32_t crc;
120-
MbedCRC<POLY_32BIT_ANSI, 32> ct(init_crc, 0x0, true, false);
121-
ct.compute(data_buf, data_size, &crc);
122-
return crc;
123-
}
124-
125106
// Class member functions
126107

127108
TDBStore::TDBStore(BlockDevice *bd) : _ram_table(0), _max_keys(0),
@@ -861,7 +842,6 @@ int TDBStore::garbage_collection()
861842
{
862843
ram_table_entry_t *ram_table = (ram_table_entry_t *) _ram_table;
863844
uint32_t to_offset, to_next_offset;
864-
uint32_t chunk_size, reserved_size;
865845
int ret;
866846
size_t ind;
867847

@@ -997,7 +977,7 @@ int TDBStore::init()
997977
uint32_t next_offset;
998978
uint32_t flags, hash;
999979
uint32_t actual_data_size;
1000-
int os_ret, ret = MBED_SUCCESS;
980+
int ret;
1001981
uint16_t versions[_num_areas];
1002982

1003983
_mutex.lock();

features/storage/nvstore/source/nvstore.cpp

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "mbed_assert.h"
2727
#include "mbed_error.h"
2828
#include "ThisThread.h"
29+
#include "features/storage/internal/utils.h"
2930
#include <algorithm>
3031
#include <string.h>
3132
#include <stdio.h>
@@ -112,38 +113,6 @@ NVStore::nvstore_area_data_t NVStore::initial_area_params[] = {{0, 0},
112113

113114
// -------------------------------------------------- Functions Implementation ----------------------------------------------------
114115

115-
// Align a value to a specified size.
116-
// Parameters :
117-
// val - [IN] Value.
118-
// size - [IN] Size.
119-
// Return : Aligned value.
120-
static inline uint32_t align_up(uint32_t val, uint32_t size)
121-
{
122-
return (((val - 1) / size) + 1) * size;
123-
}
124-
125-
// CRC32 calculation. Supports "rolling" calculation (using the initial value).
126-
// Parameters :
127-
// init_crc - [IN] Initial CRC.
128-
// data_size - [IN] Buffer's data size.
129-
// data_buf - [IN] Data buffer.
130-
// Return : CRC.
131-
static uint32_t crc32(uint32_t init_crc, uint32_t data_size, uint8_t *data_buf)
132-
{
133-
uint32_t i, j;
134-
uint32_t crc, mask;
135-
136-
crc = init_crc;
137-
for (i = 0; i < data_size; i++) {
138-
crc = crc ^ (uint32_t)(data_buf[i]);
139-
for (j = 0; j < 8; j++) {
140-
mask = -(crc & 1);
141-
crc = (crc >> 1) ^ (0xEDB88320 & mask);
142-
}
143-
}
144-
return crc;
145-
}
146-
147116
NVStore::NVStore() : _init_done(0), _init_attempts(0), _active_area(0), _max_keys(NVSTORE_MAX_KEYS),
148117
_active_area_version(0), _free_space_offset(0), _size(0), _mutex(0), _offset_by_key(0), _flash_area_params{},
149118
_flash(0), _min_prog_size(0), _page_buf(0)
@@ -359,7 +328,7 @@ int NVStore::read_record(uint8_t area, uint32_t offset, uint16_t buf_size, void
359328
return NVSTORE_READ_ERROR;
360329
}
361330

362-
crc = crc32(crc, sizeof(header) - sizeof(header.crc), (uint8_t *) &header);
331+
crc = calc_crc(crc, sizeof(header) - sizeof(header.crc), (uint8_t *) &header);
363332

364333
actual_size = 0;
365334
key = header.key_and_flags & ~header_flag_mask;
@@ -396,7 +365,7 @@ int NVStore::read_record(uint8_t area, uint32_t offset, uint16_t buf_size, void
396365
if (os_ret) {
397366
return NVSTORE_READ_ERROR;
398367
}
399-
crc = crc32(crc, chunk_size, (uint8_t *) buf_ptr);
368+
crc = calc_crc(crc, chunk_size, (uint8_t *) buf_ptr);
400369
data_size -= chunk_size;
401370
offset += chunk_size;
402371
}
@@ -423,9 +392,9 @@ int NVStore::write_record(uint8_t area, uint32_t offset, uint16_t key, uint16_t
423392
header.key_and_flags = key | flags;
424393
header.size_and_owner = data_size | (owner << owner_bit_pos);
425394
header.crc = 0; // Satisfy compiler
426-
crc = crc32(crc, sizeof(header) - sizeof(header.crc), (uint8_t *) &header);
395+
crc = calc_crc(crc, sizeof(header) - sizeof(header.crc), (uint8_t *) &header);
427396
if (data_size) {
428-
crc = crc32(crc, data_size, (uint8_t *) data_buf);
397+
crc = calc_crc(crc, data_size, (uint8_t *) data_buf);
429398
}
430399
header.crc = crc;
431400

0 commit comments

Comments
 (0)