Reorganize boot 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 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 #else
21         "calll __call16_from32\n"
22 #endif
23         : "+a" (callregs), "+m" (*callregs)
24         :
25         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
26 }
27
28 inline void
29 call16big(struct bregs *callregs)
30 {
31     extern void __force_link_error__call16big_only_in_32bit_mode();
32     if (MODE16)
33         __force_link_error__call16big_only_in_32bit_mode();
34
35     asm volatile(
36         "calll __call16big_from32\n"
37         : "+a" (callregs), "+m" (*callregs)
38         :
39         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc", "memory");
40 }
41
42 inline void
43 __call16_int(struct bregs *callregs, u16 offset)
44 {
45     if (MODE16)
46         callregs->cs = GET_SEG(CS);
47     else
48         callregs->cs = SEG_BIOS;
49     callregs->ip = offset;
50     call16(callregs);
51 }
52
53 inline void
54 call16_simpint(int nr, u32 *eax, u32 *flags)
55 {
56     extern void __force_link_error__call16_simpint_only_in_16bit_mode();
57     if (!MODE16)
58         __force_link_error__call16_simpint_only_in_16bit_mode();
59
60     asm volatile(
61         "stc\n"
62         "int %2\n"
63         "pushfl\n"
64         "popl %1\n"
65         "cld\n"
66         "cli\n"
67         : "+a"(*eax), "=r"(*flags)
68         : "i"(nr)
69         : "cc", "memory");
70 }
71
72 // Switch to the extra stack in ebda and call a function.
73 inline u32
74 stack_hop(u32 eax, u32 edx, u32 ecx, void *func)
75 {
76     extern void __force_link_error__stack_hop_only_in_16bit_mode();
77     if (!MODE16)
78         __force_link_error__stack_hop_only_in_16bit_mode();
79
80     u16 ebda_seg = get_ebda_seg(), bkup_ss;
81     u32 bkup_esp;
82     asm volatile(
83         // Backup current %ss/%esp values.
84         "movw %%ss, %w3\n"
85         "movl %%esp, %4\n"
86         // Copy ebda seg to %ds/%ss and set %esp
87         "movw %w6, %%ds\n"
88         "movw %w6, %%ss\n"
89         "movl %5, %%esp\n"
90         // Call func
91         "calll %7\n"
92         // Restore segments and stack
93         "movw %w3, %%ds\n"
94         "movw %w3, %%ss\n"
95         "movl %4, %%esp\n"
96         : "+a" (eax), "+d" (edx), "+c" (ecx), "=&r" (bkup_ss), "=&r" (bkup_esp)
97         : "i" (EBDA_OFFSET_TOP_STACK), "r" (ebda_seg), "m" (*(u8*)func)
98         : "cc", "memory");
99     return eax;
100 }
101
102 // Sum the bytes in the specified area.
103 u8
104 checksum_far(u16 buf_seg, u8 *buf_far, u32 len)
105 {
106     SET_SEG(ES, buf_seg);
107     u32 i;
108     u8 sum = 0;
109     for (i=0; i<len; i++)
110         sum += GET_VAR(ES, buf_far[i]);
111     return sum;
112 }
113
114 u8
115 checksum(u8 *buf, u32 len)
116 {
117     return checksum_far(GET_SEG(SS), buf, len);
118 }
119
120 void *
121 memset(void *s, int c, size_t n)
122 {
123     while (n)
124         ((char *)s)[--n] = c;
125     return s;
126 }
127
128 inline void
129 memcpy_far(u16 d_seg, void *d_far, u16 s_seg, const void *s_far, size_t len)
130 {
131     SET_SEG(ES, d_seg);
132     u16 bkup_ds;
133     asm volatile(
134         "movw %%ds, %w0\n"
135         "movw %w4, %%ds\n"
136         "rep movsb (%%si),%%es:(%%di)\n"
137         "movw %w0, %%ds\n"
138         : "=&r"(bkup_ds), "+c"(len), "+S"(s_far), "+D"(d_far)
139         : "r"(s_seg)
140         : "cc", "memory");
141 }
142
143 void *
144 memcpy(void *d1, const void *s1, size_t len)
145 {
146     u8 *d = (u8*)d1, *s = (u8*)s1;
147     while (len--)
148         *d++ = *s++;
149     return d1;
150 }
151
152 void *
153 memmove(void *d, const void *s, size_t len)
154 {
155     if (s >= d)
156         return memcpy(d, s, len);
157
158     d += len-1;
159     s += len-1;
160     while (len--) {
161         *(char*)d = *(char*)s;
162         d--;
163         s--;
164     }
165
166     return d;
167 }
168
169 // Wait for 'usec' microseconds with irqs enabled.
170 static void
171 usleep(u32 usec)
172 {
173     struct bregs br;
174     memset(&br, 0, sizeof(br));
175     br.ah = 0x86;
176     br.cx = usec >> 16;
177     br.dx = usec;
178     call16_int(0x15, &br);
179 }
180
181 // See if a keystroke is pending in the keyboard buffer.
182 static int
183 check_for_keystroke()
184 {
185     struct bregs br;
186     memset(&br, 0, sizeof(br));
187     br.ah = 1;
188     call16_int(0x16, &br);
189     return !(br.flags & F_ZF);
190 }
191
192 // Return a keystroke - waiting forever if necessary.
193 static int
194 get_raw_keystroke()
195 {
196     struct bregs br;
197     memset(&br, 0, sizeof(br));
198     call16_int(0x16, &br);
199     return br.ah;
200 }
201
202 // Read a keystroke - waiting up to 'msec' milliseconds.
203 int
204 get_keystroke(int msec)
205 {
206     for (;;) {
207         if (check_for_keystroke())
208             return get_raw_keystroke();
209         if (msec <= 0)
210             return -1;
211         usleep(50*1000);
212         msec -= 50;
213     }
214 }