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