Browse Source

dtc: Use the same endian-conversion functions as libfdt

Currently both libfdt and dtc define a set of endian conversion macros
for accessing the device tree blob which is always big-endian.  libfdt
uses names like cpu_to_fdt32() and dtc uses names like cpu_to_be32 (as
the Linux kernel).  This patch switches dtc over to using the libfdt
macros (including libfdt_env.h to supply them).  This has a couple of
small advantages:
	- Removes some code duplication
	- Will make conversion a bit easier if we ever need to produce
          little-endian device tree blobs.
	- dtc no longer needs to pull in netinet/in.h simply for the
          ntohs() and ntohl() functions

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
main
David Gibson 17 years ago committed by Jon Loeliger
parent
commit
c8c374b856
  1. 2
      checks.c
  2. 8
      data.c
  3. 17
      dtc.h
  4. 66
      flattree.c
  5. 2
      livetree.c
  6. 2
      treesource.c

2
checks.c

@ -363,7 +363,7 @@ static void fixup_phandle_references(struct check *c, struct node *dt, @@ -363,7 +363,7 @@ static void fixup_phandle_references(struct check *c, struct node *dt,
}

phandle = get_node_phandle(dt, refnode);
*((cell_t *)(prop->val.val + m->offset)) = cpu_to_be32(phandle);
*((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
}
}
CHECK(phandle_references, NULL, NULL, fixup_phandle_references, NULL, ERROR,

8
data.c

@ -247,7 +247,7 @@ struct data data_merge(struct data d1, struct data d2) @@ -247,7 +247,7 @@ struct data data_merge(struct data d1, struct data d2)

struct data data_append_cell(struct data d, cell_t word)
{
cell_t beword = cpu_to_be32(word);
cell_t beword = cpu_to_fdt32(word);

return data_append_data(d, &beword, sizeof(beword));
}
@ -256,15 +256,15 @@ struct data data_append_re(struct data d, const struct fdt_reserve_entry *re) @@ -256,15 +256,15 @@ struct data data_append_re(struct data d, const struct fdt_reserve_entry *re)
{
struct fdt_reserve_entry bere;

bere.address = cpu_to_be64(re->address);
bere.size = cpu_to_be64(re->size);
bere.address = cpu_to_fdt64(re->address);
bere.size = cpu_to_fdt64(re->size);

return data_append_data(d, &bere, sizeof(bere));
}

struct data data_append_addr(struct data d, uint64_t addr)
{
uint64_t beaddr = cpu_to_be64(addr);
uint64_t beaddr = cpu_to_fdt64(addr);

return data_append_data(d, &beaddr, sizeof(beaddr));
}

17
dtc.h

@ -30,10 +30,8 @@ @@ -30,10 +30,8 @@
#include <ctype.h>
#include <errno.h>
#include <unistd.h>
#include <netinet/in.h>
#include <endian.h>
#include <byteswap.h>

#include <libfdt_env.h>
#include <fdt.h>

#define DEFAULT_FDT_VERSION 17
@ -77,19 +75,6 @@ static inline void *xrealloc(void *p, size_t len) @@ -77,19 +75,6 @@ static inline void *xrealloc(void *p, size_t len)

typedef uint32_t cell_t;

#define cpu_to_be16(x) htons(x)
#define be16_to_cpu(x) ntohs(x)

#define cpu_to_be32(x) htonl(x)
#define be32_to_cpu(x) ntohl(x)

#if __BYTE_ORDER == __BIG_ENDIAN
#define cpu_to_be64(x) (x)
#define be64_to_cpu(x) (x)
#else
#define cpu_to_be64(x) bswap_64(x)
#define be64_to_cpu(x) bswap_64(x)
#endif

#define streq(a, b) (strcmp((a), (b)) == 0)
#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)

66
flattree.c

@ -170,17 +170,11 @@ static void asm_emit_data(void *e, struct data d) @@ -170,17 +170,11 @@ static void asm_emit_data(void *e, struct data d)

while ((d.len - off) >= sizeof(uint32_t)) {
fprintf(f, "\t.long\t0x%x\n",
be32_to_cpu(*((uint32_t *)(d.val+off))));
fdt32_to_cpu(*((uint32_t *)(d.val+off))));
off += sizeof(uint32_t);
}

if ((d.len - off) >= sizeof(uint16_t)) {
fprintf(f, "\t.short\t0x%hx\n",
be16_to_cpu(*((uint16_t *)(d.val+off))));
off += sizeof(uint16_t);
}

if ((d.len - off) >= 1) {
while ((d.len - off) >= 1) {
fprintf(f, "\t.byte\t0x%hhx\n", d.val[off]);
off += 1;
}
@ -333,25 +327,25 @@ static void make_fdt_header(struct fdt_header *fdt, @@ -333,25 +327,25 @@ static void make_fdt_header(struct fdt_header *fdt,

memset(fdt, 0xff, sizeof(*fdt));

fdt->magic = cpu_to_be32(FDT_MAGIC);
fdt->version = cpu_to_be32(vi->version);
fdt->last_comp_version = cpu_to_be32(vi->last_comp_version);
fdt->magic = cpu_to_fdt32(FDT_MAGIC);
fdt->version = cpu_to_fdt32(vi->version);
fdt->last_comp_version = cpu_to_fdt32(vi->last_comp_version);

/* Reserve map should be doubleword aligned */
reserve_off = ALIGN(vi->hdr_size, 8);

fdt->off_mem_rsvmap = cpu_to_be32(reserve_off);
fdt->off_dt_struct = cpu_to_be32(reserve_off + reservesize);
fdt->off_dt_strings = cpu_to_be32(reserve_off + reservesize
fdt->off_mem_rsvmap = cpu_to_fdt32(reserve_off);
fdt->off_dt_struct = cpu_to_fdt32(reserve_off + reservesize);
fdt->off_dt_strings = cpu_to_fdt32(reserve_off + reservesize
+ dtsize);
fdt->totalsize = cpu_to_be32(reserve_off + reservesize + dtsize + strsize);
fdt->totalsize = cpu_to_fdt32(reserve_off + reservesize + dtsize + strsize);

if (vi->flags & FTF_BOOTCPUID)
fdt->boot_cpuid_phys = cpu_to_be32(boot_cpuid_phys);
fdt->boot_cpuid_phys = cpu_to_fdt32(boot_cpuid_phys);
if (vi->flags & FTF_STRTABSIZE)
fdt->size_dt_strings = cpu_to_be32(strsize);
fdt->size_dt_strings = cpu_to_fdt32(strsize);
if (vi->flags & FTF_STRUCTSIZE)
fdt->size_dt_struct = cpu_to_be32(dtsize);
fdt->size_dt_struct = cpu_to_fdt32(dtsize);
}

void dt_to_blob(FILE *f, struct boot_info *bi, int version)
@ -385,20 +379,20 @@ void dt_to_blob(FILE *f, struct boot_info *bi, int version) @@ -385,20 +379,20 @@ void dt_to_blob(FILE *f, struct boot_info *bi, int version)
* If the user asked for more space than is used, adjust the totalsize.
*/
if (minsize > 0) {
padlen = minsize - be32_to_cpu(fdt.totalsize);
padlen = minsize - fdt32_to_cpu(fdt.totalsize);
if ((padlen < 0) && (quiet < 1))
fprintf(stderr,
"Warning: blob size %d >= minimum size %d\n",
be32_to_cpu(fdt.totalsize), minsize);
fdt32_to_cpu(fdt.totalsize), minsize);
}

if (padsize > 0)
padlen = padsize;

if (padlen > 0) {
int tsize = be32_to_cpu(fdt.totalsize);
int tsize = fdt32_to_cpu(fdt.totalsize);
tsize += padlen;
fdt.totalsize = cpu_to_be32(tsize);
fdt.totalsize = cpu_to_fdt32(tsize);
}

/*
@ -583,7 +577,7 @@ static uint32_t flat_read_word(struct inbuf *inb) @@ -583,7 +577,7 @@ static uint32_t flat_read_word(struct inbuf *inb)

flat_read_chunk(inb, &val, sizeof(val));

return be32_to_cpu(val);
return fdt32_to_cpu(val);
}

static void flat_realign(struct inbuf *inb, int align)
@ -689,8 +683,8 @@ static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb) @@ -689,8 +683,8 @@ static struct reserve_info *flat_read_mem_reserve(struct inbuf *inb)
p = inb->ptr;
while (1) {
flat_read_chunk(inb, &re, sizeof(re));
re.address = be64_to_cpu(re.address);
re.size = be64_to_cpu(re.size);
re.address = fdt64_to_cpu(re.address);
re.size = fdt64_to_cpu(re.size);
if (re.size == 0)
break;

@ -810,7 +804,7 @@ struct boot_info *dt_from_blob(const char *fname) @@ -810,7 +804,7 @@ struct boot_info *dt_from_blob(const char *fname)
die("Mysterious short read reading magic number\n");
}

magic = be32_to_cpu(magic);
magic = fdt32_to_cpu(magic);
if (magic != FDT_MAGIC)
die("Blob has incorrect magic number\n");

@ -824,15 +818,15 @@ struct boot_info *dt_from_blob(const char *fname) @@ -824,15 +818,15 @@ struct boot_info *dt_from_blob(const char *fname)
die("Mysterious short read reading blob size\n");
}

totalsize = be32_to_cpu(totalsize);
totalsize = fdt32_to_cpu(totalsize);
if (totalsize < FDT_V1_SIZE)
die("DT blob size (%d) is too small\n", totalsize);

blob = xmalloc(totalsize);

fdt = (struct fdt_header *)blob;
fdt->magic = cpu_to_be32(magic);
fdt->totalsize = cpu_to_be32(totalsize);
fdt->magic = cpu_to_fdt32(magic);
fdt->totalsize = cpu_to_fdt32(totalsize);

sizeleft = totalsize - sizeof(magic) - sizeof(totalsize);
p = blob + sizeof(magic) + sizeof(totalsize);
@ -851,11 +845,11 @@ struct boot_info *dt_from_blob(const char *fname) @@ -851,11 +845,11 @@ struct boot_info *dt_from_blob(const char *fname)
p += rc;
}

off_dt = be32_to_cpu(fdt->off_dt_struct);
off_str = be32_to_cpu(fdt->off_dt_strings);
off_mem_rsvmap = be32_to_cpu(fdt->off_mem_rsvmap);
version = be32_to_cpu(fdt->version);
boot_cpuid_phys = be32_to_cpu(fdt->boot_cpuid_phys);
off_dt = fdt32_to_cpu(fdt->off_dt_struct);
off_str = fdt32_to_cpu(fdt->off_dt_strings);
off_mem_rsvmap = fdt32_to_cpu(fdt->off_mem_rsvmap);
version = fdt32_to_cpu(fdt->version);
boot_cpuid_phys = fdt32_to_cpu(fdt->boot_cpuid_phys);

if (off_mem_rsvmap >= totalsize)
die("Mem Reserve structure offset exceeds total size\n");
@ -867,7 +861,7 @@ struct boot_info *dt_from_blob(const char *fname) @@ -867,7 +861,7 @@ struct boot_info *dt_from_blob(const char *fname)
die("String table offset exceeds total size\n");

if (version >= 3) {
uint32_t size_str = be32_to_cpu(fdt->size_dt_strings);
uint32_t size_str = fdt32_to_cpu(fdt->size_dt_strings);
if (off_str+size_str > totalsize)
die("String table extends past total size\n");
inbuf_init(&strbuf, blob + off_str, blob + off_str + size_str);
@ -876,7 +870,7 @@ struct boot_info *dt_from_blob(const char *fname) @@ -876,7 +870,7 @@ struct boot_info *dt_from_blob(const char *fname)
}

if (version >= 17) {
size_dt = be32_to_cpu(fdt->size_dt_struct);
size_dt = fdt32_to_cpu(fdt->size_dt_struct);
if (off_dt+size_dt > totalsize)
die("Structure block extends past total size\n");
}

2
livetree.c

@ -204,7 +204,7 @@ struct property *get_property(struct node *node, const char *propname) @@ -204,7 +204,7 @@ struct property *get_property(struct node *node, const char *propname)
cell_t propval_cell(struct property *prop)
{
assert(prop->val.len == sizeof(cell_t));
return be32_to_cpu(*((cell_t *)prop->val.val));
return fdt32_to_cpu(*((cell_t *)prop->val.val));
}

struct node *get_subnode(struct node *node, const char *nodename)

2
treesource.c

@ -147,7 +147,7 @@ static void write_propval_cells(FILE *f, struct data val) @@ -147,7 +147,7 @@ static void write_propval_cells(FILE *f, struct data val)
m = m->next;
}

fprintf(f, "0x%x", be32_to_cpu(*cp++));
fprintf(f, "0x%x", fdt32_to_cpu(*cp++));
if ((void *)cp >= propend)
break;
fprintf(f, " ");

Loading…
Cancel
Save