Commit d195ab25 authored by Alessandro Rubini's avatar Alessandro Rubini

sdbfs/lib: fix glue.c to support strange-endian stuff

Signed-off-by: Alessandro Rubini's avatarAlessandro Rubini <rubini@gnudd.com>
parent 7acbfd79
/* /*
* Copyright (C) 2012 CERN (www.cern.ch) * Copyright (C) 2012,2014 CERN (www.cern.ch)
* Author: Alessandro Rubini <rubini@gnudd.com> * Author: Alessandro Rubini <rubini@gnudd.com>
* *
* Released according to the GNU GPL, version 2 or any later version. * Released according to the GNU GPL, version 2 or any later version.
...@@ -23,13 +23,18 @@ int sdbfs_dev_create(struct sdbfs *fs, int verbose) ...@@ -23,13 +23,18 @@ int sdbfs_dev_create(struct sdbfs *fs, int verbose)
magic = *(unsigned int *)(fs->data + fs->entrypoint); magic = *(unsigned int *)(fs->data + fs->entrypoint);
else else
fs->read(fs, fs->entrypoint, &magic, sizeof(magic)); fs->read(fs, fs->entrypoint, &magic, sizeof(magic));
if (htonl(magic) != SDB_MAGIC) if (magic == SDB_MAGIC) {
/* Uh! If we are little-endian, we must convert */
if (ntohl(1) != 1)
fs->flags |= SDBFS_F_CONVERT32;
} else if (htonl(magic) == SDB_MAGIC) {
/* ok, don't convert */
} else {
return -ENOTDIR; return -ENOTDIR;
}
if (verbose) if (verbose)
fs->flags |= SDBFS_F_VERBOSE; fs->flags |= SDBFS_F_VERBOSE;
fs->next = sdbfs_list; fs->next = sdbfs_list;
sdbfs_list = fs; sdbfs_list = fs;
...@@ -70,13 +75,29 @@ static struct sdb_device *sdbfs_readentry(struct sdbfs *fs, ...@@ -70,13 +75,29 @@ static struct sdb_device *sdbfs_readentry(struct sdbfs *fs,
/* /*
* This function reads an entry from a known good offset. It * This function reads an entry from a known good offset. It
* returns the pointer to the entry, which may be stored in * returns the pointer to the entry, which may be stored in
* the fs structure itself. Only touches fs->current_record * the fs structure itself. Only touches fs->current_record.
*/ */
if (fs->data) if (fs->data) {
return (struct sdb_device *)(fs->data + offset); if (!(fs->flags & SDBFS_F_CONVERT32))
if (!fs->read) return (struct sdb_device *)(fs->data + offset);
return NULL; /* copy to local storage for conversion */
fs->read(fs, offset, &fs->current_record, sizeof(fs->current_record)); memcpy(&fs->current_record, fs->data + offset,
sizeof(fs->current_record));
} else {
if (!fs->read)
return NULL;
fs->read(fs, offset, &fs->current_record,
sizeof(fs->current_record));
}
if (fs->flags & SDBFS_F_CONVERT32) {
uint32_t *p = (void *)&fs->current_record;
int i;
for (i = 0; i < sizeof(fs->current_record) / sizeof(*p); i++)
p[i] = ntohl(p[i]);
}
return &fs->current_record; return &fs->current_record;
} }
......
...@@ -51,6 +51,7 @@ struct sdbfs { ...@@ -51,6 +51,7 @@ struct sdbfs {
}; };
#define SDBFS_F_VERBOSE 0x0001 #define SDBFS_F_VERBOSE 0x0001
#define SDBFS_F_CONVERT32 0x0002 /* swap SDB words as they are read */
/* Defined in glue.c */ /* Defined in glue.c */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment