| Nico Huber | 48d4a04 | 2026-03-08 16:07:28 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * This file is part of the flashprog project. |
| 3 | * |
| 4 | * Copyright (C) 2026 Nico Huber <nico.h@gmx.de> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | |
| 19 | #define flashprog_chip flashchip /* For now, we use direct pointers */ |
| 20 | #include "libflashprog.h" /* to the internal struct flashchip. */ |
| 21 | |
| 22 | #include "flash.h" |
| 23 | #include "flashchips.h" |
| 24 | |
| 25 | /** |
| 26 | * @defgroup flashprog-chips Chip Enumeration |
| 27 | * @{ |
| 28 | */ |
| 29 | |
| 30 | /* Magic pointer that represents our built in database. */ |
| 31 | #define FLASHCHIPS_DB (struct flashprog_chips *)(uintptr_t)-1 |
| 32 | |
| 33 | static bool chip_from_db(const struct flashprog_chip *chip) { |
| 34 | return flashchips <= chip && chip < flashchips + flashchips_size; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @brief Enumerate the internal chips database. |
| 39 | * |
| 40 | * @param[out] chips Points to a struct flashprog_chips pointer that gets |
| 41 | * set if the enumeration is successful. *chips has to be |
| 42 | * freed by the caller with @ref flashprog_chips_release. |
| 43 | * @return 0 on success |
| 44 | */ |
| 45 | int flashprog_chips_all(struct flashprog_chips **chips) { |
| 46 | *chips = FLASHCHIPS_DB; |
| 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * @brief Count the chips in an enumeration. |
| 52 | * |
| 53 | * @return The number of chips. |
| 54 | */ |
| 55 | unsigned int flashprog_chips_count(const struct flashprog_chips *chips) |
| 56 | { |
| 57 | unsigned int count = 0; |
| 58 | const struct flashprog_chip *chip; |
| 59 | for (chip = flashprog_chip_first(chips); chip; chip = flashprog_chip_next(chip)) |
| 60 | ++count; |
| 61 | return count; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * @brief Free a set of enumerated chips. |
| 66 | * |
| 67 | * This also invalidates all references that were acquired via |
| 68 | * @ref flashprog_chip_first or @ref flashprog_chip_next from |
| 69 | * the given set. |
| 70 | * |
| 71 | * @param chips Chip enumeration to free. |
| 72 | */ |
| 73 | void flashprog_chips_release(struct flashprog_chips *chips) |
| 74 | { |
| 75 | if (chips == FLASHCHIPS_DB) |
| 76 | return; |
| 77 | |
| 78 | free(chips); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * @brief Starts an iteration over a given set of enumerated chips. |
| 83 | * |
| 84 | * The referenced chip structure will stay valid until either the iteration |
| 85 | * is advanced (@ref flashprog_chip_next) or the provided chips enumeration |
| 86 | * is released. Note, each call may allocated additional resources that are |
| 87 | * only freed by walking the iteration to its end, or releasing the entire |
| 88 | * chips set. |
| 89 | * |
| 90 | * @param chips A set of enumerated chips. |
| 91 | * @return A pointer to the structure of the first chip or NULL if the set is empty. |
| 92 | */ |
| 93 | const struct flashprog_chip *flashprog_chip_first(const struct flashprog_chips *chips) |
| 94 | { |
| 95 | if (chips == FLASHCHIPS_DB) |
| 96 | return flashchips; |
| 97 | |
| 98 | return NULL; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * @brief Iterates to the next chip structure in a set of enumerated chips. |
| 103 | * |
| 104 | * The referenced chip structure will stay valid until either the iteration |
| 105 | * is advanced further or the original chips enumeration is released (cf. |
| 106 | * @ref flashprog_chip_first). |
| 107 | * |
| 108 | * @param chip The previous chip in the enumeration. The referenced |
| 109 | * structure will be invalidated by the call. |
| 110 | * @return A pointer to the structure of the next chip or NULL if there is none. |
| 111 | */ |
| 112 | const struct flashprog_chip *flashprog_chip_next(const struct flashprog_chip *chip) |
| 113 | { |
| 114 | if (chip_from_db(chip)) { |
| 115 | for (++chip; chip->name; ++chip) { |
| 116 | if (chip->id.manufacture == PROGMANUF_ID) |
| 117 | continue; |
| 118 | if (chip->id.manufacture == GENERIC_MANUF_ID) |
| 119 | continue; |
| 120 | if (chip->id.model == GENERIC_DEVICE_ID) |
| 121 | continue; |
| 122 | return chip; |
| 123 | } |
| 124 | return NULL; |
| 125 | } |
| 126 | |
| 127 | return NULL; |
| 128 | } |
| 129 | |
| 130 | /** @} */ /* end flashprog-chips */ |
| 131 | |
| 132 | |
| 133 | /** |
| 134 | * @defgroup flashprog-chip Chip Information |
| 135 | * @{ |
| 136 | */ |
| 137 | |
| 138 | /** |
| 139 | * @brief Get the vendor string of a given chip structure. |
| 140 | * |
| 141 | * @param chip reference to query. |
| 142 | * @return Vendor string. |
| 143 | */ |
| 144 | const char *flashprog_chip_vendor(const struct flashprog_chip *chip) { |
| 145 | return chip->vendor; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @brief Get the name string of a given chip structure. |
| 150 | * |
| 151 | * @param chip reference to query. |
| 152 | * @return Name string. |
| 153 | */ |
| 154 | const char *flashprog_chip_name(const struct flashprog_chip *chip) { |
| 155 | return chip->name; |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * @brief Get the size of a given chip in bytes. |
| 160 | * |
| 161 | * @param chip reference to query. |
| 162 | * @return Size in bytes. |
| 163 | */ |
| 164 | size_t flashprog_chip_size(const struct flashprog_chip *chip) { |
| 165 | return chip->total_size * KiB; |
| 166 | } |
| 167 | |
| 168 | /** @} */ /* end flashprog-chip */ |