blob: 69803fe41ccc8e420a85e233c0c6cb31b6f39989 [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
Marti Bolivardc4c42b2017-09-21 14:20:40 -040052/*
53 * Lookup the sector map for a given flash area. This should fill in
54 * `ret` with all of the sectors in the area. `*cnt` will be set to
55 * the storage at `ret` and should be set to the final number of
56 * sectors in this area.
57 */
58int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
59{
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020060 const struct flash_area *fa;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040061 uint32_t max_cnt = *cnt;
62 uint32_t rem_len;
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020063 int rc = -1;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040064
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020065 if (flash_area_open(idx, &fa)) {
66 goto out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040067 }
68
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020069 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off,
70 fa->fa_size);
71
Marti Bolivardc4c42b2017-09-21 14:20:40 -040072 if (*cnt < 1) {
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020073 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040074 }
75
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020076 rem_len = fa->fa_size;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040077 *cnt = 0;
78 while (rem_len > 0 && *cnt < max_cnt) {
79 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
80 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020081 idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE);
82 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040083 }
84
85 ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
86 ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
87 *cnt = *cnt + 1;
88 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
89 }
90
91 if (*cnt >= max_cnt) {
92 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020093 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040094 }
95
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020096 rc = 0;
97
98fa_close_out:
99 flash_area_close(fa);
100out:
101 return rc;
Marti Bolivardc4c42b2017-09-21 14:20:40 -0400102}