blob: b21794721947106a6ae82a8f45f622b469734809 [file] [log] [blame]
Marti Bolivardc4c42b2017-09-21 14:20:40 -04001/*
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 Bolivardc4c42b2017-09-21 14:20:40 -040040#include "bootutil/bootutil_log.h"
41
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020042#include <flash_map_backend/flash_map_backend.h>
Marti Bolivardc4c42b2017-09-21 14:20:40 -040043#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 Santo9f1933d2018-11-20 10:59:59 +010052MCUBOOT_LOG_MODULE_DECLARE(mcuboot);
53
Marti Bolivardc4c42b2017-09-21 14:20:40 -040054/*
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 */
60int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
61{
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020062 const struct flash_area *fa;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040063 uint32_t max_cnt = *cnt;
64 uint32_t rem_len;
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020065 int rc = -1;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040066
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020067 if (flash_area_open(idx, &fa)) {
68 goto out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040069 }
70
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020071 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off,
72 fa->fa_size);
73
Marti Bolivardc4c42b2017-09-21 14:20:40 -040074 if (*cnt < 1) {
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020075 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040076 }
77
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020078 rem_len = fa->fa_size;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040079 *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 Puzdrowskib788c712018-04-12 12:42:49 +020083 idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE);
84 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040085 }
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 Puzdrowskib788c712018-04-12 12:42:49 +020095 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040096 }
97
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020098 rc = 0;
99
100fa_close_out:
101 flash_area_close(fa);
102out:
103 return rc;
Marti Bolivardc4c42b2017-09-21 14:20:40 -0400104}