libfdt.h 67 KB

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