1 /*
2  * Copyright (c) 2021 Espressif Systems (Shanghai) Co., Ltd.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #pragma once
8 
9 #include <inttypes.h>
10 
11 struct flash_area {
12   uint8_t  fa_id;         /** The slot/scratch identification */
13   uint8_t  fa_device_id;  /** The device id (usually there's only one) */
14   uint16_t pad16;
15   uint32_t fa_off;        /** The flash offset from the beginning */
16   uint32_t fa_size;       /** The size of this sector */
17 };
18 
19 //! Structure describing a sector within a flash area.
20 struct flash_sector {
21   //! Offset of this sector, from the start of its flash area (not device).
22   uint32_t fs_off;
23 
24   //! Size of this sector, in bytes.
25   uint32_t fs_size;
26 };
27 
flash_area_get_device_id(const struct flash_area * fa)28 static inline uint8_t flash_area_get_device_id(const struct flash_area *fa)
29 {
30     return (uint8_t)fa->fa_device_id;
31 }
flash_area_get_off(const struct flash_area * fa)32 static inline uint32_t flash_area_get_off(const struct flash_area *fa)
33 {
34 	return (uint32_t)fa->fa_off;
35 }
36 
flash_area_get_size(const struct flash_area * fa)37 static inline uint32_t flash_area_get_size(const struct flash_area *fa)
38 {
39 	return (uint32_t)fa->fa_size;
40 }
41 
flash_area_get_id(const struct flash_area * fa)42 static inline uint8_t flash_area_get_id(const struct flash_area *fa)
43 {
44 	return fa->fa_id;
45 }
46 
flash_sector_get_off(const struct flash_sector * fs)47 static inline uint32_t flash_sector_get_off(const struct flash_sector *fs)
48 {
49 	return fs->fs_off;
50 }
51 
flash_sector_get_size(const struct flash_sector * fs)52 static inline uint32_t flash_sector_get_size(const struct flash_sector *fs)
53 {
54 	return fs->fs_size;
55 }
56 
57 //! Opens the area for use. id is one of the `fa_id`s */
58 int flash_area_open(uint8_t id, const struct flash_area **area_outp);
59 void flash_area_close(const struct flash_area *fa);
60 
61 //! Reads `len` bytes of flash memory at `off` to the buffer at `dst`
62 int flash_area_read(const struct flash_area *fa, uint32_t off,
63                     void *dst, uint32_t len);
64 //! Writes `len` bytes of flash memory at `off` from the buffer at `src`
65 int flash_area_write(const struct flash_area *fa, uint32_t off,
66                      const void *src, uint32_t len);
67 //! Erases `len` bytes of flash memory at `off`
68 int flash_area_erase(const struct flash_area *fa,
69                      uint32_t off, uint32_t len);
70 
71 //! Returns this `flash_area`s alignment
72 uint32_t flash_area_align(const struct flash_area *area);
73 //! Returns the value read from an erased flash area byte
74 uint8_t flash_area_erased_val(const struct flash_area *area);
75 
76 //! Given flash area ID, return info about sectors within the area
77 int flash_area_get_sectors(int fa_id, uint32_t *count,
78                            struct flash_sector *sectors);
79 
80 //! Retrieve the flash sector a given offset belongs to.
81 int flash_area_sector_from_off(uint32_t off, struct flash_sector *sector);
82 
83 //! Retrieve the flash sector a given offset belongs to.
84 int flash_area_get_sector(const struct flash_area *area, uint32_t off,
85                           struct flash_sector *sector);
86 
87 //! Returns the `fa_id` for slot, where slot is 0 (primary) or 1 (secondary).
88 //!
89 //! `image_index` (0 or 1) is the index of the image. Image index is
90 //!  relevant only when multi-image support support is enabled
91 int flash_area_id_from_multi_image_slot(int image_index, int slot);
92 int flash_area_id_from_image_slot(int slot);
93 int flash_area_to_sectors(int idx, int *cnt, struct flash_area *fa);
94