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