1 // SPDX-License-Identifier: GPL-2.0+
3 * Provide a menu of available bootflows and related options
5 * Copyright 2022 Google LLC
9 #define LOG_CATEGORY UCLASS_BOOTSTD
18 #include <video_console.h>
20 #include <linux/delay.h>
21 #include "bootflow_internal.h"
24 * struct menu_priv - information about the menu
26 * @num_bootflows: Number of bootflows in the menu
32 int bootflow_menu_new(struct expo **expp)
34 struct udevice *last_bootdev;
35 struct scene_obj_menu *menu;
36 struct menu_priv *priv;
37 struct bootflow *bflow;
43 priv = calloc(1, sizeof(*priv));
45 return log_msg_ret("prv", -ENOMEM);
47 ret = expo_new("bootflows", priv, &exp);
49 return log_msg_ret("exp", ret);
51 ret = scene_new(exp, "main", MAIN, &scn);
53 return log_msg_ret("scn", ret);
55 ret |= scene_txt_str(scn, "prompt", OBJ_PROMPT, STR_PROMPT,
56 "UP and DOWN to choose, ENTER to select", NULL);
58 ret = scene_menu(scn, "main", OBJ_MENU, &menu);
59 ret |= scene_obj_set_pos(scn, OBJ_MENU, MARGIN_LEFT, 100);
60 ret |= scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE,
61 "U-Boot - Boot Menu", NULL);
62 ret |= scene_menu_set_title(scn, OBJ_MENU, OBJ_PROMPT);
64 logo = video_get_u_boot_logo();
66 ret |= scene_img(scn, "ulogo", OBJ_U_BOOT_LOGO, logo, NULL);
67 ret |= scene_obj_set_pos(scn, OBJ_U_BOOT_LOGO, -4, 4);
70 ret |= scene_txt_str(scn, "cur_item", OBJ_POINTER, STR_POINTER, ">",
72 ret |= scene_menu_set_pointer(scn, OBJ_MENU, OBJ_POINTER);
74 return log_msg_ret("new", -EINVAL);
77 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
78 ret = bootflow_next_glob(&bflow), i++) {
79 char str[2], *label, *key;
83 if (bflow->state != BOOTFLOWST_READY)
86 *str = i < 10 ? '0' + i : 'A' + i - 10;
90 return log_msg_ret("key", -ENOMEM);
91 label = strdup(dev_get_parent(bflow->dev)->name);
94 return log_msg_ret("nam", -ENOMEM);
97 add_gap = last_bootdev != bflow->dev;
98 last_bootdev = bflow->dev;
100 ret = expo_str(exp, "prompt", STR_POINTER, ">");
101 ret |= scene_txt_str(scn, "label", ITEM_LABEL + i,
102 STR_LABEL + i, label, NULL);
103 ret |= scene_txt_str(scn, "desc", ITEM_DESC + i, STR_DESC + i,
104 bflow->os_name ? bflow->os_name :
106 ret |= scene_txt_str(scn, "key", ITEM_KEY + i, STR_KEY + i, key,
110 preview_id = ITEM_PREVIEW + i;
111 ret |= scene_img(scn, "preview", preview_id,
114 ret |= scene_menuitem(scn, OBJ_MENU, "item", ITEM + i,
115 ITEM_KEY + i, ITEM_LABEL + i,
116 ITEM_DESC + i, preview_id,
117 add_gap ? SCENEMIF_GAP_BEFORE : 0,
121 return log_msg_ret("itm", -EINVAL);
122 priv->num_bootflows++;
125 ret = scene_arrange(scn);
127 return log_msg_ret("arr", ret);
134 int bootflow_menu_apply_theme(struct expo *exp, ofnode node)
136 struct menu_priv *priv = exp->priv;
141 log_debug("Applying theme %s\n", ofnode_get_name(node));
142 scn = expo_lookup_scene_id(exp, MAIN);
144 return log_msg_ret("scn", -ENOENT);
146 /* Avoid error-checking optional items */
147 if (!ofnode_read_u32(node, "font-size", &font_size)) {
150 log_debug("font size %d\n", font_size);
151 scene_txt_set_font(scn, OBJ_PROMPT, NULL, font_size);
152 scene_txt_set_font(scn, OBJ_POINTER, NULL, font_size);
153 for (i = 0; i < priv->num_bootflows; i++) {
154 ret = scene_txt_set_font(scn, ITEM_DESC + i, NULL,
157 return log_msg_ret("des", ret);
158 scene_txt_set_font(scn, ITEM_KEY + i, NULL, font_size);
159 scene_txt_set_font(scn, ITEM_LABEL + i, NULL,
164 ret = scene_arrange(scn);
166 return log_msg_ret("arr", ret);
171 int bootflow_menu_run(struct bootstd_priv *std, bool text_mode,
172 struct bootflow **bflowp)
174 struct cli_ch_state s_cch, *cch = &s_cch;
175 struct bootflow *sel_bflow;
187 ret = bootflow_menu_new(&exp);
189 return log_msg_ret("exp", ret);
191 if (ofnode_valid(std->theme)) {
192 ret = bootflow_menu_apply_theme(exp, std->theme);
194 return log_msg_ret("thm", ret);
197 /* For now we only support a video console */
198 ret = uclass_first_device_err(UCLASS_VIDEO, &dev);
200 return log_msg_ret("vid", ret);
201 ret = expo_set_display(exp, dev);
203 return log_msg_ret("dis", ret);
205 ret = expo_set_scene_id(exp, MAIN);
207 return log_msg_ret("scn", ret);
210 expo_set_text_mode(exp, text_mode);
214 struct expo_action act;
217 ret = expo_render(exp);
221 ichar = cli_ch_process(cch, 0);
223 while (!ichar && !tstc()) {
226 ichar = cli_ch_process(cch, -ETIMEDOUT);
230 ichar = cli_ch_process(cch, ichar);
236 key = bootmenu_conv_key(ichar);
237 if (key == BKEY_NONE)
243 ret = expo_send_key(exp, key);
247 ret = expo_action_get(exp, &act);
251 sel_id = act.select.id;
264 return log_msg_ret("end", ret);
267 struct bootflow *bflow;
270 for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36;
271 ret = bootflow_next_glob(&bflow), i++) {
272 if (i == sel_id - ITEM) {