libfdt.h 78 KB

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