:: commit 5a6d6afb947ae8435cd6fb5da39314682270c19e

mintsuki <mintsuki@protonmail.com> — 2020-06-05 19:34

parents: c05c652057

Fix bug in memmap and vga textmode

diff --git a/qloader2.bin b/qloader2.bin
index db78c793..42803045 100644
Binary files a/qloader2.bin and b/qloader2.bin differ
diff --git a/src/drivers/vga_textmode.c b/src/drivers/vga_textmode.c
index 9c4b5f50..4aeafd41 100644
--- a/src/drivers/vga_textmode.c
+++ b/src/drivers/vga_textmode.c
@@ -1,5 +1,6 @@
 #include <stdint.h>
 #include <stddef.h>
+#include <stdbool.h>
 #include <lib/cio.h>
 #include <lib/real.h>
 #include <drivers/vga_textmode.h>
@@ -8,6 +9,8 @@
 #define VD_COLS (80 * 2)
 #define VD_ROWS 25
 
+static bool vga_textmode_initialised = false;
+
 static void escape_parse(char c);
 static void text_putchar(char c);
 
@@ -87,14 +90,16 @@ void init_vga_textmode(void) {
     port_out_b(0x3d4, 0x0a);
     port_out_b(0x3d5, 0x20);
     text_clear();
+
+    vga_textmode_initialised = true;
 }
 
 void deinit_vga_textmode(void) {
     struct rm_regs r = {0};
-
     r.eax = 0x0003;
-
     rm_int(0x10, &r, &r);
+
+    vga_textmode_initialised = false;
 }
 
 static void text_set_cursor_palette(uint8_t c) {
@@ -136,6 +141,8 @@ void text_set_cursor_pos(int x, int y) {
 }
 
 void text_write(const char *buf, size_t count) {
+    if (!vga_textmode_initialised)
+        return;
     for (size_t i = 0; i < count; i++)
         text_putchar(buf[i]);
 }
diff --git a/src/lib/memmap.c b/src/lib/memmap.c
index ef572556..78501120 100644
--- a/src/lib/memmap.c
+++ b/src/lib/memmap.c
@@ -34,7 +34,7 @@ static const char *memmap_type(uint32_t type) {
 
 void print_memmap(struct e820_entry_t *mm, size_t size) {
     for (size_t i = 0; i < size; i++) {
-        print("e820: [%X -> %X] : %X  <%s>\n",
+        print("[%X -> %X] : %X  <%s>\n",
               mm[i].base,
               mm[i].base + mm[i].length,
               mm[i].length,
@@ -42,16 +42,23 @@ void print_memmap(struct e820_entry_t *mm, size_t size) {
     }
 }
 
-static void align_entry_down(uint64_t *base, uint64_t *length) {
+static int align_entry_down(uint64_t *base, uint64_t *length) {
     uint64_t orig_base = *base;
 
     *base   = ALIGN_UP(*base, PAGE_SIZE);
+
+    if ((*base - orig_base) > *length)
+        return -1;
+
     *length = *length - (*base - orig_base);
     *length = ALIGN_DOWN(*length, PAGE_SIZE);
+
+    return 0;
 }
 
 static int align_entry_with_base(uint64_t *base, uint64_t *length) {
-    align_entry_down(base, length);
+    if (align_entry_down(base, length))
+        return -1;
 
     if (!length)
         return -1;
@@ -75,9 +82,8 @@ static void memmap_align_free_entries(void) {
         if (memmap[i].type != 1)
             continue;
 
-        align_entry_down(&memmap[i].base, &memmap[i].length);
-
-        if (!memmap[i].length) {
+        if (align_entry_down(&memmap[i].base, &memmap[i].length)
+         || !memmap[i].length) {
             // Eradicate from memmap
             for (size_t j = i; j < memmap_entries - 1; j++) {
                 memmap[j] = memmap[j+1];
tab: 248 wrap: offon