libfdt.h 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137
  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_TOODEEP 18
  130. /* FDT_ERR_TOODEEP: The depth of a node has exceeded the internal
  131. * libfdt limit. This can happen if you have more than
  132. * FDT_MAX_DEPTH nested nodes. */
  133. #define FDT_ERR_MAX 18
  134. /**********************************************************************/
  135. /* Low-level functions (you probably don't need these) */
  136. /**********************************************************************/
  137. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
  138. static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  139. {
  140. return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
  141. }
  142. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
  143. /**********************************************************************/
  144. /* Traversal functions */
  145. /**********************************************************************/
  146. int fdt_next_node(const void *fdt, int offset, int *depth);
  147. /**
  148. * fdt_first_subnode() - get offset of first direct subnode
  149. *
  150. * @fdt: FDT blob
  151. * @offset: Offset of node to check
  152. * @return offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
  153. */
  154. int fdt_first_subnode(const void *fdt, int offset);
  155. /**
  156. * fdt_next_subnode() - get offset of next direct subnode
  157. *
  158. * After first calling fdt_first_subnode(), call this function repeatedly to
  159. * get direct subnodes of a parent node.
  160. *
  161. * @fdt: FDT blob
  162. * @offset: Offset of previous subnode
  163. * @return offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
  164. * subnodes
  165. */
  166. int fdt_next_subnode(const void *fdt, int offset);
  167. /**
  168. * fdt_for_each_subnode - iterate over all subnodes of a parent
  169. *
  170. * @node: child node (int, lvalue)
  171. * @fdt: FDT blob (const void *)
  172. * @parent: parent node (int)
  173. *
  174. * This is actually a wrapper around a for loop and would be used like so:
  175. *
  176. * fdt_for_each_subnode(node, fdt, parent) {
  177. * Use node
  178. * ...
  179. * }
  180. *
  181. * if ((node < 0) && (node != -FDT_ERR_NOT_FOUND)) {
  182. * Error handling
  183. * }
  184. *
  185. * Note that this is implemented as a macro and @node is used as
  186. * iterator in the loop. The parent variable be constant or even a
  187. * literal.
  188. *
  189. */
  190. #define fdt_for_each_subnode(node, fdt, parent) \
  191. for (node = fdt_first_subnode(fdt, parent); \
  192. node >= 0; \
  193. node = fdt_next_subnode(fdt, node))
  194. /**********************************************************************/
  195. /* General functions */
  196. /**********************************************************************/
  197. #define fdt_get_header(fdt, field) \
  198. (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
  199. #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
  200. #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
  201. #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
  202. #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
  203. #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
  204. #define fdt_version(fdt) (fdt_get_header(fdt, version))
  205. #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
  206. #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
  207. #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
  208. #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
  209. #define __fdt_set_hdr(name) \
  210. static inline void fdt_set_##name(void *fdt, uint32_t val) \
  211. { \
  212. struct fdt_header *fdth = (struct fdt_header *)fdt; \
  213. fdth->name = cpu_to_fdt32(val); \
  214. }
  215. __fdt_set_hdr(magic);
  216. __fdt_set_hdr(totalsize);
  217. __fdt_set_hdr(off_dt_struct);
  218. __fdt_set_hdr(off_dt_strings);
  219. __fdt_set_hdr(off_mem_rsvmap);
  220. __fdt_set_hdr(version);
  221. __fdt_set_hdr(last_comp_version);
  222. __fdt_set_hdr(boot_cpuid_phys);
  223. __fdt_set_hdr(size_dt_strings);
  224. __fdt_set_hdr(size_dt_struct);
  225. #undef __fdt_set_hdr
  226. /**
  227. * fdt_check_header - sanity check a device tree or possible device tree
  228. * @fdt: pointer to data which might be a flattened device tree
  229. *
  230. * fdt_check_header() checks that the given buffer contains what
  231. * appears to be a flattened device tree with sane information in its
  232. * header.
  233. *
  234. * returns:
  235. * 0, if the buffer appears to contain a valid device tree
  236. * -FDT_ERR_BADMAGIC,
  237. * -FDT_ERR_BADVERSION,
  238. * -FDT_ERR_BADSTATE, standard meanings, as above
  239. */
  240. int fdt_check_header(const void *fdt);
  241. /**
  242. * fdt_move - move a device tree around in memory
  243. * @fdt: pointer to the device tree to move
  244. * @buf: pointer to memory where the device is to be moved
  245. * @bufsize: size of the memory space at buf
  246. *
  247. * fdt_move() relocates, if possible, the device tree blob located at
  248. * fdt to the buffer at buf of size bufsize. The buffer may overlap
  249. * with the existing device tree blob at fdt. Therefore,
  250. * fdt_move(fdt, fdt, fdt_totalsize(fdt))
  251. * should always succeed.
  252. *
  253. * returns:
  254. * 0, on success
  255. * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
  256. * -FDT_ERR_BADMAGIC,
  257. * -FDT_ERR_BADVERSION,
  258. * -FDT_ERR_BADSTATE, standard meanings
  259. */
  260. int fdt_move(const void *fdt, void *buf, int bufsize);
  261. /**********************************************************************/
  262. /* Read-only functions */
  263. /**********************************************************************/
  264. /**
  265. * fdt_string - retrieve a string from the strings block of a device tree
  266. * @fdt: pointer to the device tree blob
  267. * @stroffset: offset of the string within the strings block (native endian)
  268. *
  269. * fdt_string() retrieves a pointer to a single string from the
  270. * strings block of the device tree blob at fdt.
  271. *
  272. * returns:
  273. * a pointer to the string, on success
  274. * NULL, if stroffset is out of bounds
  275. */
  276. const char *fdt_string(const void *fdt, int stroffset);
  277. /**
  278. * fdt_get_max_phandle - retrieves the highest phandle in a tree
  279. * @fdt: pointer to the device tree blob
  280. *
  281. * fdt_get_max_phandle retrieves the highest phandle in the given
  282. * device tree. This will ignore badly formatted phandles, or phandles
  283. * with a value of 0 or -1.
  284. *
  285. * returns:
  286. * the highest phandle on success
  287. * 0, if no phandle was found in the device tree
  288. * -1, if an error occurred
  289. */
  290. uint32_t fdt_get_max_phandle(const void *fdt);
  291. /**
  292. * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
  293. * @fdt: pointer to the device tree blob
  294. *
  295. * Returns the number of entries in the device tree blob's memory
  296. * reservation map. This does not include the terminating 0,0 entry
  297. * or any other (0,0) entries reserved for expansion.
  298. *
  299. * returns:
  300. * the number of entries
  301. */
  302. int fdt_num_mem_rsv(const void *fdt);
  303. /**
  304. * fdt_get_mem_rsv - retrieve one memory reserve map entry
  305. * @fdt: pointer to the device tree blob
  306. * @address, @size: pointers to 64-bit variables
  307. *
  308. * On success, *address and *size will contain the address and size of
  309. * the n-th reserve map entry from the device tree blob, in
  310. * native-endian format.
  311. *
  312. * returns:
  313. * 0, on success
  314. * -FDT_ERR_BADMAGIC,
  315. * -FDT_ERR_BADVERSION,
  316. * -FDT_ERR_BADSTATE, standard meanings
  317. */
  318. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
  319. /**
  320. * fdt_subnode_offset_namelen - find a subnode based on substring
  321. * @fdt: pointer to the device tree blob
  322. * @parentoffset: structure block offset of a node
  323. * @name: name of the subnode to locate
  324. * @namelen: number of characters of name to consider
  325. *
  326. * Identical to fdt_subnode_offset(), but only examine the first
  327. * namelen characters of name for matching the subnode name. This is
  328. * useful for finding subnodes based on a portion of a larger string,
  329. * such as a full path.
  330. */
  331. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  332. const char *name, int namelen);
  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. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
  368. /**
  369. * fdt_path_offset - find a tree node by its full path
  370. * @fdt: pointer to the device tree blob
  371. * @path: full path of the node to locate
  372. *
  373. * fdt_path_offset() finds a node of a given path in the device tree.
  374. * Each path component may omit the unit address portion, but the
  375. * results of this are undefined if any such path component is
  376. * ambiguous (that is if there are multiple nodes at the relevant
  377. * level matching the given component, differentiated only by unit
  378. * address).
  379. *
  380. * returns:
  381. * structure block offset of the node with the requested path (>=0), on
  382. * success
  383. * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
  384. * -FDT_ERR_NOTFOUND, if the requested node does not exist
  385. * -FDT_ERR_BADMAGIC,
  386. * -FDT_ERR_BADVERSION,
  387. * -FDT_ERR_BADSTATE,
  388. * -FDT_ERR_BADSTRUCTURE,
  389. * -FDT_ERR_TRUNCATED, standard meanings.
  390. */
  391. int fdt_path_offset(const void *fdt, const char *path);
  392. /**
  393. * fdt_get_name - retrieve the name of a given node
  394. * @fdt: pointer to the device tree blob
  395. * @nodeoffset: structure block offset of the starting node
  396. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  397. *
  398. * fdt_get_name() retrieves the name (including unit address) of the
  399. * device tree node at structure block offset nodeoffset. If lenp is
  400. * non-NULL, the length of this name is also returned, in the integer
  401. * pointed to by lenp.
  402. *
  403. * returns:
  404. * pointer to the node's name, on success
  405. * If lenp is non-NULL, *lenp contains the length of that name
  406. * (>=0)
  407. * NULL, on error
  408. * if lenp is non-NULL *lenp contains an error code (<0):
  409. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  410. * tag
  411. * -FDT_ERR_BADMAGIC,
  412. * -FDT_ERR_BADVERSION,
  413. * -FDT_ERR_BADSTATE, standard meanings
  414. */
  415. const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
  416. /**
  417. * fdt_first_property_offset - find the offset of a node's first property
  418. * @fdt: pointer to the device tree blob
  419. * @nodeoffset: structure block offset of a node
  420. *
  421. * fdt_first_property_offset() finds the first property of the node at
  422. * the given structure block offset.
  423. *
  424. * returns:
  425. * structure block offset of the property (>=0), on success
  426. * -FDT_ERR_NOTFOUND, if the requested node has no properties
  427. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
  428. * -FDT_ERR_BADMAGIC,
  429. * -FDT_ERR_BADVERSION,
  430. * -FDT_ERR_BADSTATE,
  431. * -FDT_ERR_BADSTRUCTURE,
  432. * -FDT_ERR_TRUNCATED, standard meanings.
  433. */
  434. int fdt_first_property_offset(const void *fdt, int nodeoffset);
  435. /**
  436. * fdt_next_property_offset - step through a node's properties
  437. * @fdt: pointer to the device tree blob
  438. * @offset: structure block offset of a property
  439. *
  440. * fdt_next_property_offset() finds the property immediately after the
  441. * one at the given structure block offset. This will be a property
  442. * of the same node as the given property.
  443. *
  444. * returns:
  445. * structure block offset of the next property (>=0), on success
  446. * -FDT_ERR_NOTFOUND, if the given property is the last in its node
  447. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
  448. * -FDT_ERR_BADMAGIC,
  449. * -FDT_ERR_BADVERSION,
  450. * -FDT_ERR_BADSTATE,
  451. * -FDT_ERR_BADSTRUCTURE,
  452. * -FDT_ERR_TRUNCATED, standard meanings.
  453. */
  454. int fdt_next_property_offset(const void *fdt, int offset);
  455. /**
  456. * fdt_for_each_property_offset - iterate over all properties of a node
  457. *
  458. * @property_offset: property offset (int, lvalue)
  459. * @fdt: FDT blob (const void *)
  460. * @node: node offset (int)
  461. *
  462. * This is actually a wrapper around a for loop and would be used like so:
  463. *
  464. * fdt_for_each_property_offset(property, fdt, node) {
  465. * Use property
  466. * ...
  467. * }
  468. *
  469. * if ((property < 0) && (property != -FDT_ERR_NOT_FOUND)) {
  470. * Error handling
  471. * }
  472. *
  473. * Note that this is implemented as a macro and property is used as
  474. * iterator in the loop. The node variable can be constant or even a
  475. * literal.
  476. */
  477. #define fdt_for_each_property_offset(property, fdt, node) \
  478. for (property = fdt_first_property_offset(fdt, node); \
  479. property >= 0; \
  480. property = fdt_next_property_offset(fdt, property))
  481. /**
  482. * fdt_get_property_by_offset - retrieve the property at a given offset
  483. * @fdt: pointer to the device tree blob
  484. * @offset: offset of the property to retrieve
  485. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  486. *
  487. * fdt_get_property_by_offset() retrieves a pointer to the
  488. * fdt_property structure within the device tree blob at the given
  489. * offset. If lenp is non-NULL, the length of the property value is
  490. * also returned, in the integer pointed to by lenp.
  491. *
  492. * returns:
  493. * pointer to the structure representing the property
  494. * if lenp is non-NULL, *lenp contains the length of the property
  495. * value (>=0)
  496. * NULL, on error
  497. * if lenp is non-NULL, *lenp contains an error code (<0):
  498. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  499. * -FDT_ERR_BADMAGIC,
  500. * -FDT_ERR_BADVERSION,
  501. * -FDT_ERR_BADSTATE,
  502. * -FDT_ERR_BADSTRUCTURE,
  503. * -FDT_ERR_TRUNCATED, standard meanings
  504. */
  505. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  506. int offset,
  507. int *lenp);
  508. /**
  509. * fdt_get_property_namelen - find a property based on substring
  510. * @fdt: pointer to the device tree blob
  511. * @nodeoffset: offset of the node whose property to find
  512. * @name: name of the property to find
  513. * @namelen: number of characters of name to consider
  514. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  515. *
  516. * Identical to fdt_get_property(), but only examine the first namelen
  517. * characters of name for matching the property name.
  518. */
  519. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  520. int nodeoffset,
  521. const char *name,
  522. int namelen, int *lenp);
  523. /**
  524. * fdt_get_property - find a given property in a given node
  525. * @fdt: pointer to the device tree blob
  526. * @nodeoffset: offset of the node whose property to find
  527. * @name: name of the property to find
  528. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  529. *
  530. * fdt_get_property() retrieves a pointer to the fdt_property
  531. * structure within the device tree blob corresponding to the property
  532. * named 'name' of the node at offset nodeoffset. If lenp is
  533. * non-NULL, the length of the property value is also returned, in the
  534. * integer pointed to by lenp.
  535. *
  536. * returns:
  537. * pointer to the structure representing the property
  538. * if lenp is non-NULL, *lenp contains the length of the property
  539. * value (>=0)
  540. * NULL, on error
  541. * if lenp is non-NULL, *lenp contains an error code (<0):
  542. * -FDT_ERR_NOTFOUND, node does not have named property
  543. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  544. * tag
  545. * -FDT_ERR_BADMAGIC,
  546. * -FDT_ERR_BADVERSION,
  547. * -FDT_ERR_BADSTATE,
  548. * -FDT_ERR_BADSTRUCTURE,
  549. * -FDT_ERR_TRUNCATED, standard meanings
  550. */
  551. const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
  552. const char *name, int *lenp);
  553. static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
  554. const char *name,
  555. int *lenp)
  556. {
  557. return (struct fdt_property *)(uintptr_t)
  558. fdt_get_property(fdt, nodeoffset, name, lenp);
  559. }
  560. /**
  561. * fdt_getprop_by_offset - retrieve the value of a property at a given offset
  562. * @fdt: pointer to the device tree blob
  563. * @ffset: offset of the property to read
  564. * @namep: pointer to a string variable (will be overwritten) or NULL
  565. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  566. *
  567. * fdt_getprop_by_offset() retrieves a pointer to the value of the
  568. * property at structure block offset 'offset' (this will be a pointer
  569. * to within the device blob itself, not a copy of the value). If
  570. * lenp is non-NULL, the length of the property value is also
  571. * returned, in the integer pointed to by lenp. If namep is non-NULL,
  572. * the property's namne will also be returned in the char * pointed to
  573. * by namep (this will be a pointer to within the device tree's string
  574. * block, not a new copy of the name).
  575. *
  576. * returns:
  577. * pointer to the property's value
  578. * if lenp is non-NULL, *lenp contains the length of the property
  579. * value (>=0)
  580. * if namep is non-NULL *namep contiains a pointer to the property
  581. * name.
  582. * NULL, on error
  583. * if lenp is non-NULL, *lenp contains an error code (<0):
  584. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  585. * -FDT_ERR_BADMAGIC,
  586. * -FDT_ERR_BADVERSION,
  587. * -FDT_ERR_BADSTATE,
  588. * -FDT_ERR_BADSTRUCTURE,
  589. * -FDT_ERR_TRUNCATED, standard meanings
  590. */
  591. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  592. const char **namep, int *lenp);
  593. /**
  594. * fdt_getprop_namelen - get property value based on substring
  595. * @fdt: pointer to the device tree blob
  596. * @nodeoffset: offset of the node whose property to find
  597. * @name: name of the property to find
  598. * @namelen: number of characters of name to consider
  599. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  600. *
  601. * Identical to fdt_getprop(), but only examine the first namelen
  602. * characters of name for matching the property name.
  603. */
  604. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  605. const char *name, int namelen, int *lenp);
  606. static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
  607. const char *name, int namelen,
  608. int *lenp)
  609. {
  610. return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
  611. namelen, lenp);
  612. }
  613. /**
  614. * fdt_getprop - retrieve the value of a given property
  615. * @fdt: pointer to the device tree blob
  616. * @nodeoffset: offset of the node whose property to find
  617. * @name: name of the property to find
  618. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  619. *
  620. * fdt_getprop() retrieves a pointer to the value of the property
  621. * named 'name' of the node at offset nodeoffset (this will be a
  622. * pointer to within the device blob itself, not a copy of the value).
  623. * If lenp is non-NULL, the length of the property value is also
  624. * returned, in the integer pointed to by lenp.
  625. *
  626. * returns:
  627. * pointer to the property's value
  628. * if lenp is non-NULL, *lenp contains the length of the property
  629. * value (>=0)
  630. * NULL, on error
  631. * if lenp is non-NULL, *lenp contains an error code (<0):
  632. * -FDT_ERR_NOTFOUND, node does not have named property
  633. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  634. * tag
  635. * -FDT_ERR_BADMAGIC,
  636. * -FDT_ERR_BADVERSION,
  637. * -FDT_ERR_BADSTATE,
  638. * -FDT_ERR_BADSTRUCTURE,
  639. * -FDT_ERR_TRUNCATED, standard meanings
  640. */
  641. const void *fdt_getprop(const void *fdt, int nodeoffset,
  642. const char *name, int *lenp);
  643. static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
  644. const char *name, int *lenp)
  645. {
  646. return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
  647. }
  648. /**
  649. * fdt_get_phandle - retrieve the phandle of a given node
  650. * @fdt: pointer to the device tree blob
  651. * @nodeoffset: structure block offset of the node
  652. *
  653. * fdt_get_phandle() retrieves the phandle of the device tree node at
  654. * structure block offset nodeoffset.
  655. *
  656. * returns:
  657. * the phandle of the node at nodeoffset, on success (!= 0, != -1)
  658. * 0, if the node has no phandle, or another error occurs
  659. */
  660. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
  661. /**
  662. * fdt_get_alias_namelen - get alias based on substring
  663. * @fdt: pointer to the device tree blob
  664. * @name: name of the alias th look up
  665. * @namelen: number of characters of name to consider
  666. *
  667. * Identical to fdt_get_alias(), but only examine the first namelen
  668. * characters of name for matching the alias name.
  669. */
  670. const char *fdt_get_alias_namelen(const void *fdt,
  671. const char *name, int namelen);
  672. /**
  673. * fdt_get_alias - retrieve the path referenced by a given alias
  674. * @fdt: pointer to the device tree blob
  675. * @name: name of the alias th look up
  676. *
  677. * fdt_get_alias() retrieves the value of a given alias. That is, the
  678. * value of the property named 'name' in the node /aliases.
  679. *
  680. * returns:
  681. * a pointer to the expansion of the alias named 'name', if it exists
  682. * NULL, if the given alias or the /aliases node does not exist
  683. */
  684. const char *fdt_get_alias(const void *fdt, const char *name);
  685. /**
  686. * fdt_get_path - determine the full path of a node
  687. * @fdt: pointer to the device tree blob
  688. * @nodeoffset: offset of the node whose path to find
  689. * @buf: character buffer to contain the returned path (will be overwritten)
  690. * @buflen: size of the character buffer at buf
  691. *
  692. * fdt_get_path() computes the full path of the node at offset
  693. * nodeoffset, and records that path in the buffer at buf.
  694. *
  695. * NOTE: This function is expensive, as it must scan the device tree
  696. * structure from the start to nodeoffset.
  697. *
  698. * returns:
  699. * 0, on success
  700. * buf contains the absolute path of the node at
  701. * nodeoffset, as a NUL-terminated string.
  702. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  703. * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
  704. * characters and will not fit in the given buffer.
  705. * -FDT_ERR_BADMAGIC,
  706. * -FDT_ERR_BADVERSION,
  707. * -FDT_ERR_BADSTATE,
  708. * -FDT_ERR_BADSTRUCTURE, standard meanings
  709. */
  710. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
  711. /**
  712. * fdt_supernode_atdepth_offset - find a specific ancestor of a node
  713. * @fdt: pointer to the device tree blob
  714. * @nodeoffset: offset of the node whose parent to find
  715. * @supernodedepth: depth of the ancestor to find
  716. * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
  717. *
  718. * fdt_supernode_atdepth_offset() finds an ancestor of the given node
  719. * at a specific depth from the root (where the root itself has depth
  720. * 0, its immediate subnodes depth 1 and so forth). So
  721. * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
  722. * will always return 0, the offset of the root node. If the node at
  723. * nodeoffset has depth D, then:
  724. * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
  725. * will return nodeoffset itself.
  726. *
  727. * NOTE: This function is expensive, as it must scan the device tree
  728. * structure from the start to nodeoffset.
  729. *
  730. * returns:
  731. * structure block offset of the node at node offset's ancestor
  732. * of depth supernodedepth (>=0), on success
  733. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  734. * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
  735. * nodeoffset
  736. * -FDT_ERR_BADMAGIC,
  737. * -FDT_ERR_BADVERSION,
  738. * -FDT_ERR_BADSTATE,
  739. * -FDT_ERR_BADSTRUCTURE, standard meanings
  740. */
  741. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  742. int supernodedepth, int *nodedepth);
  743. /**
  744. * fdt_node_depth - find the depth of a given node
  745. * @fdt: pointer to the device tree blob
  746. * @nodeoffset: offset of the node whose parent to find
  747. *
  748. * fdt_node_depth() finds the depth of a given node. The root node
  749. * has depth 0, its immediate subnodes depth 1 and so forth.
  750. *
  751. * NOTE: This function is expensive, as it must scan the device tree
  752. * structure from the start to nodeoffset.
  753. *
  754. * returns:
  755. * depth of the node at nodeoffset (>=0), on success
  756. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  757. * -FDT_ERR_BADMAGIC,
  758. * -FDT_ERR_BADVERSION,
  759. * -FDT_ERR_BADSTATE,
  760. * -FDT_ERR_BADSTRUCTURE, standard meanings
  761. */
  762. int fdt_node_depth(const void *fdt, int nodeoffset);
  763. /**
  764. * fdt_parent_offset - find the parent of a given node
  765. * @fdt: pointer to the device tree blob
  766. * @nodeoffset: offset of the node whose parent to find
  767. *
  768. * fdt_parent_offset() locates the parent node of a given node (that
  769. * is, it finds the offset of the node which contains the node at
  770. * nodeoffset as a subnode).
  771. *
  772. * NOTE: This function is expensive, as it must scan the device tree
  773. * structure from the start to nodeoffset, *twice*.
  774. *
  775. * returns:
  776. * structure block offset of the parent of the node at nodeoffset
  777. * (>=0), on success
  778. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  779. * -FDT_ERR_BADMAGIC,
  780. * -FDT_ERR_BADVERSION,
  781. * -FDT_ERR_BADSTATE,
  782. * -FDT_ERR_BADSTRUCTURE, standard meanings
  783. */
  784. int fdt_parent_offset(const void *fdt, int nodeoffset);
  785. /**
  786. * fdt_node_offset_by_prop_value - find nodes with a given property value
  787. * @fdt: pointer to the device tree blob
  788. * @startoffset: only find nodes after this offset
  789. * @propname: property name to check
  790. * @propval: property value to search for
  791. * @proplen: length of the value in propval
  792. *
  793. * fdt_node_offset_by_prop_value() returns the offset of the first
  794. * node after startoffset, which has a property named propname whose
  795. * value is of length proplen and has value equal to propval; or if
  796. * startoffset is -1, the very first such node in the tree.
  797. *
  798. * To iterate through all nodes matching the criterion, the following
  799. * idiom can be used:
  800. * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
  801. * propval, proplen);
  802. * while (offset != -FDT_ERR_NOTFOUND) {
  803. * // other code here
  804. * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
  805. * propval, proplen);
  806. * }
  807. *
  808. * Note the -1 in the first call to the function, if 0 is used here
  809. * instead, the function will never locate the root node, even if it
  810. * matches the criterion.
  811. *
  812. * returns:
  813. * structure block offset of the located node (>= 0, >startoffset),
  814. * on success
  815. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  816. * tree after startoffset
  817. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  818. * -FDT_ERR_BADMAGIC,
  819. * -FDT_ERR_BADVERSION,
  820. * -FDT_ERR_BADSTATE,
  821. * -FDT_ERR_BADSTRUCTURE, standard meanings
  822. */
  823. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  824. const char *propname,
  825. const void *propval, int proplen);
  826. /**
  827. * fdt_node_offset_by_phandle - find the node with a given phandle
  828. * @fdt: pointer to the device tree blob
  829. * @phandle: phandle value
  830. *
  831. * fdt_node_offset_by_phandle() returns the offset of the node
  832. * which has the given phandle value. If there is more than one node
  833. * in the tree with the given phandle (an invalid tree), results are
  834. * undefined.
  835. *
  836. * returns:
  837. * structure block offset of the located node (>= 0), on success
  838. * -FDT_ERR_NOTFOUND, no node with that phandle exists
  839. * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
  840. * -FDT_ERR_BADMAGIC,
  841. * -FDT_ERR_BADVERSION,
  842. * -FDT_ERR_BADSTATE,
  843. * -FDT_ERR_BADSTRUCTURE, standard meanings
  844. */
  845. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
  846. /**
  847. * fdt_node_check_compatible: check a node's compatible property
  848. * @fdt: pointer to the device tree blob
  849. * @nodeoffset: offset of a tree node
  850. * @compatible: string to match against
  851. *
  852. *
  853. * fdt_node_check_compatible() returns 0 if the given node contains a
  854. * 'compatible' property with the given string as one of its elements,
  855. * it returns non-zero otherwise, or on error.
  856. *
  857. * returns:
  858. * 0, if the node has a 'compatible' property listing the given string
  859. * 1, if the node has a 'compatible' property, but it does not list
  860. * the given string
  861. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  862. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  863. * -FDT_ERR_BADMAGIC,
  864. * -FDT_ERR_BADVERSION,
  865. * -FDT_ERR_BADSTATE,
  866. * -FDT_ERR_BADSTRUCTURE, standard meanings
  867. */
  868. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  869. const char *compatible);
  870. /**
  871. * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
  872. * @fdt: pointer to the device tree blob
  873. * @startoffset: only find nodes after this offset
  874. * @compatible: 'compatible' string to match against
  875. *
  876. * fdt_node_offset_by_compatible() returns the offset of the first
  877. * node after startoffset, which has a 'compatible' property which
  878. * lists the given compatible string; or if startoffset is -1, the
  879. * very first such node in the tree.
  880. *
  881. * To iterate through all nodes matching the criterion, the following
  882. * idiom can be used:
  883. * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
  884. * while (offset != -FDT_ERR_NOTFOUND) {
  885. * // other code here
  886. * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
  887. * }
  888. *
  889. * Note the -1 in the first call to the function, if 0 is used here
  890. * instead, the function will never locate the root node, even if it
  891. * matches the criterion.
  892. *
  893. * returns:
  894. * structure block offset of the located node (>= 0, >startoffset),
  895. * on success
  896. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  897. * tree after startoffset
  898. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  899. * -FDT_ERR_BADMAGIC,
  900. * -FDT_ERR_BADVERSION,
  901. * -FDT_ERR_BADSTATE,
  902. * -FDT_ERR_BADSTRUCTURE, standard meanings
  903. */
  904. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  905. const char *compatible);
  906. /**
  907. * fdt_stringlist_contains - check a string list property for a string
  908. * @strlist: Property containing a list of strings to check
  909. * @listlen: Length of property
  910. * @str: String to search for
  911. *
  912. * This is a utility function provided for convenience. The list contains
  913. * one or more strings, each terminated by \0, as is found in a device tree
  914. * "compatible" property.
  915. *
  916. * @return: 1 if the string is found in the list, 0 not found, or invalid list
  917. */
  918. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
  919. /**
  920. * fdt_stringlist_count - count the number of strings in a string list
  921. * @fdt: pointer to the device tree blob
  922. * @nodeoffset: offset of a tree node
  923. * @property: name of the property containing the string list
  924. * @return:
  925. * the number of strings in the given property
  926. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  927. * -FDT_ERR_NOTFOUND if the property does not exist
  928. */
  929. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
  930. /**
  931. * fdt_stringlist_search - find a string in a string list and return its index
  932. * @fdt: pointer to the device tree blob
  933. * @nodeoffset: offset of a tree node
  934. * @property: name of the property containing the string list
  935. * @string: string to look up in the string list
  936. *
  937. * Note that it is possible for this function to succeed on property values
  938. * that are not NUL-terminated. That's because the function will stop after
  939. * finding the first occurrence of @string. This can for example happen with
  940. * small-valued cell properties, such as #address-cells, when searching for
  941. * the empty string.
  942. *
  943. * @return:
  944. * the index of the string in the list of strings
  945. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  946. * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
  947. * the given string
  948. */
  949. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  950. const char *string);
  951. /**
  952. * fdt_stringlist_get() - obtain the string at a given index in a string list
  953. * @fdt: pointer to the device tree blob
  954. * @nodeoffset: offset of a tree node
  955. * @property: name of the property containing the string list
  956. * @index: index of the string to return
  957. * @lenp: return location for the string length or an error code on failure
  958. *
  959. * Note that this will successfully extract strings from properties with
  960. * non-NUL-terminated values. For example on small-valued cell properties
  961. * this function will return the empty string.
  962. *
  963. * If non-NULL, the length of the string (on success) or a negative error-code
  964. * (on failure) will be stored in the integer pointer to by lenp.
  965. *
  966. * @return:
  967. * A pointer to the string at the given index in the string list or NULL on
  968. * failure. On success the length of the string will be stored in the memory
  969. * location pointed to by the lenp parameter, if non-NULL. On failure one of
  970. * the following negative error codes will be returned in the lenp parameter
  971. * (if non-NULL):
  972. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  973. * -FDT_ERR_NOTFOUND if the property does not exist
  974. */
  975. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  976. const char *property, int index,
  977. int *lenp);
  978. /**********************************************************************/
  979. /* Read-only functions (addressing related) */
  980. /**********************************************************************/
  981. /**
  982. * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
  983. *
  984. * This is the maximum value for #address-cells, #size-cells and
  985. * similar properties that will be processed by libfdt. IEE1275
  986. * requires that OF implementations handle values up to 4.
  987. * Implementations may support larger values, but in practice higher
  988. * values aren't used.
  989. */
  990. #define FDT_MAX_NCELLS 4
  991. /**
  992. * fdt_address_cells - retrieve address size for a bus represented in the tree
  993. * @fdt: pointer to the device tree blob
  994. * @nodeoffset: offset of the node to find the address size for
  995. *
  996. * When the node has a valid #address-cells property, returns its value.
  997. *
  998. * returns:
  999. * 0 <= n < FDT_MAX_NCELLS, on success
  1000. * 2, if the node has no #address-cells property
  1001. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1002. * #address-cells property
  1003. * -FDT_ERR_BADMAGIC,
  1004. * -FDT_ERR_BADVERSION,
  1005. * -FDT_ERR_BADSTATE,
  1006. * -FDT_ERR_BADSTRUCTURE,
  1007. * -FDT_ERR_TRUNCATED, standard meanings
  1008. */
  1009. int fdt_address_cells(const void *fdt, int nodeoffset);
  1010. /**
  1011. * fdt_size_cells - retrieve address range size for a bus represented in the
  1012. * tree
  1013. * @fdt: pointer to the device tree blob
  1014. * @nodeoffset: offset of the node to find the address range size for
  1015. *
  1016. * When the node has a valid #size-cells property, returns its value.
  1017. *
  1018. * returns:
  1019. * 0 <= n < FDT_MAX_NCELLS, on success
  1020. * 2, if the node has no #address-cells property
  1021. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1022. * #size-cells property
  1023. * -FDT_ERR_BADMAGIC,
  1024. * -FDT_ERR_BADVERSION,
  1025. * -FDT_ERR_BADSTATE,
  1026. * -FDT_ERR_BADSTRUCTURE,
  1027. * -FDT_ERR_TRUNCATED, standard meanings
  1028. */
  1029. int fdt_size_cells(const void *fdt, int nodeoffset);
  1030. /**********************************************************************/
  1031. /* Write-in-place functions */
  1032. /**********************************************************************/
  1033. /**
  1034. * fdt_setprop_inplace_namelen_partial - change a property's value,
  1035. * but not its size
  1036. * @fdt: pointer to the device tree blob
  1037. * @nodeoffset: offset of the node whose property to change
  1038. * @name: name of the property to change
  1039. * @namelen: number of characters of name to consider
  1040. * @idx: index of the property to change in the array
  1041. * @val: pointer to data to replace the property value with
  1042. * @len: length of the property value
  1043. *
  1044. * Identical to fdt_setprop_inplace(), but modifies the given property
  1045. * starting from the given index, and using only the first characters
  1046. * of the name. It is useful when you want to manipulate only one value of
  1047. * an array and you have a string that doesn't end with \0.
  1048. */
  1049. int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
  1050. const char *name, int namelen,
  1051. uint32_t idx, const void *val,
  1052. int len);
  1053. /**
  1054. * fdt_setprop_inplace - change a property's value, but not its size
  1055. * @fdt: pointer to the device tree blob
  1056. * @nodeoffset: offset of the node whose property to change
  1057. * @name: name of the property to change
  1058. * @val: pointer to data to replace the property value with
  1059. * @len: length of the property value
  1060. *
  1061. * fdt_setprop_inplace() replaces the value of a given property with
  1062. * the data in val, of length len. This function cannot change the
  1063. * size of a property, and so will only work if len is equal to the
  1064. * current length of the property.
  1065. *
  1066. * This function will alter only the bytes in the blob which contain
  1067. * the given property value, and will not alter or move any other part
  1068. * of the tree.
  1069. *
  1070. * returns:
  1071. * 0, on success
  1072. * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
  1073. * -FDT_ERR_NOTFOUND, node does not have the named property
  1074. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1075. * -FDT_ERR_BADMAGIC,
  1076. * -FDT_ERR_BADVERSION,
  1077. * -FDT_ERR_BADSTATE,
  1078. * -FDT_ERR_BADSTRUCTURE,
  1079. * -FDT_ERR_TRUNCATED, standard meanings
  1080. */
  1081. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  1082. const void *val, int len);
  1083. /**
  1084. * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
  1085. * @fdt: pointer to the device tree blob
  1086. * @nodeoffset: offset of the node whose property to change
  1087. * @name: name of the property to change
  1088. * @val: 32-bit integer value to replace the property with
  1089. *
  1090. * fdt_setprop_inplace_u32() replaces the value of a given property
  1091. * with the 32-bit integer value in val, converting val to big-endian
  1092. * if necessary. This function cannot change the size of a property,
  1093. * and so will only work if the property already exists and has length
  1094. * 4.
  1095. *
  1096. * This function will alter only the bytes in the blob which contain
  1097. * the given property value, and will not alter or move any other part
  1098. * of the tree.
  1099. *
  1100. * returns:
  1101. * 0, on success
  1102. * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
  1103. * -FDT_ERR_NOTFOUND, node does not have the named property
  1104. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1105. * -FDT_ERR_BADMAGIC,
  1106. * -FDT_ERR_BADVERSION,
  1107. * -FDT_ERR_BADSTATE,
  1108. * -FDT_ERR_BADSTRUCTURE,
  1109. * -FDT_ERR_TRUNCATED, standard meanings
  1110. */
  1111. static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
  1112. const char *name, uint32_t val)
  1113. {
  1114. fdt32_t tmp = cpu_to_fdt32(val);
  1115. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1116. }
  1117. /**
  1118. * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
  1119. * @fdt: pointer to the device tree blob
  1120. * @nodeoffset: offset of the node whose property to change
  1121. * @name: name of the property to change
  1122. * @val: 64-bit integer value to replace the property with
  1123. *
  1124. * fdt_setprop_inplace_u64() replaces the value of a given property
  1125. * with the 64-bit integer value in val, converting val to big-endian
  1126. * if necessary. This function cannot change the size of a property,
  1127. * and so will only work if the property already exists and has length
  1128. * 8.
  1129. *
  1130. * This function will alter only the bytes in the blob which contain
  1131. * the given property value, and will not alter or move any other part
  1132. * of the tree.
  1133. *
  1134. * returns:
  1135. * 0, on success
  1136. * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
  1137. * -FDT_ERR_NOTFOUND, node does not have the named property
  1138. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1139. * -FDT_ERR_BADMAGIC,
  1140. * -FDT_ERR_BADVERSION,
  1141. * -FDT_ERR_BADSTATE,
  1142. * -FDT_ERR_BADSTRUCTURE,
  1143. * -FDT_ERR_TRUNCATED, standard meanings
  1144. */
  1145. static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
  1146. const char *name, uint64_t val)
  1147. {
  1148. fdt64_t tmp = cpu_to_fdt64(val);
  1149. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1150. }
  1151. /**
  1152. * fdt_setprop_inplace_cell - change the value of a single-cell property
  1153. *
  1154. * This is an alternative name for fdt_setprop_inplace_u32()
  1155. */
  1156. static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
  1157. const char *name, uint32_t val)
  1158. {
  1159. return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
  1160. }
  1161. /**
  1162. * fdt_nop_property - replace a property with nop tags
  1163. * @fdt: pointer to the device tree blob
  1164. * @nodeoffset: offset of the node whose property to nop
  1165. * @name: name of the property to nop
  1166. *
  1167. * fdt_nop_property() will replace a given property's representation
  1168. * in the blob with FDT_NOP tags, effectively removing it from the
  1169. * tree.
  1170. *
  1171. * This function will alter only the bytes in the blob which contain
  1172. * the property, and will not alter or move any other part of the
  1173. * tree.
  1174. *
  1175. * returns:
  1176. * 0, on success
  1177. * -FDT_ERR_NOTFOUND, node does not have the named property
  1178. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1179. * -FDT_ERR_BADMAGIC,
  1180. * -FDT_ERR_BADVERSION,
  1181. * -FDT_ERR_BADSTATE,
  1182. * -FDT_ERR_BADSTRUCTURE,
  1183. * -FDT_ERR_TRUNCATED, standard meanings
  1184. */
  1185. int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  1186. /**
  1187. * fdt_nop_node - replace a node (subtree) with nop tags
  1188. * @fdt: pointer to the device tree blob
  1189. * @nodeoffset: offset of the node to nop
  1190. *
  1191. * fdt_nop_node() will replace a given node's representation in the
  1192. * blob, including all its subnodes, if any, with FDT_NOP tags,
  1193. * effectively removing it from the tree.
  1194. *
  1195. * This function will alter only the bytes in the blob which contain
  1196. * the node and its properties and subnodes, and will not alter or
  1197. * move any other part of the tree.
  1198. *
  1199. * returns:
  1200. * 0, on success
  1201. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1202. * -FDT_ERR_BADMAGIC,
  1203. * -FDT_ERR_BADVERSION,
  1204. * -FDT_ERR_BADSTATE,
  1205. * -FDT_ERR_BADSTRUCTURE,
  1206. * -FDT_ERR_TRUNCATED, standard meanings
  1207. */
  1208. int fdt_nop_node(void *fdt, int nodeoffset);
  1209. /**********************************************************************/
  1210. /* Sequential write functions */
  1211. /**********************************************************************/
  1212. int fdt_create(void *buf, int bufsize);
  1213. int fdt_resize(void *fdt, void *buf, int bufsize);
  1214. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
  1215. int fdt_finish_reservemap(void *fdt);
  1216. int fdt_begin_node(void *fdt, const char *name);
  1217. int fdt_property(void *fdt, const char *name, const void *val, int len);
  1218. static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
  1219. {
  1220. fdt32_t tmp = cpu_to_fdt32(val);
  1221. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1222. }
  1223. static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
  1224. {
  1225. fdt64_t tmp = cpu_to_fdt64(val);
  1226. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1227. }
  1228. static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
  1229. {
  1230. return fdt_property_u32(fdt, name, val);
  1231. }
  1232. /**
  1233. * fdt_property_placeholder - add a new property and return a ptr to its value
  1234. *
  1235. * @fdt: pointer to the device tree blob
  1236. * @name: name of property to add
  1237. * @len: length of property value in bytes
  1238. * @valp: returns a pointer to where where the value should be placed
  1239. *
  1240. * returns:
  1241. * 0, on success
  1242. * -FDT_ERR_BADMAGIC,
  1243. * -FDT_ERR_NOSPACE, standard meanings
  1244. */
  1245. int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
  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_appendprop - append to or create a property
  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 append to
  1470. * @val: pointer to data to append to the property value
  1471. * @len: length of the data to append to the property value
  1472. *
  1473. * fdt_appendprop() appends the value to the named property in the
  1474. * given node, creating the property if it does not already exist.
  1475. *
  1476. * This function may insert data into the blob, and will therefore
  1477. * change the offsets of some existing nodes.
  1478. *
  1479. * returns:
  1480. * 0, on success
  1481. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1482. * contain the new property value
  1483. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1484. * -FDT_ERR_BADLAYOUT,
  1485. * -FDT_ERR_BADMAGIC,
  1486. * -FDT_ERR_BADVERSION,
  1487. * -FDT_ERR_BADSTATE,
  1488. * -FDT_ERR_BADSTRUCTURE,
  1489. * -FDT_ERR_BADLAYOUT,
  1490. * -FDT_ERR_TRUNCATED, standard meanings
  1491. */
  1492. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  1493. const void *val, int len);
  1494. /**
  1495. * fdt_appendprop_u32 - append a 32-bit integer value to a property
  1496. * @fdt: pointer to the device tree blob
  1497. * @nodeoffset: offset of the node whose property to change
  1498. * @name: name of the property to change
  1499. * @val: 32-bit integer value to append to the property (native endian)
  1500. *
  1501. * fdt_appendprop_u32() appends the given 32-bit integer value
  1502. * (converting to big-endian if necessary) to the value of the named
  1503. * property in the given node, or creates a new property with that
  1504. * value if it does not already exist.
  1505. *
  1506. * This function may insert data into the blob, and will therefore
  1507. * change the offsets of some existing nodes.
  1508. *
  1509. * returns:
  1510. * 0, on success
  1511. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1512. * contain the new property value
  1513. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1514. * -FDT_ERR_BADLAYOUT,
  1515. * -FDT_ERR_BADMAGIC,
  1516. * -FDT_ERR_BADVERSION,
  1517. * -FDT_ERR_BADSTATE,
  1518. * -FDT_ERR_BADSTRUCTURE,
  1519. * -FDT_ERR_BADLAYOUT,
  1520. * -FDT_ERR_TRUNCATED, standard meanings
  1521. */
  1522. static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
  1523. const char *name, uint32_t val)
  1524. {
  1525. fdt32_t tmp = cpu_to_fdt32(val);
  1526. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1527. }
  1528. /**
  1529. * fdt_appendprop_u64 - append a 64-bit integer value to a property
  1530. * @fdt: pointer to the device tree blob
  1531. * @nodeoffset: offset of the node whose property to change
  1532. * @name: name of the property to change
  1533. * @val: 64-bit integer value to append to the property (native endian)
  1534. *
  1535. * fdt_appendprop_u64() appends the given 64-bit integer value
  1536. * (converting to big-endian if necessary) to the value of the named
  1537. * property in the given node, or creates a new property with that
  1538. * value if it does not already exist.
  1539. *
  1540. * This function may insert data into the blob, and will therefore
  1541. * change the offsets of some existing nodes.
  1542. *
  1543. * returns:
  1544. * 0, on success
  1545. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1546. * contain the new property value
  1547. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1548. * -FDT_ERR_BADLAYOUT,
  1549. * -FDT_ERR_BADMAGIC,
  1550. * -FDT_ERR_BADVERSION,
  1551. * -FDT_ERR_BADSTATE,
  1552. * -FDT_ERR_BADSTRUCTURE,
  1553. * -FDT_ERR_BADLAYOUT,
  1554. * -FDT_ERR_TRUNCATED, standard meanings
  1555. */
  1556. static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
  1557. const char *name, uint64_t val)
  1558. {
  1559. fdt64_t tmp = cpu_to_fdt64(val);
  1560. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1561. }
  1562. /**
  1563. * fdt_appendprop_cell - append a single cell value to a property
  1564. *
  1565. * This is an alternative name for fdt_appendprop_u32()
  1566. */
  1567. static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
  1568. const char *name, uint32_t val)
  1569. {
  1570. return fdt_appendprop_u32(fdt, nodeoffset, name, val);
  1571. }
  1572. /**
  1573. * fdt_appendprop_string - append a string to a property
  1574. * @fdt: pointer to the device tree blob
  1575. * @nodeoffset: offset of the node whose property to change
  1576. * @name: name of the property to change
  1577. * @str: string value to append to the property
  1578. *
  1579. * fdt_appendprop_string() appends the given string to the value of
  1580. * the named property in the given node, or creates a new property
  1581. * with that value if it does not already exist.
  1582. *
  1583. * This function may insert data into the blob, and will therefore
  1584. * change the offsets of some existing nodes.
  1585. *
  1586. * returns:
  1587. * 0, on success
  1588. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1589. * contain the new property value
  1590. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1591. * -FDT_ERR_BADLAYOUT,
  1592. * -FDT_ERR_BADMAGIC,
  1593. * -FDT_ERR_BADVERSION,
  1594. * -FDT_ERR_BADSTATE,
  1595. * -FDT_ERR_BADSTRUCTURE,
  1596. * -FDT_ERR_BADLAYOUT,
  1597. * -FDT_ERR_TRUNCATED, standard meanings
  1598. */
  1599. #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
  1600. fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1601. /**
  1602. * fdt_delprop - delete a property
  1603. * @fdt: pointer to the device tree blob
  1604. * @nodeoffset: offset of the node whose property to nop
  1605. * @name: name of the property to nop
  1606. *
  1607. * fdt_del_property() will delete the given property.
  1608. *
  1609. * This function will delete data from the blob, and will therefore
  1610. * change the offsets of some existing nodes.
  1611. *
  1612. * returns:
  1613. * 0, on success
  1614. * -FDT_ERR_NOTFOUND, node does not have the named property
  1615. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1616. * -FDT_ERR_BADLAYOUT,
  1617. * -FDT_ERR_BADMAGIC,
  1618. * -FDT_ERR_BADVERSION,
  1619. * -FDT_ERR_BADSTATE,
  1620. * -FDT_ERR_BADSTRUCTURE,
  1621. * -FDT_ERR_TRUNCATED, standard meanings
  1622. */
  1623. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  1624. /**
  1625. * fdt_add_subnode_namelen - creates a new node based on substring
  1626. * @fdt: pointer to the device tree blob
  1627. * @parentoffset: structure block offset of a node
  1628. * @name: name of the subnode to locate
  1629. * @namelen: number of characters of name to consider
  1630. *
  1631. * Identical to fdt_add_subnode(), but use only the first namelen
  1632. * characters of name as the name of the new node. This is useful for
  1633. * creating subnodes based on a portion of a larger string, such as a
  1634. * full path.
  1635. */
  1636. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  1637. const char *name, int namelen);
  1638. /**
  1639. * fdt_add_subnode - creates a new node
  1640. * @fdt: pointer to the device tree blob
  1641. * @parentoffset: structure block offset of a node
  1642. * @name: name of the subnode to locate
  1643. *
  1644. * fdt_add_subnode() creates a new node as a subnode of the node at
  1645. * structure block offset parentoffset, with the given name (which
  1646. * should include the unit address, if any).
  1647. *
  1648. * This function will insert data into the blob, and will therefore
  1649. * change the offsets of some existing nodes.
  1650. * returns:
  1651. * structure block offset of the created nodeequested subnode (>=0), on
  1652. * success
  1653. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  1654. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  1655. * tag
  1656. * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
  1657. * the given name
  1658. * -FDT_ERR_NOSPACE, if there is insufficient free space in the
  1659. * blob to contain the new node
  1660. * -FDT_ERR_NOSPACE
  1661. * -FDT_ERR_BADLAYOUT
  1662. * -FDT_ERR_BADMAGIC,
  1663. * -FDT_ERR_BADVERSION,
  1664. * -FDT_ERR_BADSTATE,
  1665. * -FDT_ERR_BADSTRUCTURE,
  1666. * -FDT_ERR_TRUNCATED, standard meanings.
  1667. */
  1668. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  1669. /**
  1670. * fdt_del_node - delete a node (subtree)
  1671. * @fdt: pointer to the device tree blob
  1672. * @nodeoffset: offset of the node to nop
  1673. *
  1674. * fdt_del_node() will remove the given node, including all its
  1675. * subnodes if any, from the blob.
  1676. *
  1677. * This function will delete data from the blob, and will therefore
  1678. * change the offsets of some existing nodes.
  1679. *
  1680. * returns:
  1681. * 0, on success
  1682. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1683. * -FDT_ERR_BADLAYOUT,
  1684. * -FDT_ERR_BADMAGIC,
  1685. * -FDT_ERR_BADVERSION,
  1686. * -FDT_ERR_BADSTATE,
  1687. * -FDT_ERR_BADSTRUCTURE,
  1688. * -FDT_ERR_TRUNCATED, standard meanings
  1689. */
  1690. int fdt_del_node(void *fdt, int nodeoffset);
  1691. /**
  1692. * fdt_overlay_apply - Applies a DT overlay on a base DT
  1693. * @fdt: pointer to the base device tree blob
  1694. * @fdto: pointer to the device tree overlay blob
  1695. *
  1696. * fdt_overlay_apply() will apply the given device tree overlay on the
  1697. * given base device tree.
  1698. *
  1699. * Expect the base device tree to be modified, even if the function
  1700. * returns an error.
  1701. *
  1702. * returns:
  1703. * 0, on success
  1704. * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
  1705. * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
  1706. * properties in the base DT
  1707. * -FDT_ERR_BADPHANDLE,
  1708. * -FDT_ERR_BADOVERLAY,
  1709. * -FDT_ERR_NOPHANDLES,
  1710. * -FDT_ERR_INTERNAL,
  1711. * -FDT_ERR_BADLAYOUT,
  1712. * -FDT_ERR_BADMAGIC,
  1713. * -FDT_ERR_BADOFFSET,
  1714. * -FDT_ERR_BADPATH,
  1715. * -FDT_ERR_BADVERSION,
  1716. * -FDT_ERR_BADSTRUCTURE,
  1717. * -FDT_ERR_BADSTATE,
  1718. * -FDT_ERR_TRUNCATED, standard meanings
  1719. */
  1720. int fdt_overlay_apply(void *fdt, void *fdto);
  1721. /**********************************************************************/
  1722. /* Debugging / informational functions */
  1723. /**********************************************************************/
  1724. const char *fdt_strerror(int errval);
  1725. /**
  1726. * fdt_remove_unused_strings() - Remove any unused strings from an FDT
  1727. *
  1728. * This creates a new device tree in @new with unused strings removed. The
  1729. * called can then use fdt_pack() to minimise the space consumed.
  1730. *
  1731. * @old: Old device tree blog
  1732. * @new: Place to put new device tree blob, which must be as large as
  1733. * @old
  1734. * @return
  1735. * 0, on success
  1736. * -FDT_ERR_BADOFFSET, corrupt device tree
  1737. * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
  1738. * is something very wrong with the device tree input
  1739. */
  1740. int fdt_remove_unused_strings(const void *old, void *new);
  1741. struct fdt_region {
  1742. int offset;
  1743. int size;
  1744. };
  1745. /*
  1746. * Flags for fdt_find_regions()
  1747. *
  1748. * Add a region for the string table (always the last region)
  1749. */
  1750. #define FDT_REG_ADD_STRING_TAB (1 << 0)
  1751. /*
  1752. * Add all supernodes of a matching node/property, useful for creating a
  1753. * valid subset tree
  1754. */
  1755. #define FDT_REG_SUPERNODES (1 << 1)
  1756. /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
  1757. #define FDT_REG_DIRECT_SUBNODES (1 << 2)
  1758. /* Add all subnodes of a matching node */
  1759. #define FDT_REG_ALL_SUBNODES (1 << 3)
  1760. /* Add a region for the mem_rsvmap table (always the first region) */
  1761. #define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
  1762. /* Indicates what an fdt part is (node, property, value) */
  1763. #define FDT_IS_NODE (1 << 0)
  1764. #define FDT_IS_PROP (1 << 1)
  1765. #define FDT_IS_VALUE (1 << 2) /* not supported */
  1766. #define FDT_IS_COMPAT (1 << 3) /* used internally */
  1767. #define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
  1768. #define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
  1769. FDT_IS_COMPAT)
  1770. #define FDT_IS_ANY 0x1f /* all the above */
  1771. /* We set a reasonable limit on the number of nested nodes */
  1772. #define FDT_MAX_DEPTH 32
  1773. /* Decribes what we want to include from the current tag */
  1774. enum want_t {
  1775. WANT_NOTHING,
  1776. WANT_NODES_ONLY, /* No properties */
  1777. WANT_NODES_AND_PROPS, /* Everything for one level */
  1778. WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
  1779. };
  1780. /* Keeps track of the state at parent nodes */
  1781. struct fdt_subnode_stack {
  1782. int offset; /* Offset of node */
  1783. enum want_t want; /* The 'want' value here */
  1784. int included; /* 1 if we included this node, 0 if not */
  1785. };
  1786. struct fdt_region_ptrs {
  1787. int depth; /* Current tree depth */
  1788. int done; /* What we have completed scanning */
  1789. enum want_t want; /* What we are currently including */
  1790. char *end; /* Pointer to end of full node path */
  1791. int nextoffset; /* Next node offset to check */
  1792. };
  1793. /* The state of our finding algortihm */
  1794. struct fdt_region_state {
  1795. struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
  1796. struct fdt_region *region; /* Contains list of regions found */
  1797. int count; /* Numnber of regions found */
  1798. const void *fdt; /* FDT blob */
  1799. int max_regions; /* Maximum regions to find */
  1800. int can_merge; /* 1 if we can merge with previous region */
  1801. int start; /* Start position of current region */
  1802. struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
  1803. };
  1804. /**
  1805. * fdt_find_regions() - find regions in device tree
  1806. *
  1807. * Given a list of nodes to include and properties to exclude, find
  1808. * the regions of the device tree which describe those included parts.
  1809. *
  1810. * The intent is to get a list of regions which will be invariant provided
  1811. * those parts are invariant. For example, if you request a list of regions
  1812. * for all nodes but exclude the property "data", then you will get the
  1813. * same region contents regardless of any change to "data" properties.
  1814. *
  1815. * This function can be used to produce a byte-stream to send to a hashing
  1816. * function to verify that critical parts of the FDT have not changed.
  1817. *
  1818. * Nodes which are given in 'inc' are included in the region list, as
  1819. * are the names of the immediate subnodes nodes (but not the properties
  1820. * or subnodes of those subnodes).
  1821. *
  1822. * For eaxample "/" means to include the root node, all root properties
  1823. * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
  1824. * ensures that we capture the names of the subnodes. In a hashing situation
  1825. * it prevents the root node from changing at all Any change to non-excluded
  1826. * properties, names of subnodes or number of subnodes would be detected.
  1827. *
  1828. * When used with FITs this provides the ability to hash and sign parts of
  1829. * the FIT based on different configurations in the FIT. Then it is
  1830. * impossible to change anything about that configuration (include images
  1831. * attached to the configuration), but it may be possible to add new
  1832. * configurations, new images or new signatures within the existing
  1833. * framework.
  1834. *
  1835. * Adding new properties to a device tree may result in the string table
  1836. * being extended (if the new property names are different from those
  1837. * already added). This function can optionally include a region for
  1838. * the string table so that this can be part of the hash too.
  1839. *
  1840. * The device tree header is not included in the list.
  1841. *
  1842. * @fdt: Device tree to check
  1843. * @inc: List of node paths to included
  1844. * @inc_count: Number of node paths in list
  1845. * @exc_prop: List of properties names to exclude
  1846. * @exc_prop_count: Number of properties in exclude list
  1847. * @region: Returns list of regions
  1848. * @max_region: Maximum length of region list
  1849. * @path: Pointer to a temporary string for the function to use for
  1850. * building path names
  1851. * @path_len: Length of path, must be large enough to hold the longest
  1852. * path in the tree
  1853. * @add_string_tab: 1 to add a region for the string table
  1854. * @return number of regions in list. If this is >max_regions then the
  1855. * region array was exhausted. You should increase max_regions and try
  1856. * the call again.
  1857. */
  1858. int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
  1859. char * const exc_prop[], int exc_prop_count,
  1860. struct fdt_region region[], int max_regions,
  1861. char *path, int path_len, int add_string_tab);
  1862. /**
  1863. * fdt_first_region() - find regions in device tree
  1864. *
  1865. * Given a nodes and properties to include and properties to exclude, find
  1866. * the regions of the device tree which describe those included parts.
  1867. *
  1868. * The use for this function is twofold. Firstly it provides a convenient
  1869. * way of performing a structure-aware grep of the tree. For example it is
  1870. * possible to grep for a node and get all the properties associated with
  1871. * that node. Trees can be subsetted easily, by specifying the nodes that
  1872. * are required, and then writing out the regions returned by this function.
  1873. * This is useful for small resource-constrained systems, such as boot
  1874. * loaders, which want to use an FDT but do not need to know about all of
  1875. * it.
  1876. *
  1877. * Secondly it makes it easy to hash parts of the tree and detect changes.
  1878. * The intent is to get a list of regions which will be invariant provided
  1879. * those parts are invariant. For example, if you request a list of regions
  1880. * for all nodes but exclude the property "data", then you will get the
  1881. * same region contents regardless of any change to "data" properties.
  1882. *
  1883. * This function can be used to produce a byte-stream to send to a hashing
  1884. * function to verify that critical parts of the FDT have not changed.
  1885. * Note that semantically null changes in order could still cause false
  1886. * hash misses. Such reordering might happen if the tree is regenerated
  1887. * from source, and nodes are reordered (the bytes-stream will be emitted
  1888. * in a different order and mnay hash functions will detect this). However
  1889. * if an existing tree is modified using libfdt functions, such as
  1890. * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
  1891. *
  1892. * The nodes/properties to include/exclude are defined by a function
  1893. * provided by the caller. This function is called for each node and
  1894. * property, and must return:
  1895. *
  1896. * 0 - to exclude this part
  1897. * 1 - to include this part
  1898. * -1 - for FDT_IS_PROP only: no information is available, so include
  1899. * if its containing node is included
  1900. *
  1901. * The last case is only used to deal with properties. Often a property is
  1902. * included if its containing node is included - this is the case where
  1903. * -1 is returned.. However if the property is specifically required to be
  1904. * included/excluded, then 0 or 1 can be returned. Note that including a
  1905. * property when the FDT_REG_SUPERNODES flag is given will force its
  1906. * containing node to be included since it is not valid to have a property
  1907. * that is not in a node.
  1908. *
  1909. * Using the information provided, the inclusion of a node can be controlled
  1910. * either by a node name or its compatible string, or any other property
  1911. * that the function can determine.
  1912. *
  1913. * As an example, including node "/" means to include the root node and all
  1914. * root properties. A flag provides a way of also including supernodes (of
  1915. * which there is none for the root node), and another flag includes
  1916. * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
  1917. * FDT_END_NODE of all subnodes of /.
  1918. *
  1919. * The subnode feature helps in a hashing situation since it prevents the
  1920. * root node from changing at all. Any change to non-excluded properties,
  1921. * names of subnodes or number of subnodes would be detected.
  1922. *
  1923. * When used with FITs this provides the ability to hash and sign parts of
  1924. * the FIT based on different configurations in the FIT. Then it is
  1925. * impossible to change anything about that configuration (include images
  1926. * attached to the configuration), but it may be possible to add new
  1927. * configurations, new images or new signatures within the existing
  1928. * framework.
  1929. *
  1930. * Adding new properties to a device tree may result in the string table
  1931. * being extended (if the new property names are different from those
  1932. * already added). This function can optionally include a region for
  1933. * the string table so that this can be part of the hash too. This is always
  1934. * the last region.
  1935. *
  1936. * The FDT also has a mem_rsvmap table which can also be included, and is
  1937. * always the first region if so.
  1938. *
  1939. * The device tree header is not included in the region list. Since the
  1940. * contents of the FDT are changing (shrinking, often), the caller will need
  1941. * to regenerate the header anyway.
  1942. *
  1943. * @fdt: Device tree to check
  1944. * @h_include: Function to call to determine whether to include a part or
  1945. * not:
  1946. *
  1947. * @priv: Private pointer as passed to fdt_find_regions()
  1948. * @fdt: Pointer to FDT blob
  1949. * @offset: Offset of this node / property
  1950. * @type: Type of this part, FDT_IS_...
  1951. * @data: Pointer to data (node name, property name, compatible
  1952. * string, value (not yet supported)
  1953. * @size: Size of data, or 0 if none
  1954. * @return 0 to exclude, 1 to include, -1 if no information is
  1955. * available
  1956. * @priv: Private pointer passed to h_include
  1957. * @region: Returns list of regions, sorted by offset
  1958. * @max_regions: Maximum length of region list
  1959. * @path: Pointer to a temporary string for the function to use for
  1960. * building path names
  1961. * @path_len: Length of path, must be large enough to hold the longest
  1962. * path in the tree
  1963. * @flags: Various flags that control the region algortihm, see
  1964. * FDT_REG_...
  1965. * @return number of regions in list. If this is >max_regions then the
  1966. * region array was exhausted. You should increase max_regions and try
  1967. * the call again. Only the first max_regions elements are available in the
  1968. * array.
  1969. *
  1970. * On error a -ve value is return, which can be:
  1971. *
  1972. * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
  1973. * -FDT_ERR_BADLAYOUT
  1974. * -FDT_ERR_NOSPACE (path area is too small)
  1975. */
  1976. int fdt_first_region(const void *fdt,
  1977. int (*h_include)(void *priv, const void *fdt, int offset,
  1978. int type, const char *data, int size),
  1979. void *priv, struct fdt_region *region,
  1980. char *path, int path_len, int flags,
  1981. struct fdt_region_state *info);
  1982. /** fdt_next_region() - find next region
  1983. *
  1984. * See fdt_first_region() for full description. This function finds the
  1985. * next region according to the provided parameters, which must be the same
  1986. * as passed to fdt_first_region().
  1987. *
  1988. * This function can additionally return -FDT_ERR_NOTFOUND when there are no
  1989. * more regions
  1990. */
  1991. int fdt_next_region(const void *fdt,
  1992. int (*h_include)(void *priv, const void *fdt, int offset,
  1993. int type, const char *data, int size),
  1994. void *priv, struct fdt_region *region,
  1995. char *path, int path_len, int flags,
  1996. struct fdt_region_state *info);
  1997. /**
  1998. * fdt_add_alias_regions() - find aliases that point to existing regions
  1999. *
  2000. * Once a device tree grep is complete some of the nodes will be present
  2001. * and some will have been dropped. This function checks all the alias nodes
  2002. * to figure out which points point to nodes which are still present. These
  2003. * aliases need to be kept, along with the nodes they reference.
  2004. *
  2005. * Given a list of regions function finds the aliases that still apply and
  2006. * adds more regions to the list for these. This function is called after
  2007. * fdt_next_region() has finished returning regions and requires the same
  2008. * state.
  2009. *
  2010. * @fdt: Device tree file to reference
  2011. * @region: List of regions that will be kept
  2012. * @count: Number of regions
  2013. * @max_regions: Number of entries that can fit in @region
  2014. * @info: Region state as returned from fdt_next_region()
  2015. * @return new number of regions in @region (i.e. count + the number added)
  2016. * or -FDT_ERR_NOSPACE if there was not enough space.
  2017. */
  2018. int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
  2019. int max_regions, struct fdt_region_state *info);
  2020. #endif /* _LIBFDT_H */