2020-06-05 21:27:52 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
2021-12-31 12:58:05 +03:00
|
|
|
#include <stdnoreturn.h>
|
2022-02-21 01:05:53 +03:00
|
|
|
#include <config.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
#include <menu.h>
|
|
|
|
#include <lib/print.h>
|
2022-08-27 00:44:47 +03:00
|
|
|
#include <lib/misc.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
#include <lib/libc.h>
|
|
|
|
#include <lib/config.h>
|
2020-09-02 10:55:56 +03:00
|
|
|
#include <lib/term.h>
|
2021-04-09 02:26:39 +03:00
|
|
|
#include <lib/gterm.h>
|
2020-10-27 10:09:27 +03:00
|
|
|
#include <lib/readline.h>
|
2020-11-02 11:20:34 +03:00
|
|
|
#include <lib/uri.h>
|
2020-09-20 13:03:44 +03:00
|
|
|
#include <mm/pmm.h>
|
2020-09-24 22:48:49 +03:00
|
|
|
#include <drivers/vbe.h>
|
2022-10-04 03:34:11 +03:00
|
|
|
#include <drivers/vga_textmode.h>
|
2021-10-09 14:32:57 +03:00
|
|
|
#include <console.h>
|
2021-12-11 17:34:30 +03:00
|
|
|
#include <protos/linux.h>
|
|
|
|
#include <protos/chainload.h>
|
2022-07-11 01:15:56 +03:00
|
|
|
#include <protos/chainload_next.h>
|
2021-12-11 17:34:30 +03:00
|
|
|
#include <protos/multiboot1.h>
|
|
|
|
#include <protos/multiboot2.h>
|
2022-03-12 21:40:49 +03:00
|
|
|
#include <protos/limine.h>
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
static char *menu_branding = NULL;
|
2021-09-06 08:31:57 +03:00
|
|
|
static char *menu_branding_colour = NULL;
|
2020-11-18 02:13:54 +03:00
|
|
|
|
|
|
|
#define EDITOR_MAX_BUFFER_SIZE 4096
|
2021-07-07 02:27:07 +03:00
|
|
|
#define TOK_KEY 0
|
|
|
|
#define TOK_EQUALS 1
|
|
|
|
#define TOK_VALUE 2
|
|
|
|
#define TOK_BADKEY 3
|
2021-07-08 18:28:43 +03:00
|
|
|
#define TOK_COMMENT 4
|
2020-11-18 02:13:54 +03:00
|
|
|
|
|
|
|
static size_t get_line_offset(size_t *displacement, size_t index, const char *buffer) {
|
|
|
|
size_t offset = 0;
|
|
|
|
size_t _index = index;
|
|
|
|
for (size_t i = 0; buffer[i]; i++) {
|
|
|
|
if (!_index--)
|
|
|
|
break;
|
|
|
|
if (buffer[i] == '\n')
|
|
|
|
offset = i + 1;
|
|
|
|
}
|
|
|
|
if (displacement)
|
|
|
|
*displacement = index - offset;
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_line_length(size_t index, const char *buffer) {
|
|
|
|
size_t i;
|
|
|
|
for (i = index; buffer[i] != '\n' && buffer[i] != 0; i++);
|
|
|
|
return i - index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_next_line(size_t index, const char *buffer) {
|
|
|
|
if (buffer[index] == 0)
|
|
|
|
return index;
|
|
|
|
size_t displacement;
|
|
|
|
get_line_offset(&displacement, index, buffer);
|
2020-12-19 00:38:18 +03:00
|
|
|
while (buffer[index] != '\n') {
|
|
|
|
if (buffer[index] == 0)
|
|
|
|
return index;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
index++;
|
2020-11-18 02:13:54 +03:00
|
|
|
size_t next_line_length = get_line_length(index, buffer);
|
|
|
|
if (displacement > next_line_length)
|
|
|
|
displacement = next_line_length;
|
|
|
|
return index + displacement;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t get_prev_line(size_t index, const char *buffer) {
|
|
|
|
size_t offset, displacement, prev_line_offset, prev_line_length;
|
|
|
|
offset = get_line_offset(&displacement, index, buffer);
|
|
|
|
if (offset) {
|
|
|
|
prev_line_offset = get_line_offset(NULL, offset - 1, buffer);
|
|
|
|
prev_line_length = get_line_length(prev_line_offset, buffer);
|
|
|
|
if (displacement > prev_line_length)
|
|
|
|
displacement = prev_line_length;
|
|
|
|
return prev_line_offset + displacement;
|
|
|
|
}
|
|
|
|
return offset;
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
static const char *VALID_KEYS[] = {
|
2021-08-17 01:58:02 +03:00
|
|
|
"COMMENT",
|
2021-07-07 02:27:07 +03:00
|
|
|
"PROTOCOL",
|
|
|
|
"CMDLINE",
|
|
|
|
"KERNEL_CMDLINE",
|
|
|
|
"KERNEL_PATH",
|
2021-11-02 09:59:04 +03:00
|
|
|
"INITRD_PATH",
|
2021-07-07 02:27:07 +03:00
|
|
|
"MODULE_PATH",
|
|
|
|
"MODULE_STRING",
|
2022-03-16 08:49:41 +03:00
|
|
|
"MODULE_CMDLINE",
|
2021-07-07 02:27:07 +03:00
|
|
|
"RESOLUTION",
|
2021-08-17 01:58:02 +03:00
|
|
|
"TEXTMODE",
|
2021-07-07 02:27:07 +03:00
|
|
|
"KASLR",
|
|
|
|
"DRIVE",
|
|
|
|
"PARTITION",
|
|
|
|
"IMAGE_PATH",
|
2022-08-15 19:40:52 +03:00
|
|
|
"TERM_CONFIG_OVERRIDE",
|
2022-10-06 06:36:02 +03:00
|
|
|
"TERM_PALETTE",
|
|
|
|
"TERM_PALETTE_BRIGHT",
|
|
|
|
"TERM_BACKGROUND",
|
|
|
|
"TERM_FOREGROUND",
|
|
|
|
"TERM_WALLPAPER",
|
|
|
|
"TERM_MARGIN",
|
|
|
|
"TERM_MARGIN_GRADIENT",
|
|
|
|
"TERM_WALLPAPER_STYLE",
|
|
|
|
"TERM_BACKDROP",
|
|
|
|
"TERM_FONT_SIZE",
|
|
|
|
"TERM_FONT",
|
|
|
|
"TERM_FONT_SPACING",
|
|
|
|
"TERM_FONT_SCALE",
|
2021-07-07 02:27:07 +03:00
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool validation_enabled = true;
|
|
|
|
static bool invalid_syntax = false;
|
|
|
|
|
|
|
|
static int validate_line(const char *buffer) {
|
|
|
|
if (!validation_enabled) return TOK_KEY;
|
2021-07-08 18:28:43 +03:00
|
|
|
if (buffer[0] == '#')
|
|
|
|
return TOK_COMMENT;
|
2021-07-07 02:27:07 +03:00
|
|
|
char keybuf[64];
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t i;
|
2021-07-07 02:27:07 +03:00
|
|
|
for (i = 0; buffer[i] && i < 64; i++) {
|
|
|
|
if (buffer[i] == '=') goto found_equals;
|
|
|
|
keybuf[i] = buffer[i];
|
|
|
|
}
|
|
|
|
fail:
|
|
|
|
if (i < 64) keybuf[i] = 0;
|
|
|
|
if (keybuf[0] == '\n' || (!keybuf[0] && buffer[0] != '=')) return TOK_KEY; // blank line is valid
|
2021-07-07 02:56:01 +03:00
|
|
|
invalid_syntax = true;
|
2021-07-07 02:27:07 +03:00
|
|
|
return TOK_BADKEY;
|
|
|
|
found_equals:
|
|
|
|
if (i < 64) keybuf[i] = 0;
|
|
|
|
for (i = 0; VALID_KEYS[i]; i++) {
|
|
|
|
if (!strcmp(keybuf, VALID_KEYS[i])) {
|
|
|
|
return TOK_KEY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2021-09-25 05:52:18 +03:00
|
|
|
static void putchar_tokencol(int type, char c) {
|
|
|
|
switch (type) {
|
|
|
|
case TOK_KEY:
|
|
|
|
print("\e[36m%c\e[0m", c);
|
|
|
|
break;
|
|
|
|
case TOK_EQUALS:
|
|
|
|
print("\e[32m%c\e[0m", c);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case TOK_VALUE:
|
|
|
|
print("\e[39m%c\e[0m", c);
|
|
|
|
break;
|
|
|
|
case TOK_BADKEY:
|
|
|
|
print("\e[31m%c\e[0m", c);
|
|
|
|
break;
|
|
|
|
case TOK_COMMENT:
|
|
|
|
print("\e[33m%c\e[0m", c);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-18 22:51:07 +03:00
|
|
|
static bool editor_no_term_reset = false;
|
|
|
|
|
2021-12-12 00:22:48 +03:00
|
|
|
char *config_entry_editor(const char *title, const char *orig_entry) {
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->autoflush = false);
|
2021-12-12 00:22:48 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->cursor_enabled = true);
|
2021-12-12 00:22:48 +03:00
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
print("\e[2J\e[H");
|
2021-12-12 00:22:48 +03:00
|
|
|
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t cursor_offset = 0;
|
|
|
|
size_t entry_size = strlen(orig_entry);
|
2023-02-16 06:11:53 +03:00
|
|
|
size_t _window_size = terms[0]->rows - 8;
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t window_offset = 0;
|
2023-02-16 06:11:53 +03:00
|
|
|
size_t line_size = terms[0]->cols - 2;
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 20:28:11 +03:00
|
|
|
bool display_overflow_error = false;
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
// Skip leading newlines
|
|
|
|
while (*orig_entry == '\n') {
|
|
|
|
orig_entry++;
|
|
|
|
entry_size--;
|
|
|
|
}
|
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
if (entry_size >= EDITOR_MAX_BUFFER_SIZE) {
|
2021-12-11 21:58:00 +03:00
|
|
|
panic(true, "Entry is too big to be edited.");
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
bool syntax_highlighting_enabled = true;
|
|
|
|
char *syntax_highlighting_enabled_config = config_get_value(NULL, 0, "EDITOR_HIGHLIGHTING");
|
2022-04-22 17:42:30 +03:00
|
|
|
if (syntax_highlighting_enabled_config != NULL
|
|
|
|
&& strcmp(syntax_highlighting_enabled_config, "no") == 0) {
|
|
|
|
syntax_highlighting_enabled = false;
|
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
validation_enabled = true;
|
|
|
|
char *validation_enabled_config = config_get_value(NULL, 0, "EDITOR_VALIDATION");
|
2022-04-22 17:42:30 +03:00
|
|
|
if (validation_enabled_config != NULL
|
|
|
|
&& strcmp(validation_enabled_config, "no") == 0) {
|
|
|
|
validation_enabled = false;
|
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-10-21 03:37:05 +03:00
|
|
|
char *buffer = ext_mem_alloc(EDITOR_MAX_BUFFER_SIZE);
|
2020-11-18 02:13:54 +03:00
|
|
|
memcpy(buffer, orig_entry, entry_size);
|
|
|
|
buffer[entry_size] = 0;
|
|
|
|
|
|
|
|
refresh:
|
2021-07-07 02:56:01 +03:00
|
|
|
invalid_syntax = false;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
print("\e[2J\e[H");
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->cursor_enabled = false);
|
2021-07-07 05:23:34 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n");
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
set_cursor_pos_helper(terms[0]->cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2), y);
|
2021-09-06 08:31:57 +03:00
|
|
|
print("\e[3%sm%s\e[37m", menu_branding_colour, menu_branding);
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n\n");
|
|
|
|
}
|
2021-07-07 02:27:07 +03:00
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
print(" \e[32mESC\e[0m Discard and Exit \e[32mF10\e[0m Boot\n\n");
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "/" : "┌");
|
2023-02-16 06:11:53 +03:00
|
|
|
for (size_t i = 0; i < terms[0]->cols - 2; i++) {
|
2020-12-19 01:01:03 +03:00
|
|
|
switch (i) {
|
|
|
|
case 1: case 2: case 3:
|
|
|
|
if (window_offset > 0) {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "^" : "↑");
|
2020-12-19 01:01:03 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLTHRU
|
2021-07-07 02:56:01 +03:00
|
|
|
default: {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t title_length = strlen(title);
|
2023-02-16 06:11:53 +03:00
|
|
|
if (i == (terms[0]->cols / 2) - DIV_ROUNDUP(title_length, 2) - 1) {
|
2021-07-07 02:56:01 +03:00
|
|
|
print("%s", title);
|
|
|
|
i += title_length - 1;
|
2021-07-07 02:27:07 +03:00
|
|
|
} else {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "-" : "─");
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2021-07-07 02:56:01 +03:00
|
|
|
}
|
2020-12-19 01:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
2022-02-07 04:10:57 +03:00
|
|
|
size_t tmpx, tmpy;
|
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &tmpx, &tmpy);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "┐");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, tmpy + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t cursor_x, cursor_y;
|
2020-12-19 19:10:28 +03:00
|
|
|
size_t current_line = 0, line_offset = 0, window_size = _window_size;
|
2020-12-19 00:38:18 +03:00
|
|
|
bool printed_cursor = false;
|
2021-09-25 05:52:18 +03:00
|
|
|
bool printed_early = false;
|
2021-07-07 02:27:07 +03:00
|
|
|
int token_type = validate_line(buffer);
|
2020-11-18 02:13:54 +03:00
|
|
|
for (size_t i = 0; ; i++) {
|
2021-07-07 02:27:07 +03:00
|
|
|
// newline
|
2020-12-19 00:38:18 +03:00
|
|
|
if (buffer[i] == '\n'
|
|
|
|
&& current_line < window_offset + window_size
|
|
|
|
&& current_line >= window_offset) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
2020-12-19 00:38:18 +03:00
|
|
|
if (i == cursor_offset) {
|
|
|
|
cursor_x = x;
|
|
|
|
cursor_y = y;
|
|
|
|
printed_cursor = true;
|
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(terms[0]->cols - 1, y);
|
2022-02-07 04:10:57 +03:00
|
|
|
if (current_line == window_offset + window_size - 1) {
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &tmpx, &tmpy);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, tmpy + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "└");
|
2022-02-07 04:10:57 +03:00
|
|
|
} else {
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &tmpx, &tmpy);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, tmpy + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2022-02-07 04:10:57 +03:00
|
|
|
}
|
2020-12-19 19:10:28 +03:00
|
|
|
line_offset = 0;
|
2021-09-25 05:52:18 +03:00
|
|
|
token_type = validate_line(buffer + i + 1);
|
2020-12-19 00:38:18 +03:00
|
|
|
current_line++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-09-25 05:52:18 +03:00
|
|
|
// switch to token type 1 if equals sign
|
|
|
|
if (token_type == TOK_KEY && buffer[i] == '=') token_type = TOK_EQUALS;
|
|
|
|
|
|
|
|
if (buffer[i] != 0 && line_offset % line_size == line_size - 1) {
|
|
|
|
if (current_line < window_offset + window_size
|
|
|
|
&& current_line >= window_offset) {
|
2021-12-10 01:37:59 +03:00
|
|
|
if (i == cursor_offset) {
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &cursor_x, &cursor_y);
|
2021-12-10 01:37:59 +03:00
|
|
|
printed_cursor = true;
|
|
|
|
}
|
2021-09-25 05:52:18 +03:00
|
|
|
if (syntax_highlighting_enabled) {
|
|
|
|
putchar_tokencol(token_type, buffer[i]);
|
|
|
|
} else {
|
|
|
|
print("%c", buffer[i]);
|
|
|
|
}
|
|
|
|
printed_early = true;
|
|
|
|
size_t x, y;
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
if (y == terms[0]->rows - 3) {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? ">" : "→");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, y + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "└");
|
2022-02-07 04:10:57 +03:00
|
|
|
} else {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? ">" : "→");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, y + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "<" : "←");
|
2022-02-07 04:10:57 +03:00
|
|
|
}
|
2021-09-25 05:52:18 +03:00
|
|
|
}
|
2020-12-19 19:10:28 +03:00
|
|
|
window_size--;
|
|
|
|
}
|
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
if (i == cursor_offset
|
|
|
|
&& current_line < window_offset + window_size
|
2021-12-10 01:37:59 +03:00
|
|
|
&& current_line >= window_offset
|
|
|
|
&& !printed_cursor) {
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &cursor_x, &cursor_y);
|
2020-12-19 00:38:18 +03:00
|
|
|
printed_cursor = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == 0 || current_line >= window_offset + window_size) {
|
|
|
|
if (!printed_cursor) {
|
|
|
|
if (i <= cursor_offset) {
|
|
|
|
window_offset++;
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
if (i > cursor_offset) {
|
|
|
|
window_offset--;
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
break;
|
2020-12-19 00:38:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (buffer[i] == '\n') {
|
2020-12-19 19:10:28 +03:00
|
|
|
line_offset = 0;
|
2021-09-25 05:52:18 +03:00
|
|
|
token_type = validate_line(buffer + i + 1);
|
2020-12-19 00:38:18 +03:00
|
|
|
current_line++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-12-19 19:10:28 +03:00
|
|
|
if (current_line >= window_offset) {
|
|
|
|
line_offset++;
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
// syntax highlighting
|
2021-09-25 05:52:18 +03:00
|
|
|
if (!printed_early) {
|
|
|
|
if (syntax_highlighting_enabled) {
|
|
|
|
putchar_tokencol(token_type, buffer[i]);
|
|
|
|
} else {
|
|
|
|
print("%c", buffer[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printed_early = false;
|
|
|
|
|
|
|
|
// switch to token type 2 after equals sign
|
|
|
|
if (token_type == TOK_EQUALS) token_type = TOK_VALUE;
|
2020-12-19 19:10:28 +03:00
|
|
|
}
|
2020-12-19 00:38:18 +03:00
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
// syntax error alert
|
|
|
|
if (validation_enabled) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
set_cursor_pos_helper(0, terms[0]->rows - 1);
|
|
|
|
FOR_TERM(TERM->scroll_enabled = false);
|
2021-07-07 02:27:07 +03:00
|
|
|
if (invalid_syntax) {
|
|
|
|
print("\e[31mConfiguration is INVALID.\e[0m");
|
|
|
|
} else {
|
|
|
|
print("\e[32mConfiguration is valid.\e[0m");
|
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = true);
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(x, y);
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 00:38:18 +03:00
|
|
|
if (current_line - window_offset < window_size) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2020-12-19 00:38:18 +03:00
|
|
|
for (size_t i = 0; i < (window_size - (current_line - window_offset)) - 1; i++) {
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
set_cursor_pos_helper(terms[0]->cols - 1, y);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, y + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2020-12-19 00:38:18 +03:00
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
set_cursor_pos_helper(terms[0]->cols - 1, y);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, y + 1);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "└");
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
for (size_t i = 0; i < terms[0]->cols - 2; i++) {
|
2020-12-19 01:01:03 +03:00
|
|
|
switch (i) {
|
|
|
|
case 1: case 2: case 3:
|
|
|
|
if (current_line - window_offset >= window_size) {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "v" : "↓");
|
2020-12-19 01:01:03 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
// FALLTHRU
|
|
|
|
default:
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "-" : "─");
|
2020-12-19 01:01:03 +03:00
|
|
|
}
|
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &tmpx, &tmpy);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "/" : "┘");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, tmpy + 1);
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2020-12-19 20:28:11 +03:00
|
|
|
if (display_overflow_error) {
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = false);
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\e[31mText buffer not big enough, delete something instead.");
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = true);
|
2020-12-19 20:28:11 +03:00
|
|
|
display_overflow_error = false;
|
|
|
|
}
|
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
// Hack to redraw the cursor
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(cursor_x, cursor_y);
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->cursor_enabled = true);
|
2020-11-18 02:13:54 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->double_buffer_flush(TERM));
|
2020-11-20 21:55:18 +03:00
|
|
|
|
2020-11-18 02:13:54 +03:00
|
|
|
int c = getchar();
|
2022-06-19 13:57:05 +03:00
|
|
|
size_t buffer_len = strlen(buffer);
|
2020-11-18 02:13:54 +03:00
|
|
|
switch (c) {
|
|
|
|
case GETCHAR_CURSOR_DOWN:
|
|
|
|
cursor_offset = get_next_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_UP:
|
|
|
|
cursor_offset = get_prev_line(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_LEFT:
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_CURSOR_RIGHT:
|
2022-06-19 13:57:05 +03:00
|
|
|
if (cursor_offset < buffer_len) {
|
2020-11-18 02:13:54 +03:00
|
|
|
cursor_offset++;
|
|
|
|
}
|
|
|
|
break;
|
2020-12-19 21:27:10 +03:00
|
|
|
case GETCHAR_HOME: {
|
|
|
|
size_t displacement;
|
|
|
|
get_line_offset(&displacement, cursor_offset, buffer);
|
|
|
|
cursor_offset -= displacement;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case GETCHAR_END: {
|
|
|
|
cursor_offset += get_line_length(cursor_offset, buffer);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-18 02:13:54 +03:00
|
|
|
case '\b':
|
|
|
|
if (cursor_offset) {
|
|
|
|
cursor_offset--;
|
|
|
|
case GETCHAR_DELETE:
|
|
|
|
for (size_t i = cursor_offset; ; i++) {
|
|
|
|
buffer[i] = buffer[i+1];
|
|
|
|
if (!buffer[i])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case GETCHAR_F10:
|
2021-12-18 22:51:07 +03:00
|
|
|
editor_no_term_reset ? editor_no_term_reset = false : reset_term();
|
2020-11-18 02:13:54 +03:00
|
|
|
return buffer;
|
2021-05-19 09:13:15 +03:00
|
|
|
case GETCHAR_ESCAPE:
|
2021-10-21 03:37:05 +03:00
|
|
|
pmm_free(buffer, EDITOR_MAX_BUFFER_SIZE);
|
2021-12-18 22:51:07 +03:00
|
|
|
editor_no_term_reset ? editor_no_term_reset = false : reset_term();
|
2020-12-15 09:45:59 +03:00
|
|
|
return NULL;
|
2020-11-18 02:13:54 +03:00
|
|
|
default:
|
2022-06-19 13:57:05 +03:00
|
|
|
if (buffer_len < EDITOR_MAX_BUFFER_SIZE - 1) {
|
2022-01-29 01:51:34 +03:00
|
|
|
if (isprint(c) || c == '\n') {
|
2022-06-19 13:57:05 +03:00
|
|
|
for (size_t i = buffer_len; ; i--) {
|
2022-01-29 01:51:34 +03:00
|
|
|
buffer[i+1] = buffer[i];
|
|
|
|
if (i == cursor_offset)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
buffer[cursor_offset++] = c;
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
2020-12-19 20:28:11 +03:00
|
|
|
} else {
|
|
|
|
display_overflow_error = true;
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
goto refresh;
|
|
|
|
}
|
|
|
|
|
2022-10-07 08:35:08 +03:00
|
|
|
static size_t print_tree(size_t offset, size_t window, const char *shift, size_t level, size_t base_index, size_t selected_entry,
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *current_entry,
|
|
|
|
struct menu_entry **selected_menu_entry) {
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t max_entries = 0;
|
2020-12-07 19:14:14 +03:00
|
|
|
|
2021-07-08 16:47:28 +03:00
|
|
|
bool no_print = false;
|
|
|
|
if (shift == NULL) {
|
|
|
|
no_print = true;
|
|
|
|
}
|
|
|
|
|
2020-11-17 01:31:03 +03:00
|
|
|
for (;;) {
|
|
|
|
if (current_entry == NULL)
|
|
|
|
break;
|
2022-10-07 08:35:08 +03:00
|
|
|
if (!no_print && base_index + max_entries < offset) {
|
|
|
|
goto skip_line;
|
|
|
|
}
|
|
|
|
if (!no_print && base_index + max_entries >= offset + window) {
|
|
|
|
goto skip_line;
|
|
|
|
}
|
2021-07-08 16:47:28 +03:00
|
|
|
if (!no_print) print("%s", shift);
|
2020-11-17 02:50:26 +03:00
|
|
|
if (level) {
|
2021-08-17 01:00:23 +03:00
|
|
|
for (size_t i = level - 1; i > 0; i--) {
|
2020-11-17 02:50:26 +03:00
|
|
|
struct menu_entry *actual_parent = current_entry;
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t j = 0; j < i; j++)
|
2020-11-17 02:50:26 +03:00
|
|
|
actual_parent = actual_parent->parent;
|
2021-07-08 16:47:28 +03:00
|
|
|
if (actual_parent->next != NULL) {
|
2023-03-30 02:23:55 +03:00
|
|
|
if (!no_print) print(serial ? " |" : " │");
|
2021-07-08 16:47:28 +03:00
|
|
|
} else {
|
|
|
|
if (!no_print) print(" ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (current_entry->next == NULL) {
|
2023-03-30 02:23:55 +03:00
|
|
|
if (!no_print) print(serial ? " `" : " └");
|
2021-07-08 16:47:28 +03:00
|
|
|
} else {
|
2023-03-30 02:23:55 +03:00
|
|
|
if (!no_print) print(serial ? " |" : " ├");
|
2020-11-17 02:50:26 +03:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 16:47:28 +03:00
|
|
|
if (current_entry->sub) {
|
|
|
|
if (!no_print) print(current_entry->expanded ? "[-]" : "[+]");
|
|
|
|
} else if (level) {
|
2023-03-30 02:23:55 +03:00
|
|
|
if (!no_print) print(serial ? "-> " : "─> ");
|
2021-07-08 16:47:28 +03:00
|
|
|
} else {
|
|
|
|
if (!no_print) print(" ");
|
|
|
|
}
|
2020-11-17 01:31:03 +03:00
|
|
|
if (base_index + max_entries == selected_entry) {
|
|
|
|
*selected_menu_entry = current_entry;
|
2022-03-02 16:38:27 +03:00
|
|
|
if (!no_print) print("\e[7m");
|
2020-11-17 01:31:03 +03:00
|
|
|
}
|
2022-03-02 16:38:27 +03:00
|
|
|
if (!no_print) print(" %s \e[27m\n", current_entry->name);
|
2022-10-07 08:35:08 +03:00
|
|
|
skip_line:
|
2020-11-17 01:31:03 +03:00
|
|
|
if (current_entry->sub && current_entry->expanded) {
|
2022-10-07 08:35:08 +03:00
|
|
|
max_entries += print_tree(offset, window, shift, level + 1, base_index + max_entries + 1,
|
2020-11-17 01:31:03 +03:00
|
|
|
selected_entry,
|
|
|
|
current_entry->sub,
|
|
|
|
selected_menu_entry);
|
|
|
|
}
|
|
|
|
max_entries++;
|
|
|
|
current_entry = current_entry->next;
|
|
|
|
}
|
|
|
|
return max_entries;
|
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2022-08-13 20:54:49 +03:00
|
|
|
static struct memmap_entry *rewound_memmap = NULL;
|
2021-12-11 21:58:00 +03:00
|
|
|
static size_t rewound_memmap_entries = 0;
|
2022-09-17 16:09:08 +03:00
|
|
|
static no_unwind uint8_t *rewound_data;
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2022-09-17 16:09:08 +03:00
|
|
|
static no_unwind uint8_t *rewound_s2_data;
|
2021-12-19 03:08:34 +03:00
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
|
2021-12-12 20:40:27 +03:00
|
|
|
extern symbol data_begin;
|
|
|
|
extern symbol data_end;
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-12-19 03:08:34 +03:00
|
|
|
extern symbol s2_data_begin;
|
|
|
|
extern symbol s2_data_end;
|
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
|
2022-09-20 16:25:24 +03:00
|
|
|
static void menu_init_term(void) {
|
|
|
|
// If there is GRAPHICS config key and the value is "yes", enable graphics
|
|
|
|
#if defined (BIOS)
|
|
|
|
char *graphics = config_get_value(NULL, 0, "GRAPHICS");
|
|
|
|
#elif defined (UEFI)
|
|
|
|
char *graphics = "yes";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (graphics == NULL || strcmp(graphics, "no") != 0) {
|
|
|
|
size_t req_width = 0, req_height = 0, req_bpp = 0;
|
|
|
|
|
|
|
|
char *menu_resolution = config_get_value(NULL, 0, "INTERFACE_RESOLUTION");
|
|
|
|
if (menu_resolution != NULL)
|
|
|
|
parse_resolution(&req_width, &req_height, &req_bpp, menu_resolution);
|
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
if (!quiet && !gterm_init(NULL, NULL, NULL, req_width, req_height)) {
|
2022-10-04 09:21:59 +03:00
|
|
|
#if defined (BIOS)
|
|
|
|
vga_textmode_init(true);
|
|
|
|
#elif defined (UEFI)
|
2022-10-05 15:21:23 +03:00
|
|
|
term_fallback();
|
2022-10-04 09:21:59 +03:00
|
|
|
#endif
|
|
|
|
}
|
2022-09-20 16:25:24 +03:00
|
|
|
} else {
|
|
|
|
#if defined (BIOS)
|
2023-04-27 00:49:22 +03:00
|
|
|
if (!quiet) {
|
|
|
|
vga_textmode_init(true);
|
|
|
|
}
|
2022-09-20 16:25:24 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-18 12:52:31 +03:00
|
|
|
noreturn void _menu(bool first_run) {
|
2021-12-12 20:40:27 +03:00
|
|
|
size_t data_size = (uintptr_t)data_end - (uintptr_t)data_begin;
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-12-19 03:08:34 +03:00
|
|
|
size_t s2_data_size = (uintptr_t)s2_data_end - (uintptr_t)s2_data_begin;
|
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
|
|
|
|
if (rewound_memmap != NULL) {
|
2021-12-12 20:40:27 +03:00
|
|
|
memcpy(data_begin, rewound_data, data_size);
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-12-19 03:08:34 +03:00
|
|
|
memcpy(s2_data_begin, rewound_s2_data, s2_data_size);
|
|
|
|
#endif
|
2022-08-13 20:54:49 +03:00
|
|
|
memcpy(memmap, rewound_memmap, rewound_memmap_entries * sizeof(struct memmap_entry));
|
2021-12-11 21:58:00 +03:00
|
|
|
memmap_entries = rewound_memmap_entries;
|
|
|
|
} else {
|
2021-12-12 20:40:27 +03:00
|
|
|
rewound_data = ext_mem_alloc(data_size);
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-12-19 03:08:34 +03:00
|
|
|
rewound_s2_data = ext_mem_alloc(s2_data_size);
|
|
|
|
#endif
|
2022-08-13 20:54:49 +03:00
|
|
|
rewound_memmap = ext_mem_alloc(256 * sizeof(struct memmap_entry));
|
|
|
|
memcpy(rewound_memmap, memmap, memmap_entries * sizeof(struct memmap_entry));
|
2021-12-11 21:58:00 +03:00
|
|
|
rewound_memmap_entries = memmap_entries;
|
2021-12-12 20:40:27 +03:00
|
|
|
memcpy(rewound_data, data_begin, data_size);
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2021-12-19 03:08:34 +03:00
|
|
|
memcpy(rewound_s2_data, s2_data_begin, s2_data_size);
|
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
}
|
|
|
|
|
2023-02-16 09:18:04 +03:00
|
|
|
term_fallback();
|
|
|
|
|
2021-12-12 20:48:36 +03:00
|
|
|
if (bad_config == false) {
|
2022-09-22 16:12:11 +03:00
|
|
|
#if defined (UEFI)
|
|
|
|
if (init_config_disk(boot_volume)) {
|
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
volume_iterate_parts(boot_volume,
|
|
|
|
if (!init_config_disk(_PART)) {
|
|
|
|
boot_volume = _PART;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
);
|
2022-09-22 16:12:11 +03:00
|
|
|
#if defined (UEFI)
|
|
|
|
}
|
|
|
|
#endif
|
2021-12-11 21:58:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
char *quiet_str = config_get_value(NULL, 0, "QUIET");
|
|
|
|
quiet = quiet_str != NULL && strcmp(quiet_str, "yes") == 0;
|
|
|
|
|
|
|
|
char *verbose_str = config_get_value(NULL, 0, "VERBOSE");
|
|
|
|
verbose = verbose_str != NULL && strcmp(verbose_str, "yes") == 0;
|
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
char *serial_str = config_get_value(NULL, 0, "SERIAL");
|
|
|
|
serial = serial_str != NULL && strcmp(serial_str, "yes") == 0;
|
|
|
|
|
2022-09-11 21:47:57 +03:00
|
|
|
char *hash_mismatch_panic_str = config_get_value(NULL, 0, "HASH_MISMATCH_PANIC");
|
|
|
|
hash_mismatch_panic = hash_mismatch_panic_str == NULL || strcmp(hash_mismatch_panic_str, "yes") == 0;
|
|
|
|
|
2022-03-28 00:33:34 +03:00
|
|
|
char *randomise_mem_str = config_get_value(NULL, 0, "RANDOMISE_MEMORY");
|
|
|
|
if (randomise_mem_str == NULL)
|
|
|
|
randomise_mem_str = config_get_value(NULL, 0, "RANDOMIZE_MEMORY");
|
|
|
|
bool randomise_mem = randomise_mem_str != NULL && strcmp(randomise_mem_str, "yes") == 0;
|
2022-03-28 08:18:09 +03:00
|
|
|
if (randomise_mem) {
|
2022-03-28 00:33:34 +03:00
|
|
|
pmm_randomise_memory();
|
2022-03-28 08:18:09 +03:00
|
|
|
}
|
2022-03-28 00:33:34 +03:00
|
|
|
|
2022-03-02 15:31:09 +03:00
|
|
|
char *editor_enabled_str = config_get_value(NULL, 0, "EDITOR_ENABLED");
|
|
|
|
if (editor_enabled_str != NULL) {
|
|
|
|
editor_enabled = strcmp(editor_enabled_str, "yes") == 0;
|
|
|
|
}
|
|
|
|
|
2022-03-22 06:39:51 +03:00
|
|
|
menu_branding = config_get_value(NULL, 0, "INTERFACE_BRANDING");
|
2020-12-10 09:32:15 +03:00
|
|
|
if (menu_branding == NULL)
|
|
|
|
menu_branding = "Limine " LIMINE_VERSION;
|
|
|
|
|
2022-03-22 06:39:51 +03:00
|
|
|
menu_branding_colour = config_get_value(NULL, 0, "INTERFACE_BRANDING_COLOUR");
|
2021-09-06 08:31:57 +03:00
|
|
|
if (menu_branding_colour == NULL)
|
2022-03-22 06:39:51 +03:00
|
|
|
menu_branding_colour = config_get_value(NULL, 0, "INTERFACE_BRANDING_COLOR");
|
2021-09-06 08:31:57 +03:00
|
|
|
if (menu_branding_colour == NULL)
|
|
|
|
menu_branding_colour = "6";
|
|
|
|
|
2020-12-02 21:16:38 +03:00
|
|
|
bool skip_timeout = false;
|
2020-12-03 14:38:53 +03:00
|
|
|
struct menu_entry *selected_menu_entry = NULL;
|
2020-11-17 01:31:03 +03:00
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t selected_entry = 0;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *default_entry = config_get_value(NULL, 0, "DEFAULT_ENTRY");
|
|
|
|
if (default_entry != NULL) {
|
|
|
|
selected_entry = strtoui(default_entry, NULL, 10);
|
2020-12-03 14:38:53 +03:00
|
|
|
if (selected_entry)
|
|
|
|
selected_entry--;
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
|
|
|
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t timeout = 5;
|
2020-11-27 21:33:34 +03:00
|
|
|
char *timeout_config = config_get_value(NULL, 0, "TIMEOUT");
|
|
|
|
if (timeout_config != NULL) {
|
2020-12-03 14:38:53 +03:00
|
|
|
if (!strcmp(timeout_config, "no"))
|
|
|
|
skip_timeout = true;
|
|
|
|
else
|
|
|
|
timeout = strtoui(timeout_config, NULL, 10);
|
2020-09-29 22:48:34 +03:00
|
|
|
}
|
2021-07-07 03:43:07 +03:00
|
|
|
|
2022-09-18 12:52:31 +03:00
|
|
|
if (!first_run) {
|
2021-11-29 00:30:05 +03:00
|
|
|
skip_timeout = true;
|
|
|
|
}
|
|
|
|
|
2021-12-11 22:28:16 +03:00
|
|
|
if (!skip_timeout && !timeout) {
|
2020-12-03 14:38:53 +03:00
|
|
|
// Use print tree to load up selected_menu_entry and determine if the
|
|
|
|
// default entry is valid.
|
2022-10-07 08:35:08 +03:00
|
|
|
print_tree(0, 0, NULL, 0, 0, selected_entry, menu_tree, &selected_menu_entry);
|
2020-12-03 14:38:53 +03:00
|
|
|
if (selected_menu_entry == NULL || selected_menu_entry->sub != NULL) {
|
2022-09-17 11:40:14 +03:00
|
|
|
quiet = false;
|
2020-12-03 14:38:53 +03:00
|
|
|
print("Default entry is not valid or directory, booting to menu.\n");
|
|
|
|
skip_timeout = true;
|
|
|
|
} else {
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 22:48:34 +03:00
|
|
|
|
2022-09-20 16:25:24 +03:00
|
|
|
menu_init_term();
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2022-10-07 08:35:08 +03:00
|
|
|
size_t tree_offset = 0;
|
|
|
|
|
2021-10-09 14:32:57 +03:00
|
|
|
refresh:
|
2023-02-16 06:11:53 +03:00
|
|
|
if (selected_entry >= tree_offset + terms[0]->rows - 10) {
|
|
|
|
tree_offset = selected_entry - (terms[0]->rows - 11);
|
2022-10-07 08:35:08 +03:00
|
|
|
}
|
|
|
|
if (selected_entry < tree_offset) {
|
|
|
|
tree_offset = selected_entry;
|
|
|
|
}
|
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->autoflush = false);
|
2020-11-20 21:55:18 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->cursor_enabled = false);
|
2021-12-18 22:51:07 +03:00
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
print("\e[2J\e[H");
|
2021-07-07 05:23:34 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n");
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
|
|
|
set_cursor_pos_helper(terms[0]->cols / 2 - DIV_ROUNDUP(strlen(menu_branding), 2), y);
|
2021-09-06 08:31:57 +03:00
|
|
|
print("\e[3%sm%s\e[37m", menu_branding_colour, menu_branding);
|
2021-07-07 05:23:34 +03:00
|
|
|
print("\n\n\n\n");
|
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2021-12-12 00:22:48 +03:00
|
|
|
while (menu_tree == NULL) {
|
2022-09-20 16:25:24 +03:00
|
|
|
if (quiet) {
|
|
|
|
quiet = false;
|
|
|
|
menu_init_term();
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->autoflush = false);
|
|
|
|
FOR_TERM(TERM->cursor_enabled = false);
|
2022-09-20 16:25:24 +03:00
|
|
|
}
|
2021-04-20 07:45:03 +03:00
|
|
|
print("Config file %s.\n\n", config_ready ? "contains no valid entries" : "not found");
|
|
|
|
print("For information on the format of Limine config entries, consult CONFIG.md in\n");
|
|
|
|
print("the root of the Limine source repository.\n\n");
|
2021-12-12 00:22:48 +03:00
|
|
|
print("Press a key to enter the Limine console...");
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->double_buffer_flush(TERM));
|
2021-04-20 07:45:03 +03:00
|
|
|
getchar();
|
2021-12-12 00:22:48 +03:00
|
|
|
reset_term();
|
|
|
|
console();
|
2021-04-20 07:45:03 +03:00
|
|
|
}
|
|
|
|
|
2021-07-08 17:07:33 +03:00
|
|
|
{ // Draw box around boot menu
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
2021-07-08 17:07:33 +03:00
|
|
|
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "/" : "┌");
|
2023-02-16 06:11:53 +03:00
|
|
|
for (size_t i = 0; i < terms[0]->cols - 2; i++) {
|
2022-10-07 09:49:13 +03:00
|
|
|
switch (i) {
|
|
|
|
case 1: case 2: case 3:
|
|
|
|
if (tree_offset > 0) {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "^" : "↑"); break;
|
2022-10-07 09:49:13 +03:00
|
|
|
}
|
|
|
|
// FALLTHRU
|
|
|
|
default:
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "-" : "─"); break;
|
2022-10-07 09:49:13 +03:00
|
|
|
}
|
2021-07-08 17:07:33 +03:00
|
|
|
}
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "┐");
|
2021-07-08 17:07:33 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
for (size_t i = y + 1; i < terms[0]->rows - 2; i++) {
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, i);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(terms[0]->cols - 1, i);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "|" : "│");
|
2021-07-08 17:07:33 +03:00
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(0, terms[0]->rows - 2);
|
2021-07-08 17:07:33 +03:00
|
|
|
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "\\" : "└");
|
2023-02-16 06:11:53 +03:00
|
|
|
for (size_t i = 0; i < terms[0]->cols - 2; i++) {
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "-" : "─");
|
2021-07-08 17:07:33 +03:00
|
|
|
}
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "/" : "┘");
|
2021-07-08 17:07:33 +03:00
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(x, y + 2);
|
2021-07-08 17:07:33 +03:00
|
|
|
}
|
|
|
|
|
2023-03-30 02:23:55 +03:00
|
|
|
size_t max_entries = print_tree(tree_offset, terms[0]->rows - 10, serial ? "| " : "│ ", 0, 0, selected_entry, menu_tree,
|
2020-11-17 01:31:03 +03:00
|
|
|
&selected_menu_entry);
|
2020-06-05 21:27:52 +03:00
|
|
|
|
2021-07-07 02:27:07 +03:00
|
|
|
{
|
2021-08-16 19:02:28 +03:00
|
|
|
size_t x, y;
|
2023-02-16 06:11:53 +03:00
|
|
|
terms[0]->get_cursor_pos(terms[0], &x, &y);
|
2022-10-07 09:49:13 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
if (tree_offset + (terms[0]->rows - 10) < max_entries) {
|
|
|
|
set_cursor_pos_helper(2, terms[0]->rows - 2);
|
2023-03-30 02:23:55 +03:00
|
|
|
print(serial ? "vvv" : "↓↓↓");
|
2022-10-07 09:49:13 +03:00
|
|
|
}
|
|
|
|
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(0, 3);
|
2021-07-07 02:27:07 +03:00
|
|
|
if (editor_enabled && selected_menu_entry->sub == NULL) {
|
2021-07-07 05:23:34 +03:00
|
|
|
print(" \e[32mARROWS\e[0m Select \e[32mENTER\e[0m Boot \e[32mE\e[0m Edit");
|
2021-07-07 02:27:07 +03:00
|
|
|
} else {
|
2021-07-08 18:15:28 +03:00
|
|
|
print(" \e[32mARROWS\e[0m Select \e[32mENTER\e[0m %s",
|
|
|
|
selected_menu_entry->expanded ? "Collapse" : "Expand");
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(terms[0]->cols - 13, 3);
|
2021-10-09 14:32:57 +03:00
|
|
|
print("\e[32mC\e[0m Console");
|
2022-02-07 04:10:57 +03:00
|
|
|
set_cursor_pos_helper(x, y);
|
2021-07-07 02:27:07 +03:00
|
|
|
}
|
2020-12-02 21:16:38 +03:00
|
|
|
|
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
skip_timeout = true;
|
2020-09-28 23:39:28 +03:00
|
|
|
|
|
|
|
int c;
|
2020-06-05 21:27:52 +03:00
|
|
|
|
|
|
|
if (skip_timeout == false) {
|
2020-09-28 23:39:28 +03:00
|
|
|
print("\n\n");
|
2021-08-16 19:02:28 +03:00
|
|
|
for (size_t i = timeout; i; i--) {
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(0, terms[0]->rows - 1);
|
|
|
|
FOR_TERM(TERM->scroll_enabled = false);
|
2021-09-14 19:35:42 +03:00
|
|
|
print("\e[2K\e[32mBooting automatically in \e[92m%u\e[32m, press any key to stop the countdown...\e[0m", i);
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = true);
|
|
|
|
FOR_TERM(TERM->double_buffer_flush(TERM));
|
2021-03-04 15:48:31 +03:00
|
|
|
if ((c = pit_sleep_and_quit_on_keypress(1))) {
|
2020-06-05 21:27:52 +03:00
|
|
|
skip_timeout = true;
|
2022-09-20 16:25:24 +03:00
|
|
|
if (quiet) {
|
|
|
|
quiet = false;
|
|
|
|
menu_init_term();
|
|
|
|
goto timeout_aborted;
|
|
|
|
}
|
2022-09-17 11:40:14 +03:00
|
|
|
print("\e[2K");
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->double_buffer_flush(TERM));
|
2022-09-17 11:40:14 +03:00
|
|
|
goto timeout_aborted;
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
set_cursor_pos_helper(0, terms[0]->rows - 1);
|
2021-07-11 03:01:03 +03:00
|
|
|
if (selected_menu_entry->comment != NULL) {
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = false);
|
2021-07-08 19:05:17 +03:00
|
|
|
print("\e[36m%s\e[0m", selected_menu_entry->comment);
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->scroll_enabled = true);
|
2021-07-11 03:01:03 +03:00
|
|
|
}
|
2021-07-08 17:57:40 +03:00
|
|
|
|
2023-02-16 06:11:53 +03:00
|
|
|
FOR_TERM(TERM->double_buffer_flush(TERM));
|
2020-11-20 21:55:18 +03:00
|
|
|
|
2020-06-05 21:27:52 +03:00
|
|
|
for (;;) {
|
2020-09-28 23:39:28 +03:00
|
|
|
c = getchar();
|
|
|
|
timeout_aborted:
|
2020-06-05 21:27:52 +03:00
|
|
|
switch (c) {
|
2022-02-05 00:25:34 +03:00
|
|
|
case '1': case '2': case '3': case '4': case '5':
|
|
|
|
case '6': case '7': case '8': case '9': {
|
|
|
|
int ent = (c - '0') - 1;
|
|
|
|
if (ent < (int)max_entries) {
|
|
|
|
selected_entry = ent;
|
2022-10-07 08:35:08 +03:00
|
|
|
print_tree(0, 0, NULL, 0, 0, selected_entry, menu_tree,
|
2022-02-05 00:25:34 +03:00
|
|
|
&selected_menu_entry);
|
|
|
|
goto autoboot;
|
|
|
|
}
|
|
|
|
goto refresh;
|
|
|
|
}
|
2022-02-05 00:30:53 +03:00
|
|
|
case GETCHAR_HOME:
|
|
|
|
selected_entry = 0;
|
|
|
|
goto refresh;
|
|
|
|
case GETCHAR_END:
|
|
|
|
selected_entry = max_entries - 1;
|
|
|
|
goto refresh;
|
2020-06-05 21:27:52 +03:00
|
|
|
case GETCHAR_CURSOR_UP:
|
2021-08-16 19:02:28 +03:00
|
|
|
if (selected_entry == 0)
|
2020-06-05 21:27:52 +03:00
|
|
|
selected_entry = max_entries - 1;
|
2021-08-16 19:02:28 +03:00
|
|
|
else
|
|
|
|
selected_entry--;
|
2020-06-05 21:27:52 +03:00
|
|
|
goto refresh;
|
|
|
|
case GETCHAR_CURSOR_DOWN:
|
|
|
|
if (++selected_entry == max_entries)
|
|
|
|
selected_entry = 0;
|
|
|
|
goto refresh;
|
2021-03-26 15:56:09 +03:00
|
|
|
case GETCHAR_CURSOR_RIGHT:
|
2020-12-15 09:45:59 +03:00
|
|
|
case '\n':
|
2022-01-20 04:34:33 +03:00
|
|
|
case ' ':
|
2020-06-05 21:27:52 +03:00
|
|
|
autoboot:
|
2020-11-17 01:31:03 +03:00
|
|
|
if (selected_menu_entry->sub != NULL) {
|
|
|
|
selected_menu_entry->expanded = !selected_menu_entry->expanded;
|
|
|
|
goto refresh;
|
|
|
|
}
|
2022-10-04 20:10:38 +03:00
|
|
|
if (!quiet) {
|
|
|
|
if (term_backend == FALLBACK) {
|
2023-02-16 06:11:53 +03:00
|
|
|
if (!gterm_init(NULL, NULL, NULL, 0, 0)) {
|
2022-09-02 03:29:12 +03:00
|
|
|
#if defined (BIOS)
|
2022-10-04 20:10:38 +03:00
|
|
|
vga_textmode_init(true);
|
2022-10-04 09:21:59 +03:00
|
|
|
#elif defined (UEFI)
|
2022-10-05 15:21:23 +03:00
|
|
|
term_fallback();
|
2021-10-31 20:16:35 +03:00
|
|
|
#endif
|
2022-10-04 20:10:38 +03:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
reset_term();
|
2022-10-04 09:21:59 +03:00
|
|
|
}
|
2021-10-31 20:16:35 +03:00
|
|
|
}
|
2021-12-12 00:22:48 +03:00
|
|
|
boot(selected_menu_entry->body);
|
2022-01-29 01:10:44 +03:00
|
|
|
case 'e':
|
|
|
|
case 'E': {
|
2021-07-07 02:27:07 +03:00
|
|
|
if (editor_enabled) {
|
|
|
|
if (selected_menu_entry->sub != NULL)
|
|
|
|
goto refresh;
|
2021-12-18 22:51:07 +03:00
|
|
|
editor_no_term_reset = true;
|
2021-07-07 02:27:07 +03:00
|
|
|
char *new_body = config_entry_editor(selected_menu_entry->name, selected_menu_entry->body);
|
|
|
|
if (new_body == NULL)
|
|
|
|
goto refresh;
|
|
|
|
selected_menu_entry->body = new_body;
|
|
|
|
goto autoboot;
|
|
|
|
}
|
2021-10-09 14:32:57 +03:00
|
|
|
break;
|
|
|
|
}
|
2022-01-29 01:10:44 +03:00
|
|
|
case 'c':
|
|
|
|
case 'C': {
|
2021-10-09 14:32:57 +03:00
|
|
|
reset_term();
|
|
|
|
console();
|
|
|
|
goto refresh;
|
2020-11-18 02:13:54 +03:00
|
|
|
}
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|
|
|
|
}
|
2021-12-12 00:22:48 +03:00
|
|
|
}
|
2021-12-11 17:34:30 +03:00
|
|
|
|
2021-12-31 12:58:05 +03:00
|
|
|
noreturn void boot(char *config) {
|
2021-12-12 00:22:48 +03:00
|
|
|
char *cmdline = config_get_value(config, 0, "KERNEL_CMDLINE");
|
|
|
|
if (!cmdline) {
|
|
|
|
cmdline = config_get_value(config, 0, "CMDLINE");
|
|
|
|
}
|
|
|
|
if (!cmdline) {
|
|
|
|
cmdline = "";
|
|
|
|
}
|
2021-12-11 17:34:30 +03:00
|
|
|
|
|
|
|
char *proto = config_get_value(config, 0, "PROTOCOL");
|
|
|
|
if (proto == NULL) {
|
2022-06-15 03:12:27 +03:00
|
|
|
panic(true, "Boot protocol not specified for this entry");
|
2021-12-11 17:34:30 +03:00
|
|
|
}
|
|
|
|
|
2022-06-14 04:56:30 +03:00
|
|
|
if (!strcmp(proto, "stivale1") || !strcmp(proto, "stivale") || !strcmp(proto, "stivale2")) {
|
2022-09-17 11:40:14 +03:00
|
|
|
quiet = false;
|
2022-06-15 03:12:27 +03:00
|
|
|
print("The stivale and stivale2 protocols are no longer supported as of Limine 4.x\n");
|
2022-06-14 04:56:30 +03:00
|
|
|
print("Please notify kernel maintainers to move to the Limine boot protocol or\n");
|
|
|
|
print("roll back to Limine 3.x.\n\n");
|
2022-03-12 21:40:49 +03:00
|
|
|
} else if (!strcmp(proto, "limine")) {
|
2022-06-15 03:12:27 +03:00
|
|
|
limine_load(config, cmdline);
|
2021-12-11 17:34:30 +03:00
|
|
|
} else if (!strcmp(proto, "linux")) {
|
2022-08-18 18:32:54 +03:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
2022-06-15 03:12:27 +03:00
|
|
|
linux_load(config, cmdline);
|
2022-08-18 18:32:54 +03:00
|
|
|
#else
|
2022-09-17 11:40:14 +03:00
|
|
|
quiet = false;
|
2022-08-18 18:32:54 +03:00
|
|
|
print("TODO: Linux is not available on aarch64.\n\n");
|
|
|
|
#endif
|
2021-12-11 17:34:30 +03:00
|
|
|
} else if (!strcmp(proto, "multiboot1") || !strcmp(proto, "multiboot")) {
|
2022-08-18 18:32:54 +03:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
2022-06-15 03:12:27 +03:00
|
|
|
multiboot1_load(config, cmdline);
|
2022-08-18 18:32:54 +03:00
|
|
|
#else
|
2022-09-17 11:40:14 +03:00
|
|
|
quiet = false;
|
2022-08-18 18:32:54 +03:00
|
|
|
print("Multiboot 1 is not available on aarch64.\n\n");
|
|
|
|
#endif
|
2021-12-11 17:34:30 +03:00
|
|
|
} else if (!strcmp(proto, "multiboot2")) {
|
2022-08-18 18:32:54 +03:00
|
|
|
#if defined (__x86_64__) || defined (__i386__)
|
2022-06-15 03:12:27 +03:00
|
|
|
multiboot2_load(config, cmdline);
|
2022-08-18 18:32:54 +03:00
|
|
|
#else
|
2022-09-17 11:40:14 +03:00
|
|
|
quiet = false;
|
2022-08-18 18:32:54 +03:00
|
|
|
print("Multiboot 2 is not available on aarch64.\n\n");
|
|
|
|
#endif
|
2022-07-11 01:15:56 +03:00
|
|
|
} else if (!strcmp(proto, "chainload_next")) {
|
2023-01-14 14:49:22 +03:00
|
|
|
chainload_next(config, cmdline);
|
2021-12-11 17:34:30 +03:00
|
|
|
} else if (!strcmp(proto, "chainload")) {
|
2023-01-14 14:49:22 +03:00
|
|
|
chainload(config, cmdline);
|
2021-12-11 17:34:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-17 11:40:14 +03:00
|
|
|
panic(true, "Unsupported protocol specified for kernel.");
|
2020-06-05 21:27:52 +03:00
|
|
|
}
|