blob: 0b40ddd867aca52c3b3bf26c22e94e961a95c0eb [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04002/*
3 * (C) Copyright 2007
4 * Gerald Van Baren, Custom IDEAS, vanbaren@cideas.com
5 *
Shengzhou Liu2a523f52011-10-14 16:26:05 +08006 * Copyright 2010-2011 Freescale Semiconductor, Inc.
Gerald Van Baren64dbbd42007-04-06 14:19:43 -04007 */
8
9#include <common.h>
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +020010#include <abuf.h>
Simon Glass7b51b572019-08-01 09:46:52 -060011#include <env.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Heinrich Schuchardt9ad0a792018-11-18 17:58:51 +010013#include <mapmem.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <net.h>
Anton Vorontsov3e303f72009-10-15 17:47:04 +040015#include <stdio_dev.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040016#include <linux/ctype.h>
17#include <linux/types.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040018#include <asm/global_data.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090019#include <linux/libfdt.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040020#include <fdt_support.h>
Kumar Gala151c8b02007-11-26 17:06:15 -060021#include <exports.h>
Bin Menga0ae3802015-12-07 01:39:47 -080022#include <fdtdec.h>
Francesco Dolcini622ecee2022-05-19 16:22:26 +020023#include <version.h>
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040024
Kumar Gala3bed2aa2008-10-23 00:05:47 -050025/**
Alexander Graf94fb1822014-04-11 17:09:40 +020026 * fdt_getprop_u32_default_node - Return a node's property or a default
27 *
28 * @fdt: ptr to device tree
29 * @off: offset of node
30 * @cell: cell offset in property
31 * @prop: property name
32 * @dflt: default value if the property isn't found
33 *
34 * Convenience function to return a node's property or a default value if
35 * the property doesn't exist.
36 */
37u32 fdt_getprop_u32_default_node(const void *fdt, int off, int cell,
38 const char *prop, const u32 dflt)
39{
40 const fdt32_t *val;
41 int len;
42
43 val = fdt_getprop(fdt, off, prop, &len);
44
45 /* Check if property exists */
46 if (!val)
47 return dflt;
48
49 /* Check if property is long enough */
50 if (len < ((cell + 1) * sizeof(uint32_t)))
51 return dflt;
52
53 return fdt32_to_cpu(*val);
54}
55
56/**
Kumar Gala3bed2aa2008-10-23 00:05:47 -050057 * fdt_getprop_u32_default - Find a node and return it's property or a default
58 *
59 * @fdt: ptr to device tree
60 * @path: path of node
61 * @prop: property name
62 * @dflt: default value if the property isn't found
63 *
64 * Convenience function to find a node and return it's property or a
65 * default value if it doesn't exist.
66 */
Gabe Black07e12782011-11-08 01:09:44 -080067u32 fdt_getprop_u32_default(const void *fdt, const char *path,
68 const char *prop, const u32 dflt)
Kumar Gala3bed2aa2008-10-23 00:05:47 -050069{
Kumar Gala3bed2aa2008-10-23 00:05:47 -050070 int off;
71
72 off = fdt_path_offset(fdt, path);
73 if (off < 0)
74 return dflt;
75
Alexander Graf94fb1822014-04-11 17:09:40 +020076 return fdt_getprop_u32_default_node(fdt, off, 0, prop, dflt);
Kumar Gala3bed2aa2008-10-23 00:05:47 -050077}
Gerald Van Baren64dbbd42007-04-06 14:19:43 -040078
Kumar Galaa3c29332007-10-24 10:21:57 -050079/**
80 * fdt_find_and_setprop: Find a node and set it's property
81 *
82 * @fdt: ptr to device tree
83 * @node: path of node
84 * @prop: property name
85 * @val: ptr to new value
86 * @len: length of new property value
87 * @create: flag to create the property if it doesn't exist
88 *
89 * Convenience function to directly set a property given the path to the node.
90 */
91int fdt_find_and_setprop(void *fdt, const char *node, const char *prop,
92 const void *val, int len, int create)
93{
Kumar Gala8d04f022007-10-24 11:04:22 -050094 int nodeoff = fdt_path_offset(fdt, node);
Kumar Galaa3c29332007-10-24 10:21:57 -050095
96 if (nodeoff < 0)
97 return nodeoff;
98
Kim Phillips8aa5ec62013-01-16 14:00:11 +000099 if ((!create) && (fdt_get_property(fdt, nodeoff, prop, NULL) == NULL))
Kumar Galaa3c29332007-10-24 10:21:57 -0500100 return 0; /* create flag not set; so exit quietly */
101
102 return fdt_setprop(fdt, nodeoff, prop, val, len);
103}
104
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900105/**
Simon Glassa9e8e292014-10-23 18:58:49 -0600106 * fdt_find_or_add_subnode() - find or possibly add a subnode of a given node
107 *
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900108 * @fdt: pointer to the device tree blob
109 * @parentoffset: structure block offset of a node
110 * @name: name of the subnode to locate
111 *
112 * fdt_subnode_offset() finds a subnode of the node with a given name.
113 * If the subnode does not exist, it will be created.
114 */
Simon Glassa9e8e292014-10-23 18:58:49 -0600115int fdt_find_or_add_subnode(void *fdt, int parentoffset, const char *name)
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900116{
117 int offset;
118
119 offset = fdt_subnode_offset(fdt, parentoffset, name);
120
121 if (offset == -FDT_ERR_NOTFOUND)
122 offset = fdt_add_subnode(fdt, parentoffset, name);
123
124 if (offset < 0)
125 printf("%s: %s: %s\n", __func__, name, fdt_strerror(offset));
126
127 return offset;
128}
129
Andre Przywarae036a1d2021-02-14 10:35:18 +0000130#if defined(CONFIG_OF_STDOUT_VIA_ALIAS) && defined(CONFIG_CONS_INDEX)
Detlev Zundel40777812008-06-20 22:24:05 +0200131static int fdt_fixup_stdout(void *fdt, int chosenoff)
Kumar Gala151c8b02007-11-26 17:06:15 -0600132{
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900133 int err;
134 int aliasoff;
Kumar Gala151c8b02007-11-26 17:06:15 -0600135 char sername[9] = { 0 };
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900136 const void *path;
137 int len;
138 char tmp[256]; /* long enough */
Kumar Gala151c8b02007-11-26 17:06:15 -0600139
Bin Meng9f29aeb2016-01-13 19:38:58 -0800140 sprintf(sername, "serial%d", CONFIG_CONS_INDEX - 1);
Kumar Gala151c8b02007-11-26 17:06:15 -0600141
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900142 aliasoff = fdt_path_offset(fdt, "/aliases");
143 if (aliasoff < 0) {
144 err = aliasoff;
Scott Woodda77c812015-09-01 22:48:08 -0500145 goto noalias;
Kumar Gala151c8b02007-11-26 17:06:15 -0600146 }
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900147
148 path = fdt_getprop(fdt, aliasoff, sername, &len);
149 if (!path) {
150 err = len;
Scott Woodda77c812015-09-01 22:48:08 -0500151 goto noalias;
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900152 }
153
154 /* fdt_setprop may break "path" so we copy it to tmp buffer */
155 memcpy(tmp, path, len);
156
157 err = fdt_setprop(fdt, chosenoff, "linux,stdout-path", tmp, len);
Kumar Gala151c8b02007-11-26 17:06:15 -0600158 if (err < 0)
159 printf("WARNING: could not set linux,stdout-path %s.\n",
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900160 fdt_strerror(err));
Kumar Gala151c8b02007-11-26 17:06:15 -0600161
162 return err;
Scott Woodda77c812015-09-01 22:48:08 -0500163
164noalias:
165 printf("WARNING: %s: could not read %s alias: %s\n",
166 __func__, sername, fdt_strerror(err));
167
168 return 0;
Kumar Gala151c8b02007-11-26 17:06:15 -0600169}
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900170#else
171static int fdt_fixup_stdout(void *fdt, int chosenoff)
172{
173 return 0;
174}
Kumar Gala151c8b02007-11-26 17:06:15 -0600175#endif
176
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900177static inline int fdt_setprop_uxx(void *fdt, int nodeoffset, const char *name,
178 uint64_t val, int is_u64)
179{
180 if (is_u64)
181 return fdt_setprop_u64(fdt, nodeoffset, name, val);
182 else
183 return fdt_setprop_u32(fdt, nodeoffset, name, (uint32_t)val);
184}
185
Paul Kocialkowski10be5b52015-05-21 11:27:03 +0200186int fdt_root(void *fdt)
187{
188 char *serial;
189 int err;
190
191 err = fdt_check_header(fdt);
192 if (err < 0) {
193 printf("fdt_root: %s\n", fdt_strerror(err));
194 return err;
195 }
196
Simon Glass00caae62017-08-03 12:22:12 -0600197 serial = env_get("serial#");
Paul Kocialkowski10be5b52015-05-21 11:27:03 +0200198 if (serial) {
199 err = fdt_setprop(fdt, 0, "serial-number", serial,
200 strlen(serial) + 1);
201
202 if (err < 0) {
203 printf("WARNING: could not set serial-number %s.\n",
204 fdt_strerror(err));
205 return err;
206 }
207 }
208
209 return 0;
210}
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900211
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900212int fdt_initrd(void *fdt, ulong initrd_start, ulong initrd_end)
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500213{
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900214 int nodeoffset;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500215 int err, j, total;
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900216 int is_u64;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500217 uint64_t addr, size;
218
Masahiro Yamada50babaf2014-04-18 17:41:05 +0900219 /* just return if the size of initrd is zero */
220 if (initrd_start == initrd_end)
221 return 0;
222
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900223 /* find or create "/chosen" node. */
224 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
225 if (nodeoffset < 0)
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500226 return nodeoffset;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500227
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500228 total = fdt_num_mem_rsv(fdt);
229
230 /*
231 * Look for an existing entry and update it. If we don't find
232 * the entry, we will j be the next available slot.
233 */
234 for (j = 0; j < total; j++) {
235 err = fdt_get_mem_rsv(fdt, j, &addr, &size);
236 if (addr == initrd_start) {
237 fdt_del_mem_rsv(fdt, j);
238 break;
239 }
240 }
241
Grant Likelyce6b27a2011-03-28 09:58:55 +0000242 err = fdt_add_mem_rsv(fdt, initrd_start, initrd_end - initrd_start);
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500243 if (err < 0) {
244 printf("fdt_initrd: %s\n", fdt_strerror(err));
245 return err;
246 }
247
Simon Glass933cdbb2014-10-23 18:58:57 -0600248 is_u64 = (fdt_address_cells(fdt, 0) == 2);
David Fengf77a6062013-12-14 11:47:29 +0800249
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900250 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-start",
251 (uint64_t)initrd_start, is_u64);
252
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900253 if (err < 0) {
254 printf("WARNING: could not set linux,initrd-start %s.\n",
255 fdt_strerror(err));
256 return err;
257 }
Masahiro Yamadaf18295d2014-04-18 17:41:04 +0900258
259 err = fdt_setprop_uxx(fdt, nodeoffset, "linux,initrd-end",
260 (uint64_t)initrd_end, is_u64);
261
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900262 if (err < 0) {
263 printf("WARNING: could not set linux,initrd-end %s.\n",
264 fdt_strerror(err));
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500265
Masahiro Yamadadbe963a2014-04-18 17:40:59 +0900266 return err;
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500267 }
268
269 return 0;
270}
271
Niko Maunof0b21eb2021-02-22 19:18:51 +0000272/**
273 * board_fdt_chosen_bootargs - boards may override this function to use
274 * alternative kernel command line arguments
275 */
276__weak char *board_fdt_chosen_bootargs(void)
277{
278 return env_get("bootargs");
279}
280
Masahiro Yamadabc6ed0f2014-04-18 17:41:00 +0900281int fdt_chosen(void *fdt)
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400282{
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +0200283 struct abuf buf = {};
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400284 int nodeoffset;
285 int err;
Gerald Van Baren35ec3982007-05-25 22:08:57 -0400286 char *str; /* used to set string properties */
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400287
288 err = fdt_check_header(fdt);
289 if (err < 0) {
Gerald Van Baren5fe6be62007-08-07 21:14:22 -0400290 printf("fdt_chosen: %s\n", fdt_strerror(err));
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400291 return err;
292 }
293
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900294 /* find or create "/chosen" node. */
295 nodeoffset = fdt_find_or_add_subnode(fdt, 0, "chosen");
296 if (nodeoffset < 0)
297 return nodeoffset;
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400298
Bo Lvf73262b2024-12-26 16:36:26 +0800299#ifndef CONFIG_AMLOGIC_MODIFY
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +0200300 if (IS_ENABLED(CONFIG_BOARD_RNG_SEED) && !board_rng_seed(&buf)) {
Bo Lvf73262b2024-12-26 16:36:26 +0800301#else
302 if (!board_rng_seed(&buf)) {
303#endif
Rasmus Villemoes6dca1d92022-08-22 09:34:23 +0200304 err = fdt_setprop(fdt, nodeoffset, "rng-seed",
305 abuf_data(&buf), abuf_size(&buf));
306 abuf_uninit(&buf);
307 if (err < 0) {
308 printf("WARNING: could not set rng-seed %s.\n",
309 fdt_strerror(err));
310 return err;
311 }
312 }
313
Niko Maunof0b21eb2021-02-22 19:18:51 +0000314 str = board_fdt_chosen_bootargs();
315
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900316 if (str) {
317 err = fdt_setprop(fdt, nodeoffset, "bootargs", str,
318 strlen(str) + 1);
319 if (err < 0) {
Masahiro Yamadabc6ed0f2014-04-18 17:41:00 +0900320 printf("WARNING: could not set bootargs %s.\n",
321 fdt_strerror(err));
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900322 return err;
323 }
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400324 }
Kumar Gala2a1a2cb2008-08-15 08:24:43 -0500325
Francesco Dolcini622ecee2022-05-19 16:22:26 +0200326 /* add u-boot version */
327 err = fdt_setprop(fdt, nodeoffset, "u-boot,version", PLAIN_VERSION,
328 strlen(PLAIN_VERSION) + 1);
329 if (err < 0) {
330 printf("WARNING: could not set u-boot,version %s.\n",
331 fdt_strerror(err));
332 return err;
333 }
334
Masahiro Yamada972f2a82014-04-18 17:41:01 +0900335 return fdt_fixup_stdout(fdt, nodeoffset);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400336}
337
Kumar Galae93becf2007-11-03 19:46:28 -0500338void do_fixup_by_path(void *fdt, const char *path, const char *prop,
339 const void *val, int len, int create)
340{
341#if defined(DEBUG)
342 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600343 debug("Updating property '%s/%s' = ", path, prop);
Kumar Galae93becf2007-11-03 19:46:28 -0500344 for (i = 0; i < len; i++)
345 debug(" %.2x", *(u8*)(val+i));
346 debug("\n");
347#endif
348 int rc = fdt_find_and_setprop(fdt, path, prop, val, len, create);
349 if (rc)
350 printf("Unable to update property %s:%s, err=%s\n",
351 path, prop, fdt_strerror(rc));
352}
353
354void do_fixup_by_path_u32(void *fdt, const char *path, const char *prop,
355 u32 val, int create)
356{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000357 fdt32_t tmp = cpu_to_fdt32(val);
358 do_fixup_by_path(fdt, path, prop, &tmp, sizeof(tmp), create);
Kumar Galae93becf2007-11-03 19:46:28 -0500359}
360
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600361void do_fixup_by_prop(void *fdt,
362 const char *pname, const void *pval, int plen,
363 const char *prop, const void *val, int len,
364 int create)
365{
366 int off;
367#if defined(DEBUG)
368 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600369 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600370 for (i = 0; i < len; i++)
371 debug(" %.2x", *(u8*)(val+i));
372 debug("\n");
373#endif
374 off = fdt_node_offset_by_prop_value(fdt, -1, pname, pval, plen);
375 while (off != -FDT_ERR_NOTFOUND) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000376 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600377 fdt_setprop(fdt, off, prop, val, len);
378 off = fdt_node_offset_by_prop_value(fdt, off, pname, pval, plen);
379 }
380}
381
382void do_fixup_by_prop_u32(void *fdt,
383 const char *pname, const void *pval, int plen,
384 const char *prop, u32 val, int create)
385{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000386 fdt32_t tmp = cpu_to_fdt32(val);
387 do_fixup_by_prop(fdt, pname, pval, plen, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600388}
389
390void do_fixup_by_compat(void *fdt, const char *compat,
391 const char *prop, const void *val, int len, int create)
392{
393 int off = -1;
394#if defined(DEBUG)
395 int i;
Kumar Galad9ad1152008-02-13 15:09:58 -0600396 debug("Updating property '%s' = ", prop);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600397 for (i = 0; i < len; i++)
398 debug(" %.2x", *(u8*)(val+i));
399 debug("\n");
400#endif
Marek Behún3058e282022-01-20 01:04:42 +0100401 fdt_for_each_node_by_compatible(off, fdt, -1, compat)
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000402 if (create || (fdt_get_property(fdt, off, prop, NULL) != NULL))
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600403 fdt_setprop(fdt, off, prop, val, len);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600404}
405
406void do_fixup_by_compat_u32(void *fdt, const char *compat,
407 const char *prop, u32 val, int create)
408{
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000409 fdt32_t tmp = cpu_to_fdt32(val);
410 do_fixup_by_compat(fdt, compat, prop, &tmp, 4, create);
Kumar Gala9eb77ce2007-11-21 13:30:15 -0600411}
412
Masahiro Yamada63c09412016-11-26 11:02:10 +0900413#ifdef CONFIG_ARCH_FIXUP_FDT_MEMORY
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900414/*
415 * fdt_pack_reg - pack address and size array into the "reg"-suitable stream
416 */
Simon Glass41f09bb2014-10-23 18:58:55 -0600417static int fdt_pack_reg(const void *fdt, void *buf, u64 *address, u64 *size,
418 int n)
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900419{
420 int i;
Hans de Goedeffccb842014-11-28 14:23:51 +0100421 int address_cells = fdt_address_cells(fdt, 0);
422 int size_cells = fdt_size_cells(fdt, 0);
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900423 char *p = buf;
424
425 for (i = 0; i < n; i++) {
Hans de Goedeffccb842014-11-28 14:23:51 +0100426 if (address_cells == 2)
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900427 *(fdt64_t *)p = cpu_to_fdt64(address[i]);
428 else
429 *(fdt32_t *)p = cpu_to_fdt32(address[i]);
Hans de Goedeffccb842014-11-28 14:23:51 +0100430 p += 4 * address_cells;
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900431
Hans de Goedeffccb842014-11-28 14:23:51 +0100432 if (size_cells == 2)
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900433 *(fdt64_t *)p = cpu_to_fdt64(size[i]);
434 else
435 *(fdt32_t *)p = cpu_to_fdt32(size[i]);
Hans de Goedeffccb842014-11-28 14:23:51 +0100436 p += 4 * size_cells;
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900437 }
438
439 return p - (char *)buf;
440}
441
Ramon Fried6b6f2162018-08-14 00:35:42 +0300442#if CONFIG_NR_DRAM_BANKS > 4
443#define MEMORY_BANKS_MAX CONFIG_NR_DRAM_BANKS
444#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000445#define MEMORY_BANKS_MAX 4
Ramon Fried6b6f2162018-08-14 00:35:42 +0300446#endif
Michal Simek5e1a3be2021-08-10 09:21:54 +0200447
448/**
449 * fdt_fixup_memory_banks - Update DT memory node
450 * @blob: Pointer to DT blob
451 * @start: Pointer to memory start addresses array
452 * @size: Pointer to memory sizes array
453 * @banks: Number of memory banks
454 *
455 * Return: 0 on success, negative value on failure
456 *
457 * Based on the passed number of banks and arrays, the function is able to
458 * update existing DT memory nodes to match run time detected/changed memory
459 * configuration. Implementation is handling one specific case with only one
460 * memory node where multiple tuples could be added/updated.
461 * The case where multiple memory nodes with a single tuple (base, size) are
462 * used, this function is only updating the first memory node without removing
463 * others.
464 */
John Rigbya6bd9e82010-10-13 13:57:33 -0600465int fdt_fixup_memory_banks(void *blob, u64 start[], u64 size[], int banks)
466{
467 int err, nodeoffset;
Thierry Reding6d29cc72018-01-30 11:34:17 +0100468 int len, i;
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000469 u8 tmp[MEMORY_BANKS_MAX * 16]; /* Up to 64-bit address + 64-bit size */
Kumar Gala3c927282007-11-26 14:57:45 -0600470
Kim Phillips8aa5ec62013-01-16 14:00:11 +0000471 if (banks > MEMORY_BANKS_MAX) {
472 printf("%s: num banks %d exceeds hardcoded limit %d."
473 " Recompile with higher MEMORY_BANKS_MAX?\n",
474 __FUNCTION__, banks, MEMORY_BANKS_MAX);
475 return -1;
476 }
477
Kumar Gala3c927282007-11-26 14:57:45 -0600478 err = fdt_check_header(blob);
479 if (err < 0) {
480 printf("%s: %s\n", __FUNCTION__, fdt_strerror(err));
481 return err;
482 }
483
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900484 /* find or create "/memory" node. */
485 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
486 if (nodeoffset < 0)
Miao Yan35940de2013-11-28 17:51:39 +0800487 return nodeoffset;
Masahiro Yamada8edb2192014-04-18 17:40:58 +0900488
Kumar Gala3c927282007-11-26 14:57:45 -0600489 err = fdt_setprop(blob, nodeoffset, "device_type", "memory",
490 sizeof("memory"));
491 if (err < 0) {
492 printf("WARNING: could not set %s %s.\n", "device_type",
493 fdt_strerror(err));
494 return err;
495 }
496
Thierry Redinged5af032018-02-15 19:05:59 +0100497 for (i = 0; i < banks; i++) {
498 if (start[i] == 0 && size[i] == 0)
499 break;
500 }
501
502 banks = i;
503
Andre Przywara5c1cf892015-06-21 00:29:54 +0100504 if (!banks)
505 return 0;
506
Masahiro Yamada739a01e2014-04-18 17:41:03 +0900507 len = fdt_pack_reg(blob, tmp, start, size, banks);
Kumar Gala3c927282007-11-26 14:57:45 -0600508
509 err = fdt_setprop(blob, nodeoffset, "reg", tmp, len);
510 if (err < 0) {
511 printf("WARNING: could not set %s %s.\n",
512 "reg", fdt_strerror(err));
513 return err;
514 }
515 return 0;
516}
Igor Opaniuk949b5a92019-12-03 14:04:46 +0200517
518int fdt_set_usable_memory(void *blob, u64 start[], u64 size[], int areas)
519{
520 int err, nodeoffset;
521 int len;
522 u8 tmp[8 * 16]; /* Up to 64-bit address + 64-bit size */
523
524 if (areas > 8) {
525 printf("%s: num areas %d exceeds hardcoded limit %d\n",
526 __func__, areas, 8);
527 return -1;
528 }
529
530 err = fdt_check_header(blob);
531 if (err < 0) {
532 printf("%s: %s\n", __func__, fdt_strerror(err));
533 return err;
534 }
535
536 /* find or create "/memory" node. */
537 nodeoffset = fdt_find_or_add_subnode(blob, 0, "memory");
538 if (nodeoffset < 0)
539 return nodeoffset;
540
541 len = fdt_pack_reg(blob, tmp, start, size, areas);
542
543 err = fdt_setprop(blob, nodeoffset, "linux,usable-memory", tmp, len);
544 if (err < 0) {
545 printf("WARNING: could not set %s %s.\n",
546 "reg", fdt_strerror(err));
547 return err;
548 }
549
550 return 0;
551}
Masahiro Yamada63c09412016-11-26 11:02:10 +0900552#endif
Kumar Gala3c927282007-11-26 14:57:45 -0600553
John Rigbya6bd9e82010-10-13 13:57:33 -0600554int fdt_fixup_memory(void *blob, u64 start, u64 size)
555{
556 return fdt_fixup_memory_banks(blob, &start, &size, 1);
557}
558
Kumar Galaba37aa02008-08-19 15:41:18 -0500559void fdt_fixup_ethernet(void *fdt)
Kumar Galaab544632007-11-21 11:11:03 -0600560{
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530561 int i = 0, j, prop;
Bin Mengbc393a72015-11-03 04:24:41 -0800562 char *tmp, *end;
Stephen Warren064d55f2013-05-27 18:01:19 +0000563 char mac[16];
Kumar Galaab544632007-11-21 11:11:03 -0600564 const char *path;
oliver@schinagl.nla40db6d2016-11-25 16:30:19 +0100565 unsigned char mac_addr[ARP_HLEN];
Bin Mengbc393a72015-11-03 04:24:41 -0800566 int offset;
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530567#ifdef FDT_SEQ_MACADDR_FROM_ENV
568 int nodeoff;
569 const struct fdt_property *fdt_prop;
570#endif
Kumar Galaab544632007-11-21 11:11:03 -0600571
Lev Iserovicha434fd12016-01-07 18:04:16 -0500572 if (fdt_path_offset(fdt, "/aliases") < 0)
Kumar Galaba37aa02008-08-19 15:41:18 -0500573 return;
574
Lev Iserovicha434fd12016-01-07 18:04:16 -0500575 /* Cycle through all aliases */
576 for (prop = 0; ; prop++) {
Bin Mengbc393a72015-11-03 04:24:41 -0800577 const char *name;
Bin Mengbc393a72015-11-03 04:24:41 -0800578
Lev Iserovicha434fd12016-01-07 18:04:16 -0500579 /* FDT might have been edited, recompute the offset */
580 offset = fdt_first_property_offset(fdt,
581 fdt_path_offset(fdt, "/aliases"));
582 /* Select property number 'prop' */
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530583 for (j = 0; j < prop; j++)
Lev Iserovicha434fd12016-01-07 18:04:16 -0500584 offset = fdt_next_property_offset(fdt, offset);
585
586 if (offset < 0)
587 break;
588
Bin Mengbc393a72015-11-03 04:24:41 -0800589 path = fdt_getprop_by_offset(fdt, offset, &name, NULL);
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200590 if (!strncmp(name, "ethernet", 8)) {
591 /* Treat plain "ethernet" same as "ethernet0". */
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530592 if (!strcmp(name, "ethernet")
593#ifdef FDT_SEQ_MACADDR_FROM_ENV
594 || !strcmp(name, "ethernet0")
595#endif
596 )
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200597 i = 0;
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530598#ifndef FDT_SEQ_MACADDR_FROM_ENV
Tuomas Tynkkynenf8e57c62017-03-20 10:04:55 +0200599 else
600 i = trailing_strtol(name);
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530601#endif
Bin Mengbc393a72015-11-03 04:24:41 -0800602 if (i != -1) {
603 if (i == 0)
604 strcpy(mac, "ethaddr");
605 else
606 sprintf(mac, "eth%daddr", i);
607 } else {
608 continue;
609 }
Prabhakar Kushwaha24acb832017-11-23 16:51:32 +0530610#ifdef FDT_SEQ_MACADDR_FROM_ENV
611 nodeoff = fdt_path_offset(fdt, path);
612 fdt_prop = fdt_get_property(fdt, nodeoff, "status",
613 NULL);
614 if (fdt_prop && !strcmp(fdt_prop->data, "disabled"))
615 continue;
616 i++;
617#endif
Simon Glass00caae62017-08-03 12:22:12 -0600618 tmp = env_get(mac);
Bin Mengbc393a72015-11-03 04:24:41 -0800619 if (!tmp)
620 continue;
621
622 for (j = 0; j < 6; j++) {
623 mac_addr[j] = tmp ?
Simon Glass7e5f4602021-07-24 09:03:29 -0600624 hextoul(tmp, &end) : 0;
Bin Mengbc393a72015-11-03 04:24:41 -0800625 if (tmp)
626 tmp = (*end) ? end + 1 : end;
627 }
628
629 do_fixup_by_path(fdt, path, "mac-address",
630 &mac_addr, 6, 0);
631 do_fixup_by_path(fdt, path, "local-mac-address",
632 &mac_addr, 6, 1);
Dan Murphyb1f49ab2013-10-02 14:00:15 -0500633 }
Kumar Galaab544632007-11-21 11:11:03 -0600634 }
635}
Anton Vorontsov18e69a32008-03-14 23:20:18 +0300636
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100637int fdt_record_loadable(void *blob, u32 index, const char *name,
638 uintptr_t load_addr, u32 size, uintptr_t entry_point,
Michal Simekbe2d1a82021-05-27 11:40:09 +0200639 const char *type, const char *os, const char *arch)
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100640{
641 int err, node;
642
643 err = fdt_check_header(blob);
644 if (err < 0) {
645 printf("%s: %s\n", __func__, fdt_strerror(err));
646 return err;
647 }
648
649 /* find or create "/fit-images" node */
650 node = fdt_find_or_add_subnode(blob, 0, "fit-images");
651 if (node < 0)
652 return node;
653
654 /* find or create "/fit-images/<name>" node */
655 node = fdt_find_or_add_subnode(blob, node, name);
656 if (node < 0)
657 return node;
658
Michal Simek13d1ca82020-09-03 12:44:51 +0200659 fdt_setprop_u64(blob, node, "load", load_addr);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100660 if (entry_point != -1)
Michal Simek13d1ca82020-09-03 12:44:51 +0200661 fdt_setprop_u64(blob, node, "entry", entry_point);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100662 fdt_setprop_u32(blob, node, "size", size);
663 if (type)
664 fdt_setprop_string(blob, node, "type", type);
665 if (os)
666 fdt_setprop_string(blob, node, "os", os);
Michal Simekbe2d1a82021-05-27 11:40:09 +0200667 if (arch)
668 fdt_setprop_string(blob, node, "arch", arch);
Philipp Tomsicha5af51a2018-02-02 12:01:44 +0100669
670 return node;
671}
672
Kumar Gala3082d232008-08-15 08:24:42 -0500673/* Resize the fdt to its actual size + a bit of padding */
Hannes Schmelzeref476832016-09-20 18:10:43 +0200674int fdt_shrink_to_minimum(void *blob, uint extrasize)
Kumar Gala3082d232008-08-15 08:24:42 -0500675{
676 int i;
677 uint64_t addr, size;
678 int total, ret;
679 uint actualsize;
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200680 int fdt_memrsv = 0;
Kumar Gala3082d232008-08-15 08:24:42 -0500681
682 if (!blob)
683 return 0;
684
685 total = fdt_num_mem_rsv(blob);
686 for (i = 0; i < total; i++) {
687 fdt_get_mem_rsv(blob, i, &addr, &size);
Simon Glass92549352011-09-17 06:48:58 +0000688 if (addr == (uintptr_t)blob) {
Kumar Gala3082d232008-08-15 08:24:42 -0500689 fdt_del_mem_rsv(blob, i);
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200690 fdt_memrsv = 1;
Kumar Gala3082d232008-08-15 08:24:42 -0500691 break;
692 }
693 }
694
Peter Korsgaardf242a082008-10-28 08:26:52 +0100695 /*
696 * Calculate the actual size of the fdt
Feng Wang3840ebf2010-08-03 16:22:43 +0200697 * plus the size needed for 5 fdt_add_mem_rsv, one
698 * for the fdt itself and 4 for a possible initrd
699 * ((initrd-start + initrd-end) * 2 (name & value))
Peter Korsgaardf242a082008-10-28 08:26:52 +0100700 */
Kumar Gala3082d232008-08-15 08:24:42 -0500701 actualsize = fdt_off_dt_strings(blob) +
Feng Wang3840ebf2010-08-03 16:22:43 +0200702 fdt_size_dt_strings(blob) + 5 * sizeof(struct fdt_reserve_entry);
Kumar Gala3082d232008-08-15 08:24:42 -0500703
Hannes Schmelzeref476832016-09-20 18:10:43 +0200704 actualsize += extrasize;
Kumar Gala3082d232008-08-15 08:24:42 -0500705 /* Make it so the fdt ends on a page boundary */
Simon Glass92549352011-09-17 06:48:58 +0000706 actualsize = ALIGN(actualsize + ((uintptr_t)blob & 0xfff), 0x1000);
707 actualsize = actualsize - ((uintptr_t)blob & 0xfff);
Kumar Gala3082d232008-08-15 08:24:42 -0500708
709 /* Change the fdt header to reflect the correct size */
710 fdt_set_totalsize(blob, actualsize);
711
Simon Goldschmidt59bd7962019-05-03 21:19:03 +0200712 if (fdt_memrsv) {
713 /* Add the new reservation */
714 ret = fdt_add_mem_rsv(blob, map_to_sysmem(blob), actualsize);
715 if (ret < 0)
716 return ret;
717 }
Kumar Gala3082d232008-08-15 08:24:42 -0500718
719 return actualsize;
720}
Kumar Gala8ab451c2008-10-22 23:33:56 -0500721
Marek Behún574506c2021-11-26 14:57:15 +0100722/**
723 * fdt_delete_disabled_nodes: Delete all nodes with status == "disabled"
724 *
725 * @blob: ptr to device tree
726 */
727int fdt_delete_disabled_nodes(void *blob)
728{
729 while (1) {
730 int ret, offset;
731
732 offset = fdt_node_offset_by_prop_value(blob, -1, "status",
733 "disabled", 9);
734 if (offset < 0)
735 break;
736
737 ret = fdt_del_node(blob, offset);
738 if (ret < 0)
739 return ret;
740 }
741
742 return 0;
743}
744
Kumar Gala8ab451c2008-10-22 23:33:56 -0500745#ifdef CONFIG_PCI
Kumar Galacfd700b2009-08-05 09:03:54 -0500746#define CONFIG_SYS_PCI_NR_INBOUND_WIN 4
Kumar Gala8ab451c2008-10-22 23:33:56 -0500747
748#define FDT_PCI_PREFETCH (0x40000000)
749#define FDT_PCI_MEM32 (0x02000000)
750#define FDT_PCI_IO (0x01000000)
751#define FDT_PCI_MEM64 (0x03000000)
752
753int fdt_pci_dma_ranges(void *blob, int phb_off, struct pci_controller *hose) {
754
755 int addrcell, sizecell, len, r;
756 u32 *dma_range;
757 /* sized based on pci addr cells, size-cells, & address-cells */
758 u32 dma_ranges[(3 + 2 + 2) * CONFIG_SYS_PCI_NR_INBOUND_WIN];
759
760 addrcell = fdt_getprop_u32_default(blob, "/", "#address-cells", 1);
761 sizecell = fdt_getprop_u32_default(blob, "/", "#size-cells", 1);
762
763 dma_range = &dma_ranges[0];
764 for (r = 0; r < hose->region_count; r++) {
765 u64 bus_start, phys_start, size;
766
Kumar Galaff4e66e2009-02-06 09:49:31 -0600767 /* skip if !PCI_REGION_SYS_MEMORY */
768 if (!(hose->regions[r].flags & PCI_REGION_SYS_MEMORY))
Kumar Gala8ab451c2008-10-22 23:33:56 -0500769 continue;
770
771 bus_start = (u64)hose->regions[r].bus_start;
772 phys_start = (u64)hose->regions[r].phys_start;
773 size = (u64)hose->regions[r].size;
774
775 dma_range[0] = 0;
Kumar Galacfd700b2009-08-05 09:03:54 -0500776 if (size >= 0x100000000ull)
Marek Vasut867aaf62019-07-09 02:49:29 +0200777 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM64);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500778 else
Marek Vasut867aaf62019-07-09 02:49:29 +0200779 dma_range[0] |= cpu_to_fdt32(FDT_PCI_MEM32);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500780 if (hose->regions[r].flags & PCI_REGION_PREFETCH)
Marek Vasut867aaf62019-07-09 02:49:29 +0200781 dma_range[0] |= cpu_to_fdt32(FDT_PCI_PREFETCH);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500782#ifdef CONFIG_SYS_PCI_64BIT
Marek Vasut867aaf62019-07-09 02:49:29 +0200783 dma_range[1] = cpu_to_fdt32(bus_start >> 32);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500784#else
785 dma_range[1] = 0;
786#endif
Marek Vasut867aaf62019-07-09 02:49:29 +0200787 dma_range[2] = cpu_to_fdt32(bus_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500788
789 if (addrcell == 2) {
Marek Vasut867aaf62019-07-09 02:49:29 +0200790 dma_range[3] = cpu_to_fdt32(phys_start >> 32);
791 dma_range[4] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500792 } else {
Marek Vasut867aaf62019-07-09 02:49:29 +0200793 dma_range[3] = cpu_to_fdt32(phys_start & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500794 }
795
796 if (sizecell == 2) {
Marek Vasut867aaf62019-07-09 02:49:29 +0200797 dma_range[3 + addrcell + 0] =
798 cpu_to_fdt32(size >> 32);
799 dma_range[3 + addrcell + 1] =
800 cpu_to_fdt32(size & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500801 } else {
Marek Vasut867aaf62019-07-09 02:49:29 +0200802 dma_range[3 + addrcell + 0] =
803 cpu_to_fdt32(size & 0xffffffff);
Kumar Gala8ab451c2008-10-22 23:33:56 -0500804 }
805
806 dma_range += (3 + addrcell + sizecell);
807 }
808
809 len = dma_range - &dma_ranges[0];
810 if (len)
811 fdt_setprop(blob, phb_off, "dma-ranges", &dma_ranges[0], len*4);
812
813 return 0;
814}
815#endif
Stefan Roese30d45c02009-10-21 11:59:52 +0200816
Matthew McClintockcb2707a2010-10-13 13:39:26 +0200817int fdt_increase_size(void *fdt, int add_len)
818{
819 int newlen;
820
821 newlen = fdt_totalsize(fdt) + add_len;
822
823 /* Open in place with a new len */
824 return fdt_open_into(fdt, fdt, newlen);
825}
826
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100827#ifdef CONFIG_FDT_FIXUP_PARTITIONS
828#include <jffs2/load_kernel.h>
829#include <mtd_node.h>
830
Michal Simekcd1457d2018-07-31 14:31:04 +0200831static int fdt_del_subnodes(const void *blob, int parent_offset)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100832{
833 int off, ndepth;
834 int ret;
835
836 for (ndepth = 0, off = fdt_next_node(blob, parent_offset, &ndepth);
837 (off >= 0) && (ndepth > 0);
838 off = fdt_next_node(blob, off, &ndepth)) {
839 if (ndepth == 1) {
840 debug("delete %s: offset: %x\n",
841 fdt_get_name(blob, off, 0), off);
842 ret = fdt_del_node((void *)blob, off);
843 if (ret < 0) {
844 printf("Can't delete node: %s\n",
845 fdt_strerror(ret));
846 return ret;
847 } else {
848 ndepth = 0;
849 off = parent_offset;
850 }
851 }
852 }
853 return 0;
854}
855
Michal Simekcd1457d2018-07-31 14:31:04 +0200856static int fdt_del_partitions(void *blob, int parent_offset)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100857{
858 const void *prop;
859 int ndepth = 0;
860 int off;
861 int ret;
862
863 off = fdt_next_node(blob, parent_offset, &ndepth);
864 if (off > 0 && ndepth == 1) {
865 prop = fdt_getprop(blob, off, "label", NULL);
866 if (prop == NULL) {
867 /*
868 * Could not find label property, nand {}; node?
869 * Check subnode, delete partitions there if any.
870 */
871 return fdt_del_partitions(blob, off);
872 } else {
873 ret = fdt_del_subnodes(blob, parent_offset);
874 if (ret < 0) {
875 printf("Can't remove subnodes: %s\n",
876 fdt_strerror(ret));
877 return ret;
878 }
879 }
880 }
881 return 0;
882}
883
Masahiro Yamada8ce8e422020-07-15 19:35:47 +0900884static int fdt_node_set_part_info(void *blob, int parent_offset,
885 struct mtd_device *dev)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100886{
887 struct list_head *pentry;
888 struct part_info *part;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100889 int off, ndepth = 0;
890 int part_num, ret;
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +0300891 int sizecell;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100892 char buf[64];
893
894 ret = fdt_del_partitions(blob, parent_offset);
895 if (ret < 0)
896 return ret;
897
898 /*
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +0300899 * Check if size/address is 1 or 2 cells.
900 * We assume #address-cells and #size-cells have same value.
901 */
902 sizecell = fdt_getprop_u32_default_node(blob, parent_offset,
903 0, "#size-cells", 1);
904
905 /*
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100906 * Check if it is nand {}; subnode, adjust
907 * the offset in this case
908 */
909 off = fdt_next_node(blob, parent_offset, &ndepth);
910 if (off > 0 && ndepth == 1)
911 parent_offset = off;
912
913 part_num = 0;
914 list_for_each_prev(pentry, &dev->parts) {
915 int newoff;
916
917 part = list_entry(pentry, struct part_info, link);
918
Scott Wood06503f12013-10-15 17:41:27 -0500919 debug("%2d: %-20s0x%08llx\t0x%08llx\t%d\n",
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100920 part_num, part->name, part->size,
921 part->offset, part->mask_flags);
922
Scott Wood06503f12013-10-15 17:41:27 -0500923 sprintf(buf, "partition@%llx", part->offset);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100924add_sub:
925 ret = fdt_add_subnode(blob, parent_offset, buf);
926 if (ret == -FDT_ERR_NOSPACE) {
927 ret = fdt_increase_size(blob, 512);
928 if (!ret)
929 goto add_sub;
930 else
931 goto err_size;
932 } else if (ret < 0) {
933 printf("Can't add partition node: %s\n",
934 fdt_strerror(ret));
935 return ret;
936 }
937 newoff = ret;
938
939 /* Check MTD_WRITEABLE_CMD flag */
940 if (part->mask_flags & 1) {
941add_ro:
942 ret = fdt_setprop(blob, newoff, "read_only", NULL, 0);
943 if (ret == -FDT_ERR_NOSPACE) {
944 ret = fdt_increase_size(blob, 512);
945 if (!ret)
946 goto add_ro;
947 else
948 goto err_size;
949 } else if (ret < 0)
950 goto err_prop;
951 }
952
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100953add_reg:
Stefan Mavrodiev3a9a62a2019-04-24 08:31:54 +0300954 if (sizecell == 2) {
955 ret = fdt_setprop_u64(blob, newoff,
956 "reg", part->offset);
957 if (!ret)
958 ret = fdt_appendprop_u64(blob, newoff,
959 "reg", part->size);
960 } else {
961 ret = fdt_setprop_u32(blob, newoff,
962 "reg", part->offset);
963 if (!ret)
964 ret = fdt_appendprop_u32(blob, newoff,
965 "reg", part->size);
966 }
967
Anatolij Gustschin3c950e22010-03-16 17:10:05 +0100968 if (ret == -FDT_ERR_NOSPACE) {
969 ret = fdt_increase_size(blob, 512);
970 if (!ret)
971 goto add_reg;
972 else
973 goto err_size;
974 } else if (ret < 0)
975 goto err_prop;
976
977add_label:
978 ret = fdt_setprop_string(blob, newoff, "label", part->name);
979 if (ret == -FDT_ERR_NOSPACE) {
980 ret = fdt_increase_size(blob, 512);
981 if (!ret)
982 goto add_label;
983 else
984 goto err_size;
985 } else if (ret < 0)
986 goto err_prop;
987
988 part_num++;
989 }
990 return 0;
991err_size:
992 printf("Can't increase blob size: %s\n", fdt_strerror(ret));
993 return ret;
994err_prop:
995 printf("Can't add property: %s\n", fdt_strerror(ret));
996 return ret;
997}
998
999/*
1000 * Update partitions in nor/nand nodes using info from
1001 * mtdparts environment variable. The nodes to update are
1002 * specified by node_info structure which contains mtd device
1003 * type and compatible string: E. g. the board code in
1004 * ft_board_setup() could use:
1005 *
1006 * struct node_info nodes[] = {
1007 * { "fsl,mpc5121-nfc", MTD_DEV_TYPE_NAND, },
1008 * { "cfi-flash", MTD_DEV_TYPE_NOR, },
1009 * };
1010 *
1011 * fdt_fixup_mtdparts(blob, nodes, ARRAY_SIZE(nodes));
1012 */
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001013void fdt_fixup_mtdparts(void *blob, const struct node_info *node_info,
1014 int node_info_size)
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001015{
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001016 struct mtd_device *dev;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001017 int i, idx;
Matthias Schiffer36fee2f2022-02-03 15:14:47 +01001018 int noff, parts;
Masahiro Yamada53a89662020-07-17 10:46:18 +09001019 bool inited = false;
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001020
1021 for (i = 0; i < node_info_size; i++) {
1022 idx = 0;
Masahiro Yamada7d8073e2020-07-17 10:46:19 +09001023
Marek Behún3058e282022-01-20 01:04:42 +01001024 fdt_for_each_node_by_compatible(noff, blob, -1,
1025 node_info[i].compat) {
Masahiro Yamada7d8073e2020-07-17 10:46:19 +09001026 const char *prop;
1027
1028 prop = fdt_getprop(blob, noff, "status", NULL);
1029 if (prop && !strcmp(prop, "disabled"))
1030 continue;
1031
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001032 debug("%s: %s, mtd dev type %d\n",
1033 fdt_get_name(blob, noff, 0),
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001034 node_info[i].compat, node_info[i].type);
Masahiro Yamada53a89662020-07-17 10:46:18 +09001035
1036 if (!inited) {
1037 if (mtdparts_init() != 0)
1038 return;
1039 inited = true;
1040 }
1041
Masahiro Yamada5f4e32d2018-07-19 16:28:22 +09001042 dev = device_find(node_info[i].type, idx++);
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001043 if (dev) {
Matthias Schiffer36fee2f2022-02-03 15:14:47 +01001044 parts = fdt_subnode_offset(blob, noff,
1045 "partitions");
1046 if (parts < 0)
1047 parts = noff;
1048
1049 if (fdt_node_set_part_info(blob, parts, dev))
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001050 return; /* return on error */
1051 }
Anatolij Gustschin3c950e22010-03-16 17:10:05 +01001052 }
1053 }
1054}
1055#endif
Kumar Gala49b97d92010-03-30 10:19:26 -05001056
1057void fdt_del_node_and_alias(void *blob, const char *alias)
1058{
1059 int off = fdt_path_offset(blob, alias);
1060
1061 if (off < 0)
1062 return;
1063
1064 fdt_del_node(blob, off);
1065
1066 off = fdt_path_offset(blob, "/aliases");
1067 fdt_delprop(blob, off, alias);
1068}
Kumar Galaa0342c02010-06-16 14:27:38 -05001069
Kumar Galaa0342c02010-06-16 14:27:38 -05001070/* Max address size we deal with */
1071#define OF_MAX_ADDR_CELLS 4
Bin Menga0ae3802015-12-07 01:39:47 -08001072#define OF_BAD_ADDR FDT_ADDR_T_NONE
Dario Binacchia47abd72021-05-01 17:05:26 +02001073#define OF_CHECK_COUNTS(na, ns) ((na) > 0 && (na) <= OF_MAX_ADDR_CELLS && \
1074 (ns) > 0)
Kumar Galaa0342c02010-06-16 14:27:38 -05001075
1076/* Debug utility */
1077#ifdef DEBUG
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001078static void of_dump_addr(const char *s, const fdt32_t *addr, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -05001079{
1080 printf("%s", s);
1081 while(na--)
1082 printf(" %08x", *(addr++));
1083 printf("\n");
1084}
1085#else
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001086static void of_dump_addr(const char *s, const fdt32_t *addr, int na) { }
Kumar Galaa0342c02010-06-16 14:27:38 -05001087#endif
1088
Paul Burton0a222d52016-05-17 07:43:24 +01001089/**
1090 * struct of_bus - Callbacks for bus specific translators
Paul Burton49717b12016-05-17 07:43:25 +01001091 * @name: A string used to identify this bus in debug output.
1092 * @addresses: The name of the DT property from which addresses are
1093 * to be read, typically "reg".
Paul Burton0a222d52016-05-17 07:43:24 +01001094 * @match: Return non-zero if the node whose parent is at
1095 * parentoffset in the FDT blob corresponds to a bus
1096 * of this type, otherwise return zero. If NULL a match
1097 * is assumed.
Paul Burton49717b12016-05-17 07:43:25 +01001098 * @count_cells:Count how many cells (be32 values) a node whose parent
1099 * is at parentoffset in the FDT blob will require to
1100 * represent its address (written to *addrc) & size
1101 * (written to *sizec).
1102 * @map: Map the address addr from the address space of this
1103 * bus to that of its parent, making use of the ranges
1104 * read from DT to an array at range. na and ns are the
1105 * number of cells (be32 values) used to hold and address
1106 * or size, respectively, for this bus. pna is the number
1107 * of cells used to hold an address for the parent bus.
1108 * Returns the address in the address space of the parent
1109 * bus.
1110 * @translate: Update the value of the address cells at addr within an
1111 * FDT by adding offset to it. na specifies the number of
1112 * cells used to hold the address being translated. Returns
1113 * zero on success, non-zero on error.
Paul Burton0a222d52016-05-17 07:43:24 +01001114 *
1115 * Each bus type will include a struct of_bus in the of_busses array,
1116 * providing implementations of some or all of the functions used to
1117 * match the bus & handle address translation for its children.
1118 */
Kumar Galaa0342c02010-06-16 14:27:38 -05001119struct of_bus {
1120 const char *name;
1121 const char *addresses;
Stephen Warren11e44fc2016-08-05 09:47:50 -06001122 int (*match)(const void *blob, int parentoffset);
1123 void (*count_cells)(const void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -05001124 int *addrc, int *sizec);
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001125 u64 (*map)(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -05001126 int na, int ns, int pna);
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001127 int (*translate)(fdt32_t *addr, u64 offset, int na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001128};
1129
1130/* Default translator (generic bus) */
Simon Glasseed36602017-05-18 20:09:26 -06001131void fdt_support_default_count_cells(const void *blob, int parentoffset,
Kumar Galaa0342c02010-06-16 14:27:38 -05001132 int *addrc, int *sizec)
1133{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001134 const fdt32_t *prop;
Scott Wood6395f312010-08-12 18:37:39 -05001135
Simon Glass933cdbb2014-10-23 18:58:57 -06001136 if (addrc)
1137 *addrc = fdt_address_cells(blob, parentoffset);
Scott Wood6395f312010-08-12 18:37:39 -05001138
1139 if (sizec) {
1140 prop = fdt_getprop(blob, parentoffset, "#size-cells", NULL);
1141 if (prop)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001142 *sizec = be32_to_cpup(prop);
Scott Wood6395f312010-08-12 18:37:39 -05001143 else
1144 *sizec = 1;
1145 }
Kumar Galaa0342c02010-06-16 14:27:38 -05001146}
1147
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001148static u64 of_bus_default_map(fdt32_t *addr, const fdt32_t *range,
Kumar Galaa0342c02010-06-16 14:27:38 -05001149 int na, int ns, int pna)
1150{
1151 u64 cp, s, da;
1152
Simon Glasseed36602017-05-18 20:09:26 -06001153 cp = fdt_read_number(range, na);
1154 s = fdt_read_number(range + na + pna, ns);
1155 da = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001156
Sekhar Norid8e9cf42018-12-06 15:20:47 +05301157 debug("OF: default map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Kumar Galaa0342c02010-06-16 14:27:38 -05001158
1159 if (da < cp || da >= (cp + s))
1160 return OF_BAD_ADDR;
1161 return da - cp;
1162}
1163
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001164static int of_bus_default_translate(fdt32_t *addr, u64 offset, int na)
Kumar Galaa0342c02010-06-16 14:27:38 -05001165{
Simon Glasseed36602017-05-18 20:09:26 -06001166 u64 a = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001167 memset(addr, 0, na * 4);
1168 a += offset;
1169 if (na > 1)
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001170 addr[na - 2] = cpu_to_fdt32(a >> 32);
1171 addr[na - 1] = cpu_to_fdt32(a & 0xffffffffu);
Kumar Galaa0342c02010-06-16 14:27:38 -05001172
1173 return 0;
1174}
1175
Paul Burton0a222d52016-05-17 07:43:24 +01001176#ifdef CONFIG_OF_ISA_BUS
1177
1178/* ISA bus translator */
Stephen Warren11e44fc2016-08-05 09:47:50 -06001179static int of_bus_isa_match(const void *blob, int parentoffset)
Paul Burton0a222d52016-05-17 07:43:24 +01001180{
1181 const char *name;
1182
1183 name = fdt_get_name(blob, parentoffset, NULL);
1184 if (!name)
1185 return 0;
1186
1187 return !strcmp(name, "isa");
1188}
1189
Stephen Warren11e44fc2016-08-05 09:47:50 -06001190static void of_bus_isa_count_cells(const void *blob, int parentoffset,
Paul Burton0a222d52016-05-17 07:43:24 +01001191 int *addrc, int *sizec)
1192{
1193 if (addrc)
1194 *addrc = 2;
1195 if (sizec)
1196 *sizec = 1;
1197}
1198
1199static u64 of_bus_isa_map(fdt32_t *addr, const fdt32_t *range,
1200 int na, int ns, int pna)
1201{
1202 u64 cp, s, da;
1203
1204 /* Check address type match */
1205 if ((addr[0] ^ range[0]) & cpu_to_be32(1))
1206 return OF_BAD_ADDR;
1207
Simon Glasseed36602017-05-18 20:09:26 -06001208 cp = fdt_read_number(range + 1, na - 1);
1209 s = fdt_read_number(range + na + pna, ns);
1210 da = fdt_read_number(addr + 1, na - 1);
Paul Burton0a222d52016-05-17 07:43:24 +01001211
Sekhar Norid8e9cf42018-12-06 15:20:47 +05301212 debug("OF: ISA map, cp=%llx, s=%llx, da=%llx\n", cp, s, da);
Paul Burton0a222d52016-05-17 07:43:24 +01001213
1214 if (da < cp || da >= (cp + s))
1215 return OF_BAD_ADDR;
1216 return da - cp;
1217}
1218
1219static int of_bus_isa_translate(fdt32_t *addr, u64 offset, int na)
1220{
1221 return of_bus_default_translate(addr + 1, offset, na - 1);
1222}
1223
1224#endif /* CONFIG_OF_ISA_BUS */
1225
Kumar Galaa0342c02010-06-16 14:27:38 -05001226/* Array of bus specific translators */
1227static struct of_bus of_busses[] = {
Paul Burton0a222d52016-05-17 07:43:24 +01001228#ifdef CONFIG_OF_ISA_BUS
1229 /* ISA */
1230 {
1231 .name = "isa",
1232 .addresses = "reg",
1233 .match = of_bus_isa_match,
1234 .count_cells = of_bus_isa_count_cells,
1235 .map = of_bus_isa_map,
1236 .translate = of_bus_isa_translate,
1237 },
1238#endif /* CONFIG_OF_ISA_BUS */
Kumar Galaa0342c02010-06-16 14:27:38 -05001239 /* Default */
1240 {
1241 .name = "default",
1242 .addresses = "reg",
Simon Glasseed36602017-05-18 20:09:26 -06001243 .count_cells = fdt_support_default_count_cells,
Kumar Galaa0342c02010-06-16 14:27:38 -05001244 .map = of_bus_default_map,
1245 .translate = of_bus_default_translate,
1246 },
1247};
1248
Stephen Warren11e44fc2016-08-05 09:47:50 -06001249static struct of_bus *of_match_bus(const void *blob, int parentoffset)
Paul Burton0a222d52016-05-17 07:43:24 +01001250{
1251 struct of_bus *bus;
1252
1253 if (ARRAY_SIZE(of_busses) == 1)
1254 return of_busses;
1255
1256 for (bus = of_busses; bus; bus++) {
1257 if (!bus->match || bus->match(blob, parentoffset))
1258 return bus;
1259 }
1260
1261 /*
1262 * We should always have matched the default bus at least, since
1263 * it has a NULL match field. If we didn't then it somehow isn't
1264 * in the of_busses array or something equally catastrophic has
1265 * gone wrong.
1266 */
1267 assert(0);
1268 return NULL;
1269}
1270
Stephen Warren11e44fc2016-08-05 09:47:50 -06001271static int of_translate_one(const void *blob, int parent, struct of_bus *bus,
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001272 struct of_bus *pbus, fdt32_t *addr,
Kumar Galaa0342c02010-06-16 14:27:38 -05001273 int na, int ns, int pna, const char *rprop)
1274{
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001275 const fdt32_t *ranges;
Kumar Galaa0342c02010-06-16 14:27:38 -05001276 int rlen;
1277 int rone;
1278 u64 offset = OF_BAD_ADDR;
1279
1280 /* Normally, an absence of a "ranges" property means we are
1281 * crossing a non-translatable boundary, and thus the addresses
1282 * below the current not cannot be converted to CPU physical ones.
1283 * Unfortunately, while this is very clear in the spec, it's not
1284 * what Apple understood, and they do have things like /uni-n or
1285 * /ht nodes with no "ranges" property and a lot of perfectly
1286 * useable mapped devices below them. Thus we treat the absence of
1287 * "ranges" as equivalent to an empty "ranges" property which means
1288 * a 1:1 translation at that level. It's up to the caller not to try
1289 * to translate addresses that aren't supposed to be translated in
1290 * the first place. --BenH.
1291 */
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001292 ranges = fdt_getprop(blob, parent, rprop, &rlen);
Kumar Galaa0342c02010-06-16 14:27:38 -05001293 if (ranges == NULL || rlen == 0) {
Simon Glasseed36602017-05-18 20:09:26 -06001294 offset = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001295 memset(addr, 0, pna * 4);
1296 debug("OF: no ranges, 1:1 translation\n");
1297 goto finish;
1298 }
1299
1300 debug("OF: walking ranges...\n");
1301
1302 /* Now walk through the ranges */
1303 rlen /= 4;
1304 rone = na + pna + ns;
1305 for (; rlen >= rone; rlen -= rone, ranges += rone) {
1306 offset = bus->map(addr, ranges, na, ns, pna);
1307 if (offset != OF_BAD_ADDR)
1308 break;
1309 }
1310 if (offset == OF_BAD_ADDR) {
1311 debug("OF: not found !\n");
1312 return 1;
1313 }
1314 memcpy(addr, ranges + na, 4 * pna);
1315
1316 finish:
1317 of_dump_addr("OF: parent translation for:", addr, pna);
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001318 debug("OF: with offset: %llu\n", offset);
Kumar Galaa0342c02010-06-16 14:27:38 -05001319
1320 /* Translate it into parent bus space */
1321 return pbus->translate(addr, offset, pna);
1322}
1323
1324/*
1325 * Translate an address from the device-tree into a CPU physical address,
1326 * this walks up the tree and applies the various bus mappings on the
1327 * way.
1328 *
1329 * Note: We consider that crossing any level with #size-cells == 0 to mean
1330 * that translation is impossible (that is we are not dealing with a value
1331 * that can be mapped to a cpu physical address). This is not really specified
1332 * that way, but this is traditionally the way IBM at least do things
1333 */
Stephen Warren11e44fc2016-08-05 09:47:50 -06001334static u64 __of_translate_address(const void *blob, int node_offset,
1335 const fdt32_t *in_addr, const char *rprop)
Kumar Galaa0342c02010-06-16 14:27:38 -05001336{
1337 int parent;
1338 struct of_bus *bus, *pbus;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001339 fdt32_t addr[OF_MAX_ADDR_CELLS];
Kumar Galaa0342c02010-06-16 14:27:38 -05001340 int na, ns, pna, pns;
1341 u64 result = OF_BAD_ADDR;
1342
1343 debug("OF: ** translation for device %s **\n",
1344 fdt_get_name(blob, node_offset, NULL));
1345
1346 /* Get parent & match bus type */
1347 parent = fdt_parent_offset(blob, node_offset);
1348 if (parent < 0)
1349 goto bail;
Paul Burton0a222d52016-05-17 07:43:24 +01001350 bus = of_match_bus(blob, parent);
Kumar Galaa0342c02010-06-16 14:27:38 -05001351
1352 /* Cound address cells & copy address locally */
Scott Wood6395f312010-08-12 18:37:39 -05001353 bus->count_cells(blob, parent, &na, &ns);
Przemyslaw Marczak4428f3c2016-01-12 15:40:44 +01001354 if (!OF_CHECK_COUNTS(na, ns)) {
Kumar Galaa0342c02010-06-16 14:27:38 -05001355 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1356 fdt_get_name(blob, node_offset, NULL));
1357 goto bail;
1358 }
1359 memcpy(addr, in_addr, na * 4);
1360
1361 debug("OF: bus is %s (na=%d, ns=%d) on %s\n",
1362 bus->name, na, ns, fdt_get_name(blob, parent, NULL));
1363 of_dump_addr("OF: translating address:", addr, na);
1364
1365 /* Translate */
1366 for (;;) {
1367 /* Switch to parent bus */
1368 node_offset = parent;
1369 parent = fdt_parent_offset(blob, node_offset);
1370
1371 /* If root, we have finished */
1372 if (parent < 0) {
1373 debug("OF: reached root node\n");
Simon Glasseed36602017-05-18 20:09:26 -06001374 result = fdt_read_number(addr, na);
Kumar Galaa0342c02010-06-16 14:27:38 -05001375 break;
1376 }
1377
1378 /* Get new parent bus and counts */
Paul Burton0a222d52016-05-17 07:43:24 +01001379 pbus = of_match_bus(blob, parent);
Scott Wood6395f312010-08-12 18:37:39 -05001380 pbus->count_cells(blob, parent, &pna, &pns);
Przemyslaw Marczak4428f3c2016-01-12 15:40:44 +01001381 if (!OF_CHECK_COUNTS(pna, pns)) {
Kumar Galaa0342c02010-06-16 14:27:38 -05001382 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1383 fdt_get_name(blob, node_offset, NULL));
1384 break;
1385 }
1386
1387 debug("OF: parent bus is %s (na=%d, ns=%d) on %s\n",
1388 pbus->name, pna, pns, fdt_get_name(blob, parent, NULL));
1389
1390 /* Apply bus translation */
1391 if (of_translate_one(blob, node_offset, bus, pbus,
1392 addr, na, ns, pna, rprop))
1393 break;
1394
1395 /* Complete the move up one level */
1396 na = pna;
1397 ns = pns;
1398 bus = pbus;
1399
1400 of_dump_addr("OF: one level translation:", addr, na);
1401 }
1402 bail:
1403
1404 return result;
1405}
1406
Stephen Warren11e44fc2016-08-05 09:47:50 -06001407u64 fdt_translate_address(const void *blob, int node_offset,
1408 const fdt32_t *in_addr)
Kumar Galaa0342c02010-06-16 14:27:38 -05001409{
1410 return __of_translate_address(blob, node_offset, in_addr, "ranges");
1411}
Kumar Gala75e73af2010-07-04 12:48:21 -05001412
Fabien Dessenne641067f2019-05-31 15:11:30 +02001413u64 fdt_translate_dma_address(const void *blob, int node_offset,
1414 const fdt32_t *in_addr)
1415{
1416 return __of_translate_address(blob, node_offset, in_addr, "dma-ranges");
1417}
1418
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001419int fdt_get_dma_range(const void *blob, int node, phys_addr_t *cpu,
1420 dma_addr_t *bus, u64 *size)
1421{
1422 bool found_dma_ranges = false;
1423 struct of_bus *bus_node;
1424 const fdt32_t *ranges;
1425 int na, ns, pna, pns;
1426 int parent = node;
1427 int ret = 0;
1428 int len;
1429
1430 /* Find the closest dma-ranges property */
1431 while (parent >= 0) {
1432 ranges = fdt_getprop(blob, parent, "dma-ranges", &len);
1433
1434 /* Ignore empty ranges, they imply no translation required */
1435 if (ranges && len > 0)
1436 break;
1437
1438 /* Once we find 'dma-ranges', then a missing one is an error */
1439 if (found_dma_ranges && !ranges) {
1440 ret = -EINVAL;
1441 goto out;
1442 }
1443
1444 if (ranges)
1445 found_dma_ranges = true;
1446
1447 parent = fdt_parent_offset(blob, parent);
1448 }
1449
1450 if (!ranges || parent < 0) {
1451 debug("no dma-ranges found for node %s\n",
1452 fdt_get_name(blob, node, NULL));
1453 ret = -ENOENT;
1454 goto out;
1455 }
1456
1457 /* switch to that node */
1458 node = parent;
1459 parent = fdt_parent_offset(blob, node);
1460 if (parent < 0) {
Vagrant Cascadian8c8bf4f2021-12-21 13:06:58 -08001461 printf("Found dma-ranges in root node, shouldn't happen\n");
Nicolas Saenz Julienne51bdb502021-01-12 13:55:22 +01001462 ret = -EINVAL;
1463 goto out;
1464 }
1465
1466 /* Get the address sizes both for the bus and its parent */
1467 bus_node = of_match_bus(blob, node);
1468 bus_node->count_cells(blob, node, &na, &ns);
1469 if (!OF_CHECK_COUNTS(na, ns)) {
1470 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1471 fdt_get_name(blob, node, NULL));
1472 return -EINVAL;
1473 goto out;
1474 }
1475
1476 bus_node = of_match_bus(blob, parent);
1477 bus_node->count_cells(blob, parent, &pna, &pns);
1478 if (!OF_CHECK_COUNTS(pna, pns)) {
1479 printf("%s: Bad cell count for %s\n", __FUNCTION__,
1480 fdt_get_name(blob, parent, NULL));
1481 return -EINVAL;
1482 goto out;
1483 }
1484
1485 *bus = fdt_read_number(ranges, na);
1486 *cpu = fdt_translate_dma_address(blob, node, ranges + na);
1487 *size = fdt_read_number(ranges + na + pna, ns);
1488out:
1489 return ret;
1490}
1491
Kumar Gala75e73af2010-07-04 12:48:21 -05001492/**
1493 * fdt_node_offset_by_compat_reg: Find a node that matches compatiable and
1494 * who's reg property matches a physical cpu address
1495 *
1496 * @blob: ptr to device tree
1497 * @compat: compatiable string to match
1498 * @compat_off: property name
1499 *
1500 */
1501int fdt_node_offset_by_compat_reg(void *blob, const char *compat,
1502 phys_addr_t compat_off)
1503{
Marek Behún3058e282022-01-20 01:04:42 +01001504 int len, off;
1505
1506 fdt_for_each_node_by_compatible(off, blob, -1, compat) {
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001507 const fdt32_t *reg = fdt_getprop(blob, off, "reg", &len);
Marek Behún3058e282022-01-20 01:04:42 +01001508 if (reg && compat_off == fdt_translate_address(blob, off, reg))
1509 return off;
Kumar Gala75e73af2010-07-04 12:48:21 -05001510 }
1511
1512 return -FDT_ERR_NOTFOUND;
1513}
1514
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001515static int vnode_offset_by_pathf(void *blob, const char *fmt, va_list ap)
1516{
1517 char path[512];
1518 int len;
1519
1520 len = vsnprintf(path, sizeof(path), fmt, ap);
1521 if (len < 0 || len + 1 > sizeof(path))
1522 return -FDT_ERR_NOSPACE;
1523
1524 return fdt_path_offset(blob, path);
1525}
1526
1527/**
1528 * fdt_node_offset_by_pathf: Find node offset by sprintf formatted path
1529 *
1530 * @blob: ptr to device tree
1531 * @fmt: path format
1532 * @ap: vsnprintf arguments
1533 */
1534int fdt_node_offset_by_pathf(void *blob, const char *fmt, ...)
1535{
1536 va_list ap;
1537 int res;
1538
1539 va_start(ap, fmt);
1540 res = vnode_offset_by_pathf(blob, fmt, ap);
1541 va_end(ap);
1542
1543 return res;
1544}
1545
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001546/*
Kumar Galaf117c0f2011-08-01 00:23:23 -05001547 * fdt_set_phandle: Create a phandle property for the given node
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001548 *
1549 * @fdt: ptr to device tree
1550 * @nodeoffset: node to update
1551 * @phandle: phandle value to set (must be unique)
Kumar Galaf117c0f2011-08-01 00:23:23 -05001552 */
1553int fdt_set_phandle(void *fdt, int nodeoffset, uint32_t phandle)
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001554{
1555 int ret;
1556
1557#ifdef DEBUG
1558 int off = fdt_node_offset_by_phandle(fdt, phandle);
1559
1560 if ((off >= 0) && (off != nodeoffset)) {
1561 char buf[64];
1562
1563 fdt_get_path(fdt, nodeoffset, buf, sizeof(buf));
1564 printf("Trying to update node %s with phandle %u ",
1565 buf, phandle);
1566
1567 fdt_get_path(fdt, off, buf, sizeof(buf));
1568 printf("that already exists in node %s.\n", buf);
1569 return -FDT_ERR_BADPHANDLE;
1570 }
1571#endif
1572
1573 ret = fdt_setprop_cell(fdt, nodeoffset, "phandle", phandle);
Gerald Van Barena8d2a752011-07-14 21:40:10 -04001574
1575 return ret;
1576}
1577
Kumar Gala10aeabd2011-08-01 00:25:20 -05001578/*
Marek Behúnc92ccba2021-11-26 14:57:09 +01001579 * fdt_create_phandle: Get or create a phandle property for the given node
Kumar Gala10aeabd2011-08-01 00:25:20 -05001580 *
1581 * @fdt: ptr to device tree
1582 * @nodeoffset: node to update
1583 */
Timur Tabi3c927cc2011-09-20 18:24:34 -05001584unsigned int fdt_create_phandle(void *fdt, int nodeoffset)
Kumar Gala10aeabd2011-08-01 00:25:20 -05001585{
1586 /* see if there is a phandle already */
Marek Behún76f5a722021-11-26 14:57:07 +01001587 uint32_t phandle = fdt_get_phandle(fdt, nodeoffset);
Kumar Gala10aeabd2011-08-01 00:25:20 -05001588
1589 /* if we got 0, means no phandle so create one */
1590 if (phandle == 0) {
Timur Tabi3c927cc2011-09-20 18:24:34 -05001591 int ret;
1592
Marek Behún76f5a722021-11-26 14:57:07 +01001593 ret = fdt_generate_phandle(fdt, &phandle);
1594 if (ret < 0) {
1595 printf("Can't generate phandle: %s\n",
1596 fdt_strerror(ret));
1597 return 0;
1598 }
1599
Timur Tabi3c927cc2011-09-20 18:24:34 -05001600 ret = fdt_set_phandle(fdt, nodeoffset, phandle);
1601 if (ret < 0) {
1602 printf("Can't set phandle %u: %s\n", phandle,
1603 fdt_strerror(ret));
1604 return 0;
1605 }
Kumar Gala10aeabd2011-08-01 00:25:20 -05001606 }
1607
1608 return phandle;
1609}
1610
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001611/**
1612 * fdt_create_phandle_by_compatible: Get or create a phandle for first node with
1613 * given compatible
1614 *
1615 * @fdt: ptr to device tree
1616 * @compat: node's compatible string
1617 */
1618unsigned int fdt_create_phandle_by_compatible(void *fdt, const char *compat)
1619{
1620 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1621
1622 if (offset < 0) {
1623 printf("Can't find node with compatible \"%s\": %s\n", compat,
1624 fdt_strerror(offset));
1625 return 0;
1626 }
1627
1628 return fdt_create_phandle(fdt, offset);
1629}
1630
1631/**
1632 * fdt_create_phandle_by_pathf: Get or create a phandle for node given by
1633 * sprintf-formatted path
1634 *
1635 * @fdt: ptr to device tree
1636 * @fmt, ...: path format string and arguments to pass to sprintf
1637 */
1638unsigned int fdt_create_phandle_by_pathf(void *fdt, const char *fmt, ...)
1639{
1640 va_list ap;
1641 int offset;
1642
1643 va_start(ap, fmt);
1644 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1645 va_end(ap);
1646
1647 if (offset < 0) {
1648 printf("Can't find node by given path: %s\n",
1649 fdt_strerror(offset));
1650 return 0;
1651 }
1652
1653 return fdt_create_phandle(fdt, offset);
1654}
1655
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001656/*
1657 * fdt_set_node_status: Set status for the given node
1658 *
1659 * @fdt: ptr to device tree
1660 * @nodeoffset: node to update
Marek Behún2105cd02021-11-26 14:57:08 +01001661 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001662 */
Marek Behún2105cd02021-11-26 14:57:08 +01001663int fdt_set_node_status(void *fdt, int nodeoffset, enum fdt_status status)
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001664{
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001665 int ret = 0;
1666
1667 if (nodeoffset < 0)
1668 return nodeoffset;
1669
1670 switch (status) {
1671 case FDT_STATUS_OKAY:
1672 ret = fdt_setprop_string(fdt, nodeoffset, "status", "okay");
1673 break;
1674 case FDT_STATUS_DISABLED:
1675 ret = fdt_setprop_string(fdt, nodeoffset, "status", "disabled");
1676 break;
1677 case FDT_STATUS_FAIL:
1678 ret = fdt_setprop_string(fdt, nodeoffset, "status", "fail");
1679 break;
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001680 default:
1681 printf("Invalid fdt status: %x\n", status);
1682 ret = -1;
1683 break;
1684 }
1685
1686 return ret;
1687}
1688
1689/*
1690 * fdt_set_status_by_alias: Set status for the given node given an alias
1691 *
1692 * @fdt: ptr to device tree
1693 * @alias: alias of node to update
Marek Behún2105cd02021-11-26 14:57:08 +01001694 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001695 */
1696int fdt_set_status_by_alias(void *fdt, const char* alias,
Marek Behún2105cd02021-11-26 14:57:08 +01001697 enum fdt_status status)
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001698{
1699 int offset = fdt_path_offset(fdt, alias);
1700
Marek Behún2105cd02021-11-26 14:57:08 +01001701 return fdt_set_node_status(fdt, offset, status);
Shengzhou Liu2a523f52011-10-14 16:26:05 +08001702}
1703
Marek Behún9ab0c2f2021-11-26 14:57:10 +01001704/**
1705 * fdt_set_status_by_compatible: Set node status for first node with given
1706 * compatible
1707 *
1708 * @fdt: ptr to device tree
1709 * @compat: node's compatible string
1710 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1711 */
1712int fdt_set_status_by_compatible(void *fdt, const char *compat,
1713 enum fdt_status status)
1714{
1715 int offset = fdt_node_offset_by_compatible(fdt, -1, compat);
1716
1717 if (offset < 0)
1718 return offset;
1719
1720 return fdt_set_node_status(fdt, offset, status);
1721}
1722
1723/**
1724 * fdt_set_status_by_pathf: Set node status for node given by sprintf-formatted
1725 * path
1726 *
1727 * @fdt: ptr to device tree
1728 * @status: FDT_STATUS_OKAY, FDT_STATUS_DISABLED, FDT_STATUS_FAIL
1729 * @fmt, ...: path format string and arguments to pass to sprintf
1730 */
1731int fdt_set_status_by_pathf(void *fdt, enum fdt_status status, const char *fmt,
1732 ...)
1733{
1734 va_list ap;
1735 int offset;
1736
1737 va_start(ap, fmt);
1738 offset = vnode_offset_by_pathf(fdt, fmt, ap);
1739 va_end(ap);
1740
1741 if (offset < 0)
1742 return offset;
1743
1744 return fdt_set_node_status(fdt, offset, status);
1745}
1746
Timur Tabibb682002011-05-03 13:24:07 -05001747/*
1748 * Verify the physical address of device tree node for a given alias
1749 *
1750 * This function locates the device tree node of a given alias, and then
1751 * verifies that the physical address of that device matches the given
1752 * parameter. It displays a message if there is a mismatch.
1753 *
1754 * Returns 1 on success, 0 on failure
1755 */
1756int fdt_verify_alias_address(void *fdt, int anode, const char *alias, u64 addr)
1757{
1758 const char *path;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001759 const fdt32_t *reg;
Timur Tabibb682002011-05-03 13:24:07 -05001760 int node, len;
1761 u64 dt_addr;
1762
1763 path = fdt_getprop(fdt, anode, alias, NULL);
1764 if (!path) {
1765 /* If there's no such alias, then it's not a failure */
1766 return 1;
1767 }
1768
1769 node = fdt_path_offset(fdt, path);
1770 if (node < 0) {
1771 printf("Warning: device tree alias '%s' points to invalid "
1772 "node %s.\n", alias, path);
1773 return 0;
1774 }
1775
1776 reg = fdt_getprop(fdt, node, "reg", &len);
1777 if (!reg) {
1778 printf("Warning: device tree node '%s' has no address.\n",
1779 path);
1780 return 0;
1781 }
1782
1783 dt_addr = fdt_translate_address(fdt, node, reg);
1784 if (addr != dt_addr) {
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001785 printf("Warning: U-Boot configured device %s at address %llu,\n"
1786 "but the device tree has it address %llx.\n",
1787 alias, addr, dt_addr);
Timur Tabibb682002011-05-03 13:24:07 -05001788 return 0;
1789 }
1790
1791 return 1;
1792}
1793
1794/*
1795 * Returns the base address of an SOC or PCI node
1796 */
Simon Glassec002112017-05-18 20:09:00 -06001797u64 fdt_get_base_address(const void *fdt, int node)
Timur Tabibb682002011-05-03 13:24:07 -05001798{
1799 int size;
Kim Phillips8aa5ec62013-01-16 14:00:11 +00001800 const fdt32_t *prop;
Timur Tabibb682002011-05-03 13:24:07 -05001801
Simon Glass336a4482017-07-04 13:31:20 -06001802 prop = fdt_getprop(fdt, node, "reg", &size);
Timur Tabibb682002011-05-03 13:24:07 -05001803
Masahiro Yamadae3665ba2019-06-27 16:39:57 +09001804 return prop ? fdt_translate_address(fdt, node, prop) : OF_BAD_ADDR;
Timur Tabibb682002011-05-03 13:24:07 -05001805}
Alexander Grafc48e6862014-04-11 17:09:41 +02001806
1807/*
Bin Menga932aa32021-02-25 17:22:24 +08001808 * Read a property of size <prop_len>. Currently only supports 1 or 2 cells,
1809 * or 3 cells specially for a PCI address.
Alexander Grafc48e6862014-04-11 17:09:41 +02001810 */
1811static int fdt_read_prop(const fdt32_t *prop, int prop_len, int cell_off,
1812 uint64_t *val, int cells)
1813{
Bin Menga932aa32021-02-25 17:22:24 +08001814 const fdt32_t *prop32;
1815 const unaligned_fdt64_t *prop64;
Alexander Grafc48e6862014-04-11 17:09:41 +02001816
1817 if ((cell_off + cells) > prop_len)
1818 return -FDT_ERR_NOSPACE;
1819
Bin Menga932aa32021-02-25 17:22:24 +08001820 prop32 = &prop[cell_off];
1821
1822 /*
1823 * Special handling for PCI address in PCI bus <ranges>
1824 *
1825 * PCI child address is made up of 3 cells. Advance the cell offset
1826 * by 1 so that the PCI child address can be correctly read.
1827 */
1828 if (cells == 3)
1829 cell_off += 1;
1830 prop64 = (const fdt64_t *)&prop[cell_off];
1831
Alexander Grafc48e6862014-04-11 17:09:41 +02001832 switch (cells) {
1833 case 1:
1834 *val = fdt32_to_cpu(*prop32);
1835 break;
1836 case 2:
Bin Menga932aa32021-02-25 17:22:24 +08001837 case 3:
Alexander Grafc48e6862014-04-11 17:09:41 +02001838 *val = fdt64_to_cpu(*prop64);
1839 break;
1840 default:
1841 return -FDT_ERR_NOSPACE;
1842 }
1843
1844 return 0;
1845}
1846
1847/**
1848 * fdt_read_range - Read a node's n'th range property
1849 *
1850 * @fdt: ptr to device tree
1851 * @node: offset of node
1852 * @n: range index
1853 * @child_addr: pointer to storage for the "child address" field
1854 * @addr: pointer to storage for the CPU view translated physical start
1855 * @len: pointer to storage for the range length
1856 *
1857 * Convenience function that reads and interprets a specific range out of
1858 * a number of the "ranges" property array.
1859 */
1860int fdt_read_range(void *fdt, int node, int n, uint64_t *child_addr,
1861 uint64_t *addr, uint64_t *len)
1862{
1863 int pnode = fdt_parent_offset(fdt, node);
1864 const fdt32_t *ranges;
1865 int pacells;
1866 int acells;
1867 int scells;
1868 int ranges_len;
1869 int cell = 0;
1870 int r = 0;
1871
1872 /*
1873 * The "ranges" property is an array of
1874 * { <child address> <parent address> <size in child address space> }
1875 *
1876 * All 3 elements can span a diffent number of cells. Fetch their size.
1877 */
1878 pacells = fdt_getprop_u32_default_node(fdt, pnode, 0, "#address-cells", 1);
1879 acells = fdt_getprop_u32_default_node(fdt, node, 0, "#address-cells", 1);
1880 scells = fdt_getprop_u32_default_node(fdt, node, 0, "#size-cells", 1);
1881
1882 /* Now try to get the ranges property */
1883 ranges = fdt_getprop(fdt, node, "ranges", &ranges_len);
1884 if (!ranges)
1885 return -FDT_ERR_NOTFOUND;
1886 ranges_len /= sizeof(uint32_t);
1887
1888 /* Jump to the n'th entry */
1889 cell = n * (pacells + acells + scells);
1890
1891 /* Read <child address> */
1892 if (child_addr) {
1893 r = fdt_read_prop(ranges, ranges_len, cell, child_addr,
1894 acells);
1895 if (r)
1896 return r;
1897 }
1898 cell += acells;
1899
1900 /* Read <parent address> */
1901 if (addr)
1902 *addr = fdt_translate_address(fdt, node, ranges + cell);
1903 cell += pacells;
1904
1905 /* Read <size in child address space> */
1906 if (len) {
1907 r = fdt_read_prop(ranges, ranges_len, cell, len, scells);
1908 if (r)
1909 return r;
1910 }
1911
1912 return 0;
1913}
Hans de Goeded4f495a2014-11-17 15:29:11 +01001914
1915/**
1916 * fdt_setup_simplefb_node - Fill and enable a simplefb node
1917 *
1918 * @fdt: ptr to device tree
1919 * @node: offset of the simplefb node
1920 * @base_address: framebuffer base address
1921 * @width: width in pixels
1922 * @height: height in pixels
1923 * @stride: bytes per line
1924 * @format: pixel format string
1925 *
1926 * Convenience function to fill and enable a simplefb node.
1927 */
1928int fdt_setup_simplefb_node(void *fdt, int node, u64 base_address, u32 width,
1929 u32 height, u32 stride, const char *format)
1930{
1931 char name[32];
1932 fdt32_t cells[4];
1933 int i, addrc, sizec, ret;
1934
Simon Glasseed36602017-05-18 20:09:26 -06001935 fdt_support_default_count_cells(fdt, fdt_parent_offset(fdt, node),
1936 &addrc, &sizec);
Hans de Goeded4f495a2014-11-17 15:29:11 +01001937 i = 0;
1938 if (addrc == 2)
1939 cells[i++] = cpu_to_fdt32(base_address >> 32);
1940 cells[i++] = cpu_to_fdt32(base_address);
1941 if (sizec == 2)
1942 cells[i++] = 0;
1943 cells[i++] = cpu_to_fdt32(height * stride);
1944
1945 ret = fdt_setprop(fdt, node, "reg", cells, sizeof(cells[0]) * i);
1946 if (ret < 0)
1947 return ret;
1948
Masahiro Yamadadee37fc2018-08-06 20:47:40 +09001949 snprintf(name, sizeof(name), "framebuffer@%llx", base_address);
Hans de Goeded4f495a2014-11-17 15:29:11 +01001950 ret = fdt_set_name(fdt, node, name);
1951 if (ret < 0)
1952 return ret;
1953
1954 ret = fdt_setprop_u32(fdt, node, "width", width);
1955 if (ret < 0)
1956 return ret;
1957
1958 ret = fdt_setprop_u32(fdt, node, "height", height);
1959 if (ret < 0)
1960 return ret;
1961
1962 ret = fdt_setprop_u32(fdt, node, "stride", stride);
1963 if (ret < 0)
1964 return ret;
1965
1966 ret = fdt_setprop_string(fdt, node, "format", format);
1967 if (ret < 0)
1968 return ret;
1969
1970 ret = fdt_setprop_string(fdt, node, "status", "okay");
1971 if (ret < 0)
1972 return ret;
1973
1974 return 0;
1975}
Tim Harvey08daa252015-04-08 11:45:39 -07001976
1977/*
1978 * Update native-mode in display-timings from display environment variable.
1979 * The node to update are specified by path.
1980 */
1981int fdt_fixup_display(void *blob, const char *path, const char *display)
1982{
1983 int off, toff;
1984
1985 if (!display || !path)
1986 return -FDT_ERR_NOTFOUND;
1987
1988 toff = fdt_path_offset(blob, path);
1989 if (toff >= 0)
1990 toff = fdt_subnode_offset(blob, toff, "display-timings");
1991 if (toff < 0)
1992 return toff;
1993
1994 for (off = fdt_first_subnode(blob, toff);
1995 off >= 0;
1996 off = fdt_next_subnode(blob, off)) {
1997 uint32_t h = fdt_get_phandle(blob, off);
1998 debug("%s:0x%x\n", fdt_get_name(blob, off, NULL),
1999 fdt32_to_cpu(h));
2000 if (strcasecmp(fdt_get_name(blob, off, NULL), display) == 0)
2001 return fdt_setprop_u32(blob, toff, "native-mode", h);
2002 }
2003 return toff;
2004}
Pantelis Antonioufc7c3182017-09-04 23:12:11 +03002005
2006#ifdef CONFIG_OF_LIBFDT_OVERLAY
2007/**
2008 * fdt_overlay_apply_verbose - Apply an overlay with verbose error reporting
2009 *
2010 * @fdt: ptr to device tree
2011 * @fdto: ptr to device tree overlay
2012 *
2013 * Convenience function to apply an overlay and display helpful messages
2014 * in the case of an error
2015 */
2016int fdt_overlay_apply_verbose(void *fdt, void *fdto)
2017{
2018 int err;
2019 bool has_symbols;
2020
2021 err = fdt_path_offset(fdt, "/__symbols__");
2022 has_symbols = err >= 0;
2023
2024 err = fdt_overlay_apply(fdt, fdto);
2025 if (err < 0) {
2026 printf("failed on fdt_overlay_apply(): %s\n",
2027 fdt_strerror(err));
2028 if (!has_symbols) {
2029 printf("base fdt does did not have a /__symbols__ node\n");
2030 printf("make sure you've compiled with -@\n");
2031 }
2032 }
2033 return err;
2034}
2035#endif
Kory Maincentbbdbcaf2021-05-04 19:31:21 +02002036
2037/**
2038 * fdt_valid() - Check if an FDT is valid. If not, change it to NULL
2039 *
2040 * @blobp: Pointer to FDT pointer
Heinrich Schuchardt185f8122022-01-19 18:05:50 +01002041 * Return: 1 if OK, 0 if bad (in which case *blobp is set to NULL)
Kory Maincentbbdbcaf2021-05-04 19:31:21 +02002042 */
2043int fdt_valid(struct fdt_header **blobp)
2044{
2045 const void *blob = *blobp;
2046 int err;
2047
2048 if (!blob) {
2049 printf("The address of the fdt is invalid (NULL).\n");
2050 return 0;
2051 }
2052
2053 err = fdt_check_header(blob);
2054 if (err == 0)
2055 return 1; /* valid */
2056
2057 if (err < 0) {
2058 printf("libfdt fdt_check_header(): %s", fdt_strerror(err));
2059 /*
2060 * Be more informative on bad version.
2061 */
2062 if (err == -FDT_ERR_BADVERSION) {
2063 if (fdt_version(blob) <
2064 FDT_FIRST_SUPPORTED_VERSION) {
2065 printf(" - too old, fdt %d < %d",
2066 fdt_version(blob),
2067 FDT_FIRST_SUPPORTED_VERSION);
2068 }
2069 if (fdt_last_comp_version(blob) >
2070 FDT_LAST_SUPPORTED_VERSION) {
2071 printf(" - too new, fdt %d > %d",
2072 fdt_version(blob),
2073 FDT_LAST_SUPPORTED_VERSION);
2074 }
2075 }
2076 printf("\n");
2077 *blobp = NULL;
2078 return 0;
2079 }
2080 return 1;
2081}