Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [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 | /** |
| 21 | * @file |
| 22 | * @brief Legacy flash fallbacks |
| 23 | * |
| 24 | * This file contains hacks for flash drivers without page layout |
| 25 | * support. They're hacks because they guess a page layout that may be |
| 26 | * incorrect, but is likely to "work". Needless to say, such guesswork |
| 27 | * is undesirable in trusted bootloader code. |
| 28 | * |
| 29 | * The behavior is: |
| 30 | * |
| 31 | * - If FLASH_AREA_IMAGE_SECTOR_SIZE is defined (this was used by |
| 32 | * older Zephyr ports), the image sectors have uniform sector sizes. |
| 33 | * We also assume that's the size of the scratch sectors. |
| 34 | * |
| 35 | * - Otherwise, we assume that the size of the entire scratch area is |
| 36 | * a least common multiple of all sector sizes, and use that as |
| 37 | * FLASH_AREA_IMAGE_SECTOR_SIZE. |
| 38 | */ |
| 39 | |
| 40 | #define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO |
| 41 | #include "bootutil/bootutil_log.h" |
| 42 | |
| 43 | #include <flash_map/flash_map.h> |
| 44 | #include <inttypes.h> |
| 45 | #include <target.h> |
| 46 | |
| 47 | #warning "The flash driver lacks page layout support; falling back on hacks." |
| 48 | |
| 49 | #if !defined(FLASH_AREA_IMAGE_SECTOR_SIZE) |
| 50 | #define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE |
| 51 | #endif |
| 52 | |
| 53 | extern int flash_area_get_bounds(int idx, uint32_t *off, uint32_t *len); |
| 54 | |
| 55 | int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret) |
| 56 | { |
| 57 | uint32_t off; |
| 58 | uint32_t len; |
| 59 | uint32_t max_cnt = *cnt; |
| 60 | uint32_t rem_len; |
| 61 | |
| 62 | if (flash_area_get_bounds(idx, &off, &len)) { |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | if (*cnt < 1) { |
| 67 | return -1; |
| 68 | } |
| 69 | |
| 70 | rem_len = len; |
| 71 | *cnt = 0; |
| 72 | while (rem_len > 0 && *cnt < max_cnt) { |
| 73 | if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) { |
| 74 | BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x", |
| 75 | idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | ret[*cnt].fa_id = idx; |
| 80 | ret[*cnt].fa_device_id = 0; |
| 81 | ret[*cnt].pad16 = 0; |
| 82 | ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt)); |
| 83 | ret[*cnt].fa_size = FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 84 | *cnt = *cnt + 1; |
| 85 | rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 86 | } |
| 87 | |
| 88 | if (*cnt >= max_cnt) { |
| 89 | BOOT_LOG_ERR("flash area %d sector count overflow", idx); |
| 90 | return -1; |
| 91 | } |
| 92 | |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | /* |
| 97 | * Lookup the sector map for a given flash area. This should fill in |
| 98 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 99 | * the storage at `ret` and should be set to the final number of |
| 100 | * sectors in this area. |
| 101 | */ |
| 102 | int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret) |
| 103 | { |
| 104 | uint32_t off; |
| 105 | uint32_t len; |
| 106 | uint32_t max_cnt = *cnt; |
| 107 | uint32_t rem_len; |
| 108 | |
| 109 | if (flash_area_get_bounds(idx, &off, &len)) { |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 113 | if (*cnt < 1) { |
| 114 | return -1; |
| 115 | } |
| 116 | |
| 117 | rem_len = len; |
| 118 | *cnt = 0; |
| 119 | while (rem_len > 0 && *cnt < max_cnt) { |
| 120 | if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) { |
| 121 | BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x", |
| 122 | idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt); |
| 127 | ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 128 | *cnt = *cnt + 1; |
| 129 | rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 130 | } |
| 131 | |
| 132 | if (*cnt >= max_cnt) { |
| 133 | BOOT_LOG_ERR("flash area %d sector count overflow", idx); |
| 134 | return -1; |
| 135 | } |
| 136 | |
| 137 | return 0; |
| 138 | } |