blob: e390a029495529416b06cacc0e91decd8cc134c3 [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"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110035#include "writeprotect.h"
Nico Huber454f6132012-12-10 13:34:10 +000036
37/**
38 * @defgroup flashrom-general General
39 * @{
40 */
41
42/** Pointer to log callback function. */
43static flashrom_log_callback *global_log_callback = NULL;
44
45/**
46 * @brief Initialize libflashrom.
47 *
48 * @param perform_selfcheck If not zero, perform a self check.
49 * @return 0 on success
50 */
51int flashrom_init(const int perform_selfcheck)
52{
53 if (perform_selfcheck && selfcheck())
54 return 1;
55 myusec_calibrate_delay();
56 return 0;
57}
58
59/**
60 * @brief Shut down libflashrom.
61 * @return 0 on success
62 */
63int flashrom_shutdown(void)
64{
65 return 0; /* TODO: nothing to do? */
66}
67
68/* TODO: flashrom_set_loglevel()? do we need it?
Angel Pons0be623c2021-04-17 17:08:44 +020069 For now, let the user decide in their callback. */
Nico Huber454f6132012-12-10 13:34:10 +000070
71/**
72 * @brief Set the log callback function.
73 *
74 * Set a callback function which will be invoked whenever libflashrom wants
75 * to output messages. This allows frontends to do whatever they see fit with
76 * such messages, e.g. write them to syslog, or to file, or print them in a
77 * GUI window, etc.
78 *
79 * @param log_callback Pointer to the new log callback function.
80 */
81void flashrom_set_log_callback(flashrom_log_callback *const log_callback)
82{
83 global_log_callback = log_callback;
84}
85/** @private */
Nico Huberd152fb92017-06-19 12:57:10 +020086int print(const enum flashrom_log_level level, const char *const fmt, ...)
Nico Huber454f6132012-12-10 13:34:10 +000087{
88 if (global_log_callback) {
89 int ret;
90 va_list args;
91 va_start(args, fmt);
Nico Huberd152fb92017-06-19 12:57:10 +020092 ret = global_log_callback(level, fmt, args);
Nico Huber454f6132012-12-10 13:34:10 +000093 va_end(args);
94 return ret;
95 }
96 return 0;
97}
98
99/** @} */ /* end flashrom-general */
100
101
102
103/**
104 * @defgroup flashrom-query Querying
105 * @{
106 */
107
Nico Huberabfb70c2022-12-22 12:09:36 +0000108/* TBD */
Nico Huber454f6132012-12-10 13:34:10 +0000109
110/** @} */ /* end flashrom-query */
111
112
113
114/**
115 * @defgroup flashrom-prog Programmers
116 * @{
117 */
118
119/**
120 * @brief Initialize the specified programmer.
121 *
122 * Currently, only one programmer may be initialized at a time.
123 *
124 * @param[out] flashprog Points to a pointer of type struct flashrom_programmer
125 * that will be set if programmer initialization succeeds.
126 * *flashprog has to be shutdown by the caller with @ref
127 * flashrom_programmer_shutdown.
128 * @param[in] prog_name Name of the programmer to initialize.
129 * @param[in] prog_param Pointer to programmer specific parameters.
130 * @return 0 on success
131 */
132int flashrom_programmer_init(struct flashrom_programmer **const flashprog,
133 const char *const prog_name, const char *const prog_param)
134{
135 unsigned prog;
136
Thomas Heijligend45cb592021-05-19 14:12:18 +0200137 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +0200138 if (strcmp(prog_name, programmer_table[prog]->name) == 0)
Nico Huber454f6132012-12-10 13:34:10 +0000139 break;
140 }
Thomas Heijligend45cb592021-05-19 14:12:18 +0200141 if (prog >= programmer_table_size) {
Nico Huber454f6132012-12-10 13:34:10 +0000142 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
143 list_programmers_linebreak(0, 80, 0);
144 return 1;
145 }
Thomas Heijligene0e93cf2021-06-01 14:37:12 +0200146 return programmer_init(programmer_table[prog], prog_param);
Nico Huber454f6132012-12-10 13:34:10 +0000147}
148
149/**
150 * @brief Shut down the initialized programmer.
151 *
152 * @param flashprog The programmer to shut down.
153 * @return 0 on success
154 */
155int flashrom_programmer_shutdown(struct flashrom_programmer *const flashprog)
156{
157 return programmer_shutdown();
158}
159
160/* TODO: flashrom_programmer_capabilities()? */
161
162/** @} */ /* end flashrom-prog */
163
164
165
166/**
167 * @defgroup flashrom-flash Flash chips
168 * @{
169 */
170
171/**
172 * @brief Probe for a flash chip.
173 *
174 * Probes for a flash chip and returns a flash context, that can be used
175 * later with flash chip and @ref flashrom-ops "image operations", if
176 * exactly one matching chip is found.
177 *
178 * @param[out] flashctx Points to a pointer of type struct flashrom_flashctx
179 * that will be set if exactly one chip is found. *flashctx
180 * has to be freed by the caller with @ref flashrom_flash_release.
181 * @param[in] flashprog The flash programmer used to access the chip.
182 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
183 * all known chips.
184 * @return 0 on success,
185 * 3 if multiple chips were found,
186 * 2 if no chip was found,
187 * or 1 on any other error.
188 */
189int flashrom_flash_probe(struct flashrom_flashctx **const flashctx,
190 const struct flashrom_programmer *const flashprog,
191 const char *const chip_name)
192{
193 int i, ret = 2;
194 struct flashrom_flashctx second_flashctx = { 0, };
195
196 chip_to_probe = chip_name; /* chip_to_probe is global in flashrom.c */
197
198 *flashctx = malloc(sizeof(**flashctx));
199 if (!*flashctx)
200 return 1;
201 memset(*flashctx, 0, sizeof(**flashctx));
202
203 for (i = 0; i < registered_master_count; ++i) {
204 int flash_idx = -1;
205 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
206 ret = 0;
207 /* We found one chip, now check that there is no second match. */
208 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
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) {
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{
Nico Huber6fb2f622022-02-24 18:17:40 +0100240 if (!flashctx)
241 return;
242
Nico Huber38450ce2019-06-16 20:07:28 +0200243 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000244 free(flashctx);
245}
246
247/**
248 * @brief Set a flag in the given flash context.
249 *
250 * @param flashctx Flash context to alter.
251 * @param flag Flag that is to be set / cleared.
252 * @param value Value to set.
253 */
254void flashrom_flag_set(struct flashrom_flashctx *const flashctx,
255 const enum flashrom_flag flag, const bool value)
256{
257 switch (flag) {
258 case FLASHROM_FLAG_FORCE: flashctx->flags.force = value; break;
259 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
260 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
261 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
262 }
263}
264
265/**
266 * @brief Return the current value of a flag in the given flash context.
267 *
268 * @param flashctx Flash context to read from.
269 * @param flag Flag to be read.
270 * @return Current value of the flag.
271 */
272bool flashrom_flag_get(const struct flashrom_flashctx *const flashctx, const enum flashrom_flag flag)
273{
274 switch (flag) {
275 case FLASHROM_FLAG_FORCE: return flashctx->flags.force;
276 case FLASHROM_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
277 case FLASHROM_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
278 case FLASHROM_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
279 default: return false;
280 }
281}
282
283/** @} */ /* end flashrom-flash */
284
285
286
287/**
288 * @defgroup flashrom-layout Layout handling
289 * @{
290 */
291
292/**
Nico Huber305f4172013-06-14 11:55:26 +0200293 * @brief Read a layout from the Intel ICH descriptor in the flash.
294 *
295 * Optionally verify that the layout matches the one in the given
296 * descriptor dump.
297 *
298 * @param[out] layout Points to a struct flashrom_layout pointer that
299 * gets set if the descriptor is read and parsed
300 * successfully.
301 * @param[in] flashctx Flash context to read the descriptor from flash.
302 * @param[in] dump The descriptor dump to compare to or NULL.
303 * @param[in] len The length of the descriptor dump.
304 *
305 * @return 0 on success,
306 * 6 if descriptor parsing isn't implemented for the host,
307 * 5 if the descriptors don't match,
308 * 4 if the descriptor dump couldn't be parsed,
309 * 3 if the descriptor on flash couldn't be parsed,
310 * 2 if the descriptor on flash couldn't be read,
311 * 1 on any other error.
312 */
313int flashrom_layout_read_from_ifd(struct flashrom_layout **const layout, struct flashctx *const flashctx,
314 const void *const dump, const size_t len)
315{
316#ifndef __FLASHROM_LITTLE_ENDIAN__
317 return 6;
318#else
319 struct ich_layout dump_layout;
320 int ret = 1;
321
322 void *const desc = malloc(0x1000);
323 struct ich_layout *const chip_layout = malloc(sizeof(*chip_layout));
324 if (!desc || !chip_layout) {
325 msg_gerr("Out of memory!\n");
326 goto _free_ret;
327 }
328
329 if (prepare_flash_access(flashctx, true, false, false, false))
330 goto _free_ret;
331
332 msg_cinfo("Reading ich descriptor... ");
333 if (flashctx->chip->read(flashctx, desc, 0, 0x1000)) {
334 msg_cerr("Read operation failed!\n");
335 msg_cinfo("FAILED.\n");
336 ret = 2;
337 goto _finalize_ret;
338 }
339 msg_cinfo("done.\n");
340
341 if (layout_from_ich_descriptors(chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200342 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200343 ret = 3;
344 goto _finalize_ret;
345 }
346
347 if (dump) {
348 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200349 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200350 ret = 4;
351 goto _finalize_ret;
352 }
353
Nico Huber354766b2019-06-16 19:28:35 +0200354 const struct romentry *chip_entry = layout_next(&chip_layout->base, NULL);
355 const struct romentry *dump_entry = layout_next(&dump_layout.base, NULL);
356 while (chip_entry && dump_entry && !memcmp(chip_entry, dump_entry, sizeof(*chip_entry))) {
357 chip_entry = layout_next(&chip_layout->base, chip_entry);
358 dump_entry = layout_next(&dump_layout.base, dump_entry);
359 }
360 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200361 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200362 ret = 5;
363 goto _finalize_ret;
364 }
365 }
366
367 *layout = (struct flashrom_layout *)chip_layout;
368 ret = 0;
369
370_finalize_ret:
371 finalize_flash_access(flashctx);
372_free_ret:
373 if (ret)
374 free(chip_layout);
375 free(desc);
376 return ret;
377#endif
378}
379
Nico Huberee13d0c2019-06-07 17:47:40 +0200380#ifdef __FLASHROM_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100381static int flashrom_layout_parse_fmap(struct flashrom_layout **layout,
382 struct flashctx *const flashctx, const struct fmap *const fmap)
383{
384 int i;
385 struct flashrom_layout *l = get_global_layout();
386
387 if (!fmap || !l)
388 return 1;
389
390 if (l->num_entries + fmap->nareas > MAX_ROMLAYOUT) {
391 msg_gerr("Cannot add fmap entries to layout - Too many entries.\n");
392 return 1;
393 }
394
395 for (i = 0; i < fmap->nareas; i++) {
396 l->entries[l->num_entries].start = fmap->areas[i].offset;
397 l->entries[l->num_entries].end = fmap->areas[i].offset + fmap->areas[i].size - 1;
398 l->entries[l->num_entries].included = false;
Nico Huber70461a92019-06-15 14:56:19 +0200399 l->entries[l->num_entries].name =
400 strndup((const char *)fmap->areas[i].name, FMAP_STRLEN);
401 if (!l->entries[l->num_entries].name) {
402 msg_gerr("Error adding layout entry: %s\n", strerror(errno));
403 return 1;
404 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100405 msg_gdbg("fmap %08x - %08x named %s\n",
406 l->entries[l->num_entries].start,
407 l->entries[l->num_entries].end,
408 l->entries[l->num_entries].name);
409 l->num_entries++;
410 }
411
412 *layout = l;
413 return 0;
414}
Nico Huberee13d0c2019-06-07 17:47:40 +0200415#endif /* __FLASHROM_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100416
417/**
418 * @brief Read a layout by searching the flash chip for fmap.
419 *
420 * @param[out] layout Points to a struct flashrom_layout pointer that
421 * gets set if the fmap is read and parsed successfully.
422 * @param[in] flashctx Flash context
423 * @param[in] offset Offset to begin searching for fmap.
424 * @param[in] offset Length of address space to search.
425 *
426 * @return 0 on success,
427 * 3 if fmap parsing isn't implemented for the host,
428 * 2 if the fmap couldn't be read,
429 * 1 on any other error.
430 */
431int flashrom_layout_read_fmap_from_rom(struct flashrom_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800432 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100433{
434#ifndef __FLASHROM_LITTLE_ENDIAN__
435 return 3;
436#else
437 struct fmap *fmap = NULL;
438 int ret = 0;
439
440 msg_gdbg("Attempting to read fmap from ROM content.\n");
441 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
442 msg_gerr("Failed to read fmap from ROM.\n");
443 return 1;
444 }
445
446 msg_gdbg("Adding fmap layout to global layout.\n");
447 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
448 msg_gerr("Failed to add fmap regions to layout.\n");
449 ret = 1;
450 }
451
452 free(fmap);
453 return ret;
454#endif
455}
456
457/**
458 * @brief Read a layout by searching buffer for fmap.
459 *
460 * @param[out] layout Points to a struct flashrom_layout pointer that
461 * gets set if the fmap is read and parsed successfully.
462 * @param[in] flashctx Flash context
463 * @param[in] buffer Buffer to search in
464 * @param[in] size Size of buffer to search
465 *
466 * @return 0 on success,
467 * 3 if fmap parsing isn't implemented for the host,
468 * 2 if the fmap couldn't be read,
469 * 1 on any other error.
470 */
471int flashrom_layout_read_fmap_from_buffer(struct flashrom_layout **const layout,
472 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
473{
474#ifndef __FLASHROM_LITTLE_ENDIAN__
475 return 3;
476#else
477 struct fmap *fmap = NULL;
478 int ret = 1;
479
480 if (!buf || !size)
481 goto _ret;
482
483 msg_gdbg("Attempting to read fmap from buffer.\n");
484 if (fmap_read_from_buffer(&fmap, buf, size)) {
485 msg_gerr("Failed to read fmap from buffer.\n");
486 goto _ret;
487 }
488
489 msg_gdbg("Adding fmap layout to global layout.\n");
490 if (flashrom_layout_parse_fmap(layout, flashctx, fmap)) {
491 msg_gerr("Failed to add fmap regions to layout.\n");
492 goto _free_ret;
493 }
494
495 ret = 0;
496_free_ret:
497 free(fmap);
498_ret:
499 return ret;
500#endif
501}
502
Nico Huber305f4172013-06-14 11:55:26 +0200503/**
Nico Huber454f6132012-12-10 13:34:10 +0000504 * @brief Set the active layout for a flash context.
505 *
506 * Note: This just sets a pointer. The caller must not release the layout
507 * as long as he uses it through the given flash context.
508 *
509 * @param flashctx Flash context whose layout will be set.
510 * @param layout Layout to bet set.
511 */
512void flashrom_layout_set(struct flashrom_flashctx *const flashctx, const struct flashrom_layout *const layout)
513{
514 flashctx->layout = layout;
515}
516
517/** @} */ /* end flashrom-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100518
519
520/**
521 * @defgroup flashrom-wp
522 * @{
523 */
524
525/**
526 * @brief Create a new empty WP configuration.
527 *
528 * @param[out] cfg Points to a pointer of type struct flashrom_wp_cfg that will
529 * be set if creation succeeds. *cfg has to be freed by the
530 * caller with @ref flashrom_wp_cfg_release.
531 * @return 0 on success
532 * >0 on failure
533 */
534enum flashrom_wp_result flashrom_wp_cfg_new(struct flashrom_wp_cfg **cfg)
535{
536 *cfg = calloc(1, sizeof(**cfg));
537 return *cfg ? 0 : FLASHROM_WP_ERR_OTHER;
538}
539
540/**
541 * @brief Free a WP configuration.
542 *
543 * @param[out] cfg Pointer to the flashrom_wp_cfg to free.
544 */
545void flashrom_wp_cfg_release(struct flashrom_wp_cfg *cfg)
546{
547 free(cfg);
548}
549
550/**
551 * @brief Set the protection mode for a WP configuration.
552 *
553 * @param[in] mode The protection mode to set.
554 * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify.
555 */
556void flashrom_wp_set_mode(struct flashrom_wp_cfg *cfg, enum flashrom_wp_mode mode)
557{
558 cfg->mode = mode;
559}
560
561/**
562 * @brief Get the protection mode from a WP configuration.
563 *
564 * @param[in] cfg The WP configuration to get the protection mode from.
565 * @return The configuration's protection mode.
566 */
567enum flashrom_wp_mode flashrom_wp_get_mode(const struct flashrom_wp_cfg *cfg)
568{
569 return cfg->mode;
570}
571
572/**
573 * @brief Set the protection range for a WP configuration.
574 *
575 * @param[out] cfg Pointer to the flashrom_wp_cfg structure to modify.
576 * @param[in] start The range's start address.
577 * @param[in] len The range's length.
578 */
579void flashrom_wp_set_range(struct flashrom_wp_cfg *cfg, size_t start, size_t len)
580{
581 cfg->range.start = start;
582 cfg->range.len = len;
583}
584
585/**
586 * @brief Get the protection range from a WP configuration.
587 *
588 * @param[out] start Points to a size_t to write the range start to.
589 * @param[out] len Points to a size_t to write the range length to.
590 * @param[in] cfg The WP configuration to get the range from.
591 */
592void flashrom_wp_get_range(size_t *start, size_t *len, const struct flashrom_wp_cfg *cfg)
593{
594 *start = cfg->range.start;
595 *len = cfg->range.len;
596}
597
598/**
599 * @brief Write a WP configuration to a flash chip.
600 *
601 * @param[in] flash The flash context used to access the chip.
602 * @param[in] cfg The WP configuration to write to the chip.
603 * @return 0 on success
604 * >0 on failure
605 */
606enum flashrom_wp_result flashrom_wp_write_cfg(struct flashctx *flash, const struct flashrom_wp_cfg *cfg)
607{
608 /*
609 * TODO: Call custom implementation if the programmer is opaque, as
610 * direct WP operations require SPI access. In particular, linux_mtd
611 * has its own WP operations we should use instead.
612 */
613 if (flash->mst->buses_supported & BUS_SPI)
614 return wp_write_cfg(flash, cfg);
615
616 return FLASHROM_WP_ERR_OTHER;
617}
618
619/**
620 * @brief Read the current WP configuration from a flash chip.
621 *
622 * @param[out] cfg Pointer to a struct flashrom_wp_cfg to store the chip's
623 * configuration in.
624 * @param[in] flash The flash context used to access the chip.
625 * @return 0 on success
626 * >0 on failure
627 */
628enum flashrom_wp_result flashrom_wp_read_cfg(struct flashrom_wp_cfg *cfg, struct flashctx *flash)
629{
630 /*
631 * TODO: Call custom implementation if the programmer is opaque, as
632 * direct WP operations require SPI access. In particular, linux_mtd
633 * has its own WP operations we should use instead.
634 */
635 if (flash->mst->buses_supported & BUS_SPI)
636 return wp_read_cfg(cfg, flash);
637
638 return FLASHROM_WP_ERR_OTHER;
639}
640
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100641/**
642 * @brief Get a list of protection ranges supported by the flash chip.
643 *
644 * @param[out] ranges Points to a pointer of type struct flashrom_wp_ranges
645 * that will be set if available ranges are found. Finding
646 * available ranges may not always be possible, even if the
647 * chip's protection range can be read or modified. *ranges
648 * must be freed using @ref flashrom_wp_ranges_free.
649 * @param[in] flash The flash context used to access the chip.
650 * @return 0 on success
651 * >0 on failure
652 */
653enum flashrom_wp_result flashrom_wp_get_available_ranges(struct flashrom_wp_ranges **list, struct flashrom_flashctx *flash)
654{
655 /*
656 * TODO: Call custom implementation if the programmer is opaque, as
657 * direct WP operations require SPI access. We actually can't implement
658 * this in linux_mtd right now, but we should adopt a proper generic
659 * architechure to match the read and write functions anyway.
660 */
661 if (flash->mst->buses_supported & BUS_SPI)
662 return wp_get_available_ranges(list, flash);
663
664 return FLASHROM_WP_ERR_OTHER;
665}
666
667/**
668 * @brief Get a number of protection ranges in a range list.
669 *
670 * @param[in] ranges The range list to get the count from.
671 * @return Number of ranges in the list.
672 */
673size_t flashrom_wp_ranges_get_count(const struct flashrom_wp_ranges *list)
674{
675 return list->count;
676}
677
678/**
679 * @brief Get a protection range from a range list.
680 *
681 * @param[out] start Points to a size_t to write the range's start to.
682 * @param[out] len Points to a size_t to write the range's length to.
683 * @param[in] ranges The range list to get the range from.
684 * @param[in] index Index of the range to get.
685 * @return 0 on success
686 * >0 on failure
687 */
688enum flashrom_wp_result flashrom_wp_ranges_get_range(size_t *start, size_t *len, const struct flashrom_wp_ranges *list, unsigned int index)
689{
690 if (index >= list->count)
691 return FLASHROM_WP_ERR_OTHER;
692
693 *start = list->ranges[index].start;
694 *len = list->ranges[index].len;
695
696 return 0;
697}
698
699/**
700 * @brief Free a WP range list.
701 *
702 * @param[out] cfg Pointer to the flashrom_wp_ranges to free.
703 */
704void flashrom_wp_ranges_release(struct flashrom_wp_ranges *list)
705{
706 if (!list)
707 return;
708
709 free(list->ranges);
710 free(list);
711}
712
713
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100714/** @} */ /* end flashrom-wp */