blob: 9c28eff8d969b8bab1369fad9323adcc1477de1d [file] [log] [blame]
Nico Huber454f6132012-12-10 13:34:10 +00001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright (C) 2012, 2016 secunet Security Networks AG
5 * (Written by Nico Huber <nico.huber@secunet.com> for secunet)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Nico Huber454f6132012-12-10 13:34:10 +000016 */
17/**
18 * @mainpage
19 *
20 * Have a look at the Modules section for a function reference.
21 */
22
Nico Huber70461a92019-06-15 14:56:19 +020023#include <errno.h>
Nico Huber454f6132012-12-10 13:34:10 +000024#include <stdlib.h>
25#include <string.h>
26#include <stdarg.h>
27
28#include "flash.h"
Arthur Heymansc82900b2018-01-10 12:48:16 +010029#include "fmap.h"
Nico Huber454f6132012-12-10 13:34:10 +000030#include "programmer.h"
31#include "layout.h"
Nico Huber305f4172013-06-14 11:55:26 +020032#include "ich_descriptors.h"
Nico Huberc3b02dc2023-08-12 01:13:45 +020033#include "libflashprog.h"
Nikolai Artemievda1c8342021-10-21 00:58:12 +110034#include "writeprotect.h"
Nico Huber454f6132012-12-10 13:34:10 +000035
36/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020037 * @defgroup flashprog-general General
Nico Huber454f6132012-12-10 13:34:10 +000038 * @{
39 */
40
41/** Pointer to log callback function. */
Nico Huberc3b02dc2023-08-12 01:13:45 +020042static flashprog_log_callback *global_log_callback = NULL;
Nico Huber454f6132012-12-10 13:34:10 +000043
44/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020045 * @brief Initialize libflashprog.
Nico Huber454f6132012-12-10 13:34:10 +000046 *
47 * @param perform_selfcheck If not zero, perform a self check.
48 * @return 0 on success
49 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020050int flashprog_init(const int perform_selfcheck)
Nico Huber454f6132012-12-10 13:34:10 +000051{
52 if (perform_selfcheck && selfcheck())
53 return 1;
54 myusec_calibrate_delay();
55 return 0;
56}
57
58/**
Nico Huberc3b02dc2023-08-12 01:13:45 +020059 * @brief Shut down libflashprog.
Nico Huber454f6132012-12-10 13:34:10 +000060 * @return 0 on success
61 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020062int flashprog_shutdown(void)
Nico Huber454f6132012-12-10 13:34:10 +000063{
64 return 0; /* TODO: nothing to do? */
65}
66
Nico Huberc3b02dc2023-08-12 01:13:45 +020067/* TODO: flashprog_set_loglevel()? do we need it?
Angel Pons0be623c2021-04-17 17:08:44 +020068 For now, let the user decide in their callback. */
Nico Huber454f6132012-12-10 13:34:10 +000069
70/**
71 * @brief Set the log callback function.
72 *
Nico Huberc3b02dc2023-08-12 01:13:45 +020073 * Set a callback function which will be invoked whenever libflashprog wants
Nico Huber454f6132012-12-10 13:34:10 +000074 * to output messages. This allows frontends to do whatever they see fit with
75 * such messages, e.g. write them to syslog, or to file, or print them in a
76 * GUI window, etc.
77 *
78 * @param log_callback Pointer to the new log callback function.
79 */
Nico Huberc3b02dc2023-08-12 01:13:45 +020080void flashprog_set_log_callback(flashprog_log_callback *const log_callback)
Nico Huber454f6132012-12-10 13:34:10 +000081{
82 global_log_callback = log_callback;
83}
84/** @private */
Nico Huberc3b02dc2023-08-12 01:13:45 +020085int print(const enum flashprog_log_level level, const char *const fmt, ...)
Nico Huber454f6132012-12-10 13:34:10 +000086{
87 if (global_log_callback) {
88 int ret;
89 va_list args;
90 va_start(args, fmt);
Nico Huberd152fb92017-06-19 12:57:10 +020091 ret = global_log_callback(level, fmt, args);
Nico Huber454f6132012-12-10 13:34:10 +000092 va_end(args);
93 return ret;
94 }
95 return 0;
96}
97
Nico Huberc3b02dc2023-08-12 01:13:45 +020098/** @} */ /* end flashprog-general */
Nico Huber454f6132012-12-10 13:34:10 +000099
100
101
102/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200103 * @defgroup flashprog-query Querying
Nico Huber454f6132012-12-10 13:34:10 +0000104 * @{
105 */
106
Nico Huberabfb70c2022-12-22 12:09:36 +0000107/* TBD */
Nico Huber454f6132012-12-10 13:34:10 +0000108
Nico Huberc3b02dc2023-08-12 01:13:45 +0200109/** @} */ /* end flashprog-query */
Nico Huber454f6132012-12-10 13:34:10 +0000110
111
112
113/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200114 * @defgroup flashprog-prog Programmers
Nico Huber454f6132012-12-10 13:34:10 +0000115 * @{
116 */
117
118/**
119 * @brief Initialize the specified programmer.
120 *
121 * Currently, only one programmer may be initialized at a time.
122 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200123 * @param[out] flashprog Points to a pointer of type struct flashprog_programmer
Nico Huber454f6132012-12-10 13:34:10 +0000124 * that will be set if programmer initialization succeeds.
125 * *flashprog has to be shutdown by the caller with @ref
Nico Huberc3b02dc2023-08-12 01:13:45 +0200126 * flashprog_programmer_shutdown.
Nico Huber454f6132012-12-10 13:34:10 +0000127 * @param[in] prog_name Name of the programmer to initialize.
128 * @param[in] prog_param Pointer to programmer specific parameters.
129 * @return 0 on success
130 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200131int flashprog_programmer_init(struct flashprog_programmer **const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000132 const char *const prog_name, const char *const prog_param)
133{
134 unsigned prog;
135
Thomas Heijligend45cb592021-05-19 14:12:18 +0200136 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +0200137 if (strcmp(prog_name, programmer_table[prog]->name) == 0)
Nico Huber454f6132012-12-10 13:34:10 +0000138 break;
139 }
Thomas Heijligend45cb592021-05-19 14:12:18 +0200140 if (prog >= programmer_table_size) {
Nico Huber454f6132012-12-10 13:34:10 +0000141 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
142 list_programmers_linebreak(0, 80, 0);
Nico Huberdafd51e2023-02-10 23:58:19 +0100143 msg_ginfo(".\n");
Nico Huber454f6132012-12-10 13:34:10 +0000144 return 1;
145 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100146
147 *flashprog = malloc(sizeof(**flashprog));
148 if (!*flashprog) {
149 msg_gerr("Out of memory!\n");
150 return 1;
151 }
152
153 (*flashprog)->driver = programmer_table[prog];
154 if (prog_param) {
155 (*flashprog)->param = strdup(prog_param);
156 if (!(*flashprog)->param) {
157 msg_gerr("Out of memory!\n");
158 goto _free_err;
159 }
160 } else {
161 (*flashprog)->param = NULL;
162 }
163
164 if (programmer_init(*flashprog))
165 goto _free_err;
166
167 return 0;
168
169_free_err:
Nico Huber74275692024-08-16 13:49:10 +0200170 programmer_shutdown(*flashprog);
Nico Huber2b66ad92023-01-11 20:15:15 +0100171 free((*flashprog)->param);
172 free(*flashprog);
173 return 1;
Nico Huber454f6132012-12-10 13:34:10 +0000174}
175
176/**
177 * @brief Shut down the initialized programmer.
178 *
179 * @param flashprog The programmer to shut down.
180 * @return 0 on success
181 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200182int flashprog_programmer_shutdown(struct flashprog_programmer *const flashprog)
Nico Huber454f6132012-12-10 13:34:10 +0000183{
Nico Huber2b66ad92023-01-11 20:15:15 +0100184 if (programmer_shutdown(flashprog))
185 return 1;
186 free(flashprog);
187 return 0;
Nico Huber454f6132012-12-10 13:34:10 +0000188}
189
Nico Huberc3b02dc2023-08-12 01:13:45 +0200190/* TODO: flashprog_programmer_capabilities()? */
Nico Huber454f6132012-12-10 13:34:10 +0000191
Nico Huberc3b02dc2023-08-12 01:13:45 +0200192/** @} */ /* end flashprog-prog */
Nico Huber454f6132012-12-10 13:34:10 +0000193
194
195
196/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200197 * @defgroup flashprog-flash Flash chips
Nico Huber454f6132012-12-10 13:34:10 +0000198 * @{
199 */
200
201/**
202 * @brief Probe for a flash chip.
203 *
204 * Probes for a flash chip and returns a flash context, that can be used
Nico Huberc3b02dc2023-08-12 01:13:45 +0200205 * later with flash chip and @ref flashprog-ops "image operations", if
Nico Huber454f6132012-12-10 13:34:10 +0000206 * exactly one matching chip is found.
207 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200208 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
Nico Huber454f6132012-12-10 13:34:10 +0000209 * that will be set if exactly one chip is found. *flashctx
Nico Huberc3b02dc2023-08-12 01:13:45 +0200210 * has to be freed by the caller with @ref flashprog_flash_release.
Nico Huber454f6132012-12-10 13:34:10 +0000211 * @param[in] flashprog The flash programmer used to access the chip.
212 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
213 * all known chips.
214 * @return 0 on success,
215 * 3 if multiple chips were found,
216 * 2 if no chip was found,
217 * or 1 on any other error.
218 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200219int flashprog_flash_probe(struct flashprog_flashctx **const flashctx,
220 const struct flashprog_programmer *const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000221 const char *const chip_name)
222{
223 int i, ret = 2;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200224 struct flashprog_flashctx second_flashctx = { 0, };
Nico Huber454f6132012-12-10 13:34:10 +0000225
Nico Huberc3b02dc2023-08-12 01:13:45 +0200226 chip_to_probe = chip_name; /* chip_to_probe is global in flashprog.c */
Nico Huber454f6132012-12-10 13:34:10 +0000227
228 *flashctx = malloc(sizeof(**flashctx));
229 if (!*flashctx)
230 return 1;
231 memset(*flashctx, 0, sizeof(**flashctx));
232
233 for (i = 0; i < registered_master_count; ++i) {
234 int flash_idx = -1;
235 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
236 ret = 0;
237 /* We found one chip, now check that there is no second match. */
238 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200239 flashprog_layout_release(second_flashctx.default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200240 free(second_flashctx.chip);
Nico Huber454f6132012-12-10 13:34:10 +0000241 ret = 3;
242 break;
243 }
244 }
245 }
246 if (ret) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200247 flashprog_flash_release(*flashctx);
Nico Huber454f6132012-12-10 13:34:10 +0000248 *flashctx = NULL;
249 }
250 return ret;
251}
252
253/**
254 * @brief Returns the size of the specified flash chip in bytes.
255 *
256 * @param flashctx The queried flash context.
257 * @return Size of flash chip in bytes.
258 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200259size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000260{
261 return flashctx->chip->total_size * 1024;
262}
263
264/**
265 * @brief Free a flash context.
266 *
267 * @param flashctx Flash context to free.
268 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200269void flashprog_flash_release(struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000270{
Nico Huber6fb2f622022-02-24 18:17:40 +0100271 if (!flashctx)
272 return;
273
Nico Huberc3b02dc2023-08-12 01:13:45 +0200274 flashprog_layout_release(flashctx->default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200275 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000276 free(flashctx);
277}
278
279/**
Richard Hughes842d6782021-01-15 09:48:12 +0000280 * @brief Set the progress callback function.
281 *
282 * Set a callback function which will be invoked whenever libflashprog wants
283 * to indicate the progress has changed. This allows frontends to do whatever
284 * they see fit with such values, e.g. update a progress bar in a GUI tool.
285 *
286 * @param flashctx Current flash context.
287 * @param progress_callback Pointer to the new progress callback function.
288 * @param user_data Pointer to any data the API user wants to have passed to the callback.
289 */
290void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx,
291 flashprog_progress_callback *const progress_callback,
292 void *const user_data)
293{
294 flashctx->progress.callback = progress_callback;
295 flashctx->progress.user_data = user_data;
296}
297
298/**
Nico Huber454f6132012-12-10 13:34:10 +0000299 * @brief Set a flag in the given flash context.
300 *
301 * @param flashctx Flash context to alter.
302 * @param flag Flag that is to be set / cleared.
303 * @param value Value to set.
304 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200305void flashprog_flag_set(struct flashprog_flashctx *const flashctx,
306 const enum flashprog_flag flag, const bool value)
Nico Huber454f6132012-12-10 13:34:10 +0000307{
308 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200309 case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break;
310 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
311 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
312 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
Nico Huber55e78842024-07-21 00:46:19 +0200313 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: flashctx->flags.non_volatile_wrsr = value; break;
Nico Huber454f6132012-12-10 13:34:10 +0000314 }
315}
316
317/**
318 * @brief Return the current value of a flag in the given flash context.
319 *
320 * @param flashctx Flash context to read from.
321 * @param flag Flag to be read.
322 * @return Current value of the flag.
323 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200324bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag)
Nico Huber454f6132012-12-10 13:34:10 +0000325{
326 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200327 case FLASHPROG_FLAG_FORCE: return flashctx->flags.force;
328 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
329 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
330 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
Nico Huber55e78842024-07-21 00:46:19 +0200331 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: return flashctx->flags.non_volatile_wrsr;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200332 default: return false;
Nico Huber454f6132012-12-10 13:34:10 +0000333 }
334}
335
Nico Huberc3b02dc2023-08-12 01:13:45 +0200336/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +0000337
338
339
340/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200341 * @defgroup flashprog-layout Layout handling
Nico Huber454f6132012-12-10 13:34:10 +0000342 * @{
343 */
344
Nico Huberdb90cf72023-01-24 23:35:52 +0100345#ifdef __FLASHPROG_LITTLE_ENDIAN__
346static int layout_cmp(const struct romentry *const a, const struct romentry *const b)
347{
348 int ret;
349
350 ret = (int)a->start - (int)b->start;
351 if (ret)
352 return ret;
353
354 ret = (int)a->end - (int)b->end;
355 if (ret)
356 return ret;
357
358 return strcmp(a->name, b->name);
359}
360#endif
361
Nico Huber454f6132012-12-10 13:34:10 +0000362/**
Nico Huber305f4172013-06-14 11:55:26 +0200363 * @brief Read a layout from the Intel ICH descriptor in the flash.
364 *
365 * Optionally verify that the layout matches the one in the given
366 * descriptor dump.
367 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200368 * @param[out] layout Points to a struct flashprog_layout pointer that
Nico Huber305f4172013-06-14 11:55:26 +0200369 * gets set if the descriptor is read and parsed
370 * successfully.
371 * @param[in] flashctx Flash context to read the descriptor from flash.
372 * @param[in] dump The descriptor dump to compare to or NULL.
373 * @param[in] len The length of the descriptor dump.
374 *
375 * @return 0 on success,
376 * 6 if descriptor parsing isn't implemented for the host,
377 * 5 if the descriptors don't match,
378 * 4 if the descriptor dump couldn't be parsed,
379 * 3 if the descriptor on flash couldn't be parsed,
380 * 2 if the descriptor on flash couldn't be read,
381 * 1 on any other error.
382 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200383int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx,
Nico Huber305f4172013-06-14 11:55:26 +0200384 const void *const dump, const size_t len)
385{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200386#ifndef __FLASHPROG_LITTLE_ENDIAN__
Nico Huber305f4172013-06-14 11:55:26 +0200387 return 6;
388#else
Nico Huberc3b02dc2023-08-12 01:13:45 +0200389 struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200390 int ret = 1;
391
392 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200393 if (prepare_flash_access(flashctx, true, false, false, false))
394 goto _free_ret;
395
396 msg_cinfo("Reading ich descriptor... ");
Richard Hughes842d6782021-01-15 09:48:12 +0000397 if (flashprog_read_range(flashctx, desc, 0, 0x1000)) {
Nico Huber305f4172013-06-14 11:55:26 +0200398 msg_cerr("Read operation failed!\n");
399 msg_cinfo("FAILED.\n");
400 ret = 2;
401 goto _finalize_ret;
402 }
403 msg_cinfo("done.\n");
404
Nico Huber5bd990c2019-06-16 19:46:46 +0200405 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200406 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200407 ret = 3;
408 goto _finalize_ret;
409 }
410
411 if (dump) {
412 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200413 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200414 ret = 4;
415 goto _finalize_ret;
416 }
417
Nico Huber5bd990c2019-06-16 19:46:46 +0200418 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
419 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huberdb90cf72023-01-24 23:35:52 +0100420 while (chip_entry && dump_entry && !layout_cmp(chip_entry, dump_entry)) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200421 chip_entry = layout_next(chip_layout, chip_entry);
422 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200423 }
Nico Huberc3b02dc2023-08-12 01:13:45 +0200424 flashprog_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200425 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200426 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200427 ret = 5;
428 goto _finalize_ret;
429 }
430 }
431
Nico Huberc3b02dc2023-08-12 01:13:45 +0200432 *layout = (struct flashprog_layout *)chip_layout;
Nico Huber305f4172013-06-14 11:55:26 +0200433 ret = 0;
434
435_finalize_ret:
436 finalize_flash_access(flashctx);
437_free_ret:
438 if (ret)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200439 flashprog_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200440 free(desc);
441 return ret;
442#endif
443}
444
Nico Huberc3b02dc2023-08-12 01:13:45 +0200445#ifdef __FLASHPROG_LITTLE_ENDIAN__
446static int flashprog_layout_parse_fmap(struct flashprog_layout **layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100447 struct flashctx *const flashctx, const struct fmap *const fmap)
448{
449 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200450 char name[FMAP_STRLEN + 1];
451 const struct fmap_area *area;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200452 struct flashprog_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100453
Nico Huberc3b02dc2023-08-12 01:13:45 +0200454 if (!fmap || flashprog_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100455 return 1;
456
Nico Huber92e0b622019-06-15 15:55:11 +0200457 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
458 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200459 if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
460 flashprog_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200461 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200462 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100463 }
464
465 *layout = l;
466 return 0;
467}
Nico Huberc3b02dc2023-08-12 01:13:45 +0200468#endif /* __FLASHPROG_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100469
470/**
471 * @brief Read a layout by searching the flash chip for fmap.
472 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200473 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100474 * gets set if the fmap is read and parsed successfully.
475 * @param[in] flashctx Flash context
476 * @param[in] offset Offset to begin searching for fmap.
477 * @param[in] offset Length of address space to search.
478 *
479 * @return 0 on success,
480 * 3 if fmap parsing isn't implemented for the host,
481 * 2 if the fmap couldn't be read,
482 * 1 on any other error.
483 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200484int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800485 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100486{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200487#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100488 return 3;
489#else
490 struct fmap *fmap = NULL;
491 int ret = 0;
492
493 msg_gdbg("Attempting to read fmap from ROM content.\n");
494 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
495 msg_gerr("Failed to read fmap from ROM.\n");
496 return 1;
497 }
498
499 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200500 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100501 msg_gerr("Failed to add fmap regions to layout.\n");
502 ret = 1;
503 }
504
505 free(fmap);
506 return ret;
507#endif
508}
509
510/**
511 * @brief Read a layout by searching buffer for fmap.
512 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200513 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100514 * gets set if the fmap is read and parsed successfully.
515 * @param[in] flashctx Flash context
516 * @param[in] buffer Buffer to search in
517 * @param[in] size Size of buffer to search
518 *
519 * @return 0 on success,
520 * 3 if fmap parsing isn't implemented for the host,
521 * 2 if the fmap couldn't be read,
522 * 1 on any other error.
523 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200524int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100525 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
526{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200527#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100528 return 3;
529#else
530 struct fmap *fmap = NULL;
531 int ret = 1;
532
533 if (!buf || !size)
534 goto _ret;
535
536 msg_gdbg("Attempting to read fmap from buffer.\n");
537 if (fmap_read_from_buffer(&fmap, buf, size)) {
538 msg_gerr("Failed to read fmap from buffer.\n");
539 goto _ret;
540 }
541
542 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200543 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100544 msg_gerr("Failed to add fmap regions to layout.\n");
545 goto _free_ret;
546 }
547
548 ret = 0;
549_free_ret:
550 free(fmap);
551_ret:
552 return ret;
553#endif
554}
555
Nico Huber305f4172013-06-14 11:55:26 +0200556/**
Nico Huber454f6132012-12-10 13:34:10 +0000557 * @brief Set the active layout for a flash context.
558 *
559 * Note: This just sets a pointer. The caller must not release the layout
560 * as long as he uses it through the given flash context.
561 *
562 * @param flashctx Flash context whose layout will be set.
563 * @param layout Layout to bet set.
564 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200565void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout)
Nico Huber454f6132012-12-10 13:34:10 +0000566{
567 flashctx->layout = layout;
568}
569
Nico Huberc3b02dc2023-08-12 01:13:45 +0200570/** @} */ /* end flashprog-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100571
572
573/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200574 * @defgroup flashprog-wp
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100575 * @{
576 */
577
578/**
579 * @brief Create a new empty WP configuration.
580 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200581 * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100582 * be set if creation succeeds. *cfg has to be freed by the
Nico Huberc3b02dc2023-08-12 01:13:45 +0200583 * caller with @ref flashprog_wp_cfg_release.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100584 * @return 0 on success
585 * >0 on failure
586 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200587enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100588{
589 *cfg = calloc(1, sizeof(**cfg));
Nico Huberc3b02dc2023-08-12 01:13:45 +0200590 return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100591}
592
593/**
594 * @brief Free a WP configuration.
595 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200596 * @param[out] cfg Pointer to the flashprog_wp_cfg to free.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100597 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200598void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100599{
600 free(cfg);
601}
602
603/**
604 * @brief Set the protection mode for a WP configuration.
605 *
606 * @param[in] mode The protection mode to set.
Nico Huberc3b02dc2023-08-12 01:13:45 +0200607 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100608 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200609void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100610{
611 cfg->mode = mode;
612}
613
614/**
615 * @brief Get the protection mode from a WP configuration.
616 *
617 * @param[in] cfg The WP configuration to get the protection mode from.
618 * @return The configuration's protection mode.
619 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200620enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100621{
622 return cfg->mode;
623}
624
625/**
626 * @brief Set the protection range for a WP configuration.
627 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200628 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100629 * @param[in] start The range's start address.
630 * @param[in] len The range's length.
631 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200632void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100633{
634 cfg->range.start = start;
635 cfg->range.len = len;
636}
637
638/**
639 * @brief Get the protection range from a WP configuration.
640 *
641 * @param[out] start Points to a size_t to write the range start to.
642 * @param[out] len Points to a size_t to write the range length to.
643 * @param[in] cfg The WP configuration to get the range from.
644 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200645void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100646{
647 *start = cfg->range.start;
648 *len = cfg->range.len;
649}
650
651/**
652 * @brief Write a WP configuration to a flash chip.
653 *
654 * @param[in] flash The flash context used to access the chip.
655 * @param[in] cfg The WP configuration to write to the chip.
656 * @return 0 on success
657 * >0 on failure
658 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200659enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100660{
Nico Huberaabb3e02023-01-13 00:22:30 +0100661 if (!flash->chip->wp_write_cfg)
662 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100663
Nico Huberaabb3e02023-01-13 00:22:30 +0100664 return flash->chip->wp_write_cfg(flash, cfg);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100665}
666
667/**
668 * @brief Read the current WP configuration from a flash chip.
669 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200670 * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100671 * configuration in.
672 * @param[in] flash The flash context used to access the chip.
673 * @return 0 on success
674 * >0 on failure
675 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200676enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100677{
Nico Huberaabb3e02023-01-13 00:22:30 +0100678 if (!flash->chip->wp_read_cfg)
679 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100680
Nico Huberaabb3e02023-01-13 00:22:30 +0100681 return flash->chip->wp_read_cfg(cfg, flash);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100682}
683
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100684/**
685 * @brief Get a list of protection ranges supported by the flash chip.
686 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200687 * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100688 * that will be set if available ranges are found. Finding
689 * available ranges may not always be possible, even if the
690 * chip's protection range can be read or modified. *ranges
Nico Huberc3b02dc2023-08-12 01:13:45 +0200691 * must be freed using @ref flashprog_wp_ranges_free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100692 * @param[in] flash The flash context used to access the chip.
693 * @return 0 on success
694 * >0 on failure
695 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200696enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100697{
Nico Huberaabb3e02023-01-13 00:22:30 +0100698 if (!flash->chip->wp_get_ranges)
699 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100700
Nico Huberaabb3e02023-01-13 00:22:30 +0100701 return flash->chip->wp_get_ranges(list, flash);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100702}
703
704/**
705 * @brief Get a number of protection ranges in a range list.
706 *
707 * @param[in] ranges The range list to get the count from.
708 * @return Number of ranges in the list.
709 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200710size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100711{
712 return list->count;
713}
714
715/**
716 * @brief Get a protection range from a range list.
717 *
718 * @param[out] start Points to a size_t to write the range's start to.
719 * @param[out] len Points to a size_t to write the range's length to.
720 * @param[in] ranges The range list to get the range from.
721 * @param[in] index Index of the range to get.
722 * @return 0 on success
723 * >0 on failure
724 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200725enum 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 +1100726{
727 if (index >= list->count)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200728 return FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100729
730 *start = list->ranges[index].start;
731 *len = list->ranges[index].len;
732
733 return 0;
734}
735
736/**
737 * @brief Free a WP range list.
738 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200739 * @param[out] cfg Pointer to the flashprog_wp_ranges to free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100740 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200741void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100742{
743 if (!list)
744 return;
745
746 free(list->ranges);
747 free(list);
748}
749
750
Nico Huberc3b02dc2023-08-12 01:13:45 +0200751/** @} */ /* end flashprog-wp */