libfdt.h 68 KB

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