blob: e7daa415ea2271e3182fe63ee065d6c8624440ea [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
40#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
41#include "bootutil/bootutil_log.h"
42
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020043#include <flash_map_backend/flash_map_backend.h>
Marti Bolivardc4c42b2017-09-21 14:20:40 -040044#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
Marti Bolivardc4c42b2017-09-21 14:20:40 -040053/*
54 * Lookup the sector map for a given flash area. This should fill in
55 * `ret` with all of the sectors in the area. `*cnt` will be set to
56 * the storage at `ret` and should be set to the final number of
57 * sectors in this area.
58 */
59int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
60{
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020061 const struct flash_area *fa;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040062 uint32_t max_cnt = *cnt;
63 uint32_t rem_len;
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020064 int rc = -1;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040065
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020066 if (flash_area_open(idx, &fa)) {
67 goto out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040068 }
69
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020070 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off,
71 fa->fa_size);
72
Marti Bolivardc4c42b2017-09-21 14:20:40 -040073 if (*cnt < 1) {
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020074 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040075 }
76
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020077 rem_len = fa->fa_size;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040078 *cnt = 0;
79 while (rem_len > 0 && *cnt < max_cnt) {
80 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
81 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020082 idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE);
83 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040084 }
85
86 ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
87 ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
88 *cnt = *cnt + 1;
89 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
90 }
91
92 if (*cnt >= max_cnt) {
93 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020094 goto fa_close_out;
Marti Bolivardc4c42b2017-09-21 14:20:40 -040095 }
96
Andrzej Puzdrowskib788c712018-04-12 12:42:49 +020097 rc = 0;
98
99fa_close_out:
100 flash_area_close(fa);
101out:
102 return rc;
Marti Bolivardc4c42b2017-09-21 14:20:40 -0400103}