libfdt.h 69 KB

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