blob: 1281d2ed6439c7442ddffdcafd705caf3c8bdc1d [file] [log] [blame]
Shubham Kulkarni052561d2021-07-20 11:42:44 +05301/*
2 * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#include <string.h>
8#include <stdlib.h>
9
10#include "mcuboot_config/mcuboot_logging.h"
11#include "flash_map_backend/flash_map_backend.h"
12#include "sysflash/sysflash.h"
13#include "bootutil/bootutil.h"
14
Shubham Kulkarnicd869652021-07-20 11:44:08 +053015#include "esp_err.h"
16#include "bootloader_flash.h"
17#include "bootloader_flash_priv.h"
18
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -030019#ifndef ARRAY_SIZE
20# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
21#endif
22
23#ifndef MIN
24# define MIN(a, b) (((a) < (b)) ? (a) : (b))
25#endif
26
27#ifndef ALIGN_UP
28# define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
29#endif
30
31#ifndef ALIGN_DOWN
32# define ALIGN_DOWN(num, align) ((num) & ~((align) - 1))
33#endif
34
35#ifndef ALIGN_OFFSET
36# define ALIGN_OFFSET(num, align) ((num) & ((align) - 1))
37#endif
38
39#ifndef IS_ALIGNED
40# define IS_ALIGNED(num, align) (ALIGN_OFFSET((num), (align)) == 0)
41#endif
42
43#define FLASH_BUFFER_SIZE 256 /* SPI Flash block size */
44
45_Static_assert(IS_ALIGNED(FLASH_BUFFER_SIZE, 4), "Buffer size for SPI Flash operations must be 4-byte aligned.");
Shubham Kulkarni052561d2021-07-20 11:42:44 +053046
47#define BOOTLOADER_START_ADDRESS 0x1000
48#define BOOTLOADER_SIZE CONFIG_ESP_BOOTLOADER_SIZE
49#define APPLICATION_PRIMARY_START_ADDRESS CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS
50#define APPLICATION_SECONDARY_START_ADDRESS CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS
51#define APPLICATION_SIZE CONFIG_ESP_APPLICATION_SIZE
52#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET
53#define SCRATCH_SIZE CONFIG_ESP_SCRATCH_SIZE
54
Shubham Kulkarni052561d2021-07-20 11:42:44 +053055extern int ets_printf(const char *fmt, ...);
56
57static const struct flash_area bootloader = {
58 .fa_id = FLASH_AREA_BOOTLOADER,
59 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
60 .fa_off = BOOTLOADER_START_ADDRESS,
61 .fa_size = BOOTLOADER_SIZE,
62};
63
64static const struct flash_area primary_img0 = {
65 .fa_id = FLASH_AREA_IMAGE_PRIMARY(0),
66 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
67 .fa_off = APPLICATION_PRIMARY_START_ADDRESS,
68 .fa_size = APPLICATION_SIZE,
69};
70
71static const struct flash_area secondary_img0 = {
72 .fa_id = FLASH_AREA_IMAGE_SECONDARY(0),
73 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
74 .fa_off = APPLICATION_SECONDARY_START_ADDRESS,
75 .fa_size = APPLICATION_SIZE,
76};
77
78static const struct flash_area scratch_img0 = {
79 .fa_id = FLASH_AREA_IMAGE_SCRATCH,
80 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
81 .fa_off = SCRATCH_OFFSET,
82 .fa_size = SCRATCH_SIZE,
83};
84
85static const struct flash_area *s_flash_areas[] = {
86 &bootloader,
87 &primary_img0,
88 &secondary_img0,
89 &scratch_img0,
90};
91
92static const struct flash_area *prv_lookup_flash_area(uint8_t id) {
93 for (size_t i = 0; i < ARRAY_SIZE(s_flash_areas); i++) {
94 const struct flash_area *area = s_flash_areas[i];
95 if (id == area->fa_id) {
96 return area;
97 }
98 }
99 return NULL;
100}
101
102int flash_area_open(uint8_t id, const struct flash_area **area_outp)
103{
104 MCUBOOT_LOG_DBG("%s: ID=%d", __func__, (int)id);
105 const struct flash_area *area = prv_lookup_flash_area(id);
106 *area_outp = area;
107 return area != NULL ? 0 : -1;
108}
109
110void flash_area_close(const struct flash_area *area)
111{
112
113}
114
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300115static bool aligned_flash_read(uintptr_t addr, void *dest, size_t size)
116{
117 if (IS_ALIGNED(addr, 4) && IS_ALIGNED((uintptr_t)dest, 4) && IS_ALIGNED(size, 4)) {
118 /* A single read operation is enough when when all parameters are aligned */
119
120 return bootloader_flash_read(addr, dest, size, true) == ESP_OK;
121 }
122
123 const uint32_t aligned_addr = ALIGN_DOWN(addr, 4);
124 const uint32_t addr_offset = ALIGN_OFFSET(addr, 4);
125 uint32_t bytes_remaining = size;
126 uint8_t read_data[FLASH_BUFFER_SIZE] = {0};
127
128 /* Align the read address to 4-byte boundary and ensure read size is a multiple of 4 bytes */
129
130 uint32_t bytes = MIN(bytes_remaining + addr_offset, sizeof(read_data));
131 if (bootloader_flash_read(aligned_addr, read_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
132 return false;
133 }
134
135 /* Skip non-useful data which may have been read for adjusting the alignment */
136
137 uint32_t bytes_read = bytes - addr_offset;
138 memcpy(dest, &read_data[addr_offset], bytes_read);
139
140 bytes_remaining -= bytes_read;
141
142 /* Read remaining data from Flash in case requested size is greater than buffer size */
143
144 uint32_t offset = bytes;
145
146 while (bytes_remaining != 0) {
147 bytes = MIN(bytes_remaining, sizeof(read_data));
148 if (bootloader_flash_read(aligned_addr + offset, read_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
149 return false;
150 }
151
152 memcpy(&((uint8_t *)dest)[bytes_read], read_data, bytes);
153
154 offset += bytes;
155 bytes_read += bytes;
156 bytes_remaining -= bytes;
157 }
158
159 return true;
160}
161
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530162int flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
163 uint32_t len)
164{
165 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
166 return -1;
167 }
168
169 const uint32_t end_offset = off + len;
170 if (end_offset > fa->fa_size) {
171 MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
172 return -1;
173 }
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300174
175 bool success = aligned_flash_read(fa->fa_off + off, dst, len);
176 if (!success) {
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530177 MCUBOOT_LOG_ERR("%s: Flash read failed", __func__);
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300178
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530179 return -1;
180 }
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300181
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530182 return 0;
183}
184
185int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
186 uint32_t len)
187{
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530188 uint32_t write_len = len, write_data = 0;
189 void *write_ptr = (void *)src;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530190 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
191 return -1;
192 }
193
194 const uint32_t end_offset = off + len;
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530195
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530196 if (end_offset > fa->fa_size) {
197 MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
198 return -1;
199 }
200
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530201 const uint32_t start_addr = fa->fa_off + off;
202 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
203 if (len < 4) {
204 flash_area_read(fa, start_addr, &write_data, sizeof(uint32_t));
205 memcpy(&write_data, src, len);
206 write_ptr = (void *)&write_data;
207 write_len = sizeof(uint32_t);
208 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530209
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530210 if (bootloader_flash_write(start_addr, write_ptr, write_len, false) != ESP_OK) {
211 MCUBOOT_LOG_ERR("%s: Flash write failed", __func__);
212 return -1;
213 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530214#if VALIDATE_PROGRAM_OP
215 if (memcmp((void *)addr, src, len) != 0) {
216 MCUBOOT_LOG_ERR("%s: Program Failed", __func__);
217 assert(0);
218 }
219#endif
220
221 return 0;
222}
223
224int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
225{
226 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
227 return -1;
228 }
229
230 if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) {
231 MCUBOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", __func__,
232 (int)off, (int)len);
233 return -1;
234 }
235
236 const uint32_t start_addr = fa->fa_off + off;
237 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
238
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530239 if (bootloader_flash_erase_range(start_addr, len) != ESP_OK) {
240 MCUBOOT_LOG_ERR("%s: Flash erase failed", __func__);
241 return -1;
242 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530243#if VALIDATE_PROGRAM_OP
244 for (size_t i = 0; i < len; i++) {
245 uint8_t *val = (void *)(start_addr + i);
246 if (*val != 0xff) {
247 MCUBOOT_LOG_ERR("%s: Erase at 0x%x Failed", __func__, (int)val);
248 assert(0);
249 }
250 }
251#endif
252
253 return 0;
254}
255
256size_t flash_area_align(const struct flash_area *area)
257{
258 return 4;
259}
260
261uint8_t flash_area_erased_val(const struct flash_area *area)
262{
263 return 0xff;
264}
265
266int flash_area_get_sectors(int fa_id, uint32_t *count,
267 struct flash_sector *sectors)
268{
269 const struct flash_area *fa = prv_lookup_flash_area(fa_id);
270 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
271 return -1;
272 }
273
274 const size_t sector_size = FLASH_SECTOR_SIZE;
275 uint32_t total_count = 0;
276 for (size_t off = 0; off < fa->fa_size; off += sector_size) {
277 // Note: Offset here is relative to flash area, not device
278 sectors[total_count].fs_off = off;
279 sectors[total_count].fs_size = sector_size;
280 total_count++;
281 }
282
283 *count = total_count;
284 return 0;
285}
286
287int flash_area_id_from_multi_image_slot(int image_index, int slot)
288{
289 MCUBOOT_LOG_DBG("%s", __func__);
290 switch (slot) {
291 case 0:
292 return FLASH_AREA_IMAGE_PRIMARY(image_index);
293 case 1:
294 return FLASH_AREA_IMAGE_SECONDARY(image_index);
295 }
296
297 MCUBOOT_LOG_ERR("Unexpected Request: image_index=%d, slot=%d", image_index, slot);
298 return -1; /* flash_area_open will fail on that */
299}
300
301int flash_area_id_from_image_slot(int slot)
302{
Almir Okatod5320292021-06-18 02:00:40 -0300303 return flash_area_id_from_multi_image_slot(0, slot);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530304}
305
306int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa)
307{
Almir Okatod5320292021-06-18 02:00:40 -0300308 return -1;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530309}
310
311void mcuboot_assert_handler(const char *file, int line, const char *func)
312{
313 ets_printf("assertion failed: file \"%s\", line %d, func: %s\n", file, line, func);
314 abort();
315}