Skip to content

Commit 637c330

Browse files
committed
boards/esp32: parse MCUboot load header in board_boot_image
Read the MCUboot-Espressif load header at hdr_size and use it to populate entry point plus IROM/DROM mapping parameters. This replaces the incorrect esp_image_header_t parsing path in board_boot_image() for this flow and aligns with the image layout used by MCUboot/nxboot on ESP32. Signed-off-by: aviralgarg05 <gargaviral99@gmail.com>
1 parent c065fd2 commit 637c330

File tree

1 file changed

+54
-59
lines changed

1 file changed

+54
-59
lines changed

boards/xtensa/esp32/common/src/esp32_boot_image.c

Lines changed: 54 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include <debug.h>
2828
#include <fcntl.h>
29+
#include <inttypes.h>
30+
#include <stdint.h>
2931
#include <sys/ioctl.h>
3032
#include <unistd.h>
3133

@@ -34,19 +36,43 @@
3436
#include <nuttx/cache.h>
3537
#include <nuttx/irq.h>
3638

37-
#include "esp_app_format.h"
3839
#include "esp_loader.h"
3940

4041
/****************************************************************************
4142
* Pre-processor Definitions
4243
****************************************************************************/
4344

44-
#define ESP32_APP_IMAGE_MAGIC 0xE9
45+
#define ESP32_APP_LOAD_HEADER_MAGIC 0xace637d3
4546

4647
/****************************************************************************
4748
* Private Types
4849
****************************************************************************/
4950

51+
struct esp32_load_header_s
52+
{
53+
uint32_t header_magic;
54+
uint32_t entry_addr;
55+
uint32_t iram_dest_addr;
56+
uint32_t iram_flash_offset;
57+
uint32_t iram_size;
58+
uint32_t dram_dest_addr;
59+
uint32_t dram_flash_offset;
60+
uint32_t dram_size;
61+
uint32_t lp_rtc_iram_dest_addr;
62+
uint32_t lp_rtc_iram_flash_offset;
63+
uint32_t lp_rtc_iram_size;
64+
uint32_t lp_rtc_dram_dest_addr;
65+
uint32_t lp_rtc_dram_flash_offset;
66+
uint32_t lp_rtc_dram_size;
67+
uint32_t irom_map_addr;
68+
uint32_t irom_flash_offset;
69+
uint32_t irom_size;
70+
uint32_t drom_map_addr;
71+
uint32_t drom_flash_offset;
72+
uint32_t drom_size;
73+
uint32_t reserved[4];
74+
};
75+
5076
struct esp32_boot_loader_args_s
5177
{
5278
uint32_t entry_addr;
@@ -122,7 +148,7 @@ static void IRAM_ATTR esp32_boot_loader_stub(void *arg)
122148
*
123149
* Input Parameters:
124150
* path - Path to the image file/partition
125-
* hdr_size - Size of the image header (unused for ESP32)
151+
* hdr_size - Size of the prepended image header (e.g. MCUboot/nxboot)
126152
*
127153
* Returned Value:
128154
* Does not return on success; returns error code on failure.
@@ -134,20 +160,17 @@ int board_boot_image(FAR const char *path, uint32_t hdr_size)
134160
int fd;
135161
int ret;
136162
uint32_t offset;
137-
esp_image_header_t image_header;
138-
esp_image_segment_header_t segment_hdr;
163+
struct esp32_load_header_s load_header;
139164
struct esp32_boot_loader_args_s args =
140165
{
141166
0
142167
};
143168

144-
uint32_t current_offset;
145-
int i;
146-
147-
/* Check for legacy format (not supported) */
169+
/* Legacy/simple boot image formats are not supported */
148170

149-
#ifdef CONFIG_ESP32_APP_FORMAT_LEGACY
150-
ferr("ERROR: Legacy format not supported for board_boot_image\n");
171+
#if defined(CONFIG_ESP32_APP_FORMAT_LEGACY) || \
172+
defined(CONFIG_ESPRESSIF_SIMPLE_BOOT)
173+
ferr("ERROR: Unsupported image format for board_boot_image\n");
151174
return -ENOTSUP;
152175
#endif
153176

@@ -174,67 +197,39 @@ int board_boot_image(FAR const char *path, uint32_t hdr_size)
174197

175198
if (hdr_size > 0)
176199
{
177-
lseek(fd, hdr_size, SEEK_SET);
200+
if (lseek(fd, hdr_size, SEEK_SET) < 0)
201+
{
202+
ferr("ERROR: Failed to seek load header: %d\n", errno);
203+
close(fd);
204+
return -errno;
205+
}
178206
}
179207

180-
/* Read image header */
208+
/* Read image load header generated for MCUboot app format */
181209

182-
ret = read(fd, &image_header, sizeof(esp_image_header_t));
183-
if (ret != sizeof(esp_image_header_t))
210+
ret = read(fd, &load_header, sizeof(struct esp32_load_header_s));
211+
if (ret != sizeof(struct esp32_load_header_s))
184212
{
185-
ferr("ERROR: Failed to read image header: %d\n", errno);
213+
ferr("ERROR: Failed to read image load header: %d\n", errno);
186214
close(fd);
187215
return -errno;
188216
}
189217

190-
if (image_header.magic != ESP32_APP_IMAGE_MAGIC)
218+
if (load_header.header_magic != ESP32_APP_LOAD_HEADER_MAGIC)
191219
{
192-
ferr("ERROR: Invalid image magic: 0x%02x\n", image_header.magic);
220+
ferr("ERROR: Invalid load header magic: 0x%08" PRIx32 "\n",
221+
load_header.header_magic);
193222
close(fd);
194223
return -EINVAL;
195224
}
196225

197-
args.entry_addr = image_header.entry_addr;
198-
current_offset = hdr_size + sizeof(esp_image_header_t);
199-
200-
/* Parse segments */
201-
202-
for (i = 0; i < image_header.segment_count; i++)
203-
{
204-
ret = read(fd, &segment_hdr, sizeof(esp_image_segment_header_t));
205-
if (ret != sizeof(esp_image_segment_header_t))
206-
{
207-
ferr("ERROR: Failed to read segment header: %d\n", errno);
208-
close(fd);
209-
return -errno;
210-
}
211-
212-
/* Check for IROM/DROM segments */
213-
214-
if (segment_hdr.load_addr >= 0x3f400000 &&
215-
segment_hdr.load_addr < 0x3f800000) /* DROM */
216-
{
217-
args.drom_addr =
218-
offset + current_offset + sizeof(esp_image_segment_header_t);
219-
args.drom_vaddr = segment_hdr.load_addr;
220-
args.drom_size = segment_hdr.data_len;
221-
}
222-
else if (segment_hdr.load_addr >= 0x400d0000 &&
223-
segment_hdr.load_addr < 0x40400000) /* IROM */
224-
{
225-
args.irom_addr =
226-
offset + current_offset + sizeof(esp_image_segment_header_t);
227-
args.irom_vaddr = segment_hdr.load_addr;
228-
args.irom_size = segment_hdr.data_len;
229-
}
230-
231-
current_offset += sizeof(esp_image_segment_header_t) +
232-
segment_hdr.data_len;
233-
234-
/* Advance file pointer to next segment */
235-
236-
lseek(fd, segment_hdr.data_len, SEEK_CUR);
237-
}
226+
args.entry_addr = load_header.entry_addr;
227+
args.drom_addr = offset + load_header.drom_flash_offset;
228+
args.drom_vaddr = load_header.drom_map_addr;
229+
args.drom_size = load_header.drom_size;
230+
args.irom_addr = offset + load_header.irom_flash_offset;
231+
args.irom_vaddr = load_header.irom_map_addr;
232+
args.irom_size = load_header.irom_size;
238233

239234
close(fd);
240235

0 commit comments

Comments
 (0)