xref: /aosp_15_r20/external/coreboot/util/cbfstool/cbfs_sections.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* track which sections of the image will contain CBFSes */
2 /* SPDX-License-Identifier: GPL-2.0-only */
3 
4 #include "cbfs_sections.h"
5 #include "common.h"
6 
7 #include <assert.h>
8 #include <stdlib.h>
9 #include <string.h>
10 
11 struct descriptor_node {
12 	const struct flashmap_descriptor *val;
13 	struct descriptor_node *next;
14 };
15 
16 static struct descriptor_list {
17 	struct descriptor_node *head;
18 	struct descriptor_node *tail;
19 } cbfs_sections;
20 
21 static bool seen_primary_section = false;
22 
descriptor_list_prepend(struct descriptor_list * list,struct descriptor_node * new_head)23 static 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 
descriptor_list_append(struct descriptor_list * list,struct descriptor_node * new_tail)35 static 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 */
fmd_process_flag_cbfs(const struct flashmap_descriptor * node)49 bool fmd_process_flag_cbfs(const struct flashmap_descriptor *node)
50 {
51 	struct descriptor_node *list_node;
52 
53 	if (node->list_len != 0)
54 		return false;
55 
56 	list_node = (struct descriptor_node *)malloc(sizeof(*list_node));
57 	if (!list_node) {
58 		ERROR("Cannot allocate CBFS flag node!\n");
59 		return false;
60 	}
61 	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);
69 	}
70 
71 	return true;
72 }
73 
cbfs_sections_iterator(void)74 cbfs_section_iterator_t cbfs_sections_iterator(void)
75 {
76 	return cbfs_sections.head;
77 }
78 
cbfs_sections_iterator_advance(cbfs_section_iterator_t * it)79 bool 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 
cbfs_sections_iterator_deref(cbfs_section_iterator_t it)89 const struct flashmap_descriptor *cbfs_sections_iterator_deref(
90 						cbfs_section_iterator_t it)
91 {
92 	assert(it);
93 	return it->val;
94 }
95 
cbfs_sections_primary_cbfs_accounted_for(void)96 bool cbfs_sections_primary_cbfs_accounted_for(void)
97 {
98 	return seen_primary_section;
99 }
100 
cbfs_sections_cleanup(void)101 void 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 }
111