2017-01-10 01:41:35 +08:00
|
|
|
/* Run the boot image. */
|
|
|
|
|
2017-11-24 05:49:00 +08:00
|
|
|
#include <assert.h>
|
2017-01-10 01:41:35 +08:00
|
|
|
#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"
|
|
|
|
|
2017-07-12 23:51:31 +08:00
|
|
|
#include "../../../boot/bootutil/src/bootutil_priv.h"
|
2017-12-13 07:48:30 +08:00
|
|
|
#include "bootsim.h"
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-12-05 18:52:53 +08:00
|
|
|
#ifdef MCUBOOT_SIGN_EC256
|
|
|
|
#include "../../../ext/tinycrypt/lib/include/tinycrypt/ecc_dsa.h"
|
|
|
|
#endif
|
|
|
|
|
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-07-07 00:14:37 +08:00
|
|
|
extern int sim_flash_erase(uint32_t offset, uint32_t size);
|
|
|
|
extern int sim_flash_read(uint32_t offset, uint8_t *dest, uint32_t size);
|
|
|
|
extern int sim_flash_write(uint32_t offset, const uint8_t *src, uint32_t size);
|
2017-01-10 01:41:35 +08:00
|
|
|
|
|
|
|
static jmp_buf boot_jmpbuf;
|
|
|
|
int flash_counter;
|
|
|
|
|
|
|
|
int jumped = 0;
|
2017-11-24 05:49:00 +08:00
|
|
|
uint8_t c_asserts = 0;
|
|
|
|
uint8_t c_catch_asserts = 0;
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-12-05 18:52:53 +08:00
|
|
|
int ecdsa256_sign_(const uint8_t *privkey, const uint8_t *hash,
|
|
|
|
unsigned hash_len, uint8_t *signature)
|
|
|
|
{
|
|
|
|
#ifdef MCUBOOT_SIGN_EC256
|
|
|
|
return uECC_sign(privkey, hash, hash_len, signature, uECC_secp256r1());
|
|
|
|
#else
|
|
|
|
(void)privkey;
|
|
|
|
(void)hash;
|
|
|
|
(void)hash_len;
|
|
|
|
(void)signature;
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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-11-29 20:18:26 +08:00
|
|
|
(void)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;
|
|
|
|
|
2017-07-12 03:24:25 +08:00
|
|
|
void *(*mbedtls_calloc)(size_t n, size_t size);
|
|
|
|
void (*mbedtls_free)(void *ptr);
|
|
|
|
|
2017-07-07 00:14:37 +08:00
|
|
|
int invoke_boot_go(struct area_desc *adesc)
|
2017-01-10 01:41:35 +08:00
|
|
|
{
|
2017-06-21 05:30:36 +08:00
|
|
|
int res;
|
|
|
|
struct boot_rsp rsp;
|
2017-01-10 01:41:35 +08:00
|
|
|
|
2017-07-12 03:24:25 +08:00
|
|
|
mbedtls_calloc = calloc;
|
|
|
|
mbedtls_free = free;
|
|
|
|
|
2017-06-21 05:30:36 +08:00
|
|
|
flash_areas = adesc;
|
|
|
|
if (setjmp(boot_jmpbuf) == 0) {
|
|
|
|
res = boot_go(&rsp);
|
2017-07-07 00:14:37 +08:00
|
|
|
flash_areas = NULL;
|
2017-06-21 05:30:36 +08:00
|
|
|
/* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */
|
|
|
|
return res;
|
|
|
|
} else {
|
2017-07-07 00:14:37 +08:00
|
|
|
flash_areas = NULL;
|
2017-06-21 05:30:36 +08:00
|
|
|
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-11-29 20:18:26 +08:00
|
|
|
(void)flash_id;
|
2017-06-21 05:30:36 +08:00
|
|
|
// printf("hal_flash_read: %d, 0x%08x (0x%x)\n",
|
|
|
|
// flash_id, address, num_bytes);
|
2017-07-07 00:14:37 +08:00
|
|
|
return sim_flash_read(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-11-29 20:18:26 +08:00
|
|
|
(void)flash_id;
|
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);
|
|
|
|
}
|
2017-07-07 00:14:37 +08:00
|
|
|
return sim_flash_write(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-11-29 20:18:26 +08:00
|
|
|
(void)flash_id;
|
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);
|
|
|
|
}
|
2017-07-07 00:14:37 +08:00
|
|
|
return sim_flash_erase(address, num_bytes);
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t hal_flash_align(uint8_t flash_id)
|
|
|
|
{
|
2017-11-29 20:18:26 +08:00
|
|
|
(void)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-11-29 20:18:26 +08:00
|
|
|
uint32_t 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)
|
|
|
|
{
|
2017-11-29 20:18:26 +08:00
|
|
|
(void)area;
|
2017-01-10 01:41:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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-11-29 20:18:26 +08:00
|
|
|
uint32_t i;
|
2017-06-21 05:30:36 +08:00
|
|
|
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-11-29 20:18:26 +08:00
|
|
|
if (slot->num_areas > (uint32_t)*cnt) {
|
2017-06-21 05:30:36 +08:00
|
|
|
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-11-29 20:18:26 +08:00
|
|
|
uint32_t i;
|
2017-06-21 05:30:36 +08:00
|
|
|
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-11-24 05:49:00 +08:00
|
|
|
|
|
|
|
void sim_assert(int x, const char *assertion, const char *file, unsigned int line, const char *function)
|
|
|
|
{
|
|
|
|
if (!(x)) {
|
|
|
|
if (c_catch_asserts) {
|
|
|
|
c_asserts++;
|
|
|
|
} else {
|
|
|
|
BOOT_LOG_ERR("%s:%d: %s: Assertion `%s' failed.", file, line, function, assertion);
|
|
|
|
|
|
|
|
/* NOTE: if the assert below is triggered, the place where it was originally
|
|
|
|
* asserted is printed by the message above...
|
|
|
|
*/
|
|
|
|
assert(x);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|