1 /*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "libfdt.h"
21 #include "libufdt_sysdeps.h"
22
23 #include "util.h"
24
25
find_dtb_header_pos(const char * buf,size_t buf_size)26 int find_dtb_header_pos(const char *buf, size_t buf_size) {
27 if (buf == NULL || buf_size == 0) {
28 return -1;
29 }
30
31 int pos = buf_size;
32 while (1) {
33 pos--;
34 if (pos < 0) {
35 break;
36 }
37 uint32_t tag = fdt32_to_cpu(*(fdt32_t *)(buf + pos));
38 if (tag == FDT_MAGIC) {
39 // Hit!
40 break;
41 }
42 }
43
44 return pos;
45 }
46
find_and_write_dtb(const char * filename,const char * buf,size_t buf_size)47 int find_and_write_dtb(const char *filename,
48 const char *buf, size_t buf_size) {
49 int tag_pos = find_dtb_header_pos(buf, buf_size);
50 if (tag_pos < 0) {
51 goto end;
52 }
53
54 // Check FDT header
55 const char *fdt_ptr = buf + tag_pos;
56 if (fdt_check_header(fdt_ptr) != 0) {
57 fprintf(stderr, "Bad DTB header.\n");
58 goto end;
59 }
60
61 // Check FDT size and actual size
62 size_t fdt_size = fdt_totalsize(fdt_ptr);
63 size_t fdt_actual_size = buf_size - tag_pos;
64 int fdt_size_diff = (int)fdt_actual_size - (int)fdt_size;
65 if (fdt_size_diff) {
66 fprintf(stderr, "Wrong size: actual size = %d FDT size = %d(%d)\n",
67 fdt_actual_size, fdt_size, fdt_size_diff);
68 }
69
70 // Print the DT basic information
71 int root_node_off = fdt_path_offset(fdt_ptr, "/");
72 if (root_node_off < 0) {
73 fprintf(stderr, "Can not get the root node.\n");
74 goto end;
75 }
76 printf("Output %s\n", filename);
77 const char *model =
78 (const char *)fdt_getprop(fdt_ptr, root_node_off, "model", NULL);
79 printf(" model=%s\n", model ? model : "(unknown)");
80 const char *compatible =
81 (const char *)fdt_getprop(fdt_ptr, root_node_off, "compatible", NULL);
82 printf(" compatible=%s\n", compatible ? compatible : "(unknown)");
83
84 // Output DTB file
85 if (write_fdt_to_file(filename, fdt_ptr) != 0) {
86 fprintf(stderr, "Write file error: %s\n", filename);
87 goto end;
88 }
89
90 end:
91 return tag_pos;
92 }
93
extract_dtbs(const char * in_filename,const char * out_dtb_filename,const char * out_image_filename)94 int extract_dtbs(const char *in_filename,
95 const char *out_dtb_filename,
96 const char *out_image_filename) {
97 int ret = 1;
98 char *buf = NULL;
99
100 size_t buf_size;
101 buf = load_file(in_filename, &buf_size);
102 if (!buf) {
103 fprintf(stderr, "Can not load file: %s\n", in_filename);
104 goto end;
105 }
106
107 char filename[128];
108 int index = 0;
109 while (buf_size > 0) {
110 int tag_pos;
111 if (index > 0) {
112 snprintf(filename, sizeof(filename), "%s.%d", out_dtb_filename, index);
113 tag_pos = find_and_write_dtb(filename, buf, buf_size);
114 } else {
115 tag_pos = find_and_write_dtb(out_dtb_filename, buf, buf_size);
116 }
117 if (tag_pos < 0) {
118 break;
119 }
120
121 buf_size = tag_pos;
122 index++;
123 }
124
125 if (index == 0) {
126 fprintf(stderr, "Can not find any DTB.\n");
127 goto end;
128 }
129
130 // Output the rest buffer as image file
131 if (out_image_filename != NULL) {
132 printf("Output %s\n", out_image_filename);
133 if (write_buf_to_file(out_image_filename, buf, buf_size) != 0) {
134 fprintf(stderr, "Write file error: %s\n", out_image_filename);
135 goto end;
136 }
137 }
138
139 ret = 0;
140
141 end:
142 if (buf) dto_free(buf);
143
144 return ret;
145 }
146
main(int argc,char ** argv)147 int main(int argc, char **argv) {
148 if (argc < 3) {
149 fprintf(stderr, "Usage: %s <image.gz-dtb_file> <out_dtb_name> (<out_image_name>)\n\n", argv[0]);
150 fprintf(stderr, " Note: If there are more than one DTB, it outputs DTB files named\n" \
151 " out_dtb_name, out_dtb_name.1, out_dtb_name.2, etc.\n");
152 return 1;
153 }
154
155 const char *in_file = argv[1];
156 const char *out_dtb_file = argv[2];
157 const char *out_image_file = (argc > 3) ? argv[3] : NULL;
158 int ret = extract_dtbs(in_file, out_dtb_file, out_image_file);
159
160 return ret;
161 }
162