2017-01-07 02:16:53 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2012-2014 Wind River Systems, Inc.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
* 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 <zephyr.h>
|
|
|
|
#include <flash.h>
|
|
|
|
#include <asm_inline.h>
|
2017-02-28 10:00:31 +08:00
|
|
|
#include <drivers/system_timer.h>
|
2017-01-07 02:16:53 +08:00
|
|
|
|
2017-03-20 23:03:41 +08:00
|
|
|
#include "target.h"
|
|
|
|
|
2017-02-01 07:20:02 +08:00
|
|
|
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
|
|
|
|
#include "bootutil/bootutil_log.h"
|
2017-01-19 20:22:35 +08:00
|
|
|
#include "bootutil/image.h"
|
|
|
|
#include "bootutil/bootutil.h"
|
|
|
|
|
2017-01-07 02:16:53 +08:00
|
|
|
struct device *boot_flash_device;
|
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
void os_heap_init(void);
|
|
|
|
|
|
|
|
#if defined(CONFIG_ARM)
|
|
|
|
struct arm_vector_table {
|
2017-04-12 07:33:30 +08:00
|
|
|
uint32_t msp;
|
|
|
|
uint32_t reset;
|
2017-01-07 02:16:53 +08:00
|
|
|
};
|
|
|
|
|
2017-03-03 05:39:06 +08:00
|
|
|
static void do_boot(struct boot_rsp *rsp)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
struct arm_vector_table *vt;
|
|
|
|
|
|
|
|
/* The beginning of the image is the ARM vector table, containing
|
|
|
|
* the initial stack pointer address and the reset vector
|
|
|
|
* consecutively. Manually set the stack pointer and jump into the
|
|
|
|
* reset vector
|
|
|
|
*/
|
2017-05-02 10:30:02 +08:00
|
|
|
vt = (struct arm_vector_table *)(rsp->br_image_off +
|
2017-04-12 07:33:30 +08:00
|
|
|
rsp->br_hdr->ih_hdr_size);
|
|
|
|
irq_lock();
|
|
|
|
sys_clock_disable();
|
|
|
|
_MspSet(vt->msp);
|
|
|
|
((void (*)(void))vt->reset)();
|
2017-03-03 05:39:06 +08:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Default: Assume entry point is at the very beginning of the image. Simply
|
|
|
|
* lock interrupts and jump there. This is the right thing to do for X86 and
|
|
|
|
* possibly other platforms.
|
|
|
|
*/
|
|
|
|
static void do_boot(struct boot_rsp *rsp)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
void *start;
|
2017-03-03 05:39:06 +08:00
|
|
|
|
2017-05-02 10:30:02 +08:00
|
|
|
start = (void *)(rsp->br_image_off + rsp->br_hdr->ih_hdr_size);
|
2017-03-03 05:39:06 +08:00
|
|
|
|
2017-04-12 07:33:30 +08:00
|
|
|
/* Lock interrupts and dive into the entry point */
|
|
|
|
irq_lock();
|
|
|
|
((void (*)(void))start)();
|
2017-03-03 05:39:06 +08:00
|
|
|
}
|
|
|
|
#endif
|
2017-01-07 02:16:53 +08:00
|
|
|
|
|
|
|
void main(void)
|
|
|
|
{
|
2017-04-12 07:33:30 +08:00
|
|
|
struct boot_rsp rsp;
|
|
|
|
int rc;
|
|
|
|
|
|
|
|
BOOT_LOG_INF("Starting bootloader");
|
|
|
|
|
|
|
|
os_heap_init();
|
|
|
|
|
|
|
|
boot_flash_device = device_get_binding(FLASH_DRIVER_NAME);
|
|
|
|
if (!boot_flash_device) {
|
|
|
|
BOOT_LOG_ERR("Flash device not found");
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = boot_go(&rsp);
|
|
|
|
if (rc != 0) {
|
|
|
|
BOOT_LOG_ERR("Unable to find bootable image");
|
|
|
|
while (1)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2017-05-02 10:30:02 +08:00
|
|
|
BOOT_LOG_INF("Bootloader chainload address offset: 0x%x",
|
|
|
|
rsp.br_image_off);
|
2017-04-12 07:33:30 +08:00
|
|
|
BOOT_LOG_INF("Jumping to the first image slot");
|
|
|
|
do_boot(&rsp);
|
|
|
|
|
|
|
|
BOOT_LOG_ERR("Never should get here");
|
|
|
|
while (1)
|
|
|
|
;
|
2017-01-07 02:16:53 +08:00
|
|
|
}
|