blob: 088749c3e18b8c0e2725fd2dbd8722983e27f99f [file] [log] [blame]
Shubham Kulkarni052561d2021-07-20 11:42:44 +05301/*
Almir Okato14763b12021-11-25 00:45:26 -03002 * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
Shubham Kulkarni052561d2021-07-20 11:42:44 +05303 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -03007#include <stdbool.h>
Shubham Kulkarni052561d2021-07-20 11:42:44 +05308#include <stdlib.h>
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -03009#include <string.h>
Shubham Kulkarni052561d2021-07-20 11:42:44 +053010
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030011#include <bootutil/bootutil.h>
12#include <bootutil/bootutil_log.h>
Shubham Kulkarni052561d2021-07-20 11:42:44 +053013
Almir Okato14763b12021-11-25 00:45:26 -030014#include "sdkconfig.h"
Shubham Kulkarnicd869652021-07-20 11:44:08 +053015#include "esp_err.h"
Shubham Kulkarnicd869652021-07-20 11:44:08 +053016#include "bootloader_flash_priv.h"
Almir Okato14763b12021-11-25 00:45:26 -030017#include "esp_flash_encrypt.h"
Shubham Kulkarnicd869652021-07-20 11:44:08 +053018
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -030019#include "flash_map_backend/flash_map_backend.h"
20#include "sysflash/sysflash.h"
21
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -030022#ifndef ARRAY_SIZE
23# define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
24#endif
25
26#ifndef MIN
27# define MIN(a, b) (((a) < (b)) ? (a) : (b))
28#endif
29
30#ifndef ALIGN_UP
31# define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
32#endif
33
34#ifndef ALIGN_DOWN
35# define ALIGN_DOWN(num, align) ((num) & ~((align) - 1))
36#endif
37
38#ifndef ALIGN_OFFSET
39# define ALIGN_OFFSET(num, align) ((num) & ((align) - 1))
40#endif
41
42#ifndef IS_ALIGNED
43# define IS_ALIGNED(num, align) (ALIGN_OFFSET((num), (align)) == 0)
44#endif
45
46#define FLASH_BUFFER_SIZE 256 /* SPI Flash block size */
47
48_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 +053049
Almir Okato14763b12021-11-25 00:45:26 -030050#define BOOTLOADER_START_ADDRESS CONFIG_BOOTLOADER_OFFSET_IN_FLASH
Shubham Kulkarni052561d2021-07-20 11:42:44 +053051#define BOOTLOADER_SIZE CONFIG_ESP_BOOTLOADER_SIZE
Almir Okatoa1d641d2022-02-21 19:31:46 -030052#define IMAGE0_PRIMARY_START_ADDRESS CONFIG_ESP_IMAGE0_PRIMARY_START_ADDRESS
53#define IMAGE0_SECONDARY_START_ADDRESS CONFIG_ESP_IMAGE0_SECONDARY_START_ADDRESS
Shubham Kulkarni052561d2021-07-20 11:42:44 +053054#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET
Almir Okatoa1d641d2022-02-21 19:31:46 -030055#if (MCUBOOT_IMAGE_NUMBER == 2)
56#define IMAGE1_PRIMARY_START_ADDRESS CONFIG_ESP_IMAGE1_PRIMARY_START_ADDRESS
57#define IMAGE1_SECONDARY_START_ADDRESS CONFIG_ESP_IMAGE1_SECONDARY_START_ADDRESS
58#endif
59
60#define APPLICATION_SIZE CONFIG_ESP_APPLICATION_SIZE
Shubham Kulkarni052561d2021-07-20 11:42:44 +053061#define SCRATCH_SIZE CONFIG_ESP_SCRATCH_SIZE
62
Shubham Kulkarni052561d2021-07-20 11:42:44 +053063extern int ets_printf(const char *fmt, ...);
64
65static const struct flash_area bootloader = {
66 .fa_id = FLASH_AREA_BOOTLOADER,
67 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
68 .fa_off = BOOTLOADER_START_ADDRESS,
69 .fa_size = BOOTLOADER_SIZE,
70};
71
72static const struct flash_area primary_img0 = {
73 .fa_id = FLASH_AREA_IMAGE_PRIMARY(0),
74 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
Almir Okatoa1d641d2022-02-21 19:31:46 -030075 .fa_off = IMAGE0_PRIMARY_START_ADDRESS,
Shubham Kulkarni052561d2021-07-20 11:42:44 +053076 .fa_size = APPLICATION_SIZE,
77};
78
79static const struct flash_area secondary_img0 = {
80 .fa_id = FLASH_AREA_IMAGE_SECONDARY(0),
81 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
Almir Okatoa1d641d2022-02-21 19:31:46 -030082 .fa_off = IMAGE0_SECONDARY_START_ADDRESS,
Shubham Kulkarni052561d2021-07-20 11:42:44 +053083 .fa_size = APPLICATION_SIZE,
84};
85
Almir Okatoa1d641d2022-02-21 19:31:46 -030086#if (MCUBOOT_IMAGE_NUMBER == 2)
87static const struct flash_area primary_img1 = {
88 .fa_id = FLASH_AREA_IMAGE_PRIMARY(1),
89 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
90 .fa_off = IMAGE1_PRIMARY_START_ADDRESS,
91 .fa_size = APPLICATION_SIZE,
92};
93
94static const struct flash_area secondary_img1 = {
95 .fa_id = FLASH_AREA_IMAGE_SECONDARY(1),
96 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
97 .fa_off = IMAGE1_SECONDARY_START_ADDRESS,
98 .fa_size = APPLICATION_SIZE,
99};
100#endif
101
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530102static const struct flash_area scratch_img0 = {
103 .fa_id = FLASH_AREA_IMAGE_SCRATCH,
104 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
105 .fa_off = SCRATCH_OFFSET,
106 .fa_size = SCRATCH_SIZE,
107};
108
109static const struct flash_area *s_flash_areas[] = {
110 &bootloader,
111 &primary_img0,
112 &secondary_img0,
Almir Okatoa1d641d2022-02-21 19:31:46 -0300113#if (MCUBOOT_IMAGE_NUMBER == 2)
114 &primary_img1,
115 &secondary_img1,
116#endif
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530117 &scratch_img0,
118};
119
120static const struct flash_area *prv_lookup_flash_area(uint8_t id) {
121 for (size_t i = 0; i < ARRAY_SIZE(s_flash_areas); i++) {
122 const struct flash_area *area = s_flash_areas[i];
123 if (id == area->fa_id) {
124 return area;
125 }
126 }
127 return NULL;
128}
129
130int flash_area_open(uint8_t id, const struct flash_area **area_outp)
131{
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300132 BOOT_LOG_DBG("%s: ID=%d", __func__, (int)id);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530133 const struct flash_area *area = prv_lookup_flash_area(id);
134 *area_outp = area;
135 return area != NULL ? 0 : -1;
136}
137
138void flash_area_close(const struct flash_area *area)
139{
140
141}
142
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300143static bool aligned_flash_read(uintptr_t addr, void *dest, size_t size)
144{
145 if (IS_ALIGNED(addr, 4) && IS_ALIGNED((uintptr_t)dest, 4) && IS_ALIGNED(size, 4)) {
146 /* A single read operation is enough when when all parameters are aligned */
147
148 return bootloader_flash_read(addr, dest, size, true) == ESP_OK;
149 }
150
151 const uint32_t aligned_addr = ALIGN_DOWN(addr, 4);
152 const uint32_t addr_offset = ALIGN_OFFSET(addr, 4);
153 uint32_t bytes_remaining = size;
154 uint8_t read_data[FLASH_BUFFER_SIZE] = {0};
155
156 /* Align the read address to 4-byte boundary and ensure read size is a multiple of 4 bytes */
157
158 uint32_t bytes = MIN(bytes_remaining + addr_offset, sizeof(read_data));
159 if (bootloader_flash_read(aligned_addr, read_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
160 return false;
161 }
162
163 /* Skip non-useful data which may have been read for adjusting the alignment */
164
165 uint32_t bytes_read = bytes - addr_offset;
166 memcpy(dest, &read_data[addr_offset], bytes_read);
167
168 bytes_remaining -= bytes_read;
169
170 /* Read remaining data from Flash in case requested size is greater than buffer size */
171
172 uint32_t offset = bytes;
173
174 while (bytes_remaining != 0) {
175 bytes = MIN(bytes_remaining, sizeof(read_data));
176 if (bootloader_flash_read(aligned_addr + offset, read_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
177 return false;
178 }
179
180 memcpy(&((uint8_t *)dest)[bytes_read], read_data, bytes);
181
182 offset += bytes;
183 bytes_read += bytes;
184 bytes_remaining -= bytes;
185 }
186
187 return true;
188}
189
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530190int flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
191 uint32_t len)
192{
193 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
194 return -1;
195 }
196
197 const uint32_t end_offset = off + len;
198 if (end_offset > fa->fa_size) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300199 BOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530200 return -1;
201 }
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300202
203 bool success = aligned_flash_read(fa->fa_off + off, dst, len);
204 if (!success) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300205 BOOT_LOG_ERR("%s: Flash read failed", __func__);
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300206
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530207 return -1;
208 }
Gustavo Henrique Nihei74a27422021-10-29 09:25:55 -0300209
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530210 return 0;
211}
212
Gustavo Henrique Nihei1eb3eca2021-10-29 19:30:33 -0300213static bool aligned_flash_write(size_t dest_addr, const void *src, size_t size)
214{
215 bool flash_encryption_enabled = esp_flash_encryption_enabled();
216
217 if (IS_ALIGNED(dest_addr, 4) && IS_ALIGNED((uintptr_t)src, 4) && IS_ALIGNED(size, 4)) {
218 /* A single write operation is enough when all parameters are aligned */
219
220 return bootloader_flash_write(dest_addr, (void *)src, size, flash_encryption_enabled) == ESP_OK;
221 }
222
223 const uint32_t aligned_addr = ALIGN_DOWN(dest_addr, 4);
224 const uint32_t addr_offset = ALIGN_OFFSET(dest_addr, 4);
225 uint32_t bytes_remaining = size;
226 uint8_t write_data[FLASH_BUFFER_SIZE] = {0};
227
228 /* Perform a read operation considering an offset not aligned to 4-byte boundary */
229
230 uint32_t bytes = MIN(bytes_remaining + addr_offset, sizeof(write_data));
231 if (bootloader_flash_read(aligned_addr, write_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
232 return false;
233 }
234
235 uint32_t bytes_written = bytes - addr_offset;
236 memcpy(&write_data[addr_offset], src, bytes_written);
237
238 if (bootloader_flash_write(aligned_addr, write_data, ALIGN_UP(bytes, 4), flash_encryption_enabled) != ESP_OK) {
239 return false;
240 }
241
242 bytes_remaining -= bytes_written;
243
244 /* Write remaining data to Flash if any */
245
246 uint32_t offset = bytes;
247
248 while (bytes_remaining != 0) {
249 bytes = MIN(bytes_remaining, sizeof(write_data));
250 if (bootloader_flash_read(aligned_addr + offset, write_data, ALIGN_UP(bytes, 4), true) != ESP_OK) {
251 return false;
252 }
253
254 memcpy(write_data, &((uint8_t *)src)[bytes_written], bytes);
255
256 if (bootloader_flash_write(aligned_addr + offset, write_data, ALIGN_UP(bytes, 4), flash_encryption_enabled) != ESP_OK) {
257 return false;
258 }
259
260 offset += bytes;
261 bytes_written += bytes;
262 bytes_remaining -= bytes;
263 }
264
265 return true;
266}
267
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530268int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
269 uint32_t len)
270{
271 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
272 return -1;
273 }
274
275 const uint32_t end_offset = off + len;
276 if (end_offset > fa->fa_size) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300277 BOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530278 return -1;
279 }
280
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530281 const uint32_t start_addr = fa->fa_off + off;
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300282 BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530283
Gustavo Henrique Nihei1eb3eca2021-10-29 19:30:33 -0300284 bool success = aligned_flash_write(start_addr, src, len);
285 if (!success) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300286 BOOT_LOG_ERR("%s: Flash write failed", __func__);
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530287 return -1;
288 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530289
290 return 0;
291}
292
293int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
294{
295 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
296 return -1;
297 }
298
299 if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300300 BOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x",
301 __func__, (int)off, (int)len);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530302 return -1;
303 }
304
305 const uint32_t start_addr = fa->fa_off + off;
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300306 BOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530307
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530308 if (bootloader_flash_erase_range(start_addr, len) != ESP_OK) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300309 BOOT_LOG_ERR("%s: Flash erase failed", __func__);
Shubham Kulkarnicd869652021-07-20 11:44:08 +0530310 return -1;
311 }
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530312#if VALIDATE_PROGRAM_OP
313 for (size_t i = 0; i < len; i++) {
314 uint8_t *val = (void *)(start_addr + i);
315 if (*val != 0xff) {
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300316 BOOT_LOG_ERR("%s: Erase at 0x%x Failed", __func__, (int)val);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530317 assert(0);
318 }
319 }
320#endif
321
322 return 0;
323}
324
Gustavo Henrique Nihei4aa286d2021-11-24 14:54:56 -0300325uint32_t flash_area_align(const struct flash_area *area)
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530326{
Almir Okato14763b12021-11-25 00:45:26 -0300327 static size_t align = 0;
328
329 if (align == 0) {
330 bool flash_encryption_enabled = esp_flash_encryption_enabled();
331
332 if (flash_encryption_enabled) {
333 align = 32;
334 } else {
335 align = 4;
336 }
337 }
338 return align;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530339}
340
341uint8_t flash_area_erased_val(const struct flash_area *area)
342{
343 return 0xff;
344}
345
346int flash_area_get_sectors(int fa_id, uint32_t *count,
347 struct flash_sector *sectors)
348{
349 const struct flash_area *fa = prv_lookup_flash_area(fa_id);
350 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
351 return -1;
352 }
353
354 const size_t sector_size = FLASH_SECTOR_SIZE;
355 uint32_t total_count = 0;
356 for (size_t off = 0; off < fa->fa_size; off += sector_size) {
357 // Note: Offset here is relative to flash area, not device
358 sectors[total_count].fs_off = off;
359 sectors[total_count].fs_size = sector_size;
360 total_count++;
361 }
362
363 *count = total_count;
364 return 0;
365}
366
Almir Okato707a69d2022-09-23 15:22:07 -0300367int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector)
368{
369 sector->fs_off = (off / FLASH_SECTOR_SIZE) * FLASH_SECTOR_SIZE;
370 sector->fs_size = FLASH_SECTOR_SIZE;
371
372 return 0;
373}
374
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530375int flash_area_id_from_multi_image_slot(int image_index, int slot)
376{
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300377 BOOT_LOG_DBG("%s", __func__);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530378 switch (slot) {
379 case 0:
380 return FLASH_AREA_IMAGE_PRIMARY(image_index);
381 case 1:
382 return FLASH_AREA_IMAGE_SECONDARY(image_index);
383 }
384
Gustavo Henrique Niheid985d222021-11-12 14:21:12 -0300385 BOOT_LOG_ERR("Unexpected Request: image_index=%d, slot=%d", image_index, slot);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530386 return -1; /* flash_area_open will fail on that */
387}
388
389int flash_area_id_from_image_slot(int slot)
390{
Almir Okatod5320292021-06-18 02:00:40 -0300391 return flash_area_id_from_multi_image_slot(0, slot);
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530392}
393
394int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa)
395{
Almir Okatod5320292021-06-18 02:00:40 -0300396 return -1;
Shubham Kulkarni052561d2021-07-20 11:42:44 +0530397}
398
399void mcuboot_assert_handler(const char *file, int line, const char *func)
400{
401 ets_printf("assertion failed: file \"%s\", line %d, func: %s\n", file, line, func);
402 abort();
403}