Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2010-2011 Calxeda, Inc. |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 4 | * Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 7 | #include <ansi.h> |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 8 | #include <common.h> |
Simon Glass | dec3c01 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 9 | #include <cli.h> |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 10 | #include <malloc.h> |
| 11 | #include <errno.h> |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 12 | #include <linux/delay.h> |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 13 | #include <linux/list.h> |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 14 | #include <watchdog.h> |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 15 | |
| 16 | #include "menu.h" |
| 17 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 18 | #define ansi 0 |
| 19 | |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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; |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 41 | int timeout; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 42 | char *title; |
| 43 | int prompt; |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 44 | void (*display_statusline)(struct menu *); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 45 | void (*item_data_print)(void *); |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 46 | char *(*item_choice)(void *); |
| 47 | void *item_choice_data; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 48 | struct list_head items; |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 49 | int item_cnt; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* |
| 53 | * An iterator function for menu items. callback will be called for each item |
| 54 | * in m, with m, a pointer to the item, and extra being passed to callback. If |
| 55 | * callback returns a value other than NULL, iteration stops and the value |
| 56 | * return by callback is returned from menu_items_iter. This allows it to be |
| 57 | * used for search type operations. It is also safe for callback to remove the |
| 58 | * item from the list of items. |
| 59 | */ |
| 60 | static inline void *menu_items_iter(struct menu *m, |
| 61 | void *(*callback)(struct menu *, struct menu_item *, void *), |
| 62 | void *extra) |
| 63 | { |
| 64 | struct list_head *pos, *n; |
| 65 | struct menu_item *item; |
| 66 | void *ret; |
| 67 | |
| 68 | list_for_each_safe(pos, n, &m->items) { |
| 69 | item = list_entry(pos, struct menu_item, list); |
| 70 | |
| 71 | ret = callback(m, item, extra); |
| 72 | |
| 73 | if (ret) |
| 74 | return ret; |
| 75 | } |
| 76 | |
| 77 | return NULL; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Print a menu_item. If the consumer provided an item_data_print function |
| 82 | * when creating the menu, call it with a pointer to the item's private data. |
| 83 | * Otherwise, print the key of the item. |
| 84 | */ |
| 85 | static inline void *menu_item_print(struct menu *m, |
| 86 | struct menu_item *item, |
| 87 | void *extra) |
| 88 | { |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 89 | if (!m->item_data_print) { |
Anatolij Gustschin | b50e1e0 | 2011-12-03 06:46:07 +0000 | [diff] [blame] | 90 | puts(item->key); |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 91 | putc('\n'); |
| 92 | } else { |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 93 | m->item_data_print(item->data); |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 94 | } |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 95 | |
| 96 | return NULL; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Free the memory used by a menu item. This includes the memory used by its |
| 101 | * key. |
| 102 | */ |
| 103 | static inline void *menu_item_destroy(struct menu *m, |
| 104 | struct menu_item *item, |
| 105 | void *extra) |
| 106 | { |
| 107 | if (item->key) |
| 108 | free(item->key); |
| 109 | |
| 110 | free(item); |
| 111 | |
| 112 | return NULL; |
| 113 | } |
| 114 | |
| 115 | /* |
| 116 | * Display a menu so the user can make a choice of an item. First display its |
| 117 | * title, if any, and then each item in the menu. |
| 118 | */ |
| 119 | static inline void menu_display(struct menu *m) |
| 120 | { |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 121 | if (m->title) { |
| 122 | puts(m->title); |
| 123 | putc('\n'); |
| 124 | } |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 125 | if (m->display_statusline) |
| 126 | m->display_statusline(m); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 127 | |
| 128 | menu_items_iter(m, menu_item_print, NULL); |
| 129 | } |
| 130 | |
| 131 | /* |
| 132 | * Check if an item's key matches a provided string, pointed to by extra. If |
| 133 | * extra is NULL, an item with a NULL key will match. Otherwise, the item's |
| 134 | * key has to match according to strcmp. |
| 135 | * |
| 136 | * This is called via menu_items_iter, so it returns a pointer to the item if |
| 137 | * the key matches, and returns NULL otherwise. |
| 138 | */ |
| 139 | static inline void *menu_item_key_match(struct menu *m, |
| 140 | struct menu_item *item, void *extra) |
| 141 | { |
| 142 | char *item_key = extra; |
| 143 | |
| 144 | if (!item_key || !item->key) { |
| 145 | if (item_key == item->key) |
| 146 | return item; |
| 147 | |
| 148 | return NULL; |
| 149 | } |
| 150 | |
| 151 | if (strcmp(item->key, item_key) == 0) |
| 152 | return item; |
| 153 | |
| 154 | return NULL; |
| 155 | } |
| 156 | |
| 157 | /* |
| 158 | * Find the first item with a key matching item_key, if any exists. |
| 159 | */ |
| 160 | static inline struct menu_item *menu_item_by_key(struct menu *m, |
| 161 | char *item_key) |
| 162 | { |
| 163 | return menu_items_iter(m, menu_item_key_match, item_key); |
| 164 | } |
| 165 | |
| 166 | /* |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 167 | * Set *choice to point to the default item's data, if any default item was |
| 168 | * set, and returns 1. If no default item was set, returns -ENOENT. |
| 169 | */ |
Anatolij Gustschin | fb6068a | 2013-03-23 14:52:04 +0000 | [diff] [blame] | 170 | int menu_default_choice(struct menu *m, void **choice) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 171 | { |
| 172 | if (m->default_item) { |
| 173 | *choice = m->default_item->data; |
| 174 | return 1; |
| 175 | } |
| 176 | |
| 177 | return -ENOENT; |
| 178 | } |
| 179 | |
| 180 | /* |
| 181 | * Displays the menu and asks the user to choose an item. *choice will point |
| 182 | * to the private data of the item the user chooses. The user makes a choice |
| 183 | * by inputting a string matching the key of an item. Invalid choices will |
| 184 | * cause the user to be prompted again, repeatedly, until the user makes a |
| 185 | * valid choice. The user can exit the menu without making a choice via ^c. |
| 186 | * |
| 187 | * Returns 1 if the user made a choice, or -EINTR if they bail via ^c. |
| 188 | */ |
| 189 | static inline int menu_interactive_choice(struct menu *m, void **choice) |
| 190 | { |
| 191 | char cbuf[CONFIG_SYS_CBSIZE]; |
| 192 | struct menu_item *choice_item = NULL; |
| 193 | int readret; |
| 194 | |
| 195 | while (!choice_item) { |
| 196 | cbuf[0] = '\0'; |
| 197 | |
| 198 | menu_display(m); |
| 199 | |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 200 | if (!m->item_choice) { |
Simon Glass | be6aafc | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 201 | readret = cli_readline_into_buffer("Enter choice: ", |
Masahiro Yamada | c46d0fe | 2018-05-24 17:04:57 +0900 | [diff] [blame] | 202 | cbuf, m->timeout); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 203 | |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 204 | if (readret >= 0) { |
| 205 | choice_item = menu_item_by_key(m, cbuf); |
| 206 | if (!choice_item) |
| 207 | printf("%s not found\n", cbuf); |
Tuomas Tynkkynen | 60c47af | 2015-05-07 21:29:19 +0300 | [diff] [blame] | 208 | } else if (readret == -1) { |
| 209 | printf("<INTERRUPT>\n"); |
| 210 | return -EINTR; |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 211 | } else { |
| 212 | return menu_default_choice(m, choice); |
Heiko Schocher | e795b2e | 2012-01-16 22:24:29 +0000 | [diff] [blame] | 213 | } |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 214 | } else { |
| 215 | char *key = m->item_choice(m->item_choice_data); |
| 216 | |
| 217 | if (key) |
| 218 | choice_item = menu_item_by_key(m, key); |
| 219 | } |
| 220 | |
| 221 | if (!choice_item) |
| 222 | m->timeout = 0; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | *choice = choice_item->data; |
| 226 | |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * menu_default_set() - Sets the default choice for the menu. This is safe to |
| 232 | * call more than once on a menu. |
| 233 | * |
| 234 | * m - Points to a menu created by menu_create(). |
| 235 | * |
| 236 | * item_key - Points to a string that, when compared using strcmp, matches the |
| 237 | * key for an existing item in the menu. |
| 238 | * |
| 239 | * Returns 1 if successful, -EINVAL if m is NULL, or -ENOENT if no item with a |
| 240 | * key matching item_key is found. |
| 241 | */ |
| 242 | int menu_default_set(struct menu *m, char *item_key) |
| 243 | { |
| 244 | struct menu_item *item; |
| 245 | |
| 246 | if (!m) |
| 247 | return -EINVAL; |
| 248 | |
| 249 | item = menu_item_by_key(m, item_key); |
| 250 | |
| 251 | if (!item) |
| 252 | return -ENOENT; |
| 253 | |
| 254 | m->default_item = item; |
| 255 | |
| 256 | return 1; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * menu_get_choice() - Returns the user's selected menu entry, or the default |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 261 | * if the menu is set to not prompt or the timeout expires. This is safe to |
| 262 | * call more than once. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 263 | * |
| 264 | * m - Points to a menu created by menu_create(). |
| 265 | * |
| 266 | * choice - Points to a location that will store a pointer to the selected |
| 267 | * menu item. If no item is selected or there is an error, no value will be |
| 268 | * written at the location it points to. |
| 269 | * |
| 270 | * Returns 1 if successful, -EINVAL if m or choice is NULL, -ENOENT if no |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 271 | * default has been set and the menu is set to not prompt or the timeout |
| 272 | * expires, or -EINTR if the user exits the menu via ^c. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 273 | */ |
| 274 | int menu_get_choice(struct menu *m, void **choice) |
| 275 | { |
| 276 | if (!m || !choice) |
| 277 | return -EINVAL; |
| 278 | |
Masahisa Kojima | 72f823f | 2022-04-28 17:09:37 +0900 | [diff] [blame] | 279 | if (!m->item_cnt) |
| 280 | return -ENOENT; |
| 281 | |
Masahisa Kojima | 6f59d19 | 2022-04-28 17:09:36 +0900 | [diff] [blame] | 282 | if (!m->prompt) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 283 | return menu_default_choice(m, choice); |
| 284 | |
| 285 | return menu_interactive_choice(m, choice); |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * menu_item_add() - Adds or replaces a menu item. Note that this replaces the |
| 290 | * data of an item if it already exists, but doesn't change the order of the |
| 291 | * item. |
| 292 | * |
| 293 | * m - Points to a menu created by menu_create(). |
| 294 | * |
| 295 | * item_key - Points to a string that will uniquely identify the item. The |
| 296 | * string will be copied to internal storage, and is safe to discard after |
| 297 | * passing to menu_item_add. |
| 298 | * |
| 299 | * item_data - An opaque pointer associated with an item. It is never |
| 300 | * dereferenced internally, but will be passed to the item_data_print, and |
| 301 | * will be returned from menu_get_choice if the menu item is selected. |
| 302 | * |
| 303 | * Returns 1 if successful, -EINVAL if m is NULL, or -ENOMEM if there is |
| 304 | * insufficient memory to add the menu item. |
| 305 | */ |
| 306 | int menu_item_add(struct menu *m, char *item_key, void *item_data) |
| 307 | { |
| 308 | struct menu_item *item; |
| 309 | |
| 310 | if (!m) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | item = menu_item_by_key(m, item_key); |
| 314 | |
| 315 | if (item) { |
| 316 | item->data = item_data; |
| 317 | return 1; |
| 318 | } |
| 319 | |
| 320 | item = malloc(sizeof *item); |
| 321 | if (!item) |
| 322 | return -ENOMEM; |
| 323 | |
| 324 | item->key = strdup(item_key); |
| 325 | |
| 326 | if (!item->key) { |
| 327 | free(item); |
| 328 | return -ENOMEM; |
| 329 | } |
| 330 | |
| 331 | item->data = item_data; |
| 332 | |
| 333 | list_add_tail(&item->list, &m->items); |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 334 | m->item_cnt++; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 335 | |
| 336 | return 1; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * menu_create() - Creates a menu handle with default settings |
| 341 | * |
| 342 | * title - If not NULL, points to a string that will be displayed before the |
| 343 | * list of menu items. It will be copied to internal storage, and is safe to |
| 344 | * discard after passing to menu_create(). |
| 345 | * |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 346 | * timeout - A delay in seconds to wait for user input. If 0, timeout is |
| 347 | * disabled, and the default choice will be returned unless prompt is 1. |
| 348 | * |
| 349 | * prompt - If 0, don't ask for user input unless there is an interrupted |
| 350 | * timeout. If 1, the user will be prompted for input regardless of the value |
| 351 | * of timeout. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 352 | * |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 353 | * display_statusline - If not NULL, will be called to show a statusline when |
| 354 | * the menu is displayed. |
| 355 | * |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 356 | * item_data_print - If not NULL, will be called for each item when the menu |
| 357 | * is displayed, with the pointer to the item's data passed as the argument. |
| 358 | * If NULL, each item's key will be printed instead. Since an item's key is |
| 359 | * what must be entered to select an item, the item_data_print function should |
| 360 | * make it obvious what the key for each entry is. |
| 361 | * |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 362 | * item_choice - If not NULL, will be called when asking the user to choose an |
Alexander Merkle | 0137e60 | 2016-03-17 15:44:47 +0100 | [diff] [blame] | 363 | * item. Returns a key string corresponding to the chosen item or NULL if |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 364 | * no item has been selected. |
| 365 | * |
| 366 | * item_choice_data - Will be passed as the argument to the item_choice function |
| 367 | * |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 368 | * Returns a pointer to the menu if successful, or NULL if there is |
| 369 | * insufficient memory available to create the menu. |
| 370 | */ |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 371 | struct menu *menu_create(char *title, int timeout, int prompt, |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 372 | void (*display_statusline)(struct menu *), |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 373 | void (*item_data_print)(void *), |
| 374 | char *(*item_choice)(void *), |
| 375 | void *item_choice_data) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 376 | { |
| 377 | struct menu *m; |
| 378 | |
| 379 | m = malloc(sizeof *m); |
| 380 | |
| 381 | if (!m) |
| 382 | return NULL; |
| 383 | |
| 384 | m->default_item = NULL; |
| 385 | m->prompt = prompt; |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 386 | m->timeout = timeout; |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 387 | m->display_statusline = display_statusline; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 388 | m->item_data_print = item_data_print; |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 389 | m->item_choice = item_choice; |
| 390 | m->item_choice_data = item_choice_data; |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 391 | m->item_cnt = 0; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 392 | |
| 393 | if (title) { |
| 394 | m->title = strdup(title); |
| 395 | if (!m->title) { |
| 396 | free(m); |
| 397 | return NULL; |
| 398 | } |
| 399 | } else |
| 400 | m->title = NULL; |
| 401 | |
| 402 | |
| 403 | INIT_LIST_HEAD(&m->items); |
| 404 | |
| 405 | return m; |
| 406 | } |
| 407 | |
| 408 | /* |
| 409 | * menu_destroy() - frees the memory used by a menu and its items. |
| 410 | * |
| 411 | * m - Points to a menu created by menu_create(). |
| 412 | * |
| 413 | * Returns 1 if successful, or -EINVAL if m is NULL. |
| 414 | */ |
| 415 | int menu_destroy(struct menu *m) |
| 416 | { |
| 417 | if (!m) |
| 418 | return -EINVAL; |
| 419 | |
| 420 | menu_items_iter(m, menu_item_destroy, NULL); |
| 421 | |
| 422 | if (m->title) |
| 423 | free(m->title); |
| 424 | |
| 425 | free(m); |
| 426 | |
| 427 | return 1; |
| 428 | } |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 429 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 430 | enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, |
| 431 | struct cli_ch_state *cch) |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 432 | { |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 433 | enum bootmenu_key key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 434 | int i, c; |
| 435 | |
| 436 | while (menu->delay > 0) { |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 437 | if (ansi) |
| 438 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 439 | printf("Hit any key to stop autoboot: %d ", menu->delay); |
| 440 | for (i = 0; i < 100; ++i) { |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 441 | int ichar; |
| 442 | |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 443 | if (!tstc()) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 444 | schedule(); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 445 | mdelay(10); |
| 446 | continue; |
| 447 | } |
| 448 | |
| 449 | menu->delay = -1; |
| 450 | c = getchar(); |
| 451 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 452 | ichar = cli_ch_process(cch, c); |
| 453 | |
| 454 | switch (ichar) { |
| 455 | case '\0': |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 456 | key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 457 | break; |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 458 | case '\n': |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 459 | key = BKEY_SELECT; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 460 | break; |
| 461 | case 0x3: /* ^C */ |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 462 | key = BKEY_QUIT; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 463 | break; |
| 464 | default: |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 465 | key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 466 | break; |
| 467 | } |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 468 | break; |
| 469 | } |
| 470 | |
| 471 | if (menu->delay < 0) |
| 472 | break; |
| 473 | |
| 474 | --menu->delay; |
| 475 | } |
| 476 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 477 | if (ansi) |
| 478 | printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 479 | |
| 480 | if (menu->delay == 0) |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 481 | key = BKEY_SELECT; |
| 482 | |
| 483 | return key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 484 | } |
| 485 | |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 486 | enum bootmenu_key bootmenu_conv_key(int ichar) |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 487 | { |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 488 | enum bootmenu_key key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 489 | |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 490 | switch (ichar) { |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 491 | case '\n': |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 492 | /* enter key was pressed */ |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 493 | key = BKEY_SELECT; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 494 | break; |
| 495 | case CTL_CH('c'): |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 496 | case '\e': |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 497 | /* ^C was pressed */ |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 498 | key = BKEY_QUIT; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 499 | break; |
| 500 | case CTL_CH('p'): |
| 501 | key = BKEY_UP; |
| 502 | break; |
| 503 | case CTL_CH('n'): |
| 504 | key = BKEY_DOWN; |
| 505 | break; |
| 506 | case '+': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 507 | key = BKEY_PLUS; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 508 | break; |
| 509 | case '-': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 510 | key = BKEY_MINUS; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 511 | break; |
| 512 | case ' ': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 513 | key = BKEY_SPACE; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 514 | break; |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 515 | default: |
| 516 | key = BKEY_NONE; |
| 517 | break; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 518 | } |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 519 | |
| 520 | return key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 521 | } |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 522 | |
| 523 | enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, |
| 524 | struct cli_ch_state *cch) |
| 525 | { |
| 526 | enum bootmenu_key key; |
| 527 | int c; |
| 528 | |
| 529 | c = cli_ch_process(cch, 0); |
| 530 | if (!c) { |
| 531 | while (!c && !tstc()) { |
| 532 | schedule(); |
| 533 | mdelay(10); |
| 534 | c = cli_ch_process(cch, -ETIMEDOUT); |
| 535 | } |
| 536 | if (!c) { |
| 537 | c = getchar(); |
| 538 | c = cli_ch_process(cch, c); |
| 539 | } |
| 540 | } |
| 541 | |
| 542 | key = bootmenu_conv_key(c); |
| 543 | |
| 544 | return key; |
| 545 | } |