blob: af62002a603763d629ac567ffcbc098b31fb0e75 [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
107/* TBD */
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 */
131int flashrom_programmer_init(struct flashrom_programmer **const flashprog,
132 const char *const prog_name, const char *const prog_param)
133{
134 unsigned prog;
135
136 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
137 if (strcmp(prog_name, programmer_table[prog].name) == 0)
138 break;
139 }
140 if (prog >= PROGRAMMER_INVALID) {
141 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
142 list_programmers_linebreak(0, 80, 0);
143 return 1;
144 }
145 return programmer_init(prog, prog_param);
146}
147
148/**
149 * @brief Shut down the initialized programmer.
150 *
151 * @param flashprog The programmer to shut down.
152 * @return 0 on success
153 */
154int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)
155{
156 return programmer_shutdown();
157}
158
159/* TODO: flashrom_programmer_capabilities()? */
160
161/** @} */ /* end flashrom-prog */
162
163
164
165/**
166 * @defgroup flashrom-flash Flash chips
167 * @{
168 */
169
170/**
171 * @brief Probe for a flash chip.
172 *
173 * Probes for a flash chip and returns a flash context, that can be used
174 * later with flash chip and @ref flashrom-ops "image operations", if
175 * exactly one matching chip is found.
176 *
177 * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx
178 * that will be set if exactly one chip is found. *flashctx
179 * has to be freed by the caller with @ref flashrom_flash_release.
180 * @param[in] flashprog The flash programmer used to access the chip.
181 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
182 * all known chips.
183 * @return 0 on success,
184 * 3 if multiple chips were found,
185 * 2 if no chip was found,
186 * or 1 on any other error.
187 */
188int flashrom_flash_probe(struct flashrom_flashctx **const flashctx,
189 const struct flashrom_programmer *const flashprog,
190 const char *const chip_name)
191{
192 int i, ret = 2;
193 struct flashrom_flashctx second_flashctx = { 0, };
194
195 chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */
196
197 *flashctx = malloc(sizeof(**flashctx));
198 if (!*flashctx)
199 return 1;
200 memset(*flashctx, 0, sizeof(**flashctx));
201
202 for (i = 0; i < registered_master_count; ++i) {
203 int flash_idx = -1;
204 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
205 ret = 0;
206 /* We found one chip, now check that there is no second match. */
207 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
208 ret = 3;
209 break;
210 }
211 }
212 }
213 if (ret) {
214 free(*flashctx);
215 *flashctx = NULL;
216 }
217 return ret;
218}
219
220/**
221 * @brief Returns the size of the specified flash chip in bytes.
222 *
223 * @param flashctx The queried flash context.
224 * @return Size of flash chip in bytes.
225 */
226size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx)
227{
228 return flashctx->chip->total_size * 1024;
229}
230
231/**
232 * @brief Free a flash context.
233 *
234 * @param flashctx Flash context to free.
235 */
236void flashrom_flash_release(struct flashrom_flashctx *const flashctx)
237{
238 free(flashctx);
239}
240
241/**
242 * @brief Set a flag in the given flash context.
243 *
244 * @param flashctx Flash context to alter.
245 * @param flag Flag that is to be set / cleared.
246 * @param value Value to set.
247 */
248void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
249 const enum flashrom_flag flag, const bool value)
250{
251 switch (flag) {
252 case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
253 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
254 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
255 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
256 }
257}
258
259/**
260 * @brief Return the current value of a flag in the given flash context.
261 *
262 * @param flashctx Flash context to read from.
263 * @param flag Flag to be read.
264 * @return Current value of the flag.
265 */
266bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
267{
268 switch (flag) {
269 case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
270 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
271 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
272 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
273 default: return false;
274 }
275}
276
277/** @} */ /* end flashrom-flash */
278
279
280
281/**
282 * @defgroup flashrom-layout Layout handling
283 * @{
284 */
285
286/**
Nico Huber305f4172013-06-14 11:55:26 +0200287 * @brief Read a layout from the Intel ICH descriptor in the flash.
288 *
289 * Optionally verify that the layout matches the one in the given
290 * descriptor dump.
291 *
292 * @param[out] layout Points to a struct flashrom_layout pointer that
293 * gets set if the descriptor is read and parsed
294 * successfully.
295 * @param[in] flashctx Flash context to read the descriptor from flash.
296 * @param[in] dump The descriptor dump to compare to or NULL.
297 * @param[in] len The length of the descriptor dump.
298 *
299 * @return 0 on success,
300 * 6 if descriptor parsing isn't implemented for the host,
301 * 5 if the descriptors don't match,
302 * 4 if the descriptor dump couldn't be parsed,
303 * 3 if the descriptor on flash couldn't be parsed,
304 * 2 if the descriptor on flash couldn't be read,
305 * 1 on any other error.
306 */
307int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx,
308 const void *const dump, const size_t len)
309{
310#ifndef __FLASHROM_LITTLE_ENDIAN__
311 return 6;
312#else
313 struct ich_layout dump_layout;
314 int ret = 1;
315
316 void *const desc = malloc(0x1000);
317 struct ich_layout *const chip_layout = malloc(sizeof(*chip_layout));
318 if (!desc || !chip_layout) {
319 msg_gerr("Out of memory!\n");
320 goto _free_ret;
321 }
322
323 if (prepare_flash_access(flashctx, true, false, false, false))
324 goto _free_ret;
325
326 msg_cinfo("Reading ich descriptor... ");
327 if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) {
328 msg_cerr("Read operation failed!\n");
329 msg_cinfo("FAILED.\n");
330 ret = 2;
331 goto _finalize_ret;
332 }
333 msg_cinfo("done.\n");
334
335 if (layout_from_ich_descriptors(chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200336 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200337 ret = 3;
338 goto _finalize_ret;
339 }
340
341 if (dump) {
342 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200343 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200344 ret = 4;
345 goto _finalize_ret;
346 }
347
348 if (chip_layout->base.num_entries != dump_layout.base.num_entries ||
349 memcmp(chip_layout->entries, dump_layout.entries, sizeof(dump_layout.entries))) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200350 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200351 ret = 5;
352 goto _finalize_ret;
353 }
354 }
355
356 *layout = (struct flashrom_layout *)chip_layout;
357 ret = 0;
358
359_finalize_ret:
360 finalize_flash_access(flashctx);
361_free_ret:
362 if (ret)
363 free(chip_layout);
364 free(desc);
365 return ret;
366#endif
367}
368
Nico Huberee13d0c2019-06-07 17:47:40 +0200369#ifdef __FLASHROM_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100370static int flashrom_layout_parse_fmap(struct flashrom_layout **layout,
371 struct flashctx *const flashctx, const struct fmap *const fmap)
372{
373 int i;
374 struct flashrom_layout *l = get_global_layout();
375
376 if (!fmap || !l)
377 return 1;
378
379 if (l->num_entries + fmap->nareas > MAX_ROMLAYOUT) {
380 msg_gerr("Cannot add fmap entries to layout - Too many entries.\n");
381 return 1;
382 }
383
384 for (i = 0; i < fmap->nareas; i++) {
385 l->entries[l->num_entries].start = fmap->areas[i].offset;
386 l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1;
387 l->entries[l->num_entries].included = false;
Nico Huber70461a92019-06-15 14:56:19 +0200388 l->entries[l->num_entries].name =
389 strndup((const char *)fmap->areas[i].name, FMAP_STRLEN);
390 if (!l->entries[l->num_entries].name) {
391 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
392 return 1;
393 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100394 msg_gdbg("fmap %08x - %08x named %s\n",
395 l->entries[l->num_entries].start,
396 l->entries[l->num_entries].end,
397 l->entries[l->num_entries].name);
398 l->num_entries++;
399 }
400
401 *layout = l;
402 return 0;
403}
Nico Huberee13d0c2019-06-07 17:47:40 +0200404#endif /* __FLASHROM_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100405
406/**
407 * @brief Read a layout by searching the flash chip for fmap.
408 *
409 * @param[out] layout Points to a struct flashrom_layout pointer that
410 * gets set if the fmap is read and parsed successfully.
411 * @param[in] flashctx Flash context
412 * @param[in] offset Offset to begin searching for fmap.
413 * @param[in] offset Length of address space to search.
414 *
415 * @return 0 on success,
416 * 3 if fmap parsing isn't implemented for the host,
417 * 2 if the fmap couldn't be read,
418 * 1 on any other error.
419 */
420int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout,
421 struct flashctx *const flashctx, off_t offset, size_t len)
422{
423#ifndef __FLASHROM_LITTLE_ENDIAN__
424 return 3;
425#else
426 struct fmap *fmap = NULL;
427 int ret = 0;
428
429 msg_gdbg("Attempting to read fmap from ROM content.\n");
430 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
431 msg_gerr("Failed to read fmap from ROM.\n");
432 return 1;
433 }
434
435 msg_gdbg("Adding fmap layout to global layout.\n");
436 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
437 msg_gerr("Failed to add fmap regions to layout.\n");
438 ret = 1;
439 }
440
441 free(fmap);
442 return ret;
443#endif
444}
445
446/**
447 * @brief Read a layout by searching buffer for fmap.
448 *
449 * @param[out] layout Points to a struct flashrom_layout pointer that
450 * gets set if the fmap is read and parsed successfully.
451 * @param[in] flashctx Flash context
452 * @param[in] buffer Buffer to search in
453 * @param[in] size Size of buffer to search
454 *
455 * @return 0 on success,
456 * 3 if fmap parsing isn't implemented for the host,
457 * 2 if the fmap couldn't be read,
458 * 1 on any other error.
459 */
460int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout,
461 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
462{
463#ifndef __FLASHROM_LITTLE_ENDIAN__
464 return 3;
465#else
466 struct fmap *fmap = NULL;
467 int ret = 1;
468
469 if (!buf || !size)
470 goto _ret;
471
472 msg_gdbg("Attempting to read fmap from buffer.\n");
473 if (fmap_read_from_buffer(&fmap, buf, size)) {
474 msg_gerr("Failed to read fmap from buffer.\n");
475 goto _ret;
476 }
477
478 msg_gdbg("Adding fmap layout to global layout.\n");
479 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
480 msg_gerr("Failed to add fmap regions to layout.\n");
481 goto _free_ret;
482 }
483
484 ret = 0;
485_free_ret:
486 free(fmap);
487_ret:
488 return ret;
489#endif
490}
491
Nico Huber305f4172013-06-14 11:55:26 +0200492/**
Nico Huber454f6132012-12-10 13:34:10 +0000493 * @brief Set the active layout for a flash context.
494 *
495 * Note: This just sets a pointer. The caller must not release the layout
496 * as long as he uses it through the given flash context.
497 *
498 * @param flashctx Flash context whose layout will be set.
499 * @param layout Layout to bet set.
500 */
501void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
502{
503 flashctx->layout = layout;
504}
505
506/** @} */ /* end flashrom-layout */