Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 16 | */ |
| 17 | /** |
| 18 | * @mainpage |
| 19 | * |
| 20 | * Have a look at the Modules section for a function reference. |
| 21 | */ |
| 22 | |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 23 | #include <errno.h> |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <stdarg.h> |
| 27 | |
| 28 | #include "flash.h" |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 29 | #include "fmap.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 30 | #include "programmer.h" |
| 31 | #include "layout.h" |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 32 | #include "ich_descriptors.h" |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 33 | #include "libflashprog.h" |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 34 | #include "writeprotect.h" |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 35 | |
| 36 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 37 | * @defgroup flashprog-general General |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 38 | * @{ |
| 39 | */ |
| 40 | |
| 41 | /** Pointer to log callback function. */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 42 | static flashprog_log_callback *global_log_callback = NULL; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 43 | |
| 44 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 45 | * @brief Initialize libflashprog. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 46 | * |
| 47 | * @param perform_selfcheck If not zero, perform a self check. |
| 48 | * @return 0 on success |
| 49 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 50 | int flashprog_init(const int perform_selfcheck) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 51 | { |
| 52 | if (perform_selfcheck && selfcheck()) |
| 53 | return 1; |
| 54 | myusec_calibrate_delay(); |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 59 | * @brief Shut down libflashprog. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 60 | * @return 0 on success |
| 61 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 62 | int flashprog_shutdown(void) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 63 | { |
| 64 | return 0; /* TODO: nothing to do? */ |
| 65 | } |
| 66 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 67 | /* TODO: flashprog_set_loglevel()? do we need it? |
Angel Pons | 0be623c | 2021-04-17 17:08:44 +0200 | [diff] [blame] | 68 | For now, let the user decide in their callback. */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 69 | |
| 70 | /** |
| 71 | * @brief Set the log callback function. |
| 72 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 73 | * Set a callback function which will be invoked whenever libflashprog wants |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 74 | * 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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 80 | void flashprog_set_log_callback(flashprog_log_callback *const log_callback) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 81 | { |
| 82 | global_log_callback = log_callback; |
| 83 | } |
| 84 | /** @private */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 85 | int print(const enum flashprog_log_level level, const char *const fmt, ...) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 86 | { |
| 87 | if (global_log_callback) { |
| 88 | int ret; |
| 89 | va_list args; |
| 90 | va_start(args, fmt); |
Nico Huber | d152fb9 | 2017-06-19 12:57:10 +0200 | [diff] [blame] | 91 | ret = global_log_callback(level, fmt, args); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 92 | va_end(args); |
| 93 | return ret; |
| 94 | } |
| 95 | return 0; |
| 96 | } |
| 97 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 98 | /** @} */ /* end flashprog-general */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 99 | |
| 100 | |
| 101 | |
| 102 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 103 | * @defgroup flashprog-query Querying |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 104 | * @{ |
| 105 | */ |
| 106 | |
Nico Huber | abfb70c | 2022-12-22 12:09:36 +0000 | [diff] [blame] | 107 | /* TBD */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 108 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 109 | /** @} */ /* end flashprog-query */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | |
| 113 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 114 | * @defgroup flashprog-prog Programmers |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 115 | * @{ |
| 116 | */ |
| 117 | |
| 118 | /** |
| 119 | * @brief Initialize the specified programmer. |
| 120 | * |
| 121 | * Currently, only one programmer may be initialized at a time. |
| 122 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 123 | * @param[out] flashprog Points to a pointer of type struct flashprog_programmer |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 124 | * that will be set if programmer initialization succeeds. |
| 125 | * *flashprog has to be shutdown by the caller with @ref |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 126 | * flashprog_programmer_shutdown. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 127 | * @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 Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 131 | int flashprog_programmer_init(struct flashprog_programmer **const flashprog, |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 132 | const char *const prog_name, const char *const prog_param) |
| 133 | { |
| 134 | unsigned prog; |
| 135 | |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 136 | for (prog = 0; prog < programmer_table_size; prog++) { |
Thomas Heijligen | 633d6db | 2021-03-31 19:09:44 +0200 | [diff] [blame] | 137 | if (strcmp(prog_name, programmer_table[prog]->name) == 0) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 138 | break; |
| 139 | } |
Thomas Heijligen | d45cb59 | 2021-05-19 14:12:18 +0200 | [diff] [blame] | 140 | if (prog >= programmer_table_size) { |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 141 | msg_ginfo("Error: Unknown programmer \"%s\". Valid choices are:\n", prog_name); |
| 142 | list_programmers_linebreak(0, 80, 0); |
Nico Huber | dafd51e | 2023-02-10 23:58:19 +0100 | [diff] [blame] | 143 | msg_ginfo(".\n"); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 144 | return 1; |
| 145 | } |
Nico Huber | 2b66ad9 | 2023-01-11 20:15:15 +0100 | [diff] [blame] | 146 | |
| 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: |
| 170 | free((*flashprog)->param); |
| 171 | free(*flashprog); |
| 172 | return 1; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | /** |
| 176 | * @brief Shut down the initialized programmer. |
| 177 | * |
| 178 | * @param flashprog The programmer to shut down. |
| 179 | * @return 0 on success |
| 180 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 181 | int flashprog_programmer_shutdown(struct flashprog_programmer *const flashprog) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 182 | { |
Nico Huber | 2b66ad9 | 2023-01-11 20:15:15 +0100 | [diff] [blame] | 183 | if (programmer_shutdown(flashprog)) |
| 184 | return 1; |
| 185 | free(flashprog); |
| 186 | return 0; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 189 | /* TODO: flashprog_programmer_capabilities()? */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 190 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 191 | /** @} */ /* end flashprog-prog */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 192 | |
| 193 | |
| 194 | |
| 195 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 196 | * @defgroup flashprog-flash Flash chips |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 197 | * @{ |
| 198 | */ |
| 199 | |
| 200 | /** |
| 201 | * @brief Probe for a flash chip. |
| 202 | * |
| 203 | * Probes for a flash chip and returns a flash context, that can be used |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 204 | * later with flash chip and @ref flashprog-ops "image operations", if |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 205 | * exactly one matching chip is found. |
| 206 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 207 | * @param[out] flashctx Points to a pointer of type struct flashprog_flashctx |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 208 | * that will be set if exactly one chip is found. *flashctx |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 209 | * has to be freed by the caller with @ref flashprog_flash_release. |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 210 | * @param[in] flashprog The flash programmer used to access the chip. |
| 211 | * @param[in] chip_name Name of a chip to probe for, or NULL to probe for |
| 212 | * all known chips. |
| 213 | * @return 0 on success, |
| 214 | * 3 if multiple chips were found, |
| 215 | * 2 if no chip was found, |
| 216 | * or 1 on any other error. |
| 217 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 218 | int flashprog_flash_probe(struct flashprog_flashctx **const flashctx, |
| 219 | const struct flashprog_programmer *const flashprog, |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 220 | const char *const chip_name) |
| 221 | { |
| 222 | int i, ret = 2; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 223 | struct flashprog_flashctx second_flashctx = { 0, }; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 224 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 225 | chip_to_probe = chip_name; /* chip_to_probe is global in flashprog.c */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 226 | |
| 227 | *flashctx = malloc(sizeof(**flashctx)); |
| 228 | if (!*flashctx) |
| 229 | return 1; |
| 230 | memset(*flashctx, 0, sizeof(**flashctx)); |
| 231 | |
| 232 | for (i = 0; i < registered_master_count; ++i) { |
| 233 | int flash_idx = -1; |
| 234 | if (!ret || (flash_idx = probe_flash(®istered_masters[i], 0, *flashctx, 0)) != -1) { |
| 235 | ret = 0; |
| 236 | /* We found one chip, now check that there is no second match. */ |
| 237 | if (probe_flash(®istered_masters[i], flash_idx + 1, &second_flashctx, 0) != -1) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 238 | flashprog_layout_release(second_flashctx.default_layout); |
Nico Huber | 38450ce | 2019-06-16 20:07:28 +0200 | [diff] [blame] | 239 | free(second_flashctx.chip); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 240 | ret = 3; |
| 241 | break; |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | if (ret) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 246 | flashprog_flash_release(*flashctx); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 247 | *flashctx = NULL; |
| 248 | } |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * @brief Returns the size of the specified flash chip in bytes. |
| 254 | * |
| 255 | * @param flashctx The queried flash context. |
| 256 | * @return Size of flash chip in bytes. |
| 257 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 258 | size_t flashprog_flash_getsize(const struct flashprog_flashctx *const flashctx) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 259 | { |
| 260 | return flashctx->chip->total_size * 1024; |
| 261 | } |
| 262 | |
| 263 | /** |
| 264 | * @brief Free a flash context. |
| 265 | * |
| 266 | * @param flashctx Flash context to free. |
| 267 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 268 | void flashprog_flash_release(struct flashprog_flashctx *const flashctx) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 269 | { |
Nico Huber | 6fb2f62 | 2022-02-24 18:17:40 +0100 | [diff] [blame] | 270 | if (!flashctx) |
| 271 | return; |
| 272 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 273 | flashprog_layout_release(flashctx->default_layout); |
Nico Huber | 38450ce | 2019-06-16 20:07:28 +0200 | [diff] [blame] | 274 | free(flashctx->chip); |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 275 | free(flashctx); |
| 276 | } |
| 277 | |
| 278 | /** |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 279 | * @brief Set the progress callback function. |
| 280 | * |
| 281 | * Set a callback function which will be invoked whenever libflashprog wants |
| 282 | * to indicate the progress has changed. This allows frontends to do whatever |
| 283 | * they see fit with such values, e.g. update a progress bar in a GUI tool. |
| 284 | * |
| 285 | * @param flashctx Current flash context. |
| 286 | * @param progress_callback Pointer to the new progress callback function. |
| 287 | * @param user_data Pointer to any data the API user wants to have passed to the callback. |
| 288 | */ |
| 289 | void flashprog_set_progress_callback(struct flashprog_flashctx *const flashctx, |
| 290 | flashprog_progress_callback *const progress_callback, |
| 291 | void *const user_data) |
| 292 | { |
| 293 | flashctx->progress.callback = progress_callback; |
| 294 | flashctx->progress.user_data = user_data; |
| 295 | } |
| 296 | |
| 297 | /** |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 298 | * @brief Set a flag in the given flash context. |
| 299 | * |
| 300 | * @param flashctx Flash context to alter. |
| 301 | * @param flag Flag that is to be set / cleared. |
| 302 | * @param value Value to set. |
| 303 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 304 | void flashprog_flag_set(struct flashprog_flashctx *const flashctx, |
| 305 | const enum flashprog_flag flag, const bool value) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 306 | { |
| 307 | switch (flag) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 308 | case FLASHPROG_FLAG_FORCE: flashctx->flags.force = value; break; |
| 309 | case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: flashctx->flags.force_boardmismatch = value; break; |
| 310 | case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: flashctx->flags.verify_after_write = value; break; |
| 311 | case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: flashctx->flags.verify_whole_chip = value; break; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 312 | } |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * @brief Return the current value of a flag in the given flash context. |
| 317 | * |
| 318 | * @param flashctx Flash context to read from. |
| 319 | * @param flag Flag to be read. |
| 320 | * @return Current value of the flag. |
| 321 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 322 | bool flashprog_flag_get(const struct flashprog_flashctx *const flashctx, const enum flashprog_flag flag) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 323 | { |
| 324 | switch (flag) { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 325 | case FLASHPROG_FLAG_FORCE: return flashctx->flags.force; |
| 326 | case FLASHPROG_FLAG_FORCE_BOARDMISMATCH: return flashctx->flags.force_boardmismatch; |
| 327 | case FLASHPROG_FLAG_VERIFY_AFTER_WRITE: return flashctx->flags.verify_after_write; |
| 328 | case FLASHPROG_FLAG_VERIFY_WHOLE_CHIP: return flashctx->flags.verify_whole_chip; |
| 329 | default: return false; |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 333 | /** @} */ /* end flashprog-flash */ |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 334 | |
| 335 | |
| 336 | |
| 337 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 338 | * @defgroup flashprog-layout Layout handling |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 339 | * @{ |
| 340 | */ |
| 341 | |
| 342 | /** |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 343 | * @brief Read a layout from the Intel ICH descriptor in the flash. |
| 344 | * |
| 345 | * Optionally verify that the layout matches the one in the given |
| 346 | * descriptor dump. |
| 347 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 348 | * @param[out] layout Points to a struct flashprog_layout pointer that |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 349 | * gets set if the descriptor is read and parsed |
| 350 | * successfully. |
| 351 | * @param[in] flashctx Flash context to read the descriptor from flash. |
| 352 | * @param[in] dump The descriptor dump to compare to or NULL. |
| 353 | * @param[in] len The length of the descriptor dump. |
| 354 | * |
| 355 | * @return 0 on success, |
| 356 | * 6 if descriptor parsing isn't implemented for the host, |
| 357 | * 5 if the descriptors don't match, |
| 358 | * 4 if the descriptor dump couldn't be parsed, |
| 359 | * 3 if the descriptor on flash couldn't be parsed, |
| 360 | * 2 if the descriptor on flash couldn't be read, |
| 361 | * 1 on any other error. |
| 362 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 363 | int flashprog_layout_read_from_ifd(struct flashprog_layout **const layout, struct flashctx *const flashctx, |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 364 | const void *const dump, const size_t len) |
| 365 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 366 | #ifndef __FLASHPROG_LITTLE_ENDIAN__ |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 367 | return 6; |
| 368 | #else |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 369 | struct flashprog_layout *dump_layout = NULL, *chip_layout = NULL; |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 370 | int ret = 1; |
| 371 | |
| 372 | void *const desc = malloc(0x1000); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 373 | if (prepare_flash_access(flashctx, true, false, false, false)) |
| 374 | goto _free_ret; |
| 375 | |
| 376 | msg_cinfo("Reading ich descriptor... "); |
Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 377 | if (flashprog_read_range(flashctx, desc, 0, 0x1000)) { |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 378 | msg_cerr("Read operation failed!\n"); |
| 379 | msg_cinfo("FAILED.\n"); |
| 380 | ret = 2; |
| 381 | goto _finalize_ret; |
| 382 | } |
| 383 | msg_cinfo("done.\n"); |
| 384 | |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 385 | if (layout_from_ich_descriptors(&chip_layout, desc, 0x1000)) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 386 | msg_cerr("Couldn't parse the descriptor!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 387 | ret = 3; |
| 388 | goto _finalize_ret; |
| 389 | } |
| 390 | |
| 391 | if (dump) { |
| 392 | if (layout_from_ich_descriptors(&dump_layout, dump, len)) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 393 | msg_cerr("Couldn't parse the descriptor!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 394 | ret = 4; |
| 395 | goto _finalize_ret; |
| 396 | } |
| 397 | |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 398 | const struct romentry *chip_entry = layout_next(chip_layout, NULL); |
| 399 | const struct romentry *dump_entry = layout_next(dump_layout, NULL); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 400 | while (chip_entry && dump_entry && !memcmp(chip_entry, dump_entry, sizeof(*chip_entry))) { |
Nico Huber | 5bd990c | 2019-06-16 19:46:46 +0200 | [diff] [blame] | 401 | chip_entry = layout_next(chip_layout, chip_entry); |
| 402 | dump_entry = layout_next(dump_layout, dump_entry); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 403 | } |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 404 | flashprog_layout_release(dump_layout); |
Nico Huber | 354766b | 2019-06-16 19:28:35 +0200 | [diff] [blame] | 405 | if (chip_entry || dump_entry) { |
Patrick Rudolph | 911b8d8 | 2019-06-06 11:23:55 +0200 | [diff] [blame] | 406 | msg_cerr("Descriptors don't match!\n"); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 407 | ret = 5; |
| 408 | goto _finalize_ret; |
| 409 | } |
| 410 | } |
| 411 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 412 | *layout = (struct flashprog_layout *)chip_layout; |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 413 | ret = 0; |
| 414 | |
| 415 | _finalize_ret: |
| 416 | finalize_flash_access(flashctx); |
| 417 | _free_ret: |
| 418 | if (ret) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 419 | flashprog_layout_release(chip_layout); |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 420 | free(desc); |
| 421 | return ret; |
| 422 | #endif |
| 423 | } |
| 424 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 425 | #ifdef __FLASHPROG_LITTLE_ENDIAN__ |
| 426 | static int flashprog_layout_parse_fmap(struct flashprog_layout **layout, |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 427 | struct flashctx *const flashctx, const struct fmap *const fmap) |
| 428 | { |
| 429 | int i; |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 430 | char name[FMAP_STRLEN + 1]; |
| 431 | const struct fmap_area *area; |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 432 | struct flashprog_layout *l; |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 433 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 434 | if (!fmap || flashprog_layout_new(&l)) |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 435 | return 1; |
| 436 | |
Nico Huber | 92e0b62 | 2019-06-15 15:55:11 +0200 | [diff] [blame] | 437 | for (i = 0, area = fmap->areas; i < fmap->nareas; i++, area++) { |
| 438 | snprintf(name, sizeof(name), "%s", area->name); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 439 | if (flashprog_layout_add_region(l, area->offset, area->offset + area->size - 1, name)) { |
| 440 | flashprog_layout_release(l); |
Nico Huber | 70461a9 | 2019-06-15 14:56:19 +0200 | [diff] [blame] | 441 | return 1; |
Nico Huber | efe96a9 | 2021-05-14 00:39:24 +0200 | [diff] [blame] | 442 | } |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | *layout = l; |
| 446 | return 0; |
| 447 | } |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 448 | #endif /* __FLASHPROG_LITTLE_ENDIAN__ */ |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 449 | |
| 450 | /** |
| 451 | * @brief Read a layout by searching the flash chip for fmap. |
| 452 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 453 | * @param[out] layout Points to a struct flashprog_layout pointer that |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 454 | * gets set if the fmap is read and parsed successfully. |
| 455 | * @param[in] flashctx Flash context |
| 456 | * @param[in] offset Offset to begin searching for fmap. |
| 457 | * @param[in] offset Length of address space to search. |
| 458 | * |
| 459 | * @return 0 on success, |
| 460 | * 3 if fmap parsing isn't implemented for the host, |
| 461 | * 2 if the fmap couldn't be read, |
| 462 | * 1 on any other error. |
| 463 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 464 | int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **const layout, |
Julius Werner | 8f0db7d | 2022-02-14 17:07:39 -0800 | [diff] [blame] | 465 | struct flashctx *const flashctx, size_t offset, size_t len) |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 466 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 467 | #ifndef __FLASHPROG_LITTLE_ENDIAN__ |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 468 | return 3; |
| 469 | #else |
| 470 | struct fmap *fmap = NULL; |
| 471 | int ret = 0; |
| 472 | |
| 473 | msg_gdbg("Attempting to read fmap from ROM content.\n"); |
| 474 | if (fmap_read_from_rom(&fmap, flashctx, offset, len)) { |
| 475 | msg_gerr("Failed to read fmap from ROM.\n"); |
| 476 | return 1; |
| 477 | } |
| 478 | |
| 479 | msg_gdbg("Adding fmap layout to global layout.\n"); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 480 | if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) { |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 481 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 482 | ret = 1; |
| 483 | } |
| 484 | |
| 485 | free(fmap); |
| 486 | return ret; |
| 487 | #endif |
| 488 | } |
| 489 | |
| 490 | /** |
| 491 | * @brief Read a layout by searching buffer for fmap. |
| 492 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 493 | * @param[out] layout Points to a struct flashprog_layout pointer that |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 494 | * gets set if the fmap is read and parsed successfully. |
| 495 | * @param[in] flashctx Flash context |
| 496 | * @param[in] buffer Buffer to search in |
| 497 | * @param[in] size Size of buffer to search |
| 498 | * |
| 499 | * @return 0 on success, |
| 500 | * 3 if fmap parsing isn't implemented for the host, |
| 501 | * 2 if the fmap couldn't be read, |
| 502 | * 1 on any other error. |
| 503 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 504 | int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **const layout, |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 505 | struct flashctx *const flashctx, const uint8_t *const buf, size_t size) |
| 506 | { |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 507 | #ifndef __FLASHPROG_LITTLE_ENDIAN__ |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 508 | return 3; |
| 509 | #else |
| 510 | struct fmap *fmap = NULL; |
| 511 | int ret = 1; |
| 512 | |
| 513 | if (!buf || !size) |
| 514 | goto _ret; |
| 515 | |
| 516 | msg_gdbg("Attempting to read fmap from buffer.\n"); |
| 517 | if (fmap_read_from_buffer(&fmap, buf, size)) { |
| 518 | msg_gerr("Failed to read fmap from buffer.\n"); |
| 519 | goto _ret; |
| 520 | } |
| 521 | |
| 522 | msg_gdbg("Adding fmap layout to global layout.\n"); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 523 | if (flashprog_layout_parse_fmap(layout, flashctx, fmap)) { |
Arthur Heymans | c82900b | 2018-01-10 12:48:16 +0100 | [diff] [blame] | 524 | msg_gerr("Failed to add fmap regions to layout.\n"); |
| 525 | goto _free_ret; |
| 526 | } |
| 527 | |
| 528 | ret = 0; |
| 529 | _free_ret: |
| 530 | free(fmap); |
| 531 | _ret: |
| 532 | return ret; |
| 533 | #endif |
| 534 | } |
| 535 | |
Nico Huber | 305f417 | 2013-06-14 11:55:26 +0200 | [diff] [blame] | 536 | /** |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 537 | * @brief Set the active layout for a flash context. |
| 538 | * |
| 539 | * Note: This just sets a pointer. The caller must not release the layout |
| 540 | * as long as he uses it through the given flash context. |
| 541 | * |
| 542 | * @param flashctx Flash context whose layout will be set. |
| 543 | * @param layout Layout to bet set. |
| 544 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 545 | void flashprog_layout_set(struct flashprog_flashctx *const flashctx, const struct flashprog_layout *const layout) |
Nico Huber | 454f613 | 2012-12-10 13:34:10 +0000 | [diff] [blame] | 546 | { |
| 547 | flashctx->layout = layout; |
| 548 | } |
| 549 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 550 | /** @} */ /* end flashprog-layout */ |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 551 | |
| 552 | |
| 553 | /** |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 554 | * @defgroup flashprog-wp |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 555 | * @{ |
| 556 | */ |
| 557 | |
| 558 | /** |
| 559 | * @brief Create a new empty WP configuration. |
| 560 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 561 | * @param[out] cfg Points to a pointer of type struct flashprog_wp_cfg that will |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 562 | * be set if creation succeeds. *cfg has to be freed by the |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 563 | * caller with @ref flashprog_wp_cfg_release. |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 564 | * @return 0 on success |
| 565 | * >0 on failure |
| 566 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 567 | enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 568 | { |
| 569 | *cfg = calloc(1, sizeof(**cfg)); |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 570 | return *cfg ? 0 : FLASHPROG_WP_ERR_OTHER; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | /** |
| 574 | * @brief Free a WP configuration. |
| 575 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 576 | * @param[out] cfg Pointer to the flashprog_wp_cfg to free. |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 577 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 578 | void flashprog_wp_cfg_release(struct flashprog_wp_cfg *cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 579 | { |
| 580 | free(cfg); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * @brief Set the protection mode for a WP configuration. |
| 585 | * |
| 586 | * @param[in] mode The protection mode to set. |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 587 | * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify. |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 588 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 589 | void flashprog_wp_set_mode(struct flashprog_wp_cfg *cfg, enum flashprog_wp_mode mode) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 590 | { |
| 591 | cfg->mode = mode; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * @brief Get the protection mode from a WP configuration. |
| 596 | * |
| 597 | * @param[in] cfg The WP configuration to get the protection mode from. |
| 598 | * @return The configuration's protection mode. |
| 599 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 600 | enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 601 | { |
| 602 | return cfg->mode; |
| 603 | } |
| 604 | |
| 605 | /** |
| 606 | * @brief Set the protection range for a WP configuration. |
| 607 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 608 | * @param[out] cfg Pointer to the flashprog_wp_cfg structure to modify. |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 609 | * @param[in] start The range's start address. |
| 610 | * @param[in] len The range's length. |
| 611 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 612 | void flashprog_wp_set_range(struct flashprog_wp_cfg *cfg, size_t start, size_t len) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 613 | { |
| 614 | cfg->range.start = start; |
| 615 | cfg->range.len = len; |
| 616 | } |
| 617 | |
| 618 | /** |
| 619 | * @brief Get the protection range from a WP configuration. |
| 620 | * |
| 621 | * @param[out] start Points to a size_t to write the range start to. |
| 622 | * @param[out] len Points to a size_t to write the range length to. |
| 623 | * @param[in] cfg The WP configuration to get the range from. |
| 624 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 625 | void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 626 | { |
| 627 | *start = cfg->range.start; |
| 628 | *len = cfg->range.len; |
| 629 | } |
| 630 | |
| 631 | /** |
| 632 | * @brief Write a WP configuration to a flash chip. |
| 633 | * |
| 634 | * @param[in] flash The flash context used to access the chip. |
| 635 | * @param[in] cfg The WP configuration to write to the chip. |
| 636 | * @return 0 on success |
| 637 | * >0 on failure |
| 638 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 639 | enum flashprog_wp_result flashprog_wp_write_cfg(struct flashctx *flash, const struct flashprog_wp_cfg *cfg) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 640 | { |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 641 | if (!flash->chip->wp_write_cfg) |
| 642 | return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 643 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 644 | return flash->chip->wp_write_cfg(flash, cfg); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | /** |
| 648 | * @brief Read the current WP configuration from a flash chip. |
| 649 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 650 | * @param[out] cfg Pointer to a struct flashprog_wp_cfg to store the chip's |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 651 | * configuration in. |
| 652 | * @param[in] flash The flash context used to access the chip. |
| 653 | * @return 0 on success |
| 654 | * >0 on failure |
| 655 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 656 | enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *cfg, struct flashctx *flash) |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 657 | { |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 658 | if (!flash->chip->wp_read_cfg) |
| 659 | return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 660 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 661 | return flash->chip->wp_read_cfg(cfg, flash); |
Nikolai Artemiev | da1c834 | 2021-10-21 00:58:12 +1100 | [diff] [blame] | 662 | } |
| 663 | |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 664 | /** |
| 665 | * @brief Get a list of protection ranges supported by the flash chip. |
| 666 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 667 | * @param[out] ranges Points to a pointer of type struct flashprog_wp_ranges |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 668 | * that will be set if available ranges are found. Finding |
| 669 | * available ranges may not always be possible, even if the |
| 670 | * chip's protection range can be read or modified. *ranges |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 671 | * must be freed using @ref flashprog_wp_ranges_free. |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 672 | * @param[in] flash The flash context used to access the chip. |
| 673 | * @return 0 on success |
| 674 | * >0 on failure |
| 675 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 676 | enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **list, struct flashprog_flashctx *flash) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 677 | { |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 678 | if (!flash->chip->wp_get_ranges) |
| 679 | return FLASHPROG_WP_ERR_CHIP_UNSUPPORTED; |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 680 | |
Nico Huber | aabb3e0 | 2023-01-13 00:22:30 +0100 | [diff] [blame] | 681 | return flash->chip->wp_get_ranges(list, flash); |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 682 | } |
| 683 | |
| 684 | /** |
| 685 | * @brief Get a number of protection ranges in a range list. |
| 686 | * |
| 687 | * @param[in] ranges The range list to get the count from. |
| 688 | * @return Number of ranges in the list. |
| 689 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 690 | size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *list) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 691 | { |
| 692 | return list->count; |
| 693 | } |
| 694 | |
| 695 | /** |
| 696 | * @brief Get a protection range from a range list. |
| 697 | * |
| 698 | * @param[out] start Points to a size_t to write the range's start to. |
| 699 | * @param[out] len Points to a size_t to write the range's length to. |
| 700 | * @param[in] ranges The range list to get the range from. |
| 701 | * @param[in] index Index of the range to get. |
| 702 | * @return 0 on success |
| 703 | * >0 on failure |
| 704 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 705 | enum flashprog_wp_result flashprog_wp_ranges_get_range(size_t *start, size_t *len, const struct flashprog_wp_ranges *list, unsigned int index) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 706 | { |
| 707 | if (index >= list->count) |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 708 | return FLASHPROG_WP_ERR_OTHER; |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 709 | |
| 710 | *start = list->ranges[index].start; |
| 711 | *len = list->ranges[index].len; |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | /** |
| 717 | * @brief Free a WP range list. |
| 718 | * |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 719 | * @param[out] cfg Pointer to the flashprog_wp_ranges to free. |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 720 | */ |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 721 | void flashprog_wp_ranges_release(struct flashprog_wp_ranges *list) |
Nikolai Artemiev | 077c0d1 | 2021-10-21 01:50:15 +1100 | [diff] [blame] | 722 | { |
| 723 | if (!list) |
| 724 | return; |
| 725 | |
| 726 | free(list->ranges); |
| 727 | free(list); |
| 728 | } |
| 729 | |
| 730 | |
Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 731 | /** @} */ /* end flashprog-wp */ |