2016-08-17 11:08:22 -04:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2016-10-19 05:05:37 -04:00
|
|
|
//
|
2016-08-17 11:08:22 -04:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// 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 <string.h>
|
|
|
|
#include <stdint.h>
|
2018-04-16 11:35:41 -04:00
|
|
|
#include <stdbool.h>
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2016-09-14 12:53:33 -04:00
|
|
|
#include "esp_log.h"
|
2016-10-27 04:17:28 -04:00
|
|
|
#include "rom/gpio.h"
|
2018-04-16 11:35:41 -04:00
|
|
|
#include "bootloader_config.h"
|
|
|
|
#include "bootloader_init.h"
|
|
|
|
#include "bootloader_utility.h"
|
|
|
|
#include "bootloader_common.h"
|
2016-08-17 11:08:22 -04:00
|
|
|
#include "sdkconfig.h"
|
2016-11-01 19:41:58 -04:00
|
|
|
#include "esp_image_format.h"
|
2016-09-14 12:53:33 -04:00
|
|
|
|
|
|
|
static const char* TAG = "boot";
|
2016-08-17 11:08:22 -04:00
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
static esp_err_t select_image (esp_image_metadata_t *image_data);
|
|
|
|
static int selected_boot_partition(const bootloader_state_t *bs);
|
2017-06-16 02:30:21 -04:00
|
|
|
/*
|
|
|
|
* We arrive here after the ROM bootloader finished loading this second stage bootloader from flash.
|
|
|
|
* The hardware is mostly uninitialized, flash cache is down and the app CPU is in reset.
|
|
|
|
* We do have a stack, so we can do the initialization in C.
|
|
|
|
*/
|
|
|
|
void call_start_cpu0()
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2018-04-16 11:35:41 -04:00
|
|
|
// 1. Hardware initialization
|
|
|
|
if(bootloader_init() != ESP_OK){
|
2016-08-17 11:08:22 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
// 2. Select image to boot
|
2017-05-18 01:31:19 -04:00
|
|
|
esp_image_metadata_t image_data;
|
2018-04-16 11:35:41 -04:00
|
|
|
if(select_image(&image_data) != ESP_OK){
|
2017-05-18 01:31:19 -04:00
|
|
|
return;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
// 3. Loading the selected image
|
|
|
|
bootloader_utility_load_image(&image_data);
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
// Selects image to boot
|
|
|
|
static esp_err_t select_image (esp_image_metadata_t *image_data)
|
2016-08-17 11:08:22 -04:00
|
|
|
{
|
2018-04-16 11:35:41 -04:00
|
|
|
// 1. Load partition table
|
|
|
|
bootloader_state_t bs = { 0 };
|
|
|
|
if (!bootloader_utility_load_partition_table(&bs)) {
|
|
|
|
ESP_LOGE(TAG, "load partition table error!");
|
|
|
|
return ESP_FAIL;
|
2016-08-17 11:08:22 -04:00
|
|
|
}
|
2017-04-24 06:36:47 -04:00
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
// 2. Select boot partition
|
|
|
|
int boot_index = selected_boot_partition(&bs);
|
|
|
|
if(boot_index == INVALID_INDEX) {
|
|
|
|
return ESP_FAIL; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
|
2017-11-03 03:09:19 -04:00
|
|
|
}
|
2016-10-27 04:17:28 -04:00
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
// 3. Load the app image for booting
|
|
|
|
if (!bootloader_utility_load_boot_image(&bs, boot_index, image_data)) {
|
|
|
|
return ESP_FAIL;
|
2016-10-27 04:17:28 -04:00
|
|
|
}
|
2018-04-16 11:35:41 -04:00
|
|
|
return ESP_OK;
|
2017-04-11 15:55:31 -04:00
|
|
|
}
|
|
|
|
|
2018-04-16 11:35:41 -04:00
|
|
|
/*
|
|
|
|
* Selects a boot partition.
|
|
|
|
* The conditions for switching to another firmware are checked.
|
|
|
|
*/
|
|
|
|
static int selected_boot_partition(const bootloader_state_t *bs)
|
2017-04-11 15:55:31 -04:00
|
|
|
{
|
2018-04-16 11:35:41 -04:00
|
|
|
int boot_index = bootloader_utility_get_selected_boot_partition(bs);
|
|
|
|
if (boot_index == INVALID_INDEX) {
|
|
|
|
return boot_index; // Unrecoverable failure (not due to corrupt ota data or bad partition contents)
|
2017-04-11 15:55:31 -04:00
|
|
|
} else {
|
2018-04-16 11:35:41 -04:00
|
|
|
// Check for reset to the factory firmware or for launch OTA[x] firmware.
|
|
|
|
// Customer implementation.
|
|
|
|
// if (gpio_pin_1 == true && ...){
|
|
|
|
// boot_index = required_boot_partition;
|
|
|
|
// } ...
|
2017-04-11 15:55:31 -04:00
|
|
|
}
|
2018-04-16 11:35:41 -04:00
|
|
|
return boot_index;
|
2017-06-16 02:30:21 -04:00
|
|
|
}
|