libfdt: Export accessors for header fields
This patch adds exported accessor macros for the various flat device
tree header fields to libfdt.h. This necessitates moving some of the
byte-swapping functions.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
diff --git a/fdt.c b/fdt.c
index 03f1a0d..b24a3fe 100644
--- a/fdt.c
+++ b/fdt.c
@@ -27,7 +27,7 @@
{
void *p;
- p = (void *)fdt + fdt32_to_cpu(fdt->off_dt_struct) + offset;
+ p = (void *)fdt + fdt_off_dt_struct(fdt) + offset;
if (p + len < p)
return NULL;
diff --git a/fdt_ro.c b/fdt_ro.c
index 43a79e5..db5281c 100644
--- a/fdt_ro.c
+++ b/fdt_ro.c
@@ -25,19 +25,15 @@
static int check_header(const struct fdt_header *fdt)
{
- uint32_t magic = fdt32_to_cpu(fdt->magic);
- uint32_t version = fdt32_to_cpu(fdt->version);
- uint32_t last_comp_version = fdt32_to_cpu(fdt->last_comp_version);
-
- if (magic == FDT_MAGIC) {
+ if (fdt_magic(fdt) == FDT_MAGIC) {
/* Complete tree */
- if (version < FDT_FIRST_SUPPORTED_VERSION)
+ if (fdt_version(fdt) < FDT_FIRST_SUPPORTED_VERSION)
return FDT_ERR_BADVERSION;
- if (last_comp_version > FDT_LAST_SUPPORTED_VERSION)
+ if (fdt_last_comp_version(fdt) > FDT_LAST_SUPPORTED_VERSION)
return FDT_ERR_BADVERSION;
- } else if (magic == SW_MAGIC) {
+ } else if (fdt_magic(fdt) == SW_MAGIC) {
/* Unfinished sequential-write blob */
- if (SW_SIZE_DT_STRUCT(fdt) == 0)
+ if (sw_size_dt_struct(fdt) == 0)
return FDT_ERR_BADSTATE;
} else {
return FDT_ERR_BADMAGIC;
@@ -73,7 +69,7 @@
char *fdt_string(const struct fdt_header *fdt, int stroffset)
{
- return (char *)fdt + fdt32_to_cpu(fdt->off_dt_strings) + stroffset;
+ return (char *)fdt + fdt_off_dt_strings(fdt) + stroffset;
}
int fdt_property_offset(const struct fdt_header *fdt, int nodeoffset,
diff --git a/fdt_sw.c b/fdt_sw.c
index 84a38d8..b2545b9 100644
--- a/fdt_sw.c
+++ b/fdt_sw.c
@@ -25,24 +25,23 @@
static int check_header_sw(struct fdt_header *fdt)
{
- if (fdt32_to_cpu(fdt->magic) != SW_MAGIC)
+ if (fdt_magic(fdt) != SW_MAGIC)
return FDT_ERR_BADMAGIC;
return 0;
}
static void *grab_space(struct fdt_header *fdt, int len)
{
- int off_dt_struct = fdt32_to_cpu(fdt->off_dt_struct);
- int offset = fdt32_to_cpu(SW_SIZE_DT_STRUCT(fdt));
+ int offset = sw_size_dt_struct(fdt);
int spaceleft;
- spaceleft = fdt32_to_cpu(fdt->totalsize) - off_dt_struct
- - fdt32_to_cpu(fdt->size_dt_strings);
+ spaceleft = fdt_totalsize(fdt) - fdt_off_dt_struct(fdt)
+ - fdt_size_dt_strings(fdt);
if ((offset + len < offset) || (offset + len > spaceleft))
return NULL;
- SW_SIZE_DT_STRUCT(fdt) = cpu_to_fdt32(offset + len);
+ fdt->version = cpu_to_fdt32(offset + len);
return fdt_offset_ptr(fdt, offset, len);
}
@@ -74,11 +73,11 @@
if (err)
return err;
- if (SW_SIZE_DT_STRUCT(fdt))
+ if (sw_size_dt_struct(fdt))
return FDT_ERR_BADSTATE;
- offset = fdt32_to_cpu(fdt->off_dt_struct);
- if ((offset + sizeof(*re)) > fdt32_to_cpu(fdt->totalsize))
+ offset = fdt_off_dt_struct(fdt);
+ if ((offset + sizeof(*re)) > fdt_totalsize(fdt))
return FDT_ERR_NOSPACE;
re = (struct fdt_reserve_entry *)((void *)fdt + offset);
@@ -131,9 +130,8 @@
static int find_add_string(struct fdt_header *fdt, const char *s)
{
- int totalsize = fdt32_to_cpu(fdt->totalsize);
- char *strtab = (char *)fdt + totalsize;
- int strtabsize = fdt32_to_cpu(fdt->size_dt_strings);
+ char *strtab = (char *)fdt + fdt_totalsize(fdt);
+ int strtabsize = fdt_size_dt_strings(fdt);
int len = strlen(s) + 1;
int struct_top, offset;
@@ -149,9 +147,8 @@
/* Add it */
offset = -strtabsize - len;
- struct_top = fdt32_to_cpu(fdt->off_dt_struct)
- + fdt32_to_cpu(SW_SIZE_DT_STRUCT(fdt));
- if (totalsize + offset < struct_top)
+ struct_top = fdt_off_dt_struct(fdt) + sw_size_dt_struct(fdt);
+ if (fdt_totalsize(fdt) + offset < struct_top)
return 0; /* no more room :( */
memcpy(strtab + offset, s, len);
@@ -188,7 +185,7 @@
int err = check_header_sw(fdt);
char *p = (char *)fdt;
uint32_t *end;
- int oldstroffset, newstroffset, strsize;
+ int oldstroffset, newstroffset;
uint32_t tag;
int offset, nextoffset;
@@ -202,11 +199,9 @@
*end = cpu_to_fdt32(FDT_END);
/* Relocate the string table */
- strsize = fdt32_to_cpu(fdt->size_dt_strings);
- oldstroffset = fdt32_to_cpu(fdt->totalsize) - strsize;
- newstroffset = fdt32_to_cpu(fdt->off_dt_struct)
- + fdt32_to_cpu(SW_SIZE_DT_STRUCT(fdt));
- memmove(p + newstroffset, p + oldstroffset, strsize);
+ oldstroffset = fdt_totalsize(fdt) - fdt_size_dt_strings(fdt);
+ newstroffset = fdt_off_dt_struct(fdt) + sw_size_dt_struct(fdt);
+ memmove(p + newstroffset, p + oldstroffset, fdt_size_dt_strings(fdt));
fdt->off_dt_strings = fdt32_to_cpu(newstroffset);
/* Walk the structure, correcting string offsets */
@@ -221,14 +216,14 @@
return FDT_ERR_BADSTRUCTURE;
nameoff = fdt32_to_cpu(prop->nameoff);
- nameoff += strsize;
+ nameoff += fdt_size_dt_strings(fdt);
prop->nameoff = cpu_to_fdt32(nameoff);
}
offset = nextoffset;
}
/* Finally, adjust the header */
- fdt->totalsize = cpu_to_fdt32(newstroffset + strsize);
+ fdt->totalsize = cpu_to_fdt32(newstroffset + fdt_size_dt_strings(fdt));
fdt->version = cpu_to_fdt32(FDT_LAST_SUPPORTED_VERSION);
fdt->last_comp_version= cpu_to_fdt32(FDT_FIRST_SUPPORTED_VERSION);
fdt->magic = cpu_to_fdt32(FDT_MAGIC);
diff --git a/libfdt.h b/libfdt.h
index b58ae0f..d4f98dc 100644
--- a/libfdt.h
+++ b/libfdt.h
@@ -20,6 +20,7 @@
*/
#include <fdt.h>
+#include <libfdt_env.h>
#define FDT_FIRST_SUPPORTED_VERSION 0x10
#define FDT_LAST_SUPPORTED_VERSION 0x10
@@ -42,7 +43,16 @@
#define FDT_ERR_MAX 13
-/* Offset handling functions */
+#define fdt_magic(fdt) (fdt32_to_cpu(fdt)->magic)
+#define fdt_totalsize(fdt) (fdt32_to_cpu(fdt)->totalsize)
+#define fdt_off_dt_struct(fdt) (fdt32_to_cpu(fdt)->off_dt_struct)
+#define fdt_off_dt_strings(fdt) (fdt32_to_cpu(fdt)->off_dt_strings)
+#define fdt_off_mem_rsvmap(fdt) (fdt32_to_cpu(fdt)->off_mem_rsvmap)
+#define fdt_version(fdt) (fdt32_to_cpu(fdt)->version)
+#define fdt_last_comp_version(fdt) (fdt32_to_cpu(fdt)->last_comp_version)
+#define fdt_boot_cpuid_phys(fdt) (fdt32_to_cpu(fdt)->boot_cpuid_phys)
+#define fdt_size_dt_strings(fdt) (fdt32_to_cpu(fdt)->size_dt_strings)
+
void *fdt_offset_ptr(const struct fdt_header *fdt, int offset, int checklen);
#define fdt_offset_ptr_typed(fdt, offset, var) \
diff --git a/libfdt_env.h b/libfdt_env.h
index a9b196f..59f2536 100644
--- a/libfdt_env.h
+++ b/libfdt_env.h
@@ -1,3 +1,6 @@
+#ifndef _LIBFDT_ENV_H
+#define _LIBFDT_ENV_H
+
#include <stddef.h>
#include <stdint.h>
#include <string.h>
@@ -16,4 +19,4 @@
#define cpu_to_fdt64(x) (bswap_64((x)))
#endif
-#include "libfdt.h"
+#endif /* _LIBFDT_ENV_H */
diff --git a/libfdt_internal.h b/libfdt_internal.h
index 0bc2786..06fa1cc 100644
--- a/libfdt_internal.h
+++ b/libfdt_internal.h
@@ -35,6 +35,6 @@
#define PTR_ERROR(code) (void *)(-(code))
#define SW_MAGIC (~FDT_MAGIC)
-#define SW_SIZE_DT_STRUCT(fdt) ((fdt)->version)
+#define sw_size_dt_struct(fdt) (fdt32_to_cpu(((fdt)->version)))
#endif /* _LIBFDT_INTERNAL_H */
diff --git a/tests/dumptrees.c b/tests/dumptrees.c
index d7e110d..f0a9b2c 100644
--- a/tests/dumptrees.c
+++ b/tests/dumptrees.c
@@ -4,8 +4,8 @@
#include <fcntl.h>
#include <fdt.h>
+#include <libfdt_env.h>
-#include "tests.h"
#include "testdata.h"
struct {
diff --git a/tests/tests.h b/tests/tests.h
index 64ab64f..732e6a4 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -37,18 +37,6 @@
#define streq(s1, s2) (strcmp((s1),(s2)) == 0)
-#if __BYTE_ORDER == __BIG_ENDIAN
-#define fdt32_to_cpu(x) (x)
-#define cpu_to_fdt32(x) (x)
-#define fdt64_to_cpu(x) (x)
-#define cpu_to_fdt64(x) (x)
-#else
-#define fdt32_to_cpu(x) (bswap_32((x)))
-#define cpu_to_fdt32(x) (bswap_32((x)))
-#define fdt64_to_cpu(x) (bswap_64((x)))
-#define cpu_to_fdt64(x) (bswap_64((x)))
-#endif
-
/* Each test case must define this function */
void cleanup(void);