blob: ae5afa147660bdb7c49f58ea6ab019ec93a9bf10 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jason Hobbs0685d822011-08-23 11:06:49 +00002/*
3 * Copyright 2010-2011 Calxeda, Inc.
Leon Yu9686a932019-06-21 12:12:39 +08004 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
Jason Hobbs0685d822011-08-23 11:06:49 +00005 */
6
Masahisa Kojima0e1b9962022-04-28 17:09:45 +09007#include <ansi.h>
Simon Glassdec3c012014-04-10 20:01:25 -06008#include <cli.h>
Jason Hobbs0685d822011-08-23 11:06:49 +00009#include <malloc.h>
10#include <errno.h>
Christian Marangib357fc62025-05-25 15:43:58 +020011#include <linux/ctype.h>
Masahisa Kojima0e1b9962022-04-28 17:09:45 +090012#include <linux/delay.h>
Jason Hobbs0685d822011-08-23 11:06:49 +000013#include <linux/list.h>
Masahisa Kojima0e1b9962022-04-28 17:09:45 +090014#include <watchdog.h>
Jason Hobbs0685d822011-08-23 11:06:49 +000015
16#include "menu.h"
17
Simon Glass1ee7ab82023-06-17 11:49:48 +010018#define ansi 1
Simon Glass9d8d3872023-01-06 08:52:26 -060019
Jason Hobbs0685d822011-08-23 11:06:49 +000020/*
21 * Internally, each item in a menu is represented by a struct menu_item.
22 *
23 * These items will be alloc'd and initialized by menu_item_add and destroyed
24 * by menu_item_destroy, and the consumer of the interface never sees that
25 * this struct is used at all.
26 */
27struct menu_item {
28 char *key;
29 void *data;
30 struct list_head list;
31};
32
33/*
34 * The menu is composed of a list of items along with settings and callbacks
35 * provided by the user. An incomplete definition of this struct is available
36 * in menu.h, but the full definition is here to prevent consumers from
37 * relying on its contents.
38 */
39struct menu {
40 struct menu_item *default_item;
Jason Hobbs65febe62011-08-23 11:06:50 +000041 int timeout;
Jason Hobbs0685d822011-08-23 11:06:49 +000042 char *title;
43 int prompt;
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -070044 void (*display_statusline)(struct menu *);
Jason Hobbs0685d822011-08-23 11:06:49 +000045 void (*item_data_print)(void *);
Pali Rohárd0d8d3b2013-03-23 14:50:40 +000046 char *(*item_choice)(void *);
developerc2a1e9e2024-10-29 17:47:16 +080047 bool (*need_reprint)(void *);
Pali Rohárd0d8d3b2013-03-23 14:50:40 +000048 void *item_choice_data;
Jason Hobbs0685d822011-08-23 11:06:49 +000049 struct list_head items;
Leon Yu9686a932019-06-21 12:12:39 +080050 int item_cnt;
Jason Hobbs0685d822011-08-23 11:06:49 +000051};
52
53/*
54 * An iterator function for menu items. callback will be called for each item
55 * in m, with m, a pointer to the item, and extra being passed to callback. If
56 * callback returns a value other than NULL, iteration stops and the value
57 * return by callback is returned from menu_items_iter. This allows it to be
58 * used for search type operations. It is also safe for callback to remove the
59 * item from the list of items.
60 */
61static inline void *menu_items_iter(struct menu *m,
62 void *(*callback)(struct menu *, struct menu_item *, void *),
63 void *extra)
64{
65 struct list_head *pos, *n;
66 struct menu_item *item;
67 void *ret;
68
69 list_for_each_safe(pos, n, &m->items) {
70 item = list_entry(pos, struct menu_item, list);
71
72 ret = callback(m, item, extra);
73
74 if (ret)
75 return ret;
76 }
77
78 return NULL;
79}
80
81/*
82 * Print a menu_item. If the consumer provided an item_data_print function
83 * when creating the menu, call it with a pointer to the item's private data.
84 * Otherwise, print the key of the item.
85 */
86static inline void *menu_item_print(struct menu *m,
87 struct menu_item *item,
88 void *extra)
89{
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010090 if (!m->item_data_print) {
Anatolij Gustschinb50e1e02011-12-03 06:46:07 +000091 puts(item->key);
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010092 putc('\n');
93 } else {
Jason Hobbs0685d822011-08-23 11:06:49 +000094 m->item_data_print(item->data);
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +010095 }
Jason Hobbs0685d822011-08-23 11:06:49 +000096
97 return NULL;
98}
99
100/*
101 * Free the memory used by a menu item. This includes the memory used by its
102 * key.
103 */
104static inline void *menu_item_destroy(struct menu *m,
105 struct menu_item *item,
106 void *extra)
107{
108 if (item->key)
109 free(item->key);
110
111 free(item);
112
113 return NULL;
114}
115
116/*
117 * Display a menu so the user can make a choice of an item. First display its
118 * title, if any, and then each item in the menu.
119 */
120static inline void menu_display(struct menu *m)
121{
developerc2a1e9e2024-10-29 17:47:16 +0800122 if (m->need_reprint) {
123 if (!m->need_reprint(m->item_choice_data))
124 return;
125 }
126
Wolfgang Denk4f5c100b2011-11-28 20:19:41 +0100127 if (m->title) {
128 puts(m->title);
129 putc('\n');
130 }
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700131 if (m->display_statusline)
132 m->display_statusline(m);
Jason Hobbs0685d822011-08-23 11:06:49 +0000133
134 menu_items_iter(m, menu_item_print, NULL);
135}
136
137/*
138 * Check if an item's key matches a provided string, pointed to by extra. If
139 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
140 * key has to match according to strcmp.
141 *
142 * This is called via menu_items_iter, so it returns a pointer to the item if
143 * the key matches, and returns NULL otherwise.
144 */
145static inline void *menu_item_key_match(struct menu *m,
146 struct menu_item *item, void *extra)
147{
148 char *item_key = extra;
149
150 if (!item_key || !item->key) {
151 if (item_key == item->key)
152 return item;
153
154 return NULL;
155 }
156
157 if (strcmp(item->key, item_key) == 0)
158 return item;
159
160 return NULL;
161}
162
163/*
164 * Find the first item with a key matching item_key, if any exists.
165 */
166static inline struct menu_item *menu_item_by_key(struct menu *m,
167 char *item_key)
168{
169 return menu_items_iter(m, menu_item_key_match, item_key);
170}
171
172/*
Jason Hobbs0685d822011-08-23 11:06:49 +0000173 * Set *choice to point to the default item's data, if any default item was
174 * set, and returns 1. If no default item was set, returns -ENOENT.
175 */
Anatolij Gustschinfb6068a2013-03-23 14:52:04 +0000176int menu_default_choice(struct menu *m, void **choice)
Jason Hobbs0685d822011-08-23 11:06:49 +0000177{
178 if (m->default_item) {
179 *choice = m->default_item->data;
180 return 1;
181 }
182
183 return -ENOENT;
184}
185
186/*
187 * Displays the menu and asks the user to choose an item. *choice will point
188 * to the private data of the item the user chooses. The user makes a choice
189 * by inputting a string matching the key of an item. Invalid choices will
190 * cause the user to be prompted again, repeatedly, until the user makes a
191 * valid choice. The user can exit the menu without making a choice via ^c.
192 *
193 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
194 */
195static inline int menu_interactive_choice(struct menu *m, void **choice)
196{
197 char cbuf[CONFIG_SYS_CBSIZE];
198 struct menu_item *choice_item = NULL;
199 int readret;
200
201 while (!choice_item) {
202 cbuf[0] = '\0';
203
204 menu_display(m);
205
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000206 if (!m->item_choice) {
Simon Glassbe6aafc2014-04-10 20:01:27 -0600207 readret = cli_readline_into_buffer("Enter choice: ",
Masahiro Yamadac46d0fe2018-05-24 17:04:57 +0900208 cbuf, m->timeout);
Jason Hobbs0685d822011-08-23 11:06:49 +0000209
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000210 if (readret >= 0) {
211 choice_item = menu_item_by_key(m, cbuf);
212 if (!choice_item)
213 printf("%s not found\n", cbuf);
Tuomas Tynkkynen60c47af2015-05-07 21:29:19 +0300214 } else if (readret == -1) {
215 printf("<INTERRUPT>\n");
216 return -EINTR;
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000217 } else {
218 return menu_default_choice(m, choice);
Heiko Schochere795b2e2012-01-16 22:24:29 +0000219 }
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000220 } else {
221 char *key = m->item_choice(m->item_choice_data);
222
223 if (key)
224 choice_item = menu_item_by_key(m, key);
225 }
226
227 if (!choice_item)
228 m->timeout = 0;
Jason Hobbs0685d822011-08-23 11:06:49 +0000229 }
230
231 *choice = choice_item->data;
232
233 return 1;
234}
235
236/*
237 * menu_default_set() - Sets the default choice for the menu. This is safe to
238 * call more than once on a menu.
239 *
240 * m - Points to a menu created by menu_create().
241 *
242 * item_key - Points to a string that, when compared using strcmp, matches the
243 * key for an existing item in the menu.
244 *
245 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
246 * key matching item_key is found.
247 */
248int menu_default_set(struct menu *m, char *item_key)
249{
250 struct menu_item *item;
251
252 if (!m)
253 return -EINVAL;
254
255 item = menu_item_by_key(m, item_key);
256
257 if (!item)
258 return -ENOENT;
259
260 m->default_item = item;
261
262 return 1;
263}
264
265/*
266 * menu_get_choice() - Returns the user's selected menu entry, or the default
Jason Hobbs65febe62011-08-23 11:06:50 +0000267 * if the menu is set to not prompt or the timeout expires. This is safe to
268 * call more than once.
Jason Hobbs0685d822011-08-23 11:06:49 +0000269 *
270 * m - Points to a menu created by menu_create().
271 *
272 * choice - Points to a location that will store a pointer to the selected
273 * menu item. If no item is selected or there is an error, no value will be
274 * written at the location it points to.
275 *
276 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
Jason Hobbs65febe62011-08-23 11:06:50 +0000277 * default has been set and the menu is set to not prompt or the timeout
278 * expires, or -EINTR if the user exits the menu via ^c.
Jason Hobbs0685d822011-08-23 11:06:49 +0000279 */
280int menu_get_choice(struct menu *m, void **choice)
281{
282 if (!m || !choice)
283 return -EINVAL;
284
Masahisa Kojima72f823f2022-04-28 17:09:37 +0900285 if (!m->item_cnt)
286 return -ENOENT;
287
Masahisa Kojima6f59d192022-04-28 17:09:36 +0900288 if (!m->prompt)
Jason Hobbs0685d822011-08-23 11:06:49 +0000289 return menu_default_choice(m, choice);
290
291 return menu_interactive_choice(m, choice);
292}
293
294/*
295 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
296 * data of an item if it already exists, but doesn't change the order of the
297 * item.
298 *
299 * m - Points to a menu created by menu_create().
300 *
301 * item_key - Points to a string that will uniquely identify the item. The
302 * string will be copied to internal storage, and is safe to discard after
303 * passing to menu_item_add.
304 *
305 * item_data - An opaque pointer associated with an item. It is never
306 * dereferenced internally, but will be passed to the item_data_print, and
307 * will be returned from menu_get_choice if the menu item is selected.
308 *
309 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
310 * insufficient memory to add the menu item.
311 */
312int menu_item_add(struct menu *m, char *item_key, void *item_data)
313{
314 struct menu_item *item;
315
316 if (!m)
317 return -EINVAL;
318
319 item = menu_item_by_key(m, item_key);
320
321 if (item) {
322 item->data = item_data;
323 return 1;
324 }
325
326 item = malloc(sizeof *item);
327 if (!item)
328 return -ENOMEM;
329
330 item->key = strdup(item_key);
331
332 if (!item->key) {
333 free(item);
334 return -ENOMEM;
335 }
336
337 item->data = item_data;
338
339 list_add_tail(&item->list, &m->items);
Leon Yu9686a932019-06-21 12:12:39 +0800340 m->item_cnt++;
Jason Hobbs0685d822011-08-23 11:06:49 +0000341
342 return 1;
343}
344
345/*
346 * menu_create() - Creates a menu handle with default settings
347 *
348 * title - If not NULL, points to a string that will be displayed before the
349 * list of menu items. It will be copied to internal storage, and is safe to
350 * discard after passing to menu_create().
351 *
Jason Hobbs65febe62011-08-23 11:06:50 +0000352 * timeout - A delay in seconds to wait for user input. If 0, timeout is
353 * disabled, and the default choice will be returned unless prompt is 1.
354 *
355 * prompt - If 0, don't ask for user input unless there is an interrupted
356 * timeout. If 1, the user will be prompted for input regardless of the value
357 * of timeout.
Jason Hobbs0685d822011-08-23 11:06:49 +0000358 *
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700359 * display_statusline - If not NULL, will be called to show a statusline when
360 * the menu is displayed.
361 *
Jason Hobbs0685d822011-08-23 11:06:49 +0000362 * item_data_print - If not NULL, will be called for each item when the menu
363 * is displayed, with the pointer to the item's data passed as the argument.
364 * If NULL, each item's key will be printed instead. Since an item's key is
365 * what must be entered to select an item, the item_data_print function should
366 * make it obvious what the key for each entry is.
367 *
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000368 * item_choice - If not NULL, will be called when asking the user to choose an
Alexander Merkle0137e602016-03-17 15:44:47 +0100369 * item. Returns a key string corresponding to the chosen item or NULL if
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000370 * no item has been selected.
371 *
developerc2a1e9e2024-10-29 17:47:16 +0800372 * need_reprint - If not NULL, will be called before printing the menu.
373 * Returning FALSE means the menu does not need reprint.
374 *
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000375 * item_choice_data - Will be passed as the argument to the item_choice function
376 *
Jason Hobbs0685d822011-08-23 11:06:49 +0000377 * Returns a pointer to the menu if successful, or NULL if there is
378 * insufficient memory available to create the menu.
379 */
Jason Hobbs65febe62011-08-23 11:06:50 +0000380struct menu *menu_create(char *title, int timeout, int prompt,
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700381 void (*display_statusline)(struct menu *),
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000382 void (*item_data_print)(void *),
383 char *(*item_choice)(void *),
developerc2a1e9e2024-10-29 17:47:16 +0800384 bool (*need_reprint)(void *),
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000385 void *item_choice_data)
Jason Hobbs0685d822011-08-23 11:06:49 +0000386{
387 struct menu *m;
388
389 m = malloc(sizeof *m);
390
391 if (!m)
392 return NULL;
393
394 m->default_item = NULL;
395 m->prompt = prompt;
Jason Hobbs65febe62011-08-23 11:06:50 +0000396 m->timeout = timeout;
Thirupathaiah Annapureddyd6b9f6b2020-03-18 11:38:42 -0700397 m->display_statusline = display_statusline;
Jason Hobbs0685d822011-08-23 11:06:49 +0000398 m->item_data_print = item_data_print;
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000399 m->item_choice = item_choice;
developerc2a1e9e2024-10-29 17:47:16 +0800400 m->need_reprint = need_reprint;
Pali Rohárd0d8d3b2013-03-23 14:50:40 +0000401 m->item_choice_data = item_choice_data;
Leon Yu9686a932019-06-21 12:12:39 +0800402 m->item_cnt = 0;
Jason Hobbs0685d822011-08-23 11:06:49 +0000403
404 if (title) {
405 m->title = strdup(title);
406 if (!m->title) {
407 free(m);
408 return NULL;
409 }
410 } else
411 m->title = NULL;
412
Jason Hobbs0685d822011-08-23 11:06:49 +0000413 INIT_LIST_HEAD(&m->items);
414
415 return m;
416}
417
418/*
419 * menu_destroy() - frees the memory used by a menu and its items.
420 *
421 * m - Points to a menu created by menu_create().
422 *
423 * Returns 1 if successful, or -EINVAL if m is NULL.
424 */
425int menu_destroy(struct menu *m)
426{
427 if (!m)
428 return -EINVAL;
429
430 menu_items_iter(m, menu_item_destroy, NULL);
431
432 if (m->title)
433 free(m->title);
434
435 free(m);
436
437 return 1;
438}
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900439
Christian Marangib357fc62025-05-25 15:43:58 +0200440static int bootmenu_conv_shortcut_key(struct bootmenu_data *menu, int ichar)
441{
442 int shortcut_key;
443
444 ichar = tolower(ichar);
445 switch (ichar) {
446 /* a-z for bootmenu entry > 9 */
447 case 'a' ... 'z':
448 shortcut_key = ichar - 'a' + 9;
449 break;
450 /* 1-9 for bootmenu entry <= 9 */
451 case '1' ... '9':
452 shortcut_key = ichar - '1';
453 break;
454 /* Reserve 0 for last option (aka Exit) */
455 case '0':
456 default:
457 return -1;
458 }
459
460 return shortcut_key;
461}
462
Simon Glass9d8d3872023-01-06 08:52:26 -0600463enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
464 struct cli_ch_state *cch)
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900465{
Simon Glass81544c42023-01-06 08:52:23 -0600466 enum bootmenu_key key = BKEY_NONE;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900467 int i, c;
468
469 while (menu->delay > 0) {
Christian Marangib357fc62025-05-25 15:43:58 +0200470 int ichar;
471
Simon Glass9d8d3872023-01-06 08:52:26 -0600472 if (ansi)
473 printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900474 printf("Hit any key to stop autoboot: %d ", menu->delay);
475 for (i = 0; i < 100; ++i) {
476 if (!tstc()) {
Stefan Roese80877fa2022-09-02 14:10:46 +0200477 schedule();
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900478 mdelay(10);
479 continue;
480 }
481
482 menu->delay = -1;
483 c = getchar();
484
Simon Glass9d8d3872023-01-06 08:52:26 -0600485 ichar = cli_ch_process(cch, c);
486
487 switch (ichar) {
488 case '\0':
Simon Glass81544c42023-01-06 08:52:23 -0600489 key = BKEY_NONE;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900490 break;
Simon Glass9d8d3872023-01-06 08:52:26 -0600491 case '\n':
Simon Glass81544c42023-01-06 08:52:23 -0600492 key = BKEY_SELECT;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900493 break;
494 case 0x3: /* ^C */
Simon Glass81544c42023-01-06 08:52:23 -0600495 key = BKEY_QUIT;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900496 break;
Christian Marangib357fc62025-05-25 15:43:58 +0200497 case 'A' ... 'Z':
498 case 'a' ... 'z':
499 case '0' ... '9':
500 key = BKEY_SHORTCUT;
501 break;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900502 default:
Simon Glass81544c42023-01-06 08:52:23 -0600503 key = BKEY_NONE;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900504 break;
505 }
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900506 break;
507 }
508
Christian Marangib357fc62025-05-25 15:43:58 +0200509 if (key == BKEY_SHORTCUT)
510 cch->shortcut_key = bootmenu_conv_shortcut_key(menu, ichar);
511
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900512 if (menu->delay < 0)
513 break;
514
515 --menu->delay;
516 }
517
Simon Glass9d8d3872023-01-06 08:52:26 -0600518 if (ansi)
519 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900520
521 if (menu->delay == 0)
Simon Glass81544c42023-01-06 08:52:23 -0600522 key = BKEY_SELECT;
523
524 return key;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900525}
526
Simon Glass1b56fe12023-01-06 08:52:35 -0600527enum bootmenu_key bootmenu_conv_key(int ichar)
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900528{
Simon Glass1b56fe12023-01-06 08:52:35 -0600529 enum bootmenu_key key;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900530
Simon Glass1b56fe12023-01-06 08:52:35 -0600531 switch (ichar) {
Simon Glass9d8d3872023-01-06 08:52:26 -0600532 case '\n':
Simon Glassd52ce652023-01-06 08:52:25 -0600533 /* enter key was pressed */
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600534 key = BKEY_SELECT;
Simon Glassd52ce652023-01-06 08:52:25 -0600535 break;
536 case CTL_CH('c'):
Simon Glass9d8d3872023-01-06 08:52:26 -0600537 case '\e':
Simon Glassd52ce652023-01-06 08:52:25 -0600538 /* ^C was pressed */
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600539 key = BKEY_QUIT;
Simon Glassd52ce652023-01-06 08:52:25 -0600540 break;
541 case CTL_CH('p'):
542 key = BKEY_UP;
543 break;
544 case CTL_CH('n'):
545 key = BKEY_DOWN;
546 break;
Masahisa Kojimad38ecb72023-02-02 18:24:44 +0900547 case CTL_CH('s'):
548 key = BKEY_SAVE;
549 break;
Simon Glassd52ce652023-01-06 08:52:25 -0600550 case '+':
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600551 key = BKEY_PLUS;
Simon Glassd52ce652023-01-06 08:52:25 -0600552 break;
553 case '-':
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600554 key = BKEY_MINUS;
Simon Glassd52ce652023-01-06 08:52:25 -0600555 break;
556 case ' ':
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600557 key = BKEY_SPACE;
Simon Glassd52ce652023-01-06 08:52:25 -0600558 break;
Christian Marangib357fc62025-05-25 15:43:58 +0200559 case 'A' ... 'Z':
560 case 'a' ... 'z':
561 case '0' ... '9':
562 key = BKEY_SHORTCUT;
563 break;
Simon Glass1b56fe12023-01-06 08:52:35 -0600564 default:
565 key = BKEY_NONE;
566 break;
Simon Glassd52ce652023-01-06 08:52:25 -0600567 }
Simon Glassf1d3c8e2023-01-06 08:52:24 -0600568
569 return key;
Masahisa Kojima0e1b9962022-04-28 17:09:45 +0900570}
Simon Glass1b56fe12023-01-06 08:52:35 -0600571
572enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
573 struct cli_ch_state *cch)
574{
575 enum bootmenu_key key;
developer744a0a22024-10-29 17:47:10 +0800576 int c, errchar = 0;
Simon Glass1b56fe12023-01-06 08:52:35 -0600577
578 c = cli_ch_process(cch, 0);
579 if (!c) {
580 while (!c && !tstc()) {
581 schedule();
582 mdelay(10);
developer744a0a22024-10-29 17:47:10 +0800583 c = cli_ch_process(cch, errchar);
584 errchar = -ETIMEDOUT;
Simon Glass1b56fe12023-01-06 08:52:35 -0600585 }
586 if (!c) {
587 c = getchar();
588 c = cli_ch_process(cch, c);
589 }
590 }
591
592 key = bootmenu_conv_key(c);
593
Christian Marangib357fc62025-05-25 15:43:58 +0200594 if (key == BKEY_SHORTCUT)
595 cch->shortcut_key = bootmenu_conv_shortcut_key(menu, c);
596
Simon Glass1b56fe12023-01-06 08:52:35 -0600597 return key;
598}