Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 1 | /* |
| 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 Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 14 | */ |
| 15 | |
| 16 | #include "cbfs_sections.h" |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 17 | #include "common.h" |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 18 | |
| 19 | #include <assert.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | struct descriptor_node { |
| 24 | const struct flashmap_descriptor *val; |
| 25 | struct descriptor_node *next; |
| 26 | }; |
| 27 | |
| 28 | static struct descriptor_list { |
| 29 | struct descriptor_node *head; |
| 30 | struct descriptor_node *tail; |
| 31 | } cbfs_sections; |
| 32 | |
| 33 | static bool seen_primary_section = false; |
| 34 | |
| 35 | static 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 | |
| 47 | static 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 Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 61 | bool fmd_process_flag_cbfs(const struct flashmap_descriptor *node) |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 62 | { |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 63 | struct descriptor_node *list_node; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 64 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 65 | if (node->list_len != 0) |
| 66 | return false; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 67 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 68 | list_node = (struct descriptor_node *)malloc(sizeof(*list_node)); |
Thejaswani Putta | 6f5225c | 2019-04-11 18:36:08 -0700 | [diff] [blame] | 69 | if (!list_node) { |
| 70 | ERROR("Cannot allocate CBFS flag node!\n"); |
| 71 | return false; |
| 72 | } |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 73 | 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 Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Hung-Te Lin | 9497fcb | 2019-03-04 14:28:37 +0800 | [diff] [blame] | 83 | return true; |
Sol Boucher | 023e829 | 2015-03-18 10:13:48 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | cbfs_section_iterator_t cbfs_sections_iterator(void) |
| 87 | { |
| 88 | return cbfs_sections.head; |
| 89 | } |
| 90 | |
| 91 | bool 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 | |
| 101 | const struct flashmap_descriptor *cbfs_sections_iterator_deref( |
| 102 | cbfs_section_iterator_t it) |
| 103 | { |
| 104 | assert(it); |
| 105 | return it->val; |
| 106 | } |
| 107 | |
| 108 | bool cbfs_sections_primary_cbfs_accounted_for(void) |
| 109 | { |
| 110 | return seen_primary_section; |
| 111 | } |
| 112 | |
| 113 | void 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 | } |