blob: f751e882fc1e3542d815e0aecc99845c0453ec39 [file] [log] [blame]
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +01001/* tools/mkbootimg/dtbimg.c
2**
3** Copyright 2007, The Android Open Source Project
4** Copyright 2013, Sony Mobile Communications
5**
6** Licensed under the Apache License, Version 2.0 (the "License");
7** you may not use this file except in compliance with the License.
8** You may obtain a copy of the License at
9**
10** http://www.apache.org/licenses/LICENSE-2.0
11**
12** Unless required by applicable law or agreed to in writing, software
13** distributed under the License is distributed on an "AS IS" BASIS,
14** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15** See the License for the specific language governing permissions and
16** limitations under the License.
17**
18** December 2016, Christopher N. Hesse
19** Separate dt creation from mkbootimg program.
20*/
21
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <unistd.h>
26#include <fcntl.h>
27#include <errno.h>
28#include <limits.h>
29
30#include <sys/types.h>
31#include <arpa/inet.h>
32#include <assert.h>
33#include <dirent.h>
34#include <err.h>
35#include <stdint.h>
36
37/* must be provided by the device tree */
38#include <samsung_dtbh.h>
39
40#include "libfdt.h"
41
42struct dt_blob;
43
44struct dt_entry {
45 uint32_t chip;
46 uint32_t platform;
47 uint32_t subtype;
48 uint32_t hw_rev;
49 uint32_t hw_rev_end;
50 uint32_t offset;
51 uint32_t size; /* including padding */
52 uint32_t space;
53
54 struct dt_blob *blob;
55};
56
57/*
58 * Comparator for sorting dt_entries
59 */
60static int dt_entry_cmp(const void *ap, const void *bp)
61{
62 struct dt_entry *a = (struct dt_entry*)ap;
63 struct dt_entry *b = (struct dt_entry*)bp;
64
65 if (a->chip != b->chip)
66 return a->chip - b->chip;
67 return a->hw_rev - b->hw_rev;
68}
69
70/*
71 * In memory representation of a dtb blob
72 */
73struct dt_blob {
74 uint32_t size;
75 uint32_t offset;
76
77 void *payload;
78 struct dt_blob *next;
79};
80
81static void *load_file(const char *fn, unsigned *_sz)
82{
83 char *data;
84 int sz;
85 int fd;
86
87 data = 0;
88 fd = open(fn, O_RDONLY);
89 if(fd < 0) return 0;
90
91 sz = lseek(fd, 0, SEEK_END);
92 if(sz < 0) goto oops;
93
94 if(lseek(fd, 0, SEEK_SET) != 0) goto oops;
95
96 data = (char*) malloc(sz);
97 if(data == 0) goto oops;
98
99 if(read(fd, data, sz) != sz) goto oops;
100 close(fd);
101
102 if(_sz) *_sz = sz;
103 return data;
104
105oops:
106 close(fd);
107 if(data != 0) free(data);
108 return 0;
109}
110
111void *load_dtbh_block(const char *dtb_path, unsigned pagesize, unsigned *_sz)
112{
113 const unsigned pagemask = pagesize - 1;
114 struct dt_entry *new_entries;
115 struct dt_entry *entries = NULL;
116 struct dt_entry *entry;
117 struct dt_blob *blob;
118 struct dt_blob *blob_list = NULL;
119 struct dt_blob *last_blob = NULL;
120 struct dirent *de;
121 unsigned new_count;
122 unsigned entry_count = 0;
123 unsigned offset;
124 unsigned dtb_sz;
125 unsigned hdr_sz = DT_HEADER_PHYS_SIZE;
126 uint32_t version = DTBH_VERSION;
127 unsigned blob_sz = 0;
128 char fname[PATH_MAX];
Stricted3336e042018-09-24 20:50:26 +0200129#ifdef DTBH_MODEL
Christopher N. Hesse1ecbe862018-07-19 23:08:14 +0200130 const unsigned *model;
Stricted3336e042018-09-24 20:50:26 +0200131#endif
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100132 const unsigned *prop_chip;
133 const unsigned *prop_platform;
134 const unsigned *prop_subtype;
135 const unsigned *prop_hw_rev;
136 const unsigned *prop_hw_rev_end;
137 int namlen;
138 int len;
139 void *dtb;
140 char *dtbh;
141 DIR *dir;
142 unsigned c;
143
144 dir = opendir(dtb_path);
145
146 if (dir == NULL)
147 err(1, "failed to open '%s'", dtb_path);
148
149 while ((de = readdir(dir)) != NULL) {
150 namlen = strlen(de->d_name);
151 if (namlen < 4 || strcmp(&de->d_name[namlen - 4], ".dtb"))
152 continue;
153
154 snprintf(fname, sizeof(fname), "%s/%s", dtb_path, de->d_name);
155
156 dtb = load_file(fname, &dtb_sz);
157 if (dtb == NULL)
158 err(1, "failed to read dtb '%s'", fname);
159
160 if (fdt_check_header(dtb) != 0) {
161 warnx("'%s' is not a valid dtb, skipping", fname);
162 free(dtb);
163 continue;
164 }
165
166 offset = fdt_path_offset(dtb, "/");
Christopher N. Hesse1ecbe862018-07-19 23:08:14 +0200167
168#ifdef DTBH_MODEL
169 model = fdt_getprop(dtb, offset, "model", &len);
170 if (strstr((char *)&model[0], DTBH_MODEL) == NULL) {
171 warnx("model of %s is invalid, skipping (expected *%s* but got %s)",
172 fname, DTBH_MODEL, (char *)&model[0]);
173 free(dtb);
174 continue;
175 }
176#endif
177
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100178 prop_chip = fdt_getprop(dtb, offset, "model_info-chip", &len);
179 if (len % (sizeof(uint32_t)) != 0) {
180 warnx("model_info-chip of %s is of invalid size, skipping", fname);
181 free(dtb);
182 continue;
183 }
184
185 prop_platform = fdt_getprop(dtb, offset, "model_info-platform", &len);
186 if (strcmp((char *)&prop_platform[0], DTBH_PLATFORM)) {
Christopher N. Hesse4ada34b2018-01-29 22:47:52 +0100187 warnx("model_info-platform of %s is invalid, skipping (expected %s but got %s)",
188 fname, DTBH_PLATFORM, (char *)&prop_platform[0]);
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100189 free(dtb);
190 continue;
191 }
192
193 prop_subtype = fdt_getprop(dtb, offset, "model_info-subtype", &len);
194 if (strcmp((char *)&prop_subtype[0], DTBH_SUBTYPE)) {
Christopher N. Hesse4ada34b2018-01-29 22:47:52 +0100195 warnx("model_info-subtype of %s is invalid, skipping (expected %s but got %s)",
196 fname, DTBH_SUBTYPE, (char *)&prop_subtype[0]);
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100197 free(dtb);
198 continue;
199 }
200
201 prop_hw_rev = fdt_getprop(dtb, offset, "model_info-hw_rev", &len);
202 if (len % (sizeof(uint32_t)) != 0) {
203 warnx("model_info-hw_rev of %s is of invalid size, skipping", fname);
204 free(dtb);
205 continue;
206 }
207
208 prop_hw_rev_end = fdt_getprop(dtb, offset, "model_info-hw_rev_end", &len);
209 if (len % (sizeof(uint32_t)) != 0) {
210 warnx("model_info-hw_rev_end of %s is of invalid size, skipping", fname);
211 free(dtb);
212 continue;
213 }
214
215 blob = calloc(1, sizeof(struct dt_blob));
216 if (blob == NULL)
217 err(1, "failed to allocate memory");
218
219 blob->payload = dtb;
220 blob->size = dtb_sz;
221 if (blob_list == NULL) {
222 blob_list = blob;
223 last_blob = blob;
224 } else {
225 last_blob->next = blob;
226 last_blob = blob;
227 }
228
229 blob_sz += (blob->size + pagemask) & ~pagemask;
230 new_count = entry_count + 1;
231 new_entries = realloc(entries, new_count * sizeof(struct dt_entry));
232 if (new_entries == NULL)
233 err(1, "failed to allocate memory");
234
235 entries = new_entries;
236 entry = &entries[entry_count];
237 memset(entry, 0, sizeof(*entry));
238 entry->chip = ntohl(prop_chip[0]);
239 entry->platform = DTBH_PLATFORM_CODE;
240 entry->subtype = DTBH_SUBTYPE_CODE;
241 entry->hw_rev = ntohl(prop_hw_rev[0]);
242 entry->hw_rev_end = ntohl(prop_hw_rev_end[0]);
243 entry->space = 0x20; /* space delimiter */
244 entry->blob = blob;
245
246 entry_count++;
247
248 hdr_sz += entry_count * DT_ENTRY_PHYS_SIZE;
249 }
250
251 closedir(dir);
252
253 if (entry_count == 0) {
254 warnx("unable to locate any dtbs in the given path");
255 return NULL;
256 }
257
258 hdr_sz += sizeof(uint32_t); /* eot marker */
259 hdr_sz = (hdr_sz + pagemask) & ~pagemask;
260
261 qsort(entries, entry_count, sizeof(struct dt_entry), dt_entry_cmp);
262
263 /* The size of the dt header is now known, calculate the blob offsets... */
264 offset = hdr_sz;
265 for (blob = blob_list; blob; blob = blob->next) {
266 blob->offset = offset;
267 offset += (blob->size + pagemask) & ~pagemask;
268 }
269
270 /* ...and update the entries */
271 for (c = 0; c < entry_count; c++) {
272 entry = &entries[c];
273
274 entry->offset = entry->blob->offset;
275 entry->size = (entry->blob->size + pagemask) & ~pagemask;
276 }
277
278 /*
279 * All parts are now gathered, so build the dt block
280 */
281 dtbh = calloc(hdr_sz + blob_sz, 1);
282 if (dtbh == NULL)
283 err(1, "failed to allocate memory");
284
285 offset = 0;
286
287 memcpy(dtbh, DTBH_MAGIC, sizeof(uint32_t));
288 memcpy(dtbh + sizeof(uint32_t), &version, sizeof(uint32_t));
289 memcpy(dtbh + (sizeof(uint32_t) * 2), &entry_count, sizeof(uint32_t));
290
291 offset += DT_HEADER_PHYS_SIZE;
292
293 /* add dtbh entries */
294 for (c = 0; c < entry_count; c++) {
295 entry = &entries[c];
296 memcpy(dtbh + offset, entry, DT_ENTRY_PHYS_SIZE);
297 offset += DT_ENTRY_PHYS_SIZE;
298 }
299
300 /* add padding after dt header */
301 offset += pagesize - (offset & pagemask);
302
303 for (blob = blob_list; blob; blob = blob->next) {
304 memcpy(dtbh + offset, blob->payload, blob->size);
305 offset += (blob->size + pagemask) & ~pagemask;
306 }
307
308 *_sz = hdr_sz + blob_sz;
309
310 return dtbh;
311}