Simplify boot code.
[seabios.git] / src / util.c
index 0a60e171a7837e43af6c43c1be1c1ed6e4e26eac..7eec39d846594d009613b20acb4446473e4390bd 100644 (file)
@@ -4,9 +4,8 @@
 //
 // This file may be distributed under the terms of the GNU LGPLv3 license.
 
-#include "util.h" // usleep
+#include "util.h" // call16
 #include "bregs.h" // struct bregs
-#include "config.h" // SEG_BIOS
 #include "farptr.h" // GET_FLATPTR
 #include "biosvar.h" // get_ebda_seg
 
@@ -43,7 +42,10 @@ call16big(struct bregs *callregs)
 inline void
 __call16_int(struct bregs *callregs, u16 offset)
 {
-    callregs->cs = SEG_BIOS;
+    if (MODE16)
+        callregs->cs = GET_SEG(CS);
+    else
+        callregs->cs = SEG_BIOS;
     callregs->ip = offset;
     call16(callregs);
 }
@@ -99,15 +101,22 @@ stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
 
 // Sum the bytes in the specified area.
 u8
-checksum(u8 *buf_fl, u32 len)
+checksum_far(u16 buf_seg, u8 *buf_far, u32 len)
 {
+    SET_SEG(ES, buf_seg);
     u32 i;
     u8 sum = 0;
     for (i=0; i<len; i++)
-        sum += GET_FLATPTR(buf_fl[i]);
+        sum += GET_VAR(ES, buf_far[i]);
     return sum;
 }
 
+u8
+checksum(u8 *buf, u32 len)
+{
+    return checksum_far(GET_SEG(SS), buf, len);
+}
+
 void *
 memset(void *s, int c, size_t n)
 {
@@ -116,19 +125,19 @@ memset(void *s, int c, size_t n)
     return s;
 }
 
-void *
-memcpy_fl(void *d_fl, const void *s_fl, size_t len)
+inline void
+memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
 {
-    u8 *d = d_fl;
-    u8 *s = (u8*)s_fl;
-
-    while (len--) {
-        SET_FLATPTR(*d, GET_FLATPTR(*s));
-        d++;
-        s++;
-    }
-
-    return d_fl;
+    SET_SEG(ES, d_seg);
+    u16 bkup_ds;
+    asm volatile(
+        "movw %%ds, %w0\n"
+        "movw %w4, %%ds\n"
+        "rep movsb (%%si),%%es:(%%di)\n"
+        "movw %w0, %%ds\n"
+        : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
+        : "r"(s_seg)
+        : "cc", "memory");
 }
 
 void *
@@ -156,3 +165,61 @@ memmove(void *d, const void *s, size_t len)
 
     return d;
 }
+
+// Copy a string - truncating it if necessary.
+char *
+strtcpy(char *dest, const char *src, size_t len)
+{
+    char *d = dest;
+    while (len-- && *src != '\0')
+        *d++ = *src++;
+    *d = '\0';
+    return dest;
+}
+
+// Wait for 'usec' microseconds with irqs enabled.
+static void
+usleep(u32 usec)
+{
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    br.ah = 0x86;
+    br.cx = usec >> 16;
+    br.dx = usec;
+    call16_int(0x15, &br);
+}
+
+// See if a keystroke is pending in the keyboard buffer.
+static int
+check_for_keystroke()
+{
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    br.ah = 1;
+    call16_int(0x16, &br);
+    return !(br.flags & F_ZF);
+}
+
+// Return a keystroke - waiting forever if necessary.
+static int
+get_raw_keystroke()
+{
+    struct bregs br;
+    memset(&br, 0, sizeof(br));
+    call16_int(0x16, &br);
+    return br.ah;
+}
+
+// Read a keystroke - waiting up to 'msec' milliseconds.
+int
+get_keystroke(int msec)
+{
+    for (;;) {
+        if (check_for_keystroke())
+            return get_raw_keystroke();
+        if (msec <= 0)
+            return -1;
+        usleep(50*1000);
+        msec -= 50;
+    }
+}