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 | |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 11 | #include "../../../boot/bootutil/src/bootutil_priv.h" |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 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 | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 16 | extern int sim_flash_erase(uint32_t offset, uint32_t size); |
| 17 | extern int sim_flash_read(uint32_t offset, uint8_t *dest, uint32_t size); |
| 18 | extern int sim_flash_write(uint32_t offset, const uint8_t *src, uint32_t size); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 19 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 20 | static jmp_buf boot_jmpbuf; |
| 21 | int flash_counter; |
| 22 | |
| 23 | int jumped = 0; |
| 24 | |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 25 | uint8_t sim_flash_align = 1; |
| 26 | uint8_t flash_area_align(const struct flash_area *area) |
| 27 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 28 | (void)area; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 29 | return sim_flash_align; |
David Brown | 5acda26 | 2017-01-23 15:42:19 -0700 | [diff] [blame] | 30 | } |
| 31 | |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 32 | struct area { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 33 | struct flash_area whole; |
| 34 | struct flash_area *areas; |
| 35 | uint32_t num_areas; |
| 36 | uint8_t id; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct area_desc { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 40 | struct area slots[16]; |
| 41 | uint32_t num_slots; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | static struct area_desc *flash_areas; |
| 45 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 46 | void *(*mbedtls_calloc)(size_t n, size_t size); |
| 47 | void (*mbedtls_free)(void *ptr); |
| 48 | |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 49 | int invoke_boot_go(struct area_desc *adesc) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 50 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 51 | int res; |
| 52 | struct boot_rsp rsp; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 53 | |
David Brown | 7e701d8 | 2017-07-11 13:24:25 -0600 | [diff] [blame] | 54 | mbedtls_calloc = calloc; |
| 55 | mbedtls_free = free; |
| 56 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 57 | flash_areas = adesc; |
| 58 | if (setjmp(boot_jmpbuf) == 0) { |
| 59 | res = boot_go(&rsp); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 60 | flash_areas = NULL; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 61 | /* printf("boot_go off: %d (0x%08x)\n", res, rsp.br_image_off); */ |
| 62 | return res; |
| 63 | } else { |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 64 | flash_areas = NULL; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 65 | return -0x13579; |
| 66 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | int hal_flash_read(uint8_t flash_id, uint32_t address, void *dst, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 70 | uint32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 71 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 72 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 73 | // printf("hal_flash_read: %d, 0x%08x (0x%x)\n", |
| 74 | // flash_id, address, num_bytes); |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 75 | return sim_flash_read(address, dst, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | int hal_flash_write(uint8_t flash_id, uint32_t address, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 79 | const void *src, int32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 80 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 81 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 82 | // printf("hal_flash_write: 0x%08x (0x%x)\n", address, num_bytes); |
| 83 | // fflush(stdout); |
| 84 | if (--flash_counter == 0) { |
| 85 | jumped++; |
| 86 | longjmp(boot_jmpbuf, 1); |
| 87 | } |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 88 | return sim_flash_write(address, src, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | int hal_flash_erase(uint8_t flash_id, uint32_t address, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 92 | uint32_t num_bytes) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 93 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 94 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 95 | // printf("hal_flash_erase: 0x%08x, (0x%x)\n", address, num_bytes); |
| 96 | // fflush(stdout); |
| 97 | if (--flash_counter == 0) { |
| 98 | jumped++; |
| 99 | longjmp(boot_jmpbuf, 1); |
| 100 | } |
David Brown | bdb6db7 | 2017-07-06 10:14:37 -0600 | [diff] [blame] | 101 | return sim_flash_erase(address, num_bytes); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | uint8_t hal_flash_align(uint8_t flash_id) |
| 105 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 106 | (void)flash_id; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 107 | return sim_flash_align; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | void *os_malloc(size_t size) |
| 111 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 112 | // printf("os_malloc 0x%x bytes\n", size); |
| 113 | return malloc(size); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | int flash_area_id_from_image_slot(int slot) |
| 117 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 118 | return slot + 1; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 122 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 123 | uint32_t i; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 124 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 125 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 126 | if (flash_areas->slots[i].id == id) |
| 127 | break; |
| 128 | } |
| 129 | if (i == flash_areas->num_slots) { |
| 130 | printf("Unsupported area\n"); |
| 131 | abort(); |
| 132 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 133 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 134 | /* Unsure if this is right, just returning the first area. */ |
| 135 | *area = &flash_areas->slots[i].whole; |
| 136 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | void flash_area_close(const struct flash_area *area) |
| 140 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 141 | (void)area; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Read/write/erase. Offset is relative from beginning of flash area. |
| 146 | */ |
| 147 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 148 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 149 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 150 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", |
| 151 | __func__, area->fa_id, off, len); |
| 152 | return hal_flash_read(area->fa_id, |
| 153 | area->fa_off + off, |
| 154 | dst, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 158 | uint32_t len) |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 159 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 160 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 161 | area->fa_id, off, len); |
| 162 | return hal_flash_write(area->fa_id, |
| 163 | area->fa_off + off, |
| 164 | src, len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 168 | { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 169 | BOOT_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 170 | area->fa_id, off, len); |
| 171 | return hal_flash_erase(area->fa_id, |
| 172 | area->fa_off + off, |
| 173 | len); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 177 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 178 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 179 | struct area *slot; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 180 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 181 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 182 | if (flash_areas->slots[i].id == idx) |
| 183 | break; |
| 184 | } |
| 185 | if (i == flash_areas->num_slots) { |
| 186 | printf("Unsupported area\n"); |
| 187 | abort(); |
| 188 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 189 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 190 | slot = &flash_areas->slots[i]; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 191 | |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 192 | if (slot->num_areas > (uint32_t)*cnt) { |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 193 | printf("Too many areas in slot\n"); |
| 194 | abort(); |
| 195 | } |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 196 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 197 | *cnt = slot->num_areas; |
| 198 | memcpy(ret, slot->areas, slot->num_areas * sizeof(struct flash_area)); |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 199 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 200 | return 0; |
David Brown | de7729e | 2017-01-09 10:41:35 -0700 | [diff] [blame] | 201 | } |
| 202 | |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 203 | int flash_area_get_sectors(int fa_id, uint32_t *count, |
| 204 | struct flash_sector *sectors) |
| 205 | { |
Fabio Utzig | cd5774b | 2017-11-29 10:18:26 -0200 | [diff] [blame] | 206 | uint32_t i; |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 207 | struct area *slot; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 208 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 209 | for (i = 0; i < flash_areas->num_slots; i++) { |
| 210 | if (flash_areas->slots[i].id == fa_id) |
| 211 | break; |
| 212 | } |
| 213 | if (i == flash_areas->num_slots) { |
| 214 | printf("Unsupported area\n"); |
| 215 | abort(); |
| 216 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 217 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 218 | slot = &flash_areas->slots[i]; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 219 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 220 | if (slot->num_areas > *count) { |
| 221 | printf("Too many areas in slot\n"); |
| 222 | abort(); |
| 223 | } |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 224 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 225 | for (i = 0; i < slot->num_areas; i++) { |
| 226 | sectors[i].fs_off = slot->areas[i].fa_off - |
| 227 | slot->whole.fa_off; |
| 228 | sectors[i].fs_size = slot->areas[i].fa_size; |
| 229 | } |
| 230 | *count = slot->num_areas; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 231 | |
David Brown | 7ad8088 | 2017-06-20 15:30:36 -0600 | [diff] [blame] | 232 | return 0; |
David Brown | 60399f6 | 2017-05-11 10:20:34 -0600 | [diff] [blame] | 233 | } |