blob: 34e881ad7b7f8ea69edfa8d15ae2a7d3c75dafcb [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
23#include <stdlib.h>
24#include <string.h>
25#include <stdarg.h>
26
27#include "flash.h"
28#include "programmer.h"
29#include "layout.h"
Nico Huber305f4172013-06-14 11:55:26 +020030#include "hwaccess.h"
31#include "ich_descriptors.h"
Nico Huber454f6132012-12-10 13:34:10 +000032#include "libflashrom.h"
33
34/**
35 * @defgroup flashrom-general General
36 * @{
37 */
38
39/** Pointer to log callback function. */
40static flashrom_log_callback *global_log_callback = NULL;
41
42/**
43 * @brief Initialize libflashrom.
44 *
45 * @param perform_selfcheck If not zero, perform a self check.
46 * @return 0 on success
47 */
48int flashrom_init(const int perform_selfcheck)
49{
50 if (perform_selfcheck && selfcheck())
51 return 1;
52 myusec_calibrate_delay();
53 return 0;
54}
55
56/**
57 * @brief Shut down libflashrom.
58 * @return 0 on success
59 */
60int flashrom_shutdown(void)
61{
62 return 0; /* TODO: nothing to do? */
63}
64
65/* TODO: flashrom_set_loglevel()? do we need it?
66 For now, let the user decide in his callback. */
67
68/**
69 * @brief Set the log callback function.
70 *
71 * Set a callback function which will be invoked whenever libflashrom wants
72 * to output messages. This allows frontends to do whatever they see fit with
73 * such messages, e.g. write them to syslog, or to file, or print them in a
74 * GUI window, etc.
75 *
76 * @param log_callback Pointer to the new log callback function.
77 */
78void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
79{
80 global_log_callback = log_callback;
81}
82/** @private */
Nico Huberd152fb92017-06-19 12:57:10 +020083int print(const enum flashrom_log_level level, const char *const fmt, ...)
Nico Huber454f6132012-12-10 13:34:10 +000084{
85 if (global_log_callback) {
86 int ret;
87 va_list args;
88 va_start(args, fmt);
Nico Huberd152fb92017-06-19 12:57:10 +020089 ret = global_log_callback(level, fmt, args);
Nico Huber454f6132012-12-10 13:34:10 +000090 va_end(args);
91 return ret;
92 }
93 return 0;
94}
95
96/** @} */ /* end flashrom-general */
97
98
99
100/**
101 * @defgroup flashrom-query Querying
102 * @{
103 */
104
105/* TBD */
106
107/** @} */ /* end flashrom-query */
108
109
110
111/**
112 * @defgroup flashrom-prog Programmers
113 * @{
114 */
115
116/**
117 * @brief Initialize the specified programmer.
118 *
119 * Currently, only one programmer may be initialized at a time.
120 *
121 * @param[out] flashprog Points to a pointer of type struct flashrom_programmer
122 * that will be set if programmer initialization succeeds.
123 * *flashprog has to be shutdown by the caller with @ref
124 * flashrom_programmer_shutdown.
125 * @param[in] prog_name Name of the programmer to initialize.
126 * @param[in] prog_param Pointer to programmer specific parameters.
127 * @return 0 on success
128 */
129int flashrom_programmer_init(struct flashrom_programmer **const flashprog,
130 const char *const prog_name, const char *const prog_param)
131{
132 unsigned prog;
133
134 for (prog = 0; prog < PROGRAMMER_INVALID; prog++) {
135 if (strcmp(prog_name, programmer_table[prog].name) == 0)
136 break;
137 }
138 if (prog >= PROGRAMMER_INVALID) {
139 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
140 list_programmers_linebreak(0, 80, 0);
141 return 1;
142 }
143 return programmer_init(prog, prog_param);
144}
145
146/**
147 * @brief Shut down the initialized programmer.
148 *
149 * @param flashprog The programmer to shut down.
150 * @return 0 on success
151 */
152int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)
153{
154 return programmer_shutdown();
155}
156
157/* TODO: flashrom_programmer_capabilities()? */
158
159/** @} */ /* end flashrom-prog */
160
161
162
163/**
164 * @defgroup flashrom-flash Flash chips
165 * @{
166 */
167
168/**
169 * @brief Probe for a flash chip.
170 *
171 * Probes for a flash chip and returns a flash context, that can be used
172 * later with flash chip and @ref flashrom-ops "image operations", if
173 * exactly one matching chip is found.
174 *
175 * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx
176 * that will be set if exactly one chip is found. *flashctx
177 * has to be freed by the caller with @ref flashrom_flash_release.
178 * @param[in] flashprog The flash programmer used to access the chip.
179 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
180 * all known chips.
181 * @return 0 on success,
182 * 3 if multiple chips were found,
183 * 2 if no chip was found,
184 * or 1 on any other error.
185 */
186int flashrom_flash_probe(struct flashrom_flashctx **const flashctx,
187 const struct flashrom_programmer *const flashprog,
188 const char *const chip_name)
189{
190 int i, ret = 2;
191 struct flashrom_flashctx second_flashctx = { 0, };
192
193 chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */
194
195 *flashctx = malloc(sizeof(**flashctx));
196 if (!*flashctx)
197 return 1;
198 memset(*flashctx, 0, sizeof(**flashctx));
199
200 for (i = 0; i < registered_master_count; ++i) {
201 int flash_idx = -1;
202 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
203 ret = 0;
204 /* We found one chip, now check that there is no second match. */
205 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
206 ret = 3;
207 break;
208 }
209 }
210 }
211 if (ret) {
212 free(*flashctx);
213 *flashctx = NULL;
214 }
215 return ret;
216}
217
218/**
219 * @brief Returns the size of the specified flash chip in bytes.
220 *
221 * @param flashctx The queried flash context.
222 * @return Size of flash chip in bytes.
223 */
224size_t flashrom_flash_getsize(const struct flashrom_flashctx *const flashctx)
225{
226 return flashctx->chip->total_size * 1024;
227}
228
229/**
230 * @brief Free a flash context.
231 *
232 * @param flashctx Flash context to free.
233 */
234void flashrom_flash_release(struct flashrom_flashctx *const flashctx)
235{
236 free(flashctx);
237}
238
239/**
240 * @brief Set a flag in the given flash context.
241 *
242 * @param flashctx Flash context to alter.
243 * @param flag Flag that is to be set / cleared.
244 * @param value Value to set.
245 */
246void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
247 const enum flashrom_flag flag, const bool value)
248{
249 switch (flag) {
250 case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
251 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
252 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
253 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
254 }
255}
256
257/**
258 * @brief Return the current value of a flag in the given flash context.
259 *
260 * @param flashctx Flash context to read from.
261 * @param flag Flag to be read.
262 * @return Current value of the flag.
263 */
264bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
265{
266 switch (flag) {
267 case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
268 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
269 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
270 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
271 default: return false;
272 }
273}
274
275/** @} */ /* end flashrom-flash */
276
277
278
279/**
280 * @defgroup flashrom-layout Layout handling
281 * @{
282 */
283
284/**
285 * @brief Mark given region as included.
286 *
287 * @param layout The layout to alter.
288 * @param name The name of the region to include.
289 *
290 * @return 0 on success,
291 * 1 if the given name can't be found.
292 */
293int flashrom_layout_include_region(struct flashrom_layout *const layout, const char *name)
294{
295 size_t i;
296 for (i = 0; i < layout->num_entries; ++i) {
297 if (!strcmp(layout->entries[i].name, name)) {
298 layout->entries[i].included = true;
299 return 0;
300 }
301 }
302 return 1;
303}
304
305/**
Nico Huber305f4172013-06-14 11:55:26 +0200306 * @brief Read a layout from the Intel ICH descriptor in the flash.
307 *
308 * Optionally verify that the layout matches the one in the given
309 * descriptor dump.
310 *
311 * @param[out] layout Points to a struct flashrom_layout pointer that
312 * gets set if the descriptor is read and parsed
313 * successfully.
314 * @param[in] flashctx Flash context to read the descriptor from flash.
315 * @param[in] dump The descriptor dump to compare to or NULL.
316 * @param[in] len The length of the descriptor dump.
317 *
318 * @return 0 on success,
319 * 6 if descriptor parsing isn't implemented for the host,
320 * 5 if the descriptors don't match,
321 * 4 if the descriptor dump couldn't be parsed,
322 * 3 if the descriptor on flash couldn't be parsed,
323 * 2 if the descriptor on flash couldn't be read,
324 * 1 on any other error.
325 */
326int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx,
327 const void *const dump, const size_t len)
328{
329#ifndef __FLASHROM_LITTLE_ENDIAN__
330 return 6;
331#else
332 struct ich_layout dump_layout;
333 int ret = 1;
334
335 void *const desc = malloc(0x1000);
336 struct ich_layout *const chip_layout = malloc(sizeof(*chip_layout));
337 if (!desc || !chip_layout) {
338 msg_gerr("Out of memory!\n");
339 goto _free_ret;
340 }
341
342 if (prepare_flash_access(flashctx, true, false, false, false))
343 goto _free_ret;
344
345 msg_cinfo("Reading ich descriptor... ");
346 if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) {
347 msg_cerr("Read operation failed!\n");
348 msg_cinfo("FAILED.\n");
349 ret = 2;
350 goto _finalize_ret;
351 }
352 msg_cinfo("done.\n");
353
354 if (layout_from_ich_descriptors(chip_layout, desc, 0x1000)) {
355 ret = 3;
356 goto _finalize_ret;
357 }
358
359 if (dump) {
360 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
361 ret = 4;
362 goto _finalize_ret;
363 }
364
365 if (chip_layout->base.num_entries != dump_layout.base.num_entries ||
366 memcmp(chip_layout->entries, dump_layout.entries, sizeof(dump_layout.entries))) {
367 ret = 5;
368 goto _finalize_ret;
369 }
370 }
371
372 *layout = (struct flashrom_layout *)chip_layout;
373 ret = 0;
374
375_finalize_ret:
376 finalize_flash_access(flashctx);
377_free_ret:
378 if (ret)
379 free(chip_layout);
380 free(desc);
381 return ret;
382#endif
383}
384
385/**
Nico Huber454f6132012-12-10 13:34:10 +0000386 * @brief Free a layout.
387 *
388 * @param layout Layout to free.
389 */
390void flashrom_layout_release(struct flashrom_layout *const layout)
391{
392 if (layout == get_global_layout())
393 return;
394
395 free(layout);
396}
397
398/**
399 * @brief Set the active layout for a flash context.
400 *
401 * Note: This just sets a pointer. The caller must not release the layout
402 * as long as he uses it through the given flash context.
403 *
404 * @param flashctx Flash context whose layout will be set.
405 * @param layout Layout to bet set.
406 */
407void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
408{
409 flashctx->layout = layout;
410}
411
412/** @} */ /* end flashrom-layout */