100-16-cmd-bootmenu-add-ability-to-select-item-by-shortkey.patch 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. From 5a15437610e8e8c68dc347845a83d0cbad80ca08 Mon Sep 17 00:00:00 2001
  2. From: Weijie Gao <[email protected]>
  3. Date: Tue, 19 Jan 2021 10:58:48 +0800
  4. Subject: [PATCH 51/71] cmd: bootmenu: add ability to select item by shortkey
  5. Add ability to use shortkey to select item for bootmenu command
  6. Signed-off-by: Weijie Gao <[email protected]>
  7. ---
  8. cmd/bootmenu.c | 34 ++++++++++++++++++++++++-----
  9. common/menu.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++--
  10. include/menu.h | 12 +++++++----
  11. 3 files changed, 93 insertions(+), 11 deletions(-)
  12. --- a/cmd/bootmenu.c
  13. +++ b/cmd/bootmenu.c
  14. @@ -89,6 +89,7 @@ static char *bootmenu_choice_entry(void
  15. struct bootmenu_data *menu = data;
  16. struct bootmenu_entry *iter;
  17. enum bootmenu_key key = BKEY_NONE;
  18. + int choice = -1;
  19. int i;
  20. cli_ch_init(cch);
  21. @@ -96,10 +97,10 @@ static char *bootmenu_choice_entry(void
  22. while (1) {
  23. if (menu->delay >= 0) {
  24. /* Autoboot was not stopped */
  25. - key = bootmenu_autoboot_loop(menu, cch);
  26. + key = bootmenu_autoboot_loop(menu, cch, &choice);
  27. } else {
  28. /* Some key was pressed, so autoboot was stopped */
  29. - key = bootmenu_loop(menu, cch);
  30. + key = bootmenu_loop(menu, cch, &choice);
  31. }
  32. switch (key) {
  33. @@ -113,6 +114,12 @@ static char *bootmenu_choice_entry(void
  34. ++menu->active;
  35. /* no menu key selected, regenerate menu */
  36. return NULL;
  37. + case BKEY_CHOICE:
  38. + menu->active = choice;
  39. + if (!menu->last_choiced) {
  40. + menu->last_choiced = true;
  41. + return NULL;
  42. + }
  43. case BKEY_SELECT:
  44. iter = menu->first;
  45. for (i = 0; i < menu->active; ++i)
  46. @@ -170,6 +177,9 @@ static int prepare_bootmenu_entry(struct
  47. unsigned short int i = *index;
  48. struct bootmenu_entry *entry = NULL;
  49. struct bootmenu_entry *iter = *current;
  50. + char *choice_option;
  51. + char choice_char;
  52. + int len;
  53. while ((option = bootmenu_getoption(i))) {
  54. @@ -184,11 +194,24 @@ static int prepare_bootmenu_entry(struct
  55. if (!entry)
  56. return -ENOMEM;
  57. - entry->title = strndup(option, sep - option);
  58. + /* Add KEY_CHOICE support: '%d. %s\0' : len --> len + 4 */
  59. + len = sep - option + 4;
  60. + choice_option = malloc(len);
  61. + if (!choice_option) {
  62. + free(entry->title);
  63. + free(entry);
  64. + return -ENOMEM;
  65. + }
  66. + if (!get_choice_char(i, &choice_char))
  67. + len = snprintf(choice_option, len, "%c. %s", choice_char, option);
  68. + else
  69. + len = snprintf(choice_option, len, " %s", option);
  70. + entry->title = strndup(choice_option, len);
  71. if (!entry->title) {
  72. free(entry);
  73. return -ENOMEM;
  74. }
  75. + free(choice_option);
  76. entry->command = strdup(sep + 1);
  77. if (!entry->command) {
  78. @@ -334,6 +357,7 @@ static struct bootmenu_data *bootmenu_cr
  79. menu->delay = delay;
  80. menu->active = 0;
  81. menu->first = NULL;
  82. + menu->last_choiced = false;
  83. default_str = env_get("bootmenu_default");
  84. if (default_str)
  85. @@ -369,9 +393,9 @@ static struct bootmenu_data *bootmenu_cr
  86. /* Add Quit entry if entering U-Boot console is disabled */
  87. if (!IS_ENABLED(CONFIG_BOOTMENU_DISABLE_UBOOT_CONSOLE))
  88. - entry->title = strdup("U-Boot console");
  89. + entry->title = strdup("0. U-Boot console");
  90. else
  91. - entry->title = strdup("Quit");
  92. + entry->title = strdup("0. Quit");
  93. if (!entry->title) {
  94. free(entry);
  95. --- a/common/menu.c
  96. +++ b/common/menu.c
  97. @@ -49,6 +49,33 @@ struct menu {
  98. int item_cnt;
  99. };
  100. +const char choice_chars[] = {
  101. + '1', '2', '3', '4', '5', '6', '7', '8', '9',
  102. + 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
  103. + 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
  104. + 'u', 'v', 'w', 'x', 'y', 'z'
  105. +};
  106. +
  107. +static int find_choice(char choice)
  108. +{
  109. + int i;
  110. +
  111. + for (i = 0; i < ARRAY_SIZE(choice_chars); i++)
  112. + if (tolower(choice) == choice_chars[i])
  113. + return i;
  114. +
  115. + return -1;
  116. +}
  117. +
  118. +int get_choice_char(int index, char *result)
  119. +{
  120. + if (index < ARRAY_SIZE(choice_chars))
  121. + *result = choice_chars[index];
  122. + else
  123. + return -1;
  124. + return 0;
  125. +}
  126. +
  127. /*
  128. * An iterator function for menu items. callback will be called for each item
  129. * in m, with m, a pointer to the item, and extra being passed to callback. If
  130. @@ -428,7 +455,7 @@ int menu_destroy(struct menu *m)
  131. }
  132. enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
  133. - struct cli_ch_state *cch)
  134. + struct cli_ch_state *cch, int *choice)
  135. {
  136. enum bootmenu_key key = BKEY_NONE;
  137. int i, c;
  138. @@ -463,6 +490,19 @@ enum bootmenu_key bootmenu_autoboot_loop
  139. break;
  140. default:
  141. key = BKEY_NONE;
  142. + if (cch->esc_len || !choice)
  143. + break;
  144. +
  145. + *choice = find_choice(c);
  146. + if ((*choice >= 0 &&
  147. + *choice < menu->count - 1)) {
  148. + key = BKEY_CHOICE;
  149. + } else if (c == '0') {
  150. + *choice = menu->count - 1;
  151. + key = BKEY_CHOICE;
  152. + } else {
  153. + key = BKEY_NONE;
  154. + }
  155. break;
  156. }
  157. break;
  158. @@ -483,7 +523,8 @@ enum bootmenu_key bootmenu_autoboot_loop
  159. return key;
  160. }
  161. -enum bootmenu_key bootmenu_conv_key(int ichar)
  162. +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
  163. + int *choice)
  164. {
  165. enum bootmenu_key key;
  166. @@ -515,6 +556,20 @@ enum bootmenu_key bootmenu_conv_key(int
  167. case ' ':
  168. key = BKEY_SPACE;
  169. break;
  170. + case '0' ... '9':
  171. + case 'a' ... 'z':
  172. + if (choice && menu) {
  173. + *choice = find_choice(ichar);
  174. + if ((*choice >= 0 && *choice < menu->count - 1)) {
  175. + key = BKEY_CHOICE;
  176. + break;
  177. + } else if (ichar == '0') {
  178. + *choice = menu->count - 1;
  179. + key = BKEY_CHOICE;
  180. + break;
  181. + }
  182. + }
  183. + fallthrough;
  184. default:
  185. key = BKEY_NONE;
  186. break;
  187. @@ -524,11 +579,16 @@ enum bootmenu_key bootmenu_conv_key(int
  188. }
  189. enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
  190. - struct cli_ch_state *cch)
  191. + struct cli_ch_state *cch, int *choice)
  192. {
  193. enum bootmenu_key key;
  194. int c;
  195. + if (menu->last_choiced) {
  196. + menu->last_choiced = false;
  197. + return BKEY_SELECT;
  198. + }
  199. +
  200. c = cli_ch_process(cch, 0);
  201. if (!c) {
  202. while (!c && !tstc()) {
  203. @@ -542,7 +602,7 @@ enum bootmenu_key bootmenu_loop(struct b
  204. }
  205. }
  206. - key = bootmenu_conv_key(c);
  207. + key = bootmenu_conv_key(menu, c, choice);
  208. return key;
  209. }
  210. --- a/include/menu.h
  211. +++ b/include/menu.h
  212. @@ -6,6 +6,8 @@
  213. #ifndef __MENU_H__
  214. #define __MENU_H__
  215. +#include <linux/ctype.h>
  216. +
  217. struct cli_ch_state;
  218. struct menu;
  219. @@ -19,6 +21,8 @@ int menu_get_choice(struct menu *m, void
  220. int menu_item_add(struct menu *m, char *item_key, void *item_data);
  221. int menu_destroy(struct menu *m);
  222. int menu_default_choice(struct menu *m, void **choice);
  223. +/* Add KEY_CHOICE support */
  224. +int get_choice_char(int index, char *result);
  225. /**
  226. * menu_show() Show a boot menu
  227. @@ -41,6 +45,7 @@ struct bootmenu_data {
  228. int active; /* active menu entry */
  229. int count; /* total count of menu entries */
  230. struct bootmenu_entry *first; /* first menu entry */
  231. + bool last_choiced;
  232. };
  233. /** enum bootmenu_key - keys that can be returned by the bootmenu */
  234. @@ -51,6 +56,7 @@ enum bootmenu_key {
  235. BKEY_SELECT,
  236. BKEY_QUIT,
  237. BKEY_SAVE,
  238. + BKEY_CHOICE,
  239. /* 'extra' keys, which are used by menus but not cedit */
  240. BKEY_PLUS,
  241. @@ -81,7 +87,7 @@ enum bootmenu_key {
  242. * anything else: KEY_NONE
  243. */
  244. enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu,
  245. - struct cli_ch_state *cch);
  246. + struct cli_ch_state *cch, int *choice);
  247. /**
  248. * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled
  249. @@ -107,7 +113,7 @@ enum bootmenu_key bootmenu_autoboot_loop
  250. * Space: BKEY_SPACE
  251. */
  252. enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu,
  253. - struct cli_ch_state *cch);
  254. + struct cli_ch_state *cch, int *choice);
  255. /**
  256. * bootmenu_conv_key() - Convert a U-Boot keypress into a menu key
  257. @@ -115,6 +121,7 @@ enum bootmenu_key bootmenu_loop(struct b
  258. * @ichar: Keypress to convert (ASCII, including control characters)
  259. * Returns: Menu key that corresponds to @ichar, or BKEY_NONE if none
  260. */
  261. -enum bootmenu_key bootmenu_conv_key(int ichar);
  262. +enum bootmenu_key bootmenu_conv_key(struct bootmenu_data *menu, int ichar,
  263. + int *choice);
  264. #endif /* __MENU_H__ */
  265. --- a/cmd/eficonfig.c
  266. +++ b/cmd/eficonfig.c
  267. @@ -239,7 +239,7 @@ char *eficonfig_choice_entry(void *data)
  268. cli_ch_init(cch);
  269. while (1) {
  270. - key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch);
  271. + key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch, NULL);
  272. switch (key) {
  273. case BKEY_UP:
  274. @@ -1838,7 +1838,7 @@ char *eficonfig_choice_change_boot_order
  275. cli_ch_init(cch);
  276. while (1) {
  277. - key = bootmenu_loop(NULL, cch);
  278. + key = bootmenu_loop(NULL, cch, NULL);
  279. switch (key) {
  280. case BKEY_PLUS:
  281. --- a/boot/bootflow_menu.c
  282. +++ b/boot/bootflow_menu.c
  283. @@ -235,7 +235,7 @@ int bootflow_menu_run(struct bootstd_pri
  284. key = 0;
  285. if (ichar) {
  286. - key = bootmenu_conv_key(ichar);
  287. + key = bootmenu_conv_key(NULL, ichar, NULL);
  288. if (key == BKEY_NONE)
  289. key = ichar;
  290. }