David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | |
| 20 | #include <zephyr.h> |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 21 | #include <flash.h> |
| 22 | |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 23 | #include MCUBOOT_TARGET_CONFIG |
| 24 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 25 | #include <flash_map/flash_map.h> |
| 26 | #include <hal/hal_flash.h> |
| 27 | #include <sysflash/sysflash.h> |
| 28 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 29 | #define SYS_LOG_DOMAIN "BOOTLOADER" |
| 30 | #define SYS_LOG_LEVEL SYS_LOG_LEVEL_INFO |
| 31 | #include <logging/sys_log.h> |
| 32 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 33 | extern struct device *boot_flash_device; |
| 34 | |
| 35 | /* |
| 36 | * The flash area describes essentially the partition table of the |
| 37 | * flash. In this case, it starts with FLASH_AREA_IMAGE_0. |
| 38 | */ |
| 39 | static const struct flash_area part_map[] = { |
| 40 | { |
| 41 | .fa_id = FLASH_AREA_IMAGE_0, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 42 | .fa_off = FLASH_AREA_IMAGE_0_OFFSET, |
| 43 | .fa_size = FLASH_AREA_IMAGE_0_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 44 | }, |
| 45 | { |
| 46 | .fa_id = FLASH_AREA_IMAGE_1, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 47 | .fa_off = FLASH_AREA_IMAGE_1_OFFSET, |
| 48 | .fa_size = FLASH_AREA_IMAGE_1_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 49 | }, |
| 50 | { |
| 51 | .fa_id = FLASH_AREA_IMAGE_SCRATCH, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 52 | .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET, |
| 53 | .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 54 | }, |
| 55 | }; |
| 56 | |
| 57 | /* |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 58 | * `open` a flash area. The `area` in this case is not the individual |
| 59 | * sectors, but describes the particular flash area in question. |
| 60 | */ |
| 61 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 62 | { |
| 63 | int i; |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 64 | |
| 65 | SYS_LOG_DBG("%s: area %d", __func__, id); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 66 | |
| 67 | for (i = 0; i < ARRAY_SIZE(part_map); i++) { |
| 68 | if (id == part_map[i].fa_id) |
| 69 | break; |
| 70 | } |
| 71 | if (i == ARRAY_SIZE(part_map)) |
| 72 | return -1; |
| 73 | |
| 74 | *area = &part_map[i]; |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Nothing to do on close. |
| 80 | */ |
| 81 | void flash_area_close(const struct flash_area *area) |
| 82 | { |
| 83 | } |
| 84 | |
| 85 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 86 | uint32_t len) |
| 87 | { |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 88 | SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 89 | area->fa_id, off, len); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 90 | return flash_read(boot_flash_device, area->fa_off + off, dst, len); |
| 91 | } |
| 92 | |
| 93 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
| 94 | uint32_t len) |
| 95 | { |
Michael Scott | e12746c | 2017-01-31 16:07:08 -0800 | [diff] [blame] | 96 | int rc = 0; |
| 97 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 98 | SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 99 | area->fa_id, off, len); |
Michael Scott | e12746c | 2017-01-31 16:07:08 -0800 | [diff] [blame] | 100 | flash_write_protection_set(boot_flash_device, false); |
| 101 | rc = flash_write(boot_flash_device, area->fa_off + off, src, len); |
| 102 | flash_write_protection_set(boot_flash_device, true); |
| 103 | return rc; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 107 | { |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 108 | SYS_LOG_DBG("%s: area=%d, off=%x, len=%x", __func__, |
| 109 | area->fa_id, off, len); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 110 | return flash_erase(boot_flash_device, area->fa_off + off, len); |
| 111 | } |
| 112 | |
| 113 | uint8_t flash_area_align(const struct flash_area *area) |
| 114 | { |
| 115 | return hal_flash_align(area->fa_id); |
| 116 | } |
| 117 | |
| 118 | /* |
| 119 | * This depends on the mappings defined in sysflash.h, and assumes |
| 120 | * that slot 0, slot 1, and the scratch area area contiguous. |
| 121 | */ |
| 122 | int flash_area_id_from_image_slot(int slot) |
| 123 | { |
| 124 | return slot + FLASH_AREA_IMAGE_0; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * Lookup the sector map for a given flash area. This should fill in |
| 129 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 130 | * the storage at `ret` and should be set to the final number of |
| 131 | * sectors in this area. |
| 132 | */ |
| 133 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 134 | { |
| 135 | uint32_t off; |
Michael Scott | de6ee59 | 2017-01-31 16:11:57 -0800 | [diff] [blame^] | 136 | uint32_t len; |
| 137 | uint32_t max_cnt = *cnt; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 138 | |
Ricardo Salveti | 7cf3d9e | 2017-01-18 16:38:22 -0200 | [diff] [blame] | 139 | SYS_LOG_DBG("%s: lookup area %d", __func__, idx); |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 140 | /* |
| 141 | * This simple layout has uniform slots, so just fill in the |
| 142 | * right one. |
| 143 | */ |
| 144 | if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) |
| 145 | return -1; |
| 146 | |
| 147 | if (*cnt < 1) |
| 148 | return -1; |
| 149 | |
Michael Scott | de6ee59 | 2017-01-31 16:11:57 -0800 | [diff] [blame^] | 150 | switch (idx) { |
| 151 | case FLASH_AREA_IMAGE_0: |
| 152 | off = FLASH_AREA_IMAGE_0_OFFSET; |
| 153 | len = FLASH_AREA_IMAGE_0_SIZE; |
| 154 | break; |
| 155 | case FLASH_AREA_IMAGE_1: |
| 156 | off = FLASH_AREA_IMAGE_1_OFFSET; |
| 157 | len = FLASH_AREA_IMAGE_1_SIZE; |
| 158 | break; |
| 159 | case FLASH_AREA_IMAGE_SCRATCH: |
| 160 | off = FLASH_AREA_IMAGE_SCRATCH_OFFSET; |
| 161 | len = FLASH_AREA_IMAGE_SCRATCH_SIZE; |
| 162 | break; |
| 163 | default: |
| 164 | SYS_LOG_ERR("%s: unknown flash area %d", __func__, idx); |
| 165 | return -1; |
| 166 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 167 | |
Michael Scott | de6ee59 | 2017-01-31 16:11:57 -0800 | [diff] [blame^] | 168 | *cnt = 0; |
| 169 | while (len > 0 && *cnt < max_cnt) { |
| 170 | ret[*cnt].fa_id = idx; |
| 171 | ret[*cnt].fa_device_id = 0; |
| 172 | ret[*cnt].pad16 = 0; |
| 173 | ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SCRATCH_SIZE * (*cnt)); |
| 174 | ret[*cnt].fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE; |
| 175 | *cnt = *cnt + 1; |
| 176 | len -= FLASH_AREA_IMAGE_SCRATCH_SIZE; |
| 177 | } |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 178 | |
| 179 | return 0; |
| 180 | } |