diff --git a/src/include/rimage/misc_utils.h b/src/include/rimage/misc_utils.h index cf9353ac9..8ec051d74 100644 --- a/src/include/rimage/misc_utils.h +++ b/src/include/rimage/misc_utils.h @@ -3,6 +3,9 @@ * Copyright(c) 2023 Intel Corporation. All rights reserved. */ +#ifndef __MISC_UTILS_H__ +#define __MISC_UTILS_H__ + #include /** @@ -11,3 +14,16 @@ * @param size of the array */ void bytes_swap(uint8_t *ptr, uint32_t size); + +struct name_val { + const char *name; + unsigned long value; +}; + +#define NAME_VAL_ENTRY(x) { .name = #x, .value = x } +#define NAME_VAL_END { .name = 0, .value = 0 } + +void print_enum(unsigned long value, const struct name_val *values); +void print_flags(unsigned long value, const struct name_val *flags); + +#endif /* __MISC_UTILS_H__ */ diff --git a/src/misc_utils.c b/src/misc_utils.c index e4b5e4d4c..2c0146f5b 100644 --- a/src/misc_utils.c +++ b/src/misc_utils.c @@ -1,8 +1,11 @@ // SPDX-License-Identifier: BSD-3-Clause /* * Copyright(c) 2018-2023 Intel Corporation. All rights reserved. + * + * Author: Adrian Warecki */ +#include #include void bytes_swap(uint8_t *ptr, uint32_t size) @@ -16,3 +19,33 @@ void bytes_swap(uint8_t *ptr, uint32_t size) ptr[size - 1 - index] = tmp; } } + +void print_enum(unsigned long value, const struct name_val *values) +{ + while (values->name) { + if (values->value == value) { + fprintf(stdout, "%s\n", values->name); + return; + } + + values++; + } + + printf("Unknown: 0x%lx\n", value); +} + +void print_flags(unsigned long value, const struct name_val *flags) +{ + while (flags->name) { + if (value & flags->value) { + fprintf(stdout, "%s ", flags->name); + value &= ~flags->value; + } + + flags++; + } + + if (value) + fprintf(stdout, "+ 0x%lx", value); + printf("\n"); +}