topology: fix the unaligned access

Introduce unaligned_get32/put32 helpers to deal with the
packed structures.

Use the gcc __BYTE_ORDER__ defines for the endian checks.
It may be improved to support other compilation environment.

Signed-off-by: Jaroslav Kysela <perex@perex.cz>
This commit is contained in:
Jaroslav Kysela 2020-08-31 14:27:26 +02:00
parent 84c6aeef5c
commit ab73253924
3 changed files with 29 additions and 16 deletions

View file

@ -427,10 +427,8 @@ void snd_tplg_verbose(snd_tplg_t *tplg, int verbose)
static bool is_little_endian(void) static bool is_little_endian(void)
{ {
#ifdef __BYTE_ORDER #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
#if __BYTE_ORDER == __LITTLE_ENDIAN return true;
return true;
#endif
#endif #endif
return false; return false;
} }

View file

@ -376,19 +376,19 @@ static int split_rate(struct snd_soc_tplg_stream_caps *caps, char *str)
return 0; return 0;
} }
static int parse_unsigned(snd_config_t *n, unsigned int *dst) static int parse_unsigned(snd_config_t *n, void *dst)
{ {
int ival; int ival;
if (tplg_get_integer(n, &ival, 0) < 0) if (tplg_get_integer(n, &ival, 0) < 0)
return -EINVAL; return -EINVAL;
*dst = ival; unaligned_put32(dst, ival);
#if TPLG_DEBUG #if TPLG_DEBUG
{ {
const char *id; const char *id;
if (snd_config_get_id(n, &id) >= 0) if (snd_config_get_id(n, &id) >= 0)
tplg_dbg("\t\t%s: %d", id, *dst); tplg_dbg("\t\t%s: %d", id, ival);
} }
#endif #endif
return 0; return 0;
@ -621,7 +621,7 @@ static int tplg_parse_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
struct tplg_elem *elem = private; struct tplg_elem *elem = private;
struct snd_soc_tplg_pcm *pcm; struct snd_soc_tplg_pcm *pcm;
struct snd_soc_tplg_dai *dai; struct snd_soc_tplg_dai *dai;
unsigned int *playback, *capture; void *playback, *capture;
struct snd_soc_tplg_stream_caps *caps; struct snd_soc_tplg_stream_caps *caps;
const char *id, *value; const char *id, *value;
int stream; int stream;
@ -651,10 +651,10 @@ static int tplg_parse_streams(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
if (strcmp(id, "playback") == 0) { if (strcmp(id, "playback") == 0) {
stream = SND_SOC_TPLG_STREAM_PLAYBACK; stream = SND_SOC_TPLG_STREAM_PLAYBACK;
*playback = 1; unaligned_put32(playback, 1);
} else if (strcmp(id, "capture") == 0) { } else if (strcmp(id, "capture") == 0) {
stream = SND_SOC_TPLG_STREAM_CAPTURE; stream = SND_SOC_TPLG_STREAM_CAPTURE;
*capture = 1; unaligned_put32(capture, 1);
} else } else
return -EINVAL; return -EINVAL;
@ -747,6 +747,7 @@ static int tplg_parse_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
snd_config_iterator_t i, next; snd_config_iterator_t i, next;
snd_config_t *n; snd_config_t *n;
const char *id; const char *id;
unsigned int dai_id;
snd_config_get_id(cfg, &id); snd_config_get_id(cfg, &id);
tplg_dbg("\t\tFE DAI %s:", id); tplg_dbg("\t\tFE DAI %s:", id);
@ -761,12 +762,13 @@ static int tplg_parse_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
continue; continue;
if (strcmp(id, "id") == 0) { if (strcmp(id, "id") == 0) {
if (tplg_get_unsigned(n, &pcm->dai_id, 0)) { if (tplg_get_unsigned(n, &dai_id, 0)) {
SNDERR("invalid fe dai ID"); SNDERR("invalid fe dai ID");
return -EINVAL; return -EINVAL;
} }
tplg_dbg("\t\t\tindex: %d", pcm->dai_id); unaligned_put32(&pcm->dai_id, dai_id);
tplg_dbg("\t\t\tindex: %d", dai_id);
} }
} }
@ -790,7 +792,7 @@ int tplg_save_fe_dai(snd_tplg_t *tplg ATTRIBUTE_UNUSED,
/* parse a flag bit of the given mask */ /* parse a flag bit of the given mask */
static int parse_flag(snd_config_t *n, unsigned int mask_in, static int parse_flag(snd_config_t *n, unsigned int mask_in,
unsigned int *mask, unsigned int *flags) void *mask, void *flags)
{ {
int ret; int ret;
@ -798,11 +800,11 @@ static int parse_flag(snd_config_t *n, unsigned int mask_in,
if (ret < 0) if (ret < 0)
return ret; return ret;
*mask |= mask_in; unaligned_put32(mask, unaligned_get32(mask) | mask_in);
if (ret) if (ret)
*flags |= mask_in; unaligned_put32(flags, unaligned_get32(flags) | mask_in);
else else
*flags &= ~mask_in; unaligned_put32(flags, unaligned_get32(flags) & (~mask_in));
return 0; return 0;
} }

View file

@ -225,6 +225,19 @@ struct tplg_table {
extern struct tplg_table tplg_table[]; extern struct tplg_table tplg_table[];
extern unsigned int tplg_table_items; extern unsigned int tplg_table_items;
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ && __SIZEOF_INT__ == 4
static inline unsigned int unaligned_get32(void *src)
{
unsigned int ret;
memcpy(&ret, src, sizeof(ret));
return ret;
}
static inline void unaligned_put32(void *dst, unsigned int val)
{
memcpy(dst, &val, sizeof(val));
}
#endif
#define tplg_log(tplg, type, pos, fmt, args...) do { \ #define tplg_log(tplg, type, pos, fmt, args...) do { \
if ((tplg)->verbose) \ if ((tplg)->verbose) \
tplg_log_((tplg), (type), (pos), (fmt), ##args); \ tplg_log_((tplg), (type), (pos), (fmt), ##args); \