fdt_overlay.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  1. /*-
  2. * Copyright (c) 2015 Oleksandr Tymoshenko <gonzo@FreeBSD.org>
  3. * All rights reserved.
  4. *
  5. * This software was developed by Semihalf under sponsorship from
  6. * the FreeBSD Foundation.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #include "fdt_overlay.h"
  30. //#include <sys/cdefs.h>
  31. // CJP __FBSDID("$FreeBSD$");
  32. // CJP #include <stand.h>
  33. #include <stdio.h>
  34. #include <stdlib.h> // for strtoul()
  35. // CJP from BSD/sys/boot/common/commands.c
  36. char *command_errmsg;
  37. char command_errbuf[256];
  38. #ifdef DEBUG
  39. #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
  40. printf(fmt,##args); } while (0)
  41. #else
  42. #define debugf(fmt, args...)
  43. #endif
  44. /*
  45. * Get max phandle
  46. */
  47. static uint32_t
  48. fdt_max_phandle(void *fdtp)
  49. {
  50. int o, depth;
  51. uint32_t max_phandle, phandle;
  52. depth = 1;
  53. o = fdt_path_offset(fdtp, "/");
  54. max_phandle = fdt_get_phandle(fdtp, o);
  55. for (depth = 0; (o >= 0) && (depth >= 0); o = fdt_next_node(fdtp, o, &depth)) {
  56. phandle = fdt_get_phandle(fdtp, o);
  57. if (max_phandle < phandle) {
  58. max_phandle = phandle;
  59. }
  60. }
  61. return max_phandle;
  62. }
  63. /*
  64. * Returns exact memory location specified by fixup in format
  65. * /path/to/node:property:offset
  66. */
  67. static void *
  68. fdt_get_fixup_location(void *fdtp, const char *fixup)
  69. {
  70. char *path, *prop, *offsetp, *endp;
  71. int prop_offset, o, proplen;
  72. void *prop_w;
  73. // TODO: Keep track of substring lengths so we don't have to malloc
  74. // a copy and split it up.
  75. path = strdup(fixup);
  76. if (!path)
  77. return NULL;
  78. prop = strchr(path, ':');
  79. if (prop == NULL) {
  80. printf("missing property part in \"%s\"\n", path);
  81. free(path);
  82. return NULL;
  83. }
  84. *prop = 0;
  85. prop++;
  86. offsetp = strchr(prop, ':');
  87. if (offsetp == NULL) {
  88. printf("missing offset part in \"%s\"\n", path);
  89. free(path);
  90. return NULL;
  91. }
  92. *offsetp = 0;
  93. offsetp++;
  94. prop_offset = (int)strtoul(offsetp, &endp, 10);
  95. if (*endp != '\0') {
  96. printf("\"%s\" is not valid number\n", offsetp);
  97. free(path);
  98. return NULL;
  99. }
  100. o = fdt_path_offset(fdtp, path);
  101. if (o < 0) {
  102. printf("path \"%s\" not found\n", path);
  103. free(path);
  104. return NULL;
  105. }
  106. prop_w = fdt_getprop_w(fdtp, o, prop, &proplen);
  107. if (prop_w == NULL) {
  108. printf("property \"%s\" not found in \"%s\" node\n", prop, path);
  109. free(path);
  110. return NULL;
  111. }
  112. if (proplen < prop_offset + (int)sizeof(uint32_t)) {
  113. printf("%s: property length is too small for fixup\n", path);
  114. free(path);
  115. return NULL;
  116. }
  117. return (char*)prop_w + prop_offset;
  118. }
  119. /*
  120. * Process one entry in __fixups__ { } node
  121. * @fixups is property value, array of NUL-terminated strings
  122. * with fixup locations
  123. * @fixups_len length of the fixups array in bytes
  124. * @phandle is value for these locations
  125. */
  126. static int
  127. fdt_do_one_fixup(void *fdtp, const char *fixups, int fixups_len, int phandle)
  128. {
  129. void *fixup_pos;
  130. uint32_t val;
  131. val = cpu_to_fdt32(phandle);
  132. while (fixups_len > 0) {
  133. fixup_pos = fdt_get_fixup_location(fdtp, fixups);
  134. if (fixup_pos != NULL) {
  135. memcpy(fixup_pos, &val, sizeof(val));
  136. }
  137. fixups_len -= strlen(fixups) + 1;
  138. fixups += strlen(fixups) + 1;
  139. }
  140. return 0;
  141. }
  142. /*
  143. * Increase u32 value at pos by offset
  144. */
  145. static void
  146. fdt_increase_u32(void *pos, uint32_t offset)
  147. {
  148. uint32_t val;
  149. memcpy(&val, pos, sizeof(val));
  150. val = cpu_to_fdt32(fdt32_to_cpu(val) + offset);
  151. memcpy(pos, &val, sizeof(val));
  152. }
  153. /*
  154. * Process local fixups
  155. * @fixups is property value, array of NUL-terminated strings
  156. * with fixup locations
  157. * @fixups_len length of the fixups array in bytes
  158. * @offset value these locations should be increased by
  159. */
  160. static int
  161. fdt_do_local_fixup(void *fdtp, const char *fixups, int fixups_len, int offset)
  162. {
  163. void *fixup_pos;
  164. while (fixups_len > 0) {
  165. fixup_pos = fdt_get_fixup_location(fdtp, fixups);
  166. if (fixup_pos != NULL) {
  167. fdt_increase_u32(fixup_pos, offset);
  168. }
  169. fixups_len -= strlen(fixups) + 1;
  170. fixups += strlen(fixups) + 1;
  171. }
  172. return 0;
  173. }
  174. /*
  175. * Increase node phandle by phandle_offset
  176. */
  177. static void
  178. fdt_increase_phandle(void *fdtp, int node_offset, uint32_t phandle_offset)
  179. {
  180. int proplen;
  181. void *phandle_pos;
  182. phandle_pos = fdt_getprop_w(fdtp, node_offset, "phandle", &proplen);
  183. if (phandle_pos) {
  184. fdt_increase_u32(phandle_pos, phandle_offset);
  185. }
  186. phandle_pos = fdt_getprop_w(fdtp, node_offset, "linux,phandle", &proplen);
  187. if (phandle_pos) {
  188. fdt_increase_u32(phandle_pos, phandle_offset);
  189. }
  190. }
  191. /*
  192. * Increase all phandles by offset
  193. */
  194. static void
  195. fdt_increase_phandles(void *fdtp, uint32_t offset)
  196. {
  197. int o, depth;
  198. o = fdt_path_offset(fdtp, "/");
  199. for (depth = 0; (o >= 0) && (depth >= 0); o = fdt_next_node(fdtp, o, &depth)) {
  200. fdt_increase_phandle(fdtp, o, offset);
  201. }
  202. }
  203. /*
  204. * Overlay one node deviced by <overlay_fdtp, overlay_o> over <main_fdtp, target_o>
  205. */
  206. static void
  207. fdt_overlay_node(void *main_fdtp, int target_o, void *overlay_fdtp, int overlay_o)
  208. {
  209. int len, o, depth;
  210. const char *name;
  211. const void *val;
  212. int target_subnode_o;
  213. /* Overlay properties */
  214. for (o = fdt_first_property_offset(overlay_fdtp, overlay_o);
  215. o >= 0; o = fdt_next_property_offset(overlay_fdtp, o)) {
  216. val = fdt_getprop_by_offset(overlay_fdtp, o, &name, &len);
  217. if (val) {
  218. int ret;
  219. ret = fdt_setprop(main_fdtp, target_o, name, val, len);
  220. if (ret != 0)
  221. return;
  222. }
  223. }
  224. /* Now overlay nodes */
  225. o = overlay_o;
  226. for (depth = 0; (o >= 0) && (depth >= 0);
  227. o = fdt_next_node(overlay_fdtp, o, &depth)) {
  228. if (depth != 1) {
  229. continue;
  230. }
  231. /* Check if there is node with the same name */
  232. name = fdt_get_name(overlay_fdtp, o, NULL);
  233. target_subnode_o = fdt_subnode_offset(main_fdtp, target_o, name);
  234. if (target_subnode_o < 0) {
  235. /* create new subnode and run merge recursively */
  236. target_subnode_o = fdt_add_subnode(main_fdtp, target_o, name);
  237. if (target_subnode_o < 0) {
  238. printf("failed to create subnode \"%s\": %d\n",
  239. name, target_subnode_o);
  240. return;
  241. }
  242. }
  243. fdt_overlay_node(main_fdtp, target_subnode_o,
  244. overlay_fdtp, o);
  245. }
  246. }
  247. /*
  248. * Apply one overlay fragment
  249. */
  250. static void
  251. fdt_apply_fragment(void *main_fdtp, void *overlay_fdtp, int fragment_o)
  252. {
  253. uint32_t target;
  254. const char *target_path;
  255. const void *val;
  256. int target_node_o, overlay_node_o;
  257. target_node_o = -1;
  258. val = fdt_getprop(overlay_fdtp, fragment_o, "target", NULL);
  259. if (val) {
  260. memcpy(&target, val, sizeof(target));
  261. target = fdt32_to_cpu(target);
  262. target_node_o = fdt_node_offset_by_phandle(main_fdtp, target);
  263. if (target_node_o < 0) {
  264. printf("failed to find target %04x\n", target);
  265. return;
  266. }
  267. }
  268. if (target_node_o < 0) {
  269. target_path = fdt_getprop(overlay_fdtp, fragment_o, "target-path", NULL);
  270. if (target_path == NULL) {
  271. return;
  272. }
  273. target_node_o = fdt_path_offset(main_fdtp, target_path);
  274. if (target_node_o < 0) {
  275. printf("failed to find target-path %s\n", target_path);
  276. return;
  277. }
  278. }
  279. overlay_node_o = fdt_subnode_offset(overlay_fdtp, fragment_o, "__overlay__");
  280. if (overlay_node_o < 0) {
  281. printf("missing __overlay__ sub-node\n");
  282. return;
  283. }
  284. fdt_overlay_node(main_fdtp, target_node_o, overlay_fdtp, overlay_node_o);
  285. }
  286. /*
  287. * Handle __fixups__ node in overlay DTB
  288. */
  289. static int
  290. fdt_overlay_do_fixups(void *main_fdtp, void *overlay_fdtp)
  291. {
  292. int main_symbols_o, symbol_o, overlay_fixups_o;
  293. int fixup_prop_o;
  294. int len;
  295. const char *fixups, *name;
  296. const char *symbol_path;
  297. uint32_t phandle;
  298. main_symbols_o = fdt_path_offset(main_fdtp, "/__symbols__");
  299. overlay_fixups_o = fdt_path_offset(overlay_fdtp, "/__fixups__");
  300. if (main_symbols_o < 0) {
  301. printf("Bad main_symbols in fodf\n");
  302. return -1;
  303. }
  304. // Jim
  305. #if 1
  306. if (overlay_fixups_o < 0) {
  307. printf("Bad overlay_fixups in fodf\n");
  308. return -1;
  309. }
  310. #endif
  311. for (fixup_prop_o = fdt_first_property_offset(overlay_fdtp, overlay_fixups_o);
  312. fixup_prop_o >= 0;
  313. fixup_prop_o = fdt_next_property_offset(overlay_fdtp, fixup_prop_o)) {
  314. fixups = fdt_getprop_by_offset(overlay_fdtp, fixup_prop_o, &name, &len);
  315. symbol_path = fdt_getprop(main_fdtp, main_symbols_o, name, NULL);
  316. if (symbol_path == NULL) {
  317. printf("couldn't find \"%s\" symbol in main dtb\n", name);
  318. return -1;
  319. }
  320. symbol_o = fdt_path_offset(main_fdtp, symbol_path);
  321. if (symbol_o < 0) {
  322. printf("couldn't find \"%s\" path in main dtb\n", symbol_path);
  323. return -1;
  324. }
  325. phandle = fdt_get_phandle(main_fdtp, symbol_o);
  326. if (fdt_do_one_fixup(overlay_fdtp, fixups, len, phandle) < 0) {
  327. printf("Failed one fixup in fodf\n");
  328. return -1;
  329. }
  330. }
  331. return 0;
  332. }
  333. /*
  334. * Handle __local_fixups__ node in overlay DTB
  335. */
  336. static int
  337. fdt_overlay_do_local_fixups(void *main_fdtp, void *overlay_fdtp)
  338. {
  339. int overlay_local_fixups_o;
  340. int len;
  341. const char *fixups;
  342. uint32_t phandle_offset;
  343. overlay_local_fixups_o = fdt_path_offset(overlay_fdtp, "/__local_fixups__");
  344. if (overlay_local_fixups_o < 0) {
  345. return 0;
  346. }
  347. phandle_offset = fdt_max_phandle(main_fdtp);
  348. fdt_increase_phandles(overlay_fdtp, phandle_offset);
  349. fixups = fdt_getprop_w(overlay_fdtp, overlay_local_fixups_o, "fixup", &len);
  350. if (fixups) {
  351. if (fdt_do_local_fixup(overlay_fdtp, fixups, len, phandle_offset) < 0) {
  352. printf("Failed a local fixup in fodlf\n");
  353. return -1;
  354. }
  355. }
  356. return 0;
  357. }
  358. /*
  359. * Apply all fragments to main DTB
  360. */
  361. static int
  362. fdt_overlay_apply_fragments(void *main_fdtp, void *overlay_fdtp)
  363. {
  364. int o, depth;
  365. o = fdt_path_offset(overlay_fdtp, "/");
  366. for (depth = 0; (o >= 0) && (depth >= 0); o = fdt_next_node(overlay_fdtp, o, &depth)) {
  367. if (depth != 1) {
  368. continue;
  369. }
  370. fdt_apply_fragment(main_fdtp, overlay_fdtp, o);
  371. }
  372. return 0;
  373. }
  374. static int
  375. fdt_overlay_apply(void *main_fdtp, void *overlay_fdtp, size_t overlay_length)
  376. {
  377. if (overlay_length < sizeof(struct fdt_header)) {
  378. printf("Overlay_length %zu smaller than header size %zu\n",
  379. overlay_length, sizeof(struct fdt_header));
  380. return -1;
  381. }
  382. if (fdt_overlay_do_fixups(main_fdtp, overlay_fdtp) < 0) {
  383. printf("failed to perform fixups in overlay\n");
  384. return -1;
  385. }
  386. if (fdt_overlay_do_local_fixups(main_fdtp, overlay_fdtp) < 0) {
  387. printf("failed to perform local fixups in overlay\n");
  388. return -1;
  389. }
  390. if (fdt_overlay_apply_fragments(main_fdtp, overlay_fdtp) < 0) {
  391. printf("failed to apply fragments\n");
  392. return -1;
  393. }
  394. return 0;
  395. }
  396. // From fdt_loader_cmd.c
  397. struct fdt_header *
  398. fdt_install_blob(void *blob, size_t blob_size)
  399. {
  400. struct fdt_header *pHeader;
  401. int err;
  402. struct fdt_header *fdtp = NULL;
  403. debugf("fdt_load_dtb(0x%08x)\n", (unsigned int)blob);
  404. if (blob_size < sizeof(struct fdt_header)) {
  405. printf("Blob_size %zu smaller than header size %zu\n",
  406. blob_size, sizeof(struct fdt_header));
  407. return NULL;
  408. }
  409. pHeader = (struct fdt_header *)blob;
  410. err = fdt_check_header(pHeader);
  411. if (err < 0) {
  412. if (err == -FDT_ERR_BADVERSION) {
  413. if (fdtp)
  414. snprintf(command_errbuf, sizeof(command_errbuf),
  415. "incompatible blob version: %d, should be: %d",
  416. fdt_version(fdtp), FDT_LAST_SUPPORTED_VERSION);
  417. } else {
  418. snprintf(command_errbuf, sizeof(command_errbuf), "error validating blob: %s",
  419. fdt_strerror(err));
  420. }
  421. return NULL;
  422. }
  423. return pHeader;
  424. }
  425. // From Google, based on fdt_overlay_apply() logic
  426. // Will malloc a new fdt blob and return it. Will not free parameters.
  427. int apply_overlay(struct fdt_header *fdtp, size_t fdt_size, void *overlay, size_t overlay_size, struct fdt_header *new_fdtp)
  428. {
  429. size_t new_fdtp_size;
  430. int rv;
  431. int ret;
  432. if (fdtp == NULL) {
  433. return -1;
  434. }
  435. if (overlay_size < 8 || overlay_size != fdt_totalsize(overlay)) {
  436. printf("Bad overlay size!\n");
  437. //exit(1);
  438. return -1;
  439. }
  440. if (fdt_size < 8 || fdt_size != fdt_totalsize(fdtp)) {
  441. printf("Bad fdt size!\n");
  442. //exit(1);
  443. return -1;
  444. }
  445. new_fdtp_size = fdt_totalsize(fdtp) + overlay_size;
  446. /* It's actually more than enough */
  447. rv = fdt_open_into(fdtp, new_fdtp, new_fdtp_size);
  448. if (rv != 0) {
  449. printf("failed to open DTB blob for applying overlays\n");
  450. return -1;
  451. }
  452. ret = fdt_overlay_apply(new_fdtp, overlay, overlay_size);
  453. return ret;
  454. }