blob: 900ecc2fa809391ca6540dd4dc846cd900f4d2a2 [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 Huber47412a92026-03-10 23:48:25 +0100230/** @private */
231const struct flashchip *flashprog_chip_by_name(const char *chip_name)
Nico Huberda16bd42026-03-08 22:00:28 +0100232{
233 const struct flashchip *chip;
234 for (chip = flashchips; chip->name; ++chip) {
235 if (!strcmp(chip->name, chip_name))
236 return chip;
237 }
238 msg_cerr("Error: Unknown chip '%s' specified.\n", chip_name);
239 return NULL;
240}
241
Nico Huber454f6132012-12-10 13:34:10 +0000242/**
243 * @brief Probe for a flash chip.
244 *
245 * Probes for a flash chip and returns a flash context, that can be used
Nico Huberc3b02dc2023-08-12 01:13:45 +0200246 * later with flash chip and @ref flashprog-ops "image operations", if
Nico Huber454f6132012-12-10 13:34:10 +0000247 * exactly one matching chip is found.
248 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200249 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
Nico Huber454f6132012-12-10 13:34:10 +0000250 * that will be set if exactly one chip is found. *flashctx
Nico Huberc3b02dc2023-08-12 01:13:45 +0200251 * has to be freed by the caller with @ref flashprog_flash_release.
Nico Huber454f6132012-12-10 13:34:10 +0000252 * @param[in] flashprog The flash programmer used to access the chip.
253 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
254 * all known chips.
255 * @return 0 on success,
Nico Huberda16bd42026-03-08 22:00:28 +0100256 * 5 if an unknown chip name was provided,
257 * 4 if the chip was detected, but preparation failed,
Nico Huber454f6132012-12-10 13:34:10 +0000258 * 3 if multiple chips were found,
259 * 2 if no chip was found,
260 * or 1 on any other error.
261 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200262int flashprog_flash_probe(struct flashprog_flashctx **const flashctx,
263 const struct flashprog_programmer *const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000264 const char *const chip_name)
265{
Nico Huberda16bd42026-03-08 22:00:28 +0100266 if (!chip_name)
267 return flashprog_flash_probe_any(flashctx, flashprog);
Nico Huber454f6132012-12-10 13:34:10 +0000268
Nico Huberda16bd42026-03-08 22:00:28 +0100269 const struct flashchip *const chip = flashprog_chip_by_name(chip_name);
270 if (!chip)
271 return 5;
Nico Huber454f6132012-12-10 13:34:10 +0000272
Nico Huberda16bd42026-03-08 22:00:28 +0100273 return flashprog_flash_probe_chip(flashctx, flashprog, chip);
Nico Huber454f6132012-12-10 13:34:10 +0000274}
275
Nico Huberd4926fe2026-03-08 21:37:52 +0100276/** @private */
277int flashprog_flash_prepare_context(struct flashprog_flashctx **flashctx,
278 const struct flashprog_programmer *flashprog,
279 const struct master_common *bus,
280 const struct flashchip *chip)
281{
282 if (bus->adapt_voltage) {
283 if (bus->adapt_voltage(bus, chip->voltage.min, chip->voltage.max))
284 return 4;
285 }
286
287 struct flashprog_flashctx *const flash = calloc(1, sizeof(*flash));
288 if (!flash) {
289 msg_gerr("Out of memory!\n");
290 return 1;
291 }
292
293 flash->chip = calloc(1, sizeof(*flash->chip));
294 if (!flash->chip) {
295 msg_gerr("Out of memory!\n");
296 free(flash);
297 return 1;
298 }
299
300 *flash->chip = *chip;
301 flash->mst.common = bus; /* `mst` is a union, so we need only one pointer */
302
303 if (chip->prepare_access && chip->prepare_access(flash, PREPARE_POST_PROBE))
304 goto free_flash;
305
306 /* Fill default layout covering the whole chip. */
307 if (flashprog_layout_new(&flash->default_layout) ||
308 flashprog_layout_add_region(flash->default_layout,
309 0, flash->chip->total_size * 1024 - 1, "complete flash") ||
310 flashprog_layout_include_region(flash->default_layout, "complete flash"))
311 goto free_flash;
312
313 char *const tmp = flashbuses_to_text(flash->chip->bustype);
314 msg_cinfo("Using %s flash chip \"%s\" (%d kB, %s) ",
315 flash->chip->vendor, flash->chip->name, flash->chip->total_size, tmp);
316 free(tmp);
317 if (strcmp(flashprog->driver->name, "internal") == 0 && flash->physical_memory != 0)
318 msg_cinfo("mapped at physical address 0x%0*" PRIxPTR ".\n",
319 PRIxPTR_WIDTH, flash->physical_memory);
320 else
321 msg_cinfo("on %s.\n", flashprog->driver->name);
322
323 if (flash->chip->printlock)
324 flash->chip->printlock(flash);
325
326 if (flash->chip->finish_access)
327 flash->chip->finish_access(flash);
328
329 *flashctx = flash;
330 return 0;
331
332free_flash:
333 if (flash->chip && flash->chip->finish_access)
334 flash->chip->finish_access(flash);
335 free(flash->chip);
336 free(flash);
337 return 4;
338}
339
340/**
341 * @brief Probe for a specific flash chip.
342 *
343 * Probes for a flash chip and returns a flash context, that can be used
344 * later with flash chip and @ref flashprog-ops "image operations".
345 *
346 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
347 * that will be set. *flashctx has to be freed by the
348 * caller with @ref flashprog_flash_release.
349 * @param[in] flashprog The flash programmer used to access the chip.
350 * @param[in] chip A reference to a chip structure that was previously
351 * fetched by @ref flashprog-chips "enumeration". Its
352 * contents will be copied, so the underlying chip
353 * enumeration can be released directly after the call.
354 * @return 0 on success,
355 * 4 if the chip was detected, but preparation failed,
356 * 3 if multiple chips were found,
357 * 2 if no chip was detected,
358 * or 1 on any other error.
359 */
360int flashprog_flash_probe_chip(struct flashprog_flashctx **flashctx,
361 const struct flashprog_programmer *flashprog,
362 const struct flashprog_chip *chip)
363{
364 const struct master_common *const bus = flashprog_chip_probe(flashprog, chip);
365 if (!bus)
366 return 2;
367
368 return flashprog_flash_prepare_context(flashctx, flashprog, bus, chip);
369}
370
Nico Huber454f6132012-12-10 13:34:10 +0000371/**
372 * @brief Returns the size of the specified flash chip in bytes.
373 *
374 * @param flashctx The queried flash context.
375 * @return Size of flash chip in bytes.
376 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200377size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000378{
379 return flashctx->chip->total_size * 1024;
380}
381
382/**
383 * @brief Free a flash context.
384 *
385 * @param flashctx Flash context to free.
386 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200387void flashprog_flash_release(struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000388{
Nico Huber6fb2f622022-02-24 18:17:40 +0100389 if (!flashctx)
390 return;
391
Nico Huberc3b02dc2023-08-12 01:13:45 +0200392 flashprog_layout_release(flashctx->default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200393 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000394 free(flashctx);
395}
396
397/**
Richard Hughes842d6782021-01-15 09:48:12 +0000398 * @brief Set the progress callback function.
399 *
400 * Set a callback function which will be invoked whenever libflashprog wants
401 * to indicate the progress has changed. This allows frontends to do whatever
402 * they see fit with such values, e.g. update a progress bar in a GUI tool.
403 *
404 * @param flashctx Current flash context.
405 * @param progress_callback Pointer to the new progress callback function.
406 * @param user_data Pointer to any data the API user wants to have passed to the callback.
407 */
408void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx,
409 flashprog_progress_callback *const progress_callback,
410 void *const user_data)
411{
412 flashctx->progress.callback = progress_callback;
413 flashctx->progress.user_data = user_data;
414}
415
416/**
Nico Huber454f6132012-12-10 13:34:10 +0000417 * @brief Set a flag in the given flash context.
418 *
419 * @param flashctx Flash context to alter.
420 * @param flag Flag that is to be set / cleared.
421 * @param value Value to set.
422 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200423void flashprog_flag_set(struct flashprog_flashctx *const flashctx,
424 const enum flashprog_flag flag, const bool value)
Nico Huber454f6132012-12-10 13:34:10 +0000425{
426 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200427 case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break;
428 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
429 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
430 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
Nico Huber55e78842024-07-21 00:46:19 +0200431 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: flashctx->flags.non_volatile_wrsr = value; break;
Nico Huber454f6132012-12-10 13:34:10 +0000432 }
433}
434
435/**
436 * @brief Return the current value of a flag in the given flash context.
437 *
438 * @param flashctx Flash context to read from.
439 * @param flag Flag to be read.
440 * @return Current value of the flag.
441 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200442bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag)
Nico Huber454f6132012-12-10 13:34:10 +0000443{
444 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200445 case FLASHPROG_FLAG_FORCE: return flashctx->flags.force;
446 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
447 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
448 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
Nico Huber55e78842024-07-21 00:46:19 +0200449 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: return flashctx->flags.non_volatile_wrsr;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200450 default: return false;
Nico Huber454f6132012-12-10 13:34:10 +0000451 }
452}
453
Nico Huberc3b02dc2023-08-12 01:13:45 +0200454/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +0000455
456
457
458/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200459 * @defgroup flashprog-layout Layout handling
Nico Huber454f6132012-12-10 13:34:10 +0000460 * @{
461 */
462
Nico Huberdb90cf72023-01-24 23:35:52 +0100463#ifdef __FLASHPROG_LITTLE_ENDIAN__
464static int layout_cmp(const struct romentry *const a, const struct romentry *const b)
465{
466 int ret;
467
468 ret = (int)a->start - (int)b->start;
469 if (ret)
470 return ret;
471
472 ret = (int)a->end - (int)b->end;
473 if (ret)
474 return ret;
475
476 return strcmp(a->name, b->name);
477}
478#endif
479
Nico Huber454f6132012-12-10 13:34:10 +0000480/**
Nico Huber305f4172013-06-14 11:55:26 +0200481 * @brief Read a layout from the Intel ICH descriptor in the flash.
482 *
483 * Optionally verify that the layout matches the one in the given
484 * descriptor dump.
485 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200486 * @param[out] layout Points to a struct flashprog_layout pointer that
Nico Huber305f4172013-06-14 11:55:26 +0200487 * gets set if the descriptor is read and parsed
488 * successfully.
489 * @param[in] flashctx Flash context to read the descriptor from flash.
490 * @param[in] dump The descriptor dump to compare to or NULL.
491 * @param[in] len The length of the descriptor dump.
492 *
493 * @return 0 on success,
494 * 6 if descriptor parsing isn't implemented for the host,
495 * 5 if the descriptors don't match,
496 * 4 if the descriptor dump couldn't be parsed,
497 * 3 if the descriptor on flash couldn't be parsed,
498 * 2 if the descriptor on flash couldn't be read,
499 * 1 on any other error.
500 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200501int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx,
Nico Huber305f4172013-06-14 11:55:26 +0200502 const void *const dump, const size_t len)
503{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200504#ifndef __FLASHPROG_LITTLE_ENDIAN__
Nico Huber305f4172013-06-14 11:55:26 +0200505 return 6;
506#else
Nico Huberc3b02dc2023-08-12 01:13:45 +0200507 struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200508 int ret = 1;
509
510 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200511 if (prepare_flash_access(flashctx, true, false, false, false))
512 goto _free_ret;
513
514 msg_cinfo("Reading ich descriptor... ");
Richard Hughes842d6782021-01-15 09:48:12 +0000515 if (flashprog_read_range(flashctx, desc, 0, 0x1000)) {
Nico Huber305f4172013-06-14 11:55:26 +0200516 msg_cerr("Read operation failed!\n");
517 msg_cinfo("FAILED.\n");
518 ret = 2;
519 goto _finalize_ret;
520 }
521 msg_cinfo("done.\n");
522
Nico Huber5bd990c2019-06-16 19:46:46 +0200523 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200524 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200525 ret = 3;
526 goto _finalize_ret;
527 }
528
529 if (dump) {
530 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200531 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200532 ret = 4;
533 goto _finalize_ret;
534 }
535
Nico Huber5bd990c2019-06-16 19:46:46 +0200536 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
537 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huberdb90cf72023-01-24 23:35:52 +0100538 while (chip_entry && dump_entry && !layout_cmp(chip_entry, dump_entry)) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200539 chip_entry = layout_next(chip_layout, chip_entry);
540 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200541 }
Nico Huberc3b02dc2023-08-12 01:13:45 +0200542 flashprog_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200543 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200544 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200545 ret = 5;
546 goto _finalize_ret;
547 }
548 }
549
Nico Huberc3b02dc2023-08-12 01:13:45 +0200550 *layout = (struct flashprog_layout *)chip_layout;
Nico Huber305f4172013-06-14 11:55:26 +0200551 ret = 0;
552
553_finalize_ret:
554 finalize_flash_access(flashctx);
555_free_ret:
556 if (ret)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200557 flashprog_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200558 free(desc);
559 return ret;
560#endif
561}
562
Nico Huberc3b02dc2023-08-12 01:13:45 +0200563#ifdef __FLASHPROG_LITTLE_ENDIAN__
564static int flashprog_layout_parse_fmap(struct flashprog_layout **layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100565 struct flashctx *const flashctx, const struct fmap *const fmap)
566{
567 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200568 char name[FMAP_STRLEN + 1];
569 const struct fmap_area *area;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200570 struct flashprog_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100571
Nico Huberc3b02dc2023-08-12 01:13:45 +0200572 if (!fmap || flashprog_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100573 return 1;
574
Nico Huber92e0b622019-06-15 15:55:11 +0200575 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
576 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200577 if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
578 flashprog_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200579 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200580 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100581 }
582
583 *layout = l;
584 return 0;
585}
Nico Huberc3b02dc2023-08-12 01:13:45 +0200586#endif /* __FLASHPROG_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100587
588/**
589 * @brief Read a layout by searching the flash chip for fmap.
590 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200591 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100592 * gets set if the fmap is read and parsed successfully.
593 * @param[in] flashctx Flash context
594 * @param[in] offset Offset to begin searching for fmap.
595 * @param[in] offset Length of address space to search.
596 *
597 * @return 0 on success,
598 * 3 if fmap parsing isn't implemented for the host,
599 * 2 if the fmap couldn't be read,
600 * 1 on any other error.
601 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200602int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800603 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100604{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200605#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100606 return 3;
607#else
608 struct fmap *fmap = NULL;
609 int ret = 0;
610
611 msg_gdbg("Attempting to read fmap from ROM content.\n");
612 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
613 msg_gerr("Failed to read fmap from ROM.\n");
614 return 1;
615 }
616
617 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200618 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100619 msg_gerr("Failed to add fmap regions to layout.\n");
620 ret = 1;
621 }
622
623 free(fmap);
624 return ret;
625#endif
626}
627
628/**
629 * @brief Read a layout by searching buffer for fmap.
630 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200631 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100632 * gets set if the fmap is read and parsed successfully.
633 * @param[in] flashctx Flash context
634 * @param[in] buffer Buffer to search in
635 * @param[in] size Size of buffer to search
636 *
637 * @return 0 on success,
638 * 3 if fmap parsing isn't implemented for the host,
639 * 2 if the fmap couldn't be read,
640 * 1 on any other error.
641 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200642int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100643 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
644{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200645#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100646 return 3;
647#else
648 struct fmap *fmap = NULL;
649 int ret = 1;
650
651 if (!buf || !size)
652 goto _ret;
653
654 msg_gdbg("Attempting to read fmap from buffer.\n");
655 if (fmap_read_from_buffer(&fmap, buf, size)) {
656 msg_gerr("Failed to read fmap from buffer.\n");
657 goto _ret;
658 }
659
660 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200661 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100662 msg_gerr("Failed to add fmap regions to layout.\n");
663 goto _free_ret;
664 }
665
666 ret = 0;
667_free_ret:
668 free(fmap);
669_ret:
670 return ret;
671#endif
672}
673
Nico Huber305f4172013-06-14 11:55:26 +0200674/**
Nico Huber454f6132012-12-10 13:34:10 +0000675 * @brief Set the active layout for a flash context.
676 *
677 * Note: This just sets a pointer. The caller must not release the layout
678 * as long as he uses it through the given flash context.
679 *
680 * @param flashctx Flash context whose layout will be set.
681 * @param layout Layout to bet set.
682 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200683void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout)
Nico Huber454f6132012-12-10 13:34:10 +0000684{
685 flashctx->layout = layout;
686}
687
Nico Huberc3b02dc2023-08-12 01:13:45 +0200688/** @} */ /* end flashprog-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100689
690
691/**
Nico Huberfd0fa752026-06-17 21:10:59 +0200692 * @defgroup flashprog-wp Write Protection
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100693 * @{
694 */
695
696/**
697 * @brief Create a new empty WP configuration.
698 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200699 * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100700 * be set if creation succeeds. *cfg has to be freed by the
Nico Huberc3b02dc2023-08-12 01:13:45 +0200701 * caller with @ref flashprog_wp_cfg_release.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100702 * @return 0 on success
703 * >0 on failure
704 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200705enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100706{
707 *cfg = calloc(1, sizeof(**cfg));
Nico Huberc3b02dc2023-08-12 01:13:45 +0200708 return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100709}
710
711/**
712 * @brief Free a WP configuration.
713 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200714 * @param[out] cfg Pointer to the flashprog_wp_cfg to free.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100715 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200716void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100717{
718 free(cfg);
719}
720
721/**
722 * @brief Set the protection mode for a WP configuration.
723 *
724 * @param[in] mode The protection mode to set.
Nico Huberc3b02dc2023-08-12 01:13:45 +0200725 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100726 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200727void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100728{
729 cfg->mode = mode;
730}
731
732/**
733 * @brief Get the protection mode from a WP configuration.
734 *
735 * @param[in] cfg The WP configuration to get the protection mode from.
736 * @return The configuration's protection mode.
737 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200738enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100739{
740 return cfg->mode;
741}
742
743/**
744 * @brief Set the protection range for a WP configuration.
745 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200746 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100747 * @param[in] start The range's start address.
748 * @param[in] len The range's length.
749 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200750void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100751{
752 cfg->range.start = start;
753 cfg->range.len = len;
754}
755
756/**
757 * @brief Get the protection range from a WP configuration.
758 *
759 * @param[out] start Points to a size_t to write the range start to.
760 * @param[out] len Points to a size_t to write the range length to.
761 * @param[in] cfg The WP configuration to get the range from.
762 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200763void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100764{
765 *start = cfg->range.start;
766 *len = cfg->range.len;
767}
768
769/**
770 * @brief Write a WP configuration to a flash chip.
771 *
772 * @param[in] flash The flash context used to access the chip.
773 * @param[in] cfg The WP configuration to write to the chip.
774 * @return 0 on success
775 * >0 on failure
776 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200777enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100778{
Nico Huberaabb3e02023-01-13 00:22:30 +0100779 if (!flash->chip->wp_write_cfg)
780 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100781
Nico Huberaabb3e02023-01-13 00:22:30 +0100782 return flash->chip->wp_write_cfg(flash, cfg);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100783}
784
785/**
786 * @brief Read the current WP configuration from a flash chip.
787 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200788 * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100789 * configuration in.
790 * @param[in] flash The flash context used to access the chip.
791 * @return 0 on success
792 * >0 on failure
793 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200794enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100795{
Nico Huberaabb3e02023-01-13 00:22:30 +0100796 if (!flash->chip->wp_read_cfg)
797 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100798
Nico Huberaabb3e02023-01-13 00:22:30 +0100799 return flash->chip->wp_read_cfg(cfg, flash);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100800}
801
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100802/**
803 * @brief Get a list of protection ranges supported by the flash chip.
804 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200805 * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100806 * that will be set if available ranges are found. Finding
807 * available ranges may not always be possible, even if the
808 * chip's protection range can be read or modified. *ranges
Nico Huberc3b02dc2023-08-12 01:13:45 +0200809 * must be freed using @ref flashprog_wp_ranges_free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100810 * @param[in] flash The flash context used to access the chip.
811 * @return 0 on success
812 * >0 on failure
813 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200814enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100815{
Nico Huberaabb3e02023-01-13 00:22:30 +0100816 if (!flash->chip->wp_get_ranges)
817 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100818
Nico Huberaabb3e02023-01-13 00:22:30 +0100819 return flash->chip->wp_get_ranges(list, flash);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100820}
821
822/**
823 * @brief Get a number of protection ranges in a range list.
824 *
825 * @param[in] ranges The range list to get the count from.
826 * @return Number of ranges in the list.
827 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200828size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100829{
830 return list->count;
831}
832
833/**
834 * @brief Get a protection range from a range list.
835 *
836 * @param[out] start Points to a size_t to write the range's start to.
837 * @param[out] len Points to a size_t to write the range's length to.
838 * @param[in] ranges The range list to get the range from.
839 * @param[in] index Index of the range to get.
840 * @return 0 on success
841 * >0 on failure
842 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200843enum 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 +1100844{
845 if (index >= list->count)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200846 return FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100847
848 *start = list->ranges[index].start;
849 *len = list->ranges[index].len;
850
851 return 0;
852}
853
854/**
855 * @brief Free a WP range list.
856 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200857 * @param[out] cfg Pointer to the flashprog_wp_ranges to free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100858 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200859void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100860{
861 if (!list)
862 return;
863
864 free(list->ranges);
865 free(list);
866}
867
868
Nico Huberc3b02dc2023-08-12 01:13:45 +0200869/** @} */ /* end flashprog-wp */