Fix assorted sparse warnings

This fixes a great many sparse warnings on the fdt and libfdt sources.
These are mostly due to incorrect mixing of endian annotated and native
integer types.

This includes fixing a couple of quasi-bugs where we had endian conversions
the wrong way around (this will have the right effect in practice, but is
certainly conceptually incorrect).

This doesn't make the whole tree sparse clean: there are many warnings in
bison and lex generated code, and there are a handful of other remaining
warnings that are (for now) more trouble than they're worth to fix (and
are not genuine bugs).

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
diff --git a/checks.c b/checks.c
index 4b3e1cb..38f548e 100644
--- a/checks.c
+++ b/checks.c
@@ -534,13 +534,13 @@
 					FAIL(c, dti, "Reference to non-existent node or "
 							"label \"%s\"\n", m->ref);
 				else /* mark the entry as unresolved */
-					*((cell_t *)(prop->val.val + m->offset)) =
+					*((fdt32_t *)(prop->val.val + m->offset)) =
 						cpu_to_fdt32(0xffffffff);
 				continue;
 			}
 
 			phandle = get_node_phandle(dt, refnode);
-			*((cell_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
+			*((fdt32_t *)(prop->val.val + m->offset)) = cpu_to_fdt32(phandle);
 		}
 	}
 }
diff --git a/convert-dtsv0-lexer.l b/convert-dtsv0-lexer.l
index aa32dc8..d6d68cd 100644
--- a/convert-dtsv0-lexer.l
+++ b/convert-dtsv0-lexer.l
@@ -49,7 +49,7 @@
 static unsigned long long last_val;
 static char *last_name; /* = NULL */
 
-const struct {
+static const struct {
 	const char *pattern;
 	int obase, width;
 } guess_table[] = {
diff --git a/data.c b/data.c
index 2eeeed3..aa37a16 100644
--- a/data.c
+++ b/data.c
@@ -171,9 +171,9 @@
 struct data data_append_integer(struct data d, uint64_t value, int bits)
 {
 	uint8_t value_8;
-	uint16_t value_16;
-	uint32_t value_32;
-	uint64_t value_64;
+	fdt16_t value_16;
+	fdt32_t value_32;
+	fdt64_t value_64;
 
 	switch (bits) {
 	case 8:
diff --git a/dtc.c b/dtc.c
index bb1e52b..f5eed9d 100644
--- a/dtc.c
+++ b/dtc.c
@@ -138,7 +138,7 @@
 static const char *guess_input_format(const char *fname, const char *fallback)
 {
 	struct stat statbuf;
-	uint32_t magic;
+	fdt32_t magic;
 	FILE *f;
 
 	if (stat(fname, &statbuf) != 0)
@@ -159,8 +159,7 @@
 	}
 	fclose(f);
 
-	magic = fdt32_to_cpu(magic);
-	if (magic == FDT_MAGIC)
+	if (fdt32_to_cpu(magic) == FDT_MAGIC)
 		return "dtb";
 
 	return guess_type_by_name(fname, fallback);
diff --git a/fdtdump.c b/fdtdump.c
index 194e9d6..4eaade9 100644
--- a/fdtdump.c
+++ b/fdtdump.c
@@ -20,7 +20,7 @@
 
 #define ALIGN(x, a)	(((x) + ((a) - 1)) & ~((a) - 1))
 #define PALIGN(p, a)	((void *)(ALIGN((unsigned long)(p), (a))))
-#define GET_CELL(p)	(p += 4, *((const uint32_t *)(p-4)))
+#define GET_CELL(p)	(p += 4, *((const fdt32_t *)(p-4)))
 
 static const char *tagname(uint32_t tag)
 {
diff --git a/fdtget.c b/fdtget.c
index fb9d0e1..8bd346b 100644
--- a/fdtget.c
+++ b/fdtget.c
@@ -105,7 +105,7 @@
 	for (i = 0; i < len; i += size, p += size) {
 		if (i)
 			printf(" ");
-		value = size == 4 ? fdt32_to_cpu(*(const uint32_t *)p) :
+		value = size == 4 ? fdt32_to_cpu(*(const fdt32_t *)p) :
 			size == 2 ? (*p << 8) | p[1] : *p;
 		printf(fmt, value);
 	}
diff --git a/fdtput.c b/fdtput.c
index db65e96..8a7e8a8 100644
--- a/fdtput.c
+++ b/fdtput.c
@@ -107,7 +107,7 @@
 			if (disp->verbose)
 				fprintf(stderr, "\tstring: '%s'\n", ptr);
 		} else {
-			int *iptr = (int *)ptr;
+			fdt32_t *iptr = (fdt32_t *)ptr;
 			sscanf(*arg, fmt, &ival);
 			if (len == 4)
 				*iptr = cpu_to_fdt32(ival);
diff --git a/flattree.c b/flattree.c
index fba6ab3..5de5fee 100644
--- a/flattree.c
+++ b/flattree.c
@@ -179,7 +179,7 @@
 		emit_offset_label(f, m->ref, m->offset);
 
 	while ((d.len - off) >= sizeof(uint32_t)) {
-		asm_emit_cell(e, fdt32_to_cpu(*((uint32_t *)(d.val+off))));
+		asm_emit_cell(e, fdt32_to_cpu(*((fdt32_t *)(d.val+off))));
 		off += sizeof(uint32_t);
 	}
 
@@ -608,7 +608,7 @@
 
 static uint32_t flat_read_word(struct inbuf *inb)
 {
-	uint32_t val;
+	fdt32_t val;
 
 	assert(((inb->ptr - inb->base) % sizeof(val)) == 0);
 
@@ -717,13 +717,15 @@
 	 * First pass, count entries.
 	 */
 	while (1) {
+		uint64_t address, size;
+
 		flat_read_chunk(inb, &re, sizeof(re));
-		re.address  = fdt64_to_cpu(re.address);
-		re.size = fdt64_to_cpu(re.size);
-		if (re.size == 0)
+		address  = fdt64_to_cpu(re.address);
+		size = fdt64_to_cpu(re.size);
+		if (size == 0)
 			break;
 
-		new = build_reserve_entry(re.address, re.size);
+		new = build_reserve_entry(address, size);
 		reservelist = add_reserve_entry(reservelist, new);
 	}
 
@@ -816,6 +818,7 @@
 struct dt_info *dt_from_blob(const char *fname)
 {
 	FILE *f;
+	fdt32_t magic_buf, totalsize_buf;
 	uint32_t magic, totalsize, version, size_dt, boot_cpuid_phys;
 	uint32_t off_dt, off_str, off_mem_rsvmap;
 	int rc;
@@ -832,7 +835,7 @@
 
 	f = srcfile_relative_open(fname, NULL);
 
-	rc = fread(&magic, sizeof(magic), 1, f);
+	rc = fread(&magic_buf, sizeof(magic_buf), 1, f);
 	if (ferror(f))
 		die("Error reading DT blob magic number: %s\n",
 		    strerror(errno));
@@ -843,11 +846,11 @@
 			die("Mysterious short read reading magic number\n");
 	}
 
-	magic = fdt32_to_cpu(magic);
+	magic = fdt32_to_cpu(magic_buf);
 	if (magic != FDT_MAGIC)
 		die("Blob has incorrect magic number\n");
 
-	rc = fread(&totalsize, sizeof(totalsize), 1, f);
+	rc = fread(&totalsize_buf, sizeof(totalsize_buf), 1, f);
 	if (ferror(f))
 		die("Error reading DT blob size: %s\n", strerror(errno));
 	if (rc < 1) {
@@ -857,7 +860,7 @@
 			die("Mysterious short read reading blob size\n");
 	}
 
-	totalsize = fdt32_to_cpu(totalsize);
+	totalsize = fdt32_to_cpu(totalsize_buf);
 	if (totalsize < FDT_V1_SIZE)
 		die("DT blob size (%d) is too small\n", totalsize);
 
diff --git a/libfdt/fdt_overlay.c b/libfdt/fdt_overlay.c
index 56cb70e..9d03221 100644
--- a/libfdt/fdt_overlay.c
+++ b/libfdt/fdt_overlay.c
@@ -21,14 +21,14 @@
  */
 static uint32_t overlay_get_target_phandle(const void *fdto, int fragment)
 {
-	const uint32_t *val;
+	const fdt32_t *val;
 	int len;
 
 	val = fdt_getprop(fdto, fragment, "target", &len);
 	if (!val)
 		return 0;
 
-	if ((len != sizeof(*val)) || (*val == (uint32_t)-1))
+	if ((len != sizeof(*val)) || (fdt32_to_cpu(*val) == (uint32_t)-1))
 		return (uint32_t)-1;
 
 	return fdt32_to_cpu(*val);
@@ -99,7 +99,7 @@
 static int overlay_phandle_add_offset(void *fdt, int node,
 				      const char *name, uint32_t delta)
 {
-	const uint32_t *val;
+	const fdt32_t *val;
 	uint32_t adj_val;
 	int len;
 
@@ -210,7 +210,7 @@
 	int ret;
 
 	fdt_for_each_property_offset(fixup_prop, fdto, fixup_node) {
-		const uint32_t *fixup_val;
+		const fdt32_t *fixup_val;
 		const char *tree_val;
 		const char *name;
 		int fixup_len;
@@ -234,7 +234,8 @@
 		}
 
 		for (i = 0; i < (fixup_len / sizeof(uint32_t)); i++) {
-			uint32_t adj_val, poffset;
+			fdt32_t adj_val;
+			uint32_t poffset;
 
 			poffset = fdt32_to_cpu(fixup_val[i]);
 
@@ -246,9 +247,7 @@
 			 */
 			memcpy(&adj_val, tree_val + poffset, sizeof(adj_val));
 
-			adj_val = fdt32_to_cpu(adj_val);
-			adj_val += delta;
-			adj_val = cpu_to_fdt32(adj_val);
+			adj_val = cpu_to_fdt32(fdt32_to_cpu(adj_val) + delta);
 
 			ret = fdt_setprop_inplace_namelen_partial(fdto,
 								  tree_node,
@@ -356,6 +355,7 @@
 {
 	const char *symbol_path;
 	uint32_t phandle;
+	fdt32_t phandle_prop;
 	int symbol_off, fixup_off;
 	int prop_len;
 
@@ -381,10 +381,11 @@
 	if (fixup_off < 0)
 		return fixup_off;
 
-	phandle = cpu_to_fdt32(phandle);
+	phandle_prop = cpu_to_fdt32(phandle);
 	return fdt_setprop_inplace_namelen_partial(fdto, fixup_off,
 						   name, name_len, poffset,
-						   &phandle, sizeof(phandle));
+						   &phandle_prop,
+						   sizeof(phandle_prop));
 };
 
 /**
diff --git a/livetree.c b/livetree.c
index 3130dd2..3673de0 100644
--- a/livetree.c
+++ b/livetree.c
@@ -393,7 +393,7 @@
 cell_t propval_cell(struct property *prop)
 {
 	assert(prop->val.len == sizeof(cell_t));
-	return fdt32_to_cpu(*((cell_t *)prop->val.val));
+	return fdt32_to_cpu(*((fdt32_t *)prop->val.val));
 }
 
 struct property *get_property_by_label(struct node *tree, const char *label,
@@ -902,7 +902,7 @@
 		struct node *refnode)
 {
 	struct node *wn, *nwn;	/* local fixup node, walk node, new */
-	uint32_t value_32;
+	fdt32_t value_32;
 	char **compp;
 	int i, depth;
 
diff --git a/tests/char_literal.c b/tests/char_literal.c
index d7a4773..da1f964 100644
--- a/tests/char_literal.c
+++ b/tests/char_literal.c
@@ -31,7 +31,7 @@
 int main(int argc, char *argv[])
 {
 	void *fdt;
-	uint32_t expected_cells[5];
+	fdt32_t expected_cells[5];
 
 	expected_cells[0] = cpu_to_fdt32((unsigned char)TEST_CHAR1);
 	expected_cells[1] = cpu_to_fdt32((unsigned char)TEST_CHAR2);
diff --git a/tests/dtbs_equal_ordered.c b/tests/dtbs_equal_ordered.c
index 12495de..98bf0e7 100644
--- a/tests/dtbs_equal_ordered.c
+++ b/tests/dtbs_equal_ordered.c
@@ -28,7 +28,7 @@
 #include "tests.h"
 #include "testdata.h"
 
-int notequal; /* = 0 */
+static int notequal; /* = 0 */
 
 #define MISMATCH(fmt, ...)			\
 	do { \
diff --git a/tests/dtbs_equal_unordered.c b/tests/dtbs_equal_unordered.c
index 20b4356..baf2ae7 100644
--- a/tests/dtbs_equal_unordered.c
+++ b/tests/dtbs_equal_unordered.c
@@ -29,7 +29,7 @@
 #include "tests.h"
 #include "testdata.h"
 
-int notequal; /* = 0 */
+static int notequal; /* = 0 */
 
 #define MISMATCH(fmt, ...)			\
 	do { \
@@ -59,14 +59,14 @@
 	const struct fdt_reserve_entry *re1 = p1;
 	const struct fdt_reserve_entry *re2 = p2;
 
-	if (re1->address < re2->address)
+	if (fdt64_to_cpu(re1->address) < fdt64_to_cpu(re2->address))
 		return -1;
-	else if (re1->address > re2->address)
+	else if (fdt64_to_cpu(re1->address) > fdt64_to_cpu(re2->address))
 		return 1;
 
-	if (re1->size < re2->size)
+	if (fdt64_to_cpu(re1->size) < fdt64_to_cpu(re2->size))
 		return -1;
-	else if (re1->size > re2->size)
+	else if (fdt64_to_cpu(re1->size) > fdt64_to_cpu(re2->size))
 		return 1;
 
 	return 0;
diff --git a/tests/dumptrees.c b/tests/dumptrees.c
index a49dbfa..0728811 100644
--- a/tests/dumptrees.c
+++ b/tests/dumptrees.c
@@ -29,7 +29,7 @@
 
 #include "testdata.h"
 
-struct {
+static struct {
 	void *blob;
 	const char *filename;
 } trees[] = {
diff --git a/tests/integer-expressions.c b/tests/integer-expressions.c
index 57e2ff6..ed1f967 100644
--- a/tests/integer-expressions.c
+++ b/tests/integer-expressions.c
@@ -30,7 +30,7 @@
 #include "tests.h"
 #include "testdata.h"
 
-struct test_expr {
+static struct test_expr {
 	const char *expr;
 	uint32_t result;
 } expr_table[] = {
@@ -70,7 +70,7 @@
 int main(int argc, char *argv[])
 {
 	void *fdt;
-	const uint32_t *res;
+	const fdt32_t *res;
 	int reslen;
 	int i;
 
diff --git a/tests/node_offset_by_prop_value.c b/tests/node_offset_by_prop_value.c
index 9212a4e..286f1e7 100644
--- a/tests/node_offset_by_prop_value.c
+++ b/tests/node_offset_by_prop_value.c
@@ -69,7 +69,7 @@
 
 #define check_search_cell(fdt, propname, propval, ...) \
 	{ \
-		uint32_t val = cpu_to_fdt32(propval); \
+		fdt32_t val = cpu_to_fdt32(propval); \
 		check_search((fdt), (propname), &val, sizeof(val), \
 			     ##__VA_ARGS__); \
 	}
diff --git a/tests/nopulate.c b/tests/nopulate.c
index cd79872..94ce8ad 100644
--- a/tests/nopulate.c
+++ b/tests/nopulate.c
@@ -45,7 +45,7 @@
 		       nextoffset - offset);
 		p += nextoffset - offset;
 
-		*((uint32_t *)p) = cpu_to_fdt32(FDT_NOP);
+		*((fdt32_t *)p) = cpu_to_fdt32(FDT_NOP);
 		p += FDT_TAGSIZE;
 
 	} while (tag != FDT_END);
diff --git a/tests/property_iterate.c b/tests/property_iterate.c
index 0f3959c..b5cedbe 100644
--- a/tests/property_iterate.c
+++ b/tests/property_iterate.c
@@ -33,7 +33,7 @@
 
 static void test_node(void *fdt, int parent_offset)
 {
-	fdt32_t properties;
+	uint32_t properties;
 	const fdt32_t *prop;
 	int offset, property;
 	int count;
@@ -48,7 +48,7 @@
 		FAIL("Missing/invalid test-properties property at '%s'",
 		     fdt_get_name(fdt, parent_offset, NULL));
 	}
-	properties = cpu_to_fdt32(*prop);
+	properties = fdt32_to_cpu(*prop);
 
 	count = 0;
 	offset = fdt_first_subnode(fdt, parent_offset);
diff --git a/tests/references.c b/tests/references.c
index 46662fc..cab70f6 100644
--- a/tests/references.c
+++ b/tests/references.c
@@ -29,7 +29,7 @@
 
 static void check_ref(const void *fdt, int node, uint32_t checkref)
 {
-	const uint32_t *p;
+	const fdt32_t *p;
 	uint32_t ref;
 	int len;
 
@@ -58,7 +58,7 @@
 
 static void check_rref(const void *fdt)
 {
-	const uint32_t *p;
+	const fdt32_t *p;
 	uint32_t ref;
 	int len;
 
diff --git a/tests/sized_cells.c b/tests/sized_cells.c
index 94da03b..0b2b8dc 100644
--- a/tests/sized_cells.c
+++ b/tests/sized_cells.c
@@ -54,9 +54,9 @@
 				 TEST_CHAR4,
 				 TEST_CHAR5,
 				 TEST_VALUE_1 >> 24};
-	uint16_t expected_16[6];
-	uint32_t expected_32[6];
-	uint64_t expected_64[6];
+	fdt16_t expected_16[6];
+	fdt32_t expected_32[6];
+	fdt64_t expected_64[6];
 	int i;
 
 	for (i = 0; i < 5; ++i) {
diff --git a/tests/subnode_iterate.c b/tests/subnode_iterate.c
index 0fb5c90..7be5706 100644
--- a/tests/subnode_iterate.c
+++ b/tests/subnode_iterate.c
@@ -33,7 +33,7 @@
 
 static void test_node(void *fdt, int parent_offset)
 {
-	fdt32_t subnodes;
+	uint32_t subnodes;
 	const fdt32_t *prop;
 	int offset;
 	int count;
@@ -45,7 +45,7 @@
 		FAIL("Missing/invalid subnodes property at '%s'",
 		     fdt_get_name(fdt, parent_offset, NULL));
 	}
-	subnodes = cpu_to_fdt32(*prop);
+	subnodes = fdt32_to_cpu(*prop);
 
 	count = 0;
 	fdt_for_each_subnode(offset, fdt, parent_offset)
diff --git a/tests/tests.h b/tests/tests.h
index 56a843c..c0e4d3c 100644
--- a/tests/tests.h
+++ b/tests/tests.h
@@ -99,7 +99,7 @@
 		    int len, const void *val);
 #define check_property_cell(fdt, nodeoffset, name, val) \
 	({ \
-		uint32_t x = cpu_to_fdt32(val);			      \
+		fdt32_t x = cpu_to_fdt32(val);			      \
 		check_property(fdt, nodeoffset, name, sizeof(x), &x); \
 	})
 
@@ -108,12 +108,12 @@
 			  int len, const void *val);
 #define check_getprop_cell(fdt, nodeoffset, name, val) \
 	({ \
-		uint32_t x = cpu_to_fdt32(val);			     \
+		fdt32_t x = cpu_to_fdt32(val);			     \
 		check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
 	})
 #define check_getprop_64(fdt, nodeoffset, name, val) \
 	({ \
-		uint64_t x = cpu_to_fdt64(val);			     \
+		fdt64_t x = cpu_to_fdt64(val);			     \
 		check_getprop(fdt, nodeoffset, name, sizeof(x), &x); \
 	})
 #define check_getprop_string(fdt, nodeoffset, name, s) \
diff --git a/tests/value-labels.c b/tests/value-labels.c
index dcf2059..8aced74 100644
--- a/tests/value-labels.c
+++ b/tests/value-labels.c
@@ -36,13 +36,13 @@
 	int propoff;
 };
 
-struct val_label labels1[] = {
+static struct val_label labels1[] = {
 	{ "start1", 0 },
 	{ "mid1", 2 },
 	{ "end1", -1 },
 };
 
-struct val_label labels2[] = {
+static struct val_label labels2[] = {
 	{ "start2", 0 },
 	{ "innerstart2", 0 },
 	{ "innermid2", 4 },
@@ -50,7 +50,7 @@
 	{ "end2", -1 },
 };
 
-struct val_label labels3[] = {
+static struct val_label labels3[] = {
 	{ "start3", 0 },
 	{ "innerstart3", 0 },
 	{ "innermid3", 1 },
diff --git a/treesource.c b/treesource.c
index 2bc963f..2461a3d 100644
--- a/treesource.c
+++ b/treesource.c
@@ -137,7 +137,7 @@
 static void write_propval_cells(FILE *f, struct data val)
 {
 	void *propend = val.val + val.len;
-	cell_t *cp = (cell_t *)val.val;
+	fdt32_t *cp = (fdt32_t *)val.val;
 	struct marker *m = val.markers;
 
 	fprintf(f, "<");
diff --git a/util.c b/util.c
index 3550f86..9953c32 100644
--- a/util.c
+++ b/util.c
@@ -396,7 +396,7 @@
 		} while (s < data + len);
 
 	} else if ((len % 4) == 0) {
-		const uint32_t *cell = (const uint32_t *)data;
+		const fdt32_t *cell = (const fdt32_t *)data;
 
 		printf(" = <");
 		for (i = 0, len /= 4; i < len; i++)
@@ -412,15 +412,16 @@
 	}
 }
 
-void util_version(void)
+void NORETURN util_version(void)
 {
 	printf("Version: %s\n", DTC_VERSION);
 	exit(0);
 }
 
-void util_usage(const char *errmsg, const char *synopsis,
-		const char *short_opts, struct option const long_opts[],
-		const char * const opts_help[])
+void NORETURN util_usage(const char *errmsg, const char *synopsis,
+			 const char *short_opts,
+			 struct option const long_opts[],
+			 const char * const opts_help[])
 {
 	FILE *fp = errmsg ? stderr : stdout;
 	const char a_arg[] = "<arg>";