Skip to content

Commit 6db5f0b

Browse files
committed
esp32: Adds support for custom app flash mappings location
1 parent 5da4357 commit 6db5f0b

File tree

16 files changed

+817
-520
lines changed

16 files changed

+817
-520
lines changed

.gitlab-ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,10 @@ build_test_app:
268268
- testing/esp/test_apps/gen_ut_app/output/gcov_single/gen_ut_app.bin
269269
- testing/esp/test_apps/gen_ut_app/output/gcov_single/partitions_singleapp.bin
270270
- testing/esp/test_apps/gen_ut_app/output/gcov_single/bootloader/bootloader.bin
271+
- testing/esp/test_apps/gen_ut_app/output/multi_app_images/gen_ut_app.elf
272+
- testing/esp/test_apps/gen_ut_app/output/multi_app_images/gen_ut_app.bin
273+
- testing/esp/test_apps/gen_ut_app/output/multi_app_images/partitions_multi_apps.bin
274+
- testing/esp/test_apps/gen_ut_app/output/multi_app_images/bootloader/bootloader.bin
271275
expire_in: 2 weeks
272276
script:
273277
- *add_gitlab_key

contrib/loaders/flash/esp32/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,11 @@ CFLAGS += $(INCLUDES) $(DEFINES)
7373

7474
LDFLAGS += -L$(IDF_PATH)/components/esp32/ld -Tstub.ld -T$(IDF_PATH)/components/esp32/ld/esp32.rom.ld \
7575
-T$(IDF_PATH)/components/esp32/ld/esp32.rom.spiram_incompatible_fns.ld -T$(IDF_PATH)/components/esp32/ld/esp32.peripherals.ld \
76+
-T$(IDF_PATH)/components/esp32/ld/esp32.rom.libgcc.ld \
7677
-Wl,--start-group -lgcc -lc -Wl,--end-group
7778

79+
CWD := $(shell pwd)
80+
7881
$(STUB_ELF): $(SRCS) stub.ld $(BUILD_DIR)
7982
@echo " CC $^ -> $@"
8083
$(Q) $(CROSS)gcc $(CFLAGS) -DSTUB_IMAGE=1 -Wl,-Map=$(@:.elf=.map) -o $@ $(LDFLAGS) $(filter %.c, $^)
@@ -100,6 +103,8 @@ $(STUB_IMAGE_HDR): $(STUB_ELF)
100103
$(Q) $(CROSS)readelf -S $^ | fgrep .bss | awk '{print $$7"UL"}' >> $(STUB_IMAGE_HDR)
101104
$(Q) @echo -n "\n#define ESP32_STUB_ENTRY_ADDR 0x0" >> $(STUB_IMAGE_HDR)
102105
$(Q) $(CROSS)readelf -s $^ | fgrep stub_main | awk '{print $$2"UL"}' >> $(STUB_IMAGE_HDR)
106+
$(Q) @echo -n "\n//#define ESP32_STUB_BUILD_IDF_REV " >> $(STUB_IMAGE_HDR)
107+
$(Q) cd $(IDF_PATH); git rev-parse --short HEAD >> $(CWD)/$(STUB_IMAGE_HDR)
103108

104109
clean:
105110
$(Q) rm -rf $(BUILD_DIR) $(STUB_CODE_SECT) $(STUB_DATA_SECT) $(STUB_WRAPPER) $(STUB_IMAGE_HDR)

contrib/loaders/flash/esp32/stub_flasher.c

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,61 @@ static inline bool stub_flash_should_map(uint32_t load_addr)
551551
|| (load_addr >= SOC_DROM_LOW && load_addr < SOC_DROM_HIGH);
552552
}
553553

554-
static int stub_flash_get_map(uint32_t maps_addr)
554+
static int stub_flash_get_app_mappings(uint32_t off, struct esp32_flash_mapping *flash_map)
555+
{
556+
esp_image_header_t img_hdr;
557+
uint16_t maps_num = 0;
558+
559+
esp_rom_spiflash_result_t rc = esp_rom_spiflash_read(off, (uint32_t *)&img_hdr, sizeof(img_hdr));
560+
if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
561+
STUB_LOGE("Failed to read app image header (%d)!\n", rc);
562+
return ESP32_STUB_ERR_FAIL;
563+
}
564+
if (img_hdr.magic != ESP_IMAGE_HEADER_MAGIC) {
565+
STUB_LOGE("Invalid magic number 0x%x in app image!\n", img_hdr.magic);
566+
return ESP32_STUB_ERR_FAIL;
567+
}
568+
569+
STUB_LOGI("Found app image: magic 0x%x, %d segments, entry @ 0x%x\n", img_hdr.magic, img_hdr.segment_count, img_hdr.entry_addr);
570+
uint32_t flash_addr = off + sizeof(img_hdr);
571+
for (int k = 0; k < img_hdr.segment_count; k++) {
572+
esp_image_segment_header_t seg_hdr;
573+
rc = esp_rom_spiflash_read(flash_addr, (uint32_t *)&seg_hdr, sizeof(seg_hdr));
574+
if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
575+
STUB_LOGE("Failed to read app segment header (%d)!\n", rc);
576+
return ESP32_STUB_ERR_FAIL;
577+
}
578+
STUB_LOGI("App segment %d: %d bytes @ 0x%x\n", k, seg_hdr.data_len, seg_hdr.load_addr);
579+
if (stub_flash_should_map(seg_hdr.load_addr)) {
580+
STUB_LOGI("Mapped segment %d: %d bytes @ 0x%x -> 0x%x\n", maps_num, seg_hdr.data_len, flash_addr + sizeof(seg_hdr), seg_hdr.load_addr);
581+
if (maps_num < ESP32_STUB_FLASH_MAPPINGS_MAX_NUM) {
582+
flash_map->maps[maps_num].phy_addr = flash_addr + sizeof(seg_hdr);
583+
flash_map->maps[maps_num].load_addr = seg_hdr.load_addr;
584+
flash_map->maps[maps_num].size = seg_hdr.data_len;
585+
maps_num++;
586+
} else {
587+
break;
588+
}
589+
}
590+
flash_addr += sizeof(seg_hdr) + seg_hdr.data_len;
591+
}
592+
flash_map->maps_num = maps_num;
593+
return ESP32_STUB_ERR_OK;
594+
}
595+
596+
static int stub_flash_get_map(uint32_t app_off, uint32_t maps_addr)
555597
{
556598
esp_rom_spiflash_result_t rc;
557-
struct esp32_flash_mapping *flash_map = (struct esp32_flash_mapping *)maps_addr;
558599
esp_partition_info_t parts[ESP32_STUB_PARTITION_TABLE_MAX_ENTRIES];
600+
struct esp32_flash_mapping *flash_map = (struct esp32_flash_mapping *)maps_addr;
559601
uint32_t flash_size = stub_flash_get_size();
560-
uint32_t maps_num = 0;
561602

562-
STUB_LOGD("%s: 0x%x\n", __func__, flash_map);
603+
STUB_LOGD("%s: 0x%x 0x%x\n", __func__, app_off, maps_addr);
604+
flash_map->maps_num = 0;
605+
if (app_off != (uint32_t)-1) {
606+
return stub_flash_get_app_mappings(app_off, flash_map);
607+
}
608+
563609
rc = esp_rom_spiflash_read(ESP_PARTITION_TABLE_OFFSET, (uint32_t *)parts, sizeof(parts));
564610
if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
565611
STUB_LOGE("Failed to read partitions table (%d)!\n", rc);
@@ -582,43 +628,12 @@ static int stub_flash_get_map(uint32_t maps_addr)
582628
}
583629
STUB_LOGD("Found partition %d, m 0x%x, t 0x%x, st 0x%x, l '%s'\n", i, parts[i].magic, parts[i].type, parts[i].subtype, parts[i].label);
584630
if (parts[i].type == PART_TYPE_APP) {
585-
STUB_LOGI("Found app partition: '%s' %d KB @ 0x%x\n", parts[i].label, parts[i].pos.size / 1024, parts[i].pos.offset);
586-
esp_image_header_t img_hdr;
587-
rc = esp_rom_spiflash_read(pos->offset, (uint32_t *)&img_hdr, sizeof(img_hdr));
588-
if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
589-
STUB_LOGE("Failed to read app image header (%d)!\n", rc);
590-
return ESP32_STUB_ERR_FAIL;
591-
}
592-
if (img_hdr.magic != ESP_IMAGE_HEADER_MAGIC) {
593-
STUB_LOGE("Invalid magic number 0x%x in app image!\n", i, img_hdr.magic);
594-
} else {
595-
STUB_LOGI("Found app image: magic 0x%x, %d segments, entry @ 0x%x\n", img_hdr.magic, img_hdr.segment_count, img_hdr.entry_addr);
596-
uint32_t flash_addr = pos->offset + sizeof(img_hdr);
597-
for (int k = 0; k < img_hdr.segment_count; k++) {
598-
esp_image_segment_header_t seg_hdr;
599-
rc = esp_rom_spiflash_read(flash_addr, (uint32_t *)&seg_hdr, sizeof(seg_hdr));
600-
if (rc != ESP_ROM_SPIFLASH_RESULT_OK) {
601-
STUB_LOGE("Failed to read app segment header (%d)!\n", rc);
602-
return ESP32_STUB_ERR_FAIL;
603-
}
604-
STUB_LOGI("App segment %d: %d bytes @ 0x%x\n", k, seg_hdr.data_len, seg_hdr.load_addr);
605-
if (stub_flash_should_map(seg_hdr.load_addr)) {
606-
STUB_LOGI("Mapped segment %d: %d bytes @ 0x%x -> 0x%x\n", maps_num, seg_hdr.data_len, flash_addr + sizeof(seg_hdr), seg_hdr.load_addr);
607-
if (maps_num < flash_map->maps_num) {
608-
flash_map->maps[maps_num].phy_addr = flash_addr + sizeof(seg_hdr);
609-
flash_map->maps[maps_num].load_addr = seg_hdr.load_addr;
610-
flash_map->maps[maps_num].size = seg_hdr.data_len;
611-
}
612-
maps_num++;
613-
}
614-
flash_addr += sizeof(seg_hdr) + seg_hdr.data_len;
615-
}
616-
if (maps_num > 0) {
617-
STUB_LOGI("IROM base 0x%x, maps num %d\n", pos->offset, maps_num);
618-
flash_map->maps_num = maps_num;
619-
break;
620-
}
631+
STUB_LOGI("Found app partition: '%s' %d KB @ 0x%x\n", parts[i].label, pos->size / 1024, pos->offset);
632+
int ret = stub_flash_get_app_mappings(pos->offset, flash_map);
633+
if (ret != ESP32_STUB_ERR_OK) {
634+
return ret;
621635
}
636+
break;
622637
}
623638
}
624639
return ESP32_STUB_ERR_OK;
@@ -775,7 +790,7 @@ static int stub_flash_handler(int cmd, va_list ap)
775790
ret = stub_flash_get_size();
776791
break;
777792
case ESP32_STUB_CMD_FLASH_MAP_GET:
778-
ret = stub_flash_get_map(arg1);
793+
ret = stub_flash_get_map(arg1, arg2);
779794
break;
780795
case ESP32_STUB_CMD_FLASH_BP_SET:
781796
ret = stub_flash_set_bp(arg1, arg2, arg3);
@@ -801,6 +816,11 @@ static int stub_flash_handler(int cmd, va_list ap)
801816
return ret;
802817
}
803818

819+
void ets_update_cpu_frequency(uint32_t ticks_per_us)
820+
{
821+
/* do nothing for stub */
822+
}
823+
804824
#if STUB_LOG_LOCAL_LEVEL > STUB_LOG_NONE
805825
static void clock_configure(void)
806826
{
@@ -826,7 +846,6 @@ static void clock_configure(void)
826846
clk_cfg.fast_freq = rtc_clk_fast_freq_get();
827847
rtc_clk_init(clk_cfg);
828848
}
829-
830849
static void uart_console_configure(void)
831850
{
832851
uartAttach();

0 commit comments

Comments
 (0)