| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright (C) 2010 Google Inc. |
| 5 | * Copyright (C) 2012 secunet Security Networks AG |
| 6 | * (Written by Nico Huber <nico.huber@secunet.com> for secunet) |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | */ |
| 18 | |
| 19 | #ifndef __LIBFLASHPROG_H__ |
| 20 | #define __LIBFLASHPROG_H__ 1 |
| 21 | |
| 22 | #include <sys/types.h> |
| 23 | #include <stddef.h> |
| 24 | #include <stdbool.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdarg.h> |
| 27 | |
| 28 | int flashprog_init(int perform_selfcheck); |
| 29 | int flashprog_shutdown(void); |
| 30 | /** @ingroup flashprog-general */ |
| 31 | enum flashprog_log_level { |
| 32 | FLASHPROG_MSG_ERROR = 0, |
| 33 | FLASHPROG_MSG_WARN = 1, |
| 34 | FLASHPROG_MSG_INFO = 2, |
| 35 | FLASHPROG_MSG_DEBUG = 3, |
| 36 | FLASHPROG_MSG_DEBUG2 = 4, |
| 37 | FLASHPROG_MSG_SPEW = 5, |
| 38 | }; |
| 39 | /** @ingroup flashprog-general */ |
| 40 | typedef int(flashprog_log_callback)(enum flashprog_log_level, const char *format, va_list); |
| 41 | void flashprog_set_log_callback(flashprog_log_callback *); |
| 42 | |
| 43 | /** @ingroup flashprog-prog */ |
| 44 | struct flashprog_programmer; |
| 45 | int flashprog_programmer_init(struct flashprog_programmer **, const char *prog_name, const char *prog_params); |
| 46 | int flashprog_programmer_shutdown(struct flashprog_programmer *); |
| 47 | |
| Nico Huber | 48d4a04 | 2026-03-08 16:07:28 +0100 | [diff] [blame] | 48 | struct flashprog_chips; |
| 49 | __attribute__((nonnull)) |
| 50 | int flashprog_chips_all(struct flashprog_chips **); |
| 51 | __attribute__((nonnull)) |
| 52 | unsigned int flashprog_chips_count(const struct flashprog_chips *); |
| 53 | void flashprog_chips_release(struct flashprog_chips *); |
| 54 | |
| 55 | struct flashprog_chip; |
| 56 | __attribute__((nonnull)) |
| 57 | const struct flashprog_chip *flashprog_chip_first(const struct flashprog_chips *); |
| 58 | __attribute__((nonnull)) |
| 59 | const struct flashprog_chip *flashprog_chip_next(const struct flashprog_chip *); |
| 60 | __attribute__((nonnull)) |
| 61 | const char *flashprog_chip_vendor(const struct flashprog_chip *); |
| 62 | __attribute__((nonnull)) |
| 63 | const char *flashprog_chip_name(const struct flashprog_chip *); |
| 64 | __attribute__((nonnull)) |
| 65 | size_t flashprog_chip_size(const struct flashprog_chip *); |
| 66 | |
| Nico Huber | 0d2b45e | 2026-03-23 22:42:46 +0100 | [diff] [blame^] | 67 | /** |
| 68 | * @brief Bit masks that represent supported bus types. |
| 69 | * @ingroup flashprog-chip |
| 70 | */ |
| 71 | enum flashprog_bus_type { |
| 72 | FLASHPROG_BUS_PARALLEL = 1 << 0, /**< Parallel flash chip */ |
| 73 | FLASHPROG_BUS_LPC = 1 << 1, /**< Low Pin Count (LPC) flash */ |
| 74 | FLASHPROG_BUS_FWH = 1 << 2, /**< Firmware Hub (FWH) flash */ |
| 75 | FLASHPROG_BUS_SPI = 1 << 3, /**< Serial Peripheral Interface (SPI) flash */ |
| 76 | FLASHPROG_BUS_OPAQUE = 1 << 4, /**< Chip behind an opaque bus interface */ |
| 77 | }; |
| 78 | __attribute__((nonnull)) |
| 79 | enum flashprog_bus_type flashprog_chip_buses(const struct flashprog_chip *); |
| 80 | __attribute__((nonnull)) |
| 81 | char *flashprog_chip_bus_names(const struct flashprog_chip *); |
| 82 | |
| 83 | /** |
| 84 | * @brief Documents the minimal and maximal operating voltage for a chip. |
| 85 | * @ingroup flashprog-chip |
| 86 | */ |
| 87 | struct flashprog_voltage_range { |
| 88 | float min; /**< Lowest operating voltage */ |
| 89 | float max; /**< Highest operating voltage */ |
| 90 | }; |
| 91 | __attribute__((nonnull)) |
| 92 | struct flashprog_voltage_range flashprog_chip_voltage_range(const struct flashprog_chip *); |
| 93 | |
| 94 | /** |
| 95 | * @brief Documents the test status of a chip operation. |
| 96 | * @ingroup flashprog-chip |
| 97 | */ |
| 98 | enum flashprog_test_state { |
| 99 | FLASHPROG_TEST_OK = 0, /**< Tested positively */ |
| 100 | FLASHPROG_TEST_NT = 1, /**< Not tested */ |
| 101 | FLASHPROG_TEST_BAD, /**< Known to not work */ |
| 102 | FLASHPROG_TEST_DEP, /**< Support depends on configuration (e.g. Intel flash descriptor) */ |
| 103 | FLASHPROG_TEST_NA, /**< Not applicable (e.g. write support on ROM chips) */ |
| 104 | }; |
| 105 | /** |
| 106 | * @brief Documents the test status of various chip operations. |
| 107 | * @ingroup flashprog-chip |
| 108 | */ |
| 109 | struct flashprog_test_status { |
| 110 | enum flashprog_test_state probe:3; /**< Test status for probing. */ |
| 111 | enum flashprog_test_state read:3; /**< Test status for reading. */ |
| 112 | enum flashprog_test_state erase:3; /**< Test status for erasing. */ |
| 113 | enum flashprog_test_state write:3; /**< Test status for writing. */ |
| 114 | enum flashprog_test_state block_protection:3; /**< Test status for block-protection configuration. */ |
| 115 | enum flashprog_test_state :3, :3, :3, :3, :3, :3, :3, :3, :3, :3, reserved:3; |
| 116 | /* XXX: Used as return value. Consider ABI compatibility when extending. */ |
| 117 | }; |
| 118 | __attribute__((nonnull)) |
| 119 | struct flashprog_test_status flashprog_chip_test_status(const struct flashprog_chip *); |
| 120 | |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 121 | struct flashprog_flashctx; |
| 122 | int flashprog_flash_probe(struct flashprog_flashctx **, const struct flashprog_programmer *, const char *chip_name); |
| 123 | size_t flashprog_flash_getsize(const struct flashprog_flashctx *); |
| 124 | int flashprog_flash_erase(struct flashprog_flashctx *); |
| 125 | void flashprog_flash_release(struct flashprog_flashctx *); |
| 126 | |
| Richard Hughes | 842d678 | 2021-01-15 09:48:12 +0000 | [diff] [blame] | 127 | enum flashprog_progress_stage { |
| 128 | FLASHPROG_PROGRESS_READ, |
| 129 | FLASHPROG_PROGRESS_WRITE, |
| 130 | FLASHPROG_PROGRESS_ERASE, |
| 131 | }; |
| 132 | typedef void(flashprog_progress_callback)(enum flashprog_progress_stage, size_t current, size_t total, void *user_data); |
| 133 | void flashprog_set_progress_callback(struct flashprog_flashctx *, flashprog_progress_callback *, void *user_data); |
| 134 | |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 135 | /** @ingroup flashprog-flash */ |
| 136 | enum flashprog_flag { |
| 137 | FLASHPROG_FLAG_FORCE, |
| 138 | FLASHPROG_FLAG_FORCE_BOARDMISMATCH, |
| 139 | FLASHPROG_FLAG_VERIFY_AFTER_WRITE, |
| 140 | FLASHPROG_FLAG_VERIFY_WHOLE_CHIP, |
| Nico Huber | 55e7884 | 2024-07-21 00:46:19 +0200 | [diff] [blame] | 141 | FLASHPROG_FLAG_NON_VOLATILE_WRSR, |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 142 | }; |
| 143 | void flashprog_flag_set(struct flashprog_flashctx *, enum flashprog_flag, bool value); |
| 144 | bool flashprog_flag_get(const struct flashprog_flashctx *, enum flashprog_flag); |
| 145 | |
| 146 | int flashprog_image_read(struct flashprog_flashctx *, void *buffer, size_t buffer_len); |
| 147 | int flashprog_image_write(struct flashprog_flashctx *, void *buffer, size_t buffer_len, const void *refbuffer); |
| 148 | int flashprog_image_verify(struct flashprog_flashctx *, const void *buffer, size_t buffer_len); |
| 149 | |
| 150 | struct flashprog_layout; |
| 151 | int flashprog_layout_new(struct flashprog_layout **); |
| 152 | int flashprog_layout_read_from_ifd(struct flashprog_layout **, struct flashprog_flashctx *, const void *dump, size_t len); |
| 153 | int flashprog_layout_read_fmap_from_rom(struct flashprog_layout **, |
| 154 | struct flashprog_flashctx *, size_t offset, size_t length); |
| 155 | int flashprog_layout_read_fmap_from_buffer(struct flashprog_layout **layout, |
| 156 | struct flashprog_flashctx *, const uint8_t *buf, size_t len); |
| 157 | int flashprog_layout_add_region(struct flashprog_layout *, size_t start, size_t end, const char *name); |
| 158 | int flashprog_layout_include_region(struct flashprog_layout *, const char *name); |
| Nico Huber | eed122d | 2023-02-11 02:05:07 +0100 | [diff] [blame] | 159 | int flashprog_layout_get_region_range(const struct flashprog_layout *, const char *name, size_t *start, size_t *len); |
| Nico Huber | c3b02dc | 2023-08-12 01:13:45 +0200 | [diff] [blame] | 160 | void flashprog_layout_release(struct flashprog_layout *); |
| 161 | void flashprog_layout_set(struct flashprog_flashctx *, const struct flashprog_layout *); |
| 162 | |
| 163 | /** @ingroup flashprog-wp */ |
| 164 | enum flashprog_wp_result { |
| 165 | FLASHPROG_WP_OK = 0, |
| 166 | FLASHPROG_WP_ERR_CHIP_UNSUPPORTED = 1, |
| 167 | FLASHPROG_WP_ERR_OTHER = 2, |
| 168 | FLASHPROG_WP_ERR_READ_FAILED = 3, |
| 169 | FLASHPROG_WP_ERR_WRITE_FAILED = 4, |
| 170 | FLASHPROG_WP_ERR_VERIFY_FAILED = 5, |
| 171 | FLASHPROG_WP_ERR_RANGE_UNSUPPORTED = 6, |
| 172 | FLASHPROG_WP_ERR_MODE_UNSUPPORTED = 7, |
| 173 | FLASHPROG_WP_ERR_RANGE_LIST_UNAVAILABLE = 8, |
| 174 | FLASHPROG_WP_ERR_UNSUPPORTED_STATE = 9, |
| 175 | }; |
| 176 | |
| 177 | enum flashprog_wp_mode { |
| 178 | FLASHPROG_WP_MODE_DISABLED, |
| 179 | FLASHPROG_WP_MODE_HARDWARE, |
| 180 | FLASHPROG_WP_MODE_POWER_CYCLE, |
| 181 | FLASHPROG_WP_MODE_PERMANENT |
| 182 | }; |
| 183 | struct flashprog_wp_cfg; |
| 184 | struct flashprog_wp_ranges; |
| 185 | |
| 186 | enum flashprog_wp_result flashprog_wp_cfg_new(struct flashprog_wp_cfg **); |
| 187 | void flashprog_wp_cfg_release(struct flashprog_wp_cfg *); |
| 188 | void flashprog_wp_set_mode(struct flashprog_wp_cfg *, enum flashprog_wp_mode); |
| 189 | enum flashprog_wp_mode flashprog_wp_get_mode(const struct flashprog_wp_cfg *); |
| 190 | void flashprog_wp_set_range(struct flashprog_wp_cfg *, size_t start, size_t len); |
| 191 | void flashprog_wp_get_range(size_t *start, size_t *len, const struct flashprog_wp_cfg *); |
| 192 | |
| 193 | enum flashprog_wp_result flashprog_wp_read_cfg(struct flashprog_wp_cfg *, struct flashprog_flashctx *); |
| 194 | enum flashprog_wp_result flashprog_wp_write_cfg(struct flashprog_flashctx *, const struct flashprog_wp_cfg *); |
| 195 | |
| 196 | enum flashprog_wp_result flashprog_wp_get_available_ranges(struct flashprog_wp_ranges **, struct flashprog_flashctx *); |
| 197 | size_t flashprog_wp_ranges_get_count(const struct flashprog_wp_ranges *); |
| 198 | enum flashprog_wp_result flashprog_wp_ranges_get_range(size_t *start, size_t *len, const struct flashprog_wp_ranges *, unsigned int index); |
| 199 | void flashprog_wp_ranges_release(struct flashprog_wp_ranges *); |
| 200 | |
| 201 | #endif /* !__LIBFLASHPROG_H__ */ |