Skip to content

Commit 0b0925e

Browse files
abonislawskilgirdwood
authored andcommitted
rimage: toml_utils: add uint8/16 parsing support for TOML keys
Extends existing parsing functionality Signed-off-by: Adrian Bonislawski <adrian.bonislawski@intel.com>
1 parent 9597b82 commit 0b0925e

2 files changed

Lines changed: 149 additions & 1 deletion

File tree

tools/rimage/src/include/rimage/toml_utils.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ int assert_everything_parsed(const toml_table_t *table, struct parse_ctx *ctx);
6565
*/
6666
uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
6767
const char *key, int64_t def, int *error);
68+
uint16_t parse_uint16_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
69+
const char *key, int64_t def, int *error);
70+
uint8_t parse_uint8_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
71+
const char *key, int64_t def, int *error);
6872

6973
/**
7074
* Parse integer value from key in given toml table
@@ -77,6 +81,8 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
7781
*/
7882
uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
7983
int64_t def, int *error);
84+
uint8_t parse_uint8_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
85+
int64_t def, int *error);
8086

8187
/**
8288
* Parse string value from key in given toml table to uint8_t array. The

tools/rimage/src/toml_utils.c

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,106 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
166166
return (uint32_t)val;
167167
}
168168

169+
/**
170+
* Parse hex value from key in given toml table
171+
* @param table toml table where key is specified
172+
* @param ctx parsing context, key counter will be incremented after successful key parse
173+
* @param key field name
174+
* @param def is default value or -1 when value don't have default value
175+
* @param error code, 0 when success
176+
* @return default, parsed, or UINT16_MAX value for error cases
177+
*/
178+
uint16_t parse_uint16_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
179+
const char *key, int64_t def, int *error)
180+
{
181+
toml_raw_t raw;
182+
char *temp_s;
183+
unsigned long val; /* strtoul return type */
184+
int ret;
185+
186+
raw = toml_raw_in(table, key);
187+
if (!raw) {
188+
if (def < 0 || def > UINT16_MAX) {
189+
*error = err_key_not_found(key);
190+
return UINT16_MAX;
191+
}
192+
*error = 0;
193+
return (uint16_t)def;
194+
}
195+
196+
ret = toml_rtos(raw, &temp_s);
197+
if (ret < 0) {
198+
*error = err_key_parse(key, NULL);
199+
return UINT16_MAX;
200+
}
201+
errno = 0;
202+
val = strtoul(temp_s, 0, 0);
203+
free(temp_s);
204+
205+
if (errno != 0) {
206+
*error = err_key_parse(key, "can't convert hex value");
207+
return UINT16_MAX;
208+
}
209+
if (val > UINT16_MAX) {
210+
*error = log_err(-ERANGE, "key %s out of uint16_t hex range", key);
211+
return UINT16_MAX;
212+
}
213+
214+
*error = 0;
215+
++ctx->key_cnt;
216+
return (uint16_t)val;
217+
}
218+
219+
/**
220+
* Parse hex value from key in given toml table
221+
* @param table toml table where key is specified
222+
* @param ctx parsing context, key counter will be incremented after successful key parse
223+
* @param key field name
224+
* @param def is default value or -1 when value don't have default value
225+
* @param error code, 0 when success
226+
* @return default, parsed, or UINT8_MAX value for error cases
227+
*/
228+
uint8_t parse_uint8_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
229+
const char *key, int64_t def, int *error)
230+
{
231+
toml_raw_t raw;
232+
char *temp_s;
233+
unsigned long val;
234+
int ret;
235+
236+
raw = toml_raw_in(table, key);
237+
if (!raw) {
238+
if (def < 0 || def > UINT8_MAX) {
239+
*error = err_key_not_found(key);
240+
return UINT8_MAX;
241+
}
242+
*error = 0;
243+
return (uint8_t)def;
244+
}
245+
246+
ret = toml_rtos(raw, &temp_s);
247+
if (ret < 0) {
248+
*error = err_key_parse(key, NULL);
249+
return UINT8_MAX;
250+
}
251+
errno = 0;
252+
val = strtoul(temp_s, 0, 0);
253+
free(temp_s);
254+
255+
if (errno != 0) {
256+
*error = err_key_parse(key, "can't convert hex value");
257+
return UINT8_MAX;
258+
}
259+
if (val > UINT8_MAX) {
260+
*error = log_err(-ERANGE, "key %s out of uint8_t hex range", key);
261+
return UINT8_MAX;
262+
}
263+
264+
*error = 0;
265+
++ctx->key_cnt;
266+
return (uint8_t)val;
267+
}
268+
169269
/**
170270
* Parse integer value from key in given toml table
171271
* @param table toml table where key is specified
@@ -176,7 +276,7 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx,
176276
* @return default, parsed, or UINT32_MAX value for error cases
177277
*/
178278
uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
179-
int64_t def, int *error)
279+
int64_t def, int *error)
180280
{
181281
toml_raw_t raw;
182282
int64_t val;
@@ -210,6 +310,48 @@ uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, cons
210310
return (uint32_t)val;
211311
}
212312

313+
/**
314+
* Parse unsigned 8-bit integer value from key in given toml table.
315+
* @param table toml table where key is specified
316+
* @param ctx parsing context, key counter will be incremented after successful key parse
317+
* @param key field name
318+
* @param def is default value or -1 when value don't have default value
319+
* @param error code, 0 when success
320+
* @return default, parsed, or UINT8_MAX value for error cases
321+
*/
322+
uint8_t parse_uint8_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key,
323+
int64_t def, int *error)
324+
{
325+
toml_raw_t raw;
326+
int64_t val;
327+
int ret;
328+
329+
raw = toml_raw_in(table, key);
330+
if (!raw) {
331+
if (def < 0 || def > UINT8_MAX) {
332+
*error = err_key_not_found(key);
333+
return UINT8_MAX;
334+
} else {
335+
*error = 0;
336+
return (uint8_t)def;
337+
}
338+
}
339+
340+
ret = toml_rtoi(raw, &val);
341+
if (ret < 0) {
342+
*error = err_key_parse(key, "can't convert to integer value");
343+
return UINT8_MAX;
344+
}
345+
if (val < 0 || val > UINT8_MAX) {
346+
*error = log_err(-ERANGE, "key %s out of uint8_t range", key);
347+
return UINT8_MAX;
348+
}
349+
350+
*error = 0;
351+
++ctx->key_cnt;
352+
return (uint8_t)val;
353+
}
354+
213355
/**
214356
* Parse string value from key in given toml table to uint8_t array. The
215357
* destination is NOT a string because it is padded with zeros if and

0 commit comments

Comments
 (0)