blob: 088534adc9732283e59e4752ae59bbb04f2ecb65 [file] [log] [blame]
Sol Boucher023e8292015-03-18 10:13:48 -07001/*
2 * fmap_sections.c, track which sections of the image will contain CBFSes
3 *
4 * Copyright (C) 2015 Google, Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Sol Boucher023e8292015-03-18 10:13:48 -070014 */
15
16#include "cbfs_sections.h"
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070017#include "common.h"
Sol Boucher023e8292015-03-18 10:13:48 -070018
19#include <assert.h>
20#include <stdlib.h>
21#include <string.h>
22
23struct descriptor_node {
24 const struct flashmap_descriptor *val;
25 struct descriptor_node *next;
26};
27
28static struct descriptor_list {
29 struct descriptor_node *head;
30 struct descriptor_node *tail;
31} cbfs_sections;
32
33static bool seen_primary_section = false;
34
35static void descriptor_list_prepend(struct descriptor_list *list,
36 struct descriptor_node *new_head)
37{
38 assert(list);
39 assert(new_head);
40
41 new_head->next = list->head;
42 list->head = new_head;
43 if (!list->tail)
44 list->tail = new_head;
45}
46
47static void descriptor_list_append(struct descriptor_list *list,
48 struct descriptor_node *new_tail)
49{
50 assert(list);
51 assert(new_tail);
52
53 if (list->tail)
54 list->tail->next = new_tail;
55 list->tail = new_tail;
56 if (!list->head)
57 list->head = new_tail;
58}
59
60/* Implementation of cbfs module's callback; invoked during fmd file parsing */
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080061bool fmd_process_flag_cbfs(const struct flashmap_descriptor *node)
Sol Boucher023e8292015-03-18 10:13:48 -070062{
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080063 struct descriptor_node *list_node;
Sol Boucher023e8292015-03-18 10:13:48 -070064
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080065 if (node->list_len != 0)
66 return false;
Sol Boucher023e8292015-03-18 10:13:48 -070067
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080068 list_node = (struct descriptor_node *)malloc(sizeof(*list_node));
Thejaswani Putta6f5225c2019-04-11 18:36:08 -070069 if (!list_node) {
70 ERROR("Cannot allocate CBFS flag node!\n");
71 return false;
72 }
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080073 list_node->val = node;
74 list_node->next = NULL;
75
76 if (strcmp(node->name, SECTION_NAME_PRIMARY_CBFS) == 0) {
77 descriptor_list_prepend(&cbfs_sections, list_node);
78 seen_primary_section = true;
79 } else {
80 descriptor_list_append(&cbfs_sections, list_node);
Sol Boucher023e8292015-03-18 10:13:48 -070081 }
82
Hung-Te Lin9497fcb2019-03-04 14:28:37 +080083 return true;
Sol Boucher023e8292015-03-18 10:13:48 -070084}
85
86cbfs_section_iterator_t cbfs_sections_iterator(void)
87{
88 return cbfs_sections.head;
89}
90
91bool cbfs_sections_iterator_advance(cbfs_section_iterator_t *it)
92{
93 assert(it);
94 if (!*it)
95 return false;
96
97 *it = (*it)->next;
98 return true;
99}
100
101const struct flashmap_descriptor *cbfs_sections_iterator_deref(
102 cbfs_section_iterator_t it)
103{
104 assert(it);
105 return it->val;
106}
107
108bool cbfs_sections_primary_cbfs_accounted_for(void)
109{
110 return seen_primary_section;
111}
112
113void cbfs_sections_cleanup(void)
114{
115 for (struct descriptor_node *cur = cbfs_sections.head, *next = NULL;
116 cur; cur = next) {
117 next = cur->next;
118 free(cur);
119 }
120 cbfs_sections.head = NULL;
121 cbfs_sections.tail = NULL;
122}