2017-01-10 01:41:35 +08:00
|
|
|
/* Run the boot image. */
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <bootutil/bootutil.h>
|
|
|
|
#include <bootutil/image.h>
|
|
|
|
#include "flash_map/flash_map.h"
|
|
|
|
|
|
|
|
#include "../../boot/bootutil/src/bootutil_priv.h"
|
|
|
|
|
2017-05-05 23:40:01 +08:00
|
|
|
#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR
|
2017-05-04 23:04:47 +08:00
|
|
|
#include <bootutil/bootutil_log.h>
|
|
|
|
|
2017-01-10 01:41:35 +08:00
|
|
|
extern int sim_flash_erase(void *flash, uint32_t offset, uint32_t size);
|
|
|
|
extern int sim_flash_read(void *flash, uint32_t offset, uint8_t *dest, uint32_t size);
|
|
|
|
extern int sim_flash_write(void *flash, uint32_t offset, const uint8_t *src, uint32_t size);
|
|
|
|
|
|
|
|
static void *flash_device;
|
|
|
|
static jmp_buf boot_jmpbuf;
|
|
|
|
int flash_counter;
|
|
|
|
|
|
|
|
int jumped = 0;
|
|
|
|
|
2017-01-24 06:42:19 +08:00
|
|
|
uint8_t sim_flash_align = 1;
|
|
|
|
uint8_t flash_area_align(const struct flash_area *area)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
return sim_flash_align;
|
2017-01-24 06:42:19 +08:00
|
|
|
}
|
|
|
|
|
2017-01-10 01:41:35 +08:00
|
|
|
struct area {
|
2017-06-21 05:30:36 +08:00
|
|
|
struct flash_area whole;
|
|
|
|
struct flash_area *areas;
|
|
|
|
uint32_t num_areas;
|
|
|
|
uint8_t id;
|
2017-01-10 01:41:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct area_desc {
|
2017-06-21 05:30:36 +08:00
|
|
|
struct area slots[16];
|
|
|
|
uint32_t num_slots;
|
2017-01-10 01:41:35 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct area_desc *flash_areas;
|
|
|
|
|
|
|
|
int invoke_boot_go(void *flash, struct area_desc *adesc)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
int res;
|
|
|
|
struct boot_rsp rsp;
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
flash_device = flash;
|
|
|
|
flash_areas = adesc;
|
|
|
|
if (setjmp(boot_jmpbuf) == 0) {
|
|
|
|
res = boot_go(&rsp);
|
|
|
|
/* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */
|
|
|
|
return res;
|
|
|
|
} else {
|
|
|
|
return -0x13579;
|
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst,
|
2017-06-21 05:30:36 +08:00
|
|
|
uint32_t num_bytes)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
// printf("hal_flash_read: %d, 0x%08x (0x%x)\n",
|
|
|
|
// flash_id, address, num_bytes);
|
|
|
|
return sim_flash_read(flash_device, address, dst, num_bytes);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int hal_flash_write(uint8_t flash_id, uint32_t address,
|
2017-06-21 05:30:36 +08:00
|
|
|
const void *src, int32_t num_bytes)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
// printf("hal_flash_write: 0x%08x (0x%x)\n", address, num_bytes);
|
|
|
|
// fflush(stdout);
|
|
|
|
if (--flash_counter == 0) {
|
|
|
|
jumped++;
|
|
|
|
longjmp(boot_jmpbuf, 1);
|
|
|
|
}
|
|
|
|
return sim_flash_write(flash_device, address, src, num_bytes);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int hal_flash_erase(uint8_t flash_id, uint32_t address,
|
2017-06-21 05:30:36 +08:00
|
|
|
uint32_t num_bytes)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
// printf("hal_flash_erase: 0x%08x, (0x%x)\n", address, num_bytes);
|
|
|
|
// fflush(stdout);
|
|
|
|
if (--flash_counter == 0) {
|
|
|
|
jumped++;
|
|
|
|
longjmp(boot_jmpbuf, 1);
|
|
|
|
}
|
|
|
|
return sim_flash_erase(flash_device, address, num_bytes);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t hal_flash_align(uint8_t flash_id)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
return sim_flash_align;
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void *os_malloc(size_t size)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
// printf("os_malloc 0x%x bytes\n", size);
|
|
|
|
return malloc(size);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int flash_area_id_from_image_slot(int slot)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
return slot + 1;
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int flash_area_open(uint8_t id, const struct flash_area **area)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
int i;
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
for (i = 0; i < flash_areas->num_slots; i++) {
|
|
|
|
if (flash_areas->slots[i].id == id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == flash_areas->num_slots) {
|
|
|
|
printf("Unsupported area\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
/* Unsure if this is right, just returning the first area. */
|
|
|
|
*area = &flash_areas->slots[i].whole;
|
|
|
|
return 0;
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void flash_area_close(const struct flash_area *area)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read/write/erase. Offset is relative from beginning of flash area.
|
|
|
|
*/
|
|
|
|
int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
|
2017-06-21 05:30:36 +08:00
|
|
|
uint32_t len)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x",
|
|
|
|
__func__, area->fa_id, off, len);
|
|
|
|
return hal_flash_read(area->fa_id,
|
|
|
|
area->fa_off + off,
|
|
|
|
dst, len);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
|
2017-06-21 05:30:36 +08:00
|
|
|
uint32_t len)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
|
|
|
|
area->fa_id, off, len);
|
|
|
|
return hal_flash_write(area->fa_id,
|
|
|
|
area->fa_off + off,
|
|
|
|
src, len);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__,
|
|
|
|
area->fa_id, off, len);
|
|
|
|
return hal_flash_erase(area->fa_id,
|
|
|
|
area->fa_off + off,
|
|
|
|
len);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
int i;
|
|
|
|
struct area *slot;
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
for (i = 0; i < flash_areas->num_slots; i++) {
|
|
|
|
if (flash_areas->slots[i].id == idx)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == flash_areas->num_slots) {
|
|
|
|
printf("Unsupported area\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
slot = &flash_areas->slots[i];
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
if (slot->num_areas > *cnt) {
|
|
|
|
printf("Too many areas in slot\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
*cnt = slot->num_areas;
|
|
|
|
memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area));
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
return 0;
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
2017-05-12 00:20:34 +08:00
|
|
|
int flash_area_get_sectors(int fa_id, uint32_t *count,
|
|
|
|
struct flash_sector *sectors)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
int i;
|
|
|
|
struct area *slot;
|
2017-05-12 00:20:34 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
for (i = 0; i < flash_areas->num_slots; i++) {
|
|
|
|
if (flash_areas->slots[i].id == fa_id)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (i == flash_areas->num_slots) {
|
|
|
|
printf("Unsupported area\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-05-12 00:20:34 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
slot = &flash_areas->slots[i];
|
2017-05-12 00:20:34 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
if (slot->num_areas > *count) {
|
|
|
|
printf("Too many areas in slot\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-05-12 00:20:34 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
for (i = 0; i < slot->num_areas; i++) {
|
|
|
|
sectors[i].fs_off = slot->areas[i].fa_off -
|
|
|
|
slot->whole.fa_off;
|
|
|
|
sectors[i].fs_size = slot->areas[i].fa_size;
|
|
|
|
}
|
|
|
|
*count = slot->num_areas;
|
2017-05-12 00:20:34 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
return 0;
|
2017-05-12 00:20:34 +08:00
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
|
|
|
|
int bootutil_img_validate(struct image_header *hdr,
|
|
|
|
const struct flash_area *fap,
|
|
|
|
uint8_t *tmp_buf, uint32_t tmp_buf_sz,
|
|
|
|
uint8_t *seed, int seed_len, uint8_t *out_hash)
|
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
if (hal_flash_read(fap->fa_id, fap->fa_off, tmp_buf, 4)) {
|
|
|
|
printf("Flash read error\n");
|
|
|
|
abort();
|
|
|
|
}
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
return (*((uint32_t *) tmp_buf) != 0x96f3b83c);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|