Minor - formatting enhancements; add memset_far funcs.
[seabios.git] / src / util.c
1 // Misc utility functions.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 //
5 // This file may be distributed under the terms of the GNU LGPLv3 license.
6
7 #include "util.h" // call16
8 #include "bregs.h" // struct bregs
9 #include "farptr.h" // GET_FLATPTR
10 #include "biosvar.h" // get_ebda_seg
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         "cli\n"
21         "cld"
22 #else
23         "calll __call16_from32"
24 #endif
25         : "+a" (callregs), "+m" (*callregs)
26         :
27         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
28 }
29
30 inline void
31 call16big(struct bregs *callregs)
32 {
33     extern void __force_link_error__call16big_only_in_32bit_mode();
34     if (MODE16)
35         __force_link_error__call16big_only_in_32bit_mode();
36
37     asm volatile(
38         "calll __call16big_from32"
39         : "+a" (callregs), "+m" (*callregs)
40         :
41         : "ebx", "ecx", "edx", "esi", "edi", "cc", "memory");
42 }
43
44 inline void
45 __call16_int(struct bregs *callregs, u16 offset)
46 {
47     if (MODE16)
48         callregs->cs = GET_SEG(CS);
49     else
50         callregs->cs = SEG_BIOS;
51     callregs->ip = offset;
52     call16(callregs);
53 }
54
55 inline void
56 call16_simpint(int nr, u32 *eax, u32 *flags)
57 {
58     extern void __force_link_error__call16_simpint_only_in_16bit_mode();
59     if (!MODE16)
60         __force_link_error__call16_simpint_only_in_16bit_mode();
61
62     asm volatile(
63         "stc\n"
64         "int %2\n"
65         "pushfl\n"
66         "popl %1\n"
67         "cli\n"
68         "cld"
69         : "+a"(*eax), "=r"(*flags)
70         : "i"(nr)
71         : "cc", "memory");
72 }
73
74 // Switch to the extra stack in ebda and call a function.
75 inline u32
76 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
77 {
78     extern void __force_link_error__stack_hop_only_in_16bit_mode();
79     if (!MODE16)
80         __force_link_error__stack_hop_only_in_16bit_mode();
81
82     u16 ebda_seg = get_ebda_seg(), bkup_ss;
83     u32 bkup_esp;
84     asm volatile(
85         // Backup current %ss/%esp values.
86         "movw %%ss, %w3\n"
87         "movl %%esp, %4\n"
88         // Copy ebda seg to %ds/%ss and set %esp
89         "movw %w6, %%ds\n"
90         "movw %w6, %%ss\n"
91         "movl %5, %%esp\n"
92         // Call func
93         "calll %7\n"
94         // Restore segments and stack
95         "movw %w3, %%ds\n"
96         "movw %w3, %%ss\n"
97         "movl %4, %%esp"
98         : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
99         : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
100         : "cc", "memory");
101     return eax;
102 }
103
104 // Sum the bytes in the specified area.
105 u8
106 checksum_far(u16 buf_seg, void *buf_far, u32 len)
107 {
108     SET_SEG(ES, buf_seg);
109     u32 i;
110     u8 sum = 0;
111     for (i=0; i<len; i++)
112         sum += GET_VAR(ES, ((u8*)buf_far)[i]);
113     return sum;
114 }
115
116 u8
117 checksum(void *buf, u32 len)
118 {
119     return checksum_far(GET_SEG(SS), buf, len);
120 }
121
122 size_t
123 strlen(const char *s)
124 {
125     if (__builtin_constant_p(s))
126         return __builtin_strlen(s);
127     const char *p = s;
128     while (*p)
129         p++;
130     return p-s;
131 }
132
133 // Compare two areas of memory.
134 int
135 memcmp(const void *s1, const void *s2, size_t n)
136 {
137     while (n) {
138         if (*(u8*)s1 != *(u8*)s2)
139             return *(u8*)s1 < *(u8*)s2 ? -1 : 1;
140         s1++;
141         s2++;
142         n--;
143     }
144     return 0;
145 }
146
147 // Compare two strings.
148 int
149 strcmp(const char *s1, const char *s2)
150 {
151     for (;;) {
152         if (*s1 != *s2)
153             return *s1 < *s2 ? -1 : 1;
154         if (! *s1)
155             return 0;
156         s1++;
157         s2++;
158     }
159 }
160
161 inline void
162 memset_far(u16 d_seg, void *d_far, u8 c, size_t len)
163 {
164     SET_SEG(ES, d_seg);
165     asm volatile(
166         "rep stosb %%es:(%%di)"
167         : "+c"(len), "+D"(d_far)
168         : "a"(c)
169         : "cc", "memory");
170 }
171
172 inline void
173 memset16_far(u16 d_seg, void *d_far, u16 c, size_t len)
174 {
175     len /= 2;
176     SET_SEG(ES, d_seg);
177     asm volatile(
178         "rep stosw %%es:(%%di)"
179         : "+c"(len), "+D"(d_far)
180         : "a"(c)
181         : "cc", "memory");
182 }
183
184 void *
185 memset(void *s, int c, size_t n)
186 {
187     while (n)
188         ((char *)s)[--n] = c;
189     return s;
190 }
191
192 inline void
193 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
194 {
195     SET_SEG(ES, d_seg);
196     u16 bkup_ds;
197     asm volatile(
198         "movw %%ds, %w0\n"
199         "movw %w4, %%ds\n"
200         "rep movsb (%%si),%%es:(%%di)\n"
201         "movw %w0, %%ds"
202         : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
203         : "r"(s_seg)
204         : "cc", "memory");
205 }
206
207 // Memcpy that uses 4-byte accesses
208 void
209 memcpy4(void *d1, const void *s1, size_t len)
210 {
211     len /= 4;
212     asm volatile(
213         "rep movsl (%%esi),%%es:(%%edi)"
214         : "+c"(len), "+S"(s1), "+D"(d1)
215         : : "cc", "memory");
216 }
217
218 void *
219 memmove(void *d, const void *s, size_t len)
220 {
221     if (s >= d)
222         return memcpy(d, s, len);
223
224     d += len-1;
225     s += len-1;
226     while (len--) {
227         *(char*)d = *(char*)s;
228         d--;
229         s--;
230     }
231
232     return d;
233 }
234
235 // Copy a string - truncating it if necessary.
236 char *
237 strtcpy(char *dest, const char *src, size_t len)
238 {
239     char *d = dest;
240     while (len-- && *src != '\0')
241         *d++ = *src++;
242     *d = '\0';
243     return dest;
244 }
245
246 // Wait for 'usec' microseconds with irqs enabled.
247 void
248 usleep(u32 usec)
249 {
250     struct bregs br;
251     memset(&br, 0, sizeof(br));
252     br.ah = 0x86;
253     br.cx = usec >> 16;
254     br.dx = usec;
255     call16_int(0x15, &br);
256 }
257
258 // See if a keystroke is pending in the keyboard buffer.
259 static int
260 check_for_keystroke()
261 {
262     struct bregs br;
263     memset(&br, 0, sizeof(br));
264     br.ah = 1;
265     call16_int(0x16, &br);
266     return !(br.flags & F_ZF);
267 }
268
269 // Return a keystroke - waiting forever if necessary.
270 static int
271 get_raw_keystroke()
272 {
273     struct bregs br;
274     memset(&br, 0, sizeof(br));
275     call16_int(0x16, &br);
276     return br.ah;
277 }
278
279 // Read a keystroke - waiting up to 'msec' milliseconds.
280 int
281 get_keystroke(int msec)
282 {
283     for (;;) {
284         if (check_for_keystroke())
285             return get_raw_keystroke();
286         if (msec <= 0)
287             return -1;
288         usleep(50*1000);
289         msec -= 50;
290     }
291 }