X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=src%2Futil.c;h=ed73d632f11e619d1e83fbba7d7c581f82adc481;hb=2c23a7ab6a1b1616835715949144c9adab331186;hp=66b3343c9462ee79a44acb7c0449d3a466cc0aad;hpb=8b267cb8a739576cd08c82d0ee75d6b14407c09c;p=seabios.git diff --git a/src/util.c b/src/util.c index 66b3343..ed73d63 100644 --- a/src/util.c +++ b/src/util.c @@ -1,120 +1,147 @@ // Misc utility functions. // -// Copyright (C) 2008 Kevin O'Connor +// Copyright (C) 2008,2009 Kevin O'Connor // // 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 +#include "config.h" // BUILD_STACK_ADDR + + +/**************************************************************** + * 16bit calls + ****************************************************************/ // Call a function with a specified register state. Note that on // return, the interrupt enable/disable flag may be altered. inline void call16(struct bregs *callregs) { + if (!MODESEGMENT && getesp() > BUILD_STACK_ADDR) + panic("call16 with invalid stack\n"); asm volatile( #if MODE16 == 1 "calll __call16\n" + "cli\n" + "cld" #else - "calll __call16_from32\n" + "calll __call16_from32" #endif : "+a" (callregs), "+m" (*callregs) : - : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory"); + : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory"); } inline void call16big(struct bregs *callregs) { - extern void __force_link_error__call16big_only_in_32bit_mode(); - if (MODE16) - __force_link_error__call16big_only_in_32bit_mode(); - + ASSERT32FLAT(); + if (getesp() > BUILD_STACK_ADDR) + panic("call16 with invalid stack\n"); asm volatile( - "calll __call16big_from32\n" + "calll __call16big_from32" : "+a" (callregs), "+m" (*callregs) : - : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory"); + : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory"); } inline void __call16_int(struct bregs *callregs, u16 offset) { - callregs->cs = SEG_BIOS; - callregs->ip = offset; + if (MODESEGMENT) + callregs->code.seg = GET_SEG(CS); + else + callregs->code.seg = SEG_BIOS; + callregs->code.offset = offset; call16(callregs); } -inline void -call16_simpint(int nr, u32 *eax, u32 *flags) -{ - extern void __force_link_error__call16_simpint_only_in_16bit_mode(); - if (!MODE16) - __force_link_error__call16_simpint_only_in_16bit_mode(); - asm volatile( - "stc\n" - "int %2\n" - "pushfl\n" - "popl %1\n" - "cld\n" - "cli\n" - : "+a"(*eax), "=r"(*flags) - : "i"(nr) - : "cc", "memory"); -} - -// Switch to the extra stack in ebda and call a function. -inline u32 -stack_hop(u32 eax, u32 edx, u32 ecx, void *func) -{ - extern void __force_link_error__stack_hop_only_in_16bit_mode(); - if (!MODE16) - __force_link_error__stack_hop_only_in_16bit_mode(); - - u16 ebda_seg = get_ebda_seg(), bkup_ss; - u32 bkup_esp; - asm volatile( - // Backup current %ss/%esp values. - "movw %%ss, %w3\n" - "movl %%esp, %4\n" - // Copy ebda seg to %ds/%ss and set %esp - "movw %w6, %%ds\n" - "movw %w6, %%ss\n" - "movl %5, %%esp\n" - // Call func - "calll %7\n" - // Restore segments and stack - "movw %w3, %%ds\n" - "movw %w3, %%ss\n" - "movl %4, %%esp\n" - : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp) - : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func) - : "cc", "memory"); - return eax; -} +/**************************************************************** + * String ops + ****************************************************************/ // Sum the bytes in the specified area. u8 -checksum_far(u16 buf_seg, u8 *buf_far, u32 len) +checksum_far(u16 buf_seg, void *buf_far, u32 len) { SET_SEG(ES, buf_seg); u32 i; u8 sum = 0; for (i=0; i 3) { + u32 copylen = len; + if (copylen > 2048) + copylen = 2048; + copylen /= 4; + len -= copylen * 4; + asm volatile( + "rep movsl (%%esi),%%es:(%%edi)" + : "+c"(copylen), "+S"(s), "+D"(d) + : : "cc", "memory"); + yield(); + } + if (len) + // Copy any remaining bytes. + memcpy(d, s, len); +} + void * memmove(void *d, const void *s, size_t len) { @@ -163,3 +250,75 @@ 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; +} + +// locate first occurance of character c in the string s +char * +strchr(const char *s, int c) +{ + for (; *s; s++) + if (*s == c) + return (char*)s; + return NULL; +} + +// Remove any trailing blank characters (spaces, new lines, carriage returns) +void +nullTrailingSpace(char *buf) +{ + int len = strlen(buf); + char *end = &buf[len-1]; + while (end >= buf && *end <= ' ') + *(end--) = '\0'; +} + +/**************************************************************** + * Keyboard calls + ****************************************************************/ + +// See if a keystroke is pending in the keyboard buffer. +static int +check_for_keystroke(void) +{ + struct bregs br; + memset(&br, 0, sizeof(br)); + br.flags = F_IF; + br.ah = 1; + call16_int(0x16, &br); + return !(br.flags & F_ZF); +} + +// Return a keystroke - waiting forever if necessary. +static int +get_raw_keystroke(void) +{ + struct bregs br; + memset(&br, 0, sizeof(br)); + br.flags = F_IF; + call16_int(0x16, &br); + return br.ah; +} + +// Read a keystroke - waiting up to 'msec' milliseconds. +int +get_keystroke(int msec) +{ + u32 end = calc_future_timer(msec); + for (;;) { + if (check_for_keystroke()) + return get_raw_keystroke(); + if (check_timer(end)) + return -1; + wait_irq(); + } +}