libfdt.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. #ifndef _LIBFDT_H
  2. #define _LIBFDT_H
  3. /*
  4. * libfdt - Flat Device Tree manipulation
  5. * Copyright (C) 2006 David Gibson, IBM Corporation.
  6. *
  7. * libfdt is dual licensed: you can use it either under the terms of
  8. * the GPL, or the BSD license, at your option.
  9. *
  10. * a) This library is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation; either version 2 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public
  21. * License along with this library; if not, write to the Free
  22. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  23. * MA 02110-1301 USA
  24. *
  25. * Alternatively,
  26. *
  27. * b) Redistribution and use in source and binary forms, with or
  28. * without modification, are permitted provided that the following
  29. * conditions are met:
  30. *
  31. * 1. Redistributions of source code must retain the above
  32. * copyright notice, this list of conditions and the following
  33. * disclaimer.
  34. * 2. Redistributions in binary form must reproduce the above
  35. * copyright notice, this list of conditions and the following
  36. * disclaimer in the documentation and/or other materials
  37. * provided with the distribution.
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  40. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  41. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  42. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  43. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  44. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  49. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  50. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  51. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  52. */
  53. #include <libfdt_env.h>
  54. #include <fdt.h>
  55. #define FDT_FIRST_SUPPORTED_VERSION 0x10
  56. #define FDT_LAST_SUPPORTED_VERSION 0x11
  57. /* Error codes: informative error codes */
  58. #define FDT_ERR_NOTFOUND 1
  59. /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
  60. #define FDT_ERR_EXISTS 2
  61. /* FDT_ERR_EXISTS: Attemped to create a node or property which
  62. * already exists */
  63. #define FDT_ERR_NOSPACE 3
  64. /* FDT_ERR_NOSPACE: Operation needed to expand the device
  65. * tree, but its buffer did not have sufficient space to
  66. * contain the expanded tree. Use fdt_open_into() to move the
  67. * device tree to a buffer with more space. */
  68. /* Error codes: codes for bad parameters */
  69. #define FDT_ERR_BADOFFSET 4
  70. /* FDT_ERR_BADOFFSET: Function was passed a structure block
  71. * offset which is out-of-bounds, or which points to an
  72. * unsuitable part of the structure for the operation. */
  73. #define FDT_ERR_BADPATH 5
  74. /* FDT_ERR_BADPATH: Function was passed a badly formatted path
  75. * (e.g. missing a leading / for a function which requires an
  76. * absolute path) */
  77. #define FDT_ERR_BADPHANDLE 6
  78. /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle
  79. * value. phandle values of 0 and -1 are not permitted. */
  80. #define FDT_ERR_BADSTATE 7
  81. /* FDT_ERR_BADSTATE: Function was passed an incomplete device
  82. * tree created by the sequential-write functions, which is
  83. * not sufficiently complete for the requested operation. */
  84. /* Error codes: codes for bad device tree blobs */
  85. #define FDT_ERR_TRUNCATED 8
  86. /* FDT_ERR_TRUNCATED: Structure block of the given device tree
  87. * ends without an FDT_END tag. */
  88. #define FDT_ERR_BADMAGIC 9
  89. /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
  90. * device tree at all - it is missing the flattened device
  91. * tree magic number. */
  92. #define FDT_ERR_BADVERSION 10
  93. /* FDT_ERR_BADVERSION: Given device tree has a version which
  94. * can't be handled by the requested operation. For
  95. * read-write functions, this may mean that fdt_open_into() is
  96. * required to convert the tree to the expected version. */
  97. #define FDT_ERR_BADSTRUCTURE 11
  98. /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
  99. * structure block or other serious error (e.g. misnested
  100. * nodes, or subnodes preceding properties). */
  101. #define FDT_ERR_BADLAYOUT 12
  102. /* FDT_ERR_BADLAYOUT: For read-write functions, the given
  103. * device tree has it's sub-blocks in an order that the
  104. * function can't handle (memory reserve map, then structure,
  105. * then strings). Use fdt_open_into() to reorganize the tree
  106. * into a form suitable for the read-write operations. */
  107. /* "Can't happen" error indicating a bug in libfdt */
  108. #define FDT_ERR_INTERNAL 13
  109. /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
  110. * Should never be returned, if it is, it indicates a bug in
  111. * libfdt itself. */
  112. #define FDT_ERR_MAX 13
  113. /**********************************************************************/
  114. /* Low-level functions (you probably don't need these) */
  115. /**********************************************************************/
  116. const void *fdt_offset_ptr(const void *fdt, int offset, int checklen);
  117. static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  118. {
  119. return (void *)fdt_offset_ptr(fdt, offset, checklen);
  120. }
  121. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
  122. /**********************************************************************/
  123. /* General functions */
  124. /**********************************************************************/
  125. #define fdt_get_header(fdt, field) \
  126. (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
  127. #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
  128. #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
  129. #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
  130. #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
  131. #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
  132. #define fdt_version(fdt) (fdt_get_header(fdt, version))
  133. #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
  134. #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
  135. #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
  136. #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
  137. #define __fdt_set_hdr(name) \
  138. static inline void fdt_set_##name(void *fdt, uint32_t val) \
  139. { \
  140. struct fdt_header *fdth = fdt; \
  141. fdth->name = cpu_to_fdt32(val); \
  142. }
  143. __fdt_set_hdr(magic);
  144. __fdt_set_hdr(totalsize);
  145. __fdt_set_hdr(off_dt_struct);
  146. __fdt_set_hdr(off_dt_strings);
  147. __fdt_set_hdr(off_mem_rsvmap);
  148. __fdt_set_hdr(version);
  149. __fdt_set_hdr(last_comp_version);
  150. __fdt_set_hdr(boot_cpuid_phys);
  151. __fdt_set_hdr(size_dt_strings);
  152. __fdt_set_hdr(size_dt_struct);
  153. #undef __fdt_set_hdr
  154. /**
  155. * fdt_check_header - sanity check a device tree or possible device tree
  156. * @fdt: pointer to data which might be a flattened device tree
  157. *
  158. * fdt_check_header() checks that the given buffer contains what
  159. * appears to be a flattened device tree with sane information in its
  160. * header.
  161. *
  162. * returns:
  163. * 0, if the buffer appears to contain a valid device tree
  164. * -FDT_ERR_BADMAGIC,
  165. * -FDT_ERR_BADVERSION,
  166. * -FDT_ERR_BADSTATE, standard meanings, as above
  167. */
  168. int fdt_check_header(const void *fdt);
  169. /**
  170. * fdt_move - move a device tree around in memory
  171. * @fdt: pointer to the device tree to move
  172. * @buf: pointer to memory where the device is to be moved
  173. * @bufsize: size of the memory space at buf
  174. *
  175. * fdt_move() relocates, if possible, the device tree blob located at
  176. * fdt to the buffer at buf of size bufsize. The buffer may overlap
  177. * with the existing device tree blob at fdt. Therefore,
  178. * fdt_move(fdt, fdt, fdt_totalsize(fdt))
  179. * should always succeed.
  180. *
  181. * returns:
  182. * 0, on success
  183. * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
  184. * -FDT_ERR_BADMAGIC,
  185. * -FDT_ERR_BADVERSION,
  186. * -FDT_ERR_BADSTATE, standard meanings
  187. */
  188. int fdt_move(const void *fdt, void *buf, int bufsize);
  189. /**********************************************************************/
  190. /* Read-only functions */
  191. /**********************************************************************/
  192. /**
  193. * fdt_string - retreive a string from the strings block of a device tree
  194. * @fdt: pointer to the device tree blob
  195. * @stroffset: offset of the string within the strings block (native endian)
  196. *
  197. * fdt_string() retrieves a pointer to a single string from the
  198. * strings block of the device tree blob at fdt.
  199. *
  200. * returns:
  201. * a pointer to the string, on success
  202. * NULL, if stroffset is out of bounds
  203. */
  204. const char *fdt_string(const void *fdt, int stroffset);
  205. /**
  206. * fdt_num_mem_rsv - retreive the number of memory reserve map entries
  207. * @fdt: pointer to the device tree blob
  208. *
  209. * Returns the number of entries in the device tree blob's memory
  210. * reservation map. This does not include the terminating 0,0 entry
  211. * or any other (0,0) entries reserved for expansion.
  212. *
  213. * returns:
  214. * the number of entries
  215. */
  216. int fdt_num_mem_rsv(const void *fdt);
  217. /**
  218. * fdt_get_mem_rsv - retreive one memory reserve map entry
  219. * @fdt: pointer to the device tree blob
  220. * @address, @size: pointers to 64-bit variables
  221. *
  222. * On success, *address and *size will contain the address and size of
  223. * the n-th reserve map entry from the device tree blob, in
  224. * native-endian format.
  225. *
  226. * returns:
  227. * 0, on success
  228. * -FDT_ERR_BADMAGIC,
  229. * -FDT_ERR_BADVERSION,
  230. * -FDT_ERR_BADSTATE, standard meanings
  231. */
  232. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
  233. /**
  234. * fdt_subnode_offset_namelen - find a subnode based on substring
  235. * @fdt: pointer to the device tree blob
  236. * @parentoffset: structure block offset of a node
  237. * @name: name of the subnode to locate
  238. * @namelen: number of characters of name to consider
  239. *
  240. * Identical to fdt_subnode_offset(), but only examine the first
  241. * namelen characters of name for matching the subnode name. This is
  242. * useful for finding subnodes based on a portion of a larger string,
  243. * such as a full path.
  244. */
  245. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  246. const char *name, int namelen);
  247. /**
  248. * fdt_subnode_offset - find a subnode of a given node
  249. * @fdt: pointer to the device tree blob
  250. * @parentoffset: structure block offset of a node
  251. * @name: name of the subnode to locate
  252. *
  253. * fdt_subnode_offset() finds a subnode of the node at structure block
  254. * offset parentoffset with the given name. name may include a unit
  255. * address, in which case fdt_subnode_offset() will find the subnode
  256. * with that unit address, or the unit address may be omitted, in
  257. * which case fdt_subnode_offset() will find an arbitrary subnode
  258. * whose name excluding unit address matches the given name.
  259. *
  260. * returns:
  261. * structure block offset of the requested subnode (>=0), on success
  262. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  263. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
  264. * -FDT_ERR_BADMAGIC,
  265. * -FDT_ERR_BADVERSION,
  266. * -FDT_ERR_BADSTATE,
  267. * -FDT_ERR_BADSTRUCTURE,
  268. * -FDT_ERR_TRUNCATED, standard meanings.
  269. */
  270. int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
  271. /**
  272. * fdt_path_offset - find a tree node by its full path
  273. * @fdt: pointer to the device tree blob
  274. * @path: full path of the node to locate
  275. *
  276. * fdt_path_offset() finds a node of a given path in the device tree.
  277. * Each path component may omit the unit address portion, but the
  278. * results of this are undefined if any such path component is
  279. * ambiguous (that is if there are multiple nodes at the relevant
  280. * level matching the given component, differentiated only by unit
  281. * address).
  282. *
  283. * returns:
  284. * structure block offset of the node with the requested path (>=0), on success
  285. * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
  286. * -FDT_ERR_NOTFOUND, if the requested node does not exist
  287. * -FDT_ERR_BADMAGIC,
  288. * -FDT_ERR_BADVERSION,
  289. * -FDT_ERR_BADSTATE,
  290. * -FDT_ERR_BADSTRUCTURE,
  291. * -FDT_ERR_TRUNCATED, standard meanings.
  292. */
  293. int fdt_path_offset(const void *fdt, const char *path);
  294. /**
  295. * fdt_get_name - retreive the name of a given node
  296. * @fdt: pointer to the device tree blob
  297. * @nodeoffset: structure block offset of the starting node
  298. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  299. *
  300. * fdt_get_name() retrieves the name (including unit address) of the
  301. * device tree node at structure block offset nodeoffset. If lenp is
  302. * non-NULL, the length of this name is also returned, in the integer
  303. * pointed to by lenp.
  304. *
  305. * returns:
  306. * pointer to the node's name, on success
  307. * If lenp is non-NULL, *lenp contains the length of that name (>=0)
  308. * NULL, on error
  309. * if lenp is non-NULL *lenp contains an error code (<0):
  310. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  311. * -FDT_ERR_BADMAGIC,
  312. * -FDT_ERR_BADVERSION,
  313. * -FDT_ERR_BADSTATE, standard meanings
  314. */
  315. const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
  316. /**
  317. * fdt_get_property - find a given property in a given node
  318. * @fdt: pointer to the device tree blob
  319. * @nodeoffset: offset of the node whose property to find
  320. * @name: name of the property to find
  321. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  322. *
  323. * fdt_get_property() retrieves a pointer to the fdt_property
  324. * structure within the device tree blob corresponding to the property
  325. * named 'name' of the node at offset nodeoffset. If lenp is
  326. * non-NULL, the length of the property value also returned, in the
  327. * integer pointed to by lenp.
  328. *
  329. * returns:
  330. * pointer to the structure representing the property
  331. * if lenp is non-NULL, *lenp contains the length of the property
  332. * value (>=0)
  333. * NULL, on error
  334. * if lenp is non-NULL, *lenp contains an error code (<0):
  335. * -FDT_ERR_NOTFOUND, node does not have named property
  336. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  337. * -FDT_ERR_BADMAGIC,
  338. * -FDT_ERR_BADVERSION,
  339. * -FDT_ERR_BADSTATE,
  340. * -FDT_ERR_BADSTRUCTURE,
  341. * -FDT_ERR_TRUNCATED, standard meanings
  342. */
  343. const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
  344. const char *name, int *lenp);
  345. static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
  346. const char *name,
  347. int *lenp)
  348. {
  349. return (struct fdt_property *)fdt_get_property(fdt, nodeoffset,
  350. name, lenp);
  351. }
  352. /**
  353. * fdt_getprop - retrieve the value of a given property
  354. * @fdt: pointer to the device tree blob
  355. * @nodeoffset: offset of the node whose property to find
  356. * @name: name of the property to find
  357. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  358. *
  359. * fdt_getprop() retrieves a pointer to the value of the property
  360. * named 'name' of the node at offset nodeoffset (this will be a
  361. * pointer to within the device blob itself, not a copy of the value).
  362. * If lenp is non-NULL, the length of the property value also
  363. * returned, in the integer pointed to by lenp.
  364. *
  365. * returns:
  366. * pointer to the property's value
  367. * if lenp is non-NULL, *lenp contains the length of the property
  368. * value (>=0)
  369. * NULL, on error
  370. * if lenp is non-NULL, *lenp contains an error code (<0):
  371. * -FDT_ERR_NOTFOUND, node does not have named property
  372. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  373. * -FDT_ERR_BADMAGIC,
  374. * -FDT_ERR_BADVERSION,
  375. * -FDT_ERR_BADSTATE,
  376. * -FDT_ERR_BADSTRUCTURE,
  377. * -FDT_ERR_TRUNCATED, standard meanings
  378. */
  379. const void *fdt_getprop(const void *fdt, int nodeoffset,
  380. const char *name, int *lenp);
  381. static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
  382. const char *name, int *lenp)
  383. {
  384. return (void *)fdt_getprop(fdt, nodeoffset, name, lenp);
  385. }
  386. /**
  387. * fdt_get_phandle - retreive the phandle of a given node
  388. * @fdt: pointer to the device tree blob
  389. * @nodeoffset: structure block offset of the node
  390. *
  391. * fdt_get_phandle() retrieves the phandle of the device tree node at
  392. * structure block offset nodeoffset.
  393. *
  394. * returns:
  395. * the phandle of the node at nodeoffset, on succes (!= 0, != -1)
  396. * 0, if the node has no phandle, or another error occurs
  397. */
  398. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
  399. /**
  400. * fdt_get_path - determine the full path of a node
  401. * @fdt: pointer to the device tree blob
  402. * @nodeoffset: offset of the node whose path to find
  403. * @buf: character buffer to contain the returned path (will be overwritten)
  404. * @buflen: size of the character buffer at buf
  405. *
  406. * fdt_get_path() computes the full path of the node at offset
  407. * nodeoffset, and records that path in the buffer at buf.
  408. *
  409. * NOTE: This function is expensive, as it must scan the device tree
  410. * structure from the start to nodeoffset.
  411. *
  412. * returns:
  413. * 0, on success
  414. * buf contains the absolute path of the node at
  415. * nodeoffset, as a NUL-terminated string.
  416. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  417. * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
  418. * characters and will not fit in the given buffer.
  419. * -FDT_ERR_BADMAGIC,
  420. * -FDT_ERR_BADVERSION,
  421. * -FDT_ERR_BADSTATE,
  422. * -FDT_ERR_BADSTRUCTURE, standard meanings
  423. */
  424. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
  425. /**
  426. * fdt_supernode_atdepth_offset - find a specific ancestor of a node
  427. * @fdt: pointer to the device tree blob
  428. * @nodeoffset: offset of the node whose parent to find
  429. * @supernodedepth: depth of the ancestor to find
  430. * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
  431. *
  432. * fdt_supernode_atdepth_offset() finds an ancestor of the given node
  433. * at a specific depth from the root (where the root itself has depth
  434. * 0, its immediate subnodes depth 1 and so forth). So
  435. * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
  436. * will always return 0, the offset of the root node. If the node at
  437. * nodeoffset has depth D, then:
  438. * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
  439. * will return nodeoffset itself.
  440. *
  441. * NOTE: This function is expensive, as it must scan the device tree
  442. * structure from the start to nodeoffset.
  443. *
  444. * returns:
  445. * structure block offset of the node at node offset's ancestor
  446. * of depth supernodedepth (>=0), on success
  447. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  448. * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
  449. * -FDT_ERR_BADMAGIC,
  450. * -FDT_ERR_BADVERSION,
  451. * -FDT_ERR_BADSTATE,
  452. * -FDT_ERR_BADSTRUCTURE, standard meanings
  453. */
  454. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  455. int supernodedepth, int *nodedepth);
  456. /**
  457. * fdt_node_depth - find the depth of a given node
  458. * @fdt: pointer to the device tree blob
  459. * @nodeoffset: offset of the node whose parent to find
  460. *
  461. * fdt_node_depth() finds the depth of a given node. The root node
  462. * has depth 0, its immediate subnodes depth 1 and so forth.
  463. *
  464. * NOTE: This function is expensive, as it must scan the device tree
  465. * structure from the start to nodeoffset.
  466. *
  467. * returns:
  468. * depth of the node at nodeoffset (>=0), on success
  469. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  470. * -FDT_ERR_BADMAGIC,
  471. * -FDT_ERR_BADVERSION,
  472. * -FDT_ERR_BADSTATE,
  473. * -FDT_ERR_BADSTRUCTURE, standard meanings
  474. */
  475. int fdt_node_depth(const void *fdt, int nodeoffset);
  476. /**
  477. * fdt_parent_offset - find the parent of a given node
  478. * @fdt: pointer to the device tree blob
  479. * @nodeoffset: offset of the node whose parent to find
  480. *
  481. * fdt_parent_offset() locates the parent node of a given node (that
  482. * is, it finds the offset of the node which contains the node at
  483. * nodeoffset as a subnode).
  484. *
  485. * NOTE: This function is expensive, as it must scan the device tree
  486. * structure from the start to nodeoffset, *twice*.
  487. *
  488. * returns:
  489. * stucture block offset of the parent of the node at nodeoffset
  490. * (>=0), on success
  491. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  492. * -FDT_ERR_BADMAGIC,
  493. * -FDT_ERR_BADVERSION,
  494. * -FDT_ERR_BADSTATE,
  495. * -FDT_ERR_BADSTRUCTURE, standard meanings
  496. */
  497. int fdt_parent_offset(const void *fdt, int nodeoffset);
  498. /**
  499. * fdt_node_offset_by_prop_value - find nodes with a given property value
  500. * @fdt: pointer to the device tree blob
  501. * @startoffset: only find nodes after this offset
  502. * @propname: property name to check
  503. * @propval: property value to search for
  504. * @proplen: length of the value in propval
  505. *
  506. * fdt_node_offset_by_prop_value() returns the offset of the first
  507. * node after startoffset, which has a property named propname whose
  508. * value is of length proplen and has value equal to propval; or if
  509. * startoffset is -1, the very first such node in the tree.
  510. *
  511. * To iterate through all nodes matching the criterion, the following
  512. * idiom can be used:
  513. * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
  514. * propval, proplen);
  515. * while (offset != -FDT_ERR_NOTFOUND) {
  516. * // other code here
  517. * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
  518. * propval, proplen);
  519. * }
  520. *
  521. * Note the -1 in the first call to the function, if 0 is used here
  522. * instead, the function will never locate the root node, even if it
  523. * matches the criterion.
  524. *
  525. * returns:
  526. * structure block offset of the located node (>= 0, >startoffset),
  527. * on success
  528. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  529. * tree after startoffset
  530. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  531. * -FDT_ERR_BADMAGIC,
  532. * -FDT_ERR_BADVERSION,
  533. * -FDT_ERR_BADSTATE,
  534. * -FDT_ERR_BADSTRUCTURE, standard meanings
  535. */
  536. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  537. const char *propname,
  538. const void *propval, int proplen);
  539. /**
  540. * fdt_node_offset_by_phandle - find the node with a given phandle
  541. * @fdt: pointer to the device tree blob
  542. * @phandle: phandle value
  543. *
  544. * fdt_node_offset_by_prop_value() returns the offset of the node
  545. * which has the given phandle value. If there is more than one node
  546. * in the tree with the given phandle (an invalid tree), results are
  547. * undefined.
  548. *
  549. * returns:
  550. * structure block offset of the located node (>= 0), on success
  551. * -FDT_ERR_NOTFOUND, no node with that phandle exists
  552. * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
  553. * -FDT_ERR_BADMAGIC,
  554. * -FDT_ERR_BADVERSION,
  555. * -FDT_ERR_BADSTATE,
  556. * -FDT_ERR_BADSTRUCTURE, standard meanings
  557. */
  558. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
  559. /**
  560. * fdt_node_check_compatible: check a node's compatible property
  561. * @fdt: pointer to the device tree blob
  562. * @nodeoffset: offset of a tree node
  563. * @compatible: string to match against
  564. *
  565. *
  566. * fdt_node_check_compatible() returns 0 if the given node contains a
  567. * 'compatible' property with the given string as one of its elements,
  568. * it returns non-zero otherwise, or on error.
  569. *
  570. * returns:
  571. * 0, if the node has a 'compatible' property listing the given string
  572. * 1, if the node has a 'compatible' property, but it does not list
  573. * the given string
  574. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  575. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  576. * -FDT_ERR_BADMAGIC,
  577. * -FDT_ERR_BADVERSION,
  578. * -FDT_ERR_BADSTATE,
  579. * -FDT_ERR_BADSTRUCTURE, standard meanings
  580. */
  581. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  582. const char *compatible);
  583. /**
  584. * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
  585. * @fdt: pointer to the device tree blob
  586. * @startoffset: only find nodes after this offset
  587. * @compatible: 'compatible' string to match against
  588. *
  589. * fdt_node_offset_by_compatible() returns the offset of the first
  590. * node after startoffset, which has a 'compatible' property which
  591. * lists the given compatible string; or if startoffset is -1, the
  592. * very first such node in the tree.
  593. *
  594. * To iterate through all nodes matching the criterion, the following
  595. * idiom can be used:
  596. * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
  597. * while (offset != -FDT_ERR_NOTFOUND) {
  598. * // other code here
  599. * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
  600. * }
  601. *
  602. * Note the -1 in the first call to the function, if 0 is used here
  603. * instead, the function will never locate the root node, even if it
  604. * matches the criterion.
  605. *
  606. * returns:
  607. * structure block offset of the located node (>= 0, >startoffset),
  608. * on success
  609. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  610. * tree after startoffset
  611. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  612. * -FDT_ERR_BADMAGIC,
  613. * -FDT_ERR_BADVERSION,
  614. * -FDT_ERR_BADSTATE,
  615. * -FDT_ERR_BADSTRUCTURE, standard meanings
  616. */
  617. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  618. const char *compatible);
  619. /**********************************************************************/
  620. /* Write-in-place functions */
  621. /**********************************************************************/
  622. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  623. const void *val, int len);
  624. static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
  625. const char *name, uint32_t val)
  626. {
  627. val = cpu_to_fdt32(val);
  628. return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
  629. }
  630. int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  631. int fdt_nop_node(void *fdt, int nodeoffset);
  632. /**********************************************************************/
  633. /* Sequential write functions */
  634. /**********************************************************************/
  635. int fdt_create(void *buf, int bufsize);
  636. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
  637. int fdt_finish_reservemap(void *fdt);
  638. int fdt_begin_node(void *fdt, const char *name);
  639. int fdt_property(void *fdt, const char *name, const void *val, int len);
  640. static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
  641. {
  642. val = cpu_to_fdt32(val);
  643. return fdt_property(fdt, name, &val, sizeof(val));
  644. }
  645. #define fdt_property_string(fdt, name, str) \
  646. fdt_property(fdt, name, str, strlen(str)+1)
  647. int fdt_end_node(void *fdt);
  648. int fdt_finish(void *fdt);
  649. /**********************************************************************/
  650. /* Read-write functions */
  651. /**********************************************************************/
  652. int fdt_open_into(const void *fdt, void *buf, int bufsize);
  653. int fdt_pack(void *fdt);
  654. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
  655. int fdt_del_mem_rsv(void *fdt, int n);
  656. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  657. const void *val, int len);
  658. static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
  659. uint32_t val)
  660. {
  661. val = cpu_to_fdt32(val);
  662. return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
  663. }
  664. #define fdt_setprop_string(fdt, nodeoffset, name, str) \
  665. fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  666. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  667. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  668. const char *name, int namelen);
  669. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  670. int fdt_del_node(void *fdt, int nodeoffset);
  671. /**********************************************************************/
  672. /* Debugging / informational functions */
  673. /**********************************************************************/
  674. const char *fdt_strerror(int errval);
  675. #endif /* _LIBFDT_H */