blob: 6a37fa8fa68f9bb62aa3cf884d39d4fe4f6ba472 [file] [log] [blame]
Simon Glass0a2f6a32023-01-06 08:52:40 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Provide a menu of available bootflows and related options
4 *
5 * Copyright 2022 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#define LOG_CATEGORY UCLASS_BOOTSTD
10
Simon Glass0a2f6a32023-01-06 08:52:40 -060011#include <bootflow.h>
Quentin Schulz21a6aec2024-06-12 16:58:49 +020012#include <bootmeth.h>
Simon Glass0a2f6a32023-01-06 08:52:40 -060013#include <bootstd.h>
14#include <cli.h>
15#include <dm.h>
16#include <expo.h>
17#include <malloc.h>
18#include <menu.h>
19#include <video_console.h>
20#include <watchdog.h>
21#include <linux/delay.h>
22#include "bootflow_internal.h"
23
24/**
25 * struct menu_priv - information about the menu
26 *
27 * @num_bootflows: Number of bootflows in the menu
Simon Glass9f5a7922025-05-02 08:46:29 -060028 * @last_bootdev: bootdev of the last bootflow added to the menu, NULL if none
Simon Glass0a2f6a32023-01-06 08:52:40 -060029 */
30struct menu_priv {
31 int num_bootflows;
Simon Glass9f5a7922025-05-02 08:46:29 -060032 struct udevice *last_bootdev;
Simon Glass0a2f6a32023-01-06 08:52:40 -060033};
34
35int bootflow_menu_new(struct expo **expp)
36{
Simon Glass0a2f6a32023-01-06 08:52:40 -060037 struct scene_obj_menu *menu;
38 struct menu_priv *priv;
Simon Glass0a2f6a32023-01-06 08:52:40 -060039 struct scene *scn;
40 struct expo *exp;
41 void *logo;
Simon Glassd9da19a2025-05-02 08:46:28 -060042 int ret;
Simon Glass0a2f6a32023-01-06 08:52:40 -060043
44 priv = calloc(1, sizeof(*priv));
45 if (!priv)
46 return log_msg_ret("prv", -ENOMEM);
47
48 ret = expo_new("bootflows", priv, &exp);
49 if (ret)
50 return log_msg_ret("exp", ret);
51
52 ret = scene_new(exp, "main", MAIN, &scn);
53 if (ret < 0)
54 return log_msg_ret("scn", ret);
55
56 ret |= scene_txt_str(scn, "prompt", OBJ_PROMPT, STR_PROMPT,
57 "UP and DOWN to choose, ENTER to select", NULL);
58
59 ret = scene_menu(scn, "main", OBJ_MENU, &menu);
60 ret |= scene_obj_set_pos(scn, OBJ_MENU, MARGIN_LEFT, 100);
61 ret |= scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
62 "U-Boot - Boot Menu", NULL);
63 ret |= scene_menu_set_title(scn, OBJ_MENU, OBJ_PROMPT);
64
65 logo = video_get_u_boot_logo();
66 if (logo) {
67 ret |= scene_img(scn, "ulogo", OBJ_U_BOOT_LOGO, logo, NULL);
68 ret |= scene_obj_set_pos(scn, OBJ_U_BOOT_LOGO, -4, 4);
69 }
70
71 ret |= scene_txt_str(scn, "cur_item", OBJ_POINTER, STR_POINTER, ">",
72 NULL);
73 ret |= scene_menu_set_pointer(scn, OBJ_MENU, OBJ_POINTER);
74 if (ret < 0)
75 return log_msg_ret("new", -EINVAL);
76
Simon Glassd9da19a2025-05-02 08:46:28 -060077 *expp = exp;
78
79 return 0;
80}
81
Simon Glass9f5a7922025-05-02 08:46:29 -060082int bootflow_menu_add(struct expo *exp, struct bootflow *bflow, int seq,
83 struct scene **scnp)
Simon Glassd9da19a2025-05-02 08:46:28 -060084{
85 struct menu_priv *priv = exp->priv;
Simon Glass9f5a7922025-05-02 08:46:29 -060086 char str[2], *label, *key;
Simon Glassd9da19a2025-05-02 08:46:28 -060087 struct scene *scn;
Simon Glass9f5a7922025-05-02 08:46:29 -060088 uint preview_id;
Simon Glassd9da19a2025-05-02 08:46:28 -060089 uint scene_id;
Simon Glass9f5a7922025-05-02 08:46:29 -060090 bool add_gap;
91 int ret;
Simon Glassd9da19a2025-05-02 08:46:28 -060092
93 ret = expo_first_scene_id(exp);
94 if (ret < 0)
95 return log_msg_ret("scn", ret);
96 scene_id = ret;
97 scn = expo_lookup_scene_id(exp, scene_id);
98
Simon Glass9f5a7922025-05-02 08:46:29 -060099 *str = seq < 10 ? '0' + seq : 'A' + seq - 10;
100 str[1] = '\0';
101 key = strdup(str);
102 if (!key)
103 return log_msg_ret("key", -ENOMEM);
104 label = strdup(dev_get_parent(bflow->dev)->name);
105 if (!label) {
106 free(key);
107 return log_msg_ret("nam", -ENOMEM);
108 }
109
110 add_gap = priv->last_bootdev != bflow->dev;
111 priv->last_bootdev = bflow->dev;
112
113 ret = expo_str(exp, "prompt", STR_POINTER, ">");
114 ret |= scene_txt_str(scn, "label", ITEM_LABEL + seq,
115 STR_LABEL + seq, label, NULL);
116 ret |= scene_txt_str(scn, "desc", ITEM_DESC + seq, STR_DESC + seq,
117 bflow->os_name ? bflow->os_name :
118 bflow->name, NULL);
119 ret |= scene_txt_str(scn, "key", ITEM_KEY + seq, STR_KEY + seq, key,
120 NULL);
121 preview_id = 0;
122 if (bflow->logo) {
123 preview_id = ITEM_PREVIEW + seq;
124 ret |= scene_img(scn, "preview", preview_id,
125 bflow->logo, NULL);
126 }
127 ret |= scene_menuitem(scn, OBJ_MENU, "item", ITEM + seq,
128 ITEM_KEY + seq, ITEM_LABEL + seq,
129 ITEM_DESC + seq, preview_id,
130 add_gap ? SCENEMIF_GAP_BEFORE : 0,
131 NULL);
132
133 if (ret < 0)
134 return log_msg_ret("itm", -EINVAL);
135 priv->num_bootflows++;
136 *scnp = scn;
137
138 return 0;
139}
140
141int bootflow_menu_add_all(struct expo *exp)
142{
143 struct bootflow *bflow;
144 struct scene *scn;
145 int ret, i;
146
Simon Glass0a2f6a32023-01-06 08:52:40 -0600147 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
148 ret = bootflow_next_glob(&bflow), i++) {
Quentin Schulz21a6aec2024-06-12 16:58:49 +0200149 struct bootmeth_uc_plat *ucp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600150
151 if (bflow->state != BOOTFLOWST_READY)
152 continue;
153
Quentin Schulz21a6aec2024-06-12 16:58:49 +0200154 /* No media to show for BOOTMETHF_GLOBAL bootmeths */
155 ucp = dev_get_uclass_plat(bflow->method);
156 if (ucp->flags & BOOTMETHF_GLOBAL)
157 continue;
158
Simon Glass9f5a7922025-05-02 08:46:29 -0600159 ret = bootflow_menu_add(exp, bflow, i, &scn);
160 if (ret)
161 return log_msg_ret("bao", ret);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600162
Simon Glass0a2f6a32023-01-06 08:52:40 -0600163 }
164
Simon Glassd7e32a82023-06-01 10:22:35 -0600165 ret = scene_arrange(scn);
166 if (ret)
167 return log_msg_ret("arr", ret);
168
Simon Glass0a2f6a32023-01-06 08:52:40 -0600169 return 0;
170}
171
Simon Glassd92bcc42023-01-06 08:52:42 -0600172int bootflow_menu_apply_theme(struct expo *exp, ofnode node)
173{
174 struct menu_priv *priv = exp->priv;
175 struct scene *scn;
176 u32 font_size;
177 int ret;
178
179 log_debug("Applying theme %s\n", ofnode_get_name(node));
180 scn = expo_lookup_scene_id(exp, MAIN);
181 if (!scn)
182 return log_msg_ret("scn", -ENOENT);
183
184 /* Avoid error-checking optional items */
185 if (!ofnode_read_u32(node, "font-size", &font_size)) {
186 int i;
187
188 log_debug("font size %d\n", font_size);
189 scene_txt_set_font(scn, OBJ_PROMPT, NULL, font_size);
190 scene_txt_set_font(scn, OBJ_POINTER, NULL, font_size);
191 for (i = 0; i < priv->num_bootflows; i++) {
192 ret = scene_txt_set_font(scn, ITEM_DESC + i, NULL,
193 font_size);
194 if (ret)
195 return log_msg_ret("des", ret);
196 scene_txt_set_font(scn, ITEM_KEY + i, NULL, font_size);
197 scene_txt_set_font(scn, ITEM_LABEL + i, NULL,
198 font_size);
199 }
200 }
201
202 ret = scene_arrange(scn);
203 if (ret)
204 return log_msg_ret("arr", ret);
205
206 return 0;
207}
208
Simon Glassd7cef832025-05-02 08:46:27 -0600209int bootflow_menu_start(struct bootstd_priv *std, bool text_mode,
210 struct expo **expp)
Simon Glass0a2f6a32023-01-06 08:52:40 -0600211{
Simon Glass0a2f6a32023-01-06 08:52:40 -0600212 struct udevice *dev;
213 struct expo *exp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600214 int ret;
215
Simon Glass0a2f6a32023-01-06 08:52:40 -0600216 ret = bootflow_menu_new(&exp);
217 if (ret)
Simon Glassd9da19a2025-05-02 08:46:28 -0600218 return log_msg_ret("bmn", ret);
219 ret = bootflow_menu_add_all(exp);
220 if (ret)
221 return log_msg_ret("bma", ret);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600222
Simon Glassd92bcc42023-01-06 08:52:42 -0600223 if (ofnode_valid(std->theme)) {
224 ret = bootflow_menu_apply_theme(exp, std->theme);
225 if (ret)
226 return log_msg_ret("thm", ret);
227 }
228
Simon Glass0a2f6a32023-01-06 08:52:40 -0600229 /* For now we only support a video console */
230 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
231 if (ret)
232 return log_msg_ret("vid", ret);
233 ret = expo_set_display(exp, dev);
234 if (ret)
235 return log_msg_ret("dis", ret);
236
237 ret = expo_set_scene_id(exp, MAIN);
238 if (ret)
239 return log_msg_ret("scn", ret);
240
241 if (text_mode)
Simon Glassb2c40342023-06-01 10:22:37 -0600242 expo_set_text_mode(exp, text_mode);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600243
Simon Glassd7cef832025-05-02 08:46:27 -0600244 *expp = exp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600245
Simon Glassd7cef832025-05-02 08:46:27 -0600246 return 0;
247}
248
249int bootflow_menu_poll(struct expo *exp, struct bootflow **bflowp)
250{
251 struct bootflow *sel_bflow;
252 struct expo_action act;
253 int ret;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600254
Simon Glassd7cef832025-05-02 08:46:27 -0600255 sel_bflow = NULL;
256 *bflowp = NULL;
257
258 ret = expo_poll(exp, &act);
259 if (ret)
260 return log_msg_ret("bmp", ret);
261
262 switch (act.type) {
263 case EXPOACT_SELECT: {
Simon Glass0a2f6a32023-01-06 08:52:40 -0600264 struct bootflow *bflow;
265 int i;
266
267 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
268 ret = bootflow_next_glob(&bflow), i++) {
Simon Glassd7cef832025-05-02 08:46:27 -0600269 if (i == act.select.id - ITEM) {
270 *bflowp = bflow;
271 // printf("found %p\n", bflow);
272 return 0;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600273 }
274 }
Simon Glassd7cef832025-05-02 08:46:27 -0600275 break;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600276 }
Simon Glassd7cef832025-05-02 08:46:27 -0600277 case EXPOACT_POINT_ITEM: {
278 struct scene *scn = expo_lookup_scene_id(exp, MAIN);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600279
Simon Glassd7cef832025-05-02 08:46:27 -0600280 if (!scn)
281 return log_msg_ret("bms", -ENOENT);
282 ret = scene_menu_select_item(scn, OBJ_MENU, act.select.id);
283 if (ret)
284 return log_msg_ret("bmp", ret);
285 break;
286 }
287 case EXPOACT_QUIT:
288 return -EPIPE;
289 default:
290 break;
291 }
Simon Glass0a2f6a32023-01-06 08:52:40 -0600292
Simon Glassd7cef832025-05-02 08:46:27 -0600293 return -EAGAIN;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600294}