blob: 34b6b544d8fa5a997164596084133aec258e1581 [file] [log] [blame]
David Vinczecea8b592019-10-29 16:09:51 +01001/*
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 * Original code taken from mcuboot project at:
22 * https://github.com/JuulLabs-OSS/mcuboot
23 * Git SHA of the original version: 4f0ea747c314547daa6b6299ccbd77ae4dee6758
24 * Modifications are Copyright (c) 2019 Arm Limited.
25 */
26
27#include "bootutil/bootutil_log.h"
28#include "flash_map/flash_map.h"
29#include <inttypes.h>
30#include <target.h>
31
32/*
33 * Lookup the sector map for a given flash area. This should fill in
34 * `ret` with all of the sectors in the area. `*cnt` will be set to
35 * the storage at `ret` and should be set to the final number of
36 * sectors in this area.
37 */
38int flash_area_get_sectors(int idx, uint32_t *cnt, struct flash_sector *ret)
39{
40 const struct flash_area *fa;
41 uint32_t max_cnt = *cnt;
42 uint32_t rem_len;
43 int rc = -1;
44
45 if (flash_area_open(idx, &fa)) {
46 goto out;
47 }
48
49 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x", idx, fa->fa_off,
50 fa->fa_size);
51
52 if (*cnt < 1) {
53 goto fa_close_out;
54 }
55
56 rem_len = fa->fa_size;
57 *cnt = 0;
58 while (rem_len > 0 && *cnt < max_cnt) {
59 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
60 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
61 idx, fa->fa_size, FLASH_AREA_IMAGE_SECTOR_SIZE);
62 goto fa_close_out;
63 }
64
65 ret[*cnt].fs_off = FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt);
66 ret[*cnt].fs_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
67 *cnt = *cnt + 1;
68 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
69 }
70
71 if (*cnt > max_cnt) {
72 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
73 goto fa_close_out;
74 }
75
76 rc = 0;
77
78fa_close_out:
79 flash_area_close(fa);
80out:
81 return rc;
82}