From 7ac20e06150c3904b77504ede2e9ed2a0ce9cd82 Mon Sep 17 00:00:00 2001 From: mintsuki Date: Sun, 11 Apr 2021 02:30:48 +0200 Subject: [PATCH] gterm: Many improvements --- CONFIG.md | 5 +- stage23/drivers/gop.c | 6 ++ stage23/drivers/vbe.c | 6 ++ stage23/entry.s3.c | 16 +---- stage23/lib/config.c | 2 +- stage23/lib/fb.h | 1 + stage23/lib/gterm.c | 126 +++++++++++++++++++++++++++++++++----- stage23/lib/gterm.h | 3 +- stage23/lib/term.c | 6 +- stage23/lib/term.h | 4 +- stage23/menu.c | 82 ++----------------------- stage23/protos/stivale2.c | 41 +++++-------- test/limine.cfg | 2 +- 13 files changed, 160 insertions(+), 140 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 0cac8b30..9f2913f7 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -54,9 +54,12 @@ Some keys take *URIs* as values; these are described in the next section. * `GRAPHICS` - If set to `yes`, use a graphical framebuffer for the boot menu, else use text mode. Ignored with Limine UEFI, forced to `yes`. * `MENU_RESOLUTION` - Specify screen resolution to be used by the Limine menu in the form `x`. This will *only* affect the menu, not any booted OS. If not specified, Limine will pick a resolution automatically. If the resolution is not available, Limine will pick another one automatically. Ignored if `GRAPHICS` is not `yes`. * `MENU_BRANDING` - A string that will be displayed on top of the Limine menu. -* `MENU_FONT` - URI path to a font file to be used instead of the default one for the menu. The font file must be a code page 437 character set comprised of 256 consecutive 8x16 glyphs bitmaps (4096 byte font file). Each glyph's bitmap must be expressed left to right (1 byte per row), and top to bottom (16 bytes per whole glyph). +* `MENU_FONT` - URI path to a font file to be used instead of the default one for the menu and terminal. The font file must be a code page 437 character set comprised of 256 consecutive 8x16 glyphs bitmaps (4096 byte font file). Each glyph's bitmap must be expressed left to right (1 byte per row), and top to bottom (16 bytes per whole glyph). +* `TERMINAL_FONT` - Alias of `MENU_FONT`. * `THEME_COLOURS` - Specifies the colour palette used by the terminal (AARRGGBB). It is a `;` separated array of 10 colours: black, red, green, brown, blue, magenta, cyan, gray, background, and foreground respectively. While an alpha transparency value can be specified for every colour, it is ignored for all but background. Ignored if `GRAPHICS` is not `yes`. * `THEME_COLORS` - Alias of `THEME_COLOURS`. +* `THEME_BACKGROUND` - Alias of the background value in `THEME_COLOURS`. +* `THEME_FOREGROUND` - Alias of the foreground value in `THEME_COLOURS`. * `THEME_MARGIN` - Set the amount of margin around the terminal. Ignored if `GRAPHICS` is not `yes`. * `THEME_MARGIN_GRADIENT` - Set the thickness in pixel for the gradient around the terminal. Ignored if `GRAPHICS` is not `yes`. * `BACKGROUND_PATH` - URI where to find the background .BMP file. Ignored if `GRAPHICS` is not `yes`. diff --git a/stage23/drivers/gop.c b/stage23/drivers/gop.c index 046e33b2..4d53fcf6 100644 --- a/stage23/drivers/gop.c +++ b/stage23/drivers/gop.c @@ -130,6 +130,8 @@ static size_t preset_mode = INVALID_PRESET_MODE; bool init_gop(struct fb_info *ret, uint16_t target_width, uint16_t target_height, uint16_t target_bpp) { + ret->default_res = false; + EFI_STATUS status; EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID; @@ -167,6 +169,8 @@ bool init_gop(struct fb_info *ret, size_t current_fallback = 0; if (!target_width || !target_height || !target_bpp) { + ret->default_res = true; + struct edid_info_struct *edid_info = get_edid_info(); if (edid_info != NULL) { int edid_width = (int)edid_info->det_timing_desc1[2]; @@ -195,6 +199,8 @@ retry: } fallback: + ret->default_res = true; + if (current_fallback == 0) { if (try_mode(ret, preset_mode, 0, 0, 0)) return true; diff --git a/stage23/drivers/vbe.c b/stage23/drivers/vbe.c index 24702532..8bd0378a 100644 --- a/stage23/drivers/vbe.c +++ b/stage23/drivers/vbe.c @@ -120,6 +120,8 @@ bool init_vbe(struct fb_info *ret, uint16_t target_width, uint16_t target_height, uint16_t target_bpp) { print("vbe: Initialising...\n"); + ret->default_res = false; + size_t current_fallback = 0; struct vbe_info_struct vbe_info; @@ -141,6 +143,8 @@ bool init_vbe(struct fb_info *ret, }; if (!target_width || !target_height || !target_bpp) { + ret->default_res = true; + struct edid_info_struct *edid_info = get_edid_info(); if (edid_info != NULL) { int edid_width = (int)edid_info->det_timing_desc1[2]; @@ -216,6 +220,8 @@ retry: } fallback: + ret->default_res = true; + if (current_fallback < SIZEOF_ARRAY(fallback_resolutions)) { target_width = fallback_resolutions[current_fallback].width; target_height = fallback_resolutions[current_fallback].height; diff --git a/stage23/entry.s3.c b/stage23/entry.s3.c index d8a1c85d..9ad6a700 100644 --- a/stage23/entry.s3.c +++ b/stage23/entry.s3.c @@ -31,20 +31,8 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable init_memmap(); - uint32_t colourscheme[] = { - 0x00000000, // black - 0x00aa0000, // red - 0x0000aa00, // green - 0x00aa5500, // brown - 0x000000aa, // blue - 0x00aa00aa, // magenta - 0x0000aaaa, // cyan - 0x00aaaaaa, // grey - 0x00000000, // background (black) - 0x00aaaaaa // foreground (white) - }; - - term_vbe(colourscheme, 64, 0, NULL); + term_vbe(0, 0); + early_term = true; print("Limine " LIMINE_VERSION "\n\n"); diff --git a/stage23/lib/config.c b/stage23/lib/config.c index fdcd6157..aa1b0741 100644 --- a/stage23/lib/config.c +++ b/stage23/lib/config.c @@ -207,7 +207,7 @@ cont: } char *config_get_value(const char *config, size_t index, const char *key) { - if (!key) + if (!key || !config_ready) return NULL; if (config == NULL) diff --git a/stage23/lib/fb.h b/stage23/lib/fb.h index efce5576..d19b1567 100644 --- a/stage23/lib/fb.h +++ b/stage23/lib/fb.h @@ -10,6 +10,7 @@ struct resolution { }; struct fb_info { + bool default_res; uint8_t memory_model; uint64_t framebuffer_addr; uint16_t framebuffer_pitch; diff --git a/stage23/lib/gterm.c b/stage23/lib/gterm.c index 53f00e70..e68b19db 100644 --- a/stage23/lib/gterm.c +++ b/stage23/lib/gterm.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -365,17 +366,116 @@ void gterm_putchar(uint8_t c) { } } -bool gterm_init(int *_rows, int *_cols, uint32_t *_colours, int _margin, int _margin_gradient, struct image *_background) { - int req_width = 0, req_height = 0, req_bpp = 0; +bool gterm_init(int *_rows, int *_cols, int width, int height) { + if (current_video_mode >= 0 + && fbinfo.default_res == true + && width == 0 + && height == 0 + && fbinfo.framebuffer_bpp == 32 + && !early_term) { + *_rows = rows; + *_cols = cols; + gterm_clear(true); + return true; + } - char *menu_resolution = config_get_value(NULL, 0, "MENU_RESOLUTION"); - if (menu_resolution != NULL) - parse_resolution(&req_width, &req_height, &req_bpp, menu_resolution); + if (current_video_mode >= 0 + && fbinfo.framebuffer_width == width + && fbinfo.framebuffer_height == height + && fbinfo.framebuffer_bpp == 32 + && !early_term) { + *_rows = rows; + *_cols = cols; + gterm_clear(true); + return true; + } + + early_term = false; // We force bpp to 32 - req_bpp = 32; + if (!fb_init(&fbinfo, width, height, 32)) + return false; - fb_init(&fbinfo, req_width, req_height, req_bpp); + // default scheme + int margin = 64; + margin_gradient = 20; + + ansi_colours[0] = 0x00000000; // black + ansi_colours[1] = 0x00aa0000; // red + ansi_colours[2] = 0x0000aa00; // green + ansi_colours[3] = 0x00aa5500; // brown + ansi_colours[4] = 0x000000aa; // blue + ansi_colours[5] = 0x00aa00aa; // magenta + ansi_colours[6] = 0x0000aaaa; // cyan + ansi_colours[7] = 0x00aaaaaa; // grey + ansi_colours[8] = 0x00000000; // background (black) + ansi_colours[9] = 0x00aaaaaa; // foreground (grey) + + char *colours = config_get_value(NULL, 0, "THEME_COLOURS"); + if (colours == NULL) + colours = config_get_value(NULL, 0, "THEME_COLORS"); + if (colours != NULL) { + const char *first = colours; + int i; + for (i = 0; i < 10; i++) { + const char *last; + uint32_t col = strtoui(first, &last, 16); + if (first == last) + break; + ansi_colours[i] = col; + if (*last == 0) + break; + first = last + 1; + } + if (i < 8) { + ansi_colours[8] = ansi_colours[0]; + ansi_colours[9] = ansi_colours[7]; + } + } + + char *theme_background = config_get_value(NULL, 0, "THEME_BACKGROUND"); + if (theme_background != NULL) { + ansi_colours[8] = strtoui(theme_background, NULL, 16); + } + + char *theme_foreground = config_get_value(NULL, 0, "THEME_FOREGROUND"); + if (theme_foreground != NULL) { + ansi_colours[9] = strtoui(theme_foreground, NULL, 16); + } + + char *theme_margin = config_get_value(NULL, 0, "THEME_MARGIN"); + if (theme_margin != NULL) { + margin = strtoui(theme_margin, NULL, 10); + } + + char *theme_margin_gradient = config_get_value(NULL, 0, "THEME_MARGIN_GRADIENT"); + if (theme_margin_gradient != NULL) { + margin_gradient = strtoui(theme_margin_gradient, NULL, 10); + } + + char *background_path = config_get_value(NULL, 0, "BACKGROUND_PATH"); + if (background_path != NULL) { + struct file_handle *bg_file = ext_mem_alloc(sizeof(struct file_handle)); + if (uri_open(bg_file, background_path)) { + background = ext_mem_alloc(sizeof(struct image)); + if (open_image(background, bg_file)) { + background = NULL; + } + } + } + + if (background != NULL) { + char *background_layout = config_get_value(NULL, 0, "BACKGROUND_STYLE"); + if (background_layout != NULL && strcmp(background_layout, "centered") == 0) { + char *background_colour = config_get_value(NULL, 0, "BACKDROP_COLOUR"); + if (background_colour == NULL) + background_colour = config_get_value(NULL, 0, "BACKDROP_COLOR"); + if (background_colour == NULL) + background_colour = "0"; + uint32_t bg_col = strtoui(background_colour, NULL, 16); + image_make_centered(background, fbinfo.framebuffer_width, fbinfo.framebuffer_height, bg_col); + } + } // Ensure this is xRGB8888, we only support that for the menu if (fbinfo.red_mask_size != 8 @@ -398,6 +498,8 @@ bool gterm_init(int *_rows, int *_cols, uint32_t *_colours, int _margin, int _ma memcpy(vga_font, (void *)_binary_font_bin_start, VGA_FONT_MAX); char *menu_font = config_get_value(NULL, 0, "MENU_FONT"); + if (menu_font == NULL) + menu_font = config_get_value(NULL, 0, "TERMINAL_FONT"); if (menu_font != NULL) { struct file_handle f; if (!uri_open(&f, menu_font)) { @@ -407,8 +509,8 @@ bool gterm_init(int *_rows, int *_cols, uint32_t *_colours, int _margin, int _ma } } - *_cols = cols = (gterm_width - _margin * 2) / VGA_FONT_WIDTH; - *_rows = rows = (gterm_height - _margin * 2) / VGA_FONT_HEIGHT; + *_cols = cols = (gterm_width - margin * 2) / VGA_FONT_WIDTH; + *_rows = rows = (gterm_height - margin * 2) / VGA_FONT_HEIGHT; size_t new_grid_size = rows * cols * sizeof(struct gterm_char); if (new_grid_size > last_grid_size) { @@ -422,12 +524,6 @@ bool gterm_init(int *_rows, int *_cols, uint32_t *_colours, int _margin, int _ma last_front_grid_size = new_front_grid_size; } - background = _background; - - memcpy(ansi_colours, _colours, sizeof(ansi_colours)); - - margin_gradient = _margin_gradient; - frame_height = gterm_height / 2 - (VGA_FONT_HEIGHT * rows) / 2; frame_width = gterm_width / 2 - (VGA_FONT_WIDTH * cols) / 2; diff --git a/stage23/lib/gterm.h b/stage23/lib/gterm.h index 382b85b9..5ccfc8e2 100644 --- a/stage23/lib/gterm.h +++ b/stage23/lib/gterm.h @@ -8,8 +8,7 @@ extern struct fb_info fbinfo; -bool gterm_init(int *rows, int *cols, uint32_t *colours, int margin, - int margin_gradient, struct image *background); +bool gterm_init(int *_rows, int *_cols, int width, int height); void gterm_putchar(uint8_t c); void gterm_clear(bool move); diff --git a/stage23/lib/term.c b/stage23/lib/term.c index 6910a4e4..9510517b 100644 --- a/stage23/lib/term.c +++ b/stage23/lib/term.c @@ -6,10 +6,12 @@ #include #include -void term_vbe(uint32_t *colours, int margin, int margin_gradient, struct image *background) { +bool early_term = false; + +void term_vbe(int width, int height) { term_backend = NOT_READY; - if (!gterm_init(&term_rows, &term_cols, colours, margin, margin_gradient, background)) { + if (!gterm_init(&term_rows, &term_cols, width, height)) { #if defined (bios) // Failed to set VBE properly, default to text mode term_textmode(); diff --git a/stage23/lib/term.h b/stage23/lib/term.h index d260a339..b1a2a9ac 100644 --- a/stage23/lib/term.h +++ b/stage23/lib/term.h @@ -17,7 +17,7 @@ extern void (*set_text_bg)(int bg); extern void (*term_double_buffer)(bool status); extern void (*term_double_buffer_flush)(void); -void term_vbe(uint32_t *colours, int margin, int margin_gradient, struct image *background); +void term_vbe(int width, int height); void term_textmode(void); void term_write(const char *buf, size_t count); @@ -34,4 +34,6 @@ enum { extern int term_backend; extern int current_video_mode; +extern bool early_term; + #endif diff --git a/stage23/menu.c b/stage23/menu.c index a8d31ec3..653de514 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -369,85 +369,13 @@ char *menu(char **cmdline) { char *graphics = "yes"; #endif if (graphics != NULL && !strcmp(graphics, "yes")) { - // default scheme - int margin = 64; - int margin_gradient = 20; - uint32_t colourscheme[] = { - 0x00000000, // black - 0x00aa0000, // red - 0x0000aa00, // green - 0x00aa5500, // brown - 0x000000aa, // blue - 0x00aa00aa, // magenta - 0x0000aaaa, // cyan - 0x00aaaaaa, // grey - 0x00000000, // background (black) - 0x00aaaaaa // foreground (grey) - }; + int req_width = 0, req_height = 0, req_bpp = 0; - char *colours = config_get_value(NULL, 0, "THEME_COLOURS"); - if (colours == NULL) - colours = config_get_value(NULL, 0, "THEME_COLORS"); - if (colours != NULL) { - const char *first = colours; - int i; - for (i = 0; i < 10; i++) { - const char *last; - uint32_t col = strtoui(first, &last, 16); - if (first == last) - break; - colourscheme[i] = col; - if (*last == 0) - break; - first = last + 1; - } - if (i < 8) { - colourscheme[8] = colourscheme[0]; - colourscheme[9] = colourscheme[7]; - } - } + char *menu_resolution = config_get_value(NULL, 0, "MENU_RESOLUTION"); + if (menu_resolution != NULL) + parse_resolution(&req_width, &req_height, &req_bpp, menu_resolution); - char *theme_margin = config_get_value(NULL, 0, "THEME_MARGIN"); - if (theme_margin != NULL) { - margin = strtoui(theme_margin, NULL, 10); - } - - char *theme_margin_gradient = config_get_value(NULL, 0, "THEME_MARGIN_GRADIENT"); - if (theme_margin_gradient != NULL) { - margin_gradient = strtoui(theme_margin_gradient, NULL, 10); - } - - struct image *bg = NULL; - - char *background_path = config_get_value(NULL, 0, "BACKGROUND_PATH"); - if (background_path == NULL) - goto nobg; - - struct file_handle *bg_file = ext_mem_alloc(sizeof(struct file_handle)); - if (!uri_open(bg_file, background_path)) - goto nobg; - - bg = ext_mem_alloc(sizeof(struct image)); - if (open_image(bg, bg_file)) - bg = NULL; - - nobg: - term_vbe(colourscheme, margin, margin_gradient, NULL); - - if (bg != NULL) { - char *background_layout = config_get_value(NULL, 0, "BACKGROUND_STYLE"); - if (background_layout != NULL && strcmp(background_layout, "centered") == 0) { - char *background_colour = config_get_value(NULL, 0, "BACKDROP_COLOUR"); - if (background_colour == NULL) - background_colour = config_get_value(NULL, 0, "BACKDROP_COLOR"); - if (background_colour == NULL) - background_colour = "0"; - uint32_t bg_col = strtoui(background_colour, NULL, 16); - image_make_centered(bg, fbinfo.framebuffer_width, fbinfo.framebuffer_height, bg_col); - } - } - - term_vbe(colourscheme, margin, margin_gradient, bg); + term_vbe(req_width, req_height); } disable_cursor(); diff --git a/stage23/protos/stivale2.c b/stage23/protos/stivale2.c index d83c40a6..c8d119c1 100644 --- a/stage23/protos/stivale2.c +++ b/stage23/protos/stivale2.c @@ -269,27 +269,24 @@ void stivale2_load(char *config, char *cmdline, bool pxe, void *efi_system_table struct fb_info *fb = NULL; struct fb_info _fb; - struct stivale2_header_tag_terminal *terminal_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_TERMINAL_ID); - struct stivale2_header_tag_framebuffer *hdrtag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_FRAMEBUFFER_ID); + int req_width, req_height, req_bpp; + + if (hdrtag != NULL) { + req_width = hdrtag->framebuffer_width; + req_height = hdrtag->framebuffer_height; + req_bpp = hdrtag->framebuffer_bpp; + + char *resolution = config_get_value(config, 0, "RESOLUTION"); + if (resolution != NULL) + parse_resolution(&req_width, &req_height, &req_bpp, resolution); + } + + struct stivale2_header_tag_terminal *terminal_hdr_tag = get_tag(&stivale2_hdr, STIVALE2_HEADER_TAG_TERMINAL_ID); + if (bits == 64 && terminal_hdr_tag != NULL && hdrtag != NULL) { - // If we're coming from some odd mode (text mode), reinitialise the graphical console - if (current_video_mode < 0) { - uint32_t colourscheme[] = { - 0x00000000, // black - 0x00aa0000, // red - 0x0000aa00, // green - 0x00aa5500, // brown - 0x000000aa, // blue - 0x00aa00aa, // magenta - 0x0000aaaa, // cyan - 0x00aaaaaa, // grey - 0x00000000, // background (black) - 0x00aaaaaa // foreground (grey) - }; - term_vbe(colourscheme, 64, 20, NULL); - } + term_vbe(req_width, req_height); if (current_video_mode < 0) { panic("stivale2: Failed to initialise terminal"); @@ -316,14 +313,6 @@ void stivale2_load(char *config, char *cmdline, bool pxe, void *efi_system_table if (hdrtag != NULL) { term_deinit(); - int req_width = hdrtag->framebuffer_width; - int req_height = hdrtag->framebuffer_height; - int req_bpp = hdrtag->framebuffer_bpp; - - char *resolution = config_get_value(config, 0, "RESOLUTION"); - if (resolution != NULL) - parse_resolution(&req_width, &req_height, &req_bpp, resolution); - if (fb_init(fb, req_width, req_height, req_bpp)) { skip_modeset:; struct stivale2_struct_tag_framebuffer *tag = ext_mem_alloc(sizeof(struct stivale2_struct_tag_framebuffer)); diff --git a/test/limine.cfg b/test/limine.cfg index e9f09aa7..0c44931a 100644 --- a/test/limine.cfg +++ b/test/limine.cfg @@ -3,7 +3,7 @@ TIMEOUT=3 GRAPHICS=yes MENU_FONT=boot:///boot/font.bin -THEME_COLOURS=000000;aa0000;00aaff;aa5500;0000aa;aa00aa;9076de;aaaaaa;60000000;aaaaaa +THEME_BACKGROUND=60000000 THEME_MARGIN=64 BACKGROUND_PATH=boot:///boot/bg.bmp