1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
11 #include <linux/delay.h>
12 #include <linux/list.h>
20 * Internally, each item in a menu is represented by a struct menu_item.
22 * These items will be alloc'd and initialized by menu_item_add and destroyed
23 * by menu_item_destroy, and the consumer of the interface never sees that
24 * this struct is used at all.
29 struct list_head list;
33 * The menu is composed of a list of items along with settings and callbacks
34 * provided by the user. An incomplete definition of this struct is available
35 * in menu.h, but the full definition is here to prevent consumers from
36 * relying on its contents.
39 struct menu_item *default_item;
43 void (*display_statusline)(struct menu *);
44 void (*item_data_print)(void *);
45 char *(*item_choice)(void *);
46 void *item_choice_data;
47 struct list_head items;
52 * An iterator function for menu items. callback will be called for each item
53 * in m, with m, a pointer to the item, and extra being passed to callback. If
54 * callback returns a value other than NULL, iteration stops and the value
55 * return by callback is returned from menu_items_iter. This allows it to be
56 * used for search type operations. It is also safe for callback to remove the
57 * item from the list of items.
59 static inline void *menu_items_iter(struct menu *m,
60 void *(*callback)(struct menu *, struct menu_item *, void *),
63 struct list_head *pos, *n;
64 struct menu_item *item;
67 list_for_each_safe(pos, n, &m->items) {
68 item = list_entry(pos, struct menu_item, list);
70 ret = callback(m, item, extra);
80 * Print a menu_item. If the consumer provided an item_data_print function
81 * when creating the menu, call it with a pointer to the item's private data.
82 * Otherwise, print the key of the item.
84 static inline void *menu_item_print(struct menu *m,
85 struct menu_item *item,
88 if (!m->item_data_print) {
92 m->item_data_print(item->data);
99 * Free the memory used by a menu item. This includes the memory used by its
102 static inline void *menu_item_destroy(struct menu *m,
103 struct menu_item *item,
115 * Display a menu so the user can make a choice of an item. First display its
116 * title, if any, and then each item in the menu.
118 static inline void menu_display(struct menu *m)
124 if (m->display_statusline)
125 m->display_statusline(m);
127 menu_items_iter(m, menu_item_print, NULL);
131 * Check if an item's key matches a provided string, pointed to by extra. If
132 * extra is NULL, an item with a NULL key will match. Otherwise, the item's
133 * key has to match according to strcmp.
135 * This is called via menu_items_iter, so it returns a pointer to the item if
136 * the key matches, and returns NULL otherwise.
138 static inline void *menu_item_key_match(struct menu *m,
139 struct menu_item *item, void *extra)
141 char *item_key = extra;
143 if (!item_key || !item->key) {
144 if (item_key == item->key)
150 if (strcmp(item->key, item_key) == 0)
157 * Find the first item with a key matching item_key, if any exists.
159 static inline struct menu_item *menu_item_by_key(struct menu *m,
162 return menu_items_iter(m, menu_item_key_match, item_key);
166 * Set *choice to point to the default item's data, if any default item was
167 * set, and returns 1. If no default item was set, returns -ENOENT.
169 int menu_default_choice(struct menu *m, void **choice)
171 if (m->default_item) {
172 *choice = m->default_item->data;
180 * Displays the menu and asks the user to choose an item. *choice will point
181 * to the private data of the item the user chooses. The user makes a choice
182 * by inputting a string matching the key of an item. Invalid choices will
183 * cause the user to be prompted again, repeatedly, until the user makes a
184 * valid choice. The user can exit the menu without making a choice via ^c.
186 * Returns 1 if the user made a choice, or -EINTR if they bail via ^c.
188 static inline int menu_interactive_choice(struct menu *m, void **choice)
190 char cbuf[CONFIG_SYS_CBSIZE];
191 struct menu_item *choice_item = NULL;
194 while (!choice_item) {
199 if (!m->item_choice) {
200 readret = cli_readline_into_buffer("Enter choice: ",
204 choice_item = menu_item_by_key(m, cbuf);
206 printf("%s not found\n", cbuf);
207 } else if (readret == -1) {
208 printf("<INTERRUPT>\n");
211 return menu_default_choice(m, choice);
214 char *key = m->item_choice(m->item_choice_data);
217 choice_item = menu_item_by_key(m, key);
224 *choice = choice_item->data;
230 * menu_default_set() - Sets the default choice for the menu. This is safe to
231 * call more than once on a menu.
233 * m - Points to a menu created by menu_create().
235 * item_key - Points to a string that, when compared using strcmp, matches the
236 * key for an existing item in the menu.
238 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a
239 * key matching item_key is found.
241 int menu_default_set(struct menu *m, char *item_key)
243 struct menu_item *item;
248 item = menu_item_by_key(m, item_key);
253 m->default_item = item;
259 * menu_get_choice() - Returns the user's selected menu entry, or the default
260 * if the menu is set to not prompt or the timeout expires. This is safe to
261 * call more than once.
263 * m - Points to a menu created by menu_create().
265 * choice - Points to a location that will store a pointer to the selected
266 * menu item. If no item is selected or there is an error, no value will be
267 * written at the location it points to.
269 * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no
270 * default has been set and the menu is set to not prompt or the timeout
271 * expires, or -EINTR if the user exits the menu via ^c.
273 int menu_get_choice(struct menu *m, void **choice)
282 return menu_default_choice(m, choice);
284 return menu_interactive_choice(m, choice);
288 * menu_item_add() - Adds or replaces a menu item. Note that this replaces the
289 * data of an item if it already exists, but doesn't change the order of the
292 * m - Points to a menu created by menu_create().
294 * item_key - Points to a string that will uniquely identify the item. The
295 * string will be copied to internal storage, and is safe to discard after
296 * passing to menu_item_add.
298 * item_data - An opaque pointer associated with an item. It is never
299 * dereferenced internally, but will be passed to the item_data_print, and
300 * will be returned from menu_get_choice if the menu item is selected.
302 * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is
303 * insufficient memory to add the menu item.
305 int menu_item_add(struct menu *m, char *item_key, void *item_data)
307 struct menu_item *item;
312 item = menu_item_by_key(m, item_key);
315 item->data = item_data;
319 item = malloc(sizeof *item);
323 item->key = strdup(item_key);
330 item->data = item_data;
332 list_add_tail(&item->list, &m->items);
339 * menu_create() - Creates a menu handle with default settings
341 * title - If not NULL, points to a string that will be displayed before the
342 * list of menu items. It will be copied to internal storage, and is safe to
343 * discard after passing to menu_create().
345 * timeout - A delay in seconds to wait for user input. If 0, timeout is
346 * disabled, and the default choice will be returned unless prompt is 1.
348 * prompt - If 0, don't ask for user input unless there is an interrupted
349 * timeout. If 1, the user will be prompted for input regardless of the value
352 * display_statusline - If not NULL, will be called to show a statusline when
353 * the menu is displayed.
355 * item_data_print - If not NULL, will be called for each item when the menu
356 * is displayed, with the pointer to the item's data passed as the argument.
357 * If NULL, each item's key will be printed instead. Since an item's key is
358 * what must be entered to select an item, the item_data_print function should
359 * make it obvious what the key for each entry is.
361 * item_choice - If not NULL, will be called when asking the user to choose an
362 * item. Returns a key string corresponding to the chosen item or NULL if
363 * no item has been selected.
365 * item_choice_data - Will be passed as the argument to the item_choice function
367 * Returns a pointer to the menu if successful, or NULL if there is
368 * insufficient memory available to create the menu.
370 struct menu *menu_create(char *title, int timeout, int prompt,
371 void (*display_statusline)(struct menu *),
372 void (*item_data_print)(void *),
373 char *(*item_choice)(void *),
374 void *item_choice_data)
378 m = malloc(sizeof *m);
383 m->default_item = NULL;
385 m->timeout = timeout;
386 m->display_statusline = display_statusline;
387 m->item_data_print = item_data_print;
388 m->item_choice = item_choice;
389 m->item_choice_data = item_choice_data;
393 m->title = strdup(title);
401 INIT_LIST_HEAD(&m->items);
407 * menu_destroy() - frees the memory used by a menu and its items.
409 * m - Points to a menu created by menu_create().
411 * Returns 1 if successful, or -EINVAL if m is NULL.
413 int menu_destroy(struct menu *m)
418 menu_items_iter(m, menu_item_destroy, NULL);
428 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
429 struct cli_ch_state *cch)
431 enum bootmenu_key key = BKEY_NONE;
434 while (menu->delay > 0) {
436 printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
437 printf("Hit any key to stop autoboot: %d ", menu->delay);
438 for (i = 0; i < 100; ++i) {
450 ichar = cli_ch_process(cch, c);
476 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
478 if (menu->delay == 0)
484 enum bootmenu_key bootmenu_conv_key(int ichar)
486 enum bootmenu_key key;
490 /* enter key was pressed */
524 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
525 struct cli_ch_state *cch)
527 enum bootmenu_key key;
530 c = cli_ch_process(cch, 0);
532 while (!c && !tstc()) {
535 c = cli_ch_process(cch, -ETIMEDOUT);
539 c = cli_ch_process(cch, c);
543 key = bootmenu_conv_key(c);