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" |
| 17 | |
| 18 | #include <assert.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | |
| 22 | struct descriptor_node { |
| 23 | const struct flashmap_descriptor *val; |
| 24 | struct descriptor_node *next; |
| 25 | }; |
| 26 | |
| 27 | static struct descriptor_list { |
| 28 | struct descriptor_node *head; |
| 29 | struct descriptor_node *tail; |
| 30 | } cbfs_sections; |
| 31 | |
| 32 | static bool seen_primary_section = false; |
| 33 | |
| 34 | static void descriptor_list_prepend(struct descriptor_list *list, |
| 35 | struct descriptor_node *new_head) |
| 36 | { |
| 37 | assert(list); |
| 38 | assert(new_head); |
| 39 | |
| 40 | new_head->next = list->head; |
| 41 | list->head = new_head; |
| 42 | if (!list->tail) |
| 43 | list->tail = new_head; |
| 44 | } |
| 45 | |
| 46 | static void descriptor_list_append(struct descriptor_list *list, |
| 47 | struct descriptor_node *new_tail) |
| 48 | { |
| 49 | assert(list); |
| 50 | assert(new_tail); |
| 51 | |
| 52 | if (list->tail) |
| 53 | list->tail->next = new_tail; |
| 54 | list->tail = new_tail; |
| 55 | if (!list->head) |
| 56 | list->head = new_tail; |
| 57 | } |
| 58 | |
| 59 | /* Implementation of cbfs module's callback; invoked during fmd file parsing */ |
| 60 | bool fmd_process_annotation_impl(const struct flashmap_descriptor *node, |
| 61 | const char *annotation) |
| 62 | { |
| 63 | if (strcmp(annotation, SECTION_ANNOTATION_CBFS) == 0 && |
| 64 | node->list_len == 0) { |
| 65 | struct descriptor_node *list_node = malloc(sizeof(*list_node)); |
| 66 | list_node->val = node; |
| 67 | list_node->next = NULL; |
| 68 | |
| 69 | if (strcmp(node->name, SECTION_NAME_PRIMARY_CBFS) == 0) { |
| 70 | descriptor_list_prepend(&cbfs_sections, list_node); |
| 71 | seen_primary_section = true; |
| 72 | } else { |
| 73 | descriptor_list_append(&cbfs_sections, list_node); |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | cbfs_section_iterator_t cbfs_sections_iterator(void) |
| 83 | { |
| 84 | return cbfs_sections.head; |
| 85 | } |
| 86 | |
| 87 | bool cbfs_sections_iterator_advance(cbfs_section_iterator_t *it) |
| 88 | { |
| 89 | assert(it); |
| 90 | if (!*it) |
| 91 | return false; |
| 92 | |
| 93 | *it = (*it)->next; |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | const struct flashmap_descriptor *cbfs_sections_iterator_deref( |
| 98 | cbfs_section_iterator_t it) |
| 99 | { |
| 100 | assert(it); |
| 101 | return it->val; |
| 102 | } |
| 103 | |
| 104 | bool cbfs_sections_primary_cbfs_accounted_for(void) |
| 105 | { |
| 106 | return seen_primary_section; |
| 107 | } |
| 108 | |
| 109 | void cbfs_sections_cleanup(void) |
| 110 | { |
| 111 | for (struct descriptor_node *cur = cbfs_sections.head, *next = NULL; |
| 112 | cur; cur = next) { |
| 113 | next = cur->next; |
| 114 | free(cur); |
| 115 | } |
| 116 | cbfs_sections.head = NULL; |
| 117 | cbfs_sections.tail = NULL; |
| 118 | } |