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 | |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <stdarg.h> |
| 26 | |
| 27 | #include "flash.h" |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 28 | #include "fmap.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 29 | #include "programmer.h" |
| 30 | #include "layout.h" |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 31 | #include "hwaccess.h" |
| 32 | #include "ich_descriptors.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 33 | #include "libflashrom.h" |
| 34 | |
| 35 | /** |
| 36 | * @defgroup flashrom-general General |
| 37 | * @{ |
| 38 | */ |
| 39 | |
| 40 | /** Pointer to log callback function. */ |
| 41 | static flashrom_log_callback *global_log_callback = NULL; |
| 42 | |
| 43 | /** |
| 44 | * @brief Initialize libflashrom. |
| 45 | * |
| 46 | * @param perform_selfcheck If not zero, perform a self check. |
| 47 | * @return 0 on success |
| 48 | */ |
| 49 | int flashrom_init(const int perform_selfcheck) |
| 50 | { |
| 51 | if (perform_selfcheck && selfcheck()) |
| 52 | return 1; |
| 53 | myusec_calibrate_delay(); |
| 54 | return 0; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @brief Shut down libflashrom. |
| 59 | * @return 0 on success |
| 60 | */ |
| 61 | int flashrom_shutdown(void) |
| 62 | { |
| 63 | return 0; /* TODO: nothing to do? */ |
| 64 | } |
| 65 | |
| 66 | /* TODO: flashrom_set_loglevel()? do we need it? |
| 67 | For now, let the user decide in his callback. */ |
| 68 | |
| 69 | /** |
| 70 | * @brief Set the log callback function. |
| 71 | * |
| 72 | * Set a callback function which will be invoked whenever libflashrom wants |
| 73 | * to output messages. This allows frontends to do whatever they see fit with |
| 74 | * such messages, e.g. write them to syslog, or to file, or print them in a |
| 75 | * GUI window, etc. |
| 76 | * |
| 77 | * @param log_callback Pointer to the new log callback function. |
| 78 | */ |
| 79 | void flashrom_set_log_callback(flashrom_log_callback *const log_callback) |
| 80 | { |
| 81 | global_log_callback = log_callback; |
| 82 | } |
| 83 | /** @private */ |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 84 | int print(const enum flashrom_log_level level, const char *const fmt, ...) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 85 | { |
| 86 | if (global_log_callback) { |
| 87 | int ret; |
| 88 | va_list args; |
| 89 | va_start(args, fmt); |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 90 | ret = global_log_callback(level, fmt, args); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 91 | va_end(args); |
| 92 | return ret; |
| 93 | } |
| 94 | return 0; |
| 95 | } |
| 96 | |
| 97 | /** @} */ /* end flashrom-general */ |
| 98 | |
| 99 | |
| 100 | |
| 101 | /** |
| 102 | * @defgroup flashrom-query Querying |
| 103 | * @{ |
| 104 | */ |
| 105 | |
| 106 | /* TBD */ |
| 107 | |
| 108 | /** @} */ /* end flashrom-query */ |
| 109 | |
| 110 | |
| 111 | |
| 112 | /** |
| 113 | * @defgroup flashrom-prog Programmers |
| 114 | * @{ |
| 115 | */ |
| 116 | |
| 117 | /** |
| 118 | * @brief Initialize the specified programmer. |
| 119 | * |
| 120 | * Currently, only one programmer may be initialized at a time. |
| 121 | * |
| 122 | * @param[out] flashprog Points to a pointer of type struct flashrom_programmer |
| 123 | * that will be set if programmer initialization succeeds. |
| 124 | * *flashprog has to be shutdown by the caller with @ref |
| 125 | * flashrom_programmer_shutdown. |
| 126 | * @param[in] prog_name Name of the programmer to initialize. |
| 127 | * @param[in] prog_param Pointer to programmer specific parameters. |
| 128 | * @return 0 on success |
| 129 | */ |
| 130 | int flashrom_programmer_init(struct flashrom_programmer **const flashprog, |
| 131 | const char *const prog_name, const char *const prog_param) |
| 132 | { |
| 133 | unsigned prog; |
| 134 | |
| 135 | for (prog = 0; prog < PROGRAMMER_INVALID; prog++) { |
| 136 | if (strcmp(prog_name, programmer_table[prog].name) == 0) |
| 137 | break; |
| 138 | } |
| 139 | if (prog >= PROGRAMMER_INVALID) { |
| 140 | msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name); |
| 141 | list_programmers_linebreak(0, 80, 0); |
| 142 | return 1; |
| 143 | } |
| 144 | return programmer_init(prog, prog_param); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * @brief Shut down the initialized programmer. |
| 149 | * |
| 150 | * @param flashprog The programmer to shut down. |
| 151 | * @return 0 on success |
| 152 | */ |
| 153 | int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog) |
| 154 | { |
| 155 | return programmer_shutdown(); |
| 156 | } |
| 157 | |
| 158 | /* TODO: flashrom_programmer_capabilities()? */ |
| 159 | |
| 160 | /** @} */ /* end flashrom-prog */ |
| 161 | |
| 162 | |
| 163 | |
| 164 | /** |
| 165 | * @defgroup flashrom-flash Flash chips |
| 166 | * @{ |
| 167 | */ |
| 168 | |
| 169 | /** |
| 170 | * @brief Probe for a flash chip. |
| 171 | * |
| 172 | * Probes for a flash chip and returns a flash context, that can be used |
| 173 | * later with flash chip and @ref flashrom-ops "image operations", if |
| 174 | * exactly one matching chip is found. |
| 175 | * |
| 176 | * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx |
| 177 | * that will be set if exactly one chip is found. *flashctx |
| 178 | * has to be freed by the caller with @ref flashrom_flash_release. |
| 179 | * @param[in] flashprog The flash programmer used to access the chip. |
| 180 | * @param[in] chip_name Name of a chip to probe for, or NULL to probe for |
| 181 | * all known chips. |
| 182 | * @return 0 on success, |
| 183 | * 3 if multiple chips were found, |
| 184 | * 2 if no chip was found, |
| 185 | * or 1 on any other error. |
| 186 | */ |
| 187 | int flashrom_flash_probe(struct flashrom_flashctx **const flashctx, |
| 188 | const struct flashrom_programmer *const flashprog, |
| 189 | const char *const chip_name) |
| 190 | { |
| 191 | int i, ret = 2; |
| 192 | struct flashrom_flashctx second_flashctx = { 0, }; |
| 193 | |
| 194 | chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */ |
| 195 | |
| 196 | *flashctx = malloc(sizeof(**flashctx)); |
| 197 | if (!*flashctx) |
| 198 | return 1; |
| 199 | memset(*flashctx, 0, sizeof(**flashctx)); |
| 200 | |
| 201 | for (i = 0; i < registered_master_count; ++i) { |
| 202 | int flash_idx = -1; |
| 203 | if (!ret || (flash_idx = probe_flash(®istered_masters[i], 0, *flashctx, 0)) != -1) { |
| 204 | ret = 0; |
| 205 | /* We found one chip, now check that there is no second match. */ |
| 206 | if (probe_flash(®istered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) { |
| 207 | ret = 3; |
| 208 | break; |
| 209 | } |
| 210 | } |
| 211 | } |
| 212 | if (ret) { |
| 213 | free(*flashctx); |
| 214 | *flashctx = NULL; |
| 215 | } |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * @brief Returns the size of the specified flash chip in bytes. |
| 221 | * |
| 222 | * @param flashctx The queried flash context. |
| 223 | * @return Size of flash chip in bytes. |
| 224 | */ |
| 225 | size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx) |
| 226 | { |
| 227 | return flashctx->chip->total_size * 1024; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * @brief Free a flash context. |
| 232 | * |
| 233 | * @param flashctx Flash context to free. |
| 234 | */ |
| 235 | void flashrom_flash_release(struct flashrom_flashctx *const flashctx) |
| 236 | { |
| 237 | free(flashctx); |
| 238 | } |
| 239 | |
| 240 | /** |
| 241 | * @brief Set a flag in the given flash context. |
| 242 | * |
| 243 | * @param flashctx Flash context to alter. |
| 244 | * @param flag Flag that is to be set / cleared. |
| 245 | * @param value Value to set. |
| 246 | */ |
| 247 | void flashrom_flag_set(struct flashrom_flashctx *const flashctx, |
| 248 | const enum flashrom_flag flag, const bool value) |
| 249 | { |
| 250 | switch (flag) { |
| 251 | case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break; |
| 252 | case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break; |
| 253 | case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break; |
| 254 | case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | /** |
| 259 | * @brief Return the current value of a flag in the given flash context. |
| 260 | * |
| 261 | * @param flashctx Flash context to read from. |
| 262 | * @param flag Flag to be read. |
| 263 | * @return Current value of the flag. |
| 264 | */ |
| 265 | bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag) |
| 266 | { |
| 267 | switch (flag) { |
| 268 | case FLASHROM_FLAG_FORCE: return flashctx->flags.force; |
| 269 | case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch; |
| 270 | case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write; |
| 271 | case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip; |
| 272 | default: return false; |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /** @} */ /* end flashrom-flash */ |
| 277 | |
| 278 | |
| 279 | |
| 280 | /** |
| 281 | * @defgroup flashrom-layout Layout handling |
| 282 | * @{ |
| 283 | */ |
| 284 | |
| 285 | /** |
| 286 | * @brief Mark given region as included. |
| 287 | * |
| 288 | * @param layout The layout to alter. |
| 289 | * @param name The name of the region to include. |
| 290 | * |
| 291 | * @return 0 on success, |
| 292 | * 1 if the given name can't be found. |
| 293 | */ |
| 294 | int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name) |
| 295 | { |
| 296 | size_t i; |
| 297 | for (i = 0; i < layout->num_entries; ++i) { |
| 298 | if (!strcmp(layout->entries[i].name, name)) { |
| 299 | layout->entries[i].included = true; |
| 300 | return 0; |
| 301 | } |
| 302 | } |
| 303 | return 1; |
| 304 | } |
| 305 | |
| 306 | /** |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 307 | * @brief Read a layout from the Intel ICH descriptor in the flash. |
| 308 | * |
| 309 | * Optionally verify that the layout matches the one in the given |
| 310 | * descriptor dump. |
| 311 | * |
| 312 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 313 | * gets set if the descriptor is read and parsed |
| 314 | * successfully. |
| 315 | * @param[in] flashctx Flash context to read the descriptor from flash. |
| 316 | * @param[in] dump The descriptor dump to compare to or NULL. |
| 317 | * @param[in] len The length of the descriptor dump. |
| 318 | * |
| 319 | * @return 0 on success, |
| 320 | * 6 if descriptor parsing isn't implemented for the host, |
| 321 | * 5 if the descriptors don't match, |
| 322 | * 4 if the descriptor dump couldn't be parsed, |
| 323 | * 3 if the descriptor on flash couldn't be parsed, |
| 324 | * 2 if the descriptor on flash couldn't be read, |
| 325 | * 1 on any other error. |
| 326 | */ |
| 327 | int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx, |
| 328 | const void *const dump, const size_t len) |
| 329 | { |
| 330 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 331 | return 6; |
| 332 | #else |
| 333 | struct ich_layout dump_layout; |
| 334 | int ret = 1; |
| 335 | |
| 336 | void *const desc = malloc(0x1000); |
| 337 | struct ich_layout *const chip_layout = malloc(sizeof(*chip_layout)); |
| 338 | if (!desc || !chip_layout) { |
| 339 | msg_gerr("Out of memory!\n"); |
| 340 | goto _free_ret; |
| 341 | } |
| 342 | |
| 343 | if (prepare_flash_access(flashctx, true, false, false, false)) |
| 344 | goto _free_ret; |
| 345 | |
| 346 | msg_cinfo("Reading ich descriptor... "); |
| 347 | if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) { |
| 348 | msg_cerr("Read operation failed!\n"); |
| 349 | msg_cinfo("FAILED.\n"); |
| 350 | ret = 2; |
| 351 | goto _finalize_ret; |
| 352 | } |
| 353 | msg_cinfo("done.\n"); |
| 354 | |
| 355 | if (layout_from_ich_descriptors(chip_layout, desc, 0x1000)) { |
| 356 | ret = 3; |
| 357 | goto _finalize_ret; |
| 358 | } |
| 359 | |
| 360 | if (dump) { |
| 361 | if (layout_from_ich_descriptors(&dump_layout, dump, len)) { |
| 362 | ret = 4; |
| 363 | goto _finalize_ret; |
| 364 | } |
| 365 | |
| 366 | if (chip_layout->base.num_entries != dump_layout.base.num_entries || |
| 367 | memcmp(chip_layout->entries, dump_layout.entries, sizeof(dump_layout.entries))) { |
| 368 | ret = 5; |
| 369 | goto _finalize_ret; |
| 370 | } |
| 371 | } |
| 372 | |
| 373 | *layout = (struct flashrom_layout *)chip_layout; |
| 374 | ret = 0; |
| 375 | |
| 376 | _finalize_ret: |
| 377 | finalize_flash_access(flashctx); |
| 378 | _free_ret: |
| 379 | if (ret) |
| 380 | free(chip_layout); |
| 381 | free(desc); |
| 382 | return ret; |
| 383 | #endif |
| 384 | } |
| 385 | |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 386 | static int flashrom_layout_parse_fmap(struct flashrom_layout **layout, |
| 387 | struct flashctx *const flashctx, const struct fmap *const fmap) |
| 388 | { |
| 389 | int i; |
| 390 | struct flashrom_layout *l = get_global_layout(); |
| 391 | |
| 392 | if (!fmap || !l) |
| 393 | return 1; |
| 394 | |
| 395 | if (l->num_entries + fmap->nareas > MAX_ROMLAYOUT) { |
| 396 | msg_gerr("Cannot add fmap entries to layout - Too many entries.\n"); |
| 397 | return 1; |
| 398 | } |
| 399 | |
| 400 | for (i = 0; i < fmap->nareas; i++) { |
| 401 | l->entries[l->num_entries].start = fmap->areas[i].offset; |
| 402 | l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1; |
| 403 | l->entries[l->num_entries].included = false; |
| 404 | memset(l->entries[l->num_entries].name, 0, sizeof(l->entries[i].name)); |
| 405 | memcpy(l->entries[l->num_entries].name, fmap->areas[i].name, |
| 406 | min(FMAP_STRLEN, sizeof(l->entries[i].name))); |
| 407 | msg_gdbg("fmap %08x - %08x named %s\n", |
| 408 | l->entries[l->num_entries].start, |
| 409 | l->entries[l->num_entries].end, |
| 410 | l->entries[l->num_entries].name); |
| 411 | l->num_entries++; |
| 412 | } |
| 413 | |
| 414 | *layout = l; |
| 415 | return 0; |
| 416 | } |
| 417 | |
| 418 | /** |
| 419 | * @brief Read a layout by searching the flash chip for fmap. |
| 420 | * |
| 421 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 422 | * gets set if the fmap is read and parsed successfully. |
| 423 | * @param[in] flashctx Flash context |
| 424 | * @param[in] offset Offset to begin searching for fmap. |
| 425 | * @param[in] offset Length of address space to search. |
| 426 | * |
| 427 | * @return 0 on success, |
| 428 | * 3 if fmap parsing isn't implemented for the host, |
| 429 | * 2 if the fmap couldn't be read, |
| 430 | * 1 on any other error. |
| 431 | */ |
| 432 | int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout, |
| 433 | struct flashctx *const flashctx, off_t offset, size_t len) |
| 434 | { |
| 435 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 436 | return 3; |
| 437 | #else |
| 438 | struct fmap *fmap = NULL; |
| 439 | int ret = 0; |
| 440 | |
| 441 | msg_gdbg("Attempting to read fmap from ROM content.\n"); |
| 442 | if (fmap_read_from_rom(&fmap, flashctx, offset, len)) { |
| 443 | msg_gerr("Failed to read fmap from ROM.\n"); |
| 444 | return 1; |
| 445 | } |
| 446 | |
| 447 | msg_gdbg("Adding fmap layout to global layout.\n"); |
| 448 | if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) { |
| 449 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 450 | ret = 1; |
| 451 | } |
| 452 | |
| 453 | free(fmap); |
| 454 | return ret; |
| 455 | #endif |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * @brief Read a layout by searching buffer for fmap. |
| 460 | * |
| 461 | * @param[out] layout Points to a struct flashrom_layout pointer that |
| 462 | * gets set if the fmap is read and parsed successfully. |
| 463 | * @param[in] flashctx Flash context |
| 464 | * @param[in] buffer Buffer to search in |
| 465 | * @param[in] size Size of buffer to search |
| 466 | * |
| 467 | * @return 0 on success, |
| 468 | * 3 if fmap parsing isn't implemented for the host, |
| 469 | * 2 if the fmap couldn't be read, |
| 470 | * 1 on any other error. |
| 471 | */ |
| 472 | int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout, |
| 473 | struct flashctx *const flashctx, const uint8_t *const buf, size_t size) |
| 474 | { |
| 475 | #ifndef __FLASHROM_LITTLE_ENDIAN__ |
| 476 | return 3; |
| 477 | #else |
| 478 | struct fmap *fmap = NULL; |
| 479 | int ret = 1; |
| 480 | |
| 481 | if (!buf || !size) |
| 482 | goto _ret; |
| 483 | |
| 484 | msg_gdbg("Attempting to read fmap from buffer.\n"); |
| 485 | if (fmap_read_from_buffer(&fmap, buf, size)) { |
| 486 | msg_gerr("Failed to read fmap from buffer.\n"); |
| 487 | goto _ret; |
| 488 | } |
| 489 | |
| 490 | msg_gdbg("Adding fmap layout to global layout.\n"); |
| 491 | if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) { |
| 492 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 493 | goto _free_ret; |
| 494 | } |
| 495 | |
| 496 | ret = 0; |
| 497 | _free_ret: |
| 498 | free(fmap); |
| 499 | _ret: |
| 500 | return ret; |
| 501 | #endif |
| 502 | } |
| 503 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 504 | /** |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 505 | * @brief Free a layout. |
| 506 | * |
| 507 | * @param layout Layout to free. |
| 508 | */ |
| 509 | void flashrom_layout_release(struct flashrom_layout *const layout) |
| 510 | { |
| 511 | if (layout == get_global_layout()) |
| 512 | return; |
| 513 | |
| 514 | free(layout); |
| 515 | } |
| 516 | |
| 517 | /** |
| 518 | * @brief Set the active layout for a flash context. |
| 519 | * |
| 520 | * Note: This just sets a pointer. The caller must not release the layout |
| 521 | * as long as he uses it through the given flash context. |
| 522 | * |
| 523 | * @param flashctx Flash context whose layout will be set. |
| 524 | * @param layout Layout to bet set. |
| 525 | */ |
| 526 | void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout) |
| 527 | { |
| 528 | flashctx->layout = layout; |
| 529 | } |
| 530 | |
| 531 | /** @} */ /* end flashrom-layout */ |