Loading...
   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
// SPDX-License-Identifier: GPL-2.0+
/*
 * Tests for fdt command
 *
 * Copyright 2022 Google LLC
 */

#include <console.h>
#include <env.h>
#include <fdt_support.h>
#include <mapmem.h>
#include <asm/global_data.h>
#include <linux/libfdt.h>
#include <test/ut.h>

DECLARE_GLOBAL_DATA_PTR;
/*
 * Missing tests:
 * fdt boardsetup         - Do board-specific set up
 * fdt checksign [<addr>] - check FIT signature
 *                          <addr> - address of key blob
 *                                   default gd->fdt_blob
 */

/* Declare a new fdt test */
#define FDT_TEST(_name, _flags)	UNIT_TEST(_name, _flags, fdt)

/**
 * make_test_fdt() - Create an FDT with just a root node
 *
 * The size is set to the minimum needed. This also sets the working FDT and
 * checks that the expected output is received from doing so.
 *
 * @uts: Test state
 * @fdt: Place to write FDT
 * @size: Maximum size of space for fdt
 * @addrp: Returns address of the devicetree
 */
static int make_test_fdt(struct unit_test_state *uts, void *fdt, int size,
			 ulong *addrp)
{
	ulong addr;

	ut_assertok(fdt_create(fdt, size));
	ut_assertok(fdt_finish_reservemap(fdt));
	ut_assert(fdt_begin_node(fdt, "") >= 0);
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_finish(fdt));

	addr = map_to_sysmem(fdt);
	set_working_fdt_addr(addr);
	ut_assert_nextline("Working FDT set to %lx", addr);
	*addrp = addr;

	return 0;
}

/**
 * make_fuller_fdt() - Create an FDT with root node and properties
 *
 * The size is set to the minimum needed. This also sets the working FDT and
 * checks that the expected output is received from doing so.
 *
 * @uts: Test state
 * @fdt: Place to write FDT
 * @size: Maximum size of space for fdt
 * @addrp: Returns address of the devicetree
 */
static int make_fuller_fdt(struct unit_test_state *uts, void *fdt, int size,
			   ulong *addrp)
{
	fdt32_t regs[2] = { cpu_to_fdt32(0x1234), cpu_to_fdt32(0x1000) };
	ulong addr;

	/*
	 * Assemble the following DT for test purposes:
	 *
	 * / {
	 * 	#address-cells = <0x00000001>;
	 * 	#size-cells = <0x00000001>;
	 * 	compatible = "u-boot,fdt-test";
	 * 	model = "U-Boot FDT test";
	 *
	 *	aliases {
	 *		badalias = "/bad/alias";
	 *		subnodealias = "/test-node@1234/subnode";
	 *		testnodealias = "/test-node@1234";
	 *	};
	 *
	 * 	test-node@1234 {
	 * 		#address-cells = <0x00000000>;
	 * 		#size-cells = <0x00000000>;
	 * 		compatible = "u-boot,fdt-test-device1";
	 * 		clock-names = "fixed", "i2c", "spi", "uart2", "uart1";
	 * 		u-boot,empty-property;
	 * 		clock-frequency = <0x00fde800>;
	 * 		regs = <0x00001234 0x00001000>;
	 *
	 * 		subnode {
	 * 			#address-cells = <0x00000000>;
	 * 			#size-cells = <0x00000000>;
	 * 			compatible = "u-boot,fdt-subnode-test-device";
	 * 		};
	 * 	};
	 * };
	 */

	ut_assertok(fdt_create(fdt, size));
	ut_assertok(fdt_finish_reservemap(fdt));
	ut_assert(fdt_begin_node(fdt, "") >= 0);

	ut_assertok(fdt_property_u32(fdt, "#address-cells", 1));
	ut_assertok(fdt_property_u32(fdt, "#size-cells", 1));
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test"));
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "model", "U-Boot FDT test"));

	ut_assert(fdt_begin_node(fdt, "aliases") >= 0);
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "badalias", "/bad/alias"));
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "subnodealias", "/test-node@1234/subnode"));
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "testnodealias", "/test-node@1234"));
	ut_assertok(fdt_end_node(fdt));

	ut_assert(fdt_begin_node(fdt, "test-node@1234") >= 0);
	ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
	ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
	/* <string> */
	ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-test-device1"));
	/* <stringlist> */
	ut_assertok(fdt_property(fdt, "clock-names", "fixed\0i2c\0spi\0uart2\0uart1\0", 26));
	/* <empty> */
	ut_assertok(fdt_property(fdt, "u-boot,empty-property", NULL, 0));
	/*
	 * <u32>
	 * This value is deliberate as it used to break cmd/fdt.c
	 * fdt_printable_str() implementation.
	 */
	ut_assertok(fdt_property_u32(fdt, "clock-frequency", 16640000));
	/* <prop-encoded-array> */
	ut_assertok(fdt_property(fdt, "regs", &regs, sizeof(regs)));
	ut_assert(fdt_begin_node(fdt, "subnode") >= 0);
	ut_assertok(fdt_property_cell(fdt, "#address-cells", 0));
	ut_assertok(fdt_property_cell(fdt, "#size-cells", 0));
	ut_assertok(fdt_property_string(fdt, "compatible", "u-boot,fdt-subnode-test-device"));
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_end_node(fdt));

	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_finish(fdt));

	addr = map_to_sysmem(fdt);
	set_working_fdt_addr(addr);
	ut_assert_nextline("Working FDT set to %lx", addr);
	*addrp = addr;

	return 0;
}

/* Test 'fdt addr' getting/setting address */
static int fdt_test_addr(struct unit_test_state *uts)
{
	const void *fdt_blob, *new_fdt;
	char fdt[256];
	ulong addr;
	int ret;

	ut_assertok(run_command("fdt addr -c", 0));
	ut_assert_nextline("Control fdt: %08lx",
			   (ulong)map_to_sysmem(gd->fdt_blob));
	ut_assert_console_end();

	/* The working fdt is not set, so this should fail */
	set_working_fdt_addr(0);
	ut_assert_nextline("Working FDT set to 0");
	ut_asserteq(CMD_RET_FAILURE, run_command("fdt addr", 0));

	/*
	 * sandbox fails the check for !blob since the 0 pointer is mapped to
	 * memory somewhere other than at 0x0
	 */
	if (IS_ENABLED(CONFIG_SANDBOX))
		ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
	ut_assert_console_end();

	/* Set up a working FDT and try again */
	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));
	ut_assertok(run_command("fdt addr", 0));
	ut_assert_nextline("Working fdt: %08lx", (ulong)map_to_sysmem(fdt));
	ut_assert_console_end();

	/* Set the working FDT */
	set_working_fdt_addr(0);
	ut_assert_nextline("Working FDT set to 0");
	ut_assertok(run_commandf("fdt addr %08lx", addr));
	ut_assert_nextline("Working FDT set to %lx", addr);
	ut_asserteq(addr, map_to_sysmem(working_fdt));
	ut_assert_console_end();
	set_working_fdt_addr(0);
	ut_assert_nextline("Working FDT set to 0");

	/* Set the control FDT */
	fdt_blob = gd->fdt_blob;
	gd->fdt_blob = NULL;
	ret = run_commandf("fdt addr -c %08lx", addr);
	new_fdt = gd->fdt_blob;
	gd->fdt_blob = fdt_blob;
	ut_assertok(ret);
	ut_asserteq(addr, map_to_sysmem(new_fdt));
	ut_assert_console_end();

	/* Test setting an invalid FDT */
	fdt[0] = 123;
	ut_asserteq(1, run_commandf("fdt addr %08lx", addr));
	ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
	ut_assert_console_end();

	/* Test detecting an invalid FDT */
	fdt[0] = 123;
	set_working_fdt_addr(addr);
	ut_assert_nextline("Working FDT set to %lx", addr);
	ut_asserteq(1, run_commandf("fdt addr"));
	ut_assert_nextline("libfdt fdt_check_header(): FDT_ERR_BADMAGIC");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_addr, UTF_CONSOLE);

/* Test 'fdt addr' resizing an fdt */
static int fdt_test_addr_resize(struct unit_test_state *uts)
{
	char fdt[256];
	const int newsize = sizeof(fdt) / 2;
	ulong addr;

	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test setting and resizing the working FDT to a larger size */
	ut_assertok(run_commandf("fdt addr %08lx %x", addr, newsize));
	ut_assert_nextline("Working FDT set to %lx", addr);
	ut_assert_console_end();

	/* Try shrinking it */
	ut_assertok(run_commandf("fdt addr %08lx %zx", addr, sizeof(fdt) / 4));
	ut_assert_nextline("Working FDT set to %lx", addr);
	ut_assert_nextline("New length %d < existing length %d, ignoring",
			   (int)sizeof(fdt) / 4, newsize);
	ut_assert_console_end();

	/* ...quietly */
	ut_assertok(run_commandf("fdt addr -q %08lx %zx", addr, sizeof(fdt) / 4));
	ut_assert_console_end();

	/* We cannot easily provoke errors in fdt_open_into(), so ignore that */

	return 0;
}
FDT_TEST(fdt_test_addr_resize, UTF_CONSOLE);

static int fdt_test_move(struct unit_test_state *uts)
{
	char fdt[256];
	ulong addr, newaddr = 0x10000;
	const int size = sizeof(fdt);
	uint32_t ts;
	void *buf;

	/* Original source DT */
	ut_assertok(make_test_fdt(uts, fdt, size, &addr));
	ts = fdt_totalsize(fdt);

	/* Moved target DT location */
	buf = map_sysmem(newaddr, size);
	memset(buf, 0, size);

	/* Test moving the working FDT to a new location */
	ut_assertok(run_commandf("fdt move %08lx %08lx %x", addr, newaddr, ts));
	ut_assert_nextline("Working FDT set to %lx", newaddr);
	ut_assert_console_end();

	/* Compare the source and destination DTs */
	ut_assertok(run_commandf("cmp.b %08lx %08lx %x", addr, newaddr, ts));
	ut_assert_nextline("Total of %d byte(s) were the same", ts);
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_move, UTF_CONSOLE);

static int fdt_test_resize(struct unit_test_state *uts)
{
	char fdt[256];
	const unsigned int newsize = 0x2000;
	uint32_t ts;
	ulong addr;

	/* Original source DT */
	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));
	fdt_shrink_to_minimum(fdt, 0);	/* Resize with 0 extra bytes */
	ts = fdt_totalsize(fdt);

	/* Test resizing the working FDT and verify the new space was added */
	ut_assertok(run_commandf("fdt resize %x", newsize));
	ut_asserteq(ts + newsize, fdt_totalsize(fdt));
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_resize, UTF_CONSOLE);

static int fdt_test_print_list_common(struct unit_test_state *uts,
				      const char *opc, const char *node)
{
	/*
	 * Test printing/listing the working FDT
	 * subnode $node/subnode
	 */
	ut_assertok(run_commandf("fdt %s %s/subnode", opc, node));
	ut_assert_nextline("subnode {");
	ut_assert_nextline("\t#address-cells = <0x00000000>;");
	ut_assert_nextline("\t#size-cells = <0x00000000>;");
	ut_assert_nextline("\tcompatible = \"u-boot,fdt-subnode-test-device\";");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path / string property model
	 */
	ut_assertok(run_commandf("fdt %s / model", opc));
	ut_assert_nextline("model = \"U-Boot FDT test\"");
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path $node string property compatible
	 */
	ut_assertok(run_commandf("fdt %s %s compatible", opc, node));
	ut_assert_nextline("compatible = \"u-boot,fdt-test-device1\"");
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path $node stringlist property clock-names
	 */
	ut_assertok(run_commandf("fdt %s %s clock-names", opc, node));
	ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path $node u32 property clock-frequency
	 */
	ut_assertok(run_commandf("fdt %s %s clock-frequency", opc, node));
	ut_assert_nextline("clock-frequency = <0x00fde800>");
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path $node empty property u-boot,empty-property
	 */
	ut_assertok(run_commandf("fdt %s %s u-boot,empty-property", opc, node));
	/*
	 * This is the only 'fdt print' / 'fdt list' incantation which
	 * prefixes the property with node path. This has been in U-Boot
	 * since the beginning of the command 'fdt', keep it.
	 */
	ut_assert_nextline("%s u-boot,empty-property", node);
	ut_assert_console_end();

	/*
	 * Test printing/listing the working FDT
	 * path $node prop-encoded array property regs
	 */
	ut_assertok(run_commandf("fdt %s %s regs", opc, node));
	ut_assert_nextline("regs = <0x00001234 0x00001000>");
	ut_assert_console_end();

	return 0;
}

static int fdt_test_print_list(struct unit_test_state *uts, bool print)
{
	const char *opc = print ? "print" : "list";
	char fdt[4096];
	ulong addr;
	int ret;

	/* Original source DT */
	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test printing/listing the working FDT -- node / */
	ut_assertok(run_commandf("fdt %s", opc));
	ut_assert_nextline("/ {");
	ut_assert_nextline("\t#address-cells = <0x00000001>;");
	ut_assert_nextline("\t#size-cells = <0x00000001>;");
	ut_assert_nextline("\tcompatible = \"u-boot,fdt-test\";");
	ut_assert_nextline("\tmodel = \"U-Boot FDT test\";");
	ut_assert_nextline("\taliases {");
	if (print) {
		ut_assert_nextline("\t\tbadalias = \"/bad/alias\";");
		ut_assert_nextline("\t\tsubnodealias = \"/test-node@1234/subnode\";");
		ut_assert_nextline("\t\ttestnodealias = \"/test-node@1234\";");
	}
	ut_assert_nextline("\t};");
	ut_assert_nextline("\ttest-node@1234 {");
	if (print) {
		ut_assert_nextline("\t\t#address-cells = <0x00000000>;");
		ut_assert_nextline("\t\t#size-cells = <0x00000000>;");
		ut_assert_nextline("\t\tcompatible = \"u-boot,fdt-test-device1\";");
		ut_assert_nextline("\t\tclock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\";");
		ut_assert_nextline("\t\tu-boot,empty-property;");
		ut_assert_nextline("\t\tclock-frequency = <0x00fde800>;");
		ut_assert_nextline("\t\tregs = <0x00001234 0x00001000>;");
		ut_assert_nextline("\t\tsubnode {");
		ut_assert_nextline("\t\t\t#address-cells = <0x00000000>;");
		ut_assert_nextline("\t\t\t#size-cells = <0x00000000>;");
		ut_assert_nextline("\t\t\tcompatible = \"u-boot,fdt-subnode-test-device\";");
		ut_assert_nextline("\t\t};");
	}
	ut_assert_nextline("\t};");
	ut_assert_nextline("};");
	ut_assert_console_end();

	ret = fdt_test_print_list_common(uts, opc, "/test-node@1234");
	if (!ret)
		ret = fdt_test_print_list_common(uts, opc, "testnodealias");

	return 0;
}

static int fdt_test_print(struct unit_test_state *uts)
{
	return fdt_test_print_list(uts, true);
}
FDT_TEST(fdt_test_print, UTF_CONSOLE);

static int fdt_test_list(struct unit_test_state *uts)
{
	return fdt_test_print_list(uts, false);
}
FDT_TEST(fdt_test_list, UTF_CONSOLE);

/* Test 'fdt get value' reading an fdt */
static int fdt_test_get_value_string(struct unit_test_state *uts,
				     const char *node, const char *prop,
				     const char *idx,  const char *strres,
				     const int intres)
{
	ut_assertok(run_commandf("fdt get value var %s %s %s",
				 node, prop, idx ? : ""));
	if (strres)
		ut_asserteq_str(strres, env_get("var"));
	else
		ut_asserteq(intres, env_get_hex("var", 0x1234));
	ut_assert_console_end();

	return 0;
}

static int fdt_test_get_value_common(struct unit_test_state *uts,
				     const char *node)
{
	/* Test getting default element of $node node clock-names property */
	ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", NULL,
					      "fixed", 0));

	/* Test getting 0th element of $node node clock-names property */
	ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "0",
					      "fixed", 0));

	/* Test getting 1st element of $node node clock-names property */
	ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "1",
					      "i2c", 0));

	/* Test getting 2nd element of $node node clock-names property */
	ut_assertok(fdt_test_get_value_string(uts, node, "clock-names", "2",
					      "spi", 0));

	/*
	 * Test getting default element of $node node regs property.
	 * The result here is highly unusual, the non-index value read from
	 * integer array is a string of concatenated values from the array,
	 * but only if the array is shorter than 40 characters. Anything
	 * longer is an error. This is a special case for handling hashes.
	 */
	ut_assertok(fdt_test_get_value_string(uts, node, "regs", NULL,
					      "3412000000100000", 0));

	/* Test getting 0th element of $node node regs property */
	ut_assertok(fdt_test_get_value_string(uts, node, "regs", "0", NULL,
					      0x1234));

	/* Test getting 1st element of $node node regs property */
	ut_assertok(fdt_test_get_value_string(uts, node, "regs", "1", NULL,
					      0x1000));

	/* Test missing 10th element of $node node clock-names property */
	ut_asserteq(1, run_commandf("fdt get value ften %s clock-names 10", node));
	ut_assert_console_end();

	/* Test missing 10th element of $node node regs property */
	ut_asserteq(1, run_commandf("fdt get value ften %s regs 10", node));
	ut_assert_console_end();

	/* Test getting default element of $node node nonexistent property */
	ut_asserteq(1, run_commandf("fdt get value fnone %s nonexistent", node));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	return 0;
}

static int fdt_test_get_value(struct unit_test_state *uts)
{
	char fdt[4096];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	ut_assertok(fdt_test_get_value_common(uts, "/test-node@1234"));
	ut_assertok(fdt_test_get_value_common(uts, "testnodealias"));

	/* Test getting default element of /nonexistent node */
	ut_asserteq(1, run_command("fdt get value fnode /nonexistent nonexistent", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting default element of bad alias */
	ut_asserteq(1, run_command("fdt get value vbadalias badalias nonexistent", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting default element of nonexistent alias */
	ut_asserteq(1, run_command("fdt get value vnoalias noalias nonexistent", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_get_value, UTF_CONSOLE);

static int fdt_test_get_name(struct unit_test_state *uts)
{
	char fdt[4096];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test getting name of node 0 in /, which is /aliases node */
	ut_assertok(run_command("fdt get name nzero / 0", 0));
	ut_asserteq_str("aliases", env_get("nzero"));
	ut_assert_console_end();

	/* Test getting name of node 1 in /, which is /test-node@1234 node */
	ut_assertok(run_command("fdt get name none / 1", 0));
	ut_asserteq_str("test-node@1234", env_get("none"));
	ut_assert_console_end();

	/* Test getting name of node -1 in /, which is /aliases node, same as 0 */
	ut_assertok(run_command("fdt get name nmone / -1", 0));
	ut_asserteq_str("aliases", env_get("nmone"));
	ut_assert_console_end();

	/* Test getting name of node 2 in /, which does not exist */
	ut_asserteq(1, run_command("fdt get name ntwo / 2", 1));
	ut_assert_nextline("libfdt node not found");
	ut_assert_console_end();

	/* Test getting name of node 0 in /test-node@1234, which is /subnode node */
	ut_assertok(run_command("fdt get name snzero /test-node@1234 0", 0));
	ut_asserteq_str("subnode", env_get("snzero"));
	ut_assertok(run_command("fdt get name asnzero testnodealias 0", 0));
	ut_asserteq_str("subnode", env_get("asnzero"));
	ut_assert_console_end();

	/* Test getting name of node 1 in /test-node@1234, which does not exist */
	ut_asserteq(1, run_command("fdt get name snone /test-node@1234 1", 1));
	ut_assert_nextline("libfdt node not found");
	ut_asserteq(1, run_command("fdt get name asnone testnodealias 1", 1));
	ut_assert_nextline("libfdt node not found");
	ut_assert_console_end();

	/* Test getting name of node -1 in /test-node@1234, which is /subnode node, same as 0 */
	ut_assertok(run_command("fdt get name snmone /test-node@1234 -1", 0));
	ut_asserteq_str("subnode", env_get("snmone"));
	ut_assertok(run_command("fdt get name asnmone testnodealias -1", 0));
	ut_asserteq_str("subnode", env_get("asnmone"));
	ut_assert_console_end();

	/* Test getting name of nonexistent node */
	ut_asserteq(1, run_command("fdt get name nonode /nonexistent 0", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting name of bad alias */
	ut_asserteq(1, run_command("fdt get name vbadalias badalias 0", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting name of nonexistent alias */
	ut_asserteq(1, run_command("fdt get name vnoalias noalias 0", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_get_name, UTF_CONSOLE);

static int fdt_test_get_addr_common(struct unit_test_state *uts, char *fdt,
				    const char *path, const char *prop)
{
	unsigned int offset;
	int path_offset;
	void *prop_ptr;
	int len = 0;

	path_offset = fdt_path_offset(fdt, path);
	ut_assert(path_offset >= 0);
	prop_ptr = (void *)fdt_getprop(fdt, path_offset, prop, &len);
	ut_assertnonnull(prop_ptr);
	offset = (char *)prop_ptr - fdt;

	ut_assertok(run_commandf("fdt get addr pstr %s %s", path, prop));
	ut_asserteq((ulong)map_sysmem(env_get_hex("fdtaddr", 0x1234), 0),
		    (ulong)(map_sysmem(env_get_hex("pstr", 0x1234), 0) - offset));
	ut_assert_console_end();

	return 0;
}

static int fdt_test_get_addr(struct unit_test_state *uts)
{
	char fdt[4096];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test getting address of root node / string property "compatible" */
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "/", "compatible"));

	/* Test getting address of node /test-node@1234 stringlist property "clock-names" */
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
					     "clock-names"));
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
					     "clock-names"));

	/* Test getting address of node /test-node@1234 u32 property "clock-frequency" */
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
					     "clock-frequency"));
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
					     "clock-frequency"));

	/* Test getting address of node /test-node@1234 empty property "u-boot,empty-property" */
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
					     "u-boot,empty-property"));
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
					     "u-boot,empty-property"));

	/* Test getting address of node /test-node@1234 array property "regs" */
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "/test-node@1234",
					     "regs"));
	ut_assertok(fdt_test_get_addr_common(uts, fdt, "testnodealias",
					     "regs"));

	/* Test getting address of node /test-node@1234/subnode non-existent property "noprop" */
	ut_asserteq(1, run_command("fdt get addr pnoprop /test-node@1234/subnode noprop", 1));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting address of non-existent node /test-node@1234/nonode@1 property "noprop" */
	ut_asserteq(1, run_command("fdt get addr pnonode /test-node@1234/nonode@1 noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_get_addr, UTF_CONSOLE);

static int fdt_test_get_size_common(struct unit_test_state *uts,
				     const char *path, const char *prop,
				     const unsigned int val)
{
	if (prop) {
		ut_assertok(run_commandf("fdt get size sstr %s %s", path, prop));
	} else {
		ut_assertok(run_commandf("fdt get size sstr %s", path));
	}
	ut_asserteq(val, env_get_hex("sstr", 0x1234));
	ut_assert_console_end();

	return 0;
}

static int fdt_test_get_size(struct unit_test_state *uts)
{
	char fdt[4096];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test getting size of root node / string property "compatible" */
	ut_assertok(fdt_test_get_size_common(uts, "/", "compatible", 16));

	/* Test getting size of node /test-node@1234 stringlist property "clock-names" */
	ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
					     "clock-names", 26));
	ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
					     "clock-names", 26));

	/* Test getting size of node /test-node@1234 u32 property "clock-frequency" */
	ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
					     "clock-frequency", 4));
	ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
					     "clock-frequency", 4));

	/* Test getting size of node /test-node@1234 empty property "u-boot,empty-property" */
	ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234",
					     "u-boot,empty-property", 0));
	ut_assertok(fdt_test_get_size_common(uts, "testnodealias",
					     "u-boot,empty-property", 0));

	/* Test getting size of node /test-node@1234 array property "regs" */
	ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234", "regs",
					     8));
	ut_assertok(fdt_test_get_size_common(uts, "testnodealias", "regs", 8));

	/* Test getting node count of node / */
	ut_assertok(fdt_test_get_size_common(uts, "/", NULL, 2));

	/* Test getting node count of node /test-node@1234/subnode */
	ut_assertok(fdt_test_get_size_common(uts, "/test-node@1234/subnode",
					     NULL, 0));
	ut_assertok(fdt_test_get_size_common(uts, "subnodealias", NULL, 0));

	/* Test getting size of node /test-node@1234/subnode non-existent property "noprop" */
	ut_asserteq(1, run_command("fdt get size pnoprop /test-node@1234/subnode noprop", 1));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_asserteq(1, run_command("fdt get size pnoprop subnodealias noprop", 1));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting size of non-existent node /test-node@1234/nonode@1 property "noprop" */
	ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1 noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting node count of non-existent node /test-node@1234/nonode@1 */
	ut_asserteq(1, run_command("fdt get size pnonode /test-node@1234/nonode@1", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting node count of bad alias badalias */
	ut_asserteq(1, run_command("fdt get size pnonode badalias noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test getting node count of non-existent alias noalias */
	ut_asserteq(1, run_command("fdt get size pnonode noalias", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_get_size, UTF_CONSOLE);

static int fdt_test_set_single(struct unit_test_state *uts,
			       const char *path, const char *prop,
			       const char *sval, int ival, bool integer)
{
	/*
	 * Set single element string/integer/<empty> property into DT, that is:
	 * => fdt set /path property string
	 * => fdt set /path property integer
	 * => fdt set /path property
	 */
	if (sval)
		ut_assertok(run_commandf("fdt set %s %s %s", path, prop, sval));
	else if (integer)
		ut_assertok(run_commandf("fdt set %s %s <%d>", path, prop, ival));
	else
		ut_assertok(run_commandf("fdt set %s %s", path, prop));

	/* Validate the property is present and has correct value. */
	ut_assertok(run_commandf("fdt get value svar %s %s", path, prop));
	if (sval)
		ut_asserteq_str(sval, env_get("svar"));
	else if (integer)
		ut_asserteq(ival, env_get_hex("svar", 0x1234));
	else
		ut_assertnull(env_get("svar"));
	ut_assert_console_end();

	return 0;
}

static int fdt_test_set_multi(struct unit_test_state *uts,
			      const char *path, const char *prop,
			      const char *sval1, const char *sval2,
			      int ival1, int ival2)
{
	/*
	 * Set multi element string/integer array property in DT, that is:
	 * => fdt set /path property <string1 string2>
	 * => fdt set /path property <integer1 integer2>
	 *
	 * The set is done twice in here deliberately, The first set adds
	 * the property with an extra trailing element in its array to make
	 * the array longer, the second set is the expected final content of
	 * the array property. The longer array is used to verify that the
	 * new array is correctly sized and read past the new array length
	 * triggers failure.
	 */
	if (sval1 && sval2) {
		ut_assertok(run_commandf("fdt set %s %s %s %s end", path, prop, sval1, sval2));
		ut_assertok(run_commandf("fdt set %s %s %s %s", path, prop, sval1, sval2));
	} else {
		ut_assertok(run_commandf("fdt set %s %s <%d %d 10>", path, prop, ival1, ival2));
		ut_assertok(run_commandf("fdt set %s %s <%d %d>", path, prop, ival1, ival2));
	}

	/*
	 * Validate the property is present and has correct value.
	 *
	 * The "end/10" above and "svarn" below is used to validate that
	 * previous 'fdt set' to longer array does not polute newly set
	 * shorter array.
	 */
	ut_assertok(run_commandf("fdt get value svar1 %s %s 0", path, prop));
	ut_assertok(run_commandf("fdt get value svar2 %s %s 1", path, prop));
	ut_asserteq(1, run_commandf("fdt get value svarn %s %s 2", path, prop));
	if (sval1 && sval2) {
		ut_asserteq_str(sval1, env_get("svar1"));
		ut_asserteq_str(sval2, env_get("svar2"));
		ut_assertnull(env_get("svarn"));
	} else {
		ut_asserteq(ival1, env_get_hex("svar1", 0x1234));
		ut_asserteq(ival2, env_get_hex("svar2", 0x1234));
		ut_assertnull(env_get("svarn"));
	}
	ut_assert_console_end();

	return 0;
}

static int fdt_test_set_node(struct unit_test_state *uts,
			     const char *path, const char *prop)
{
	ut_assertok(fdt_test_set_single(uts, path, prop, "new", 0, false));
	ut_assertok(fdt_test_set_single(uts, path, prop, "rewrite", 0, false));
	ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 42, true));
	ut_assertok(fdt_test_set_single(uts, path, prop, NULL, 0, false));
	ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 42, 1701));
	ut_assertok(fdt_test_set_multi(uts, path, prop, NULL, NULL, 74656, 9));
	ut_assertok(fdt_test_set_multi(uts, path, prop, "42", "1701", 0, 0));
	ut_assertok(fdt_test_set_multi(uts, path, prop, "74656", "9", 0, 0));

	return 0;
}

static int fdt_test_set(struct unit_test_state *uts)
{
	char fdt[8192];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */

	/* Test setting of root node / existing property "compatible" */
	ut_assertok(fdt_test_set_node(uts, "/", "compatible"));

	/* Test setting of root node / new property "newproperty" */
	ut_assertok(fdt_test_set_node(uts, "/", "newproperty"));

	/* Test setting of subnode existing property "compatible" */
	ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode",
				      "compatible"));
	ut_assertok(fdt_test_set_node(uts, "subnodealias", "compatible"));

	/* Test setting of subnode new property "newproperty" */
	ut_assertok(fdt_test_set_node(uts, "/test-node@1234/subnode",
				      "newproperty"));
	ut_assertok(fdt_test_set_node(uts, "subnodealias", "newproperty"));

	/* Test setting property of non-existent node */
	ut_asserteq(1, run_command("fdt set /no-node noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test setting property of non-existent alias */
	ut_asserteq(1, run_command("fdt set noalias noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	/* Test setting property of bad alias */
	ut_asserteq(1, run_command("fdt set badalias noprop", 1));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_set, UTF_CONSOLE);

static int fdt_test_mknode(struct unit_test_state *uts)
{
	char fdt[8192];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */

	/* Test creation of new node in / */
	ut_assertok(run_commandf("fdt mknode / newnode"));
	ut_assertok(run_commandf("fdt list /newnode"));
	ut_assert_nextline("newnode {");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/* Test creation of new node in /test-node@1234 */
	ut_assertok(run_commandf("fdt mknode /test-node@1234 newsubnode"));
	ut_assertok(run_commandf("fdt list /test-node@1234/newsubnode"));
	ut_assert_nextline("newsubnode {");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/* Test creation of new node in /test-node@1234 by alias */
	ut_assertok(run_commandf("fdt mknode testnodealias newersubnode"));
	ut_assertok(run_commandf("fdt list testnodealias/newersubnode"));
	ut_assert_nextline("newersubnode {");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/* Test creation of new node in /test-node@1234 over existing node */
	ut_asserteq(1, run_commandf("fdt mknode testnodealias newsubnode"));
	ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
	ut_assert_console_end();

	/* Test creation of new node in /test-node@1234 by alias over existing node */
	ut_asserteq(1, run_commandf("fdt mknode testnodealias newersubnode"));
	ut_assert_nextline("libfdt fdt_add_subnode(): FDT_ERR_EXISTS");
	ut_assert_console_end();

	/* Test creation of new node in non-existent node */
	ut_asserteq(1, run_commandf("fdt mknode /no-node newnosubnode"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test creation of new node in non-existent alias */
	ut_asserteq(1, run_commandf("fdt mknode noalias newfailsubnode"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	/* Test creation of new node in bad alias */
	ut_asserteq(1, run_commandf("fdt mknode badalias newbadsubnode"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_mknode, UTF_CONSOLE);

static int fdt_test_rm(struct unit_test_state *uts)
{
	char fdt[4096];
	ulong addr;

	ut_assertok(make_fuller_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test removal of property in root node / */
	ut_assertok(run_commandf("fdt print / compatible"));
	ut_assert_nextline("compatible = \"u-boot,fdt-test\"");
	ut_assertok(run_commandf("fdt rm / compatible"));
	ut_asserteq(1, run_commandf("fdt print / compatible"));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of property clock-names in subnode /test-node@1234 */
	ut_assertok(run_commandf("fdt print /test-node@1234 clock-names"));
	ut_assert_nextline("clock-names = \"fixed\", \"i2c\", \"spi\", \"uart2\", \"uart1\"");
	ut_assertok(run_commandf("fdt rm /test-node@1234 clock-names"));
	ut_asserteq(1, run_commandf("fdt print /test-node@1234 clock-names"));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of property u-boot,empty-property in subnode /test-node@1234 by alias */
	ut_assertok(run_commandf("fdt print testnodealias u-boot,empty-property"));
	ut_assert_nextline("testnodealias u-boot,empty-property");
	ut_assertok(run_commandf("fdt rm testnodealias u-boot,empty-property"));
	ut_asserteq(1, run_commandf("fdt print testnodealias u-boot,empty-property"));
	ut_assert_nextline("libfdt fdt_getprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of non-existent property noprop in subnode /test-node@1234 */
	ut_asserteq(1, run_commandf("fdt rm /test-node@1234 noprop"));
	ut_assert_nextline("libfdt fdt_delprop(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of non-existent node /no-node@5678 */
	ut_asserteq(1, run_commandf("fdt rm /no-node@5678"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of subnode /test-node@1234/subnode by alias */
	ut_assertok(run_commandf("fdt rm subnodealias"));
	ut_asserteq(1, run_commandf("fdt print /test-node@1234/subnode"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of node by non-existent alias */
	ut_asserteq(1, run_commandf("fdt rm noalias"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	/* Test removal of node by bad alias */
	ut_asserteq(1, run_commandf("fdt rm noalias"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_BADPATH");
	ut_assert_console_end();

	/* Test removal of node /test-node@1234 */
	ut_assertok(run_commandf("fdt rm /test-node@1234"));
	ut_asserteq(1, run_commandf("fdt print /test-node@1234"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test removal of node / */
	ut_assertok(run_commandf("fdt rm /"));
	ut_asserteq(1, run_commandf("fdt print /"));
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_rm, UTF_CONSOLE);

static int fdt_test_bootcpu(struct unit_test_state *uts)
{
	char fdt[256];
	ulong addr;
	int i;

	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test getting default bootcpu entry */
	ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
	ut_asserteq(0, env_get_ulong("bootcpu", 10, 0x1234));
	ut_assert_console_end();

	/* Test setting and getting new bootcpu entry, twice, to test overwrite */
	for (i = 42; i <= 43; i++) {
		ut_assertok(run_commandf("fdt bootcpu %d", i));
		ut_assert_console_end();

		/* Test getting new bootcpu entry */
		ut_assertok(run_commandf("fdt header get bootcpu boot_cpuid_phys"));
		ut_asserteq(i, env_get_ulong("bootcpu", 10, 0x1234));
		ut_assert_console_end();
	}

	return 0;
}
FDT_TEST(fdt_test_bootcpu, UTF_CONSOLE);

static int fdt_test_header_get(struct unit_test_state *uts,
			       const char *field, const unsigned long val)
{
	/* Test getting valid header entry */
	ut_assertok(run_commandf("fdt header get fvar %s", field));
	ut_asserteq(val, env_get_hex("fvar", 0x1234));
	ut_assert_console_end();

	/* Test getting malformed header entry */
	ut_asserteq(1, run_commandf("fdt header get fvar typo%stypo", field));
	ut_assert_console_end();

	return 0;
}

static int fdt_test_header(struct unit_test_state *uts)
{
	char fdt[256];
	ulong addr;

	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));

	/* Test header print */
	ut_assertok(run_commandf("fdt header"));
	ut_assert_nextline("magic:\t\t\t0x%x", fdt_magic(fdt));
	ut_assert_nextline("totalsize:\t\t0x%x (%d)", fdt_totalsize(fdt), fdt_totalsize(fdt));
	ut_assert_nextline("off_dt_struct:\t\t0x%x", fdt_off_dt_struct(fdt));
	ut_assert_nextline("off_dt_strings:\t\t0x%x", fdt_off_dt_strings(fdt));
	ut_assert_nextline("off_mem_rsvmap:\t\t0x%x", fdt_off_mem_rsvmap(fdt));
	ut_assert_nextline("version:\t\t%d", fdt_version(fdt));
	ut_assert_nextline("last_comp_version:\t%d", fdt_last_comp_version(fdt));
	ut_assert_nextline("boot_cpuid_phys:\t0x%x", fdt_boot_cpuid_phys(fdt));
	ut_assert_nextline("size_dt_strings:\t0x%x", fdt_size_dt_strings(fdt));
	ut_assert_nextline("size_dt_struct:\t\t0x%x", fdt_size_dt_struct(fdt));
	ut_assert_nextline("number mem_rsv:\t\t0x%x", fdt_num_mem_rsv(fdt));
	ut_assert_nextline_empty();
	ut_assert_console_end();

	/* Test header get */
	ut_assertok(fdt_test_header_get(uts, "magic", fdt_magic(fdt)));
	ut_assertok(fdt_test_header_get(uts, "totalsize", fdt_totalsize(fdt)));
	ut_assertok(fdt_test_header_get(uts, "off_dt_struct",
					fdt_off_dt_struct(fdt)));
	ut_assertok(fdt_test_header_get(uts, "off_dt_strings",
					fdt_off_dt_strings(fdt)));
	ut_assertok(fdt_test_header_get(uts, "off_mem_rsvmap",
					fdt_off_mem_rsvmap(fdt)));
	ut_assertok(fdt_test_header_get(uts, "version", fdt_version(fdt)));
	ut_assertok(fdt_test_header_get(uts, "last_comp_version",
					fdt_last_comp_version(fdt)));
	ut_assertok(fdt_test_header_get(uts, "boot_cpuid_phys",
					fdt_boot_cpuid_phys(fdt)));
	ut_assertok(fdt_test_header_get(uts, "size_dt_strings",
					fdt_size_dt_strings(fdt)));
	ut_assertok(fdt_test_header_get(uts, "size_dt_struct",
					fdt_size_dt_struct(fdt)));

	return 0;
}
FDT_TEST(fdt_test_header, UTF_CONSOLE);

static int fdt_test_memory_cells(struct unit_test_state *uts,
				 const unsigned int cells)
{
	unsigned char *pada, *pads;
	unsigned char *seta, *sets;
	char fdt[8192];
	const int size = sizeof(fdt);
	fdt32_t *regs;
	ulong addr;
	char *spc;
	int i;

	/* Create DT with node /memory { regs = <0x100 0x200>; } and #*cells */
	ut_assertnonnull(regs = calloc(2 * cells, sizeof(*regs)));
	ut_assertnonnull(pada = calloc(12, cells));
	ut_assertnonnull(pads = calloc(12, cells));
	ut_assertnonnull(seta = calloc(12, cells));
	ut_assertnonnull(sets = calloc(12, cells));
	for (i = cells; i >= 1; i--) {
		regs[cells - 1] = cpu_to_fdt32(i * 0x10000);
		regs[(cells * 2) - 1] = cpu_to_fdt32(~i);
		snprintf(seta + (8 * (cells - i)), 9, "%08x", i * 0x10000);
		snprintf(sets + (8 * (cells - i)), 9, "%08x", ~i);
		spc = (i != 1) ? " " : "";
		snprintf(pada + (11 * (cells - i)), 12, "0x%08x%s", i * 0x10000, spc);
		snprintf(pads + (11 * (cells - i)), 12, "0x%08x%s", ~i, spc);
	}

	ut_assertok(fdt_create(fdt, size));
	ut_assertok(fdt_finish_reservemap(fdt));
	ut_assert(fdt_begin_node(fdt, "") >= 0);
	ut_assertok(fdt_property_u32(fdt, "#address-cells", cells));
	ut_assertok(fdt_property_u32(fdt, "#size-cells", cells));
	ut_assert(fdt_begin_node(fdt, "memory") >= 0);
	ut_assertok(fdt_property_string(fdt, "device_type", "memory"));
	ut_assertok(fdt_property(fdt, "reg", &regs, cells * 2));
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_finish(fdt));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */
	addr = map_to_sysmem(fdt);
	set_working_fdt_addr(addr);
	ut_assert_nextline("Working FDT set to %lx", addr);

	/* Test updating the memory node */
	ut_assertok(run_commandf("fdt memory 0x%s 0x%s", seta, sets));
	ut_assertok(run_commandf("fdt print /memory"));
	ut_assert_nextline("memory {");
	ut_assert_nextline("\tdevice_type = \"memory\";");
	ut_assert_nextline("\treg = <%s %s>;", pada, pads);
	ut_assert_nextline("};");
	ut_assert_console_end();

	free(sets);
	free(seta);
	free(pads);
	free(pada);
	free(regs);

	return 0;
}

static int fdt_test_memory(struct unit_test_state *uts)
{
	/*
	 * Test memory fixup for 32 and 64 bit systems, anything bigger is
	 * so far unsupported and fails because of simple_stroull() being
	 * 64bit tops in the 'fdt memory' command implementation.
	 */
	ut_assertok(fdt_test_memory_cells(uts, 1));
	ut_assertok(fdt_test_memory_cells(uts, 2));

	/*
	 * The 'fdt memory' command is limited to /memory node, it does
	 * not support any other valid DT memory node format, which is
	 * either one or multiple /memory@adresss nodes. Therefore, this
	 * DT variant is not tested here.
	 */

	return 0;
}
FDT_TEST(fdt_test_memory, UTF_CONSOLE);

static int fdt_test_rsvmem(struct unit_test_state *uts)
{
	char fdt[8192];
	ulong addr;

	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */
	fdt_add_mem_rsv(fdt, 0x42, 0x1701);
	fdt_add_mem_rsv(fdt, 0x74656, 0x9);

	/* Test default reserved memory node presence */
	ut_assertok(run_commandf("fdt rsvmem print"));
	ut_assert_nextline("index\t\t   start\t\t    size");
	ut_assert_nextline("------------------------------------------------");
	ut_assert_nextline("    %x\t%016x\t%016x", 0, 0x42, 0x1701);
	ut_assert_nextline("    %x\t%016x\t%016x", 1, 0x74656, 0x9);
	ut_assert_console_end();

	/* Test add new reserved memory node */
	ut_assertok(run_commandf("fdt rsvmem add 0x1234 0x5678"));
	ut_assertok(run_commandf("fdt rsvmem print"));
	ut_assert_nextline("index\t\t   start\t\t    size");
	ut_assert_nextline("------------------------------------------------");
	ut_assert_nextline("    %x\t%016x\t%016x", 0, 0x42, 0x1701);
	ut_assert_nextline("    %x\t%016x\t%016x", 1, 0x74656, 0x9);
	ut_assert_nextline("    %x\t%016x\t%016x", 2, 0x1234, 0x5678);
	ut_assert_console_end();

	/* Test delete reserved memory node */
	ut_assertok(run_commandf("fdt rsvmem delete 0"));
	ut_assertok(run_commandf("fdt rsvmem print"));
	ut_assert_nextline("index\t\t   start\t\t    size");
	ut_assert_nextline("------------------------------------------------");
	ut_assert_nextline("    %x\t%016x\t%016x", 0, 0x74656, 0x9);
	ut_assert_nextline("    %x\t%016x\t%016x", 1, 0x1234, 0x5678);
	ut_assert_console_end();

	/* Test re-add new reserved memory node */
	ut_assertok(run_commandf("fdt rsvmem add 0x42 0x1701"));
	ut_assertok(run_commandf("fdt rsvmem print"));
	ut_assert_nextline("index\t\t   start\t\t    size");
	ut_assert_nextline("------------------------------------------------");
	ut_assert_nextline("    %x\t%016x\t%016x", 0, 0x74656, 0x9);
	ut_assert_nextline("    %x\t%016x\t%016x", 1, 0x1234, 0x5678);
	ut_assert_nextline("    %x\t%016x\t%016x", 2, 0x42, 0x1701);
	ut_assert_console_end();

	/* Test delete nonexistent reserved memory node */
	ut_asserteq(1, run_commandf("fdt rsvmem delete 10"));
	ut_assert_nextline("libfdt fdt_del_mem_rsv(): FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_rsvmem, UTF_CONSOLE);

static int fdt_test_chosen(struct unit_test_state *uts)
{
	const char *env_bootargs = env_get("bootargs");
	char fdt[8192];
	ulong addr;

	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */

	/* Test default chosen node presence, fail as there is no /chosen node */
	ut_asserteq(1, run_commandf("fdt print /chosen"));
	ut_assert_nextline("libfdt fdt_path_offset() returned FDT_ERR_NOTFOUND");
	ut_assert_console_end();

	/* Test add new chosen node without initrd */
	ut_assertok(run_commandf("fdt chosen"));
	ut_assertok(run_commandf("fdt print /chosen"));
	ut_assert_nextline("chosen {");
	ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
	if (env_bootargs)
		ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
	if (IS_ENABLED(CONFIG_DM_RNG) &&
	    !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
	    !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
		ut_assert_nextlinen("\tkaslr-seed = ");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/* Test add new chosen node with initrd */
	ut_assertok(run_commandf("fdt chosen 0x1234 0x5678"));
	ut_assertok(run_commandf("fdt print /chosen"));
	ut_assert_nextline("chosen {");
	ut_assert_nextline("\tlinux,initrd-end = <0x%08x 0x%08x>;",
			   upper_32_bits(0x1234 + 0x5678),
			   lower_32_bits(0x1234 + 0x5678));
	ut_assert_nextline("\tlinux,initrd-start = <0x%08x 0x%08x>;",
			   upper_32_bits(0x1234), lower_32_bits(0x1234));
	ut_assert_nextlinen("\tu-boot,version = "); /* Ignore the version string */
	if (env_bootargs)
		ut_assert_nextline("\tbootargs = \"%s\";", env_bootargs);
	if (IS_ENABLED(CONFIG_DM_RNG) &&
	    !IS_ENABLED(CONFIG_MEASURED_BOOT) &&
	    !IS_ENABLED(CONFIG_ARMV8_SEC_FIRMWARE_SUPPORT))
		ut_assert_nextlinen("\tkaslr-seed = ");
	ut_assert_nextline("};");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_chosen, UTF_CONSOLE);

static int fdt_test_apply(struct unit_test_state *uts)
{
	char fdt[8192], fdto[8192];
	ulong addr, addro;

	if (!IS_ENABLED(CONFIG_OF_LIBFDT_OVERLAY))
		return -EAGAIN;

	/* Create base DT with __symbols__ node */
	ut_assertok(fdt_create(fdt, sizeof(fdt)));
	ut_assertok(fdt_finish_reservemap(fdt));
	ut_assert(fdt_begin_node(fdt, "") >= 0);
	ut_assert(fdt_begin_node(fdt, "__symbols__") >= 0);
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_end_node(fdt));
	ut_assertok(fdt_finish(fdt));
	fdt_shrink_to_minimum(fdt, 4096);	/* Resize with 4096 extra bytes */
	addr = map_to_sysmem(fdt);
	set_working_fdt_addr(addr);
	ut_assert_nextline("Working FDT set to %lx", addr);

	/* Create DTO which adds single property to root node / */
	ut_assertok(fdt_create(fdto, sizeof(fdto)));
	ut_assertok(fdt_finish_reservemap(fdto));
	ut_assert(fdt_begin_node(fdto, "") >= 0);
	ut_assert(fdt_begin_node(fdto, "fragment") >= 0);
	ut_assertok(fdt_property_string(fdto, "target-path", "/"));
	ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
	ut_assertok(fdt_property_string(fdto, "newstring", "newvalue"));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_finish(fdto));
	addro = map_to_sysmem(fdto);

	/* Test default DT print */
	ut_assertok(run_commandf("fdt print /"));
	ut_assert_nextline("/ {");
	ut_assert_nextline("\t__symbols__ {");
	ut_assert_nextline("\t};");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/* Test simple DTO application */
	ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
	ut_assertok(run_commandf("fdt print /"));
	ut_assert_nextline("/ {");
	ut_assert_nextline("\tnewstring = \"newvalue\";");
	ut_assert_nextline("\t__symbols__ {");
	ut_assert_nextline("\t};");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/*
	 * Create complex DTO which:
	 * - modifies newstring property in root node /
	 * - adds new properties to root node /
	 * - adds new subnode with properties to root node /
	 * - adds phandle to the subnode and therefore __symbols__ node
	 */
	ut_assertok(fdt_create(fdto, sizeof(fdto)));
	ut_assertok(fdt_finish_reservemap(fdto));
	ut_assert(fdt_begin_node(fdto, "") >= 0);
	ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
	ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));

	ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
	ut_assertok(fdt_property_string(fdto, "target-path", "/"));
	ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
	ut_assertok(fdt_property_string(fdto, "newstring", "newervalue"));
	ut_assertok(fdt_property_u32(fdto, "newu32", 0x12345678));
	ut_assertok(fdt_property(fdto, "empty-property", NULL, 0));
	ut_assert(fdt_begin_node(fdto, "subnode") >= 0);
	ut_assertok(fdt_property_string(fdto, "subnewstring", "newervalue"));
	ut_assertok(fdt_property_u32(fdto, "subnewu32", 0x12345678));
	ut_assertok(fdt_property(fdto, "subempty-property", NULL, 0));
	ut_assertok(fdt_property_u32(fdto, "phandle", 0x01));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_end_node(fdto));

	ut_assert(fdt_begin_node(fdto, "__symbols__") >= 0);
	ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0/__overlay__/subnode"));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_finish(fdto));
	addro = map_to_sysmem(fdto);

	/* Test complex DTO application */
	ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
	ut_assertok(run_commandf("fdt print /"));
	ut_assert_nextline("/ {");
	ut_assert_nextline("\tempty-property;");
	ut_assert_nextline("\tnewu32 = <0x12345678>;");
	ut_assert_nextline("\tnewstring = \"newervalue\";");
	ut_assert_nextline("\tsubnode {");
	ut_assert_nextline("\t\tphandle = <0x00000001>;");
	ut_assert_nextline("\t\tsubempty-property;");
	ut_assert_nextline("\t\tsubnewu32 = <0x12345678>;");
	ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
	ut_assert_nextline("\t};");
	ut_assert_nextline("\t__symbols__ {");
	ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
	ut_assert_nextline("\t};");
	ut_assert_nextline("};");
	ut_assert_console_end();

	/*
	 * Create complex DTO which:
	 * - modifies subnewu32 property in subnode via phandle and uses __fixups__ node
	 */
	ut_assertok(fdt_create(fdto, sizeof(fdto)));
	ut_assertok(fdt_finish_reservemap(fdto));
	ut_assert(fdt_begin_node(fdto, "") >= 0);
	ut_assertok(fdt_property_cell(fdto, "#address-cells", 1));
	ut_assertok(fdt_property_cell(fdto, "#size-cells", 0));

	ut_assert(fdt_begin_node(fdto, "fragment@0") >= 0);
	ut_assertok(fdt_property_u32(fdto, "target", 0xffffffff));
	ut_assert(fdt_begin_node(fdto, "__overlay__") >= 0);
	ut_assertok(fdt_property_u32(fdto, "subnewu32", 0xabcdef01));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_end_node(fdto));

	ut_assert(fdt_begin_node(fdto, "__fixups__") >= 0);
	ut_assertok(fdt_property_string(fdto, "subnodephandle", "/fragment@0:target:0"));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_end_node(fdto));
	ut_assertok(fdt_finish(fdto));
	addro = map_to_sysmem(fdto);

	/* Test complex DTO application */
	ut_assertok(run_commandf("fdt apply 0x%08lx", addro));
	ut_assertok(run_commandf("fdt print /"));
	ut_assert_nextline("/ {");
	ut_assert_nextline("\tempty-property;");
	ut_assert_nextline("\tnewu32 = <0x12345678>;");
	ut_assert_nextline("\tnewstring = \"newervalue\";");
	ut_assert_nextline("\tsubnode {");
	ut_assert_nextline("\t\tphandle = <0x00000001>;");
	ut_assert_nextline("\t\tsubempty-property;");
	ut_assert_nextline("\t\tsubnewu32 = <0xabcdef01>;");
	ut_assert_nextline("\t\tsubnewstring = \"newervalue\";");
	ut_assert_nextline("\t};");
	ut_assert_nextline("\t__symbols__ {");
	ut_assert_nextline("\t\tsubnodephandle = \"/subnode\";");
	ut_assert_nextline("\t};");
	ut_assert_nextline("};");
	ut_assert_console_end();

	return 0;
}
FDT_TEST(fdt_test_apply, UTF_CONSOLE);

/* Test 'fdt reserved' reading reserved-memory nodes */
static int fdt_test_reserved(struct unit_test_state *uts)
{
	struct fdt_header *old_working_fdt;
	char fdt[8192];
	ulong addr;

	if (!IS_ENABLED(CONFIG_SANDBOX))
		return -EAGAIN;

	/* Save the original working_fdt */
	old_working_fdt = working_fdt;

	/* Set working_fdt to control FDT (sandbox has reserved memory) */
	working_fdt = (struct fdt_header *)gd->fdt_blob;

	ut_assertok(run_command("fdt reserved", 0));

	/* Just verify we get the header */
	ut_assert_nextlinen("ID");
	ut_assert_nextlinen("------");

	/* Verify we get a tcg_event_log entry with correct address/size */
	ut_assert_nextline("0    tcg_event_log        100000           2000            ");

	/* Test with FDT that has no reserved-memory node */
	ut_assertok(make_test_fdt(uts, fdt, sizeof(fdt), &addr));
	ut_assertok(run_command("fdt reserved", 0));
	ut_assert_nextlinen("ID   Name                 Start            Size");
	ut_assert_nextline("----------------------------------------------------------------");
	ut_assert_nextline("No /reserved-memory node found in device tree");
	ut_assert_console_end();

	/* Restore the original working_fdt */
	working_fdt = old_working_fdt;

	return 0;
}
FDT_TEST(fdt_test_reserved, UTF_CONSOLE);