fdt.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Tests for fdt command
  4. *
  5. * Copyright 2022 Google LLC
  6. */
  7. #include <common.h>
  8. #include <console.h>
  9. #include <fdt_support.h>
  10. #include <mapmem.h>
  11. #include <asm/global_data.h>
  12. #include <linux/libfdt.h>
  13. #include <test/suites.h>
  14. #include <test/ut.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. /*
  17. * Missing tests:
  18. * fdt boardsetup - Do board-specific set up
  19. * fdt checksign [<addr>] - check FIT signature
  20. * <addr> - address of key blob
  21. * default gd->fdt_blob
  22. */
  23. /* Declare a new fdt test */
  24. #define FDT_TEST(_name, _flags) UNIT_TEST(_name, _flags, fdt_test)
  25. /**
  26. * make_test_fdt() - Create an FDT with just a root node
  27. *
  28. * The size is set to the minimum needed
  29. *
  30. * @uts: Test state
  31. * @fdt: Place to write FDT
  32. * @size: Maximum size of space for fdt
  33. */
  34. static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size)
  35. {
  36. ut_assertok(fdt_create(fdt, size));
  37. ut_assertok(fdt_finish_reservemap(fdt));
  38. ut_assert(fdt_begin_node(fdt, "") >= 0);
  39. ut_assertok(fdt_end_node(fdt));
  40. ut_assertok(fdt_finish(fdt));
  41. return 0;
  42. }
  43. /**
  44. * make_fuller_fdt() - Create an FDT with root node and properties
  45. *
  46. * The size is set to the minimum needed
  47. *
  48. * @uts: Test state
  49. * @fdt: Place to write FDT
  50. * @size: Maximum size of space for fdt
  51. */
  52. static int make_fuller_fdt(struct unit_test_state *uts, void *fdt, int size)
  53. {
  54. fdt32_t regs[2] = { cpu_to_fdt32(0x1234), cpu_to_fdt32(0x1000) };
  55. /*
  56. * Assemble the following DT for test purposes:
  57. *
  58. * / {
  59. * #address-cells = <0x00000001>;
  60. * #size-cells = <0x00000001>;
  61. * compatible = "u-boot,fdt-test";
  62. * model = "U-Boot FDT test";
  63. *
  64. * aliases {
  65. * badalias = "/bad/alias";
  66. * subnodealias = "/test-node@1234/subnode";
  67. * testnodealias = "/test-node@1234";
  68. * };
  69. *
  70. * test-node@1234 {
  71. * #address-cells = <0x00000000>;
  72. * #size-cells = <0x00000000>;
  73. * compatible = "u-boot,fdt-test-device1";
  74. * clock-names = "fixed", "i2c", "spi", "uart2", "uart1";
  75. * u-boot,empty-property;
  76. * clock-frequency = <0x00fde800>;
  77. * regs = <0x00001234 0x00001000>;
  78. *
  79. * subnode {
  80. * #address-cells = <0x00000000>;
  81. * #size-cells = <0x00000000>;
  82. * compatible = "u-boot,fdt-subnode-test-device";
  83. * };
  84. * };
  85. * };
  86. */
  87. ut_assertok(fdt_create(fdt, size));
  88. ut_assertok(fdt_finish_reservemap(fdt));
  89. ut_assert(fdt_begin_node(fdt, "") >= 0);
  90. ut_assertok(fdt_property_u32(fdt, "#address-cells", 1));
  91. ut_assertok(fdt_property_u32(fdt, "#size-cells", 1));
  92. /* <string> */
  93. ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test"));
  94. /* <string> */
  95. ut_assertok(fdt_property_string(fdt, "model", "U-Boot FDT test"));
  96. ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
  97. /* <string> */
  98. ut_assertok(fdt_property_string(fdt, "badalias", "/bad/alias"));
  99. /* <string> */
  100. ut_assertok(fdt_property_string(fdt, "subnodealias", "/test-node@1234/subnode"));
  101. /* <string> */
  102. ut_assertok(fdt_property_string(fdt, "testnodealias", "/test-node@1234"));
  103. ut_assertok(fdt_end_node(fdt));
  104. ut_assert(fdt_begin_node(fdt, "test-node@1234") >= 0);
  105. ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
  106. ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
  107. /* <string> */
  108. ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test-device1"));
  109. /* <stringlist> */
  110. ut_assertok(fdt_property(fdt, "clock-names", "fixed\0i2c\0spi\0uart2\0uart1\0", 26));
  111. /* <empty> */
  112. ut_assertok(fdt_property(fdt, "u-boot,empty-property", NULL, 0));
  113. /*
  114. * <u32>
  115. * This value is deliberate as it used to break cmd/fdt.c
  116. * is_printable_string() implementation.
  117. */
  118. ut_assertok(fdt_property_u32(fdt, "clock-frequency", 16640000));
  119. /* <prop-encoded-array> */
  120. ut_assertok(fdt_property(fdt, "regs", &regs, sizeof(regs)));
  121. ut_assert(fdt_begin_node(fdt, "subnode") >= 0);
  122. ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
  123. ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
  124. ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-subnode-test-device"));
  125. ut_assertok(fdt_end_node(fdt));
  126. ut_assertok(fdt_end_node(fdt));
  127. ut_assertok(fdt_end_node(fdt));
  128. ut_assertok(fdt_finish(fdt));
  129. return 0;
  130. }
  131. /* Test 'fdt addr' getting/setting address */
  132. static int fdt_test_addr(struct unit_test_state *uts)
  133. {
  134. const void *fdt_blob, *new_fdt;
  135. char fdt[256];
  136. ulong addr;
  137. int ret;
  138. ut_assertok(console_record_reset_enable());
  139. ut_assertok(run_command("fdt addr -c", 0));
  140. ut_assert_nextline("Control fdt: %08lx",
  141. (ulong)map_to_sysmem(gd->fdt_blob));
  142. ut_assertok(ut_check_console_end(uts));
  143. /* The working fdt is not set, so this should fail */
  144. set_working_fdt_addr(0);
  145. ut_assert_nextline("Working FDT set to 0");
  146. ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));
  147. ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
  148. ut_assertok(ut_check_console_end(uts));
  149. /* Set up a working FDT and try again */
  150. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  151. addr = map_to_sysmem(fdt);
  152. set_working_fdt_addr(addr);
  153. ut_assert_nextline("Working FDT set to %lx", addr);
  154. ut_assertok(run_command("fdt addr", 0));
  155. ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
  156. ut_assertok(ut_check_console_end(uts));
  157. /* Set the working FDT */
  158. set_working_fdt_addr(0);
  159. ut_assert_nextline("Working FDT set to 0");
  160. ut_assertok(run_commandf("fdt addr %08lx", addr));
  161. ut_assert_nextline("Working FDT set to %lx", addr);
  162. ut_asserteq(addr, map_to_sysmem(working_fdt));
  163. ut_assertok(ut_check_console_end(uts));
  164. set_working_fdt_addr(0);
  165. ut_assert_nextline("Working FDT set to 0");
  166. /* Set the control FDT */
  167. fdt_blob = gd->fdt_blob;
  168. gd->fdt_blob = NULL;
  169. ret = run_commandf("fdt addr -c %08lx", addr);
  170. new_fdt = gd->fdt_blob;
  171. gd->fdt_blob = fdt_blob;
  172. ut_assertok(ret);
  173. ut_asserteq(addr, map_to_sysmem(new_fdt));
  174. ut_assertok(ut_check_console_end(uts));
  175. /* Test setting an invalid FDT */
  176. fdt[0] = 123;
  177. ut_asserteq(1, run_commandf("fdt addr %08lx", addr));
  178. ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
  179. ut_assertok(ut_check_console_end(uts));
  180. /* Test detecting an invalid FDT */
  181. fdt[0] = 123;
  182. set_working_fdt_addr(addr);
  183. ut_assert_nextline("Working FDT set to %lx", addr);
  184. ut_asserteq(1, run_commandf("fdt addr"));
  185. ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
  186. ut_assertok(ut_check_console_end(uts));
  187. return 0;
  188. }
  189. FDT_TEST(fdt_test_addr, UT_TESTF_CONSOLE_REC);
  190. /* Test 'fdt addr' resizing an fdt */
  191. static int fdt_test_addr_resize(struct unit_test_state *uts)
  192. {
  193. char fdt[256];
  194. const int newsize = sizeof(fdt) / 2;
  195. ulong addr;
  196. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  197. addr = map_to_sysmem(fdt);
  198. set_working_fdt_addr(addr);
  199. /* Test setting and resizing the working FDT to a larger size */
  200. ut_assertok(console_record_reset_enable());
  201. ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize));
  202. ut_assert_nextline("Working FDT set to %lx", addr);
  203. ut_assertok(ut_check_console_end(uts));
  204. /* Try shrinking it */
  205. ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4));
  206. ut_assert_nextline("Working FDT set to %lx", addr);
  207. ut_assert_nextline("New length %d < existing length %d, ignoring",
  208. (int)sizeof(fdt) / 4, newsize);
  209. ut_assertok(ut_check_console_end(uts));
  210. /* ...quietly */
  211. ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4));
  212. ut_assertok(ut_check_console_end(uts));
  213. /* We cannot easily provoke errors in fdt_open_into(), so ignore that */
  214. return 0;
  215. }
  216. FDT_TEST(fdt_test_addr_resize, UT_TESTF_CONSOLE_REC);
  217. static int fdt_test_move(struct unit_test_state *uts)
  218. {
  219. char fdt[256];
  220. ulong addr, newaddr = 0x10000;
  221. const int size = sizeof(fdt);
  222. uint32_t ts;
  223. void *buf;
  224. /* Original source DT */
  225. ut_assertok(make_test_fdt(uts, fdt, size));
  226. ts = fdt_totalsize(fdt);
  227. addr = map_to_sysmem(fdt);
  228. set_working_fdt_addr(addr);
  229. /* Moved target DT location */
  230. buf = map_sysmem(newaddr, size);
  231. memset(buf, 0, size);
  232. /* Test moving the working FDT to a new location */
  233. ut_assertok(console_record_reset_enable());
  234. ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts));
  235. ut_assert_nextline("Working FDT set to %lx", newaddr);
  236. ut_assertok(ut_check_console_end(uts));
  237. /* Compare the source and destination DTs */
  238. ut_assertok(console_record_reset_enable());
  239. ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts));
  240. ut_assert_nextline("Total of %d byte(s) were the same", ts);
  241. ut_assertok(ut_check_console_end(uts));
  242. return 0;
  243. }
  244. FDT_TEST(fdt_test_move, UT_TESTF_CONSOLE_REC);
  245. static int fdt_test_resize(struct unit_test_state *uts)
  246. {
  247. char fdt[256];
  248. const unsigned int newsize = 0x2000;
  249. uint32_t ts;
  250. ulong addr;
  251. /* Original source DT */
  252. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  253. fdt_shrink_to_minimum(fdt, 0); /* Resize with 0 extra bytes */
  254. ts = fdt_totalsize(fdt);
  255. addr = map_to_sysmem(fdt);
  256. set_working_fdt_addr(addr);
  257. /* Test resizing the working FDT and verify the new space was added */
  258. ut_assertok(console_record_reset_enable());
  259. ut_assertok(run_commandf("fdt resize %x", newsize));
  260. ut_asserteq(ts + newsize, fdt_totalsize(fdt));
  261. ut_assertok(ut_check_console_end(uts));
  262. return 0;
  263. }
  264. FDT_TEST(fdt_test_resize, UT_TESTF_CONSOLE_REC);
  265. static int fdt_test_print_list_common(struct unit_test_state *uts,
  266. const char *opc, const char *node)
  267. {
  268. /*
  269. * Test printing/listing the working FDT
  270. * subnode $node/subnode
  271. */
  272. ut_assertok(console_record_reset_enable());
  273. ut_assertok(run_commandf("fdt %s %s/subnode", opc, node));
  274. ut_assert_nextline("subnode {");
  275. ut_assert_nextline("\t#address-cells = <0x00000000>;");
  276. ut_assert_nextline("\t#size-cells = <0x00000000>;");
  277. ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";");
  278. ut_assert_nextline("};");
  279. ut_assertok(ut_check_console_end(uts));
  280. /*
  281. * Test printing/listing the working FDT
  282. * path / string property model
  283. */
  284. ut_assertok(console_record_reset_enable());
  285. ut_assertok(run_commandf("fdt %s / model", opc));
  286. ut_assert_nextline("model = \"U-Boot FDT test\"");
  287. ut_assertok(ut_check_console_end(uts));
  288. /*
  289. * Test printing/listing the working FDT
  290. * path $node string property compatible
  291. */
  292. ut_assertok(console_record_reset_enable());
  293. ut_assertok(run_commandf("fdt %s %s compatible", opc, node));
  294. ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\"");
  295. ut_assertok(ut_check_console_end(uts));
  296. /*
  297. * Test printing/listing the working FDT
  298. * path $node stringlist property clock-names
  299. */
  300. ut_assertok(console_record_reset_enable());
  301. ut_assertok(run_commandf("fdt %s %s clock-names", opc, node));
  302. ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
  303. ut_assertok(ut_check_console_end(uts));
  304. /*
  305. * Test printing/listing the working FDT
  306. * path $node u32 property clock-frequency
  307. */
  308. ut_assertok(console_record_reset_enable());
  309. ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node));
  310. ut_assert_nextline("clock-frequency = <0x00fde800>");
  311. ut_assertok(ut_check_console_end(uts));
  312. /*
  313. * Test printing/listing the working FDT
  314. * path $node empty property u-boot,empty-property
  315. */
  316. ut_assertok(console_record_reset_enable());
  317. ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node));
  318. /*
  319. * This is the only 'fdt print' / 'fdt list' incantation which
  320. * prefixes the property with node path. This has been in U-Boot
  321. * since the beginning of the command 'fdt', keep it.
  322. */
  323. ut_assert_nextline("%s u-boot,empty-property", node);
  324. ut_assertok(ut_check_console_end(uts));
  325. /*
  326. * Test printing/listing the working FDT
  327. * path $node prop-encoded array property regs
  328. */
  329. ut_assertok(console_record_reset_enable());
  330. ut_assertok(run_commandf("fdt %s %s regs", opc, node));
  331. ut_assert_nextline("regs = <0x00001234 0x00001000>");
  332. ut_assertok(ut_check_console_end(uts));
  333. return 0;
  334. }
  335. static int fdt_test_print_list(struct unit_test_state *uts, bool print)
  336. {
  337. const char *opc = print ? "print" : "list";
  338. char fdt[4096];
  339. ulong addr;
  340. int ret;
  341. /* Original source DT */
  342. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  343. addr = map_to_sysmem(fdt);
  344. set_working_fdt_addr(addr);
  345. /* Test printing/listing the working FDT -- node / */
  346. ut_assertok(console_record_reset_enable());
  347. ut_assertok(run_commandf("fdt %s", opc));
  348. ut_assert_nextline("/ {");
  349. ut_assert_nextline("\t#address-cells = <0x00000001>;");
  350. ut_assert_nextline("\t#size-cells = <0x00000001>;");
  351. ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";");
  352. ut_assert_nextline("\tmodel = \"U-Boot FDT test\";");
  353. ut_assert_nextline("\taliases {");
  354. if (print) {
  355. ut_assert_nextline("\t\tbadalias = \"/bad/alias\";");
  356. ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";");
  357. ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";");
  358. }
  359. ut_assert_nextline("\t};");
  360. ut_assert_nextline("\ttest-node@1234 {");
  361. if (print) {
  362. ut_assert_nextline("\t\t#address-cells = <0x00000000>;");
  363. ut_assert_nextline("\t\t#size-cells = <0x00000000>;");
  364. ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";");
  365. ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";");
  366. ut_assert_nextline("\t\tu-boot,empty-property;");
  367. ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;");
  368. ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;");
  369. ut_assert_nextline("\t\tsubnode {");
  370. ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;");
  371. ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;");
  372. ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";");
  373. ut_assert_nextline("\t\t};");
  374. }
  375. ut_assert_nextline("\t};");
  376. ut_assert_nextline("};");
  377. ut_assertok(ut_check_console_end(uts));
  378. ret = fdt_test_print_list_common(uts, opc, "/test-node@1234");
  379. if (!ret)
  380. ret = fdt_test_print_list_common(uts, opc, "testnodealias");
  381. return 0;
  382. }
  383. static int fdt_test_print(struct unit_test_state *uts)
  384. {
  385. return fdt_test_print_list(uts, true);
  386. }
  387. FDT_TEST(fdt_test_print, UT_TESTF_CONSOLE_REC);
  388. static int fdt_test_list(struct unit_test_state *uts)
  389. {
  390. return fdt_test_print_list(uts, false);
  391. }
  392. FDT_TEST(fdt_test_list, UT_TESTF_CONSOLE_REC);
  393. /* Test 'fdt get value' reading an fdt */
  394. static int fdt_test_get_value_string(struct unit_test_state *uts,
  395. const char *node, const char *prop,
  396. const char *idx, const char *strres,
  397. const int intres)
  398. {
  399. ut_assertok(console_record_reset_enable());
  400. ut_assertok(run_commandf("fdt get value var %s %s %s",
  401. node, prop, idx ? : ""));
  402. if (strres) {
  403. ut_asserteq_str(strres, env_get("var"));
  404. } else {
  405. ut_asserteq(intres, env_get_hex("var", 0x1234));
  406. }
  407. ut_assertok(ut_check_console_end(uts));
  408. return 0;
  409. }
  410. static int fdt_test_get_value_common(struct unit_test_state *uts,
  411. const char *node)
  412. {
  413. /* Test getting default element of $node node clock-names property */
  414. fdt_test_get_value_string(uts, node, "clock-names", NULL, "fixed", 0);
  415. /* Test getting 0th element of $node node clock-names property */
  416. fdt_test_get_value_string(uts, node, "clock-names", "0", "fixed", 0);
  417. /* Test getting 1st element of $node node clock-names property */
  418. fdt_test_get_value_string(uts, node, "clock-names", "1", "i2c", 0);
  419. /* Test getting 2nd element of $node node clock-names property */
  420. fdt_test_get_value_string(uts, node, "clock-names", "2", "spi", 0);
  421. /*
  422. * Test getting default element of $node node regs property.
  423. * The result here is highly unusual, the non-index value read from
  424. * integer array is a string of concatenated values from the array,
  425. * but only if the array is shorter than 40 characters. Anything
  426. * longer is an error. This is a special case for handling hashes.
  427. */
  428. fdt_test_get_value_string(uts, node, "regs", NULL, "3412000000100000", 0);
  429. /* Test getting 0th element of $node node regs property */
  430. fdt_test_get_value_string(uts, node, "regs", "0", NULL, 0x1234);
  431. /* Test getting 1st element of $node node regs property */
  432. fdt_test_get_value_string(uts, node, "regs", "1", NULL, 0x1000);
  433. /* Test missing 10th element of $node node clock-names property */
  434. ut_assertok(console_record_reset_enable());
  435. ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
  436. ut_assertok(ut_check_console_end(uts));
  437. /* Test missing 10th element of $node node regs property */
  438. ut_assertok(console_record_reset_enable());
  439. ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
  440. ut_assertok(ut_check_console_end(uts));
  441. /* Test getting default element of $node node nonexistent property */
  442. ut_assertok(console_record_reset_enable());
  443. ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
  444. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  445. ut_assertok(ut_check_console_end(uts));
  446. return 0;
  447. }
  448. static int fdt_test_get_value(struct unit_test_state *uts)
  449. {
  450. char fdt[4096];
  451. ulong addr;
  452. int ret;
  453. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  454. addr = map_to_sysmem(fdt);
  455. set_working_fdt_addr(addr);
  456. ret = fdt_test_get_value_common(uts, "/test-node@1234");
  457. if (!ret)
  458. ret = fdt_test_get_value_common(uts, "testnodealias");
  459. if (ret)
  460. return ret;
  461. /* Test getting default element of /nonexistent node */
  462. ut_assertok(console_record_reset_enable());
  463. ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
  464. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  465. ut_assertok(ut_check_console_end(uts));
  466. /* Test getting default element of bad alias */
  467. ut_assertok(console_record_reset_enable());
  468. ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
  469. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  470. ut_assertok(ut_check_console_end(uts));
  471. /* Test getting default element of nonexistent alias */
  472. ut_assertok(console_record_reset_enable());
  473. ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
  474. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  475. ut_assertok(ut_check_console_end(uts));
  476. return 0;
  477. }
  478. FDT_TEST(fdt_test_get_value, UT_TESTF_CONSOLE_REC);
  479. static int fdt_test_get_name(struct unit_test_state *uts)
  480. {
  481. char fdt[4096];
  482. ulong addr;
  483. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  484. addr = map_to_sysmem(fdt);
  485. set_working_fdt_addr(addr);
  486. /* Test getting name of node 0 in /, which is /aliases node */
  487. ut_assertok(console_record_reset_enable());
  488. ut_assertok(run_command("fdt get name nzero / 0", 0));
  489. ut_asserteq_str("aliases", env_get("nzero"));
  490. ut_assertok(ut_check_console_end(uts));
  491. /* Test getting name of node 1 in /, which is /test-node@1234 node */
  492. ut_assertok(console_record_reset_enable());
  493. ut_assertok(run_command("fdt get name none / 1", 0));
  494. ut_asserteq_str("test-node@1234", env_get("none"));
  495. ut_assertok(ut_check_console_end(uts));
  496. /* Test getting name of node -1 in /, which is /aliases node, same as 0 */
  497. ut_assertok(console_record_reset_enable());
  498. ut_assertok(run_command("fdt get name nmone / -1", 0));
  499. ut_asserteq_str("aliases", env_get("nmone"));
  500. ut_assertok(ut_check_console_end(uts));
  501. /* Test getting name of node 2 in /, which does not exist */
  502. ut_assertok(console_record_reset_enable());
  503. ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
  504. ut_assert_nextline("libfdt node not found");
  505. ut_assertok(ut_check_console_end(uts));
  506. /* Test getting name of node 0 in /test-node@1234, which is /subnode node */
  507. ut_assertok(console_record_reset_enable());
  508. ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
  509. ut_asserteq_str("subnode", env_get("snzero"));
  510. ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
  511. ut_asserteq_str("subnode", env_get("asnzero"));
  512. ut_assertok(ut_check_console_end(uts));
  513. /* Test getting name of node 1 in /test-node@1234, which does not exist */
  514. ut_assertok(console_record_reset_enable());
  515. ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
  516. ut_assert_nextline("libfdt node not found");
  517. ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
  518. ut_assert_nextline("libfdt node not found");
  519. ut_assertok(ut_check_console_end(uts));
  520. /* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
  521. ut_assertok(console_record_reset_enable());
  522. ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
  523. ut_asserteq_str("subnode", env_get("snmone"));
  524. ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
  525. ut_asserteq_str("subnode", env_get("asnmone"));
  526. ut_assertok(ut_check_console_end(uts));
  527. /* Test getting name of nonexistent node */
  528. ut_assertok(console_record_reset_enable());
  529. ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
  530. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  531. ut_assertok(ut_check_console_end(uts));
  532. /* Test getting name of bad alias */
  533. ut_assertok(console_record_reset_enable());
  534. ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
  535. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  536. ut_assertok(ut_check_console_end(uts));
  537. /* Test getting name of nonexistent alias */
  538. ut_assertok(console_record_reset_enable());
  539. ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
  540. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  541. ut_assertok(ut_check_console_end(uts));
  542. return 0;
  543. }
  544. FDT_TEST(fdt_test_get_name, UT_TESTF_CONSOLE_REC);
  545. static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
  546. const char *path, const char *prop)
  547. {
  548. unsigned int offset;
  549. int path_offset;
  550. void *prop_ptr;
  551. int len = 0;
  552. path_offset = fdt_path_offset(fdt, path);
  553. ut_assert(path_offset >= 0);
  554. prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
  555. ut_assertnonnull(prop_ptr);
  556. offset = (char *)prop_ptr - fdt;
  557. ut_assertok(console_record_reset_enable());
  558. ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
  559. ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
  560. (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
  561. ut_assertok(ut_check_console_end(uts));
  562. return 0;
  563. }
  564. static int fdt_test_get_addr(struct unit_test_state *uts)
  565. {
  566. char fdt[4096];
  567. ulong addr;
  568. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  569. addr = map_to_sysmem(fdt);
  570. set_working_fdt_addr(addr);
  571. /* Test getting address of root node / string property "compatible" */
  572. fdt_test_get_addr_common(uts, fdt, "/", "compatible");
  573. /* Test getting address of node /test-node@1234 stringlist property "clock-names" */
  574. fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-names");
  575. fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-names");
  576. /* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
  577. fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "clock-frequency");
  578. fdt_test_get_addr_common(uts, fdt, "testnodealias", "clock-frequency");
  579. /* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
  580. fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "u-boot,empty-property");
  581. fdt_test_get_addr_common(uts, fdt, "testnodealias", "u-boot,empty-property");
  582. /* Test getting address of node /test-node@1234 array property "regs" */
  583. fdt_test_get_addr_common(uts, fdt, "/test-node@1234", "regs");
  584. fdt_test_get_addr_common(uts, fdt, "testnodealias", "regs");
  585. /* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
  586. ut_assertok(console_record_reset_enable());
  587. ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
  588. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  589. ut_assertok(ut_check_console_end(uts));
  590. /* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
  591. ut_assertok(console_record_reset_enable());
  592. ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
  593. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  594. ut_assertok(ut_check_console_end(uts));
  595. return 0;
  596. }
  597. FDT_TEST(fdt_test_get_addr, UT_TESTF_CONSOLE_REC);
  598. static int fdt_test_get_size_common(struct unit_test_state *uts,
  599. const char *path, const char *prop,
  600. const unsigned int val)
  601. {
  602. ut_assertok(console_record_reset_enable());
  603. if (prop) {
  604. ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
  605. } else {
  606. ut_assertok(run_commandf("fdt get size sstr %s", path));
  607. }
  608. ut_asserteq(val, env_get_hex("sstr", 0x1234));
  609. ut_assertok(ut_check_console_end(uts));
  610. return 0;
  611. }
  612. static int fdt_test_get_size(struct unit_test_state *uts)
  613. {
  614. char fdt[4096];
  615. ulong addr;
  616. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  617. addr = map_to_sysmem(fdt);
  618. set_working_fdt_addr(addr);
  619. /* Test getting size of root node / string property "compatible" */
  620. fdt_test_get_size_common(uts, "/", "compatible", 16);
  621. /* Test getting size of node /test-node@1234 stringlist property "clock-names" */
  622. fdt_test_get_size_common(uts, "/test-node@1234", "clock-names", 26);
  623. fdt_test_get_size_common(uts, "testnodealias", "clock-names", 26);
  624. /* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
  625. fdt_test_get_size_common(uts, "/test-node@1234", "clock-frequency", 4);
  626. fdt_test_get_size_common(uts, "testnodealias", "clock-frequency", 4);
  627. /* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
  628. fdt_test_get_size_common(uts, "/test-node@1234", "u-boot,empty-property", 0);
  629. fdt_test_get_size_common(uts, "testnodealias", "u-boot,empty-property", 0);
  630. /* Test getting size of node /test-node@1234 array property "regs" */
  631. fdt_test_get_size_common(uts, "/test-node@1234", "regs", 8);
  632. fdt_test_get_size_common(uts, "testnodealias", "regs", 8);
  633. /* Test getting node count of node / */
  634. fdt_test_get_size_common(uts, "/", NULL, 2);
  635. /* Test getting node count of node /test-node@1234/subnode */
  636. fdt_test_get_size_common(uts, "/test-node@1234/subnode", NULL, 0);
  637. fdt_test_get_size_common(uts, "subnodealias", NULL, 0);
  638. /* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
  639. ut_assertok(console_record_reset_enable());
  640. ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
  641. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  642. ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
  643. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  644. ut_assertok(ut_check_console_end(uts));
  645. /* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
  646. ut_assertok(console_record_reset_enable());
  647. ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
  648. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  649. ut_assertok(ut_check_console_end(uts));
  650. /* Test getting node count of non-existent node /test-node@1234/nonode@1 */
  651. ut_assertok(console_record_reset_enable());
  652. ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
  653. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  654. ut_assertok(ut_check_console_end(uts));
  655. /* Test getting node count of bad alias badalias */
  656. ut_assertok(console_record_reset_enable());
  657. ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
  658. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  659. ut_assertok(ut_check_console_end(uts));
  660. /* Test getting node count of non-existent alias noalias */
  661. ut_assertok(console_record_reset_enable());
  662. ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
  663. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  664. ut_assertok(ut_check_console_end(uts));
  665. return 0;
  666. }
  667. FDT_TEST(fdt_test_get_size, UT_TESTF_CONSOLE_REC);
  668. static int fdt_test_set_single(struct unit_test_state *uts,
  669. const char *path, const char *prop,
  670. const char *sval, int ival, bool integer)
  671. {
  672. /*
  673. * Set single element string/integer/<empty> property into DT, that is:
  674. * => fdt set /path property string
  675. * => fdt set /path property integer
  676. * => fdt set /path property
  677. */
  678. ut_assertok(console_record_reset_enable());
  679. if (sval)
  680. ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
  681. else if (integer)
  682. ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
  683. else
  684. ut_assertok(run_commandf("fdt set %s %s", path, prop));
  685. /* Validate the property is present and has correct value. */
  686. ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
  687. if (sval)
  688. ut_asserteq_str(sval, env_get("svar"));
  689. else if (integer)
  690. ut_asserteq(ival, env_get_hex("svar", 0x1234));
  691. else
  692. ut_assertnull(env_get("svar"));
  693. ut_assertok(ut_check_console_end(uts));
  694. return 0;
  695. }
  696. static int fdt_test_set_multi(struct unit_test_state *uts,
  697. const char *path, const char *prop,
  698. const char *sval1, const char *sval2,
  699. int ival1, int ival2)
  700. {
  701. /*
  702. * Set multi element string/integer array property in DT, that is:
  703. * => fdt set /path property <string1 string2>
  704. * => fdt set /path property <integer1 integer2>
  705. *
  706. * The set is done twice in here deliberately, The first set adds
  707. * the property with an extra trailing element in its array to make
  708. * the array longer, the second set is the expected final content of
  709. * the array property. The longer array is used to verify that the
  710. * new array is correctly sized and read past the new array length
  711. * triggers failure.
  712. */
  713. ut_assertok(console_record_reset_enable());
  714. if (sval1 && sval2) {
  715. ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
  716. ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
  717. } else {
  718. ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
  719. ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
  720. }
  721. /*
  722. * Validate the property is present and has correct value.
  723. *
  724. * The "end/10" above and "svarn" below is used to validate that
  725. * previous 'fdt set' to longer array does not polute newly set
  726. * shorter array.
  727. */
  728. ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
  729. ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
  730. ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
  731. if (sval1 && sval2) {
  732. ut_asserteq_str(sval1, env_get("svar1"));
  733. ut_asserteq_str(sval2, env_get("svar2"));
  734. ut_assertnull(env_get("svarn"));
  735. } else {
  736. ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
  737. ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
  738. ut_assertnull(env_get("svarn"));
  739. }
  740. ut_assertok(ut_check_console_end(uts));
  741. return 0;
  742. }
  743. static int fdt_test_set_node(struct unit_test_state *uts,
  744. const char *path, const char *prop)
  745. {
  746. fdt_test_set_single(uts, path, prop, "new", 0, false);
  747. fdt_test_set_single(uts, path, prop, "rewrite", 0, false);
  748. fdt_test_set_single(uts, path, prop, NULL, 42, true);
  749. fdt_test_set_single(uts, path, prop, NULL, 0, false);
  750. fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701);
  751. fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9);
  752. fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0);
  753. fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0);
  754. return 0;
  755. }
  756. static int fdt_test_set(struct unit_test_state *uts)
  757. {
  758. char fdt[8192];
  759. ulong addr;
  760. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  761. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  762. addr = map_to_sysmem(fdt);
  763. set_working_fdt_addr(addr);
  764. /* Test setting of root node / existing property "compatible" */
  765. fdt_test_set_node(uts, "/", "compatible");
  766. /* Test setting of root node / new property "newproperty" */
  767. fdt_test_set_node(uts, "/", "newproperty");
  768. /* Test setting of subnode existing property "compatible" */
  769. fdt_test_set_node(uts, "/test-node@1234/subnode", "compatible");
  770. fdt_test_set_node(uts, "subnodealias", "compatible");
  771. /* Test setting of subnode new property "newproperty" */
  772. fdt_test_set_node(uts, "/test-node@1234/subnode", "newproperty");
  773. fdt_test_set_node(uts, "subnodealias", "newproperty");
  774. /* Test setting property of non-existent node */
  775. ut_assertok(console_record_reset_enable());
  776. ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
  777. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  778. ut_assertok(ut_check_console_end(uts));
  779. /* Test setting property of non-existent alias */
  780. ut_assertok(console_record_reset_enable());
  781. ut_asserteq(1, run_command("fdt set noalias noprop", 1));
  782. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  783. ut_assertok(ut_check_console_end(uts));
  784. /* Test setting property of bad alias */
  785. ut_assertok(console_record_reset_enable());
  786. ut_asserteq(1, run_command("fdt set badalias noprop", 1));
  787. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  788. ut_assertok(ut_check_console_end(uts));
  789. return 0;
  790. }
  791. FDT_TEST(fdt_test_set, UT_TESTF_CONSOLE_REC);
  792. static int fdt_test_mknode(struct unit_test_state *uts)
  793. {
  794. char fdt[8192];
  795. ulong addr;
  796. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  797. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  798. addr = map_to_sysmem(fdt);
  799. set_working_fdt_addr(addr);
  800. /* Test creation of new node in / */
  801. ut_assertok(console_record_reset_enable());
  802. ut_assertok(run_commandf("fdt mknode / newnode"));
  803. ut_assertok(run_commandf("fdt list /newnode"));
  804. ut_assert_nextline("newnode {");
  805. ut_assert_nextline("};");
  806. ut_assertok(ut_check_console_end(uts));
  807. /* Test creation of new node in /test-node@1234 */
  808. ut_assertok(console_record_reset_enable());
  809. ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
  810. ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
  811. ut_assert_nextline("newsubnode {");
  812. ut_assert_nextline("};");
  813. ut_assertok(ut_check_console_end(uts));
  814. /* Test creation of new node in /test-node@1234 by alias */
  815. ut_assertok(console_record_reset_enable());
  816. ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
  817. ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
  818. ut_assert_nextline("newersubnode {");
  819. ut_assert_nextline("};");
  820. ut_assertok(ut_check_console_end(uts));
  821. /* Test creation of new node in /test-node@1234 over existing node */
  822. ut_assertok(console_record_reset_enable());
  823. ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
  824. ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
  825. ut_assertok(ut_check_console_end(uts));
  826. /* Test creation of new node in /test-node@1234 by alias over existing node */
  827. ut_assertok(console_record_reset_enable());
  828. ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
  829. ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
  830. ut_assertok(ut_check_console_end(uts));
  831. /* Test creation of new node in non-existent node */
  832. ut_assertok(console_record_reset_enable());
  833. ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
  834. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  835. ut_assertok(ut_check_console_end(uts));
  836. /* Test creation of new node in non-existent alias */
  837. ut_assertok(console_record_reset_enable());
  838. ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
  839. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  840. ut_assertok(ut_check_console_end(uts));
  841. /* Test creation of new node in bad alias */
  842. ut_assertok(console_record_reset_enable());
  843. ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
  844. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  845. ut_assertok(ut_check_console_end(uts));
  846. return 0;
  847. }
  848. FDT_TEST(fdt_test_mknode, UT_TESTF_CONSOLE_REC);
  849. static int fdt_test_rm(struct unit_test_state *uts)
  850. {
  851. char fdt[4096];
  852. ulong addr;
  853. ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt)));
  854. addr = map_to_sysmem(fdt);
  855. set_working_fdt_addr(addr);
  856. /* Test removal of property in root node / */
  857. ut_assertok(console_record_reset_enable());
  858. ut_assertok(run_commandf("fdt print / compatible"));
  859. ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
  860. ut_assertok(run_commandf("fdt rm / compatible"));
  861. ut_asserteq(1, run_commandf("fdt print / compatible"));
  862. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  863. ut_assertok(ut_check_console_end(uts));
  864. /* Test removal of property clock-names in subnode /test-node@1234 */
  865. ut_assertok(console_record_reset_enable());
  866. ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
  867. ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
  868. ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
  869. ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
  870. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  871. ut_assertok(ut_check_console_end(uts));
  872. /* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
  873. ut_assertok(console_record_reset_enable());
  874. ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
  875. ut_assert_nextline("testnodealias u-boot,empty-property");
  876. ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
  877. ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
  878. ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
  879. ut_assertok(ut_check_console_end(uts));
  880. /* Test removal of non-existent property noprop in subnode /test-node@1234 */
  881. ut_assertok(console_record_reset_enable());
  882. ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
  883. ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
  884. ut_assertok(ut_check_console_end(uts));
  885. /* Test removal of non-existent node /no-node@5678 */
  886. ut_assertok(console_record_reset_enable());
  887. ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
  888. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  889. ut_assertok(ut_check_console_end(uts));
  890. /* Test removal of subnode /test-node@1234/subnode by alias */
  891. ut_assertok(console_record_reset_enable());
  892. ut_assertok(run_commandf("fdt rm subnodealias"));
  893. ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
  894. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  895. ut_assertok(ut_check_console_end(uts));
  896. /* Test removal of node by non-existent alias */
  897. ut_assertok(console_record_reset_enable());
  898. ut_asserteq(1, run_commandf("fdt rm noalias"));
  899. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  900. ut_assertok(ut_check_console_end(uts));
  901. /* Test removal of node by bad alias */
  902. ut_assertok(console_record_reset_enable());
  903. ut_asserteq(1, run_commandf("fdt rm noalias"));
  904. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
  905. ut_assertok(ut_check_console_end(uts));
  906. /* Test removal of node /test-node@1234 */
  907. ut_assertok(console_record_reset_enable());
  908. ut_assertok(run_commandf("fdt rm /test-node@1234"));
  909. ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
  910. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  911. ut_assertok(ut_check_console_end(uts));
  912. /* Test removal of node / */
  913. ut_assertok(console_record_reset_enable());
  914. ut_assertok(run_commandf("fdt rm /"));
  915. ut_asserteq(1, run_commandf("fdt print /"));
  916. ut_assertok(ut_check_console_end(uts));
  917. return 0;
  918. }
  919. FDT_TEST(fdt_test_rm, UT_TESTF_CONSOLE_REC);
  920. static int fdt_test_bootcpu(struct unit_test_state *uts)
  921. {
  922. char fdt[256];
  923. ulong addr;
  924. int i;
  925. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  926. addr = map_to_sysmem(fdt);
  927. set_working_fdt_addr(addr);
  928. /* Test getting default bootcpu entry */
  929. ut_assertok(console_record_reset_enable());
  930. ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
  931. ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
  932. ut_assertok(ut_check_console_end(uts));
  933. /* Test setting and getting new bootcpu entry, twice, to test overwrite */
  934. for (i = 42; i <= 43; i++) {
  935. ut_assertok(console_record_reset_enable());
  936. ut_assertok(run_commandf("fdt bootcpu %d", i));
  937. ut_assertok(ut_check_console_end(uts));
  938. /* Test getting new bootcpu entry */
  939. ut_assertok(console_record_reset_enable());
  940. ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
  941. ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
  942. ut_assertok(ut_check_console_end(uts));
  943. }
  944. return 0;
  945. }
  946. FDT_TEST(fdt_test_bootcpu, UT_TESTF_CONSOLE_REC);
  947. static int fdt_test_header_get(struct unit_test_state *uts,
  948. const char *field, const unsigned long val)
  949. {
  950. /* Test getting valid header entry */
  951. ut_assertok(console_record_reset_enable());
  952. ut_assertok(run_commandf("fdt header get fvar %s", field));
  953. ut_asserteq(val, env_get_hex("fvar", 0x1234));
  954. ut_assertok(ut_check_console_end(uts));
  955. /* Test getting malformed header entry */
  956. ut_assertok(console_record_reset_enable());
  957. ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
  958. ut_assertok(ut_check_console_end(uts));
  959. return 0;
  960. }
  961. static int fdt_test_header(struct unit_test_state *uts)
  962. {
  963. char fdt[256];
  964. ulong addr;
  965. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  966. addr = map_to_sysmem(fdt);
  967. set_working_fdt_addr(addr);
  968. /* Test header print */
  969. ut_assertok(console_record_reset_enable());
  970. ut_assertok(run_commandf("fdt header"));
  971. ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
  972. ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
  973. ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
  974. ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
  975. ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
  976. ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
  977. ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
  978. ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
  979. ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
  980. ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
  981. ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
  982. ut_assert_nextline_empty();
  983. ut_assertok(ut_check_console_end(uts));
  984. /* Test header get */
  985. fdt_test_header_get(uts, "magic", fdt_magic(fdt));
  986. fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt));
  987. fdt_test_header_get(uts, "off_dt_struct", fdt_off_dt_struct(fdt));
  988. fdt_test_header_get(uts, "off_dt_strings", fdt_off_dt_strings(fdt));
  989. fdt_test_header_get(uts, "off_mem_rsvmap", fdt_off_mem_rsvmap(fdt));
  990. fdt_test_header_get(uts, "version", fdt_version(fdt));
  991. fdt_test_header_get(uts, "last_comp_version", fdt_last_comp_version(fdt));
  992. fdt_test_header_get(uts, "boot_cpuid_phys", fdt_boot_cpuid_phys(fdt));
  993. fdt_test_header_get(uts, "size_dt_strings", fdt_size_dt_strings(fdt));
  994. fdt_test_header_get(uts, "size_dt_struct", fdt_size_dt_struct(fdt));
  995. return 0;
  996. }
  997. FDT_TEST(fdt_test_header, UT_TESTF_CONSOLE_REC);
  998. static int fdt_test_memory_cells(struct unit_test_state *uts,
  999. const unsigned int cells)
  1000. {
  1001. unsigned char *pada, *pads;
  1002. unsigned char *seta, *sets;
  1003. char fdt[8192];
  1004. const int size = sizeof(fdt);
  1005. fdt32_t *regs;
  1006. ulong addr;
  1007. char *spc;
  1008. int i;
  1009. /* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
  1010. ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
  1011. ut_assertnonnull(pada = calloc(12, cells));
  1012. ut_assertnonnull(pads = calloc(12, cells));
  1013. ut_assertnonnull(seta = calloc(12, cells));
  1014. ut_assertnonnull(sets = calloc(12, cells));
  1015. for (i = cells; i >= 1; i--) {
  1016. regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
  1017. regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
  1018. snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
  1019. snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
  1020. spc = (i != 1) ? " " : "";
  1021. snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
  1022. snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
  1023. }
  1024. ut_assertok(fdt_create(fdt, size));
  1025. ut_assertok(fdt_finish_reservemap(fdt));
  1026. ut_assert(fdt_begin_node(fdt, "") >= 0);
  1027. ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
  1028. ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
  1029. ut_assert(fdt_begin_node(fdt, "memory") >= 0);
  1030. ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
  1031. ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
  1032. ut_assertok(fdt_end_node(fdt));
  1033. ut_assertok(fdt_end_node(fdt));
  1034. ut_assertok(fdt_finish(fdt));
  1035. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  1036. addr = map_to_sysmem(fdt);
  1037. set_working_fdt_addr(addr);
  1038. /* Test updating the memory node */
  1039. ut_assertok(console_record_reset_enable());
  1040. ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
  1041. ut_assertok(run_commandf("fdt print /memory"));
  1042. ut_assert_nextline("memory {");
  1043. ut_assert_nextline("\tdevice_type = \"memory\";");
  1044. ut_assert_nextline("\treg = <%s %s>;", pada, pads);
  1045. ut_assert_nextline("};");
  1046. ut_assertok(ut_check_console_end(uts));
  1047. free(sets);
  1048. free(seta);
  1049. free(pads);
  1050. free(pada);
  1051. free(regs);
  1052. return 0;
  1053. }
  1054. static int fdt_test_memory(struct unit_test_state *uts)
  1055. {
  1056. /*
  1057. * Test memory fixup for 32 and 64 bit systems, anything bigger is
  1058. * so far unsupported and fails because of simple_stroull() being
  1059. * 64bit tops in the 'fdt memory' command implementation.
  1060. */
  1061. fdt_test_memory_cells(uts, 1);
  1062. fdt_test_memory_cells(uts, 2);
  1063. /*
  1064. * The 'fdt memory' command is limited to /memory node, it does
  1065. * not support any other valid DT memory node format, which is
  1066. * either one or multiple /memory@adresss nodes. Therefore, this
  1067. * DT variant is not tested here.
  1068. */
  1069. return 0;
  1070. }
  1071. FDT_TEST(fdt_test_memory, UT_TESTF_CONSOLE_REC);
  1072. static int fdt_test_rsvmem(struct unit_test_state *uts)
  1073. {
  1074. char fdt[8192];
  1075. ulong addr;
  1076. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  1077. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  1078. fdt_add_mem_rsv(fdt, 0x42, 0x1701);
  1079. fdt_add_mem_rsv(fdt, 0x74656, 0x9);
  1080. addr = map_to_sysmem(fdt);
  1081. set_working_fdt_addr(addr);
  1082. /* Test default reserved memory node presence */
  1083. ut_assertok(console_record_reset_enable());
  1084. ut_assertok(run_commandf("fdt rsvmem print"));
  1085. ut_assert_nextline("index\t\t start\t\t size");
  1086. ut_assert_nextline("------------------------------------------------");
  1087. ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
  1088. ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
  1089. ut_assertok(ut_check_console_end(uts));
  1090. /* Test add new reserved memory node */
  1091. ut_assertok(console_record_reset_enable());
  1092. ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
  1093. ut_assertok(run_commandf("fdt rsvmem print"));
  1094. ut_assert_nextline("index\t\t start\t\t size");
  1095. ut_assert_nextline("------------------------------------------------");
  1096. ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x42, 0x1701);
  1097. ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x74656, 0x9);
  1098. ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x1234, 0x5678);
  1099. ut_assertok(ut_check_console_end(uts));
  1100. /* Test delete reserved memory node */
  1101. ut_assertok(console_record_reset_enable());
  1102. ut_assertok(run_commandf("fdt rsvmem delete 0"));
  1103. ut_assertok(run_commandf("fdt rsvmem print"));
  1104. ut_assert_nextline("index\t\t start\t\t size");
  1105. ut_assert_nextline("------------------------------------------------");
  1106. ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
  1107. ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
  1108. ut_assertok(ut_check_console_end(uts));
  1109. /* Test re-add new reserved memory node */
  1110. ut_assertok(console_record_reset_enable());
  1111. ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
  1112. ut_assertok(run_commandf("fdt rsvmem print"));
  1113. ut_assert_nextline("index\t\t start\t\t size");
  1114. ut_assert_nextline("------------------------------------------------");
  1115. ut_assert_nextline(" %x\t%016x\t%016x", 0, 0x74656, 0x9);
  1116. ut_assert_nextline(" %x\t%016x\t%016x", 1, 0x1234, 0x5678);
  1117. ut_assert_nextline(" %x\t%016x\t%016x", 2, 0x42, 0x1701);
  1118. ut_assertok(ut_check_console_end(uts));
  1119. /* Test delete nonexistent reserved memory node */
  1120. ut_assertok(console_record_reset_enable());
  1121. ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
  1122. ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
  1123. ut_assertok(ut_check_console_end(uts));
  1124. return 0;
  1125. }
  1126. FDT_TEST(fdt_test_rsvmem, UT_TESTF_CONSOLE_REC);
  1127. static int fdt_test_chosen(struct unit_test_state *uts)
  1128. {
  1129. const char *env_bootargs = env_get("bootargs");
  1130. char fdt[8192];
  1131. ulong addr;
  1132. ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt)));
  1133. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  1134. addr = map_to_sysmem(fdt);
  1135. set_working_fdt_addr(addr);
  1136. /* Test default chosen node presence, fail as there is no /chosen node */
  1137. ut_assertok(console_record_reset_enable());
  1138. ut_asserteq(1, run_commandf("fdt print /chosen"));
  1139. ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
  1140. ut_assertok(ut_check_console_end(uts));
  1141. /* Test add new chosen node without initrd */
  1142. ut_assertok(console_record_reset_enable());
  1143. ut_assertok(run_commandf("fdt chosen"));
  1144. ut_assertok(run_commandf("fdt print /chosen"));
  1145. ut_assert_nextline("chosen {");
  1146. ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
  1147. if (env_bootargs)
  1148. ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
  1149. ut_assert_nextline("};");
  1150. ut_assertok(ut_check_console_end(uts));
  1151. /* Test add new chosen node with initrd */
  1152. ut_assertok(console_record_reset_enable());
  1153. ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
  1154. ut_assertok(run_commandf("fdt print /chosen"));
  1155. ut_assert_nextline("chosen {");
  1156. ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
  1157. upper_32_bits(0x1234 + 0x5678 - 1),
  1158. lower_32_bits(0x1234 + 0x5678 - 1));
  1159. ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
  1160. upper_32_bits(0x1234), lower_32_bits(0x1234));
  1161. ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
  1162. if (env_bootargs)
  1163. ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
  1164. ut_assert_nextline("};");
  1165. ut_assertok(ut_check_console_end(uts));
  1166. return 0;
  1167. }
  1168. FDT_TEST(fdt_test_chosen, UT_TESTF_CONSOLE_REC);
  1169. static int fdt_test_apply(struct unit_test_state *uts)
  1170. {
  1171. char fdt[8192], fdto[8192];
  1172. ulong addr, addro;
  1173. /* Create base DT with __symbols__ node */
  1174. ut_assertok(fdt_create(fdt, sizeof(fdt)));
  1175. ut_assertok(fdt_finish_reservemap(fdt));
  1176. ut_assert(fdt_begin_node(fdt, "") >= 0);
  1177. ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
  1178. ut_assertok(fdt_end_node(fdt));
  1179. ut_assertok(fdt_end_node(fdt));
  1180. ut_assertok(fdt_finish(fdt));
  1181. fdt_shrink_to_minimum(fdt, 4096); /* Resize with 4096 extra bytes */
  1182. addr = map_to_sysmem(fdt);
  1183. set_working_fdt_addr(addr);
  1184. /* Create DTO which adds single property to root node / */
  1185. ut_assertok(fdt_create(fdto, sizeof(fdto)));
  1186. ut_assertok(fdt_finish_reservemap(fdto));
  1187. ut_assert(fdt_begin_node(fdto, "") >= 0);
  1188. ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
  1189. ut_assertok(fdt_property_string(fdto, "target-path", "/"));
  1190. ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
  1191. ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
  1192. ut_assertok(fdt_end_node(fdto));
  1193. ut_assertok(fdt_end_node(fdto));
  1194. ut_assertok(fdt_finish(fdto));
  1195. addro = map_to_sysmem(fdto);
  1196. /* Test default DT print */
  1197. ut_assertok(console_record_reset_enable());
  1198. ut_assertok(run_commandf("fdt print /"));
  1199. ut_assert_nextline("/ {");
  1200. ut_assert_nextline("\t__symbols__ {");
  1201. ut_assert_nextline("\t};");
  1202. ut_assert_nextline("};");
  1203. ut_assertok(ut_check_console_end(uts));
  1204. /* Test simple DTO application */
  1205. ut_assertok(console_record_reset_enable());
  1206. ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
  1207. ut_assertok(run_commandf("fdt print /"));
  1208. ut_assert_nextline("/ {");
  1209. ut_assert_nextline("\tnewstring = \"newvalue\";");
  1210. ut_assert_nextline("\t__symbols__ {");
  1211. ut_assert_nextline("\t};");
  1212. ut_assert_nextline("};");
  1213. ut_assertok(ut_check_console_end(uts));
  1214. /*
  1215. * Create complex DTO which:
  1216. * - modifies newstring property in root node /
  1217. * - adds new properties to root node /
  1218. * - adds new subnode with properties to root node /
  1219. * - adds phandle to the subnode and therefore __symbols__ node
  1220. */
  1221. ut_assertok(fdt_create(fdto, sizeof(fdto)));
  1222. ut_assertok(fdt_finish_reservemap(fdto));
  1223. ut_assert(fdt_begin_node(fdto, "") >= 0);
  1224. ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
  1225. ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
  1226. ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
  1227. ut_assertok(fdt_property_string(fdto, "target-path", "/"));
  1228. ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
  1229. ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
  1230. ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
  1231. ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
  1232. ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
  1233. ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
  1234. ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
  1235. ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
  1236. ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
  1237. ut_assertok(fdt_end_node(fdto));
  1238. ut_assertok(fdt_end_node(fdto));
  1239. ut_assertok(fdt_end_node(fdto));
  1240. ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
  1241. ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
  1242. ut_assertok(fdt_end_node(fdto));
  1243. ut_assertok(fdt_finish(fdto));
  1244. addro = map_to_sysmem(fdto);
  1245. /* Test complex DTO application */
  1246. ut_assertok(console_record_reset_enable());
  1247. ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
  1248. ut_assertok(run_commandf("fdt print /"));
  1249. ut_assert_nextline("/ {");
  1250. ut_assert_nextline("\tempty-property;");
  1251. ut_assert_nextline("\tnewu32 = <0x12345678>;");
  1252. ut_assert_nextline("\tnewstring = \"newervalue\";");
  1253. ut_assert_nextline("\tsubnode {");
  1254. ut_assert_nextline("\t\tphandle = <0x00000001>;");
  1255. ut_assert_nextline("\t\tsubempty-property;");
  1256. ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
  1257. ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
  1258. ut_assert_nextline("\t};");
  1259. ut_assert_nextline("\t__symbols__ {");
  1260. ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
  1261. ut_assert_nextline("\t};");
  1262. ut_assert_nextline("};");
  1263. ut_assertok(ut_check_console_end(uts));
  1264. /*
  1265. * Create complex DTO which:
  1266. * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
  1267. */
  1268. ut_assertok(fdt_create(fdto, sizeof(fdto)));
  1269. ut_assertok(fdt_finish_reservemap(fdto));
  1270. ut_assert(fdt_begin_node(fdto, "") >= 0);
  1271. ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
  1272. ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));
  1273. ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
  1274. ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
  1275. ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
  1276. ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
  1277. ut_assertok(fdt_end_node(fdto));
  1278. ut_assertok(fdt_end_node(fdto));
  1279. ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
  1280. ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
  1281. ut_assertok(fdt_end_node(fdto));
  1282. ut_assertok(fdt_end_node(fdto));
  1283. ut_assertok(fdt_finish(fdto));
  1284. addro = map_to_sysmem(fdto);
  1285. /* Test complex DTO application */
  1286. ut_assertok(console_record_reset_enable());
  1287. ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
  1288. ut_assertok(run_commandf("fdt print /"));
  1289. ut_assert_nextline("/ {");
  1290. ut_assert_nextline("\tempty-property;");
  1291. ut_assert_nextline("\tnewu32 = <0x12345678>;");
  1292. ut_assert_nextline("\tnewstring = \"newervalue\";");
  1293. ut_assert_nextline("\tsubnode {");
  1294. ut_assert_nextline("\t\tphandle = <0x00000001>;");
  1295. ut_assert_nextline("\t\tsubempty-property;");
  1296. ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
  1297. ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
  1298. ut_assert_nextline("\t};");
  1299. ut_assert_nextline("\t__symbols__ {");
  1300. ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
  1301. ut_assert_nextline("\t};");
  1302. ut_assert_nextline("};");
  1303. ut_assertok(ut_check_console_end(uts));
  1304. return 0;
  1305. }
  1306. FDT_TEST(fdt_test_apply, UT_TESTF_CONSOLE_REC);
  1307. int do_ut_fdt(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  1308. {
  1309. struct unit_test *tests = UNIT_TEST_SUITE_START(fdt_test);
  1310. const int n_ents = UNIT_TEST_SUITE_COUNT(fdt_test);
  1311. return cmd_ut_category("fdt", "fdt_test_", tests, n_ents, argc, argv);
  1312. }