uwe | f562d97 | 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) |
Edward O'Callaghan | 627ca37 | 2019-09-07 18:52:30 +1000 | [diff] [blame] | 6 | * Copyright (C) 2011-2013 Stefan Tauner |
uwe | f562d97 | 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 | f562d97 | 2008-03-04 16:29:54 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 18 | #include <ctype.h> |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 19 | #include <errno.h> |
hailfinger | a83a5fe | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 20 | #include <stdio.h> |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
hailfinger | 05857ac | 2010-11-02 03:12:51 +0000 | [diff] [blame] | 23 | #include <limits.h> |
uwe | 4529d20 | 2007-08-23 13:34:59 +0000 | [diff] [blame] | 24 | #include "flash.h" |
hailfinger | 428f685 | 2010-07-27 22:41:39 +0000 | [diff] [blame] | 25 | #include "programmer.h" |
Edward O'Callaghan | 8bc8b09 | 2020-12-04 15:31:56 +1100 | [diff] [blame] | 26 | #include "layout.h" |
Jack Rosenthal | 8fd4bc4 | 2020-05-29 14:55:41 -0600 | [diff] [blame] | 27 | |
Edward O'Callaghan | 2520fac | 2019-06-24 19:10:49 +1000 | [diff] [blame] | 28 | static struct romentry entries[MAX_ROMLAYOUT]; |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 29 | static struct flashrom_layout global_layout = { entries, 0 }; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 30 | |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 31 | struct flashrom_layout *get_global_layout(void) |
Edward O'Callaghan | 2520fac | 2019-06-24 19:10:49 +1000 | [diff] [blame] | 32 | { |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 33 | return &global_layout; |
Edward O'Callaghan | 2520fac | 2019-06-24 19:10:49 +1000 | [diff] [blame] | 34 | } |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 35 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 36 | const struct flashrom_layout *get_layout(const struct flashrom_flashctx *const flashctx) |
Edward O'Callaghan | 743904d | 2020-12-17 13:06:56 +1100 | [diff] [blame] | 37 | { |
| 38 | if (flashctx->layout && flashctx->layout->num_entries) |
| 39 | return flashctx->layout; |
| 40 | else |
| 41 | return &flashctx->fallback_layout.base; |
| 42 | } |
| 43 | |
oxygene | 5027589 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 44 | #ifndef __LIBPAYLOAD__ |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 45 | int read_romlayout(const char *name) |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 46 | { |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 47 | struct flashrom_layout *const layout = get_global_layout(); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 48 | FILE *romlayout; |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 49 | char tempstr[256], tempname[256]; |
| 50 | unsigned int i; |
| 51 | int ret = 1; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 52 | |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 53 | romlayout = fopen(name, "r"); |
| 54 | |
| 55 | if (!romlayout) { |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 56 | msg_gerr("ERROR: Could not open layout file (%s).\n", |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 57 | name); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 58 | return -1; |
| 59 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 60 | |
| 61 | while (!feof(romlayout)) { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 62 | char *tstr1, *tstr2; |
hailfinger | cc4459c | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 63 | |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 64 | if (layout->num_entries >= MAX_ROMLAYOUT) { |
hailfinger | cc4459c | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 65 | msg_gerr("Maximum number of ROM images (%i) in layout " |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 66 | "file reached.\n", MAX_ROMLAYOUT); |
| 67 | goto _close_ret; |
hailfinger | cc4459c | 2010-12-04 11:56:52 +0000 | [diff] [blame] | 68 | } |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 69 | if (2 != fscanf(romlayout, "%255s %255s\n", tempstr, tempname)) |
stuge | bbcc2f1 | 2009-01-12 21:00:35 +0000 | [diff] [blame] | 70 | continue; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 71 | #if 0 |
| 72 | // fscanf does not like arbitrary comments like that :( later |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 73 | if (tempstr[0] == '#') { |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 74 | continue; |
| 75 | } |
| 76 | #endif |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 77 | tstr1 = strtok(tempstr, ":"); |
| 78 | tstr2 = strtok(NULL, ":"); |
uwe | 3aee1dd | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 79 | if (!tstr1 || !tstr2) { |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 80 | msg_gerr("Error parsing layout file. Offending string: \"%s\"\n", tempstr); |
| 81 | goto _close_ret; |
uwe | 3aee1dd | 2008-12-22 16:42:59 +0000 | [diff] [blame] | 82 | } |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 83 | layout->entries[layout->num_entries].start = strtol(tstr1, (char **)NULL, 16); |
| 84 | layout->entries[layout->num_entries].end = strtol(tstr2, (char **)NULL, 16); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 85 | layout->entries[layout->num_entries].included = false; |
Edward O'Callaghan | 8b0f177 | 2020-12-16 20:19:08 +1100 | [diff] [blame] | 86 | layout->entries[layout->num_entries].file = NULL; |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 87 | layout->entries[layout->num_entries].name = strdup(tempname); |
| 88 | if (!layout->entries[layout->num_entries].name) { |
| 89 | msg_gerr("Error adding layout entry: %s\n", strerror(errno)); |
| 90 | goto _close_ret; |
| 91 | } |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 92 | layout->num_entries++; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 93 | } |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 94 | |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 95 | for (i = 0; i < layout->num_entries; i++) { |
hailfinger | a83a5fe | 2010-05-30 22:24:40 +0000 | [diff] [blame] | 96 | msg_gdbg("romlayout %08x - %08x named %s\n", |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 97 | layout->entries[i].start, |
| 98 | layout->entries[i].end, layout->entries[i].name); |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 101 | ret = 0; |
uwe | be4477b | 2007-08-23 16:08:21 +0000 | [diff] [blame] | 102 | |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 103 | _close_ret: |
| 104 | (void)fclose(romlayout); |
| 105 | return ret; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 106 | } |
oxygene | 5027589 | 2010-09-30 17:03:32 +0000 | [diff] [blame] | 107 | #endif |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 108 | |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 109 | /* register an include argument (-i) for later processing */ |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 110 | int register_include_arg(struct layout_include_args **args, const char *arg) |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 111 | { |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 112 | struct layout_include_args *tmp; |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 113 | char *colon; |
| 114 | char *name; |
| 115 | char *file; |
| 116 | |
| 117 | if (arg == NULL) { |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 118 | msg_gerr("<NULL> is a bad region name.\n"); |
| 119 | return 1; |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 122 | /* -i <image>[:<file>] */ |
| 123 | colon = strchr(arg, ':'); |
| 124 | if (colon && !colon[1]) { |
| 125 | msg_gerr("Missing filename parameter in %s\n", arg); |
| 126 | return 1; |
| 127 | } |
| 128 | name = colon ? strndup(arg, colon - arg) : strdup(arg); |
| 129 | file = colon ? strdup(colon + 1) : NULL; |
| 130 | |
| 131 | for (tmp = *args; tmp; tmp = tmp->next) { |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 132 | if (!strcmp(tmp->name, name)) { |
| 133 | msg_gerr("Duplicate region name: \"%s\".\n", name); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 134 | goto error; |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 135 | } |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | tmp = malloc(sizeof(struct layout_include_args)); |
| 139 | if (tmp == NULL) { |
| 140 | msg_gerr("Could not allocate memory"); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 141 | goto error; |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 142 | } |
| 143 | |
Jack Rosenthal | 14f24e5 | 2021-04-16 23:22:04 +0000 | [diff] [blame] | 144 | tmp->name = name; |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 145 | tmp->file = file; |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 146 | tmp->next = *args; |
| 147 | *args = tmp; |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 148 | return 0; |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 149 | |
| 150 | error: |
| 151 | free(name); |
| 152 | free(file); |
| 153 | return 1; |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 156 | /* returns 0 to indicate success, 1 to indicate failure */ |
| 157 | static int include_region(struct flashrom_layout *const l, const char *name, |
| 158 | const char *file) |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 159 | { |
| 160 | size_t i; |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 161 | for (i = 0; i < l->num_entries; ++i) { |
| 162 | if (!strcmp(l->entries[i].name, name)) { |
| 163 | l->entries[i].included = true; |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 164 | if (file) |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 165 | l->entries[i].file = strdup(file); |
| 166 | return 0; |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 167 | } |
| 168 | } |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 169 | return 1; |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 170 | } |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 171 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 172 | /* returns -1 if an entry is not found, 0 if found. */ |
| 173 | static int find_romentry(struct flashrom_layout *const l, char *name, char *file) |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 174 | { |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 175 | if (l->num_entries == 0) |
uwe | f664164 | 2007-05-09 10:17:44 +0000 | [diff] [blame] | 176 | return -1; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 177 | |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 178 | msg_gspew("Looking for region \"%s\"... ", name); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 179 | if (include_region(l, name, file)) { |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 180 | msg_gspew("not found.\n"); |
| 181 | return -1; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 182 | } |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 183 | msg_gspew("found.\n"); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 184 | return 0; |
ollie | 6a60099 | 2005-11-26 21:55:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 187 | int get_region_range(struct flashrom_layout *const l, const char *name, |
| 188 | unsigned int *start, unsigned int *len) |
David Hendricks | c36685a | 2016-09-12 20:41:05 -0700 | [diff] [blame] | 189 | { |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 190 | size_t i; |
| 191 | for (i = 0; i < l->num_entries; ++i) { |
| 192 | if (!strcmp(l->entries[i].name, name)) { |
| 193 | *start = l->entries[i].start; |
| 194 | *len = l->entries[i].end - l->entries[i].start + 1; |
| 195 | return 0; |
| 196 | } |
| 197 | } |
| 198 | return 1; |
David Hendricks | c36685a | 2016-09-12 20:41:05 -0700 | [diff] [blame] | 199 | } |
| 200 | |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 201 | /* process -i arguments |
| 202 | * returns 0 to indicate success, >0 to indicate failure |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 203 | */ |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 204 | int process_include_args(struct flashrom_layout *l, const struct layout_include_args *const args) |
| 205 | { |
| 206 | unsigned int found = 0; |
| 207 | const struct layout_include_args *tmp; |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 208 | |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 209 | if (args == NULL) |
| 210 | return 0; |
Louis Yung-Chieh Lo | 8fc0740 | 2011-06-15 15:02:45 +0800 | [diff] [blame] | 211 | |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 212 | /* User has specified an include argument, but no layout is loaded. */ |
| 213 | if (l->num_entries == 0) { |
| 214 | msg_gerr("Region requested (with -i \"%s\"), " |
| 215 | "but no layout data is available.\n", |
| 216 | args->name); |
| 217 | return 1; |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 218 | } |
| 219 | |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 220 | tmp = args; |
| 221 | while (tmp) { |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 222 | if (find_romentry(l, tmp->name, tmp->file) < 0) { |
| 223 | msg_gerr("Invalid region specified: \"%s\".\n", |
| 224 | tmp->name); |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | tmp = tmp->next; |
| 228 | found++; |
| 229 | } |
| 230 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 231 | msg_ginfo("Using region%s: ", found > 1 ? "s" : ""); |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 232 | tmp = args; |
| 233 | while (tmp) { |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 234 | msg_ginfo("\"%s\"", tmp->name); |
| 235 | if (tmp->file) |
| 236 | msg_ginfo(":\"%s\"", tmp->file); |
| 237 | if (found > 1) |
| 238 | msg_ginfo(", "); |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 239 | found--; |
| 240 | tmp = tmp->next; |
| 241 | } |
| 242 | msg_ginfo(".\n"); |
David Hendricks | d0ea9ed | 2011-03-04 17:31:57 -0800 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 246 | /* returns boolean 1 if any regions overlap, 0 otherwise */ |
| 247 | int included_regions_overlap(const struct flashrom_layout *const l) |
| 248 | { |
| 249 | size_t i; |
| 250 | int overlap_detected = 0; |
| 251 | |
| 252 | for (i = 0; i < l->num_entries; i++) { |
| 253 | size_t j; |
| 254 | |
| 255 | if (!l->entries[i].included) |
| 256 | continue; |
| 257 | |
| 258 | for (j = i + 1; j < l->num_entries; j++) { |
| 259 | if (!l->entries[j].included) |
| 260 | continue; |
| 261 | |
| 262 | if (l->entries[i].start > l->entries[j].end) |
| 263 | continue; |
| 264 | |
| 265 | if (l->entries[i].end < l->entries[j].start) |
| 266 | continue; |
| 267 | |
| 268 | msg_gdbg("Regions %s [0x%08x-0x%08x] and " |
| 269 | "%s [0x%08x-0x%08x] overlap\n", |
| 270 | l->entries[i].name, l->entries[i].start, |
| 271 | l->entries[i].end, l->entries[j].name, |
| 272 | l->entries[j].start, l->entries[j].end); |
| 273 | overlap_detected = 1; |
| 274 | } |
| 275 | } |
| 276 | return overlap_detected; |
| 277 | } |
| 278 | |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 279 | void layout_cleanup(struct layout_include_args **args) |
Edward O'Callaghan | 627ca37 | 2019-09-07 18:52:30 +1000 | [diff] [blame] | 280 | { |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 281 | struct flashrom_layout *const layout = get_global_layout(); |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 282 | unsigned int i; |
| 283 | struct layout_include_args *tmp; |
| 284 | |
| 285 | while (*args) { |
| 286 | tmp = (*args)->next; |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 287 | free((*args)->name); |
| 288 | free((*args)->file); |
Edward O'Callaghan | 10bb9ae | 2020-12-17 13:06:10 +1100 | [diff] [blame] | 289 | free(*args); |
| 290 | *args = tmp; |
| 291 | } |
| 292 | |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 293 | for (i = 0; i < layout->num_entries; i++) { |
Edward O'Callaghan | 9494cba | 2020-07-24 22:35:22 +1000 | [diff] [blame] | 294 | free(layout->entries[i].name); |
Edward O'Callaghan | 8b0f177 | 2020-12-16 20:19:08 +1100 | [diff] [blame] | 295 | free(layout->entries[i].file); |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 296 | layout->entries[i].included = false; |
Edward O'Callaghan | 627ca37 | 2019-09-07 18:52:30 +1000 | [diff] [blame] | 297 | } |
Edward O'Callaghan | ffb17a3 | 2019-09-07 19:00:33 +1000 | [diff] [blame] | 298 | layout->num_entries = 0; |
Edward O'Callaghan | 627ca37 | 2019-09-07 18:52:30 +1000 | [diff] [blame] | 299 | } |
| 300 | |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 301 | /* Validate and - if needed - normalize layout entries. */ |
| 302 | int normalize_romentries(const struct flashctx *flash) |
| 303 | { |
Edward O'Callaghan | a6a6f7b | 2020-07-30 22:57:58 +1000 | [diff] [blame] | 304 | struct flashrom_layout *const layout = get_global_layout(); |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 305 | chipsize_t total_size = flash->chip->total_size * 1024; |
| 306 | int ret = 0; |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 307 | |
Edward O'Callaghan | a6a6f7b | 2020-07-30 22:57:58 +1000 | [diff] [blame] | 308 | unsigned int i; |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 309 | for (i = 0; i < layout->num_entries; i++) { |
Edward O'Callaghan | a6a6f7b | 2020-07-30 22:57:58 +1000 | [diff] [blame] | 310 | if (layout->entries[i].start >= total_size || layout->entries[i].end >= total_size) { |
| 311 | msg_gwarn("Warning: Address range of region \"%s\" exceeds the current chip's " |
| 312 | "address space.\n", layout->entries[i].name); |
| 313 | if (layout->entries[i].included) |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 314 | ret = 1; |
| 315 | } |
Edward O'Callaghan | a6a6f7b | 2020-07-30 22:57:58 +1000 | [diff] [blame] | 316 | if (layout->entries[i].start > layout->entries[i].end) { |
| 317 | msg_gerr("Error: Size of the address range of region \"%s\" is not positive.\n", |
| 318 | layout->entries[i].name); |
Edward O'Callaghan | a2f3e2a | 2020-07-26 16:49:30 +1000 | [diff] [blame] | 319 | ret = 1; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | return ret; |
| 324 | } |
| 325 | |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 326 | void prepare_layout_for_extraction(struct flashctx *flash) |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 327 | { |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 328 | const struct flashrom_layout *const l = get_layout(flash); |
| 329 | unsigned int i, j; |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 330 | |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 331 | for (i = 0; i < l->num_entries; ++i) { |
| 332 | l->entries[i].included = true; |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 333 | |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 334 | if (!l->entries[i].file) |
| 335 | l->entries[i].file = strdup(l->entries[i].name); |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 336 | |
Daniel Campello | 83752f8 | 2021-04-16 14:54:27 -0600 | [diff] [blame] | 337 | for (j = 0; l->entries[i].file[j]; j++) { |
| 338 | if (isspace(l->entries[i].file[j])) |
| 339 | l->entries[i].file[j] = '_'; |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 340 | } |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 341 | } |
Simon Glass | 9ad06c1 | 2013-07-03 22:08:17 +0900 | [diff] [blame] | 342 | } |
Vadim Bendebury | cc9577d | 2018-05-09 11:52:00 -0700 | [diff] [blame] | 343 | |
Edward O'Callaghan | 4a28887 | 2020-12-18 10:26:04 +1100 | [diff] [blame] | 344 | const struct romentry *layout_next_included_region( |
| 345 | const struct flashrom_layout *const l, const chipoff_t where) |
| 346 | { |
| 347 | unsigned int i; |
| 348 | const struct romentry *lowest = NULL; |
| 349 | |
| 350 | for (i = 0; i < l->num_entries; ++i) { |
| 351 | if (!l->entries[i].included) |
| 352 | continue; |
| 353 | if (l->entries[i].end < where) |
| 354 | continue; |
| 355 | if (!lowest || lowest->start > l->entries[i].start) |
| 356 | lowest = &l->entries[i]; |
| 357 | } |
| 358 | |
| 359 | return lowest; |
| 360 | } |
| 361 | |
| 362 | const struct romentry *layout_next_included( |
| 363 | const struct flashrom_layout *const layout, const struct romentry *iterator) |
| 364 | { |
| 365 | const struct romentry *const end = layout->entries + layout->num_entries; |
| 366 | |
| 367 | if (iterator) |
| 368 | ++iterator; |
| 369 | else |
| 370 | iterator = &layout->entries[0]; |
| 371 | |
| 372 | for (; iterator < end; ++iterator) { |
| 373 | if (!iterator->included) |
| 374 | continue; |
| 375 | return iterator; |
| 376 | } |
| 377 | return NULL; |
| 378 | } |
| 379 | |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 380 | /** |
| 381 | * @addtogroup flashrom-layout |
| 382 | * @{ |
| 383 | */ |
| 384 | |
| 385 | /** |
| 386 | * @brief Mark given region as included. |
| 387 | * |
| 388 | * @param layout The layout to alter. |
| 389 | * @param name The name of the region to include. |
| 390 | * |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 391 | * @return 0 on success, |
| 392 | * 1 if the given name can't be found. |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 393 | */ |
Daniel Campello | 30efd0e | 2021-03-19 13:23:42 -0600 | [diff] [blame] | 394 | int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name) |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 395 | { |
Daniel Campello | 2fdc837 | 2021-04-16 17:52:51 -0600 | [diff] [blame] | 396 | return include_region(layout, name, NULL); |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 397 | } |
| 398 | |
Edward O'Callaghan | 5d0a4b1 | 2020-12-19 11:46:50 +1100 | [diff] [blame] | 399 | /** |
| 400 | * @brief Free a layout. |
| 401 | * |
| 402 | * @param layout Layout to free. |
| 403 | */ |
| 404 | void flashrom_layout_release(struct flashrom_layout *const layout) |
| 405 | { |
| 406 | unsigned int i; |
| 407 | |
| 408 | if (!layout || layout == get_global_layout()) |
| 409 | return; |
| 410 | |
| 411 | for (i = 0; i < layout->num_entries; ++i) { |
| 412 | free(layout->entries[i].name); |
| 413 | free(layout->entries[i].file); |
| 414 | } |
| 415 | free(layout); |
| 416 | } |
| 417 | |
Edward O'Callaghan | 13e8ed8 | 2020-06-01 13:22:19 +1000 | [diff] [blame] | 418 | /** @} */ /* end flashrom-layout */ |