feat(bootloader): modify to output something

using CONFIG_TARGET_PLATFORM_ESP32 to disable original bootloader_support code for ESP32
This commit is contained in:
Wu Jian Gang
2018-06-18 00:42:38 +08:00
committed by Dong Heng
parent 5ca0b194ef
commit 1bc665a4ff
21 changed files with 406 additions and 363 deletions

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include "rom/uart.h"
#include "rom/rtc.h"
#include "soc/soc.h"
@ -59,3 +64,4 @@ void bootloader_clock_configure()
}
#endif
}
#endif

View File

@ -11,10 +11,14 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <stdbool.h>
#include <assert.h>
#include "string.h"
#include "sdkconfig.h"
#include "esp_err.h"
#include "esp_log.h"
#include "rom/spi_flash.h"
@ -153,3 +157,5 @@ bool bootloader_common_erase_part_type_data(const char *list_erase, bool ota_dat
return ret;
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <stddef.h>
#include <bootloader_flash.h>
@ -248,3 +253,80 @@ esp_err_t bootloader_flash_erase_sector(size_t sector)
}
#endif
#endif
#ifdef CONFIG_TARGET_PLATFORM_ESP8266
#include <stdint.h>
#include <stdbool.h>
#include "esp_err.h"
#include "esp_log.h"
static const char *TAG = "bootloader_flash";
typedef enum { SPI_FLASH_RESULT_OK = 0,
SPI_FLASH_RESULT_ERR = 1,
SPI_FLASH_RESULT_TIMEOUT = 2 } SpiFlashOpResult;
SpiFlashOpResult SPIRead(uint32_t addr, void *dst, uint32_t size);
SpiFlashOpResult SPIWrite(uint32_t addr, const uint8_t *src, uint32_t size);
SpiFlashOpResult SPIEraseSector(uint32_t sector_num);
static esp_err_t bootloader_flash_read_no_decrypt(size_t src_addr, void *dest, size_t size)
{
SPIRead(src_addr, dest, size);
return ESP_OK;
}
esp_err_t bootloader_flash_read(size_t src_addr, void *dest, size_t size, bool allow_decrypt)
{
if (src_addr & 3) {
ESP_LOGE(TAG, "bootloader_flash_read src_addr 0x%x not 4-byte aligned", src_addr);
return ESP_FAIL;
}
if (size & 3) {
ESP_LOGE(TAG, "bootloader_flash_read size 0x%x not 4-byte aligned", size);
return ESP_FAIL;
}
if ((intptr_t)dest & 3) {
ESP_LOGE(TAG, "bootloader_flash_read dest 0x%x not 4-byte aligned", (intptr_t)dest);
return ESP_FAIL;
}
return bootloader_flash_read_no_decrypt(src_addr, dest, size);
}
esp_err_t bootloader_flash_write(size_t dest_addr, void *src, size_t size, bool write_encrypted)
{
esp_err_t err;
size_t alignment = write_encrypted ? 32 : 4;
if ((dest_addr % alignment) != 0) {
ESP_LOGE(TAG, "bootloader_flash_write dest_addr 0x%x not %d-byte aligned", dest_addr, alignment);
return ESP_FAIL;
}
if ((size % alignment) != 0) {
ESP_LOGE(TAG, "bootloader_flash_write size 0x%x not %d-byte aligned", size, alignment);
return ESP_FAIL;
}
if (((intptr_t)src % 4) != 0) {
ESP_LOGE(TAG, "bootloader_flash_write src 0x%x not 4 byte aligned", (intptr_t)src);
return ESP_FAIL;
}
SPIWrite(dest_addr, src, size);
return ESP_OK;
}
esp_err_t bootloader_flash_erase_sector(size_t sector)
{
SPIEraseSector(sector);
return ESP_OK;
}
#endif

View File

@ -11,10 +11,15 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <string.h>
#include <stdint.h>
//#include <limits.h>
//#include <sys/param.h>
#include <limits.h>
#include <sys/param.h>
#include "esp_attr.h"
#include "esp_log.h"
@ -528,3 +533,164 @@ void __assert_func(const char *file, int line, const char *func, const char *exp
ESP_LOGE(TAG, "Assert failed in %s, %s:%d (%s)", func, file, line, expr);
while(1) {}
}
#endif
#ifdef CONFIG_TARGET_PLATFORM_ESP8266
#include <string.h>
#include "esp_err.h"
#include "esp_log.h"
#include "esp_image_format.h"
#include "esp_flash_partitions.h"
extern int _bss_start;
extern int _bss_end;
extern int _data_start;
extern int _data_end;
static const char* TAG = "boot";
static esp_err_t bootloader_main();
static void print_flash_info(const esp_image_header_t* pfhdr);
static void update_flash_config(const esp_image_header_t* pfhdr);
esp_err_t bootloader_init()
{
//Clear bss
memset(&_bss_start, 0, (&_bss_end - &_bss_start) * sizeof(_bss_start));
if(bootloader_main() != ESP_OK){
return ESP_FAIL;
}
return ESP_OK;
}
static esp_err_t bootloader_main()
{
esp_image_header_t fhdr;
if (bootloader_flash_read(ESP_BOOTLOADER_OFFSET, &fhdr, sizeof(esp_image_header_t), true) != ESP_OK) {
ESP_LOGE(TAG, "failed to load bootloader header!");
return ESP_FAIL;
}
ESP_LOGI(TAG, "ESP-IDF %s 2nd stage bootloader", IDF_VER);
ESP_LOGI(TAG, "compile time " __TIME__ );
print_flash_info(&fhdr);
update_flash_config(&fhdr);
return ESP_OK;
}
static void update_flash_config(const esp_image_header_t* pfhdr)
{
uint32_t size;
switch(pfhdr->spi_size) {
case ESP_IMAGE_FLASH_SIZE_1MB:
size = 1;
break;
case ESP_IMAGE_FLASH_SIZE_2MB:
case ESP_IMAGE_FLASH_SIZE_2MB_C1:
size = 2;
break;
case ESP_IMAGE_FLASH_SIZE_4MB:
case ESP_IMAGE_FLASH_SIZE_4MB_C1:
size = 4;
break;
case ESP_IMAGE_FLASH_SIZE_8MB:
size = 8;
break;
case ESP_IMAGE_FLASH_SIZE_16MB:
size = 16;
break;
default:
size = 2;
}
// Set flash chip size
// esp_rom_spiflash_config_param(g_rom_flashchip.device_id, size * 0x100000, 0x10000, 0x1000, 0x100, 0xffff);
// TODO: set mode
// TODO: set frequency
}
static void print_flash_info(const esp_image_header_t* phdr)
{
#if (BOOT_LOG_LEVEL >= BOOT_LOG_LEVEL_NOTICE)
ESP_LOGD(TAG, "magic %02x", phdr->magic );
ESP_LOGD(TAG, "segments %02x", phdr->segment_count );
ESP_LOGD(TAG, "spi_mode %02x", phdr->spi_mode );
ESP_LOGD(TAG, "spi_speed %02x", phdr->spi_speed );
ESP_LOGD(TAG, "spi_size %02x", phdr->spi_size );
const char* str;
switch ( phdr->spi_speed ) {
case ESP_IMAGE_SPI_SPEED_40M:
str = "40MHz";
break;
case ESP_IMAGE_SPI_SPEED_26M:
str = "26.7MHz";
break;
case ESP_IMAGE_SPI_SPEED_20M:
str = "20MHz";
break;
case ESP_IMAGE_SPI_SPEED_80M:
str = "80MHz";
break;
default:
str = "20MHz";
break;
}
ESP_LOGI(TAG, "SPI Speed : %s", str );
switch ( phdr->spi_mode ) {
case ESP_IMAGE_SPI_MODE_QIO:
str = "QIO";
break;
case ESP_IMAGE_SPI_MODE_QOUT:
str = "QOUT";
break;
case ESP_IMAGE_SPI_MODE_DIO:
str = "DIO";
break;
case ESP_IMAGE_SPI_MODE_DOUT:
str = "DOUT";
break;
default:
str = "QIO";
break;
}
ESP_LOGI(TAG, "SPI Mode : %s", str );
switch ( phdr->spi_size ) {
case ESP_IMAGE_FLASH_SIZE_1MB:
str = "1MB";
break;
case ESP_IMAGE_FLASH_SIZE_2MB:
case ESP_IMAGE_FLASH_SIZE_2MB_C1:
str = "2MB";
break;
case ESP_IMAGE_FLASH_SIZE_4MB:
case ESP_IMAGE_FLASH_SIZE_4MB_C1:
str = "4MB";
break;
case ESP_IMAGE_FLASH_SIZE_8MB:
str = "8MB";
break;
case ESP_IMAGE_FLASH_SIZE_16MB:
str = "16MB";
break;
default:
str = "2MB";
break;
}
ESP_LOGI(TAG, "SPI Flash Size : %s", str );
#endif
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include "bootloader_random.h"
#include "soc/cpu.h"
#include "soc/wdev_reg.h"
@ -143,3 +148,5 @@ void bootloader_random_disable(void)
CLEAR_PERI_REG_MASK(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_ENT_RTC);
SET_PERI_REG_BITS(RTC_CNTL_TEST_MUX_REG, RTC_CNTL_DTEST_RTC, 0, RTC_CNTL_DTEST_RTC_S);
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include "bootloader_sha.h"
#include <stdbool.h>
#include <string.h>
@ -164,3 +169,5 @@ void bootloader_sha256_finish(bootloader_sha256_handle_t handle, uint8_t *digest
}
#endif
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <string.h>
#include <stdint.h>
#include <limits.h>
@ -470,3 +475,5 @@ static void set_cache_and_start_app(
// use "movsp" instruction to reset stack back to where ROM stack starts.
(*entry)();
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include "esp_efuse.h"
#include "esp_log.h"
@ -58,3 +63,5 @@ void esp_efuse_disable_basic_rom_console(void)
esp_efuse_burn_new_values();
}
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <string.h>
#include <sys/param.h>
@ -571,3 +576,5 @@ static void debug_log_hash(const uint8_t *image_hash, const char *label)
ESP_LOGD(TAG, "%s: %s", label, hash_print);
#endif
}
#endif

View File

@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <strings.h>
#include "bootloader_flash.h"
@ -337,3 +341,5 @@ esp_err_t esp_flash_encrypt_region(uint32_t src_addr, size_t data_length)
ESP_LOGE(TAG, "flash operation failed: 0x%x", err);
return err;
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <string.h>
#include "esp_flash_partitions.h"
#include "esp_log.h"
@ -84,3 +89,5 @@ esp_err_t esp_partition_table_basic_verify(const esp_partition_info_t *partition
return ESP_ERR_INVALID_STATE;
}
#endif

View File

@ -11,6 +11,11 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <stddef.h>
#include <stdint.h>
#include "flash_qio_mode.h"
@ -280,3 +285,5 @@ static uint32_t execute_flash_command(uint8_t command, uint32_t mosi_data, uint8
SPIFLASH.ctrl.val = old_ctrl_reg;
return SPIFLASH.data_buf[0];
}
#endif

View File

@ -12,6 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include <string.h>
#include "esp_attr.h"
@ -202,3 +206,5 @@ esp_err_t esp_secure_boot_permanently_enable(void) {
return ESP_ERR_INVALID_STATE;
}
}
#endif

View File

@ -13,6 +13,8 @@
// limitations under the License.
#include "sdkconfig.h"
#ifdef CONFIG_TARGET_PLATFORM_ESP32
#include "bootloader_flash.h"
#include "bootloader_sha.h"
#include "esp_log.h"
@ -91,3 +93,5 @@ esp_err_t esp_secure_boot_verify_signature_block(const esp_secure_boot_sig_block
uECC_secp256r1());
return is_valid ? ESP_OK : ESP_ERR_IMAGE_INVALID;
}
#endif