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