blob: 7b44b6439b638f395e9d0978b30917b96d44b58b [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 Glasscffde572025-05-02 08:46:47 -060087 struct udevice *media;
Simon Glassd9da19a2025-05-02 08:46:28 -060088 struct scene *scn;
Simon Glasscffde572025-05-02 08:46:47 -060089 const char *name;
Simon Glass9f5a7922025-05-02 08:46:29 -060090 uint preview_id;
Simon Glassd9da19a2025-05-02 08:46:28 -060091 uint scene_id;
Simon Glass9f5a7922025-05-02 08:46:29 -060092 bool add_gap;
93 int ret;
Simon Glassd9da19a2025-05-02 08:46:28 -060094
95 ret = expo_first_scene_id(exp);
96 if (ret < 0)
97 return log_msg_ret("scn", ret);
98 scene_id = ret;
99 scn = expo_lookup_scene_id(exp, scene_id);
100
Simon Glass9f5a7922025-05-02 08:46:29 -0600101 *str = seq < 10 ? '0' + seq : 'A' + seq - 10;
102 str[1] = '\0';
103 key = strdup(str);
104 if (!key)
105 return log_msg_ret("key", -ENOMEM);
Simon Glasscffde572025-05-02 08:46:47 -0600106
107 media = dev_get_parent(bflow->dev);
108 if (device_get_uclass_id(media) == UCLASS_MASS_STORAGE)
109 name = "usb";
110 else
111 name = media->name;
112 label = strdup(name);
113
Simon Glass9f5a7922025-05-02 08:46:29 -0600114 if (!label) {
115 free(key);
116 return log_msg_ret("nam", -ENOMEM);
117 }
118
119 add_gap = priv->last_bootdev != bflow->dev;
120 priv->last_bootdev = bflow->dev;
121
122 ret = expo_str(exp, "prompt", STR_POINTER, ">");
123 ret |= scene_txt_str(scn, "label", ITEM_LABEL + seq,
124 STR_LABEL + seq, label, NULL);
125 ret |= scene_txt_str(scn, "desc", ITEM_DESC + seq, STR_DESC + seq,
126 bflow->os_name ? bflow->os_name :
127 bflow->name, NULL);
128 ret |= scene_txt_str(scn, "key", ITEM_KEY + seq, STR_KEY + seq, key,
129 NULL);
130 preview_id = 0;
131 if (bflow->logo) {
132 preview_id = ITEM_PREVIEW + seq;
133 ret |= scene_img(scn, "preview", preview_id,
134 bflow->logo, NULL);
135 }
136 ret |= scene_menuitem(scn, OBJ_MENU, "item", ITEM + seq,
137 ITEM_KEY + seq, ITEM_LABEL + seq,
138 ITEM_DESC + seq, preview_id,
139 add_gap ? SCENEMIF_GAP_BEFORE : 0,
140 NULL);
141
142 if (ret < 0)
143 return log_msg_ret("itm", -EINVAL);
144 priv->num_bootflows++;
145 *scnp = scn;
146
147 return 0;
148}
149
150int bootflow_menu_add_all(struct expo *exp)
151{
152 struct bootflow *bflow;
153 struct scene *scn;
154 int ret, i;
155
Simon Glass0a2f6a32023-01-06 08:52:40 -0600156 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
157 ret = bootflow_next_glob(&bflow), i++) {
Quentin Schulz21a6aec2024-06-12 16:58:49 +0200158 struct bootmeth_uc_plat *ucp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600159
160 if (bflow->state != BOOTFLOWST_READY)
161 continue;
162
Quentin Schulz21a6aec2024-06-12 16:58:49 +0200163 /* No media to show for BOOTMETHF_GLOBAL bootmeths */
164 ucp = dev_get_uclass_plat(bflow->method);
165 if (ucp->flags & BOOTMETHF_GLOBAL)
166 continue;
167
Simon Glass9f5a7922025-05-02 08:46:29 -0600168 ret = bootflow_menu_add(exp, bflow, i, &scn);
169 if (ret)
170 return log_msg_ret("bao", ret);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600171
Simon Glass0a2f6a32023-01-06 08:52:40 -0600172 }
173
Simon Glassd7e32a82023-06-01 10:22:35 -0600174 ret = scene_arrange(scn);
175 if (ret)
176 return log_msg_ret("arr", ret);
177
Simon Glass0a2f6a32023-01-06 08:52:40 -0600178 return 0;
179}
180
Simon Glassd92bcc42023-01-06 08:52:42 -0600181int bootflow_menu_apply_theme(struct expo *exp, ofnode node)
182{
183 struct menu_priv *priv = exp->priv;
184 struct scene *scn;
185 u32 font_size;
186 int ret;
187
188 log_debug("Applying theme %s\n", ofnode_get_name(node));
189 scn = expo_lookup_scene_id(exp, MAIN);
190 if (!scn)
191 return log_msg_ret("scn", -ENOENT);
192
193 /* Avoid error-checking optional items */
194 if (!ofnode_read_u32(node, "font-size", &font_size)) {
195 int i;
196
197 log_debug("font size %d\n", font_size);
198 scene_txt_set_font(scn, OBJ_PROMPT, NULL, font_size);
199 scene_txt_set_font(scn, OBJ_POINTER, NULL, font_size);
200 for (i = 0; i < priv->num_bootflows; i++) {
201 ret = scene_txt_set_font(scn, ITEM_DESC + i, NULL,
202 font_size);
203 if (ret)
204 return log_msg_ret("des", ret);
205 scene_txt_set_font(scn, ITEM_KEY + i, NULL, font_size);
206 scene_txt_set_font(scn, ITEM_LABEL + i, NULL,
207 font_size);
208 }
209 }
210
211 ret = scene_arrange(scn);
212 if (ret)
213 return log_msg_ret("arr", ret);
214
215 return 0;
216}
217
Simon Glassd7cef832025-05-02 08:46:27 -0600218int bootflow_menu_start(struct bootstd_priv *std, bool text_mode,
219 struct expo **expp)
Simon Glass0a2f6a32023-01-06 08:52:40 -0600220{
Simon Glass0a2f6a32023-01-06 08:52:40 -0600221 struct udevice *dev;
222 struct expo *exp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600223 int ret;
224
Simon Glass0a2f6a32023-01-06 08:52:40 -0600225 ret = bootflow_menu_new(&exp);
226 if (ret)
Simon Glassd9da19a2025-05-02 08:46:28 -0600227 return log_msg_ret("bmn", ret);
228 ret = bootflow_menu_add_all(exp);
229 if (ret)
230 return log_msg_ret("bma", ret);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600231
Simon Glassd92bcc42023-01-06 08:52:42 -0600232 if (ofnode_valid(std->theme)) {
233 ret = bootflow_menu_apply_theme(exp, std->theme);
234 if (ret)
235 return log_msg_ret("thm", ret);
236 }
237
Simon Glass0a2f6a32023-01-06 08:52:40 -0600238 /* For now we only support a video console */
239 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
240 if (ret)
241 return log_msg_ret("vid", ret);
242 ret = expo_set_display(exp, dev);
243 if (ret)
244 return log_msg_ret("dis", ret);
245
246 ret = expo_set_scene_id(exp, MAIN);
247 if (ret)
248 return log_msg_ret("scn", ret);
249
250 if (text_mode)
Simon Glassb2c40342023-06-01 10:22:37 -0600251 expo_set_text_mode(exp, text_mode);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600252
Simon Glasscfb4f2c2025-05-02 08:46:42 -0600253 ret = expo_calc_dims(exp);
254 if (ret)
255 return log_msg_ret("bmd", ret);
256
Simon Glassd7cef832025-05-02 08:46:27 -0600257 *expp = exp;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600258
Simon Glassd7cef832025-05-02 08:46:27 -0600259 return 0;
260}
261
262int bootflow_menu_poll(struct expo *exp, struct bootflow **bflowp)
263{
264 struct bootflow *sel_bflow;
265 struct expo_action act;
266 int ret;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600267
Simon Glassd7cef832025-05-02 08:46:27 -0600268 sel_bflow = NULL;
269 *bflowp = NULL;
270
271 ret = expo_poll(exp, &act);
272 if (ret)
273 return log_msg_ret("bmp", ret);
274
275 switch (act.type) {
276 case EXPOACT_SELECT: {
Simon Glass0a2f6a32023-01-06 08:52:40 -0600277 struct bootflow *bflow;
278 int i;
279
280 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
281 ret = bootflow_next_glob(&bflow), i++) {
Simon Glassd7cef832025-05-02 08:46:27 -0600282 if (i == act.select.id - ITEM) {
283 *bflowp = bflow;
284 // printf("found %p\n", bflow);
285 return 0;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600286 }
287 }
Simon Glassd7cef832025-05-02 08:46:27 -0600288 break;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600289 }
Simon Glassd7cef832025-05-02 08:46:27 -0600290 case EXPOACT_POINT_ITEM: {
291 struct scene *scn = expo_lookup_scene_id(exp, MAIN);
Simon Glass0a2f6a32023-01-06 08:52:40 -0600292
Simon Glassd7cef832025-05-02 08:46:27 -0600293 if (!scn)
294 return log_msg_ret("bms", -ENOENT);
295 ret = scene_menu_select_item(scn, OBJ_MENU, act.select.id);
296 if (ret)
297 return log_msg_ret("bmp", ret);
298 break;
299 }
300 case EXPOACT_QUIT:
301 return -EPIPE;
302 default:
303 break;
304 }
Simon Glass0a2f6a32023-01-06 08:52:40 -0600305
Simon Glassd7cef832025-05-02 08:46:27 -0600306 return -EAGAIN;
Simon Glass0a2f6a32023-01-06 08:52:40 -0600307}