|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 Espressif Systems (Shanghai) Co., Ltd. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +#include <esp32c3/rom/cache.h> |
| 8 | +#include "soc/cache_memory.h" |
| 9 | +#include "soc/extmem_reg.h" |
| 10 | +#include <bootloader_flash_priv.h> |
| 11 | + |
| 12 | +#include <zephyr.h> |
| 13 | +#include <soc.h> |
| 14 | +#include <storage/flash_map.h> |
| 15 | +#include <esp_log.h> |
| 16 | +#include <stdlib.h> |
| 17 | + |
| 18 | +#ifdef CONFIG_BOOTLOADER_MCUBOOT |
| 19 | +#define HDR_ATTR __attribute__((section(".entry_addr"))) __attribute__((used)) |
| 20 | + |
| 21 | +extern uint32_t _image_irom_start, _image_irom_size, _image_irom_vaddr; |
| 22 | +extern uint32_t _image_drom_start, _image_drom_size, _image_drom_vaddr; |
| 23 | + |
| 24 | +void __start(void); |
| 25 | + |
| 26 | +static HDR_ATTR void (*_entry_point)(void) = &__start; |
| 27 | + |
| 28 | +static int map_rom_segments(void) |
| 29 | +{ |
| 30 | + int rc = 0; |
| 31 | + |
| 32 | + size_t _partition_offset = FLASH_AREA_OFFSET(image_0); |
| 33 | + uint32_t _app_irom_start = _partition_offset + (uint32_t)&_image_irom_start; |
| 34 | + uint32_t _app_irom_size = (uint32_t)&_image_irom_size; |
| 35 | + uint32_t _app_irom_vaddr = (uint32_t)&_image_irom_vaddr; |
| 36 | + |
| 37 | + uint32_t _app_drom_start = _partition_offset + (uint32_t)&_image_drom_start; |
| 38 | + uint32_t _app_drom_size = (uint32_t)&_image_drom_size; |
| 39 | + uint32_t _app_drom_vaddr = (uint32_t)&_image_drom_vaddr; |
| 40 | + |
| 41 | + uint32_t autoload = esp_rom_Cache_Suspend_ICache(); |
| 42 | + |
| 43 | + esp_rom_Cache_Invalidate_ICache_All(); |
| 44 | + |
| 45 | + /* Clear the MMU entries that are already set up, |
| 46 | + * so the new app only has the mappings it creates. |
| 47 | + */ |
| 48 | + for (size_t i = 0; i < FLASH_MMU_TABLE_SIZE; i++) { |
| 49 | + FLASH_MMU_TABLE[i] = MMU_TABLE_INVALID_VAL; |
| 50 | + } |
| 51 | + |
| 52 | + uint32_t drom_page_count = bootloader_cache_pages_to_map(_app_drom_size, _app_drom_vaddr); |
| 53 | + |
| 54 | + rc |= esp_rom_Cache_Dbus_MMU_Set(MMU_ACCESS_FLASH, _app_drom_vaddr & 0xffff0000, |
| 55 | + _app_drom_start & 0xffff0000, 64, drom_page_count, 0); |
| 56 | + |
| 57 | + uint32_t irom_page_count = bootloader_cache_pages_to_map(_app_irom_size, _app_irom_vaddr); |
| 58 | + |
| 59 | + rc |= esp_rom_Cache_Ibus_MMU_Set(MMU_ACCESS_FLASH, _app_irom_vaddr & 0xffff0000, |
| 60 | + _app_irom_start & 0xffff0000, 64, irom_page_count, 0); |
| 61 | + |
| 62 | + REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_IBUS); |
| 63 | + REG_CLR_BIT(EXTMEM_ICACHE_CTRL1_REG, EXTMEM_ICACHE_SHUT_DBUS); |
| 64 | + |
| 65 | + esp_rom_Cache_Resume_ICache(autoload); |
| 66 | + |
| 67 | + return rc; |
| 68 | +} |
| 69 | +#endif |
| 70 | + |
| 71 | +void __start(void) |
| 72 | +{ |
| 73 | +#ifdef CONFIG_BOOTLOADER_MCUBOOT |
| 74 | + int err = map_rom_segments(); |
| 75 | + |
| 76 | + if (err != 0) { |
| 77 | + ets_printf("Failed to setup XIP, aborting\n"); |
| 78 | + abort(); |
| 79 | + } |
| 80 | +#endif |
| 81 | + __esp_platform_start(); |
| 82 | +} |
0 commit comments