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
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright 2021 Google LLC
 * Written by Simon Glass <sjg@chromium.org>
 */

#define LOG_CATEGORY UCLASS_BOOTSTD

#include <bootdev.h>
#include <bootflow.h>
#include <bootmeth.h>
#include <bootstd.h>
#include <dm.h>
#include <efi_device_path.h>
#include <env_internal.h>
#include <malloc.h>
#include <serial.h>
#include <dm/device-internal.h>
#include <dm/uclass-internal.h>

/* ensure BOOTMETH_MAX_COUNT fits in method_flags field */
static_assert(BOOTMETH_MAX_COUNT <=
	      (sizeof(((struct bootflow_iter *)NULL)->method_flags) * 8));

/* error codes used to signal running out of things */
enum {
	BF_NO_MORE_PARTS	= -ESHUTDOWN,
	BF_NO_MORE_DEVICES	= -ENODEV,
};

static const char *const bootflow_img[BFI_COUNT - BFI_FIRST] = {
	"extlinux_cfg",
	"bls_cfg",
	"logo",
	"efi",
	"cmdline",
	"vbe-state",
	"vbe-oem-fit",
};

/**
 * bootflow_state - name for each state
 *
 * See enum bootflow_state_t for what each of these means
 */
static const char *const bootflow_state[BOOTFLOWST_COUNT] = {
	"base",
	"media",
	"part",
	"fs",
	"file",
	"ready",
};

const char *bootflow_state_get_name(enum bootflow_state_t state)
{
	/* This doesn't need to be a useful name, since it will never occur */
	if (state < 0 || state >= BOOTFLOWST_COUNT)
		return "?";

	return bootflow_state[state];
}

/**
 * report_bootflow_err() - Report where a bootflow failed
 *
 * When a bootflow does not make it to the 'loaded' state, something went wrong.
 * Print a helpful message if there is an error
 *
 * @bflow: Bootflow to process
 * @err: Error code (0 if none)
 */
static void report_bootflow_err(struct bootflow *bflow, int err)
{
	if (!err)
		return;

	/* Indent out to 'Method' */
	printf("     ** ");

	switch (bflow->state) {
	case BOOTFLOWST_BASE:
		printf("No media/partition found");
		break;
	case BOOTFLOWST_MEDIA:
		printf("No partition found");
		break;
	case BOOTFLOWST_PART:
		printf("No filesystem found");
		break;
	case BOOTFLOWST_FS:
		printf("File not found");
		break;
	case BOOTFLOWST_FILE:
		printf("File cannot be loaded");
		break;
	case BOOTFLOWST_READY:
		printf("Ready");
		break;
	case BOOTFLOWST_COUNT:
		log_err("Unexpected boot value of bootflow error %d",
			 bflow->state);
		break;
	}

	printf(", err=%dE\n", err);
}

void bootflow_show(int index, struct bootflow *bflow, bool errors)
{
	const char *name = bootflow_guess_label(bflow);
	char enc_mark = (bflow->flags & BOOTFLOWF_ENCRYPTED) ? 'E' : ' ';
	char ent_str[8];

	strcpy(ent_str, "   ");
	if (bflow->method) {
		struct bootmeth_uc_plat *ucp;

		ucp = dev_get_uclass_plat(bflow->method);
		if (ucp->flags & BOOTMETHF_MULTI)
			snprintf(ent_str, sizeof(ent_str), "%3d",
				 bflow->entry);
	}

	printf("%3x  %-11s  %-6s  %-9.9s %4x  %s  %c  %-25.25s %s\n", index,
	       bflow->method ? bflow->method->name : "(none)",
	       bootflow_state_get_name(bflow->state), name, bflow->part,
	       ent_str, enc_mark, bflow->name, bflow->fname ?: "");
	if (errors)
		report_bootflow_err(bflow, bflow->err);
}

int bootflow_first_glob(struct bootflow **bflowp)
{
	struct bootstd_priv *std;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return ret;

	if (!std->bootflows.count)
		return -ENOENT;

	*bflowp = alist_getw(&std->bootflows, 0, struct bootflow);

	return 0;
}

int bootflow_next_glob(struct bootflow **bflowp)
{
	struct bootstd_priv *std;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return ret;

	*bflowp = alist_nextw(&std->bootflows, *bflowp);
	if (!*bflowp)
		return -ENOENT;

	return 0;
}

void bootflow_iter_init(struct bootflow_iter *iter, int flags)
{
	memset(iter, '\0', sizeof(*iter));
	iter->first_glob_method = -1;
	iter->flags = flags;

	/* remember the first bootdevs we see */
	iter->max_devs = BOOTFLOW_MAX_USED_DEVS;
}

void bootflow_iter_uninit(struct bootflow_iter *iter)
{
	free(iter->method_order);
}

int bootflow_iter_drop_bootmeth(struct bootflow_iter *iter,
				const struct udevice *bmeth)
{
	/* We only support disabling the current bootmeth */
	if (bmeth != iter->method || iter->cur_method >= iter->num_methods ||
	    iter->method_order[iter->cur_method] != bmeth)
		return -EINVAL;

	log_debug("Dropping bootmeth '%s'\n", bmeth->name);

	memmove(&iter->method_order[iter->cur_method],
		&iter->method_order[iter->cur_method + 1],
		(iter->num_methods - iter->cur_method - 1) * sizeof(void *));

	iter->num_methods--;
	if (iter->first_glob_method > 0) {
		iter->first_glob_method--;
		log_debug("first_glob_method %d\n", iter->first_glob_method);
	}

	return 0;
}

/**
 * bootflow_iter_set_dev() - switch to the next bootdev when iterating
 *
 * This sets iter->dev, records the device in the dev_used[] list and shows a
 * message if required
 *
 * @iter: Iterator to update
 * @dev: Bootdev to use, or NULL if there are no more
 */
static void bootflow_iter_set_dev(struct bootflow_iter *iter,
				  struct udevice *dev, int method_flags)
{
	struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(iter->method);

	log_debug("iter: Setting dev to %s, flags %x\n",
		  dev ? dev->name : "(none)", method_flags);
	iter->dev = dev;
	iter->method_flags = method_flags;

	if (IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
		/* record the device for later */
		if (dev && iter->num_devs < iter->max_devs)
			iter->dev_used[iter->num_devs++] = dev;

		if ((iter->flags & (BOOTFLOWIF_SHOW | BOOTFLOWIF_SINGLE_DEV)) ==
		    BOOTFLOWIF_SHOW) {
			if (dev)
				printf("Scanning bootdev '%s':\n", dev->name);
			else if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) &&
				 ucp->flags & BOOTMETHF_GLOBAL)
				printf("Scanning global bootmeth '%s':\n",
				iter->method->name);
			else
				printf("No more bootdevs\n");
		}
	}
}

/**
 * scan_next_in_uclass() - Scan for the next bootdev in the same media uclass
 *
 * Move through the following bootdevs until we find another in this media
 * uclass, or run out
 *
 * @devp: On entry, the device to check, on exit the new device, or NULL if
 * there is none
 */
static void scan_next_in_uclass(struct udevice **devp)
{
	struct udevice *dev = *devp;
	enum uclass_id cur_id = device_get_uclass_id(dev->parent);

	do {
		uclass_find_next_device(&dev);
	} while (dev && cur_id != device_get_uclass_id(dev->parent));

	*devp = dev;
}

/**
 * bootmeth_glob_allowed() - Check if a global bootmeth is usable at this point
 *
 * @iter: Bootflow iterator being used
 * Return: true if the global bootmeth has a suitable priority and has not
 * already been used
 */
static bool bootmeth_glob_allowed(struct bootflow_iter *iter, int meth_seq)
{
	struct udevice *meth = iter->method_order[meth_seq];
	bool done = iter->methods_done & BIT(meth_seq);
	struct bootmeth_uc_plat *ucp;

	ucp = dev_get_uclass_plat(meth);
	log_debug("considering glob '%s': done %d glob_prio %d\n", meth->name,
		  done, ucp->glob_prio);

	/*
	 * if this one has already been used, or its priority is too low, try
	 * the next
	 */
	if (done || ucp->glob_prio > iter->cur_prio)
		return false;

	return true;
}

/**
 * next_glob_bootmeth() - Find the next global bootmeth to use
 *
 * Scans the global bootmeths to find the first unused one whose priority has
 * been reached. If found, iter->cur_method and iter->method are set up and
 * doing_global is set to true
 *
 * @iter: Bootflow iterator being used
 * Return 0 if found, -ENOENT if no more global bootmeths are available
 */
static int next_glob_bootmeth(struct bootflow_iter *iter)
{
	log_debug("rescan global bootmeths have_global %d\n",
		  iter->have_global);
	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->have_global) {
		int i;

		/* rescan the global bootmeths */
		log_debug("first_glob_method %d num_methods %d methods_done %x\n",
			  iter->first_glob_method, iter->num_methods,
			  iter->methods_done);
		for (i = iter->first_glob_method; i < iter->num_methods; i++) {
			if (bootmeth_glob_allowed(iter, i)) {
				iter->cur_method = i;
				iter->method = iter->method_order[i];
				iter->doing_global = true;
				iter->dev = NULL;
				return 0;
			}
		}
	}

	return -ENOENT;
}

/**
 * prepare_bootdev() - Get ready to use a bootdev
 *
 * @iter: Bootflow iterator being used
 * @dev: UCLASS_BOOTDEV device to use
 * @method_flags: Method flag for the bootdev
 * @check_global: true to check global bootmeths before processing @dev
 * Return 0 if OK, -ve if the bootdev failed to probe
 */
static int prepare_bootdev(struct bootflow_iter *iter, struct udevice *dev,
			   int method_flags, bool check_global)
{
	int ret;

	if (check_global && !next_glob_bootmeth(iter)) {
		iter->pending_bootdev = dev;
		iter->pending_method_flags = method_flags;
		return 0;
	}

	/*
	 * Probe the bootdev. This does not probe any attached block device,
	 * since they are siblings
	 */
	ret = device_probe(dev);
	log_debug("probe %s %d\n", dev->name, ret);
	if (ret)
		return log_msg_ret("probe", ret);
	bootflow_iter_set_dev(iter, dev, method_flags);

	return 0;
}

/**
 * iter_incr() - Move to the next item (method, part, bootdev)
 *
 * Return: 0 if OK, BF_NO_MORE_DEVICES if there are no more bootdevs
 */
static int iter_incr(struct bootflow_iter *iter)
{
	struct udevice *dev;
	bool inc_dev = true;
	bool global;
	int ret;

	log_debug("entry: err=%d\n", iter->err);
	global = iter->doing_global;

	if (iter->err == BF_NO_MORE_DEVICES) {
		log_debug("-> err: no more devices1\n");
		return BF_NO_MORE_DEVICES;
	}

	/*
	 * If the current method supports multiple entries and the last call
	 * succeeded, try the next entry before advancing the method
	 */
	if (iter->method && !iter->err) {
		struct bootmeth_uc_plat *ucp;

		ucp = dev_get_uclass_plat(iter->method);
		if (ucp->flags & BOOTMETHF_MULTI) {
			iter->entry++;
			log_debug("-> next entry %d for method '%s'\n",
				  iter->entry, iter->method->name);
			return 0;
		}
	}
	iter->entry = 0;

	/* Get the next boothmethod */
	for (iter->cur_method++; iter->cur_method < iter->num_methods;
	     iter->cur_method++) {
		/* loop until we find a global bootmeth we haven't used */
		if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
			if (!bootmeth_glob_allowed(iter, iter->cur_method))
				continue;

			iter->method = iter->method_order[iter->cur_method];
			log_debug("-> next global method '%s'\n",
				  iter->method->name);
			return 0;
		}

		/* at this point we are only considering non-global bootmeths */
		if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->have_global &&
		    iter->cur_method >= iter->first_glob_method)
			break;

		iter->method = iter->method_order[iter->cur_method];
		log_debug("-> next method '%s'\n", iter->method->name);
		return 0;
	}
	log_debug("! no more methods: cur_method %d num_methods %d\n",
		  iter->cur_method, iter->num_methods);

	/*
	 * If we have finished scanning the global bootmeths, start the
	 * normal bootdev scan
	 */
	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && global) {
		iter->doing_global = false;

		/*
		 * we've come to the end, so see if we should use a pending
		 * bootdev from when we decided to rescan the global bootmeths
		 */
		if (iter->pending_bootdev) {
			int meth_flags = iter->pending_method_flags;

			dev = iter->pending_bootdev;
			iter->pending_bootdev = NULL;
			iter->pending_method_flags = 0;

			ret = prepare_bootdev(iter, dev, meth_flags, false);
			if (ret)
				return log_msg_ret("ipb", ret);

			iter->cur_method = 0;
			iter->method = iter->method_order[iter->cur_method];

			log_debug("-> using pending bootdev '%s' method '%s'\n",
				  dev->name, iter->method->name);

			return 0;
		}

		/* if this was the final global bootmeth check, we are done */
		if (iter->cur_prio == BOOTDEVP_COUNT) {
			log_debug("-> done global bootmeths\n");

			/* print the same message as bootflow_iter_set_dev() */
			if ((iter->flags & (BOOTFLOWIF_SHOW |
					    BOOTFLOWIF_SINGLE_DEV)) ==
					    BOOTFLOWIF_SHOW)
				printf("No more bootdevs\n");
			return BF_NO_MORE_DEVICES;
		}

		/*
		 * Don't move to the next dev as we haven't tried this
		 * one yet!
		 */
		inc_dev = false;
	}

	if (iter->flags & BOOTFLOWIF_SINGLE_PARTITION) {
		log_debug("-> single partition: no more devices\n");
		return BF_NO_MORE_DEVICES;
	}

	/* No more bootmeths; start at the first one, and... */
	iter->cur_method = 0;
	iter->method = iter->method_order[iter->cur_method];

	if (iter->err != BF_NO_MORE_PARTS) {
		/* ...select next partition  */
		if (++iter->part <= iter->max_part) {
			log_debug("-> next partition %d max %d\n", iter->part,
				  iter->max_part);
			return 0;
		}
	}

	/* No more partitions; start at the first one and... */
	log_debug("! no more partitions\n");
	iter->part = 0;

	/*
	 * Note: as far as we know, there is no partition table on the next
	 * bootdev, so set max_part to 0 until we discover otherwise. See
	 * bootdev_find_in_blk() for where this is set.
	 */
	iter->max_part = 0;

	/* ...select next bootdev */
	if (iter->flags & BOOTFLOWIF_SINGLE_DEV) {
		ret = -ENOENT;
	} else {
		int method_flags = 0;

		ret = 0;
		dev = iter->dev;
		log_debug("inc_dev=%d\n", inc_dev);
		if (!inc_dev) {
			ret = bootdev_setup_iter(iter, NULL, &dev,
						 &method_flags);
		} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
			   (iter->flags & BOOTFLOWIF_SINGLE_UCLASS)) {
			scan_next_in_uclass(&dev);
			if (!dev) {
				log_debug("finished uclass %s\n",
					  dev_get_uclass_name(dev));
				ret = -ENODEV;
			}
		} else if (IS_ENABLED(CONFIG_BOOTSTD_FULL) &&
			   iter->flags & BOOTFLOWIF_SINGLE_MEDIA) {
			log_debug("next in single\n");
			do {
				/*
				 * Move to the next bootdev child of this media
				 * device. This ensures that we cover all the
				 * available SCSI IDs and LUNs.
				 */
				device_find_next_child(&dev);
				log_debug("- next %s\n",
					dev ? dev->name : "(none)");
			} while (dev && device_get_uclass_id(dev) !=
				UCLASS_BOOTDEV);
			if (!dev) {
				log_debug("finished uclass %s\n",
					  dev_get_uclass_name(dev));
				ret = -ENODEV;
			}
		} else {
			log_debug("labels %p\n", iter->labels);
			if (iter->labels) {
				/*
				 * when the label is "mmc" we want to scan all
				 * mmc bootdevs, not just the first. See
				 * bootdev_find_by_label() where this flag is
				 * set up
				 */
				method_flags = iter->method_flags;
				if (method_flags &
				    BOOTFLOW_METHF_SINGLE_UCLASS) {
					scan_next_in_uclass(&dev);
					log_debug("looking for next device %s: %s\n",
						  iter->dev->name,
						  dev ? dev->name : "<none>");
					method_flags = BOOTFLOW_METHF_SINGLE_UCLASS;
				} else {
					dev = NULL;
				}
				if (!dev) {
					log_debug("looking at next label\n");
					ret = bootdev_next_label(iter, &dev,
								 &method_flags);
				}
			} else {
				ret = bootdev_next_prio(iter, &dev);
			}
		}
		log_debug("ret=%d, dev=%p %s\n", ret, dev,
			  dev ? dev->name : "none");
		if (ret)
			bootflow_iter_set_dev(iter, NULL, 0);
		else
			ret = prepare_bootdev(iter, dev, method_flags, true);
	}

	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && ret) {
		log_debug("no more bootdevs, trying global\n");

		/* allow global bootmeths with any priority */
		iter->cur_prio = BOOTDEVP_COUNT;
		if (!next_glob_bootmeth(iter)) {
			log_debug("-> next method '%s'\n", iter->method->name);
			return 0;
		}
	}

	/* if there are no more bootdevs, give up */
	if (ret) {
		log_debug("-> no more bootdevs\n");
		return log_msg_ret("incr", BF_NO_MORE_DEVICES);
	}

	log_debug("-> bootdev '%s' method '%s'\n", dev->name,
		  iter->method->name);

	return 0;
}

/**
 * bootflow_check() - Check if a bootflow can be obtained
 *
 * @iter: Provides part, bootmeth to use
 * @bflow: Bootflow to update on success
 * Return: 0 if OK, -ENOSYS if there is no bootflow support on this device,
 *	BF_NO_MORE_PARTS if there are no more partitions on bootdev
 */
static int bootflow_check(struct bootflow_iter *iter, struct bootflow *bflow)
{
	struct udevice *dev;
	int ret;

	/* handle global bootmeths if needed */
	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) && iter->doing_global) {
		bootflow_iter_set_dev(iter, NULL, 0);
		ret = bootmeth_get_bootflow(iter->method, bflow);
		if (ret)
			return log_msg_ret("glob", ret);

		return 0;
	}

	dev = iter->dev;
	ret = bootdev_get_bootflow(dev, iter, bflow);

	/* If we got a valid bootflow, return it */
	if (!ret) {
		log_debug("Bootdev '%s' part %d method '%s': Found bootflow\n",
			  dev->name, iter->part, iter->method->name);
		return 0;
	}

	/* Unless there is nothing more to try, move to the next device */
	if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
		log_debug("Bootdev '%s' part %d method '%s': Error %d\n",
			  dev->name, iter->part, iter->method->name, ret);
		/*
		 * For 'all' we return all bootflows, even
		 * those with errors
		 */
		if (iter->flags & BOOTFLOWIF_ALL)
			return log_msg_ret("all", ret);
	}

	return log_msg_ret("check", ret);
}

int bootflow_scan_first(struct udevice *dev, const char *label,
			struct bootflow_iter *iter, int flags,
			struct bootflow *bflow)
{
	int ret;

	if (dev || label)
		flags |= BOOTFLOWIF_SKIP_GLOBAL;
	bootflow_iter_init(iter, flags);

	/*
	 * Set up the ordering of bootmeths. This sets iter->doing_global and
	 * iter->first_glob_method if we are starting with the global bootmeths
	 */
	ret = bootmeth_setup_iter_order(iter, !(flags & BOOTFLOWIF_SKIP_GLOBAL));
	if (ret)
		return log_msg_ret("obmeth", -ENODEV);

	/* Find the first bootmeth (there must be at least one!) */
	iter->method = iter->method_order[iter->cur_method];

	if (!IS_ENABLED(CONFIG_BOOTMETH_GLOBAL) || !iter->doing_global) {
		struct udevice *dev = NULL;
		int method_flags;

		ret = bootdev_setup_iter(iter, label, &dev, &method_flags);
		if (ret)
			return log_msg_ret("obdev", -ENODEV);

		bootflow_iter_set_dev(iter, dev, method_flags);
	}

	if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
		iter->methods_done |= BIT(iter->cur_method);
		log_debug("methods_done now %x\n", iter->cur_method);
	}
	ret = bootflow_check(iter, bflow);
	if (ret) {
		log_debug("check - ret=%d\n", ret);
		iter->err = ret;
		if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
			if (iter->flags & BOOTFLOWIF_ALL)
				return log_msg_ret("all", ret);
		}
		bootflow_free(bflow);
		ret = bootflow_scan_next(iter, bflow);
		if (ret)
			return log_msg_ret("get", ret);
	}

	return 0;
}

int bootflow_scan_next(struct bootflow_iter *iter, struct bootflow *bflow)
{
	int ret;

	do {
		ret = iter_incr(iter);
		log_debug("iter_incr: ret=%d\n", ret);
		if (ret == BF_NO_MORE_DEVICES)
			return log_msg_ret("done", ret);

		if (!ret) {
			if (IS_ENABLED(CONFIG_BOOTMETH_GLOBAL)) {
				iter->methods_done |= BIT(iter->cur_method);
				log_debug("methods_done now %x\n",
					  iter->cur_method);
			}
			ret = bootflow_check(iter, bflow);
			log_debug("check - ret=%d\n", ret);
			if (!ret) {
				iter->err = 0;
				return 0;
			}
			iter->err = ret;
			if (ret != BF_NO_MORE_PARTS && ret != -ENOSYS) {
				if (iter->flags & BOOTFLOWIF_ALL)
					return log_msg_ret("all", ret);
			}
			bootflow_free(bflow);
		} else {
			log_debug("incr failed, err=%d\n", ret);
			iter->err = ret;
		}

	} while (1);
}

void bootflow_init(struct bootflow *bflow, struct udevice *bootdev,
		   struct udevice *meth)
{
	memset(bflow, '\0', sizeof(*bflow));
	bflow->dev = bootdev;
	bflow->method = meth;
	bflow->state = BOOTFLOWST_BASE;
	bflow->bootmeth_id = -1;
	alist_init_struct(&bflow->images, struct bootflow_img);
}

void bootflow_free(struct bootflow *bflow)
{
	struct bootflow_img *img;

	free(bflow->name);
	free(bflow->entry_name);
	free(bflow->subdir);
	free(bflow->fname);
	if (!(bflow->flags & BOOTFLOWF_STATIC_BUF))
		free(bflow->buf);
	free(bflow->os_name);
	free(bflow->fdt_fname);
	/* bootmeth_priv is only set when method is set */
	if (bflow->method)
		bootmeth_free_bootflow(bflow->method, bflow);

	alist_for_each(img, &bflow->images)
		free(img->fname);
	alist_empty(&bflow->images);
}

void bootflow_remove(struct bootflow *bflow)
{
	bootflow_free(bflow);
}

#if CONFIG_IS_ENABLED(BOOTSTD_FULL)
int bootflow_read_all(struct bootflow *bflow)
{
	int ret;

	if (bflow->state != BOOTFLOWST_READY)
		return log_msg_ret("rd", -EPROTO);

	ret = bootmeth_read_all(bflow->method, bflow);
	if (ret)
		return log_msg_ret("rd2", ret);

	return 0;
}
#endif /* BOOTSTD_FULL */

int bootflow_boot(struct bootflow *bflow)
{
	int ret;

	if (bflow->state != BOOTFLOWST_READY)
		return log_msg_ret("load", -EPROTO);

	ret = bootmeth_boot(bflow->method, bflow);
	if (ret)
		return log_msg_ret("boot", ret);

	/*
	 * internal error, should not get here since we should have booted
	 * something or returned an error
	 */

	return log_msg_ret("end", -EFAULT);
}

int bootflow_run_boot(struct bootflow_iter *iter, struct bootflow *bflow)
{
	int ret;

	printf("** Booting bootflow '%s' with %s\n", bflow->name,
	       bflow->method->name);
	if (IS_ENABLED(CONFIG_OF_HAS_PRIOR_STAGE) &&
	    (bflow->flags & BOOTFLOWF_USE_PRIOR_FDT))
		printf("Using prior-stage device tree\n");
	ret = bootflow_boot(bflow);
	if (!IS_ENABLED(CONFIG_BOOTSTD_FULL)) {
		printf("Boot failed (err=%d)\n", ret);
		return ret;
	}

	switch (ret) {
	case -EPROTO:
		printf("Bootflow not loaded (state '%s')\n",
		       bootflow_state_get_name(bflow->state));
		break;
	case -ENOSYS:
		printf("Boot method '%s' not supported\n", bflow->method->name);
		break;
	case -ENOTSUPP:
		/* Disable this bootflow for this iteration */
		if (iter) {
			int ret2;

			ret2 = bootflow_iter_drop_bootmeth(iter, bflow->method);
			if (!ret2) {
				printf("Boot method '%s' failed and will not be retried\n",
				       bflow->method->name);
			}
		}

		break;
	default:
		printf("Boot failed (err=%d)\n", ret);
		break;
	}

	return ret;
}

int bootflow_iter_check_blk(const struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
	if (id != UCLASS_ETH && id != UCLASS_BOOTSTD && id != UCLASS_QFW)
		return 0;

	return -ENOTSUPP;
}

int bootflow_iter_check_mmc(const struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
	if (id == UCLASS_MMC)
		return 0;

	return -ENOTSUPP;
}

int bootflow_iter_check_sf(const struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
	if (id == UCLASS_SPI_FLASH)
		return 0;

	return -ENOTSUPP;
}

int bootflow_iter_check_net(const struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
	if (id == UCLASS_ETH)
		return 0;

	return -ENOTSUPP;
}

int bootflow_iter_check_system(const struct bootflow_iter *iter)
{
	const struct udevice *media = dev_get_parent(iter->dev);
	enum uclass_id id = device_get_uclass_id(media);

	log_debug("uclass %d: %s\n", id, uclass_get_name(id));
	if (id == UCLASS_BOOTSTD)
		return 0;

	return -ENOTSUPP;
}

/**
 * bootflow_cmdline_set() - Set the command line for a bootflow
 *
 * @value: New command-line string
 * Returns 0 if OK, -ENOENT if no current bootflow, -ENOMEM if out of memory
 */
int bootflow_cmdline_set(struct bootflow *bflow, const char *value)
{
	char *cmdline = NULL;

	if (value) {
		cmdline = strdup(value);
		if (!cmdline)
			return -ENOMEM;
	}

	free(bflow->cmdline);
	bflow->cmdline = cmdline;

	return 0;
}

#ifdef CONFIG_BOOTSTD_FULL
/**
 * on_bootargs() - Update the cmdline of a bootflow
 */
static int on_bootargs(const char *name, const char *value, enum env_op op,
		       int flags)
{
	struct bootstd_priv *std;
	struct bootflow *bflow;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return 0;
	bflow = std->cur_bootflow;
	if (!bflow)
		return 0;

	switch (op) {
	case env_op_create:
	case env_op_overwrite:
		ret = bootflow_cmdline_set(bflow, value);
		if (ret && ret != ENOENT)
			return 1;
		return 0;
	case env_op_delete:
		bootflow_cmdline_set(bflow, NULL);
		fallthrough;
	default:
		return 0;
	}
}
U_BOOT_ENV_CALLBACK(bootargs, on_bootargs);
#endif

/**
 * copy_in() - Copy a string into a cmdline buffer
 *
 * @buf: Buffer to copy into
 * @end: End of buffer (pointer to char after the end)
 * @arg: String to copy from
 * @len: Number of chars to copy from @arg (note that this is not usually the
 * sane as strlen(arg) since the string may contain following arguments)
 * @new_val: Value to put after arg, or BOOTFLOWCL_EMPTY to use an empty value
 * with no '=' sign
 * Returns: Number of chars written to @buf
 */
static int copy_in(char *buf, char *end, const char *arg, int len,
		   const char *new_val)
{
	char *to = buf;

	/* copy the arg name */
	if (to + len >= end)
		return -E2BIG;
	memcpy(to, arg, len);
	to += len;

	if (new_val == BOOTFLOWCL_EMPTY) {
		/* no value */
	} else {
		bool need_quote = strchr(new_val, ' ');
		len = strlen(new_val);

		/* need space for value, equals sign and maybe two quotes */
		if (to + 1 + (need_quote ? 2 : 0) + len >= end)
			return -E2BIG;
		*to++ = '=';
		if (need_quote)
			*to++ = '"';
		memcpy(to, new_val, len);
		to += len;
		if (need_quote)
			*to++ = '"';
	}

	return to - buf;
}

int cmdline_set_arg(char *buf, int maxlen, const char *cmdline,
		    const char *set_arg, const char *new_val, int *posp)
{
	bool found_arg = false;
	const char *from;
	char *to, *end;
	int set_arg_len;
	char empty = '\0';
	int ret;

	from = cmdline ?: &empty;

	/* check if the value has quotes inside */
	if (new_val && new_val != BOOTFLOWCL_EMPTY && strchr(new_val, '"'))
		return -EBADF;

	set_arg_len = strlen(set_arg);
	for (to = buf, end = buf + maxlen; *from;) {
		const char *val, *arg_end, *val_end, *p;
		bool in_quote;

		if (to >= end)
			return -E2BIG;
		while (*from == ' ')
			from++;
		if (!*from)
			break;

		/* find the end of this arg */
		val = NULL;
		arg_end = NULL;
		val_end = NULL;
		in_quote = false;
		for (p = from;; p++) {
			if (in_quote) {
				if (!*p)
					return -EINVAL;
				if (*p == '"')
					in_quote = false;
				continue;
			}
			if (*p == '=' && !arg_end) {
				arg_end = p;
				val = p + 1;
			} else if (*p == '"') {
				in_quote = true;
			} else if (!*p || *p == ' ') {
				val_end = p;
				if (!arg_end)
					arg_end = p;
				break;
			}
		}
		/*
		 * At this point val_end points to the end of the value, or the
		 * last char after the arg name, if there is no label.
		 * arg_end is the char after the arg name
		 * val points to the value, or NULL if there is none
		 * char after the value.
		 *
		 *        fred=1234
		 *        ^   ^^   ^
		 *      from  ||   |
		 *           / \    \
		 *    arg_end  val   val_end
		 */
		log_debug("from %s arg_end %ld val %ld val_end %ld\n", from,
			  (long)(arg_end - from), (long)(val - from),
			  (long)(val_end - from));

		if (to != buf) {
			if (to >= end)
				return -E2BIG;
			*to++ = ' ';
		}

		/* if this is the target arg, update it */
		if (arg_end - from == set_arg_len &&
		    !strncmp(from, set_arg, set_arg_len)) {
			if (!buf) {
				bool has_quote = val_end[-1] == '"';

				/*
				 * exclude any start/end quotes from
				 * calculations
				 */
				if (!val)
					val = val_end;
				*posp = val - cmdline + has_quote;
				return val_end - val - 2 * has_quote;
			}
			found_arg = true;
			if (!new_val) {
				/* delete this arg */
				from = val_end + (*val_end == ' ');
				log_debug("delete from: %s\n", from);
				if (to != buf)
					to--; /* drop the space we added */
				continue;
			}

			ret = copy_in(to, end, from, arg_end - from, new_val);
			if (ret < 0)
				return ret;
			to += ret;

		/* if not the target arg, copy it unchanged */
		} else if (to) {
			int len;

			len = val_end - from;
			if (to + len >= end)
				return -E2BIG;
			memcpy(to, from, len);
			to += len;
		}
		from = val_end;
	}

	/* If we didn't find the arg, add it */
	if (!found_arg) {
		/* trying to delete something that is not there */
		if (!new_val || !buf)
			return -ENOENT;
		if (to >= end)
			return -E2BIG;

		/* add a space to separate it from the previous arg */
		if (to != buf && to[-1] != ' ')
			*to++ = ' ';
		ret = copy_in(to, end, set_arg, set_arg_len, new_val);
		log_debug("ret=%d, to: %s buf: %s\n", ret, to, buf);
		if (ret < 0)
			return ret;
		to += ret;
	}

	/* delete any trailing space */
	if (to > buf && to[-1] == ' ')
		to--;

	if (to >= end)
		return -E2BIG;
	*to++ = '\0';

	return to - buf;
}

int bootflow_cmdline_set_arg(struct bootflow *bflow, const char *set_arg,
			     const char *new_val, bool set_env)
{
	char buf[2048];
	char *cmd = NULL;
	int ret;

	ret = cmdline_set_arg(buf, sizeof(buf), bflow->cmdline, set_arg,
			      new_val, NULL);
	if (ret < 0)
		return ret;

	ret = bootflow_cmdline_set(bflow, buf);
	if (*buf) {
		cmd = strdup(buf);
		if (!cmd)
			return -ENOMEM;
	}
	free(bflow->cmdline);
	bflow->cmdline = cmd;

	if (set_env) {
		ret = env_set("bootargs", bflow->cmdline);
		if (ret)
			return ret;
	}

	return 0;
}

int cmdline_get_arg(const char *cmdline, const char *arg, int *posp)
{
	int ret;

	ret = cmdline_set_arg(NULL, 1, cmdline, arg, NULL, posp);

	return ret;
}

int bootflow_cmdline_get_arg(struct bootflow *bflow, const char *arg,
			     const char **val)
{
	int ret;
	int pos;

	ret = cmdline_get_arg(bflow->cmdline, arg, &pos);
	if (ret < 0)
		return ret;
	*val = bflow->cmdline + pos;

	return ret;
}

int bootflow_cmdline_auto(struct bootflow *bflow, const char *arg)
{
	struct serial_device_info info;
	char buf[50];
	int ret;

	ret = serial_getinfo(gd->cur_serial_dev, &info);
	if (ret)
		return ret;

	*buf = '\0';
	if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
		snprintf(buf, sizeof(buf),
			 "uart8250,%s,%#lx,%dn8",
			 info.addr_space == SERIAL_ADDRESS_SPACE_IO ? "io" :
			 "mmio", info.addr, info.baudrate);
	} else if (!strcmp("earlycon", arg) && info.type == SERIAL_CHIP_PL01X) {
		snprintf(buf, sizeof(buf),
			 "pl011,mmio32,%#lx,%dn8", info.addr,
			 info.baudrate);
	} else if (!strcmp("console", arg) && info.type == SERIAL_CHIP_16550_COMPATIBLE) {
		snprintf(buf, sizeof(buf),
			 "ttyS0,%dn8", info.baudrate);
	}

	if (!*buf) {
		printf("Unknown param '%s'\n", arg);
		return -ENOENT;
	}

	ret = bootflow_cmdline_set_arg(bflow, arg, buf, true);
	if (ret)
		return ret;

	return 0;
}

const char *bootflow_img_type_name(enum bootflow_img_t type)
{
	const char *name;

	if (type >= BFI_FIRST && type < BFI_COUNT)
		name = bootflow_img[type - BFI_FIRST];
	else
		name = genimg_get_type_short_name(type);

	return name;
}

struct bootflow_img *bootflow_img_add(struct bootflow *bflow, const char *fname,
				      enum bootflow_img_t type, ulong addr,
				      ulong size)
{
	struct bootflow_img img, *ptr;

	memset(&img, '\0', sizeof(struct bootflow_img));
	img.fname = strdup(fname);
	if (!img.fname)
		return NULL;

	img.type = type;
	img.addr = addr;
	img.size = size;
	ptr = alist_add(&bflow->images, img);
	if (!ptr)
		return NULL;

	return ptr;
}

struct bootflow_img *bootflow_img_findw(const struct bootflow *bflow,
					enum bootflow_img_t type)
{
	struct bootflow_img *img;

	alist_for_each(img, &bflow->images) {
		if (img->type == type)
			return img;
	}

	return NULL;
}

int bootflow_get_seq(const struct bootflow *bflow)
{
	struct bootstd_priv *std;
	int ret;

	ret = bootstd_get_priv(&std);
	if (ret)
		return ret;

	return alist_calc_index(&std->bootflows, bflow);
}

const char *bootflow_guess_label(const struct bootflow *bflow)
{
	const char *name = NULL;

	if (IS_ENABLED(CONFIG_EFI_APP)) {
		struct efi_device_path *dp;
		enum uclass_id id;
		int ret;

		ret = efi_dp_from_bootflow(bflow, &dp, NULL);
		if (!ret)
			name = efi_dp_guess_uclass(dp, &id);
	} else if (bflow->dev) {
		struct udevice *media = dev_get_parent(bflow->dev);

		if (device_get_uclass_id(media) == UCLASS_MASS_STORAGE)
			name = "usb";
		else
			name = dev_get_uclass_name(media);
	}
	if (!name)
		name = "(none)";

	return name;
}