blob: 828fc9eaecb2f5165185f646a410219d95e82ded [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
15#define ARRAY_SIZE(arr) sizeof(arr)/sizeof(arr[0])
16
17#define BOOTLOADER_START_ADDRESS 0x1000
18#define BOOTLOADER_SIZE CONFIG_ESP_BOOTLOADER_SIZE
19#define APPLICATION_PRIMARY_START_ADDRESS CONFIG_ESP_APPLICATION_PRIMARY_START_ADDRESS
20#define APPLICATION_SECONDARY_START_ADDRESS CONFIG_ESP_APPLICATION_SECONDARY_START_ADDRESS
21#define APPLICATION_SIZE CONFIG_ESP_APPLICATION_SIZE
22#define SCRATCH_OFFSET CONFIG_ESP_SCRATCH_OFFSET
23#define SCRATCH_SIZE CONFIG_ESP_SCRATCH_SIZE
24
25#define FLASH_SECTOR_SIZE 4096
26
27extern int ets_printf(const char *fmt, ...);
28
29static const struct flash_area bootloader = {
30 .fa_id = FLASH_AREA_BOOTLOADER,
31 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
32 .fa_off = BOOTLOADER_START_ADDRESS,
33 .fa_size = BOOTLOADER_SIZE,
34};
35
36static const struct flash_area primary_img0 = {
37 .fa_id = FLASH_AREA_IMAGE_PRIMARY(0),
38 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
39 .fa_off = APPLICATION_PRIMARY_START_ADDRESS,
40 .fa_size = APPLICATION_SIZE,
41};
42
43static const struct flash_area secondary_img0 = {
44 .fa_id = FLASH_AREA_IMAGE_SECONDARY(0),
45 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
46 .fa_off = APPLICATION_SECONDARY_START_ADDRESS,
47 .fa_size = APPLICATION_SIZE,
48};
49
50static const struct flash_area scratch_img0 = {
51 .fa_id = FLASH_AREA_IMAGE_SCRATCH,
52 .fa_device_id = FLASH_DEVICE_INTERNAL_FLASH,
53 .fa_off = SCRATCH_OFFSET,
54 .fa_size = SCRATCH_SIZE,
55};
56
57static const struct flash_area *s_flash_areas[] = {
58 &bootloader,
59 &primary_img0,
60 &secondary_img0,
61 &scratch_img0,
62};
63
64static const struct flash_area *prv_lookup_flash_area(uint8_t id) {
65 for (size_t i = 0; i < ARRAY_SIZE(s_flash_areas); i++) {
66 const struct flash_area *area = s_flash_areas[i];
67 if (id == area->fa_id) {
68 return area;
69 }
70 }
71 return NULL;
72}
73
74int flash_area_open(uint8_t id, const struct flash_area **area_outp)
75{
76 MCUBOOT_LOG_DBG("%s: ID=%d", __func__, (int)id);
77 const struct flash_area *area = prv_lookup_flash_area(id);
78 *area_outp = area;
79 return area != NULL ? 0 : -1;
80}
81
82void flash_area_close(const struct flash_area *area)
83{
84
85}
86
87int flash_area_read(const struct flash_area *fa, uint32_t off, void *dst,
88 uint32_t len)
89{
90 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
91 return -1;
92 }
93
94 const uint32_t end_offset = off + len;
95 if (end_offset > fa->fa_size) {
96 MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
97 return -1;
98 }
99
100 return 0;
101}
102
103int flash_area_write(const struct flash_area *fa, uint32_t off, const void *src,
104 uint32_t len)
105{
106 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
107 return -1;
108 }
109
110 const uint32_t end_offset = off + len;
111 if (end_offset > fa->fa_size) {
112 MCUBOOT_LOG_ERR("%s: Out of Bounds (0x%x vs 0x%x)", __func__, end_offset, fa->fa_size);
113 return -1;
114 }
115
116 const uint32_t addr = fa->fa_off + off;
117 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)addr, (int)len);
118
119#if VALIDATE_PROGRAM_OP
120 if (memcmp((void *)addr, src, len) != 0) {
121 MCUBOOT_LOG_ERR("%s: Program Failed", __func__);
122 assert(0);
123 }
124#endif
125
126 return 0;
127}
128
129int flash_area_erase(const struct flash_area *fa, uint32_t off, uint32_t len)
130{
131 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
132 return -1;
133 }
134
135 if ((len % FLASH_SECTOR_SIZE) != 0 || (off % FLASH_SECTOR_SIZE) != 0) {
136 MCUBOOT_LOG_ERR("%s: Not aligned on sector Offset: 0x%x Length: 0x%x", __func__,
137 (int)off, (int)len);
138 return -1;
139 }
140
141 const uint32_t start_addr = fa->fa_off + off;
142 MCUBOOT_LOG_DBG("%s: Addr: 0x%08x Length: %d", __func__, (int)start_addr, (int)len);
143
144#if VALIDATE_PROGRAM_OP
145 for (size_t i = 0; i < len; i++) {
146 uint8_t *val = (void *)(start_addr + i);
147 if (*val != 0xff) {
148 MCUBOOT_LOG_ERR("%s: Erase at 0x%x Failed", __func__, (int)val);
149 assert(0);
150 }
151 }
152#endif
153
154 return 0;
155}
156
157size_t flash_area_align(const struct flash_area *area)
158{
159 return 4;
160}
161
162uint8_t flash_area_erased_val(const struct flash_area *area)
163{
164 return 0xff;
165}
166
167int flash_area_get_sectors(int fa_id, uint32_t *count,
168 struct flash_sector *sectors)
169{
170 const struct flash_area *fa = prv_lookup_flash_area(fa_id);
171 if (fa->fa_device_id != FLASH_DEVICE_INTERNAL_FLASH) {
172 return -1;
173 }
174
175 const size_t sector_size = FLASH_SECTOR_SIZE;
176 uint32_t total_count = 0;
177 for (size_t off = 0; off < fa->fa_size; off += sector_size) {
178 // Note: Offset here is relative to flash area, not device
179 sectors[total_count].fs_off = off;
180 sectors[total_count].fs_size = sector_size;
181 total_count++;
182 }
183
184 *count = total_count;
185 return 0;
186}
187
188int flash_area_id_from_multi_image_slot(int image_index, int slot)
189{
190 MCUBOOT_LOG_DBG("%s", __func__);
191 switch (slot) {
192 case 0:
193 return FLASH_AREA_IMAGE_PRIMARY(image_index);
194 case 1:
195 return FLASH_AREA_IMAGE_SECONDARY(image_index);
196 }
197
198 MCUBOOT_LOG_ERR("Unexpected Request: image_index=%d, slot=%d", image_index, slot);
199 return -1; /* flash_area_open will fail on that */
200}
201
202int flash_area_id_from_image_slot(int slot)
203{
204 return flash_area_id_from_multi_image_slot(0, slot);
205}
206
207int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa)
208{
209 return -1;
210}
211
212void mcuboot_assert_handler(const char *file, int line, const char *func)
213{
214 ets_printf("assertion failed: file \"%s\", line %d, func: %s\n", file, line, func);
215 abort();
216}