libfdt.h 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139
  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_u32 - set a property to a 32-bit integer
  1329. * @fdt: pointer to the device tree blob
  1330. * @nodeoffset: offset of the node whose property to change
  1331. * @name: name of the property to change
  1332. * @val: 32-bit integer value for the property (native endian)
  1333. *
  1334. * fdt_setprop_u32() sets the value of the named property in the given
  1335. * node to the given 32-bit integer value (converting to big-endian if
  1336. * necessary), or creates a new property with that value if it does
  1337. * not already exist.
  1338. *
  1339. * This function may insert or delete data from the blob, and will
  1340. * therefore change the offsets of some existing nodes.
  1341. *
  1342. * returns:
  1343. * 0, on success
  1344. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1345. * contain the new property value
  1346. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1347. * -FDT_ERR_BADLAYOUT,
  1348. * -FDT_ERR_BADMAGIC,
  1349. * -FDT_ERR_BADVERSION,
  1350. * -FDT_ERR_BADSTATE,
  1351. * -FDT_ERR_BADSTRUCTURE,
  1352. * -FDT_ERR_BADLAYOUT,
  1353. * -FDT_ERR_TRUNCATED, standard meanings
  1354. */
  1355. static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
  1356. uint32_t val)
  1357. {
  1358. fdt32_t tmp = cpu_to_fdt32(val);
  1359. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1360. }
  1361. /**
  1362. * fdt_setprop_u64 - set a property to a 64-bit integer
  1363. * @fdt: pointer to the device tree blob
  1364. * @nodeoffset: offset of the node whose property to change
  1365. * @name: name of the property to change
  1366. * @val: 64-bit integer value for the property (native endian)
  1367. *
  1368. * fdt_setprop_u64() sets the value of the named property in the given
  1369. * node to the given 64-bit integer value (converting to big-endian if
  1370. * necessary), or creates a new property with that value if it does
  1371. * not already exist.
  1372. *
  1373. * This function may insert or delete data from the blob, and will
  1374. * therefore change the offsets of some existing nodes.
  1375. *
  1376. * returns:
  1377. * 0, on success
  1378. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1379. * contain the new property value
  1380. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1381. * -FDT_ERR_BADLAYOUT,
  1382. * -FDT_ERR_BADMAGIC,
  1383. * -FDT_ERR_BADVERSION,
  1384. * -FDT_ERR_BADSTATE,
  1385. * -FDT_ERR_BADSTRUCTURE,
  1386. * -FDT_ERR_BADLAYOUT,
  1387. * -FDT_ERR_TRUNCATED, standard meanings
  1388. */
  1389. static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
  1390. uint64_t val)
  1391. {
  1392. fdt64_t tmp = cpu_to_fdt64(val);
  1393. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1394. }
  1395. /**
  1396. * fdt_setprop_cell - set a property to a single cell value
  1397. *
  1398. * This is an alternative name for fdt_setprop_u32()
  1399. */
  1400. static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
  1401. uint32_t val)
  1402. {
  1403. return fdt_setprop_u32(fdt, nodeoffset, name, val);
  1404. }
  1405. /**
  1406. * fdt_setprop_string - set a property to a string value
  1407. * @fdt: pointer to the device tree blob
  1408. * @nodeoffset: offset of the node whose property to change
  1409. * @name: name of the property to change
  1410. * @str: string value for the property
  1411. *
  1412. * fdt_setprop_string() sets the value of the named property in the
  1413. * given node to the given string value (using the length of the
  1414. * string to determine the new length of the property), or creates a
  1415. * new property with that value if it does not already exist.
  1416. *
  1417. * This function may insert or delete data from the blob, and will
  1418. * therefore change the offsets of some existing nodes.
  1419. *
  1420. * returns:
  1421. * 0, on success
  1422. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1423. * contain the new property value
  1424. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1425. * -FDT_ERR_BADLAYOUT,
  1426. * -FDT_ERR_BADMAGIC,
  1427. * -FDT_ERR_BADVERSION,
  1428. * -FDT_ERR_BADSTATE,
  1429. * -FDT_ERR_BADSTRUCTURE,
  1430. * -FDT_ERR_BADLAYOUT,
  1431. * -FDT_ERR_TRUNCATED, standard meanings
  1432. */
  1433. #define fdt_setprop_string(fdt, nodeoffset, name, str) \
  1434. fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1435. /**
  1436. * fdt_setprop_empty - set a property to an empty 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. *
  1441. * fdt_setprop_empty() sets the value of the named property in the
  1442. * given node to an empty (zero length) value, or creates a new empty
  1443. * property if it does not already exist.
  1444. *
  1445. * This function may insert or delete data from the blob, and will
  1446. * therefore change the offsets of some existing nodes.
  1447. *
  1448. * returns:
  1449. * 0, on success
  1450. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1451. * contain the new property value
  1452. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1453. * -FDT_ERR_BADLAYOUT,
  1454. * -FDT_ERR_BADMAGIC,
  1455. * -FDT_ERR_BADVERSION,
  1456. * -FDT_ERR_BADSTATE,
  1457. * -FDT_ERR_BADSTRUCTURE,
  1458. * -FDT_ERR_BADLAYOUT,
  1459. * -FDT_ERR_TRUNCATED, standard meanings
  1460. */
  1461. #define fdt_setprop_empty(fdt, nodeoffset, name) \
  1462. fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
  1463. /**
  1464. * fdt_appendprop - append to or create a property
  1465. * @fdt: pointer to the device tree blob
  1466. * @nodeoffset: offset of the node whose property to change
  1467. * @name: name of the property to append to
  1468. * @val: pointer to data to append to the property value
  1469. * @len: length of the data to append to the property value
  1470. *
  1471. * fdt_appendprop() appends the value to the named property in the
  1472. * given node, creating the property if it does not already exist.
  1473. *
  1474. * This function may insert data into the blob, and will therefore
  1475. * change the offsets of some existing nodes.
  1476. *
  1477. * returns:
  1478. * 0, on success
  1479. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1480. * contain the new property value
  1481. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1482. * -FDT_ERR_BADLAYOUT,
  1483. * -FDT_ERR_BADMAGIC,
  1484. * -FDT_ERR_BADVERSION,
  1485. * -FDT_ERR_BADSTATE,
  1486. * -FDT_ERR_BADSTRUCTURE,
  1487. * -FDT_ERR_BADLAYOUT,
  1488. * -FDT_ERR_TRUNCATED, standard meanings
  1489. */
  1490. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  1491. const void *val, int len);
  1492. /**
  1493. * fdt_appendprop_u32 - append a 32-bit integer value to a property
  1494. * @fdt: pointer to the device tree blob
  1495. * @nodeoffset: offset of the node whose property to change
  1496. * @name: name of the property to change
  1497. * @val: 32-bit integer value to append to the property (native endian)
  1498. *
  1499. * fdt_appendprop_u32() appends the given 32-bit integer value
  1500. * (converting to big-endian if necessary) to the value of the named
  1501. * property in the given node, or creates a new property with that
  1502. * value 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. static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
  1521. const char *name, uint32_t val)
  1522. {
  1523. fdt32_t tmp = cpu_to_fdt32(val);
  1524. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1525. }
  1526. /**
  1527. * fdt_appendprop_u64 - append a 64-bit integer value to a property
  1528. * @fdt: pointer to the device tree blob
  1529. * @nodeoffset: offset of the node whose property to change
  1530. * @name: name of the property to change
  1531. * @val: 64-bit integer value to append to the property (native endian)
  1532. *
  1533. * fdt_appendprop_u64() appends the given 64-bit integer value
  1534. * (converting to big-endian if necessary) to the value of the named
  1535. * property in the given node, or creates a new property with that
  1536. * value if it does not already exist.
  1537. *
  1538. * This function may insert data into the blob, and will therefore
  1539. * change the offsets of some existing nodes.
  1540. *
  1541. * returns:
  1542. * 0, on success
  1543. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1544. * contain the new property value
  1545. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1546. * -FDT_ERR_BADLAYOUT,
  1547. * -FDT_ERR_BADMAGIC,
  1548. * -FDT_ERR_BADVERSION,
  1549. * -FDT_ERR_BADSTATE,
  1550. * -FDT_ERR_BADSTRUCTURE,
  1551. * -FDT_ERR_BADLAYOUT,
  1552. * -FDT_ERR_TRUNCATED, standard meanings
  1553. */
  1554. static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
  1555. const char *name, uint64_t val)
  1556. {
  1557. fdt64_t tmp = cpu_to_fdt64(val);
  1558. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1559. }
  1560. /**
  1561. * fdt_appendprop_cell - append a single cell value to a property
  1562. *
  1563. * This is an alternative name for fdt_appendprop_u32()
  1564. */
  1565. static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
  1566. const char *name, uint32_t val)
  1567. {
  1568. return fdt_appendprop_u32(fdt, nodeoffset, name, val);
  1569. }
  1570. /**
  1571. * fdt_appendprop_string - append a string to a property
  1572. * @fdt: pointer to the device tree blob
  1573. * @nodeoffset: offset of the node whose property to change
  1574. * @name: name of the property to change
  1575. * @str: string value to append to the property
  1576. *
  1577. * fdt_appendprop_string() appends the given string to the value of
  1578. * the named property in the given node, or creates a new property
  1579. * with that value if it does not already exist.
  1580. *
  1581. * This function may insert data into the blob, and will therefore
  1582. * change the offsets of some existing nodes.
  1583. *
  1584. * returns:
  1585. * 0, on success
  1586. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1587. * contain the new property value
  1588. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1589. * -FDT_ERR_BADLAYOUT,
  1590. * -FDT_ERR_BADMAGIC,
  1591. * -FDT_ERR_BADVERSION,
  1592. * -FDT_ERR_BADSTATE,
  1593. * -FDT_ERR_BADSTRUCTURE,
  1594. * -FDT_ERR_BADLAYOUT,
  1595. * -FDT_ERR_TRUNCATED, standard meanings
  1596. */
  1597. #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
  1598. fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1599. /**
  1600. * fdt_delprop - delete a property
  1601. * @fdt: pointer to the device tree blob
  1602. * @nodeoffset: offset of the node whose property to nop
  1603. * @name: name of the property to nop
  1604. *
  1605. * fdt_del_property() will delete the given property.
  1606. *
  1607. * This function will delete data from the blob, and will therefore
  1608. * change the offsets of some existing nodes.
  1609. *
  1610. * returns:
  1611. * 0, on success
  1612. * -FDT_ERR_NOTFOUND, node does not have the named property
  1613. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1614. * -FDT_ERR_BADLAYOUT,
  1615. * -FDT_ERR_BADMAGIC,
  1616. * -FDT_ERR_BADVERSION,
  1617. * -FDT_ERR_BADSTATE,
  1618. * -FDT_ERR_BADSTRUCTURE,
  1619. * -FDT_ERR_TRUNCATED, standard meanings
  1620. */
  1621. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  1622. /**
  1623. * fdt_add_subnode_namelen - creates a new node based on substring
  1624. * @fdt: pointer to the device tree blob
  1625. * @parentoffset: structure block offset of a node
  1626. * @name: name of the subnode to locate
  1627. * @namelen: number of characters of name to consider
  1628. *
  1629. * Identical to fdt_add_subnode(), but use only the first namelen
  1630. * characters of name as the name of the new node. This is useful for
  1631. * creating subnodes based on a portion of a larger string, such as a
  1632. * full path.
  1633. */
  1634. #ifndef SWIG /* Not available in Python */
  1635. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  1636. const char *name, int namelen);
  1637. #endif
  1638. /**
  1639. * fdt_add_subnode - creates a new node
  1640. * @fdt: pointer to the device tree blob
  1641. * @parentoffset: structure block offset of a node
  1642. * @name: name of the subnode to locate
  1643. *
  1644. * fdt_add_subnode() creates a new node as a subnode of the node at
  1645. * structure block offset parentoffset, with the given name (which
  1646. * should include the unit address, if any).
  1647. *
  1648. * This function will insert data into the blob, and will therefore
  1649. * change the offsets of some existing nodes.
  1650. * returns:
  1651. * structure block offset of the created nodeequested subnode (>=0), on
  1652. * success
  1653. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  1654. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  1655. * tag
  1656. * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
  1657. * the given name
  1658. * -FDT_ERR_NOSPACE, if there is insufficient free space in the
  1659. * blob to contain the new node
  1660. * -FDT_ERR_NOSPACE
  1661. * -FDT_ERR_BADLAYOUT
  1662. * -FDT_ERR_BADMAGIC,
  1663. * -FDT_ERR_BADVERSION,
  1664. * -FDT_ERR_BADSTATE,
  1665. * -FDT_ERR_BADSTRUCTURE,
  1666. * -FDT_ERR_TRUNCATED, standard meanings.
  1667. */
  1668. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  1669. /**
  1670. * fdt_del_node - delete a node (subtree)
  1671. * @fdt: pointer to the device tree blob
  1672. * @nodeoffset: offset of the node to nop
  1673. *
  1674. * fdt_del_node() will remove the given node, including all its
  1675. * subnodes if any, from the blob.
  1676. *
  1677. * This function will delete data from the blob, and will therefore
  1678. * change the offsets of some existing nodes.
  1679. *
  1680. * returns:
  1681. * 0, on success
  1682. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1683. * -FDT_ERR_BADLAYOUT,
  1684. * -FDT_ERR_BADMAGIC,
  1685. * -FDT_ERR_BADVERSION,
  1686. * -FDT_ERR_BADSTATE,
  1687. * -FDT_ERR_BADSTRUCTURE,
  1688. * -FDT_ERR_TRUNCATED, standard meanings
  1689. */
  1690. int fdt_del_node(void *fdt, int nodeoffset);
  1691. /**
  1692. * fdt_overlay_apply - Applies a DT overlay on a base DT
  1693. * @fdt: pointer to the base device tree blob
  1694. * @fdto: pointer to the device tree overlay blob
  1695. *
  1696. * fdt_overlay_apply() will apply the given device tree overlay on the
  1697. * given base device tree.
  1698. *
  1699. * Expect the base device tree to be modified, even if the function
  1700. * returns an error.
  1701. *
  1702. * returns:
  1703. * 0, on success
  1704. * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
  1705. * -FDT_ERR_NOTFOUND, the overlay points to some inexistant nodes or
  1706. * properties in the base DT
  1707. * -FDT_ERR_BADPHANDLE,
  1708. * -FDT_ERR_BADOVERLAY,
  1709. * -FDT_ERR_NOPHANDLES,
  1710. * -FDT_ERR_INTERNAL,
  1711. * -FDT_ERR_BADLAYOUT,
  1712. * -FDT_ERR_BADMAGIC,
  1713. * -FDT_ERR_BADOFFSET,
  1714. * -FDT_ERR_BADPATH,
  1715. * -FDT_ERR_BADVERSION,
  1716. * -FDT_ERR_BADSTRUCTURE,
  1717. * -FDT_ERR_BADSTATE,
  1718. * -FDT_ERR_TRUNCATED, standard meanings
  1719. */
  1720. int fdt_overlay_apply(void *fdt, void *fdto);
  1721. /**********************************************************************/
  1722. /* Debugging / informational functions */
  1723. /**********************************************************************/
  1724. #ifndef SWIG /* Not available in Python */
  1725. const char *fdt_strerror(int errval);
  1726. /**
  1727. * fdt_remove_unused_strings() - Remove any unused strings from an FDT
  1728. *
  1729. * This creates a new device tree in @new with unused strings removed. The
  1730. * called can then use fdt_pack() to minimise the space consumed.
  1731. *
  1732. * @old: Old device tree blog
  1733. * @new: Place to put new device tree blob, which must be as large as
  1734. * @old
  1735. * @return
  1736. * 0, on success
  1737. * -FDT_ERR_BADOFFSET, corrupt device tree
  1738. * -FDT_ERR_NOSPACE, out of space, which should not happen unless there
  1739. * is something very wrong with the device tree input
  1740. */
  1741. int fdt_remove_unused_strings(const void *old, void *new);
  1742. struct fdt_region {
  1743. int offset;
  1744. int size;
  1745. };
  1746. /*
  1747. * Flags for fdt_find_regions()
  1748. *
  1749. * Add a region for the string table (always the last region)
  1750. */
  1751. #define FDT_REG_ADD_STRING_TAB (1 << 0)
  1752. /*
  1753. * Add all supernodes of a matching node/property, useful for creating a
  1754. * valid subset tree
  1755. */
  1756. #define FDT_REG_SUPERNODES (1 << 1)
  1757. /* Add the FDT_BEGIN_NODE tags of subnodes, including their names */
  1758. #define FDT_REG_DIRECT_SUBNODES (1 << 2)
  1759. /* Add all subnodes of a matching node */
  1760. #define FDT_REG_ALL_SUBNODES (1 << 3)
  1761. /* Add a region for the mem_rsvmap table (always the first region) */
  1762. #define FDT_REG_ADD_MEM_RSVMAP (1 << 4)
  1763. /* Indicates what an fdt part is (node, property, value) */
  1764. #define FDT_IS_NODE (1 << 0)
  1765. #define FDT_IS_PROP (1 << 1)
  1766. #define FDT_IS_VALUE (1 << 2) /* not supported */
  1767. #define FDT_IS_COMPAT (1 << 3) /* used internally */
  1768. #define FDT_NODE_HAS_PROP (1 << 4) /* node contains prop */
  1769. #define FDT_ANY_GLOBAL (FDT_IS_NODE | FDT_IS_PROP | FDT_IS_VALUE | \
  1770. FDT_IS_COMPAT)
  1771. #define FDT_IS_ANY 0x1f /* all the above */
  1772. /* We set a reasonable limit on the number of nested nodes */
  1773. #define FDT_MAX_DEPTH 32
  1774. /* Decribes what we want to include from the current tag */
  1775. enum want_t {
  1776. WANT_NOTHING,
  1777. WANT_NODES_ONLY, /* No properties */
  1778. WANT_NODES_AND_PROPS, /* Everything for one level */
  1779. WANT_ALL_NODES_AND_PROPS /* Everything for all levels */
  1780. };
  1781. /* Keeps track of the state at parent nodes */
  1782. struct fdt_subnode_stack {
  1783. int offset; /* Offset of node */
  1784. enum want_t want; /* The 'want' value here */
  1785. int included; /* 1 if we included this node, 0 if not */
  1786. };
  1787. struct fdt_region_ptrs {
  1788. int depth; /* Current tree depth */
  1789. int done; /* What we have completed scanning */
  1790. enum want_t want; /* What we are currently including */
  1791. char *end; /* Pointer to end of full node path */
  1792. int nextoffset; /* Next node offset to check */
  1793. };
  1794. /* The state of our finding algortihm */
  1795. struct fdt_region_state {
  1796. struct fdt_subnode_stack stack[FDT_MAX_DEPTH]; /* node stack */
  1797. struct fdt_region *region; /* Contains list of regions found */
  1798. int count; /* Numnber of regions found */
  1799. const void *fdt; /* FDT blob */
  1800. int max_regions; /* Maximum regions to find */
  1801. int can_merge; /* 1 if we can merge with previous region */
  1802. int start; /* Start position of current region */
  1803. struct fdt_region_ptrs ptrs; /* Pointers for what we are up to */
  1804. };
  1805. /**
  1806. * fdt_find_regions() - find regions in device tree
  1807. *
  1808. * Given a list of nodes to include and properties to exclude, find
  1809. * the regions of the device tree which describe those included parts.
  1810. *
  1811. * The intent is to get a list of regions which will be invariant provided
  1812. * those parts are invariant. For example, if you request a list of regions
  1813. * for all nodes but exclude the property "data", then you will get the
  1814. * same region contents regardless of any change to "data" properties.
  1815. *
  1816. * This function can be used to produce a byte-stream to send to a hashing
  1817. * function to verify that critical parts of the FDT have not changed.
  1818. *
  1819. * Nodes which are given in 'inc' are included in the region list, as
  1820. * are the names of the immediate subnodes nodes (but not the properties
  1821. * or subnodes of those subnodes).
  1822. *
  1823. * For eaxample "/" means to include the root node, all root properties
  1824. * and the FDT_BEGIN_NODE and FDT_END_NODE of all subnodes of /. The latter
  1825. * ensures that we capture the names of the subnodes. In a hashing situation
  1826. * it prevents the root node from changing at all Any change to non-excluded
  1827. * properties, names of subnodes or number of subnodes would be detected.
  1828. *
  1829. * When used with FITs this provides the ability to hash and sign parts of
  1830. * the FIT based on different configurations in the FIT. Then it is
  1831. * impossible to change anything about that configuration (include images
  1832. * attached to the configuration), but it may be possible to add new
  1833. * configurations, new images or new signatures within the existing
  1834. * framework.
  1835. *
  1836. * Adding new properties to a device tree may result in the string table
  1837. * being extended (if the new property names are different from those
  1838. * already added). This function can optionally include a region for
  1839. * the string table so that this can be part of the hash too.
  1840. *
  1841. * The device tree header is not included in the list.
  1842. *
  1843. * @fdt: Device tree to check
  1844. * @inc: List of node paths to included
  1845. * @inc_count: Number of node paths in list
  1846. * @exc_prop: List of properties names to exclude
  1847. * @exc_prop_count: Number of properties in exclude list
  1848. * @region: Returns list of regions
  1849. * @max_region: Maximum length of region list
  1850. * @path: Pointer to a temporary string for the function to use for
  1851. * building path names
  1852. * @path_len: Length of path, must be large enough to hold the longest
  1853. * path in the tree
  1854. * @add_string_tab: 1 to add a region for the string table
  1855. * @return number of regions in list. If this is >max_regions then the
  1856. * region array was exhausted. You should increase max_regions and try
  1857. * the call again.
  1858. */
  1859. int fdt_find_regions(const void *fdt, char * const inc[], int inc_count,
  1860. char * const exc_prop[], int exc_prop_count,
  1861. struct fdt_region region[], int max_regions,
  1862. char *path, int path_len, int add_string_tab);
  1863. /**
  1864. * fdt_first_region() - find regions in device tree
  1865. *
  1866. * Given a nodes and properties to include and properties to exclude, find
  1867. * the regions of the device tree which describe those included parts.
  1868. *
  1869. * The use for this function is twofold. Firstly it provides a convenient
  1870. * way of performing a structure-aware grep of the tree. For example it is
  1871. * possible to grep for a node and get all the properties associated with
  1872. * that node. Trees can be subsetted easily, by specifying the nodes that
  1873. * are required, and then writing out the regions returned by this function.
  1874. * This is useful for small resource-constrained systems, such as boot
  1875. * loaders, which want to use an FDT but do not need to know about all of
  1876. * it.
  1877. *
  1878. * Secondly it makes it easy to hash parts of the tree and detect changes.
  1879. * The intent is to get a list of regions which will be invariant provided
  1880. * those parts are invariant. For example, if you request a list of regions
  1881. * for all nodes but exclude the property "data", then you will get the
  1882. * same region contents regardless of any change to "data" properties.
  1883. *
  1884. * This function can be used to produce a byte-stream to send to a hashing
  1885. * function to verify that critical parts of the FDT have not changed.
  1886. * Note that semantically null changes in order could still cause false
  1887. * hash misses. Such reordering might happen if the tree is regenerated
  1888. * from source, and nodes are reordered (the bytes-stream will be emitted
  1889. * in a different order and mnay hash functions will detect this). However
  1890. * if an existing tree is modified using libfdt functions, such as
  1891. * fdt_add_subnode() and fdt_setprop(), then this problem is avoided.
  1892. *
  1893. * The nodes/properties to include/exclude are defined by a function
  1894. * provided by the caller. This function is called for each node and
  1895. * property, and must return:
  1896. *
  1897. * 0 - to exclude this part
  1898. * 1 - to include this part
  1899. * -1 - for FDT_IS_PROP only: no information is available, so include
  1900. * if its containing node is included
  1901. *
  1902. * The last case is only used to deal with properties. Often a property is
  1903. * included if its containing node is included - this is the case where
  1904. * -1 is returned.. However if the property is specifically required to be
  1905. * included/excluded, then 0 or 1 can be returned. Note that including a
  1906. * property when the FDT_REG_SUPERNODES flag is given will force its
  1907. * containing node to be included since it is not valid to have a property
  1908. * that is not in a node.
  1909. *
  1910. * Using the information provided, the inclusion of a node can be controlled
  1911. * either by a node name or its compatible string, or any other property
  1912. * that the function can determine.
  1913. *
  1914. * As an example, including node "/" means to include the root node and all
  1915. * root properties. A flag provides a way of also including supernodes (of
  1916. * which there is none for the root node), and another flag includes
  1917. * immediate subnodes, so in this case we would get the FDT_BEGIN_NODE and
  1918. * FDT_END_NODE of all subnodes of /.
  1919. *
  1920. * The subnode feature helps in a hashing situation since it prevents the
  1921. * root node from changing at all. Any change to non-excluded properties,
  1922. * names of subnodes or number of subnodes would be detected.
  1923. *
  1924. * When used with FITs this provides the ability to hash and sign parts of
  1925. * the FIT based on different configurations in the FIT. Then it is
  1926. * impossible to change anything about that configuration (include images
  1927. * attached to the configuration), but it may be possible to add new
  1928. * configurations, new images or new signatures within the existing
  1929. * framework.
  1930. *
  1931. * Adding new properties to a device tree may result in the string table
  1932. * being extended (if the new property names are different from those
  1933. * already added). This function can optionally include a region for
  1934. * the string table so that this can be part of the hash too. This is always
  1935. * the last region.
  1936. *
  1937. * The FDT also has a mem_rsvmap table which can also be included, and is
  1938. * always the first region if so.
  1939. *
  1940. * The device tree header is not included in the region list. Since the
  1941. * contents of the FDT are changing (shrinking, often), the caller will need
  1942. * to regenerate the header anyway.
  1943. *
  1944. * @fdt: Device tree to check
  1945. * @h_include: Function to call to determine whether to include a part or
  1946. * not:
  1947. *
  1948. * @priv: Private pointer as passed to fdt_find_regions()
  1949. * @fdt: Pointer to FDT blob
  1950. * @offset: Offset of this node / property
  1951. * @type: Type of this part, FDT_IS_...
  1952. * @data: Pointer to data (node name, property name, compatible
  1953. * string, value (not yet supported)
  1954. * @size: Size of data, or 0 if none
  1955. * @return 0 to exclude, 1 to include, -1 if no information is
  1956. * available
  1957. * @priv: Private pointer passed to h_include
  1958. * @region: Returns list of regions, sorted by offset
  1959. * @max_regions: Maximum length of region list
  1960. * @path: Pointer to a temporary string for the function to use for
  1961. * building path names
  1962. * @path_len: Length of path, must be large enough to hold the longest
  1963. * path in the tree
  1964. * @flags: Various flags that control the region algortihm, see
  1965. * FDT_REG_...
  1966. * @return number of regions in list. If this is >max_regions then the
  1967. * region array was exhausted. You should increase max_regions and try
  1968. * the call again. Only the first max_regions elements are available in the
  1969. * array.
  1970. *
  1971. * On error a -ve value is return, which can be:
  1972. *
  1973. * -FDT_ERR_BADSTRUCTURE (too deep or more END tags than BEGIN tags
  1974. * -FDT_ERR_BADLAYOUT
  1975. * -FDT_ERR_NOSPACE (path area is too small)
  1976. */
  1977. int fdt_first_region(const void *fdt,
  1978. int (*h_include)(void *priv, const void *fdt, int offset,
  1979. int type, const char *data, int size),
  1980. void *priv, struct fdt_region *region,
  1981. char *path, int path_len, int flags,
  1982. struct fdt_region_state *info);
  1983. /** fdt_next_region() - find next region
  1984. *
  1985. * See fdt_first_region() for full description. This function finds the
  1986. * next region according to the provided parameters, which must be the same
  1987. * as passed to fdt_first_region().
  1988. *
  1989. * This function can additionally return -FDT_ERR_NOTFOUND when there are no
  1990. * more regions
  1991. */
  1992. int fdt_next_region(const void *fdt,
  1993. int (*h_include)(void *priv, const void *fdt, int offset,
  1994. int type, const char *data, int size),
  1995. void *priv, struct fdt_region *region,
  1996. char *path, int path_len, int flags,
  1997. struct fdt_region_state *info);
  1998. /**
  1999. * fdt_add_alias_regions() - find aliases that point to existing regions
  2000. *
  2001. * Once a device tree grep is complete some of the nodes will be present
  2002. * and some will have been dropped. This function checks all the alias nodes
  2003. * to figure out which points point to nodes which are still present. These
  2004. * aliases need to be kept, along with the nodes they reference.
  2005. *
  2006. * Given a list of regions function finds the aliases that still apply and
  2007. * adds more regions to the list for these. This function is called after
  2008. * fdt_next_region() has finished returning regions and requires the same
  2009. * state.
  2010. *
  2011. * @fdt: Device tree file to reference
  2012. * @region: List of regions that will be kept
  2013. * @count: Number of regions
  2014. * @max_regions: Number of entries that can fit in @region
  2015. * @info: Region state as returned from fdt_next_region()
  2016. * @return new number of regions in @region (i.e. count + the number added)
  2017. * or -FDT_ERR_NOSPACE if there was not enough space.
  2018. */
  2019. int fdt_add_alias_regions(const void *fdt, struct fdt_region *region, int count,
  2020. int max_regions, struct fdt_region_state *info);
  2021. #endif /* SWIG */
  2022. #endif /* _LIBFDT_H */