blob: 5c574faab63ed30718c715bd57b0c86ce840f538 [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 "ich_descriptors.h"
Nico Huber454f6132012-12-10 13:34:10 +000033#include "libflashrom.h"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110034#include "writeprotect.h"
Nico Huber454f6132012-12-10 13:34:10 +000035
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?
Angel Pons0be623c2021-04-17 17:08:44 +020068 For now, let the user decide in their callback. */
Nico Huber454f6132012-12-10 13:34:10 +000069
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
Nico Huberabfb70c2022-12-22 12:09:36 +0000107/* TBD */
Nico Huber454f6132012-12-10 13:34:10 +0000108
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
Thomas Heijligend45cb592021-05-19 14:12:18 +0200136 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +0200137 if (strcmp(prog_name, programmer_table[prog]->name) == 0)
Nico Huber454f6132012-12-10 13:34:10 +0000138 break;
139 }
Thomas Heijligend45cb592021-05-19 14:12:18 +0200140 if (prog >= programmer_table_size) {
Nico Huber454f6132012-12-10 13:34:10 +0000141 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
142 list_programmers_linebreak(0, 80, 0);
143 return 1;
144 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200145 return programmer_init(programmer_table[prog], prog_param);
Nico Huber454f6132012-12-10 13:34:10 +0000146}
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) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200208 flashrom_layout_release(second_flashctx.default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200209 free(second_flashctx.chip);
Nico Huber454f6132012-12-10 13:34:10 +0000210 ret = 3;
211 break;
212 }
213 }
214 }
215 if (ret) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200216 flashrom_flash_release(*flashctx);
Nico Huber454f6132012-12-10 13:34:10 +0000217 *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{
Nico Huber6fb2f622022-02-24 18:17:40 +0100240 if (!flashctx)
241 return;
242
Nico Huber5bd990c2019-06-16 19:46:46 +0200243 flashrom_layout_release(flashctx->default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200244 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000245 free(flashctx);
246}
247
248/**
249 * @brief Set a flag in the given flash context.
250 *
251 * @param flashctx Flash context to alter.
252 * @param flag Flag that is to be set / cleared.
253 * @param value Value to set.
254 */
255void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
256 const enum flashrom_flag flag, const bool value)
257{
258 switch (flag) {
259 case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
260 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
261 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
262 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
263 }
264}
265
266/**
267 * @brief Return the current value of a flag in the given flash context.
268 *
269 * @param flashctx Flash context to read from.
270 * @param flag Flag to be read.
271 * @return Current value of the flag.
272 */
273bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
274{
275 switch (flag) {
276 case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
277 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
278 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
279 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
280 default: return false;
281 }
282}
283
284/** @} */ /* end flashrom-flash */
285
286
287
288/**
289 * @defgroup flashrom-layout Layout handling
290 * @{
291 */
292
293/**
Nico Huber305f4172013-06-14 11:55:26 +0200294 * @brief Read a layout from the Intel ICH descriptor in the flash.
295 *
296 * Optionally verify that the layout matches the one in the given
297 * descriptor dump.
298 *
299 * @param[out] layout Points to a struct flashrom_layout pointer that
300 * gets set if the descriptor is read and parsed
301 * successfully.
302 * @param[in] flashctx Flash context to read the descriptor from flash.
303 * @param[in] dump The descriptor dump to compare to or NULL.
304 * @param[in] len The length of the descriptor dump.
305 *
306 * @return 0 on success,
307 * 6 if descriptor parsing isn't implemented for the host,
308 * 5 if the descriptors don't match,
309 * 4 if the descriptor dump couldn't be parsed,
310 * 3 if the descriptor on flash couldn't be parsed,
311 * 2 if the descriptor on flash couldn't be read,
312 * 1 on any other error.
313 */
314int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx,
315 const void *const dump, const size_t len)
316{
317#ifndef __FLASHROM_LITTLE_ENDIAN__
318 return 6;
319#else
aaryad1dacf72022-03-10 08:47:48 +0530320 struct flashrom_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200321 int ret = 1;
322
323 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200324 if (prepare_flash_access(flashctx, true, false, false, false))
325 goto _free_ret;
326
327 msg_cinfo("Reading ich descriptor... ");
328 if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) {
329 msg_cerr("Read operation failed!\n");
330 msg_cinfo("FAILED.\n");
331 ret = 2;
332 goto _finalize_ret;
333 }
334 msg_cinfo("done.\n");
335
Nico Huber5bd990c2019-06-16 19:46:46 +0200336 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200337 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200338 ret = 3;
339 goto _finalize_ret;
340 }
341
342 if (dump) {
343 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200344 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200345 ret = 4;
346 goto _finalize_ret;
347 }
348
Nico Huber5bd990c2019-06-16 19:46:46 +0200349 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
350 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huber354766b2019-06-16 19:28:35 +0200351 while (chip_entry && dump_entry && !memcmp(chip_entry, dump_entry, sizeof(*chip_entry))) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200352 chip_entry = layout_next(chip_layout, chip_entry);
353 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200354 }
Nico Huber5bd990c2019-06-16 19:46:46 +0200355 flashrom_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200356 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200357 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200358 ret = 5;
359 goto _finalize_ret;
360 }
361 }
362
363 *layout = (struct flashrom_layout *)chip_layout;
364 ret = 0;
365
366_finalize_ret:
367 finalize_flash_access(flashctx);
368_free_ret:
369 if (ret)
Nico Huber5bd990c2019-06-16 19:46:46 +0200370 flashrom_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200371 free(desc);
372 return ret;
373#endif
374}
375
Nico Huberee13d0c2019-06-07 17:47:40 +0200376#ifdef __FLASHROM_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100377static int flashrom_layout_parse_fmap(struct flashrom_layout **layout,
378 struct flashctx *const flashctx, const struct fmap *const fmap)
379{
380 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200381 char name[FMAP_STRLEN + 1];
382 const struct fmap_area *area;
Nico Huberefe96a92021-05-14 00:39:24 +0200383 struct flashrom_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100384
Nico Huberefe96a92021-05-14 00:39:24 +0200385 if (!fmap || flashrom_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100386 return 1;
387
Nico Huber92e0b622019-06-15 15:55:11 +0200388 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
389 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberefe96a92021-05-14 00:39:24 +0200390 if (flashrom_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
391 flashrom_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200392 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200393 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100394 }
395
396 *layout = l;
397 return 0;
398}
Nico Huberee13d0c2019-06-07 17:47:40 +0200399#endif /* __FLASHROM_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100400
401/**
402 * @brief Read a layout by searching the flash chip for fmap.
403 *
404 * @param[out] layout Points to a struct flashrom_layout pointer that
405 * gets set if the fmap is read and parsed successfully.
406 * @param[in] flashctx Flash context
407 * @param[in] offset Offset to begin searching for fmap.
408 * @param[in] offset Length of address space to search.
409 *
410 * @return 0 on success,
411 * 3 if fmap parsing isn't implemented for the host,
412 * 2 if the fmap couldn't be read,
413 * 1 on any other error.
414 */
415int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800416 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100417{
418#ifndef __FLASHROM_LITTLE_ENDIAN__
419 return 3;
420#else
421 struct fmap *fmap = NULL;
422 int ret = 0;
423
424 msg_gdbg("Attempting to read fmap from ROM content.\n");
425 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
426 msg_gerr("Failed to read fmap from ROM.\n");
427 return 1;
428 }
429
430 msg_gdbg("Adding fmap layout to global layout.\n");
431 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
432 msg_gerr("Failed to add fmap regions to layout.\n");
433 ret = 1;
434 }
435
436 free(fmap);
437 return ret;
438#endif
439}
440
441/**
442 * @brief Read a layout by searching buffer for fmap.
443 *
444 * @param[out] layout Points to a struct flashrom_layout pointer that
445 * gets set if the fmap is read and parsed successfully.
446 * @param[in] flashctx Flash context
447 * @param[in] buffer Buffer to search in
448 * @param[in] size Size of buffer to search
449 *
450 * @return 0 on success,
451 * 3 if fmap parsing isn't implemented for the host,
452 * 2 if the fmap couldn't be read,
453 * 1 on any other error.
454 */
455int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout,
456 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
457{
458#ifndef __FLASHROM_LITTLE_ENDIAN__
459 return 3;
460#else
461 struct fmap *fmap = NULL;
462 int ret = 1;
463
464 if (!buf || !size)
465 goto _ret;
466
467 msg_gdbg("Attempting to read fmap from buffer.\n");
468 if (fmap_read_from_buffer(&fmap, buf, size)) {
469 msg_gerr("Failed to read fmap from buffer.\n");
470 goto _ret;
471 }
472
473 msg_gdbg("Adding fmap layout to global layout.\n");
474 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
475 msg_gerr("Failed to add fmap regions to layout.\n");
476 goto _free_ret;
477 }
478
479 ret = 0;
480_free_ret:
481 free(fmap);
482_ret:
483 return ret;
484#endif
485}
486
Nico Huber305f4172013-06-14 11:55:26 +0200487/**
Nico Huber454f6132012-12-10 13:34:10 +0000488 * @brief Set the active layout for a flash context.
489 *
490 * Note: This just sets a pointer. The caller must not release the layout
491 * as long as he uses it through the given flash context.
492 *
493 * @param flashctx Flash context whose layout will be set.
494 * @param layout Layout to bet set.
495 */
496void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
497{
498 flashctx->layout = layout;
499}
500
501/** @} */ /* end flashrom-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100502
503
504/**
505 * @defgroup flashrom-wp
506 * @{
507 */
508
509/**
510 * @brief Create a new empty WP configuration.
511 *
512 * @param[out] cfg Points to a pointer of type struct flashrom_wp_cfg that will
513 * be set if creation succeeds. *cfg has to be freed by the
514 * caller with @ref flashrom_wp_cfg_release.
515 * @return 0 on success
516 * >0 on failure
517 */
518enum flashrom_wp_result flashrom_wp_cfg_new(struct flashrom_wp_cfg **cfg)
519{
520 *cfg = calloc(1, sizeof(**cfg));
521 return *cfg ? 0 : FLASHROM_WP_ERR_OTHER;
522}
523
524/**
525 * @brief Free a WP configuration.
526 *
527 * @param[out] cfg Pointer to the flashrom_wp_cfg to free.
528 */
529void flashrom_wp_cfg_release(struct flashrom_wp_cfg *cfg)
530{
531 free(cfg);
532}
533
534/**
535 * @brief Set the protection mode for a WP configuration.
536 *
537 * @param[in] mode The protection mode to set.
538 * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify.
539 */
540void flashrom_wp_set_mode(struct flashrom_wp_cfg *cfg, enum flashrom_wp_mode mode)
541{
542 cfg->mode = mode;
543}
544
545/**
546 * @brief Get the protection mode from a WP configuration.
547 *
548 * @param[in] cfg The WP configuration to get the protection mode from.
549 * @return The configuration's protection mode.
550 */
551enum flashrom_wp_mode flashrom_wp_get_mode(const struct flashrom_wp_cfg *cfg)
552{
553 return cfg->mode;
554}
555
556/**
557 * @brief Set the protection range for a WP configuration.
558 *
559 * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify.
560 * @param[in] start The range's start address.
561 * @param[in] len The range's length.
562 */
563void flashrom_wp_set_range(struct flashrom_wp_cfg *cfg, size_t start, size_t len)
564{
565 cfg->range.start = start;
566 cfg->range.len = len;
567}
568
569/**
570 * @brief Get the protection range from a WP configuration.
571 *
572 * @param[out] start Points to a size_t to write the range start to.
573 * @param[out] len Points to a size_t to write the range length to.
574 * @param[in] cfg The WP configuration to get the range from.
575 */
576void flashrom_wp_get_range(size_t *start, size_t *len, const struct flashrom_wp_cfg *cfg)
577{
578 *start = cfg->range.start;
579 *len = cfg->range.len;
580}
581
582/**
583 * @brief Write a WP configuration to a flash chip.
584 *
585 * @param[in] flash The flash context used to access the chip.
586 * @param[in] cfg The WP configuration to write to the chip.
587 * @return 0 on success
588 * >0 on failure
589 */
590enum flashrom_wp_result flashrom_wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
591{
592 /*
593 * TODO: Call custom implementation if the programmer is opaque, as
594 * direct WP operations require SPI access. In particular, linux_mtd
595 * has its own WP operations we should use instead.
596 */
597 if (flash->mst->buses_supported & BUS_SPI)
598 return wp_write_cfg(flash, cfg);
599
600 return FLASHROM_WP_ERR_OTHER;
601}
602
603/**
604 * @brief Read the current WP configuration from a flash chip.
605 *
606 * @param[out] cfg Pointer to a struct flashrom_wp_cfg to store the chip's
607 * configuration in.
608 * @param[in] flash The flash context used to access the chip.
609 * @return 0 on success
610 * >0 on failure
611 */
612enum flashrom_wp_result flashrom_wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
613{
614 /*
615 * TODO: Call custom implementation if the programmer is opaque, as
616 * direct WP operations require SPI access. In particular, linux_mtd
617 * has its own WP operations we should use instead.
618 */
619 if (flash->mst->buses_supported & BUS_SPI)
620 return wp_read_cfg(cfg, flash);
621
622 return FLASHROM_WP_ERR_OTHER;
623}
624
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100625/**
626 * @brief Get a list of protection ranges supported by the flash chip.
627 *
628 * @param[out] ranges Points to a pointer of type struct flashrom_wp_ranges
629 * that will be set if available ranges are found. Finding
630 * available ranges may not always be possible, even if the
631 * chip's protection range can be read or modified. *ranges
632 * must be freed using @ref flashrom_wp_ranges_free.
633 * @param[in] flash The flash context used to access the chip.
634 * @return 0 on success
635 * >0 on failure
636 */
637enum flashrom_wp_result flashrom_wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
638{
639 /*
640 * TODO: Call custom implementation if the programmer is opaque, as
641 * direct WP operations require SPI access. We actually can't implement
642 * this in linux_mtd right now, but we should adopt a proper generic
643 * architechure to match the read and write functions anyway.
644 */
645 if (flash->mst->buses_supported & BUS_SPI)
646 return wp_get_available_ranges(list, flash);
647
648 return FLASHROM_WP_ERR_OTHER;
649}
650
651/**
652 * @brief Get a number of protection ranges in a range list.
653 *
654 * @param[in] ranges The range list to get the count from.
655 * @return Number of ranges in the list.
656 */
657size_t flashrom_wp_ranges_get_count(const struct flashrom_wp_ranges *list)
658{
659 return list->count;
660}
661
662/**
663 * @brief Get a protection range from a range list.
664 *
665 * @param[out] start Points to a size_t to write the range's start to.
666 * @param[out] len Points to a size_t to write the range's length to.
667 * @param[in] ranges The range list to get the range from.
668 * @param[in] index Index of the range to get.
669 * @return 0 on success
670 * >0 on failure
671 */
672enum flashrom_wp_result flashrom_wp_ranges_get_range(size_t *start, size_t *len, const struct flashrom_wp_ranges *list, unsigned int index)
673{
674 if (index >= list->count)
675 return FLASHROM_WP_ERR_OTHER;
676
677 *start = list->ranges[index].start;
678 *len = list->ranges[index].len;
679
680 return 0;
681}
682
683/**
684 * @brief Free a WP range list.
685 *
686 * @param[out] cfg Pointer to the flashrom_wp_ranges to free.
687 */
688void flashrom_wp_ranges_release(struct flashrom_wp_ranges *list)
689{
690 if (!list)
691 return;
692
693 free(list->ranges);
694 free(list);
695}
696
697
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100698/** @} */ /* end flashrom-wp */