From 2da4a15e7e947d2d304ec1ba392556c3e0393e13 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:22 -0600 Subject: menu: Rename KEY_... to BKEY_... This enum values conflict with linux/input.h so rename them. Signed-off-by: Simon Glass --- cmd/bootmenu.c | 10 +++++----- cmd/eficonfig.c | 26 +++++++++++++------------- 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'cmd') diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index e5a10f5d5c4..1a14e8a1909 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -86,7 +86,7 @@ static char *bootmenu_choice_entry(void *data) { struct bootmenu_data *menu = data; struct bootmenu_entry *iter; - enum bootmenu_key key = KEY_NONE; + enum bootmenu_key key = BKEY_NONE; int esc = 0; int i; @@ -100,22 +100,22 @@ static char *bootmenu_choice_entry(void *data) } switch (key) { - case KEY_UP: + case BKEY_UP: if (menu->active > 0) --menu->active; /* no menu key selected, regenerate menu */ return NULL; - case KEY_DOWN: + case BKEY_DOWN: if (menu->active < menu->count - 1) ++menu->active; /* no menu key selected, regenerate menu */ return NULL; - case KEY_SELECT: + case BKEY_SELECT: iter = menu->first; for (i = 0; i < menu->active; ++i) iter = iter->next; return iter->key; - case KEY_QUIT: + case BKEY_QUIT: /* Quit by choosing the last entry - U-Boot console */ iter = menu->first; while (iter->next) diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index ce7175a5666..8f246bc271f 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -187,31 +187,31 @@ static char *eficonfig_choice_entry(void *data) int esc = 0; struct list_head *pos, *n; struct eficonfig_entry *entry; - enum bootmenu_key key = KEY_NONE; + enum bootmenu_key key = BKEY_NONE; struct efimenu *efi_menu = data; while (1) { bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc); switch (key) { - case KEY_UP: + case BKEY_UP: if (efi_menu->active > 0) --efi_menu->active; /* no menu key selected, regenerate menu */ return NULL; - case KEY_DOWN: + case BKEY_DOWN: if (efi_menu->active < efi_menu->count - 1) ++efi_menu->active; /* no menu key selected, regenerate menu */ return NULL; - case KEY_SELECT: + case BKEY_SELECT: list_for_each_safe(pos, n, &efi_menu->list) { entry = list_entry(pos, struct eficonfig_entry, list); if (entry->num == efi_menu->active) return entry->key; } break; - case KEY_QUIT: + case BKEY_QUIT: /* Quit by choosing the last entry */ entry = list_last_entry(&efi_menu->list, struct eficonfig_entry, list); return entry->key; @@ -1864,14 +1864,14 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) { int esc = 0; struct list_head *pos, *n; - enum bootmenu_key key = KEY_NONE; + enum bootmenu_key key = BKEY_NONE; struct eficonfig_entry *entry, *tmp; while (1) { bootmenu_loop(NULL, &key, &esc); switch (key) { - case KEY_PLUS: + case BKEY_PLUS: if (efi_menu->active > 0) { list_for_each_safe(pos, n, &efi_menu->list) { entry = list_entry(pos, struct eficonfig_entry, list); @@ -1885,11 +1885,11 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) list_add(&tmp->list, &entry->list); } fallthrough; - case KEY_UP: + case BKEY_UP: if (efi_menu->active > 0) --efi_menu->active; return EFI_NOT_READY; - case KEY_MINUS: + case BKEY_MINUS: if (efi_menu->active < efi_menu->count - 3) { list_for_each_safe(pos, n, &efi_menu->list) { entry = list_entry(pos, struct eficonfig_entry, list); @@ -1905,11 +1905,11 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) ++efi_menu->active; } return EFI_NOT_READY; - case KEY_DOWN: + case BKEY_DOWN: if (efi_menu->active < efi_menu->count - 1) ++efi_menu->active; return EFI_NOT_READY; - case KEY_SELECT: + case BKEY_SELECT: /* "Save" */ if (efi_menu->active == efi_menu->count - 2) return EFI_SUCCESS; @@ -1919,7 +1919,7 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) return EFI_ABORTED; break; - case KEY_SPACE: + case BKEY_SPACE: if (efi_menu->active < efi_menu->count - 2) { list_for_each_safe(pos, n, &efi_menu->list) { entry = list_entry(pos, struct eficonfig_entry, list); @@ -1932,7 +1932,7 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) } } break; - case KEY_QUIT: + case BKEY_QUIT: return EFI_ABORTED; default: /* Pressed key is not valid, no need to regenerate the menu */ -- cgit v1.3.1 From 5712976b26f7865f348aba51c9fa367c456e1795 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:23 -0600 Subject: menu: Update bootmenu_autoboot_loop() to return the code Use the return value to save having to pass around a pointer. This also resolves any ambiguity about what *key contains when the function is called. Signed-off-by: Simon Glass --- cmd/bootmenu.c | 2 +- common/menu.c | 16 +++++++++------- include/menu.h | 7 +++---- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'cmd') diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index 1a14e8a1909..086d04148ab 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -93,7 +93,7 @@ static char *bootmenu_choice_entry(void *data) while (1) { if (menu->delay >= 0) { /* Autoboot was not stopped */ - bootmenu_autoboot_loop(menu, &key, &esc); + key = bootmenu_autoboot_loop(menu, &esc); } else { /* Some key was pressed, so autoboot was stopped */ bootmenu_loop(menu, &key, &esc); diff --git a/common/menu.c b/common/menu.c index a245c5a9c6c..bafc8470d7d 100644 --- a/common/menu.c +++ b/common/menu.c @@ -425,9 +425,9 @@ int menu_destroy(struct menu *m) return 1; } -void bootmenu_autoboot_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) { + enum bootmenu_key key = BKEY_NONE; int i, c; while (menu->delay > 0) { @@ -446,16 +446,16 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, switch (c) { case '\e': *esc = 1; - *key = BKEY_NONE; + key = BKEY_NONE; break; case '\r': - *key = BKEY_SELECT; + key = BKEY_SELECT; break; case 0x3: /* ^C */ - *key = BKEY_QUIT; + key = BKEY_QUIT; break; default: - *key = BKEY_NONE; + key = BKEY_NONE; break; } @@ -471,7 +471,9 @@ void bootmenu_autoboot_loop(struct bootmenu_data *menu, printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); if (menu->delay == 0) - *key = BKEY_SELECT; + key = BKEY_SELECT; + + return key; } void bootmenu_loop(struct bootmenu_data *menu, diff --git a/include/menu.h b/include/menu.h index 29b457921e9..9f30a3c1acd 100644 --- a/include/menu.h +++ b/include/menu.h @@ -65,14 +65,13 @@ enum bootmenu_key { * indicating that the current option should be chosen. * * @menu: Menu being processed - * @key: Returns the code for the key the user pressed: + * @esc: Set to 1 if the escape key is pressed, otherwise not updated + * Returns: code for the key the user pressed: * enter: KEY_SELECT * Ctrl-C: KEY_QUIT * anything else: KEY_NONE - * @esc: Set to 1 if the escape key is pressed, otherwise not updated */ -void bootmenu_autoboot_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc); +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc); /** * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled -- cgit v1.3.1 From d0ca98dbd99c2534c9e96e4c226e52ab80f2248f Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:24 -0600 Subject: menu: Update bootmenu_loop() to return the code Use the return value to save having to pass around a pointer. This also resolves any ambiguity about what *key contains when the function is called. Signed-off-by: Simon Glass --- cmd/bootmenu.c | 2 +- cmd/eficonfig.c | 4 ++-- common/menu.c | 30 ++++++++++++++++-------------- include/menu.h | 11 +++++------ 4 files changed, 24 insertions(+), 23 deletions(-) (limited to 'cmd') diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index 086d04148ab..43553dbcc9b 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -96,7 +96,7 @@ static char *bootmenu_choice_entry(void *data) key = bootmenu_autoboot_loop(menu, &esc); } else { /* Some key was pressed, so autoboot was stopped */ - bootmenu_loop(menu, &key, &esc); + key = bootmenu_loop(menu, &esc); } switch (key) { diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index 8f246bc271f..96cb1a367f3 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -191,7 +191,7 @@ static char *eficonfig_choice_entry(void *data) struct efimenu *efi_menu = data; while (1) { - bootmenu_loop((struct bootmenu_data *)efi_menu, &key, &esc); + key = bootmenu_loop((struct bootmenu_data *)efi_menu, &esc); switch (key) { case BKEY_UP: @@ -1868,7 +1868,7 @@ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) struct eficonfig_entry *entry, *tmp; while (1) { - bootmenu_loop(NULL, &key, &esc); + key = bootmenu_loop(NULL, &esc); switch (key) { case BKEY_PLUS: diff --git a/common/menu.c b/common/menu.c index bafc8470d7d..6842f5409d6 100644 --- a/common/menu.c +++ b/common/menu.c @@ -476,9 +476,9 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) return key; } -void bootmenu_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc) +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc) { + enum bootmenu_key key = BKEY_NONE; int c; if (*esc == 1) { @@ -505,17 +505,17 @@ void bootmenu_loop(struct bootmenu_data *menu, /* First char of ANSI escape sequence '\e' */ if (c == '\e') { *esc = 1; - *key = BKEY_NONE; + key = BKEY_NONE; } break; case 1: /* Second char of ANSI '[' */ if (c == '[') { *esc = 2; - *key = BKEY_NONE; + key = BKEY_NONE; } else { /* Alone ESC key was pressed */ - *key = BKEY_QUIT; + key = BKEY_QUIT; *esc = (c == '\e') ? 1 : 0; } break; @@ -524,7 +524,7 @@ void bootmenu_loop(struct bootmenu_data *menu, /* Third char of ANSI (number '1') - optional */ if (*esc == 2 && c == '1') { *esc = 3; - *key = BKEY_NONE; + key = BKEY_NONE; break; } @@ -532,31 +532,33 @@ void bootmenu_loop(struct bootmenu_data *menu, /* ANSI 'A' - key up was pressed */ if (c == 'A') - *key = BKEY_UP; + key = BKEY_UP; /* ANSI 'B' - key down was pressed */ else if (c == 'B') - *key = BKEY_DOWN; + key = BKEY_DOWN; /* other key was pressed */ else - *key = BKEY_NONE; + key = BKEY_NONE; break; } /* enter key was pressed */ if (c == '\r') - *key = BKEY_SELECT; + key = BKEY_SELECT; /* ^C was pressed */ if (c == 0x3) - *key = BKEY_QUIT; + key = BKEY_QUIT; if (c == '+') - *key = BKEY_PLUS; + key = BKEY_PLUS; if (c == '-') - *key = BKEY_MINUS; + key = BKEY_MINUS; if (c == ' ') - *key = BKEY_SPACE; + key = BKEY_SPACE; + + return key; } diff --git a/include/menu.h b/include/menu.h index 9f30a3c1acd..8b9b36214f7 100644 --- a/include/menu.h +++ b/include/menu.h @@ -83,7 +83,10 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc); * character is recognised * * @menu: Menu being processed - * @key: Returns the code for the key the user pressed: + * @esc: On input, a non-zero value indicates that an escape sequence has + * resulted in that many characters so far. On exit this is updated to the + * new number of characters + * Returns: code for the key the user pressed: * enter: BKEY_SELECT * Ctrl-C: BKEY_QUIT * Up arrow: BKEY_UP @@ -92,11 +95,7 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc); * Plus: BKEY_PLUS * Minus: BKEY_MINUS * Space: BKEY_SPACE - * @esc: On input, a non-zero value indicates that an escape sequence has - * resulted in that many characters so far. On exit this is updated to the - * new number of characters */ -void bootmenu_loop(struct bootmenu_data *menu, - enum bootmenu_key *key, int *esc); +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc); #endif /* __MENU_H__ */ -- cgit v1.3.1 From 32bab0eae51b55898d1e2804e6614d9143840581 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:26 -0600 Subject: menu: Make use of CLI character processing Avoid duplicating some of the escape-sequence processing here and use the CLI function instead. Signed-off-by: Simon Glass --- cmd/bootmenu.c | 9 ++++-- cmd/eficonfig.c | 12 ++++--- common/cli_getch.c | 12 ++++--- common/menu.c | 92 +++++++++++++++--------------------------------------- include/cli.h | 4 ++- include/menu.h | 7 +++-- 6 files changed, 56 insertions(+), 80 deletions(-) (limited to 'cmd') diff --git a/cmd/bootmenu.c b/cmd/bootmenu.c index 43553dbcc9b..3236ca5d799 100644 --- a/cmd/bootmenu.c +++ b/cmd/bootmenu.c @@ -4,6 +4,7 @@ */ #include +#include #include #include #include @@ -84,19 +85,21 @@ static void bootmenu_print_entry(void *data) static char *bootmenu_choice_entry(void *data) { + struct cli_ch_state s_cch, *cch = &s_cch; struct bootmenu_data *menu = data; struct bootmenu_entry *iter; enum bootmenu_key key = BKEY_NONE; - int esc = 0; int i; + cli_ch_init(cch); + while (1) { if (menu->delay >= 0) { /* Autoboot was not stopped */ - key = bootmenu_autoboot_loop(menu, &esc); + key = bootmenu_autoboot_loop(menu, cch); } else { /* Some key was pressed, so autoboot was stopped */ - key = bootmenu_loop(menu, &esc); + key = bootmenu_loop(menu, cch); } switch (key) { diff --git a/cmd/eficonfig.c b/cmd/eficonfig.c index 96cb1a367f3..d830e4af53b 100644 --- a/cmd/eficonfig.c +++ b/cmd/eficonfig.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -184,14 +185,16 @@ static void eficonfig_display_statusline(struct menu *m) */ static char *eficonfig_choice_entry(void *data) { - int esc = 0; + struct cli_ch_state s_cch, *cch = &s_cch; struct list_head *pos, *n; struct eficonfig_entry *entry; enum bootmenu_key key = BKEY_NONE; struct efimenu *efi_menu = data; + cli_ch_init(cch); + while (1) { - key = bootmenu_loop((struct bootmenu_data *)efi_menu, &esc); + key = bootmenu_loop((struct bootmenu_data *)efi_menu, cch); switch (key) { case BKEY_UP: @@ -1862,13 +1865,14 @@ static void eficonfig_display_change_boot_order(struct efimenu *efi_menu) */ static efi_status_t eficonfig_choice_change_boot_order(struct efimenu *efi_menu) { - int esc = 0; + struct cli_ch_state s_cch, *cch = &s_cch; struct list_head *pos, *n; enum bootmenu_key key = BKEY_NONE; struct eficonfig_entry *entry, *tmp; + cli_ch_init(cch); while (1) { - key = bootmenu_loop(NULL, &esc); + key = bootmenu_loop(NULL, cch); switch (key) { case BKEY_PLUS: diff --git a/common/cli_getch.c b/common/cli_getch.c index 9eeea7fef29..87c23edcf4b 100644 --- a/common/cli_getch.c +++ b/common/cli_getch.c @@ -140,10 +140,11 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar) * sequence */ if (!ichar) { - if (cch->emit_upto) { + if (cch->emitting) { if (cch->emit_upto < cch->esc_len) return cch->esc_save[cch->emit_upto++]; cch->emit_upto = 0; + cch->emitting = false; } return 0; } else if (ichar == -ETIMEDOUT) { @@ -174,18 +175,21 @@ int cli_ch_process(struct cli_ch_state *cch, int ichar) case ESC_SAVE: /* save this character and return nothing */ cch->esc_save[cch->esc_len++] = ichar; - return 0; + ichar = 0; + break; case ESC_REJECT: /* * invalid escape sequence, start returning the * characters in it */ cch->esc_save[cch->esc_len++] = ichar; - return cch->esc_save[cch->emit_upto++]; + ichar = cch->esc_save[cch->emit_upto++]; + cch->emitting = true; + break; case ESC_CONVERTED: /* valid escape sequence, return the resulting char */ cch->esc_len = 0; - return ichar; + break; } } diff --git a/common/menu.c b/common/menu.c index 7db98942a61..45f36ae3ede 100644 --- a/common/menu.c +++ b/common/menu.c @@ -15,6 +15,8 @@ #include "menu.h" +#define ansi 0 + /* * Internally, each item in a menu is represented by a struct menu_item. * @@ -425,15 +427,19 @@ int menu_destroy(struct menu *m) return 1; } -enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch) { enum bootmenu_key key = BKEY_NONE; int i, c; while (menu->delay > 0) { - printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); + if (ansi) + printf(ANSI_CURSOR_POSITION, menu->count + 5, 3); printf("Hit any key to stop autoboot: %d ", menu->delay); for (i = 0; i < 100; ++i) { + int ichar; + if (!tstc()) { schedule(); mdelay(10); @@ -443,12 +449,13 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) menu->delay = -1; c = getchar(); - switch (c) { - case '\e': - *esc = 1; + ichar = cli_ch_process(cch, c); + + switch (ichar) { + case '\0': key = BKEY_NONE; break; - case '\r': + case '\n': key = BKEY_SELECT; break; case 0x3: /* ^C */ @@ -458,7 +465,6 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) key = BKEY_NONE; break; } - break; } @@ -468,7 +474,8 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) --menu->delay; } - printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); + if (ansi) + printf(ANSI_CURSOR_POSITION ANSI_CLEAR_LINE, menu->count + 5, 1); if (menu->delay == 0) key = BKEY_SELECT; @@ -476,79 +483,32 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc) return key; } -enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc) +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch) { enum bootmenu_key key = BKEY_NONE; int c; - if (*esc == 1) { - if (tstc()) { - c = getchar(); - } else { + c = cli_ch_process(cch, 0); + if (!c) { + while (!c && !tstc()) { schedule(); mdelay(10); - if (tstc()) - c = getchar(); - else - c = '\e'; + c = cli_ch_process(cch, -ETIMEDOUT); } - } else { - while (!tstc()) { - schedule(); - mdelay(10); - } - c = getchar(); - } - - switch (*esc) { - case 0: - /* First char of ANSI escape sequence '\e' */ - if (c == '\e') { - *esc = 1; - key = BKEY_NONE; - } - break; - case 1: - /* Second char of ANSI '[' */ - if (c == '[') { - *esc = 2; - key = BKEY_NONE; - } else { - /* Alone ESC key was pressed */ - key = BKEY_QUIT; - *esc = (c == '\e') ? 1 : 0; - } - break; - case 2: - case 3: - /* Third char of ANSI (number '1') - optional */ - if (*esc == 2 && c == '1') { - *esc = 3; - key = BKEY_NONE; - break; + if (!c) { + c = getchar(); + c = cli_ch_process(cch, c); } - - *esc = 0; - - /* ANSI 'A' - key up was pressed */ - if (c == 'A') - key = BKEY_UP; - /* ANSI 'B' - key down was pressed */ - else if (c == 'B') - key = BKEY_DOWN; - /* other key was pressed */ - else - key = BKEY_NONE; - - break; } switch (c) { - case '\r': + case '\n': /* enter key was pressed */ key = BKEY_SELECT; break; case CTL_CH('c'): + case '\e': /* ^C was pressed */ key = BKEY_QUIT; break; diff --git a/include/cli.h b/include/cli.h index 863519e4b13..c777c90313f 100644 --- a/include/cli.h +++ b/include/cli.h @@ -14,12 +14,14 @@ * * @esc_len: Number of escape characters read so far * @esc_save: Escape characters collected so far - * @emit_upto: Next character to emit from esc_save (0 if not emitting) + * @emit_upto: Next index to emit from esc_save + * @emitting: true if emitting from esc_save */ struct cli_ch_state { int esc_len; char esc_save[8]; int emit_upto; + bool emitting; }; /** diff --git a/include/menu.h b/include/menu.h index 8b9b36214f7..3996075a337 100644 --- a/include/menu.h +++ b/include/menu.h @@ -6,6 +6,7 @@ #ifndef __MENU_H__ #define __MENU_H__ +struct cli_ch_state; struct menu; struct menu *menu_create(char *title, int timeout, int prompt, @@ -71,7 +72,8 @@ enum bootmenu_key { * Ctrl-C: KEY_QUIT * anything else: KEY_NONE */ -enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc); +enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch); /** * bootmenu_loop() - handle waiting for a keypress when autoboot is disabled @@ -96,6 +98,7 @@ enum bootmenu_key bootmenu_autoboot_loop(struct bootmenu_data *menu, int *esc); * Minus: BKEY_MINUS * Space: BKEY_SPACE */ -enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, int *esc); +enum bootmenu_key bootmenu_loop(struct bootmenu_data *menu, + struct cli_ch_state *cch); #endif /* __MENU_H__ */ -- cgit v1.3.1 From 858fefd5fc3ae9006a0f545d7744e6f95270b14d Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:27 -0600 Subject: image: Add a function to find a script in an image Split this functionality out of the 'source' command so it can be used from another place. For now leave it where it is, but a future patch will move it out of cmd/ Signed-off-by: Simon Glass --- cmd/source.c | 217 ++++++++++++++++++++++++++++++-------------------------- include/image.h | 14 ++++ 2 files changed, 130 insertions(+), 101 deletions(-) (limited to 'cmd') diff --git a/cmd/source.c b/cmd/source.c index 94da5d8d6ad..1186a541bc7 100644 --- a/cmd/source.c +++ b/cmd/source.c @@ -24,7 +24,6 @@ #include #include -#if defined(CONFIG_FIT) /** * get_default_image() - Return default property from /images * @@ -40,149 +39,165 @@ static const char *get_default_image(const void *fit) return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); } -#endif -int image_source_script(ulong addr, const char *fit_uname, const char *confname) +int image_locate_script(void *buf, int size, const char *fit_uname, + const char *confname, char **datap, uint *lenp) { ulong len; -#if defined(CONFIG_LEGACY_IMAGE_FORMAT) const struct legacy_img_hdr *hdr; -#endif u32 *data; int verify; - void *buf; -#if defined(CONFIG_FIT) const void* fit_hdr; int noffset; const void *fit_data; size_t fit_len; -#endif verify = env_get_yesno("verify"); - buf = map_sysmem(addr, 0); switch (genimg_get_format(buf)) { -#if defined(CONFIG_LEGACY_IMAGE_FORMAT) case IMAGE_FORMAT_LEGACY: - hdr = buf; + if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { + hdr = buf; - if (!image_check_magic (hdr)) { - puts ("Bad magic number\n"); - return 1; - } + if (!image_check_magic(hdr)) { + puts("Bad magic number\n"); + return 1; + } - if (!image_check_hcrc (hdr)) { - puts ("Bad header crc\n"); - return 1; - } + if (!image_check_hcrc(hdr)) { + puts("Bad header crc\n"); + return 1; + } + + if (verify) { + if (!image_check_dcrc(hdr)) { + puts("Bad data crc\n"); + return 1; + } + } - if (verify) { - if (!image_check_dcrc (hdr)) { - puts ("Bad data crc\n"); + if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { + puts("Bad image type\n"); return 1; } - } - if (!image_check_type (hdr, IH_TYPE_SCRIPT)) { - puts ("Bad image type\n"); - return 1; - } + /* get length of script */ + data = (u32 *)image_get_data(hdr); - /* get length of script */ - data = (u32 *)image_get_data (hdr); + len = uimage_to_cpu(*data); + if (!len) { + puts("Empty Script\n"); + return 1; + } - if ((len = uimage_to_cpu (*data)) == 0) { - puts ("Empty Script\n"); - return 1; + /* + * scripts are just multi-image files with one + * component, so seek past the zero-terminated sequence + * of image lengths to get to the actual image data + */ + while (*data++); } - - /* - * scripts are just multi-image files with one component, seek - * past the zero-terminated sequence of image lengths to get - * to the actual image data - */ - while (*data++); break; -#endif -#if defined(CONFIG_FIT) case IMAGE_FORMAT_FIT: - fit_hdr = buf; - if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { - puts ("Bad FIT image format\n"); - return 1; - } - - if (!fit_uname) { - /* If confname is empty, use the default */ - if (confname && *confname) - noffset = fit_conf_get_node(fit_hdr, confname); - else - noffset = fit_conf_get_node(fit_hdr, NULL); - if (noffset < 0) { - if (!confname) - goto fallback; - printf("Could not find config %s\n", confname); + if (IS_ENABLED(CONFIG_FIT)) { + fit_hdr = buf; + if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { + puts("Bad FIT image format\n"); return 1; } - if (verify && fit_config_verify(fit_hdr, noffset)) - return 1; + if (!fit_uname) { + /* If confname is empty, use the default */ + if (confname && *confname) + noffset = fit_conf_get_node(fit_hdr, confname); + else + noffset = fit_conf_get_node(fit_hdr, NULL); + if (noffset < 0) { + if (!confname) + goto fallback; + printf("Could not find config %s\n", confname); + return 1; + } + + if (verify && fit_config_verify(fit_hdr, noffset)) + return 1; + + noffset = fit_conf_get_prop_node(fit_hdr, + noffset, + FIT_SCRIPT_PROP, + IH_PHASE_NONE); + if (noffset < 0) { + if (!confname) + goto fallback; + printf("Could not find script in %s\n", confname); + return 1; + } + } else { +fallback: + if (!fit_uname || !*fit_uname) + fit_uname = get_default_image(fit_hdr); + if (!fit_uname) { + puts("No FIT subimage unit name\n"); + return 1; + } + + /* get script component image node offset */ + noffset = fit_image_get_node(fit_hdr, fit_uname); + if (noffset < 0) { + printf("Can't find '%s' FIT subimage\n", + fit_uname); + return 1; + } + } - noffset = fit_conf_get_prop_node(fit_hdr, noffset, - FIT_SCRIPT_PROP, - IH_PHASE_NONE); - if (noffset < 0) { - if (!confname) - goto fallback; - printf("Could not find script in %s\n", confname); + if (!fit_image_check_type(fit_hdr, noffset, + IH_TYPE_SCRIPT)) { + puts("Not a image image\n"); return 1; } - } else { -fallback: - if (!fit_uname || !*fit_uname) - fit_uname = get_default_image(fit_hdr); - if (!fit_uname) { - puts("No FIT subimage unit name\n"); + + /* verify integrity */ + if (verify && !fit_image_verify(fit_hdr, noffset)) { + puts("Bad Data Hash\n"); return 1; } - /* get script component image node offset */ - noffset = fit_image_get_node(fit_hdr, fit_uname); - if (noffset < 0) { - printf("Can't find '%s' FIT subimage\n", - fit_uname); + /* get script subimage data address and length */ + if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) { + puts("Could not find script subimage data\n"); return 1; } - } - - if (!fit_image_check_type (fit_hdr, noffset, IH_TYPE_SCRIPT)) { - puts("Not a script image\n"); - return 1; - } - - /* verify integrity */ - if (verify && !fit_image_verify(fit_hdr, noffset)) { - puts("Bad Data Hash\n"); - return 1; - } - /* get script subimage data address and length */ - if (fit_image_get_data (fit_hdr, noffset, &fit_data, &fit_len)) { - puts ("Could not find script subimage data\n"); - return 1; + data = (u32 *)fit_data; + len = (ulong)fit_len; } - - data = (u32 *)fit_data; - len = (ulong)fit_len; break; -#endif default: - puts ("Wrong image format for \"source\" command\n"); - return 1; + puts("Wrong image format for \"source\" command\n"); + return -EPERM; } - debug("** Script length: %ld\n", len); - return run_command_list((char *)data, len, 0); + *datap = (char *)data; + *lenp = len; + + return 0; +} + +int image_source_script(ulong addr, const char *fit_uname, const char *confname) +{ + char *data; + void *buf; + uint len; + int ret; + + buf = map_sysmem(addr, 0); + ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len); + unmap_sysmem(buf); + if (ret) + return CMD_RET_FAILURE; + + debug("** Script length: %d\n", len); + return run_command_list(data, len, 0); } /**************************************************/ diff --git a/include/image.h b/include/image.h index bed75ce1b38..f7ebe9cbe8c 100644 --- a/include/image.h +++ b/include/image.h @@ -729,6 +729,20 @@ int fit_image_load(struct bootm_headers *images, ulong addr, int image_source_script(ulong addr, const char *fit_uname, const char *confname); +/** + * image_locate_script() - Locate the raw script in an image + * + * @buf: Address of image + * @size: Size of image in bytes + * @fit_uname: Node name of FIT image to read + * @confname: Node name of FIT config to read + * @datap: Returns pointer to raw script on success + * @lenp: Returns size of raw script on success + * @return 0 if OK, non-zero on error + */ +int image_locate_script(void *buf, int size, const char *fit_uname, + const char *confname, char **datap, uint *lenp); + /** * fit_get_node_from_config() - Look up an image a FIT by type * -- cgit v1.3.1 From 30f3333d8860fd97e131e24ad33a80f4d46e98b1 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:28 -0600 Subject: image: Move common image code to image_board and command We should use the cmd/ directory for commands rather than for common code used elsewhere in U-Boot. Move the common 'source' code into image-board.c to achieve this. The image_source_script() function needs to call run_command_list() so seems to belong better in the command library. Move and rename it. Signed-off-by: Simon Glass --- .../arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c | 2 +- boot/bootmeth_script.c | 2 +- boot/image-board.c | 159 ++++++++++++++++++ cmd/source.c | 181 +-------------------- common/command.c | 19 +++ drivers/usb/gadget/f_sdp.c | 2 +- include/command.h | 12 ++ include/image.h | 20 --- 8 files changed, 194 insertions(+), 203 deletions(-) (limited to 'cmd') diff --git a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c index cb13a14d829..a8372356b0e 100644 --- a/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c +++ b/arch/arm/mach-stm32mp/cmd_stm32prog/cmd_stm32prog.c @@ -154,7 +154,7 @@ static int do_stm32prog(struct cmd_tbl *cmdtp, int flag, int argc, do_bootz(cmdtp, 0, 4, bootm_argv); } if (data->script) - image_source_script(data->script, NULL, NULL); + cmd_source_script(data->script, NULL, NULL); if (reset) { puts("Reset...\n"); diff --git a/boot/bootmeth_script.c b/boot/bootmeth_script.c index 6c84721d1cd..5799c89a466 100644 --- a/boot/bootmeth_script.c +++ b/boot/bootmeth_script.c @@ -101,7 +101,7 @@ static int script_boot(struct udevice *dev, struct bootflow *bflow) log_debug("mmc_bootdev: %s\n", env_get("mmc_bootdev")); addr = map_to_sysmem(bflow->buf); - ret = image_source_script(addr, NULL, NULL); + ret = cmd_source_script(addr, NULL, NULL); if (ret) return log_msg_ret("boot", ret); diff --git a/boot/image-board.c b/boot/image-board.c index 0fd63291d3f..e5d71a3d541 100644 --- a/boot/image-board.c +++ b/boot/image-board.c @@ -971,3 +971,162 @@ void genimg_print_time(time_t timestamp) tm.tm_year, tm.tm_mon, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec); } + +/** + * get_default_image() - Return default property from /images + * + * Return: Pointer to value of default property (or NULL) + */ +static const char *get_default_image(const void *fit) +{ + int images_noffset; + + images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); + if (images_noffset < 0) + return NULL; + + return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); +} + +int image_locate_script(void *buf, int size, const char *fit_uname, + const char *confname, char **datap, uint *lenp) +{ + const struct legacy_img_hdr *hdr; + const void *fit_data; + const void *fit_hdr; + size_t fit_len; + int noffset; + int verify; + ulong len; + u32 *data; + + verify = env_get_yesno("verify"); + + switch (genimg_get_format(buf)) { + case IMAGE_FORMAT_LEGACY: + if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { + hdr = buf; + + if (!image_check_magic(hdr)) { + puts("Bad magic number\n"); + return 1; + } + + if (!image_check_hcrc(hdr)) { + puts("Bad header crc\n"); + return 1; + } + + if (verify) { + if (!image_check_dcrc(hdr)) { + puts("Bad data crc\n"); + return 1; + } + } + + if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { + puts("Bad image type\n"); + return 1; + } + + /* get length of script */ + data = (u32 *)image_get_data(hdr); + + len = uimage_to_cpu(*data); + if (!len) { + puts("Empty Script\n"); + return 1; + } + + /* + * scripts are just multi-image files with one + * component, so seek past the zero-terminated sequence + * of image lengths to get to the actual image data + */ + while (*data++); + } + break; + case IMAGE_FORMAT_FIT: + if (IS_ENABLED(CONFIG_FIT)) { + fit_hdr = buf; + if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { + puts("Bad FIT image format\n"); + return 1; + } + + if (!fit_uname) { + /* If confname is empty, use the default */ + if (confname && *confname) + noffset = fit_conf_get_node(fit_hdr, confname); + else + noffset = fit_conf_get_node(fit_hdr, NULL); + if (noffset < 0) { + if (!confname) + goto fallback; + printf("Could not find config %s\n", confname); + return 1; + } + + if (verify && fit_config_verify(fit_hdr, noffset)) + return 1; + + noffset = fit_conf_get_prop_node(fit_hdr, + noffset, + FIT_SCRIPT_PROP, + IH_PHASE_NONE); + if (noffset < 0) { + if (!confname) + goto fallback; + printf("Could not find script in %s\n", confname); + return 1; + } + } else { +fallback: + if (!fit_uname || !*fit_uname) + fit_uname = get_default_image(fit_hdr); + if (!fit_uname) { + puts("No FIT subimage unit name\n"); + return 1; + } + + /* get script component image node offset */ + noffset = fit_image_get_node(fit_hdr, fit_uname); + if (noffset < 0) { + printf("Can't find '%s' FIT subimage\n", + fit_uname); + return 1; + } + } + + if (!fit_image_check_type(fit_hdr, noffset, + IH_TYPE_SCRIPT)) { + puts("Not a image image\n"); + return 1; + } + + /* verify integrity */ + if (verify && !fit_image_verify(fit_hdr, noffset)) { + puts("Bad Data Hash\n"); + return 1; + } + + /* get script subimage data address and length */ + if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) { + puts("Could not find script subimage data\n"); + return 1; + } + + data = (u32 *)fit_data; + len = (ulong)fit_len; + } + break; + default: + puts("Wrong image format for \"source\" command\n"); + return -EPERM; + } + + *datap = (char *)data; + *lenp = len; + + return 0; +} diff --git a/cmd/source.c b/cmd/source.c index 1186a541bc7..92c7835bf50 100644 --- a/cmd/source.c +++ b/cmd/source.c @@ -24,184 +24,6 @@ #include #include -/** - * get_default_image() - Return default property from /images - * - * Return: Pointer to value of default property (or NULL) - */ -static const char *get_default_image(const void *fit) -{ - int images_noffset; - - images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH); - if (images_noffset < 0) - return NULL; - - return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL); -} - -int image_locate_script(void *buf, int size, const char *fit_uname, - const char *confname, char **datap, uint *lenp) -{ - ulong len; - const struct legacy_img_hdr *hdr; - u32 *data; - int verify; - const void* fit_hdr; - int noffset; - const void *fit_data; - size_t fit_len; - - verify = env_get_yesno("verify"); - - switch (genimg_get_format(buf)) { - case IMAGE_FORMAT_LEGACY: - if (IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) { - hdr = buf; - - if (!image_check_magic(hdr)) { - puts("Bad magic number\n"); - return 1; - } - - if (!image_check_hcrc(hdr)) { - puts("Bad header crc\n"); - return 1; - } - - if (verify) { - if (!image_check_dcrc(hdr)) { - puts("Bad data crc\n"); - return 1; - } - } - - if (!image_check_type(hdr, IH_TYPE_SCRIPT)) { - puts("Bad image type\n"); - return 1; - } - - /* get length of script */ - data = (u32 *)image_get_data(hdr); - - len = uimage_to_cpu(*data); - if (!len) { - puts("Empty Script\n"); - return 1; - } - - /* - * scripts are just multi-image files with one - * component, so seek past the zero-terminated sequence - * of image lengths to get to the actual image data - */ - while (*data++); - } - break; - case IMAGE_FORMAT_FIT: - if (IS_ENABLED(CONFIG_FIT)) { - fit_hdr = buf; - if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) { - puts("Bad FIT image format\n"); - return 1; - } - - if (!fit_uname) { - /* If confname is empty, use the default */ - if (confname && *confname) - noffset = fit_conf_get_node(fit_hdr, confname); - else - noffset = fit_conf_get_node(fit_hdr, NULL); - if (noffset < 0) { - if (!confname) - goto fallback; - printf("Could not find config %s\n", confname); - return 1; - } - - if (verify && fit_config_verify(fit_hdr, noffset)) - return 1; - - noffset = fit_conf_get_prop_node(fit_hdr, - noffset, - FIT_SCRIPT_PROP, - IH_PHASE_NONE); - if (noffset < 0) { - if (!confname) - goto fallback; - printf("Could not find script in %s\n", confname); - return 1; - } - } else { -fallback: - if (!fit_uname || !*fit_uname) - fit_uname = get_default_image(fit_hdr); - if (!fit_uname) { - puts("No FIT subimage unit name\n"); - return 1; - } - - /* get script component image node offset */ - noffset = fit_image_get_node(fit_hdr, fit_uname); - if (noffset < 0) { - printf("Can't find '%s' FIT subimage\n", - fit_uname); - return 1; - } - } - - if (!fit_image_check_type(fit_hdr, noffset, - IH_TYPE_SCRIPT)) { - puts("Not a image image\n"); - return 1; - } - - /* verify integrity */ - if (verify && !fit_image_verify(fit_hdr, noffset)) { - puts("Bad Data Hash\n"); - return 1; - } - - /* get script subimage data address and length */ - if (fit_image_get_data(fit_hdr, noffset, &fit_data, &fit_len)) { - puts("Could not find script subimage data\n"); - return 1; - } - - data = (u32 *)fit_data; - len = (ulong)fit_len; - } - break; - default: - puts("Wrong image format for \"source\" command\n"); - return -EPERM; - } - - *datap = (char *)data; - *lenp = len; - - return 0; -} - -int image_source_script(ulong addr, const char *fit_uname, const char *confname) -{ - char *data; - void *buf; - uint len; - int ret; - - buf = map_sysmem(addr, 0); - ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len); - unmap_sysmem(buf); - if (ret) - return CMD_RET_FAILURE; - - debug("** Script length: %d\n", len); - return run_command_list(data, len, 0); -} - -/**************************************************/ -#if defined(CONFIG_CMD_SOURCE) static int do_source(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { @@ -228,7 +50,7 @@ static int do_source(struct cmd_tbl *cmdtp, int flag, int argc, } printf ("## Executing script at %08lx\n", addr); - rcode = image_source_script(addr, fit_uname, confname); + rcode = cmd_source_script(addr, fit_uname, confname); return rcode; } @@ -250,4 +72,3 @@ U_BOOT_CMD( source, 2, 0, do_source, "run script from memory", source_help_text ); -#endif diff --git a/common/command.c b/common/command.c index 41c91c6d8c8..7a86bd76a4c 100644 --- a/common/command.c +++ b/common/command.c @@ -13,7 +13,9 @@ #include #include #include +#include #include +#include #include #include @@ -654,3 +656,20 @@ int cmd_process_error(struct cmd_tbl *cmdtp, int err) return CMD_RET_SUCCESS; } + +int cmd_source_script(ulong addr, const char *fit_uname, const char *confname) +{ + char *data; + void *buf; + uint len; + int ret; + + buf = map_sysmem(addr, 0); + ret = image_locate_script(buf, 0, fit_uname, confname, &data, &len); + unmap_sysmem(buf); + if (ret) + return CMD_RET_FAILURE; + + debug("** Script length: %d\n", len); + return run_command_list(data, len, 0); +} diff --git a/drivers/usb/gadget/f_sdp.c b/drivers/usb/gadget/f_sdp.c index 5ae5b62741a..9ea43f29cfb 100644 --- a/drivers/usb/gadget/f_sdp.c +++ b/drivers/usb/gadget/f_sdp.c @@ -868,7 +868,7 @@ static int sdp_handle_in_ep(struct spl_image_info *spl_image, jump_to_image_no_args(&spl_image); #else /* In U-Boot, allow jumps to scripts */ - image_source_script(sdp_func->jmp_address, NULL, NULL); + cmd_source_script(sdp_func->jmp_address, NULL, NULL); #endif } diff --git a/include/command.h b/include/command.h index 966fd23c639..3c6132e0c54 100644 --- a/include/command.h +++ b/include/command.h @@ -279,6 +279,18 @@ int run_commandf(const char *fmt, ...); * Return: 0 on success, or != 0 on error. */ int run_command_list(const char *cmd, int len, int flag); + +/** + * cmd_source_script() - Execute a script + * + * Executes a U-Boot script at a particular address in memory. The script should + * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT). + * + * @addr: Address of script + * @fit_uname: FIT subimage name + * Return: result code (enum command_ret_t) + */ +int cmd_source_script(ulong addr, const char *fit_uname, const char *confname); #endif /* __ASSEMBLY__ */ /* diff --git a/include/image.h b/include/image.h index f7ebe9cbe8c..7717a4c13d3 100644 --- a/include/image.h +++ b/include/image.h @@ -709,26 +709,6 @@ int fit_image_load(struct bootm_headers *images, ulong addr, int arch, int image_ph_type, int bootstage_id, enum fit_load_op load_op, ulong *datap, ulong *lenp); -/** - * image_source_script() - Execute a script - * @addr: Address of script - * @fit_uname: FIT subimage name - * @confname: FIT config name. The subimage is chosen based on FIT_SCRIPT_PROP. - * - * Executes a U-Boot script at a particular address in memory. The script should - * have a header (FIT or legacy) with the script type (IH_TYPE_SCRIPT). - * - * If @fit_uname is the empty string, then the default image is used. If - * @confname is the empty string, the default config is used. If @confname and - * @fit_uname are both non-%NULL, then @confname is ignored. If @confname and - * @fit_uname are both %NULL, then first the default config is tried, and then - * the default image. - * - * Return: result code (enum command_ret_t) - */ -int image_source_script(ulong addr, const char *fit_uname, - const char *confname); - /** * image_locate_script() - Locate the raw script in an image * -- cgit v1.3.1 From 0e38bd848d41bcfc7a113603ee37805016a09a42 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:32 -0600 Subject: video: Add font functions to the vidconsole API Support for fonts currently depends on the type of vidconsole in use. Add two new methods to enumerate fonts and to set the font. Fix a few other method comments while we are here. Signed-off-by: Simon Glass --- cmd/font.c | 11 ++++-- drivers/video/console_truetype.c | 27 +++++++++----- drivers/video/vidconsole-uclass.c | 33 +++++++++++++++++ include/video_console.h | 75 ++++++++++++++++++++++++++++++++------- test/cmd/font.c | 6 ++-- 5 files changed, 127 insertions(+), 25 deletions(-) (limited to 'cmd') diff --git a/cmd/font.c b/cmd/font.c index 3e522f3aaa1..7b4347f32b5 100644 --- a/cmd/font.c +++ b/cmd/font.c @@ -15,7 +15,11 @@ static int do_font_list(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { - vidconsole_list_fonts(); + struct udevice *dev; + + if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev)) + return CMD_RET_FAILURE; + vidconsole_list_fonts(dev); return 0; } @@ -47,6 +51,7 @@ static int do_font_select(struct cmd_tbl *cmdtp, int flag, int argc, static int do_font_size(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) { + const char *font_name; struct udevice *dev; uint size; int ret; @@ -56,9 +61,11 @@ static int do_font_size(struct cmd_tbl *cmdtp, int flag, int argc, if (uclass_first_device_err(UCLASS_VIDEO_CONSOLE, &dev)) return CMD_RET_FAILURE; + font_name = vidconsole_get_font_size(dev, &size); size = dectoul(argv[1], NULL); - ret = vidconsole_select_font(dev, NULL, size); + + ret = vidconsole_select_font(dev, font_name, size); if (ret) { printf("Failed (error %d)\n", ret); return CMD_RET_FAILURE; diff --git a/drivers/video/console_truetype.c b/drivers/video/console_truetype.c index 4abc0bc2ff1..9cac9a6de4d 100644 --- a/drivers/video/console_truetype.c +++ b/drivers/video/console_truetype.c @@ -584,14 +584,20 @@ static struct font_info *console_truetype_find_font(void) return NULL; } -void vidconsole_list_fonts(void) +int console_truetype_get_font(struct udevice *dev, int seq, + struct vidfont_info *info) { struct font_info *tab; + int i; - for (tab = font_table; tab->begin; tab++) { - if (abs(tab->begin - tab->end) > 4) - printf("%s\n", tab->name); + for (i = 0, tab = font_table; tab->begin; tab++, i++) { + if (i == seq && font_valid(tab)) { + info->name = tab->name; + return 0; + } } + + return -ENOENT; } /** @@ -674,7 +680,8 @@ static void select_metrics(struct udevice *dev, struct console_tt_metrics *met) vc_priv->tab_width_frac = VID_TO_POS(met->font_size) * 8 / 2; } -int vidconsole_select_font(struct udevice *dev, const char *name, uint size) +static int truetype_select_font(struct udevice *dev, const char *name, + uint size) { struct console_tt_priv *priv = dev_get_priv(dev); struct console_tt_metrics *met; @@ -684,7 +691,7 @@ int vidconsole_select_font(struct udevice *dev, const char *name, uint size) if (!size) size = CONFIG_CONSOLE_TRUETYPE_SIZE; if (!name) - name = priv->cur_met->font_name; + name = font_table->name; met = find_metrics(dev, name, size); if (!met) { @@ -694,7 +701,9 @@ int vidconsole_select_font(struct udevice *dev, const char *name, uint size) int ret; ret = truetype_add_metrics(dev, - tab->name, size, tab->begin); + tab->name, + size, + tab->begin); if (ret < 0) return log_msg_ret("add", ret); @@ -715,7 +724,7 @@ int vidconsole_select_font(struct udevice *dev, const char *name, uint size) return 0; } -const char *vidconsole_get_font(struct udevice *dev, uint *sizep) +const char *vidconsole_get_font_size(struct udevice *dev, uint *sizep) { struct console_tt_priv *priv = dev_get_priv(dev); struct console_tt_metrics *met = priv->cur_met; @@ -763,6 +772,8 @@ struct vidconsole_ops console_truetype_ops = { .set_row = console_truetype_set_row, .backspace = console_truetype_backspace, .entry_start = console_truetype_entry_start, + .get_font = console_truetype_get_font, + .select_font = truetype_select_font, }; U_BOOT_DRIVER(vidconsole_truetype) = { diff --git a/drivers/video/vidconsole-uclass.c b/drivers/video/vidconsole-uclass.c index 6bdfb6e37dd..aadd54bfd4f 100644 --- a/drivers/video/vidconsole-uclass.c +++ b/drivers/video/vidconsole-uclass.c @@ -557,6 +557,39 @@ static void vidconsole_puts(struct stdio_dev *sdev, const char *s) } } +void vidconsole_list_fonts(struct udevice *dev) +{ + struct vidfont_info info; + int ret, i; + + for (i = 0, ret = 0; !ret; i++) { + ret = vidconsole_get_font(dev, i, &info); + if (!ret) + printf("%s\n", info.name); + } +} + +int vidconsole_get_font(struct udevice *dev, int seq, + struct vidfont_info *info) +{ + struct vidconsole_ops *ops = vidconsole_get_ops(dev); + + if (!ops->get_font) + return -ENOSYS; + + return ops->get_font(dev, seq, info); +} + +int vidconsole_select_font(struct udevice *dev, const char *name, uint size) +{ + struct vidconsole_ops *ops = vidconsole_get_ops(dev); + + if (!ops->select_font) + return -ENOSYS; + + return ops->select_font(dev, name, size); +} + /* Set up the number of rows and colours (rotated drivers override this) */ static int vidconsole_pre_probe(struct udevice *dev) { diff --git a/include/video_console.h b/include/video_console.h index d755eb73cf2..9d2c0f210e4 100644 --- a/include/video_console.h +++ b/include/video_console.h @@ -62,6 +62,15 @@ struct vidconsole_priv { char escape_buf[32]; }; +/** + * struct vidfont_info - information about a font + * + * @name: Font name, e.g. nimbus_sans_l_regular + */ +struct vidfont_info { + const char *name; +}; + /** * struct vidconsole_ops - Video console operations * @@ -111,6 +120,9 @@ struct vidconsole_ops { /** * entry_start() - Indicate that text entry is starting afresh * + * @dev: Device to adjust + * Returns: 0 on success, -ve on error + * * Consoles which use proportional fonts need to track the position of * each character output so that backspace will return to the correct * place. This method signals to the console driver that a new entry @@ -123,6 +135,9 @@ struct vidconsole_ops { /** * backspace() - Handle erasing the last character * + * @dev: Device to adjust + * Returns: 0 on success, -ve on error + * * With proportional fonts the vidconsole uclass cannot itself erase * the previous character. This optional method will be called when * a backspace is needed. The driver should erase the previous @@ -133,11 +148,53 @@ struct vidconsole_ops { * characters. */ int (*backspace)(struct udevice *dev); + + /** + * get_font() - Obtain information about a font (optional) + * + * @dev: Device to check + * @seq: Font number to query (0=first, 1=second, etc.) + * @info: Returns font information on success + * Returns: 0 on success, -ENOENT if no such font + */ + int (*get_font)(struct udevice *dev, int seq, + struct vidfont_info *info); + + /** + * select_font() - Select a particular font by name / size + * + * @dev: Device to adjust + * @name: Font name to use (NULL to use default) + * @size: Font size to use (0 to use default) + * Returns: 0 on success, -ENOENT if no such font + */ + int (*select_font)(struct udevice *dev, const char *name, uint size); }; /* Get a pointer to the driver operations for a video console device */ #define vidconsole_get_ops(dev) ((struct vidconsole_ops *)(dev)->driver->ops) +/** + * vidconsole_get_font() - Obtain information about a font + * + * @dev: Device to check + * @seq: Font number to query (0=first, 1=second, etc.) + * @info: Returns font information on success + * Returns: 0 on success, -ENOENT if no such font, -ENOSYS if there is no such + * method + */ +int vidconsole_get_font(struct udevice *dev, int seq, + struct vidfont_info *info); + +/** + * vidconsole_select_font() - Select a particular font by name / size + * + * @dev: Device to adjust + * @name: Font name to use (NULL to use default) + * @size: Font size to use (0 to use default) + */ +int vidconsole_select_font(struct udevice *dev, const char *name, uint size); + /** * vidconsole_putc_xy() - write a single character to a position * @@ -234,27 +291,21 @@ void vidconsole_set_cursor_pos(struct udevice *dev, int x, int y); /** * vidconsole_list_fonts() - List the available fonts * - * This shows a list on the console - */ -void vidconsole_list_fonts(void); - -/** - * vidconsole_select_font() - Select a font to use + * @dev: vidconsole device to check * - * @dev: vidconsole device - * @name: Font name - * @size: Size of the font (norminal pixel height) or 0 for default + * This shows a list of fonts known by this vidconsole. The list is displayed on + * the console (not necessarily @dev but probably) */ -int vidconsole_select_font(struct udevice *dev, const char *name, uint size); +void vidconsole_list_fonts(struct udevice *dev); /** - * vidconsole_get_font() - get the current font name and size + * vidconsole_get_font_size() - get the current font name and size * * @dev: vidconsole device * @sizep: Place to put the font size (nominal height in pixels) * Returns: Current font name */ -const char *vidconsole_get_font(struct udevice *dev, uint *sizep); +const char *vidconsole_get_font_size(struct udevice *dev, uint *sizep); #ifdef CONFIG_VIDEO_COPY /** diff --git a/test/cmd/font.c b/test/cmd/font.c index 7a4156ade62..adb353965a7 100644 --- a/test/cmd/font.c +++ b/test/cmd/font.c @@ -33,7 +33,7 @@ static int font_test_base(struct unit_test_state *uts) ut_assertok(ut_check_console_end(uts)); ut_asserteq_str("nimbus_sans_l_regular", - vidconsole_get_font(dev, &size)); + vidconsole_get_font_size(dev, &size)); ut_asserteq(18, size); max_metrics = 1; @@ -53,14 +53,14 @@ static int font_test_base(struct unit_test_state *uts) ut_assertok(ut_check_console_end(uts)); ut_asserteq_str("cantoraone_regular", - vidconsole_get_font(dev, &size)); + vidconsole_get_font_size(dev, &size)); ut_asserteq(40, size); ut_assertok(run_command("font size 30", 0)); ut_assertok(ut_check_console_end(uts)); ut_asserteq_str("cantoraone_regular", - vidconsole_get_font(dev, &size)); + vidconsole_get_font_size(dev, &size)); ut_asserteq(30, size); return 0; -- cgit v1.3.1 From 2175e76a51e53798ee4e19903b368a7e6c98356a Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:33 -0600 Subject: bootstd: Read the Operating System name for distro/scripts Add the concept of an OS name to the bootflow. This typically includes the OS name, version and kernel version. Implement this for the distro and script bootmeths so that it works with Armbian and older version of Fedora. Signed-off-by: Simon Glass --- boot/bootflow.c | 1 + boot/bootmeth_distro.c | 36 ++++++++++++++++++++++++++++++++++++ boot/bootmeth_script.c | 34 ++++++++++++++++++++++++++++++++++ cmd/bootflow.c | 1 + include/bootflow.h | 3 +++ test/boot/bootflow.c | 1 + 6 files changed, 76 insertions(+) (limited to 'cmd') diff --git a/boot/bootflow.c b/boot/bootflow.c index f9ad4099244..163cd4953dd 100644 --- a/boot/bootflow.c +++ b/boot/bootflow.c @@ -354,6 +354,7 @@ void bootflow_free(struct bootflow *bflow) free(bflow->subdir); free(bflow->fname); free(bflow->buf); + free(bflow->os_name); } void bootflow_remove(struct bootflow *bflow) diff --git a/boot/bootmeth_distro.c b/boot/bootmeth_distro.c index 5c6c687f0a6..6ef0fa1f2c9 100644 --- a/boot/bootmeth_distro.c +++ b/boot/bootmeth_distro.c @@ -66,6 +66,38 @@ static int distro_check(struct udevice *dev, struct bootflow_iter *iter) return 0; } +/** + * distro_fill_info() - Decode the extlinux file to find out distro info + * + * @bflow: Bootflow to process + * @return 0 if OK, -ve on error + */ +static int distro_fill_info(struct bootflow *bflow) +{ + struct membuff mb; + char line[200]; + char *data; + int len; + + log_debug("parsing bflow file size %x\n", bflow->size); + membuff_init(&mb, bflow->buf, bflow->size); + membuff_putraw(&mb, bflow->size, true, &data); + while (len = membuff_readline(&mb, line, sizeof(line) - 1, ' '), len) { + char *tok, *p = line; + + tok = strsep(&p, " "); + if (p) { + if (!strcmp("label", tok)) { + bflow->os_name = strdup(p); + if (!bflow->os_name) + return log_msg_ret("os", -ENOMEM); + } + } + } + + return 0; +} + static int distro_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc; @@ -99,6 +131,10 @@ static int distro_read_bootflow(struct udevice *dev, struct bootflow *bflow) if (ret) return log_msg_ret("read", ret); + ret = distro_fill_info(bflow); + if (ret) + return log_msg_ret("inf", ret); + return 0; } diff --git a/boot/bootmeth_script.c b/boot/bootmeth_script.c index 5799c89a466..ba8e5d0438a 100644 --- a/boot/bootmeth_script.c +++ b/boot/bootmeth_script.c @@ -35,6 +35,36 @@ static int script_check(struct udevice *dev, struct bootflow_iter *iter) return 0; } +/** + * script_fill_info() - Decode the U-Boot script to find out distro info + * + * @bflow: Bootflow to process + * @return 0 if OK, -ve on error + */ +static int script_fill_info(struct bootflow *bflow) +{ + char *name = NULL; + char *data; + uint len; + int ret; + + log_debug("parsing bflow file size %x\n", bflow->size); + + ret = image_locate_script(bflow->buf, bflow->size, NULL, NULL, &data, &len); + if (!ret) { + if (strstr(data, "armbianEnv")) + name = "Armbian"; + } + + if (name) { + bflow->os_name = strdup(name); + if (!bflow->os_name) + return log_msg_ret("os", -ENOMEM); + } + + return 0; +} + static int script_read_bootflow(struct udevice *dev, struct bootflow *bflow) { struct blk_desc *desc = NULL; @@ -75,6 +105,10 @@ static int script_read_bootflow(struct udevice *dev, struct bootflow *bflow) if (ret) return log_msg_ret("read", ret); + ret = script_fill_info(bflow); + if (ret) + return log_msg_ret("inf", ret); + return 0; } diff --git a/cmd/bootflow.c b/cmd/bootflow.c index 313103d2775..6b8ac8c8504 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -337,6 +337,7 @@ static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc, printf("Filename: %s\n", bflow->fname); printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf)); printf("Size: %x (%d bytes)\n", bflow->size, bflow->size); + printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)"); printf("Error: %d\n", bflow->err); if (dump && bflow->buf) { /* Set some sort of maximum on the size */ diff --git a/include/bootflow.h b/include/bootflow.h index 32dbbbbe261..776158c65df 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -52,6 +52,8 @@ enum bootflow_state_t { * @buf: Bootflow file contents (allocated) * @size: Size of bootflow file in bytes * @err: Error number received (0 if OK) + * @os_name: Name of the OS / distro being booted, or NULL if not known + * (allocated) */ struct bootflow { struct list_head bm_node; @@ -68,6 +70,7 @@ struct bootflow { char *buf; int size; int err; + char *os_name; }; /** diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index e1e07082105..3296316cf0d 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -188,6 +188,7 @@ static int bootflow_cmd_info(struct unit_test_state *uts) ut_assert_nextline("Filename: /extlinux/extlinux.conf"); ut_assert_nextlinen("Buffer: "); ut_assert_nextline("Size: 253 (595 bytes)"); + ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)"); ut_assert_nextline("Error: 0"); ut_assert_console_end(); -- cgit v1.3.1 From 24d8e1b37b90760a6c9867f37210aa4b1f2e8f63 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:34 -0600 Subject: bootstd: Allow reading a logo for the OS Some operating systems provide a logo in bmp format. Read this in if present so it can be displayed in the menu. Signed-off-by: Simon Glass --- boot/bootmeth-uclass.c | 69 +++++++++++++++++++++++++++++++++++++++++++------- boot/bootmeth_script.c | 4 +++ cmd/bootflow.c | 6 +++++ include/bootflow.h | 4 +++ include/bootmeth.h | 16 ++++++++++++ test/boot/bootflow.c | 1 + 6 files changed, 91 insertions(+), 9 deletions(-) (limited to 'cmd') diff --git a/boot/bootmeth-uclass.c b/boot/bootmeth-uclass.c index 25552dd96f6..4c3529d1555 100644 --- a/boot/bootmeth-uclass.c +++ b/boot/bootmeth-uclass.c @@ -290,25 +290,19 @@ int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc, return 0; } -int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align) +static int alloc_file(const char *fname, uint size, void **bufp) { loff_t bytes_read; ulong addr; char *buf; - uint size; int ret; - size = bflow->size; - log_debug(" - script file size %x\n", size); - if (size > size_limit) - return log_msg_ret("chk", -E2BIG); - - buf = memalign(align, size + 1); + buf = malloc(size + 1); if (!buf) return log_msg_ret("buf", -ENOMEM); addr = map_to_sysmem(buf); - ret = fs_read(bflow->fname, addr, 0, 0, &bytes_read); + ret = fs_read(fname, addr, 0, size, &bytes_read); if (ret) { free(buf); return log_msg_ret("read", ret); @@ -316,12 +310,69 @@ int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align) if (size != bytes_read) return log_msg_ret("bread", -EINVAL); buf[size] = '\0'; + + *bufp = buf; + + return 0; +} + +int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align) +{ + void *buf; + uint size; + int ret; + + size = bflow->size; + log_debug(" - script file size %x\n", size); + if (size > size_limit) + return log_msg_ret("chk", -E2BIG); + + ret = alloc_file(bflow->fname, bflow->size, &buf); + if (ret) + return log_msg_ret("all", ret); + bflow->state = BOOTFLOWST_READY; bflow->buf = buf; return 0; } +int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, + void **bufp, uint *sizep) +{ + struct blk_desc *desc = NULL; + char path[200]; + loff_t size; + void *buf; + int ret; + + snprintf(path, sizeof(path), "%s%s", bflow->subdir, fname); + log_debug("trying: %s\n", path); + + if (bflow->blk) + desc = dev_get_uclass_plat(bflow->blk); + + ret = setup_fs(bflow, desc); + if (ret) + return log_msg_ret("fs", ret); + + ret = fs_size(path, &size); + log_debug(" %s - err=%d\n", path, ret); + + ret = setup_fs(bflow, desc); + if (ret) + return log_msg_ret("fs", ret); + + ret = alloc_file(path, size, &buf); + if (ret) + return log_msg_ret("all", ret); + + *bufp = buf; + *sizep = size; + + return 0; +} + int bootmeth_common_read_file(struct udevice *dev, struct bootflow *bflow, const char *file_path, ulong addr, ulong *sizep) { diff --git a/boot/bootmeth_script.c b/boot/bootmeth_script.c index ba8e5d0438a..c7061eb998f 100644 --- a/boot/bootmeth_script.c +++ b/boot/bootmeth_script.c @@ -109,6 +109,10 @@ static int script_read_bootflow(struct udevice *dev, struct bootflow *bflow) if (ret) return log_msg_ret("inf", ret); + ret = bootmeth_alloc_other(bflow, "boot.bmp", &bflow->logo, + &bflow->logo_size); + /* ignore error */ + return 0; } diff --git a/cmd/bootflow.c b/cmd/bootflow.c index 6b8ac8c8504..495ef85f25b 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -338,6 +338,12 @@ static int do_bootflow_info(struct cmd_tbl *cmdtp, int flag, int argc, printf("Buffer: %lx\n", (ulong)map_to_sysmem(bflow->buf)); printf("Size: %x (%d bytes)\n", bflow->size, bflow->size); printf("OS: %s\n", bflow->os_name ? bflow->os_name : "(none)"); + printf("Logo: %s\n", bflow->logo ? + simple_xtoa((ulong)map_to_sysmem(bflow->logo)) : "(none)"); + if (bflow->logo) { + printf("Logo size: %x (%d bytes)\n", bflow->logo_size, + bflow->logo_size); + } printf("Error: %d\n", bflow->err); if (dump && bflow->buf) { /* Set some sort of maximum on the size */ diff --git a/include/bootflow.h b/include/bootflow.h index 776158c65df..8a07ab30191 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -49,6 +49,8 @@ enum bootflow_state_t { * @state: Current state (enum bootflow_state_t) * @subdir: Subdirectory to fetch files from (with trailing /), or NULL if none * @fname: Filename of bootflow file (allocated) + * @logo: Logo to display for this bootflow (BMP format) + * @logo_size: Size of the logo in bytes * @buf: Bootflow file contents (allocated) * @size: Size of bootflow file in bytes * @err: Error number received (0 if OK) @@ -67,6 +69,8 @@ struct bootflow { enum bootflow_state_t state; char *subdir; char *fname; + void *logo; + uint logo_size; char *buf; int size; int err; diff --git a/include/bootmeth.h b/include/bootmeth.h index 50ded055f3f..669b14ce81e 100644 --- a/include/bootmeth.h +++ b/include/bootmeth.h @@ -265,6 +265,22 @@ int bootmeth_try_file(struct bootflow *bflow, struct blk_desc *desc, */ int bootmeth_alloc_file(struct bootflow *bflow, uint size_limit, uint align); +/** + * bootmeth_alloc_other() - Allocate and read a file for a bootflow + * + * This reads an arbitrary file in the same directory as the bootflow, + * allocating memory for it. The buffer is one byte larger than the file length, + * so that it can be nul-terminated. + * + * @bflow: Information about file to read + * @fname: Filename to read from (within bootflow->subdir) + * @bufp: Returns a pointer to the allocated buffer + * @sizep: Returns the size of the buffer + * Return: 0 if OK, -ENOMEM if out of memory, other -ve on other error + */ +int bootmeth_alloc_other(struct bootflow *bflow, const char *fname, + void **bufp, uint *sizep); + /** * bootmeth_common_read_file() - Common handler for reading a file * diff --git a/test/boot/bootflow.c b/test/boot/bootflow.c index 3296316cf0d..00dfd990687 100644 --- a/test/boot/bootflow.c +++ b/test/boot/bootflow.c @@ -189,6 +189,7 @@ static int bootflow_cmd_info(struct unit_test_state *uts) ut_assert_nextlinen("Buffer: "); ut_assert_nextline("Size: 253 (595 bytes)"); ut_assert_nextline("OS: Fedora-Workstation-armhfp-31-1.9 (5.3.7-301.fc31.armv7hl)"); + ut_assert_nextline("Logo: (none)"); ut_assert_nextline("Error: 0"); ut_assert_console_end(); -- cgit v1.3.1 From 02d929bfb25af22171dbd100f38ffd8fa6bf6238 Mon Sep 17 00:00:00 2001 From: Simon Glass Date: Fri, 6 Jan 2023 08:52:40 -0600 Subject: bootstd: Support creating a boot menu Create an expo to handle the boot menu. For now this is quite simple, with just a header, some menu items and a pointer to show the current one. Signed-off-by: Simon Glass --- boot/Makefile | 1 + boot/bootflow_internal.h | 47 +++++++++ boot/bootflow_menu.c | 241 +++++++++++++++++++++++++++++++++++++++++++++++ cmd/bootflow.c | 37 +++++++- include/bootflow.h | 23 +++++ 5 files changed, 347 insertions(+), 2 deletions(-) create mode 100644 boot/bootflow_internal.h create mode 100644 boot/bootflow_menu.c (limited to 'cmd') diff --git a/boot/Makefile b/boot/Makefile index 0b30fcd64a9..f990e66f522 100644 --- a/boot/Makefile +++ b/boot/Makefile @@ -30,6 +30,7 @@ obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_SANDBOX) += bootmeth_sandbox.o obj-$(CONFIG_$(SPL_TPL_)BOOTMETH_SCRIPT) += bootmeth_script.o ifdef CONFIG_$(SPL_TPL_)BOOTSTD_FULL obj-$(CONFIG_$(SPL_TPL_)CMD_BOOTEFI_BOOTMGR) += bootmeth_efi_mgr.o +obj-$(CONFIG_$(SPL_TPL_)BOOTSTD) += bootflow_menu.o endif obj-$(CONFIG_$(SPL_TPL_)OF_LIBFDT) += image-fdt.o diff --git a/boot/bootflow_internal.h b/boot/bootflow_internal.h new file mode 100644 index 00000000000..38cf02a55b5 --- /dev/null +++ b/boot/bootflow_internal.h @@ -0,0 +1,47 @@ +/* SPDX-License-Identifier: GPL-2.0+ */ +/* + * Internal header file for bootflow + * + * Copyright 2022 Google LLC + * Written by Simon Glass + */ + +#ifndef __BOOTFLOW_INTERNAL_H +#define __BOOTFLOW_INTERNAL_H + +/* expo IDs for elements of the bootflow menu */ +enum { + START, + + /* strings */ + STR_PROMPT, + STR_MENU_TITLE, + STR_POINTER, + + /* scene */ + MAIN, + + /* objects */ + OBJ_U_BOOT_LOGO, + OBJ_MENU, + OBJ_PROMPT, + OBJ_MENU_TITLE, + OBJ_POINTER, + + /* strings for menu items */ + STR_LABEL = 100, + STR_DESC = 200, + STR_KEY = 300, + + /* menu items / components (bootflow number is added to these) */ + ITEM = 400, + ITEM_LABEL = 500, + ITEM_DESC = 600, + ITEM_KEY = 700, + ITEM_PREVIEW = 800, + + /* left margin for the main menu */ + MARGIN_LEFT = 100, +}; + +#endif /* __BOOTFLOW_INTERNAL_H */ diff --git a/boot/bootflow_menu.c b/boot/bootflow_menu.c new file mode 100644 index 00000000000..1105783afeb --- /dev/null +++ b/boot/bootflow_menu.c @@ -0,0 +1,241 @@ +// SPDX-License-Identifier: GPL-2.0+ +/* + * Provide a menu of available bootflows and related options + * + * Copyright 2022 Google LLC + * Written by Simon Glass + */ + +#define LOG_CATEGORY UCLASS_BOOTSTD + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "bootflow_internal.h" + +/** + * struct menu_priv - information about the menu + * + * @num_bootflows: Number of bootflows in the menu + */ +struct menu_priv { + int num_bootflows; +}; + +int bootflow_menu_new(struct expo **expp) +{ + struct udevice *last_bootdev; + struct scene_obj_menu *menu; + struct menu_priv *priv; + struct bootflow *bflow; + struct scene *scn; + struct expo *exp; + void *logo; + int ret, i; + + priv = calloc(1, sizeof(*priv)); + if (!priv) + return log_msg_ret("prv", -ENOMEM); + + ret = expo_new("bootflows", priv, &exp); + if (ret) + return log_msg_ret("exp", ret); + + ret = scene_new(exp, "main", MAIN, &scn); + if (ret < 0) + return log_msg_ret("scn", ret); + + ret |= scene_txt_str(scn, "prompt", OBJ_PROMPT, STR_PROMPT, + "UP and DOWN to choose, ENTER to select", NULL); + + ret = scene_menu(scn, "main", OBJ_MENU, &menu); + ret |= scene_obj_set_pos(scn, OBJ_MENU, MARGIN_LEFT, 100); + ret |= scene_txt_str(scn, "title", OBJ_MENU_TITLE, STR_MENU_TITLE, + "U-Boot - Boot Menu", NULL); + ret |= scene_menu_set_title(scn, OBJ_MENU, OBJ_PROMPT); + + logo = video_get_u_boot_logo(); + if (logo) { + ret |= scene_img(scn, "ulogo", OBJ_U_BOOT_LOGO, logo, NULL); + ret |= scene_obj_set_pos(scn, OBJ_U_BOOT_LOGO, -4, 4); + } + + ret |= scene_txt_str(scn, "cur_item", OBJ_POINTER, STR_POINTER, ">", + NULL); + ret |= scene_menu_set_pointer(scn, OBJ_MENU, OBJ_POINTER); + if (ret < 0) + return log_msg_ret("new", -EINVAL); + + last_bootdev = NULL; + for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36; + ret = bootflow_next_glob(&bflow), i++) { + char str[2], *label, *key; + uint preview_id; + bool add_gap; + + if (bflow->state != BOOTFLOWST_READY) + continue; + + *str = i < 10 ? '0' + i : 'A' + i - 10; + str[1] = '\0'; + key = strdup(str); + if (!key) + return log_msg_ret("key", -ENOMEM); + label = strdup(dev_get_parent(bflow->dev)->name); + if (!label) { + free(key); + return log_msg_ret("nam", -ENOMEM); + } + + add_gap = last_bootdev != bflow->dev; + last_bootdev = bflow->dev; + + ret = expo_str(exp, "prompt", STR_POINTER, ">"); + ret |= scene_txt_str(scn, "label", ITEM_LABEL + i, + STR_LABEL + i, label, NULL); + ret |= scene_txt_str(scn, "desc", ITEM_DESC + i, STR_DESC + i, + bflow->os_name ? bflow->os_name : + bflow->name, NULL); + ret |= scene_txt_str(scn, "key", ITEM_KEY + i, STR_KEY + i, key, + NULL); + preview_id = 0; + if (bflow->logo) { + preview_id = ITEM_PREVIEW + i; + ret |= scene_img(scn, "preview", preview_id, + bflow->logo, NULL); + } + ret |= scene_menuitem(scn, OBJ_MENU, "item", ITEM + i, + ITEM_KEY + i, ITEM_LABEL + i, + ITEM_DESC + i, preview_id, + add_gap ? SCENEMIF_GAP_BEFORE : 0, + NULL); + + if (ret < 0) + return log_msg_ret("itm", -EINVAL); + ret = 0; + priv->num_bootflows++; + } + + *expp = exp; + + return 0; +} + +int bootflow_menu_run(struct bootstd_priv *std, bool text_mode, + struct bootflow **bflowp) +{ + struct cli_ch_state s_cch, *cch = &s_cch; + struct bootflow *sel_bflow; + struct udevice *dev; + struct expo *exp; + uint sel_id; + bool done; + int ret; + + cli_ch_init(cch); + + sel_bflow = NULL; + *bflowp = NULL; + + ret = bootflow_menu_new(&exp); + if (ret) + return log_msg_ret("exp", ret); + + /* For now we only support a video console */ + ret = uclass_first_device_err(UCLASS_VIDEO, &dev); + if (ret) + return log_msg_ret("vid", ret); + ret = expo_set_display(exp, dev); + if (ret) + return log_msg_ret("dis", ret); + + ret = expo_set_scene_id(exp, MAIN); + if (ret) + return log_msg_ret("scn", ret); + + if (text_mode) + exp_set_text_mode(exp, text_mode); + + done = false; + do { + struct expo_action act; + int ichar, key; + + ret = expo_render(exp); + if (ret) + break; + + ichar = cli_ch_process(cch, 0); + if (!ichar) { + while (!ichar && !tstc()) { + schedule(); + mdelay(2); + ichar = cli_ch_process(cch, -ETIMEDOUT); + } + if (!ichar) { + ichar = getchar(); + ichar = cli_ch_process(cch, ichar); + } + } + + key = 0; + if (ichar) { + key = bootmenu_conv_key(ichar); + if (key == BKEY_NONE) + key = ichar; + } + if (!key) + continue; + + ret = expo_send_key(exp, key); + if (ret) + break; + + ret = expo_action_get(exp, &act); + if (!ret) { + switch (act.type) { + case EXPOACT_SELECT: + sel_id = act.select.id; + done = true; + break; + case EXPOACT_QUIT: + done = true; + break; + default: + break; + } + } + } while (!done); + + if (ret) + return log_msg_ret("end", ret); + + if (sel_id) { + struct bootflow *bflow; + int i; + + for (ret = bootflow_first_glob(&bflow), i = 0; !ret && i < 36; + ret = bootflow_next_glob(&bflow), i++) { + if (i == sel_id - ITEM) { + sel_bflow = bflow; + break; + } + } + } + + expo_destroy(exp); + + if (!sel_bflow) + return -EAGAIN; + *bflowp = sel_bflow; + + return 0; +} diff --git a/cmd/bootflow.c b/cmd/bootflow.c index 495ef85f25b..2b6ed26fdcb 100644 --- a/cmd/bootflow.c +++ b/cmd/bootflow.c @@ -389,6 +389,37 @@ static int do_bootflow_boot(struct cmd_tbl *cmdtp, int flag, int argc, return 0; } + +static int do_bootflow_menu(struct cmd_tbl *cmdtp, int flag, int argc, + char *const argv[]) +{ + struct bootstd_priv *std; + struct bootflow *bflow; + bool text_mode = false; + int ret; + + if (argc > 1 && *argv[1] == '-') + text_mode = strchr(argv[1], 't'); + + ret = bootstd_get_priv(&std); + if (ret) + return CMD_RET_FAILURE; + + ret = bootflow_menu_run(std, text_mode, &bflow); + if (ret) { + if (ret == -EAGAIN) + printf("Nothing chosen\n"); + else + printf("Menu failed (err=%d)\n", ret); + + return CMD_RET_FAILURE; + } + + printf("Selected: %s\n", bflow->os_name ? bflow->os_name : bflow->name); + std->cur_bootflow = bflow; + + return 0; +} #endif /* CONFIG_CMD_BOOTFLOW_FULL */ #ifdef CONFIG_SYS_LONGHELP @@ -398,7 +429,8 @@ static char bootflow_help_text[] = "bootflow list [-e] - list scanned bootflows (-e errors)\n" "bootflow select [|] - select a bootflow\n" "bootflow info [-d] - show info on current bootflow (-d dump bootflow)\n" - "bootflow boot - boot current bootflow (or first available if none selected)"; + "bootflow boot - boot current bootflow (or first available if none selected)\n" + "bootflow menu [-t] - show a menu of available bootflows"; #else "scan - boot first available bootflow\n"; #endif @@ -410,6 +442,7 @@ U_BOOT_CMD_WITH_SUBCMDS(bootflow, "Boot flows", bootflow_help_text, U_BOOT_SUBCMD_MKENT(list, 2, 1, do_bootflow_list), U_BOOT_SUBCMD_MKENT(select, 2, 1, do_bootflow_select), U_BOOT_SUBCMD_MKENT(info, 2, 1, do_bootflow_info), - U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot) + U_BOOT_SUBCMD_MKENT(boot, 1, 1, do_bootflow_boot), + U_BOOT_SUBCMD_MKENT(menu, 2, 1, do_bootflow_menu), #endif ); diff --git a/include/bootflow.h b/include/bootflow.h index 8a07ab30191..e7a09568f1b 100644 --- a/include/bootflow.h +++ b/include/bootflow.h @@ -9,6 +9,9 @@ #include +struct bootstd_priv; +struct expo; + /** * enum bootflow_state_t - states that a particular bootflow can be in * @@ -336,4 +339,24 @@ int bootflow_iter_uses_network(const struct bootflow_iter *iter); */ int bootflow_iter_uses_system(const struct bootflow_iter *iter); +/** + * bootflow_menu_new() - Create a new bootflow menu + * + * @expp: Returns the expo created + * Returns 0 on success, -ve on error + */ +int bootflow_menu_new(struct expo **expp); + +/** + * bootflow_menu_run() - Create and run a menu of available bootflows + * + * @std: Bootstd information + * @text_mode: Uses a text-based menu suitable for a serial port + * @bflowp: Returns chosen bootflow (set to NULL if nothing is chosen) + * @return 0 if an option was chosen, -EAGAIN if nothing was chosen, -ve on + * error + */ +int bootflow_menu_run(struct bootstd_priv *std, bool text_mode, + struct bootflow **bflowp); + #endif -- cgit v1.3.1