David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 1 | /* Run the boot image. */ |
| 2 | |
| 3 | #include <setjmp.h> |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <string.h> |
| 7 | #include <bootutil/bootutil.h> |
| 8 | #include <bootutil/image.h> |
| 9 | #include "flash_map/flash_map.h" |
| 10 | |
| 11 | #include "../../boot/bootutil/src/bootutil_priv.h" |
| 12 | |
David Brown | 54b7779 | 2017-05-05 09:40:01 -0600 | [diff] [blame] | 13 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_ERROR |
David Brown | 75fd5dc | 2017-05-04 09:04:47 -0600 | [diff] [blame] | 14 | #include <bootutil/bootutil_log.h> |
| 15 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 16 | extern int sim_flash_erase(void *flash, uint32_t offset, uint32_t size); |
| 17 | extern int sim_flash_read(void *flash, uint32_t offset, uint8_t *dest, uint32_t size); |
| 18 | extern int sim_flash_write(void *flash, uint32_t offset, const uint8_t *src, uint32_t size); |
| 19 | |
| 20 | static void *flash_device; |
| 21 | static jmp_buf boot_jmpbuf; |
| 22 | int flash_counter; |
| 23 | |
| 24 | int jumped = 0; |
| 25 | |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 26 | uint8_t sim_flash_align = 1; |
| 27 | uint8_t flash_area_align(const struct flash_area *area) |
| 28 | { |
| 29 | return sim_flash_align; |
| 30 | } |
| 31 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 32 | struct area { |
| 33 | struct flash_area whole; |
| 34 | struct flash_area *areas; |
| 35 | uint32_t num_areas; |
| 36 | uint8_t id; |
| 37 | }; |
| 38 | |
| 39 | struct area_desc { |
| 40 | struct area slots[16]; |
| 41 | uint32_t num_slots; |
| 42 | }; |
| 43 | |
| 44 | static struct area_desc *flash_areas; |
| 45 | |
| 46 | int invoke_boot_go(void *flash, struct area_desc *adesc) |
| 47 | { |
| 48 | int res; |
| 49 | struct boot_rsp rsp; |
| 50 | |
| 51 | flash_device = flash; |
| 52 | flash_areas = adesc; |
| 53 | if (setjmp(boot_jmpbuf) == 0) { |
| 54 | res = boot_go(&rsp); |
Marti Bolivar | 88f48d9 | 2017-05-01 22:30:02 -0400 | [diff] [blame] | 55 | /* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */ |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 56 | return res; |
| 57 | } else { |
| 58 | return -0x13579; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst, |
| 63 | uint32_t num_bytes) |
| 64 | { |
| 65 | // printf("hal_flash_read: %d, 0x%08x (0x%x)\n", |
| 66 | // flash_id, address, num_bytes); |
| 67 | return sim_flash_read(flash_device, address, dst, num_bytes); |
| 68 | } |
| 69 | |
| 70 | int hal_flash_write(uint8_t flash_id, uint32_t address, |
| 71 | const void *src, int32_t num_bytes) |
| 72 | { |
| 73 | // printf("hal_flash_write: 0x%08x (0x%x)\n", address, num_bytes); |
| 74 | // fflush(stdout); |
| 75 | if (--flash_counter == 0) { |
| 76 | jumped++; |
| 77 | longjmp(boot_jmpbuf, 1); |
| 78 | } |
| 79 | return sim_flash_write(flash_device, address, src, num_bytes); |
| 80 | } |
| 81 | |
| 82 | int hal_flash_erase(uint8_t flash_id, uint32_t address, |
| 83 | uint32_t num_bytes) |
| 84 | { |
| 85 | // printf("hal_flash_erase: 0x%08x, (0x%x)\n", address, num_bytes); |
| 86 | // fflush(stdout); |
| 87 | if (--flash_counter == 0) { |
| 88 | jumped++; |
| 89 | longjmp(boot_jmpbuf, 1); |
| 90 | } |
| 91 | return sim_flash_erase(flash_device, address, num_bytes); |
| 92 | } |
| 93 | |
| 94 | uint8_t hal_flash_align(uint8_t flash_id) |
| 95 | { |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 96 | return sim_flash_align; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void *os_malloc(size_t size) |
| 100 | { |
| 101 | // printf("os_malloc 0x%x bytes\n", size); |
| 102 | return malloc(size); |
| 103 | } |
| 104 | |
| 105 | int flash_area_id_from_image_slot(int slot) |
| 106 | { |
| 107 | return slot + 1; |
| 108 | } |
| 109 | |
| 110 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 111 | { |
| 112 | int i; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 113 | |
| 114 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 115 | if (flash_areas->slots[i].id == id) |
| 116 | break; |
| 117 | } |
| 118 | if (i == flash_areas->num_slots) { |
| 119 | printf("Unsupported area\n"); |
| 120 | abort(); |
| 121 | } |
| 122 | |
| 123 | /* Unsure if this is right, just returning the first area. */ |
| 124 | *area = &flash_areas->slots[i].whole; |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | void flash_area_close(const struct flash_area *area) |
| 129 | { |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 134 | */ |
| 135 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 136 | uint32_t len) |
| 137 | { |
David Brown | 75fd5dc | 2017-05-04 09:04:47 -0600 | [diff] [blame] | 138 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", |
| 139 | __func__, area->fa_id, off, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 140 | return hal_flash_read(area->fa_id, |
| 141 | area->fa_off + off, |
| 142 | dst, len); |
| 143 | } |
| 144 | |
| 145 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
| 146 | uint32_t len) |
| 147 | { |
David Brown | 54b7779 | 2017-05-05 09:40:01 -0600 | [diff] [blame] | 148 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 149 | area->fa_id, off, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 150 | return hal_flash_write(area->fa_id, |
| 151 | area->fa_off + off, |
| 152 | src, len); |
| 153 | } |
| 154 | |
| 155 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 156 | { |
David Brown | 54b7779 | 2017-05-05 09:40:01 -0600 | [diff] [blame] | 157 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 158 | area->fa_id, off, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 159 | return hal_flash_erase(area->fa_id, |
| 160 | area->fa_off + off, |
| 161 | len); |
| 162 | } |
| 163 | |
| 164 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 165 | { |
| 166 | int i; |
| 167 | struct area *slot; |
| 168 | |
| 169 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 170 | if (flash_areas->slots[i].id == idx) |
| 171 | break; |
| 172 | } |
| 173 | if (i == flash_areas->num_slots) { |
| 174 | printf("Unsupported area\n"); |
| 175 | abort(); |
| 176 | } |
| 177 | |
| 178 | slot = &flash_areas->slots[i]; |
| 179 | |
| 180 | if (slot->num_areas > *cnt) { |
| 181 | printf("Too many areas in slot\n"); |
| 182 | abort(); |
| 183 | } |
| 184 | |
| 185 | *cnt = slot->num_areas; |
| 186 | memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 191 | |
| 192 | int bootutil_img_validate(struct image_header *hdr, |
| 193 | const struct flash_area *fap, |
| 194 | uint8_t *tmp_buf, uint32_t tmp_buf_sz, |
| 195 | uint8_t *seed, int seed_len, uint8_t *out_hash) |
| 196 | { |
| 197 | if (hal_flash_read(fap->fa_id, fap->fa_off, tmp_buf, 4)) { |
| 198 | printf("Flash read error\n"); |
| 199 | abort(); |
| 200 | } |
| 201 | |
| 202 | return (*((uint32_t *) tmp_buf) != 0x96f3b83c); |
| 203 | } |