general: Make byteswapping arch-independent

ipmiXXtoh() and htoipmiXX() functions were broken
and only worked properly for big-endian hosts.

This commit makes them endianness-independent.

Signed-off-by: Alexander Amelkin <alexander@amelkin.msk.ru>
This commit is contained in:
Alexander Amelkin 2018-07-20 19:41:44 +03:00
parent b3d258b234
commit 249e092967
No known key found for this signature in database
GPG Key ID: E893587B5B74178D

View File

@ -118,16 +118,31 @@ static inline uint16_t ipmi16toh(void *ipmi16)
uint8_t *ipmi = (uint8_t *)ipmi16;
uint16_t h;
/*
* ipmi16 address may be word-wise misaligned, so don't
* use type-pun and instead explicity shift corresponding bytes
* to where they belong.
*/
#if WORDS_BIGENDIAN
h = ipmi[0] << 8; /* MSB */
h |= ipmi[1]; /* LSB */
#else
h = ipmi[1] << 8; /* MSB */
h |= ipmi[0]; /* LSB */
#endif
return h;
}
static inline void htoipmi16(uint16_t h, uint8_t *ipmi)
{
#if WORDS_BIGENDIAN
ipmi[0] = h & 0xFF; /* LSB */
ipmi[1] = h >> 8; /* MSB */
#else
ipmi[1] = h & 0xFF; /* LSB */
ipmi[0] = h >> 8; /* MSB */
#endif
}
static inline uint32_t ipmi24toh(void *ipmi24)
@ -135,26 +150,58 @@ static inline uint32_t ipmi24toh(void *ipmi24)
uint8_t *ipmi = (uint8_t *)ipmi24;
uint32_t h = 0;
h = ipmi[2] << 16;
#if WORDS_BIGENDIAN
h = ipmi[0] << 16; /* LSB */
h |= ipmi[1] << 8;
h |= ipmi[0];
h |= ipmi[2]; /* MSB */
#else
h = ipmi[2] << 16; /* LSB */
h |= ipmi[1] << 8;
h |= ipmi[0]; /* MSB */
#endif
return h;
}
static inline uint32_t ipmi32toh(void *ipmi32)
{
uint8_t *ipmi = (uint8_t *)ipmi32;
uint8_t *ipmi = ipmi32;
uint32_t h;
h = ipmi[3] << 24;
/*
* ipmi32 address may be dword-wise misaligned, so don't
* use type-pun and instead explicity shift corresponding bytes
* to where they belong.
*/
#if WORDS_BIGENDIAN
h = ipmi[0] << 24; /* LSB */
h |= ipmi[1] << 16;
h |= ipmi[2] << 8;
h |= ipmi[3]; /* MSB */
#else
h = ipmi[3] << 24; /* LSB */
h |= ipmi[2] << 16;
h |= ipmi[1] << 8;
h |= ipmi[0];
h |= ipmi[0]; /* MSB */
#endif
return h;
}
static inline void htoipmi32(uint32_t h, uint8_t *ipmi)
{
#if WORDS_BIGENDIAN
ipmi[0] = h & 0xFF; /* LSB */
ipmi[1] = (h >> 8) & 0xFF;
ipmi[2] = (h >> 16) & 0xFF;
ipmi[3] = (h >> 24) & 0xFF; /* MSB */
#else
ipmi[3] = h & 0xFF; /* LSB */
ipmi[2] = (h >> 8) & 0xFF;
ipmi[1] = (h >> 16) & 0xFF;
ipmi[0] = (h >> 24) & 0xFF; /* MSB */
#endif
}
#define ipmi_open_file_read(file) ipmi_open_file(file, 0)
#define ipmi_open_file_write(file) ipmi_open_file(file, 1)