Skip to content

Commit b6628f2

Browse files
committed
[ot] hw/opentitan: ot_common: add a utility function to parse hex strings
Signed-off-by: Emmanuel Blot <eblot@rivosinc.com>
1 parent 839d818 commit b6628f2

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

hw/opentitan/ot_common.c

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,33 @@ void ot_common_configure_devices_with_id(
341341
ibex_connect_devices(devices, defs, count);
342342
}
343343

344+
int ot_common_string_ends_with(const char *str, const char *suffix)
345+
{
346+
size_t str_len = strlen(str);
347+
size_t suffix_len = strlen(suffix);
348+
349+
return (str_len >= suffix_len) &&
350+
(!memcmp(str + str_len - suffix_len, suffix, suffix_len));
351+
}
352+
353+
int ot_common_parse_hexa_str(uint8_t *out, const char *xstr, size_t olen,
354+
bool reverse, bool exact)
355+
{
356+
for (size_t ix = 0; ix < olen; ix++) {
357+
gint hi = g_ascii_xdigit_value(xstr[ix * 2u]);
358+
if (hi < 0) {
359+
return hi;
360+
}
361+
gint lo = g_ascii_xdigit_value(xstr[ix * 2u + 1u]);
362+
if (lo < 0) {
363+
return lo;
364+
}
365+
out[reverse ? olen - ix - 1u : ix] = (uint8_t)(hi << 4) | (uint8_t)(lo);
366+
}
367+
368+
return (!exact || !xstr[olen * 2u]) ? 0 : 1;
369+
}
370+
344371
/*
345372
* Unfortunately, there is no QEMU API to properly disable serial control lines
346373
*/
@@ -364,12 +391,3 @@ void ot_common_ignore_chr_status_lines(CharBackend *chr)
364391
tcsetattr(fioc->fd, TCSANOW, &tty);
365392
#endif
366393
}
367-
368-
int ot_common_string_ends_with(const char *str, const char *suffix)
369-
{
370-
size_t str_len = strlen(str);
371-
size_t suffix_len = strlen(suffix);
372-
373-
return (str_len >= suffix_len) &&
374-
(!memcmp(str + str_len - suffix_len, suffix, suffix_len));
375-
}

include/hw/opentitan/ot_common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,9 @@ void ot_common_ignore_chr_status_lines(CharBackend *chr);
299299

300300
int ot_common_string_ends_with(const char *str, const char *suffix);
301301

302+
int ot_common_parse_hexa_str(uint8_t *out, const char *xstr, size_t olen,
303+
bool reverse, bool exact);
304+
302305
/* ------------------------------------------------------------------------ */
303306
/* Configuration utilities */
304307
/* ------------------------------------------------------------------------ */

0 commit comments

Comments
 (0)