Introduce set_fail_silent() helpers.
[seabios.git] / src / util.h
1 // Basic x86 asm functions and function defs.
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 #ifndef __UTIL_H
7 #define __UTIL_H
8
9 #include "ioport.h" // outb
10 #include "biosvar.h" // struct bregs
11
12 static inline void irq_disable(void)
13 {
14     asm volatile("cli": : :"memory");
15 }
16
17 static inline void irq_enable(void)
18 {
19     asm volatile("sti": : :"memory");
20 }
21
22 static inline unsigned long irq_save(void)
23 {
24     unsigned long flags;
25     asm volatile("pushfl ; popl %0" : "=g" (flags));
26     irq_disable();
27     return flags;
28 }
29
30 static inline void irq_restore(unsigned long flags)
31 {
32     asm volatile("pushl %0 ; popfl" : : "g" (flags) : "memory", "cc");
33 }
34
35 static inline void cpu_relax(void)
36 {
37     asm volatile("rep ; nop": : :"memory");
38 }
39
40 static inline void nop(void)
41 {
42     asm volatile("nop");
43 }
44
45 static inline void hlt(void)
46 {
47     asm volatile("hlt");
48 }
49
50 void *memset(void *s, int c, size_t n);
51 void *memcpy(void *d1, const void *s1, size_t len);
52
53 static inline void
54 eoi_master_pic()
55 {
56     outb(PIC1_IRQ5, PORT_PIC1);
57 }
58
59 static inline void
60 eoi_both_pics()
61 {
62     outb(PIC2_IRQ13, PORT_PIC2);
63     eoi_master_pic();
64 }
65
66 // Call a function with a specified register state.  Note that on
67 // return, the interrupt enable/disable flag may be altered.
68 static inline
69 void call16(struct bregs *callregs)
70 {
71     asm volatile(
72 #ifdef MODE16
73         "calll __call16\n"
74 #else
75         "calll __call16_from32\n"
76 #endif
77         : "+a" (callregs), "+m" (*callregs)
78         :
79         : "ebx", "ecx", "edx", "esi", "edi", "ebp", "cc");
80 }
81
82 static inline
83 void __call16_int(struct bregs *callregs, u16 offset)
84 {
85     callregs->cs = SEG_BIOS;
86     callregs->ip = offset;
87     call16(callregs);
88 }
89
90 #ifdef MODE16
91 #define call16_int(nr, callregs) do {                           \
92         extern void irq_trampoline_ ##nr ();                    \
93         __call16_int((callregs), (u32)&irq_trampoline_ ##nr );  \
94     } while (0)
95 #else
96 #include "../out/rom16.offset.auto.h"
97 #define call16_int(nr, callregs)                                \
98     __call16_int((callregs), OFFSET_irq_trampoline_ ##nr )
99 #endif
100
101 // output.c
102 void BX_PANIC(const char *fmt, ...)
103     __attribute__ ((format (printf, 1, 2)))
104     __attribute__ ((noreturn));
105 void printf(const char *fmt, ...)
106     __attribute__ ((format (printf, 1, 2)));
107 void __dprintf(const char *fmt, ...)
108     __attribute__ ((format (printf, 1, 2)));
109 #define dprintf(lvl, fmt, args...) do {                         \
110         if (CONFIG_DEBUG_LEVEL && (lvl) <= CONFIG_DEBUG_LEVEL)  \
111             __dprintf((fmt) , ##args );                         \
112     } while (0)
113 void __debug_enter(const char *fname, struct bregs *regs);
114 void __debug_fail(const char *fname, struct bregs *regs);
115 void __debug_stub(const char *fname, struct bregs *regs);
116 void __debug_isr(const char *fname);
117 #define debug_enter(regs) \
118     __debug_enter(__func__, regs)
119 #define debug_stub(regs) \
120     __debug_stub(__func__, regs)
121 #define debug_isr(regs) \
122     __debug_isr(__func__)
123
124 // Frequently used return codes
125 #define RET_EUNSUPPORTED 0x86
126 static inline void
127 set_success(struct bregs *regs)
128 {
129     set_cf(regs, 0);
130 }
131
132 static inline void
133 set_code_success(struct bregs *regs)
134 {
135     regs->ah = 0;
136     set_cf(regs, 0);
137 }
138
139 static inline void
140 set_fail_silent(struct bregs *regs)
141 {
142     set_cf(regs, 1);
143 }
144
145 static inline void
146 set_code_fail_silent(struct bregs *regs, u8 code)
147 {
148     regs->ah = code;
149     set_cf(regs, 1);
150 }
151
152 void __set_fail(const char *fname, struct bregs *regs);
153 void __set_code_fail(const char *fname, struct bregs *regs, u8 code);
154
155 #define set_fail(regs) \
156     __set_fail(__func__, (regs))
157 #define set_code_fail(regs, code)               \
158     __set_code_fail(__func__, (regs), (code))
159
160 // kbd.c
161 void handle_15c2(struct bregs *regs);
162
163 // serial.c
164 void serial_setup();
165 void lpt_setup();
166
167 // clock.c
168 void timer_setup();
169 int usleep(u32 count);
170 void handle_1583(struct bregs *regs);
171 void handle_1586(struct bregs *regs);
172
173 // apm.c
174 void VISIBLE16 handle_1553(struct bregs *regs);
175
176 // pcibios.c
177 void handle_1ab1(struct bregs *regs);
178
179 // util.c
180 u8 checksum(u8 *far_data, u32 len);
181
182 // rombios32.c
183 void rombios32_init(void);
184
185 // boot.c
186 void printf_bootdev(u16 bootdev);
187
188 // post_menu.c
189 void interactive_bootmenu();
190
191 #endif // util.h