Rename memcpy() to memcpy_far() and add regular memcpy() code.
[seabios.git] / src / util.c
1 // Misc utility functions.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU GPLv3 license.
6
7 #include "util.h" // usleep
8 #include "bregs.h" // struct bregs
9 #include "config.h" // SEG_BIOS
10 #include "farptr.h" // GET_FARPTR
11
12 // Call a function with a specified register state.  Note that on
13 // return, the interrupt enable/disable flag may be altered.
14 inline void
15 call16(struct bregs *callregs)
16 {
17     asm volatile(
18 #if MODE16 == 1
19         "calll __call16\n"
20 #else
21         "calll __call16_from32\n"
22 #endif
23         : "+a" (callregs), "+m" (*callregs)
24         :
25         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
26 }
27
28 inline void
29 __call16_int(struct bregs *callregs, u16 offset)
30 {
31     callregs->cs = SEG_BIOS;
32     callregs->ip = offset;
33     call16(callregs);
34 }
35
36 // Sum the bytes in the specified area.
37 u8
38 checksum(u8 *far_data, u32 len)
39 {
40     u32 i;
41     u8 sum = 0;
42     for (i=0; i<len; i++)
43         sum += GET_FARPTR(far_data[i]);
44     return sum;
45 }
46
47 void *
48 memset(void *s, int c, size_t n)
49 {
50     while (n)
51         ((char *)s)[--n] = c;
52     return s;
53 }
54
55 void *
56 memcpy_far(void *far_d1, const void *far_s1, size_t len)
57 {
58     u8 *d = far_d1;
59     u8 *s = (u8*)far_s1;
60
61     while (len--) {
62         SET_FARPTR(*d, GET_FARPTR(*s));
63         d++;
64         s++;
65     }
66
67     return far_d1;
68 }
69
70 void *
71 memcpy(void *d1, const void *s1, size_t len)
72 {
73     u8 *d = (u8*)d1, *s = (u8*)s1;
74     while (len--)
75         *d++ = *s++;
76     return d1;
77 }
78
79 void *
80 memmove(void *d, const void *s, size_t len)
81 {
82     if (s >= d)
83         return memcpy(d, s, len);
84
85     d += len-1;
86     s += len-1;
87     while (len--) {
88         *(char*)d = *(char*)s;
89         d--;
90         s--;
91     }
92
93     return d;
94 }