Add extract_dtb

extract_dtb is a small utility to extract DTB files from Image.gz-dtb.
It searches the FDT header magic numbers in reversed order,
and output all found DTBs.

Test: extract_dtb Image.gz-dtb dtb Image.gz
Test: extract_dtb Image.gz-dtb dtb
Change-Id: I4e03291fd8ab5cf003f4d4cb2d153fbee56bfe7d
diff --git a/tests/src/Android.mk b/tests/src/Android.mk
index 357776c..adac198 100644
--- a/tests/src/Android.mk
+++ b/tests/src/Android.mk
@@ -30,3 +30,16 @@
 include $(BUILD_HOST_EXECUTABLE)
 
 ###################################################
+
+include $(CLEAR_VARS)
+
+LOCAL_MODULE := extract_dtb
+LOCAL_SRC_FILES := extract_dtb.c util.c
+LOCAL_STATIC_LIBRARIES := \
+    libfdt \
+    libufdt_sysdeps
+LOCAL_REQUIRED_MODULES := dtc
+
+include $(BUILD_HOST_EXECUTABLE)
+
+###################################################
diff --git a/tests/src/extract_dtb.c b/tests/src/extract_dtb.c
new file mode 100644
index 0000000..71b763f
--- /dev/null
+++ b/tests/src/extract_dtb.c
@@ -0,0 +1,145 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libfdt.h"
+#include "libufdt_sysdeps.h"
+
+#include "util.h"
+
+
+int find_dtb_header_pos(const char *buf, size_t buf_size) {
+  if (buf == NULL || buf_size == 0) {
+    return -1;
+  }
+
+  int pos = buf_size;
+  while (1) {
+    pos--;
+    if (pos < 0) {
+      break;
+    }
+    uint32_t tag = fdt32_to_cpu(*(fdt32_t *)(buf + pos));
+    if (tag == FDT_MAGIC) {
+      // Hit!
+      break;
+    }
+  }
+
+  return pos;
+}
+
+int find_and_write_dtb(const char *filename,
+                       const char *buf, size_t buf_size) {
+  int tag_pos = find_dtb_header_pos(buf, buf_size);
+  if (tag_pos < 0) {
+    goto end;
+  }
+
+  // Check FDT header
+  const char *fdt_ptr = buf + tag_pos;
+  if (fdt_check_header(fdt_ptr) != 0) {
+    fprintf(stderr, "Bad DTB header.\n");
+    goto end;
+  }
+
+  // Check FDT size and actual size
+  size_t fdt_size = fdt_totalsize(fdt_ptr);
+  size_t fdt_actual_size = buf_size - tag_pos;
+  int fdt_size_diff = (int)fdt_actual_size - (int)fdt_size;
+  if (fdt_size_diff) {
+    fprintf(stderr, "Wrong size: actual size = %d FDT size = %d(%d)\n",
+      fdt_actual_size, fdt_size, fdt_size_diff);
+  }
+
+  // Print the DT basic information
+  int root_node_off = fdt_path_offset(fdt_ptr, "/");
+  if (root_node_off < 0) {
+    fprintf(stderr, "Can not get the root node.\n");
+    goto end;
+  }
+  printf("Output %s\n", filename);
+  const char *model =
+    (const char *)fdt_getprop(fdt_ptr, root_node_off, "model", NULL);
+  printf("  model=%s\n", model ? model : "(unknown)");
+  const char *compatible =
+    (const char *)fdt_getprop(fdt_ptr, root_node_off, "compatible", NULL);
+  printf("  compatible=%s\n", compatible ? compatible : "(unknown)");
+
+  // Output DTB file
+  if (write_fdt_to_file(filename, fdt_ptr) != 0) {
+    fprintf(stderr, "Write file error: %s\n", filename);
+    goto end;
+  }
+
+end:
+  return tag_pos;
+}
+
+int extract_dtbs(const char *in_filename,
+                 const char *out_dtb_filename,
+                 const char *out_image_filename) {
+  int ret = 1;
+  char *buf = NULL;
+
+  size_t buf_size;
+  buf = load_file(in_filename, &buf_size);
+  if (!buf) {
+    fprintf(stderr, "Can not load file: %s\n", in_filename);
+    goto end;
+  }
+
+  char filename[128];
+  int index = 0;
+  while (buf_size > 0) {
+    int tag_pos;
+    if (index > 0) {
+      snprintf(filename, sizeof(filename), "%s.%d", out_dtb_filename, index);
+      tag_pos = find_and_write_dtb(filename, buf, buf_size);
+    } else {
+      tag_pos = find_and_write_dtb(out_dtb_filename, buf, buf_size);
+    }
+    if (tag_pos < 0) {
+      break;
+    }
+
+    buf_size = tag_pos;
+    index++;
+  }
+
+  if (index == 0) {
+    fprintf(stderr, "Can not find any DTB.\n");
+    goto end;
+  }
+
+  // Output the rest buffer as image file
+  if (out_image_filename != NULL) {
+    printf("Output %s\n", out_image_filename);
+    if (write_buf_to_file(out_image_filename, buf, buf_size) != 0) {
+      fprintf(stderr, "Write file error: %s\n", out_image_filename);
+      goto end;
+    }
+  }
+
+  ret = 0;
+
+end:
+  if (buf) dto_free(buf);
+
+  return ret;
+}
+
+int main(int argc, char **argv) {
+  if (argc < 3) {
+    fprintf(stderr, "Usage: %s <image.gz-dtb_file> <out_dtb_name> (<out_image_name>)\n\n", argv[0]);
+    fprintf(stderr, " Note: If there are more than one DTB, it outputs DTB files named\n" \
+                    "       out_dtb_name, out_dtb_name.1, out_dtb_name.2, etc.\n");
+    return 1;
+  }
+
+  const char *in_file = argv[1];
+  const char *out_dtb_file = argv[2];
+  const char *out_image_file = (argc > 3) ? argv[3] : NULL;
+  int ret = extract_dtbs(in_file, out_dtb_file, out_image_file);
+
+  return ret;
+}
diff --git a/tests/src/util.c b/tests/src/util.c
index 69f5c34..0d8040d 100644
--- a/tests/src/util.c
+++ b/tests/src/util.c
@@ -1,6 +1,7 @@
 #include "util.h"
 
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "libfdt.h"
 #include "libufdt_sysdeps.h"
@@ -29,7 +30,8 @@
   return buf;
 }
 
-int write_fdt_to_file(const char *filename, void *fdt) {
+int write_buf_to_file(const char *filename,
+                      const void *buf, size_t buf_size) {
   int ret = 0;
   FILE *fout = NULL;
 
@@ -38,7 +40,7 @@
     ret = 1;
     goto end;
   }
-  if (fwrite(fdt, 1, fdt_totalsize(fdt), fout) < 1) {
+  if (fwrite(buf, 1, buf_size, fout) < 1) {
     ret = 2;
     goto end;
   }
@@ -48,3 +50,7 @@
 
   return ret;
 }
+
+int write_fdt_to_file(const char *filename, const void *fdt) {
+  return write_buf_to_file(filename, fdt, fdt_totalsize(fdt));
+}
diff --git a/tests/src/util.h b/tests/src/util.h
index f77d541..9df0ae7 100644
--- a/tests/src/util.h
+++ b/tests/src/util.h
@@ -5,6 +5,7 @@
 
 
 char *load_file(const char *filename, size_t *pLen);
-int write_fdt_to_file(const char *filename, void *fdt);
+int write_buf_to_file(const char *filename, const void *buf, size_t buf_size);
+int write_fdt_to_file(const char *filename, const void *fdt);
 
 #endif