Merge branch '2022-04-25-initial-implementation-of-stdboot'
[platform/kernel/u-boot.git] / cmd / bootmeth.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * 'bootmeth' command
4  *
5  * Copyright 2021 Google LLC
6  * Written by Simon Glass <sjg@chromium.org>
7  */
8
9 #include <common.h>
10 #include <bootdev.h>
11 #include <bootmeth.h>
12 #include <bootstd.h>
13 #include <command.h>
14 #include <dm.h>
15 #include <malloc.h>
16 #include <dm/uclass-internal.h>
17
18 static int do_bootmeth_list(struct cmd_tbl *cmdtp, int flag, int argc,
19                             char *const argv[])
20 {
21         struct bootstd_priv *std;
22         struct udevice *dev;
23         bool use_order;
24         bool all = false;
25         int ret;
26         int i;
27
28         if (argc > 1 && *argv[1] == '-') {
29                 all = strchr(argv[1], 'a');
30                 argc--;
31                 argv++;
32         }
33
34         ret = bootstd_get_priv(&std);
35         if (ret) {
36                 printf("Cannot get bootstd (err=%d)\n", ret);
37                 return CMD_RET_FAILURE;
38         }
39
40         printf("Order  Seq  Name                Description\n");
41         printf("-----  ---  ------------------  ------------------\n");
42
43         /*
44          * Use the ordering if we have one, so long as we are not trying to list
45          * all bootmethds
46          */
47         use_order = std->bootmeth_count && !all;
48         if (use_order)
49                 dev = std->bootmeth_order[0];
50         else
51                 ret = uclass_find_first_device(UCLASS_BOOTMETH, &dev);
52
53         for (i = 0; dev;) {
54                 struct bootmeth_uc_plat *ucp = dev_get_uclass_plat(dev);
55                 int order = i;
56
57                 /*
58                  * With the -a flag we may list bootdevs that are not in the
59                  * ordering. Find their place in the order
60                  */
61                 if (all && std->bootmeth_count) {
62                         int j;
63
64                         /* Find the position of this bootmeth in the order */
65                         order = -1;
66                         for (j = 0; j < std->bootmeth_count; j++) {
67                                 if (std->bootmeth_order[j] == dev)
68                                         order = j;
69                         }
70                 }
71
72                 if (order == -1)
73                         printf("%5s", "-");
74                 else
75                         printf("%5x", order);
76                 printf("  %3x  %-19.19s %s\n", dev_seq(dev), dev->name,
77                        ucp->desc);
78                 i++;
79                 if (use_order)
80                         dev = std->bootmeth_order[i];
81                 else
82                         uclass_find_next_device(&dev);
83         }
84         printf("-----  ---  ------------------  ------------------\n");
85         printf("(%d bootmeth%s)\n", i, i != 1 ? "s" : "");
86
87         return 0;
88 }
89
90 static int do_bootmeth_order(struct cmd_tbl *cmdtp, int flag, int argc,
91                              char *const argv[])
92 {
93         int ret;
94
95         ret = bootmeth_set_order(argv[1]);
96         if (ret) {
97                 printf("Failed (err=%d)\n", ret);
98                 return CMD_RET_FAILURE;
99         }
100         env_set("bootmeths", argv[1]);
101
102         return 0;
103 }
104
105 #ifdef CONFIG_SYS_LONGHELP
106 static char bootmeth_help_text[] =
107         "list [-a]     - list available bootmeths (-a all)\n"
108         "bootmeth order [<bd> ...]  - select bootmeth order / subset to use";
109 #endif
110
111 U_BOOT_CMD_WITH_SUBCMDS(bootmeth, "Boot methods", bootmeth_help_text,
112         U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootmeth_list),
113         U_BOOT_SUBCMD_MKENT(order, CONFIG_SYS_MAXARGS, 1, do_bootmeth_order));