xref: /aosp_15_r20/external/vboot_reference/host/lib/include/flashrom.h (revision 8617a60d3594060b7ecbd21bc622a7c14f3cf2bc)
1 /* Copyright 2020 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  *
5  * Host utilites to execute flashrom command.
6  */
7 
8 #include <stdint.h>
9 
10 #include "2return_codes.h"
11 #include "fmap.h"
12 
13 #define FLASHROM_PROGRAMMER_INTERNAL_AP "internal"
14 #define FLASHROM_PROGRAMMER_INTERNAL_EC "ec"
15 
16 /* Utilities for firmware images and (FMAP) sections */
17 struct firmware_image {
18 	/**
19 	 * programmer	The name of the programmer to use. Use either
20 	 *		FLASHROM_PROGRAMMER_INTERNAL_AP or,
21 	 *		FLASHROM_PROGRAMMER_INTERNAL_EC
22 	 *		for the AP and EC respectively.
23 	 */
24 	const char *programmer;
25 	uint32_t size; /* buffer size. */
26 	uint8_t *data; /* data allocated buffer to read/write with. */
27 	char *file_name;
28 	char *ro_version, *rw_version_a, *rw_version_b;
29 	/* AP RW sections may contain a special ECRW binary for syncing EC
30 	   firmware on boot. These 2 fields are valid only for AP image. */
31 	char *ecrw_version_a, *ecrw_version_b;
32 	FmapHeader *fmap_header;
33 };
34 
35 /**
36  * Read using flashrom into an allocated buffer.
37  *
38  * flashrom_read subprocesses the flashrom binary and returns a buffer truncated
39  * to the region.
40  *
41  * flashrom_read_image reads the returns a full sized buffer with only the
42  * regions filled with data.
43  *
44  * flashrom_read_region returns the buffer truncated to the region.
45  *
46  * @param image		The parameter that contains the programmer, buffer and
47  *			size to use in the read operation.
48  * @param regions	A list of the names of the fmap regions to read. Must
49  *			be non-null if regions_len is non-zero. Otherwise, must
50  *			be at least regions_len items long.
51  * @param regions_len	The size of regions, or 0 to read the entire flash
52  *			chip.
53  *
54  * @return VB2_SUCCESS on success, or a relevant error.
55  */
56 vb2_error_t flashrom_read(struct firmware_image *image, const char *region);
57 int flashrom_read_image(struct firmware_image *image,
58 			const char *const regions[], size_t regions_len,
59 			int verbosity);
60 int flashrom_read_region(struct firmware_image *image, const char *region,
61 			 int verbosity);
62 
63 /**
64  * Write using flashrom from a buffer.
65  *
66  * @param image		The parameter that contains the programmer, buffer and
67  *			size to use in the write operation.
68  * @param regions	A list of the names of the fmap regions to write. Must
69  *			be non-null if regions_len is non-zero. Otherwise, must
70  *			be at least regions_len items long.
71  * @param regions_len	The size of regions, or 0 to write the entire flash
72  *			chip.
73  *
74  * @return VB2_SUCCESS on success, or a relevant error.
75  */
76 vb2_error_t flashrom_write(struct firmware_image *image, const char *region);
77 int flashrom_write_image(const struct firmware_image *image,
78 			 const char *const regions[], size_t regions_len,
79 			 const struct firmware_image *diff_image, int do_verify,
80 			 int verbosity);
81 
82 /**
83  * Get wp state using flashrom.
84  *
85  * @param programmer	The name of the programmer to use for reading the
86  *                      writeprotect state.
87  * @param wp_mode       Pointer to a bool to store the WP mode. Will be set to
88  *                      false if WP is disabled, true if WP is enabled.
89  *                      NULL can be passed if not needed.
90  * @param wp_start      Pointer to a uint32_t to store the WP start addr.
91  *                      NULL can be passed if not needed.
92  * @param wp_len        Pointer to a uint32_t to store the WP region length.
93  *                      NULL can be passed if not needed.
94  *
95  * @return 0 on success, or a relevant error.
96  */
97 int flashrom_get_wp(const char *programmer, bool *wp_mode,
98 		    uint32_t *wp_start, uint32_t *wp_len, int verbosity);
99 
100 /**
101  * Set wp state using flashrom.
102  *
103  * @param programmer	The name of the programmer to use for writing the
104  *                      writeprotect state.
105  * @param wp_mode       WP mode to set. true to enable, false disable WP.
106  * @param wp_start      WP start addr to set
107  * @param wp_len        WP region length set
108  *
109  * @return 0 on success, or a relevant error.
110  */
111 int flashrom_set_wp(const char *programmer, bool wp_mode,
112 		    uint32_t wp_start, uint32_t wp_len, int verbosity);
113 
114 /**
115  * Get flash info using flashrom.
116  *
117  * @param programmer	The name of the programmer to use.
118  * @param vendor        The chip vendor name, non-NULLable.
119  * @param name          The chip product name, non-NULLable.
120  * @param vid           The chip vendor id, non-NULLable.
121  * @param pid           The chip product id, non-NULLable.
122  * @param flash_len     Pointer to a uint32_t to store chip length, non-NULLable.
123  *
124  * @return 0 on success, or a relevant error.
125  */
126 int flashrom_get_info(const char *prog_with_params,
127 		      char **vendor, char **name,
128 		      uint32_t *vid, uint32_t *pid,
129 		      uint32_t *flash_len, int verbosity);
130 
131 /**
132  * Get flash size using flashrom.
133  *
134  * @param programmer	The name of the programmer to use.
135  * @param flash_len     Pointer to a uint32_t to store chip length.
136  *
137  * @return 0 on success, or a relevant error.
138  */
139 int flashrom_get_size(const char *programmer, uint32_t *flash_len,
140 		      int verbosity);
141