/
bin_descriptors.c
75 lines (62 loc) · 1.88 KB
/
bin_descriptors.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <zephyr/kernel.h>
#include <zephyr/bin_descriptors.h>
#include <zephyr/sys/util.h>
#define BIN_DESC_ALIGN(x) (struct bin_descriptor *)ROUND_UP((x), BIN_DESCRIPTOR_ALIGNMENT)
static int is_valid_range(uint32_t *address, size_t count)
{
#ifdef CONFIG_QEMU_TARGET
return true;
#else
return !((address < (uint32_t *)CONFIG_FLASH_BASE_ADDRESS) ||
(address >= (uint32_t *)CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_SIZE) ||
(address + count >= (uint32_t *)CONFIG_FLASH_BASE_ADDRESS + CONFIG_FLASH_SIZE));
#endif
}
static int bin_descriptors_walk(uint32_t *address, bin_descriptors_foreach_cb callback,
void *user_data, bool search, uint32_t search_id,
struct bin_descriptor **bd)
{
struct bin_descriptor *iterator;
uint32_t magic;
magic = (uint32_t)*address++;
if (magic != BIN_DESCRIPTOR_MAGIC) {
return -EINVAL;
}
iterator = ((struct bin_descriptor *)address);
while (iterator->id != BIN_DESC_TYPE_DESCRIPTORS_END) {
if (search) {
if (iterator->id == search_id) {
*bd = iterator;
return 0;
}
} else {
callback(iterator->id, &(iterator->data), user_data);
}
iterator = BIN_DESC_ALIGN((uint8_t *)iterator +
sizeof(struct bin_descriptor) + iterator->len);
}
return search ? -1 : 0;
}
int bin_descriptors_foreach(uint32_t *address, bin_descriptors_foreach_cb callback,
void *user_data)
{
return bin_descriptors_walk(address, callback, user_data, false, 0, NULL);
}
int bin_descriptors_get_by_id(uint32_t *address, uint32_t id, struct bin_descriptor **entry)
{
return bin_descriptors_walk(address, NULL, NULL, true, id, entry);
}
int bin_descriptors_search_header(uint32_t *start_address, size_t count, uint32_t **result)
{
size_t i;
if (!is_valid_range(start_address, count)) {
return -EINVAL;
}
for (i = 0; i < count; i++) {
if (*(start_address + i) == BIN_DESCRIPTOR_MAGIC) {
*result = start_address + i;
return 0;
}
}
return -1;
}