Coverage Report

Created: 2026-12-01 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/common/state/state.h
Line
Count
Source
1
// SPDX-License-Identifier: GPL-2.0-only
2
3
#include <linux/types.h>
4
#include <linux/list.h>
5
#include <linux/err.h>
6
#include <driver.h>
7
8
struct state;
9
struct mtd_info_user;
10
11
enum state_flags {
12
  STATE_FLAG_NO_AUTHENTICATION = (1 << 0),
13
};
14
15
enum state_variable_type {
16
  STATE_VARIABLE_TYPE_UINT8,
17
  STATE_VARIABLE_TYPE_UINT32,
18
  STATE_VARIABLE_TYPE_ENUM32,
19
  STATE_VARIABLE_TYPE_MAC,
20
  STATE_VARIABLE_TYPE_STRING
21
};
22
23
/**
24
 * state_backend_storage_bucket - This class describes a single backend storage
25
 * object copy
26
 *
27
 * @init Optional, initiates the given bucket
28
 * @write Required, writes the given data to the storage in any form. Returns 0
29
 * on success
30
 * @read Required, reads the last successfully written data from the backend
31
 * storage. Returns 0 on success and allocates a matching memory area to buf.
32
 * len_hint can be a hint of the storage format how large the data to be read
33
 * is. After the operation len_hint contains the size of the allocated buffer.
34
 * @free Required, Frees all internally used memory
35
 * @bucket_list A list element struct to attach this bucket to a list
36
 */
37
struct state_backend_storage_bucket {
38
  int (*write) (struct state_backend_storage_bucket * bucket,
39
          const void * buf, ssize_t len);
40
  int (*read) (struct state_backend_storage_bucket * bucket,
41
         void ** buf, ssize_t * len_hint);
42
  void (*free) (struct state_backend_storage_bucket * bucket);
43
44
  int num;
45
  off_t offset;
46
47
  struct list_head bucket_list;
48
49
  void *buf;
50
  ssize_t len;
51
  bool needs_refresh;
52
  bool wrong_magic;
53
};
54
55
/**
56
 * state_backend_format - This class describes a data format.
57
 *
58
 * @verify Required, Verifies the validity of the given data. The buffer that is
59
 * passed into this function may be larger than the actual data in the buffer.
60
 * The magic is supplied by the state to verify that this is an expected state
61
 * entity. The function should return 0 on success or a negative errno otherwise.
62
 * @pack Required, Packs data from the given state into a newly created buffer.
63
 * The buffer and its length are stored in the given argument pointers. Returns
64
 * 0 on success, -errno otherwise.
65
 * @unpack Required, Unpacks the data from the given buffer into the state. Do
66
 * not free the buffer.
67
 * @free Optional, Frees all allocated memory and structures.
68
 * @name Name of this backend.
69
 */
70
struct state_backend_format {
71
  int (*verify) (struct state_backend_format * format, uint32_t magic,
72
           const void * buf, ssize_t *lenp, enum state_flags flags);
73
  int (*pack) (struct state_backend_format * format, struct state * state,
74
         void ** buf, ssize_t * len);
75
  int (*unpack) (struct state_backend_format * format,
76
           struct state * state, const void * buf, ssize_t len);
77
  void (*free) (struct state_backend_format * format);
78
  const char *name;
79
};
80
81
/**
82
 * state_backend_storage - Storage backend of the state.
83
 *
84
 * @buckets List of storage buckets that are available
85
 * @stridesize The distance between copies
86
 * @offset Offset in the backend device where the data starts
87
 * @max_size The maximum size of the data we can use
88
 */
89
struct state_backend_storage {
90
  struct list_head buckets;
91
92
  /* For outputs */
93
  struct device *dev;
94
95
  const char *name;
96
97
  uint32_t stridesize;
98
  off_t offset;
99
  size_t max_size;
100
  const char *path;
101
102
  bool readonly;
103
};
104
105
struct state {
106
  struct list_head list; /* Entry to enqueue on list of states */
107
108
  struct device dev;
109
  char *of_path;
110
  const char *name;
111
  uint32_t magic;
112
  bool keep_prev_content;
113
114
  struct list_head variables; /* Sorted list of variables */
115
116
  unsigned int dirty;
117
  unsigned int init_from_defaults;
118
  unsigned int save_on_shutdown;
119
120
  struct state_backend_format *format;
121
  struct state_backend_storage storage;
122
  char *backend_path;
123
  char *backend_reproducible_name;
124
};
125
126
enum state_convert {
127
  STATE_CONVERT_FROM_NODE,
128
  STATE_CONVERT_FROM_NODE_CREATE,
129
  STATE_CONVERT_TO_NODE,
130
  STATE_CONVERT_FIXUP,
131
};
132
133
struct state_variable;
134
135
/* A variable type (uint32, enum32) */
136
struct variable_type {
137
  const char *type_name;
138
  enum state_variable_type type;
139
  struct list_head list;
140
  int (*export) (struct state_variable *, struct device_node *,
141
           enum state_convert);
142
  int (*import) (struct state_variable *, struct device_node *);
143
  struct state_variable *(*create) (struct state *,
144
            const char *,
145
            struct device_node *,
146
            const struct variable_type *);
147
};
148
149
/* instance of a single variable */
150
struct state_variable {
151
  struct state *state;
152
  struct list_head list;
153
  const struct variable_type *type;
154
  const char *name;
155
  unsigned int start;
156
  unsigned int size;
157
  void *raw;
158
};
159
160
/*
161
 *  uint32
162
 */
163
struct state_uint32 {
164
  struct state_variable var;
165
  uint32_t value;
166
  uint32_t value_default;
167
};
168
169
/*
170
 *  enum32
171
 */
172
struct state_enum32 {
173
  struct state_variable var;
174
  uint32_t value;
175
  uint32_t value_default;
176
  const char **names;
177
  int num_names;
178
};
179
180
/*
181
 *  MAC address
182
 */
183
struct state_mac {
184
  struct state_variable var;
185
  uint8_t value[6];
186
  uint8_t value_default[6];
187
};
188
189
/*
190
 *  string
191
 */
192
struct state_string {
193
  struct state_variable var;
194
  char *value;
195
  const char *value_default;
196
  char raw[];
197
};
198
199
int state_from_node(struct state *state, struct device_node *node, bool create);
200
struct device_node *state_to_node(struct state *state,
201
          struct device_node *parent,
202
          enum state_convert conv);
203
int backend_format_raw_create(struct state_backend_format **format,
204
            struct device_node *node, const char *secret_name,
205
            struct device *dev);
206
int backend_format_dtb_create(struct state_backend_format **format,
207
            struct device *dev);
208
int state_storage_init(struct state *state, const char *path,
209
           off_t offset, size_t max_size, uint32_t stridesize,
210
           const char *storagetype);
211
void state_storage_set_readonly(struct state_backend_storage *storage);
212
void state_add_var(struct state *state, struct state_variable *var);
213
struct variable_type *state_find_type_by_name(const char *name);
214
int state_backend_bucket_circular_create(struct device *dev, const char *path,
215
           struct state_backend_storage_bucket **bucket,
216
           unsigned int eraseblock,
217
           ssize_t writesize,
218
           struct mtd_info_user *mtd_uinfo);
219
int state_backend_bucket_cached_create(struct device *dev,
220
               struct state_backend_storage_bucket *raw,
221
               struct state_backend_storage_bucket **out);
222
struct state_variable *state_find_var(struct state *state, const char *name);
223
struct digest *state_backend_format_raw_get_digest(struct state_backend_format
224
               *format);
225
void state_backend_set_readonly(struct state *state);
226
void state_storage_free(struct state_backend_storage *storage);
227
int state_backend_bucket_direct_create(struct device *dev, const char *path,
228
               struct state_backend_storage_bucket **bucket,
229
               off_t offset, ssize_t max_size, bool readonly);
230
int state_storage_write(struct state_backend_storage *storage,
231
      const void * buf, ssize_t len);
232
int state_storage_read(struct state_backend_storage *storage,
233
           struct state_backend_format *format,
234
           uint32_t magic, void **buf, ssize_t *len,
235
           enum state_flags flags);
236
237
static inline struct state_uint32 *to_state_uint32(struct state_variable *s)
238
64
{
239
64
  return container_of(s, struct state_uint32, var);
240
64
}
Unexecuted instantiation: state.c:to_state_uint32
state_variables.c:to_state_uint32
Line
Count
Source
238
64
{
239
64
  return container_of(s, struct state_uint32, var);
240
64
}
Unexecuted instantiation: backend_format_dtb.c:to_state_uint32
Unexecuted instantiation: backend_format_raw.c:to_state_uint32
Unexecuted instantiation: backend_storage.c:to_state_uint32
Unexecuted instantiation: backend_bucket_direct.c:to_state_uint32
Unexecuted instantiation: backend_bucket_circular.c:to_state_uint32
Unexecuted instantiation: state_fuzz.c:to_state_uint32
241
242
static inline struct state_enum32 *to_state_enum32(struct state_variable *s)
243
0
{
244
0
  return container_of(s, struct state_enum32, var);
245
0
}
Unexecuted instantiation: state.c:to_state_enum32
Unexecuted instantiation: state_variables.c:to_state_enum32
Unexecuted instantiation: backend_format_dtb.c:to_state_enum32
Unexecuted instantiation: backend_format_raw.c:to_state_enum32
Unexecuted instantiation: backend_storage.c:to_state_enum32
Unexecuted instantiation: backend_bucket_direct.c:to_state_enum32
Unexecuted instantiation: backend_bucket_circular.c:to_state_enum32
Unexecuted instantiation: state_fuzz.c:to_state_enum32
246
247
static inline struct state_mac *to_state_mac(struct state_variable *s)
248
0
{
249
0
  return container_of(s, struct state_mac, var);
250
0
}
Unexecuted instantiation: state.c:to_state_mac
Unexecuted instantiation: state_variables.c:to_state_mac
Unexecuted instantiation: backend_format_dtb.c:to_state_mac
Unexecuted instantiation: backend_format_raw.c:to_state_mac
Unexecuted instantiation: backend_storage.c:to_state_mac
Unexecuted instantiation: backend_bucket_direct.c:to_state_mac
Unexecuted instantiation: backend_bucket_circular.c:to_state_mac
Unexecuted instantiation: state_fuzz.c:to_state_mac
251
252
static inline struct state_string *to_state_string(struct state_variable *s)
253
0
{
254
0
  return container_of(s, struct state_string, var);
255
0
}
Unexecuted instantiation: state.c:to_state_string
Unexecuted instantiation: state_variables.c:to_state_string
Unexecuted instantiation: backend_format_dtb.c:to_state_string
Unexecuted instantiation: backend_format_raw.c:to_state_string
Unexecuted instantiation: backend_storage.c:to_state_string
Unexecuted instantiation: backend_bucket_direct.c:to_state_string
Unexecuted instantiation: backend_bucket_circular.c:to_state_string
Unexecuted instantiation: state_fuzz.c:to_state_string
256
257
static inline int state_string_copy_to_raw(struct state_string *string,
258
            const char *src)
259
0
{
260
0
  size_t len;
261
262
0
  len = strlen(src);
263
0
  if (len > string->var.size)
264
0
    return -EILSEQ;
265
266
  /* copy string and clear remaining contents of buffer */
267
0
  memcpy(string->raw, src, len);
268
0
  memset(string->raw + len, 0x0, string->var.size - len);
269
270
0
  return 0;
271
0
}
Unexecuted instantiation: state.c:state_string_copy_to_raw
Unexecuted instantiation: state_variables.c:state_string_copy_to_raw
Unexecuted instantiation: backend_format_dtb.c:state_string_copy_to_raw
Unexecuted instantiation: backend_format_raw.c:state_string_copy_to_raw
Unexecuted instantiation: backend_storage.c:state_string_copy_to_raw
Unexecuted instantiation: backend_bucket_direct.c:state_string_copy_to_raw
Unexecuted instantiation: backend_bucket_circular.c:state_string_copy_to_raw
Unexecuted instantiation: state_fuzz.c:state_string_copy_to_raw
272
273
#ifdef DEBUG
274
#define MSG_STATE_ZERO_INIT LOGLEVEL_INFO
275
#else
276
#define MSG_STATE_ZERO_INIT LOGLEVEL_DEBUG
277
#endif
278
279
76
#define dev_err_state_init(dev, ret, fmt, ...) ({ \
280
152
  int __ret = (ret); \
281
76
  __dev_printf(__ret == -ENOMEDIUM ? MSG_STATE_ZERO_INIT : LOGLEVEL_ERR, \
282
76
         (dev), "init error: %pe: " fmt, ERR_PTR(__ret), ##__VA_ARGS__); \
283
76
  __ret; \
284
76
})