diff options
Diffstat (limited to 'scripts/dtc/dtc.c')
-rw-r--r-- | scripts/dtc/dtc.c | 62 |
1 files changed, 60 insertions, 2 deletions
diff --git a/scripts/dtc/dtc.c b/scripts/dtc/dtc.c index 8c4add69a765..5fa23c406266 100644 --- a/scripts/dtc/dtc.c +++ b/scripts/dtc/dtc.c | |||
@@ -18,6 +18,8 @@ | |||
18 | * USA | 18 | * USA |
19 | */ | 19 | */ |
20 | 20 | ||
21 | #include <sys/stat.h> | ||
22 | |||
21 | #include "dtc.h" | 23 | #include "dtc.h" |
22 | #include "srcpos.h" | 24 | #include "srcpos.h" |
23 | 25 | ||
@@ -104,11 +106,56 @@ static const char * const usage_opts_help[] = { | |||
104 | NULL, | 106 | NULL, |
105 | }; | 107 | }; |
106 | 108 | ||
109 | static const char *guess_type_by_name(const char *fname, const char *fallback) | ||
110 | { | ||
111 | const char *s; | ||
112 | |||
113 | s = strrchr(fname, '.'); | ||
114 | if (s == NULL) | ||
115 | return fallback; | ||
116 | if (!strcasecmp(s, ".dts")) | ||
117 | return "dts"; | ||
118 | if (!strcasecmp(s, ".dtb")) | ||
119 | return "dtb"; | ||
120 | return fallback; | ||
121 | } | ||
122 | |||
123 | static const char *guess_input_format(const char *fname, const char *fallback) | ||
124 | { | ||
125 | struct stat statbuf; | ||
126 | uint32_t magic; | ||
127 | FILE *f; | ||
128 | |||
129 | if (stat(fname, &statbuf) != 0) | ||
130 | return fallback; | ||
131 | |||
132 | if (S_ISDIR(statbuf.st_mode)) | ||
133 | return "fs"; | ||
134 | |||
135 | if (!S_ISREG(statbuf.st_mode)) | ||
136 | return fallback; | ||
137 | |||
138 | f = fopen(fname, "r"); | ||
139 | if (f == NULL) | ||
140 | return fallback; | ||
141 | if (fread(&magic, 4, 1, f) != 1) { | ||
142 | fclose(f); | ||
143 | return fallback; | ||
144 | } | ||
145 | fclose(f); | ||
146 | |||
147 | magic = fdt32_to_cpu(magic); | ||
148 | if (magic == FDT_MAGIC) | ||
149 | return "dtb"; | ||
150 | |||
151 | return guess_type_by_name(fname, fallback); | ||
152 | } | ||
153 | |||
107 | int main(int argc, char *argv[]) | 154 | int main(int argc, char *argv[]) |
108 | { | 155 | { |
109 | struct boot_info *bi; | 156 | struct boot_info *bi; |
110 | const char *inform = "dts"; | 157 | const char *inform = NULL; |
111 | const char *outform = "dts"; | 158 | const char *outform = NULL; |
112 | const char *outname = "-"; | 159 | const char *outname = "-"; |
113 | const char *depname = NULL; | 160 | const char *depname = NULL; |
114 | bool force = false, sort = false; | 161 | bool force = false, sort = false; |
@@ -213,6 +260,17 @@ int main(int argc, char *argv[]) | |||
213 | fprintf(depfile, "%s:", outname); | 260 | fprintf(depfile, "%s:", outname); |
214 | } | 261 | } |
215 | 262 | ||
263 | if (inform == NULL) | ||
264 | inform = guess_input_format(arg, "dts"); | ||
265 | if (outform == NULL) { | ||
266 | outform = guess_type_by_name(outname, NULL); | ||
267 | if (outform == NULL) { | ||
268 | if (streq(inform, "dts")) | ||
269 | outform = "dtb"; | ||
270 | else | ||
271 | outform = "dts"; | ||
272 | } | ||
273 | } | ||
216 | if (streq(inform, "dts")) | 274 | if (streq(inform, "dts")) |
217 | bi = dt_from_source(arg); | 275 | bi = dt_from_source(arg); |
218 | else if (streq(inform, "fs")) | 276 | else if (streq(inform, "fs")) |