blob: 0b43e6f20fbba368b52a32beb36bf6e8e95391c5 [file] [log] [blame]
Nico Huber48d4a042026-03-08 16:07:28 +01001/*
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
33static 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 */
45int 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 */
55unsigned 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 */
73void 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 */
93const 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 */
112const 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 */
144const 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 */
154const 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 */
164size_t flashprog_chip_size(const struct flashprog_chip *chip) {
165 return chip->total_size * KiB;
166}
167
Nico Huber0d2b45e2026-03-23 22:42:46 +0100168/**
169 * @brief Get a bit mask of the supported bus types.
170 *
171 * @param chip reference to query.
172 * @return bit mask of supported bus types.
173 */
174enum flashprog_bus_type flashprog_chip_buses(const struct flashprog_chip *chip) {
175 return chip->bustype;
176}
177
178/**
179 * @brief Get a string that lists the supported bus types.
180 *
181 * The resulting string needs to be freed with free().
182 *
183 * @param chip reference to query.
184 * @return comma-separated string of supported bus types.
185 */
186char *flashprog_chip_bus_names(const struct flashprog_chip *chip) {
187 return flashbuses_to_text(chip->bustype);
188}
189
190/**
191 * @brief Get the supported operating voltage range.
192 *
193 * @param chip reference to query.
194 * @return supported operating voltage range.
195 */
196struct flashprog_voltage_range flashprog_chip_voltage_range(const struct flashprog_chip *chip)
197{
198 return (struct flashprog_voltage_range) {
199 .min = chip->voltage.min / 1000.f,
200 .max = chip->voltage.max / 1000.f,
201 };
202}
203
204/**
205 * @brief Get the test status of a chip's standard features.
206 *
207 * @param chip reference to query.
208 * @return struct with the test status of the chip's standard features.
209 */
210struct flashprog_test_status flashprog_chip_test_status(const struct flashprog_chip *chip) {
211 return chip->tested;
212}
213
Nico Huber48d4a042026-03-08 16:07:28 +0100214/** @} */ /* end flashprog-chip */