blob: d32f9bdd5b42918a80b3b1f4bc967d83bfc1ac84 [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
195/**
196 * @brief Probe for a flash chip.
197 *
198 * Probes for a flash chip and returns a flash context, that can be used
Nico Huberc3b02dc2023-08-12 01:13:45 +0200199 * later with flash chip and @ref flashprog-ops "image operations", if
Nico Huber454f6132012-12-10 13:34:10 +0000200 * exactly one matching chip is found.
201 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200202 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
Nico Huber454f6132012-12-10 13:34:10 +0000203 * that will be set if exactly one chip is found. *flashctx
Nico Huberc3b02dc2023-08-12 01:13:45 +0200204 * has to be freed by the caller with @ref flashprog_flash_release.
Nico Huber454f6132012-12-10 13:34:10 +0000205 * @param[in] flashprog The flash programmer used to access the chip.
206 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
207 * all known chips.
208 * @return 0 on success,
209 * 3 if multiple chips were found,
210 * 2 if no chip was found,
211 * or 1 on any other error.
212 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200213int flashprog_flash_probe(struct flashprog_flashctx **const flashctx,
214 const struct flashprog_programmer *const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000215 const char *const chip_name)
216{
217 int i, ret = 2;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200218 struct flashprog_flashctx second_flashctx = { 0, };
Nico Huber454f6132012-12-10 13:34:10 +0000219
Nico Huberc3b02dc2023-08-12 01:13:45 +0200220 chip_to_probe = chip_name; /* chip_to_probe is global in flashprog.c */
Nico Huber454f6132012-12-10 13:34:10 +0000221
222 *flashctx = malloc(sizeof(**flashctx));
223 if (!*flashctx)
224 return 1;
225 memset(*flashctx, 0, sizeof(**flashctx));
226
227 for (i = 0; i < registered_master_count; ++i) {
228 int flash_idx = -1;
229 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
230 ret = 0;
231 /* We found one chip, now check that there is no second match. */
232 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200233 flashprog_layout_release(second_flashctx.default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200234 free(second_flashctx.chip);
Nico Huber454f6132012-12-10 13:34:10 +0000235 ret = 3;
236 break;
237 }
238 }
239 }
240 if (ret) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200241 flashprog_flash_release(*flashctx);
Nico Huber454f6132012-12-10 13:34:10 +0000242 *flashctx = NULL;
243 }
244 return ret;
245}
246
Nico Huberd4926fe2026-03-08 21:37:52 +0100247/** @private */
248int flashprog_flash_prepare_context(struct flashprog_flashctx **flashctx,
249 const struct flashprog_programmer *flashprog,
250 const struct master_common *bus,
251 const struct flashchip *chip)
252{
253 if (bus->adapt_voltage) {
254 if (bus->adapt_voltage(bus, chip->voltage.min, chip->voltage.max))
255 return 4;
256 }
257
258 struct flashprog_flashctx *const flash = calloc(1, sizeof(*flash));
259 if (!flash) {
260 msg_gerr("Out of memory!\n");
261 return 1;
262 }
263
264 flash->chip = calloc(1, sizeof(*flash->chip));
265 if (!flash->chip) {
266 msg_gerr("Out of memory!\n");
267 free(flash);
268 return 1;
269 }
270
271 *flash->chip = *chip;
272 flash->mst.common = bus; /* `mst` is a union, so we need only one pointer */
273
274 if (chip->prepare_access && chip->prepare_access(flash, PREPARE_POST_PROBE))
275 goto free_flash;
276
277 /* Fill default layout covering the whole chip. */
278 if (flashprog_layout_new(&flash->default_layout) ||
279 flashprog_layout_add_region(flash->default_layout,
280 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
281 flashprog_layout_include_region(flash->default_layout, "complete flash"))
282 goto free_flash;
283
284 char *const tmp = flashbuses_to_text(flash->chip->bustype);
285 msg_cinfo("Using %s flash chip \"%s\" (%d kB, %s) ",
286 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
287 free(tmp);
288 if (strcmp(flashprog->driver->name, "internal") == 0 && flash->physical_memory != 0)
289 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
290 PRIxPTR_WIDTH, flash->physical_memory);
291 else
292 msg_cinfo("on %s.\n", flashprog->driver->name);
293
294 if (flash->chip->printlock)
295 flash->chip->printlock(flash);
296
297 if (flash->chip->finish_access)
298 flash->chip->finish_access(flash);
299
300 *flashctx = flash;
301 return 0;
302
303free_flash:
304 if (flash->chip && flash->chip->finish_access)
305 flash->chip->finish_access(flash);
306 free(flash->chip);
307 free(flash);
308 return 4;
309}
310
311/**
312 * @brief Probe for a specific flash chip.
313 *
314 * Probes for a flash chip and returns a flash context, that can be used
315 * later with flash chip and @ref flashprog-ops "image operations".
316 *
317 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
318 * that will be set. *flashctx has to be freed by the
319 * caller with @ref flashprog_flash_release.
320 * @param[in] flashprog The flash programmer used to access the chip.
321 * @param[in] chip A reference to a chip structure that was previously
322 * fetched by @ref flashprog-chips "enumeration". Its
323 * contents will be copied, so the underlying chip
324 * enumeration can be released directly after the call.
325 * @return 0 on success,
326 * 4 if the chip was detected, but preparation failed,
327 * 3 if multiple chips were found,
328 * 2 if no chip was detected,
329 * or 1 on any other error.
330 */
331int flashprog_flash_probe_chip(struct flashprog_flashctx **flashctx,
332 const struct flashprog_programmer *flashprog,
333 const struct flashprog_chip *chip)
334{
335 const struct master_common *const bus = flashprog_chip_probe(flashprog, chip);
336 if (!bus)
337 return 2;
338
339 return flashprog_flash_prepare_context(flashctx, flashprog, bus, chip);
340}
341
Nico Huber454f6132012-12-10 13:34:10 +0000342/**
343 * @brief Returns the size of the specified flash chip in bytes.
344 *
345 * @param flashctx The queried flash context.
346 * @return Size of flash chip in bytes.
347 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200348size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000349{
350 return flashctx->chip->total_size * 1024;
351}
352
353/**
354 * @brief Free a flash context.
355 *
356 * @param flashctx Flash context to free.
357 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200358void flashprog_flash_release(struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000359{
Nico Huber6fb2f622022-02-24 18:17:40 +0100360 if (!flashctx)
361 return;
362
Nico Huberc3b02dc2023-08-12 01:13:45 +0200363 flashprog_layout_release(flashctx->default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200364 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000365 free(flashctx);
366}
367
368/**
Richard Hughes842d6782021-01-15 09:48:12 +0000369 * @brief Set the progress callback function.
370 *
371 * Set a callback function which will be invoked whenever libflashprog wants
372 * to indicate the progress has changed. This allows frontends to do whatever
373 * they see fit with such values, e.g. update a progress bar in a GUI tool.
374 *
375 * @param flashctx Current flash context.
376 * @param progress_callback Pointer to the new progress callback function.
377 * @param user_data Pointer to any data the API user wants to have passed to the callback.
378 */
379void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx,
380 flashprog_progress_callback *const progress_callback,
381 void *const user_data)
382{
383 flashctx->progress.callback = progress_callback;
384 flashctx->progress.user_data = user_data;
385}
386
387/**
Nico Huber454f6132012-12-10 13:34:10 +0000388 * @brief Set a flag in the given flash context.
389 *
390 * @param flashctx Flash context to alter.
391 * @param flag Flag that is to be set / cleared.
392 * @param value Value to set.
393 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200394void flashprog_flag_set(struct flashprog_flashctx *const flashctx,
395 const enum flashprog_flag flag, const bool value)
Nico Huber454f6132012-12-10 13:34:10 +0000396{
397 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200398 case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break;
399 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
400 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
401 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
Nico Huber55e78842024-07-21 00:46:19 +0200402 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: flashctx->flags.non_volatile_wrsr = value; break;
Nico Huber454f6132012-12-10 13:34:10 +0000403 }
404}
405
406/**
407 * @brief Return the current value of a flag in the given flash context.
408 *
409 * @param flashctx Flash context to read from.
410 * @param flag Flag to be read.
411 * @return Current value of the flag.
412 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200413bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag)
Nico Huber454f6132012-12-10 13:34:10 +0000414{
415 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200416 case FLASHPROG_FLAG_FORCE: return flashctx->flags.force;
417 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
418 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
419 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
Nico Huber55e78842024-07-21 00:46:19 +0200420 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: return flashctx->flags.non_volatile_wrsr;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200421 default: return false;
Nico Huber454f6132012-12-10 13:34:10 +0000422 }
423}
424
Nico Huberc3b02dc2023-08-12 01:13:45 +0200425/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +0000426
427
428
429/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200430 * @defgroup flashprog-layout Layout handling
Nico Huber454f6132012-12-10 13:34:10 +0000431 * @{
432 */
433
Nico Huberdb90cf72023-01-24 23:35:52 +0100434#ifdef __FLASHPROG_LITTLE_ENDIAN__
435static int layout_cmp(const struct romentry *const a, const struct romentry *const b)
436{
437 int ret;
438
439 ret = (int)a->start - (int)b->start;
440 if (ret)
441 return ret;
442
443 ret = (int)a->end - (int)b->end;
444 if (ret)
445 return ret;
446
447 return strcmp(a->name, b->name);
448}
449#endif
450
Nico Huber454f6132012-12-10 13:34:10 +0000451/**
Nico Huber305f4172013-06-14 11:55:26 +0200452 * @brief Read a layout from the Intel ICH descriptor in the flash.
453 *
454 * Optionally verify that the layout matches the one in the given
455 * descriptor dump.
456 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200457 * @param[out] layout Points to a struct flashprog_layout pointer that
Nico Huber305f4172013-06-14 11:55:26 +0200458 * gets set if the descriptor is read and parsed
459 * successfully.
460 * @param[in] flashctx Flash context to read the descriptor from flash.
461 * @param[in] dump The descriptor dump to compare to or NULL.
462 * @param[in] len The length of the descriptor dump.
463 *
464 * @return 0 on success,
465 * 6 if descriptor parsing isn't implemented for the host,
466 * 5 if the descriptors don't match,
467 * 4 if the descriptor dump couldn't be parsed,
468 * 3 if the descriptor on flash couldn't be parsed,
469 * 2 if the descriptor on flash couldn't be read,
470 * 1 on any other error.
471 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200472int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx,
Nico Huber305f4172013-06-14 11:55:26 +0200473 const void *const dump, const size_t len)
474{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200475#ifndef __FLASHPROG_LITTLE_ENDIAN__
Nico Huber305f4172013-06-14 11:55:26 +0200476 return 6;
477#else
Nico Huberc3b02dc2023-08-12 01:13:45 +0200478 struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200479 int ret = 1;
480
481 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200482 if (prepare_flash_access(flashctx, true, false, false, false))
483 goto _free_ret;
484
485 msg_cinfo("Reading ich descriptor... ");
Richard Hughes842d6782021-01-15 09:48:12 +0000486 if (flashprog_read_range(flashctx, desc, 0, 0x1000)) {
Nico Huber305f4172013-06-14 11:55:26 +0200487 msg_cerr("Read operation failed!\n");
488 msg_cinfo("FAILED.\n");
489 ret = 2;
490 goto _finalize_ret;
491 }
492 msg_cinfo("done.\n");
493
Nico Huber5bd990c2019-06-16 19:46:46 +0200494 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200495 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200496 ret = 3;
497 goto _finalize_ret;
498 }
499
500 if (dump) {
501 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200502 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200503 ret = 4;
504 goto _finalize_ret;
505 }
506
Nico Huber5bd990c2019-06-16 19:46:46 +0200507 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
508 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huberdb90cf72023-01-24 23:35:52 +0100509 while (chip_entry && dump_entry && !layout_cmp(chip_entry, dump_entry)) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200510 chip_entry = layout_next(chip_layout, chip_entry);
511 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200512 }
Nico Huberc3b02dc2023-08-12 01:13:45 +0200513 flashprog_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200514 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200515 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200516 ret = 5;
517 goto _finalize_ret;
518 }
519 }
520
Nico Huberc3b02dc2023-08-12 01:13:45 +0200521 *layout = (struct flashprog_layout *)chip_layout;
Nico Huber305f4172013-06-14 11:55:26 +0200522 ret = 0;
523
524_finalize_ret:
525 finalize_flash_access(flashctx);
526_free_ret:
527 if (ret)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200528 flashprog_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200529 free(desc);
530 return ret;
531#endif
532}
533
Nico Huberc3b02dc2023-08-12 01:13:45 +0200534#ifdef __FLASHPROG_LITTLE_ENDIAN__
535static int flashprog_layout_parse_fmap(struct flashprog_layout **layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100536 struct flashctx *const flashctx, const struct fmap *const fmap)
537{
538 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200539 char name[FMAP_STRLEN + 1];
540 const struct fmap_area *area;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200541 struct flashprog_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100542
Nico Huberc3b02dc2023-08-12 01:13:45 +0200543 if (!fmap || flashprog_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100544 return 1;
545
Nico Huber92e0b622019-06-15 15:55:11 +0200546 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
547 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200548 if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
549 flashprog_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200550 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200551 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100552 }
553
554 *layout = l;
555 return 0;
556}
Nico Huberc3b02dc2023-08-12 01:13:45 +0200557#endif /* __FLASHPROG_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100558
559/**
560 * @brief Read a layout by searching the flash chip for fmap.
561 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200562 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100563 * gets set if the fmap is read and parsed successfully.
564 * @param[in] flashctx Flash context
565 * @param[in] offset Offset to begin searching for fmap.
566 * @param[in] offset Length of address space to search.
567 *
568 * @return 0 on success,
569 * 3 if fmap parsing isn't implemented for the host,
570 * 2 if the fmap couldn't be read,
571 * 1 on any other error.
572 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200573int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800574 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100575{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200576#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100577 return 3;
578#else
579 struct fmap *fmap = NULL;
580 int ret = 0;
581
582 msg_gdbg("Attempting to read fmap from ROM content.\n");
583 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
584 msg_gerr("Failed to read fmap from ROM.\n");
585 return 1;
586 }
587
588 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200589 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100590 msg_gerr("Failed to add fmap regions to layout.\n");
591 ret = 1;
592 }
593
594 free(fmap);
595 return ret;
596#endif
597}
598
599/**
600 * @brief Read a layout by searching buffer for fmap.
601 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200602 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100603 * gets set if the fmap is read and parsed successfully.
604 * @param[in] flashctx Flash context
605 * @param[in] buffer Buffer to search in
606 * @param[in] size Size of buffer to search
607 *
608 * @return 0 on success,
609 * 3 if fmap parsing isn't implemented for the host,
610 * 2 if the fmap couldn't be read,
611 * 1 on any other error.
612 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200613int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100614 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
615{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200616#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100617 return 3;
618#else
619 struct fmap *fmap = NULL;
620 int ret = 1;
621
622 if (!buf || !size)
623 goto _ret;
624
625 msg_gdbg("Attempting to read fmap from buffer.\n");
626 if (fmap_read_from_buffer(&fmap, buf, size)) {
627 msg_gerr("Failed to read fmap from buffer.\n");
628 goto _ret;
629 }
630
631 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200632 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100633 msg_gerr("Failed to add fmap regions to layout.\n");
634 goto _free_ret;
635 }
636
637 ret = 0;
638_free_ret:
639 free(fmap);
640_ret:
641 return ret;
642#endif
643}
644
Nico Huber305f4172013-06-14 11:55:26 +0200645/**
Nico Huber454f6132012-12-10 13:34:10 +0000646 * @brief Set the active layout for a flash context.
647 *
648 * Note: This just sets a pointer. The caller must not release the layout
649 * as long as he uses it through the given flash context.
650 *
651 * @param flashctx Flash context whose layout will be set.
652 * @param layout Layout to bet set.
653 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200654void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout)
Nico Huber454f6132012-12-10 13:34:10 +0000655{
656 flashctx->layout = layout;
657}
658
Nico Huberc3b02dc2023-08-12 01:13:45 +0200659/** @} */ /* end flashprog-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100660
661
662/**
Nico Huberfd0fa752026-06-17 21:10:59 +0200663 * @defgroup flashprog-wp Write Protection
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100664 * @{
665 */
666
667/**
668 * @brief Create a new empty WP configuration.
669 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200670 * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100671 * be set if creation succeeds. *cfg has to be freed by the
Nico Huberc3b02dc2023-08-12 01:13:45 +0200672 * caller with @ref flashprog_wp_cfg_release.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100673 * @return 0 on success
674 * >0 on failure
675 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200676enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100677{
678 *cfg = calloc(1, sizeof(**cfg));
Nico Huberc3b02dc2023-08-12 01:13:45 +0200679 return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100680}
681
682/**
683 * @brief Free a WP configuration.
684 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200685 * @param[out] cfg Pointer to the flashprog_wp_cfg to free.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100686 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200687void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100688{
689 free(cfg);
690}
691
692/**
693 * @brief Set the protection mode for a WP configuration.
694 *
695 * @param[in] mode The protection mode to set.
Nico Huberc3b02dc2023-08-12 01:13:45 +0200696 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100697 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200698void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100699{
700 cfg->mode = mode;
701}
702
703/**
704 * @brief Get the protection mode from a WP configuration.
705 *
706 * @param[in] cfg The WP configuration to get the protection mode from.
707 * @return The configuration's protection mode.
708 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200709enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100710{
711 return cfg->mode;
712}
713
714/**
715 * @brief Set the protection range for a WP configuration.
716 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200717 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100718 * @param[in] start The range's start address.
719 * @param[in] len The range's length.
720 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200721void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100722{
723 cfg->range.start = start;
724 cfg->range.len = len;
725}
726
727/**
728 * @brief Get the protection range from a WP configuration.
729 *
730 * @param[out] start Points to a size_t to write the range start to.
731 * @param[out] len Points to a size_t to write the range length to.
732 * @param[in] cfg The WP configuration to get the range from.
733 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200734void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100735{
736 *start = cfg->range.start;
737 *len = cfg->range.len;
738}
739
740/**
741 * @brief Write a WP configuration to a flash chip.
742 *
743 * @param[in] flash The flash context used to access the chip.
744 * @param[in] cfg The WP configuration to write to the chip.
745 * @return 0 on success
746 * >0 on failure
747 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200748enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100749{
Nico Huberaabb3e02023-01-13 00:22:30 +0100750 if (!flash->chip->wp_write_cfg)
751 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100752
Nico Huberaabb3e02023-01-13 00:22:30 +0100753 return flash->chip->wp_write_cfg(flash, cfg);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100754}
755
756/**
757 * @brief Read the current WP configuration from a flash chip.
758 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200759 * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100760 * configuration in.
761 * @param[in] flash The flash context used to access the chip.
762 * @return 0 on success
763 * >0 on failure
764 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200765enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100766{
Nico Huberaabb3e02023-01-13 00:22:30 +0100767 if (!flash->chip->wp_read_cfg)
768 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100769
Nico Huberaabb3e02023-01-13 00:22:30 +0100770 return flash->chip->wp_read_cfg(cfg, flash);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100771}
772
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100773/**
774 * @brief Get a list of protection ranges supported by the flash chip.
775 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200776 * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100777 * that will be set if available ranges are found. Finding
778 * available ranges may not always be possible, even if the
779 * chip's protection range can be read or modified. *ranges
Nico Huberc3b02dc2023-08-12 01:13:45 +0200780 * must be freed using @ref flashprog_wp_ranges_free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100781 * @param[in] flash The flash context used to access the chip.
782 * @return 0 on success
783 * >0 on failure
784 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200785enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100786{
Nico Huberaabb3e02023-01-13 00:22:30 +0100787 if (!flash->chip->wp_get_ranges)
788 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100789
Nico Huberaabb3e02023-01-13 00:22:30 +0100790 return flash->chip->wp_get_ranges(list, flash);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100791}
792
793/**
794 * @brief Get a number of protection ranges in a range list.
795 *
796 * @param[in] ranges The range list to get the count from.
797 * @return Number of ranges in the list.
798 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200799size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100800{
801 return list->count;
802}
803
804/**
805 * @brief Get a protection range from a range list.
806 *
807 * @param[out] start Points to a size_t to write the range's start to.
808 * @param[out] len Points to a size_t to write the range's length to.
809 * @param[in] ranges The range list to get the range from.
810 * @param[in] index Index of the range to get.
811 * @return 0 on success
812 * >0 on failure
813 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200814enum 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 +1100815{
816 if (index >= list->count)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200817 return FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100818
819 *start = list->ranges[index].start;
820 *len = list->ranges[index].len;
821
822 return 0;
823}
824
825/**
826 * @brief Free a WP range list.
827 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200828 * @param[out] cfg Pointer to the flashprog_wp_ranges to free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100829 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200830void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100831{
832 if (!list)
833 return;
834
835 free(list->ranges);
836 free(list);
837}
838
839
Nico Huberc3b02dc2023-08-12 01:13:45 +0200840/** @} */ /* end flashprog-wp */