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 | |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 40 | #include "bootutil/bootutil_log.h" |
| 41 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 42 | #include <flash_map_backend/flash_map_backend.h> |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 43 | #include <inttypes.h> |
| 44 | #include <target.h> |
| 45 | |
| 46 | #warning "The flash driver lacks page layout support; falling back on hacks." |
| 47 | |
| 48 | #if !defined(FLASH_AREA_IMAGE_SECTOR_SIZE) |
| 49 | #define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE |
| 50 | #endif |
| 51 | |
Emanuele Di Santo | 9f1933d | 2018-11-20 10:59:59 +0100 | [diff] [blame] | 52 | MCUBOOT_LOG_MODULE_DECLARE(mcuboot); |
| 53 | |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 54 | /* |
| 55 | * Lookup the sector map for a given flash area. This should fill in |
| 56 | * `ret` with all of the sectors in the area. `*cnt` will be set to |
| 57 | * the storage at `ret` and should be set to the final number of |
| 58 | * sectors in this area. |
| 59 | */ |
| 60 | int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret) |
| 61 | { |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 62 | const struct flash_area *fa; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 63 | uint32_t max_cnt = *cnt; |
| 64 | uint32_t rem_len; |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 65 | int rc = -1; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 66 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 67 | if (flash_area_open(idx, &fa)) { |
| 68 | goto out; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 69 | } |
| 70 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 71 | BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off, |
| 72 | fa->fa_size); |
| 73 | |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 74 | if (*cnt < 1) { |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 75 | goto fa_close_out; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 76 | } |
| 77 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 78 | rem_len = fa->fa_size; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 79 | *cnt = 0; |
| 80 | while (rem_len > 0 && *cnt < max_cnt) { |
| 81 | if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) { |
| 82 | BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x", |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 83 | idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE); |
| 84 | goto fa_close_out; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt); |
| 88 | ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 89 | *cnt = *cnt + 1; |
| 90 | rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE; |
| 91 | } |
| 92 | |
| 93 | if (*cnt >= max_cnt) { |
| 94 | BOOT_LOG_ERR("flash area %d sector count overflow", idx); |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 95 | goto fa_close_out; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 96 | } |
| 97 | |
Andrzej Puzdrowski | b788c71 | 2018-04-12 12:42:49 +0200 | [diff] [blame] | 98 | rc = 0; |
| 99 | |
| 100 | fa_close_out: |
| 101 | flash_area_close(fa); |
| 102 | out: |
| 103 | return rc; |
Marti Bolivar | dc4c42b | 2017-09-21 14:20:40 -0400 | [diff] [blame] | 104 | } |