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