:: commit 5b95375d621e8679a1c627f01117afca600e4250

mintsuki <mintsuki@protonmail.com> — 2022-09-23 18:53

parents: a9a7dda214

term: Move more of it to stage 3

diff --git a/common/entry.s2.c b/common/entry.s2.c
index 03e1af9e..fa84d8df 100644
--- a/common/entry.s2.c
+++ b/common/entry.s2.c
@@ -80,8 +80,6 @@ enum {
 };
 
 noreturn void entry(uint8_t boot_drive, int boot_from) {
-    term_fallback();
-
     // XXX DO NOT MOVE A20 ENABLE CALL
     if (!a20_enable()) {
         panic(false, "Could not enable A20 line");
@@ -122,6 +120,8 @@ noreturn void entry(uint8_t boot_drive, int boot_from) {
         panic(false, "Failed to load stage 3.");
     }
 
+    term_fallback();
+
     stage3_common();
 }
 
diff --git a/common/lib/panic.s2.c b/common/lib/panic.s2.c
index 2ea7d4b3..e9eb3d25 100644
--- a/common/lib/panic.s2.c
+++ b/common/lib/panic.s2.c
@@ -21,11 +21,11 @@ noreturn void panic(bool allow_menu, const char *fmt, ...) {
 
     quiet = false;
 
-    if (term_backend == _NOT_READY) {
+    if (stage3_loaded && term_backend == _NOT_READY) {
         term_fallback();
     }
 
-    if (term_backend != FALLBACK) {
+    if (stage3_loaded && term_backend != FALLBACK) {
         print("\033[31mPANIC\033[37;1m\033[0m: ");
     } else {
         print("PANIC: ");
diff --git a/common/lib/term.c b/common/lib/term.c
index b027148d..bb8c40f5 100644
--- a/common/lib/term.c
+++ b/common/lib/term.c
@@ -10,6 +10,227 @@
 #include <lib/print.h>
 #include <mm/pmm.h>
 
+int current_video_mode = -1;
+int term_backend = _NOT_READY;
+size_t term_rows, term_cols;
+bool term_runtime = false;
+
+void (*raw_putchar)(uint8_t c);
+void (*clear)(bool move);
+void (*enable_cursor)(void);
+bool (*disable_cursor)(void);
+void (*set_cursor_pos)(size_t x, size_t y);
+void (*get_cursor_pos)(size_t *x, size_t *y);
+void (*set_text_fg)(size_t fg);
+void (*set_text_bg)(size_t bg);
+void (*set_text_fg_bright)(size_t fg);
+void (*set_text_bg_bright)(size_t bg);
+void (*set_text_fg_rgb)(uint32_t fg);
+void (*set_text_bg_rgb)(uint32_t bg);
+void (*set_text_fg_default)(void);
+void (*set_text_bg_default)(void);
+bool (*scroll_disable)(void);
+void (*scroll_enable)(void);
+void (*term_move_character)(size_t new_x, size_t new_y, size_t old_x, size_t old_y);
+void (*term_scroll)(void);
+void (*term_revscroll)(void);
+void (*term_swap_palette)(void);
+void (*term_save_state)(void);
+void (*term_restore_state)(void);
+
+void (*term_double_buffer_flush)(void);
+
+uint64_t (*term_context_size)(void);
+void (*term_context_save)(uint64_t ptr);
+void (*term_context_restore)(uint64_t ptr);
+void (*term_full_refresh)(void);
+
+// --- fallback ---
+
+#if defined (BIOS)
+static void fallback_raw_putchar(uint8_t c) {
+    struct rm_regs r = {0};
+    r.eax = 0x0e00 | c;
+    rm_int(0x10, &r, &r);
+}
+
+static void fallback_clear(bool move) {
+    (void)move;
+    struct rm_regs r = {0};
+    rm_int(0x11, &r, &r);
+    switch ((r.eax >> 4) & 3) {
+        case 0:
+            r.eax = 3;
+            break;
+        case 1:
+            r.eax = 1;
+            break;
+        case 2:
+            r.eax = 3;
+            break;
+        case 3:
+            r.eax = 7;
+            break;
+    }
+    rm_int(0x10, &r, &r);
+}
+
+static void fallback_set_cursor_pos(size_t x, size_t y) {
+    struct rm_regs r = {0};
+    r.eax = 0x0200;
+    r.ebx = 0;
+    r.edx = (y << 8) + x;
+    rm_int(0x10, &r, &r);
+}
+
+static void fallback_get_cursor_pos(size_t *x, size_t *y) {
+    struct rm_regs r = {0};
+    r.eax = 0x0300;
+    r.ebx = 0;
+    rm_int(0x10, &r, &r);
+    *x = r.edx & 0xff;
+    *y = r.edx >> 8;
+}
+
+#elif defined (UEFI)
+static int cursor_x = 0, cursor_y = 0;
+
+static void fallback_raw_putchar(uint8_t c) {
+    CHAR16 string[2];
+    string[0] = c;
+    string[1] = 0;
+    gST->ConOut->OutputString(gST->ConOut, string);
+    switch (c) {
+        case 0x08:
+            if (cursor_x > 0)
+                cursor_x--;
+            break;
+        case 0x0A:
+            cursor_x = 0;
+            break;
+        case 0x0D:
+            if (cursor_y < 24)
+                cursor_y++;
+            break;
+        default:
+            if (++cursor_x > 80) {
+                cursor_x = 0;
+                if (cursor_y < 24)
+                    cursor_y++;
+            }
+    }
+}
+
+static void fallback_clear(bool move) {
+    (void)move;
+    gST->ConOut->ClearScreen(gST->ConOut);
+    cursor_x = cursor_y = 0;
+}
+
+static void fallback_set_cursor_pos(size_t x, size_t y) {
+    if (x >= 80 || y >= 25)
+        return;
+    gST->ConOut->SetCursorPosition(gST->ConOut, x, y);
+    cursor_x = x;
+    cursor_y = y;
+}
+
+static void fallback_get_cursor_pos(size_t *x, size_t *y) {
+    *x = cursor_x;
+    *y = cursor_y;
+}
+#endif
+
+void term_fallback(void) {
+#if defined (UEFI)
+    if (!efi_boot_services_exited) {
+        gST->ConOut->Reset(gST->ConOut, false);
+        gST->ConOut->SetMode(gST->ConOut, 0);
+        cursor_x = cursor_y = 0;
+#elif defined (BIOS)
+        fallback_clear(true);
+#endif
+        term_notready();
+        raw_putchar = fallback_raw_putchar;
+        clear = fallback_clear;
+        set_cursor_pos = fallback_set_cursor_pos;
+        get_cursor_pos = fallback_get_cursor_pos;
+        term_backend = FALLBACK;
+#if defined (UEFI)
+    }
+#endif
+}
+
+// --- notready ---
+
+static void notready_raw_putchar(uint8_t c) {
+    (void)c;
+}
+static void notready_clear(bool move) {
+    (void)move;
+}
+static void notready_void(void) {}
+static void notready_set_cursor_pos(size_t x, size_t y) {
+    (void)x; (void)y;
+}
+static void notready_get_cursor_pos(size_t *x, size_t *y) {
+    *x = 0;
+    *y = 0;
+}
+static void notready_size_t(size_t n) {
+    (void)n;
+}
+static bool notready_disable(void) {
+    return false;
+}
+static void notready_move_character(size_t a, size_t b, size_t c, size_t d) {
+    (void)a; (void)b; (void)c; (void)d;
+}
+static uint64_t notready_context_size(void) {
+    return 0;
+}
+static void notready_uint32_t(uint32_t n) {
+    (void)n;
+}
+static void notready_uint64_t(uint64_t n) {
+    (void)n;
+}
+
+void term_notready(void) {
+    raw_putchar = notready_raw_putchar;
+    clear = notready_clear;
+    enable_cursor = notready_void;
+    disable_cursor = notready_disable;
+    set_cursor_pos = notready_set_cursor_pos;
+    get_cursor_pos = notready_get_cursor_pos;
+    set_text_fg = notready_size_t;
+    set_text_bg = notready_size_t;
+    set_text_fg_bright = notready_size_t;
+    set_text_bg_bright = notready_size_t;
+    set_text_fg_rgb = notready_uint32_t;
+    set_text_bg_rgb = notready_uint32_t;
+    set_text_fg_default = notready_void;
+    set_text_bg_default = notready_void;
+    scroll_disable = notready_disable;
+    scroll_enable = notready_void;
+    term_move_character = notready_move_character;
+    term_scroll = notready_void;
+    term_revscroll = notready_void;
+    term_swap_palette = notready_void;
+    term_save_state = notready_void;
+    term_restore_state = notready_void;
+    term_double_buffer_flush = notready_void;
+    term_context_size = notready_context_size;
+    term_context_save = notready_uint64_t;
+    term_context_restore = notready_uint64_t;
+    term_full_refresh = notready_void;
+
+    term_cols = 80;
+    term_rows = 24;
+
+    term_backend = _NOT_READY;
+}
+
 static const uint32_t col256[] = {
     0x000000, 0x00005f, 0x000087, 0x0000af, 0x0000d7, 0x0000ff, 0x005f00, 0x005f5f,
     0x005f87, 0x005faf, 0x005fd7, 0x005fff, 0x008700, 0x00875f, 0x008787, 0x0087af,
diff --git a/common/lib/term.s2.c b/common/lib/term.s2.c
deleted file mode 100644
index 132b1a5b..00000000
--- a/common/lib/term.s2.c
+++ /dev/null
@@ -1,227 +0,0 @@
-#include <stdint.h>
-#include <stddef.h>
-#include <stdbool.h>
-#include <lib/term.h>
-#include <lib/misc.h>
-#include <lib/real.h>
-
-int current_video_mode = -1;
-int term_backend = _NOT_READY;
-size_t term_rows, term_cols;
-bool term_runtime = false;
-
-void (*raw_putchar)(uint8_t c);
-void (*clear)(bool move);
-void (*enable_cursor)(void);
-bool (*disable_cursor)(void);
-void (*set_cursor_pos)(size_t x, size_t y);
-void (*get_cursor_pos)(size_t *x, size_t *y);
-void (*set_text_fg)(size_t fg);
-void (*set_text_bg)(size_t bg);
-void (*set_text_fg_bright)(size_t fg);
-void (*set_text_bg_bright)(size_t bg);
-void (*set_text_fg_rgb)(uint32_t fg);
-void (*set_text_bg_rgb)(uint32_t bg);
-void (*set_text_fg_default)(void);
-void (*set_text_bg_default)(void);
-bool (*scroll_disable)(void);
-void (*scroll_enable)(void);
-void (*term_move_character)(size_t new_x, size_t new_y, size_t old_x, size_t old_y);
-void (*term_scroll)(void);
-void (*term_revscroll)(void);
-void (*term_swap_palette)(void);
-void (*term_save_state)(void);
-void (*term_restore_state)(void);
-
-void (*term_double_buffer_flush)(void);
-
-uint64_t (*term_context_size)(void);
-void (*term_context_save)(uint64_t ptr);
-void (*term_context_restore)(uint64_t ptr);
-void (*term_full_refresh)(void);
-
-// --- fallback ---
-
-#if defined (BIOS)
-static void fallback_raw_putchar(uint8_t c) {
-    struct rm_regs r = {0};
-    r.eax = 0x0e00 | c;
-    rm_int(0x10, &r, &r);
-}
-
-static void fallback_clear(bool move) {
-    (void)move;
-    struct rm_regs r = {0};
-    rm_int(0x11, &r, &r);
-    switch ((r.eax >> 4) & 3) {
-        case 0:
-            r.eax = 3;
-            break;
-        case 1:
-            r.eax = 1;
-            break;
-        case 2:
-            r.eax = 3;
-            break;
-        case 3:
-            r.eax = 7;
-            break;
-    }
-    rm_int(0x10, &r, &r);
-}
-
-static void fallback_set_cursor_pos(size_t x, size_t y) {
-    struct rm_regs r = {0};
-    r.eax = 0x0200;
-    r.ebx = 0;
-    r.edx = (y << 8) + x;
-    rm_int(0x10, &r, &r);
-}
-
-static void fallback_get_cursor_pos(size_t *x, size_t *y) {
-    struct rm_regs r = {0};
-    r.eax = 0x0300;
-    r.ebx = 0;
-    rm_int(0x10, &r, &r);
-    *x = r.edx & 0xff;
-    *y = r.edx >> 8;
-}
-
-#elif defined (UEFI)
-static int cursor_x = 0, cursor_y = 0;
-
-static void fallback_raw_putchar(uint8_t c) {
-    CHAR16 string[2];
-    string[0] = c;
-    string[1] = 0;
-    gST->ConOut->OutputString(gST->ConOut, string);
-    switch (c) {
-        case 0x08:
-            if (cursor_x > 0)
-                cursor_x--;
-            break;
-        case 0x0A:
-            cursor_x = 0;
-            break;
-        case 0x0D:
-            if (cursor_y < 24)
-                cursor_y++;
-            break;
-        default:
-            if (++cursor_x > 80) {
-                cursor_x = 0;
-                if (cursor_y < 24)
-                    cursor_y++;
-            }
-    }
-}
-
-static void fallback_clear(bool move) {
-    (void)move;
-    gST->ConOut->ClearScreen(gST->ConOut);
-    cursor_x = cursor_y = 0;
-}
-
-static void fallback_set_cursor_pos(size_t x, size_t y) {
-    if (x >= 80 || y >= 25)
-        return;
-    gST->ConOut->SetCursorPosition(gST->ConOut, x, y);
-    cursor_x = x;
-    cursor_y = y;
-}
-
-static void fallback_get_cursor_pos(size_t *x, size_t *y) {
-    *x = cursor_x;
-    *y = cursor_y;
-}
-#endif
-
-void term_fallback(void) {
-#if defined (UEFI)
-    if (!efi_boot_services_exited) {
-        gST->ConOut->Reset(gST->ConOut, false);
-        gST->ConOut->SetMode(gST->ConOut, 0);
-        cursor_x = cursor_y = 0;
-#elif defined (BIOS)
-        fallback_clear(true);
-#endif
-        term_notready();
-        raw_putchar = fallback_raw_putchar;
-        clear = fallback_clear;
-        set_cursor_pos = fallback_set_cursor_pos;
-        get_cursor_pos = fallback_get_cursor_pos;
-        term_backend = FALLBACK;
-#if defined (UEFI)
-    }
-#endif
-}
-
-// --- notready ---
-
-static void notready_raw_putchar(uint8_t c) {
-    (void)c;
-}
-static void notready_clear(bool move) {
-    (void)move;
-}
-static void notready_void(void) {}
-static void notready_set_cursor_pos(size_t x, size_t y) {
-    (void)x; (void)y;
-}
-static void notready_get_cursor_pos(size_t *x, size_t *y) {
-    *x = 0;
-    *y = 0;
-}
-static void notready_size_t(size_t n) {
-    (void)n;
-}
-static bool notready_disable(void) {
-    return false;
-}
-static void notready_move_character(size_t a, size_t b, size_t c, size_t d) {
-    (void)a; (void)b; (void)c; (void)d;
-}
-static uint64_t notready_context_size(void) {
-    return 0;
-}
-static void notready_uint32_t(uint32_t n) {
-    (void)n;
-}
-static void notready_uint64_t(uint64_t n) {
-    (void)n;
-}
-
-void term_notready(void) {
-    raw_putchar = notready_raw_putchar;
-    clear = notready_clear;
-    enable_cursor = notready_void;
-    disable_cursor = notready_disable;
-    set_cursor_pos = notready_set_cursor_pos;
-    get_cursor_pos = notready_get_cursor_pos;
-    set_text_fg = notready_size_t;
-    set_text_bg = notready_size_t;
-    set_text_fg_bright = notready_size_t;
-    set_text_bg_bright = notready_size_t;
-    set_text_fg_rgb = notready_uint32_t;
-    set_text_bg_rgb = notready_uint32_t;
-    set_text_fg_default = notready_void;
-    set_text_bg_default = notready_void;
-    scroll_disable = notready_disable;
-    scroll_enable = notready_void;
-    term_move_character = notready_move_character;
-    term_scroll = notready_void;
-    term_revscroll = notready_void;
-    term_swap_palette = notready_void;
-    term_save_state = notready_void;
-    term_restore_state = notready_void;
-    term_double_buffer_flush = notready_void;
-    term_context_size = notready_context_size;
-    term_context_save = notready_uint64_t;
-    term_context_restore = notready_uint64_t;
-    term_full_refresh = notready_void;
-
-    term_cols = 80;
-    term_rows = 24;
-
-    term_backend = _NOT_READY;
-}
diff --git a/common/linker_bios.ld.in b/common/linker_bios.ld.in
index 632ced84..74562944 100644
--- a/common/linker_bios.ld.in
+++ b/common/linker_bios.ld.in
@@ -41,6 +41,8 @@ SECTIONS
         getchar = .;
         menu = .;
         term_write = .;
+        term_backend = .;
+        term_fallback = .;
         stage3_addr = .;
 #else
 #ifdef LINKER_NOS2MAP
tab: 248 wrap: offon