From: Cédric Le G. <cl...@fr...> - 2013-10-28 14:11:12
|
From: Cedric Le Goater <cl...@fr...> The nvram is in big endian order. The partition length needs to be byte-swapped when it is read or written on little endian machines. Signed-off-by: Cedric Le Goater <cl...@fr...> --- src/nvram.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/nvram.c b/src/nvram.c index 8d567fa..d062c30 100644 --- a/src/nvram.c +++ b/src/nvram.c @@ -44,6 +44,8 @@ #include <getopt.h> #include <inttypes.h> #include <zlib.h> +#include <endian.h> +#include <byteswap.h> #include "nvram.h" @@ -407,6 +409,16 @@ parse_of_common(char *data, int data_len, char *outname, char *outval) return p - data; } +static inline unsigned short +nvram_swap_short(unsigned short length) +{ +#if __BYTE_ORDER == __LITTLE_ENDIAN + return bswap_16(length); +#else + return length; +#endif +} + /** * nvram_parse_partitions * @brief fill in the nvram structure with data from nvram @@ -439,6 +451,7 @@ nvram_parse_partitions(struct nvram *nvram) c_sum = checksum(phead); if (c_sum != phead->checksum) warn_msg("this partition checksum should be %02x!\n", c_sum); + phead->length = nvram_swap_short(phead->length); p_start += phead->length * NVRAM_BLOCK_SIZE; } @@ -484,7 +497,8 @@ nvram_find_fd_partition(struct nvram *nvram, char *name) if (! strncmp(phead.name, name, sizeof(phead.name))) found = 1; else { - int offset = phead.length * NVRAM_BLOCK_SIZE - len; + int offset = + nvram_swap_short(phead.length) * NVRAM_BLOCK_SIZE - len; if (lseek(nvram->fd, offset, SEEK_CUR) == -1) { err_msg("seek error in file %s: %s\n", nvram->filename, strerror(errno)); @@ -1337,6 +1351,7 @@ update_of_config_var(struct nvram *nvram, char *config_var, char *pname) /* make sure the new config var will fit into the partition and add it */ new_phead = (struct partition_header *)new_part; + new_phead->length = nvram_swap_short(new_phead->length); new_part_offset = new_part + (data_offset - (char *)phead); new_part_end = new_part + part_size; -- 1.7.10.4 |