libfdt.h 78 KB

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