Uwe Hermann | 75f5107 | 2008-03-04 16:29:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2005-2008 coresystems GmbH |
| 5 | * (Written by Stefan Reinauer <stepan@coresystems.de> for coresystems GmbH) |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 6 | * Copyright (C) 2011-2013 Stefan Tauner |
Uwe Hermann | 75f5107 | 2008-03-04 16:29:54 +0000 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
Uwe Hermann | 75f5107 | 2008-03-04 16:29:54 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 18 | #include <errno.h> |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 19 | #include <stdio.h> |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
Carl-Daniel Hailfinger | cb6ad16 | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 22 | #include <limits.h> |
Uwe Hermann | 0846f89 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 23 | #include "flash.h" |
Carl-Daniel Hailfinger | 5b997c3 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 24 | #include "programmer.h" |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 25 | #include "layout.h" |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 26 | |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 27 | struct flashrom_layout { |
| 28 | struct romentry *head; |
| 29 | }; |
| 30 | |
| 31 | static struct flashrom_layout *global_layout; |
Stefan Tauner | c70bc8a | 2013-08-30 22:22:57 +0000 | [diff] [blame] | 32 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 33 | struct flashrom_layout *get_global_layout(void) |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 34 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 35 | if (!global_layout) |
Nico Huber | 671c0f0 | 2019-06-16 20:17:19 +0200 | [diff] [blame^] | 36 | flashrom_layout_new(&global_layout); |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 37 | return global_layout; |
Nico Huber | 3a9939b | 2016-04-27 15:56:14 +0200 | [diff] [blame] | 38 | } |
| 39 | |
Nico Huber | 8edcd15 | 2019-06-16 03:22:58 +0200 | [diff] [blame] | 40 | const struct flashrom_layout *get_default_layout(const struct flashrom_flashctx *const flashctx) |
| 41 | { |
| 42 | return flashctx->default_layout; |
| 43 | } |
| 44 | |
dhendrix | beaefe0 | 2017-09-03 18:06:53 -0700 | [diff] [blame] | 45 | const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx) |
| 46 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 47 | if (flashctx->layout) |
dhendrix | beaefe0 | 2017-09-03 18:06:53 -0700 | [diff] [blame] | 48 | return flashctx->layout; |
| 49 | else |
Nico Huber | 8edcd15 | 2019-06-16 03:22:58 +0200 | [diff] [blame] | 50 | return get_default_layout(flashctx); |
dhendrix | beaefe0 | 2017-09-03 18:06:53 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 53 | static struct romentry *mutable_layout_next( |
| 54 | const struct flashrom_layout *const layout, struct romentry *iterator) |
| 55 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 56 | return iterator ? iterator->next : layout->head; |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 59 | #ifndef __LIBPAYLOAD__ |
Mark Marshall | f20b7be | 2014-05-09 21:16:21 +0000 | [diff] [blame] | 60 | int read_romlayout(const char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 61 | { |
Nico Huber | 2b94cdb | 2019-06-15 18:19:26 +0200 | [diff] [blame] | 62 | struct flashrom_layout *const layout = get_global_layout(); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 63 | FILE *romlayout; |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 64 | char tempstr[256], tempname[256]; |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 65 | int ret = 1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 66 | |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 67 | romlayout = fopen(name, "r"); |
| 68 | |
| 69 | if (!romlayout) { |
Carl-Daniel Hailfinger | 831e8f4 | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 70 | msg_gerr("ERROR: Could not open ROM layout (%s).\n", |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 71 | name); |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 72 | return -1; |
| 73 | } |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 74 | |
| 75 | while (!feof(romlayout)) { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 76 | char *tstr1, *tstr2; |
Carl-Daniel Hailfinger | da53ada | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 77 | |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 78 | if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname)) |
Peter Stuge | 1fec0f3 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 79 | continue; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 80 | #if 0 |
| 81 | // fscanf does not like arbitrary comments like that :( later |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 82 | if (tempstr[0] == '#') { |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 83 | continue; |
| 84 | } |
| 85 | #endif |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 86 | tstr1 = strtok(tempstr, ":"); |
| 87 | tstr2 = strtok(NULL, ":"); |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 88 | if (!tstr1 || !tstr2) { |
Stefan Tauner | eb58257 | 2012-09-21 12:52:50 +0000 | [diff] [blame] | 89 | msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr); |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 90 | goto _close_ret; |
Uwe Hermann | 58783e3 | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 91 | } |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 92 | if (flashrom_layout_add_region(layout, |
| 93 | strtol(tstr1, NULL, 16), strtol(tstr2, NULL, 16), tempname)) |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 94 | goto _close_ret; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 95 | } |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 96 | ret = 0; |
Uwe Hermann | ffec5f3 | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 97 | |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 98 | _close_ret: |
| 99 | (void)fclose(romlayout); |
| 100 | return ret; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 101 | } |
Patrick Georgi | a9095a9 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 102 | #endif |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 103 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 104 | /* register an include argument (-i) for later processing */ |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 105 | int register_include_arg(struct layout_include_args **args, char *name) |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 106 | { |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 107 | struct layout_include_args *tmp; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 108 | if (name == NULL) { |
| 109 | msg_gerr("<NULL> is a bad region name.\n"); |
| 110 | return 1; |
| 111 | } |
| 112 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 113 | tmp = *args; |
| 114 | while (tmp) { |
| 115 | if (!strcmp(tmp->name, name)) { |
| 116 | msg_gerr("Duplicate region name: \"%s\".\n", name); |
| 117 | return 1; |
| 118 | } |
| 119 | tmp = tmp->next; |
| 120 | } |
| 121 | |
Angel Pons | 690a944 | 2021-06-07 12:33:53 +0200 | [diff] [blame] | 122 | tmp = malloc(sizeof(*tmp)); |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 123 | if (tmp == NULL) { |
Edward O'Callaghan | a31a572 | 2022-11-12 12:05:36 +1100 | [diff] [blame] | 124 | msg_gerr("Out of memory"); |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 125 | return 1; |
| 126 | } |
| 127 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 128 | tmp->name = name; |
| 129 | tmp->next = *args; |
| 130 | *args = tmp; |
| 131 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
Arthur Heymans | 32b9f5c | 2019-02-05 16:14:55 +0100 | [diff] [blame] | 135 | /* returns -1 if an entry is not found, 0 if found. */ |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 136 | static int find_romentry(struct flashrom_layout *const l, char *name) |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 137 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 138 | if (!l->head) |
Uwe Hermann | a7e0548 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 139 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 140 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 141 | msg_gspew("Looking for region \"%s\"... ", name); |
Arthur Heymans | 32b9f5c | 2019-02-05 16:14:55 +0100 | [diff] [blame] | 142 | if (flashrom_layout_include_region(l, name)) { |
| 143 | msg_gspew("not found.\n"); |
| 144 | return -1; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 145 | } |
Arthur Heymans | 32b9f5c | 2019-02-05 16:14:55 +0100 | [diff] [blame] | 146 | msg_gspew("found.\n"); |
| 147 | return 0; |
Ollie Lho | 184a404 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 150 | /* process -i arguments |
| 151 | * returns 0 to indicate success, >0 to indicate failure |
| 152 | */ |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 153 | int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args) |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 154 | { |
Nico Huber | 519be66 | 2018-12-23 20:03:35 +0100 | [diff] [blame] | 155 | unsigned int found = 0; |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 156 | const struct layout_include_args *tmp; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 157 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 158 | if (args == NULL) |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 159 | return 0; |
| 160 | |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 161 | /* User has specified an area, but no layout file is loaded. */ |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 162 | if (!l->head) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 163 | msg_gerr("Region requested (with -i \"%s\"), " |
| 164 | "but no layout data is available.\n", |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 165 | args->name); |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 166 | return 1; |
| 167 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 168 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 169 | tmp = args; |
| 170 | while (tmp) { |
| 171 | if (find_romentry(l, tmp->name) < 0) { |
Stefan Tauner | 23bb6d5 | 2012-04-15 14:09:16 +0000 | [diff] [blame] | 172 | msg_gerr("Invalid region specified: \"%s\".\n", |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 173 | tmp->name); |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 174 | return 1; |
| 175 | } |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 176 | tmp = tmp->next; |
| 177 | found++; |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 180 | msg_ginfo("Using region%s:", found > 1 ? "s" : ""); |
| 181 | tmp = args; |
| 182 | while (tmp) { |
| 183 | msg_ginfo(" \"%s\"%s", tmp->name, found > 1 ? "," : ""); |
| 184 | found--; |
| 185 | tmp = tmp->next; |
| 186 | } |
Louis Yung-Chieh Lo | 9bcf268 | 2011-12-25 09:12:16 +0000 | [diff] [blame] | 187 | msg_ginfo(".\n"); |
| 188 | return 0; |
| 189 | } |
| 190 | |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 191 | void layout_cleanup(struct layout_include_args **args) |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 192 | { |
Nico Huber | 2b94cdb | 2019-06-15 18:19:26 +0200 | [diff] [blame] | 193 | struct flashrom_layout *const layout = get_global_layout(); |
Arthur Heymans | b04fef9 | 2019-02-05 17:35:05 +0100 | [diff] [blame] | 194 | struct layout_include_args *tmp; |
| 195 | |
| 196 | while (*args) { |
| 197 | tmp = (*args)->next; |
| 198 | free(*args); |
| 199 | *args = tmp; |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 200 | } |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 201 | |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 202 | global_layout = NULL; |
| 203 | flashrom_layout_release(layout); |
Stefan Tauner | 949ccc8 | 2013-09-15 14:01:06 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 206 | /* Validate and - if needed - normalize layout entries. */ |
| 207 | int normalize_romentries(const struct flashctx *flash) |
| 208 | { |
Nico Huber | 2b94cdb | 2019-06-15 18:19:26 +0200 | [diff] [blame] | 209 | struct flashrom_layout *const layout = get_global_layout(); |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 210 | chipsize_t total_size = flash->chip->total_size * 1024; |
| 211 | int ret = 0; |
| 212 | |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 213 | const struct romentry *entry = NULL; |
| 214 | while ((entry = layout_next(layout, entry))) { |
| 215 | if (entry->start >= total_size || entry->end >= total_size) { |
| 216 | msg_gwarn("Warning: Address range of region \"%s\" " |
| 217 | "exceeds the current chip's address space.\n", entry->name); |
| 218 | if (entry->included) |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 219 | ret = 1; |
| 220 | } |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 221 | if (entry->start > entry->end) { |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 222 | msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 223 | entry->name); |
Stefan Tauner | 8268fdb | 2013-09-23 14:21:06 +0000 | [diff] [blame] | 224 | ret = 1; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | return ret; |
| 229 | } |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 230 | |
| 231 | const struct romentry *layout_next_included_region( |
| 232 | const struct flashrom_layout *const l, const chipoff_t where) |
| 233 | { |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 234 | const struct romentry *entry = NULL, *lowest = NULL; |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 235 | |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 236 | while ((entry = layout_next(l, entry))) { |
| 237 | if (!entry->included) |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 238 | continue; |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 239 | if (entry->end < where) |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 240 | continue; |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 241 | if (!lowest || lowest->start > entry->start) |
| 242 | lowest = entry; |
Nico Huber | 3d7b1e3 | 2018-12-22 00:53:14 +0100 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | return lowest; |
| 246 | } |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 247 | |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 248 | const struct romentry *layout_next_included( |
| 249 | const struct flashrom_layout *const layout, const struct romentry *iterator) |
| 250 | { |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 251 | while ((iterator = layout_next(layout, iterator))) { |
| 252 | if (iterator->included) |
| 253 | break; |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 254 | } |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 255 | return iterator; |
| 256 | } |
| 257 | |
| 258 | const struct romentry *layout_next( |
| 259 | const struct flashrom_layout *const layout, const struct romentry *iterator) |
| 260 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 261 | return iterator ? iterator->next : layout->head; |
Nico Huber | 5ca5523 | 2019-06-15 22:29:08 +0200 | [diff] [blame] | 262 | } |
| 263 | |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 264 | /** |
| 265 | * @addtogroup flashrom-layout |
| 266 | * @{ |
| 267 | */ |
| 268 | |
| 269 | /** |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 270 | * @brief Create a new, empty layout. |
| 271 | * |
| 272 | * @param layout Pointer to returned layout reference. |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 273 | * |
| 274 | * @return 0 on success, |
| 275 | * 1 if out of memory. |
| 276 | */ |
Nico Huber | 671c0f0 | 2019-06-16 20:17:19 +0200 | [diff] [blame^] | 277 | int flashrom_layout_new(struct flashrom_layout **const layout) |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 278 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 279 | *layout = malloc(sizeof(**layout)); |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 280 | if (!*layout) { |
| 281 | msg_gerr("Error creating layout: %s\n", strerror(errno)); |
| 282 | return 1; |
| 283 | } |
| 284 | |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 285 | const struct flashrom_layout tmp = { 0 }; |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 286 | **layout = tmp; |
| 287 | return 0; |
| 288 | } |
| 289 | |
| 290 | /** |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 291 | * @brief Add another region to an existing layout. |
| 292 | * |
| 293 | * @param layout The existing layout. |
| 294 | * @param start Start address of the region. |
| 295 | * @param end End address (inclusive) of the region. |
| 296 | * @param name Name of the region. |
| 297 | * |
| 298 | * @return 0 on success, |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 299 | * 1 if out of memory. |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 300 | */ |
| 301 | int flashrom_layout_add_region( |
| 302 | struct flashrom_layout *const layout, |
| 303 | const size_t start, const size_t end, const char *const name) |
| 304 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 305 | struct romentry *const entry = malloc(sizeof(*entry)); |
| 306 | if (!entry) |
| 307 | goto _err_ret; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 308 | |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 309 | const struct romentry tmp = { |
| 310 | .next = layout->head, |
| 311 | .start = start, |
| 312 | .end = end, |
| 313 | .included = false, |
| 314 | .name = strdup(name), |
| 315 | }; |
| 316 | *entry = tmp; |
| 317 | if (!entry->name) |
| 318 | goto _err_ret; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 319 | |
| 320 | msg_gdbg("Added layout entry %08zx - %08zx named %s\n", start, end, name); |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 321 | layout->head = entry; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 322 | return 0; |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 323 | |
| 324 | _err_ret: |
| 325 | msg_gerr("Error adding layout entry: %s\n", strerror(errno)); |
| 326 | free(entry); |
| 327 | return 1; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | /** |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 331 | * @brief Mark given region as included. |
| 332 | * |
| 333 | * @param layout The layout to alter. |
| 334 | * @param name The name of the region to include. |
| 335 | * |
| 336 | * @return 0 on success, |
| 337 | * 1 if the given name can't be found. |
| 338 | */ |
| 339 | int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name) |
| 340 | { |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 341 | struct romentry *entry = NULL; |
| 342 | while ((entry = mutable_layout_next(layout, entry))) { |
| 343 | if (!strcmp(entry->name, name)) { |
| 344 | entry->included = true; |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 345 | return 0; |
| 346 | } |
| 347 | } |
| 348 | return 1; |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * @brief Free a layout. |
| 353 | * |
| 354 | * @param layout Layout to free. |
| 355 | */ |
| 356 | void flashrom_layout_release(struct flashrom_layout *const layout) |
| 357 | { |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 358 | if (layout == global_layout) |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 359 | return; |
| 360 | |
Nico Huber | 4925861 | 2019-06-15 21:41:21 +0200 | [diff] [blame] | 361 | if (!layout) |
| 362 | return; |
| 363 | |
| 364 | while (layout->head) { |
| 365 | struct romentry *const entry = layout->head; |
| 366 | layout->head = entry->next; |
| 367 | free(entry->name); |
| 368 | free(entry); |
| 369 | } |
Nico Huber | 4f21328 | 2019-06-15 17:33:49 +0200 | [diff] [blame] | 370 | free(layout); |
| 371 | } |
| 372 | |
| 373 | /** @} */ /* end flashrom-layout */ |