1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Copyright 2010-2011 Calxeda, Inc.
4 * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
5 */
6
7 #include <ansi.h>
8 #include <cli.h>
9 #include <malloc.h>
10 #include <errno.h>
11 #include <linux/ctype.h>
12 #include <linux/delay.h>
13 #include <linux/list.h>
14 #include <watchdog.h>
15
16 #include "menu.h"
17
18 #define ansi 1
19
20 /*
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 */
27 struct 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 */
39 struct menu {
40 struct menu_item *default_item;
41 int timeout;
42 char *title;
43 int prompt;
44 void (*display_statusline)(struct menu *);
45 void (*item_data_print)(void *);
46 char *(*item_choice)(void *);
47 bool (*need_reprint)(void *);
48 void *item_choice_data;
49 struct list_head items;
50 int item_cnt;
51 };
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 */
menu_items_iter(struct menu * m,void * (* callback)(struct menu *,struct menu_item *,void *),void * extra)61 static 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 */
menu_item_print(struct menu * m,struct menu_item * item,void * extra)86 static inline void *menu_item_print(struct menu *m,
87 struct menu_item *item,
88 void *extra)
89 {
90 if (!m->item_data_print) {
91 puts(item->key);
92 putc('\n');
93 } else {
94 m->item_data_print(item->data);
95 }
96
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 */
menu_item_destroy(struct menu * m,struct menu_item * item,void * extra)104 static 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 */
menu_display(struct menu * m)120 static inline void menu_display(struct menu *m)
121 {
122 if (m->need_reprint) {
123 if (!m->need_reprint(m->item_choice_data))
124 return;
125 }
126
127 if (m->title) {
128 puts(m->title);
129 putc('\n');
130 }
131 if (m->display_statusline)
132 m->display_statusline(m);
133
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 */
menu_item_key_match(struct menu * m,struct menu_item * item,void * extra)145 static 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 */
menu_item_by_key(struct menu * m,char * item_key)166 static 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 /*
173 * 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 */
menu_default_choice(struct menu * m,void ** choice)176 int menu_default_choice(struct menu *m, void **choice)
177 {
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 */
menu_interactive_choice(struct menu * m,void ** choice)195 static 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
206 if (!m->item_choice) {
207 readret = cli_readline_into_buffer("Enter choice: ",
208 cbuf, m->timeout);
209
210 if (readret >= 0) {
211 choice_item = menu_item_by_key(m, cbuf);
212 if (!choice_item)
213 printf("%s not found\n", cbuf);
214 } else if (readret == -1) {
215 printf("<INTERRUPT>\n");
216 return -EINTR;
217 } else {
218 return menu_default_choice(m, choice);
219 }
220 } 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;
229 }
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 */
menu_default_set(struct menu * m,char * item_key)248 int 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
267 * if the menu is set to not prompt or the timeout expires. This is safe to
268 * call more than once.
269 *
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
277 * 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.
279 */
menu_get_choice(struct menu * m,void ** choice)280 int menu_get_choice(struct menu *m, void **choice)
281 {
282 if (!m || !choice)
283 return -EINVAL;
284
285 if (!m->item_cnt)
286 return -ENOENT;
287
288 if (!m->prompt)
289 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 */
menu_item_add(struct menu * m,char * item_key,void * item_data)312 int 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);
340 m->item_cnt++;
341
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 *
352 * 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.
358 *
359 * display_statusline - If not NULL, will be called to show a statusline when
360 * the menu is displayed.
361 *
362 * 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 *
368 * item_choice - If not NULL, will be called when asking the user to choose an
369 * item. Returns a key string corresponding to the chosen item or NULL if
370 * no item has been selected.
371 *
372 * need_reprint - If not NULL, will be called before printing the menu.
373 * Returning FALSE means the menu does not need reprint.
374 *
375 * item_choice_data - Will be passed as the argument to the item_choice function
376 *
377 * Returns a pointer to the menu if successful, or NULL if there is
378 * insufficient memory available to create the menu.
379 */
menu_create(char * title,int timeout,int prompt,void (* display_statusline)(struct menu *),void (* item_data_print)(void *),char * (* item_choice)(void *),bool (* need_reprint)(void *),void * item_choice_data)380 struct menu *menu_create(char *title, int timeout, int prompt,
381 void (*display_statusline)(struct menu *),
382 void (*item_data_print)(void *),
383 char *(*item_choice)(void *),
384 bool (*need_reprint)(void *),
385 void *item_choice_data)
386 {
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;
396 m->timeout = timeout;
397 m->display_statusline = display_statusline;
398 m->item_data_print = item_data_print;
399 m->item_choice = item_choice;
400 m->need_reprint = need_reprint;
401 m->item_choice_data = item_choice_data;
402 m->item_cnt = 0;
403
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
413 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 */
menu_destroy(struct menu * m)425 int 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 }
439
bootmenu_conv_shortcut_key(struct bootmenu_data * menu,int ichar)440 static 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
bootmenu_autoboot_loop(struct bootmenu_data * menu,struct cli_ch_state * cch)463 enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
464 struct cli_ch_state *cch)
465 {
466 enum bootmenu_key key = BKEY_NONE;
467 int i, c;
468
469 while (menu->delay > 0) {
470 int ichar;
471
472 if (ansi)
473 printf(ANSI_CURSOR_POSITION, menu->count + 5, 3);
474 printf("Hit any key to stop autoboot: %d ", menu->delay);
475 for (i = 0; i < 100; ++i) {
476 if (!tstc()) {
477 schedule();
478 mdelay(10);
479 continue;
480 }
481
482 menu->delay = -1;
483 c = getchar();
484
485 ichar = cli_ch_process(cch, c);
486
487 switch (ichar) {
488 case '\0':
489 key = BKEY_NONE;
490 break;
491 case '\n':
492 key = BKEY_SELECT;
493 break;
494 case 0x3: /* ^C */
495 key = BKEY_QUIT;
496 break;
497 case 'A' ... 'Z':
498 case 'a' ... 'z':
499 case '0' ... '9':
500 key = BKEY_SHORTCUT;
501 break;
502 default:
503 key = BKEY_NONE;
504 break;
505 }
506 break;
507 }
508
509 if (key == BKEY_SHORTCUT)
510 cch->shortcut_key = bootmenu_conv_shortcut_key(menu, ichar);
511
512 if (menu->delay < 0)
513 break;
514
515 --menu->delay;
516 }
517
518 if (ansi)
519 printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1);
520
521 if (menu->delay == 0)
522 key = BKEY_SELECT;
523
524 return key;
525 }
526
bootmenu_conv_key(int ichar)527 enum bootmenu_key bootmenu_conv_key(int ichar)
528 {
529 enum bootmenu_key key;
530
531 switch (ichar) {
532 case '\n':
533 /* enter key was pressed */
534 key = BKEY_SELECT;
535 break;
536 case CTL_CH('c'):
537 case '\e':
538 /* ^C was pressed */
539 key = BKEY_QUIT;
540 break;
541 case CTL_CH('p'):
542 key = BKEY_UP;
543 break;
544 case CTL_CH('n'):
545 key = BKEY_DOWN;
546 break;
547 case CTL_CH('s'):
548 key = BKEY_SAVE;
549 break;
550 case '+':
551 key = BKEY_PLUS;
552 break;
553 case '-':
554 key = BKEY_MINUS;
555 break;
556 case ' ':
557 key = BKEY_SPACE;
558 break;
559 case 'A' ... 'Z':
560 case 'a' ... 'z':
561 case '0' ... '9':
562 key = BKEY_SHORTCUT;
563 break;
564 default:
565 key = BKEY_NONE;
566 break;
567 }
568
569 return key;
570 }
571
bootmenu_loop(struct bootmenu_data * menu,struct cli_ch_state * cch)572 enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
573 struct cli_ch_state *cch)
574 {
575 enum bootmenu_key key;
576 int c, errchar = 0;
577
578 c = cli_ch_process(cch, 0);
579 if (!c) {
580 while (!c && !tstc()) {
581 schedule();
582 mdelay(10);
583 c = cli_ch_process(cch, errchar);
584 errchar = -ETIMEDOUT;
585 }
586 if (!c) {
587 c = getchar();
588 c = cli_ch_process(cch, c);
589 }
590 }
591
592 key = bootmenu_conv_key(c);
593
594 if (key == BKEY_SHORTCUT)
595 cch->shortcut_key = bootmenu_conv_shortcut_key(menu, c);
596
597 return key;
598 }
599