blob: a8560dc560fc3e928091b92b76c77e5691c89c2d [file] [log] [blame]
Patrick Georgiea063cb2020-05-08 19:28:13 +02001/* track which sections of the image will contain CBFSes */
Patrick Georgi7333a112020-05-08 20:48:04 +02002/* SPDX-License-Identifier: GPL-2.0-only */
Sol Boucher023e8292015-03-18 10:13:48 -07003
4#include "cbfs_sections.h"
Thejaswani Putta6f5225c2019-04-11 18:36:08 -07005#include "common.h"
Sol Boucher023e8292015-03-18 10:13:48 -07006
7#include <assert.h>
8#include <stdlib.h>
9#include <string.h>
10
11struct descriptor_node {
12 const struct flashmap_descriptor *val;
13 struct descriptor_node *next;
14};
15
16static struct descriptor_list {
17 struct descriptor_node *head;
18 struct descriptor_node *tail;
19} cbfs_sections;
20
21static bool seen_primary_section = false;
22
23static void descriptor_list_prepend(struct descriptor_list *list,
24 struct descriptor_node *new_head)
25{
26 assert(list);
27 assert(new_head);
28
29 new_head->next = list->head;
30 list->head = new_head;
31 if (!list->tail)
32 list->tail = new_head;
33}
34
35static void descriptor_list_append(struct descriptor_list *list,
36 struct descriptor_node *new_tail)
37{
38 assert(list);
39 assert(new_tail);
40
41 if (list->tail)
42 list->tail->next = new_tail;
43 list->tail = new_tail;
44 if (!list->head)
45 list->head = new_tail;
46}
47
48/* Implementation of cbfs module's callback; invoked during fmd file parsing */
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080049bool fmd_process_flag_cbfs(const struct flashmap_descriptor *node)
Sol Boucher023e8292015-03-18 10:13:48 -070050{
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080051 struct descriptor_node *list_node;
Sol Boucher023e8292015-03-18 10:13:48 -070052
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080053 if (node->list_len != 0)
54 return false;
Sol Boucher023e8292015-03-18 10:13:48 -070055
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080056 list_node = (struct descriptor_node *)malloc(sizeof(*list_node));
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070057 if (!list_node) {
58 ERROR("Cannot allocate CBFS flag node!\n");
59 return false;
60 }
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080061 list_node->val = node;
62 list_node->next = NULL;
63
64 if (strcmp(node->name, SECTION_NAME_PRIMARY_CBFS) == 0) {
65 descriptor_list_prepend(&cbfs_sections, list_node);
66 seen_primary_section = true;
67 } else {
68 descriptor_list_append(&cbfs_sections, list_node);
Sol Boucher023e8292015-03-18 10:13:48 -070069 }
70
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080071 return true;
Sol Boucher023e8292015-03-18 10:13:48 -070072}
73
74cbfs_section_iterator_t cbfs_sections_iterator(void)
75{
76 return cbfs_sections.head;
77}
78
79bool cbfs_sections_iterator_advance(cbfs_section_iterator_t *it)
80{
81 assert(it);
82 if (!*it)
83 return false;
84
85 *it = (*it)->next;
86 return true;
87}
88
89const struct flashmap_descriptor *cbfs_sections_iterator_deref(
90 cbfs_section_iterator_t it)
91{
92 assert(it);
93 return it->val;
94}
95
96bool cbfs_sections_primary_cbfs_accounted_for(void)
97{
98 return seen_primary_section;
99}
100
101void cbfs_sections_cleanup(void)
102{
103 for (struct descriptor_node *cur = cbfs_sections.head, *next = NULL;
104 cur; cur = next) {
105 next = cur->next;
106 free(cur);
107 }
108 cbfs_sections.head = NULL;
109 cbfs_sections.tail = NULL;
110}