Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 20 additions & 20 deletions tools/rimage/src/adsp_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions tools/rimage/src/include/rimage/toml_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
147 changes: 145 additions & 2 deletions tools/rimage/src/toml_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Loading