Patrick Georgi | ea063cb | 2020-05-08 19:28:13 +0200 | [diff] [blame] | 1 | /* track which sections of the image will contain CBFSes */ |
Patrick Georgi | 7333a11 | 2020-05-08 20:48:04 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-only */ |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 3 | |
| 4 | #include "cbfs_sections.h" |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 5 | #include "common.h" |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 */ |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 49 | bool fmd_process_flag_cbfs(const struct flashmap_descriptor *node) |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 50 | { |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 51 | struct descriptor_node *list_node; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 52 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 53 | if (node->list_len != 0) |
| 54 | return false; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 55 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 56 | list_node = (struct descriptor_node *)malloc(sizeof(*list_node)); |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 57 | if (!list_node) { |
| 58 | ERROR("Cannot allocate CBFS flag node!\n"); |
| 59 | return false; |
| 60 | } |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 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); |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 71 | return true; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | cbfs_section_iterator_t cbfs_sections_iterator(void) |
| 75 | { |
| 76 | return cbfs_sections.head; |
| 77 | } |
| 78 | |
| 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 | |
| 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 | |
| 96 | bool cbfs_sections_primary_cbfs_accounted_for(void) |
| 97 | { |
| 98 | return seen_primary_section; |
| 99 | } |
| 100 | |
| 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 | } |