blob: edb02ed38701f8f622fb03c939caf5ac45e3735d [file] [log] [blame]
David Brown5153bd62017-01-06 11:16:53 -07001/*
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#include <zephyr.h>
David Brown5153bd62017-01-06 11:16:53 -070021#include <flash.h>
22
Marti Bolivar51181cf2017-03-20 11:03:41 -040023#include "target.h"
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020024
David Brown5153bd62017-01-06 11:16:53 -070025#include <flash_map/flash_map.h>
26#include <hal/hal_flash.h>
27#include <sysflash/sysflash.h>
28
Marti Bolivar4a97b4c2017-01-31 18:20:02 -050029#define BOOT_LOG_LEVEL BOOT_LOG_LEVEL_INFO
30#include "bootutil/bootutil_log.h"
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020031
David Brown5153bd62017-01-06 11:16:53 -070032extern struct device *boot_flash_device;
33
34/*
35 * The flash area describes essentially the partition table of the
36 * flash. In this case, it starts with FLASH_AREA_IMAGE_0.
37 */
38static const struct flash_area part_map[] = {
39 {
40 .fa_id = FLASH_AREA_IMAGE_0,
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020041 .fa_off = FLASH_AREA_IMAGE_0_OFFSET,
42 .fa_size = FLASH_AREA_IMAGE_0_SIZE,
David Brown5153bd62017-01-06 11:16:53 -070043 },
44 {
45 .fa_id = FLASH_AREA_IMAGE_1,
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020046 .fa_off = FLASH_AREA_IMAGE_1_OFFSET,
47 .fa_size = FLASH_AREA_IMAGE_1_SIZE,
David Brown5153bd62017-01-06 11:16:53 -070048 },
49 {
50 .fa_id = FLASH_AREA_IMAGE_SCRATCH,
Ricardo Salveti3a2c1242017-01-19 10:22:35 -020051 .fa_off = FLASH_AREA_IMAGE_SCRATCH_OFFSET,
52 .fa_size = FLASH_AREA_IMAGE_SCRATCH_SIZE,
David Brown5153bd62017-01-06 11:16:53 -070053 },
54};
55
56/*
David Brown5153bd62017-01-06 11:16:53 -070057 * `open` a flash area. The `area` in this case is not the individual
58 * sectors, but describes the particular flash area in question.
59 */
60int flash_area_open(uint8_t id, const struct flash_area **area)
61{
62 int i;
Ricardo Salveti7cf3d9e2017-01-18 16:38:22 -020063
Marti Bolivar1011b432017-03-17 17:15:58 -040064 BOOT_LOG_DBG("area %d", id);
David Brown5153bd62017-01-06 11:16:53 -070065
66 for (i = 0; i < ARRAY_SIZE(part_map); i++) {
67 if (id == part_map[i].fa_id)
68 break;
69 }
70 if (i == ARRAY_SIZE(part_map))
71 return -1;
72
73 *area = &part_map[i];
74 return 0;
75}
76
77/*
78 * Nothing to do on close.
79 */
80void flash_area_close(const struct flash_area *area)
81{
82}
83
84int flash_area_read(const struct flash_area *area, uint32_t off, void *dst,
85 uint32_t len)
86{
Marti Bolivar1011b432017-03-17 17:15:58 -040087 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
David Brown5153bd62017-01-06 11:16:53 -070088 return flash_read(boot_flash_device, area->fa_off + off, dst, len);
89}
90
91int flash_area_write(const struct flash_area *area, uint32_t off, const void *src,
92 uint32_t len)
93{
Michael Scotte12746c2017-01-31 16:07:08 -080094 int rc = 0;
95
Marti Bolivar1011b432017-03-17 17:15:58 -040096 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
Michael Scotte12746c2017-01-31 16:07:08 -080097 flash_write_protection_set(boot_flash_device, false);
98 rc = flash_write(boot_flash_device, area->fa_off + off, src, len);
99 flash_write_protection_set(boot_flash_device, true);
100 return rc;
David Brown5153bd62017-01-06 11:16:53 -0700101}
102
103int flash_area_erase(const struct flash_area *area, uint32_t off, uint32_t len)
104{
Marti Bolivar53cfdb92017-02-10 15:05:22 -0500105 int rc;
106
Marti Bolivar1011b432017-03-17 17:15:58 -0400107 BOOT_LOG_DBG("area=%d, off=%x, len=%x", area->fa_id, off, len);
Marti Bolivar53cfdb92017-02-10 15:05:22 -0500108 flash_write_protection_set(boot_flash_device, false);
109 rc = flash_erase(boot_flash_device, area->fa_off + off, len);
110 flash_write_protection_set(boot_flash_device, true);
111 return rc;
David Brown5153bd62017-01-06 11:16:53 -0700112}
113
114uint8_t flash_area_align(const struct flash_area *area)
115{
116 return hal_flash_align(area->fa_id);
117}
118
119/*
120 * This depends on the mappings defined in sysflash.h, and assumes
121 * that slot 0, slot 1, and the scratch area area contiguous.
122 */
123int flash_area_id_from_image_slot(int slot)
124{
125 return slot + FLASH_AREA_IMAGE_0;
126}
127
Marti Bolivar29d3a772017-03-20 10:44:25 -0400128#ifndef FLASH_AREA_IMAGE_SECTOR_SIZE
129#warning "Missing FLASH_AREA_IMAGE_SECTOR_SIZE; assuming scratch size instead"
130#define FLASH_AREA_IMAGE_SECTOR_SIZE FLASH_AREA_IMAGE_SCRATCH_SIZE
131#endif
132
David Brown5153bd62017-01-06 11:16:53 -0700133/*
134 * Lookup the sector map for a given flash area. This should fill in
135 * `ret` with all of the sectors in the area. `*cnt` will be set to
136 * the storage at `ret` and should be set to the final number of
137 * sectors in this area.
138 */
139int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret)
140{
141 uint32_t off;
Michael Scottde6ee592017-01-31 16:11:57 -0800142 uint32_t len;
143 uint32_t max_cnt = *cnt;
Marti Bolivar29d3a772017-03-20 10:44:25 -0400144 uint32_t rem_len;
David Brown5153bd62017-01-06 11:16:53 -0700145
David Brown5153bd62017-01-06 11:16:53 -0700146 /*
147 * This simple layout has uniform slots, so just fill in the
148 * right one.
149 */
150 if (idx < FLASH_AREA_IMAGE_0 || idx > FLASH_AREA_IMAGE_SCRATCH)
151 return -1;
152
153 if (*cnt < 1)
154 return -1;
155
Michael Scottde6ee592017-01-31 16:11:57 -0800156 switch (idx) {
157 case FLASH_AREA_IMAGE_0:
158 off = FLASH_AREA_IMAGE_0_OFFSET;
159 len = FLASH_AREA_IMAGE_0_SIZE;
160 break;
161 case FLASH_AREA_IMAGE_1:
162 off = FLASH_AREA_IMAGE_1_OFFSET;
163 len = FLASH_AREA_IMAGE_1_SIZE;
164 break;
165 case FLASH_AREA_IMAGE_SCRATCH:
166 off = FLASH_AREA_IMAGE_SCRATCH_OFFSET;
167 len = FLASH_AREA_IMAGE_SCRATCH_SIZE;
168 break;
169 default:
Marti Bolivar1011b432017-03-17 17:15:58 -0400170 BOOT_LOG_ERR("unknown flash area %d", idx);
Michael Scottde6ee592017-01-31 16:11:57 -0800171 return -1;
172 }
David Brown5153bd62017-01-06 11:16:53 -0700173
Marti Bolivar29d3a772017-03-20 10:44:25 -0400174 BOOT_LOG_DBG("area %d: offset=0x%x, length=0x%x, sector size=0x%x",
175 idx, off, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
176
177 rem_len = len;
Michael Scottde6ee592017-01-31 16:11:57 -0800178 *cnt = 0;
Marti Bolivar29d3a772017-03-20 10:44:25 -0400179 while (rem_len > 0 && *cnt < max_cnt) {
180 if (rem_len < FLASH_AREA_IMAGE_SECTOR_SIZE) {
181 BOOT_LOG_ERR("area %d size 0x%x not divisible by sector size 0x%x",
182 idx, len, FLASH_AREA_IMAGE_SECTOR_SIZE);
183 return -1;
184 }
185
Michael Scottde6ee592017-01-31 16:11:57 -0800186 ret[*cnt].fa_id = idx;
187 ret[*cnt].fa_device_id = 0;
188 ret[*cnt].pad16 = 0;
Marti Bolivar29d3a772017-03-20 10:44:25 -0400189 ret[*cnt].fa_off = off + (FLASH_AREA_IMAGE_SECTOR_SIZE * (*cnt));
190 ret[*cnt].fa_size = FLASH_AREA_IMAGE_SECTOR_SIZE;
Michael Scottde6ee592017-01-31 16:11:57 -0800191 *cnt = *cnt + 1;
Marti Bolivar29d3a772017-03-20 10:44:25 -0400192 rem_len -= FLASH_AREA_IMAGE_SECTOR_SIZE;
193 }
194
195 if (*cnt >= max_cnt) {
196 BOOT_LOG_ERR("flash area %d sector count overflow", idx);
197 return -1;
Michael Scottde6ee592017-01-31 16:11:57 -0800198 }
David Brown5153bd62017-01-06 11:16:53 -0700199
200 return 0;
201}