libfdt.h 68 KB

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