1 /*
2  * Copyright (c) 2021-2022 Arm Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may 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,
12  * WITHOUT 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 "emulated_flash_drv.h"
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <string.h>
21 #include <arm_cmse.h>
22 
is_range_valid(struct emulated_flash_dev_t * flash_dev,uint32_t offset)23 static int32_t is_range_valid(struct emulated_flash_dev_t *flash_dev,
24                               uint32_t offset)
25 {
26     uint32_t flash_limit = 0;
27     int32_t rc = 0;
28 
29     flash_limit = (flash_dev->data->sector_count * flash_dev->data->sector_size)
30                    - 1;
31 
32     if (offset > flash_limit) {
33         rc = -1;
34     }
35     return rc;
36 }
37 
is_write_aligned(struct emulated_flash_dev_t * flash_dev,uint32_t param)38 static int32_t is_write_aligned(struct emulated_flash_dev_t *flash_dev,
39                                 uint32_t param)
40 {
41     int32_t rc = 0;
42 
43     if ((param % flash_dev->data->program_unit) != 0) {
44         rc = -1;
45     }
46     return rc;
47 }
48 
is_sector_aligned(struct emulated_flash_dev_t * flash_dev,uint32_t offset)49 static int32_t is_sector_aligned(struct emulated_flash_dev_t *flash_dev,
50                                  uint32_t offset)
51 {
52     int32_t rc = 0;
53 
54     if ((offset % flash_dev->data->sector_size) != 0) {
55         rc = -1;
56     }
57     return rc;
58 }
59 
is_secure_alias_needed(uint32_t addr)60 static int32_t is_secure_alias_needed(uint32_t addr)
61 {
62     int32_t rc = -1;
63 
64     /* Only have to check it if the object is building for secure side */
65 #if defined(__ARM_FEATURE_CMSE) && (__ARM_FEATURE_CMSE == 3U)
66     cmse_address_info_t address_info;
67     /* Check if address can be accessed from non-secure */
68     address_info = cmse_TTA((void*)addr);
69     /* We only care about the security of the address here */
70     if(address_info.flags.nonsecure_read_ok) {
71         rc = 0;
72     }
73     else {
74         rc = 1;
75     }
76 #else
77     rc = 0;
78 #endif
79     return rc;
80 }
81 
is_flash_ready_to_write(const uint8_t * start_addr,uint32_t cnt)82 static int32_t is_flash_ready_to_write(const uint8_t *start_addr, uint32_t cnt)
83 {
84     int32_t rc = 0;
85     uint32_t i;
86 
87     for (i = 0; i < cnt; i++) {
88         if(start_addr[i] != EMULATED_FLASH_DRV_ERASE_VALUE) {
89             rc = -1;
90             break;
91         }
92     }
93 
94     return rc;
95 }
96 
emulated_flash_read_data(struct emulated_flash_dev_t * dev,uint32_t addr,void * data,uint32_t cnt)97 enum emulated_flash_error_t emulated_flash_read_data(struct emulated_flash_dev_t* dev,
98                                                      uint32_t addr, void *data,
99                                                      uint32_t cnt)
100 {
101     uint32_t start_addr = 0;
102     int32_t rc = 0;
103 
104     /* Check flash memory boundaries */
105     rc = is_range_valid(dev, addr + cnt);
106     if (rc != 0) {
107         return EMULATED_FLASH_ERR_INVALID_PARAM;
108     }
109 
110     /* Check which alias(S or NS) should be used to access the data */
111     rc = is_secure_alias_needed(addr + dev->memory_base_ns);
112     if(rc == 1) {
113         start_addr = dev->memory_base_s + addr;
114     }
115     else if(rc == 0) {
116         start_addr = dev->memory_base_ns + addr;
117     }
118 
119     /* Flash interface just emulated over SRAM, use memcpy */
120     memcpy(data, (void *)start_addr, cnt);
121     return EMULATED_FLASH_ERR_NONE;
122 }
123 
emulated_flash_program_data(struct emulated_flash_dev_t * dev,uint32_t addr,const void * data,uint32_t cnt)124 enum emulated_flash_error_t emulated_flash_program_data(struct emulated_flash_dev_t* dev,
125                                                         uint32_t addr, const void *data,
126                                                         uint32_t cnt)
127 {
128     uint32_t start_addr = 0;
129     int32_t rc = 0;
130 
131     /* Check flash memory boundaries and alignment with minimal write size */
132     rc  = is_range_valid(dev, addr + cnt);
133     rc |= is_write_aligned(dev, addr);
134     rc |= is_write_aligned(dev, cnt);
135     if (rc != 0) {
136         return EMULATED_FLASH_ERR_INVALID_PARAM;
137     }
138 
139     /* Check which alias(S or NS) should be used to access the data */
140     rc = is_secure_alias_needed(addr + dev->memory_base_ns);
141     if(rc == 1) {
142         start_addr = dev->memory_base_s + addr;
143     }
144     else if(rc == 0) {
145         start_addr = dev->memory_base_ns + addr;
146     }
147 
148     /* Check if the flash area to write the data was erased previously */
149     rc = is_flash_ready_to_write((const uint8_t*)start_addr, cnt);
150     if (rc != 0) {
151         return EMULATED_FLASH_NOT_READY;
152     }
153 
154     /* Flash interface just emulated over SRAM, use memcpy */
155     memcpy((void *)start_addr, data, cnt);
156     return EMULATED_FLASH_ERR_NONE;
157 }
158 
emulated_flash_erase_sector(struct emulated_flash_dev_t * dev,uint32_t addr)159 enum emulated_flash_error_t emulated_flash_erase_sector(struct emulated_flash_dev_t* dev,
160                                                         uint32_t addr)
161 {
162     uint32_t start_addr = 0;
163     int32_t rc = 0;
164 
165     rc  = is_range_valid(dev, addr);
166     rc |= is_sector_aligned(dev, addr);
167     if (rc != 0) {
168         return EMULATED_FLASH_ERR_INVALID_PARAM;
169     }
170 
171     /* Check which alias(S or NS) should be used to access the data */
172     rc = is_secure_alias_needed(addr + dev->memory_base_ns);
173     if(rc == 1) {
174         start_addr = dev->memory_base_s + addr;
175     }
176     else if(rc == 0) {
177         start_addr = dev->memory_base_ns + addr;
178     }
179 
180 
181     /* Flash interface just emulated over SRAM, use memset */
182     memset((void *)start_addr,
183            dev->data->erased_value,
184            dev->data->sector_size);
185     return EMULATED_FLASH_ERR_NONE;
186 }
187 
emulated_flash_erase_chip(struct emulated_flash_dev_t * dev)188 void emulated_flash_erase_chip(struct emulated_flash_dev_t* dev)
189 {
190     uint32_t i;
191     uint32_t addr = 0;
192     int32_t rc = 0;
193 
194     /* Only check 1 byte, as the whole memory should have the same security */
195     rc = is_secure_alias_needed(dev->memory_base_ns);
196     if(rc == 1) {
197         addr = dev->memory_base_s;
198     }
199     else if(rc == 0) {
200         addr = dev->memory_base_ns;
201     }
202 
203     for (i = 0; i < dev->data->sector_count; i++) {
204         /* Flash interface just emulated over SRAM, use memset */
205         memset((void *)addr,
206                 dev->data->erased_value,
207                 dev->data->sector_size);
208 
209         addr += dev->data->sector_size;
210     }
211 }
212 
emulated_flash_get_info(struct emulated_flash_dev_t * dev)213 ARM_FLASH_INFO* emulated_flash_get_info(struct emulated_flash_dev_t* dev)
214 {
215     return dev->data;
216 }
217