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