[Skiboot] [PATCH] hdata: Add FDT output to hdata_to_dt

Oliver O'Halloran oohall at gmail.com
Tue Jan 24 16:54:30 AEDT 2017


Adds a new -d <filename> command line option to the hdata_to_dt utility
to output a FDT blob. This lets us output the output DT in standard form
that can be handled by the usual tools such as dtc.

Signed-off-by: Oliver O'Halloran <oohall at gmail.com>
---
 hdata/test/hdata_to_dt.c | 48 ++++++++++++++++++++++++++++++++++++++++++----
 hdata/test/stubs.c       | 50 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 94 insertions(+), 4 deletions(-)

diff --git a/hdata/test/hdata_to_dt.c b/hdata/test/hdata_to_dt.c
index 06f900544fcb..04a31f8b0d46 100644
--- a/hdata/test/hdata_to_dt.c
+++ b/hdata/test/hdata_to_dt.c
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdint.h>
+#include <mem_region-malloc.h>
 
 #include <interrupts.h>
 
@@ -29,6 +30,8 @@
 
 #include "../../libfdt/fdt.c"
 #include "../../libfdt/fdt_ro.c"
+#include "../../libfdt/fdt_sw.c"
+#include "../../libfdt/fdt_strerror.c"
 
 struct dt_node *opal_node;
 
@@ -44,7 +47,6 @@ static void *ntuple_addr(const struct spira_ntuple *n);
 
 /* Stuff which core expects. */
 #define __this_cpu ((struct cpu_thread *)NULL)
-#define zalloc(expr) calloc(1, (expr))
 
 unsigned long tb_hz = 512000000;
 
@@ -105,6 +107,7 @@ static bool spira_check_ptr(const void *ptr, const char *file, unsigned int line
 #include "../../core/device.c"
 #include "../../core/chip.c"
 #include "../../test/dt_common.c"
+#include "../../core/fdt.c"
 
 #include <err.h>
 
@@ -145,13 +148,42 @@ static void undefined_bytes(void *p, size_t len)
 	VALGRIND_MAKE_MEM_UNDEFINED(p, len);
 }
 
+static void dump_hdata_fdt(struct dt_node *root, const char *filename)
+{
+	void *fdt_blob;
+	FILE *f;
+
+	fdt_blob = create_dtb(root, false);
+
+	if (!fdt_blob) {
+		fprintf(stderr, "Unable to make flattened DT, no FDT written\n");
+		return;
+	}
+
+	f = fopen(filename, "wb");
+	if (!f) {
+		fprintf(stderr, "Unable to open '%s' for writing\n", filename);
+		free(fdt_blob);
+		return;
+	}
+
+	fwrite(fdt_blob, fdt_totalsize(fdt_blob), 1, f);
+	fclose(f);
+
+	free(fdt_blob);
+}
+
 int main(int argc, char *argv[])
 {
 	int fd, r, i = 0, opt_count = 0;
 	bool verbose = false, quiet = false, tree_only = false, new_spira = false;
+	const char *fdt_filename = NULL;
 
 	while (argv[++i]) {
-		if (strcmp(argv[i], "-v") == 0) {
+		if (strcmp(argv[i], "-f") == 0) {
+			fdt_filename = argv[++i];
+			opt_count += 2;
+		} else if (strcmp(argv[i], "-v") == 0) {
 			verbose = true;
 			opt_count++;
 		} else if (strcmp(argv[i], "-q") == 0) {
@@ -170,8 +202,13 @@ int main(int argc, char *argv[])
 	argv += opt_count;
 	if (argc != 3) {
 		errx(1, "Usage:\n"
-		        "       hdata [-v|-q|-t] <spira-dump> <heap-dump>\n"
-		        "       hdata -s [-v|-q|-t] <spirah-dump> <spiras-dump>\n");
+			"	hdata <opts> <spira-dump> <heap-dump>\n"
+			"	hdata <opts> -s <spirah-dump> <spiras-dump>\n"
+			"Options: \n"
+			"	-v Verbose\n"
+			"	-q Quiet mode\n"
+			"	-t Print the DT nodes only, no properties\n"
+			"	-f <filename> File to write the FDT into\n");
 	}
 
 	/* Copy in spira dump (assumes little has changed!). */
@@ -242,6 +279,9 @@ int main(int argc, char *argv[])
 	if (!quiet)
 		dump_dt(dt_root, 0, !tree_only);
 
+	if (fdt_filename)
+		dump_hdata_fdt(dt_root, fdt_filename);
+
 	dt_free(dt_root);
 	return 0;
 }
diff --git a/hdata/test/stubs.c b/hdata/test/stubs.c
index 735813f21363..17f17a8351ca 100644
--- a/hdata/test/stubs.c
+++ b/hdata/test/stubs.c
@@ -16,9 +16,15 @@
 #include <stdlib.h>
 #include <stdio.h>
 #include <stdarg.h>
+#include <string.h>
+#include <malloc.h>
+
+#include <compiler.h>
 
 #include "../../ccan/list/list.c"
 
+unsigned long top_of_ram = 16ULL * 1024 * 1024 * 1024;
+
 void _prlog(int log_level __attribute__((unused)), const char* fmt, ...) __attribute__((format (printf, 2, 3)));
 
 #ifndef pr_fmt
@@ -36,6 +42,50 @@ void _prlog(int log_level __attribute__((unused)), const char* fmt, ...)
         va_end(ap);
 }
 
+/*
+ * Skiboot malloc stubs
+ *
+ * The actual prototypes for these are defined in mem_region-malloc.h,
+ * but that file also #defines malloc, and friends so we don't pull that in
+ * directly.
+ */
+
+#define DEFAULT_ALIGN __alignof__(long)
+
+void *__memalign(size_t blocksize, size_t bytes, const char *location __unused);
+void *__memalign(size_t blocksize, size_t bytes, const char *location __unused)
+{
+	return memalign(blocksize, bytes);
+}
+
+void *__malloc(size_t bytes, const char *location);
+void *__malloc(size_t bytes, const char *location)
+{
+	return __memalign(DEFAULT_ALIGN, bytes, location);
+}
+
+void __free(void *p, const char *location __unused);
+void __free(void *p, const char *location __unused)
+{
+	free(p);
+}
+
+void *__realloc(void *ptr, size_t size, const char *location __unused);
+void *__realloc(void *ptr, size_t size, const char *location __unused)
+{
+	return realloc(ptr, size);
+}
+
+void *__zalloc(size_t bytes, const char *location);
+void *__zalloc(size_t bytes, const char *location)
+{
+	void *p = __malloc(bytes, location);
+
+	if (p)
+		memset(p, 0, bytes);
+	return p;
+}
+
 /* Add any stub functions required for linking here. */
 static void stub_function(void)
 {
-- 
2.7.4



More information about the Skiboot mailing list