libfdt.h 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852
  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: Attempted 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. * This can be caused either by an invalid phandle property
  80. * length, or the phandle value was either 0 or -1, which are
  81. * not permitted. */
  82. #define FDT_ERR_BADSTATE 7
  83. /* FDT_ERR_BADSTATE: Function was passed an incomplete device
  84. * tree created by the sequential-write functions, which is
  85. * not sufficiently complete for the requested operation. */
  86. /* Error codes: codes for bad device tree blobs */
  87. #define FDT_ERR_TRUNCATED 8
  88. /* FDT_ERR_TRUNCATED: Structure block of the given device tree
  89. * ends without an FDT_END tag. */
  90. #define FDT_ERR_BADMAGIC 9
  91. /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
  92. * device tree at all - it is missing the flattened device
  93. * tree magic number. */
  94. #define FDT_ERR_BADVERSION 10
  95. /* FDT_ERR_BADVERSION: Given device tree has a version which
  96. * can't be handled by the requested operation. For
  97. * read-write functions, this may mean that fdt_open_into() is
  98. * required to convert the tree to the expected version. */
  99. #define FDT_ERR_BADSTRUCTURE 11
  100. /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
  101. * structure block or other serious error (e.g. misnested
  102. * nodes, or subnodes preceding properties). */
  103. #define FDT_ERR_BADLAYOUT 12
  104. /* FDT_ERR_BADLAYOUT: For read-write functions, the given
  105. * device tree has it's sub-blocks in an order that the
  106. * function can't handle (memory reserve map, then structure,
  107. * then strings). Use fdt_open_into() to reorganize the tree
  108. * into a form suitable for the read-write operations. */
  109. /* "Can't happen" error indicating a bug in libfdt */
  110. #define FDT_ERR_INTERNAL 13
  111. /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
  112. * Should never be returned, if it is, it indicates a bug in
  113. * libfdt itself. */
  114. /* Errors in device tree content */
  115. #define FDT_ERR_BADNCELLS 14
  116. /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
  117. * or similar property with a bad format or value */
  118. #define FDT_ERR_BADVALUE 15
  119. /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
  120. * value. For example: a property expected to contain a string list
  121. * is not NUL-terminated within the length of its value. */
  122. #define FDT_ERR_BADOVERLAY 16
  123. /* FDT_ERR_BADOVERLAY: The device tree overlay, while
  124. * correctly structured, cannot be applied due to some
  125. * unexpected or missing value, property or node. */
  126. #define FDT_ERR_NOPHANDLES 17
  127. /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
  128. * phandle available anymore without causing an overflow */
  129. #define FDT_ERR_MAX 17
  130. /**********************************************************************/
  131. /* Low-level functions (you probably don't need these) */
  132. /**********************************************************************/
  133. #ifndef SWIG /* This function is not useful in Python */
  134. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
  135. #endif
  136. static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  137. {
  138. return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
  139. }
  140. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
  141. /**********************************************************************/
  142. /* Traversal functions */
  143. /**********************************************************************/
  144. int fdt_next_node(const void *fdt, int offset, int *depth);
  145. /**
  146. * fdt_first_subnode() - get offset of first direct subnode
  147. *
  148. * @fdt: FDT blob
  149. * @offset: Offset of node to check
  150. * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
  151. */
  152. int fdt_first_subnode(const void *fdt, int offset);
  153. /**
  154. * fdt_next_subnode() - get offset of next direct subnode
  155. *
  156. * After first calling fdt_first_subnode(), call this function repeatedly to
  157. * get direct subnodes of a parent node.
  158. *
  159. * @fdt: FDT blob
  160. * @offset: Offset of previous subnode
  161. * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
  162. * subnodes
  163. */
  164. int fdt_next_subnode(const void *fdt, int offset);
  165. /**
  166. * fdt_for_each_subnode - iterate over all subnodes of a parent
  167. *
  168. * @node: child node (int, lvalue)
  169. * @fdt: FDT blob (const void *)
  170. * @parent: parent node (int)
  171. *
  172. * This is actually a wrapper around a for loop and would be used like so:
  173. *
  174. * fdt_for_each_subnode(node, fdt, parent) {
  175. * Use node
  176. * ...
  177. * }
  178. *
  179. * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
  180. * Error handling
  181. * }
  182. *
  183. * Note that this is implemented as a macro and @node is used as
  184. * iterator in the loop. The parent variable be constant or even a
  185. * literal.
  186. *
  187. */
  188. #define fdt_for_each_subnode(node, fdt, parent) \
  189. for (node = fdt_first_subnode(fdt, parent); \
  190. node >= 0; \
  191. node = fdt_next_subnode(fdt, node))
  192. /**********************************************************************/
  193. /* General functions */
  194. /**********************************************************************/
  195. #define fdt_get_header(fdt, field) \
  196. (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
  197. #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
  198. #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
  199. #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
  200. #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
  201. #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
  202. #define fdt_version(fdt) (fdt_get_header(fdt, version))
  203. #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
  204. #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
  205. #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
  206. #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
  207. #define __fdt_set_hdr(name) \
  208. static inline void fdt_set_##name(void *fdt, uint32_t val) \
  209. { \
  210. struct fdt_header *fdth = (struct fdt_header *)fdt; \
  211. fdth->name = cpu_to_fdt32(val); \
  212. }
  213. __fdt_set_hdr(magic);
  214. __fdt_set_hdr(totalsize);
  215. __fdt_set_hdr(off_dt_struct);
  216. __fdt_set_hdr(off_dt_strings);
  217. __fdt_set_hdr(off_mem_rsvmap);
  218. __fdt_set_hdr(version);
  219. __fdt_set_hdr(last_comp_version);
  220. __fdt_set_hdr(boot_cpuid_phys);
  221. __fdt_set_hdr(size_dt_strings);
  222. __fdt_set_hdr(size_dt_struct);
  223. #undef __fdt_set_hdr
  224. /**
  225. * fdt_check_header - sanity check a device tree or possible device tree
  226. * @fdt: pointer to data which might be a flattened device tree
  227. *
  228. * fdt_check_header() checks that the given buffer contains what
  229. * appears to be a flattened device tree with sane information in its
  230. * header.
  231. *
  232. * returns:
  233. * 0, if the buffer appears to contain a valid device tree
  234. * -FDT_ERR_BADMAGIC,
  235. * -FDT_ERR_BADVERSION,
  236. * -FDT_ERR_BADSTATE, standard meanings, as above
  237. */
  238. int fdt_check_header(const void *fdt);
  239. /**
  240. * fdt_move - move a device tree around in memory
  241. * @fdt: pointer to the device tree to move
  242. * @buf: pointer to memory where the device is to be moved
  243. * @bufsize: size of the memory space at buf
  244. *
  245. * fdt_move() relocates, if possible, the device tree blob located at
  246. * fdt to the buffer at buf of size bufsize. The buffer may overlap
  247. * with the existing device tree blob at fdt. Therefore,
  248. * fdt_move(fdt, fdt, fdt_totalsize(fdt))
  249. * should always succeed.
  250. *
  251. * returns:
  252. * 0, on success
  253. * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
  254. * -FDT_ERR_BADMAGIC,
  255. * -FDT_ERR_BADVERSION,
  256. * -FDT_ERR_BADSTATE, standard meanings
  257. */
  258. int fdt_move(const void *fdt, void *buf, int bufsize);
  259. /**********************************************************************/
  260. /* Read-only functions */
  261. /**********************************************************************/
  262. /**
  263. * fdt_string - retrieve a string from the strings block of a device tree
  264. * @fdt: pointer to the device tree blob
  265. * @stroffset: offset of the string within the strings block (native endian)
  266. *
  267. * fdt_string() retrieves a pointer to a single string from the
  268. * strings block of the device tree blob at fdt.
  269. *
  270. * returns:
  271. * a pointer to the string, on success
  272. * NULL, if stroffset is out of bounds
  273. */
  274. const char *fdt_string(const void *fdt, int stroffset);
  275. /**
  276. * fdt_get_max_phandle - retrieves the highest phandle in a tree
  277. * @fdt: pointer to the device tree blob
  278. *
  279. * fdt_get_max_phandle retrieves the highest phandle in the given
  280. * device tree. This will ignore badly formatted phandles, or phandles
  281. * with a value of 0 or -1.
  282. *
  283. * returns:
  284. * the highest phandle on success
  285. * 0, if no phandle was found in the device tree
  286. * -1, if an error occurred
  287. */
  288. uint32_t fdt_get_max_phandle(const void *fdt);
  289. /**
  290. * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
  291. * @fdt: pointer to the device tree blob
  292. *
  293. * Returns the number of entries in the device tree blob's memory
  294. * reservation map. This does not include the terminating 0,0 entry
  295. * or any other (0,0) entries reserved for expansion.
  296. *
  297. * returns:
  298. * the number of entries
  299. */
  300. int fdt_num_mem_rsv(const void *fdt);
  301. /**
  302. * fdt_get_mem_rsv - retrieve one memory reserve map entry
  303. * @fdt: pointer to the device tree blob
  304. * @address, @size: pointers to 64-bit variables
  305. *
  306. * On success, *address and *size will contain the address and size of
  307. * the n-th reserve map entry from the device tree blob, in
  308. * native-endian format.
  309. *
  310. * returns:
  311. * 0, on success
  312. * -FDT_ERR_BADMAGIC,
  313. * -FDT_ERR_BADVERSION,
  314. * -FDT_ERR_BADSTATE, standard meanings
  315. */
  316. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
  317. /**
  318. * fdt_subnode_offset_namelen - find a subnode based on substring
  319. * @fdt: pointer to the device tree blob
  320. * @parentoffset: structure block offset of a node
  321. * @name: name of the subnode to locate
  322. * @namelen: number of characters of name to consider
  323. *
  324. * Identical to fdt_subnode_offset(), but only examine the first
  325. * namelen characters of name for matching the subnode name. This is
  326. * useful for finding subnodes based on a portion of a larger string,
  327. * such as a full path.
  328. */
  329. #ifndef SWIG /* Not available in Python */
  330. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  331. const char *name, int namelen);
  332. #endif
  333. /**
  334. * fdt_subnode_offset - find a subnode of a given node
  335. * @fdt: pointer to the device tree blob
  336. * @parentoffset: structure block offset of a node
  337. * @name: name of the subnode to locate
  338. *
  339. * fdt_subnode_offset() finds a subnode of the node at structure block
  340. * offset parentoffset with the given name. name may include a unit
  341. * address, in which case fdt_subnode_offset() will find the subnode
  342. * with that unit address, or the unit address may be omitted, in
  343. * which case fdt_subnode_offset() will find an arbitrary subnode
  344. * whose name excluding unit address matches the given name.
  345. *
  346. * returns:
  347. * structure block offset of the requested subnode (>=0), on success
  348. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  349. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  350. * tag
  351. * -FDT_ERR_BADMAGIC,
  352. * -FDT_ERR_BADVERSION,
  353. * -FDT_ERR_BADSTATE,
  354. * -FDT_ERR_BADSTRUCTURE,
  355. * -FDT_ERR_TRUNCATED, standard meanings.
  356. */
  357. int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
  358. /**
  359. * fdt_path_offset_namelen - find a tree node by its full path
  360. * @fdt: pointer to the device tree blob
  361. * @path: full path of the node to locate
  362. * @namelen: number of characters of path to consider
  363. *
  364. * Identical to fdt_path_offset(), but only consider the first namelen
  365. * characters of path as the path name.
  366. */
  367. #ifndef SWIG /* Not available in Python */
  368. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
  369. #endif
  370. /**
  371. * fdt_path_offset - find a tree node by its full path
  372. * @fdt: pointer to the device tree blob
  373. * @path: full path of the node to locate
  374. *
  375. * fdt_path_offset() finds a node of a given path in the device tree.
  376. * Each path component may omit the unit address portion, but the
  377. * results of this are undefined if any such path component is
  378. * ambiguous (that is if there are multiple nodes at the relevant
  379. * level matching the given component, differentiated only by unit
  380. * address).
  381. *
  382. * returns:
  383. * structure block offset of the node with the requested path (>=0), on
  384. * success
  385. * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
  386. * -FDT_ERR_NOTFOUND, if the requested node does not exist
  387. * -FDT_ERR_BADMAGIC,
  388. * -FDT_ERR_BADVERSION,
  389. * -FDT_ERR_BADSTATE,
  390. * -FDT_ERR_BADSTRUCTURE,
  391. * -FDT_ERR_TRUNCATED, standard meanings.
  392. */
  393. int fdt_path_offset(const void *fdt, const char *path);
  394. /**
  395. * fdt_get_name - retrieve the name of a given node
  396. * @fdt: pointer to the device tree blob
  397. * @nodeoffset: structure block offset of the starting node
  398. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  399. *
  400. * fdt_get_name() retrieves the name (including unit address) of the
  401. * device tree node at structure block offset nodeoffset. If lenp is
  402. * non-NULL, the length of this name is also returned, in the integer
  403. * pointed to by lenp.
  404. *
  405. * returns:
  406. * pointer to the node's name, on success
  407. * If lenp is non-NULL, *lenp contains the length of that name
  408. * (>=0)
  409. * NULL, on error
  410. * if lenp is non-NULL *lenp contains an error code (<0):
  411. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  412. * tag
  413. * -FDT_ERR_BADMAGIC,
  414. * -FDT_ERR_BADVERSION,
  415. * -FDT_ERR_BADSTATE, standard meanings
  416. */
  417. const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
  418. /**
  419. * fdt_first_property_offset - find the offset of a node's first property
  420. * @fdt: pointer to the device tree blob
  421. * @nodeoffset: structure block offset of a node
  422. *
  423. * fdt_first_property_offset() finds the first property of the node at
  424. * the given structure block offset.
  425. *
  426. * returns:
  427. * structure block offset of the property (>=0), on success
  428. * -FDT_ERR_NOTFOUND, if the requested node has no properties
  429. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
  430. * -FDT_ERR_BADMAGIC,
  431. * -FDT_ERR_BADVERSION,
  432. * -FDT_ERR_BADSTATE,
  433. * -FDT_ERR_BADSTRUCTURE,
  434. * -FDT_ERR_TRUNCATED, standard meanings.
  435. */
  436. int fdt_first_property_offset(const void *fdt, int nodeoffset);
  437. /**
  438. * fdt_next_property_offset - step through a node's properties
  439. * @fdt: pointer to the device tree blob
  440. * @offset: structure block offset of a property
  441. *
  442. * fdt_next_property_offset() finds the property immediately after the
  443. * one at the given structure block offset. This will be a property
  444. * of the same node as the given property.
  445. *
  446. * returns:
  447. * structure block offset of the next property (>=0), on success
  448. * -FDT_ERR_NOTFOUND, if the given property is the last in its node
  449. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
  450. * -FDT_ERR_BADMAGIC,
  451. * -FDT_ERR_BADVERSION,
  452. * -FDT_ERR_BADSTATE,
  453. * -FDT_ERR_BADSTRUCTURE,
  454. * -FDT_ERR_TRUNCATED, standard meanings.
  455. */
  456. int fdt_next_property_offset(const void *fdt, int offset);
  457. /**
  458. * fdt_for_each_property_offset - iterate over all properties of a node
  459. *
  460. * @property_offset: property offset (int, lvalue)
  461. * @fdt: FDT blob (const void *)
  462. * @node: node offset (int)
  463. *
  464. * This is actually a wrapper around a for loop and would be used like so:
  465. *
  466. * fdt_for_each_property_offset(property, fdt, node) {
  467. * Use property
  468. * ...
  469. * }
  470. *
  471. * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
  472. * Error handling
  473. * }
  474. *
  475. * Note that this is implemented as a macro and property is used as
  476. * iterator in the loop. The node variable can be constant or even a
  477. * literal.
  478. */
  479. #define fdt_for_each_property_offset(property, fdt, node) \
  480. for (property = fdt_first_property_offset(fdt, node); \
  481. property >= 0; \
  482. property = fdt_next_property_offset(fdt, property))
  483. /**
  484. * fdt_get_property_by_offset - retrieve the property at a given offset
  485. * @fdt: pointer to the device tree blob
  486. * @offset: offset of the property to retrieve
  487. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  488. *
  489. * fdt_get_property_by_offset() retrieves a pointer to the
  490. * fdt_property structure within the device tree blob at the given
  491. * offset. If lenp is non-NULL, the length of the property value is
  492. * also returned, in the integer pointed to by lenp.
  493. *
  494. * returns:
  495. * pointer to the structure representing the property
  496. * if lenp is non-NULL, *lenp contains the length of the property
  497. * value (>=0)
  498. * NULL, on error
  499. * if lenp is non-NULL, *lenp contains an error code (<0):
  500. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  501. * -FDT_ERR_BADMAGIC,
  502. * -FDT_ERR_BADVERSION,
  503. * -FDT_ERR_BADSTATE,
  504. * -FDT_ERR_BADSTRUCTURE,
  505. * -FDT_ERR_TRUNCATED, standard meanings
  506. */
  507. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  508. int offset,
  509. int *lenp);
  510. /**
  511. * fdt_get_property_namelen - find a property based on substring
  512. * @fdt: pointer to the device tree blob
  513. * @nodeoffset: offset of the node whose property to find
  514. * @name: name of the property to find
  515. * @namelen: number of characters of name to consider
  516. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  517. *
  518. * Identical to fdt_get_property(), but only examine the first namelen
  519. * characters of name for matching the property name.
  520. */
  521. #ifndef SWIG /* Not available in Python */
  522. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  523. int nodeoffset,
  524. const char *name,
  525. int namelen, int *lenp);
  526. #endif
  527. /**
  528. * fdt_get_property - find a given property in a given node
  529. * @fdt: pointer to the device tree blob
  530. * @nodeoffset: offset of the node whose property to find
  531. * @name: name of the property to find
  532. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  533. *
  534. * fdt_get_property() retrieves a pointer to the fdt_property
  535. * structure within the device tree blob corresponding to the property
  536. * named 'name' of the node at offset nodeoffset. If lenp is
  537. * non-NULL, the length of the property value is also returned, in the
  538. * integer pointed to by lenp.
  539. *
  540. * returns:
  541. * pointer to the structure representing the property
  542. * if lenp is non-NULL, *lenp contains the length of the property
  543. * value (>=0)
  544. * NULL, on error
  545. * if lenp is non-NULL, *lenp contains an error code (<0):
  546. * -FDT_ERR_NOTFOUND, node does not have named property
  547. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  548. * tag
  549. * -FDT_ERR_BADMAGIC,
  550. * -FDT_ERR_BADVERSION,
  551. * -FDT_ERR_BADSTATE,
  552. * -FDT_ERR_BADSTRUCTURE,
  553. * -FDT_ERR_TRUNCATED, standard meanings
  554. */
  555. const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
  556. const char *name, int *lenp);
  557. static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
  558. const char *name,
  559. int *lenp)
  560. {
  561. return (struct fdt_property *)(uintptr_t)
  562. fdt_get_property(fdt, nodeoffset, name, lenp);
  563. }
  564. /**
  565. * fdt_getprop_by_offset - retrieve the value of a property at a given offset
  566. * @fdt: pointer to the device tree blob
  567. * @ffset: offset of the property to read
  568. * @namep: pointer to a string variable (will be overwritten) or NULL
  569. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  570. *
  571. * fdt_getprop_by_offset() retrieves a pointer to the value of the
  572. * property at structure block offset 'offset' (this will be a pointer
  573. * to within the device blob itself, not a copy of the value). If
  574. * lenp is non-NULL, the length of the property value is also
  575. * returned, in the integer pointed to by lenp. If namep is non-NULL,
  576. * the property's namne will also be returned in the char * pointed to
  577. * by namep (this will be a pointer to within the device tree's string
  578. * block, not a new copy of the name).
  579. *
  580. * returns:
  581. * pointer to the property's value
  582. * if lenp is non-NULL, *lenp contains the length of the property
  583. * value (>=0)
  584. * if namep is non-NULL *namep contiains a pointer to the property
  585. * name.
  586. * NULL, on error
  587. * if lenp is non-NULL, *lenp contains an error code (<0):
  588. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  589. * -FDT_ERR_BADMAGIC,
  590. * -FDT_ERR_BADVERSION,
  591. * -FDT_ERR_BADSTATE,
  592. * -FDT_ERR_BADSTRUCTURE,
  593. * -FDT_ERR_TRUNCATED, standard meanings
  594. */
  595. #ifndef SWIG /* This function is not useful in Python */
  596. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  597. const char **namep, int *lenp);
  598. #endif
  599. /**
  600. * fdt_getprop_namelen - get property value based on substring
  601. * @fdt: pointer to the device tree blob
  602. * @nodeoffset: offset of the node whose property to find
  603. * @name: name of the property to find
  604. * @namelen: number of characters of name to consider
  605. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  606. *
  607. * Identical to fdt_getprop(), but only examine the first namelen
  608. * characters of name for matching the property name.
  609. */
  610. #ifndef SWIG /* Not available in Python */
  611. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  612. const char *name, int namelen, int *lenp);
  613. static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
  614. const char *name, int namelen,
  615. int *lenp)
  616. {
  617. return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
  618. namelen, lenp);
  619. }
  620. #endif
  621. /**
  622. * fdt_getprop - retrieve the value of a given property
  623. * @fdt: pointer to the device tree blob
  624. * @nodeoffset: offset of the node whose property to find
  625. * @name: name of the property to find
  626. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  627. *
  628. * fdt_getprop() retrieves a pointer to the value of the property
  629. * named 'name' of the node at offset nodeoffset (this will be a
  630. * pointer to within the device blob itself, not a copy of the value).
  631. * If lenp is non-NULL, the length of the property value is also
  632. * returned, in the integer pointed to by lenp.
  633. *
  634. * returns:
  635. * pointer to the property's value
  636. * if lenp is non-NULL, *lenp contains the length of the property
  637. * value (>=0)
  638. * NULL, on error
  639. * if lenp is non-NULL, *lenp contains an error code (<0):
  640. * -FDT_ERR_NOTFOUND, node does not have named property
  641. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  642. * tag
  643. * -FDT_ERR_BADMAGIC,
  644. * -FDT_ERR_BADVERSION,
  645. * -FDT_ERR_BADSTATE,
  646. * -FDT_ERR_BADSTRUCTURE,
  647. * -FDT_ERR_TRUNCATED, standard meanings
  648. */
  649. const void *fdt_getprop(const void *fdt, int nodeoffset,
  650. const char *name, int *lenp);
  651. static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
  652. const char *name, int *lenp)
  653. {
  654. return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
  655. }
  656. /**
  657. * fdt_get_phandle - retrieve the phandle of a given node
  658. * @fdt: pointer to the device tree blob
  659. * @nodeoffset: structure block offset of the node
  660. *
  661. * fdt_get_phandle() retrieves the phandle of the device tree node at
  662. * structure block offset nodeoffset.
  663. *
  664. * returns:
  665. * the phandle of the node at nodeoffset, on success (!= 0, != -1)
  666. * 0, if the node has no phandle, or another error occurs
  667. */
  668. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
  669. /**
  670. * fdt_get_alias_namelen - get alias based on substring
  671. * @fdt: pointer to the device tree blob
  672. * @name: name of the alias th look up
  673. * @namelen: number of characters of name to consider
  674. *
  675. * Identical to fdt_get_alias(), but only examine the first namelen
  676. * characters of name for matching the alias name.
  677. */
  678. #ifndef SWIG /* Not available in Python */
  679. const char *fdt_get_alias_namelen(const void *fdt,
  680. const char *name, int namelen);
  681. #endif
  682. /**
  683. * fdt_get_alias - retrieve the path referenced by a given alias
  684. * @fdt: pointer to the device tree blob
  685. * @name: name of the alias th look up
  686. *
  687. * fdt_get_alias() retrieves the value of a given alias. That is, the
  688. * value of the property named 'name' in the node /aliases.
  689. *
  690. * returns:
  691. * a pointer to the expansion of the alias named 'name', if it exists
  692. * NULL, if the given alias or the /aliases node does not exist
  693. */
  694. const char *fdt_get_alias(const void *fdt, const char *name);
  695. /**
  696. * fdt_get_path - determine the full path of a node
  697. * @fdt: pointer to the device tree blob
  698. * @nodeoffset: offset of the node whose path to find
  699. * @buf: character buffer to contain the returned path (will be overwritten)
  700. * @buflen: size of the character buffer at buf
  701. *
  702. * fdt_get_path() computes the full path of the node at offset
  703. * nodeoffset, and records that path in the buffer at buf.
  704. *
  705. * NOTE: This function is expensive, as it must scan the device tree
  706. * structure from the start to nodeoffset.
  707. *
  708. * returns:
  709. * 0, on success
  710. * buf contains the absolute path of the node at
  711. * nodeoffset, as a NUL-terminated string.
  712. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  713. * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
  714. * characters and will not fit in the given buffer.
  715. * -FDT_ERR_BADMAGIC,
  716. * -FDT_ERR_BADVERSION,
  717. * -FDT_ERR_BADSTATE,
  718. * -FDT_ERR_BADSTRUCTURE, standard meanings
  719. */
  720. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
  721. /**
  722. * fdt_supernode_atdepth_offset - find a specific ancestor of a node
  723. * @fdt: pointer to the device tree blob
  724. * @nodeoffset: offset of the node whose parent to find
  725. * @supernodedepth: depth of the ancestor to find
  726. * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
  727. *
  728. * fdt_supernode_atdepth_offset() finds an ancestor of the given node
  729. * at a specific depth from the root (where the root itself has depth
  730. * 0, its immediate subnodes depth 1 and so forth). So
  731. * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
  732. * will always return 0, the offset of the root node. If the node at
  733. * nodeoffset has depth D, then:
  734. * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
  735. * will return nodeoffset itself.
  736. *
  737. * NOTE: This function is expensive, as it must scan the device tree
  738. * structure from the start to nodeoffset.
  739. *
  740. * returns:
  741. * structure block offset of the node at node offset's ancestor
  742. * of depth supernodedepth (>=0), on success
  743. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  744. * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
  745. * nodeoffset
  746. * -FDT_ERR_BADMAGIC,
  747. * -FDT_ERR_BADVERSION,
  748. * -FDT_ERR_BADSTATE,
  749. * -FDT_ERR_BADSTRUCTURE, standard meanings
  750. */
  751. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  752. int supernodedepth, int *nodedepth);
  753. /**
  754. * fdt_node_depth - find the depth of a given node
  755. * @fdt: pointer to the device tree blob
  756. * @nodeoffset: offset of the node whose parent to find
  757. *
  758. * fdt_node_depth() finds the depth of a given node. The root node
  759. * has depth 0, its immediate subnodes depth 1 and so forth.
  760. *
  761. * NOTE: This function is expensive, as it must scan the device tree
  762. * structure from the start to nodeoffset.
  763. *
  764. * returns:
  765. * depth of the node at nodeoffset (>=0), on success
  766. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  767. * -FDT_ERR_BADMAGIC,
  768. * -FDT_ERR_BADVERSION,
  769. * -FDT_ERR_BADSTATE,
  770. * -FDT_ERR_BADSTRUCTURE, standard meanings
  771. */
  772. int fdt_node_depth(const void *fdt, int nodeoffset);
  773. /**
  774. * fdt_parent_offset - find the parent of a given node
  775. * @fdt: pointer to the device tree blob
  776. * @nodeoffset: offset of the node whose parent to find
  777. *
  778. * fdt_parent_offset() locates the parent node of a given node (that
  779. * is, it finds the offset of the node which contains the node at
  780. * nodeoffset as a subnode).
  781. *
  782. * NOTE: This function is expensive, as it must scan the device tree
  783. * structure from the start to nodeoffset, *twice*.
  784. *
  785. * returns:
  786. * structure block offset of the parent of the node at nodeoffset
  787. * (>=0), on success
  788. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  789. * -FDT_ERR_BADMAGIC,
  790. * -FDT_ERR_BADVERSION,
  791. * -FDT_ERR_BADSTATE,
  792. * -FDT_ERR_BADSTRUCTURE, standard meanings
  793. */
  794. int fdt_parent_offset(const void *fdt, int nodeoffset);
  795. /**
  796. * fdt_node_offset_by_prop_value - find nodes with a given property value
  797. * @fdt: pointer to the device tree blob
  798. * @startoffset: only find nodes after this offset
  799. * @propname: property name to check
  800. * @propval: property value to search for
  801. * @proplen: length of the value in propval
  802. *
  803. * fdt_node_offset_by_prop_value() returns the offset of the first
  804. * node after startoffset, which has a property named propname whose
  805. * value is of length proplen and has value equal to propval; or if
  806. * startoffset is -1, the very first such node in the tree.
  807. *
  808. * To iterate through all nodes matching the criterion, the following
  809. * idiom can be used:
  810. * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
  811. * propval, proplen);
  812. * while (offset != -FDT_ERR_NOTFOUND) {
  813. * // other code here
  814. * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
  815. * propval, proplen);
  816. * }
  817. *
  818. * Note the -1 in the first call to the function, if 0 is used here
  819. * instead, the function will never locate the root node, even if it
  820. * matches the criterion.
  821. *
  822. * returns:
  823. * structure block offset of the located node (>= 0, >startoffset),
  824. * on success
  825. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  826. * tree after startoffset
  827. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  828. * -FDT_ERR_BADMAGIC,
  829. * -FDT_ERR_BADVERSION,
  830. * -FDT_ERR_BADSTATE,
  831. * -FDT_ERR_BADSTRUCTURE, standard meanings
  832. */
  833. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  834. const char *propname,
  835. const void *propval, int proplen);
  836. /**
  837. * fdt_node_offset_by_phandle - find the node with a given phandle
  838. * @fdt: pointer to the device tree blob
  839. * @phandle: phandle value
  840. *
  841. * fdt_node_offset_by_phandle() returns the offset of the node
  842. * which has the given phandle value. If there is more than one node
  843. * in the tree with the given phandle (an invalid tree), results are
  844. * undefined.
  845. *
  846. * returns:
  847. * structure block offset of the located node (>= 0), on success
  848. * -FDT_ERR_NOTFOUND, no node with that phandle exists
  849. * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
  850. * -FDT_ERR_BADMAGIC,
  851. * -FDT_ERR_BADVERSION,
  852. * -FDT_ERR_BADSTATE,
  853. * -FDT_ERR_BADSTRUCTURE, standard meanings
  854. */
  855. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
  856. /**
  857. * fdt_node_check_compatible: check a node's compatible property
  858. * @fdt: pointer to the device tree blob
  859. * @nodeoffset: offset of a tree node
  860. * @compatible: string to match against
  861. *
  862. *
  863. * fdt_node_check_compatible() returns 0 if the given node contains a
  864. * 'compatible' property with the given string as one of its elements,
  865. * it returns non-zero otherwise, or on error.
  866. *
  867. * returns:
  868. * 0, if the node has a 'compatible' property listing the given string
  869. * 1, if the node has a 'compatible' property, but it does not list
  870. * the given string
  871. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  872. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  873. * -FDT_ERR_BADMAGIC,
  874. * -FDT_ERR_BADVERSION,
  875. * -FDT_ERR_BADSTATE,
  876. * -FDT_ERR_BADSTRUCTURE, standard meanings
  877. */
  878. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  879. const char *compatible);
  880. /**
  881. * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
  882. * @fdt: pointer to the device tree blob
  883. * @startoffset: only find nodes after this offset
  884. * @compatible: 'compatible' string to match against
  885. *
  886. * fdt_node_offset_by_compatible() returns the offset of the first
  887. * node after startoffset, which has a 'compatible' property which
  888. * lists the given compatible string; or if startoffset is -1, the
  889. * very first such node in the tree.
  890. *
  891. * To iterate through all nodes matching the criterion, the following
  892. * idiom can be used:
  893. * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
  894. * while (offset != -FDT_ERR_NOTFOUND) {
  895. * // other code here
  896. * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
  897. * }
  898. *
  899. * Note the -1 in the first call to the function, if 0 is used here
  900. * instead, the function will never locate the root node, even if it
  901. * matches the criterion.
  902. *
  903. * returns:
  904. * structure block offset of the located node (>= 0, >startoffset),
  905. * on success
  906. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  907. * tree after startoffset
  908. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  909. * -FDT_ERR_BADMAGIC,
  910. * -FDT_ERR_BADVERSION,
  911. * -FDT_ERR_BADSTATE,
  912. * -FDT_ERR_BADSTRUCTURE, standard meanings
  913. */
  914. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  915. const char *compatible);
  916. /**
  917. * fdt_stringlist_contains - check a string list property for a string
  918. * @strlist: Property containing a list of strings to check
  919. * @listlen: Length of property
  920. * @str: String to search for
  921. *
  922. * This is a utility function provided for convenience. The list contains
  923. * one or more strings, each terminated by \0, as is found in a device tree
  924. * "compatible" property.
  925. *
  926. * @return: 1 if the string is found in the list, 0 not found, or invalid list
  927. */
  928. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
  929. /**
  930. * fdt_stringlist_count - count the number of strings in a string list
  931. * @fdt: pointer to the device tree blob
  932. * @nodeoffset: offset of a tree node
  933. * @property: name of the property containing the string list
  934. * @return:
  935. * the number of strings in the given property
  936. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  937. * -FDT_ERR_NOTFOUND if the property does not exist
  938. */
  939. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
  940. /**
  941. * fdt_stringlist_search - find a string in a string list and return its index
  942. * @fdt: pointer to the device tree blob
  943. * @nodeoffset: offset of a tree node
  944. * @property: name of the property containing the string list
  945. * @string: string to look up in the string list
  946. *
  947. * Note that it is possible for this function to succeed on property values
  948. * that are not NUL-terminated. That's because the function will stop after
  949. * finding the first occurrence of @string. This can for example happen with
  950. * small-valued cell properties, such as #address-cells, when searching for
  951. * the empty string.
  952. *
  953. * @return:
  954. * the index of the string in the list of strings
  955. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  956. * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
  957. * the given string
  958. */
  959. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  960. const char *string);
  961. /**
  962. * fdt_stringlist_get() - obtain the string at a given index in a string list
  963. * @fdt: pointer to the device tree blob
  964. * @nodeoffset: offset of a tree node
  965. * @property: name of the property containing the string list
  966. * @index: index of the string to return
  967. * @lenp: return location for the string length or an error code on failure
  968. *
  969. * Note that this will successfully extract strings from properties with
  970. * non-NUL-terminated values. For example on small-valued cell properties
  971. * this function will return the empty string.
  972. *
  973. * If non-NULL, the length of the string (on success) or a negative error-code
  974. * (on failure) will be stored in the integer pointer to by lenp.
  975. *
  976. * @return:
  977. * A pointer to the string at the given index in the string list or NULL on
  978. * failure. On success the length of the string will be stored in the memory
  979. * location pointed to by the lenp parameter, if non-NULL. On failure one of
  980. * the following negative error codes will be returned in the lenp parameter
  981. * (if non-NULL):
  982. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  983. * -FDT_ERR_NOTFOUND if the property does not exist
  984. */
  985. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  986. const char *property, int index,
  987. int *lenp);
  988. /**********************************************************************/
  989. /* Read-only functions (addressing related) */
  990. /**********************************************************************/
  991. /**
  992. * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
  993. *
  994. * This is the maximum value for #address-cells, #size-cells and
  995. * similar properties that will be processed by libfdt. IEE1275
  996. * requires that OF implementations handle values up to 4.
  997. * Implementations may support larger values, but in practice higher
  998. * values aren't used.
  999. */
  1000. #define FDT_MAX_NCELLS 4
  1001. /**
  1002. * fdt_address_cells - retrieve address size for a bus represented in the tree
  1003. * @fdt: pointer to the device tree blob
  1004. * @nodeoffset: offset of the node to find the address size for
  1005. *
  1006. * When the node has a valid #address-cells property, returns its value.
  1007. *
  1008. * returns:
  1009. * 0 <= n < FDT_MAX_NCELLS, on success
  1010. * 2, if the node has no #address-cells property
  1011. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1012. * #address-cells property
  1013. * -FDT_ERR_BADMAGIC,
  1014. * -FDT_ERR_BADVERSION,
  1015. * -FDT_ERR_BADSTATE,
  1016. * -FDT_ERR_BADSTRUCTURE,
  1017. * -FDT_ERR_TRUNCATED, standard meanings
  1018. */
  1019. int fdt_address_cells(const void *fdt, int nodeoffset);
  1020. /**
  1021. * fdt_size_cells - retrieve address range size for a bus represented in the
  1022. * tree
  1023. * @fdt: pointer to the device tree blob
  1024. * @nodeoffset: offset of the node to find the address range size for
  1025. *
  1026. * When the node has a valid #size-cells property, returns its value.
  1027. *
  1028. * returns:
  1029. * 0 <= n < FDT_MAX_NCELLS, on success
  1030. * 2, if the node has no #address-cells property
  1031. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1032. * #size-cells property
  1033. * -FDT_ERR_BADMAGIC,
  1034. * -FDT_ERR_BADVERSION,
  1035. * -FDT_ERR_BADSTATE,
  1036. * -FDT_ERR_BADSTRUCTURE,
  1037. * -FDT_ERR_TRUNCATED, standard meanings
  1038. */
  1039. int fdt_size_cells(const void *fdt, int nodeoffset);
  1040. /**********************************************************************/
  1041. /* Write-in-place functions */
  1042. /**********************************************************************/
  1043. /**
  1044. * fdt_setprop_inplace_namelen_partial - change a property's value,
  1045. * but not its size
  1046. * @fdt: pointer to the device tree blob
  1047. * @nodeoffset: offset of the node whose property to change
  1048. * @name: name of the property to change
  1049. * @namelen: number of characters of name to consider
  1050. * @idx: index of the property to change in the array
  1051. * @val: pointer to data to replace the property value with
  1052. * @len: length of the property value
  1053. *
  1054. * Identical to fdt_setprop_inplace(), but modifies the given property
  1055. * starting from the given index, and using only the first characters
  1056. * of the name. It is useful when you want to manipulate only one value of
  1057. * an array and you have a string that doesn't end with \0.
  1058. */
  1059. #ifndef SWIG /* Not available in Python */
  1060. int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
  1061. const char *name, int namelen,
  1062. uint32_t idx, const void *val,
  1063. int len);
  1064. #endif
  1065. /**
  1066. * fdt_setprop_inplace - change a property's value, but not its size
  1067. * @fdt: pointer to the device tree blob
  1068. * @nodeoffset: offset of the node whose property to change
  1069. * @name: name of the property to change
  1070. * @val: pointer to data to replace the property value with
  1071. * @len: length of the property value
  1072. *
  1073. * fdt_setprop_inplace() replaces the value of a given property with
  1074. * the data in val, of length len. This function cannot change the
  1075. * size of a property, and so will only work if len is equal to the
  1076. * current length of the property.
  1077. *
  1078. * This function will alter only the bytes in the blob which contain
  1079. * the given property value, and will not alter or move any other part
  1080. * of the tree.
  1081. *
  1082. * returns:
  1083. * 0, on success
  1084. * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
  1085. * -FDT_ERR_NOTFOUND, node does not have the named property
  1086. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1087. * -FDT_ERR_BADMAGIC,
  1088. * -FDT_ERR_BADVERSION,
  1089. * -FDT_ERR_BADSTATE,
  1090. * -FDT_ERR_BADSTRUCTURE,
  1091. * -FDT_ERR_TRUNCATED, standard meanings
  1092. */
  1093. #ifndef SWIG /* Not available in Python */
  1094. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  1095. const void *val, int len);
  1096. #endif
  1097. /**
  1098. * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
  1099. * @fdt: pointer to the device tree blob
  1100. * @nodeoffset: offset of the node whose property to change
  1101. * @name: name of the property to change
  1102. * @val: 32-bit integer value to replace the property with
  1103. *
  1104. * fdt_setprop_inplace_u32() replaces the value of a given property
  1105. * with the 32-bit integer value in val, converting val to big-endian
  1106. * if necessary. This function cannot change the size of a property,
  1107. * and so will only work if the property already exists and has length
  1108. * 4.
  1109. *
  1110. * This function will alter only the bytes in the blob which contain
  1111. * the given property value, and will not alter or move any other part
  1112. * of the tree.
  1113. *
  1114. * returns:
  1115. * 0, on success
  1116. * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
  1117. * -FDT_ERR_NOTFOUND, node does not have the named property
  1118. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1119. * -FDT_ERR_BADMAGIC,
  1120. * -FDT_ERR_BADVERSION,
  1121. * -FDT_ERR_BADSTATE,
  1122. * -FDT_ERR_BADSTRUCTURE,
  1123. * -FDT_ERR_TRUNCATED, standard meanings
  1124. */
  1125. static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
  1126. const char *name, uint32_t val)
  1127. {
  1128. fdt32_t tmp = cpu_to_fdt32(val);
  1129. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1130. }
  1131. /**
  1132. * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
  1133. * @fdt: pointer to the device tree blob
  1134. * @nodeoffset: offset of the node whose property to change
  1135. * @name: name of the property to change
  1136. * @val: 64-bit integer value to replace the property with
  1137. *
  1138. * fdt_setprop_inplace_u64() replaces the value of a given property
  1139. * with the 64-bit integer value in val, converting val to big-endian
  1140. * if necessary. This function cannot change the size of a property,
  1141. * and so will only work if the property already exists and has length
  1142. * 8.
  1143. *
  1144. * This function will alter only the bytes in the blob which contain
  1145. * the given property value, and will not alter or move any other part
  1146. * of the tree.
  1147. *
  1148. * returns:
  1149. * 0, on success
  1150. * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
  1151. * -FDT_ERR_NOTFOUND, node does not have the named property
  1152. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1153. * -FDT_ERR_BADMAGIC,
  1154. * -FDT_ERR_BADVERSION,
  1155. * -FDT_ERR_BADSTATE,
  1156. * -FDT_ERR_BADSTRUCTURE,
  1157. * -FDT_ERR_TRUNCATED, standard meanings
  1158. */
  1159. static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
  1160. const char *name, uint64_t val)
  1161. {
  1162. fdt64_t tmp = cpu_to_fdt64(val);
  1163. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1164. }
  1165. /**
  1166. * fdt_setprop_inplace_cell - change the value of a single-cell property
  1167. *
  1168. * This is an alternative name for fdt_setprop_inplace_u32()
  1169. */
  1170. static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
  1171. const char *name, uint32_t val)
  1172. {
  1173. return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
  1174. }
  1175. /**
  1176. * fdt_nop_property - replace a property with nop tags
  1177. * @fdt: pointer to the device tree blob
  1178. * @nodeoffset: offset of the node whose property to nop
  1179. * @name: name of the property to nop
  1180. *
  1181. * fdt_nop_property() will replace a given property's representation
  1182. * in the blob with FDT_NOP tags, effectively removing it from the
  1183. * tree.
  1184. *
  1185. * This function will alter only the bytes in the blob which contain
  1186. * the property, and will not alter or move any other part of the
  1187. * tree.
  1188. *
  1189. * returns:
  1190. * 0, on success
  1191. * -FDT_ERR_NOTFOUND, node does not have the named property
  1192. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1193. * -FDT_ERR_BADMAGIC,
  1194. * -FDT_ERR_BADVERSION,
  1195. * -FDT_ERR_BADSTATE,
  1196. * -FDT_ERR_BADSTRUCTURE,
  1197. * -FDT_ERR_TRUNCATED, standard meanings
  1198. */
  1199. int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  1200. /**
  1201. * fdt_nop_node - replace a node (subtree) with nop tags
  1202. * @fdt: pointer to the device tree blob
  1203. * @nodeoffset: offset of the node to nop
  1204. *
  1205. * fdt_nop_node() will replace a given node's representation in the
  1206. * blob, including all its subnodes, if any, with FDT_NOP tags,
  1207. * effectively removing it from the tree.
  1208. *
  1209. * This function will alter only the bytes in the blob which contain
  1210. * the node and its properties and subnodes, and will not alter or
  1211. * move any other part of the tree.
  1212. *
  1213. * returns:
  1214. * 0, on success
  1215. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1216. * -FDT_ERR_BADMAGIC,
  1217. * -FDT_ERR_BADVERSION,
  1218. * -FDT_ERR_BADSTATE,
  1219. * -FDT_ERR_BADSTRUCTURE,
  1220. * -FDT_ERR_TRUNCATED, standard meanings
  1221. */
  1222. int fdt_nop_node(void *fdt, int nodeoffset);
  1223. /**********************************************************************/
  1224. /* Sequential write functions */
  1225. /**********************************************************************/
  1226. int fdt_create(void *buf, int bufsize);
  1227. int fdt_resize(void *fdt, void *buf, int bufsize);
  1228. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
  1229. int fdt_finish_reservemap(void *fdt);
  1230. int fdt_begin_node(void *fdt, const char *name);
  1231. int fdt_property(void *fdt, const char *name, const void *val, int len);
  1232. static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
  1233. {
  1234. fdt32_t tmp = cpu_to_fdt32(val);
  1235. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1236. }
  1237. static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
  1238. {
  1239. fdt64_t tmp = cpu_to_fdt64(val);
  1240. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1241. }
  1242. static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
  1243. {
  1244. return fdt_property_u32(fdt, name, val);
  1245. }
  1246. #define fdt_property_string(fdt, name, str) \
  1247. fdt_property(fdt, name, str, strlen(str)+1)
  1248. int fdt_end_node(void *fdt);
  1249. int fdt_finish(void *fdt);
  1250. /**********************************************************************/
  1251. /* Read-write functions */
  1252. /**********************************************************************/
  1253. int fdt_create_empty_tree(void *buf, int bufsize);
  1254. int fdt_open_into(const void *fdt, void *buf, int bufsize);
  1255. int fdt_pack(void *fdt);
  1256. /**
  1257. * fdt_add_mem_rsv - add one memory reserve map entry
  1258. * @fdt: pointer to the device tree blob
  1259. * @address, @size: 64-bit values (native endian)
  1260. *
  1261. * Adds a reserve map entry to the given blob reserving a region at
  1262. * address address of length size.
  1263. *
  1264. * This function will insert data into the reserve map and will
  1265. * therefore change the indexes of some entries in the table.
  1266. *
  1267. * returns:
  1268. * 0, on success
  1269. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1270. * contain the new reservation entry
  1271. * -FDT_ERR_BADMAGIC,
  1272. * -FDT_ERR_BADVERSION,
  1273. * -FDT_ERR_BADSTATE,
  1274. * -FDT_ERR_BADSTRUCTURE,
  1275. * -FDT_ERR_BADLAYOUT,
  1276. * -FDT_ERR_TRUNCATED, standard meanings
  1277. */
  1278. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
  1279. /**
  1280. * fdt_del_mem_rsv - remove a memory reserve map entry
  1281. * @fdt: pointer to the device tree blob
  1282. * @n: entry to remove
  1283. *
  1284. * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
  1285. * the blob.
  1286. *
  1287. * This function will delete data from the reservation table and will
  1288. * therefore change the indexes of some entries in the table.
  1289. *
  1290. * returns:
  1291. * 0, on success
  1292. * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
  1293. * are less than n+1 reserve map entries)
  1294. * -FDT_ERR_BADMAGIC,
  1295. * -FDT_ERR_BADVERSION,
  1296. * -FDT_ERR_BADSTATE,
  1297. * -FDT_ERR_BADSTRUCTURE,
  1298. * -FDT_ERR_BADLAYOUT,
  1299. * -FDT_ERR_TRUNCATED, standard meanings
  1300. */
  1301. int fdt_del_mem_rsv(void *fdt, int n);
  1302. /**
  1303. * fdt_set_name - change the name of a given node
  1304. * @fdt: pointer to the device tree blob
  1305. * @nodeoffset: structure block offset of a node
  1306. * @name: name to give the node
  1307. *
  1308. * fdt_set_name() replaces the name (including unit address, if any)
  1309. * of the given node with the given string. NOTE: this function can't
  1310. * efficiently check if the new name is unique amongst the given
  1311. * node's siblings; results are undefined if this function is invoked
  1312. * with a name equal to one of the given node's siblings.
  1313. *
  1314. * This function may insert or delete data from the blob, and will
  1315. * therefore change the offsets of some existing nodes.
  1316. *
  1317. * returns:
  1318. * 0, on success
  1319. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
  1320. * to contain the new name
  1321. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1322. * -FDT_ERR_BADMAGIC,
  1323. * -FDT_ERR_BADVERSION,
  1324. * -FDT_ERR_BADSTATE, standard meanings
  1325. */
  1326. int fdt_set_name(void *fdt, int nodeoffset, const char *name);
  1327. /**
  1328. * fdt_setprop - create or change a property
  1329. * @fdt: pointer to the device tree blob
  1330. * @nodeoffset: offset of the node whose property to change
  1331. * @name: name of the property to change
  1332. * @val: pointer to data to set the property value to
  1333. * @len: length of the property value
  1334. *
  1335. * fdt_setprop() sets the value of the named property in the given
  1336. * node to the given value and length, creating the property if it
  1337. * does not already exist.
  1338. *
  1339. * This function may insert or delete data from the blob, and will
  1340. * therefore change the offsets of some existing nodes.
  1341. *
  1342. * returns:
  1343. * 0, on success
  1344. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1345. * contain the new property value
  1346. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1347. * -FDT_ERR_BADLAYOUT,
  1348. * -FDT_ERR_BADMAGIC,
  1349. * -FDT_ERR_BADVERSION,
  1350. * -FDT_ERR_BADSTATE,
  1351. * -FDT_ERR_BADSTRUCTURE,
  1352. * -FDT_ERR_BADLAYOUT,
  1353. * -FDT_ERR_TRUNCATED, standard meanings
  1354. */
  1355. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  1356. const void *val, int len);
  1357. /**
  1358. * fdt_setprop_u32 - set a property to a 32-bit integer
  1359. * @fdt: pointer to the device tree blob
  1360. * @nodeoffset: offset of the node whose property to change
  1361. * @name: name of the property to change
  1362. * @val: 32-bit integer value for the property (native endian)
  1363. *
  1364. * fdt_setprop_u32() sets the value of the named property in the given
  1365. * node to the given 32-bit integer value (converting to big-endian if
  1366. * necessary), or creates a new property with that value if it does
  1367. * not already exist.
  1368. *
  1369. * This function may insert or delete data from the blob, and will
  1370. * therefore change the offsets of some existing nodes.
  1371. *
  1372. * returns:
  1373. * 0, on success
  1374. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1375. * contain the new property value
  1376. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1377. * -FDT_ERR_BADLAYOUT,
  1378. * -FDT_ERR_BADMAGIC,
  1379. * -FDT_ERR_BADVERSION,
  1380. * -FDT_ERR_BADSTATE,
  1381. * -FDT_ERR_BADSTRUCTURE,
  1382. * -FDT_ERR_BADLAYOUT,
  1383. * -FDT_ERR_TRUNCATED, standard meanings
  1384. */
  1385. static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
  1386. uint32_t val)
  1387. {
  1388. fdt32_t tmp = cpu_to_fdt32(val);
  1389. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1390. }
  1391. /**
  1392. * fdt_setprop_u64 - set a property to a 64-bit integer
  1393. * @fdt: pointer to the device tree blob
  1394. * @nodeoffset: offset of the node whose property to change
  1395. * @name: name of the property to change
  1396. * @val: 64-bit integer value for the property (native endian)
  1397. *
  1398. * fdt_setprop_u64() sets the value of the named property in the given
  1399. * node to the given 64-bit integer value (converting to big-endian if
  1400. * necessary), or creates a new property with that value if it does
  1401. * not already exist.
  1402. *
  1403. * This function may insert or delete data from the blob, and will
  1404. * therefore change the offsets of some existing nodes.
  1405. *
  1406. * returns:
  1407. * 0, on success
  1408. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1409. * contain the new property value
  1410. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1411. * -FDT_ERR_BADLAYOUT,
  1412. * -FDT_ERR_BADMAGIC,
  1413. * -FDT_ERR_BADVERSION,
  1414. * -FDT_ERR_BADSTATE,
  1415. * -FDT_ERR_BADSTRUCTURE,
  1416. * -FDT_ERR_BADLAYOUT,
  1417. * -FDT_ERR_TRUNCATED, standard meanings
  1418. */
  1419. static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
  1420. uint64_t val)
  1421. {
  1422. fdt64_t tmp = cpu_to_fdt64(val);
  1423. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1424. }
  1425. /**
  1426. * fdt_setprop_cell - set a property to a single cell value
  1427. *
  1428. * This is an alternative name for fdt_setprop_u32()
  1429. */
  1430. static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
  1431. uint32_t val)
  1432. {
  1433. return fdt_setprop_u32(fdt, nodeoffset, name, val);
  1434. }
  1435. /**
  1436. * fdt_setprop_string - set a property to a string value
  1437. * @fdt: pointer to the device tree blob
  1438. * @nodeoffset: offset of the node whose property to change
  1439. * @name: name of the property to change
  1440. * @str: string value for the property
  1441. *
  1442. * fdt_setprop_string() sets the value of the named property in the
  1443. * given node to the given string value (using the length of the
  1444. * string to determine the new length of the property), or creates a
  1445. * new property with that value if it does not already exist.
  1446. *
  1447. * This function may insert or delete data from the blob, and will
  1448. * therefore change the offsets of some existing nodes.
  1449. *
  1450. * returns:
  1451. * 0, on success
  1452. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1453. * contain the new property value
  1454. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1455. * -FDT_ERR_BADLAYOUT,
  1456. * -FDT_ERR_BADMAGIC,
  1457. * -FDT_ERR_BADVERSION,
  1458. * -FDT_ERR_BADSTATE,
  1459. * -FDT_ERR_BADSTRUCTURE,
  1460. * -FDT_ERR_BADLAYOUT,
  1461. * -FDT_ERR_TRUNCATED, standard meanings
  1462. */
  1463. #define fdt_setprop_string(fdt, nodeoffset, name, str) \
  1464. fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1465. /**
  1466. * fdt_setprop_empty - set a property to an empty value
  1467. * @fdt: pointer to the device tree blob
  1468. * @nodeoffset: offset of the node whose property to change
  1469. * @name: name of the property to change
  1470. *
  1471. * fdt_setprop_empty() sets the value of the named property in the
  1472. * given node to an empty (zero length) value, or creates a new empty
  1473. * property if it does not already exist.
  1474. *
  1475. * This function may insert or delete data from the blob, and will
  1476. * therefore change the offsets of some existing nodes.
  1477. *
  1478. * returns:
  1479. * 0, on success
  1480. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1481. * contain the new property value
  1482. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1483. * -FDT_ERR_BADLAYOUT,
  1484. * -FDT_ERR_BADMAGIC,
  1485. * -FDT_ERR_BADVERSION,
  1486. * -FDT_ERR_BADSTATE,
  1487. * -FDT_ERR_BADSTRUCTURE,
  1488. * -FDT_ERR_BADLAYOUT,
  1489. * -FDT_ERR_TRUNCATED, standard meanings
  1490. */
  1491. #define fdt_setprop_empty(fdt, nodeoffset, name) \
  1492. fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
  1493. /**
  1494. * fdt_appendprop - append to or create a property
  1495. * @fdt: pointer to the device tree blob
  1496. * @nodeoffset: offset of the node whose property to change
  1497. * @name: name of the property to append to
  1498. * @val: pointer to data to append to the property value
  1499. * @len: length of the data to append to the property value
  1500. *
  1501. * fdt_appendprop() appends the value to the named property in the
  1502. * given node, creating the property if it does not already exist.
  1503. *
  1504. * This function may insert data into the blob, and will therefore
  1505. * change the offsets of some existing nodes.
  1506. *
  1507. * returns:
  1508. * 0, on success
  1509. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1510. * contain the new property value
  1511. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1512. * -FDT_ERR_BADLAYOUT,
  1513. * -FDT_ERR_BADMAGIC,
  1514. * -FDT_ERR_BADVERSION,
  1515. * -FDT_ERR_BADSTATE,
  1516. * -FDT_ERR_BADSTRUCTURE,
  1517. * -FDT_ERR_BADLAYOUT,
  1518. * -FDT_ERR_TRUNCATED, standard meanings
  1519. */
  1520. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  1521. const void *val, int len);
  1522. /**
  1523. * fdt_appendprop_u32 - append a 32-bit integer value to a property
  1524. * @fdt: pointer to the device tree blob
  1525. * @nodeoffset: offset of the node whose property to change
  1526. * @name: name of the property to change
  1527. * @val: 32-bit integer value to append to the property (native endian)
  1528. *
  1529. * fdt_appendprop_u32() appends the given 32-bit integer value
  1530. * (converting to big-endian if necessary) to the value of the named
  1531. * property in the given node, or creates a new property with that
  1532. * value if it does not already exist.
  1533. *
  1534. * This function may insert data into the blob, and will therefore
  1535. * change the offsets of some existing nodes.
  1536. *
  1537. * returns:
  1538. * 0, on success
  1539. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1540. * contain the new property value
  1541. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1542. * -FDT_ERR_BADLAYOUT,
  1543. * -FDT_ERR_BADMAGIC,
  1544. * -FDT_ERR_BADVERSION,
  1545. * -FDT_ERR_BADSTATE,
  1546. * -FDT_ERR_BADSTRUCTURE,
  1547. * -FDT_ERR_BADLAYOUT,
  1548. * -FDT_ERR_TRUNCATED, standard meanings
  1549. */
  1550. static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
  1551. const char *name, uint32_t val)
  1552. {
  1553. fdt32_t tmp = cpu_to_fdt32(val);
  1554. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1555. }
  1556. /**
  1557. * fdt_appendprop_u64 - append a 64-bit integer value to a property
  1558. * @fdt: pointer to the device tree blob
  1559. * @nodeoffset: offset of the node whose property to change
  1560. * @name: name of the property to change
  1561. * @val: 64-bit integer value to append to the property (native endian)
  1562. *
  1563. * fdt_appendprop_u64() appends the given 64-bit integer value
  1564. * (converting to big-endian if necessary) to the value of the named
  1565. * property in the given node, or creates a new property with that
  1566. * value if it does not already exist.
  1567. *
  1568. * This function may insert data into the blob, and will therefore
  1569. * change the offsets of some existing nodes.
  1570. *
  1571. * returns:
  1572. * 0, on success
  1573. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1574. * contain the new property value
  1575. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1576. * -FDT_ERR_BADLAYOUT,
  1577. * -FDT_ERR_BADMAGIC,
  1578. * -FDT_ERR_BADVERSION,
  1579. * -FDT_ERR_BADSTATE,
  1580. * -FDT_ERR_BADSTRUCTURE,
  1581. * -FDT_ERR_BADLAYOUT,
  1582. * -FDT_ERR_TRUNCATED, standard meanings
  1583. */
  1584. static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
  1585. const char *name, uint64_t val)
  1586. {
  1587. fdt64_t tmp = cpu_to_fdt64(val);
  1588. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1589. }
  1590. /**
  1591. * fdt_appendprop_cell - append a single cell value to a property
  1592. *
  1593. * This is an alternative name for fdt_appendprop_u32()
  1594. */
  1595. static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
  1596. const char *name, uint32_t val)
  1597. {
  1598. return fdt_appendprop_u32(fdt, nodeoffset, name, val);
  1599. }
  1600. /**
  1601. * fdt_appendprop_string - append a string to a property
  1602. * @fdt: pointer to the device tree blob
  1603. * @nodeoffset: offset of the node whose property to change
  1604. * @name: name of the property to change
  1605. * @str: string value to append to the property
  1606. *
  1607. * fdt_appendprop_string() appends the given string to the value of
  1608. * the named property in the given node, or creates a new property
  1609. * with that value if it does not already exist.
  1610. *
  1611. * This function may insert data into the blob, and will therefore
  1612. * change the offsets of some existing nodes.
  1613. *
  1614. * returns:
  1615. * 0, on success
  1616. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1617. * contain the new property value
  1618. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1619. * -FDT_ERR_BADLAYOUT,
  1620. * -FDT_ERR_BADMAGIC,
  1621. * -FDT_ERR_BADVERSION,
  1622. * -FDT_ERR_BADSTATE,
  1623. * -FDT_ERR_BADSTRUCTURE,
  1624. * -FDT_ERR_BADLAYOUT,
  1625. * -FDT_ERR_TRUNCATED, standard meanings
  1626. */
  1627. #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
  1628. fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1629. /**
  1630. * fdt_delprop - delete a property
  1631. * @fdt: pointer to the device tree blob
  1632. * @nodeoffset: offset of the node whose property to nop
  1633. * @name: name of the property to nop
  1634. *
  1635. * fdt_del_property() will delete the given property.
  1636. *
  1637. * This function will delete data from the blob, and will therefore
  1638. * change the offsets of some existing nodes.
  1639. *
  1640. * returns:
  1641. * 0, on success
  1642. * -FDT_ERR_NOTFOUND, node does not have the named property
  1643. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1644. * -FDT_ERR_BADLAYOUT,
  1645. * -FDT_ERR_BADMAGIC,
  1646. * -FDT_ERR_BADVERSION,
  1647. * -FDT_ERR_BADSTATE,
  1648. * -FDT_ERR_BADSTRUCTURE,
  1649. * -FDT_ERR_TRUNCATED, standard meanings
  1650. */
  1651. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  1652. /**
  1653. * fdt_add_subnode_namelen - creates a new node based on substring
  1654. * @fdt: pointer to the device tree blob
  1655. * @parentoffset: structure block offset of a node
  1656. * @name: name of the subnode to locate
  1657. * @namelen: number of characters of name to consider
  1658. *
  1659. * Identical to fdt_add_subnode(), but use only the first namelen
  1660. * characters of name as the name of the new node. This is useful for
  1661. * creating subnodes based on a portion of a larger string, such as a
  1662. * full path.
  1663. */
  1664. #ifndef SWIG /* Not available in Python */
  1665. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  1666. const char *name, int namelen);
  1667. #endif
  1668. /**
  1669. * fdt_add_subnode - creates a new node
  1670. * @fdt: pointer to the device tree blob
  1671. * @parentoffset: structure block offset of a node
  1672. * @name: name of the subnode to locate
  1673. *
  1674. * fdt_add_subnode() creates a new node as a subnode of the node at
  1675. * structure block offset parentoffset, with the given name (which
  1676. * should include the unit address, if any).
  1677. *
  1678. * This function will insert data into the blob, and will therefore
  1679. * change the offsets of some existing nodes.
  1680. * returns:
  1681. * structure block offset of the created nodeequested subnode (>=0), on
  1682. * success
  1683. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  1684. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  1685. * tag
  1686. * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
  1687. * the given name
  1688. * -FDT_ERR_NOSPACE, if there is insufficient free space in the
  1689. * blob to contain the new node
  1690. * -FDT_ERR_NOSPACE
  1691. * -FDT_ERR_BADLAYOUT
  1692. * -FDT_ERR_BADMAGIC,
  1693. * -FDT_ERR_BADVERSION,
  1694. * -FDT_ERR_BADSTATE,
  1695. * -FDT_ERR_BADSTRUCTURE,
  1696. * -FDT_ERR_TRUNCATED, standard meanings.
  1697. */
  1698. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  1699. /**
  1700. * fdt_del_node - delete a node (subtree)
  1701. * @fdt: pointer to the device tree blob
  1702. * @nodeoffset: offset of the node to nop
  1703. *
  1704. * fdt_del_node() will remove the given node, including all its
  1705. * subnodes if any, from the blob.
  1706. *
  1707. * This function will delete data from the blob, and will therefore
  1708. * change the offsets of some existing nodes.
  1709. *
  1710. * returns:
  1711. * 0, on success
  1712. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1713. * -FDT_ERR_BADLAYOUT,
  1714. * -FDT_ERR_BADMAGIC,
  1715. * -FDT_ERR_BADVERSION,
  1716. * -FDT_ERR_BADSTATE,
  1717. * -FDT_ERR_BADSTRUCTURE,
  1718. * -FDT_ERR_TRUNCATED, standard meanings
  1719. */
  1720. int fdt_del_node(void *fdt, int nodeoffset);
  1721. /**
  1722. * fdt_overlay_apply - Applies a DT overlay on a base DT
  1723. * @fdt: pointer to the base device tree blob
  1724. * @fdto: pointer to the device tree overlay blob
  1725. *
  1726. * fdt_overlay_apply() will apply the given device tree overlay on the
  1727. * given base device tree.
  1728. *
  1729. * Expect the base device tree to be modified, even if the function
  1730. * returns an error.
  1731. *
  1732. * returns:
  1733. * 0, on success
  1734. * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
  1735. * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
  1736. * properties in the base DT
  1737. * -FDT_ERR_BADPHANDLE,
  1738. * -FDT_ERR_BADOVERLAY,
  1739. * -FDT_ERR_NOPHANDLES,
  1740. * -FDT_ERR_INTERNAL,
  1741. * -FDT_ERR_BADLAYOUT,
  1742. * -FDT_ERR_BADMAGIC,
  1743. * -FDT_ERR_BADOFFSET,
  1744. * -FDT_ERR_BADPATH,
  1745. * -FDT_ERR_BADVERSION,
  1746. * -FDT_ERR_BADSTRUCTURE,
  1747. * -FDT_ERR_BADSTATE,
  1748. * -FDT_ERR_TRUNCATED, standard meanings
  1749. */
  1750. int fdt_overlay_apply(void *fdt, void *fdto);
  1751. /**********************************************************************/
  1752. /* Debugging / informational functions */
  1753. /**********************************************************************/
  1754. const char *fdt_strerror(int errval);
  1755. #endif /* _LIBFDT_H */