libfdt.h 67 KB

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