aboutsummaryrefslogtreecommitdiff
path: root/components/service/secure_storage/provider/secure_flash_store/flash/sfs_flash_ram.c
blob: e4af6e610131a89e9f70b708c32c81641355d11b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
 * Copyright (c) 2019-2020, Arm Limited. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 *
 */

#include "sfs_flash_ram.h"
#include <string.h>

/**
 * \brief Gets physical address of the given block ID.
 *
 * \param[in] info      Flash device information
 * \param[in] block_id  Block ID
 * \param[in] offset    Offset position from the init of the block
 *
 * \returns Returns physical address for the given block ID.
 */
static uint32_t get_phys_address(const struct sfs_flash_info_t *info,
                                 uint32_t block_id, size_t offset)
{
    return (block_id * info->block_size) + offset;
}

psa_status_t sfs_flash_ram_init(const struct sfs_flash_info_t *info)
{
    /* Nothing needs to be done in case of flash emulated in RAM */
    (void)info;
    return PSA_SUCCESS;
}

psa_status_t sfs_flash_ram_read(const struct sfs_flash_info_t *info,
                                uint32_t block_id, uint8_t *buff, size_t offset,
                                size_t size)
{
    uint32_t idx = get_phys_address(info, block_id, offset);

    (void)memcpy(buff, (uint8_t *)info->flash_dev + idx, size);

    return PSA_SUCCESS;
}

psa_status_t sfs_flash_ram_write(const struct sfs_flash_info_t *info,
                                 uint32_t block_id, const uint8_t *buff,
                                 size_t offset, size_t size)
{
    uint32_t idx = get_phys_address(info, block_id, offset);

    (void)memcpy((uint8_t *)info->flash_dev + idx, buff, size);

    return PSA_SUCCESS;
}

psa_status_t sfs_flash_ram_flush(const struct sfs_flash_info_t *info)
{
    /* Nothing needs to be done for flash emulated in RAM, as writes are
     * commited immediately.
     */
    (void)info;
    return PSA_SUCCESS;
}

psa_status_t sfs_flash_ram_erase(const struct sfs_flash_info_t *info,
                                 uint32_t block_id)
{
    uint32_t idx = get_phys_address(info, block_id, 0);

    (void)memset((uint8_t *)info->flash_dev + idx, info->erase_val,
                     info->block_size);

    return PSA_SUCCESS;
}