blob: 7622dd0851be7fb8a7d921e7a9c9fe6b3de4beea [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#ifndef H_UTIL_FLASH_MAP_
21#define H_UTIL_FLASH_MAP_
22
23#ifdef __cplusplus
24extern "C" {
25#endif
26
27/**
28 *
29 * Provides abstraction of flash regions for type of use.
30 * I.e. dude where's my image?
31 *
32 * System will contain a map which contains flash areas. Every
33 * region will contain flash identifier, offset within flash and length.
34 *
35 * 1. This system map could be in a file within filesystem (Initializer
36 * must know/figure out where the filesystem is at).
37 * 2. Map could be at fixed location for project (compiled to code)
38 * 3. Map could be at specific place in flash (put in place at mfg time).
39 *
40 * Note that the map you use must be valid for BSP it's for,
41 * match the linker scripts when platform executes from flash,
42 * and match the target offset specified in download script.
43 */
44#include <inttypes.h>
45
Marti Bolivarf0d08f22017-05-01 17:14:48 -040046/**
47 * @brief Structure describing an area on a flash device.
48 *
49 * Multiple flash devices may be available in the system, each of
50 * which may have its own areas. For this reason, flash areas track
51 * which flash device they are part of.
52 */
David Brown5153bd62017-01-06 11:16:53 -070053struct flash_area {
Marti Bolivarf0d08f22017-05-01 17:14:48 -040054 /**
55 * This flash area's ID; unique in the system.
56 */
David Brown5153bd62017-01-06 11:16:53 -070057 uint8_t fa_id;
Marti Bolivarf0d08f22017-05-01 17:14:48 -040058
59 /**
60 * ID of the flash device this area is a part of.
61 */
David Brown5153bd62017-01-06 11:16:53 -070062 uint8_t fa_device_id;
Marti Bolivarf0d08f22017-05-01 17:14:48 -040063
David Brown5153bd62017-01-06 11:16:53 -070064 uint16_t pad16;
Marti Bolivarf0d08f22017-05-01 17:14:48 -040065
66 /**
67 * This area's offset, relative to the beginning of its flash
68 * device's storage.
69 */
David Brown5153bd62017-01-06 11:16:53 -070070 uint32_t fa_off;
Marti Bolivarf0d08f22017-05-01 17:14:48 -040071
72 /**
73 * This area's size, in bytes.
74 */
David Brown5153bd62017-01-06 11:16:53 -070075 uint32_t fa_size;
76};
77
Marti Bolivara2e1b032017-06-14 09:47:36 -040078/**
79 * @brief Structure describing a sector within a flash area.
80 *
81 * Each sector has an offset relative to the start of its flash area
82 * (NOT relative to the start of its flash device), and a size. A
83 * flash area may contain sectors with different sizes.
84 */
85struct flash_sector {
86 /**
87 * Offset of this sector, from the start of its flash area (not device).
88 */
89 uint32_t fs_off;
90
91 /**
92 * Size of this sector, in bytes.
93 */
94 uint32_t fs_size;
95};
96
Marti Bolivarf6603062017-05-01 17:34:22 -040097/*
Marti Bolivard5bf5702017-06-14 09:45:37 -040098 * Retrieve a memory-mapped flash device's base address.
99 *
100 * On success, the address will be stored in the value pointed to by
101 * ret.
102 *
103 * Returns 0 on success, or an error code on failure.
104 */
105int flash_device_base(uint8_t fd_id, uintptr_t *ret);
106
107/*
David Brown5153bd62017-01-06 11:16:53 -0700108 * Start using flash area.
109 */
110int flash_area_open(uint8_t id, const struct flash_area **);
111
112void flash_area_close(const struct flash_area *);
113
114/*
115 * Read/write/erase. Offset is relative from beginning of flash area.
116 */
117int flash_area_read(const struct flash_area *, uint32_t off, void *dst,
118 uint32_t len);
119int flash_area_write(const struct flash_area *, uint32_t off, const void *src,
120 uint32_t len);
121int flash_area_erase(const struct flash_area *, uint32_t off, uint32_t len);
122
123/*
124 * Alignment restriction for flash writes.
125 */
126uint8_t flash_area_align(const struct flash_area *);
127
128/*
Fabio Utzigea0290b2018-08-09 14:23:01 -0300129 * What is value is read from erased flash bytes.
130 */
131uint8_t flash_area_erased_val(const struct flash_area *);
132
133/*
Marti Bolivara2e1b032017-06-14 09:47:36 -0400134 * Given flash area ID, return info about sectors within the area.
David Brown5153bd62017-01-06 11:16:53 -0700135 */
Marti Bolivara2e1b032017-06-14 09:47:36 -0400136int flash_area_get_sectors(int fa_id, uint32_t *count,
137 struct flash_sector *sectors);
138
139/*
140 * Similar to flash_area_get_sectors(), but return the values in an
141 * array of struct flash_area instead.
142 */
143__attribute__((deprecated))
David Brown5153bd62017-01-06 11:16:53 -0700144int flash_area_to_sectors(int idx, int *cnt, struct flash_area *ret);
145
146int flash_area_id_from_image_slot(int slot);
147int flash_area_id_to_image_slot(int area_id);
148
149#ifdef __cplusplus
150}
151#endif
152
153#endif /* H_UTIL_FLASH_MAP_ */