blob: ffc59fd8b36a0940fbbf67d1a7e6ee848378f884 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04002/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 *
Shengzhou Liua7be8022011-10-14 16:26:05 +08006 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -04007 */
8
9#include <common.h>
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +020010#include <abuf.h>
Simon Glass0af6e2d2019-08-01 09:46:52 -060011#include <env.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Heinrich Schuchardt668cabc2018-11-18 17:58:51 +010013#include <mapmem.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <net.h>
Anton Vorontsov9e9cf602009-10-15 17:47:04 +040015#include <stdio_dev.h>
Patrick Delaunaye0682262023-06-08 17:16:37 +020016#include <dm/ofnode.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040017#include <linux/ctype.h>
18#include <linux/types.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040019#include <asm/global_data.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090020#include <linux/libfdt.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040021#include <fdt_support.h>
Kumar Gala2dc9b4a2007-11-26 17:06:15 -060022#include <exports.h>
Bin Mengd9a00932015-12-07 01:39:47 -080023#include <fdtdec.h>
Francesco Dolcini05cc8272022-05-19 16:22:26 +020024#include <version.h>
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040025
Kumar Gala1ba00402008-10-23 00:05:47 -050026/**
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020027 * fdt_getprop_u32_default_node - Return a node's property or a default
28 *
29 * @fdt: ptr to device tree
30 * @off: offset of node
31 * @cell: cell offset in property
32 * @prop: property name
33 * @dflt: default value if the property isn't found
34 *
35 * Convenience function to return a node's property or a default value if
36 * the property doesn't exist.
37 */
38u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
39 const char *prop, const u32 dflt)
40{
41 const fdt32_t *val;
42 int len;
43
44 val = fdt_getprop(fdt, off, prop, &len);
45
46 /* Check if property exists */
47 if (!val)
48 return dflt;
49
50 /* Check if property is long enough */
51 if (len < ((cell + 1) * sizeof(uint32_t)))
52 return dflt;
53
54 return fdt32_to_cpu(*val);
55}
56
57/**
Kumar Gala1ba00402008-10-23 00:05:47 -050058 * fdt_getprop_u32_default - Find a node and return it's property or a default
59 *
60 * @fdt: ptr to device tree
61 * @path: path of node
62 * @prop: property name
63 * @dflt: default value if the property isn't found
64 *
65 * Convenience function to find a node and return it's property or a
66 * default value if it doesn't exist.
67 */
Gabe Blackcd37de72011-11-08 01:09:44 -080068u32 fdt_getprop_u32_default(const void *fdt, const char *path,
69 const char *prop, const u32 dflt)
Kumar Gala1ba00402008-10-23 00:05:47 -050070{
Kumar Gala1ba00402008-10-23 00:05:47 -050071 int off;
72
73 off = fdt_path_offset(fdt, path);
74 if (off < 0)
75 return dflt;
76
Alexander Graf7c8a6cf2014-04-11 17:09:40 +020077 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala1ba00402008-10-23 00:05:47 -050078}
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -040079
Kumar Galabf771dc2007-10-24 10:21:57 -050080/**
81 * fdt_find_and_setprop: Find a node and set it's property
82 *
83 * @fdt: ptr to device tree
84 * @node: path of node
85 * @prop: property name
86 * @val: ptr to new value
87 * @len: length of new property value
88 * @create: flag to create the property if it doesn't exist
89 *
90 * Convenience function to directly set a property given the path to the node.
91 */
92int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
93 const void *val, int len, int create)
94{
Kumar Galac8ab7052007-10-24 11:04:22 -050095 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galabf771dc2007-10-24 10:21:57 -050096
97 if (nodeoff < 0)
98 return nodeoff;
99
Kim Phillips6542c072013-01-16 14:00:11 +0000100 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galabf771dc2007-10-24 10:21:57 -0500101 return 0; /* create flag not set; so exit quietly */
102
103 return fdt_setprop(fdt, nodeoff, prop, val, len);
104}
105
Masahiro Yamadac0207282014-04-18 17:40:58 +0900106/**
Simon Glass014aa052014-10-23 18:58:49 -0600107 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
108 *
Masahiro Yamadac0207282014-04-18 17:40:58 +0900109 * @fdt: pointer to the device tree blob
110 * @parentoffset: structure block offset of a node
111 * @name: name of the subnode to locate
112 *
113 * fdt_subnode_offset() finds a subnode of the node with a given name.
114 * If the subnode does not exist, it will be created.
115 */
Simon Glass014aa052014-10-23 18:58:49 -0600116int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamadac0207282014-04-18 17:40:58 +0900117{
118 int offset;
119
120 offset = fdt_subnode_offset(fdt, parentoffset, name);
121
122 if (offset == -FDT_ERR_NOTFOUND)
123 offset = fdt_add_subnode(fdt, parentoffset, name);
124
125 if (offset < 0)
126 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
127
128 return offset;
129}
130
Andre Przywara0d1fcf82021-02-14 10:35:18 +0000131#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel46e192a2008-06-20 22:24:05 +0200132static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600133{
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900134 int err;
135 int aliasoff;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600136 char sername[9] = { 0 };
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900137 const void *path;
138 int len;
139 char tmp[256]; /* long enough */
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600140
Bin Mengb8199112016-01-13 19:38:58 -0800141 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600142
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900143 aliasoff = fdt_path_offset(fdt, "/aliases");
144 if (aliasoff < 0) {
145 err = aliasoff;
Scott Woodedd551f2015-09-01 22:48:08 -0500146 goto noalias;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600147 }
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900148
149 path = fdt_getprop(fdt, aliasoff, sername, &len);
150 if (!path) {
151 err = len;
Scott Woodedd551f2015-09-01 22:48:08 -0500152 goto noalias;
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900153 }
154
155 /* fdt_setprop may break "path" so we copy it to tmp buffer */
156 memcpy(tmp, path, len);
157
158 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600159 if (err < 0)
160 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900161 fdt_strerror(err));
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600162
163 return err;
Scott Woodedd551f2015-09-01 22:48:08 -0500164
165noalias:
166 printf("WARNING: %s: could not read %s alias: %s\n",
167 __func__, sername, fdt_strerror(err));
168
169 return 0;
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600170}
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900171#else
172static int fdt_fixup_stdout(void *fdt, int chosenoff)
173{
174 return 0;
175}
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600176#endif
177
Masahiro Yamadab891a342014-04-18 17:41:04 +0900178static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
179 uint64_t val, int is_u64)
180{
181 if (is_u64)
182 return fdt_setprop_u64(fdt, nodeoffset, name, val);
183 else
184 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
185}
186
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200187int fdt_root(void *fdt)
188{
189 char *serial;
190 int err;
191
192 err = fdt_check_header(fdt);
193 if (err < 0) {
194 printf("fdt_root: %s\n", fdt_strerror(err));
195 return err;
196 }
197
Simon Glass64b723f2017-08-03 12:22:12 -0600198 serial = env_get("serial#");
Paul Kocialkowski2c0fbc72015-05-21 11:27:03 +0200199 if (serial) {
200 err = fdt_setprop(fdt, 0, "serial-number", serial,
201 strlen(serial) + 1);
202
203 if (err < 0) {
204 printf("WARNING: could not set serial-number %s.\n",
205 fdt_strerror(err));
206 return err;
207 }
208 }
209
210 return 0;
211}
Masahiro Yamadab891a342014-04-18 17:41:04 +0900212
Masahiro Yamada5b114892014-04-18 17:40:59 +0900213int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400214{
Masahiro Yamadab891a342014-04-18 17:41:04 +0900215 int nodeoffset;
Kumar Gala99ebc052008-08-15 08:24:43 -0500216 int err, j, total;
Masahiro Yamadab891a342014-04-18 17:41:04 +0900217 int is_u64;
Kumar Gala99ebc052008-08-15 08:24:43 -0500218 uint64_t addr, size;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400219
Masahiro Yamada9a3b4de2014-04-18 17:41:05 +0900220 /* just return if the size of initrd is zero */
221 if (initrd_start == initrd_end)
222 return 0;
223
Masahiro Yamadac0207282014-04-18 17:40:58 +0900224 /* find or create "/chosen" node. */
225 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
226 if (nodeoffset < 0)
Kumar Gala99ebc052008-08-15 08:24:43 -0500227 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400228
Kumar Gala99ebc052008-08-15 08:24:43 -0500229 total = fdt_num_mem_rsv(fdt);
230
231 /*
232 * Look for an existing entry and update it. If we don't find
233 * the entry, we will j be the next available slot.
234 */
235 for (j = 0; j < total; j++) {
236 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
237 if (addr == initrd_start) {
238 fdt_del_mem_rsv(fdt, j);
239 break;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400240 }
Kumar Gala99ebc052008-08-15 08:24:43 -0500241 }
242
Grant Likelyc379a562011-03-28 09:58:55 +0000243 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala99ebc052008-08-15 08:24:43 -0500244 if (err < 0) {
245 printf("fdt_initrd: %s\n", fdt_strerror(err));
246 return err;
247 }
Kumar Galac8ab7052007-10-24 11:04:22 -0500248
Simon Glass9fbc6322014-10-23 18:58:57 -0600249 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengd9850132013-12-14 11:47:29 +0800250
Masahiro Yamadab891a342014-04-18 17:41:04 +0900251 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
252 (uint64_t)initrd_start, is_u64);
253
Masahiro Yamada5b114892014-04-18 17:40:59 +0900254 if (err < 0) {
255 printf("WARNING: could not set linux,initrd-start %s.\n",
256 fdt_strerror(err));
257 return err;
258 }
Masahiro Yamadab891a342014-04-18 17:41:04 +0900259
260 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
261 (uint64_t)initrd_end, is_u64);
262
Masahiro Yamada5b114892014-04-18 17:40:59 +0900263 if (err < 0) {
264 printf("WARNING: could not set linux,initrd-end %s.\n",
265 fdt_strerror(err));
Kumar Gala99ebc052008-08-15 08:24:43 -0500266
Masahiro Yamada5b114892014-04-18 17:40:59 +0900267 return err;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400268 }
269
Kumar Gala99ebc052008-08-15 08:24:43 -0500270 return 0;
271}
272
Niko Mauno673db432021-02-22 19:18:51 +0000273/**
274 * board_fdt_chosen_bootargs - boards may override this function to use
275 * alternative kernel command line arguments
276 */
277__weak char *board_fdt_chosen_bootargs(void)
278{
279 return env_get("bootargs");
280}
281
Masahiro Yamada451c2042014-04-18 17:41:00 +0900282int fdt_chosen(void *fdt)
Kumar Gala99ebc052008-08-15 08:24:43 -0500283{
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200284 struct abuf buf = {};
Kumar Gala99ebc052008-08-15 08:24:43 -0500285 int nodeoffset;
286 int err;
287 char *str; /* used to set string properties */
Kumar Gala99ebc052008-08-15 08:24:43 -0500288
289 err = fdt_check_header(fdt);
290 if (err < 0) {
291 printf("fdt_chosen: %s\n", fdt_strerror(err));
292 return err;
293 }
294
Masahiro Yamadac0207282014-04-18 17:40:58 +0900295 /* find or create "/chosen" node. */
296 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
297 if (nodeoffset < 0)
298 return nodeoffset;
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400299
Rasmus Villemoesbd2e3532022-08-22 09:34:23 +0200300 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
301 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
302 abuf_data(&buf), abuf_size(&buf));
303 abuf_uninit(&buf);
304 if (err < 0) {
305 printf("WARNING: could not set rng-seed %s.\n",
306 fdt_strerror(err));
307 return err;
308 }
309 }
310
Niko Mauno673db432021-02-22 19:18:51 +0000311 str = board_fdt_chosen_bootargs();
312
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900313 if (str) {
314 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
315 strlen(str) + 1);
316 if (err < 0) {
Masahiro Yamada451c2042014-04-18 17:41:00 +0900317 printf("WARNING: could not set bootargs %s.\n",
318 fdt_strerror(err));
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900319 return err;
320 }
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400321 }
Kumar Gala2dc9b4a2007-11-26 17:06:15 -0600322
Francesco Dolcini05cc8272022-05-19 16:22:26 +0200323 /* add u-boot version */
324 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
325 strlen(PLAIN_VERSION) + 1);
326 if (err < 0) {
327 printf("WARNING: could not set u-boot,version %s.\n",
328 fdt_strerror(err));
329 return err;
330 }
331
Masahiro Yamadaa467792c62014-04-18 17:41:01 +0900332 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400333}
334
Kumar Gala7e64cf82007-11-03 19:46:28 -0500335void do_fixup_by_path(void *fdt, const char *path, const char *prop,
336 const void *val, int len, int create)
337{
338#if defined(DEBUG)
339 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600340 debug("Updating property '%s/%s' = ", path, prop);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500341 for (i = 0; i < len; i++)
342 debug(" %.2x", *(u8*)(val+i));
343 debug("\n");
344#endif
345 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
346 if (rc)
347 printf("Unable to update property %s:%s, err=%s\n",
348 path, prop, fdt_strerror(rc));
349}
350
351void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
352 u32 val, int create)
353{
Kim Phillips6542c072013-01-16 14:00:11 +0000354 fdt32_t tmp = cpu_to_fdt32(val);
355 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Gala7e64cf82007-11-03 19:46:28 -0500356}
357
Kumar Gala7590a192007-11-21 13:30:15 -0600358void do_fixup_by_prop(void *fdt,
359 const char *pname, const void *pval, int plen,
360 const char *prop, const void *val, int len,
361 int create)
362{
363 int off;
364#if defined(DEBUG)
365 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600366 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600367 for (i = 0; i < len; i++)
368 debug(" %.2x", *(u8*)(val+i));
369 debug("\n");
370#endif
371 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
372 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips6542c072013-01-16 14:00:11 +0000373 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600374 fdt_setprop(fdt, off, prop, val, len);
375 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
376 }
377}
378
379void do_fixup_by_prop_u32(void *fdt,
380 const char *pname, const void *pval, int plen,
381 const char *prop, u32 val, int create)
382{
Kim Phillips6542c072013-01-16 14:00:11 +0000383 fdt32_t tmp = cpu_to_fdt32(val);
384 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600385}
386
387void do_fixup_by_compat(void *fdt, const char *compat,
388 const char *prop, const void *val, int len, int create)
389{
390 int off = -1;
391#if defined(DEBUG)
392 int i;
Kumar Gala88f0ca22008-02-13 15:09:58 -0600393 debug("Updating property '%s' = ", prop);
Kumar Gala7590a192007-11-21 13:30:15 -0600394 for (i = 0; i < len; i++)
395 debug(" %.2x", *(u8*)(val+i));
396 debug("\n");
397#endif
Marek Behún5d6b4482022-01-20 01:04:42 +0100398 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips6542c072013-01-16 14:00:11 +0000399 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala7590a192007-11-21 13:30:15 -0600400 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala7590a192007-11-21 13:30:15 -0600401}
402
403void do_fixup_by_compat_u32(void *fdt, const char *compat,
404 const char *prop, u32 val, int create)
405{
Kim Phillips6542c072013-01-16 14:00:11 +0000406 fdt32_t tmp = cpu_to_fdt32(val);
407 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala7590a192007-11-21 13:30:15 -0600408}
409
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900410#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900411/*
412 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
413 */
Simon Glass0058e112014-10-23 18:58:55 -0600414static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
415 int n)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900416{
417 int i;
Hans de Goede4c80fb02014-11-28 14:23:51 +0100418 int address_cells = fdt_address_cells(fdt, 0);
419 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900420 char *p = buf;
421
422 for (i = 0; i < n; i++) {
Hans de Goede4c80fb02014-11-28 14:23:51 +0100423 if (address_cells == 2)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900424 *(fdt64_t *)p = cpu_to_fdt64(address[i]);
425 else
426 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100427 p += 4 * address_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900428
Hans de Goede4c80fb02014-11-28 14:23:51 +0100429 if (size_cells == 2)
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900430 *(fdt64_t *)p = cpu_to_fdt64(size[i]);
431 else
432 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goede4c80fb02014-11-28 14:23:51 +0100433 p += 4 * size_cells;
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900434 }
435
436 return p - (char *)buf;
437}
438
Ramon Fried9cab1832018-08-14 00:35:42 +0300439#if CONFIG_NR_DRAM_BANKS > 4
440#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
441#else
Kim Phillips6542c072013-01-16 14:00:11 +0000442#define MEMORY_BANKS_MAX 4
Ramon Fried9cab1832018-08-14 00:35:42 +0300443#endif
Michal Simek96283732021-08-10 09:21:54 +0200444
445/**
446 * fdt_fixup_memory_banks - Update DT memory node
447 * @blob: Pointer to DT blob
448 * @start: Pointer to memory start addresses array
449 * @size: Pointer to memory sizes array
450 * @banks: Number of memory banks
451 *
452 * Return: 0 on success, negative value on failure
453 *
454 * Based on the passed number of banks and arrays, the function is able to
455 * update existing DT memory nodes to match run time detected/changed memory
456 * configuration. Implementation is handling one specific case with only one
457 * memory node where multiple tuples could be added/updated.
458 * The case where multiple memory nodes with a single tuple (base, size) are
459 * used, this function is only updating the first memory node without removing
460 * others.
461 */
John Rigbyb4ae9422010-10-13 13:57:33 -0600462int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
463{
464 int err, nodeoffset;
Thierry Reding74af8a32018-01-30 11:34:17 +0100465 int len, i;
Kim Phillips6542c072013-01-16 14:00:11 +0000466 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala0db72ed2007-11-26 14:57:45 -0600467
Kim Phillips6542c072013-01-16 14:00:11 +0000468 if (banks > MEMORY_BANKS_MAX) {
469 printf("%s: num banks %d exceeds hardcoded limit %d."
470 " Recompile with higher MEMORY_BANKS_MAX?\n",
471 __FUNCTION__, banks, MEMORY_BANKS_MAX);
472 return -1;
473 }
474
Kumar Gala0db72ed2007-11-26 14:57:45 -0600475 err = fdt_check_header(blob);
476 if (err < 0) {
477 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
478 return err;
479 }
480
Masahiro Yamadac0207282014-04-18 17:40:58 +0900481 /* find or create "/memory" node. */
482 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
483 if (nodeoffset < 0)
Miao Yan0904eb22013-11-28 17:51:39 +0800484 return nodeoffset;
Masahiro Yamadac0207282014-04-18 17:40:58 +0900485
Kumar Gala0db72ed2007-11-26 14:57:45 -0600486 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
487 sizeof("memory"));
488 if (err < 0) {
489 printf("WARNING: could not set %s %s.\n", "device_type",
490 fdt_strerror(err));
491 return err;
492 }
493
Thierry Reding9eb0e7e2018-02-15 19:05:59 +0100494 for (i = 0; i < banks; i++) {
495 if (start[i] == 0 && size[i] == 0)
496 break;
497 }
498
499 banks = i;
500
Andre Przywara3d2d4412015-06-21 00:29:54 +0100501 if (!banks)
502 return 0;
503
Masahiro Yamadaa6ae8d32014-04-18 17:41:03 +0900504 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala0db72ed2007-11-26 14:57:45 -0600505
506 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
507 if (err < 0) {
508 printf("WARNING: could not set %s %s.\n",
509 "reg", fdt_strerror(err));
510 return err;
511 }
512 return 0;
513}
Igor Opaniuk80b95782019-12-03 14:04:46 +0200514
515int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
516{
517 int err, nodeoffset;
518 int len;
519 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
520
521 if (areas > 8) {
522 printf("%s: num areas %d exceeds hardcoded limit %d\n",
523 __func__, areas, 8);
524 return -1;
525 }
526
527 err = fdt_check_header(blob);
528 if (err < 0) {
529 printf("%s: %s\n", __func__, fdt_strerror(err));
530 return err;
531 }
532
533 /* find or create "/memory" node. */
534 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
535 if (nodeoffset < 0)
536 return nodeoffset;
537
538 len = fdt_pack_reg(blob, tmp, start, size, areas);
539
540 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
541 if (err < 0) {
542 printf("WARNING: could not set %s %s.\n",
543 "reg", fdt_strerror(err));
544 return err;
545 }
546
547 return 0;
548}
Masahiro Yamadaf6aa39e2016-11-26 11:02:10 +0900549#endif
Kumar Gala0db72ed2007-11-26 14:57:45 -0600550
John Rigbyb4ae9422010-10-13 13:57:33 -0600551int fdt_fixup_memory(void *blob, u64 start, u64 size)
552{
553 return fdt_fixup_memory_banks(blob, &start, &size, 1);
554}
555
Kumar Galafabda922008-08-19 15:41:18 -0500556void fdt_fixup_ethernet(void *fdt)
Kumar Gala22699102007-11-21 11:11:03 -0600557{
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530558 int i = 0, j, prop;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800559 char *tmp, *end;
Stephen Warren7c15c772013-05-27 18:01:19 +0000560 char mac[16];
Kumar Gala22699102007-11-21 11:11:03 -0600561 const char *path;
oliver@schinagl.nl496904b2016-11-25 16:30:19 +0100562 unsigned char mac_addr[ARP_HLEN];
Bin Mengbfb93cc2015-11-03 04:24:41 -0800563 int offset;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530564#ifdef FDT_SEQ_MACADDR_FROM_ENV
565 int nodeoff;
566 const struct fdt_property *fdt_prop;
567#endif
Kumar Gala22699102007-11-21 11:11:03 -0600568
Lev Iserovichcde77612016-01-07 18:04:16 -0500569 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galafabda922008-08-19 15:41:18 -0500570 return;
571
Lev Iserovichcde77612016-01-07 18:04:16 -0500572 /* Cycle through all aliases */
573 for (prop = 0; ; prop++) {
Bin Mengbfb93cc2015-11-03 04:24:41 -0800574 const char *name;
Dan Murphy0b9bf272013-10-02 14:00:15 -0500575
Lev Iserovichcde77612016-01-07 18:04:16 -0500576 /* FDT might have been edited, recompute the offset */
577 offset = fdt_first_property_offset(fdt,
578 fdt_path_offset(fdt, "/aliases"));
579 /* Select property number 'prop' */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530580 for (j = 0; j < prop; j++)
Lev Iserovichcde77612016-01-07 18:04:16 -0500581 offset = fdt_next_property_offset(fdt, offset);
582
583 if (offset < 0)
584 break;
585
Bin Mengbfb93cc2015-11-03 04:24:41 -0800586 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200587 if (!strncmp(name, "ethernet", 8)) {
588 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530589 if (!strcmp(name, "ethernet")
590#ifdef FDT_SEQ_MACADDR_FROM_ENV
591 || !strcmp(name, "ethernet0")
592#endif
593 )
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200594 i = 0;
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530595#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynena7042f52017-03-20 10:04:55 +0200596 else
597 i = trailing_strtol(name);
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530598#endif
Bin Mengbfb93cc2015-11-03 04:24:41 -0800599 if (i != -1) {
600 if (i == 0)
601 strcpy(mac, "ethaddr");
602 else
603 sprintf(mac, "eth%daddr", i);
604 } else {
605 continue;
606 }
Prabhakar Kushwaha2dec06f2017-11-23 16:51:32 +0530607#ifdef FDT_SEQ_MACADDR_FROM_ENV
608 nodeoff = fdt_path_offset(fdt, path);
609 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
610 NULL);
611 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
612 continue;
613 i++;
614#endif
Simon Glass64b723f2017-08-03 12:22:12 -0600615 tmp = env_get(mac);
Bin Mengbfb93cc2015-11-03 04:24:41 -0800616 if (!tmp)
617 continue;
Kumar Galafabda922008-08-19 15:41:18 -0500618
Bin Mengbfb93cc2015-11-03 04:24:41 -0800619 for (j = 0; j < 6; j++) {
620 mac_addr[j] = tmp ?
Simon Glass3ff49ec2021-07-24 09:03:29 -0600621 hextoul(tmp, &end) : 0;
Bin Mengbfb93cc2015-11-03 04:24:41 -0800622 if (tmp)
623 tmp = (*end) ? end + 1 : end;
624 }
Kumar Galafabda922008-08-19 15:41:18 -0500625
Bin Mengbfb93cc2015-11-03 04:24:41 -0800626 do_fixup_by_path(fdt, path, "mac-address",
627 &mac_addr, 6, 0);
628 do_fixup_by_path(fdt, path, "local-mac-address",
629 &mac_addr, 6, 1);
630 }
Kumar Gala22699102007-11-21 11:11:03 -0600631 }
632}
Anton Vorontsov07e60912008-03-14 23:20:18 +0300633
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100634int fdt_record_loadable(void *blob, u32 index, const char *name,
635 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simek0e4f43d2021-05-27 11:40:09 +0200636 const char *type, const char *os, const char *arch)
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100637{
638 int err, node;
639
640 err = fdt_check_header(blob);
641 if (err < 0) {
642 printf("%s: %s\n", __func__, fdt_strerror(err));
643 return err;
644 }
645
646 /* find or create "/fit-images" node */
647 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
648 if (node < 0)
649 return node;
650
651 /* find or create "/fit-images/<name>" node */
652 node = fdt_find_or_add_subnode(blob, node, name);
653 if (node < 0)
654 return node;
655
Michal Simekd31b40f2020-09-03 12:44:51 +0200656 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100657 if (entry_point != -1)
Michal Simekd31b40f2020-09-03 12:44:51 +0200658 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100659 fdt_setprop_u32(blob, node, "size", size);
660 if (type)
661 fdt_setprop_string(blob, node, "type", type);
662 if (os)
663 fdt_setprop_string(blob, node, "os", os);
Michal Simek0e4f43d2021-05-27 11:40:09 +0200664 if (arch)
665 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsich1b1b4e42018-02-02 12:01:44 +0100666
667 return node;
668}
669
Kumar Galaf2f58c52008-08-15 08:24:42 -0500670/* Resize the fdt to its actual size + a bit of padding */
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200671int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Galaf2f58c52008-08-15 08:24:42 -0500672{
673 int i;
674 uint64_t addr, size;
675 int total, ret;
676 uint actualsize;
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200677 int fdt_memrsv = 0;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500678
679 if (!blob)
680 return 0;
681
682 total = fdt_num_mem_rsv(blob);
683 for (i = 0; i < total; i++) {
684 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glassfc953242011-09-17 06:48:58 +0000685 if (addr == (uintptr_t)blob) {
Kumar Galaf2f58c52008-08-15 08:24:42 -0500686 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200687 fdt_memrsv = 1;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500688 break;
689 }
690 }
691
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100692 /*
693 * Calculate the actual size of the fdt
Feng Wangba31fb62010-08-03 16:22:43 +0200694 * plus the size needed for 5 fdt_add_mem_rsv, one
695 * for the fdt itself and 4 for a possible initrd
696 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaard9c7b7052008-10-28 08:26:52 +0100697 */
Kumar Galaf2f58c52008-08-15 08:24:42 -0500698 actualsize = fdt_off_dt_strings(blob) +
Feng Wangba31fb62010-08-03 16:22:43 +0200699 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500700
Hannes Schmelzerd3dbac82016-09-20 18:10:43 +0200701 actualsize += extrasize;
Kumar Galaf2f58c52008-08-15 08:24:42 -0500702 /* Make it so the fdt ends on a page boundary */
Simon Glassfc953242011-09-17 06:48:58 +0000703 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
704 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Galaf2f58c52008-08-15 08:24:42 -0500705
706 /* Change the fdt header to reflect the correct size */
707 fdt_set_totalsize(blob, actualsize);
708
Simon Goldschmidt5d5a0c02019-05-03 21:19:03 +0200709 if (fdt_memrsv) {
710 /* Add the new reservation */
711 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
712 if (ret < 0)
713 return ret;
714 }
Kumar Galaf2f58c52008-08-15 08:24:42 -0500715
716 return actualsize;
717}
Kumar Galae0475cd2008-10-22 23:33:56 -0500718
Marek Behúnac4d9ff2021-11-26 14:57:15 +0100719/**
720 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
721 *
722 * @blob: ptr to device tree
723 */
724int fdt_delete_disabled_nodes(void *blob)
725{
726 while (1) {
727 int ret, offset;
728
729 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
730 "disabled", 9);
731 if (offset < 0)
732 break;
733
734 ret = fdt_del_node(blob, offset);
735 if (ret < 0)
736 return ret;
737 }
738
739 return 0;
740}
741
Kumar Galae0475cd2008-10-22 23:33:56 -0500742#ifdef CONFIG_PCI
Tom Rini56af6592022-11-16 13:10:33 -0500743#define CFG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Galae0475cd2008-10-22 23:33:56 -0500744
745#define FDT_PCI_PREFETCH (0x40000000)
746#define FDT_PCI_MEM32 (0x02000000)
747#define FDT_PCI_IO (0x01000000)
748#define FDT_PCI_MEM64 (0x03000000)
749
750int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
751
752 int addrcell, sizecell, len, r;
753 u32 *dma_range;
754 /* sized based on pci addr cells, size-cells, & address-cells */
Tom Rini56af6592022-11-16 13:10:33 -0500755 u32 dma_ranges[(3 + 2 + 2) * CFG_SYS_PCI_NR_INBOUND_WIN];
Kumar Galae0475cd2008-10-22 23:33:56 -0500756
757 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
758 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
759
760 dma_range = &dma_ranges[0];
761 for (r = 0; r < hose->region_count; r++) {
762 u64 bus_start, phys_start, size;
763
Kumar Galaefa1f1d2009-02-06 09:49:31 -0600764 /* skip if !PCI_REGION_SYS_MEMORY */
765 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Galae0475cd2008-10-22 23:33:56 -0500766 continue;
767
768 bus_start = (u64)hose->regions[r].bus_start;
769 phys_start = (u64)hose->regions[r].phys_start;
770 size = (u64)hose->regions[r].size;
771
772 dma_range[0] = 0;
Kumar Galabaf41892009-08-05 09:03:54 -0500773 if (size >= 0x100000000ull)
Marek Vasut22203f52019-07-09 02:49:29 +0200774 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Galae0475cd2008-10-22 23:33:56 -0500775 else
Marek Vasut22203f52019-07-09 02:49:29 +0200776 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500777 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut22203f52019-07-09 02:49:29 +0200778 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Galae0475cd2008-10-22 23:33:56 -0500779#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut22203f52019-07-09 02:49:29 +0200780 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Galae0475cd2008-10-22 23:33:56 -0500781#else
782 dma_range[1] = 0;
783#endif
Marek Vasut22203f52019-07-09 02:49:29 +0200784 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500785
786 if (addrcell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200787 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
788 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500789 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200790 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500791 }
792
793 if (sizecell == 2) {
Marek Vasut22203f52019-07-09 02:49:29 +0200794 dma_range[3 + addrcell + 0] =
795 cpu_to_fdt32(size >> 32);
796 dma_range[3 + addrcell + 1] =
797 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500798 } else {
Marek Vasut22203f52019-07-09 02:49:29 +0200799 dma_range[3 + addrcell + 0] =
800 cpu_to_fdt32(size & 0xffffffff);
Kumar Galae0475cd2008-10-22 23:33:56 -0500801 }
802
803 dma_range += (3 + addrcell + sizecell);
804 }
805
806 len = dma_range - &dma_ranges[0];
807 if (len)
808 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
809
810 return 0;
811}
812#endif
Stefan Roesea0f10c92009-10-21 11:59:52 +0200813
Matthew McClintockacda3a42010-10-13 13:39:26 +0200814int fdt_increase_size(void *fdt, int add_len)
815{
816 int newlen;
817
818 newlen = fdt_totalsize(fdt) + add_len;
819
820 /* Open in place with a new len */
821 return fdt_open_into(fdt, fdt, newlen);
822}
823
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100824#ifdef CONFIG_FDT_FIXUP_PARTITIONS
825#include <jffs2/load_kernel.h>
826#include <mtd_node.h>
827
Michal Simekc2ed0072018-07-31 14:31:04 +0200828static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100829{
830 int off, ndepth;
831 int ret;
832
833 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
834 (off >= 0) && (ndepth > 0);
835 off = fdt_next_node(blob, off, &ndepth)) {
836 if (ndepth == 1) {
837 debug("delete %s: offset: %x\n",
838 fdt_get_name(blob, off, 0), off);
839 ret = fdt_del_node((void *)blob, off);
840 if (ret < 0) {
841 printf("Can't delete node: %s\n",
842 fdt_strerror(ret));
843 return ret;
844 } else {
845 ndepth = 0;
846 off = parent_offset;
847 }
848 }
849 }
850 return 0;
851}
852
Michal Simekc2ed0072018-07-31 14:31:04 +0200853static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100854{
855 const void *prop;
856 int ndepth = 0;
857 int off;
858 int ret;
859
860 off = fdt_next_node(blob, parent_offset, &ndepth);
861 if (off > 0 && ndepth == 1) {
862 prop = fdt_getprop(blob, off, "label", NULL);
863 if (prop == NULL) {
864 /*
865 * Could not find label property, nand {}; node?
866 * Check subnode, delete partitions there if any.
867 */
868 return fdt_del_partitions(blob, off);
869 } else {
870 ret = fdt_del_subnodes(blob, parent_offset);
871 if (ret < 0) {
872 printf("Can't remove subnodes: %s\n",
873 fdt_strerror(ret));
874 return ret;
875 }
876 }
877 }
878 return 0;
879}
880
Masahiro Yamada65669602020-07-15 19:35:47 +0900881static int fdt_node_set_part_info(void *blob, int parent_offset,
882 struct mtd_device *dev)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100883{
884 struct list_head *pentry;
885 struct part_info *part;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100886 int off, ndepth = 0;
887 int part_num, ret;
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300888 int sizecell;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100889 char buf[64];
890
891 ret = fdt_del_partitions(blob, parent_offset);
892 if (ret < 0)
893 return ret;
894
895 /*
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300896 * Check if size/address is 1 or 2 cells.
897 * We assume #address-cells and #size-cells have same value.
898 */
899 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
900 0, "#size-cells", 1);
901
902 /*
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100903 * Check if it is nand {}; subnode, adjust
904 * the offset in this case
905 */
906 off = fdt_next_node(blob, parent_offset, &ndepth);
907 if (off > 0 && ndepth == 1)
908 parent_offset = off;
909
910 part_num = 0;
911 list_for_each_prev(pentry, &dev->parts) {
912 int newoff;
913
914 part = list_entry(pentry, struct part_info, link);
915
Scott Woodefe7f302013-10-15 17:41:27 -0500916 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100917 part_num, part->name, part->size,
918 part->offset, part->mask_flags);
919
Scott Woodefe7f302013-10-15 17:41:27 -0500920 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100921add_sub:
922 ret = fdt_add_subnode(blob, parent_offset, buf);
923 if (ret == -FDT_ERR_NOSPACE) {
924 ret = fdt_increase_size(blob, 512);
925 if (!ret)
926 goto add_sub;
927 else
928 goto err_size;
929 } else if (ret < 0) {
930 printf("Can't add partition node: %s\n",
931 fdt_strerror(ret));
932 return ret;
933 }
934 newoff = ret;
935
936 /* Check MTD_WRITEABLE_CMD flag */
937 if (part->mask_flags & 1) {
938add_ro:
939 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
940 if (ret == -FDT_ERR_NOSPACE) {
941 ret = fdt_increase_size(blob, 512);
942 if (!ret)
943 goto add_ro;
944 else
945 goto err_size;
946 } else if (ret < 0)
947 goto err_prop;
948 }
949
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100950add_reg:
Stefan Mavrodiev51b0c032019-04-24 08:31:54 +0300951 if (sizecell == 2) {
952 ret = fdt_setprop_u64(blob, newoff,
953 "reg", part->offset);
954 if (!ret)
955 ret = fdt_appendprop_u64(blob, newoff,
956 "reg", part->size);
957 } else {
958 ret = fdt_setprop_u32(blob, newoff,
959 "reg", part->offset);
960 if (!ret)
961 ret = fdt_appendprop_u32(blob, newoff,
962 "reg", part->size);
963 }
964
Anatolij Gustschin6c44c382010-03-16 17:10:05 +0100965 if (ret == -FDT_ERR_NOSPACE) {
966 ret = fdt_increase_size(blob, 512);
967 if (!ret)
968 goto add_reg;
969 else
970 goto err_size;
971 } else if (ret < 0)
972 goto err_prop;
973
974add_label:
975 ret = fdt_setprop_string(blob, newoff, "label", part->name);
976 if (ret == -FDT_ERR_NOSPACE) {
977 ret = fdt_increase_size(blob, 512);
978 if (!ret)
979 goto add_label;
980 else
981 goto err_size;
982 } else if (ret < 0)
983 goto err_prop;
984
985 part_num++;
986 }
987 return 0;
988err_size:
989 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
990 return ret;
991err_prop:
992 printf("Can't add property: %s\n", fdt_strerror(ret));
993 return ret;
994}
995
996/*
997 * Update partitions in nor/nand nodes using info from
998 * mtdparts environment variable. The nodes to update are
999 * specified by node_info structure which contains mtd device
1000 * type and compatible string: E. g. the board code in
1001 * ft_board_setup() could use:
1002 *
1003 * struct node_info nodes[] = {
1004 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1005 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1006 * };
1007 *
1008 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1009 */
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001010void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1011 int node_info_size)
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001012{
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001013 struct mtd_device *dev;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001014 int i, idx;
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001015 int noff, parts;
Masahiro Yamada030348f2020-07-17 10:46:18 +09001016 bool inited = false;
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001017
1018 for (i = 0; i < node_info_size; i++) {
1019 idx = 0;
Masahiro Yamada21644292020-07-17 10:46:19 +09001020
Marek Behún5d6b4482022-01-20 01:04:42 +01001021 fdt_for_each_node_by_compatible(noff, blob, -1,
1022 node_info[i].compat) {
Masahiro Yamada21644292020-07-17 10:46:19 +09001023 const char *prop;
1024
1025 prop = fdt_getprop(blob, noff, "status", NULL);
1026 if (prop && !strcmp(prop, "disabled"))
1027 continue;
1028
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001029 debug("%s: %s, mtd dev type %d\n",
1030 fdt_get_name(blob, noff, 0),
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001031 node_info[i].compat, node_info[i].type);
Masahiro Yamada030348f2020-07-17 10:46:18 +09001032
1033 if (!inited) {
1034 if (mtdparts_init() != 0)
1035 return;
1036 inited = true;
1037 }
1038
Masahiro Yamadaf3d40142018-07-19 16:28:22 +09001039 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001040 if (dev) {
Matthias Schiffer01f078f2022-02-03 15:14:47 +01001041 parts = fdt_subnode_offset(blob, noff,
1042 "partitions");
1043 if (parts < 0)
1044 parts = noff;
1045
1046 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001047 return; /* return on error */
1048 }
Anatolij Gustschin6c44c382010-03-16 17:10:05 +01001049 }
1050 }
1051}
1052#endif
Kumar Galaf4ad4fd2010-03-30 10:19:26 -05001053
1054void fdt_del_node_and_alias(void *blob, const char *alias)
1055{
1056 int off = fdt_path_offset(blob, alias);
1057
1058 if (off < 0)
1059 return;
1060
1061 fdt_del_node(blob, off);
1062
1063 off = fdt_path_offset(blob, "/aliases");
1064 fdt_delprop(blob, off, alias);
1065}
Kumar Galab87e7932010-06-16 14:27:38 -05001066
Kumar Galab87e7932010-06-16 14:27:38 -05001067/* Max address size we deal with */
1068#define OF_MAX_ADDR_CELLS 4
Dario Binacchif8fc7032021-05-01 17:05:26 +02001069#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1070 (ns) > 0)
Kumar Galab87e7932010-06-16 14:27:38 -05001071
1072/* Debug utility */
1073#ifdef DEBUG
Kim Phillips6542c072013-01-16 14:00:11 +00001074static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001075{
1076 printf("%s", s);
1077 while(na--)
1078 printf(" %08x", *(addr++));
1079 printf("\n");
1080}
1081#else
Kim Phillips6542c072013-01-16 14:00:11 +00001082static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galab87e7932010-06-16 14:27:38 -05001083#endif
1084
Paul Burtondbef2f02016-05-17 07:43:24 +01001085/**
1086 * struct of_bus - Callbacks for bus specific translators
Paul Burton03f08c52016-05-17 07:43:25 +01001087 * @name: A string used to identify this bus in debug output.
1088 * @addresses: The name of the DT property from which addresses are
1089 * to be read, typically "reg".
Paul Burtondbef2f02016-05-17 07:43:24 +01001090 * @match: Return non-zero if the node whose parent is at
1091 * parentoffset in the FDT blob corresponds to a bus
1092 * of this type, otherwise return zero. If NULL a match
1093 * is assumed.
Paul Burton03f08c52016-05-17 07:43:25 +01001094 * @count_cells:Count how many cells (be32 values) a node whose parent
1095 * is at parentoffset in the FDT blob will require to
1096 * represent its address (written to *addrc) & size
1097 * (written to *sizec).
1098 * @map: Map the address addr from the address space of this
1099 * bus to that of its parent, making use of the ranges
1100 * read from DT to an array at range. na and ns are the
1101 * number of cells (be32 values) used to hold and address
1102 * or size, respectively, for this bus. pna is the number
1103 * of cells used to hold an address for the parent bus.
1104 * Returns the address in the address space of the parent
1105 * bus.
1106 * @translate: Update the value of the address cells at addr within an
1107 * FDT by adding offset to it. na specifies the number of
1108 * cells used to hold the address being translated. Returns
1109 * zero on success, non-zero on error.
Paul Burtondbef2f02016-05-17 07:43:24 +01001110 *
1111 * Each bus type will include a struct of_bus in the of_busses array,
1112 * providing implementations of some or all of the functions used to
1113 * match the bus & handle address translation for its children.
1114 */
Kumar Galab87e7932010-06-16 14:27:38 -05001115struct of_bus {
1116 const char *name;
1117 const char *addresses;
Stephen Warrenc99581b2016-08-05 09:47:50 -06001118 int (*match)(const void *blob, int parentoffset);
1119 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001120 int *addrc, int *sizec);
Kim Phillips6542c072013-01-16 14:00:11 +00001121 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001122 int na, int ns, int pna);
Kim Phillips6542c072013-01-16 14:00:11 +00001123 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galab87e7932010-06-16 14:27:38 -05001124};
1125
1126/* Default translator (generic bus) */
Simon Glassbb7c01e2017-05-18 20:09:26 -06001127void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galab87e7932010-06-16 14:27:38 -05001128 int *addrc, int *sizec)
1129{
Kim Phillips6542c072013-01-16 14:00:11 +00001130 const fdt32_t *prop;
Scott Wood99899712010-08-12 18:37:39 -05001131
Simon Glass9fbc6322014-10-23 18:58:57 -06001132 if (addrc)
1133 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood99899712010-08-12 18:37:39 -05001134
1135 if (sizec) {
1136 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1137 if (prop)
Kim Phillips6542c072013-01-16 14:00:11 +00001138 *sizec = be32_to_cpup(prop);
Scott Wood99899712010-08-12 18:37:39 -05001139 else
1140 *sizec = 1;
1141 }
Kumar Galab87e7932010-06-16 14:27:38 -05001142}
1143
Kim Phillips6542c072013-01-16 14:00:11 +00001144static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galab87e7932010-06-16 14:27:38 -05001145 int na, int ns, int pna)
1146{
1147 u64 cp, s, da;
1148
Simon Glassbb7c01e2017-05-18 20:09:26 -06001149 cp = fdt_read_number(range, na);
1150 s = fdt_read_number(range + na + pna, ns);
1151 da = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001152
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301153 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galab87e7932010-06-16 14:27:38 -05001154
1155 if (da < cp || da >= (cp + s))
1156 return OF_BAD_ADDR;
1157 return da - cp;
1158}
1159
Kim Phillips6542c072013-01-16 14:00:11 +00001160static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galab87e7932010-06-16 14:27:38 -05001161{
Simon Glassbb7c01e2017-05-18 20:09:26 -06001162 u64 a = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001163 memset(addr, 0, na * 4);
1164 a += offset;
1165 if (na > 1)
Kim Phillips6542c072013-01-16 14:00:11 +00001166 addr[na - 2] = cpu_to_fdt32(a >> 32);
1167 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galab87e7932010-06-16 14:27:38 -05001168
1169 return 0;
1170}
1171
Paul Burtondbef2f02016-05-17 07:43:24 +01001172#ifdef CONFIG_OF_ISA_BUS
1173
1174/* ISA bus translator */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001175static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001176{
1177 const char *name;
1178
1179 name = fdt_get_name(blob, parentoffset, NULL);
1180 if (!name)
1181 return 0;
1182
1183 return !strcmp(name, "isa");
1184}
1185
Stephen Warrenc99581b2016-08-05 09:47:50 -06001186static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burtondbef2f02016-05-17 07:43:24 +01001187 int *addrc, int *sizec)
1188{
1189 if (addrc)
1190 *addrc = 2;
1191 if (sizec)
1192 *sizec = 1;
1193}
1194
1195static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1196 int na, int ns, int pna)
1197{
1198 u64 cp, s, da;
1199
1200 /* Check address type match */
1201 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1202 return OF_BAD_ADDR;
1203
Simon Glassbb7c01e2017-05-18 20:09:26 -06001204 cp = fdt_read_number(range + 1, na - 1);
1205 s = fdt_read_number(range + na + pna, ns);
1206 da = fdt_read_number(addr + 1, na - 1);
Paul Burtondbef2f02016-05-17 07:43:24 +01001207
Sekhar Noria98ba7f2018-12-06 15:20:47 +05301208 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burtondbef2f02016-05-17 07:43:24 +01001209
1210 if (da < cp || da >= (cp + s))
1211 return OF_BAD_ADDR;
1212 return da - cp;
1213}
1214
1215static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1216{
1217 return of_bus_default_translate(addr + 1, offset, na - 1);
1218}
1219
1220#endif /* CONFIG_OF_ISA_BUS */
1221
Kumar Galab87e7932010-06-16 14:27:38 -05001222/* Array of bus specific translators */
1223static struct of_bus of_busses[] = {
Paul Burtondbef2f02016-05-17 07:43:24 +01001224#ifdef CONFIG_OF_ISA_BUS
1225 /* ISA */
1226 {
1227 .name = "isa",
1228 .addresses = "reg",
1229 .match = of_bus_isa_match,
1230 .count_cells = of_bus_isa_count_cells,
1231 .map = of_bus_isa_map,
1232 .translate = of_bus_isa_translate,
1233 },
1234#endif /* CONFIG_OF_ISA_BUS */
Kumar Galab87e7932010-06-16 14:27:38 -05001235 /* Default */
1236 {
1237 .name = "default",
1238 .addresses = "reg",
Simon Glassbb7c01e2017-05-18 20:09:26 -06001239 .count_cells = fdt_support_default_count_cells,
Kumar Galab87e7932010-06-16 14:27:38 -05001240 .map = of_bus_default_map,
1241 .translate = of_bus_default_translate,
1242 },
1243};
1244
Stephen Warrenc99581b2016-08-05 09:47:50 -06001245static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burtondbef2f02016-05-17 07:43:24 +01001246{
1247 struct of_bus *bus;
1248
1249 if (ARRAY_SIZE(of_busses) == 1)
1250 return of_busses;
1251
1252 for (bus = of_busses; bus; bus++) {
1253 if (!bus->match || bus->match(blob, parentoffset))
1254 return bus;
1255 }
1256
1257 /*
1258 * We should always have matched the default bus at least, since
1259 * it has a NULL match field. If we didn't then it somehow isn't
1260 * in the of_busses array or something equally catastrophic has
1261 * gone wrong.
1262 */
1263 assert(0);
1264 return NULL;
1265}
1266
Stephen Warrenc99581b2016-08-05 09:47:50 -06001267static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips6542c072013-01-16 14:00:11 +00001268 struct of_bus *pbus, fdt32_t *addr,
Kumar Galab87e7932010-06-16 14:27:38 -05001269 int na, int ns, int pna, const char *rprop)
1270{
Kim Phillips6542c072013-01-16 14:00:11 +00001271 const fdt32_t *ranges;
Kumar Galab87e7932010-06-16 14:27:38 -05001272 int rlen;
1273 int rone;
1274 u64 offset = OF_BAD_ADDR;
1275
1276 /* Normally, an absence of a "ranges" property means we are
1277 * crossing a non-translatable boundary, and thus the addresses
1278 * below the current not cannot be converted to CPU physical ones.
1279 * Unfortunately, while this is very clear in the spec, it's not
1280 * what Apple understood, and they do have things like /uni-n or
1281 * /ht nodes with no "ranges" property and a lot of perfectly
1282 * useable mapped devices below them. Thus we treat the absence of
1283 * "ranges" as equivalent to an empty "ranges" property which means
1284 * a 1:1 translation at that level. It's up to the caller not to try
1285 * to translate addresses that aren't supposed to be translated in
1286 * the first place. --BenH.
1287 */
Kim Phillips6542c072013-01-16 14:00:11 +00001288 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galab87e7932010-06-16 14:27:38 -05001289 if (ranges == NULL || rlen == 0) {
Simon Glassbb7c01e2017-05-18 20:09:26 -06001290 offset = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001291 memset(addr, 0, pna * 4);
1292 debug("OF: no ranges, 1:1 translation\n");
1293 goto finish;
1294 }
1295
1296 debug("OF: walking ranges...\n");
1297
1298 /* Now walk through the ranges */
1299 rlen /= 4;
1300 rone = na + pna + ns;
1301 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1302 offset = bus->map(addr, ranges, na, ns, pna);
1303 if (offset != OF_BAD_ADDR)
1304 break;
1305 }
1306 if (offset == OF_BAD_ADDR) {
1307 debug("OF: not found !\n");
1308 return 1;
1309 }
1310 memcpy(addr, ranges + na, 4 * pna);
1311
1312 finish:
1313 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001314 debug("OF: with offset: %llu\n", offset);
Kumar Galab87e7932010-06-16 14:27:38 -05001315
1316 /* Translate it into parent bus space */
1317 return pbus->translate(addr, offset, pna);
1318}
1319
1320/*
1321 * Translate an address from the device-tree into a CPU physical address,
1322 * this walks up the tree and applies the various bus mappings on the
1323 * way.
1324 *
1325 * Note: We consider that crossing any level with #size-cells == 0 to mean
1326 * that translation is impossible (that is we are not dealing with a value
1327 * that can be mapped to a cpu physical address). This is not really specified
1328 * that way, but this is traditionally the way IBM at least do things
1329 */
Stephen Warrenc99581b2016-08-05 09:47:50 -06001330static u64 __of_translate_address(const void *blob, int node_offset,
1331 const fdt32_t *in_addr, const char *rprop)
Kumar Galab87e7932010-06-16 14:27:38 -05001332{
1333 int parent;
1334 struct of_bus *bus, *pbus;
Kim Phillips6542c072013-01-16 14:00:11 +00001335 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galab87e7932010-06-16 14:27:38 -05001336 int na, ns, pna, pns;
1337 u64 result = OF_BAD_ADDR;
1338
1339 debug("OF: ** translation for device %s **\n",
1340 fdt_get_name(blob, node_offset, NULL));
1341
1342 /* Get parent & match bus type */
1343 parent = fdt_parent_offset(blob, node_offset);
1344 if (parent < 0)
1345 goto bail;
Paul Burtondbef2f02016-05-17 07:43:24 +01001346 bus = of_match_bus(blob, parent);
Kumar Galab87e7932010-06-16 14:27:38 -05001347
1348 /* Cound address cells & copy address locally */
Scott Wood99899712010-08-12 18:37:39 -05001349 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001350 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001351 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1352 fdt_get_name(blob, node_offset, NULL));
1353 goto bail;
1354 }
1355 memcpy(addr, in_addr, na * 4);
1356
1357 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1358 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1359 of_dump_addr("OF: translating address:", addr, na);
1360
1361 /* Translate */
1362 for (;;) {
1363 /* Switch to parent bus */
1364 node_offset = parent;
1365 parent = fdt_parent_offset(blob, node_offset);
1366
1367 /* If root, we have finished */
1368 if (parent < 0) {
1369 debug("OF: reached root node\n");
Simon Glassbb7c01e2017-05-18 20:09:26 -06001370 result = fdt_read_number(addr, na);
Kumar Galab87e7932010-06-16 14:27:38 -05001371 break;
1372 }
1373
1374 /* Get new parent bus and counts */
Paul Burtondbef2f02016-05-17 07:43:24 +01001375 pbus = of_match_bus(blob, parent);
Scott Wood99899712010-08-12 18:37:39 -05001376 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczakf4d337c2016-01-12 15:40:44 +01001377 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galab87e7932010-06-16 14:27:38 -05001378 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1379 fdt_get_name(blob, node_offset, NULL));
1380 break;
1381 }
1382
1383 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1384 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1385
1386 /* Apply bus translation */
1387 if (of_translate_one(blob, node_offset, bus, pbus,
1388 addr, na, ns, pna, rprop))
1389 break;
1390
1391 /* Complete the move up one level */
1392 na = pna;
1393 ns = pns;
1394 bus = pbus;
1395
1396 of_dump_addr("OF: one level translation:", addr, na);
1397 }
1398 bail:
1399
1400 return result;
1401}
1402
Stephen Warrenc99581b2016-08-05 09:47:50 -06001403u64 fdt_translate_address(const void *blob, int node_offset,
1404 const fdt32_t *in_addr)
Kumar Galab87e7932010-06-16 14:27:38 -05001405{
1406 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1407}
Kumar Gala800d1d12010-07-04 12:48:21 -05001408
Fabien Dessenne22236e02019-05-31 15:11:30 +02001409u64 fdt_translate_dma_address(const void *blob, int node_offset,
1410 const fdt32_t *in_addr)
1411{
1412 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1413}
1414
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001415int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1416 dma_addr_t *bus, u64 *size)
1417{
1418 bool found_dma_ranges = false;
1419 struct of_bus *bus_node;
1420 const fdt32_t *ranges;
1421 int na, ns, pna, pns;
1422 int parent = node;
1423 int ret = 0;
1424 int len;
1425
1426 /* Find the closest dma-ranges property */
1427 while (parent >= 0) {
1428 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1429
1430 /* Ignore empty ranges, they imply no translation required */
1431 if (ranges && len > 0)
1432 break;
1433
1434 /* Once we find 'dma-ranges', then a missing one is an error */
1435 if (found_dma_ranges && !ranges) {
1436 ret = -EINVAL;
1437 goto out;
1438 }
1439
1440 if (ranges)
1441 found_dma_ranges = true;
1442
1443 parent = fdt_parent_offset(blob, parent);
1444 }
1445
1446 if (!ranges || parent < 0) {
1447 debug("no dma-ranges found for node %s\n",
1448 fdt_get_name(blob, node, NULL));
1449 ret = -ENOENT;
1450 goto out;
1451 }
1452
1453 /* switch to that node */
1454 node = parent;
1455 parent = fdt_parent_offset(blob, node);
1456 if (parent < 0) {
Vagrant Cascadiand00c5c72021-12-21 13:06:58 -08001457 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne50d2fa42021-01-12 13:55:22 +01001458 ret = -EINVAL;
1459 goto out;
1460 }
1461
1462 /* Get the address sizes both for the bus and its parent */
1463 bus_node = of_match_bus(blob, node);
1464 bus_node->count_cells(blob, node, &na, &ns);
1465 if (!OF_CHECK_COUNTS(na, ns)) {
1466 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1467 fdt_get_name(blob, node, NULL));
1468 return -EINVAL;
1469 goto out;
1470 }
1471
1472 bus_node = of_match_bus(blob, parent);
1473 bus_node->count_cells(blob, parent, &pna, &pns);
1474 if (!OF_CHECK_COUNTS(pna, pns)) {
1475 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1476 fdt_get_name(blob, parent, NULL));
1477 return -EINVAL;
1478 goto out;
1479 }
1480
1481 *bus = fdt_read_number(ranges, na);
1482 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1483 *size = fdt_read_number(ranges + na + pna, ns);
1484out:
1485 return ret;
1486}
1487
Kumar Gala800d1d12010-07-04 12:48:21 -05001488/**
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001489 * fdt_node_offset_by_compat_reg: Find a node that matches compatible and
Kumar Gala800d1d12010-07-04 12:48:21 -05001490 * who's reg property matches a physical cpu address
1491 *
1492 * @blob: ptr to device tree
Hugo Villeneuve3ab6d9c2023-04-24 16:51:50 -04001493 * @compat: compatible string to match
Kumar Gala800d1d12010-07-04 12:48:21 -05001494 * @compat_off: property name
1495 *
1496 */
1497int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1498 phys_addr_t compat_off)
1499{
Marek Behún5d6b4482022-01-20 01:04:42 +01001500 int len, off;
1501
1502 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips6542c072013-01-16 14:00:11 +00001503 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún5d6b4482022-01-20 01:04:42 +01001504 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1505 return off;
Kumar Gala800d1d12010-07-04 12:48:21 -05001506 }
1507
1508 return -FDT_ERR_NOTFOUND;
1509}
1510
Marek Behún4ef5ca62021-11-26 14:57:10 +01001511static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1512{
1513 char path[512];
1514 int len;
1515
1516 len = vsnprintf(path, sizeof(path), fmt, ap);
1517 if (len < 0 || len + 1 > sizeof(path))
1518 return -FDT_ERR_NOSPACE;
1519
1520 return fdt_path_offset(blob, path);
1521}
1522
1523/**
1524 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1525 *
1526 * @blob: ptr to device tree
1527 * @fmt: path format
1528 * @ap: vsnprintf arguments
1529 */
1530int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1531{
1532 va_list ap;
1533 int res;
1534
1535 va_start(ap, fmt);
1536 res = vnode_offset_by_pathf(blob, fmt, ap);
1537 va_end(ap);
1538
1539 return res;
1540}
1541
Gerald Van Barenaf737882011-07-14 21:40:10 -04001542/*
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001543 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barenaf737882011-07-14 21:40:10 -04001544 *
1545 * @fdt: ptr to device tree
1546 * @nodeoffset: node to update
1547 * @phandle: phandle value to set (must be unique)
Kumar Galaf2f1c5a2011-08-01 00:23:23 -05001548 */
1549int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barenaf737882011-07-14 21:40:10 -04001550{
1551 int ret;
1552
1553#ifdef DEBUG
1554 int off = fdt_node_offset_by_phandle(fdt, phandle);
1555
1556 if ((off >= 0) && (off != nodeoffset)) {
1557 char buf[64];
1558
1559 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1560 printf("Trying to update node %s with phandle %u ",
1561 buf, phandle);
1562
1563 fdt_get_path(fdt, off, buf, sizeof(buf));
1564 printf("that already exists in node %s.\n", buf);
1565 return -FDT_ERR_BADPHANDLE;
1566 }
1567#endif
1568
1569 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barenaf737882011-07-14 21:40:10 -04001570
1571 return ret;
1572}
1573
Kumar Galad5fc2582011-08-01 00:25:20 -05001574/*
Marek Behúnae0ef952021-11-26 14:57:09 +01001575 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Galad5fc2582011-08-01 00:25:20 -05001576 *
1577 * @fdt: ptr to device tree
1578 * @nodeoffset: node to update
1579 */
Timur Tabi0f46c802011-09-20 18:24:34 -05001580unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Galad5fc2582011-08-01 00:25:20 -05001581{
1582 /* see if there is a phandle already */
Marek Behún3577eba32021-11-26 14:57:07 +01001583 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Galad5fc2582011-08-01 00:25:20 -05001584
1585 /* if we got 0, means no phandle so create one */
1586 if (phandle == 0) {
Timur Tabi0f46c802011-09-20 18:24:34 -05001587 int ret;
1588
Marek Behún3577eba32021-11-26 14:57:07 +01001589 ret = fdt_generate_phandle(fdt, &phandle);
1590 if (ret < 0) {
1591 printf("Can't generate phandle: %s\n",
1592 fdt_strerror(ret));
1593 return 0;
1594 }
1595
Timur Tabi0f46c802011-09-20 18:24:34 -05001596 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1597 if (ret < 0) {
1598 printf("Can't set phandle %u: %s\n", phandle,
1599 fdt_strerror(ret));
1600 return 0;
1601 }
Kumar Galad5fc2582011-08-01 00:25:20 -05001602 }
1603
1604 return phandle;
1605}
1606
Marek Behún4ef5ca62021-11-26 14:57:10 +01001607/**
1608 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1609 * given compatible
1610 *
1611 * @fdt: ptr to device tree
1612 * @compat: node's compatible string
1613 */
1614unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1615{
1616 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1617
1618 if (offset < 0) {
1619 printf("Can't find node with compatible \"%s\": %s\n", compat,
1620 fdt_strerror(offset));
1621 return 0;
1622 }
1623
1624 return fdt_create_phandle(fdt, offset);
1625}
1626
1627/**
1628 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1629 * sprintf-formatted path
1630 *
1631 * @fdt: ptr to device tree
1632 * @fmt, ...: path format string and arguments to pass to sprintf
1633 */
1634unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1635{
1636 va_list ap;
1637 int offset;
1638
1639 va_start(ap, fmt);
1640 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1641 va_end(ap);
1642
1643 if (offset < 0) {
1644 printf("Can't find node by given path: %s\n",
1645 fdt_strerror(offset));
1646 return 0;
1647 }
1648
1649 return fdt_create_phandle(fdt, offset);
1650}
1651
Shengzhou Liua7be8022011-10-14 16:26:05 +08001652/*
1653 * fdt_set_node_status: Set status for the given node
1654 *
1655 * @fdt: ptr to device tree
1656 * @nodeoffset: node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001657 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001658 */
Marek Behúnf872e832021-11-26 14:57:08 +01001659int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001660{
Shengzhou Liua7be8022011-10-14 16:26:05 +08001661 int ret = 0;
1662
1663 if (nodeoffset < 0)
1664 return nodeoffset;
1665
1666 switch (status) {
1667 case FDT_STATUS_OKAY:
1668 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1669 break;
1670 case FDT_STATUS_DISABLED:
1671 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1672 break;
1673 case FDT_STATUS_FAIL:
1674 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1675 break;
Shengzhou Liua7be8022011-10-14 16:26:05 +08001676 default:
1677 printf("Invalid fdt status: %x\n", status);
1678 ret = -1;
1679 break;
1680 }
1681
1682 return ret;
1683}
1684
1685/*
1686 * fdt_set_status_by_alias: Set status for the given node given an alias
1687 *
1688 * @fdt: ptr to device tree
1689 * @alias: alias of node to update
Marek Behúnf872e832021-11-26 14:57:08 +01001690 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liua7be8022011-10-14 16:26:05 +08001691 */
1692int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behúnf872e832021-11-26 14:57:08 +01001693 enum fdt_status status)
Shengzhou Liua7be8022011-10-14 16:26:05 +08001694{
1695 int offset = fdt_path_offset(fdt, alias);
1696
Marek Behúnf872e832021-11-26 14:57:08 +01001697 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liua7be8022011-10-14 16:26:05 +08001698}
1699
Marek Behún4ef5ca62021-11-26 14:57:10 +01001700/**
1701 * fdt_set_status_by_compatible: Set node status for first node with given
1702 * compatible
1703 *
1704 * @fdt: ptr to device tree
1705 * @compat: node's compatible string
1706 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1707 */
1708int fdt_set_status_by_compatible(void *fdt, const char *compat,
1709 enum fdt_status status)
1710{
1711 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1712
1713 if (offset < 0)
1714 return offset;
1715
1716 return fdt_set_node_status(fdt, offset, status);
1717}
1718
1719/**
1720 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1721 * path
1722 *
1723 * @fdt: ptr to device tree
1724 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1725 * @fmt, ...: path format string and arguments to pass to sprintf
1726 */
1727int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1728 ...)
1729{
1730 va_list ap;
1731 int offset;
1732
1733 va_start(ap, fmt);
1734 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1735 va_end(ap);
1736
1737 if (offset < 0)
1738 return offset;
1739
1740 return fdt_set_node_status(fdt, offset, status);
1741}
1742
Timur Tabi8ebaf202011-05-03 13:24:07 -05001743/*
1744 * Verify the physical address of device tree node for a given alias
1745 *
1746 * This function locates the device tree node of a given alias, and then
1747 * verifies that the physical address of that device matches the given
1748 * parameter. It displays a message if there is a mismatch.
1749 *
1750 * Returns 1 on success, 0 on failure
1751 */
1752int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1753{
1754 const char *path;
Kim Phillips6542c072013-01-16 14:00:11 +00001755 const fdt32_t *reg;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001756 int node, len;
1757 u64 dt_addr;
1758
1759 path = fdt_getprop(fdt, anode, alias, NULL);
1760 if (!path) {
1761 /* If there's no such alias, then it's not a failure */
1762 return 1;
1763 }
1764
1765 node = fdt_path_offset(fdt, path);
1766 if (node < 0) {
1767 printf("Warning: device tree alias '%s' points to invalid "
1768 "node %s.\n", alias, path);
1769 return 0;
1770 }
1771
1772 reg = fdt_getprop(fdt, node, "reg", &len);
1773 if (!reg) {
1774 printf("Warning: device tree node '%s' has no address.\n",
1775 path);
1776 return 0;
1777 }
1778
1779 dt_addr = fdt_translate_address(fdt, node, reg);
1780 if (addr != dt_addr) {
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001781 printf("Warning: U-Boot configured device %s at address %llu,\n"
1782 "but the device tree has it address %llx.\n",
1783 alias, addr, dt_addr);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001784 return 0;
1785 }
1786
1787 return 1;
1788}
1789
1790/*
1791 * Returns the base address of an SOC or PCI node
1792 */
Simon Glass293340a2017-05-18 20:09:00 -06001793u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabi8ebaf202011-05-03 13:24:07 -05001794{
1795 int size;
Kim Phillips6542c072013-01-16 14:00:11 +00001796 const fdt32_t *prop;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001797
Simon Glass29f304b2017-07-04 13:31:20 -06001798 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabi8ebaf202011-05-03 13:24:07 -05001799
Masahiro Yamadab1dd47c2019-06-27 16:39:57 +09001800 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabi8ebaf202011-05-03 13:24:07 -05001801}
Alexander Graf19555be2014-04-11 17:09:41 +02001802
1803/*
Bin Mengd67d7d02021-02-25 17:22:24 +08001804 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1805 * or 3 cells specially for a PCI address.
Alexander Graf19555be2014-04-11 17:09:41 +02001806 */
1807static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1808 uint64_t *val, int cells)
1809{
Bin Mengd67d7d02021-02-25 17:22:24 +08001810 const fdt32_t *prop32;
1811 const unaligned_fdt64_t *prop64;
Alexander Graf19555be2014-04-11 17:09:41 +02001812
1813 if ((cell_off + cells) > prop_len)
1814 return -FDT_ERR_NOSPACE;
1815
Bin Mengd67d7d02021-02-25 17:22:24 +08001816 prop32 = &prop[cell_off];
1817
1818 /*
1819 * Special handling for PCI address in PCI bus <ranges>
1820 *
1821 * PCI child address is made up of 3 cells. Advance the cell offset
1822 * by 1 so that the PCI child address can be correctly read.
1823 */
1824 if (cells == 3)
1825 cell_off += 1;
1826 prop64 = (const fdt64_t *)&prop[cell_off];
1827
Alexander Graf19555be2014-04-11 17:09:41 +02001828 switch (cells) {
1829 case 1:
1830 *val = fdt32_to_cpu(*prop32);
1831 break;
1832 case 2:
Bin Mengd67d7d02021-02-25 17:22:24 +08001833 case 3:
Alexander Graf19555be2014-04-11 17:09:41 +02001834 *val = fdt64_to_cpu(*prop64);
1835 break;
1836 default:
1837 return -FDT_ERR_NOSPACE;
1838 }
1839
1840 return 0;
1841}
1842
1843/**
1844 * fdt_read_range - Read a node's n'th range property
1845 *
1846 * @fdt: ptr to device tree
1847 * @node: offset of node
1848 * @n: range index
1849 * @child_addr: pointer to storage for the "child address" field
1850 * @addr: pointer to storage for the CPU view translated physical start
1851 * @len: pointer to storage for the range length
1852 *
1853 * Convenience function that reads and interprets a specific range out of
1854 * a number of the "ranges" property array.
1855 */
1856int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1857 uint64_t *addr, uint64_t *len)
1858{
1859 int pnode = fdt_parent_offset(fdt, node);
1860 const fdt32_t *ranges;
1861 int pacells;
1862 int acells;
1863 int scells;
1864 int ranges_len;
1865 int cell = 0;
1866 int r = 0;
1867
1868 /*
1869 * The "ranges" property is an array of
1870 * { <child address> <parent address> <size in child address space> }
1871 *
1872 * All 3 elements can span a diffent number of cells. Fetch their size.
1873 */
1874 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1875 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1876 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1877
1878 /* Now try to get the ranges property */
1879 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1880 if (!ranges)
1881 return -FDT_ERR_NOTFOUND;
1882 ranges_len /= sizeof(uint32_t);
1883
1884 /* Jump to the n'th entry */
1885 cell = n * (pacells + acells + scells);
1886
1887 /* Read <child address> */
1888 if (child_addr) {
1889 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1890 acells);
1891 if (r)
1892 return r;
1893 }
1894 cell += acells;
1895
1896 /* Read <parent address> */
1897 if (addr)
1898 *addr = fdt_translate_address(fdt, node, ranges + cell);
1899 cell += pacells;
1900
1901 /* Read <size in child address space> */
1902 if (len) {
1903 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1904 if (r)
1905 return r;
1906 }
1907
1908 return 0;
1909}
Hans de Goedeea876372014-11-17 15:29:11 +01001910
1911/**
1912 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1913 *
1914 * @fdt: ptr to device tree
1915 * @node: offset of the simplefb node
1916 * @base_address: framebuffer base address
1917 * @width: width in pixels
1918 * @height: height in pixels
1919 * @stride: bytes per line
1920 * @format: pixel format string
1921 *
1922 * Convenience function to fill and enable a simplefb node.
1923 */
1924int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1925 u32 height, u32 stride, const char *format)
1926{
1927 char name[32];
1928 fdt32_t cells[4];
1929 int i, addrc, sizec, ret;
1930
Simon Glassbb7c01e2017-05-18 20:09:26 -06001931 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1932 &addrc, &sizec);
Hans de Goedeea876372014-11-17 15:29:11 +01001933 i = 0;
1934 if (addrc == 2)
1935 cells[i++] = cpu_to_fdt32(base_address >> 32);
1936 cells[i++] = cpu_to_fdt32(base_address);
1937 if (sizec == 2)
1938 cells[i++] = 0;
1939 cells[i++] = cpu_to_fdt32(height * stride);
1940
1941 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1942 if (ret < 0)
1943 return ret;
1944
Masahiro Yamadac7570a32018-08-06 20:47:40 +09001945 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goedeea876372014-11-17 15:29:11 +01001946 ret = fdt_set_name(fdt, node, name);
1947 if (ret < 0)
1948 return ret;
1949
1950 ret = fdt_setprop_u32(fdt, node, "width", width);
1951 if (ret < 0)
1952 return ret;
1953
1954 ret = fdt_setprop_u32(fdt, node, "height", height);
1955 if (ret < 0)
1956 return ret;
1957
1958 ret = fdt_setprop_u32(fdt, node, "stride", stride);
1959 if (ret < 0)
1960 return ret;
1961
1962 ret = fdt_setprop_string(fdt, node, "format", format);
1963 if (ret < 0)
1964 return ret;
1965
1966 ret = fdt_setprop_string(fdt, node, "status", "okay");
1967 if (ret < 0)
1968 return ret;
1969
1970 return 0;
1971}
Tim Harvey5d7c6b52015-04-08 11:45:39 -07001972
1973/*
1974 * Update native-mode in display-timings from display environment variable.
1975 * The node to update are specified by path.
1976 */
1977int fdt_fixup_display(void *blob, const char *path, const char *display)
1978{
1979 int off, toff;
1980
1981 if (!display || !path)
1982 return -FDT_ERR_NOTFOUND;
1983
1984 toff = fdt_path_offset(blob, path);
1985 if (toff >= 0)
1986 toff = fdt_subnode_offset(blob, toff, "display-timings");
1987 if (toff < 0)
1988 return toff;
1989
1990 for (off = fdt_first_subnode(blob, toff);
1991 off >= 0;
1992 off = fdt_next_subnode(blob, off)) {
1993 uint32_t h = fdt_get_phandle(blob, off);
1994 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
1995 fdt32_to_cpu(h));
1996 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
1997 return fdt_setprop_u32(blob, toff, "native-mode", h);
1998 }
1999 return toff;
2000}
Pantelis Antoniou592386d2017-09-04 23:12:11 +03002001
2002#ifdef CONFIG_OF_LIBFDT_OVERLAY
2003/**
2004 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2005 *
2006 * @fdt: ptr to device tree
2007 * @fdto: ptr to device tree overlay
2008 *
2009 * Convenience function to apply an overlay and display helpful messages
2010 * in the case of an error
2011 */
2012int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2013{
2014 int err;
2015 bool has_symbols;
2016
2017 err = fdt_path_offset(fdt, "/__symbols__");
2018 has_symbols = err >= 0;
2019
2020 err = fdt_overlay_apply(fdt, fdto);
2021 if (err < 0) {
2022 printf("failed on fdt_overlay_apply(): %s\n",
2023 fdt_strerror(err));
2024 if (!has_symbols) {
2025 printf("base fdt does did not have a /__symbols__ node\n");
2026 printf("make sure you've compiled with -@\n");
2027 }
2028 }
2029 return err;
2030}
2031#endif
Kory Maincent623e1ac2021-05-04 19:31:21 +02002032
2033/**
2034 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2035 *
2036 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01002037 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincent623e1ac2021-05-04 19:31:21 +02002038 */
2039int fdt_valid(struct fdt_header **blobp)
2040{
2041 const void *blob = *blobp;
2042 int err;
2043
2044 if (!blob) {
2045 printf("The address of the fdt is invalid (NULL).\n");
2046 return 0;
2047 }
2048
2049 err = fdt_check_header(blob);
2050 if (err == 0)
2051 return 1; /* valid */
2052
2053 if (err < 0) {
2054 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2055 /*
2056 * Be more informative on bad version.
2057 */
2058 if (err == -FDT_ERR_BADVERSION) {
2059 if (fdt_version(blob) <
2060 FDT_FIRST_SUPPORTED_VERSION) {
2061 printf(" - too old, fdt %d < %d",
2062 fdt_version(blob),
2063 FDT_FIRST_SUPPORTED_VERSION);
2064 }
2065 if (fdt_last_comp_version(blob) >
2066 FDT_LAST_SUPPORTED_VERSION) {
2067 printf(" - too new, fdt %d > %d",
2068 fdt_version(blob),
2069 FDT_LAST_SUPPORTED_VERSION);
2070 }
2071 }
2072 printf("\n");
2073 *blobp = NULL;
2074 return 0;
2075 }
2076 return 1;
2077}