blob: 744222fe0c14a77f580d6400d5e3f432621dc2ac [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)) {
Christopher N. Hesse4ada34b2018-01-29 22:47:52 +0100173 warnx("model_info-platform of %s is invalid, skipping (expected %s but got %s)",
174 fname, DTBH_PLATFORM, (char *)&prop_platform[0]);
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100175 free(dtb);
176 continue;
177 }
178
179 prop_subtype = fdt_getprop(dtb, offset, "model_info-subtype", &len);
180 if (strcmp((char *)&prop_subtype[0], DTBH_SUBTYPE)) {
Christopher N. Hesse4ada34b2018-01-29 22:47:52 +0100181 warnx("model_info-subtype of %s is invalid, skipping (expected %s but got %s)",
182 fname, DTBH_SUBTYPE, (char *)&prop_subtype[0]);
Christopher N. Hesse7e73d2e2016-12-30 00:11:33 +0100183 free(dtb);
184 continue;
185 }
186
187 prop_hw_rev = fdt_getprop(dtb, offset, "model_info-hw_rev", &len);
188 if (len % (sizeof(uint32_t)) != 0) {
189 warnx("model_info-hw_rev of %s is of invalid size, skipping", fname);
190 free(dtb);
191 continue;
192 }
193
194 prop_hw_rev_end = fdt_getprop(dtb, offset, "model_info-hw_rev_end", &len);
195 if (len % (sizeof(uint32_t)) != 0) {
196 warnx("model_info-hw_rev_end of %s is of invalid size, skipping", fname);
197 free(dtb);
198 continue;
199 }
200
201 blob = calloc(1, sizeof(struct dt_blob));
202 if (blob == NULL)
203 err(1, "failed to allocate memory");
204
205 blob->payload = dtb;
206 blob->size = dtb_sz;
207 if (blob_list == NULL) {
208 blob_list = blob;
209 last_blob = blob;
210 } else {
211 last_blob->next = blob;
212 last_blob = blob;
213 }
214
215 blob_sz += (blob->size + pagemask) & ~pagemask;
216 new_count = entry_count + 1;
217 new_entries = realloc(entries, new_count * sizeof(struct dt_entry));
218 if (new_entries == NULL)
219 err(1, "failed to allocate memory");
220
221 entries = new_entries;
222 entry = &entries[entry_count];
223 memset(entry, 0, sizeof(*entry));
224 entry->chip = ntohl(prop_chip[0]);
225 entry->platform = DTBH_PLATFORM_CODE;
226 entry->subtype = DTBH_SUBTYPE_CODE;
227 entry->hw_rev = ntohl(prop_hw_rev[0]);
228 entry->hw_rev_end = ntohl(prop_hw_rev_end[0]);
229 entry->space = 0x20; /* space delimiter */
230 entry->blob = blob;
231
232 entry_count++;
233
234 hdr_sz += entry_count * DT_ENTRY_PHYS_SIZE;
235 }
236
237 closedir(dir);
238
239 if (entry_count == 0) {
240 warnx("unable to locate any dtbs in the given path");
241 return NULL;
242 }
243
244 hdr_sz += sizeof(uint32_t); /* eot marker */
245 hdr_sz = (hdr_sz + pagemask) & ~pagemask;
246
247 qsort(entries, entry_count, sizeof(struct dt_entry), dt_entry_cmp);
248
249 /* The size of the dt header is now known, calculate the blob offsets... */
250 offset = hdr_sz;
251 for (blob = blob_list; blob; blob = blob->next) {
252 blob->offset = offset;
253 offset += (blob->size + pagemask) & ~pagemask;
254 }
255
256 /* ...and update the entries */
257 for (c = 0; c < entry_count; c++) {
258 entry = &entries[c];
259
260 entry->offset = entry->blob->offset;
261 entry->size = (entry->blob->size + pagemask) & ~pagemask;
262 }
263
264 /*
265 * All parts are now gathered, so build the dt block
266 */
267 dtbh = calloc(hdr_sz + blob_sz, 1);
268 if (dtbh == NULL)
269 err(1, "failed to allocate memory");
270
271 offset = 0;
272
273 memcpy(dtbh, DTBH_MAGIC, sizeof(uint32_t));
274 memcpy(dtbh + sizeof(uint32_t), &version, sizeof(uint32_t));
275 memcpy(dtbh + (sizeof(uint32_t) * 2), &entry_count, sizeof(uint32_t));
276
277 offset += DT_HEADER_PHYS_SIZE;
278
279 /* add dtbh entries */
280 for (c = 0; c < entry_count; c++) {
281 entry = &entries[c];
282 memcpy(dtbh + offset, entry, DT_ENTRY_PHYS_SIZE);
283 offset += DT_ENTRY_PHYS_SIZE;
284 }
285
286 /* add padding after dt header */
287 offset += pagesize - (offset & pagemask);
288
289 for (blob = blob_list; blob; blob = blob->next) {
290 memcpy(dtbh + offset, blob->payload, blob->size);
291 offset += (blob->size + pagemask) & ~pagemask;
292 }
293
294 *_sz = hdr_sz + blob_sz;
295
296 return dtbh;
297}