Skip to content

Commit bd90b09

Browse files
sjg20trini
authored andcommitted
bootstd: Add the concept of a bootdev hunter
Some bootdevs must be enumerated before they appear. For example, USB bootdevs are not visible until USB is enumerated. With standard boot this needs to happen automatically, since we only want to enumerate a bus if it is needed. Add a way to define bootdev 'hunters' which can be used to hunt for bootdevs of a given type. Track which ones have been used and add a command to list them. Include a clang work-around which seems to be needed. Signed-off-by: Simon Glass <sjg@chromium.org>
1 parent 3722cc9 commit bd90b09

5 files changed

Lines changed: 176 additions & 3 deletions

File tree

boot/bootdev-uclass.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,37 @@ int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp)
636636
return 0;
637637
}
638638

639+
void bootdev_list_hunters(struct bootstd_priv *std)
640+
{
641+
struct bootdev_hunter *orig, *start;
642+
int n_ent, i;
643+
644+
orig = ll_entry_start(struct bootdev_hunter, bootdev_hunter);
645+
n_ent = ll_entry_count(struct bootdev_hunter, bootdev_hunter);
646+
647+
/*
648+
* workaround for strange bug in clang-12 which sees all the below data
649+
* as zeroes. Any access of start seems to fix it, such as
650+
*
651+
* printf("%p", start);
652+
*
653+
* Use memcpy() to force the correct behaviour.
654+
*/
655+
memcpy(&start, &orig, sizeof(orig));
656+
printf("%4s %4s %-15s %s\n", "Prio", "Used", "Uclass", "Hunter");
657+
printf("%4s %4s %-15s %s\n", "----", "----", "---------------", "---------------");
658+
for (i = 0; i < n_ent; i++) {
659+
struct bootdev_hunter *info = start + i;
660+
661+
printf("%4d %4s %-15s %s\n", info->prio,
662+
std->hunters_used & BIT(i) ? "*" : "",
663+
uclass_get_name(info->uclass),
664+
info->drv ? info->drv->name : "(none)");
665+
}
666+
667+
printf("(total hunters: %d)\n", n_ent);
668+
}
669+
639670
static int bootdev_post_bind(struct udevice *dev)
640671
{
641672
struct bootdev_uc_plat *ucp = dev_get_uclass_plat(dev);

cmd/bootdev.c

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,43 @@ static int do_bootdev_info(struct cmd_tbl *cmdtp, int flag, int argc,
107107
return 0;
108108
}
109109

110+
static int do_bootdev_hunt(struct cmd_tbl *cmdtp, int flag, int argc,
111+
char *const argv[])
112+
{
113+
struct bootstd_priv *priv;
114+
const char *spec = NULL;
115+
bool list = false;
116+
int ret = 0;
117+
118+
if (argc >= 2) {
119+
if (!strcmp(argv[1], "-l"))
120+
list = true;
121+
else
122+
spec = argv[1];
123+
}
124+
125+
ret = bootstd_get_priv(&priv);
126+
if (ret)
127+
return ret;
128+
if (list) {
129+
bootdev_list_hunters(priv);
130+
} else {
131+
/* TODO: implement hunting */
132+
}
133+
134+
return 0;
135+
}
136+
110137
#ifdef CONFIG_SYS_LONGHELP
111138
static char bootdev_help_text[] =
112-
"list [-p] - list all available bootdevs (-p to probe)\n"
113-
"bootdev select <bd> - select a bootdev by name | label | seq\n"
114-
"bootdev info [-p] - show information about a bootdev (-p to probe)";
139+
"list [-p] - list all available bootdevs (-p to probe)\n"
140+
"bootdev hunt [-l|<spec>] - use hunt drivers to find bootdevs\n"
141+
"bootdev select <bd> - select a bootdev by name | label | seq\n"
142+
"bootdev info [-p] - show information about a bootdev (-p to probe)";
115143
#endif
116144

117145
U_BOOT_CMD_WITH_SUBCMDS(bootdev, "Boot devices", bootdev_help_text,
118146
U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootdev_list),
147+
U_BOOT_SUBCMD_MKENT(hunt, 2, 1, do_bootdev_hunt),
119148
U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootdev_select),
120149
U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootdev_info));

include/bootdev.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
struct bootflow;
1313
struct bootflow_iter;
14+
struct bootstd_priv;
1415
struct udevice;
1516

1617
/**
@@ -33,6 +34,53 @@ enum bootdev_prio_t {
3334
BOOTDEVP_COUNT,
3435
};
3536

37+
struct bootdev_hunter;
38+
39+
/**
40+
* bootdev_hunter_func - function to probe for bootdevs of a given type
41+
*
42+
* This should hunt around for bootdevs of the given type, binding them as it
43+
* finds them. This may involve bus enumeration, etc.
44+
*
45+
* @info: Info structure describing this hunter
46+
* @show: true to show information from the hunter
47+
* Returns: 0 if OK, -ve on error
48+
*/
49+
typedef int (*bootdev_hunter_func)(struct bootdev_hunter *info, bool show);
50+
51+
/**
52+
* struct bootdev_hunter - information about how to hunt for bootdevs
53+
*
54+
* @prio: Scanning priority of this hunter
55+
* @uclass: Uclass ID for the media associated with this bootdev
56+
* @drv: bootdev driver for the things found by this hunter
57+
* @hunt: Function to call to hunt for bootdevs of this type (NULL if none)
58+
*
59+
* Some bootdevs are not visible until other devices are enumerated. For
60+
* example, USB bootdevs only appear when the USB bus is enumerated.
61+
*
62+
* On the other hand, we don't always want to enumerate all the buses just to
63+
* find the first valid bootdev. Ideally we want to work through them in
64+
* priority order, so that the fastest bootdevs are discovered first.
65+
*
66+
* This struct holds information about the bootdev so we can determine the probe
67+
* order and how to hunt for bootdevs of this type
68+
*/
69+
struct bootdev_hunter {
70+
enum bootdev_prio_t prio;
71+
enum uclass_id uclass;
72+
struct driver *drv;
73+
bootdev_hunter_func hunt;
74+
};
75+
76+
/* declare a new bootdev hunter */
77+
#define BOOTDEV_HUNTER(__name) \
78+
ll_entry_declare(struct bootdev_hunter, __name, bootdev_hunter)
79+
80+
/* access a bootdev hunter by name */
81+
#define BOOTDEV_HUNTER_GET(__name) \
82+
ll_entry_get(struct bootdev_hunter, __name, bootdev_hunter)
83+
3684
/**
3785
* struct bootdev_uc_plat - uclass information about a bootdev
3886
*
@@ -205,6 +253,16 @@ int bootdev_find_by_any(const char *name, struct udevice **devp);
205253
*/
206254
int bootdev_setup_iter_order(struct bootflow_iter *iter, struct udevice **devp);
207255

256+
/**
257+
* bootdev_list_hunters() - List the available bootdev hunters
258+
*
259+
* These provide a way to find new bootdevs by enumerating buses, etc. This
260+
* function lists the available hunters
261+
*
262+
* @std: Pointer to bootstd private info
263+
*/
264+
void bootdev_list_hunters(struct bootstd_priv *std);
265+
208266
#if CONFIG_IS_ENABLED(BOOTSTD)
209267
/**
210268
* bootdev_setup_for_dev() - Bind a new bootdev device (deprecated)

include/bootstd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ struct udevice;
3333
* @bootmeth_order: List of bootmeth devices to use, in order, NULL-terminated
3434
* @vbe_bootmeth: Currently selected VBE bootmeth, NULL if none
3535
* @theme: Node containing the theme information
36+
* @hunters_used: Bitmask of used hunters, indexed by their position in the
37+
* linker list. The bit is set if the hunter has been used already
3638
*/
3739
struct bootstd_priv {
3840
const char **prefixes;
@@ -45,6 +47,7 @@ struct bootstd_priv {
4547
struct udevice **bootmeth_order;
4648
struct udevice *vbe_bootmeth;
4749
ofnode theme;
50+
uint hunters_used;
4851
};
4952

5053
/**

test/boot/bootdev.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,55 @@ static int bootdev_test_prio(struct unit_test_state *uts)
221221
return 0;
222222
}
223223
BOOTSTD_TEST(bootdev_test_prio, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
224+
225+
/* Check listing hunters */
226+
static int bootdev_test_hunter(struct unit_test_state *uts)
227+
{
228+
struct bootstd_priv *std;
229+
230+
/* get access to the used hunters */
231+
ut_assertok(bootstd_get_priv(&std));
232+
233+
console_record_reset_enable();
234+
bootdev_list_hunters(std);
235+
ut_assert_nextline("Prio Used Uclass Hunter");
236+
ut_assert_nextlinen("----");
237+
ut_assert_nextline("(total hunters: 0)");
238+
ut_assert_console_end();
239+
240+
return 0;
241+
}
242+
BOOTSTD_TEST(bootdev_test_hunter, UT_TESTF_DM | UT_TESTF_SCAN_FDT);
243+
244+
/* Check 'bootdev hunt' command */
245+
static int bootdev_test_cmd_hunt(struct unit_test_state *uts)
246+
{
247+
struct bootstd_priv *std;
248+
249+
/* get access to the used hunters */
250+
ut_assertok(bootstd_get_priv(&std));
251+
252+
console_record_reset_enable();
253+
ut_assertok(run_command("bootdev hunt -l", 0));
254+
ut_assert_nextline("Prio Used Uclass Hunter");
255+
ut_assert_nextlinen("----");
256+
ut_assert_nextline("(total hunters: 0)");
257+
ut_assert_console_end();
258+
259+
/* Scan all hunters */
260+
ut_assertok(run_command("bootdev hunt", 0));
261+
ut_assert_console_end();
262+
263+
/* List available hunters */
264+
ut_assertok(run_command("bootdev hunt -l", 0));
265+
ut_assert_nextlinen("Prio");
266+
ut_assert_nextlinen("----");
267+
ut_assert_nextline("(total hunters: 0)");
268+
ut_assert_console_end();
269+
270+
ut_asserteq(0, std->hunters_used);
271+
272+
return 0;
273+
}
274+
BOOTSTD_TEST(bootdev_test_cmd_hunt, UT_TESTF_DM | UT_TESTF_SCAN_FDT |
275+
UT_TESTF_ETH_BOOTDEV);

0 commit comments

Comments
 (0)