libfdt.h 88 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540
  1. /* SPDX-License-Identifier: (GPL-2.0-or-later OR BSD-2-Clause) */
  2. #ifndef LIBFDT_H
  3. #define LIBFDT_H
  4. /*
  5. * libfdt - Flat Device Tree manipulation
  6. * Copyright (C) 2006 David Gibson, IBM Corporation.
  7. */
  8. #include "libfdt_env.h"
  9. #include "fdt.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define FDT_FIRST_SUPPORTED_VERSION 0x02
  14. #define FDT_LAST_COMPATIBLE_VERSION 0x10
  15. #define FDT_LAST_SUPPORTED_VERSION 0x11
  16. /* Error codes: informative error codes */
  17. #define FDT_ERR_NOTFOUND 1
  18. /* FDT_ERR_NOTFOUND: The requested node or property does not exist */
  19. #define FDT_ERR_EXISTS 2
  20. /* FDT_ERR_EXISTS: Attempted to create a node or property which
  21. * already exists */
  22. #define FDT_ERR_NOSPACE 3
  23. /* FDT_ERR_NOSPACE: Operation needed to expand the device
  24. * tree, but its buffer did not have sufficient space to
  25. * contain the expanded tree. Use fdt_open_into() to move the
  26. * device tree to a buffer with more space. */
  27. /* Error codes: codes for bad parameters */
  28. #define FDT_ERR_BADOFFSET 4
  29. /* FDT_ERR_BADOFFSET: Function was passed a structure block
  30. * offset which is out-of-bounds, or which points to an
  31. * unsuitable part of the structure for the operation. */
  32. #define FDT_ERR_BADPATH 5
  33. /* FDT_ERR_BADPATH: Function was passed a badly formatted path
  34. * (e.g. missing a leading / for a function which requires an
  35. * absolute path) */
  36. #define FDT_ERR_BADPHANDLE 6
  37. /* FDT_ERR_BADPHANDLE: Function was passed an invalid phandle.
  38. * This can be caused either by an invalid phandle property
  39. * length, or the phandle value was either 0 or -1, which are
  40. * not permitted. */
  41. #define FDT_ERR_BADSTATE 7
  42. /* FDT_ERR_BADSTATE: Function was passed an incomplete device
  43. * tree created by the sequential-write functions, which is
  44. * not sufficiently complete for the requested operation. */
  45. /* Error codes: codes for bad device tree blobs */
  46. #define FDT_ERR_TRUNCATED 8
  47. /* FDT_ERR_TRUNCATED: FDT or a sub-block is improperly
  48. * terminated (overflows, goes outside allowed bounds, or
  49. * isn't properly terminated). */
  50. #define FDT_ERR_BADMAGIC 9
  51. /* FDT_ERR_BADMAGIC: Given "device tree" appears not to be a
  52. * device tree at all - it is missing the flattened device
  53. * tree magic number. */
  54. #define FDT_ERR_BADVERSION 10
  55. /* FDT_ERR_BADVERSION: Given device tree has a version which
  56. * can't be handled by the requested operation. For
  57. * read-write functions, this may mean that fdt_open_into() is
  58. * required to convert the tree to the expected version. */
  59. #define FDT_ERR_BADSTRUCTURE 11
  60. /* FDT_ERR_BADSTRUCTURE: Given device tree has a corrupt
  61. * structure block or other serious error (e.g. misnested
  62. * nodes, or subnodes preceding properties). */
  63. #define FDT_ERR_BADLAYOUT 12
  64. /* FDT_ERR_BADLAYOUT: For read-write functions, the given
  65. * device tree has it's sub-blocks in an order that the
  66. * function can't handle (memory reserve map, then structure,
  67. * then strings). Use fdt_open_into() to reorganize the tree
  68. * into a form suitable for the read-write operations. */
  69. /* "Can't happen" error indicating a bug in libfdt */
  70. #define FDT_ERR_INTERNAL 13
  71. /* FDT_ERR_INTERNAL: libfdt has failed an internal assertion.
  72. * Should never be returned, if it is, it indicates a bug in
  73. * libfdt itself. */
  74. /* Errors in device tree content */
  75. #define FDT_ERR_BADNCELLS 14
  76. /* FDT_ERR_BADNCELLS: Device tree has a #address-cells, #size-cells
  77. * or similar property with a bad format or value */
  78. #define FDT_ERR_BADVALUE 15
  79. /* FDT_ERR_BADVALUE: Device tree has a property with an unexpected
  80. * value. For example: a property expected to contain a string list
  81. * is not NUL-terminated within the length of its value. */
  82. #define FDT_ERR_BADOVERLAY 16
  83. /* FDT_ERR_BADOVERLAY: The device tree overlay, while
  84. * correctly structured, cannot be applied due to some
  85. * unexpected or missing value, property or node. */
  86. #define FDT_ERR_NOPHANDLES 17
  87. /* FDT_ERR_NOPHANDLES: The device tree doesn't have any
  88. * phandle available anymore without causing an overflow */
  89. #define FDT_ERR_BADFLAGS 18
  90. /* FDT_ERR_BADFLAGS: The function was passed a flags field that
  91. * contains invalid flags or an invalid combination of flags. */
  92. #define FDT_ERR_ALIGNMENT 19
  93. /* FDT_ERR_ALIGNMENT: The device tree base address is not 8-byte
  94. * aligned. */
  95. #define FDT_ERR_MAX 19
  96. /* constants */
  97. #define FDT_MAX_PHANDLE 0xfffffffe
  98. /* Valid values for phandles range from 1 to 2^32-2. */
  99. /**********************************************************************/
  100. /* Low-level functions (you probably don't need these) */
  101. /**********************************************************************/
  102. /**
  103. * fdt_offset_ptr - safely get a byte range within the device tree blob
  104. * @fdt: Pointer to the device tree blob
  105. * @offset: Offset within the blob to the desired byte range
  106. * @checklen: Required length of the byte range
  107. *
  108. * fdt_offset_ptr() returns a pointer to the byte range of length @checklen at
  109. * the given @offset within the device tree blob, after verifying that the byte
  110. * range fits entirely within the blob and does not overflow.
  111. *
  112. * returns:
  113. * pointer to the byte range, on success
  114. * NULL, if the requested range does not fit within the blob
  115. */
  116. #ifndef SWIG /* This function is not useful in Python */
  117. const void *fdt_offset_ptr(const void *fdt, int offset, unsigned int checklen);
  118. #endif
  119. static inline void *fdt_offset_ptr_w(void *fdt, int offset, int checklen)
  120. {
  121. return (void *)(uintptr_t)fdt_offset_ptr(fdt, offset, checklen);
  122. }
  123. /**
  124. * fdt_next_tag - get next tag in the device tree
  125. * @fdt: Pointer to the device tree blob
  126. * @offset: Offset within the blob to start searching
  127. * @nextoffset: Pointer to variable to store the offset of the next tag
  128. *
  129. * fdt_next_tag() returns the tag type of the next tag in the device tree
  130. * blob starting from the given @offset. If @nextoffset is non-NULL, it will
  131. * be set to the offset immediately following the tag.
  132. *
  133. * returns:
  134. * the tag type (FDT_BEGIN_NODE, FDT_END_NODE, FDT_PROP, FDT_NOP, FDT_END),
  135. * FDT_END, if offset is out of bounds
  136. */
  137. uint32_t fdt_next_tag(const void *fdt, int offset, int *nextoffset);
  138. /*
  139. * External helpers to access words from a device tree blob. They're built
  140. * to work even with unaligned pointers on platforms (such as ARMv5) that don't
  141. * like unaligned loads and stores.
  142. */
  143. static inline uint16_t fdt16_ld(const fdt16_t *p)
  144. {
  145. const uint8_t *bp = (const uint8_t *)p;
  146. return ((uint16_t)bp[0] << 8) | bp[1];
  147. }
  148. static inline uint32_t fdt32_ld(const fdt32_t *p)
  149. {
  150. const uint8_t *bp = (const uint8_t *)p;
  151. return ((uint32_t)bp[0] << 24)
  152. | ((uint32_t)bp[1] << 16)
  153. | ((uint32_t)bp[2] << 8)
  154. | bp[3];
  155. }
  156. static inline void fdt32_st(void *property, uint32_t value)
  157. {
  158. uint8_t *bp = (uint8_t *)property;
  159. bp[0] = value >> 24;
  160. bp[1] = (value >> 16) & 0xff;
  161. bp[2] = (value >> 8) & 0xff;
  162. bp[3] = value & 0xff;
  163. }
  164. static inline uint64_t fdt64_ld(const fdt64_t *p)
  165. {
  166. const uint8_t *bp = (const uint8_t *)p;
  167. return ((uint64_t)bp[0] << 56)
  168. | ((uint64_t)bp[1] << 48)
  169. | ((uint64_t)bp[2] << 40)
  170. | ((uint64_t)bp[3] << 32)
  171. | ((uint64_t)bp[4] << 24)
  172. | ((uint64_t)bp[5] << 16)
  173. | ((uint64_t)bp[6] << 8)
  174. | bp[7];
  175. }
  176. static inline void fdt64_st(void *property, uint64_t value)
  177. {
  178. uint8_t *bp = (uint8_t *)property;
  179. bp[0] = value >> 56;
  180. bp[1] = (value >> 48) & 0xff;
  181. bp[2] = (value >> 40) & 0xff;
  182. bp[3] = (value >> 32) & 0xff;
  183. bp[4] = (value >> 24) & 0xff;
  184. bp[5] = (value >> 16) & 0xff;
  185. bp[6] = (value >> 8) & 0xff;
  186. bp[7] = value & 0xff;
  187. }
  188. /**********************************************************************/
  189. /* Traversal functions */
  190. /**********************************************************************/
  191. int fdt_next_node(const void *fdt, int offset, int *depth);
  192. /**
  193. * fdt_first_subnode() - get offset of first direct subnode
  194. * @fdt: FDT blob
  195. * @offset: Offset of node to check
  196. *
  197. * Return: offset of first subnode, or -FDT_ERR_NOTFOUND if there is none
  198. */
  199. int fdt_first_subnode(const void *fdt, int offset);
  200. /**
  201. * fdt_next_subnode() - get offset of next direct subnode
  202. * @fdt: FDT blob
  203. * @offset: Offset of previous subnode
  204. *
  205. * After first calling fdt_first_subnode(), call this function repeatedly to
  206. * get direct subnodes of a parent node.
  207. *
  208. * Return: offset of next subnode, or -FDT_ERR_NOTFOUND if there are no more
  209. * subnodes
  210. */
  211. int fdt_next_subnode(const void *fdt, int offset);
  212. /**
  213. * fdt_for_each_subnode - iterate over all subnodes of a parent
  214. *
  215. * @node: child node (int, lvalue)
  216. * @fdt: FDT blob (const void *)
  217. * @parent: parent node (int)
  218. *
  219. * This is actually a wrapper around a for loop and would be used like so:
  220. *
  221. * fdt_for_each_subnode(node, fdt, parent) {
  222. * Use node
  223. * ...
  224. * }
  225. *
  226. * if ((node < 0) && (node != -FDT_ERR_NOTFOUND)) {
  227. * Error handling
  228. * }
  229. *
  230. * Note that this is implemented as a macro and @node is used as
  231. * iterator in the loop. The parent variable be constant or even a
  232. * literal.
  233. */
  234. #define fdt_for_each_subnode(node, fdt, parent) \
  235. for (node = fdt_first_subnode(fdt, parent); \
  236. node >= 0; \
  237. node = fdt_next_subnode(fdt, node))
  238. /**********************************************************************/
  239. /* General functions */
  240. /**********************************************************************/
  241. #define fdt_get_header(fdt, field) \
  242. (fdt32_ld(&((const struct fdt_header *)(fdt))->field))
  243. #define fdt_magic(fdt) (fdt_get_header(fdt, magic))
  244. #define fdt_totalsize(fdt) (fdt_get_header(fdt, totalsize))
  245. #define fdt_off_dt_struct(fdt) (fdt_get_header(fdt, off_dt_struct))
  246. #define fdt_off_dt_strings(fdt) (fdt_get_header(fdt, off_dt_strings))
  247. #define fdt_off_mem_rsvmap(fdt) (fdt_get_header(fdt, off_mem_rsvmap))
  248. #define fdt_version(fdt) (fdt_get_header(fdt, version))
  249. #define fdt_last_comp_version(fdt) (fdt_get_header(fdt, last_comp_version))
  250. #define fdt_boot_cpuid_phys(fdt) (fdt_get_header(fdt, boot_cpuid_phys))
  251. #define fdt_size_dt_strings(fdt) (fdt_get_header(fdt, size_dt_strings))
  252. #define fdt_size_dt_struct(fdt) (fdt_get_header(fdt, size_dt_struct))
  253. #define fdt_set_hdr_(name) \
  254. static inline void fdt_set_##name(void *fdt, uint32_t val) \
  255. { \
  256. struct fdt_header *fdth = (struct fdt_header *)fdt; \
  257. fdth->name = cpu_to_fdt32(val); \
  258. }
  259. fdt_set_hdr_(magic)
  260. fdt_set_hdr_(totalsize)
  261. fdt_set_hdr_(off_dt_struct)
  262. fdt_set_hdr_(off_dt_strings)
  263. fdt_set_hdr_(off_mem_rsvmap)
  264. fdt_set_hdr_(version)
  265. fdt_set_hdr_(last_comp_version)
  266. fdt_set_hdr_(boot_cpuid_phys)
  267. fdt_set_hdr_(size_dt_strings)
  268. fdt_set_hdr_(size_dt_struct)
  269. #undef fdt_set_hdr_
  270. /**
  271. * fdt_header_size - return the size of the tree's header
  272. * @fdt: pointer to a flattened device tree
  273. *
  274. * Return: size of DTB header in bytes
  275. */
  276. size_t fdt_header_size(const void *fdt);
  277. /**
  278. * fdt_header_size_ - internal function to get header size from a version number
  279. * @version: device tree version number
  280. *
  281. * Return: size of DTB header in bytes
  282. */
  283. size_t fdt_header_size_(uint32_t version);
  284. /**
  285. * fdt_check_header - sanity check a device tree header
  286. * @fdt: pointer to data which might be a flattened device tree
  287. *
  288. * fdt_check_header() checks that the given buffer contains what
  289. * appears to be a flattened device tree, and that the header contains
  290. * valid information (to the extent that can be determined from the
  291. * header alone).
  292. *
  293. * returns:
  294. * 0, if the buffer appears to contain a valid device tree
  295. * -FDT_ERR_BADMAGIC,
  296. * -FDT_ERR_BADVERSION,
  297. * -FDT_ERR_BADSTATE,
  298. * -FDT_ERR_TRUNCATED, standard meanings, as above
  299. */
  300. int fdt_check_header(const void *fdt);
  301. /**
  302. * fdt_move - move a device tree around in memory
  303. * @fdt: pointer to the device tree to move
  304. * @buf: pointer to memory where the device is to be moved
  305. * @bufsize: size of the memory space at buf
  306. *
  307. * fdt_move() relocates, if possible, the device tree blob located at
  308. * fdt to the buffer at buf of size bufsize. The buffer may overlap
  309. * with the existing device tree blob at fdt. Therefore,
  310. * fdt_move(fdt, fdt, fdt_totalsize(fdt))
  311. * should always succeed.
  312. *
  313. * returns:
  314. * 0, on success
  315. * -FDT_ERR_NOSPACE, bufsize is insufficient to contain the device tree
  316. * -FDT_ERR_BADMAGIC,
  317. * -FDT_ERR_BADVERSION,
  318. * -FDT_ERR_BADSTATE, standard meanings
  319. */
  320. int fdt_move(const void *fdt, void *buf, int bufsize);
  321. /**********************************************************************/
  322. /* Read-only functions */
  323. /**********************************************************************/
  324. /**
  325. * fdt_check_full - check device tree validity
  326. * @fdt: pointer to the device tree blob
  327. * @bufsize: size of the buffer containing the device tree
  328. *
  329. * fdt_check_full() checks that the given buffer contains a valid
  330. * flattened device tree and that the tree structure is internally
  331. * consistent. This is a more thorough check than fdt_check_header().
  332. *
  333. * returns:
  334. * 0, on success
  335. * -FDT_ERR_BADMAGIC,
  336. * -FDT_ERR_BADVERSION,
  337. * -FDT_ERR_BADSTATE,
  338. * -FDT_ERR_BADSTRUCTURE,
  339. * -FDT_ERR_TRUNCATED, standard meanings
  340. */
  341. int fdt_check_full(const void *fdt, size_t bufsize);
  342. /**
  343. * fdt_get_string - retrieve a string from the strings block of a device tree
  344. * @fdt: pointer to the device tree blob
  345. * @stroffset: offset of the string within the strings block (native endian)
  346. * @lenp: optional pointer to return the string's length
  347. *
  348. * fdt_get_string() retrieves a pointer to a single string from the
  349. * strings block of the device tree blob at fdt, and optionally also
  350. * returns the string's length in *lenp.
  351. *
  352. * returns:
  353. * a pointer to the string, on success
  354. * NULL, if stroffset is out of bounds, or doesn't point to a valid string
  355. */
  356. const char *fdt_get_string(const void *fdt, int stroffset, int *lenp);
  357. /**
  358. * fdt_string - retrieve a string from the strings block of a device tree
  359. * @fdt: pointer to the device tree blob
  360. * @stroffset: offset of the string within the strings block (native endian)
  361. *
  362. * fdt_string() retrieves a pointer to a single string from the
  363. * strings block of the device tree blob at fdt.
  364. *
  365. * returns:
  366. * a pointer to the string, on success
  367. * NULL, if stroffset is out of bounds, or doesn't point to a valid string
  368. */
  369. const char *fdt_string(const void *fdt, int stroffset);
  370. /**
  371. * fdt_find_max_phandle - find and return the highest phandle in a tree
  372. * @fdt: pointer to the device tree blob
  373. * @phandle: return location for the highest phandle value found in the tree
  374. *
  375. * fdt_find_max_phandle() finds the highest phandle value in the given device
  376. * tree. The value returned in @phandle is only valid if the function returns
  377. * success.
  378. *
  379. * returns:
  380. * 0 on success or a negative error code on failure
  381. */
  382. int fdt_find_max_phandle(const void *fdt, uint32_t *phandle);
  383. /**
  384. * fdt_get_max_phandle - retrieves the highest phandle in a tree
  385. * @fdt: pointer to the device tree blob
  386. *
  387. * fdt_get_max_phandle retrieves the highest phandle in the given
  388. * device tree. This will ignore badly formatted phandles, or phandles
  389. * with a value of 0 or -1.
  390. *
  391. * This function is deprecated in favour of fdt_find_max_phandle().
  392. *
  393. * returns:
  394. * the highest phandle on success
  395. * 0, if no phandle was found in the device tree
  396. * -1, if an error occurred
  397. */
  398. static inline uint32_t fdt_get_max_phandle(const void *fdt)
  399. {
  400. uint32_t phandle;
  401. int err;
  402. err = fdt_find_max_phandle(fdt, &phandle);
  403. if (err < 0)
  404. return (uint32_t)-1;
  405. return phandle;
  406. }
  407. /**
  408. * fdt_generate_phandle - return a new, unused phandle for a device tree blob
  409. * @fdt: pointer to the device tree blob
  410. * @phandle: return location for the new phandle
  411. *
  412. * Walks the device tree blob and looks for the highest phandle value. On
  413. * success, the new, unused phandle value (one higher than the previously
  414. * highest phandle value in the device tree blob) will be returned in the
  415. * @phandle parameter.
  416. *
  417. * Return: 0 on success or a negative error-code on failure
  418. */
  419. int fdt_generate_phandle(const void *fdt, uint32_t *phandle);
  420. /**
  421. * fdt_num_mem_rsv - retrieve the number of memory reserve map entries
  422. * @fdt: pointer to the device tree blob
  423. *
  424. * Returns the number of entries in the device tree blob's memory
  425. * reservation map. This does not include the terminating 0,0 entry
  426. * or any other (0,0) entries reserved for expansion.
  427. *
  428. * returns:
  429. * the number of entries
  430. */
  431. int fdt_num_mem_rsv(const void *fdt);
  432. /**
  433. * fdt_get_mem_rsv - retrieve one memory reserve map entry
  434. * @fdt: pointer to the device tree blob
  435. * @n: index of reserve map entry
  436. * @address: pointer to 64-bit variable to hold the start address
  437. * @size: pointer to 64-bit variable to hold the size of the entry
  438. *
  439. * On success, @address and @size will contain the address and size of
  440. * the n-th reserve map entry from the device tree blob, in
  441. * native-endian format.
  442. *
  443. * returns:
  444. * 0, on success
  445. * -FDT_ERR_BADMAGIC,
  446. * -FDT_ERR_BADVERSION,
  447. * -FDT_ERR_BADSTATE, standard meanings
  448. */
  449. int fdt_get_mem_rsv(const void *fdt, int n, uint64_t *address, uint64_t *size);
  450. /**
  451. * fdt_subnode_offset_namelen - find a subnode based on substring
  452. * @fdt: pointer to the device tree blob
  453. * @parentoffset: structure block offset of a node
  454. * @name: name of the subnode to locate
  455. * @namelen: number of characters of name to consider
  456. *
  457. * Identical to fdt_subnode_offset(), but only examine the first
  458. * namelen characters of name for matching the subnode name. This is
  459. * useful for finding subnodes based on a portion of a larger string,
  460. * such as a full path.
  461. *
  462. * Return: offset of the subnode or -FDT_ERR_NOTFOUND if name not found.
  463. */
  464. #ifndef SWIG /* Not available in Python */
  465. int fdt_subnode_offset_namelen(const void *fdt, int parentoffset,
  466. const char *name, int namelen);
  467. #endif
  468. /**
  469. * fdt_subnode_offset - find a subnode of a given node
  470. * @fdt: pointer to the device tree blob
  471. * @parentoffset: structure block offset of a node
  472. * @name: name of the subnode to locate
  473. *
  474. * fdt_subnode_offset() finds a subnode of the node at structure block
  475. * offset parentoffset with the given name. name may include a unit
  476. * address, in which case fdt_subnode_offset() will find the subnode
  477. * with that unit address, or the unit address may be omitted, in
  478. * which case fdt_subnode_offset() will find an arbitrary subnode
  479. * whose name excluding unit address matches the given name.
  480. *
  481. * returns:
  482. * structure block offset of the requested subnode (>=0), on success
  483. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  484. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  485. * tag
  486. * -FDT_ERR_BADMAGIC,
  487. * -FDT_ERR_BADVERSION,
  488. * -FDT_ERR_BADSTATE,
  489. * -FDT_ERR_BADSTRUCTURE,
  490. * -FDT_ERR_TRUNCATED, standard meanings.
  491. */
  492. int fdt_subnode_offset(const void *fdt, int parentoffset, const char *name);
  493. /**
  494. * fdt_path_offset_namelen - find a tree node by its full path
  495. * @fdt: pointer to the device tree blob
  496. * @path: full path of the node to locate
  497. * @namelen: number of characters of path to consider
  498. *
  499. * Identical to fdt_path_offset(), but only consider the first namelen
  500. * characters of path as the path name.
  501. *
  502. * Return: offset of the node or negative libfdt error value otherwise
  503. */
  504. #ifndef SWIG /* Not available in Python */
  505. int fdt_path_offset_namelen(const void *fdt, const char *path, int namelen);
  506. #endif
  507. /**
  508. * fdt_path_offset - find a tree node by its full path
  509. * @fdt: pointer to the device tree blob
  510. * @path: full path of the node to locate
  511. *
  512. * fdt_path_offset() finds a node of a given path in the device tree.
  513. * Each path component may omit the unit address portion, but the
  514. * results of this are undefined if any such path component is
  515. * ambiguous (that is if there are multiple nodes at the relevant
  516. * level matching the given component, differentiated only by unit
  517. * address).
  518. *
  519. * If the path is not absolute (i.e. does not begin with '/'), the
  520. * first component is treated as an alias. That is, the property by
  521. * that name is looked up in the /aliases node, and the value of that
  522. * property used in place of that first component.
  523. *
  524. * For example, for this small fragment
  525. *
  526. * / {
  527. * aliases {
  528. * i2c2 = &foo; // RHS compiles to "/soc@0/i2c@30a40000/eeprom@52"
  529. * };
  530. * soc@0 {
  531. * foo: i2c@30a40000 {
  532. * bar: eeprom@52 {
  533. * };
  534. * };
  535. * };
  536. * };
  537. *
  538. * these would be equivalent:
  539. *
  540. * /soc@0/i2c@30a40000/eeprom@52
  541. * i2c2/eeprom@52
  542. *
  543. * returns:
  544. * structure block offset of the node with the requested path (>=0), on
  545. * success
  546. * -FDT_ERR_BADPATH, given path does not begin with '/' and the first
  547. * component is not a valid alias
  548. * -FDT_ERR_NOTFOUND, if the requested node does not exist
  549. * -FDT_ERR_BADMAGIC,
  550. * -FDT_ERR_BADVERSION,
  551. * -FDT_ERR_BADSTATE,
  552. * -FDT_ERR_BADSTRUCTURE,
  553. * -FDT_ERR_TRUNCATED, standard meanings.
  554. */
  555. int fdt_path_offset(const void *fdt, const char *path);
  556. /**
  557. * fdt_get_name - retrieve the name of a given node
  558. * @fdt: pointer to the device tree blob
  559. * @nodeoffset: structure block offset of the starting node
  560. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  561. *
  562. * fdt_get_name() retrieves the name (including unit address) of the
  563. * device tree node at structure block offset nodeoffset. If lenp is
  564. * non-NULL, the length of this name is also returned, in the integer
  565. * pointed to by lenp.
  566. *
  567. * returns:
  568. * pointer to the node's name, on success
  569. * If lenp is non-NULL, *lenp contains the length of that name
  570. * (>=0)
  571. * NULL, on error
  572. * if lenp is non-NULL *lenp contains an error code (<0):
  573. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  574. * tag
  575. * -FDT_ERR_BADMAGIC,
  576. * -FDT_ERR_BADVERSION,
  577. * -FDT_ERR_BADSTATE, standard meanings
  578. */
  579. const char *fdt_get_name(const void *fdt, int nodeoffset, int *lenp);
  580. /**
  581. * fdt_first_property_offset - find the offset of a node's first property
  582. * @fdt: pointer to the device tree blob
  583. * @nodeoffset: structure block offset of a node
  584. *
  585. * fdt_first_property_offset() finds the first property of the node at
  586. * the given structure block offset.
  587. *
  588. * returns:
  589. * structure block offset of the property (>=0), on success
  590. * -FDT_ERR_NOTFOUND, if the requested node has no properties
  591. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_BEGIN_NODE tag
  592. * -FDT_ERR_BADMAGIC,
  593. * -FDT_ERR_BADVERSION,
  594. * -FDT_ERR_BADSTATE,
  595. * -FDT_ERR_BADSTRUCTURE,
  596. * -FDT_ERR_TRUNCATED, standard meanings.
  597. */
  598. int fdt_first_property_offset(const void *fdt, int nodeoffset);
  599. /**
  600. * fdt_next_property_offset - step through a node's properties
  601. * @fdt: pointer to the device tree blob
  602. * @offset: structure block offset of a property
  603. *
  604. * fdt_next_property_offset() finds the property immediately after the
  605. * one at the given structure block offset. This will be a property
  606. * of the same node as the given property.
  607. *
  608. * returns:
  609. * structure block offset of the next property (>=0), on success
  610. * -FDT_ERR_NOTFOUND, if the given property is the last in its node
  611. * -FDT_ERR_BADOFFSET, if nodeoffset did not point to an FDT_PROP tag
  612. * -FDT_ERR_BADMAGIC,
  613. * -FDT_ERR_BADVERSION,
  614. * -FDT_ERR_BADSTATE,
  615. * -FDT_ERR_BADSTRUCTURE,
  616. * -FDT_ERR_TRUNCATED, standard meanings.
  617. */
  618. int fdt_next_property_offset(const void *fdt, int offset);
  619. /**
  620. * fdt_for_each_property_offset - iterate over all properties of a node
  621. *
  622. * @property: property offset (int, lvalue)
  623. * @fdt: FDT blob (const void *)
  624. * @node: node offset (int)
  625. *
  626. * This is actually a wrapper around a for loop and would be used like so:
  627. *
  628. * fdt_for_each_property_offset(property, fdt, node) {
  629. * Use property
  630. * ...
  631. * }
  632. *
  633. * if ((property < 0) && (property != -FDT_ERR_NOTFOUND)) {
  634. * Error handling
  635. * }
  636. *
  637. * Note that this is implemented as a macro and property is used as
  638. * iterator in the loop. The node variable can be constant or even a
  639. * literal.
  640. */
  641. #define fdt_for_each_property_offset(property, fdt, node) \
  642. for (property = fdt_first_property_offset(fdt, node); \
  643. property >= 0; \
  644. property = fdt_next_property_offset(fdt, property))
  645. /**
  646. * fdt_get_property_by_offset - retrieve the property at a given offset
  647. * @fdt: pointer to the device tree blob
  648. * @offset: offset of the property to retrieve
  649. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  650. *
  651. * fdt_get_property_by_offset() retrieves a pointer to the
  652. * fdt_property structure within the device tree blob at the given
  653. * offset. If lenp is non-NULL, the length of the property value is
  654. * also returned, in the integer pointed to by lenp.
  655. *
  656. * Note that this code only works on device tree versions >= 16. fdt_getprop()
  657. * works on all versions.
  658. *
  659. * returns:
  660. * pointer to the structure representing the property
  661. * if lenp is non-NULL, *lenp contains the length of the property
  662. * value (>=0)
  663. * NULL, on error
  664. * if lenp is non-NULL, *lenp contains an error code (<0):
  665. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  666. * -FDT_ERR_BADMAGIC,
  667. * -FDT_ERR_BADVERSION,
  668. * -FDT_ERR_BADSTATE,
  669. * -FDT_ERR_BADSTRUCTURE,
  670. * -FDT_ERR_TRUNCATED, standard meanings
  671. */
  672. const struct fdt_property *fdt_get_property_by_offset(const void *fdt,
  673. int offset,
  674. int *lenp);
  675. static inline struct fdt_property *fdt_get_property_by_offset_w(void *fdt,
  676. int offset,
  677. int *lenp)
  678. {
  679. return (struct fdt_property *)(uintptr_t)
  680. fdt_get_property_by_offset(fdt, offset, lenp);
  681. }
  682. /**
  683. * fdt_get_property_namelen - find a property based on substring
  684. * @fdt: pointer to the device tree blob
  685. * @nodeoffset: offset of the node whose property to find
  686. * @name: name of the property to find
  687. * @namelen: number of characters of name to consider
  688. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  689. *
  690. * Identical to fdt_get_property(), but only examine the first namelen
  691. * characters of name for matching the property name.
  692. *
  693. * Return: pointer to the structure representing the property, or NULL
  694. * if not found
  695. */
  696. #ifndef SWIG /* Not available in Python */
  697. const struct fdt_property *fdt_get_property_namelen(const void *fdt,
  698. int nodeoffset,
  699. const char *name,
  700. int namelen, int *lenp);
  701. static inline struct fdt_property *
  702. fdt_get_property_namelen_w(void *fdt, int nodeoffset, const char *name,
  703. int namelen, int *lenp)
  704. {
  705. return (struct fdt_property *)(uintptr_t)fdt_get_property_namelen(
  706. fdt, nodeoffset, name, namelen, lenp);
  707. }
  708. #endif
  709. /**
  710. * fdt_get_property - find a given property in a given node
  711. * @fdt: pointer to the device tree blob
  712. * @nodeoffset: offset of the node whose property to find
  713. * @name: name of the property to find
  714. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  715. *
  716. * fdt_get_property() retrieves a pointer to the fdt_property
  717. * structure within the device tree blob corresponding to the property
  718. * named 'name' of the node at offset nodeoffset. If lenp is
  719. * non-NULL, the length of the property value is also returned, in the
  720. * integer pointed to by lenp.
  721. *
  722. * returns:
  723. * pointer to the structure representing the property
  724. * if lenp is non-NULL, *lenp contains the length of the property
  725. * value (>=0)
  726. * NULL, on error
  727. * if lenp is non-NULL, *lenp contains an error code (<0):
  728. * -FDT_ERR_NOTFOUND, node does not have named property
  729. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  730. * tag
  731. * -FDT_ERR_BADMAGIC,
  732. * -FDT_ERR_BADVERSION,
  733. * -FDT_ERR_BADSTATE,
  734. * -FDT_ERR_BADSTRUCTURE,
  735. * -FDT_ERR_TRUNCATED, standard meanings
  736. */
  737. const struct fdt_property *fdt_get_property(const void *fdt, int nodeoffset,
  738. const char *name, int *lenp);
  739. static inline struct fdt_property *fdt_get_property_w(void *fdt, int nodeoffset,
  740. const char *name,
  741. int *lenp)
  742. {
  743. return (struct fdt_property *)(uintptr_t)
  744. fdt_get_property(fdt, nodeoffset, name, lenp);
  745. }
  746. /**
  747. * fdt_getprop_by_offset - retrieve the value of a property at a given offset
  748. * @fdt: pointer to the device tree blob
  749. * @offset: offset of the property to read
  750. * @namep: pointer to a string variable (will be overwritten) or NULL
  751. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  752. *
  753. * fdt_getprop_by_offset() retrieves a pointer to the value of the
  754. * property at structure block offset 'offset' (this will be a pointer
  755. * to within the device blob itself, not a copy of the value). If
  756. * lenp is non-NULL, the length of the property value is also
  757. * returned, in the integer pointed to by lenp. If namep is non-NULL,
  758. * the property's name will also be returned in the char * pointed to
  759. * by namep (this will be a pointer to within the device tree's string
  760. * block, not a new copy of the name).
  761. *
  762. * returns:
  763. * pointer to the property's value
  764. * if lenp is non-NULL, *lenp contains the length of the property
  765. * value (>=0)
  766. * if namep is non-NULL *namep contains a pointer to the property
  767. * name.
  768. * NULL, on error
  769. * if lenp is non-NULL, *lenp contains an error code (<0):
  770. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_PROP tag
  771. * -FDT_ERR_BADMAGIC,
  772. * -FDT_ERR_BADVERSION,
  773. * -FDT_ERR_BADSTATE,
  774. * -FDT_ERR_BADSTRUCTURE,
  775. * -FDT_ERR_TRUNCATED, standard meanings
  776. */
  777. #ifndef SWIG /* This function is not useful in Python */
  778. const void *fdt_getprop_by_offset(const void *fdt, int offset,
  779. const char **namep, int *lenp);
  780. #endif
  781. /**
  782. * fdt_getprop_namelen - get property value based on substring
  783. * @fdt: pointer to the device tree blob
  784. * @nodeoffset: offset of the node whose property to find
  785. * @name: name of the property to find
  786. * @namelen: number of characters of name to consider
  787. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  788. *
  789. * Identical to fdt_getprop(), but only examine the first namelen
  790. * characters of name for matching the property name.
  791. *
  792. * Return: pointer to the property's value or NULL on error
  793. */
  794. #ifndef SWIG /* Not available in Python */
  795. const void *fdt_getprop_namelen(const void *fdt, int nodeoffset,
  796. const char *name, int namelen, int *lenp);
  797. static inline void *fdt_getprop_namelen_w(void *fdt, int nodeoffset,
  798. const char *name, int namelen,
  799. int *lenp)
  800. {
  801. return (void *)(uintptr_t)fdt_getprop_namelen(fdt, nodeoffset, name,
  802. namelen, lenp);
  803. }
  804. #endif
  805. /**
  806. * fdt_getprop - retrieve the value of a given property
  807. * @fdt: pointer to the device tree blob
  808. * @nodeoffset: offset of the node whose property to find
  809. * @name: name of the property to find
  810. * @lenp: pointer to an integer variable (will be overwritten) or NULL
  811. *
  812. * fdt_getprop() retrieves a pointer to the value of the property
  813. * named @name of the node at offset @nodeoffset (this will be a
  814. * pointer to within the device blob itself, not a copy of the value).
  815. * If @lenp is non-NULL, the length of the property value is also
  816. * returned, in the integer pointed to by @lenp.
  817. *
  818. * returns:
  819. * pointer to the property's value
  820. * if lenp is non-NULL, *lenp contains the length of the property
  821. * value (>=0)
  822. * NULL, on error
  823. * if lenp is non-NULL, *lenp contains an error code (<0):
  824. * -FDT_ERR_NOTFOUND, node does not have named property
  825. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE
  826. * tag
  827. * -FDT_ERR_BADMAGIC,
  828. * -FDT_ERR_BADVERSION,
  829. * -FDT_ERR_BADSTATE,
  830. * -FDT_ERR_BADSTRUCTURE,
  831. * -FDT_ERR_TRUNCATED, standard meanings
  832. */
  833. const void *fdt_getprop(const void *fdt, int nodeoffset,
  834. const char *name, int *lenp);
  835. static inline void *fdt_getprop_w(void *fdt, int nodeoffset,
  836. const char *name, int *lenp)
  837. {
  838. return (void *)(uintptr_t)fdt_getprop(fdt, nodeoffset, name, lenp);
  839. }
  840. /**
  841. * fdt_get_phandle - retrieve the phandle of a given node
  842. * @fdt: pointer to the device tree blob
  843. * @nodeoffset: structure block offset of the node
  844. *
  845. * fdt_get_phandle() retrieves the phandle of the device tree node at
  846. * structure block offset nodeoffset.
  847. *
  848. * returns:
  849. * the phandle of the node at nodeoffset, on success (!= 0, != -1)
  850. * 0, if the node has no phandle, or another error occurs
  851. */
  852. uint32_t fdt_get_phandle(const void *fdt, int nodeoffset);
  853. /**
  854. * fdt_get_alias_namelen - get alias based on substring
  855. * @fdt: pointer to the device tree blob
  856. * @name: name of the alias to look up
  857. * @namelen: number of characters of name to consider
  858. *
  859. * Identical to fdt_get_alias(), but only examine the first @namelen
  860. * characters of @name for matching the alias name.
  861. *
  862. * Return: a pointer to the expansion of the alias named @name, if it exists,
  863. * NULL otherwise
  864. */
  865. #ifndef SWIG /* Not available in Python */
  866. const char *fdt_get_alias_namelen(const void *fdt,
  867. const char *name, int namelen);
  868. #endif
  869. /**
  870. * fdt_get_alias - retrieve the path referenced by a given alias
  871. * @fdt: pointer to the device tree blob
  872. * @name: name of the alias to look up
  873. *
  874. * fdt_get_alias() retrieves the value of a given alias. That is, the
  875. * value of the property named @name in the node /aliases.
  876. *
  877. * returns:
  878. * a pointer to the expansion of the alias named 'name', if it exists
  879. * NULL, if the given alias or the /aliases node does not exist
  880. */
  881. const char *fdt_get_alias(const void *fdt, const char *name);
  882. /**
  883. * fdt_get_symbol_namelen - get symbol based on substring
  884. * @fdt: pointer to the device tree blob
  885. * @name: name of the symbol to look up
  886. * @namelen: number of characters of name to consider
  887. *
  888. * Identical to fdt_get_symbol(), but only examine the first @namelen
  889. * characters of @name for matching the symbol name.
  890. *
  891. * Return: a pointer to the expansion of the symbol named @name, if it exists,
  892. * NULL otherwise
  893. */
  894. #ifndef SWIG /* Not available in Python */
  895. const char *fdt_get_symbol_namelen(const void *fdt,
  896. const char *name, int namelen);
  897. #endif
  898. /**
  899. * fdt_get_symbol - retrieve the path referenced by a given symbol
  900. * @fdt: pointer to the device tree blob
  901. * @name: name of the symbol to look up
  902. *
  903. * fdt_get_symbol() retrieves the value of a given symbol. That is,
  904. * the value of the property named @name in the node
  905. * /__symbols__. Such a node exists only for a device tree blob that
  906. * has been compiled with the -@ dtc option. Each property corresponds
  907. * to a label appearing in the device tree source, with the name of
  908. * the property being the label and the value being the full path of
  909. * the node it is attached to.
  910. *
  911. * returns:
  912. * a pointer to the expansion of the symbol named 'name', if it exists
  913. * NULL, if the given symbol or the /__symbols__ node does not exist
  914. */
  915. const char *fdt_get_symbol(const void *fdt, const char *name);
  916. /**
  917. * fdt_get_path - determine the full path of a node
  918. * @fdt: pointer to the device tree blob
  919. * @nodeoffset: offset of the node whose path to find
  920. * @buf: character buffer to contain the returned path (will be overwritten)
  921. * @buflen: size of the character buffer at buf
  922. *
  923. * fdt_get_path() computes the full path of the node at offset
  924. * nodeoffset, and records that path in the buffer at buf.
  925. *
  926. * NOTE: This function is expensive, as it must scan the device tree
  927. * structure from the start to nodeoffset.
  928. *
  929. * returns:
  930. * 0, on success
  931. * buf contains the absolute path of the node at
  932. * nodeoffset, as a NUL-terminated string.
  933. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  934. * -FDT_ERR_NOSPACE, the path of the given node is longer than (bufsize-1)
  935. * characters and will not fit in the given buffer.
  936. * -FDT_ERR_BADMAGIC,
  937. * -FDT_ERR_BADVERSION,
  938. * -FDT_ERR_BADSTATE,
  939. * -FDT_ERR_BADSTRUCTURE, standard meanings
  940. */
  941. int fdt_get_path(const void *fdt, int nodeoffset, char *buf, int buflen);
  942. /**
  943. * fdt_supernode_atdepth_offset - find a specific ancestor of a node
  944. * @fdt: pointer to the device tree blob
  945. * @nodeoffset: offset of the node whose parent to find
  946. * @supernodedepth: depth of the ancestor to find
  947. * @nodedepth: pointer to an integer variable (will be overwritten) or NULL
  948. *
  949. * fdt_supernode_atdepth_offset() finds an ancestor of the given node
  950. * at a specific depth from the root (where the root itself has depth
  951. * 0, its immediate subnodes depth 1 and so forth). So
  952. * fdt_supernode_atdepth_offset(fdt, nodeoffset, 0, NULL);
  953. * will always return 0, the offset of the root node. If the node at
  954. * nodeoffset has depth D, then:
  955. * fdt_supernode_atdepth_offset(fdt, nodeoffset, D, NULL);
  956. * will return nodeoffset itself.
  957. *
  958. * NOTE: This function is expensive, as it must scan the device tree
  959. * structure from the start to nodeoffset.
  960. *
  961. * returns:
  962. * structure block offset of the node at node offset's ancestor
  963. * of depth supernodedepth (>=0), on success
  964. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  965. * -FDT_ERR_NOTFOUND, supernodedepth was greater than the depth of
  966. * nodeoffset
  967. * -FDT_ERR_BADMAGIC,
  968. * -FDT_ERR_BADVERSION,
  969. * -FDT_ERR_BADSTATE,
  970. * -FDT_ERR_BADSTRUCTURE, standard meanings
  971. */
  972. int fdt_supernode_atdepth_offset(const void *fdt, int nodeoffset,
  973. int supernodedepth, int *nodedepth);
  974. /**
  975. * fdt_node_depth - find the depth of a given node
  976. * @fdt: pointer to the device tree blob
  977. * @nodeoffset: offset of the node whose parent to find
  978. *
  979. * fdt_node_depth() finds the depth of a given node. The root node
  980. * has depth 0, its immediate subnodes depth 1 and so forth.
  981. *
  982. * NOTE: This function is expensive, as it must scan the device tree
  983. * structure from the start to nodeoffset.
  984. *
  985. * returns:
  986. * depth of the node at nodeoffset (>=0), on success
  987. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  988. * -FDT_ERR_BADMAGIC,
  989. * -FDT_ERR_BADVERSION,
  990. * -FDT_ERR_BADSTATE,
  991. * -FDT_ERR_BADSTRUCTURE, standard meanings
  992. */
  993. int fdt_node_depth(const void *fdt, int nodeoffset);
  994. /**
  995. * fdt_parent_offset - find the parent of a given node
  996. * @fdt: pointer to the device tree blob
  997. * @nodeoffset: offset of the node whose parent to find
  998. *
  999. * fdt_parent_offset() locates the parent node of a given node (that
  1000. * is, it finds the offset of the node which contains the node at
  1001. * nodeoffset as a subnode).
  1002. *
  1003. * NOTE: This function is expensive, as it must scan the device tree
  1004. * structure from the start to nodeoffset, *twice*.
  1005. *
  1006. * returns:
  1007. * structure block offset of the parent of the node at nodeoffset
  1008. * (>=0), on success
  1009. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  1010. * -FDT_ERR_BADMAGIC,
  1011. * -FDT_ERR_BADVERSION,
  1012. * -FDT_ERR_BADSTATE,
  1013. * -FDT_ERR_BADSTRUCTURE, standard meanings
  1014. */
  1015. int fdt_parent_offset(const void *fdt, int nodeoffset);
  1016. /**
  1017. * fdt_node_offset_by_prop_value - find nodes with a given property value
  1018. * @fdt: pointer to the device tree blob
  1019. * @startoffset: only find nodes after this offset
  1020. * @propname: property name to check
  1021. * @propval: property value to search for
  1022. * @proplen: length of the value in propval
  1023. *
  1024. * fdt_node_offset_by_prop_value() returns the offset of the first
  1025. * node after startoffset, which has a property named propname whose
  1026. * value is of length proplen and has value equal to propval; or if
  1027. * startoffset is -1, the very first such node in the tree.
  1028. *
  1029. * To iterate through all nodes matching the criterion, the following
  1030. * idiom can be used:
  1031. * offset = fdt_node_offset_by_prop_value(fdt, -1, propname,
  1032. * propval, proplen);
  1033. * while (offset != -FDT_ERR_NOTFOUND) {
  1034. * // other code here
  1035. * offset = fdt_node_offset_by_prop_value(fdt, offset, propname,
  1036. * propval, proplen);
  1037. * }
  1038. *
  1039. * Note the -1 in the first call to the function, if 0 is used here
  1040. * instead, the function will never locate the root node, even if it
  1041. * matches the criterion.
  1042. *
  1043. * returns:
  1044. * structure block offset of the located node (>= 0, >startoffset),
  1045. * on success
  1046. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  1047. * tree after startoffset
  1048. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  1049. * -FDT_ERR_BADMAGIC,
  1050. * -FDT_ERR_BADVERSION,
  1051. * -FDT_ERR_BADSTATE,
  1052. * -FDT_ERR_BADSTRUCTURE, standard meanings
  1053. */
  1054. int fdt_node_offset_by_prop_value(const void *fdt, int startoffset,
  1055. const char *propname,
  1056. const void *propval, int proplen);
  1057. /**
  1058. * fdt_node_offset_by_phandle - find the node with a given phandle
  1059. * @fdt: pointer to the device tree blob
  1060. * @phandle: phandle value
  1061. *
  1062. * fdt_node_offset_by_phandle() returns the offset of the node
  1063. * which has the given phandle value. If there is more than one node
  1064. * in the tree with the given phandle (an invalid tree), results are
  1065. * undefined.
  1066. *
  1067. * returns:
  1068. * structure block offset of the located node (>= 0), on success
  1069. * -FDT_ERR_NOTFOUND, no node with that phandle exists
  1070. * -FDT_ERR_BADPHANDLE, given phandle value was invalid (0 or -1)
  1071. * -FDT_ERR_BADMAGIC,
  1072. * -FDT_ERR_BADVERSION,
  1073. * -FDT_ERR_BADSTATE,
  1074. * -FDT_ERR_BADSTRUCTURE, standard meanings
  1075. */
  1076. int fdt_node_offset_by_phandle(const void *fdt, uint32_t phandle);
  1077. /**
  1078. * fdt_node_check_compatible - check a node's compatible property
  1079. * @fdt: pointer to the device tree blob
  1080. * @nodeoffset: offset of a tree node
  1081. * @compatible: string to match against
  1082. *
  1083. * fdt_node_check_compatible() returns 0 if the given node contains a
  1084. * @compatible property with the given string as one of its elements,
  1085. * it returns non-zero otherwise, or on error.
  1086. *
  1087. * returns:
  1088. * 0, if the node has a 'compatible' property listing the given string
  1089. * 1, if the node has a 'compatible' property, but it does not list
  1090. * the given string
  1091. * -FDT_ERR_NOTFOUND, if the given node has no 'compatible' property
  1092. * -FDT_ERR_BADOFFSET, if nodeoffset does not refer to a BEGIN_NODE tag
  1093. * -FDT_ERR_BADMAGIC,
  1094. * -FDT_ERR_BADVERSION,
  1095. * -FDT_ERR_BADSTATE,
  1096. * -FDT_ERR_BADSTRUCTURE, standard meanings
  1097. */
  1098. int fdt_node_check_compatible(const void *fdt, int nodeoffset,
  1099. const char *compatible);
  1100. /**
  1101. * fdt_node_offset_by_compatible - find nodes with a given 'compatible' value
  1102. * @fdt: pointer to the device tree blob
  1103. * @startoffset: only find nodes after this offset
  1104. * @compatible: 'compatible' string to match against
  1105. *
  1106. * fdt_node_offset_by_compatible() returns the offset of the first
  1107. * node after startoffset, which has a 'compatible' property which
  1108. * lists the given compatible string; or if startoffset is -1, the
  1109. * very first such node in the tree.
  1110. *
  1111. * To iterate through all nodes matching the criterion, the following
  1112. * idiom can be used:
  1113. * offset = fdt_node_offset_by_compatible(fdt, -1, compatible);
  1114. * while (offset != -FDT_ERR_NOTFOUND) {
  1115. * // other code here
  1116. * offset = fdt_node_offset_by_compatible(fdt, offset, compatible);
  1117. * }
  1118. *
  1119. * Note the -1 in the first call to the function, if 0 is used here
  1120. * instead, the function will never locate the root node, even if it
  1121. * matches the criterion.
  1122. *
  1123. * returns:
  1124. * structure block offset of the located node (>= 0, >startoffset),
  1125. * on success
  1126. * -FDT_ERR_NOTFOUND, no node matching the criterion exists in the
  1127. * tree after startoffset
  1128. * -FDT_ERR_BADOFFSET, nodeoffset does not refer to a BEGIN_NODE tag
  1129. * -FDT_ERR_BADMAGIC,
  1130. * -FDT_ERR_BADVERSION,
  1131. * -FDT_ERR_BADSTATE,
  1132. * -FDT_ERR_BADSTRUCTURE, standard meanings
  1133. */
  1134. int fdt_node_offset_by_compatible(const void *fdt, int startoffset,
  1135. const char *compatible);
  1136. /**
  1137. * fdt_stringlist_contains - check a string list property for a string
  1138. * @strlist: Property containing a list of strings to check
  1139. * @listlen: Length of property
  1140. * @str: String to search for
  1141. *
  1142. * This is a utility function provided for convenience. The list contains
  1143. * one or more strings, each terminated by \0, as is found in a device tree
  1144. * "compatible" property.
  1145. *
  1146. * Return: 1 if the string is found in the list, 0 not found, or invalid list
  1147. */
  1148. int fdt_stringlist_contains(const char *strlist, int listlen, const char *str);
  1149. /**
  1150. * fdt_stringlist_count - count the number of strings in a string list
  1151. * @fdt: pointer to the device tree blob
  1152. * @nodeoffset: offset of a tree node
  1153. * @property: name of the property containing the string list
  1154. *
  1155. * Return:
  1156. * the number of strings in the given property
  1157. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  1158. * -FDT_ERR_NOTFOUND if the property does not exist
  1159. */
  1160. int fdt_stringlist_count(const void *fdt, int nodeoffset, const char *property);
  1161. /**
  1162. * fdt_stringlist_search - find a string in a string list and return its index
  1163. * @fdt: pointer to the device tree blob
  1164. * @nodeoffset: offset of a tree node
  1165. * @property: name of the property containing the string list
  1166. * @string: string to look up in the string list
  1167. *
  1168. * Note that it is possible for this function to succeed on property values
  1169. * that are not NUL-terminated. That's because the function will stop after
  1170. * finding the first occurrence of @string. This can for example happen with
  1171. * small-valued cell properties, such as #address-cells, when searching for
  1172. * the empty string.
  1173. *
  1174. * return:
  1175. * the index of the string in the list of strings
  1176. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  1177. * -FDT_ERR_NOTFOUND if the property does not exist or does not contain
  1178. * the given string
  1179. */
  1180. int fdt_stringlist_search(const void *fdt, int nodeoffset, const char *property,
  1181. const char *string);
  1182. /**
  1183. * fdt_stringlist_get() - obtain the string at a given index in a string list
  1184. * @fdt: pointer to the device tree blob
  1185. * @nodeoffset: offset of a tree node
  1186. * @property: name of the property containing the string list
  1187. * @index: index of the string to return
  1188. * @lenp: return location for the string length or an error code on failure
  1189. *
  1190. * Note that this will successfully extract strings from properties with
  1191. * non-NUL-terminated values. For example on small-valued cell properties
  1192. * this function will return the empty string.
  1193. *
  1194. * If non-NULL, the length of the string (on success) or a negative error-code
  1195. * (on failure) will be stored in the integer pointer to by lenp.
  1196. *
  1197. * Return:
  1198. * A pointer to the string at the given index in the string list or NULL on
  1199. * failure. On success the length of the string will be stored in the memory
  1200. * location pointed to by the lenp parameter, if non-NULL. On failure one of
  1201. * the following negative error codes will be returned in the lenp parameter
  1202. * (if non-NULL):
  1203. * -FDT_ERR_BADVALUE if the property value is not NUL-terminated
  1204. * -FDT_ERR_NOTFOUND if the property does not exist
  1205. */
  1206. const char *fdt_stringlist_get(const void *fdt, int nodeoffset,
  1207. const char *property, int index,
  1208. int *lenp);
  1209. /**********************************************************************/
  1210. /* Read-only functions (addressing related) */
  1211. /**********************************************************************/
  1212. /**
  1213. * FDT_MAX_NCELLS - maximum value for #address-cells and #size-cells
  1214. *
  1215. * This is the maximum value for #address-cells, #size-cells and
  1216. * similar properties that will be processed by libfdt. IEE1275
  1217. * requires that OF implementations handle values up to 4.
  1218. * Implementations may support larger values, but in practice higher
  1219. * values aren't used.
  1220. */
  1221. #define FDT_MAX_NCELLS 4
  1222. /**
  1223. * fdt_address_cells - retrieve address size for a bus represented in the tree
  1224. * @fdt: pointer to the device tree blob
  1225. * @nodeoffset: offset of the node to find the address size for
  1226. *
  1227. * When the node has a valid #address-cells property, returns its value.
  1228. *
  1229. * returns:
  1230. * 0 <= n < FDT_MAX_NCELLS, on success
  1231. * 2, if the node has no #address-cells property
  1232. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1233. * #address-cells property
  1234. * -FDT_ERR_BADMAGIC,
  1235. * -FDT_ERR_BADVERSION,
  1236. * -FDT_ERR_BADSTATE,
  1237. * -FDT_ERR_BADSTRUCTURE,
  1238. * -FDT_ERR_TRUNCATED, standard meanings
  1239. */
  1240. int fdt_address_cells(const void *fdt, int nodeoffset);
  1241. /**
  1242. * fdt_size_cells - retrieve address range size for a bus represented in the
  1243. * tree
  1244. * @fdt: pointer to the device tree blob
  1245. * @nodeoffset: offset of the node to find the address range size for
  1246. *
  1247. * When the node has a valid #size-cells property, returns its value.
  1248. *
  1249. * returns:
  1250. * 0 <= n < FDT_MAX_NCELLS, on success
  1251. * 1, if the node has no #size-cells property
  1252. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  1253. * #size-cells property
  1254. * -FDT_ERR_BADMAGIC,
  1255. * -FDT_ERR_BADVERSION,
  1256. * -FDT_ERR_BADSTATE,
  1257. * -FDT_ERR_BADSTRUCTURE,
  1258. * -FDT_ERR_TRUNCATED, standard meanings
  1259. */
  1260. int fdt_size_cells(const void *fdt, int nodeoffset);
  1261. /**********************************************************************/
  1262. /* Write-in-place functions */
  1263. /**********************************************************************/
  1264. /**
  1265. * fdt_setprop_inplace_namelen_partial - change a property's value,
  1266. * but not its size
  1267. * @fdt: pointer to the device tree blob
  1268. * @nodeoffset: offset of the node whose property to change
  1269. * @name: name of the property to change
  1270. * @namelen: number of characters of name to consider
  1271. * @idx: index of the property to change in the array
  1272. * @val: pointer to data to replace the property value with
  1273. * @len: length of the property value
  1274. *
  1275. * Identical to fdt_setprop_inplace(), but modifies the given property
  1276. * starting from the given index, and using only the first characters
  1277. * of the name. It is useful when you want to manipulate only one value of
  1278. * an array and you have a string that doesn't end with \0.
  1279. *
  1280. * Return: 0 on success, negative libfdt error value otherwise
  1281. */
  1282. #ifndef SWIG /* Not available in Python */
  1283. int fdt_setprop_inplace_namelen_partial(void *fdt, int nodeoffset,
  1284. const char *name, int namelen,
  1285. uint32_t idx, const void *val,
  1286. int len);
  1287. #endif
  1288. /**
  1289. * fdt_setprop_inplace - change a property's value, but not its size
  1290. * @fdt: pointer to the device tree blob
  1291. * @nodeoffset: offset of the node whose property to change
  1292. * @name: name of the property to change
  1293. * @val: pointer to data to replace the property value with
  1294. * @len: length of the property value
  1295. *
  1296. * fdt_setprop_inplace() replaces the value of a given property with
  1297. * the data in val, of length len. This function cannot change the
  1298. * size of a property, and so will only work if len is equal to the
  1299. * current length of the property.
  1300. *
  1301. * This function will alter only the bytes in the blob which contain
  1302. * the given property value, and will not alter or move any other part
  1303. * of the tree.
  1304. *
  1305. * returns:
  1306. * 0, on success
  1307. * -FDT_ERR_NOSPACE, if len is not equal to the property's current length
  1308. * -FDT_ERR_NOTFOUND, node does not have the named property
  1309. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1310. * -FDT_ERR_BADMAGIC,
  1311. * -FDT_ERR_BADVERSION,
  1312. * -FDT_ERR_BADSTATE,
  1313. * -FDT_ERR_BADSTRUCTURE,
  1314. * -FDT_ERR_TRUNCATED, standard meanings
  1315. */
  1316. #ifndef SWIG /* Not available in Python */
  1317. int fdt_setprop_inplace(void *fdt, int nodeoffset, const char *name,
  1318. const void *val, int len);
  1319. #endif
  1320. /**
  1321. * fdt_setprop_inplace_u32 - change the value of a 32-bit integer property
  1322. * @fdt: pointer to the device tree blob
  1323. * @nodeoffset: offset of the node whose property to change
  1324. * @name: name of the property to change
  1325. * @val: 32-bit integer value to replace the property with
  1326. *
  1327. * fdt_setprop_inplace_u32() replaces the value of a given property
  1328. * with the 32-bit integer value in val, converting val to big-endian
  1329. * if necessary. This function cannot change the size of a property,
  1330. * and so will only work if the property already exists and has length
  1331. * 4.
  1332. *
  1333. * This function will alter only the bytes in the blob which contain
  1334. * the given property value, and will not alter or move any other part
  1335. * of the tree.
  1336. *
  1337. * returns:
  1338. * 0, on success
  1339. * -FDT_ERR_NOSPACE, if the property's length is not equal to 4
  1340. * -FDT_ERR_NOTFOUND, node does not have the named property
  1341. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1342. * -FDT_ERR_BADMAGIC,
  1343. * -FDT_ERR_BADVERSION,
  1344. * -FDT_ERR_BADSTATE,
  1345. * -FDT_ERR_BADSTRUCTURE,
  1346. * -FDT_ERR_TRUNCATED, standard meanings
  1347. */
  1348. static inline int fdt_setprop_inplace_u32(void *fdt, int nodeoffset,
  1349. const char *name, uint32_t val)
  1350. {
  1351. fdt32_t tmp = cpu_to_fdt32(val);
  1352. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1353. }
  1354. /**
  1355. * fdt_setprop_inplace_u64 - change the value of a 64-bit integer property
  1356. * @fdt: pointer to the device tree blob
  1357. * @nodeoffset: offset of the node whose property to change
  1358. * @name: name of the property to change
  1359. * @val: 64-bit integer value to replace the property with
  1360. *
  1361. * fdt_setprop_inplace_u64() replaces the value of a given property
  1362. * with the 64-bit integer value in val, converting val to big-endian
  1363. * if necessary. This function cannot change the size of a property,
  1364. * and so will only work if the property already exists and has length
  1365. * 8.
  1366. *
  1367. * This function will alter only the bytes in the blob which contain
  1368. * the given property value, and will not alter or move any other part
  1369. * of the tree.
  1370. *
  1371. * returns:
  1372. * 0, on success
  1373. * -FDT_ERR_NOSPACE, if the property's length is not equal to 8
  1374. * -FDT_ERR_NOTFOUND, node does not have the named property
  1375. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1376. * -FDT_ERR_BADMAGIC,
  1377. * -FDT_ERR_BADVERSION,
  1378. * -FDT_ERR_BADSTATE,
  1379. * -FDT_ERR_BADSTRUCTURE,
  1380. * -FDT_ERR_TRUNCATED, standard meanings
  1381. */
  1382. static inline int fdt_setprop_inplace_u64(void *fdt, int nodeoffset,
  1383. const char *name, uint64_t val)
  1384. {
  1385. fdt64_t tmp = cpu_to_fdt64(val);
  1386. return fdt_setprop_inplace(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1387. }
  1388. /**
  1389. * fdt_setprop_inplace_cell - change the value of a single-cell property
  1390. * @fdt: pointer to the device tree blob
  1391. * @nodeoffset: offset of the node containing the property
  1392. * @name: name of the property to change the value of
  1393. * @val: new value of the 32-bit cell
  1394. *
  1395. * This is an alternative name for fdt_setprop_inplace_u32()
  1396. * Return: 0 on success, negative libfdt error number otherwise.
  1397. */
  1398. static inline int fdt_setprop_inplace_cell(void *fdt, int nodeoffset,
  1399. const char *name, uint32_t val)
  1400. {
  1401. return fdt_setprop_inplace_u32(fdt, nodeoffset, name, val);
  1402. }
  1403. /**
  1404. * fdt_nop_property - replace a property with nop tags
  1405. * @fdt: pointer to the device tree blob
  1406. * @nodeoffset: offset of the node whose property to nop
  1407. * @name: name of the property to nop
  1408. *
  1409. * fdt_nop_property() will replace a given property's representation
  1410. * in the blob with FDT_NOP tags, effectively removing it from the
  1411. * tree.
  1412. *
  1413. * This function will alter only the bytes in the blob which contain
  1414. * the property, and will not alter or move any other part of the
  1415. * tree.
  1416. *
  1417. * returns:
  1418. * 0, on success
  1419. * -FDT_ERR_NOTFOUND, node does not have the named property
  1420. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1421. * -FDT_ERR_BADMAGIC,
  1422. * -FDT_ERR_BADVERSION,
  1423. * -FDT_ERR_BADSTATE,
  1424. * -FDT_ERR_BADSTRUCTURE,
  1425. * -FDT_ERR_TRUNCATED, standard meanings
  1426. */
  1427. int fdt_nop_property(void *fdt, int nodeoffset, const char *name);
  1428. /**
  1429. * fdt_nop_node - replace a node (subtree) with nop tags
  1430. * @fdt: pointer to the device tree blob
  1431. * @nodeoffset: offset of the node to nop
  1432. *
  1433. * fdt_nop_node() will replace a given node's representation in the
  1434. * blob, including all its subnodes, if any, with FDT_NOP tags,
  1435. * effectively removing it from the tree.
  1436. *
  1437. * This function will alter only the bytes in the blob which contain
  1438. * the node and its properties and subnodes, and will not alter or
  1439. * move any other part of the tree.
  1440. *
  1441. * returns:
  1442. * 0, on success
  1443. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1444. * -FDT_ERR_BADMAGIC,
  1445. * -FDT_ERR_BADVERSION,
  1446. * -FDT_ERR_BADSTATE,
  1447. * -FDT_ERR_BADSTRUCTURE,
  1448. * -FDT_ERR_TRUNCATED, standard meanings
  1449. */
  1450. int fdt_nop_node(void *fdt, int nodeoffset);
  1451. /**********************************************************************/
  1452. /* Sequential write functions */
  1453. /**********************************************************************/
  1454. /* fdt_create_with_flags flags */
  1455. #define FDT_CREATE_FLAG_NO_NAME_DEDUP 0x1
  1456. /* FDT_CREATE_FLAG_NO_NAME_DEDUP: Do not try to de-duplicate property
  1457. * names in the fdt. This can result in faster creation times, but
  1458. * a larger fdt. */
  1459. #define FDT_CREATE_FLAGS_ALL (FDT_CREATE_FLAG_NO_NAME_DEDUP)
  1460. /**
  1461. * fdt_create_with_flags - begin creation of a new fdt
  1462. * @buf: pointer to memory allocated where fdt will be created
  1463. * @bufsize: size of the memory space at fdt
  1464. * @flags: a valid combination of FDT_CREATE_FLAG_ flags, or 0.
  1465. *
  1466. * fdt_create_with_flags() begins the process of creating a new fdt with
  1467. * the sequential write interface.
  1468. *
  1469. * fdt creation process must end with fdt_finish() to produce a valid fdt.
  1470. *
  1471. * returns:
  1472. * 0, on success
  1473. * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
  1474. * -FDT_ERR_BADFLAGS, flags is not valid
  1475. */
  1476. int fdt_create_with_flags(void *buf, int bufsize, uint32_t flags);
  1477. /**
  1478. * fdt_create - begin creation of a new fdt
  1479. * @buf: pointer to memory allocated where fdt will be created
  1480. * @bufsize: size of the memory space at fdt
  1481. *
  1482. * fdt_create() is equivalent to fdt_create_with_flags() with flags=0.
  1483. *
  1484. * returns:
  1485. * 0, on success
  1486. * -FDT_ERR_NOSPACE, bufsize is insufficient for a minimal fdt
  1487. */
  1488. int fdt_create(void *buf, int bufsize);
  1489. /**
  1490. * fdt_resize - move and resize a device tree in sequential write state
  1491. * @fdt: Pointer to the device tree to resize
  1492. * @buf: Buffer where resized tree should be placed
  1493. * @bufsize: Size of the buffer at @buf
  1494. *
  1495. * fdt_resize() moves the device tree blob from @fdt to @buf and
  1496. * resizes it to fit in the new buffer size.
  1497. *
  1498. * returns:
  1499. * 0, on success
  1500. * -FDT_ERR_NOSPACE, if @bufsize is too small
  1501. * -FDT_ERR_BADMAGIC,
  1502. * -FDT_ERR_BADVERSION,
  1503. * -FDT_ERR_BADSTATE, standard meanings
  1504. */
  1505. int fdt_resize(void *fdt, void *buf, int bufsize);
  1506. /**
  1507. * fdt_add_reservemap_entry - add an entry to the memory reserve map
  1508. * @fdt: Pointer to the device tree blob
  1509. * @addr: Start address of the reserve map entry
  1510. * @size: Size of the reserved region
  1511. *
  1512. * fdt_add_reservemap_entry() adds a memory reserve map entry to the
  1513. * device tree blob during the sequential write process. This function
  1514. * can only be called after fdt_create() and before fdt_finish_reservemap().
  1515. *
  1516. * returns:
  1517. * 0, on success
  1518. * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
  1519. * -FDT_ERR_BADSTATE, if not in the correct sequential write state
  1520. */
  1521. int fdt_add_reservemap_entry(void *fdt, uint64_t addr, uint64_t size);
  1522. /**
  1523. * fdt_finish_reservemap - complete the memory reserve map
  1524. * @fdt: Pointer to the device tree blob
  1525. *
  1526. * fdt_finish_reservemap() completes the memory reserve map section
  1527. * of the device tree blob during sequential write. After calling this
  1528. * function, no more reserve map entries can be added and the blob
  1529. * moves to the structure creation phase.
  1530. *
  1531. * returns:
  1532. * 0, on success
  1533. * -FDT_ERR_BADSTATE, if not in the correct sequential write state
  1534. */
  1535. int fdt_finish_reservemap(void *fdt);
  1536. /**
  1537. * fdt_begin_node - start creation of a new node
  1538. * @fdt: Pointer to the device tree blob
  1539. * @name: Name of the node to create
  1540. *
  1541. * fdt_begin_node() starts the creation of a new node with the given
  1542. * @name during sequential write. After calling this function, properties
  1543. * can be added with fdt_property() and subnodes can be created with
  1544. * additional fdt_begin_node() calls. The node must be completed with
  1545. * fdt_end_node().
  1546. *
  1547. * returns:
  1548. * 0, on success
  1549. * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
  1550. * -FDT_ERR_BADSTATE, if not in the correct sequential write state
  1551. */
  1552. int fdt_begin_node(void *fdt, const char *name);
  1553. /**
  1554. * fdt_property - add a property to the current node
  1555. * @fdt: Pointer to the device tree blob
  1556. * @name: Name of the property to add
  1557. * @val: Pointer to the property value
  1558. * @len: Length of the property value in bytes
  1559. *
  1560. * fdt_property() adds a property with the given @name and value to
  1561. * the current node during sequential write. This function can only
  1562. * be called between fdt_begin_node() and fdt_end_node().
  1563. *
  1564. * returns:
  1565. * 0, on success
  1566. * -FDT_ERR_NOSPACE, if there is insufficient space in the blob
  1567. * -FDT_ERR_BADSTATE, if not currently within a node
  1568. */
  1569. int fdt_property(void *fdt, const char *name, const void *val, int len);
  1570. static inline int fdt_property_u32(void *fdt, const char *name, uint32_t val)
  1571. {
  1572. fdt32_t tmp = cpu_to_fdt32(val);
  1573. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1574. }
  1575. static inline int fdt_property_u64(void *fdt, const char *name, uint64_t val)
  1576. {
  1577. fdt64_t tmp = cpu_to_fdt64(val);
  1578. return fdt_property(fdt, name, &tmp, sizeof(tmp));
  1579. }
  1580. #ifndef SWIG /* Not available in Python */
  1581. static inline int fdt_property_cell(void *fdt, const char *name, uint32_t val)
  1582. {
  1583. return fdt_property_u32(fdt, name, val);
  1584. }
  1585. #endif
  1586. /**
  1587. * fdt_property_placeholder - add a new property and return a ptr to its value
  1588. *
  1589. * @fdt: pointer to the device tree blob
  1590. * @name: name of property to add
  1591. * @len: length of property value in bytes
  1592. * @valp: returns a pointer to where the value should be placed
  1593. *
  1594. * returns:
  1595. * 0, on success
  1596. * -FDT_ERR_BADMAGIC,
  1597. * -FDT_ERR_NOSPACE, standard meanings
  1598. */
  1599. int fdt_property_placeholder(void *fdt, const char *name, int len, void **valp);
  1600. #define fdt_property_string(fdt, name, str) \
  1601. fdt_property(fdt, name, str, strlen(str)+1)
  1602. /**
  1603. * fdt_end_node - complete the current node
  1604. * @fdt: Pointer to the device tree blob
  1605. *
  1606. * fdt_end_node() completes the current node during sequential write. This
  1607. * function must be called to close each node started with
  1608. * fdt_begin_node(). After calling this function, no more properties or subnodes
  1609. * can be added to the node.
  1610. *
  1611. * returns:
  1612. * 0, on success
  1613. * -FDT_ERR_BADSTATE, if not currently within a node
  1614. */
  1615. int fdt_end_node(void *fdt);
  1616. /**
  1617. * fdt_finish - complete device tree creation
  1618. * @fdt: Pointer to the device tree blob
  1619. *
  1620. * fdt_finish() completes the device tree creation process started with
  1621. * fdt_create(). This function finalizes the device tree blob and makes it ready
  1622. * for use. After calling this function, the blob is complete and can be used
  1623. * with libfdt read-only and read-write functions, but not with sequential write
  1624. * functions.
  1625. *
  1626. * returns:
  1627. * 0, on success
  1628. * -FDT_ERR_BADSTATE, if the sequential write process is incomplete
  1629. */
  1630. int fdt_finish(void *fdt);
  1631. /**********************************************************************/
  1632. /* Read-write functions */
  1633. /**********************************************************************/
  1634. /**
  1635. * fdt_create_empty_tree - create an empty device tree
  1636. * @buf: Buffer where the empty tree should be created
  1637. * @bufsize: Size of the buffer at @buf
  1638. *
  1639. * fdt_create_empty_tree() creates a minimal empty device tree blob
  1640. * in the given buffer. The tree contains only a root node with no
  1641. * properties or subnodes.
  1642. *
  1643. * returns:
  1644. * 0, on success
  1645. * -FDT_ERR_NOSPACE, if @bufsize is too small for even an empty tree
  1646. */
  1647. int fdt_create_empty_tree(void *buf, int bufsize);
  1648. /**
  1649. * fdt_open_into - move a device tree into a new buffer and make editable
  1650. * @fdt: Pointer to the device tree to move
  1651. * @buf: Buffer where the editable tree should be placed
  1652. * @bufsize: Size of the buffer at @buf
  1653. *
  1654. * fdt_open_into() moves and reorganizes the device tree blob from @fdt
  1655. * into @buf, converting it to a format suitable for read-write operations.
  1656. * The new buffer should allow space for modifications.
  1657. *
  1658. * returns:
  1659. * 0, on success
  1660. * -FDT_ERR_NOSPACE, if @bufsize is too small
  1661. * -FDT_ERR_BADMAGIC,
  1662. * -FDT_ERR_BADVERSION,
  1663. * -FDT_ERR_BADSTATE,
  1664. * -FDT_ERR_BADSTRUCTURE,
  1665. * -FDT_ERR_TRUNCATED, standard meanings
  1666. */
  1667. int fdt_open_into(const void *fdt, void *buf, int bufsize);
  1668. /**
  1669. * fdt_pack - pack a device tree blob
  1670. * @fdt: Pointer to the device tree blob
  1671. *
  1672. * fdt_pack() reorganizes the device tree blob to eliminate any free space
  1673. * and pack it into the minimum possible size. This is useful after making
  1674. * modifications that might have left gaps in the blob.
  1675. *
  1676. * returns:
  1677. * 0, on success
  1678. * -FDT_ERR_BADMAGIC,
  1679. * -FDT_ERR_BADVERSION,
  1680. * -FDT_ERR_BADSTATE,
  1681. * -FDT_ERR_BADSTRUCTURE,
  1682. * -FDT_ERR_BADLAYOUT, standard meanings
  1683. */
  1684. int fdt_pack(void *fdt);
  1685. /**
  1686. * fdt_add_mem_rsv - add one memory reserve map entry
  1687. * @fdt: pointer to the device tree blob
  1688. * @address: 64-bit start address of the reserve map entry
  1689. * @size: 64-bit size of the reserved region
  1690. *
  1691. * Adds a reserve map entry to the given blob reserving a region at
  1692. * address address of length size.
  1693. *
  1694. * This function will insert data into the reserve map and will
  1695. * therefore change the indexes of some entries in the table.
  1696. *
  1697. * returns:
  1698. * 0, on success
  1699. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1700. * contain the new reservation entry
  1701. * -FDT_ERR_BADMAGIC,
  1702. * -FDT_ERR_BADVERSION,
  1703. * -FDT_ERR_BADSTATE,
  1704. * -FDT_ERR_BADSTRUCTURE,
  1705. * -FDT_ERR_BADLAYOUT,
  1706. * -FDT_ERR_TRUNCATED, standard meanings
  1707. */
  1708. int fdt_add_mem_rsv(void *fdt, uint64_t address, uint64_t size);
  1709. /**
  1710. * fdt_del_mem_rsv - remove a memory reserve map entry
  1711. * @fdt: pointer to the device tree blob
  1712. * @n: entry to remove
  1713. *
  1714. * fdt_del_mem_rsv() removes the n-th memory reserve map entry from
  1715. * the blob.
  1716. *
  1717. * This function will delete data from the reservation table and will
  1718. * therefore change the indexes of some entries in the table.
  1719. *
  1720. * returns:
  1721. * 0, on success
  1722. * -FDT_ERR_NOTFOUND, there is no entry of the given index (i.e. there
  1723. * are less than n+1 reserve map entries)
  1724. * -FDT_ERR_BADMAGIC,
  1725. * -FDT_ERR_BADVERSION,
  1726. * -FDT_ERR_BADSTATE,
  1727. * -FDT_ERR_BADSTRUCTURE,
  1728. * -FDT_ERR_BADLAYOUT,
  1729. * -FDT_ERR_TRUNCATED, standard meanings
  1730. */
  1731. int fdt_del_mem_rsv(void *fdt, int n);
  1732. /**
  1733. * fdt_set_name - change the name of a given node
  1734. * @fdt: pointer to the device tree blob
  1735. * @nodeoffset: structure block offset of a node
  1736. * @name: name to give the node
  1737. *
  1738. * fdt_set_name() replaces the name (including unit address, if any)
  1739. * of the given node with the given string. NOTE: this function can't
  1740. * efficiently check if the new name is unique amongst the given
  1741. * node's siblings; results are undefined if this function is invoked
  1742. * with a name equal to one of the given node's siblings.
  1743. *
  1744. * This function may insert or delete data from the blob, and will
  1745. * therefore change the offsets of some existing nodes.
  1746. *
  1747. * returns:
  1748. * 0, on success
  1749. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob
  1750. * to contain the new name
  1751. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1752. * -FDT_ERR_BADMAGIC,
  1753. * -FDT_ERR_BADVERSION,
  1754. * -FDT_ERR_BADSTATE, standard meanings
  1755. */
  1756. int fdt_set_name(void *fdt, int nodeoffset, const char *name);
  1757. /**
  1758. * fdt_setprop_namelen - create or change a property
  1759. * @fdt: pointer to the device tree blob
  1760. * @nodeoffset: offset of the node whose property to change
  1761. * @name: name of the property to change
  1762. * @namelen: length of the name
  1763. * @val: pointer to data to set the property value to
  1764. * @len: length of the property value
  1765. *
  1766. * fdt_setprop_namelen() sets the value of the named property in the given
  1767. * node to the given value and length, creating the property if it
  1768. * does not already exist.
  1769. *
  1770. * This function may insert or delete data from the blob, and will
  1771. * therefore change the offsets of some existing nodes.
  1772. *
  1773. * returns:
  1774. * 0, on success
  1775. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1776. * contain the new property value
  1777. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1778. * -FDT_ERR_BADLAYOUT,
  1779. * -FDT_ERR_BADMAGIC,
  1780. * -FDT_ERR_BADVERSION,
  1781. * -FDT_ERR_BADSTATE,
  1782. * -FDT_ERR_BADSTRUCTURE,
  1783. * -FDT_ERR_BADLAYOUT,
  1784. * -FDT_ERR_TRUNCATED, standard meanings
  1785. */
  1786. int fdt_setprop_namelen(void *fdt, int nodeoffset, const char *name,
  1787. int namelen, const void *val, int len);
  1788. /**
  1789. * fdt_setprop - create or change a property
  1790. * @fdt: pointer to the device tree blob
  1791. * @nodeoffset: offset of the node whose property to change
  1792. * @name: name of the property to change
  1793. * @val: pointer to data to set the property value to
  1794. * @len: length of the property value
  1795. *
  1796. * fdt_setprop() sets the value of the named property in the given
  1797. * node to the given value and length, creating the property if it
  1798. * does not already exist.
  1799. *
  1800. * This function may insert or delete data from the blob, and will
  1801. * therefore change the offsets of some existing nodes.
  1802. *
  1803. * returns:
  1804. * 0, on success
  1805. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1806. * contain the new property value
  1807. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1808. * -FDT_ERR_BADLAYOUT,
  1809. * -FDT_ERR_BADMAGIC,
  1810. * -FDT_ERR_BADVERSION,
  1811. * -FDT_ERR_BADSTATE,
  1812. * -FDT_ERR_BADSTRUCTURE,
  1813. * -FDT_ERR_BADLAYOUT,
  1814. * -FDT_ERR_TRUNCATED, standard meanings
  1815. */
  1816. static inline int fdt_setprop(void *fdt, int nodeoffset, const char *name,
  1817. const void *val, int len)
  1818. {
  1819. return fdt_setprop_namelen(fdt, nodeoffset, name, strlen(name), val,
  1820. len);
  1821. }
  1822. /**
  1823. * fdt_setprop_placeholder_namelen - allocate space for a property
  1824. * @fdt: pointer to the device tree blob
  1825. * @nodeoffset: offset of the node whose property to change
  1826. * @name: name of the property to change
  1827. * @namelen: length of the name
  1828. * @len: length of the property value
  1829. * @prop_data: return pointer to property data
  1830. *
  1831. * fdt_setprop_placeholder_namelen() allocates the named property in the given node.
  1832. * If the property exists it is resized. In either case a pointer to the
  1833. * property data is returned.
  1834. *
  1835. * This function may insert or delete data from the blob, and will
  1836. * therefore change the offsets of some existing nodes.
  1837. *
  1838. * returns:
  1839. * 0, on success
  1840. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1841. * contain the new property value
  1842. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1843. * -FDT_ERR_BADLAYOUT,
  1844. * -FDT_ERR_BADMAGIC,
  1845. * -FDT_ERR_BADVERSION,
  1846. * -FDT_ERR_BADSTATE,
  1847. * -FDT_ERR_BADSTRUCTURE,
  1848. * -FDT_ERR_BADLAYOUT,
  1849. * -FDT_ERR_TRUNCATED, standard meanings
  1850. */
  1851. int fdt_setprop_placeholder_namelen(void *fdt, int nodeoffset, const char *name,
  1852. int namelen, int len, void **prop_data);
  1853. /**
  1854. * fdt_setprop_placeholder - allocate space for a property
  1855. * @fdt: pointer to the device tree blob
  1856. * @nodeoffset: offset of the node whose property to change
  1857. * @name: name of the property to change
  1858. * @len: length of the property value
  1859. * @prop_data: return pointer to property data
  1860. *
  1861. * fdt_setprop_placeholder() allocates the named property in the given node.
  1862. * If the property exists it is resized. In either case a pointer to the
  1863. * property data is returned.
  1864. *
  1865. * This function may insert or delete data from the blob, and will
  1866. * therefore change the offsets of some existing nodes.
  1867. *
  1868. * returns:
  1869. * 0, on success
  1870. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1871. * contain the new property value
  1872. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1873. * -FDT_ERR_BADLAYOUT,
  1874. * -FDT_ERR_BADMAGIC,
  1875. * -FDT_ERR_BADVERSION,
  1876. * -FDT_ERR_BADSTATE,
  1877. * -FDT_ERR_BADSTRUCTURE,
  1878. * -FDT_ERR_BADLAYOUT,
  1879. * -FDT_ERR_TRUNCATED, standard meanings
  1880. */
  1881. static inline int fdt_setprop_placeholder(void *fdt, int nodeoffset,
  1882. const char *name, int len,
  1883. void **prop_data)
  1884. {
  1885. return fdt_setprop_placeholder_namelen(fdt, nodeoffset, name,
  1886. strlen(name), len, prop_data);
  1887. }
  1888. /**
  1889. * fdt_setprop_u32 - set a property to a 32-bit integer
  1890. * @fdt: pointer to the device tree blob
  1891. * @nodeoffset: offset of the node whose property to change
  1892. * @name: name of the property to change
  1893. * @val: 32-bit integer value for the property (native endian)
  1894. *
  1895. * fdt_setprop_u32() sets the value of the named property in the given
  1896. * node to the given 32-bit integer value (converting to big-endian if
  1897. * necessary), or creates a new property with that value if it does
  1898. * not already exist.
  1899. *
  1900. * This function may insert or delete data from the blob, and will
  1901. * therefore change the offsets of some existing nodes.
  1902. *
  1903. * returns:
  1904. * 0, on success
  1905. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1906. * contain the new property value
  1907. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1908. * -FDT_ERR_BADLAYOUT,
  1909. * -FDT_ERR_BADMAGIC,
  1910. * -FDT_ERR_BADVERSION,
  1911. * -FDT_ERR_BADSTATE,
  1912. * -FDT_ERR_BADSTRUCTURE,
  1913. * -FDT_ERR_BADLAYOUT,
  1914. * -FDT_ERR_TRUNCATED, standard meanings
  1915. */
  1916. static inline int fdt_setprop_u32(void *fdt, int nodeoffset, const char *name,
  1917. uint32_t val)
  1918. {
  1919. fdt32_t tmp = cpu_to_fdt32(val);
  1920. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1921. }
  1922. /**
  1923. * fdt_setprop_u64 - set a property to a 64-bit integer
  1924. * @fdt: pointer to the device tree blob
  1925. * @nodeoffset: offset of the node whose property to change
  1926. * @name: name of the property to change
  1927. * @val: 64-bit integer value for the property (native endian)
  1928. *
  1929. * fdt_setprop_u64() sets the value of the named property in the given
  1930. * node to the given 64-bit integer value (converting to big-endian if
  1931. * necessary), or creates a new property with that value if it does
  1932. * not already exist.
  1933. *
  1934. * This function may insert or delete data from the blob, and will
  1935. * therefore change the offsets of some existing nodes.
  1936. *
  1937. * returns:
  1938. * 0, on success
  1939. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1940. * contain the new property value
  1941. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1942. * -FDT_ERR_BADLAYOUT,
  1943. * -FDT_ERR_BADMAGIC,
  1944. * -FDT_ERR_BADVERSION,
  1945. * -FDT_ERR_BADSTATE,
  1946. * -FDT_ERR_BADSTRUCTURE,
  1947. * -FDT_ERR_BADLAYOUT,
  1948. * -FDT_ERR_TRUNCATED, standard meanings
  1949. */
  1950. static inline int fdt_setprop_u64(void *fdt, int nodeoffset, const char *name,
  1951. uint64_t val)
  1952. {
  1953. fdt64_t tmp = cpu_to_fdt64(val);
  1954. return fdt_setprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  1955. }
  1956. /**
  1957. * fdt_setprop_cell - set a property to a single cell value
  1958. * @fdt: pointer to the device tree blob
  1959. * @nodeoffset: offset of the node whose property to change
  1960. * @name: name of the property to change
  1961. * @val: 32-bit integer value for the property (native endian)
  1962. *
  1963. * This is an alternative name for fdt_setprop_u32()
  1964. *
  1965. * Return: 0 on success, negative libfdt error value otherwise.
  1966. */
  1967. static inline int fdt_setprop_cell(void *fdt, int nodeoffset, const char *name,
  1968. uint32_t val)
  1969. {
  1970. return fdt_setprop_u32(fdt, nodeoffset, name, val);
  1971. }
  1972. /**
  1973. * fdt_setprop_string - set a property to a string value
  1974. * @fdt: pointer to the device tree blob
  1975. * @nodeoffset: offset of the node whose property to change
  1976. * @name: name of the property to change
  1977. * @str: string value for the property
  1978. *
  1979. * fdt_setprop_string() sets the value of the named property in the
  1980. * given node to the given string value (using the length of the
  1981. * string to determine the new length of the property), or creates a
  1982. * new property with that value if it does not already exist.
  1983. *
  1984. * This function may insert or delete data from the blob, and will
  1985. * therefore change the offsets of some existing nodes.
  1986. *
  1987. * returns:
  1988. * 0, on success
  1989. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  1990. * contain the new property value
  1991. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  1992. * -FDT_ERR_BADLAYOUT,
  1993. * -FDT_ERR_BADMAGIC,
  1994. * -FDT_ERR_BADVERSION,
  1995. * -FDT_ERR_BADSTATE,
  1996. * -FDT_ERR_BADSTRUCTURE,
  1997. * -FDT_ERR_BADLAYOUT,
  1998. * -FDT_ERR_TRUNCATED, standard meanings
  1999. */
  2000. #define fdt_setprop_string(fdt, nodeoffset, name, str) \
  2001. fdt_setprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  2002. /**
  2003. * fdt_setprop_namelen_string - set a property to a string value
  2004. * @fdt: pointer to the device tree blob
  2005. * @nodeoffset: offset of the node whose property to change
  2006. * @name: name of the property to change
  2007. * @namelen: number of characters of name to consider
  2008. * @str: string value for the property
  2009. *
  2010. * fdt_setprop_namelen_string() sets the value of the named property in the
  2011. * given node to the given string value (using the length of the
  2012. * string to determine the new length of the property), or creates a
  2013. * new property with that value if it does not already exist.
  2014. *
  2015. * This function may insert or delete data from the blob, and will
  2016. * therefore change the offsets of some existing nodes.
  2017. *
  2018. * returns:
  2019. * 0, on success
  2020. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2021. * contain the new property value
  2022. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2023. * -FDT_ERR_BADLAYOUT,
  2024. * -FDT_ERR_BADMAGIC,
  2025. * -FDT_ERR_BADVERSION,
  2026. * -FDT_ERR_BADSTATE,
  2027. * -FDT_ERR_BADSTRUCTURE,
  2028. * -FDT_ERR_BADLAYOUT,
  2029. * -FDT_ERR_TRUNCATED, standard meanings
  2030. */
  2031. #define fdt_setprop_namelen_string(fdt, nodeoffset, name, namelen, str) \
  2032. fdt_setprop_namelen((fdt), (nodeoffset), (name), (namelen), (str), \
  2033. strlen(str) + 1)
  2034. /**
  2035. * fdt_setprop_empty - set a property to an empty value
  2036. * @fdt: pointer to the device tree blob
  2037. * @nodeoffset: offset of the node whose property to change
  2038. * @name: name of the property to change
  2039. *
  2040. * fdt_setprop_empty() sets the value of the named property in the
  2041. * given node to an empty (zero length) value, or creates a new empty
  2042. * property if it does not already exist.
  2043. *
  2044. * This function may insert or delete data from the blob, and will
  2045. * therefore change the offsets of some existing nodes.
  2046. *
  2047. * returns:
  2048. * 0, on success
  2049. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2050. * contain the new property value
  2051. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2052. * -FDT_ERR_BADLAYOUT,
  2053. * -FDT_ERR_BADMAGIC,
  2054. * -FDT_ERR_BADVERSION,
  2055. * -FDT_ERR_BADSTATE,
  2056. * -FDT_ERR_BADSTRUCTURE,
  2057. * -FDT_ERR_BADLAYOUT,
  2058. * -FDT_ERR_TRUNCATED, standard meanings
  2059. */
  2060. #define fdt_setprop_empty(fdt, nodeoffset, name) \
  2061. fdt_setprop((fdt), (nodeoffset), (name), NULL, 0)
  2062. /**
  2063. * fdt_appendprop - append to or create a property
  2064. * @fdt: pointer to the device tree blob
  2065. * @nodeoffset: offset of the node whose property to change
  2066. * @name: name of the property to append to
  2067. * @val: pointer to data to append to the property value
  2068. * @len: length of the data to append to the property value
  2069. *
  2070. * fdt_appendprop() appends the value to the named property in the
  2071. * given node, creating the property if it does not already exist.
  2072. *
  2073. * This function may insert data into the blob, and will therefore
  2074. * change the offsets of some existing nodes.
  2075. *
  2076. * returns:
  2077. * 0, on success
  2078. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2079. * contain the new property value
  2080. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2081. * -FDT_ERR_BADLAYOUT,
  2082. * -FDT_ERR_BADMAGIC,
  2083. * -FDT_ERR_BADVERSION,
  2084. * -FDT_ERR_BADSTATE,
  2085. * -FDT_ERR_BADSTRUCTURE,
  2086. * -FDT_ERR_BADLAYOUT,
  2087. * -FDT_ERR_TRUNCATED, standard meanings
  2088. */
  2089. int fdt_appendprop(void *fdt, int nodeoffset, const char *name,
  2090. const void *val, int len);
  2091. /**
  2092. * fdt_appendprop_u32 - append a 32-bit integer value to a property
  2093. * @fdt: pointer to the device tree blob
  2094. * @nodeoffset: offset of the node whose property to change
  2095. * @name: name of the property to change
  2096. * @val: 32-bit integer value to append to the property (native endian)
  2097. *
  2098. * fdt_appendprop_u32() appends the given 32-bit integer value
  2099. * (converting to big-endian if necessary) to the value of the named
  2100. * property in the given node, or creates a new property with that
  2101. * value if it does not already exist.
  2102. *
  2103. * This function may insert data into the blob, and will therefore
  2104. * change the offsets of some existing nodes.
  2105. *
  2106. * returns:
  2107. * 0, on success
  2108. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2109. * contain the new property value
  2110. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2111. * -FDT_ERR_BADLAYOUT,
  2112. * -FDT_ERR_BADMAGIC,
  2113. * -FDT_ERR_BADVERSION,
  2114. * -FDT_ERR_BADSTATE,
  2115. * -FDT_ERR_BADSTRUCTURE,
  2116. * -FDT_ERR_BADLAYOUT,
  2117. * -FDT_ERR_TRUNCATED, standard meanings
  2118. */
  2119. static inline int fdt_appendprop_u32(void *fdt, int nodeoffset,
  2120. const char *name, uint32_t val)
  2121. {
  2122. fdt32_t tmp = cpu_to_fdt32(val);
  2123. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  2124. }
  2125. /**
  2126. * fdt_appendprop_u64 - append a 64-bit integer value to a property
  2127. * @fdt: pointer to the device tree blob
  2128. * @nodeoffset: offset of the node whose property to change
  2129. * @name: name of the property to change
  2130. * @val: 64-bit integer value to append to the property (native endian)
  2131. *
  2132. * fdt_appendprop_u64() appends the given 64-bit integer value
  2133. * (converting to big-endian if necessary) to the value of the named
  2134. * property in the given node, or creates a new property with that
  2135. * value if it does not already exist.
  2136. *
  2137. * This function may insert data into the blob, and will therefore
  2138. * change the offsets of some existing nodes.
  2139. *
  2140. * returns:
  2141. * 0, on success
  2142. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2143. * contain the new property value
  2144. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2145. * -FDT_ERR_BADLAYOUT,
  2146. * -FDT_ERR_BADMAGIC,
  2147. * -FDT_ERR_BADVERSION,
  2148. * -FDT_ERR_BADSTATE,
  2149. * -FDT_ERR_BADSTRUCTURE,
  2150. * -FDT_ERR_BADLAYOUT,
  2151. * -FDT_ERR_TRUNCATED, standard meanings
  2152. */
  2153. static inline int fdt_appendprop_u64(void *fdt, int nodeoffset,
  2154. const char *name, uint64_t val)
  2155. {
  2156. fdt64_t tmp = cpu_to_fdt64(val);
  2157. return fdt_appendprop(fdt, nodeoffset, name, &tmp, sizeof(tmp));
  2158. }
  2159. /**
  2160. * fdt_appendprop_cell - append a single cell value to a property
  2161. * @fdt: pointer to the device tree blob
  2162. * @nodeoffset: offset of the node whose property to change
  2163. * @name: name of the property to change
  2164. * @val: 32-bit integer value to append to the property (native endian)
  2165. *
  2166. * This is an alternative name for fdt_appendprop_u32()
  2167. *
  2168. * Return: 0 on success, negative libfdt error value otherwise.
  2169. */
  2170. static inline int fdt_appendprop_cell(void *fdt, int nodeoffset,
  2171. const char *name, uint32_t val)
  2172. {
  2173. return fdt_appendprop_u32(fdt, nodeoffset, name, val);
  2174. }
  2175. /**
  2176. * fdt_appendprop_string - append a string to a property
  2177. * @fdt: pointer to the device tree blob
  2178. * @nodeoffset: offset of the node whose property to change
  2179. * @name: name of the property to change
  2180. * @str: string value to append to the property
  2181. *
  2182. * fdt_appendprop_string() appends the given string to the value of
  2183. * the named property in the given node, or creates a new property
  2184. * with that value if it does not already exist.
  2185. *
  2186. * This function may insert data into the blob, and will therefore
  2187. * change the offsets of some existing nodes.
  2188. *
  2189. * returns:
  2190. * 0, on success
  2191. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2192. * contain the new property value
  2193. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2194. * -FDT_ERR_BADLAYOUT,
  2195. * -FDT_ERR_BADMAGIC,
  2196. * -FDT_ERR_BADVERSION,
  2197. * -FDT_ERR_BADSTATE,
  2198. * -FDT_ERR_BADSTRUCTURE,
  2199. * -FDT_ERR_BADLAYOUT,
  2200. * -FDT_ERR_TRUNCATED, standard meanings
  2201. */
  2202. #define fdt_appendprop_string(fdt, nodeoffset, name, str) \
  2203. fdt_appendprop((fdt), (nodeoffset), (name), (str), strlen(str)+1)
  2204. /**
  2205. * fdt_appendprop_addrrange - append a address range property
  2206. * @fdt: pointer to the device tree blob
  2207. * @parent: offset of the parent node
  2208. * @nodeoffset: offset of the node to add a property at
  2209. * @name: name of property
  2210. * @addr: start address of a given range
  2211. * @size: size of a given range
  2212. *
  2213. * fdt_appendprop_addrrange() appends an address range value (start
  2214. * address and size) to the value of the named property in the given
  2215. * node, or creates a new property with that value if it does not
  2216. * already exist.
  2217. *
  2218. * Cell sizes are determined by parent's #address-cells and #size-cells.
  2219. *
  2220. * This function may insert data into the blob, and will therefore
  2221. * change the offsets of some existing nodes.
  2222. *
  2223. * returns:
  2224. * 0, on success
  2225. * -FDT_ERR_BADLAYOUT,
  2226. * -FDT_ERR_BADMAGIC,
  2227. * -FDT_ERR_BADNCELLS, if the node has a badly formatted or invalid
  2228. * #address-cells property
  2229. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2230. * -FDT_ERR_BADSTATE,
  2231. * -FDT_ERR_BADSTRUCTURE,
  2232. * -FDT_ERR_BADVERSION,
  2233. * -FDT_ERR_BADVALUE, addr or size doesn't fit to respective cells size
  2234. * -FDT_ERR_NOSPACE, there is insufficient free space in the blob to
  2235. * contain a new property
  2236. * -FDT_ERR_TRUNCATED, standard meanings
  2237. */
  2238. int fdt_appendprop_addrrange(void *fdt, int parent, int nodeoffset,
  2239. const char *name, uint64_t addr, uint64_t size);
  2240. /**
  2241. * fdt_delprop - delete a property
  2242. * @fdt: pointer to the device tree blob
  2243. * @nodeoffset: offset of the node whose property to nop
  2244. * @name: name of the property to nop
  2245. *
  2246. * fdt_delprop() will delete the given property.
  2247. *
  2248. * This function will delete data from the blob, and will therefore
  2249. * change the offsets of some existing nodes.
  2250. *
  2251. * returns:
  2252. * 0, on success
  2253. * -FDT_ERR_NOTFOUND, node does not have the named property
  2254. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2255. * -FDT_ERR_BADLAYOUT,
  2256. * -FDT_ERR_BADMAGIC,
  2257. * -FDT_ERR_BADVERSION,
  2258. * -FDT_ERR_BADSTATE,
  2259. * -FDT_ERR_BADSTRUCTURE,
  2260. * -FDT_ERR_TRUNCATED, standard meanings
  2261. */
  2262. int fdt_delprop(void *fdt, int nodeoffset, const char *name);
  2263. /**
  2264. * fdt_add_subnode_namelen - creates a new node based on substring
  2265. * @fdt: pointer to the device tree blob
  2266. * @parentoffset: structure block offset of a node
  2267. * @name: name of the subnode to create
  2268. * @namelen: number of characters of name to consider
  2269. *
  2270. * Identical to fdt_add_subnode(), but use only the first @namelen
  2271. * characters of @name as the name of the new node. This is useful for
  2272. * creating subnodes based on a portion of a larger string, such as a
  2273. * full path.
  2274. *
  2275. * Return: structure block offset of the created subnode (>=0),
  2276. * negative libfdt error value otherwise
  2277. */
  2278. #ifndef SWIG /* Not available in Python */
  2279. int fdt_add_subnode_namelen(void *fdt, int parentoffset,
  2280. const char *name, int namelen);
  2281. #endif
  2282. /**
  2283. * fdt_add_subnode - creates a new node
  2284. * @fdt: pointer to the device tree blob
  2285. * @parentoffset: structure block offset of a node
  2286. * @name: name of the subnode to locate
  2287. *
  2288. * fdt_add_subnode() creates a new node as a subnode of the node at
  2289. * structure block offset parentoffset, with the given name (which
  2290. * should include the unit address, if any).
  2291. *
  2292. * This function will insert data into the blob, and will therefore
  2293. * change the offsets of some existing nodes.
  2294. *
  2295. * returns:
  2296. * structure block offset of the created subnode (>=0), on success
  2297. * -FDT_ERR_NOTFOUND, if the requested subnode does not exist
  2298. * -FDT_ERR_BADOFFSET, if parentoffset did not point to an FDT_BEGIN_NODE
  2299. * tag
  2300. * -FDT_ERR_EXISTS, if the node at parentoffset already has a subnode of
  2301. * the given name
  2302. * -FDT_ERR_NOSPACE, if there is insufficient free space in the
  2303. * blob to contain the new node
  2304. * -FDT_ERR_NOSPACE
  2305. * -FDT_ERR_BADLAYOUT
  2306. * -FDT_ERR_BADMAGIC,
  2307. * -FDT_ERR_BADVERSION,
  2308. * -FDT_ERR_BADSTATE,
  2309. * -FDT_ERR_BADSTRUCTURE,
  2310. * -FDT_ERR_TRUNCATED, standard meanings.
  2311. */
  2312. int fdt_add_subnode(void *fdt, int parentoffset, const char *name);
  2313. /**
  2314. * fdt_del_node - delete a node (subtree)
  2315. * @fdt: pointer to the device tree blob
  2316. * @nodeoffset: offset of the node to nop
  2317. *
  2318. * fdt_del_node() will remove the given node, including all its
  2319. * subnodes if any, from the blob.
  2320. *
  2321. * This function will delete data from the blob, and will therefore
  2322. * change the offsets of some existing nodes.
  2323. *
  2324. * returns:
  2325. * 0, on success
  2326. * -FDT_ERR_BADOFFSET, nodeoffset did not point to FDT_BEGIN_NODE tag
  2327. * -FDT_ERR_BADLAYOUT,
  2328. * -FDT_ERR_BADMAGIC,
  2329. * -FDT_ERR_BADVERSION,
  2330. * -FDT_ERR_BADSTATE,
  2331. * -FDT_ERR_BADSTRUCTURE,
  2332. * -FDT_ERR_TRUNCATED, standard meanings
  2333. */
  2334. int fdt_del_node(void *fdt, int nodeoffset);
  2335. /**
  2336. * fdt_overlay_apply - Applies a DT overlay on a base DT
  2337. * @fdt: pointer to the base device tree blob
  2338. * @fdto: pointer to the device tree overlay blob
  2339. *
  2340. * fdt_overlay_apply() will apply the given device tree overlay on the
  2341. * given base device tree.
  2342. *
  2343. * Expect the base device tree to be modified, even if the function
  2344. * returns an error.
  2345. *
  2346. * returns:
  2347. * 0, on success
  2348. * -FDT_ERR_NOSPACE, there's not enough space in the base device tree
  2349. * -FDT_ERR_NOTFOUND, the overlay points to some nonexistent nodes or
  2350. * properties in the base DT
  2351. * -FDT_ERR_BADPHANDLE,
  2352. * -FDT_ERR_BADOVERLAY,
  2353. * -FDT_ERR_NOPHANDLES,
  2354. * -FDT_ERR_INTERNAL,
  2355. * -FDT_ERR_BADLAYOUT,
  2356. * -FDT_ERR_BADMAGIC,
  2357. * -FDT_ERR_BADOFFSET,
  2358. * -FDT_ERR_BADPATH,
  2359. * -FDT_ERR_BADVERSION,
  2360. * -FDT_ERR_BADSTRUCTURE,
  2361. * -FDT_ERR_BADSTATE,
  2362. * -FDT_ERR_TRUNCATED, standard meanings
  2363. */
  2364. int fdt_overlay_apply(void *fdt, void *fdto);
  2365. /**
  2366. * fdt_overlay_target_offset - retrieves the offset of a fragment's target
  2367. * @fdt: Base device tree blob
  2368. * @fdto: Device tree overlay blob
  2369. * @fragment_offset: node offset of the fragment in the overlay
  2370. * @pathp: pointer which receives the path of the target (or NULL)
  2371. *
  2372. * fdt_overlay_target_offset() retrieves the target offset in the base
  2373. * device tree of a fragment, no matter how the actual targeting is
  2374. * done (through a phandle or a path)
  2375. *
  2376. * returns:
  2377. * the targeted node offset in the base device tree
  2378. * Negative error code on error
  2379. */
  2380. int fdt_overlay_target_offset(const void *fdt, const void *fdto,
  2381. int fragment_offset, char const **pathp);
  2382. /**********************************************************************/
  2383. /* Debugging / informational functions */
  2384. /**********************************************************************/
  2385. /**
  2386. * fdt_strerror - return string description of error code
  2387. * @errval: Error code returned by a libfdt function
  2388. *
  2389. * fdt_strerror() returns a string description of the error code passed
  2390. * in @errval.
  2391. *
  2392. * returns:
  2393. * pointer to a string describing the error code
  2394. */
  2395. const char *fdt_strerror(int errval);
  2396. #ifdef __cplusplus
  2397. }
  2398. #endif
  2399. #endif /* LIBFDT_H */