blob: 16a58aece7fa205fe824ffaee3a5896cf7001287 [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{
188 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
189 return -1;
190 }
191
192 const uint32_t end_offset = off + len;
193 if (end_offset > fa->fa_size) {
194 MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
195 return -1;
196 }
197
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530198 const uint32_t start_addr = fa->fa_off + off;
199 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530200
Gustavo Henrique Nihei33a38312021-11-05 15:51:22 -0300201 if (bootloader_flash_write(start_addr, (void *)src, len, false) != ESP_OK) {
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530202 MCUBOOT_LOG_ERR("%s: Flash write failed", __func__);
203 return -1;
204 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530205
206 return 0;
207}
208
209int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
210{
211 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
212 return -1;
213 }
214
215 if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) {
216 MCUBOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", __func__,
217 (int)off, (int)len);
218 return -1;
219 }
220
221 const uint32_t start_addr = fa->fa_off + off;
222 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
223
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530224 if (bootloader_flash_erase_range(start_addr, len) != ESP_OK) {
225 MCUBOOT_LOG_ERR("%s: Flash erase failed", __func__);
226 return -1;
227 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530228#if VALIDATE_PROGRAM_OP
229 for (size_t i = 0; i < len; i++) {
230 uint8_t *val = (void *)(start_addr + i);
231 if (*val != 0xff) {
232 MCUBOOT_LOG_ERR("%s: Erase at 0x%x Failed", __func__, (int)val);
233 assert(0);
234 }
235 }
236#endif
237
238 return 0;
239}
240
241size_t flash_area_align(const struct flash_area *area)
242{
243 return 4;
244}
245
246uint8_t flash_area_erased_val(const struct flash_area *area)
247{
248 return 0xff;
249}
250
251int flash_area_get_sectors(int fa_id, uint32_t *count,
252 struct flash_sector *sectors)
253{
254 const struct flash_area *fa = prv_lookup_flash_area(fa_id);
255 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
256 return -1;
257 }
258
259 const size_t sector_size = FLASH_SECTOR_SIZE;
260 uint32_t total_count = 0;
261 for (size_t off = 0; off < fa->fa_size; off += sector_size) {
262 // Note: Offset here is relative to flash area, not device
263 sectors[total_count].fs_off = off;
264 sectors[total_count].fs_size = sector_size;
265 total_count++;
266 }
267
268 *count = total_count;
269 return 0;
270}
271
272int flash_area_id_from_multi_image_slot(int image_index, int slot)
273{
274 MCUBOOT_LOG_DBG("%s", __func__);
275 switch (slot) {
276 case 0:
277 return FLASH_AREA_IMAGE_PRIMARY(image_index);
278 case 1:
279 return FLASH_AREA_IMAGE_SECONDARY(image_index);
280 }
281
282 MCUBOOT_LOG_ERR("Unexpected Request: image_index=%d, slot=%d", image_index, slot);
283 return -1; /* flash_area_open will fail on that */
284}
285
286int flash_area_id_from_image_slot(int slot)
287{
Almir Okatod5320292021-06-18 02:00:40 -0300288 return flash_area_id_from_multi_image_slot(0, slot);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530289}
290
291int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa)
292{
Almir Okatod5320292021-06-18 02:00:40 -0300293 return -1;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530294}
295
296void mcuboot_assert_handler(const char *file, int line, const char *func)
297{
298 ets_printf("assertion failed: file \"%s\", line %d, func: %s\n", file, line, func);
299 abort();
300}