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 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 | // SPDX-License-Identifier: GPL-2.0+ /* * PXE parser tests - C implementation for Python wrapper * * Copyright 2026 Canonical Ltd * * These tests verify the extlinux.conf parser APIs. * * Note: The 'ontimeout' keyword is tested via the test fixtures which include * it. Since ontimeout is handled identically to 'default' (both set * cfg->default_label), it cannot be distinguished after parsing. * */ #include <blk.h> #include <dm.h> #include <env.h> #include <fdt_support.h> #include <fs_legacy.h> #include <image.h> #include <linux/libfdt.h> #include <mapmem.h> #include <net-common.h> #include <pxe_utils.h> #include <test/test.h> #include <test/ut.h> /* Define test macros for pxe suite */ #define PXE_TEST(_name, _flags) \ UNIT_TEST(_name, _flags, pxe) #define PXE_TEST_ARGS(_name, _flags, ...) \ UNIT_TEST_ARGS(_name, _flags, pxe, __VA_ARGS__) /* Argument indices */ #define PXE_ARG_FS_IMAGE 0 /* Path to filesystem image */ #define PXE_ARG_CFG_PATH 1 /* Path to config file within image */ /** * struct pxe_env_save - saved FDT-related environment variables * * @fdt_addr: Saved fdt_addr value (strdup'd, or empty string if unset) * @fdtcontroladdr: Saved fdtcontroladdr value (strdup'd, or empty) */ struct pxe_env_save { char *fdt_addr; char *fdtcontroladdr; }; /** * pxe_save_and_clear_fdt_env() - save and clear FDT env vars * * Saves the current values of fdt_addr and fdtcontroladdr, then clears * them. The values are strdup'd since env_set() frees the old value, * which would leave saved env_get() pointers dangling. * * @save: Struct to save into * Return: 0 on success, -ENOMEM on allocation failure */ static int pxe_save_and_clear_fdt_env(struct pxe_env_save *save) { save->fdt_addr = strdup(env_get("fdt_addr") ?: ""); save->fdtcontroladdr = strdup(env_get("fdtcontroladdr") ?: ""); if (!save->fdt_addr || !save->fdtcontroladdr) { free(save->fdt_addr); free(save->fdtcontroladdr); return -ENOMEM; } env_set("fdt_addr", NULL); env_set("fdtcontroladdr", NULL); return 0; } /** * pxe_restore_fdt_env() - restore FDT env vars from saved state * * @save: Struct with saved values (freed after restore) */ static void pxe_restore_fdt_env(struct pxe_env_save *save) { env_set("fdt_addr", *save->fdt_addr ? save->fdt_addr : NULL); env_set("fdtcontroladdr", *save->fdtcontroladdr ? save->fdtcontroladdr : NULL); free(save->fdt_addr); free(save->fdtcontroladdr); } /* Memory address for loading files */ #define PXE_LOAD_ADDR 0x01000000 #define PXE_KERNEL_ADDR 0x02000000 #define PXE_INITRD_ADDR 0x02800000 #define PXE_FDT_ADDR 0x03000000 #define PXE_OVERLAY_ADDR 0x03100000 /** * struct pxe_test_info - context for the test getfile callback * * @uts: Unit test state for assertions */ struct pxe_test_info { struct unit_test_state *uts; }; /** * load_file() - Load a file from the host filesystem * * @path: Path to file within the mounted filesystem * @addr: Address to load file to * @sizep: Returns file size * Return: 0 on success, -ve on error */ static int load_file(const char *path, ulong addr, ulong *sizep) { loff_t len_read; int ret; ret = fs_set_blk_dev("host", "0:0", FS_TYPE_ANY); if (ret) return ret; ret = fs_legacy_read(path, addr, 0, 0, &len_read); if (ret) return ret; *sizep = len_read; return 0; } /** * pxe_test_getfile() - Read a file from the host filesystem * * This callback is used by the PXE parser to read included files. */ static int pxe_test_getfile(struct pxe_context *ctx, const char *file_path, ulong *addrp, ulong align, enum bootflow_img_t type, ulong *sizep) { if (!*addrp) return -ENOTSUPP; return load_file(file_path, *addrp, sizep); } /** * pxe_check_menu() - Check the standard menu output lines * * This helper checks all the console output lines from loading and displaying * the PXE menu, including config file retrieval, include files, background * image attempt, and the menu itself. Note: 'say' messages are now printed * when the label is booted, not during parsing. * * @uts: Unit test state * @error_msg: Expected error message after background image, or NULL if none * Return: 0 if OK, -ve on error */ static int pxe_check_menu(struct unit_test_state *uts, const char *error_msg) { int i; /* Config file retrieval */ ut_assert_nextline("Retrieving file: /extlinux/extlinux.conf"); /* Include file retrievals */ ut_assert_nextline("Retrieving file: /extlinux/extra.conf"); for (i = 3; i <= 16; i++) ut_assert_nextline("Retrieving file: /extlinux/nest%d.conf", i); /* Background image attempt */ ut_assert_nextline("Retrieving file: /boot/background.bmp"); ut_assert_nextline("There is no valid bmp file at the given address"); /* Optional error message before menu */ if (error_msg) ut_assert_nextline(error_msg); /* Menu title and items */ ut_assert_nextline("Test Boot Menu"); ut_assert_nextline("1:\tBoot Linux"); ut_assert_nextline("2:\tRescue Mode"); ut_assert_nextline("3:\tLocal Boot"); ut_assert_nextline("4:\tFIT Boot"); ut_assert_nextline("5:\tIncluded Label"); for (i = 6; i <= 19; i++) ut_assert_nextline("%d:\tLevel %d Label", i, i - 3); return 0; } /** * Test parsing an extlinux.conf file * * This test: * 1. Binds a filesystem image containing extlinux.conf * 2. Parses the config using parse_pxefile() * 3. Verifies the parsed labels can be inspected * 4. Verifies label properties are accessible */ static int pxe_test_parse_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); ulong addr = PXE_LOAD_ADDR; struct pxe_test_info info; struct pxe_context ctx; struct pxe_label *label; struct pxe_menu *cfg; struct abuf buf; char name[16]; uint i; int ret; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, true, cfg_path, false, false, NULL)); /* Read the config file into memory (quiet since we're just parsing) */ ctx.quiet = true; ret = get_pxe_file(&ctx, cfg_path, addr); ut_asserteq(1, ret); /* get_pxe_file returns 1 on success */ /* Parse the config file */ abuf_init_addr(&buf, addr, ctx.pxe_file_size); cfg = parse_pxefile(&ctx, &buf); ut_assertnonnull(cfg); /* Process any include files */ ut_assertok(pxe_process_includes(&ctx, cfg, addr)); /* Verify no console output during parsing (say is printed on boot) */ ut_assert_console_end(); /* Verify menu properties */ ut_asserteq_str("Test Boot Menu", cfg->title); ut_asserteq_str("linux", cfg->default_label); ut_asserteq_str("rescue", cfg->fallback_label); ut_asserteq_str("/boot/background.bmp", cfg->bmp); ut_asserteq(50, cfg->timeout); ut_asserteq(1, cfg->prompt); /* Verify first label: linux (with fdt, fdtoverlays) */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq_str("", label->num); /* only set when menu is built */ ut_asserteq_str("linux", label->name); ut_asserteq_str("Boot Linux", label->menu); ut_asserteq_str("/vmlinuz", label->kernel_label); ut_asserteq_str("/vmlinuz", label->kernel); ut_assertnull(label->config); ut_asserteq_str("root=/dev/sda1 quiet", label->append); ut_asserteq(1, label->initrds.count); ut_asserteq_str("/initrd.img", *alist_get(&label->initrds, 0, char *)); ut_asserteq_str("/dtb/board.dtb", label->fdt); ut_assertnull(label->fdtdir); ut_asserteq(5, label->files.count); ut_asserteq_str("/vmlinuz", alist_get(&label->files, 0, struct pxe_file)->path); ut_asserteq_str("/initrd.img", alist_get(&label->files, 1, struct pxe_file)->path); ut_asserteq_str("/dtb/board.dtb", alist_get(&label->files, 2, struct pxe_file)->path); ut_asserteq_str("/dtb/overlay1.dtbo", alist_get(&label->files, 3, struct pxe_file)->path); ut_asserteq_str("/dtb/overlay2.dtbo", alist_get(&label->files, 4, struct pxe_file)->path); ut_asserteq_str("Booting default Linux kernel", label->say); ut_asserteq(0, label->ipappend); ut_asserteq(0, label->attempted); ut_asserteq(0, label->localboot); ut_asserteq(0, label->localboot_val); ut_asserteq(1, label->kaslrseed); /* Verify second label: rescue (linux keyword, fdtdir, ipappend) */ label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("", label->num); ut_asserteq_str("rescue", label->name); ut_asserteq_str("Rescue Mode", label->menu); ut_asserteq_str("/vmlinuz-rescue", label->kernel_label); ut_asserteq_str("/vmlinuz-rescue", label->kernel); ut_assertnull(label->config); ut_asserteq_str("single", label->append); ut_asserteq(0, label->initrds.count); ut_assertnull(label->fdt); ut_asserteq_str("/dtb/", label->fdtdir); ut_asserteq(1, label->files.count); ut_asserteq_str("/vmlinuz-rescue", alist_get(&label->files, 0, struct pxe_file)->path); ut_assertnull(label->say); ut_asserteq(3, label->ipappend); ut_asserteq(0, label->attempted); ut_asserteq(0, label->localboot); ut_asserteq(0, label->localboot_val); ut_asserteq(0, label->kaslrseed); /* Verify third label: local (localboot only) */ label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("", label->num); ut_asserteq_str("local", label->name); ut_asserteq_str("Local Boot", label->menu); ut_assertnull(label->kernel_label); ut_assertnull(label->kernel); ut_assertnull(label->config); ut_assertnull(label->append); ut_asserteq(0, label->initrds.count); ut_assertnull(label->fdt); ut_assertnull(label->fdtdir); ut_asserteq(0, label->files.count); ut_assertnull(label->say); ut_asserteq(0, label->ipappend); ut_asserteq(0, label->attempted); ut_asserteq(1, label->localboot); ut_asserteq(1, label->localboot_val); ut_asserteq(0, label->kaslrseed); /* Verify fourth label: fitboot (fit keyword sets kernel and config) */ label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("", label->num); ut_asserteq_str("fitboot", label->name); ut_asserteq_str("FIT Boot", label->menu); ut_asserteq_str("/boot/image.fit#config-1", label->kernel_label); ut_asserteq_str("/boot/image.fit", label->kernel); ut_asserteq_str("#config-1", label->config); ut_asserteq_str("console=ttyS0", label->append); ut_asserteq(0, label->initrds.count); ut_assertnull(label->fdt); ut_assertnull(label->fdtdir); ut_asserteq(1, label->files.count); ut_asserteq_str("/boot/image.fit", alist_get(&label->files, 0, struct pxe_file)->path); ut_assertnull(label->say); ut_asserteq(0, label->ipappend); ut_asserteq(0, label->attempted); ut_asserteq(0, label->localboot); ut_asserteq(0, label->localboot_val); ut_asserteq(0, label->kaslrseed); /* Verify fifth label: included (from include directive) */ label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("", label->num); ut_asserteq_str("included", label->name); ut_asserteq_str("Included Label", label->menu); ut_asserteq_str("/boot/included-kernel", label->kernel_label); ut_asserteq_str("/boot/included-kernel", label->kernel); ut_assertnull(label->config); ut_asserteq_str("root=/dev/sdb1", label->append); ut_asserteq(0, label->initrds.count); ut_assertnull(label->fdt); ut_assertnull(label->fdtdir); ut_asserteq(1, label->files.count); ut_asserteq_str("/boot/included-kernel", alist_get(&label->files, 0, struct pxe_file)->path); ut_assertnull(label->say); ut_asserteq(0, label->ipappend); ut_asserteq(0, label->attempted); ut_asserteq(0, label->localboot); ut_asserteq(0, label->localboot_val); ut_asserteq(0, label->kaslrseed); /* Verify labels from nested includes (levels 3-16) - just check names */ for (i = 3; i <= 16; i++) { label = list_entry(label->list.next, struct pxe_label, list); snprintf(name, sizeof(name), "level%d", i); ut_asserteq_str(name, label->name); } /* * Test FDT overlay loading * * Get the first label (linux) which has fdtoverlays, set up the * environment, and verify overlay files can be loaded. */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq(5, label->files.count); /* Set environment variables for file loading */ ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("ramdisk_addr_r", PXE_INITRD_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set_hex("fdtoverlay_addr_r", PXE_OVERLAY_ADDR)); /* * Load files via pxe_load_files(). Note: pxe_load_files takes * ownership of fdtfile and frees it, so we must strdup here. */ ret = pxe_load_files(&ctx, label, strdup(label->fdt)); ut_assertok(ret); /* Verify kernel and FDT were loaded */ ut_asserteq(PXE_KERNEL_ADDR, ctx.kern_addr); ut_asserteq(PXE_FDT_ADDR, ctx.fdt_addr); /* Verify overlays were loaded to valid addresses (indices 3 and 4) */ ut_assert(alist_get(&label->files, 3, struct pxe_file)->addr >= PXE_OVERLAY_ADDR); ut_assert(alist_get(&label->files, 4, struct pxe_file)->addr >= PXE_OVERLAY_ADDR); /* Second overlay should be at a higher address than the first */ ut_assert(alist_get(&label->files, 4, struct pxe_file)->addr > alist_get(&label->files, 3, struct pxe_file)->addr); /* Verify no more console output */ ut_assert_console_end(); /* Clean up */ pxe_menu_uninit(cfg); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_parse_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test booting via sysboot command * * This test: * 1. Binds a filesystem image containing extlinux.conf * 2. Sets up environment variables for file loading * 3. Runs sysboot to boot the default label * 4. Verifies files were loaded by checking console output */ static int pxe_test_sysboot_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); void *kernel, *initrd, *fdt; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set environment variables for file loading */ ut_assertok(env_set_hex("pxefile_addr_r", PXE_LOAD_ADDR)); ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("ramdisk_addr_r", PXE_INITRD_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set_hex("fdtoverlay_addr_r", PXE_OVERLAY_ADDR)); ut_assertok(env_set("bootfile", cfg_path)); ut_assertok(env_set("pxe_timeout", "1")); /* * Run sysboot - it will try all labels and return 0 after failing * to boot them all (since sandbox can't actually boot Linux) */ ut_assertok(run_commandf("sysboot host 0:0 any %x %s", PXE_LOAD_ADDR, cfg_path)); /* Skip menu output and find the first label boot attempt */ ut_assert_skip_to_line("Enter choice: Booting default Linux kernel"); ut_assert_nextline("1:\tBoot Linux"); /* Verify files were loaded in order */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /initrd.img"); ut_assert_nextline("Retrieving file: /dtb/board.dtb"); ut_assert_nextline("Retrieving file: /dtb/overlay1.dtbo"); ut_assert_nextline("Retrieving file: /dtb/overlay2.dtbo"); ut_assert_nextline("Retrieving file: /overlay0.dtbo"); ut_assert_nextline("Failed loading overlay /overlay0.dtbo"); ut_assert_nextline("Retrieving file: /overlay1.dtbo"); ut_assert_nextline("Failed loading overlay /overlay1.dtbo"); ut_assert_nextline("append: root=/dev/sda1 quiet"); /* Boot fails on sandbox */ ut_assert_nextline("Unrecognized zImage"); ut_assert_console_end(); /* Verify files were loaded at the correct addresses */ kernel = map_sysmem(PXE_KERNEL_ADDR, 0); initrd = map_sysmem(PXE_INITRD_ADDR, 0); fdt = map_sysmem(PXE_FDT_ADDR, 0); /* Kernel should contain "kernel" at start */ ut_asserteq_mem("kernel", kernel, 6); /* Initrd should contain "ramdisk" at start */ ut_asserteq_mem("ramdisk", initrd, 7); /* FDT should have valid magic number */ ut_assertok(fdt_check_header(fdt)); /* Verify overlays were applied - check for properties added by overlays */ ut_asserteq_str("from-overlay1", fdt_getprop(fdt, fdt_path_offset(fdt, "/test-node"), "overlay1-property", NULL)); ut_asserteq_str("from-overlay2", fdt_getprop(fdt, fdt_path_offset(fdt, "/test-node"), "overlay2-property", NULL)); return 0; } PXE_TEST_ARGS(pxe_test_sysboot_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test fdtdir path resolution * * This test verifies: * 1. fdtdir with fdtfile env var - uses fdtfile value directly * 2. fdtdir with soc/board env vars - constructs {soc}-{board}.dtb * 3. fdtdir without trailing slash - slash is inserted */ static int pxe_test_fdtdir_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_test_info info; struct pxe_context ctx; struct pxe_label *label; struct pxe_menu *cfg; ulong addr = PXE_LOAD_ADDR; struct abuf buf; void *fdt; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, true, cfg_path, false, false, NULL)); /* Read and parse the config file */ ut_asserteq(1, get_pxe_file(&ctx, cfg_path, addr)); abuf_init_addr(&buf, addr, ctx.pxe_file_size); cfg = parse_pxefile(&ctx, &buf); ut_assertnonnull(cfg); /* Consume parsing output */ ut_assert_nextline("Retrieving file: %s", cfg_path); ut_assert_console_end(); /* * Test 1: fdtdir with fdtfile env var * Set fdtfile=test-board.dtb, load should find /dtb/test-board.dtb */ ut_assertok(env_set("fdtfile", "test-board.dtb")); ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set_hex("fdtoverlay_addr_r", PXE_OVERLAY_ADDR)); /* Get first label (fdtfile-test) and load its files */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq_str("fdtfile-test", label->name); ut_asserteq_str("/dtb/", label->fdtdir); ut_assertnull(label->fdt); ut_assertok(pxe_load_label(&ctx, label)); /* Verify FDT was loaded */ ut_asserteq(PXE_FDT_ADDR, ctx.conf_fdt); fdt = map_sysmem(PXE_FDT_ADDR, 0); ut_assertok(fdt_check_header(fdt)); /* Check console output shows the constructed path */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /dtb/test-board.dtb"); ut_assert_nextline("Retrieving file: /dtb/overlay1.dtbo"); ut_assert_console_end(); /* * Test 2: fdtdir with soc/board env vars (no fdtfile) * Set soc=tegra, board=jetson -> /dtb/tegra-jetson.dtb */ ut_assertok(env_set("fdtfile", NULL)); /* Clear fdtfile */ ut_assertok(env_set("soc", "tegra")); ut_assertok(env_set("board", "jetson")); ctx.conf_fdt = 0; /* Reset for next load */ /* Get second label (socboard-test) */ label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("socboard-test", label->name); ut_asserteq_str("/dtb", label->fdtdir); /* No trailing slash */ ut_assertok(pxe_load_label(&ctx, label)); /* Verify FDT was loaded (slash was inserted) */ ut_asserteq(PXE_FDT_ADDR, ctx.conf_fdt); fdt = map_sysmem(PXE_FDT_ADDR, 0); ut_assertok(fdt_check_header(fdt)); /* Check console output shows soc-board construction with slash */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /dtb/tegra-jetson.dtb"); ut_assert_console_end(); /* Clean up env vars */ env_set("fdtfile", NULL); env_set("soc", NULL); env_set("board", NULL); pxe_menu_uninit(cfg); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_fdtdir_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test error handling for missing FDT and overlay files * * This test verifies: * 1. Explicit FDT not found - label should fail with error * 2. fdtdir FDT not found - should warn but continue (return success) * 3. Missing overlay - should warn but continue loading other overlays */ static int pxe_test_errors_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_test_info info; struct pxe_context ctx; struct pxe_label *label; struct pxe_menu *cfg; ulong addr = PXE_LOAD_ADDR; struct abuf buf; void *fdt; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, true, cfg_path, false, false, NULL)); /* Read and parse the config file */ ut_asserteq(1, get_pxe_file(&ctx, cfg_path, addr)); abuf_init_addr(&buf, addr, ctx.pxe_file_size); cfg = parse_pxefile(&ctx, &buf); ut_assertnonnull(cfg); /* Consume parsing output */ ut_assert_nextline("Retrieving file: %s", cfg_path); ut_assert_console_end(); /* Set up environment for loading */ ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set_hex("fdtoverlay_addr_r", PXE_OVERLAY_ADDR)); ut_assertok(env_set("fdtfile", "missing.dtb")); /* For fdtdir test */ /* * Test 1: Explicit FDT file not found * Label has fdt=/dtb/nonexistent.dtb - should fail with -ENOENT */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq_str("missing-fdt", label->name); ut_asserteq_str("/dtb/nonexistent.dtb", label->fdt); ut_asserteq(-ENOENT, pxe_load_label(&ctx, label)); /* Check error message */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /dtb/nonexistent.dtb"); ut_assert_nextline("Skipping missing-fdt for failure retrieving FDT"); ut_assert_console_end(); /* * Test 2: fdtdir with missing FDT file * Label has fdtdir=/dtb/ but fdtfile=missing.dtb doesn't exist * Should warn but return success (label continues without FDT) */ ctx.conf_fdt = 0; label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("missing-fdtdir", label->name); ut_asserteq_str("/dtb/", label->fdtdir); ut_assertnull(label->fdt); ut_assertok(pxe_load_label(&ctx, label)); /* Check warning message */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /dtb/missing.dtb"); ut_assert_nextline("Skipping fdtdir /dtb/ for failure retrieving dts"); ut_assert_console_end(); /* * Test 3: Missing overlay file (but valid FDT) * Label has fdt=/dtb/board.dtb (exists) and two overlays: * - /dtb/nonexistent.dtbo (missing - should warn) * - /dtb/overlay1.dtbo (exists - should load) */ ctx.conf_fdt = 0; label = list_entry(label->list.next, struct pxe_label, list); ut_asserteq_str("missing-overlay", label->name); ut_asserteq_str("/dtb/board.dtb", label->fdt); ut_assertok(pxe_load_label(&ctx, label)); /* FDT should be loaded */ ut_asserteq(PXE_FDT_ADDR, ctx.conf_fdt); fdt = map_sysmem(PXE_FDT_ADDR, 0); ut_assertok(fdt_check_header(fdt)); /* Check console output */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /dtb/board.dtb"); ut_assert_nextline("Retrieving file: /dtb/nonexistent.dtbo"); ut_assert_nextline("Failed loading overlay /dtb/nonexistent.dtbo"); ut_assert_nextline("Retrieving file: /dtb/overlay1.dtbo"); ut_assert_console_end(); /* Clean up */ env_set("fdtfile", NULL); pxe_menu_uninit(cfg); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_errors_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test overlay loading when fdtoverlay_addr_r is not set * * This tests that when a label has fdtoverlays but fdtoverlay_addr_r is not * set, overlay loading is attempted via LMB allocation. The FDT is still * loaded successfully even if overlays fail to load. */ static int pxe_test_overlay_no_addr_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_test_info info; struct pxe_context ctx; struct pxe_label *label; struct pxe_menu *cfg; ulong addr = PXE_LOAD_ADDR; struct abuf buf; void *fdt; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, true, cfg_path, false, false, NULL)); /* Read and parse the config file (quiet since we're just parsing) */ ctx.quiet = true; ut_asserteq(1, get_pxe_file(&ctx, cfg_path, addr)); abuf_init_addr(&buf, addr, ctx.pxe_file_size); cfg = parse_pxefile(&ctx, &buf); ut_assertnonnull(cfg); /* Process any include files */ ut_assertok(pxe_process_includes(&ctx, cfg, addr)); /* Verify no console output during parsing (say is printed on boot) */ ut_assert_console_end(); /* * Set up environment for loading, but do NOT set fdtoverlay_addr_r. * This should cause overlay loading to be skipped with a warning. */ ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("ramdisk_addr_r", PXE_INITRD_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set("fdtoverlay_addr_r", NULL)); /* Clear it */ /* Get the first label (linux) which has fdtoverlays */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq_str("linux", label->name); ut_assert(label->files.count > 0); /* Enable output for loading phase */ ctx.quiet = false; /* Load the label - should succeed but skip overlays */ ut_assertok(pxe_load_label(&ctx, label)); /* FDT should be loaded */ ut_asserteq(PXE_FDT_ADDR, ctx.conf_fdt); fdt = map_sysmem(PXE_FDT_ADDR, 0); ut_assertok(fdt_check_header(fdt)); /* * Check console output - FDT loaded, overlays attempted via LMB * allocation but fail since test environment cannot load them */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /initrd.img"); ut_assert_nextline("Retrieving file: /dtb/board.dtb"); ut_assert_nextline("Retrieving file: /dtb/overlay1.dtbo"); ut_assert_nextline("Failed loading overlay /dtb/overlay1.dtbo"); ut_assert_nextline("Retrieving file: /dtb/overlay2.dtbo"); ut_assert_nextline("Failed loading overlay /dtb/overlay2.dtbo"); ut_assert_console_end(); /* Clean up */ pxe_menu_uninit(cfg); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_overlay_no_addr_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test pxe_get_file_size() function * * This tests reading the filesize from the environment variable. */ static int pxe_test_get_file_size(struct unit_test_state *uts) { ulong size; /* Test with no filesize set - should return -ENOENT */ env_set("filesize", NULL); ut_asserteq(-ENOENT, pxe_get_file_size(&size)); /* Test with valid hex filesize */ env_set("filesize", "1234"); ut_assertok(pxe_get_file_size(&size)); ut_asserteq(0x1234, size); /* Test with larger value */ env_set("filesize", "abcdef"); ut_assertok(pxe_get_file_size(&size)); ut_asserteq(0xabcdef, size); /* Test with invalid (non-hex) value */ env_set("filesize", "not_hex"); ut_asserteq(-EINVAL, pxe_get_file_size(&size)); /* Clean up */ env_set("filesize", NULL); return 0; } PXE_TEST(pxe_test_get_file_size, 0); /** * Test format_mac_pxe() function * * This tests MAC address formatting for PXE boot paths. */ static int pxe_test_format_mac(struct unit_test_state *uts) { char buf[21]; /* Test with buffer too small */ ut_asserteq(-ENOSPC, format_mac_pxe(buf, 20)); ut_asserteq(-ENOSPC, format_mac_pxe(buf, 1)); /* Test with valid buffer - sandbox has an ethernet device */ ut_asserteq(1, format_mac_pxe(buf, sizeof(buf))); /* Verify format: 01-xx-xx-xx-xx-xx-xx */ ut_asserteq(20, strlen(buf)); ut_asserteq('0', buf[0]); ut_asserteq('1', buf[1]); ut_asserteq('-', buf[2]); ut_asserteq('-', buf[5]); ut_asserteq('-', buf[8]); ut_asserteq('-', buf[11]); ut_asserteq('-', buf[14]); ut_asserteq('-', buf[17]); return 0; } PXE_TEST(pxe_test_format_mac, UTF_ETH_BOOTDEV); /** * Test get_pxelinux_path() with path too long * * This tests the path length check in get_pxelinux_path(). */ static int pxe_test_pxelinux_path_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); struct pxe_test_info info; struct pxe_context ctx; char path[600]; ut_assertnonnull(fs_image); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, false, "/", false, false, NULL)); /* Create a path that's too long (> 512 - 13 for "pxelinux.cfg/") */ memset(path, 'a', sizeof(path) - 1); path[sizeof(path) - 1] = '\0'; /* Should fail with -ENAMETOOLONG */ ut_asserteq(-ENAMETOOLONG, get_pxelinux_path(&ctx, path, PXE_LOAD_ADDR)); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_pxelinux_path_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }); /** * Test ipappend functionality * * This tests that ipappend correctly appends IP and MAC information to * bootargs. The rescue label has ipappend=3 which enables both: * - bit 0x1: ip=<ipaddr>:<serverip>:<gatewayip>:<netmask> * - bit 0x2: BOOTIF=01-xx-xx-xx-xx-xx-xx */ static int pxe_test_ipappend_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set environment variables for file loading */ ut_assertok(env_set_hex("pxefile_addr_r", PXE_LOAD_ADDR)); ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("ramdisk_addr_r", PXE_INITRD_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set("bootfile", cfg_path)); /* Set network environment variables for ipappend */ ut_assertok(env_set("ipaddr", "192.168.1.10")); ut_assertok(env_set("serverip", "192.168.1.1")); ut_assertok(env_set("gatewayip", "192.168.1.254")); ut_assertok(env_set("netmask", "255.255.255.0")); /* * Clear fdtfile and board so the rescue label's fdtdir tries * /dtb/.dtb (the fallback constructs soc-board.dtb from env) */ ut_assertok(env_set("fdtfile", NULL)); ut_assertok(env_set("board", NULL)); /* Override to boot the rescue label which has ipappend=3 */ ut_assertok(env_set("pxe_label_override", "rescue")); ut_assertok(env_set("pxe_timeout", "1")); /* Run sysboot */ ut_assertok(run_commandf("sysboot host 0:0 any %x %s", PXE_LOAD_ADDR, cfg_path)); /* Check menu output */ ut_assertok(pxe_check_menu(uts, NULL)); ut_assert_nextline("Enter choice: 2:\tRescue Mode"); /* Rescue label boot attempt */ ut_assert_nextline("Retrieving file: /vmlinuz-rescue"); /* * Rescue label has fdtdir=/dtb/ but no fdtfile is set, so it tries * to load /dtb/.dtb which fails. FDT is loaded before append. */ ut_assert_nextline("Retrieving file: /dtb/.dtb"); ut_assert_nextline("Skipping fdtdir /dtb/ for failure retrieving dts"); /* * Verify ipappend output - should have: * - original append: "single" * - ip= string from ipappend bit 0x1 * - BOOTIF= string from ipappend bit 0x2 */ ut_assert_nextlinen("append: single ip=192.168.1.10:192.168.1.1:" "192.168.1.254:255.255.255.0 BOOTIF=01-"); ut_assert_nextline("Unrecognized zImage"); ut_assert_console_end(); /* Clean up */ env_set("board", "sandbox"); env_set("ipaddr", NULL); env_set("serverip", NULL); env_set("gatewayip", NULL); env_set("netmask", NULL); env_set("pxe_label_override", NULL); env_set("pxe_timeout", NULL); return 0; } PXE_TEST_ARGS(pxe_test_ipappend_norun, UTF_CONSOLE | UTF_MANUAL | UTF_ETH_BOOTDEV, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test pxe_get_fdt_fallback() function * * This tests the FDT address fallback logic when a label doesn't specify * an FDT file via 'fdt' or 'fdtdir' keywords. */ static int pxe_test_fdt_fallback(struct unit_test_state *uts) { ulong kern_addr = 0x1000000; struct pxe_env_save save; struct pxe_label label; void *kern_buf; /* Create a dummy kernel buffer (not FIT format) */ kern_buf = map_sysmem(kern_addr, 64); memset(kern_buf, '\0', 64); unmap_sysmem(kern_buf); memset(&label, '\0', sizeof(label)); /* Save and clear env vars (fdtcontroladdr is set by U-Boot) */ ut_assertok(pxe_save_and_clear_fdt_env(&save)); /* Test 1: No fallback env vars set - should return NULL */ ut_assertnull(pxe_get_fdt_fallback(&label, kern_addr)); /* Test 2: fdt_addr set - should return fdt_addr */ ut_assertok(env_set_hex("fdt_addr", 0x2000000)); ut_asserteq_str("2000000", pxe_get_fdt_fallback(&label, kern_addr)); /* Test 3: Both set - fdt_addr takes priority */ ut_assertok(env_set_hex("fdtcontroladdr", 0x3000000)); ut_asserteq_str("2000000", pxe_get_fdt_fallback(&label, kern_addr)); /* Test 4: Only fdtcontroladdr set - should return fdtcontroladdr */ ut_assertok(env_set("fdt_addr", NULL)); ut_asserteq_str("3000000", pxe_get_fdt_fallback(&label, kern_addr)); /* Restore env vars */ pxe_restore_fdt_env(&save); return 0; } PXE_TEST(pxe_test_fdt_fallback, 0); /** * Test pxe_label_override environment variable * * This tests that pxe_label_override can override the default label, * and that an invalid override prints an error message. */ static int pxe_test_label_override_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set environment variables for file loading */ ut_assertok(env_set_hex("pxefile_addr_r", PXE_LOAD_ADDR)); ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("ramdisk_addr_r", PXE_INITRD_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); ut_assertok(env_set_hex("fdtoverlay_addr_r", PXE_OVERLAY_ADDR)); ut_assertok(env_set("bootfile", cfg_path)); ut_assertok(env_set("pxe_timeout", "1")); /* Test 1: Override to 'local' label (localboot) */ ut_assertok(env_set("pxe_label_override", "local")); ut_assertok(run_commandf("sysboot host 0:0 any %x %s", PXE_LOAD_ADDR, cfg_path)); /* Check menu output - say message is from default label */ ut_assertok(pxe_check_menu(uts, NULL)); /* Should boot 'local' label instead of default 'linux' */ ut_assert_nextline("Enter choice: 3:\tLocal Boot"); ut_assert_nextline("missing environment variable: localcmd"); /* * Localboot fails, so try default 'linux' label instead. * Boot is minimal - just kernel/initrd, no FDT. */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /initrd.img"); ut_assert_nextline("Unrecognized zImage"); /* Test 2: Invalid override - should print error before menu */ ut_assertok(env_set("pxe_label_override", "nonexistent")); ut_assertok(run_commandf("sysboot host 0:0 any %x %s", PXE_LOAD_ADDR, cfg_path)); /* Check menu with error message before it */ ut_assertok(pxe_check_menu(uts, "Missing override pxe label: nonexistent")); /* Say message is printed when label is selected (after "Enter choice:") */ ut_assert_nextline("Enter choice: Booting default Linux kernel"); ut_assert_nextline("1:\tBoot Linux"); /* Default label boot attempt - FDT/overlays loaded before append */ ut_assert_nextline("Retrieving file: /vmlinuz"); ut_assert_nextline("Retrieving file: /initrd.img"); ut_assert_nextline("Retrieving file: /dtb/board.dtb"); ut_assert_nextline("Retrieving file: /dtb/overlay1.dtbo"); ut_assert_nextline("Retrieving file: /dtb/overlay2.dtbo"); ut_assert_nextline("Retrieving file: /overlay0.dtbo"); ut_assert_nextline("Failed loading overlay /overlay0.dtbo"); ut_assert_nextline("Retrieving file: /overlay1.dtbo"); ut_assert_nextline("Failed loading overlay /overlay1.dtbo"); ut_assert_nextline("append: root=/dev/sda1 quiet"); ut_assert_nextline("Unrecognized zImage"); ut_assert_console_end(); /* Clean up */ ut_assertok(env_set("pxe_label_override", NULL)); ut_assertok(env_set("pxe_timeout", NULL)); return 0; } PXE_TEST_ARGS(pxe_test_label_override_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * struct pxe_alloc_info - context for the alloc test getfile callback * * @uts: Unit test state for assertions * @next_addr: Next address to allocate (increments by 0x100 each call) */ struct pxe_alloc_info { struct unit_test_state *uts; ulong next_addr; }; /** * pxe_alloc_getfile() - Read a file, allocating address if not provided * * For files loaded via env vars (kernel, initrd, fdt), this verifies that * *addrp is 0 (no environment variable set), then assigns an incrementing * address to simulate LMB allocation. For the config file (which is loaded * with a direct address), it just uses the provided address. */ static int pxe_alloc_getfile(struct pxe_context *ctx, const char *file_path, ulong *addrp, ulong align, enum bootflow_img_t type, ulong *sizep) { struct pxe_alloc_info *info = ctx->userdata; loff_t len_read; int ret; /* * Config file is loaded with direct address (non-zero). * Kernel/initrd/fdt/overlays come through env vars - if not set, * addrp will be 0 and we need to allocate. */ if (!*addrp) { *addrp = info->next_addr; info->next_addr += 0x100; } ret = fs_set_blk_dev("host", "0:0", FS_TYPE_ANY); if (ret) return ret; ret = fs_legacy_read(file_path, *addrp, 0, 0, &len_read); if (ret) return ret; *sizep = len_read; return 0; } /** * Test file loading with no address environment variables * * This tests the LMB allocation path where if no address env var is set, * the getfile callback receives *addrp == 0 and must allocate memory. * Our test callback assigns incrementing addresses (0x100, 0x200, etc.) * and verifies the addresses are then stored in ctx. */ static int pxe_test_alloc_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_alloc_info info; struct pxe_env_save save; struct pxe_context ctx; ulong addr; int ret; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; info.next_addr = 0x100; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Save and clear FDT fallback env vars (fdtcontroladdr is set at boot) */ ut_assertok(pxe_save_and_clear_fdt_env(&save)); /* Ensure address env vars are NOT set */ ut_assertok(env_set("kernel_addr_r", NULL)); ut_assertok(env_set("ramdisk_addr_r", NULL)); ut_assertok(env_set("fdt_addr_r", NULL)); ut_assertok(env_set("fdtoverlay_addr_r", NULL)); ut_assertok(env_set("pxe_timeout", "1")); /* Set up the PXE context with our allocating getfile */ ut_assertok(pxe_setup_ctx(&ctx, pxe_alloc_getfile, &info, true, cfg_path, false, false, NULL)); /* Read the config file - use a fixed address for parsing */ addr = PXE_LOAD_ADDR; ret = get_pxe_file(&ctx, cfg_path, addr); ut_asserteq(1, ret); /* Parse and probe - this triggers file loading */ ut_assertok(pxe_probe(&ctx, addr, false)); /* * Verify all pxe_context fields are set correctly. * * The background BMP is loaded first (0x100), then the default * label 'linux' loads: kernel (0x200), initrd (0x300). * * Note: FDT loading requires fdt_addr_r to be set (checked in * label_process_fdt before attempting to load), so conf_fdt_str * and conf_fdt are NULL/0. */ /* Context setup fields */ ut_asserteq_ptr(pxe_alloc_getfile, ctx.getfile); ut_asserteq_ptr(&info, ctx.userdata); ut_asserteq(true, ctx.allow_abs_path); ut_assertnonnull(ctx.bootdir); ut_assert(ctx.pxe_file_size); /* set by get_pxe_file() */ ut_asserteq(false, ctx.use_ipv6); ut_asserteq(false, ctx.use_fallback); ut_asserteq(true, ctx.no_boot); ut_assertnull(ctx.bflow); ut_assertnonnull(ctx.cfg); /* BMP loaded first */ ut_asserteq(0x100, image_load_addr); /* Label selection */ ut_assertnonnull(ctx.label); ut_asserteq_str("linux", ctx.label->name); /* Kernel */ ut_asserteq_str("200", ctx.kern_addr_str); ut_asserteq(0x200, ctx.kern_addr); ut_asserteq(6, ctx.kern_size); /* Initrd */ ut_asserteq(0x300, ctx.initrd_addr); ut_asserteq(7, ctx.initrd_size); ut_asserteq_str("300:7", ctx.initrd_str); /* FDT (not loaded - no fdt_addr_r env var) */ ut_assertnull(ctx.conf_fdt_str); ut_asserteq(0, ctx.conf_fdt); /* Boot flags */ ut_asserteq(false, ctx.restart); ut_asserteq(false, ctx.fake_go); /* Clean up */ pxe_menu_uninit(ctx.cfg); pxe_destroy_ctx(&ctx); ut_assertok(env_set("pxe_timeout", NULL)); pxe_restore_fdt_env(&save); return 0; } PXE_TEST_ARGS(pxe_test_alloc_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test FIT image with embedded FDT (no explicit fdt line) * * This tests that when using 'fit /path.fit' without an explicit 'fdt' * line (label->fdt is NULL), the FDT address is set to the FIT address * so bootm can extract the FDT from the FIT image. * * The buggy behavior: When label->fdt is NULL, the FIT check fails: * if (label->fdt && label->kernel_label && * !strcmp(label->kernel_label, label->fdt)) * and conf_fdt_str is not set to the FIT address. * * The correct behavior: When the kernel is a FIT image with embedded FDT * and no explicit fdt line is provided, conf_fdt_str should be set to * the kernel (FIT) address so bootm can extract the FDT. */ static int pxe_test_fit_embedded_fdt_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_test_info info; struct pxe_context ctx; struct pxe_label *label; struct pxe_menu *cfg; ulong addr = PXE_LOAD_ADDR; struct abuf buf; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); info.uts = uts; /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); /* Set up the PXE context */ ut_assertok(pxe_setup_ctx(&ctx, pxe_test_getfile, &info, true, cfg_path, false, false, NULL)); /* Set up environment for loading */ ut_assertok(env_set_hex("kernel_addr_r", PXE_KERNEL_ADDR)); ut_assertok(env_set_hex("fdt_addr_r", PXE_FDT_ADDR)); /* Read and parse the config file */ ut_asserteq(1, get_pxe_file(&ctx, cfg_path, addr)); abuf_init_addr(&buf, addr, ctx.pxe_file_size); cfg = parse_pxefile(&ctx, &buf); ut_assertnonnull(cfg); /* Consume parsing output */ ut_assert_nextline("Retrieving file: %s", cfg_path); ut_assert_console_end(); /* Get the fitonly label which uses 'fit' without 'fdt' */ label = list_first_entry(&cfg->labels, struct pxe_label, list); ut_asserteq_str("fitonly", label->name); /* Verify this is a FIT label with no explicit fdt */ ut_assertnonnull(label->kernel); /* /boot/image.fit */ ut_assertnull(label->config); /* NULL when no #config suffix */ ut_assertnull(label->fdt); /* No explicit fdt line - this is key */ /* Load the label */ ut_assertok(pxe_load_label(&ctx, label)); /* Consume load output */ ut_assert_nextline("Retrieving file: /boot/image.fit"); ut_assert_console_end(); /* * For FIT images with embedded FDT and no explicit fdt line, * conf_fdt_str is currently NULL. Ideally it should be set to the * kernel address so bootm can extract the FDT from the FIT, but * that is a pre-existing limitation. * * This test detects regressions where conf_fdt_str is incorrectly * set to fdt_addr_r instead of NULL (which would cause bootm to * look at the wrong address for the FDT). */ ut_assertnull(ctx.conf_fdt_str); ut_asserteq(0, ctx.conf_fdt); /* Clean up */ pxe_menu_uninit(cfg); pxe_destroy_ctx(&ctx); return 0; } PXE_TEST_ARGS(pxe_test_fit_embedded_fdt_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); /** * Test callback-free file loading API with pxe_load() * * This tests the new callback-free API where the caller: * 1. Calls pxe_parse() to get a menu with labels containing files lists * 2. Iterates over label->files and loads each file manually * 3. Calls pxe_load() to record where each file was loaded * * This approach eliminates the need for getfile callbacks during loading. */ static int pxe_test_files_api_norun(struct unit_test_state *uts) { const char *fs_image = ut_str(PXE_ARG_FS_IMAGE); const char *cfg_path = ut_str(PXE_ARG_CFG_PATH); struct pxe_context *ctx; struct pxe_label *label; struct pxe_menu *menu; const struct pxe_file *file; struct pxe_file *filep; ulong addr = PXE_LOAD_ADDR; ulong file_addr; ulong start_mem; ulong size; char *buf; uint i; ut_assertnonnull(fs_image); ut_assertnonnull(cfg_path); /* Bind the filesystem image */ ut_assertok(run_commandf("host bind 0 %s", fs_image)); blkcache_free(); start_mem = ut_check_delta(0); /* Load the config file first */ ut_assertok(load_file(cfg_path, addr, &size)); /* Add null terminator - parser expects null-terminated string */ buf = map_sysmem(addr, 0); buf[size] = '\0'; unmap_sysmem(buf); ctx = pxe_parse(addr, size, cfg_path); ut_assertnonnull(ctx); menu = ctx->cfg; /* Parsing with no getfile callback should produce no output */ ut_assert_console_end(); /* * Process includes manually - load each include file and parse it. * Use an index-based loop since parsing may add more includes. */ for (i = 0; i < menu->includes.count; i++) { const struct pxe_include *inc; inc = alist_get(&menu->includes, i, struct pxe_include); ut_assertok(load_file(inc->path, addr, &size)); ut_asserteq(1, pxe_parse_include(ctx, inc, addr, size)); } /* Include parsing should also produce no output */ ut_assert_console_end(); /* Get the first label */ label = list_first_entry(&menu->labels, struct pxe_label, list); ut_asserteq_str("linux", label->name); /* * Verify the files list contains expected files: * - kernel (/vmlinuz) * - initrd (/initrd.img) * - fdt (/dtb/board.dtb) * - 2 overlays (/dtb/overlay1.dtbo, /dtb/overlay2.dtbo) */ ut_asserteq(5, label->files.count); /* Check each file has correct type and path */ file = alist_get(&label->files, 0, struct pxe_file); ut_asserteq(PFT_KERNEL, file->type); ut_asserteq_str("/vmlinuz", file->path); ut_asserteq(0, file->addr); /* Not loaded yet */ file = alist_get(&label->files, 1, struct pxe_file); ut_asserteq(PFT_INITRD, file->type); ut_asserteq_str("/initrd.img", file->path); file = alist_get(&label->files, 2, struct pxe_file); ut_asserteq(PFT_FDT, file->type); ut_asserteq_str("/dtb/board.dtb", file->path); file = alist_get(&label->files, 3, struct pxe_file); ut_asserteq(PFT_FDTOVERLAY, file->type); ut_asserteq_str("/dtb/overlay1.dtbo", file->path); file = alist_get(&label->files, 4, struct pxe_file); ut_asserteq(PFT_FDTOVERLAY, file->type); ut_asserteq_str("/dtb/overlay2.dtbo", file->path); /* * Load each file and call pxe_load() to record address/size. * This demonstrates the callback-free file loading API. */ file_addr = PXE_KERNEL_ADDR; alist_for_each(filep, &label->files) { ulong size; ut_assertok(load_file(filep->path, file_addr, &size)); pxe_load(filep, file_addr, size); file_addr += ALIGN(size, SZ_64K); } /* Verify files were loaded with valid addresses and sizes */ file = alist_get(&label->files, 0, struct pxe_file); ut_asserteq(PXE_KERNEL_ADDR, file->addr); ut_assert(file->size > 0); /* * Set up context for boot from the files list. * In the callback-free API, the caller populates ctx fields. */ ctx->kern_addr = alist_get(&label->files, 0, struct pxe_file)->addr; ctx->initrd_addr = alist_get(&label->files, 1, struct pxe_file)->addr; ctx->fdt_addr = alist_get(&label->files, 2, struct pxe_file)->addr; ctx->label = label; /* Boot - sandbox simulates this */ pxe_boot(ctx); /* Sandbox cannot boot the kernel, so we get this error */ ut_assert_nextline("Unrecognized zImage"); ut_assert_console_end(); /* Verify the files are in memory at the correct addresses */ file = alist_get(&label->files, 0, struct pxe_file); buf = map_sysmem(file->addr, file->size); ut_asserteq_mem("kernel", buf, 6); unmap_sysmem(buf); file = alist_get(&label->files, 1, struct pxe_file); buf = map_sysmem(file->addr, file->size); ut_asserteq_mem("ramdisk", buf, 7); unmap_sysmem(buf); /* Clean up and check for memory leaks */ pxe_cleanup(ctx); blkcache_free(); ut_assertok(ut_check_delta(start_mem)); return 0; } PXE_TEST_ARGS(pxe_test_files_api_norun, UTF_CONSOLE | UTF_MANUAL, { "fs_image", UT_ARG_STR }, { "cfg_path", UT_ARG_STR }); |