Debian Codegraph @

util/cbmem/cbmem_util.h

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <inttypes.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6
7 #include <commonlib/bsd/cbmem_id.h>
8 #include <commonlib/bsd/helpers.h>
9
10 extern int cbmem_util_verbose;
11
12 #define debug(x...) \
13 do { \
14 if (cbmem_util_verbose > 1) \
15 printf("[%s:%d %s()] ", __FILE__, __LINE__, __func__); \
16 if (cbmem_util_verbose) \
17 printf(x); \
18 } while (0)
19
20 #define die(x...) \
21 do { \
22 fprintf(stderr, x); \
23 exit(1); \
24 } while (0)
25
26 struct cbmem_console {
27 uint32_t size;
28 uint32_t cursor;
29 uint8_t body[];
30 } __packed;
31
32 #define CBMC_CURSOR_MASK ((1 << 28) - 1)
33 #define CBMC_OVERFLOW (1 << 31)
34
35 /**
36 * Function pointer type used by CBMEM foreach iteration calls.
37 *
38 * @param id CBMEM_ID_* value.
39 * @param physical_address CBMEM entry address in physical memory.
40 * @param buf heap-allocated buffer with CBMEM entry contents.
41 * @param size is the size of CBMEM entry in bytes.
42 * @param data callback-specific context data.
43 *
44 * @returns true if iteration should finish, false if it should continue.
45 */
46 typedef bool (*cbmem_iterator_callback)(const uint32_t id, const uint64_t physical_address,
47 const uint8_t *buf, const size_t size, void *data);
48
49 /* Common CBMEM access API */
50
51 enum cbmem_drv_backend_type {
52 CBMEM_DRV_BACKEND_ANY,
53 CBMEM_DRV_BACKEND_DEVMEM,
54 CBMEM_DRV_BACKEND_SYSFS,
55 };
56
57 /**
58 * Pick and initialize CBMEM driver. Function can either probe for available drivers or initialize only the selected one.
59 *
60 * @param backend preferred CBMEM backend to initialize.
61 * @param writeable initialize CBMEM driver in writeable mode. Required by devmem driver to modify CBMEM.
62 *
63 * @returns true on success, false otherwise.
64 */
65 bool cbmem_drv_init(enum cbmem_drv_backend_type backend, bool writeable);
66
67 /**
68 * Cleanup and terminate previously initialized CBMEM driver.
69 * **MUST** be called if cbmem_drv_init() succeeded.
70 */
71 void cbmem_drv_terminate(void);
72
73 /**
74 * Get CBMEM entry as an allocated buffer.
75 *
76 * @param id CBMEM_ID_* value.
77 * @param buf_out return pointer for the allocated buffer containing entry contents.
78 * @param size_out size of returned buffer.
79 * @param addr_out pointer to the output buffer for entry address in physical memory. Optional.
80 *
81 * @returns true on success, false otherwise.
82 */
83 bool cbmem_drv_get_cbmem_entry(uint32_t id, uint8_t **buf_out, size_t *size_out, uint64_t *addr_out);
84
85 /**
86 * Write provided buffer contents to the CBMEM entry.
87 *
88 * @param id CBMEM_ID_* value.
89 * @param buf pointer to the source buffer.
90 * @param buf_size size of the source buffer.
91 *
92 * @returns true on success, false otherwise.
93 */
94 bool cbmem_drv_write_cbmem_entry(uint32_t id, uint8_t *buf, size_t buf_size);
95
96 /**
97 * Backend-specific function iterating over CBMEM entries.
98 *
99 * @param cb user callback function to call during iteration.
100 * @param data pointer to the context data for the callback.
101 * @param with_contents tells whether the callback should get NULL (false) or copy of the entry (true).
102 */
103 void cbmem_drv_foreach_cbmem_entry(cbmem_iterator_callback cb, void *data, bool with_contents);
104
105 /* API for accessing CBMEM via /dev/mem */
106
107 /**
108 * Initialize the driver.
109 *
110 * @param writeable tries to map CBMEM in R/W mode.
111 *
112 * @returns true on success, false otherwise.
113 */
114 bool cbmem_devmem_init(bool writeable);
115
116 /**
117 * Cleanup and terminate the driver. **MUST** be called if cbmem_devmem_init succeeded before.
118 */
119 void cbmem_devmem_terminate(void);
120
121 /**
122 * Get CBMEM entry as an allocated buffer.
123 *
124 * @param id CBMEM_ID_* value.
125 * @param buf_out return pointer for the allocated buffer containing entry contents.
126 * @param size_out size of returned buffer. Optional.
127 * @param addr_out pointer to the output buffer for entry address in physical memory. Optional.
128 *
129 * @returns true on success, false otherwise.
130 */
131 bool cbmem_devmem_get_cbmem_entry(uint32_t id, uint8_t **buf_out, size_t *size_out, uint64_t *addr_out);
132
133 /**
134 * Write provided buffer contents to the CBMEM entry.
135 *
136 * @param id CBMEM_ID_* value.
137 * @param buf pointer to the source buffer.
138 * @param buf_size size of the source buffer.
139 *
140 * @returns true on success, false otherwise.
141 */
142 bool cbmem_devmem_write_cbmem_entry(uint32_t id, uint8_t *buf, size_t buf_size);
143
144 /**
145 * Backend-specific function iterating over CBMEM entries.
146 *
147 * @param cb user callback function to call during iteration.
148 * @param data pointer to the context data for the callback.
149 * @param with_contents tells whether the callback should get NULL (false) or copy of the entry (true).
150 */
151 void cbmem_devmem_foreach_cbmem_entry(cbmem_iterator_callback cb, void *data,
152 bool with_contents);
153
154 /* API for accessing CBMEM via sysfs entries */
155
156 /**
157 * Initialize the sysfs driver.
158 *
159 * @returns true on success, false otherwise.
160 */
161 bool cbmem_sysfs_init(void);
162
163 /**
164 * Get CBMEM entry as an allocated buffer.
165 *
166 * @param id CBMEM_ID_* value.
167 * @param buf_out return pointer for the allocated buffer containing entry contents.
168 * @param size_out size of returned buffer.
169 * @param addr_out pointer to the output buffer for entry address in physical memory. Optional.
170 *
171 * @returns true on success, false otherwise.
172 */
173 bool cbmem_sysfs_get_cbmem_entry(uint32_t id, uint8_t **buf_out, size_t *size_out, uint64_t *addr_out);
174
175 /**
176 * Write provided buffer contents to the CBMEM entry.
177 *
178 * @param id CBMEM_ID_* value.
179 * @param buf pointer to the source buffer.
180 * @param buf_size size of the source buffer.
181 *
182 * @returns true on success, false otherwise.
183 */
184 bool cbmem_sysfs_write_cbmem_entry(uint32_t id, uint8_t *buf, size_t buf_size);
185
186 /**
187 * Backend-specific function iterating over CBMEM entries.
188 *
189 * @param cb user callback function to call during iteration.
190 * @param data pointer to the context data for the callback.
191 * @param with_contents tells whether the callback should get NULL (false) or copy of the entry (true).
192 */
193 void cbmem_sysfs_foreach_cbmem_entry(cbmem_iterator_callback cb, void *data,
194 bool with_contents);