blob: a85c9016a22d664fe4acc8a336e1f63ffedf6e39 [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];
129 const unsigned *prop_chip;
130 const unsigned *prop_platform;
131 const unsigned *prop_subtype;
132 const unsigned *prop_hw_rev;
133 const unsigned *prop_hw_rev_end;
134 int namlen;
135 int len;
136 void *dtb;
137 char *dtbh;
138 DIR *dir;
139 unsigned c;
140
141 dir = opendir(dtb_path);
142
143 if (dir == NULL)
144 err(1, "failed to open '%s'", dtb_path);
145
146 while ((de = readdir(dir)) != NULL) {
147 namlen = strlen(de->d_name);
148 if (namlen < 4 || strcmp(&de->d_name[namlen - 4], ".dtb"))
149 continue;
150
151 snprintf(fname, sizeof(fname), "%s/%s", dtb_path, de->d_name);
152
153 dtb = load_file(fname, &dtb_sz);
154 if (dtb == NULL)
155 err(1, "failed to read dtb '%s'", fname);
156
157 if (fdt_check_header(dtb) != 0) {
158 warnx("'%s' is not a valid dtb, skipping", fname);
159 free(dtb);
160 continue;
161 }
162
163 offset = fdt_path_offset(dtb, "/");
164 prop_chip = fdt_getprop(dtb, offset, "model_info-chip", &len);
165 if (len % (sizeof(uint32_t)) != 0) {
166 warnx("model_info-chip of %s is of invalid size, skipping", fname);
167 free(dtb);
168 continue;
169 }
170
171 prop_platform = fdt_getprop(dtb, offset, "model_info-platform", &len);
172 if (strcmp((char *)&prop_platform[0], DTBH_PLATFORM)) {
173 warnx("model_info-platform of %s is invalid, skipping", fname);
174 free(dtb);
175 continue;
176 }
177
178 prop_subtype = fdt_getprop(dtb, offset, "model_info-subtype", &len);
179 if (strcmp((char *)&prop_subtype[0], DTBH_SUBTYPE)) {
180 warnx("model_info-subtype of %s is invalid, skipping", fname);
181 free(dtb);
182 continue;
183 }
184
185 prop_hw_rev = fdt_getprop(dtb, offset, "model_info-hw_rev", &len);
186 if (len % (sizeof(uint32_t)) != 0) {
187 warnx("model_info-hw_rev of %s is of invalid size, skipping", fname);
188 free(dtb);
189 continue;
190 }
191
192 prop_hw_rev_end = fdt_getprop(dtb, offset, "model_info-hw_rev_end", &len);
193 if (len % (sizeof(uint32_t)) != 0) {
194 warnx("model_info-hw_rev_end of %s is of invalid size, skipping", fname);
195 free(dtb);
196 continue;
197 }
198
199 blob = calloc(1, sizeof(struct dt_blob));
200 if (blob == NULL)
201 err(1, "failed to allocate memory");
202
203 blob->payload = dtb;
204 blob->size = dtb_sz;
205 if (blob_list == NULL) {
206 blob_list = blob;
207 last_blob = blob;
208 } else {
209 last_blob->next = blob;
210 last_blob = blob;
211 }
212
213 blob_sz += (blob->size + pagemask) & ~pagemask;
214 new_count = entry_count + 1;
215 new_entries = realloc(entries, new_count * sizeof(struct dt_entry));
216 if (new_entries == NULL)
217 err(1, "failed to allocate memory");
218
219 entries = new_entries;
220 entry = &entries[entry_count];
221 memset(entry, 0, sizeof(*entry));
222 entry->chip = ntohl(prop_chip[0]);
223 entry->platform = DTBH_PLATFORM_CODE;
224 entry->subtype = DTBH_SUBTYPE_CODE;
225 entry->hw_rev = ntohl(prop_hw_rev[0]);
226 entry->hw_rev_end = ntohl(prop_hw_rev_end[0]);
227 entry->space = 0x20; /* space delimiter */
228 entry->blob = blob;
229
230 entry_count++;
231
232 hdr_sz += entry_count * DT_ENTRY_PHYS_SIZE;
233 }
234
235 closedir(dir);
236
237 if (entry_count == 0) {
238 warnx("unable to locate any dtbs in the given path");
239 return NULL;
240 }
241
242 hdr_sz += sizeof(uint32_t); /* eot marker */
243 hdr_sz = (hdr_sz + pagemask) & ~pagemask;
244
245 qsort(entries, entry_count, sizeof(struct dt_entry), dt_entry_cmp);
246
247 /* The size of the dt header is now known, calculate the blob offsets... */
248 offset = hdr_sz;
249 for (blob = blob_list; blob; blob = blob->next) {
250 blob->offset = offset;
251 offset += (blob->size + pagemask) & ~pagemask;
252 }
253
254 /* ...and update the entries */
255 for (c = 0; c < entry_count; c++) {
256 entry = &entries[c];
257
258 entry->offset = entry->blob->offset;
259 entry->size = (entry->blob->size + pagemask) & ~pagemask;
260 }
261
262 /*
263 * All parts are now gathered, so build the dt block
264 */
265 dtbh = calloc(hdr_sz + blob_sz, 1);
266 if (dtbh == NULL)
267 err(1, "failed to allocate memory");
268
269 offset = 0;
270
271 memcpy(dtbh, DTBH_MAGIC, sizeof(uint32_t));
272 memcpy(dtbh + sizeof(uint32_t), &version, sizeof(uint32_t));
273 memcpy(dtbh + (sizeof(uint32_t) * 2), &entry_count, sizeof(uint32_t));
274
275 offset += DT_HEADER_PHYS_SIZE;
276
277 /* add dtbh entries */
278 for (c = 0; c < entry_count; c++) {
279 entry = &entries[c];
280 memcpy(dtbh + offset, entry, DT_ENTRY_PHYS_SIZE);
281 offset += DT_ENTRY_PHYS_SIZE;
282 }
283
284 /* add padding after dt header */
285 offset += pagesize - (offset & pagemask);
286
287 for (blob = blob_list; blob; blob = blob->next) {
288 memcpy(dtbh + offset, blob->payload, blob->size);
289 offset += (blob->size + pagemask) & ~pagemask;
290 }
291
292 *_sz = hdr_sz + blob_sz;
293
294 return dtbh;
295}