blob: 24885a3e5a09e5f664bb8d91184603fadcc3b343 [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 *
Nico Huberd4926fe2026-03-08 21:37:52 +01007 * Copyright (C) 2026 Nico Huber <nico.h@gmx.de>
8 *
Nico Huber454f6132012-12-10 13:34:10 +00009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
Nico Huber454f6132012-12-10 13:34:10 +000018 */
19/**
20 * @mainpage
21 *
22 * Have a look at the Modules section for a function reference.
23 */
24
Nico Huber70461a92019-06-15 14:56:19 +020025#include <errno.h>
Nico Huber454f6132012-12-10 13:34:10 +000026#include <stdlib.h>
27#include <string.h>
28#include <stdarg.h>
29
Nico Huberd4926fe2026-03-08 21:37:52 +010030#define flashprog_chip flashchip /* For now, we use direct pointers */
31#include "libflashprog.h" /* to the internal struct flashchip. */
32
Nico Huber454f6132012-12-10 13:34:10 +000033#include "flash.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010034#include "fmap.h"
Nico Huber454f6132012-12-10 13:34:10 +000035#include "programmer.h"
36#include "layout.h"
Nico Huberd4926fe2026-03-08 21:37:52 +010037#include "hwaccess_physmap.h"
Nico Huber305f4172013-06-14 11:55:26 +020038#include "ich_descriptors.h"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110039#include "writeprotect.h"
Nico Huber454f6132012-12-10 13:34:10 +000040
41/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020042 * @defgroup flashprog-general General
Nico Huber454f6132012-12-10 13:34:10 +000043 * @{
44 */
45
46/** Pointer to log callback function. */
Nico Huberc3b02dc2023-08-12 01:13:45 +020047static flashprog_log_callback *global_log_callback = NULL;
Nico Huber454f6132012-12-10 13:34:10 +000048
49/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020050 * @brief Initialize libflashprog.
Nico Huber454f6132012-12-10 13:34:10 +000051 *
52 * @param perform_selfcheck If not zero, perform a self check.
53 * @return 0 on success
54 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020055int flashprog_init(const int perform_selfcheck)
Nico Huber454f6132012-12-10 13:34:10 +000056{
57 if (perform_selfcheck && selfcheck())
58 return 1;
59 myusec_calibrate_delay();
60 return 0;
61}
62
63/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020064 * @brief Shut down libflashprog.
Nico Huber454f6132012-12-10 13:34:10 +000065 * @return 0 on success
66 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020067int flashprog_shutdown(void)
Nico Huber454f6132012-12-10 13:34:10 +000068{
69 return 0; /* TODO: nothing to do? */
70}
71
Nico Huberc3b02dc2023-08-12 01:13:45 +020072/* TODO: flashprog_set_loglevel()? do we need it?
Angel Pons0be623c2021-04-17 17:08:44 +020073 For now, let the user decide in their callback. */
Nico Huber454f6132012-12-10 13:34:10 +000074
75/**
76 * @brief Set the log callback function.
77 *
Nico Huberc3b02dc2023-08-12 01:13:45 +020078 * Set a callback function which will be invoked whenever libflashprog wants
Nico Huber454f6132012-12-10 13:34:10 +000079 * to output messages. This allows frontends to do whatever they see fit with
80 * such messages, e.g. write them to syslog, or to file, or print them in a
81 * GUI window, etc.
82 *
83 * @param log_callback Pointer to the new log callback function.
84 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020085void flashprog_set_log_callback(flashprog_log_callback *const log_callback)
Nico Huber454f6132012-12-10 13:34:10 +000086{
87 global_log_callback = log_callback;
88}
89/** @private */
Nico Huberc3b02dc2023-08-12 01:13:45 +020090int print(const enum flashprog_log_level level, const char *const fmt, ...)
Nico Huber454f6132012-12-10 13:34:10 +000091{
92 if (global_log_callback) {
93 int ret;
94 va_list args;
95 va_start(args, fmt);
Nico Huberd152fb92017-06-19 12:57:10 +020096 ret = global_log_callback(level, fmt, args);
Nico Huber454f6132012-12-10 13:34:10 +000097 va_end(args);
98 return ret;
99 }
100 return 0;
101}
102
Nico Huberc3b02dc2023-08-12 01:13:45 +0200103/** @} */ /* end flashprog-general */
Nico Huber454f6132012-12-10 13:34:10 +0000104
105
106
107/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200108 * @defgroup flashprog-prog Programmers
Nico Huber454f6132012-12-10 13:34:10 +0000109 * @{
110 */
111
112/**
113 * @brief Initialize the specified programmer.
114 *
115 * Currently, only one programmer may be initialized at a time.
116 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200117 * @param[out] flashprog Points to a pointer of type struct flashprog_programmer
Nico Huber454f6132012-12-10 13:34:10 +0000118 * that will be set if programmer initialization succeeds.
119 * *flashprog has to be shutdown by the caller with @ref
Nico Huberc3b02dc2023-08-12 01:13:45 +0200120 * flashprog_programmer_shutdown.
Nico Huber454f6132012-12-10 13:34:10 +0000121 * @param[in] prog_name Name of the programmer to initialize.
122 * @param[in] prog_param Pointer to programmer specific parameters.
123 * @return 0 on success
124 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200125int flashprog_programmer_init(struct flashprog_programmer **const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000126 const char *const prog_name, const char *const prog_param)
127{
128 unsigned prog;
129
Thomas Heijligend45cb592021-05-19 14:12:18 +0200130 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +0200131 if (strcmp(prog_name, programmer_table[prog]->name) == 0)
Nico Huber454f6132012-12-10 13:34:10 +0000132 break;
133 }
Thomas Heijligend45cb592021-05-19 14:12:18 +0200134 if (prog >= programmer_table_size) {
Nico Huber454f6132012-12-10 13:34:10 +0000135 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
136 list_programmers_linebreak(0, 80, 0);
Nico Huberdafd51e2023-02-10 23:58:19 +0100137 msg_ginfo(".\n");
Nico Huber454f6132012-12-10 13:34:10 +0000138 return 1;
139 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100140
141 *flashprog = malloc(sizeof(**flashprog));
142 if (!*flashprog) {
143 msg_gerr("Out of memory!\n");
144 return 1;
145 }
146
147 (*flashprog)->driver = programmer_table[prog];
148 if (prog_param) {
149 (*flashprog)->param = strdup(prog_param);
150 if (!(*flashprog)->param) {
151 msg_gerr("Out of memory!\n");
152 goto _free_err;
153 }
154 } else {
155 (*flashprog)->param = NULL;
156 }
157
158 if (programmer_init(*flashprog))
159 goto _free_err;
160
161 return 0;
162
163_free_err:
Nico Huber74275692024-08-16 13:49:10 +0200164 programmer_shutdown(*flashprog);
Nico Huber2b66ad92023-01-11 20:15:15 +0100165 free((*flashprog)->param);
166 free(*flashprog);
167 return 1;
Nico Huber454f6132012-12-10 13:34:10 +0000168}
169
170/**
171 * @brief Shut down the initialized programmer.
172 *
173 * @param flashprog The programmer to shut down.
174 * @return 0 on success
175 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200176int flashprog_programmer_shutdown(struct flashprog_programmer *const flashprog)
Nico Huber454f6132012-12-10 13:34:10 +0000177{
Nico Huber2b66ad92023-01-11 20:15:15 +0100178 if (programmer_shutdown(flashprog))
179 return 1;
180 free(flashprog);
181 return 0;
Nico Huber454f6132012-12-10 13:34:10 +0000182}
183
Nico Huberc3b02dc2023-08-12 01:13:45 +0200184/* TODO: flashprog_programmer_capabilities()? */
Nico Huber454f6132012-12-10 13:34:10 +0000185
Nico Huberc3b02dc2023-08-12 01:13:45 +0200186/** @} */ /* end flashprog-prog */
Nico Huber454f6132012-12-10 13:34:10 +0000187
188
189
190/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200191 * @defgroup flashprog-flash Flash chips
Nico Huber454f6132012-12-10 13:34:10 +0000192 * @{
193 */
194
Nico Huberda16bd42026-03-08 22:00:28 +0100195static int flashprog_flash_probe_any(struct flashprog_flashctx **flashctx,
196 const struct flashprog_programmer *flashprog)
197{
198 struct flashprog_chips *chip_matches;
199 const struct flashprog_chip *chip;
200 bool first = true;
201 int ret;
202
203 if (flashprog_chips_probe(&chip_matches, flashprog))
204 return 1;
205
206 switch (flashprog_chips_count(chip_matches)) {
207 case 0:
208 ret = 2;
209 goto release_matches;
210 case 1:
211 chip = flashprog_chip_first(chip_matches);
212 break;
213 default:
214 msg_cinfo("Multiple flash chip definitions match the detected chip(s): ");
215 for (chip = flashprog_chip_first(chip_matches); chip;
216 chip = flashprog_chip_next(chip), first = false)
217 msg_cinfo("%s\"%s\"", first ? "" : ", ", flashprog_chip_name(chip));
218 msg_cinfo("\n");
219 ret = 3;
220 goto release_matches;
221 }
222
223 ret = flashprog_flash_probe_chip(flashctx, flashprog, chip);
224
225release_matches:
226 flashprog_chips_release(chip_matches);
227 return ret;
228}
229
Nico Huberc78ca352026-07-19 02:47:38 +0200230/* Search for a character in string accounting for the parenthesis level. */
231static const char *strlvlchr(const char *s, int paren_level, char c)
232{
233 for (; *s != '\0'; ++s) {
234 if (*s == c && paren_level == 0)
235 return s;
236 if (*s == ')' && paren_level-- == 0)
237 return NULL;
238 if (*s == '(')
239 ++paren_level;
240 }
241
242 return NULL;
243}
244
Nico Huber278985c2026-07-19 00:58:16 +0200245/*
246 * Match our database-entry name patterns. The `searched` string can either
Nico Huberc78ca352026-07-19 02:47:38 +0200247 * be an actual chip name or the full pattern (i.e. containing '/' and paren-
248 * theses).
Nico Huber278985c2026-07-19 00:58:16 +0200249 *
250 * Our entry patterns can contain alternatives, separated by '/', for instance
251 * "MX25L4005A/MX25L4006E", optional sub-patterns in parentheses, for instance
252 * "Am29F002(N)BT" which matches both "Am29F002BT" and "Am29F002NBT", and wild-
253 * cards '.' that match any character.
254 *
255 * We compare the entry's pattern and `searched` char-by-char. The pattern
256 * only moves forward, `searched` can be rewinded when the pattern contains
257 * alternatives.
258 *
Nico Huber278985c2026-07-19 00:58:16 +0200259 * Returns the remaining part of `searched` that wasn't matched.
260 */
261static const char *match_chip_name(const char *pattern, const char *const searched)
262{
263 const char *cur_match, *best_alt_match = searched;
Nico Huberc78ca352026-07-19 02:47:38 +0200264 int paren_level;
Nico Huber278985c2026-07-19 00:58:16 +0200265
266next_alternative:
Nico Huberc78ca352026-07-19 02:47:38 +0200267 for (cur_match = searched, paren_level = 0; *pattern != '\0'; ++cur_match, ++pattern) {
268 if (paren_level == 0 && (*pattern == '/' || *pattern == ')')) {
Nico Huber278985c2026-07-19 00:58:16 +0200269 if (*cur_match == '\0') /* exact (sub-)pattern match */
270 return cur_match;
271 if (cur_match > best_alt_match) /* cache current, best match */
272 best_alt_match = cur_match;
Nico Huberc78ca352026-07-19 02:47:38 +0200273 if (*pattern == ')') /* end of sub-pattern */
274 return best_alt_match;
Nico Huber278985c2026-07-19 00:58:16 +0200275 }
276
Nico Huberc78ca352026-07-19 02:47:38 +0200277 if (*cur_match == *pattern) { /* character match */
278 if (*pattern == '(')
279 ++paren_level;
280 if (*pattern == ')')
281 --paren_level;
Nico Huber278985c2026-07-19 00:58:16 +0200282 continue;
Nico Huberc78ca352026-07-19 02:47:38 +0200283 }
Nico Huber278985c2026-07-19 00:58:16 +0200284 if (*cur_match != '\0' && *pattern == '.') /* wildcard match */
285 continue;
Nico Huberc78ca352026-07-19 02:47:38 +0200286 if (*pattern == '(') { /* potential sub-pattern */
287 const char *const sub_end = strlvlchr(pattern + 1, 0, ')');
288 if (sub_end) {
Nico Huber3507a3f2026-07-19 15:59:38 +0200289 const char *delim = strlvlchr(pattern + 1, 0, '/');
Nico Huberc78ca352026-07-19 02:47:38 +0200290 const char *sub_match = match_chip_name(pattern + 1, cur_match);
Nico Huber3507a3f2026-07-19 15:59:38 +0200291 /* if there is only one pattern in the parentheses (i.e. no '/')
292 or the first sub-pattern is empty, consider the match optional */
293 if (sub_match > cur_match || !delim || delim == pattern + 1) {
Nico Huberc78ca352026-07-19 02:47:38 +0200294 cur_match = sub_match - 1;
295 pattern = sub_end;
296 continue;
297 }
298 }
299 }
Nico Huber278985c2026-07-19 00:58:16 +0200300
301 /* skip current alternative, it didn't match */
Nico Huberc78ca352026-07-19 02:47:38 +0200302 const char *const delim = strlvlchr(pattern, paren_level, '/');
Nico Huber278985c2026-07-19 00:58:16 +0200303 if (delim) {
304 pattern = delim + 1;
305 goto next_alternative;
306 }
307
308 /* last alternative didn't match, return cached match if any */
309 return best_alt_match;
310 }
311
312 return MAX(cur_match, best_alt_match);
313}
314
Nico Huber47412a92026-03-10 23:48:25 +0100315/** @private */
316const struct flashchip *flashprog_chip_by_name(const char *chip_name)
Nico Huberda16bd42026-03-08 22:00:28 +0100317{
318 const struct flashchip *chip;
Nico Huber278985c2026-07-19 00:58:16 +0200319 for (chip = flashchips; *chip_name != '\0' && chip->name; ++chip) {
320 if (*match_chip_name(chip->name, chip_name) == '\0')
Nico Huberda16bd42026-03-08 22:00:28 +0100321 return chip;
322 }
323 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_name);
324 return NULL;
325}
326
Nico Huber454f6132012-12-10 13:34:10 +0000327/**
328 * @brief Probe for a flash chip.
329 *
330 * Probes for a flash chip and returns a flash context, that can be used
Nico Huberc3b02dc2023-08-12 01:13:45 +0200331 * later with flash chip and @ref flashprog-ops "image operations", if
Nico Huber454f6132012-12-10 13:34:10 +0000332 * exactly one matching chip is found.
333 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200334 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
Nico Huber454f6132012-12-10 13:34:10 +0000335 * that will be set if exactly one chip is found. *flashctx
Nico Huberc3b02dc2023-08-12 01:13:45 +0200336 * has to be freed by the caller with @ref flashprog_flash_release.
Nico Huber454f6132012-12-10 13:34:10 +0000337 * @param[in] flashprog The flash programmer used to access the chip.
338 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
339 * all known chips.
340 * @return 0 on success,
Nico Huberda16bd42026-03-08 22:00:28 +0100341 * 5 if an unknown chip name was provided,
342 * 4 if the chip was detected, but preparation failed,
Nico Huber454f6132012-12-10 13:34:10 +0000343 * 3 if multiple chips were found,
344 * 2 if no chip was found,
345 * or 1 on any other error.
346 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200347int flashprog_flash_probe(struct flashprog_flashctx **const flashctx,
348 const struct flashprog_programmer *const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000349 const char *const chip_name)
350{
Nico Huberda16bd42026-03-08 22:00:28 +0100351 if (!chip_name)
352 return flashprog_flash_probe_any(flashctx, flashprog);
Nico Huber454f6132012-12-10 13:34:10 +0000353
Nico Huberda16bd42026-03-08 22:00:28 +0100354 const struct flashchip *const chip = flashprog_chip_by_name(chip_name);
355 if (!chip)
356 return 5;
Nico Huber454f6132012-12-10 13:34:10 +0000357
Nico Huberda16bd42026-03-08 22:00:28 +0100358 return flashprog_flash_probe_chip(flashctx, flashprog, chip);
Nico Huber454f6132012-12-10 13:34:10 +0000359}
360
Nico Huberd4926fe2026-03-08 21:37:52 +0100361/** @private */
362int flashprog_flash_prepare_context(struct flashprog_flashctx **flashctx,
363 const struct flashprog_programmer *flashprog,
364 const struct master_common *bus,
365 const struct flashchip *chip)
366{
367 if (bus->adapt_voltage) {
368 if (bus->adapt_voltage(bus, chip->voltage.min, chip->voltage.max))
369 return 4;
370 }
371
372 struct flashprog_flashctx *const flash = calloc(1, sizeof(*flash));
373 if (!flash) {
374 msg_gerr("Out of memory!\n");
375 return 1;
376 }
377
Nico Huber45290d02026-03-14 20:15:55 +0100378 flash->chip = *chip;
Nico Huberd4926fe2026-03-08 21:37:52 +0100379 flash->mst.common = bus; /* `mst` is a union, so we need only one pointer */
380
Nico Huber517d9372026-03-14 21:48:36 +0100381 if (chip->prepare_access && chip->prepare_access(flash)) {
382 free(flash);
383 return 4;
384 }
Nico Huberd4926fe2026-03-08 21:37:52 +0100385
386 /* Fill default layout covering the whole chip. */
387 if (flashprog_layout_new(&flash->default_layout) ||
388 flashprog_layout_add_region(flash->default_layout,
Nico Huber5421b1b2026-03-14 20:18:56 +0100389 0, flashprog_flash_getsize(flash) - 1, "complete flash") ||
Nico Huber517d9372026-03-14 21:48:36 +0100390 flashprog_layout_include_region(flash->default_layout, "complete flash")) {
391 flashprog_flash_release(flash);
392 return 1;
393 }
Nico Huberd4926fe2026-03-08 21:37:52 +0100394
Nico Huber45290d02026-03-14 20:15:55 +0100395 char *const tmp = flashbuses_to_text(flash->chip.bustype);
Nico Huberd4926fe2026-03-08 21:37:52 +0100396 msg_cinfo("Using %s flash chip \"%s\" (%d kB, %s) ",
Nico Huber45290d02026-03-14 20:15:55 +0100397 flash->chip.vendor, flash->chip.name, flash->chip.total_size, tmp);
Nico Huberd4926fe2026-03-08 21:37:52 +0100398 free(tmp);
399 if (strcmp(flashprog->driver->name, "internal") == 0 && flash->physical_memory != 0)
400 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
401 PRIxPTR_WIDTH, flash->physical_memory);
402 else
403 msg_cinfo("on %s.\n", flashprog->driver->name);
404
Nico Huber45290d02026-03-14 20:15:55 +0100405 if (flash->chip.printlock)
406 flash->chip.printlock(flash);
Nico Huberd4926fe2026-03-08 21:37:52 +0100407
Nico Huberd4926fe2026-03-08 21:37:52 +0100408 *flashctx = flash;
409 return 0;
Nico Huberd4926fe2026-03-08 21:37:52 +0100410}
411
412/**
413 * @brief Probe for a specific flash chip.
414 *
415 * Probes for a flash chip and returns a flash context, that can be used
416 * later with flash chip and @ref flashprog-ops "image operations".
417 *
418 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
419 * that will be set. *flashctx has to be freed by the
420 * caller with @ref flashprog_flash_release.
421 * @param[in] flashprog The flash programmer used to access the chip.
422 * @param[in] chip A reference to a chip structure that was previously
423 * fetched by @ref flashprog-chips "enumeration". Its
424 * contents will be copied, so the underlying chip
425 * enumeration can be released directly after the call.
426 * @return 0 on success,
427 * 4 if the chip was detected, but preparation failed,
428 * 3 if multiple chips were found,
429 * 2 if no chip was detected,
430 * or 1 on any other error.
431 */
432int flashprog_flash_probe_chip(struct flashprog_flashctx **flashctx,
433 const struct flashprog_programmer *flashprog,
434 const struct flashprog_chip *chip)
435{
436 const struct master_common *const bus = flashprog_chip_probe(flashprog, chip);
437 if (!bus)
438 return 2;
439
440 return flashprog_flash_prepare_context(flashctx, flashprog, bus, chip);
441}
442
Nico Huber454f6132012-12-10 13:34:10 +0000443/**
444 * @brief Returns the size of the specified flash chip in bytes.
445 *
446 * @param flashctx The queried flash context.
447 * @return Size of flash chip in bytes.
448 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200449size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000450{
Nico Huber45290d02026-03-14 20:15:55 +0100451 return flashctx->chip.total_size * 1024;
Nico Huber454f6132012-12-10 13:34:10 +0000452}
453
454/**
455 * @brief Free a flash context.
456 *
457 * @param flashctx Flash context to free.
458 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200459void flashprog_flash_release(struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000460{
Nico Huber6fb2f622022-02-24 18:17:40 +0100461 if (!flashctx)
462 return;
463
Nico Huber45290d02026-03-14 20:15:55 +0100464 if (flashctx->chip.finish_access)
465 flashctx->chip.finish_access(flashctx);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200466 flashprog_layout_release(flashctx->default_layout);
Nico Huber454f6132012-12-10 13:34:10 +0000467 free(flashctx);
468}
469
470/**
Richard Hughes842d6782021-01-15 09:48:12 +0000471 * @brief Set the progress callback function.
472 *
473 * Set a callback function which will be invoked whenever libflashprog wants
474 * to indicate the progress has changed. This allows frontends to do whatever
475 * they see fit with such values, e.g. update a progress bar in a GUI tool.
476 *
477 * @param flashctx Current flash context.
478 * @param progress_callback Pointer to the new progress callback function.
479 * @param user_data Pointer to any data the API user wants to have passed to the callback.
480 */
481void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx,
482 flashprog_progress_callback *const progress_callback,
483 void *const user_data)
484{
485 flashctx->progress.callback = progress_callback;
486 flashctx->progress.user_data = user_data;
487}
488
489/**
Nico Huber454f6132012-12-10 13:34:10 +0000490 * @brief Set a flag in the given flash context.
491 *
492 * @param flashctx Flash context to alter.
493 * @param flag Flag that is to be set / cleared.
494 * @param value Value to set.
495 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200496void flashprog_flag_set(struct flashprog_flashctx *const flashctx,
497 const enum flashprog_flag flag, const bool value)
Nico Huber454f6132012-12-10 13:34:10 +0000498{
499 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200500 case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break;
501 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
502 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
503 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
Nico Huber55e78842024-07-21 00:46:19 +0200504 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: flashctx->flags.non_volatile_wrsr = value; break;
Nico Huber454f6132012-12-10 13:34:10 +0000505 }
506}
507
508/**
509 * @brief Return the current value of a flag in the given flash context.
510 *
511 * @param flashctx Flash context to read from.
512 * @param flag Flag to be read.
513 * @return Current value of the flag.
514 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200515bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag)
Nico Huber454f6132012-12-10 13:34:10 +0000516{
517 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200518 case FLASHPROG_FLAG_FORCE: return flashctx->flags.force;
519 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
520 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
521 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
Nico Huber55e78842024-07-21 00:46:19 +0200522 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: return flashctx->flags.non_volatile_wrsr;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200523 default: return false;
Nico Huber454f6132012-12-10 13:34:10 +0000524 }
525}
526
Nico Huberc3b02dc2023-08-12 01:13:45 +0200527/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +0000528
529
530
531/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200532 * @defgroup flashprog-layout Layout handling
Nico Huber454f6132012-12-10 13:34:10 +0000533 * @{
534 */
535
Nico Huberdb90cf72023-01-24 23:35:52 +0100536#ifdef __FLASHPROG_LITTLE_ENDIAN__
537static int layout_cmp(const struct romentry *const a, const struct romentry *const b)
538{
539 int ret;
540
541 ret = (int)a->start - (int)b->start;
542 if (ret)
543 return ret;
544
545 ret = (int)a->end - (int)b->end;
546 if (ret)
547 return ret;
548
549 return strcmp(a->name, b->name);
550}
551#endif
552
Nico Huber454f6132012-12-10 13:34:10 +0000553/**
Nico Huber305f4172013-06-14 11:55:26 +0200554 * @brief Read a layout from the Intel ICH descriptor in the flash.
555 *
556 * Optionally verify that the layout matches the one in the given
557 * descriptor dump.
558 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200559 * @param[out] layout Points to a struct flashprog_layout pointer that
Nico Huber305f4172013-06-14 11:55:26 +0200560 * gets set if the descriptor is read and parsed
561 * successfully.
562 * @param[in] flashctx Flash context to read the descriptor from flash.
563 * @param[in] dump The descriptor dump to compare to or NULL.
564 * @param[in] len The length of the descriptor dump.
565 *
566 * @return 0 on success,
567 * 6 if descriptor parsing isn't implemented for the host,
568 * 5 if the descriptors don't match,
569 * 4 if the descriptor dump couldn't be parsed,
570 * 3 if the descriptor on flash couldn't be parsed,
571 * 2 if the descriptor on flash couldn't be read,
572 * 1 on any other error.
573 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200574int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx,
Nico Huber305f4172013-06-14 11:55:26 +0200575 const void *const dump, const size_t len)
576{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200577#ifndef __FLASHPROG_LITTLE_ENDIAN__
Nico Huber305f4172013-06-14 11:55:26 +0200578 return 6;
579#else
Nico Huberc3b02dc2023-08-12 01:13:45 +0200580 struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200581 int ret = 1;
582
583 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200584 if (prepare_flash_access(flashctx, true, false, false, false))
585 goto _free_ret;
586
587 msg_cinfo("Reading ich descriptor... ");
Richard Hughes842d6782021-01-15 09:48:12 +0000588 if (flashprog_read_range(flashctx, desc, 0, 0x1000)) {
Nico Huber305f4172013-06-14 11:55:26 +0200589 msg_cerr("Read operation failed!\n");
590 msg_cinfo("FAILED.\n");
591 ret = 2;
592 goto _finalize_ret;
593 }
594 msg_cinfo("done.\n");
595
Nico Huber5bd990c2019-06-16 19:46:46 +0200596 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200597 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200598 ret = 3;
599 goto _finalize_ret;
600 }
601
602 if (dump) {
603 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200604 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200605 ret = 4;
606 goto _finalize_ret;
607 }
608
Nico Huber5bd990c2019-06-16 19:46:46 +0200609 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
610 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huberdb90cf72023-01-24 23:35:52 +0100611 while (chip_entry && dump_entry && !layout_cmp(chip_entry, dump_entry)) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200612 chip_entry = layout_next(chip_layout, chip_entry);
613 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200614 }
Nico Huberc3b02dc2023-08-12 01:13:45 +0200615 flashprog_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200616 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200617 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200618 ret = 5;
619 goto _finalize_ret;
620 }
621 }
622
Nico Huberc3b02dc2023-08-12 01:13:45 +0200623 *layout = (struct flashprog_layout *)chip_layout;
Nico Huber305f4172013-06-14 11:55:26 +0200624 ret = 0;
625
626_finalize_ret:
627 finalize_flash_access(flashctx);
628_free_ret:
629 if (ret)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200630 flashprog_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200631 free(desc);
632 return ret;
633#endif
634}
635
Nico Huberc3b02dc2023-08-12 01:13:45 +0200636#ifdef __FLASHPROG_LITTLE_ENDIAN__
637static int flashprog_layout_parse_fmap(struct flashprog_layout **layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100638 struct flashctx *const flashctx, const struct fmap *const fmap)
639{
640 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200641 char name[FMAP_STRLEN + 1];
642 const struct fmap_area *area;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200643 struct flashprog_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100644
Nico Huberc3b02dc2023-08-12 01:13:45 +0200645 if (!fmap || flashprog_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100646 return 1;
647
Nico Huber92e0b622019-06-15 15:55:11 +0200648 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
649 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200650 if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
651 flashprog_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200652 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200653 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100654 }
655
656 *layout = l;
657 return 0;
658}
Nico Huberc3b02dc2023-08-12 01:13:45 +0200659#endif /* __FLASHPROG_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100660
661/**
662 * @brief Read a layout by searching the flash chip for fmap.
663 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200664 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100665 * gets set if the fmap is read and parsed successfully.
666 * @param[in] flashctx Flash context
667 * @param[in] offset Offset to begin searching for fmap.
668 * @param[in] offset Length of address space to search.
669 *
670 * @return 0 on success,
671 * 3 if fmap parsing isn't implemented for the host,
672 * 2 if the fmap couldn't be read,
673 * 1 on any other error.
674 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200675int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800676 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100677{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200678#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100679 return 3;
680#else
681 struct fmap *fmap = NULL;
682 int ret = 0;
683
684 msg_gdbg("Attempting to read fmap from ROM content.\n");
685 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
686 msg_gerr("Failed to read fmap from ROM.\n");
687 return 1;
688 }
689
690 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200691 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100692 msg_gerr("Failed to add fmap regions to layout.\n");
693 ret = 1;
694 }
695
696 free(fmap);
697 return ret;
698#endif
699}
700
701/**
702 * @brief Read a layout by searching buffer for fmap.
703 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200704 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100705 * gets set if the fmap is read and parsed successfully.
706 * @param[in] flashctx Flash context
707 * @param[in] buffer Buffer to search in
708 * @param[in] size Size of buffer to search
709 *
710 * @return 0 on success,
711 * 3 if fmap parsing isn't implemented for the host,
712 * 2 if the fmap couldn't be read,
713 * 1 on any other error.
714 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200715int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100716 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
717{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200718#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100719 return 3;
720#else
721 struct fmap *fmap = NULL;
722 int ret = 1;
723
724 if (!buf || !size)
725 goto _ret;
726
727 msg_gdbg("Attempting to read fmap from buffer.\n");
728 if (fmap_read_from_buffer(&fmap, buf, size)) {
729 msg_gerr("Failed to read fmap from buffer.\n");
730 goto _ret;
731 }
732
733 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200734 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100735 msg_gerr("Failed to add fmap regions to layout.\n");
736 goto _free_ret;
737 }
738
739 ret = 0;
740_free_ret:
741 free(fmap);
742_ret:
743 return ret;
744#endif
745}
746
Nico Huber305f4172013-06-14 11:55:26 +0200747/**
Nico Huber454f6132012-12-10 13:34:10 +0000748 * @brief Set the active layout for a flash context.
749 *
750 * Note: This just sets a pointer. The caller must not release the layout
751 * as long as he uses it through the given flash context.
752 *
753 * @param flashctx Flash context whose layout will be set.
754 * @param layout Layout to bet set.
755 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200756void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout)
Nico Huber454f6132012-12-10 13:34:10 +0000757{
758 flashctx->layout = layout;
759}
760
Nico Huberc3b02dc2023-08-12 01:13:45 +0200761/** @} */ /* end flashprog-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100762
763
764/**
Nico Huberfd0fa752026-06-17 21:10:59 +0200765 * @defgroup flashprog-wp Write Protection
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100766 * @{
767 */
768
769/**
770 * @brief Create a new empty WP configuration.
771 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200772 * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100773 * be set if creation succeeds. *cfg has to be freed by the
Nico Huberc3b02dc2023-08-12 01:13:45 +0200774 * caller with @ref flashprog_wp_cfg_release.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100775 * @return 0 on success
776 * >0 on failure
777 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200778enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100779{
780 *cfg = calloc(1, sizeof(**cfg));
Nico Huberc3b02dc2023-08-12 01:13:45 +0200781 return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100782}
783
784/**
785 * @brief Free a WP configuration.
786 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200787 * @param[out] cfg Pointer to the flashprog_wp_cfg to free.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100788 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200789void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100790{
791 free(cfg);
792}
793
794/**
795 * @brief Set the protection mode for a WP configuration.
796 *
797 * @param[in] mode The protection mode to set.
Nico Huberc3b02dc2023-08-12 01:13:45 +0200798 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100799 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200800void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100801{
802 cfg->mode = mode;
803}
804
805/**
806 * @brief Get the protection mode from a WP configuration.
807 *
808 * @param[in] cfg The WP configuration to get the protection mode from.
809 * @return The configuration's protection mode.
810 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200811enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100812{
813 return cfg->mode;
814}
815
816/**
817 * @brief Set the protection range for a WP configuration.
818 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200819 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100820 * @param[in] start The range's start address.
821 * @param[in] len The range's length.
822 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200823void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100824{
825 cfg->range.start = start;
826 cfg->range.len = len;
827}
828
829/**
830 * @brief Get the protection range from a WP configuration.
831 *
832 * @param[out] start Points to a size_t to write the range start to.
833 * @param[out] len Points to a size_t to write the range length to.
834 * @param[in] cfg The WP configuration to get the range from.
835 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200836void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100837{
838 *start = cfg->range.start;
839 *len = cfg->range.len;
840}
841
842/**
843 * @brief Write a WP configuration to a flash chip.
844 *
845 * @param[in] flash The flash context used to access the chip.
846 * @param[in] cfg The WP configuration to write to the chip.
847 * @return 0 on success
848 * >0 on failure
849 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200850enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100851{
Nico Huber45290d02026-03-14 20:15:55 +0100852 if (!flash->chip.wp_write_cfg)
Nico Huberaabb3e02023-01-13 00:22:30 +0100853 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100854
Nico Huber45290d02026-03-14 20:15:55 +0100855 return flash->chip.wp_write_cfg(flash, cfg);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100856}
857
858/**
859 * @brief Read the current WP configuration from a flash chip.
860 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200861 * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100862 * configuration in.
863 * @param[in] flash The flash context used to access the chip.
864 * @return 0 on success
865 * >0 on failure
866 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200867enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100868{
Nico Huber45290d02026-03-14 20:15:55 +0100869 if (!flash->chip.wp_read_cfg)
Nico Huberaabb3e02023-01-13 00:22:30 +0100870 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100871
Nico Huber45290d02026-03-14 20:15:55 +0100872 return flash->chip.wp_read_cfg(cfg, flash);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100873}
874
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100875/**
876 * @brief Get a list of protection ranges supported by the flash chip.
877 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200878 * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100879 * that will be set if available ranges are found. Finding
880 * available ranges may not always be possible, even if the
881 * chip's protection range can be read or modified. *ranges
Nico Huberc3b02dc2023-08-12 01:13:45 +0200882 * must be freed using @ref flashprog_wp_ranges_free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100883 * @param[in] flash The flash context used to access the chip.
884 * @return 0 on success
885 * >0 on failure
886 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200887enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100888{
Nico Huber45290d02026-03-14 20:15:55 +0100889 if (!flash->chip.wp_get_ranges)
Nico Huberaabb3e02023-01-13 00:22:30 +0100890 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100891
Nico Huber45290d02026-03-14 20:15:55 +0100892 return flash->chip.wp_get_ranges(list, flash);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100893}
894
895/**
896 * @brief Get a number of protection ranges in a range list.
897 *
898 * @param[in] ranges The range list to get the count from.
899 * @return Number of ranges in the list.
900 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200901size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100902{
903 return list->count;
904}
905
906/**
907 * @brief Get a protection range from a range list.
908 *
909 * @param[out] start Points to a size_t to write the range's start to.
910 * @param[out] len Points to a size_t to write the range's length to.
911 * @param[in] ranges The range list to get the range from.
912 * @param[in] index Index of the range to get.
913 * @return 0 on success
914 * >0 on failure
915 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200916enum flashprog_wp_result flashprog_wp_ranges_get_range(size_t *start, size_t *len, const struct flashprog_wp_ranges *list, unsigned int index)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100917{
918 if (index >= list->count)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200919 return FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100920
921 *start = list->ranges[index].start;
922 *len = list->ranges[index].len;
923
924 return 0;
925}
926
927/**
928 * @brief Free a WP range list.
929 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200930 * @param[out] cfg Pointer to the flashprog_wp_ranges to free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100931 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200932void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100933{
934 if (!list)
935 return;
936
937 free(list->ranges);
938 free(list);
939}
940
941
Nico Huberc3b02dc2023-08-12 01:13:45 +0200942/** @} */ /* end flashprog-wp */