1
0

libfdt.h 55 KB

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