1 /*
2  * Copyright (c) 2019-2021, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 #include <string.h>
8 #include "its_flash_ram.h"
9 
10 #include "flash_fs/its_flash_fs.h"
11 
12 /**
13  * \brief Gets physical address of the given block ID.
14  *
15  * \param[in] cfg       Flash FS configuration
16  * \param[in] block_id  Block ID
17  * \param[in] offset    Offset position from the init of the block
18  *
19  * \returns Returns physical address for the given block ID.
20  */
get_phys_address(const struct its_flash_fs_config_t * cfg,uint32_t block_id,size_t offset)21 static uint32_t get_phys_address(const struct its_flash_fs_config_t *cfg,
22                                  uint32_t block_id, size_t offset)
23 {
24     return (block_id * cfg->block_size) + offset;
25 }
26 
its_flash_ram_init(const struct its_flash_fs_config_t * cfg)27 static psa_status_t its_flash_ram_init(const struct its_flash_fs_config_t *cfg)
28 {
29     /* Nothing needs to be done in case of flash emulated in RAM */
30     (void)cfg;
31     return PSA_SUCCESS;
32 }
33 
its_flash_ram_read(const struct its_flash_fs_config_t * cfg,uint32_t block_id,uint8_t * buff,size_t offset,size_t size)34 static psa_status_t its_flash_ram_read(const struct its_flash_fs_config_t *cfg,
35                                        uint32_t block_id, uint8_t *buff,
36                                        size_t offset, size_t size)
37 {
38     uint32_t idx = get_phys_address(cfg, block_id, offset);
39 
40     (void)memcpy(buff, (uint8_t *)cfg->flash_dev + idx, size);
41 
42     return PSA_SUCCESS;
43 }
44 
its_flash_ram_write(const struct its_flash_fs_config_t * cfg,uint32_t block_id,const uint8_t * buff,size_t offset,size_t size)45 static psa_status_t its_flash_ram_write(const struct its_flash_fs_config_t *cfg,
46                                         uint32_t block_id, const uint8_t *buff,
47                                         size_t offset, size_t size)
48 {
49     uint32_t idx = get_phys_address(cfg, block_id, offset);
50 
51     (void)memcpy((uint8_t *)cfg->flash_dev + idx, buff, size);
52 
53     return PSA_SUCCESS;
54 }
55 
its_flash_ram_flush(const struct its_flash_fs_config_t * cfg,uint32_t block_id)56 static psa_status_t its_flash_ram_flush(const struct its_flash_fs_config_t *cfg,
57                                         uint32_t block_id)
58 
59 {
60     /* Nothing needs to be done for flash emulated in RAM, as writes are
61      * committed immediately.
62      */
63     (void)cfg;
64     (void)block_id;
65     return PSA_SUCCESS;
66 }
67 
its_flash_ram_erase(const struct its_flash_fs_config_t * cfg,uint32_t block_id)68 static psa_status_t its_flash_ram_erase(const struct its_flash_fs_config_t *cfg,
69                                         uint32_t block_id)
70 {
71     uint32_t idx = get_phys_address(cfg, block_id, 0);
72 
73     (void)memset((uint8_t *)cfg->flash_dev + idx, cfg->erase_val,
74                  cfg->block_size);
75 
76     return PSA_SUCCESS;
77 }
78 
79 const struct its_flash_fs_ops_t its_flash_fs_ops_ram = {
80     .init = its_flash_ram_init,
81     .read = its_flash_ram_read,
82     .write = its_flash_ram_write,
83     .flush = its_flash_ram_flush,
84     .erase = its_flash_ram_erase,
85 };
86