blob: 0dec22ee527f54712ae1a3e05c4acaec82844f45 [file] [log] [blame]
Nico Huber454f6132012-12-10 13:34:10 +00001/*
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 Huber454f6132012-12-10 13:34:10 +000016 */
17/**
18 * @mainpage
19 *
20 * Have a look at the Modules section for a function reference.
21 */
22
Nico Huber70461a92019-06-15 14:56:19 +020023#include <errno.h>
Nico Huber454f6132012-12-10 13:34:10 +000024#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27
28#include "flash.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010029#include "fmap.h"
Nico Huber454f6132012-12-10 13:34:10 +000030#include "programmer.h"
31#include "layout.h"
Nico Huber305f4172013-06-14 11:55:26 +020032#include "hwaccess.h"
33#include "ich_descriptors.h"
Nico Huber454f6132012-12-10 13:34:10 +000034#include "libflashrom.h"
35
36/**
37 * @defgroup flashrom-general General
38 * @{
39 */
40
41/** Pointer to log callback function. */
42static 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 */
50int 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 */
62int flashrom_shutdown(void)
63{
64 return 0; /* TODO: nothing to do? */
65}
66
67/* TODO: flashrom_set_loglevel()? do we need it?
68 For now, let the user decide in his callback. */
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 */
80void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
81{
82 global_log_callback = log_callback;
83}
84/** @private */
Nico Huberd152fb92017-06-19 12:57:10 +020085int print(const enum flashrom_log_level level, const char *const fmt, ...)
Nico Huber454f6132012-12-10 13:34:10 +000086{
87 if (global_log_callback) {
88 int ret;
89 va_list args;
90 va_start(args, fmt);
Nico Huberd152fb92017-06-19 12:57:10 +020091 ret = global_log_callback(level, fmt, args);
Nico Huber454f6132012-12-10 13:34:10 +000092 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
Artur Raglis71b706f2019-06-05 19:24:52 +0200107/**
108 * @brief Returns flashrom version
109 * @return flashrom version
110 */
111const char *flashrom_version_info(void)
112{
113 return flashrom_version;
114}
115
116/**
117 * @brief Returns list of supported programmers
118 * @return List of supported programmers, or NULL if an error occurred
119 */
120const char **flashrom_supported_programmers(void)
121{
122 enum programmer p = 0;
123 const char **supported_programmers = malloc((PROGRAMMER_INVALID + 1) * sizeof(char*));
124
125 if (supported_programmers != NULL) {
126 for (; p < PROGRAMMER_INVALID; ++p) {
127 supported_programmers[p] = programmer_table[p].name;
128 }
129 } else {
130 msg_gerr("Memory allocation error!\n");
131 }
132
133 return supported_programmers;
134}
135
136/**
137 * @brief Returns list of supported flash chips
138 * @return List of supported flash chips, or NULL if an error occurred
139 */
140struct flashrom_flashchip_info *flashrom_supported_flash_chips(void)
141{
Nico Huber961f4a12019-10-04 17:34:22 +0200142 unsigned int i = 0;
Artur Raglis71b706f2019-06-05 19:24:52 +0200143 struct flashrom_flashchip_info *supported_flashchips =
144 malloc(flashchips_size * sizeof(*supported_flashchips));
145
146 if (supported_flashchips != NULL) {
147 for (; i < flashchips_size; ++i) {
148 supported_flashchips[i].vendor = flashchips[i].vendor;
149 supported_flashchips[i].name = flashchips[i].name;
150 supported_flashchips[i].tested.erase =
151 (enum flashrom_test_state)flashchips[i].tested.erase;
152 supported_flashchips[i].tested.probe =
153 (enum flashrom_test_state)flashchips[i].tested.probe;
154 supported_flashchips[i].tested.read =
155 (enum flashrom_test_state)flashchips[i].tested.read;
156 supported_flashchips[i].tested.write =
157 (enum flashrom_test_state)flashchips[i].tested.write;
158 supported_flashchips[i].total_size = flashchips[i].total_size;
159 }
160 } else {
161 msg_gerr("Memory allocation error!\n");
162 }
163
164 return supported_flashchips;
165}
166
167/**
168 * @brief Returns list of supported mainboards
169 * @return List of supported mainboards, or NULL if an error occurred
170 */
171struct flashrom_board_info *flashrom_supported_boards(void)
172{
Jonathan Liu7f15de12019-10-06 16:22:04 +1100173#if CONFIG_INTERNAL == 1
Artur Raglis71b706f2019-06-05 19:24:52 +0200174 int boards_known_size = 0;
175 int i = 0;
176 const struct board_info *binfo = boards_known;
177
178 while ((binfo++)->vendor)
179 ++boards_known_size;
180 binfo = boards_known;
181 /* add place for {0} */
182 ++boards_known_size;
183
184 struct flashrom_board_info *supported_boards =
185 malloc(boards_known_size * sizeof(*binfo));
186
187 if (supported_boards != NULL) {
188 for (; i < boards_known_size; ++i) {
189 supported_boards[i].vendor = binfo[i].vendor;
190 supported_boards[i].name = binfo[i].name;
191 supported_boards[i].working = binfo[i].working;
192 }
193 } else {
194 msg_gerr("Memory allocation error!\n");
195 }
196
197 return supported_boards;
Jonathan Liu7f15de12019-10-06 16:22:04 +1100198#else
199 return NULL;
200#endif
Artur Raglis71b706f2019-06-05 19:24:52 +0200201}
202
203/**
204 * @brief Returns list of supported chipsets
205 * @return List of supported chipsets, or NULL if an error occurred
206 */
207struct flashrom_chipset_info *flashrom_supported_chipsets(void)
208{
Jonathan Liu7f15de12019-10-06 16:22:04 +1100209#if CONFIG_INTERNAL == 1
Artur Raglis71b706f2019-06-05 19:24:52 +0200210 int chipset_enables_size = 0;
211 int i = 0;
212 const struct penable *chipset = chipset_enables;
213
214 while ((chipset++)->vendor_name)
215 ++chipset_enables_size;
216 chipset = chipset_enables;
217 /* add place for {0}*/
218 ++chipset_enables_size;
219
220 struct flashrom_chipset_info *supported_chipsets =
221 malloc(chipset_enables_size * sizeof(*supported_chipsets));
222
223 if (supported_chipsets != NULL) {
224 for (; i < chipset_enables_size; ++i) {
225 supported_chipsets[i].vendor = chipset[i].vendor_name;
226 supported_chipsets[i].chipset = chipset[i].device_name;
227 supported_chipsets[i].vendor_id = chipset[i].vendor_id;
228 supported_chipsets[i].chipset_id = chipset[i].device_id;
229 supported_chipsets[i].status = chipset[i].status;
230 }
231 } else {
232 msg_gerr("Memory allocation error!\n");
233 }
234
235 return supported_chipsets;
Jonathan Liu7f15de12019-10-06 16:22:04 +1100236#else
237 return NULL;
238#endif
Artur Raglis71b706f2019-06-05 19:24:52 +0200239}
240
241/**
242 * @brief Frees memory allocated by libflashrom API
243 * @param Pointer to block of memory which should be freed
244 * @return 0 on success
245 */
246int flashrom_data_free(void *const p)
247{
248 free(p);
249 return 0;
250}
Nico Huber454f6132012-12-10 13:34:10 +0000251
252/** @} */ /* end flashrom-query */
253
254
255
256/**
257 * @defgroup flashrom-prog Programmers
258 * @{
259 */
260
261/**
262 * @brief Initialize the specified programmer.
263 *
264 * Currently, only one programmer may be initialized at a time.
265 *
266 * @param[out] flashprog Points to a pointer of type struct flashrom_programmer
267 * that will be set if programmer initialization succeeds.
268 * *flashprog has to be shutdown by the caller with @ref
269 * flashrom_programmer_shutdown.
270 * @param[in] prog_name Name of the programmer to initialize.
271 * @param[in] prog_param Pointer to programmer specific parameters.
272 * @return 0 on success
273 */
274int flashrom_programmer_init(struct flashrom_programmer **const flashprog,
275 const char *const prog_name, const char *const prog_param)
276{
277 unsigned prog;
278
279 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
280 if (strcmp(prog_name, programmer_table[prog].name) == 0)
281 break;
282 }
283 if (prog >= PROGRAMMER_INVALID) {
284 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
285 list_programmers_linebreak(0, 80, 0);
286 return 1;
287 }
288 return programmer_init(prog, prog_param);
289}
290
291/**
292 * @brief Shut down the initialized programmer.
293 *
294 * @param flashprog The programmer to shut down.
295 * @return 0 on success
296 */
297int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)
298{
299 return programmer_shutdown();
300}
301
302/* TODO: flashrom_programmer_capabilities()? */
303
304/** @} */ /* end flashrom-prog */
305
306
307
308/**
309 * @defgroup flashrom-flash Flash chips
310 * @{
311 */
312
313/**
314 * @brief Probe for a flash chip.
315 *
316 * Probes for a flash chip and returns a flash context, that can be used
317 * later with flash chip and @ref flashrom-ops "image operations", if
318 * exactly one matching chip is found.
319 *
320 * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx
321 * that will be set if exactly one chip is found. *flashctx
322 * has to be freed by the caller with @ref flashrom_flash_release.
323 * @param[in] flashprog The flash programmer used to access the chip.
324 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
325 * all known chips.
326 * @return 0 on success,
327 * 3 if multiple chips were found,
328 * 2 if no chip was found,
329 * or 1 on any other error.
330 */
331int flashrom_flash_probe(struct flashrom_flashctx **const flashctx,
332 const struct flashrom_programmer *const flashprog,
333 const char *const chip_name)
334{
335 int i, ret = 2;
336 struct flashrom_flashctx second_flashctx = { 0, };
337
338 chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */
339
340 *flashctx = malloc(sizeof(**flashctx));
341 if (!*flashctx)
342 return 1;
343 memset(*flashctx, 0, sizeof(**flashctx));
344
345 for (i = 0; i < registered_master_count; ++i) {
346 int flash_idx = -1;
347 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
348 ret = 0;
349 /* We found one chip, now check that there is no second match. */
350 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
351 ret = 3;
352 break;
353 }
354 }
355 }
356 if (ret) {
357 free(*flashctx);
358 *flashctx = NULL;
359 }
360 return ret;
361}
362
363/**
364 * @brief Returns the size of the specified flash chip in bytes.
365 *
366 * @param flashctx The queried flash context.
367 * @return Size of flash chip in bytes.
368 */
369size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx)
370{
371 return flashctx->chip->total_size * 1024;
372}
373
374/**
375 * @brief Free a flash context.
376 *
377 * @param flashctx Flash context to free.
378 */
379void flashrom_flash_release(struct flashrom_flashctx *const flashctx)
380{
381 free(flashctx);
382}
383
384/**
385 * @brief Set a flag in the given flash context.
386 *
387 * @param flashctx Flash context to alter.
388 * @param flag Flag that is to be set / cleared.
389 * @param value Value to set.
390 */
391void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
392 const enum flashrom_flag flag, const bool value)
393{
394 switch (flag) {
395 case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
396 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
397 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
398 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
399 }
400}
401
402/**
403 * @brief Return the current value of a flag in the given flash context.
404 *
405 * @param flashctx Flash context to read from.
406 * @param flag Flag to be read.
407 * @return Current value of the flag.
408 */
409bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
410{
411 switch (flag) {
412 case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
413 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
414 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
415 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
416 default: return false;
417 }
418}
419
420/** @} */ /* end flashrom-flash */
421
422
423
424/**
425 * @defgroup flashrom-layout Layout handling
426 * @{
427 */
428
429/**
Nico Huber305f4172013-06-14 11:55:26 +0200430 * @brief Read a layout from the Intel ICH descriptor in the flash.
431 *
432 * Optionally verify that the layout matches the one in the given
433 * descriptor dump.
434 *
435 * @param[out] layout Points to a struct flashrom_layout pointer that
436 * gets set if the descriptor is read and parsed
437 * successfully.
438 * @param[in] flashctx Flash context to read the descriptor from flash.
439 * @param[in] dump The descriptor dump to compare to or NULL.
440 * @param[in] len The length of the descriptor dump.
441 *
442 * @return 0 on success,
443 * 6 if descriptor parsing isn't implemented for the host,
444 * 5 if the descriptors don't match,
445 * 4 if the descriptor dump couldn't be parsed,
446 * 3 if the descriptor on flash couldn't be parsed,
447 * 2 if the descriptor on flash couldn't be read,
448 * 1 on any other error.
449 */
450int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx,
451 const void *const dump, const size_t len)
452{
453#ifndef __FLASHROM_LITTLE_ENDIAN__
454 return 6;
455#else
456 struct ich_layout dump_layout;
457 int ret = 1;
458
459 void *const desc = malloc(0x1000);
460 struct ich_layout *const chip_layout = malloc(sizeof(*chip_layout));
461 if (!desc || !chip_layout) {
462 msg_gerr("Out of memory!\n");
463 goto _free_ret;
464 }
465
466 if (prepare_flash_access(flashctx, true, false, false, false))
467 goto _free_ret;
468
469 msg_cinfo("Reading ich descriptor... ");
470 if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) {
471 msg_cerr("Read operation failed!\n");
472 msg_cinfo("FAILED.\n");
473 ret = 2;
474 goto _finalize_ret;
475 }
476 msg_cinfo("done.\n");
477
478 if (layout_from_ich_descriptors(chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200479 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200480 ret = 3;
481 goto _finalize_ret;
482 }
483
484 if (dump) {
485 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200486 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200487 ret = 4;
488 goto _finalize_ret;
489 }
490
491 if (chip_layout->base.num_entries != dump_layout.base.num_entries ||
492 memcmp(chip_layout->entries, dump_layout.entries, sizeof(dump_layout.entries))) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200493 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200494 ret = 5;
495 goto _finalize_ret;
496 }
497 }
498
499 *layout = (struct flashrom_layout *)chip_layout;
500 ret = 0;
501
502_finalize_ret:
503 finalize_flash_access(flashctx);
504_free_ret:
505 if (ret)
506 free(chip_layout);
507 free(desc);
508 return ret;
509#endif
510}
511
Nico Huberee13d0c2019-06-07 17:47:40 +0200512#ifdef __FLASHROM_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100513static int flashrom_layout_parse_fmap(struct flashrom_layout **layout,
514 struct flashctx *const flashctx, const struct fmap *const fmap)
515{
516 int i;
517 struct flashrom_layout *l = get_global_layout();
518
519 if (!fmap || !l)
520 return 1;
521
522 if (l->num_entries + fmap->nareas > MAX_ROMLAYOUT) {
523 msg_gerr("Cannot add fmap entries to layout - Too many entries.\n");
524 return 1;
525 }
526
527 for (i = 0; i < fmap->nareas; i++) {
528 l->entries[l->num_entries].start = fmap->areas[i].offset;
529 l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1;
530 l->entries[l->num_entries].included = false;
Nico Huber70461a92019-06-15 14:56:19 +0200531 l->entries[l->num_entries].name =
532 strndup((const char *)fmap->areas[i].name, FMAP_STRLEN);
533 if (!l->entries[l->num_entries].name) {
534 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
535 return 1;
536 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100537 msg_gdbg("fmap %08x - %08x named %s\n",
538 l->entries[l->num_entries].start,
539 l->entries[l->num_entries].end,
540 l->entries[l->num_entries].name);
541 l->num_entries++;
542 }
543
544 *layout = l;
545 return 0;
546}
Nico Huberee13d0c2019-06-07 17:47:40 +0200547#endif /* __FLASHROM_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100548
549/**
550 * @brief Read a layout by searching the flash chip for fmap.
551 *
552 * @param[out] layout Points to a struct flashrom_layout pointer that
553 * gets set if the fmap is read and parsed successfully.
554 * @param[in] flashctx Flash context
555 * @param[in] offset Offset to begin searching for fmap.
556 * @param[in] offset Length of address space to search.
557 *
558 * @return 0 on success,
559 * 3 if fmap parsing isn't implemented for the host,
560 * 2 if the fmap couldn't be read,
561 * 1 on any other error.
562 */
563int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout,
564 struct flashctx *const flashctx, off_t offset, size_t len)
565{
566#ifndef __FLASHROM_LITTLE_ENDIAN__
567 return 3;
568#else
569 struct fmap *fmap = NULL;
570 int ret = 0;
571
572 msg_gdbg("Attempting to read fmap from ROM content.\n");
573 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
574 msg_gerr("Failed to read fmap from ROM.\n");
575 return 1;
576 }
577
578 msg_gdbg("Adding fmap layout to global layout.\n");
579 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
580 msg_gerr("Failed to add fmap regions to layout.\n");
581 ret = 1;
582 }
583
584 free(fmap);
585 return ret;
586#endif
587}
588
589/**
590 * @brief Read a layout by searching buffer for fmap.
591 *
592 * @param[out] layout Points to a struct flashrom_layout pointer that
593 * gets set if the fmap is read and parsed successfully.
594 * @param[in] flashctx Flash context
595 * @param[in] buffer Buffer to search in
596 * @param[in] size Size of buffer to search
597 *
598 * @return 0 on success,
599 * 3 if fmap parsing isn't implemented for the host,
600 * 2 if the fmap couldn't be read,
601 * 1 on any other error.
602 */
603int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout,
604 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
605{
606#ifndef __FLASHROM_LITTLE_ENDIAN__
607 return 3;
608#else
609 struct fmap *fmap = NULL;
610 int ret = 1;
611
612 if (!buf || !size)
613 goto _ret;
614
615 msg_gdbg("Attempting to read fmap from buffer.\n");
616 if (fmap_read_from_buffer(&fmap, buf, size)) {
617 msg_gerr("Failed to read fmap from buffer.\n");
618 goto _ret;
619 }
620
621 msg_gdbg("Adding fmap layout to global layout.\n");
622 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
623 msg_gerr("Failed to add fmap regions to layout.\n");
624 goto _free_ret;
625 }
626
627 ret = 0;
628_free_ret:
629 free(fmap);
630_ret:
631 return ret;
632#endif
633}
634
Nico Huber305f4172013-06-14 11:55:26 +0200635/**
Nico Huber454f6132012-12-10 13:34:10 +0000636 * @brief Set the active layout for a flash context.
637 *
638 * Note: This just sets a pointer. The caller must not release the layout
639 * as long as he uses it through the given flash context.
640 *
641 * @param flashctx Flash context whose layout will be set.
642 * @param layout Layout to bet set.
643 */
644void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
645{
646 flashctx->layout = layout;
647}
648
649/** @} */ /* end flashrom-layout */