Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2012, 2016 secunet Security Networks AG |
| 5 | * (Written by Nico Huber <nico.huber@secunet.com> for secunet) |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 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. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 16 | */ |
| 17 | /** |
| 18 | * @mainpage |
| 19 | * |
| 20 | * Have a look at the Modules section for a function reference. |
| 21 | */ |
| 22 | |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 23 | #include <errno.h> |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdarg.h> |
| 27 | |
| 28 | #include "flash.h" |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 29 | #include "fmap.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 30 | #include "programmer.h" |
| 31 | #include "layout.h" |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 32 | #include "ich_descriptors.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 33 | #include "libflashrom.h" |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 34 | #include "writeprotect.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
| 37 | * @defgroup flashrom-general General |
| 38 | * @{ |
| 39 | */ |
| 40 | |
| 41 | /** Pointer to log callback function. */ |
| 42 | static flashrom_log_callback *global_log_callback = NULL; |
| 43 | |
| 44 | /** |
| 45 | * @brief Initialize libflashrom. |
| 46 | * |
| 47 | * @param perform_selfcheck If not zero, perform a self check. |
| 48 | * @return 0 on success |
| 49 | */ |
| 50 | int flashrom_init(const int perform_selfcheck) |
| 51 | { |
| 52 | if (perform_selfcheck && selfcheck()) |
| 53 | return 1; |
| 54 | myusec_calibrate_delay(); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @brief Shut down libflashrom. |
| 60 | * @return 0 on success |
| 61 | */ |
| 62 | int flashrom_shutdown(void) |
| 63 | { |
| 64 | return 0; /* TODO: nothing to do? */ |
| 65 | } |
| 66 | |
| 67 | /* TODO: flashrom_set_loglevel()? do we need it? |
Angel Pons | 0be623c | 2021-04-17 17:08:44 +0200 | [diff] [blame] | 68 | For now, let the user decide in their callback. */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief Set the log callback function. |
| 72 | * |
| 73 | * Set a callback function which will be invoked whenever libflashrom wants |
| 74 | * to output messages. This allows frontends to do whatever they see fit with |
| 75 | * such messages, e.g. write them to syslog, or to file, or print them in a |
| 76 | * GUI window, etc. |
| 77 | * |
| 78 | * @param log_callback Pointer to the new log callback function. |
| 79 | */ |
| 80 | void flashrom_set_log_callback(flashrom_log_callback *const log_callback) |
| 81 | { |
| 82 | global_log_callback = log_callback; |
| 83 | } |
| 84 | /** @private */ |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 85 | int print(const enum flashrom_log_level level, const char *const fmt, ...) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 86 | { |
| 87 | if (global_log_callback) { |
| 88 | int ret; |
| 89 | va_list args; |
| 90 | va_start(args, fmt); |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 91 | ret = global_log_callback(level, fmt, args); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 92 | va_end(args); |
| 93 | return ret; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | /** @} */ /* end flashrom-general */ |
| 99 | |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * @defgroup flashrom-query Querying |
| 104 | * @{ |
| 105 | */ |
| 106 | |
Nico Huber | abfb70c | 2022-12-22 12:09:36 +0000 | [diff] [blame] | 107 | /* TBD */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 108 | |
| 109 | /** @} */ /* end flashrom-query */ |
| 110 | |
| 111 | |
| 112 | |
| 113 | /** |
| 114 | * @defgroup flashrom-prog Programmers |
| 115 | * @{ |
| 116 | */ |
| 117 | |
| 118 | /** |
| 119 | * @brief Initialize the specified programmer. |
| 120 | * |
| 121 | * Currently, only one programmer may be initialized at a time. |
| 122 | * |
| 123 | * @param[out] flashprog Points to a pointer of type struct flashrom_programmer |
| 124 | * that will be set if programmer initialization succeeds. |
| 125 | * *flashprog has to be shutdown by the caller with @ref |
| 126 | * flashrom_programmer_shutdown. |
| 127 | * @param[in] prog_name Name of the programmer to initialize. |
| 128 | * @param[in] prog_param Pointer to programmer specific parameters. |
| 129 | * @return 0 on success |
| 130 | */ |
| 131 | int flashrom_programmer_init(struct flashrom_programmer **const flashprog, |
| 132 | const char *const prog_name, const char *const prog_param) |
| 133 | { |
| 134 | unsigned prog; |
| 135 | |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 136 | for (prog = 0; prog < programmer_table_size; prog++) { |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 137 | if (strcmp(prog_name, programmer_table[prog]->name) == 0) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 140 | if (prog >= programmer_table_size) { |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 141 | msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name); |
| 142 | list_programmers_linebreak(0, 80, 0); |
Nico Huber | dafd51e | 2023-02-10 23:58:19 +0100 | [diff] [blame^] | 143 | msg_ginfo(".\n"); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 144 | return 1; |
| 145 | } |
Thomas Heijligen | e0e93cf | 2021-06-01 14:37:12 +0200 | [diff] [blame] | 146 | return programmer_init(programmer_table[prog], prog_param); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @brief Shut down the initialized programmer. |
| 151 | * |
| 152 | * @param flashprog The programmer to shut down. |
| 153 | * @return 0 on success |
| 154 | */ |
| 155 | int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog) |
| 156 | { |
| 157 | return programmer_shutdown(); |
| 158 | } |
| 159 | |
| 160 | /* TODO: flashrom_programmer_capabilities()? */ |
| 161 | |
| 162 | /** @} */ /* end flashrom-prog */ |
| 163 | |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * @defgroup flashrom-flash Flash chips |
| 168 | * @{ |
| 169 | */ |
| 170 | |
| 171 | /** |
| 172 | * @brief Probe for a flash chip. |
| 173 | * |
| 174 | * Probes for a flash chip and returns a flash context, that can be used |
| 175 | * later with flash chip and @ref flashrom-ops "image operations", if |
| 176 | * exactly one matching chip is found. |
| 177 | * |
| 178 | * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx |
| 179 | * that will be set if exactly one chip is found. *flashctx |
| 180 | * has to be freed by the caller with @ref flashrom_flash_release. |
| 181 | * @param[in] flashprog The flash programmer used to access the chip. |
| 182 | * @param[in] chip_name Name of a chip to probe for, or NULL to probe for |
| 183 | * all known chips. |
| 184 | * @return 0 on success, |
| 185 | * 3 if multiple chips were found, |
| 186 | * 2 if no chip was found, |
| 187 | * or 1 on any other error. |
| 188 | */ |
| 189 | int flashrom_flash_probe(struct flashrom_flashctx **const flashctx, |
| 190 | const struct flashrom_programmer *const flashprog, |
| 191 | const char *const chip_name) |
| 192 | { |
| 193 | int i, ret = 2; |
| 194 | struct flashrom_flashctx second_flashctx = { 0, }; |
| 195 | |
| 196 | chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */ |
| 197 | |
| 198 | *flashctx = malloc(sizeof(**flashctx)); |
| 199 | if (!*flashctx) |
| 200 | return 1; |
| 201 | memset(*flashctx, 0, sizeof(**flashctx)); |
| 202 | |
| 203 | for (i = 0; i < registered_master_count; ++i) { |
| 204 | int flash_idx = -1; |
| 205 | if (!ret || (flash_idx = probe_flash(®istered_masters[i], 0, *flashctx, 0)) != -1) { |
| 206 | ret = 0; |
| 207 | /* We found one chip, now check that there is no second match. */ |
| 208 | if (probe_flash(®istered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) { |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 209 | flashrom_layout_release(second_flashctx.default_layout); |
Nico Huber | 38450ce | 2019-06-16 20:07:28 +0200 | [diff] [blame] | 210 | free(second_flashctx.chip); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 211 | ret = 3; |
| 212 | break; |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | if (ret) { |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 217 | flashrom_flash_release(*flashctx); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 218 | *flashctx = NULL; |
| 219 | } |
| 220 | return ret; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * @brief Returns the size of the specified flash chip in bytes. |
| 225 | * |
| 226 | * @param flashctx The queried flash context. |
| 227 | * @return Size of flash chip in bytes. |
| 228 | */ |
| 229 | size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx) |
| 230 | { |
| 231 | return flashctx->chip->total_size * 1024; |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * @brief Free a flash context. |
| 236 | * |
| 237 | * @param flashctx Flash context to free. |
| 238 | */ |
| 239 | void flashrom_flash_release(struct flashrom_flashctx *const flashctx) |
| 240 | { |
Nico Huber | 6fb2f62 | 2022-02-24 18:17:40 +0100 | [diff] [blame] | 241 | if (!flashctx) |
| 242 | return; |
| 243 | |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 244 | flashrom_layout_release(flashctx->default_layout); |
Nico Huber | 38450ce | 2019-06-16 20:07:28 +0200 | [diff] [blame] | 245 | free(flashctx->chip); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 246 | free(flashctx); |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * @brief Set a flag in the given flash context. |
| 251 | * |
| 252 | * @param flashctx Flash context to alter. |
| 253 | * @param flag Flag that is to be set / cleared. |
| 254 | * @param value Value to set. |
| 255 | */ |
| 256 | void flashrom_flag_set(struct flashrom_flashctx *const flashctx, |
| 257 | const enum flashrom_flag flag, const bool value) |
| 258 | { |
| 259 | switch (flag) { |
| 260 | case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break; |
| 261 | case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break; |
| 262 | case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break; |
| 263 | case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break; |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | /** |
| 268 | * @brief Return the current value of a flag in the given flash context. |
| 269 | * |
| 270 | * @param flashctx Flash context to read from. |
| 271 | * @param flag Flag to be read. |
| 272 | * @return Current value of the flag. |
| 273 | */ |
| 274 | bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag) |
| 275 | { |
| 276 | switch (flag) { |
| 277 | case FLASHROM_FLAG_FORCE: return flashctx->flags.force; |
| 278 | case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch; |
| 279 | case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write; |
| 280 | case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip; |
| 281 | default: return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** @} */ /* end flashrom-flash */ |
| 286 | |
| 287 | |
| 288 | |
| 289 | /** |
| 290 | * @defgroup flashrom-layout Layout handling |
| 291 | * @{ |
| 292 | */ |
| 293 | |
| 294 | /** |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 295 | * @brief Read a layout from the Intel ICH descriptor in the flash. |
| 296 | * |
| 297 | * Optionally verify that the layout matches the one in the given |
| 298 | * descriptor dump. |
| 299 | * |
| 300 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 301 | * gets set if the descriptor is read and parsed |
| 302 | * successfully. |
| 303 | * @param[in] flashctx Flash context to read the descriptor from flash. |
| 304 | * @param[in] dump The descriptor dump to compare to or NULL. |
| 305 | * @param[in] len The length of the descriptor dump. |
| 306 | * |
| 307 | * @return 0 on success, |
| 308 | * 6 if descriptor parsing isn't implemented for the host, |
| 309 | * 5 if the descriptors don't match, |
| 310 | * 4 if the descriptor dump couldn't be parsed, |
| 311 | * 3 if the descriptor on flash couldn't be parsed, |
| 312 | * 2 if the descriptor on flash couldn't be read, |
| 313 | * 1 on any other error. |
| 314 | */ |
| 315 | int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx, |
| 316 | const void *const dump, const size_t len) |
| 317 | { |
| 318 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 319 | return 6; |
| 320 | #else |
aarya | d1dacf7 | 2022-03-10 08:47:48 +0530 | [diff] [blame] | 321 | struct flashrom_layout *dump_layout = NULL, *chip_layout = NULL; |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 322 | int ret = 1; |
| 323 | |
| 324 | void *const desc = malloc(0x1000); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 325 | if (prepare_flash_access(flashctx, true, false, false, false)) |
| 326 | goto _free_ret; |
| 327 | |
| 328 | msg_cinfo("Reading ich descriptor... "); |
| 329 | if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) { |
| 330 | msg_cerr("Read operation failed!\n"); |
| 331 | msg_cinfo("FAILED.\n"); |
| 332 | ret = 2; |
| 333 | goto _finalize_ret; |
| 334 | } |
| 335 | msg_cinfo("done.\n"); |
| 336 | |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 337 | if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 338 | msg_cerr("Couldn't parse the descriptor!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 339 | ret = 3; |
| 340 | goto _finalize_ret; |
| 341 | } |
| 342 | |
| 343 | if (dump) { |
| 344 | if (layout_from_ich_descriptors(&dump_layout, dump, len)) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 345 | msg_cerr("Couldn't parse the descriptor!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 346 | ret = 4; |
| 347 | goto _finalize_ret; |
| 348 | } |
| 349 | |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 350 | const struct romentry *chip_entry = layout_next(chip_layout, NULL); |
| 351 | const struct romentry *dump_entry = layout_next(dump_layout, NULL); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 352 | while (chip_entry && dump_entry && !memcmp(chip_entry, dump_entry, sizeof(*chip_entry))) { |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 353 | chip_entry = layout_next(chip_layout, chip_entry); |
| 354 | dump_entry = layout_next(dump_layout, dump_entry); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 355 | } |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 356 | flashrom_layout_release(dump_layout); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 357 | if (chip_entry || dump_entry) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 358 | msg_cerr("Descriptors don't match!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 359 | ret = 5; |
| 360 | goto _finalize_ret; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | *layout = (struct flashrom_layout *)chip_layout; |
| 365 | ret = 0; |
| 366 | |
| 367 | _finalize_ret: |
| 368 | finalize_flash_access(flashctx); |
| 369 | _free_ret: |
| 370 | if (ret) |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 371 | flashrom_layout_release(chip_layout); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 372 | free(desc); |
| 373 | return ret; |
| 374 | #endif |
| 375 | } |
| 376 | |
Nico Huber | ee13d0c | 2019-06-07 17:47:40 +0200 | [diff] [blame] | 377 | #ifdef __FLASHROM_LITTLE_ENDIAN__ |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 378 | static int flashrom_layout_parse_fmap(struct flashrom_layout **layout, |
| 379 | struct flashctx *const flashctx, const struct fmap *const fmap) |
| 380 | { |
| 381 | int i; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 382 | char name[FMAP_STRLEN + 1]; |
| 383 | const struct fmap_area *area; |
Nico Huber | efe96a9 | 2021-05-14 00:39:24 +0200 | [diff] [blame] | 384 | struct flashrom_layout *l; |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 385 | |
Nico Huber | efe96a9 | 2021-05-14 00:39:24 +0200 | [diff] [blame] | 386 | if (!fmap || flashrom_layout_new(&l)) |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 387 | return 1; |
| 388 | |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 389 | for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) { |
| 390 | snprintf(name, sizeof(name), "%s", area->name); |
Nico Huber | efe96a9 | 2021-05-14 00:39:24 +0200 | [diff] [blame] | 391 | if (flashrom_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) { |
| 392 | flashrom_layout_release(l); |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 393 | return 1; |
Nico Huber | efe96a9 | 2021-05-14 00:39:24 +0200 | [diff] [blame] | 394 | } |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | *layout = l; |
| 398 | return 0; |
| 399 | } |
Nico Huber | ee13d0c | 2019-06-07 17:47:40 +0200 | [diff] [blame] | 400 | #endif /* __FLASHROM_LITTLE_ENDIAN__ */ |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 401 | |
| 402 | /** |
| 403 | * @brief Read a layout by searching the flash chip for fmap. |
| 404 | * |
| 405 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 406 | * gets set if the fmap is read and parsed successfully. |
| 407 | * @param[in] flashctx Flash context |
| 408 | * @param[in] offset Offset to begin searching for fmap. |
| 409 | * @param[in] offset Length of address space to search. |
| 410 | * |
| 411 | * @return 0 on success, |
| 412 | * 3 if fmap parsing isn't implemented for the host, |
| 413 | * 2 if the fmap couldn't be read, |
| 414 | * 1 on any other error. |
| 415 | */ |
| 416 | int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout, |
Julius Werner | 8f0db7d | 2022-02-14 17:07:39 -0800 | [diff] [blame] | 417 | struct flashctx *const flashctx, size_t offset, size_t len) |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 418 | { |
| 419 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 420 | return 3; |
| 421 | #else |
| 422 | struct fmap *fmap = NULL; |
| 423 | int ret = 0; |
| 424 | |
| 425 | msg_gdbg("Attempting to read fmap from ROM content.\n"); |
| 426 | if (fmap_read_from_rom(&fmap, flashctx, offset, len)) { |
| 427 | msg_gerr("Failed to read fmap from ROM.\n"); |
| 428 | return 1; |
| 429 | } |
| 430 | |
| 431 | msg_gdbg("Adding fmap layout to global layout.\n"); |
| 432 | if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) { |
| 433 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 434 | ret = 1; |
| 435 | } |
| 436 | |
| 437 | free(fmap); |
| 438 | return ret; |
| 439 | #endif |
| 440 | } |
| 441 | |
| 442 | /** |
| 443 | * @brief Read a layout by searching buffer for fmap. |
| 444 | * |
| 445 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 446 | * gets set if the fmap is read and parsed successfully. |
| 447 | * @param[in] flashctx Flash context |
| 448 | * @param[in] buffer Buffer to search in |
| 449 | * @param[in] size Size of buffer to search |
| 450 | * |
| 451 | * @return 0 on success, |
| 452 | * 3 if fmap parsing isn't implemented for the host, |
| 453 | * 2 if the fmap couldn't be read, |
| 454 | * 1 on any other error. |
| 455 | */ |
| 456 | int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout, |
| 457 | struct flashctx *const flashctx, const uint8_t *const buf, size_t size) |
| 458 | { |
| 459 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 460 | return 3; |
| 461 | #else |
| 462 | struct fmap *fmap = NULL; |
| 463 | int ret = 1; |
| 464 | |
| 465 | if (!buf || !size) |
| 466 | goto _ret; |
| 467 | |
| 468 | msg_gdbg("Attempting to read fmap from buffer.\n"); |
| 469 | if (fmap_read_from_buffer(&fmap, buf, size)) { |
| 470 | msg_gerr("Failed to read fmap from buffer.\n"); |
| 471 | goto _ret; |
| 472 | } |
| 473 | |
| 474 | msg_gdbg("Adding fmap layout to global layout.\n"); |
| 475 | if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) { |
| 476 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 477 | goto _free_ret; |
| 478 | } |
| 479 | |
| 480 | ret = 0; |
| 481 | _free_ret: |
| 482 | free(fmap); |
| 483 | _ret: |
| 484 | return ret; |
| 485 | #endif |
| 486 | } |
| 487 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 488 | /** |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 489 | * @brief Set the active layout for a flash context. |
| 490 | * |
| 491 | * Note: This just sets a pointer. The caller must not release the layout |
| 492 | * as long as he uses it through the given flash context. |
| 493 | * |
| 494 | * @param flashctx Flash context whose layout will be set. |
| 495 | * @param layout Layout to bet set. |
| 496 | */ |
| 497 | void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout) |
| 498 | { |
| 499 | flashctx->layout = layout; |
| 500 | } |
| 501 | |
| 502 | /** @} */ /* end flashrom-layout */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 503 | |
| 504 | |
| 505 | /** |
| 506 | * @defgroup flashrom-wp |
| 507 | * @{ |
| 508 | */ |
| 509 | |
| 510 | /** |
| 511 | * @brief Create a new empty WP configuration. |
| 512 | * |
| 513 | * @param[out] cfg Points to a pointer of type struct flashrom_wp_cfg that will |
| 514 | * be set if creation succeeds. *cfg has to be freed by the |
| 515 | * caller with @ref flashrom_wp_cfg_release. |
| 516 | * @return 0 on success |
| 517 | * >0 on failure |
| 518 | */ |
| 519 | enum flashrom_wp_result flashrom_wp_cfg_new(struct flashrom_wp_cfg **cfg) |
| 520 | { |
| 521 | *cfg = calloc(1, sizeof(**cfg)); |
| 522 | return *cfg ? 0 : FLASHROM_WP_ERR_OTHER; |
| 523 | } |
| 524 | |
| 525 | /** |
| 526 | * @brief Free a WP configuration. |
| 527 | * |
| 528 | * @param[out] cfg Pointer to the flashrom_wp_cfg to free. |
| 529 | */ |
| 530 | void flashrom_wp_cfg_release(struct flashrom_wp_cfg *cfg) |
| 531 | { |
| 532 | free(cfg); |
| 533 | } |
| 534 | |
| 535 | /** |
| 536 | * @brief Set the protection mode for a WP configuration. |
| 537 | * |
| 538 | * @param[in] mode The protection mode to set. |
| 539 | * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify. |
| 540 | */ |
| 541 | void flashrom_wp_set_mode(struct flashrom_wp_cfg *cfg, enum flashrom_wp_mode mode) |
| 542 | { |
| 543 | cfg->mode = mode; |
| 544 | } |
| 545 | |
| 546 | /** |
| 547 | * @brief Get the protection mode from a WP configuration. |
| 548 | * |
| 549 | * @param[in] cfg The WP configuration to get the protection mode from. |
| 550 | * @return The configuration's protection mode. |
| 551 | */ |
| 552 | enum flashrom_wp_mode flashrom_wp_get_mode(const struct flashrom_wp_cfg *cfg) |
| 553 | { |
| 554 | return cfg->mode; |
| 555 | } |
| 556 | |
| 557 | /** |
| 558 | * @brief Set the protection range for a WP configuration. |
| 559 | * |
| 560 | * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify. |
| 561 | * @param[in] start The range's start address. |
| 562 | * @param[in] len The range's length. |
| 563 | */ |
| 564 | void flashrom_wp_set_range(struct flashrom_wp_cfg *cfg, size_t start, size_t len) |
| 565 | { |
| 566 | cfg->range.start = start; |
| 567 | cfg->range.len = len; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * @brief Get the protection range from a WP configuration. |
| 572 | * |
| 573 | * @param[out] start Points to a size_t to write the range start to. |
| 574 | * @param[out] len Points to a size_t to write the range length to. |
| 575 | * @param[in] cfg The WP configuration to get the range from. |
| 576 | */ |
| 577 | void flashrom_wp_get_range(size_t *start, size_t *len, const struct flashrom_wp_cfg *cfg) |
| 578 | { |
| 579 | *start = cfg->range.start; |
| 580 | *len = cfg->range.len; |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * @brief Write a WP configuration to a flash chip. |
| 585 | * |
| 586 | * @param[in] flash The flash context used to access the chip. |
| 587 | * @param[in] cfg The WP configuration to write to the chip. |
| 588 | * @return 0 on success |
| 589 | * >0 on failure |
| 590 | */ |
| 591 | enum flashrom_wp_result flashrom_wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg) |
| 592 | { |
| 593 | /* |
| 594 | * TODO: Call custom implementation if the programmer is opaque, as |
| 595 | * direct WP operations require SPI access. In particular, linux_mtd |
| 596 | * has its own WP operations we should use instead. |
| 597 | */ |
| 598 | if (flash->mst->buses_supported & BUS_SPI) |
| 599 | return wp_write_cfg(flash, cfg); |
| 600 | |
| 601 | return FLASHROM_WP_ERR_OTHER; |
| 602 | } |
| 603 | |
| 604 | /** |
| 605 | * @brief Read the current WP configuration from a flash chip. |
| 606 | * |
| 607 | * @param[out] cfg Pointer to a struct flashrom_wp_cfg to store the chip's |
| 608 | * configuration in. |
| 609 | * @param[in] flash The flash context used to access the chip. |
| 610 | * @return 0 on success |
| 611 | * >0 on failure |
| 612 | */ |
| 613 | enum flashrom_wp_result flashrom_wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash) |
| 614 | { |
| 615 | /* |
| 616 | * TODO: Call custom implementation if the programmer is opaque, as |
| 617 | * direct WP operations require SPI access. In particular, linux_mtd |
| 618 | * has its own WP operations we should use instead. |
| 619 | */ |
| 620 | if (flash->mst->buses_supported & BUS_SPI) |
| 621 | return wp_read_cfg(cfg, flash); |
| 622 | |
| 623 | return FLASHROM_WP_ERR_OTHER; |
| 624 | } |
| 625 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 626 | /** |
| 627 | * @brief Get a list of protection ranges supported by the flash chip. |
| 628 | * |
| 629 | * @param[out] ranges Points to a pointer of type struct flashrom_wp_ranges |
| 630 | * that will be set if available ranges are found. Finding |
| 631 | * available ranges may not always be possible, even if the |
| 632 | * chip's protection range can be read or modified. *ranges |
| 633 | * must be freed using @ref flashrom_wp_ranges_free. |
| 634 | * @param[in] flash The flash context used to access the chip. |
| 635 | * @return 0 on success |
| 636 | * >0 on failure |
| 637 | */ |
| 638 | enum flashrom_wp_result flashrom_wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash) |
| 639 | { |
| 640 | /* |
| 641 | * TODO: Call custom implementation if the programmer is opaque, as |
| 642 | * direct WP operations require SPI access. We actually can't implement |
| 643 | * this in linux_mtd right now, but we should adopt a proper generic |
| 644 | * architechure to match the read and write functions anyway. |
| 645 | */ |
| 646 | if (flash->mst->buses_supported & BUS_SPI) |
| 647 | return wp_get_available_ranges(list, flash); |
| 648 | |
| 649 | return FLASHROM_WP_ERR_OTHER; |
| 650 | } |
| 651 | |
| 652 | /** |
| 653 | * @brief Get a number of protection ranges in a range list. |
| 654 | * |
| 655 | * @param[in] ranges The range list to get the count from. |
| 656 | * @return Number of ranges in the list. |
| 657 | */ |
| 658 | size_t flashrom_wp_ranges_get_count(const struct flashrom_wp_ranges *list) |
| 659 | { |
| 660 | return list->count; |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * @brief Get a protection range from a range list. |
| 665 | * |
| 666 | * @param[out] start Points to a size_t to write the range's start to. |
| 667 | * @param[out] len Points to a size_t to write the range's length to. |
| 668 | * @param[in] ranges The range list to get the range from. |
| 669 | * @param[in] index Index of the range to get. |
| 670 | * @return 0 on success |
| 671 | * >0 on failure |
| 672 | */ |
| 673 | enum flashrom_wp_result flashrom_wp_ranges_get_range(size_t *start, size_t *len, const struct flashrom_wp_ranges *list, unsigned int index) |
| 674 | { |
| 675 | if (index >= list->count) |
| 676 | return FLASHROM_WP_ERR_OTHER; |
| 677 | |
| 678 | *start = list->ranges[index].start; |
| 679 | *len = list->ranges[index].len; |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
| 684 | /** |
| 685 | * @brief Free a WP range list. |
| 686 | * |
| 687 | * @param[out] cfg Pointer to the flashrom_wp_ranges to free. |
| 688 | */ |
| 689 | void flashrom_wp_ranges_release(struct flashrom_wp_ranges *list) |
| 690 | { |
| 691 | if (!list) |
| 692 | return; |
| 693 | |
| 694 | free(list->ranges); |
| 695 | free(list); |
| 696 | } |
| 697 | |
| 698 | |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 699 | /** @} */ /* end flashrom-wp */ |