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> |
Simon Glass | dec3c01 | 2014-04-10 20:01:25 -0600 | [diff] [blame] | 8 | #include <cli.h> |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 9 | #include <malloc.h> |
| 10 | #include <errno.h> |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 11 | #include <linux/ctype.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 | 1ee7ab8 | 2023-06-17 11:49:48 +0100 | [diff] [blame] | 18 | #define ansi 1 |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 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 *); |
developer | c2a1e9e | 2024-10-29 17:47:16 +0800 | [diff] [blame] | 47 | bool (*need_reprint)(void *); |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 48 | void *item_choice_data; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 49 | struct list_head items; |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 50 | int item_cnt; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 86 | static inline void *menu_item_print(struct menu *m, |
| 87 | struct menu_item *item, |
| 88 | void *extra) |
| 89 | { |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 90 | if (!m->item_data_print) { |
Anatolij Gustschin | b50e1e0 | 2011-12-03 06:46:07 +0000 | [diff] [blame] | 91 | puts(item->key); |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 92 | putc('\n'); |
| 93 | } else { |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 94 | m->item_data_print(item->data); |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 95 | } |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 120 | static inline void menu_display(struct menu *m) |
| 121 | { |
developer | c2a1e9e | 2024-10-29 17:47:16 +0800 | [diff] [blame] | 122 | if (m->need_reprint) { |
| 123 | if (!m->need_reprint(m->item_choice_data)) |
| 124 | return; |
| 125 | } |
| 126 | |
Wolfgang Denk | 4f5c100b | 2011-11-28 20:19:41 +0100 | [diff] [blame] | 127 | if (m->title) { |
| 128 | puts(m->title); |
| 129 | putc('\n'); |
| 130 | } |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 131 | if (m->display_statusline) |
| 132 | m->display_statusline(m); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 | */ |
| 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 | /* |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
Anatolij Gustschin | fb6068a | 2013-03-23 14:52:04 +0000 | [diff] [blame] | 176 | int menu_default_choice(struct menu *m, void **choice) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 | |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 206 | if (!m->item_choice) { |
Simon Glass | be6aafc | 2014-04-10 20:01:27 -0600 | [diff] [blame] | 207 | readret = cli_readline_into_buffer("Enter choice: ", |
Masahiro Yamada | c46d0fe | 2018-05-24 17:04:57 +0900 | [diff] [blame] | 208 | cbuf, m->timeout); |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 209 | |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 210 | if (readret >= 0) { |
| 211 | choice_item = menu_item_by_key(m, cbuf); |
| 212 | if (!choice_item) |
| 213 | printf("%s not found\n", cbuf); |
Tuomas Tynkkynen | 60c47af | 2015-05-07 21:29:19 +0300 | [diff] [blame] | 214 | } else if (readret == -1) { |
| 215 | printf("<INTERRUPT>\n"); |
| 216 | return -EINTR; |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 217 | } else { |
| 218 | return menu_default_choice(m, choice); |
Heiko Schocher | e795b2e | 2012-01-16 22:24:29 +0000 | [diff] [blame] | 219 | } |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 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; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 267 | * if the menu is set to not prompt or the timeout expires. This is safe to |
| 268 | * call more than once. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 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. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 279 | */ |
| 280 | int menu_get_choice(struct menu *m, void **choice) |
| 281 | { |
| 282 | if (!m || !choice) |
| 283 | return -EINVAL; |
| 284 | |
Masahisa Kojima | 72f823f | 2022-04-28 17:09:37 +0900 | [diff] [blame] | 285 | if (!m->item_cnt) |
| 286 | return -ENOENT; |
| 287 | |
Masahisa Kojima | 6f59d19 | 2022-04-28 17:09:36 +0900 | [diff] [blame] | 288 | if (!m->prompt) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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); |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 340 | m->item_cnt++; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | * |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 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. |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 358 | * |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 359 | * display_statusline - If not NULL, will be called to show a statusline when |
| 360 | * the menu is displayed. |
| 361 | * |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | * |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 368 | * 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] | 369 | * 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] | 370 | * no item has been selected. |
| 371 | * |
developer | c2a1e9e | 2024-10-29 17:47:16 +0800 | [diff] [blame] | 372 | * 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ár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 375 | * item_choice_data - Will be passed as the argument to the item_choice function |
| 376 | * |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 377 | * Returns a pointer to the menu if successful, or NULL if there is |
| 378 | * insufficient memory available to create the menu. |
| 379 | */ |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 380 | struct menu *menu_create(char *title, int timeout, int prompt, |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 381 | void (*display_statusline)(struct menu *), |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 382 | void (*item_data_print)(void *), |
| 383 | char *(*item_choice)(void *), |
developer | c2a1e9e | 2024-10-29 17:47:16 +0800 | [diff] [blame] | 384 | bool (*need_reprint)(void *), |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 385 | void *item_choice_data) |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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; |
Jason Hobbs | 65febe6 | 2011-08-23 11:06:50 +0000 | [diff] [blame] | 396 | m->timeout = timeout; |
Thirupathaiah Annapureddy | d6b9f6b | 2020-03-18 11:38:42 -0700 | [diff] [blame] | 397 | m->display_statusline = display_statusline; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 398 | m->item_data_print = item_data_print; |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 399 | m->item_choice = item_choice; |
developer | c2a1e9e | 2024-10-29 17:47:16 +0800 | [diff] [blame] | 400 | m->need_reprint = need_reprint; |
Pali Rohár | d0d8d3b | 2013-03-23 14:50:40 +0000 | [diff] [blame] | 401 | m->item_choice_data = item_choice_data; |
Leon Yu | 9686a93 | 2019-06-21 12:12:39 +0800 | [diff] [blame] | 402 | m->item_cnt = 0; |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | |
Jason Hobbs | 0685d82 | 2011-08-23 11:06:49 +0000 | [diff] [blame] | 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 | */ |
| 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 | } |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 439 | |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 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 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 463 | enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, |
| 464 | struct cli_ch_state *cch) |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 465 | { |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 466 | enum bootmenu_key key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 467 | int i, c; |
| 468 | |
| 469 | while (menu->delay > 0) { |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 470 | int ichar; |
| 471 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 472 | if (ansi) |
| 473 | printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 474 | printf("Hit any key to stop autoboot: %d ", menu->delay); |
| 475 | for (i = 0; i < 100; ++i) { |
| 476 | if (!tstc()) { |
Stefan Roese | 80877fa | 2022-09-02 14:10:46 +0200 | [diff] [blame] | 477 | schedule(); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 478 | mdelay(10); |
| 479 | continue; |
| 480 | } |
| 481 | |
| 482 | menu->delay = -1; |
| 483 | c = getchar(); |
| 484 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 485 | ichar = cli_ch_process(cch, c); |
| 486 | |
| 487 | switch (ichar) { |
| 488 | case '\0': |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 489 | key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 490 | break; |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 491 | case '\n': |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 492 | key = BKEY_SELECT; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 493 | break; |
| 494 | case 0x3: /* ^C */ |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 495 | key = BKEY_QUIT; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 496 | break; |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 497 | case 'A' ... 'Z': |
| 498 | case 'a' ... 'z': |
| 499 | case '0' ... '9': |
| 500 | key = BKEY_SHORTCUT; |
| 501 | break; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 502 | default: |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 503 | key = BKEY_NONE; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 504 | break; |
| 505 | } |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 506 | break; |
| 507 | } |
| 508 | |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 509 | if (key == BKEY_SHORTCUT) |
| 510 | cch->shortcut_key = bootmenu_conv_shortcut_key(menu, ichar); |
| 511 | |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 512 | if (menu->delay < 0) |
| 513 | break; |
| 514 | |
| 515 | --menu->delay; |
| 516 | } |
| 517 | |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 518 | if (ansi) |
| 519 | printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 520 | |
| 521 | if (menu->delay == 0) |
Simon Glass | 81544c4 | 2023-01-06 08:52:23 -0600 | [diff] [blame] | 522 | key = BKEY_SELECT; |
| 523 | |
| 524 | return key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 525 | } |
| 526 | |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 527 | enum bootmenu_key bootmenu_conv_key(int ichar) |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 528 | { |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 529 | enum bootmenu_key key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 530 | |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 531 | switch (ichar) { |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 532 | case '\n': |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 533 | /* enter key was pressed */ |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 534 | key = BKEY_SELECT; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 535 | break; |
| 536 | case CTL_CH('c'): |
Simon Glass | 9d8d387 | 2023-01-06 08:52:26 -0600 | [diff] [blame] | 537 | case '\e': |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 538 | /* ^C was pressed */ |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 539 | key = BKEY_QUIT; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 540 | break; |
| 541 | case CTL_CH('p'): |
| 542 | key = BKEY_UP; |
| 543 | break; |
| 544 | case CTL_CH('n'): |
| 545 | key = BKEY_DOWN; |
| 546 | break; |
Masahisa Kojima | d38ecb7 | 2023-02-02 18:24:44 +0900 | [diff] [blame] | 547 | case CTL_CH('s'): |
| 548 | key = BKEY_SAVE; |
| 549 | break; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 550 | case '+': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 551 | key = BKEY_PLUS; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 552 | break; |
| 553 | case '-': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 554 | key = BKEY_MINUS; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 555 | break; |
| 556 | case ' ': |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 557 | key = BKEY_SPACE; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 558 | break; |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 559 | case 'A' ... 'Z': |
| 560 | case 'a' ... 'z': |
| 561 | case '0' ... '9': |
| 562 | key = BKEY_SHORTCUT; |
| 563 | break; |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 564 | default: |
| 565 | key = BKEY_NONE; |
| 566 | break; |
Simon Glass | d52ce65 | 2023-01-06 08:52:25 -0600 | [diff] [blame] | 567 | } |
Simon Glass | f1d3c8e | 2023-01-06 08:52:24 -0600 | [diff] [blame] | 568 | |
| 569 | return key; |
Masahisa Kojima | 0e1b996 | 2022-04-28 17:09:45 +0900 | [diff] [blame] | 570 | } |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 571 | |
| 572 | enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, |
| 573 | struct cli_ch_state *cch) |
| 574 | { |
| 575 | enum bootmenu_key key; |
developer | 744a0a2 | 2024-10-29 17:47:10 +0800 | [diff] [blame] | 576 | int c, errchar = 0; |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 577 | |
| 578 | c = cli_ch_process(cch, 0); |
| 579 | if (!c) { |
| 580 | while (!c && !tstc()) { |
| 581 | schedule(); |
| 582 | mdelay(10); |
developer | 744a0a2 | 2024-10-29 17:47:10 +0800 | [diff] [blame] | 583 | c = cli_ch_process(cch, errchar); |
| 584 | errchar = -ETIMEDOUT; |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 585 | } |
| 586 | if (!c) { |
| 587 | c = getchar(); |
| 588 | c = cli_ch_process(cch, c); |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | key = bootmenu_conv_key(c); |
| 593 | |
Christian Marangi | b357fc6 | 2025-05-25 15:43:58 +0200 | [diff] [blame] | 594 | if (key == BKEY_SHORTCUT) |
| 595 | cch->shortcut_key = bootmenu_conv_shortcut_key(menu, c); |
| 596 | |
Simon Glass | 1b56fe1 | 2023-01-06 08:52:35 -0600 | [diff] [blame] | 597 | return key; |
| 598 | } |