blob: 6cdb1356f43227af493ee14f57500fa837a70e28 [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-prog Programmers
Nico Huber454f6132012-12-10 13:34:10 +0000104 * @{
105 */
106
107/**
108 * @brief Initialize the specified programmer.
109 *
110 * Currently, only one programmer may be initialized at a time.
111 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200112 * @param[out] flashprog Points to a pointer of type struct flashprog_programmer
Nico Huber454f6132012-12-10 13:34:10 +0000113 * that will be set if programmer initialization succeeds.
114 * *flashprog has to be shutdown by the caller with @ref
Nico Huberc3b02dc2023-08-12 01:13:45 +0200115 * flashprog_programmer_shutdown.
Nico Huber454f6132012-12-10 13:34:10 +0000116 * @param[in] prog_name Name of the programmer to initialize.
117 * @param[in] prog_param Pointer to programmer specific parameters.
118 * @return 0 on success
119 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200120int flashprog_programmer_init(struct flashprog_programmer **const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000121 const char *const prog_name, const char *const prog_param)
122{
123 unsigned prog;
124
Thomas Heijligend45cb592021-05-19 14:12:18 +0200125 for (prog = 0; prog < programmer_table_size; prog++) {
Thomas Heijligen633d6db2021-03-31 19:09:44 +0200126 if (strcmp(prog_name, programmer_table[prog]->name) == 0)
Nico Huber454f6132012-12-10 13:34:10 +0000127 break;
128 }
Thomas Heijligend45cb592021-05-19 14:12:18 +0200129 if (prog >= programmer_table_size) {
Nico Huber454f6132012-12-10 13:34:10 +0000130 msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name);
131 list_programmers_linebreak(0, 80, 0);
Nico Huberdafd51e2023-02-10 23:58:19 +0100132 msg_ginfo(".\n");
Nico Huber454f6132012-12-10 13:34:10 +0000133 return 1;
134 }
Nico Huber2b66ad92023-01-11 20:15:15 +0100135
136 *flashprog = malloc(sizeof(**flashprog));
137 if (!*flashprog) {
138 msg_gerr("Out of memory!\n");
139 return 1;
140 }
141
142 (*flashprog)->driver = programmer_table[prog];
143 if (prog_param) {
144 (*flashprog)->param = strdup(prog_param);
145 if (!(*flashprog)->param) {
146 msg_gerr("Out of memory!\n");
147 goto _free_err;
148 }
149 } else {
150 (*flashprog)->param = NULL;
151 }
152
153 if (programmer_init(*flashprog))
154 goto _free_err;
155
156 return 0;
157
158_free_err:
Nico Huber74275692024-08-16 13:49:10 +0200159 programmer_shutdown(*flashprog);
Nico Huber2b66ad92023-01-11 20:15:15 +0100160 free((*flashprog)->param);
161 free(*flashprog);
162 return 1;
Nico Huber454f6132012-12-10 13:34:10 +0000163}
164
165/**
166 * @brief Shut down the initialized programmer.
167 *
168 * @param flashprog The programmer to shut down.
169 * @return 0 on success
170 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200171int flashprog_programmer_shutdown(struct flashprog_programmer *const flashprog)
Nico Huber454f6132012-12-10 13:34:10 +0000172{
Nico Huber2b66ad92023-01-11 20:15:15 +0100173 if (programmer_shutdown(flashprog))
174 return 1;
175 free(flashprog);
176 return 0;
Nico Huber454f6132012-12-10 13:34:10 +0000177}
178
Nico Huberc3b02dc2023-08-12 01:13:45 +0200179/* TODO: flashprog_programmer_capabilities()? */
Nico Huber454f6132012-12-10 13:34:10 +0000180
Nico Huberc3b02dc2023-08-12 01:13:45 +0200181/** @} */ /* end flashprog-prog */
Nico Huber454f6132012-12-10 13:34:10 +0000182
183
184
185/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200186 * @defgroup flashprog-flash Flash chips
Nico Huber454f6132012-12-10 13:34:10 +0000187 * @{
188 */
189
190/**
191 * @brief Probe for a flash chip.
192 *
193 * Probes for a flash chip and returns a flash context, that can be used
Nico Huberc3b02dc2023-08-12 01:13:45 +0200194 * later with flash chip and @ref flashprog-ops "image operations", if
Nico Huber454f6132012-12-10 13:34:10 +0000195 * exactly one matching chip is found.
196 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200197 * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx
Nico Huber454f6132012-12-10 13:34:10 +0000198 * that will be set if exactly one chip is found. *flashctx
Nico Huberc3b02dc2023-08-12 01:13:45 +0200199 * has to be freed by the caller with @ref flashprog_flash_release.
Nico Huber454f6132012-12-10 13:34:10 +0000200 * @param[in] flashprog The flash programmer used to access the chip.
201 * @param[in] chip_name Name of a chip to probe for, or NULL to probe for
202 * all known chips.
203 * @return 0 on success,
204 * 3 if multiple chips were found,
205 * 2 if no chip was found,
206 * or 1 on any other error.
207 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200208int flashprog_flash_probe(struct flashprog_flashctx **const flashctx,
209 const struct flashprog_programmer *const flashprog,
Nico Huber454f6132012-12-10 13:34:10 +0000210 const char *const chip_name)
211{
212 int i, ret = 2;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200213 struct flashprog_flashctx second_flashctx = { 0, };
Nico Huber454f6132012-12-10 13:34:10 +0000214
Nico Huberc3b02dc2023-08-12 01:13:45 +0200215 chip_to_probe = chip_name; /* chip_to_probe is global in flashprog.c */
Nico Huber454f6132012-12-10 13:34:10 +0000216
217 *flashctx = malloc(sizeof(**flashctx));
218 if (!*flashctx)
219 return 1;
220 memset(*flashctx, 0, sizeof(**flashctx));
221
222 for (i = 0; i < registered_master_count; ++i) {
223 int flash_idx = -1;
224 if (!ret || (flash_idx = probe_flash(&registered_masters[i], 0, *flashctx, 0)) != -1) {
225 ret = 0;
226 /* We found one chip, now check that there is no second match. */
227 if (probe_flash(&registered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200228 flashprog_layout_release(second_flashctx.default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200229 free(second_flashctx.chip);
Nico Huber454f6132012-12-10 13:34:10 +0000230 ret = 3;
231 break;
232 }
233 }
234 }
235 if (ret) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200236 flashprog_flash_release(*flashctx);
Nico Huber454f6132012-12-10 13:34:10 +0000237 *flashctx = NULL;
238 }
239 return ret;
240}
241
242/**
243 * @brief Returns the size of the specified flash chip in bytes.
244 *
245 * @param flashctx The queried flash context.
246 * @return Size of flash chip in bytes.
247 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200248size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000249{
250 return flashctx->chip->total_size * 1024;
251}
252
253/**
254 * @brief Free a flash context.
255 *
256 * @param flashctx Flash context to free.
257 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200258void flashprog_flash_release(struct flashprog_flashctx *const flashctx)
Nico Huber454f6132012-12-10 13:34:10 +0000259{
Nico Huber6fb2f622022-02-24 18:17:40 +0100260 if (!flashctx)
261 return;
262
Nico Huberc3b02dc2023-08-12 01:13:45 +0200263 flashprog_layout_release(flashctx->default_layout);
Nico Huber38450ce2019-06-16 20:07:28 +0200264 free(flashctx->chip);
Nico Huber454f6132012-12-10 13:34:10 +0000265 free(flashctx);
266}
267
268/**
Richard Hughes842d6782021-01-15 09:48:12 +0000269 * @brief Set the progress callback function.
270 *
271 * Set a callback function which will be invoked whenever libflashprog wants
272 * to indicate the progress has changed. This allows frontends to do whatever
273 * they see fit with such values, e.g. update a progress bar in a GUI tool.
274 *
275 * @param flashctx Current flash context.
276 * @param progress_callback Pointer to the new progress callback function.
277 * @param user_data Pointer to any data the API user wants to have passed to the callback.
278 */
279void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx,
280 flashprog_progress_callback *const progress_callback,
281 void *const user_data)
282{
283 flashctx->progress.callback = progress_callback;
284 flashctx->progress.user_data = user_data;
285}
286
287/**
Nico Huber454f6132012-12-10 13:34:10 +0000288 * @brief Set a flag in the given flash context.
289 *
290 * @param flashctx Flash context to alter.
291 * @param flag Flag that is to be set / cleared.
292 * @param value Value to set.
293 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200294void flashprog_flag_set(struct flashprog_flashctx *const flashctx,
295 const enum flashprog_flag flag, const bool value)
Nico Huber454f6132012-12-10 13:34:10 +0000296{
297 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200298 case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break;
299 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break;
300 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break;
301 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break;
Nico Huber55e78842024-07-21 00:46:19 +0200302 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: flashctx->flags.non_volatile_wrsr = value; break;
Nico Huber454f6132012-12-10 13:34:10 +0000303 }
304}
305
306/**
307 * @brief Return the current value of a flag in the given flash context.
308 *
309 * @param flashctx Flash context to read from.
310 * @param flag Flag to be read.
311 * @return Current value of the flag.
312 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200313bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag)
Nico Huber454f6132012-12-10 13:34:10 +0000314{
315 switch (flag) {
Nico Huberc3b02dc2023-08-12 01:13:45 +0200316 case FLASHPROG_FLAG_FORCE: return flashctx->flags.force;
317 case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch;
318 case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write;
319 case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip;
Nico Huber55e78842024-07-21 00:46:19 +0200320 case FLASHPROG_FLAG_NON_VOLATILE_WRSR: return flashctx->flags.non_volatile_wrsr;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200321 default: return false;
Nico Huber454f6132012-12-10 13:34:10 +0000322 }
323}
324
Nico Huberc3b02dc2023-08-12 01:13:45 +0200325/** @} */ /* end flashprog-flash */
Nico Huber454f6132012-12-10 13:34:10 +0000326
327
328
329/**
Nico Huberc3b02dc2023-08-12 01:13:45 +0200330 * @defgroup flashprog-layout Layout handling
Nico Huber454f6132012-12-10 13:34:10 +0000331 * @{
332 */
333
Nico Huberdb90cf72023-01-24 23:35:52 +0100334#ifdef __FLASHPROG_LITTLE_ENDIAN__
335static int layout_cmp(const struct romentry *const a, const struct romentry *const b)
336{
337 int ret;
338
339 ret = (int)a->start - (int)b->start;
340 if (ret)
341 return ret;
342
343 ret = (int)a->end - (int)b->end;
344 if (ret)
345 return ret;
346
347 return strcmp(a->name, b->name);
348}
349#endif
350
Nico Huber454f6132012-12-10 13:34:10 +0000351/**
Nico Huber305f4172013-06-14 11:55:26 +0200352 * @brief Read a layout from the Intel ICH descriptor in the flash.
353 *
354 * Optionally verify that the layout matches the one in the given
355 * descriptor dump.
356 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200357 * @param[out] layout Points to a struct flashprog_layout pointer that
Nico Huber305f4172013-06-14 11:55:26 +0200358 * gets set if the descriptor is read and parsed
359 * successfully.
360 * @param[in] flashctx Flash context to read the descriptor from flash.
361 * @param[in] dump The descriptor dump to compare to or NULL.
362 * @param[in] len The length of the descriptor dump.
363 *
364 * @return 0 on success,
365 * 6 if descriptor parsing isn't implemented for the host,
366 * 5 if the descriptors don't match,
367 * 4 if the descriptor dump couldn't be parsed,
368 * 3 if the descriptor on flash couldn't be parsed,
369 * 2 if the descriptor on flash couldn't be read,
370 * 1 on any other error.
371 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200372int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx,
Nico Huber305f4172013-06-14 11:55:26 +0200373 const void *const dump, const size_t len)
374{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200375#ifndef __FLASHPROG_LITTLE_ENDIAN__
Nico Huber305f4172013-06-14 11:55:26 +0200376 return 6;
377#else
Nico Huberc3b02dc2023-08-12 01:13:45 +0200378 struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL;
Nico Huber305f4172013-06-14 11:55:26 +0200379 int ret = 1;
380
381 void *const desc = malloc(0x1000);
Nico Huber305f4172013-06-14 11:55:26 +0200382 if (prepare_flash_access(flashctx, true, false, false, false))
383 goto _free_ret;
384
385 msg_cinfo("Reading ich descriptor... ");
Richard Hughes842d6782021-01-15 09:48:12 +0000386 if (flashprog_read_range(flashctx, desc, 0, 0x1000)) {
Nico Huber305f4172013-06-14 11:55:26 +0200387 msg_cerr("Read operation failed!\n");
388 msg_cinfo("FAILED.\n");
389 ret = 2;
390 goto _finalize_ret;
391 }
392 msg_cinfo("done.\n");
393
Nico Huber5bd990c2019-06-16 19:46:46 +0200394 if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200395 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200396 ret = 3;
397 goto _finalize_ret;
398 }
399
400 if (dump) {
401 if (layout_from_ich_descriptors(&dump_layout, dump, len)) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200402 msg_cerr("Couldn't parse the descriptor!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200403 ret = 4;
404 goto _finalize_ret;
405 }
406
Nico Huber5bd990c2019-06-16 19:46:46 +0200407 const struct romentry *chip_entry = layout_next(chip_layout, NULL);
408 const struct romentry *dump_entry = layout_next(dump_layout, NULL);
Nico Huberdb90cf72023-01-24 23:35:52 +0100409 while (chip_entry && dump_entry && !layout_cmp(chip_entry, dump_entry)) {
Nico Huber5bd990c2019-06-16 19:46:46 +0200410 chip_entry = layout_next(chip_layout, chip_entry);
411 dump_entry = layout_next(dump_layout, dump_entry);
Nico Huber354766b2019-06-16 19:28:35 +0200412 }
Nico Huberc3b02dc2023-08-12 01:13:45 +0200413 flashprog_layout_release(dump_layout);
Nico Huber354766b2019-06-16 19:28:35 +0200414 if (chip_entry || dump_entry) {
Patrick Rudolph911b8d82019-06-06 11:23:55 +0200415 msg_cerr("Descriptors don't match!\n");
Nico Huber305f4172013-06-14 11:55:26 +0200416 ret = 5;
417 goto _finalize_ret;
418 }
419 }
420
Nico Huberc3b02dc2023-08-12 01:13:45 +0200421 *layout = (struct flashprog_layout *)chip_layout;
Nico Huber305f4172013-06-14 11:55:26 +0200422 ret = 0;
423
424_finalize_ret:
425 finalize_flash_access(flashctx);
426_free_ret:
427 if (ret)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200428 flashprog_layout_release(chip_layout);
Nico Huber305f4172013-06-14 11:55:26 +0200429 free(desc);
430 return ret;
431#endif
432}
433
Nico Huberc3b02dc2023-08-12 01:13:45 +0200434#ifdef __FLASHPROG_LITTLE_ENDIAN__
435static int flashprog_layout_parse_fmap(struct flashprog_layout **layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100436 struct flashctx *const flashctx, const struct fmap *const fmap)
437{
438 int i;
Nico Huber92e0b622019-06-15 15:55:11 +0200439 char name[FMAP_STRLEN + 1];
440 const struct fmap_area *area;
Nico Huberc3b02dc2023-08-12 01:13:45 +0200441 struct flashprog_layout *l;
Arthur Heymansc82900b2018-01-10 12:48:16 +0100442
Nico Huberc3b02dc2023-08-12 01:13:45 +0200443 if (!fmap || flashprog_layout_new(&l))
Arthur Heymansc82900b2018-01-10 12:48:16 +0100444 return 1;
445
Nico Huber92e0b622019-06-15 15:55:11 +0200446 for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) {
447 snprintf(name, sizeof(name), "%s", area->name);
Nico Huberc3b02dc2023-08-12 01:13:45 +0200448 if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) {
449 flashprog_layout_release(l);
Nico Huber70461a92019-06-15 14:56:19 +0200450 return 1;
Nico Huberefe96a92021-05-14 00:39:24 +0200451 }
Arthur Heymansc82900b2018-01-10 12:48:16 +0100452 }
453
454 *layout = l;
455 return 0;
456}
Nico Huberc3b02dc2023-08-12 01:13:45 +0200457#endif /* __FLASHPROG_LITTLE_ENDIAN__ */
Arthur Heymansc82900b2018-01-10 12:48:16 +0100458
459/**
460 * @brief Read a layout by searching the flash chip for fmap.
461 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200462 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100463 * gets set if the fmap is read and parsed successfully.
464 * @param[in] flashctx Flash context
465 * @param[in] offset Offset to begin searching for fmap.
466 * @param[in] offset Length of address space to search.
467 *
468 * @return 0 on success,
469 * 3 if fmap parsing isn't implemented for the host,
470 * 2 if the fmap couldn't be read,
471 * 1 on any other error.
472 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200473int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout,
Julius Werner8f0db7d2022-02-14 17:07:39 -0800474 struct flashctx *const flashctx, size_t offset, size_t len)
Arthur Heymansc82900b2018-01-10 12:48:16 +0100475{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200476#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100477 return 3;
478#else
479 struct fmap *fmap = NULL;
480 int ret = 0;
481
482 msg_gdbg("Attempting to read fmap from ROM content.\n");
483 if (fmap_read_from_rom(&fmap, flashctx, offset, len)) {
484 msg_gerr("Failed to read fmap from ROM.\n");
485 return 1;
486 }
487
488 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200489 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100490 msg_gerr("Failed to add fmap regions to layout.\n");
491 ret = 1;
492 }
493
494 free(fmap);
495 return ret;
496#endif
497}
498
499/**
500 * @brief Read a layout by searching buffer for fmap.
501 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200502 * @param[out] layout Points to a struct flashprog_layout pointer that
Arthur Heymansc82900b2018-01-10 12:48:16 +0100503 * gets set if the fmap is read and parsed successfully.
504 * @param[in] flashctx Flash context
505 * @param[in] buffer Buffer to search in
506 * @param[in] size Size of buffer to search
507 *
508 * @return 0 on success,
509 * 3 if fmap parsing isn't implemented for the host,
510 * 2 if the fmap couldn't be read,
511 * 1 on any other error.
512 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200513int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout,
Arthur Heymansc82900b2018-01-10 12:48:16 +0100514 struct flashctx *const flashctx, const uint8_t *const buf, size_t size)
515{
Nico Huberc3b02dc2023-08-12 01:13:45 +0200516#ifndef __FLASHPROG_LITTLE_ENDIAN__
Arthur Heymansc82900b2018-01-10 12:48:16 +0100517 return 3;
518#else
519 struct fmap *fmap = NULL;
520 int ret = 1;
521
522 if (!buf || !size)
523 goto _ret;
524
525 msg_gdbg("Attempting to read fmap from buffer.\n");
526 if (fmap_read_from_buffer(&fmap, buf, size)) {
527 msg_gerr("Failed to read fmap from buffer.\n");
528 goto _ret;
529 }
530
531 msg_gdbg("Adding fmap layout to global layout.\n");
Nico Huberc3b02dc2023-08-12 01:13:45 +0200532 if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) {
Arthur Heymansc82900b2018-01-10 12:48:16 +0100533 msg_gerr("Failed to add fmap regions to layout.\n");
534 goto _free_ret;
535 }
536
537 ret = 0;
538_free_ret:
539 free(fmap);
540_ret:
541 return ret;
542#endif
543}
544
Nico Huber305f4172013-06-14 11:55:26 +0200545/**
Nico Huber454f6132012-12-10 13:34:10 +0000546 * @brief Set the active layout for a flash context.
547 *
548 * Note: This just sets a pointer. The caller must not release the layout
549 * as long as he uses it through the given flash context.
550 *
551 * @param flashctx Flash context whose layout will be set.
552 * @param layout Layout to bet set.
553 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200554void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout)
Nico Huber454f6132012-12-10 13:34:10 +0000555{
556 flashctx->layout = layout;
557}
558
Nico Huberc3b02dc2023-08-12 01:13:45 +0200559/** @} */ /* end flashprog-layout */
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100560
561
562/**
Nico Huberfd0fa752026-06-17 21:10:59 +0200563 * @defgroup flashprog-wp Write Protection
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100564 * @{
565 */
566
567/**
568 * @brief Create a new empty WP configuration.
569 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200570 * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100571 * be set if creation succeeds. *cfg has to be freed by the
Nico Huberc3b02dc2023-08-12 01:13:45 +0200572 * caller with @ref flashprog_wp_cfg_release.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100573 * @return 0 on success
574 * >0 on failure
575 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200576enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100577{
578 *cfg = calloc(1, sizeof(**cfg));
Nico Huberc3b02dc2023-08-12 01:13:45 +0200579 return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100580}
581
582/**
583 * @brief Free a WP configuration.
584 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200585 * @param[out] cfg Pointer to the flashprog_wp_cfg to free.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100586 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200587void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100588{
589 free(cfg);
590}
591
592/**
593 * @brief Set the protection mode for a WP configuration.
594 *
595 * @param[in] mode The protection mode to set.
Nico Huberc3b02dc2023-08-12 01:13:45 +0200596 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100597 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200598void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100599{
600 cfg->mode = mode;
601}
602
603/**
604 * @brief Get the protection mode from a WP configuration.
605 *
606 * @param[in] cfg The WP configuration to get the protection mode from.
607 * @return The configuration's protection mode.
608 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200609enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100610{
611 return cfg->mode;
612}
613
614/**
615 * @brief Set the protection range for a WP configuration.
616 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200617 * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify.
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100618 * @param[in] start The range's start address.
619 * @param[in] len The range's length.
620 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200621void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100622{
623 cfg->range.start = start;
624 cfg->range.len = len;
625}
626
627/**
628 * @brief Get the protection range from a WP configuration.
629 *
630 * @param[out] start Points to a size_t to write the range start to.
631 * @param[out] len Points to a size_t to write the range length to.
632 * @param[in] cfg The WP configuration to get the range from.
633 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200634void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100635{
636 *start = cfg->range.start;
637 *len = cfg->range.len;
638}
639
640/**
641 * @brief Write a WP configuration to a flash chip.
642 *
643 * @param[in] flash The flash context used to access the chip.
644 * @param[in] cfg The WP configuration to write to the chip.
645 * @return 0 on success
646 * >0 on failure
647 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200648enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100649{
Nico Huberaabb3e02023-01-13 00:22:30 +0100650 if (!flash->chip->wp_write_cfg)
651 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100652
Nico Huberaabb3e02023-01-13 00:22:30 +0100653 return flash->chip->wp_write_cfg(flash, cfg);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100654}
655
656/**
657 * @brief Read the current WP configuration from a flash chip.
658 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200659 * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100660 * configuration in.
661 * @param[in] flash The flash context used to access the chip.
662 * @return 0 on success
663 * >0 on failure
664 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200665enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash)
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100666{
Nico Huberaabb3e02023-01-13 00:22:30 +0100667 if (!flash->chip->wp_read_cfg)
668 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100669
Nico Huberaabb3e02023-01-13 00:22:30 +0100670 return flash->chip->wp_read_cfg(cfg, flash);
Nikolai Artemievda1c8342021-10-21 00:58:12 +1100671}
672
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100673/**
674 * @brief Get a list of protection ranges supported by the flash chip.
675 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200676 * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100677 * that will be set if available ranges are found. Finding
678 * available ranges may not always be possible, even if the
679 * chip's protection range can be read or modified. *ranges
Nico Huberc3b02dc2023-08-12 01:13:45 +0200680 * must be freed using @ref flashprog_wp_ranges_free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100681 * @param[in] flash The flash context used to access the chip.
682 * @return 0 on success
683 * >0 on failure
684 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200685enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100686{
Nico Huberaabb3e02023-01-13 00:22:30 +0100687 if (!flash->chip->wp_get_ranges)
688 return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100689
Nico Huberaabb3e02023-01-13 00:22:30 +0100690 return flash->chip->wp_get_ranges(list, flash);
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100691}
692
693/**
694 * @brief Get a number of protection ranges in a range list.
695 *
696 * @param[in] ranges The range list to get the count from.
697 * @return Number of ranges in the list.
698 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200699size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100700{
701 return list->count;
702}
703
704/**
705 * @brief Get a protection range from a range list.
706 *
707 * @param[out] start Points to a size_t to write the range's start to.
708 * @param[out] len Points to a size_t to write the range's length to.
709 * @param[in] ranges The range list to get the range from.
710 * @param[in] index Index of the range to get.
711 * @return 0 on success
712 * >0 on failure
713 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200714enum 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 +1100715{
716 if (index >= list->count)
Nico Huberc3b02dc2023-08-12 01:13:45 +0200717 return FLASHPROG_WP_ERR_OTHER;
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100718
719 *start = list->ranges[index].start;
720 *len = list->ranges[index].len;
721
722 return 0;
723}
724
725/**
726 * @brief Free a WP range list.
727 *
Nico Huberc3b02dc2023-08-12 01:13:45 +0200728 * @param[out] cfg Pointer to the flashprog_wp_ranges to free.
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100729 */
Nico Huberc3b02dc2023-08-12 01:13:45 +0200730void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list)
Nikolai Artemiev077c0d12021-10-21 01:50:15 +1100731{
732 if (!list)
733 return;
734
735 free(list->ranges);
736 free(list);
737}
738
739
Nico Huberc3b02dc2023-08-12 01:13:45 +0200740/** @} */ /* end flashprog-wp */