1 /*
2  * Copyright (c) 2017 Nordic Semiconductor ASA
3  * Copyright (c) 2017 Linaro Limited
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #ifndef ZEPHYR_INCLUDE_DFU_FLASH_IMG_H_
9 #define ZEPHYR_INCLUDE_DFU_FLASH_IMG_H_
10 
11 #include <storage/stream_flash.h>
12 
13 #ifdef __cplusplus
14 extern "C" {
15 #endif
16 
17 struct flash_img_context {
18 	uint8_t buf[CONFIG_IMG_BLOCK_BUF_SIZE];
19 	const struct flash_area *flash_area;
20 	struct stream_flash_ctx stream;
21 };
22 
23 /**
24  * @brief Structure for verify flash region integrity
25  *
26  * Match vector length is fixed and depends on size from hash algorithm used
27  * to verify flash integrity.  The current available algorithm is SHA-256.
28  */
29 struct flash_img_check {
30 	const uint8_t *match;		/** Match vector data */
31 	size_t clen;			/** Content to be compared */
32 };
33 
34 /**
35  * @brief Initialize context needed for writing the image to the flash.
36  *
37  * @param ctx     context to be initialized
38  * @param area_id flash area id of partition where the image should be written
39  *
40  * @return  0 on success, negative errno code on fail
41  */
42 int flash_img_init_id(struct flash_img_context *ctx, uint8_t area_id);
43 
44 /**
45  * @brief Initialize context needed for writing the image to the flash.
46  *
47  * @param ctx context to be initialized
48  *
49  * @return  0 on success, negative errno code on fail
50  */
51 int flash_img_init(struct flash_img_context *ctx);
52 
53 /**
54  * @brief Read number of bytes of the image written to the flash.
55  *
56  * @param ctx context
57  *
58  * @return Number of bytes written to the image flash.
59  */
60 size_t flash_img_bytes_written(struct flash_img_context *ctx);
61 
62 /**
63  * @brief  Process input buffers to be written to the image slot 1. flash
64  * memory in single blocks. Will store remainder between calls.
65  *
66  * A final call to this function with flush set to true
67  * will write out the remaining block buffer to flash. Since flash is written to
68  * in blocks, the contents of flash from the last byte written up to the next
69  * multiple of CONFIG_IMG_BLOCK_BUF_SIZE is padded with 0xff.
70  *
71  * @param ctx context
72  * @param data data to write
73  * @param len Number of bytes to write
74  * @param flush when true this forces any buffered
75  * data to be written to flash
76  *
77  * @return  0 on success, negative errno code on fail
78  */
79 int flash_img_buffered_write(struct flash_img_context *ctx, const uint8_t *data,
80 		    size_t len, bool flush);
81 
82 /**
83  * @brief  Verify flash memory length bytes integrity from a flash area. The
84  * start point is indicated by an offset value.
85  *
86  * The function is enabled via CONFIG_IMG_ENABLE_IMAGE_CHECK Kconfig options.
87  *
88  * @param[in] ctx context.
89  * @param[in] fic flash img check data.
90  * @param[in] area_id flash area id of partition where the image should be
91  * verified.
92  *
93  * @return  0 on success, negative errno code on fail
94  */
95 int flash_img_check(struct flash_img_context *ctx,
96 		    const struct flash_img_check *fic,
97 		    uint8_t area_id);
98 
99 #ifdef __cplusplus
100 }
101 #endif
102 
103 #endif	/* ZEPHYR_INCLUDE_DFU_FLASH_IMG_H_ */
104