video: Many terminal related bug fixes and improvements

This commit is contained in:
mintsuki 2021-04-10 23:08:32 +02:00
parent 3be9003992
commit f7638e965b
15 changed files with 210 additions and 163 deletions

View File

@ -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 `<width>x<height>`. 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`.

View File

@ -101,21 +101,21 @@ static bool try_mode(struct fb_info *ret, size_t mode, int width, int height, in
if ((int)mode == current_video_mode) {
print("gop: Mode was already set, perfect!\n");
}
} else {
status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
status = uefi_call_wrapper(gop->SetMode, 2, gop, mode);
if (status) {
current_video_mode = -2;
print("gop: Failed to set video mode %x, moving on...\n", mode);
return false;
if (status) {
current_video_mode = -2;
print("gop: Failed to set video mode %x, moving on...\n", mode);
return false;
}
}
current_video_mode = mode;
ret->memory_model = 0x06;
ret->framebuffer_addr = gop->Mode->FrameBufferBase;
ret->framebuffer_pitch = gop->Mode->Info->PixelsPerScanLine * 4;
ret->framebuffer_pitch = gop->Mode->Info->PixelsPerScanLine * (ret->framebuffer_bpp / 8);
ret->framebuffer_width = gop->Mode->Info->HorizontalResolution;
ret->framebuffer_height = gop->Mode->Info->VerticalResolution;
@ -124,8 +124,14 @@ static bool try_mode(struct fb_info *ret, size_t mode, int width, int height, in
return true;
}
#define INVALID_PRESET_MODE 0xfffffffff
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;
@ -148,7 +154,8 @@ bool init_gop(struct fb_info *ret,
panic("gop: Initialisation failed");
}
size_t preset_mode = gop->Mode->Mode;
if (preset_mode == INVALID_PRESET_MODE)
preset_mode = gop->Mode->Mode;
struct resolution fallback_resolutions[] = {
{ 0, 0, 0 }, // Overridden by preset mode
@ -162,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];
@ -190,6 +199,8 @@ retry:
}
fallback:
ret->default_res = true;
if (current_fallback == 0) {
if (try_mode(ret, preset_mode, 0, 0, 0))
return true;

View File

@ -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;

View File

@ -24,6 +24,19 @@ void stage3_common(void);
#if defined (uefi)
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
// Invalid return address of 0 to end stacktraces here
asm volatile (
"push 0\n\t"
"jmp efi_entry_point\n\t"
:: "D" (ImageHandle), "S" (SystemTable)
: "memory"
);
__builtin_unreachable();
}
__attribute__((noreturn))
void efi_entry_point(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
gST = SystemTable;
gBS = SystemTable->BootServices;
gRT = SystemTable->RuntimeServices;
@ -31,20 +44,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");
@ -61,13 +62,7 @@ EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable
panic("Can't determine boot disk");
}
// Invalid return address of 0 to end stacktraces here
asm volatile (
"push 0\n\t"
"jmp stage3_common\n\t"
);
__builtin_unreachable();
stage3_common();
}
#endif

View File

@ -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)

View File

@ -16,10 +16,6 @@ bool fb_init(struct fb_info *ret,
r = init_gop(ret, target_width, target_height, target_bpp);
#endif
memmap_alloc_range(ret->framebuffer_addr,
(uint64_t)ret->framebuffer_pitch * ret->framebuffer_height,
MEMMAP_FRAMEBUFFER, false, false, false, true);
return r;
}

View File

@ -10,6 +10,7 @@ struct resolution {
};
struct fb_info {
bool default_res;
uint8_t memory_model;
uint64_t framebuffer_addr;
uint16_t framebuffer_pitch;

View File

@ -1,6 +1,7 @@
#include <stdint.h>
#include <stddef.h>
#include <lib/gterm.h>
#include <lib/term.h>
#include <lib/blib.h>
#include <lib/libc.h>
#include <lib/config.h>
@ -31,9 +32,12 @@ static int frame_height, frame_width;
static struct image *background;
static size_t last_grid_size = 0;
static struct gterm_char *grid = NULL;
static size_t last_front_grid_size = 0;
static struct gterm_char *front_grid = NULL;
static size_t last_bg_canvas_size = 0;
static uint32_t *bg_canvas = NULL;
static bool double_buffer_enabled = false;
@ -362,17 +366,112 @@ 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) {
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) {
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
@ -395,6 +494,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)) {
@ -404,23 +505,29 @@ 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;
if (grid == NULL)
grid = ext_mem_alloc(rows * cols * sizeof(struct gterm_char));
if (front_grid == NULL)
front_grid = ext_mem_alloc(rows * cols * sizeof(struct gterm_char));
background = _background;
*_cols = cols = (gterm_width - margin * 2) / VGA_FONT_WIDTH;
*_rows = rows = (gterm_height - margin * 2) / VGA_FONT_HEIGHT;
memcpy(ansi_colours, _colours, sizeof(ansi_colours));
size_t new_grid_size = rows * cols * sizeof(struct gterm_char);
if (new_grid_size > last_grid_size) {
grid = ext_mem_alloc(new_grid_size);
last_grid_size = new_grid_size;
}
margin_gradient = _margin_gradient;
size_t new_front_grid_size = rows * cols * sizeof(struct gterm_char);
if (new_front_grid_size > last_front_grid_size) {
front_grid = ext_mem_alloc(new_front_grid_size);
last_front_grid_size = new_front_grid_size;
}
frame_height = gterm_height / 2 - (VGA_FONT_HEIGHT * rows) / 2;
frame_width = gterm_width / 2 - (VGA_FONT_WIDTH * cols) / 2;
if (bg_canvas == NULL)
bg_canvas = ext_mem_alloc(gterm_width * gterm_height * sizeof(uint32_t));
size_t new_bg_canvas_size = gterm_width * gterm_height * sizeof(uint32_t);
if (new_bg_canvas_size > last_bg_canvas_size) {
bg_canvas = ext_mem_alloc(new_bg_canvas_size);
last_bg_canvas_size = new_bg_canvas_size;
}
gterm_generate_canvas();
gterm_clear(true);

View File

@ -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);

View File

@ -6,10 +6,12 @@
#include <lib/blib.h>
#include <lib/gterm.h>
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();

View File

@ -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

View File

@ -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();

View File

@ -171,6 +171,10 @@ void stivale_load(char *config, char *cmdline) {
if (!fb_init(&fbinfo, req_width, req_height, req_bpp))
panic("stivale: Unable to set video mode");
memmap_alloc_range(fbinfo.framebuffer_addr,
(uint64_t)fbinfo.framebuffer_pitch * fbinfo.framebuffer_height,
MEMMAP_FRAMEBUFFER, false, false, false, true);
stivale_struct.framebuffer_addr = (uint64_t)fbinfo.framebuffer_addr;
stivale_struct.framebuffer_width = fbinfo.framebuffer_width;
stivale_struct.framebuffer_height = fbinfo.framebuffer_height;

View File

@ -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,19 +313,15 @@ 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));
tag->tag.identifier = STIVALE2_STRUCT_TAG_FRAMEBUFFER_ID;
memmap_alloc_range(fb->framebuffer_addr,
(uint64_t)fb->framebuffer_pitch * fb->framebuffer_height,
MEMMAP_FRAMEBUFFER, false, false, false, true);
tag->memory_model = STIVALE2_FBUF_MMODEL_RGB;
tag->framebuffer_addr = fb->framebuffer_addr;
tag->framebuffer_width = fb->framebuffer_width;

View File

@ -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