compiler.h is now always included on the command line.
[people/balajirrao/gpxe.git] / src / arch / i386 / firmware / pcbios / bios.c
1 /* Etherboot routines for PCBIOS firmware.
2  *
3  * Body of routines taken from old pcbios.S
4  */
5
6 #include "stdint.h"
7 #include "realmode.h"
8
9 #define BIOS_DATA_SEG 0x0040
10
11 #define CF ( 1 << 0 )
12
13 /**************************************************************************
14 CURRTICKS - Get Time
15 Use direct memory access to BIOS variables, longword 0040:006C (ticks
16 today) and byte 0040:0070 (midnight crossover flag) instead of calling
17 timeofday BIOS interrupt.
18 **************************************************************************/
19 #if defined(CONFIG_TSC_CURRTICKS)
20 #undef CONFIG_BIOS_CURRTICKS
21 #else
22 #define CONFIG_BIOS_CURRTICKS 1
23 #endif
24 #if defined(CONFIG_BIOS_CURRTICKS)
25 unsigned long currticks ( void ) {
26         static uint32_t days = 0;
27         uint32_t ticks;
28         uint8_t midnight;
29
30         /* Re-enable interrupts so that the timer interrupt can occur
31          */
32         REAL_EXEC ( rm_currticks,
33                     "sti\n\t"
34                     "nop\n\t"
35                     "nop\n\t"
36                     "cli\n\t",
37                     0,
38                     OUT_CONSTRAINTS (),
39                     IN_CONSTRAINTS (),
40                     CLOBBER ( "eax" ) ); /* can't have an empty clobber list */
41
42         get_real ( ticks, BIOS_DATA_SEG, 0x006c );
43         get_real ( midnight, BIOS_DATA_SEG, 0x0070 );
44
45         if ( midnight ) {
46                 midnight = 0;
47                 put_real ( midnight, BIOS_DATA_SEG, 0x0070 );
48                 days += 0x1800b0;
49         }
50         return ( days + ticks );
51 }
52 #endif  /* CONFIG_BIOS_CURRTICKS */
53
54 /**************************************************************************
55 CPU_NAP - Save power by halting the CPU until the next interrupt
56 **************************************************************************/
57 void cpu_nap ( void ) {
58         REAL_EXEC ( rm_cpu_nap,
59                     "sti\n\t"
60                     "hlt\n\t"
61                     "cli\n\t",
62                     0,
63                     OUT_CONSTRAINTS (),
64                     IN_CONSTRAINTS (),
65                     CLOBBER ( "eax" ) ); /* can't have an empty clobber list */
66 }
67
68 #if     (TRY_FLOPPY_FIRST > 0)
69 /**************************************************************************
70 DISK_INIT - Initialize the disk system
71 **************************************************************************/
72 void disk_init ( void ) {
73         REAL_EXEC ( rm_disk_init,
74                     "sti\n\t"
75                     "xorw %ax,%ax\n\t"
76                     "movb $0x80,%dl\n\t"
77                     "int $0x13\n\t"
78                     "cli\n\t",
79                     0,
80                     OUT_CONSTRAINTS (),
81                     IN_CONSTRAINTS (),
82                     CLOBBER ( "eax", "ebx", "ecx", "edx",
83                               "ebp", "esi", "edi" ) );
84 }
85
86 /**************************************************************************
87 DISK_READ - Read a sector from disk
88 **************************************************************************/
89 unsigned int pcbios_disk_read ( int drive, int cylinder, int head, int sector,
90                                 char *fixme_buf ) {
91         uint16_t ax, flags, discard_c, discard_d;
92         segoff_t buf = SEGOFF ( fixme_buf );
93
94         /* FIXME: buf should be passed in as a segoff_t rather than a
95          * char *
96          */
97
98         REAL_EXEC ( rm_pcbios_disk_read,
99                     "sti\n\t"
100                     "pushl %%ebx\n\t"      /* Convert %ebx to %es:bx */
101                     "popl %%bx\n\t"
102                     "popl %%es\n\t"
103                     "movb $0x02, %%ah\n\t" /* INT 13,2 - Read disk sector */
104                     "movb $0x01, %%al\n\t" /* Read one sector */
105                     "int $0x13\n\t"
106                     "pushfw\n\t"
107                     "popw %%bx\n\t"
108                     "cli\n\t",
109                     4,
110                     OUT_CONSTRAINTS ( "=a" ( ax ), "=b" ( flags ),
111                                       "=c" ( discard_c ), "=d" ( discard_d ) ),
112                     IN_CONSTRAINTS ( "c" ( ( ( cylinder & 0xff ) << 8 ) |
113                                            ( ( cylinder >> 8 ) & 0x3 ) |
114                                            sector ),
115                                      "d" ( ( head << 8 ) | drive ),
116                                      "b" ( buf ) ),
117                     CLOBBER ( "ebp", "esi", "edi" ) );
118         );
119
120         return ( flags & CF ) ? ax : 0;
121 }
122 #endif /* TRY_FLOPPY_FIRST */