libfdt.h 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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: Attemped 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. * value. phandle values of 0 and -1 are not permitted. */
  80. #define FDT_ERR_BADSTATE 7
  81. /* FDT_ERR_BADSTATE: Function was passed an incomplete device
  82. * tree created by the sequential-write functions, which is
  83. * not sufficiently complete for the requested operation. */
  84. /* Error codes: codes for bad device tree blobs */
  85. #define FDT_ERR_TRUNCATED 8
  86. /* FDT_ERR_TRUNCATED: Structure block of the given device tree
  87. * ends without an FDT_END tag. */
  88. #define FDT_ERR_BADMAGIC 9
  89. /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
  90. * device tree at all - it is missing the flattened device
  91. * tree magic number. */
  92. #define FDT_ERR_BADVERSION 10
  93. /* FDT_ERR_BADVERSION: Given device tree has a version which
  94. * can't be handled by the requested operation. For
  95. * read-write functions, this may mean that fdt_open_into() is
  96. * required to convert the tree to the expected version. */
  97. #define FDT_ERR_BADSTRUCTURE 11
  98. /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
  99. * structure block or other serious error (e.g. misnested
  100. * nodes, or subnodes preceding properties). */
  101. #define FDT_ERR_BADLAYOUT 12
  102. /* FDT_ERR_BADLAYOUT: For read-write functions, the given
  103. * device tree has it's sub-blocks in an order that the
  104. * function can't handle (memory reserve map, then structure,
  105. * then strings). Use fdt_open_into() to reorganize the tree
  106. * into a form suitable for the read-write operations. */
  107. /* "Can't happen" error indicating a bug in libfdt */
  108. #define FDT_ERR_INTERNAL 13
  109. /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
  110. * Should never be returned, if it is, it indicates a bug in
  111. * libfdt itself. */
  112. #define FDT_ERR_MAX 13
  113. /**********************************************************************/
  114. /* Low-level functions (you probably don't need these) */
  115. /**********************************************************************/
  116. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
  117. static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  118. {
  119. return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
  120. }
  121. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
  122. /**********************************************************************/
  123. /* Traversal functions */
  124. /**********************************************************************/
  125. int fdt_next_node(const void *fdt, int offset, int *depth);
  126. /**********************************************************************/
  127. /* General functions */
  128. /**********************************************************************/
  129. #define fdt_get_header(fdt, field) \
  130. (fdt32_to_cpu(((const struct fdt_header *)(fdt))->field))
  131. #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
  132. #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
  133. #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
  134. #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
  135. #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
  136. #define fdt_version(fdt) (fdt_get_header(fdt, version))
  137. #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
  138. #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
  139. #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
  140. #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
  141. #define __fdt_set_hdr(name) \
  142. static inline void fdt_set_##name(void *fdt, uint32_t val) \
  143. { \
  144. struct fdt_header *fdth = (struct fdt_header*)fdt; \
  145. fdth->name = cpu_to_fdt32(val); \
  146. }
  147. __fdt_set_hdr(magic);
  148. __fdt_set_hdr(totalsize);
  149. __fdt_set_hdr(off_dt_struct);
  150. __fdt_set_hdr(off_dt_strings);
  151. __fdt_set_hdr(off_mem_rsvmap);
  152. __fdt_set_hdr(version);
  153. __fdt_set_hdr(last_comp_version);
  154. __fdt_set_hdr(boot_cpuid_phys);
  155. __fdt_set_hdr(size_dt_strings);
  156. __fdt_set_hdr(size_dt_struct);
  157. #undef __fdt_set_hdr
  158. /**
  159. * fdt_check_header - sanity check a device tree or possible device tree
  160. * @fdt: pointer to data which might be a flattened device tree
  161. *
  162. * fdt_check_header() checks that the given buffer contains what
  163. * appears to be a flattened device tree with sane information in its
  164. * header.
  165. *
  166. * returns:
  167. * 0, if the buffer appears to contain a valid device tree
  168. * -FDT_ERR_BADMAGIC,
  169. * -FDT_ERR_BADVERSION,
  170. * -FDT_ERR_BADSTATE, standard meanings, as above
  171. */
  172. int fdt_check_header(const void *fdt);
  173. /**
  174. * fdt_move - move a device tree around in memory
  175. * @fdt: pointer to the device tree to move
  176. * @buf: pointer to memory where the device is to be moved
  177. * @bufsize: size of the memory space at buf
  178. *
  179. * fdt_move() relocates, if possible, the device tree blob located at
  180. * fdt to the buffer at buf of size bufsize. The buffer may overlap
  181. * with the existing device tree blob at fdt. Therefore,
  182. * fdt_move(fdt, fdt, fdt_totalsize(fdt))
  183. * should always succeed.
  184. *
  185. * returns:
  186. * 0, on success
  187. * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
  188. * -FDT_ERR_BADMAGIC,
  189. * -FDT_ERR_BADVERSION,
  190. * -FDT_ERR_BADSTATE, standard meanings
  191. */
  192. int fdt_move(const void *fdt, void *buf, int bufsize);
  193. /**********************************************************************/
  194. /* Read-only functions */
  195. /**********************************************************************/
  196. /**
  197. * fdt_string - retrieve a string from the strings block of a device tree
  198. * @fdt: pointer to the device tree blob
  199. * @stroffset: offset of the string within the strings block (native endian)
  200. *
  201. * fdt_string() retrieves a pointer to a single string from the
  202. * strings block of the device tree blob at fdt.
  203. *
  204. * returns:
  205. * a pointer to the string, on success
  206. * NULL, if stroffset is out of bounds
  207. */
  208. const char *fdt_string(const void *fdt, int stroffset);
  209. /**
  210. * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
  211. * @fdt: pointer to the device tree blob
  212. *
  213. * Returns the number of entries in the device tree blob's memory
  214. * reservation map. This does not include the terminating 0,0 entry
  215. * or any other (0,0) entries reserved for expansion.
  216. *
  217. * returns:
  218. * the number of entries
  219. */
  220. int fdt_num_mem_rsv(const void *fdt);
  221. /**
  222. * fdt_get_mem_rsv - retrieve one memory reserve map entry
  223. * @fdt: pointer to the device tree blob
  224. * @address, @size: pointers to 64-bit variables
  225. *
  226. * On success, *address and *size will contain the address and size of
  227. * the n-th reserve map entry from the device tree blob, in
  228. * native-endian format.
  229. *
  230. * returns:
  231. * 0, on success
  232. * -FDT_ERR_BADMAGIC,
  233. * -FDT_ERR_BADVERSION,
  234. * -FDT_ERR_BADSTATE, standard meanings
  235. */
  236. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
  237. /**
  238. * fdt_subnode_offset_namelen - find a subnode based on substring
  239. * @fdt: pointer to the device tree blob
  240. * @parentoffset: structure block offset of a node
  241. * @name: name of the subnode to locate
  242. * @namelen: number of characters of name to consider
  243. *
  244. * Identical to fdt_subnode_offset(), but only examine the first
  245. * namelen characters of name for matching the subnode name. This is
  246. * useful for finding subnodes based on a portion of a larger string,
  247. * such as a full path.
  248. */
  249. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  250. const char *name, int namelen);
  251. /**
  252. * fdt_subnode_offset - find a subnode of a given node
  253. * @fdt: pointer to the device tree blob
  254. * @parentoffset: structure block offset of a node
  255. * @name: name of the subnode to locate
  256. *
  257. * fdt_subnode_offset() finds a subnode of the node at structure block
  258. * offset parentoffset with the given name. name may include a unit
  259. * address, in which case fdt_subnode_offset() will find the subnode
  260. * with that unit address, or the unit address may be omitted, in
  261. * which case fdt_subnode_offset() will find an arbitrary subnode
  262. * whose name excluding unit address matches the given name.
  263. *
  264. * returns:
  265. * structure block offset of the requested subnode (>=0), on success
  266. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  267. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
  268. * -FDT_ERR_BADMAGIC,
  269. * -FDT_ERR_BADVERSION,
  270. * -FDT_ERR_BADSTATE,
  271. * -FDT_ERR_BADSTRUCTURE,
  272. * -FDT_ERR_TRUNCATED, standard meanings.
  273. */
  274. int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
  275. /**
  276. * fdt_path_offset - find a tree node by its full path
  277. * @fdt: pointer to the device tree blob
  278. * @path: full path of the node to locate
  279. *
  280. * fdt_path_offset() finds a node of a given path in the device tree.
  281. * Each path component may omit the unit address portion, but the
  282. * results of this are undefined if any such path component is
  283. * ambiguous (that is if there are multiple nodes at the relevant
  284. * level matching the given component, differentiated only by unit
  285. * address).
  286. *
  287. * returns:
  288. * structure block offset of the node with the requested path (>=0), on success
  289. * -FDT_ERR_BADPATH, given path does not begin with '/' or is invalid
  290. * -FDT_ERR_NOTFOUND, if the requested node does not exist
  291. * -FDT_ERR_BADMAGIC,
  292. * -FDT_ERR_BADVERSION,
  293. * -FDT_ERR_BADSTATE,
  294. * -FDT_ERR_BADSTRUCTURE,
  295. * -FDT_ERR_TRUNCATED, standard meanings.
  296. */
  297. int fdt_path_offset(const void *fdt, const char *path);
  298. /**
  299. * fdt_get_name - retrieve the name of a given node
  300. * @fdt: pointer to the device tree blob
  301. * @nodeoffset: structure block offset of the starting node
  302. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  303. *
  304. * fdt_get_name() retrieves the name (including unit address) of the
  305. * device tree node at structure block offset nodeoffset. If lenp is
  306. * non-NULL, the length of this name is also returned, in the integer
  307. * pointed to by lenp.
  308. *
  309. * returns:
  310. * pointer to the node's name, on success
  311. * If lenp is non-NULL, *lenp contains the length of that name (>=0)
  312. * NULL, on error
  313. * if lenp is non-NULL *lenp contains an error code (<0):
  314. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  315. * -FDT_ERR_BADMAGIC,
  316. * -FDT_ERR_BADVERSION,
  317. * -FDT_ERR_BADSTATE, standard meanings
  318. */
  319. const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
  320. /**
  321. * fdt_first_property_offset - find the offset of a node's first property
  322. * @fdt: pointer to the device tree blob
  323. * @nodeoffset: structure block offset of a node
  324. *
  325. * fdt_first_property_offset() finds the first property of the node at
  326. * the given structure block offset.
  327. *
  328. * returns:
  329. * structure block offset of the property (>=0), on success
  330. * -FDT_ERR_NOTFOUND, if the requested node has no properties
  331. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
  332. * -FDT_ERR_BADMAGIC,
  333. * -FDT_ERR_BADVERSION,
  334. * -FDT_ERR_BADSTATE,
  335. * -FDT_ERR_BADSTRUCTURE,
  336. * -FDT_ERR_TRUNCATED, standard meanings.
  337. */
  338. int fdt_first_property_offset(const void *fdt, int nodeoffset);
  339. /**
  340. * fdt_next_property_offset - step through a node's properties
  341. * @fdt: pointer to the device tree blob
  342. * @offset: structure block offset of a property
  343. *
  344. * fdt_next_property_offset() finds the property immediately after the
  345. * one at the given structure block offset. This will be a property
  346. * of the same node as the given property.
  347. *
  348. * returns:
  349. * structure block offset of the next property (>=0), on success
  350. * -FDT_ERR_NOTFOUND, if the given property is the last in its node
  351. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
  352. * -FDT_ERR_BADMAGIC,
  353. * -FDT_ERR_BADVERSION,
  354. * -FDT_ERR_BADSTATE,
  355. * -FDT_ERR_BADSTRUCTURE,
  356. * -FDT_ERR_TRUNCATED, standard meanings.
  357. */
  358. int fdt_next_property_offset(const void *fdt, int offset);
  359. /**
  360. * fdt_get_property_by_offset - retrieve the property at a given offset
  361. * @fdt: pointer to the device tree blob
  362. * @offset: offset of the property to retrieve
  363. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  364. *
  365. * fdt_get_property_by_offset() retrieves a pointer to the
  366. * fdt_property structure within the device tree blob at the given
  367. * offset. If lenp is non-NULL, the length of the property value is
  368. * also returned, in the integer pointed to by lenp.
  369. *
  370. * returns:
  371. * pointer to the structure representing the property
  372. * if lenp is non-NULL, *lenp contains the length of the property
  373. * value (>=0)
  374. * NULL, on error
  375. * if lenp is non-NULL, *lenp contains an error code (<0):
  376. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP 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. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  384. int offset,
  385. int *lenp);
  386. /**
  387. * fdt_get_property_namelen - find a property based on substring
  388. * @fdt: pointer to the device tree blob
  389. * @nodeoffset: offset of the node whose property to find
  390. * @name: name of the property to find
  391. * @namelen: number of characters of name to consider
  392. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  393. *
  394. * Identical to fdt_get_property_namelen(), but only examine the first
  395. * namelen characters of name for matching the property name.
  396. */
  397. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  398. int nodeoffset,
  399. const char *name,
  400. int namelen, int *lenp);
  401. /**
  402. * fdt_get_property - find a given property in a given node
  403. * @fdt: pointer to the device tree blob
  404. * @nodeoffset: offset of the node whose property to find
  405. * @name: name of the property to find
  406. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  407. *
  408. * fdt_get_property() retrieves a pointer to the fdt_property
  409. * structure within the device tree blob corresponding to the property
  410. * named 'name' of the node at offset nodeoffset. If lenp is
  411. * non-NULL, the length of the property value is also returned, in the
  412. * integer pointed to by lenp.
  413. *
  414. * returns:
  415. * pointer to the structure representing the property
  416. * if lenp is non-NULL, *lenp contains the length of the property
  417. * value (>=0)
  418. * NULL, on error
  419. * if lenp is non-NULL, *lenp contains an error code (<0):
  420. * -FDT_ERR_NOTFOUND, node does not have named property
  421. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  422. * -FDT_ERR_BADMAGIC,
  423. * -FDT_ERR_BADVERSION,
  424. * -FDT_ERR_BADSTATE,
  425. * -FDT_ERR_BADSTRUCTURE,
  426. * -FDT_ERR_TRUNCATED, standard meanings
  427. */
  428. const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
  429. const char *name, int *lenp);
  430. static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
  431. const char *name,
  432. int *lenp)
  433. {
  434. return (struct fdt_property *)(uintptr_t)
  435. fdt_get_property(fdt, nodeoffset, name, lenp);
  436. }
  437. /**
  438. * fdt_getprop_by_offset - retrieve the value of a property at a given offset
  439. * @fdt: pointer to the device tree blob
  440. * @ffset: offset of the property to read
  441. * @namep: pointer to a string variable (will be overwritten) or NULL
  442. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  443. *
  444. * fdt_getprop_by_offset() retrieves a pointer to the value of the
  445. * property at structure block offset 'offset' (this will be a pointer
  446. * to within the device blob itself, not a copy of the value). If
  447. * lenp is non-NULL, the length of the property value is also
  448. * returned, in the integer pointed to by lenp. If namep is non-NULL,
  449. * the property's namne will also be returned in the char * pointed to
  450. * by namep (this will be a pointer to within the device tree's string
  451. * block, not a new copy of the name).
  452. *
  453. * returns:
  454. * pointer to the property's value
  455. * if lenp is non-NULL, *lenp contains the length of the property
  456. * value (>=0)
  457. * if namep is non-NULL *namep contiains a pointer to the property
  458. * name.
  459. * NULL, on error
  460. * if lenp is non-NULL, *lenp contains an error code (<0):
  461. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  462. * -FDT_ERR_BADMAGIC,
  463. * -FDT_ERR_BADVERSION,
  464. * -FDT_ERR_BADSTATE,
  465. * -FDT_ERR_BADSTRUCTURE,
  466. * -FDT_ERR_TRUNCATED, standard meanings
  467. */
  468. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  469. const char **namep, int *lenp);
  470. /**
  471. * fdt_getprop_namelen - get property value based on substring
  472. * @fdt: pointer to the device tree blob
  473. * @nodeoffset: offset of the node whose property to find
  474. * @name: name of the property to find
  475. * @namelen: number of characters of name to consider
  476. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  477. *
  478. * Identical to fdt_getprop(), but only examine the first namelen
  479. * characters of name for matching the property name.
  480. */
  481. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  482. const char *name, int namelen, int *lenp);
  483. /**
  484. * fdt_getprop - retrieve the value of a given property
  485. * @fdt: pointer to the device tree blob
  486. * @nodeoffset: offset of the node whose property to find
  487. * @name: name of the property to find
  488. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  489. *
  490. * fdt_getprop() retrieves a pointer to the value of the property
  491. * named 'name' of the node at offset nodeoffset (this will be a
  492. * pointer to within the device blob itself, not a copy of the value).
  493. * If lenp is non-NULL, the length of the property value is also
  494. * returned, in the integer pointed to by lenp.
  495. *
  496. * returns:
  497. * pointer to the property's value
  498. * if lenp is non-NULL, *lenp contains the length of the property
  499. * value (>=0)
  500. * NULL, on error
  501. * if lenp is non-NULL, *lenp contains an error code (<0):
  502. * -FDT_ERR_NOTFOUND, node does not have named property
  503. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  504. * -FDT_ERR_BADMAGIC,
  505. * -FDT_ERR_BADVERSION,
  506. * -FDT_ERR_BADSTATE,
  507. * -FDT_ERR_BADSTRUCTURE,
  508. * -FDT_ERR_TRUNCATED, standard meanings
  509. */
  510. const void *fdt_getprop(const void *fdt, int nodeoffset,
  511. const char *name, int *lenp);
  512. static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
  513. const char *name, int *lenp)
  514. {
  515. return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
  516. }
  517. /**
  518. * fdt_get_phandle - retrieve the phandle of a given node
  519. * @fdt: pointer to the device tree blob
  520. * @nodeoffset: structure block offset of the node
  521. *
  522. * fdt_get_phandle() retrieves the phandle of the device tree node at
  523. * structure block offset nodeoffset.
  524. *
  525. * returns:
  526. * the phandle of the node at nodeoffset, on success (!= 0, != -1)
  527. * 0, if the node has no phandle, or another error occurs
  528. */
  529. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
  530. /**
  531. * fdt_get_alias_namelen - get alias based on substring
  532. * @fdt: pointer to the device tree blob
  533. * @name: name of the alias th look up
  534. * @namelen: number of characters of name to consider
  535. *
  536. * Identical to fdt_get_alias(), but only examine the first namelen
  537. * characters of name for matching the alias name.
  538. */
  539. const char *fdt_get_alias_namelen(const void *fdt,
  540. const char *name, int namelen);
  541. /**
  542. * fdt_get_alias - retreive the path referenced by a given alias
  543. * @fdt: pointer to the device tree blob
  544. * @name: name of the alias th look up
  545. *
  546. * fdt_get_alias() retrieves the value of a given alias. That is, the
  547. * value of the property named 'name' in the node /aliases.
  548. *
  549. * returns:
  550. * a pointer to the expansion of the alias named 'name', of it exists
  551. * NULL, if the given alias or the /aliases node does not exist
  552. */
  553. const char *fdt_get_alias(const void *fdt, const char *name);
  554. /**
  555. * fdt_get_path - determine the full path of a node
  556. * @fdt: pointer to the device tree blob
  557. * @nodeoffset: offset of the node whose path to find
  558. * @buf: character buffer to contain the returned path (will be overwritten)
  559. * @buflen: size of the character buffer at buf
  560. *
  561. * fdt_get_path() computes the full path of the node at offset
  562. * nodeoffset, and records that path in the buffer at buf.
  563. *
  564. * NOTE: This function is expensive, as it must scan the device tree
  565. * structure from the start to nodeoffset.
  566. *
  567. * returns:
  568. * 0, on success
  569. * buf contains the absolute path of the node at
  570. * nodeoffset, as a NUL-terminated string.
  571. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  572. * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
  573. * characters and will not fit in the given buffer.
  574. * -FDT_ERR_BADMAGIC,
  575. * -FDT_ERR_BADVERSION,
  576. * -FDT_ERR_BADSTATE,
  577. * -FDT_ERR_BADSTRUCTURE, standard meanings
  578. */
  579. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
  580. /**
  581. * fdt_supernode_atdepth_offset - find a specific ancestor of a node
  582. * @fdt: pointer to the device tree blob
  583. * @nodeoffset: offset of the node whose parent to find
  584. * @supernodedepth: depth of the ancestor to find
  585. * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
  586. *
  587. * fdt_supernode_atdepth_offset() finds an ancestor of the given node
  588. * at a specific depth from the root (where the root itself has depth
  589. * 0, its immediate subnodes depth 1 and so forth). So
  590. * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
  591. * will always return 0, the offset of the root node. If the node at
  592. * nodeoffset has depth D, then:
  593. * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
  594. * will return nodeoffset itself.
  595. *
  596. * NOTE: This function is expensive, as it must scan the device tree
  597. * structure from the start to nodeoffset.
  598. *
  599. * returns:
  600. * structure block offset of the node at node offset's ancestor
  601. * of depth supernodedepth (>=0), on success
  602. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  603. * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of nodeoffset
  604. * -FDT_ERR_BADMAGIC,
  605. * -FDT_ERR_BADVERSION,
  606. * -FDT_ERR_BADSTATE,
  607. * -FDT_ERR_BADSTRUCTURE, standard meanings
  608. */
  609. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  610. int supernodedepth, int *nodedepth);
  611. /**
  612. * fdt_node_depth - find the depth of a given node
  613. * @fdt: pointer to the device tree blob
  614. * @nodeoffset: offset of the node whose parent to find
  615. *
  616. * fdt_node_depth() finds the depth of a given node. The root node
  617. * has depth 0, its immediate subnodes depth 1 and so forth.
  618. *
  619. * NOTE: This function is expensive, as it must scan the device tree
  620. * structure from the start to nodeoffset.
  621. *
  622. * returns:
  623. * depth of the node at nodeoffset (>=0), on success
  624. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  625. * -FDT_ERR_BADMAGIC,
  626. * -FDT_ERR_BADVERSION,
  627. * -FDT_ERR_BADSTATE,
  628. * -FDT_ERR_BADSTRUCTURE, standard meanings
  629. */
  630. int fdt_node_depth(const void *fdt, int nodeoffset);
  631. /**
  632. * fdt_parent_offset - find the parent of a given node
  633. * @fdt: pointer to the device tree blob
  634. * @nodeoffset: offset of the node whose parent to find
  635. *
  636. * fdt_parent_offset() locates the parent node of a given node (that
  637. * is, it finds the offset of the node which contains the node at
  638. * nodeoffset as a subnode).
  639. *
  640. * NOTE: This function is expensive, as it must scan the device tree
  641. * structure from the start to nodeoffset, *twice*.
  642. *
  643. * returns:
  644. * structure block offset of the parent of the node at nodeoffset
  645. * (>=0), on success
  646. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  647. * -FDT_ERR_BADMAGIC,
  648. * -FDT_ERR_BADVERSION,
  649. * -FDT_ERR_BADSTATE,
  650. * -FDT_ERR_BADSTRUCTURE, standard meanings
  651. */
  652. int fdt_parent_offset(const void *fdt, int nodeoffset);
  653. /**
  654. * fdt_node_offset_by_prop_value - find nodes with a given property value
  655. * @fdt: pointer to the device tree blob
  656. * @startoffset: only find nodes after this offset
  657. * @propname: property name to check
  658. * @propval: property value to search for
  659. * @proplen: length of the value in propval
  660. *
  661. * fdt_node_offset_by_prop_value() returns the offset of the first
  662. * node after startoffset, which has a property named propname whose
  663. * value is of length proplen and has value equal to propval; or if
  664. * startoffset is -1, the very first such node in the tree.
  665. *
  666. * To iterate through all nodes matching the criterion, the following
  667. * idiom can be used:
  668. * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
  669. * propval, proplen);
  670. * while (offset != -FDT_ERR_NOTFOUND) {
  671. * ... other code here ...
  672. * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
  673. * propval, proplen);
  674. * }
  675. *
  676. * Note the -1 in the first call to the function, if 0 is used here
  677. * instead, the function will never locate the root node, even if it
  678. * matches the criterion.
  679. *
  680. * returns:
  681. * structure block offset of the located node (>= 0, >startoffset),
  682. * on success
  683. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  684. * tree after startoffset
  685. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  686. * -FDT_ERR_BADMAGIC,
  687. * -FDT_ERR_BADVERSION,
  688. * -FDT_ERR_BADSTATE,
  689. * -FDT_ERR_BADSTRUCTURE, standard meanings
  690. */
  691. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  692. const char *propname,
  693. const void *propval, int proplen);
  694. /**
  695. * fdt_node_offset_by_phandle - find the node with a given phandle
  696. * @fdt: pointer to the device tree blob
  697. * @phandle: phandle value
  698. *
  699. * fdt_node_offset_by_phandle() returns the offset of the node
  700. * which has the given phandle value. If there is more than one node
  701. * in the tree with the given phandle (an invalid tree), results are
  702. * undefined.
  703. *
  704. * returns:
  705. * structure block offset of the located node (>= 0), on success
  706. * -FDT_ERR_NOTFOUND, no node with that phandle exists
  707. * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
  708. * -FDT_ERR_BADMAGIC,
  709. * -FDT_ERR_BADVERSION,
  710. * -FDT_ERR_BADSTATE,
  711. * -FDT_ERR_BADSTRUCTURE, standard meanings
  712. */
  713. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
  714. /**
  715. * fdt_node_check_compatible: check a node's compatible property
  716. * @fdt: pointer to the device tree blob
  717. * @nodeoffset: offset of a tree node
  718. * @compatible: string to match against
  719. *
  720. *
  721. * fdt_node_check_compatible() returns 0 if the given node contains a
  722. * 'compatible' property with the given string as one of its elements,
  723. * it returns non-zero otherwise, or on error.
  724. *
  725. * returns:
  726. * 0, if the node has a 'compatible' property listing the given string
  727. * 1, if the node has a 'compatible' property, but it does not list
  728. * the given string
  729. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  730. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  731. * -FDT_ERR_BADMAGIC,
  732. * -FDT_ERR_BADVERSION,
  733. * -FDT_ERR_BADSTATE,
  734. * -FDT_ERR_BADSTRUCTURE, standard meanings
  735. */
  736. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  737. const char *compatible);
  738. /**
  739. * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
  740. * @fdt: pointer to the device tree blob
  741. * @startoffset: only find nodes after this offset
  742. * @compatible: 'compatible' string to match against
  743. *
  744. * fdt_node_offset_by_compatible() returns the offset of the first
  745. * node after startoffset, which has a 'compatible' property which
  746. * lists the given compatible string; or if startoffset is -1, the
  747. * very first such node in the tree.
  748. *
  749. * To iterate through all nodes matching the criterion, the following
  750. * idiom can be used:
  751. * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
  752. * while (offset != -FDT_ERR_NOTFOUND) {
  753. * ... other code here ...
  754. * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
  755. * }
  756. *
  757. * Note the -1 in the first call to the function, if 0 is used here
  758. * instead, the function will never locate the root node, even if it
  759. * matches the criterion.
  760. *
  761. * returns:
  762. * structure block offset of the located node (>= 0, >startoffset),
  763. * on success
  764. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  765. * tree after startoffset
  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_offset_by_compatible(const void *fdt, int startoffset,
  773. const char *compatible);
  774. /**********************************************************************/
  775. /* Write-in-place functions */
  776. /**********************************************************************/
  777. /**
  778. * fdt_setprop_inplace - change a property's value, but not its size
  779. * @fdt: pointer to the device tree blob
  780. * @nodeoffset: offset of the node whose property to change
  781. * @name: name of the property to change
  782. * @val: pointer to data to replace the property value with
  783. * @len: length of the property value
  784. *
  785. * fdt_setprop_inplace() replaces the value of a given property with
  786. * the data in val, of length len. This function cannot change the
  787. * size of a property, and so will only work if len is equal to the
  788. * current length of the property.
  789. *
  790. * This function will alter only the bytes in the blob which contain
  791. * the given property value, and will not alter or move any other part
  792. * of the tree.
  793. *
  794. * returns:
  795. * 0, on success
  796. * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
  797. * -FDT_ERR_NOTFOUND, node does not have the named property
  798. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  799. * -FDT_ERR_BADMAGIC,
  800. * -FDT_ERR_BADVERSION,
  801. * -FDT_ERR_BADSTATE,
  802. * -FDT_ERR_BADSTRUCTURE,
  803. * -FDT_ERR_TRUNCATED, standard meanings
  804. */
  805. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  806. const void *val, int len);
  807. /**
  808. * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
  809. * @fdt: pointer to the device tree blob
  810. * @nodeoffset: offset of the node whose property to change
  811. * @name: name of the property to change
  812. * @val: 32-bit integer value to replace the property with
  813. *
  814. * fdt_setprop_inplace_u32() replaces the value of a given property
  815. * with the 32-bit integer value in val, converting val to big-endian
  816. * if necessary. This function cannot change the size of a property,
  817. * and so will only work if the property already exists and has length
  818. * 4.
  819. *
  820. * This function will alter only the bytes in the blob which contain
  821. * the given property value, and will not alter or move any other part
  822. * of the tree.
  823. *
  824. * returns:
  825. * 0, on success
  826. * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
  827. * -FDT_ERR_NOTFOUND, node does not have the named property
  828. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  829. * -FDT_ERR_BADMAGIC,
  830. * -FDT_ERR_BADVERSION,
  831. * -FDT_ERR_BADSTATE,
  832. * -FDT_ERR_BADSTRUCTURE,
  833. * -FDT_ERR_TRUNCATED, standard meanings
  834. */
  835. static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
  836. const char *name, uint32_t val)
  837. {
  838. val = cpu_to_fdt32(val);
  839. return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
  840. }
  841. /**
  842. * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
  843. * @fdt: pointer to the device tree blob
  844. * @nodeoffset: offset of the node whose property to change
  845. * @name: name of the property to change
  846. * @val: 64-bit integer value to replace the property with
  847. *
  848. * fdt_setprop_inplace_u64() replaces the value of a given property
  849. * with the 64-bit integer value in val, converting val to big-endian
  850. * if necessary. This function cannot change the size of a property,
  851. * and so will only work if the property already exists and has length
  852. * 8.
  853. *
  854. * This function will alter only the bytes in the blob which contain
  855. * the given property value, and will not alter or move any other part
  856. * of the tree.
  857. *
  858. * returns:
  859. * 0, on success
  860. * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
  861. * -FDT_ERR_NOTFOUND, node does not have the named property
  862. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  863. * -FDT_ERR_BADMAGIC,
  864. * -FDT_ERR_BADVERSION,
  865. * -FDT_ERR_BADSTATE,
  866. * -FDT_ERR_BADSTRUCTURE,
  867. * -FDT_ERR_TRUNCATED, standard meanings
  868. */
  869. static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
  870. const char *name, uint64_t val)
  871. {
  872. val = cpu_to_fdt64(val);
  873. return fdt_setprop_inplace(fdt, nodeoffset, name, &val, sizeof(val));
  874. }
  875. /**
  876. * fdt_setprop_inplace_cell - change the value of a single-cell property
  877. *
  878. * This is an alternative name for fdt_setprop_inplace_u32()
  879. */
  880. static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
  881. const char *name, uint32_t val)
  882. {
  883. return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
  884. }
  885. /**
  886. * fdt_nop_property - replace a property with nop tags
  887. * @fdt: pointer to the device tree blob
  888. * @nodeoffset: offset of the node whose property to nop
  889. * @name: name of the property to nop
  890. *
  891. * fdt_nop_property() will replace a given property's representation
  892. * in the blob with FDT_NOP tags, effectively removing it from the
  893. * tree.
  894. *
  895. * This function will alter only the bytes in the blob which contain
  896. * the property, and will not alter or move any other part of the
  897. * tree.
  898. *
  899. * returns:
  900. * 0, on success
  901. * -FDT_ERR_NOTFOUND, node does not have the named property
  902. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  903. * -FDT_ERR_BADMAGIC,
  904. * -FDT_ERR_BADVERSION,
  905. * -FDT_ERR_BADSTATE,
  906. * -FDT_ERR_BADSTRUCTURE,
  907. * -FDT_ERR_TRUNCATED, standard meanings
  908. */
  909. int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  910. /**
  911. * fdt_nop_node - replace a node (subtree) with nop tags
  912. * @fdt: pointer to the device tree blob
  913. * @nodeoffset: offset of the node to nop
  914. *
  915. * fdt_nop_node() will replace a given node's representation in the
  916. * blob, including all its subnodes, if any, with FDT_NOP tags,
  917. * effectively removing it from the tree.
  918. *
  919. * This function will alter only the bytes in the blob which contain
  920. * the node and its properties and subnodes, and will not alter or
  921. * move any other part of the tree.
  922. *
  923. * returns:
  924. * 0, on success
  925. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  926. * -FDT_ERR_BADMAGIC,
  927. * -FDT_ERR_BADVERSION,
  928. * -FDT_ERR_BADSTATE,
  929. * -FDT_ERR_BADSTRUCTURE,
  930. * -FDT_ERR_TRUNCATED, standard meanings
  931. */
  932. int fdt_nop_node(void *fdt, int nodeoffset);
  933. /**********************************************************************/
  934. /* Sequential write functions */
  935. /**********************************************************************/
  936. int fdt_create(void *buf, int bufsize);
  937. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
  938. int fdt_finish_reservemap(void *fdt);
  939. int fdt_begin_node(void *fdt, const char *name);
  940. int fdt_property(void *fdt, const char *name, const void *val, int len);
  941. static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
  942. {
  943. val = cpu_to_fdt32(val);
  944. return fdt_property(fdt, name, &val, sizeof(val));
  945. }
  946. static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
  947. {
  948. val = cpu_to_fdt64(val);
  949. return fdt_property(fdt, name, &val, sizeof(val));
  950. }
  951. static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
  952. {
  953. return fdt_property_u32(fdt, name, val);
  954. }
  955. #define fdt_property_string(fdt, name, str) \
  956. fdt_property(fdt, name, str, strlen(str)+1)
  957. int fdt_end_node(void *fdt);
  958. int fdt_finish(void *fdt);
  959. /**********************************************************************/
  960. /* Read-write functions */
  961. /**********************************************************************/
  962. int fdt_create_empty_tree(void *buf, int bufsize);
  963. int fdt_open_into(const void *fdt, void *buf, int bufsize);
  964. int fdt_pack(void *fdt);
  965. /**
  966. * fdt_add_mem_rsv - add one memory reserve map entry
  967. * @fdt: pointer to the device tree blob
  968. * @address, @size: 64-bit values (native endian)
  969. *
  970. * Adds a reserve map entry to the given blob reserving a region at
  971. * address address of length size.
  972. *
  973. * This function will insert data into the reserve map and will
  974. * therefore change the indexes of some entries in the table.
  975. *
  976. * returns:
  977. * 0, on success
  978. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  979. * contain the new reservation entry
  980. * -FDT_ERR_BADMAGIC,
  981. * -FDT_ERR_BADVERSION,
  982. * -FDT_ERR_BADSTATE,
  983. * -FDT_ERR_BADSTRUCTURE,
  984. * -FDT_ERR_BADLAYOUT,
  985. * -FDT_ERR_TRUNCATED, standard meanings
  986. */
  987. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
  988. /**
  989. * fdt_del_mem_rsv - remove a memory reserve map entry
  990. * @fdt: pointer to the device tree blob
  991. * @n: entry to remove
  992. *
  993. * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
  994. * the blob.
  995. *
  996. * This function will delete data from the reservation table and will
  997. * therefore change the indexes of some entries in the table.
  998. *
  999. * returns:
  1000. * 0, on success
  1001. * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
  1002. * are less than n+1 reserve map entries)
  1003. * -FDT_ERR_BADMAGIC,
  1004. * -FDT_ERR_BADVERSION,
  1005. * -FDT_ERR_BADSTATE,
  1006. * -FDT_ERR_BADSTRUCTURE,
  1007. * -FDT_ERR_BADLAYOUT,
  1008. * -FDT_ERR_TRUNCATED, standard meanings
  1009. */
  1010. int fdt_del_mem_rsv(void *fdt, int n);
  1011. /**
  1012. * fdt_set_name - change the name of a given node
  1013. * @fdt: pointer to the device tree blob
  1014. * @nodeoffset: structure block offset of a node
  1015. * @name: name to give the node
  1016. *
  1017. * fdt_set_name() replaces the name (including unit address, if any)
  1018. * of the given node with the given string. NOTE: this function can't
  1019. * efficiently check if the new name is unique amongst the given
  1020. * node's siblings; results are undefined if this function is invoked
  1021. * with a name equal to one of the given node's siblings.
  1022. *
  1023. * This function may insert or delete data from the blob, and will
  1024. * therefore change the offsets of some existing nodes.
  1025. *
  1026. * returns:
  1027. * 0, on success
  1028. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
  1029. * to contain the new name
  1030. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1031. * -FDT_ERR_BADMAGIC,
  1032. * -FDT_ERR_BADVERSION,
  1033. * -FDT_ERR_BADSTATE, standard meanings
  1034. */
  1035. int fdt_set_name(void *fdt, int nodeoffset, const char *name);
  1036. /**
  1037. * fdt_setprop - create or change a property
  1038. * @fdt: pointer to the device tree blob
  1039. * @nodeoffset: offset of the node whose property to change
  1040. * @name: name of the property to change
  1041. * @val: pointer to data to set the property value to
  1042. * @len: length of the property value
  1043. *
  1044. * fdt_setprop() sets the value of the named property in the given
  1045. * node to the given value and length, creating the property if it
  1046. * does not already exist.
  1047. *
  1048. * This function may insert or delete data from the blob, and will
  1049. * therefore change the offsets of some existing nodes.
  1050. *
  1051. * returns:
  1052. * 0, on success
  1053. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1054. * contain the new property value
  1055. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1056. * -FDT_ERR_BADLAYOUT,
  1057. * -FDT_ERR_BADMAGIC,
  1058. * -FDT_ERR_BADVERSION,
  1059. * -FDT_ERR_BADSTATE,
  1060. * -FDT_ERR_BADSTRUCTURE,
  1061. * -FDT_ERR_BADLAYOUT,
  1062. * -FDT_ERR_TRUNCATED, standard meanings
  1063. */
  1064. int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  1065. const void *val, int len);
  1066. /**
  1067. * fdt_setprop_u32 - set a property to a 32-bit integer
  1068. * @fdt: pointer to the device tree blob
  1069. * @nodeoffset: offset of the node whose property to change
  1070. * @name: name of the property to change
  1071. * @val: 32-bit integer value for the property (native endian)
  1072. *
  1073. * fdt_setprop_u32() sets the value of the named property in the given
  1074. * node to the given 32-bit integer value (converting to big-endian if
  1075. * necessary), or creates a new property with that value if it does
  1076. * not already exist.
  1077. *
  1078. * This function may insert or delete data from the blob, and will
  1079. * therefore change the offsets of some existing nodes.
  1080. *
  1081. * returns:
  1082. * 0, on success
  1083. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1084. * contain the new property value
  1085. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1086. * -FDT_ERR_BADLAYOUT,
  1087. * -FDT_ERR_BADMAGIC,
  1088. * -FDT_ERR_BADVERSION,
  1089. * -FDT_ERR_BADSTATE,
  1090. * -FDT_ERR_BADSTRUCTURE,
  1091. * -FDT_ERR_BADLAYOUT,
  1092. * -FDT_ERR_TRUNCATED, standard meanings
  1093. */
  1094. static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
  1095. uint32_t val)
  1096. {
  1097. val = cpu_to_fdt32(val);
  1098. return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
  1099. }
  1100. /**
  1101. * fdt_setprop_u64 - set a property to a 64-bit integer
  1102. * @fdt: pointer to the device tree blob
  1103. * @nodeoffset: offset of the node whose property to change
  1104. * @name: name of the property to change
  1105. * @val: 64-bit integer value for the property (native endian)
  1106. *
  1107. * fdt_setprop_u64() sets the value of the named property in the given
  1108. * node to the given 64-bit integer value (converting to big-endian if
  1109. * necessary), or creates a new property with that value if it does
  1110. * not already exist.
  1111. *
  1112. * This function may insert or delete data from the blob, and will
  1113. * therefore change the offsets of some existing nodes.
  1114. *
  1115. * returns:
  1116. * 0, on success
  1117. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1118. * contain the new property value
  1119. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1120. * -FDT_ERR_BADLAYOUT,
  1121. * -FDT_ERR_BADMAGIC,
  1122. * -FDT_ERR_BADVERSION,
  1123. * -FDT_ERR_BADSTATE,
  1124. * -FDT_ERR_BADSTRUCTURE,
  1125. * -FDT_ERR_BADLAYOUT,
  1126. * -FDT_ERR_TRUNCATED, standard meanings
  1127. */
  1128. static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
  1129. uint64_t val)
  1130. {
  1131. val = cpu_to_fdt64(val);
  1132. return fdt_setprop(fdt, nodeoffset, name, &val, sizeof(val));
  1133. }
  1134. /**
  1135. * fdt_setprop_cell - set a property to a single cell value
  1136. *
  1137. * This is an alternative name for fdt_setprop_u32()
  1138. */
  1139. static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
  1140. uint32_t val)
  1141. {
  1142. return fdt_setprop_u32(fdt, nodeoffset, name, val);
  1143. }
  1144. /**
  1145. * fdt_setprop_string - set a property to a string value
  1146. * @fdt: pointer to the device tree blob
  1147. * @nodeoffset: offset of the node whose property to change
  1148. * @name: name of the property to change
  1149. * @str: string value for the property
  1150. *
  1151. * fdt_setprop_string() sets the value of the named property in the
  1152. * given node to the given string value (using the length of the
  1153. * string to determine the new length of the property), or creates a
  1154. * new property with that value if it does not already exist.
  1155. *
  1156. * This function may insert or delete data from the blob, and will
  1157. * therefore change the offsets of some existing nodes.
  1158. *
  1159. * returns:
  1160. * 0, on success
  1161. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1162. * contain the new property value
  1163. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1164. * -FDT_ERR_BADLAYOUT,
  1165. * -FDT_ERR_BADMAGIC,
  1166. * -FDT_ERR_BADVERSION,
  1167. * -FDT_ERR_BADSTATE,
  1168. * -FDT_ERR_BADSTRUCTURE,
  1169. * -FDT_ERR_BADLAYOUT,
  1170. * -FDT_ERR_TRUNCATED, standard meanings
  1171. */
  1172. #define fdt_setprop_string(fdt, nodeoffset, name, str) \
  1173. fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1174. /**
  1175. * fdt_appendprop - append to or create a property
  1176. * @fdt: pointer to the device tree blob
  1177. * @nodeoffset: offset of the node whose property to change
  1178. * @name: name of the property to append to
  1179. * @val: pointer to data to append to the property value
  1180. * @len: length of the data to append to the property value
  1181. *
  1182. * fdt_appendprop() appends the value to the named property in the
  1183. * given node, creating the property if it does not already exist.
  1184. *
  1185. * This function may insert data into the blob, and will therefore
  1186. * change the offsets of some existing nodes.
  1187. *
  1188. * returns:
  1189. * 0, on success
  1190. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1191. * contain the new property value
  1192. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1193. * -FDT_ERR_BADLAYOUT,
  1194. * -FDT_ERR_BADMAGIC,
  1195. * -FDT_ERR_BADVERSION,
  1196. * -FDT_ERR_BADSTATE,
  1197. * -FDT_ERR_BADSTRUCTURE,
  1198. * -FDT_ERR_BADLAYOUT,
  1199. * -FDT_ERR_TRUNCATED, standard meanings
  1200. */
  1201. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  1202. const void *val, int len);
  1203. /**
  1204. * fdt_appendprop_u32 - append a 32-bit integer value to a property
  1205. * @fdt: pointer to the device tree blob
  1206. * @nodeoffset: offset of the node whose property to change
  1207. * @name: name of the property to change
  1208. * @val: 32-bit integer value to append to the property (native endian)
  1209. *
  1210. * fdt_appendprop_u32() appends the given 32-bit integer value
  1211. * (converting to big-endian if necessary) to the value of the named
  1212. * property in the given node, or creates a new property with that
  1213. * value if it does not already exist.
  1214. *
  1215. * This function may insert data into the blob, and will therefore
  1216. * change the offsets of some existing nodes.
  1217. *
  1218. * returns:
  1219. * 0, on success
  1220. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1221. * contain the new property value
  1222. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1223. * -FDT_ERR_BADLAYOUT,
  1224. * -FDT_ERR_BADMAGIC,
  1225. * -FDT_ERR_BADVERSION,
  1226. * -FDT_ERR_BADSTATE,
  1227. * -FDT_ERR_BADSTRUCTURE,
  1228. * -FDT_ERR_BADLAYOUT,
  1229. * -FDT_ERR_TRUNCATED, standard meanings
  1230. */
  1231. static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
  1232. const char *name, uint32_t val)
  1233. {
  1234. val = cpu_to_fdt32(val);
  1235. return fdt_appendprop(fdt, nodeoffset, name, &val, sizeof(val));
  1236. }
  1237. /**
  1238. * fdt_appendprop_u64 - append a 64-bit integer value to a property
  1239. * @fdt: pointer to the device tree blob
  1240. * @nodeoffset: offset of the node whose property to change
  1241. * @name: name of the property to change
  1242. * @val: 64-bit integer value to append to the property (native endian)
  1243. *
  1244. * fdt_appendprop_u64() appends the given 64-bit integer value
  1245. * (converting to big-endian if necessary) to the value of the named
  1246. * property in the given node, or creates a new property with that
  1247. * value if it does not already exist.
  1248. *
  1249. * This function may insert data into the blob, and will therefore
  1250. * change the offsets of some existing nodes.
  1251. *
  1252. * returns:
  1253. * 0, on success
  1254. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1255. * contain the new property value
  1256. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1257. * -FDT_ERR_BADLAYOUT,
  1258. * -FDT_ERR_BADMAGIC,
  1259. * -FDT_ERR_BADVERSION,
  1260. * -FDT_ERR_BADSTATE,
  1261. * -FDT_ERR_BADSTRUCTURE,
  1262. * -FDT_ERR_BADLAYOUT,
  1263. * -FDT_ERR_TRUNCATED, standard meanings
  1264. */
  1265. static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
  1266. const char *name, uint64_t val)
  1267. {
  1268. val = cpu_to_fdt64(val);
  1269. return fdt_appendprop(fdt, nodeoffset, name, &val, sizeof(val));
  1270. }
  1271. /**
  1272. * fdt_appendprop_cell - append a single cell value to a property
  1273. *
  1274. * This is an alternative name for fdt_appendprop_u32()
  1275. */
  1276. static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
  1277. const char *name, uint32_t val)
  1278. {
  1279. return fdt_appendprop_u32(fdt, nodeoffset, name, val);
  1280. }
  1281. /**
  1282. * fdt_appendprop_string - append a string to a property
  1283. * @fdt: pointer to the device tree blob
  1284. * @nodeoffset: offset of the node whose property to change
  1285. * @name: name of the property to change
  1286. * @str: string value to append to the property
  1287. *
  1288. * fdt_appendprop_string() appends the given string to the value of
  1289. * the named property in the given node, or creates a new property
  1290. * with that value if it does not already exist.
  1291. *
  1292. * This function may insert data into the blob, and will therefore
  1293. * change the offsets of some existing nodes.
  1294. *
  1295. * returns:
  1296. * 0, on success
  1297. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1298. * contain the new property value
  1299. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1300. * -FDT_ERR_BADLAYOUT,
  1301. * -FDT_ERR_BADMAGIC,
  1302. * -FDT_ERR_BADVERSION,
  1303. * -FDT_ERR_BADSTATE,
  1304. * -FDT_ERR_BADSTRUCTURE,
  1305. * -FDT_ERR_BADLAYOUT,
  1306. * -FDT_ERR_TRUNCATED, standard meanings
  1307. */
  1308. #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
  1309. fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  1310. /**
  1311. * fdt_delprop - delete a property
  1312. * @fdt: pointer to the device tree blob
  1313. * @nodeoffset: offset of the node whose property to nop
  1314. * @name: name of the property to nop
  1315. *
  1316. * fdt_del_property() will delete the given property.
  1317. *
  1318. * This function will delete data from the blob, and will therefore
  1319. * change the offsets of some existing nodes.
  1320. *
  1321. * returns:
  1322. * 0, on success
  1323. * -FDT_ERR_NOTFOUND, node does not have the named property
  1324. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1325. * -FDT_ERR_BADLAYOUT,
  1326. * -FDT_ERR_BADMAGIC,
  1327. * -FDT_ERR_BADVERSION,
  1328. * -FDT_ERR_BADSTATE,
  1329. * -FDT_ERR_BADSTRUCTURE,
  1330. * -FDT_ERR_TRUNCATED, standard meanings
  1331. */
  1332. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  1333. /**
  1334. * fdt_add_subnode_namelen - creates a new node based on substring
  1335. * @fdt: pointer to the device tree blob
  1336. * @parentoffset: structure block offset of a node
  1337. * @name: name of the subnode to locate
  1338. * @namelen: number of characters of name to consider
  1339. *
  1340. * Identical to fdt_add_subnode(), but use only the first namelen
  1341. * characters of name as the name of the new node. This is useful for
  1342. * creating subnodes based on a portion of a larger string, such as a
  1343. * full path.
  1344. */
  1345. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  1346. const char *name, int namelen);
  1347. /**
  1348. * fdt_add_subnode - creates a new node
  1349. * @fdt: pointer to the device tree blob
  1350. * @parentoffset: structure block offset of a node
  1351. * @name: name of the subnode to locate
  1352. *
  1353. * fdt_add_subnode() creates a new node as a subnode of the node at
  1354. * structure block offset parentoffset, with the given name (which
  1355. * should include the unit address, if any).
  1356. *
  1357. * This function will insert data into the blob, and will therefore
  1358. * change the offsets of some existing nodes.
  1359. * returns:
  1360. * structure block offset of the created nodeequested subnode (>=0), on success
  1361. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  1362. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE tag
  1363. * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
  1364. * the given name
  1365. * -FDT_ERR_NOSPACE, if there is insufficient free space in the
  1366. * blob to contain the new node
  1367. * -FDT_ERR_NOSPACE
  1368. * -FDT_ERR_BADLAYOUT
  1369. * -FDT_ERR_BADMAGIC,
  1370. * -FDT_ERR_BADVERSION,
  1371. * -FDT_ERR_BADSTATE,
  1372. * -FDT_ERR_BADSTRUCTURE,
  1373. * -FDT_ERR_TRUNCATED, standard meanings.
  1374. */
  1375. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  1376. /**
  1377. * fdt_del_node - delete a node (subtree)
  1378. * @fdt: pointer to the device tree blob
  1379. * @nodeoffset: offset of the node to nop
  1380. *
  1381. * fdt_del_node() will remove the given node, including all its
  1382. * subnodes if any, from the blob.
  1383. *
  1384. * This function will delete data from the blob, and will therefore
  1385. * change the offsets of some existing nodes.
  1386. *
  1387. * returns:
  1388. * 0, on success
  1389. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1390. * -FDT_ERR_BADLAYOUT,
  1391. * -FDT_ERR_BADMAGIC,
  1392. * -FDT_ERR_BADVERSION,
  1393. * -FDT_ERR_BADSTATE,
  1394. * -FDT_ERR_BADSTRUCTURE,
  1395. * -FDT_ERR_TRUNCATED, standard meanings
  1396. */
  1397. int fdt_del_node(void *fdt, int nodeoffset);
  1398. /**********************************************************************/
  1399. /* Debugging / informational functions */
  1400. /**********************************************************************/
  1401. const char *fdt_strerror(int errval);
  1402. #endif /* _LIBFDT_H */