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> |
| 21 | #include <misc/printk.h> |
| 22 | #include <flash.h> |
| 23 | |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 24 | #include MCUBOOT_TARGET_CONFIG |
| 25 | |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 26 | #include <flash_map/flash_map.h> |
| 27 | #include <hal/hal_flash.h> |
| 28 | #include <sysflash/sysflash.h> |
| 29 | |
| 30 | extern struct device *boot_flash_device; |
| 31 | |
| 32 | /* |
| 33 | * The flash area describes essentially the partition table of the |
| 34 | * flash. In this case, it starts with FLASH_AREA_IMAGE_0. |
| 35 | */ |
| 36 | static const struct flash_area part_map[] = { |
| 37 | { |
| 38 | .fa_id = FLASH_AREA_IMAGE_0, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 39 | .fa_off = FLASH_AREA_IMAGE_0_OFFSET, |
| 40 | .fa_size = FLASH_AREA_IMAGE_0_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 41 | }, |
| 42 | { |
| 43 | .fa_id = FLASH_AREA_IMAGE_1, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 44 | .fa_off = FLASH_AREA_IMAGE_1_OFFSET, |
| 45 | .fa_size = FLASH_AREA_IMAGE_1_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 46 | }, |
| 47 | { |
| 48 | .fa_id = FLASH_AREA_IMAGE_SCRATCH, |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 49 | .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET, |
| 50 | .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE, |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 51 | }, |
| 52 | }; |
| 53 | |
| 54 | /* |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 55 | * `open` a flash area. The `area` in this case is not the individual |
| 56 | * sectors, but describes the particular flash area in question. |
| 57 | */ |
| 58 | int flash_area_open(uint8_t id, const struct flash_area **area) |
| 59 | { |
| 60 | int i; |
| 61 | printk("%s: area %d\n", __func__, id); |
| 62 | |
| 63 | for (i = 0; i < ARRAY_SIZE(part_map); i++) { |
| 64 | if (id == part_map[i].fa_id) |
| 65 | break; |
| 66 | } |
| 67 | if (i == ARRAY_SIZE(part_map)) |
| 68 | return -1; |
| 69 | |
| 70 | *area = &part_map[i]; |
| 71 | return 0; |
| 72 | } |
| 73 | |
| 74 | /* |
| 75 | * Nothing to do on close. |
| 76 | */ |
| 77 | void flash_area_close(const struct flash_area *area) |
| 78 | { |
| 79 | } |
| 80 | |
| 81 | int flash_area_read(const struct flash_area *area, uint32_t off, void *dst, |
| 82 | uint32_t len) |
| 83 | { |
| 84 | // printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 85 | return flash_read(boot_flash_device, area->fa_off + off, dst, len); |
| 86 | } |
| 87 | |
| 88 | int flash_area_write(const struct flash_area *area, uint32_t off, const void *src, |
| 89 | uint32_t len) |
| 90 | { |
| 91 | printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 92 | return flash_write(boot_flash_device, area->fa_off + off, src, len); |
| 93 | } |
| 94 | |
| 95 | int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len) |
| 96 | { |
| 97 | printk("%s: area=%d, off=%x, len=%x\n", __func__, area->fa_id, off, len); |
| 98 | return flash_erase(boot_flash_device, area->fa_off + off, len); |
| 99 | } |
| 100 | |
| 101 | uint8_t flash_area_align(const struct flash_area *area) |
| 102 | { |
| 103 | return hal_flash_align(area->fa_id); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * This depends on the mappings defined in sysflash.h, and assumes |
| 108 | * that slot 0, slot 1, and the scratch area area contiguous. |
| 109 | */ |
| 110 | int flash_area_id_from_image_slot(int slot) |
| 111 | { |
| 112 | return slot + FLASH_AREA_IMAGE_0; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Lookup the sector map for a given flash area. This should fill in |
| 117 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 118 | * the storage at `ret` and should be set to the final number of |
| 119 | * sectors in this area. |
| 120 | */ |
| 121 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 122 | { |
| 123 | uint32_t off; |
| 124 | |
| 125 | printk("%s: lookup area %d\n", __func__, idx); |
| 126 | /* |
| 127 | * This simple layout has uniform slots, so just fill in the |
| 128 | * right one. |
| 129 | */ |
| 130 | if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH) |
| 131 | return -1; |
| 132 | |
| 133 | if (*cnt < 1) |
| 134 | return -1; |
| 135 | |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 136 | off = (idx - FLASH_AREA_IMAGE_0 + 1) * FLASH_AREA_IMAGE_0_OFFSET; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 137 | |
| 138 | ret->fa_id = idx; |
| 139 | ret->fa_device_id = 0; |
| 140 | ret->pad16 = 0; |
| 141 | ret->fa_off = off; |
Ricardo Salveti | 3a2c124 | 2017-01-19 10:22:35 -0200 | [diff] [blame] | 142 | ret->fa_size = FLASH_AREA_IMAGE_0_SIZE; |
David Brown | 5153bd6 | 2017-01-06 11:16:53 -0700 | [diff] [blame] | 143 | |
| 144 | return 0; |
| 145 | } |