diff --git a/tools/rimage/src/adsp_config.c b/tools/rimage/src/adsp_config.c index 5ce44a689826..6cc7434e74bc 100644 --- a/tools/rimage/src/adsp_config.c +++ b/tools/rimage/src/adsp_config.c @@ -270,11 +270,11 @@ static int parse_cse(const toml_table_t *toml, struct parse_ctx *pctx, hdr->header_length = sizeof(struct CsePartitionDirHeader); /* configurable fields */ - hdr->header_version = parse_uint32_key(cse, &ctx, "header_version", 1, &ret); + hdr->header_version = parse_uint8_key(cse, &ctx, "header_version", 1, &ret); if (ret < 0) return ret; - hdr->entry_version = parse_uint32_key(cse, &ctx, "entry_version", 1, &ret); + hdr->entry_version = parse_uint8_key(cse, &ctx, "entry_version", 1, &ret); if (ret < 0) return ret; @@ -382,11 +382,11 @@ static int parse_cse_v2_5(const toml_table_t *toml, struct parse_ctx *pctx, hdr->header_length = sizeof(struct CsePartitionDirHeader_v2_5); /* configurable fields */ - hdr->header_version = parse_uint32_key(cse, &ctx, "header_version", 2, &ret); + hdr->header_version = parse_uint8_key(cse, &ctx, "header_version", 2, &ret); if (ret < 0) return ret; - hdr->entry_version = parse_uint32_key(cse, &ctx, "entry_version", 1, &ret); + hdr->entry_version = parse_uint8_key(cse, &ctx, "entry_version", 1, &ret); if (ret < 0) return ret; @@ -771,11 +771,11 @@ static int parse_signed_pkg(const toml_table_t *toml, struct parse_ctx *pctx, if (ret < 0) return ret; - out->fw_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); + out->fw_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); if (ret < 0) return ret; - out->fw_sub_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); + out->fw_sub_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); if (ret < 0) return ret; @@ -835,15 +835,15 @@ static int parse_signed_pkg(const toml_table_t *toml, struct parse_ctx *pctx, if (ret < 0) return err_key_parse("module", NULL); - mod->type = parse_uint32_hex_key(module, &ctx, "type", 0x03, &ret); + mod->type = parse_uint8_hex_key(module, &ctx, "type", 0x03, &ret); if (ret < 0) return err_key_parse("module", NULL); - mod->hash_algo = parse_uint32_hex_key(module, &ctx, "hash_algo", 0x02, &ret); + mod->hash_algo = parse_uint8_hex_key(module, &ctx, "hash_algo", 0x02, &ret); if (ret < 0) return err_key_parse("module", NULL); - mod->hash_size = parse_uint32_hex_key(module, &ctx, "hash_size", 0x20, &ret); + mod->hash_size = parse_uint16_hex_key(module, &ctx, "hash_size", 0x20, &ret); if (ret < 0) return err_key_parse("module", NULL); @@ -929,11 +929,11 @@ static int parse_signed_pkg_v2_5(const toml_table_t *toml, struct parse_ctx *pct if (ret < 0) return ret; - out->fw_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); + out->fw_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); if (ret < 0) return ret; - out->fw_sub_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); + out->fw_sub_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); if (ret < 0) return ret; @@ -993,15 +993,15 @@ static int parse_signed_pkg_v2_5(const toml_table_t *toml, struct parse_ctx *pct if (ret < 0) return err_key_parse("module", NULL); - mod->type = parse_uint32_hex_key(module, &ctx, "type", 0x03, &ret); + mod->type = parse_uint8_hex_key(module, &ctx, "type", 0x03, &ret); if (ret < 0) return err_key_parse("module", NULL); - mod->hash_algo = parse_uint32_hex_key(module, &ctx, "hash_algo", 0x00, &ret); + mod->hash_algo = parse_uint8_hex_key(module, &ctx, "hash_algo", 0x00, &ret); if (ret < 0) return err_key_parse("module", NULL); - mod->hash_size = parse_uint32_hex_key(module, &ctx, "hash_size", 0x30, &ret); + mod->hash_size = parse_uint16_hex_key(module, &ctx, "hash_size", 0x30, &ret); if (ret < 0) return err_key_parse("module", NULL); @@ -1079,11 +1079,11 @@ static int parse_signed_pkg_ace_v1_5(const toml_table_t *toml, struct parse_ctx if (ret < 0) return ret; - out->fw_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); + out->fw_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_type", 0, &ret); if (ret < 0) return ret; - out->fw_sub_type = parse_uint32_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); + out->fw_sub_type = parse_uint8_hex_key(signed_pkg, &ctx, "fw_sub_type", 0, &ret); if (ret < 0) return ret; @@ -1121,11 +1121,11 @@ static int parse_signed_pkg_ace_v1_5(const toml_table_t *toml, struct parse_ctx if (ret < 0) return err_key_parse("module", NULL); - mod->type = parse_uint32_hex_key(module, &ctx, "type", 0x03, &ret); + mod->type = parse_uint8_hex_key(module, &ctx, "type", 0x03, &ret); if (ret < 0) return err_key_parse("module", NULL); - mod->hash_algo = parse_uint32_hex_key(module, &ctx, "hash_algo", 0x00, &ret); + mod->hash_algo = parse_uint8_hex_key(module, &ctx, "hash_algo", 0x00, &ret); if (ret < 0) return err_key_parse("module", NULL); @@ -1256,7 +1256,7 @@ static int parse_partition_info_ext(const toml_table_t *toml, struct parse_ctx * if (ret < 0) return err_key_parse("module", NULL); - mod->type = parse_uint32_hex_key(module, &ctx, "type", 0x03, &ret); + mod->type = parse_uint8_hex_key(module, &ctx, "type", 0x03, &ret); if (ret < 0) return err_key_parse("module", NULL); @@ -1917,7 +1917,7 @@ static int parse_module(const toml_table_t *toml, struct parse_ctx *pctx, if (ret < 0) return err_key_parse("affinity_mask", NULL); - mod_man->instance_max_count = parse_uint32_hex_key(mod_entry, &ctx_entry, + mod_man->instance_max_count = parse_uint16_hex_key(mod_entry, &ctx_entry, "instance_count", 1, &ret); if (ret < 0) return err_key_parse("instance_count", NULL); diff --git a/tools/rimage/src/include/rimage/toml_utils.h b/tools/rimage/src/include/rimage/toml_utils.h index 94c512f31aa0..8bbbdaca17fa 100644 --- a/tools/rimage/src/include/rimage/toml_utils.h +++ b/tools/rimage/src/include/rimage/toml_utils.h @@ -65,6 +65,10 @@ int assert_everything_parsed(const toml_table_t *table, struct parse_ctx *ctx); */ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, int64_t def, int *error); +uint16_t parse_uint16_hex_key(const toml_table_t *table, struct parse_ctx *ctx, + const char *key, int64_t def, int *error); +uint8_t parse_uint8_hex_key(const toml_table_t *table, struct parse_ctx *ctx, + const char *key, int64_t def, int *error); /** * 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, */ uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, int64_t def, int *error); +uint8_t parse_uint8_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, + int64_t def, int *error); /** * Parse string value from key in given toml table to uint8_t array. The diff --git a/tools/rimage/src/toml_utils.c b/tools/rimage/src/toml_utils.c index d7abc1e82296..40a73fa1514c 100644 --- a/tools/rimage/src/toml_utils.c +++ b/tools/rimage/src/toml_utils.c @@ -151,11 +151,12 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, *error = err_key_parse(key, NULL); return UINT32_MAX; } + errno = 0; val = strtoul(temp_s, 0, 0); free(temp_s); /* assert parsing success and value is within uint32_t range */ - if (errno < 0) { + if (errno != 0) { *error = err_key_parse(key, "can't convert hex value"); return UINT32_MAX; } @@ -166,6 +167,106 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, return (uint32_t)val; } +/** + * Parse hex value from key in given toml table + * @param table toml table where key is specified + * @param ctx parsing context, key counter will be incremented after successful key parse + * @param key field name + * @param def is default value or -1 when value don't have default value + * @param error code, 0 when success + * @return default, parsed, or UINT16_MAX value for error cases + */ +uint16_t parse_uint16_hex_key(const toml_table_t *table, struct parse_ctx *ctx, + const char *key, int64_t def, int *error) +{ + toml_raw_t raw; + char *temp_s; + unsigned long val; /* strtoul return type */ + int ret; + + raw = toml_raw_in(table, key); + if (!raw) { + if (def < 0 || def > UINT16_MAX) { + *error = err_key_not_found(key); + return UINT16_MAX; + } + *error = 0; + return (uint16_t)def; + } + + ret = toml_rtos(raw, &temp_s); + if (ret < 0) { + *error = err_key_parse(key, NULL); + return UINT16_MAX; + } + errno = 0; + val = strtoul(temp_s, 0, 0); + free(temp_s); + + if (errno != 0) { + *error = err_key_parse(key, "can't convert hex value"); + return UINT16_MAX; + } + if (val > UINT16_MAX) { + *error = log_err(-ERANGE, "key %s out of uint16_t hex range", key); + return UINT16_MAX; + } + + *error = 0; + ++ctx->key_cnt; + return (uint16_t)val; +} + +/** + * Parse hex value from key in given toml table + * @param table toml table where key is specified + * @param ctx parsing context, key counter will be incremented after successful key parse + * @param key field name + * @param def is default value or -1 when value don't have default value + * @param error code, 0 when success + * @return default, parsed, or UINT8_MAX value for error cases + */ +uint8_t parse_uint8_hex_key(const toml_table_t *table, struct parse_ctx *ctx, + const char *key, int64_t def, int *error) +{ + toml_raw_t raw; + char *temp_s; + unsigned long val; + int ret; + + raw = toml_raw_in(table, key); + if (!raw) { + if (def < 0 || def > UINT8_MAX) { + *error = err_key_not_found(key); + return UINT8_MAX; + } + *error = 0; + return (uint8_t)def; + } + + ret = toml_rtos(raw, &temp_s); + if (ret < 0) { + *error = err_key_parse(key, NULL); + return UINT8_MAX; + } + errno = 0; + val = strtoul(temp_s, 0, 0); + free(temp_s); + + if (errno != 0) { + *error = err_key_parse(key, "can't convert hex value"); + return UINT8_MAX; + } + if (val > UINT8_MAX) { + *error = log_err(-ERANGE, "key %s out of uint8_t hex range", key); + return UINT8_MAX; + } + + *error = 0; + ++ctx->key_cnt; + return (uint8_t)val; +} + /** * Parse integer value from key in given toml table * @param table toml table where key is specified @@ -176,7 +277,7 @@ uint32_t parse_uint32_hex_key(const toml_table_t *table, struct parse_ctx *ctx, * @return default, parsed, or UINT32_MAX value for error cases */ uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, - int64_t def, int *error) + int64_t def, int *error) { toml_raw_t raw; int64_t val; @@ -210,6 +311,48 @@ uint32_t parse_uint32_key(const toml_table_t *table, struct parse_ctx *ctx, cons return (uint32_t)val; } +/** + * Parse unsigned 8-bit integer value from key in given toml table. + * @param table toml table where key is specified + * @param ctx parsing context, key counter will be incremented after successful key parse + * @param key field name + * @param def is default value or -1 when value don't have default value + * @param error code, 0 when success + * @return default, parsed, or UINT8_MAX value for error cases + */ +uint8_t parse_uint8_key(const toml_table_t *table, struct parse_ctx *ctx, const char *key, + int64_t def, int *error) +{ + toml_raw_t raw; + int64_t val; + int ret; + + raw = toml_raw_in(table, key); + if (!raw) { + if (def < 0 || def > UINT8_MAX) { + *error = err_key_not_found(key); + return UINT8_MAX; + } else { + *error = 0; + return (uint8_t)def; + } + } + + ret = toml_rtoi(raw, &val); + if (ret < 0) { + *error = err_key_parse(key, "can't convert to integer value"); + return UINT8_MAX; + } + if (val < 0 || val > UINT8_MAX) { + *error = log_err(-ERANGE, "key %s out of uint8_t range", key); + return UINT8_MAX; + } + + *error = 0; + ++ctx->key_cnt; + return (uint8_t)val; +} + /** * Parse string value from key in given toml table to uint8_t array. The * destination is NOT a string because it is padded with zeros if and