a4c3975ac412855fcaea611998623789f0d97513
[linux-flexiantxendom0-3.2.10.git] / arch / i386 / boot / setup.S
1 /*
2  *      setup.S         Copyright (C) 1991, 1992 Linus Torvalds
3  *
4  * setup.s is responsible for getting the system data from the BIOS,
5  * and putting them into the appropriate places in system memory.
6  * both setup.s and system has been loaded by the bootblock.
7  *
8  * This code asks the bios for memory/disk/other parameters, and
9  * puts them in a "safe" place: 0x90000-0x901FF, ie where the
10  * boot-block used to be. It is then up to the protected mode
11  * system to read them from there before the area is overwritten
12  * for buffer-blocks.
13  *
14  * Move PS/2 aux init code to psaux.c
15  * (troyer@saifr00.cfsat.Honeywell.COM) 03Oct92
16  *
17  * some changes and additional features by Christoph Niemann,
18  * March 1993/June 1994 (Christoph.Niemann@linux.org)
19  *
20  * add APM BIOS checking by Stephen Rothwell, May 1994
21  * (sfr@canb.auug.org.au)
22  *
23  * High load stuff, initrd support and position independency
24  * by Hans Lermen & Werner Almesberger, February 1996
25  * <lermen@elserv.ffm.fgan.de>, <almesber@lrc.epfl.ch>
26  *
27  * Video handling moved to video.S by Martin Mares, March 1996
28  * <mj@k332.feld.cvut.cz>
29  *
30  * Extended memory detection scheme retwiddled by orc@pell.chi.il.us (david
31  * parsons) to avoid loadlin confusion, July 1997
32  *
33  * Transcribed from Intel (as86) -> AT&T (gas) by Chris Noe, May 1999.
34  * <stiker@northlink.com>
35  *
36  * Fix to work around buggy BIOSes which dont use carry bit correctly
37  * and/or report extended memory in CX/DX for e801h memory size detection 
38  * call.  As a result the kernel got wrong figures.  The int15/e801h docs
39  * from Ralf Brown interrupt list seem to indicate AX/BX should be used
40  * anyway.  So to avoid breaking many machines (presumably there was a reason
41  * to orginally use CX/DX instead of AX/BX), we do a kludge to see
42  * if CX/DX have been changed in the e801 call and if so use AX/BX .
43  * Michael Miller, April 2001 <michaelm@mjmm.org>
44  *
45  * New A20 code ported from SYSLINUX by H. Peter Anvin. AMD Elan bugfixes
46  * by Robert Schwebel, December 2001 <robert@schwebel.de>
47  *    
48  * BIOS Enhanced Disk Drive support
49  * by Matt Domsch <Matt_Domsch@dell.com> October 2002
50  * conformant to T13 Committee www.t13.org
51  *   projects 1572D, 1484D, 1386D, 1226DT
52  * disk signature read by Matt Domsch <Matt_Domsch@dell.com>
53  *      and Andrew Wilks <Andrew_Wilks@dell.com> September 2003
54  */
55
56 #include <linux/config.h>
57 #include <asm/segment.h>
58 #include <linux/version.h>
59 #include <linux/compile.h>
60 #include <asm/boot.h>
61 #include <asm/e820.h>
62 #include <asm/edd.h>    
63 #include <asm/page.h>
64         
65 /* Signature words to ensure LILO loaded us right */
66 #define SIG1    0xAA55
67 #define SIG2    0x5A5A
68
69 INITSEG  = DEF_INITSEG          # 0x9000, we move boot here, out of the way
70 SYSSEG   = DEF_SYSSEG           # 0x1000, system loaded at 0x10000 (65536).
71 SETUPSEG = DEF_SETUPSEG         # 0x9020, this is the current segment
72                                 # ... and the former contents of CS
73
74 DELTA_INITSEG = SETUPSEG - INITSEG      # 0x0020
75
76 .code16
77 .globl begtext, begdata, begbss, endtext, enddata, endbss
78
79 .text
80 begtext:
81 .data
82 begdata:
83 .bss
84 begbss:
85 .text
86
87 start:
88         jmp     trampoline
89
90 # This is the setup header, and it must start at %cs:2 (old 0x9020:2)
91
92                 .ascii  "HdrS"          # header signature
93                 .word   0x0203          # header version number (>= 0x0105)
94                                         # or else old loadlin-1.5 will fail)
95 realmode_swtch: .word   0, 0            # default_switch, SETUPSEG
96 start_sys_seg:  .word   SYSSEG
97                 .word   kernel_version  # pointing to kernel version string
98                                         # above section of header is compatible
99                                         # with loadlin-1.5 (header v1.5). Don't
100                                         # change it.
101
102 type_of_loader: .byte   0               # = 0, old one (LILO, Loadlin,
103                                         #      Bootlin, SYSLX, bootsect...)
104                                         # See Documentation/i386/boot.txt for
105                                         # assigned ids
106         
107 # flags, unused bits must be zero (RFU) bit within loadflags
108 loadflags:
109 LOADED_HIGH     = 1                     # If set, the kernel is loaded high
110 CAN_USE_HEAP    = 0x80                  # If set, the loader also has set
111                                         # heap_end_ptr to tell how much
112                                         # space behind setup.S can be used for
113                                         # heap purposes.
114                                         # Only the loader knows what is free
115 #ifndef __BIG_KERNEL__
116                 .byte   0
117 #else
118                 .byte   LOADED_HIGH
119 #endif
120
121 setup_move_size: .word  0x8000          # size to move, when setup is not
122                                         # loaded at 0x90000. We will move setup 
123                                         # to 0x90000 then just before jumping
124                                         # into the kernel. However, only the
125                                         # loader knows how much data behind
126                                         # us also needs to be loaded.
127
128 code32_start:                           # here loaders can put a different
129                                         # start address for 32-bit code.
130 #ifndef __BIG_KERNEL__
131                 .long   0x1000          #   0x1000 = default for zImage
132 #else
133                 .long   0x100000        # 0x100000 = default for big kernel
134 #endif
135
136 ramdisk_image:  .long   0               # address of loaded ramdisk image
137                                         # Here the loader puts the 32-bit
138                                         # address where it loaded the image.
139                                         # This only will be read by the kernel.
140
141 ramdisk_size:   .long   0               # its size in bytes
142
143 bootsect_kludge:
144                 .word  bootsect_helper, SETUPSEG
145
146 heap_end_ptr:   .word   modelist+1024   # (Header version 0x0201 or later)
147                                         # space from here (exclusive) down to
148                                         # end of setup code can be used by setup
149                                         # for local heap purposes.
150
151 pad1:           .word   0
152 cmd_line_ptr:   .long 0                 # (Header version 0x0202 or later)
153                                         # If nonzero, a 32-bit pointer
154                                         # to the kernel command line.
155                                         # The command line should be
156                                         # located between the start of
157                                         # setup and the end of low
158                                         # memory (0xa0000), or it may
159                                         # get overwritten before it
160                                         # gets read.  If this field is
161                                         # used, there is no longer
162                                         # anything magical about the
163                                         # 0x90000 segment; the setup
164                                         # can be located anywhere in
165                                         # low memory 0x10000 or higher.
166
167 ramdisk_max:    .long MAXMEM-1          # (Header version 0x0203 or later)
168                                         # The highest safe address for
169                                         # the contents of an initrd
170
171 trampoline:     call    start_of_setup
172                 .space  1024
173 # End of setup header #####################################################
174
175 start_of_setup:
176 # Bootlin depends on this being done early
177         movw    $0x01500, %ax
178         movb    $0x81, %dl
179         int     $0x13
180
181 #ifdef SAFE_RESET_DISK_CONTROLLER
182 # Reset the disk controller.
183         movw    $0x0000, %ax
184         movb    $0x80, %dl
185         int     $0x13
186 #endif
187
188 # Set %ds = %cs, we know that SETUPSEG = %cs at this point
189         movw    %cs, %ax                # aka SETUPSEG
190         movw    %ax, %ds
191 # Check signature at end of setup
192         cmpw    $SIG1, setup_sig1
193         jne     bad_sig
194
195         cmpw    $SIG2, setup_sig2
196         jne     bad_sig
197
198         jmp     good_sig1
199
200 # Routine to print asciiz string at ds:si
201 prtstr:
202         lodsb
203         andb    %al, %al
204         jz      fin
205
206         call    prtchr
207         jmp     prtstr
208
209 fin:    ret
210
211 # Space printing
212 prtsp2: call    prtspc          # Print double space
213 prtspc: movb    $0x20, %al      # Print single space (note: fall-thru)
214
215 # Part of above routine, this one just prints ascii al
216 prtchr: pushw   %ax
217         pushw   %cx
218         movw    $7,%bx
219         movw    $0x01, %cx
220         movb    $0x0e, %ah
221         int     $0x10
222         popw    %cx
223         popw    %ax
224         ret
225
226 beep:   movb    $0x07, %al
227         jmp     prtchr
228         
229 no_sig_mess: .string    "No setup signature found ..."
230
231 good_sig1:
232         jmp     good_sig
233
234 # We now have to find the rest of the setup code/data
235 bad_sig:
236         movw    %cs, %ax                        # SETUPSEG
237         subw    $DELTA_INITSEG, %ax             # INITSEG
238         movw    %ax, %ds
239         xorb    %bh, %bh
240         movb    (497), %bl                      # get setup sect from bootsect
241         subw    $4, %bx                         # LILO loads 4 sectors of setup
242         shlw    $8, %bx                         # convert to words (1sect=2^8 words)
243         movw    %bx, %cx
244         shrw    $3, %bx                         # convert to segment
245         addw    $SYSSEG, %bx
246         movw    %bx, %cs:start_sys_seg
247 # Move rest of setup code/data to here
248         movw    $2048, %di                      # four sectors loaded by LILO
249         subw    %si, %si
250         pushw   %cs
251         popw    %es
252         movw    $SYSSEG, %ax
253         movw    %ax, %ds
254         rep
255         movsw
256         movw    %cs, %ax                        # aka SETUPSEG
257         movw    %ax, %ds
258         cmpw    $SIG1, setup_sig1
259         jne     no_sig
260
261         cmpw    $SIG2, setup_sig2
262         jne     no_sig
263
264         jmp     good_sig
265
266 no_sig:
267         lea     no_sig_mess, %si
268         call    prtstr
269
270 no_sig_loop:
271         hlt
272         jmp     no_sig_loop
273
274 good_sig:
275         movw    %cs, %ax                        # aka SETUPSEG
276         subw    $DELTA_INITSEG, %ax             # aka INITSEG
277         movw    %ax, %ds
278 # Check if an old loader tries to load a big-kernel
279         testb   $LOADED_HIGH, %cs:loadflags     # Do we have a big kernel?
280         jz      loader_ok                       # No, no danger for old loaders.
281
282         cmpb    $0, %cs:type_of_loader          # Do we have a loader that
283                                                 # can deal with us?
284         jnz     loader_ok                       # Yes, continue.
285
286         pushw   %cs                             # No, we have an old loader,
287         popw    %ds                             # die. 
288         lea     loader_panic_mess, %si
289         call    prtstr
290
291         jmp     no_sig_loop
292
293 loader_panic_mess: .string "Wrong loader, giving up..."
294
295 loader_ok:
296 # Get memory size (extended mem, kB)
297
298         xorl    %eax, %eax
299         movl    %eax, (0x1e0)
300 #ifndef STANDARD_MEMORY_BIOS_CALL
301         movb    %al, (E820NR)
302 # Try three different memory detection schemes.  First, try
303 # e820h, which lets us assemble a memory map, then try e801h,
304 # which returns a 32-bit memory size, and finally 88h, which
305 # returns 0-64m
306
307 # method E820H:
308 # the memory map from hell.  e820h returns memory classified into
309 # a whole bunch of different types, and allows memory holes and
310 # everything.  We scan through this memory map and build a list
311 # of the first 32 memory areas, which we return at [E820MAP].
312 # This is documented at http://www.acpi.info/, in the ACPI 2.0 specification.
313
314 #define SMAP  0x534d4150
315
316 meme820:
317         xorl    %ebx, %ebx                      # continuation counter
318         movw    $E820MAP, %di                   # point into the whitelist
319                                                 # so we can have the bios
320                                                 # directly write into it.
321
322 jmpe820:
323         movl    $0x0000e820, %eax               # e820, upper word zeroed
324         movl    $SMAP, %edx                     # ascii 'SMAP'
325         movl    $20, %ecx                       # size of the e820rec
326         pushw   %ds                             # data record.
327         popw    %es
328         int     $0x15                           # make the call
329         jc      bail820                         # fall to e801 if it fails
330
331         cmpl    $SMAP, %eax                     # check the return is `SMAP'
332         jne     bail820                         # fall to e801 if it fails
333
334 #       cmpl    $1, 16(%di)                     # is this usable memory?
335 #       jne     again820
336
337         # If this is usable memory, we save it by simply advancing %di by
338         # sizeof(e820rec).
339         #
340 good820:
341         movb    (E820NR), %al                   # up to 32 entries
342         cmpb    $E820MAX, %al
343         jnl     bail820
344
345         incb    (E820NR)
346         movw    %di, %ax
347         addw    $20, %ax
348         movw    %ax, %di
349 again820:
350         cmpl    $0, %ebx                        # check to see if
351         jne     jmpe820                         # %ebx is set to EOF
352 bail820:
353
354
355 # method E801H:
356 # memory size is in 1k chunksizes, to avoid confusing loadlin.
357 # we store the 0xe801 memory size in a completely different place,
358 # because it will most likely be longer than 16 bits.
359 # (use 1e0 because that's what Larry Augustine uses in his
360 # alternative new memory detection scheme, and it's sensible
361 # to write everything into the same place.)
362
363 meme801:
364         stc                                     # fix to work around buggy
365         xorw    %cx,%cx                         # BIOSes which dont clear/set
366         xorw    %dx,%dx                         # carry on pass/error of
367                                                 # e801h memory size call
368                                                 # or merely pass cx,dx though
369                                                 # without changing them.
370         movw    $0xe801, %ax
371         int     $0x15
372         jc      mem88
373
374         cmpw    $0x0, %cx                       # Kludge to handle BIOSes
375         jne     e801usecxdx                     # which report their extended
376         cmpw    $0x0, %dx                       # memory in AX/BX rather than
377         jne     e801usecxdx                     # CX/DX.  The spec I have read
378         movw    %ax, %cx                        # seems to indicate AX/BX 
379         movw    %bx, %dx                        # are more reasonable anyway...
380
381 e801usecxdx:
382         andl    $0xffff, %edx                   # clear sign extend
383         shll    $6, %edx                        # and go from 64k to 1k chunks
384         movl    %edx, (0x1e0)                   # store extended memory size
385         andl    $0xffff, %ecx                   # clear sign extend
386         addl    %ecx, (0x1e0)                   # and add lower memory into
387                                                 # total size.
388
389 # Ye Olde Traditional Methode.  Returns the memory size (up to 16mb or
390 # 64mb, depending on the bios) in ax.
391 mem88:
392
393 #endif
394         movb    $0x88, %ah
395         int     $0x15
396         movw    %ax, (2)
397
398 # Set the keyboard repeat rate to the max
399         movw    $0x0305, %ax
400         xorw    %bx, %bx
401         int     $0x16
402
403 # Check for video adapter and its parameters and allow the
404 # user to browse video modes.
405         call    video                           # NOTE: we need %ds pointing
406                                                 # to bootsector
407
408 # Get hd0 data...
409         xorw    %ax, %ax
410         movw    %ax, %ds
411         ldsw    (4 * 0x41), %si
412         movw    %cs, %ax                        # aka SETUPSEG
413         subw    $DELTA_INITSEG, %ax             # aka INITSEG
414         pushw   %ax
415         movw    %ax, %es
416         movw    $0x0080, %di
417         movw    $0x10, %cx
418         pushw   %cx
419         cld
420         rep
421         movsb
422 # Get hd1 data...
423         xorw    %ax, %ax
424         movw    %ax, %ds
425         ldsw    (4 * 0x46), %si
426         popw    %cx
427         popw    %es
428         movw    $0x0090, %di
429         rep
430         movsb
431 # Check that there IS a hd1 :-)
432         movw    $0x01500, %ax
433         movb    $0x81, %dl
434         int     $0x13
435         jc      no_disk1
436         
437         cmpb    $3, %ah
438         je      is_disk1
439
440 no_disk1:
441         movw    %cs, %ax                        # aka SETUPSEG
442         subw    $DELTA_INITSEG, %ax             # aka INITSEG
443         movw    %ax, %es
444         movw    $0x0090, %di
445         movw    $0x10, %cx
446         xorw    %ax, %ax
447         cld
448         rep
449         stosb
450 is_disk1:
451 # check for Micro Channel (MCA) bus
452         movw    %cs, %ax                        # aka SETUPSEG
453         subw    $DELTA_INITSEG, %ax             # aka INITSEG
454         movw    %ax, %ds
455         xorw    %ax, %ax
456         movw    %ax, (0xa0)                     # set table length to 0
457         movb    $0xc0, %ah
458         stc
459         int     $0x15                           # moves feature table to es:bx
460         jc      no_mca
461
462         pushw   %ds
463         movw    %es, %ax
464         movw    %ax, %ds
465         movw    %cs, %ax                        # aka SETUPSEG
466         subw    $DELTA_INITSEG, %ax             # aka INITSEG
467         movw    %ax, %es
468         movw    %bx, %si
469         movw    $0xa0, %di
470         movw    (%si), %cx
471         addw    $2, %cx                         # table length is a short
472         cmpw    $0x10, %cx
473         jc      sysdesc_ok
474
475         movw    $0x10, %cx                      # we keep only first 16 bytes
476 sysdesc_ok:
477         rep
478         movsb
479         popw    %ds
480 no_mca:
481 #ifdef CONFIG_X86_VOYAGER
482         movb    $0xff, 0x40     # flag on config found
483         movb    $0xc0, %al
484         mov     $0xff, %ah
485         int     $0x15           # put voyager config info at es:di
486         jc      no_voyager
487         movw    $0x40, %si      # place voyager info in apm table
488         cld
489         movw    $7, %cx
490 voyager_rep:
491         movb    %es:(%di), %al
492         movb    %al,(%si)
493         incw    %di
494         incw    %si
495         decw    %cx
496         jnz     voyager_rep
497 no_voyager:     
498 #endif
499 # Check for PS/2 pointing device
500         movw    %cs, %ax                        # aka SETUPSEG
501         subw    $DELTA_INITSEG, %ax             # aka INITSEG
502         movw    %ax, %ds
503         movw    $0, (0x1ff)                     # default is no pointing device
504         int     $0x11                           # int 0x11: equipment list
505         testb   $0x04, %al                      # check if mouse installed
506         jz      no_psmouse
507
508         movw    $0xAA, (0x1ff)                  # device present
509 no_psmouse:
510
511 #if defined(CONFIG_X86_SPEEDSTEP_SMI) || defined(CONFIG_X86_SPEEDSTEP_SMI_MODULE)
512         movl    $0x0000E980, %eax               # IST Support 
513         movl    $0x47534943, %edx               # Request value
514         int     $0x15
515
516         movl    %eax, (96)
517         movl    %ebx, (100)
518         movl    %ecx, (104)
519         movl    %edx, (108)
520 #endif
521
522 #if defined(CONFIG_APM) || defined(CONFIG_APM_MODULE)
523 # Then check for an APM BIOS...
524                                                 # %ds points to the bootsector
525         movw    $0, 0x40                        # version = 0 means no APM BIOS
526         movw    $0x05300, %ax                   # APM BIOS installation check
527         xorw    %bx, %bx
528         int     $0x15
529         jc      done_apm_bios                   # Nope, no APM BIOS
530         
531         cmpw    $0x0504d, %bx                   # Check for "PM" signature
532         jne     done_apm_bios                   # No signature, no APM BIOS
533
534         andw    $0x02, %cx                      # Is 32 bit supported?
535         je      done_apm_bios                   # No 32-bit, no (good) APM BIOS
536
537         movw    $0x05304, %ax                   # Disconnect first just in case
538         xorw    %bx, %bx
539         int     $0x15                           # ignore return code
540         movw    $0x05303, %ax                   # 32 bit connect
541         xorl    %ebx, %ebx
542         xorw    %cx, %cx                        # paranoia :-)
543         xorw    %dx, %dx                        #   ...
544         xorl    %esi, %esi                      #   ...
545         xorw    %di, %di                        #   ...
546         int     $0x15
547         jc      no_32_apm_bios                  # Ack, error. 
548
549         movw    %ax,  (66)                      # BIOS code segment
550         movl    %ebx, (68)                      # BIOS entry point offset
551         movw    %cx,  (72)                      # BIOS 16 bit code segment
552         movw    %dx,  (74)                      # BIOS data segment
553         movl    %esi, (78)                      # BIOS code segment lengths
554         movw    %di,  (82)                      # BIOS data segment length
555 # Redo the installation check as the 32 bit connect
556 # modifies the flags returned on some BIOSs
557         movw    $0x05300, %ax                   # APM BIOS installation check
558         xorw    %bx, %bx
559         xorw    %cx, %cx                        # paranoia
560         int     $0x15
561         jc      apm_disconnect                  # error -> shouldn't happen
562
563         cmpw    $0x0504d, %bx                   # check for "PM" signature
564         jne     apm_disconnect                  # no sig -> shouldn't happen
565
566         movw    %ax, (64)                       # record the APM BIOS version
567         movw    %cx, (76)                       # and flags
568         jmp     done_apm_bios
569
570 apm_disconnect:                                 # Tidy up
571         movw    $0x05304, %ax                   # Disconnect
572         xorw    %bx, %bx
573         int     $0x15                           # ignore return code
574
575         jmp     done_apm_bios
576
577 no_32_apm_bios:
578         andw    $0xfffd, (76)                   # remove 32 bit support bit
579 done_apm_bios:
580 #endif
581
582 #if defined(CONFIG_EDD) || defined(CONFIG_EDD_MODULE)
583 # Read the first sector of device 80h and store the 4-byte signature
584         movl    $0xFFFFFFFF, %eax
585         movl    %eax, (DISK80_SIG_BUFFER)       # assume failure
586         movb    $READ_SECTORS, %ah
587         movb    $1, %al                         # read 1 sector
588         movb    $0x80, %dl                      # from device 80
589         movb    $0, %dh                         # at head 0
590         movw    $1, %cx                         # cylinder 0, sector 0
591         pushw   %es
592         pushw   %ds
593         popw    %es
594         movw    $EDDBUF, %bx
595         int     $0x13
596         jc      disk_sig_done
597         movl    (EDDBUF+MBR_SIG_OFFSET), %eax
598         movl    %eax, (DISK80_SIG_BUFFER)       # store success
599 disk_sig_done:
600         popw    %es
601
602 # Do the BIOS Enhanced Disk Drive calls
603 # This consists of two calls:
604 #    int 13h ah=41h "Check Extensions Present"
605 #    int 13h ah=48h "Get Device Parameters"
606 #
607 # A buffer of size EDDMAXNR*(EDDEXTSIZE+EDDPARMSIZE) is reserved for our use
608 # in the empty_zero_page at EDDBUF.  The first four bytes of which are
609 # used to store the device number, interface support map and version
610 # results from fn41.  The following 74 bytes are used to store
611 # the results from fn48.  Starting from device 80h, fn41, then fn48
612 # are called and their results stored in EDDBUF+n*(EDDEXTSIZE+EDDPARMIZE).
613 # Then the pointer is incremented to store the data for the next call.
614 # This repeats until either a device doesn't exist, or until EDDMAXNR
615 # devices have been stored.
616 # The one tricky part is that ds:si always points four bytes into
617 # the structure, and the fn41 results are stored at offsets
618 # from there.  This removes the need to increment the pointer for
619 # every store, and leaves it ready for the fn48 call.
620 # A second one-byte buffer, EDDNR, in the empty_zero_page stores
621 # the number of BIOS devices which exist, up to EDDMAXNR.
622 # In setup.c, copy_edd() stores both empty_zero_page buffers away
623 # for later use, as they would get overwritten otherwise. 
624 # This code is sensitive to the size of the structs in edd.h
625 edd_start:  
626                                                 # %ds points to the bootsector
627                                                 # result buffer for fn48
628         movw    $EDDBUF+EDDEXTSIZE, %si         # in ds:si, fn41 results
629                                                 # kept just before that    
630         movb    $0, (EDDNR)                     # zero value at EDDNR
631         movb    $0x80, %dl                      # BIOS device 0x80
632
633 edd_check_ext:
634         movb    $CHECKEXTENSIONSPRESENT, %ah    # Function 41
635         movw    $EDDMAGIC1, %bx                 # magic
636         int     $0x13                           # make the call
637         jc      edd_done                        # no more BIOS devices
638
639         cmpw    $EDDMAGIC2, %bx                 # is magic right?
640         jne     edd_next                        # nope, next...
641
642         movb    %dl, %ds:-4(%si)                # store device number
643         movb    %ah, %ds:-3(%si)                # store version
644         movw    %cx, %ds:-2(%si)                # store extensions
645         incb    (EDDNR)                         # note that we stored something
646         
647 edd_get_device_params:  
648         movw    $EDDPARMSIZE, %ds:(%si)         # put size
649         movb    $GETDEVICEPARAMETERS, %ah       # Function 48
650         int     $0x13                           # make the call
651                                                 # Don't check for fail return
652                                                 # it doesn't matter.
653         movw    %si, %ax                        # increment si
654         addw    $EDDPARMSIZE+EDDEXTSIZE, %ax
655         movw    %ax, %si
656
657 edd_next:
658         incb    %dl                             # increment to next device
659         cmpb    $EDDMAXNR, (EDDNR)              # Out of space?
660         jb      edd_check_ext                   # keep looping
661     
662 edd_done:   
663 #endif
664
665 # Now we want to move to protected mode ...
666         cmpw    $0, %cs:realmode_swtch
667         jz      rmodeswtch_normal
668
669         lcall   *%cs:realmode_swtch
670
671         jmp     rmodeswtch_end
672
673 rmodeswtch_normal:
674         pushw   %cs
675         call    default_switch
676
677 rmodeswtch_end:
678 # we get the code32 start address and modify the below 'jmpi'
679 # (loader may have changed it)
680         movl    %cs:code32_start, %eax
681         movl    %eax, %cs:code32
682
683 # Now we move the system to its rightful place ... but we check if we have a
684 # big-kernel. In that case we *must* not move it ...
685         testb   $LOADED_HIGH, %cs:loadflags
686         jz      do_move0                        # .. then we have a normal low
687                                                 # loaded zImage
688                                                 # .. or else we have a high
689                                                 # loaded bzImage
690         jmp     end_move                        # ... and we skip moving
691
692 do_move0:
693         movw    $0x100, %ax                     # start of destination segment
694         movw    %cs, %bp                        # aka SETUPSEG
695         subw    $DELTA_INITSEG, %bp             # aka INITSEG
696         movw    %cs:start_sys_seg, %bx          # start of source segment
697         cld
698 do_move:
699         movw    %ax, %es                        # destination segment
700         incb    %ah                             # instead of add ax,#0x100
701         movw    %bx, %ds                        # source segment
702         addw    $0x100, %bx
703         subw    %di, %di
704         subw    %si, %si
705         movw    $0x800, %cx
706         rep
707         movsw
708         cmpw    %bp, %bx                        # assume start_sys_seg > 0x200,
709                                                 # so we will perhaps read one
710                                                 # page more than needed, but
711                                                 # never overwrite INITSEG
712                                                 # because destination is a
713                                                 # minimum one page below source
714         jb      do_move
715
716 end_move:
717 # then we load the segment descriptors
718         movw    %cs, %ax                        # aka SETUPSEG
719         movw    %ax, %ds
720                 
721 # Check whether we need to be downward compatible with version <=201
722         cmpl    $0, cmd_line_ptr
723         jne     end_move_self           # loader uses version >=202 features
724         cmpb    $0x20, type_of_loader
725         je      end_move_self           # bootsect loader, we know of it
726
727 # Boot loader doesnt support boot protocol version 2.02.
728 # If we have our code not at 0x90000, we need to move it there now.
729 # We also then need to move the params behind it (commandline)
730 # Because we would overwrite the code on the current IP, we move
731 # it in two steps, jumping high after the first one.
732         movw    %cs, %ax
733         cmpw    $SETUPSEG, %ax
734         je      end_move_self
735
736         cli                                     # make sure we really have
737                                                 # interrupts disabled !
738                                                 # because after this the stack
739                                                 # should not be used
740         subw    $DELTA_INITSEG, %ax             # aka INITSEG
741         movw    %ss, %dx
742         cmpw    %ax, %dx
743         jb      move_self_1
744
745         addw    $INITSEG, %dx
746         subw    %ax, %dx                        # this will go into %ss after
747                                                 # the move
748 move_self_1:
749         movw    %ax, %ds
750         movw    $INITSEG, %ax                   # real INITSEG
751         movw    %ax, %es
752         movw    %cs:setup_move_size, %cx
753         std                                     # we have to move up, so we use
754                                                 # direction down because the
755                                                 # areas may overlap
756         movw    %cx, %di
757         decw    %di
758         movw    %di, %si
759         subw    $move_self_here+0x200, %cx
760         rep
761         movsb
762         ljmp    $SETUPSEG, $move_self_here
763
764 move_self_here:
765         movw    $move_self_here+0x200, %cx
766         rep
767         movsb
768         movw    $SETUPSEG, %ax
769         movw    %ax, %ds
770         movw    %dx, %ss
771 end_move_self:                                  # now we are at the right place
772
773 #
774 # Enable A20.  This is at the very best an annoying procedure.
775 # A20 code ported from SYSLINUX 1.52-1.63 by H. Peter Anvin.
776 # AMD Elan bug fix by Robert Schwebel.
777 #
778
779 #if defined(CONFIG_MELAN)
780         movb $0x02, %al                 # alternate A20 gate
781         outb %al, $0x92                 # this works on SC410/SC520
782 a20_elan_wait:
783         call a20_test
784         jz a20_elan_wait
785         jmp a20_done
786 #endif
787
788
789 A20_TEST_LOOPS          =  32           # Iterations per wait
790 A20_ENABLE_LOOPS        = 255           # Total loops to try            
791
792
793 #ifndef CONFIG_X86_VOYAGER
794 a20_try_loop:
795
796         # First, see if we are on a system with no A20 gate.
797 a20_none:
798         call    a20_test
799         jnz     a20_done
800
801         # Next, try the BIOS (INT 0x15, AX=0x2401)
802 a20_bios:
803         movw    $0x2401, %ax
804         pushfl                                  # Be paranoid about flags
805         int     $0x15
806         popfl
807
808         call    a20_test
809         jnz     a20_done
810
811         # Try enabling A20 through the keyboard controller
812 #endif /* CONFIG_X86_VOYAGER */
813 a20_kbc:
814         call    empty_8042
815
816 #ifndef CONFIG_X86_VOYAGER
817         call    a20_test                        # Just in case the BIOS worked
818         jnz     a20_done                        # but had a delayed reaction.
819 #endif
820
821         movb    $0xD1, %al                      # command write
822         outb    %al, $0x64
823         call    empty_8042
824
825         movb    $0xDF, %al                      # A20 on
826         outb    %al, $0x60
827         call    empty_8042
828
829 #ifndef CONFIG_X86_VOYAGER
830         # Wait until a20 really *is* enabled; it can take a fair amount of
831         # time on certain systems; Toshiba Tecras are known to have this
832         # problem.
833 a20_kbc_wait:
834         xorw    %cx, %cx
835 a20_kbc_wait_loop:
836         call    a20_test
837         jnz     a20_done
838         loop    a20_kbc_wait_loop
839
840         # Final attempt: use "configuration port A"
841 a20_fast:
842         inb     $0x92, %al                      # Configuration Port A
843         orb     $0x02, %al                      # "fast A20" version
844         andb    $0xFE, %al                      # don't accidentally reset
845         outb    %al, $0x92
846
847         # Wait for configuration port A to take effect
848 a20_fast_wait:
849         xorw    %cx, %cx
850 a20_fast_wait_loop:
851         call    a20_test
852         jnz     a20_done
853         loop    a20_fast_wait_loop
854
855         # A20 is still not responding.  Try frobbing it again.
856         # 
857         decb    (a20_tries)
858         jnz     a20_try_loop
859         
860         movw    $a20_err_msg, %si
861         call    prtstr
862
863 a20_die:
864         hlt
865         jmp     a20_die
866
867 a20_tries:
868         .byte   A20_ENABLE_LOOPS
869
870 a20_err_msg:
871         .ascii  "linux: fatal error: A20 gate not responding!"
872         .byte   13, 10, 0
873
874         # If we get here, all is good
875 a20_done:
876
877 #endif /* CONFIG_X86_VOYAGER */
878 # set up gdt and idt
879         lidt    idt_48                          # load idt with 0,0
880         xorl    %eax, %eax                      # Compute gdt_base
881         movw    %ds, %ax                        # (Convert %ds:gdt to a linear ptr)
882         shll    $4, %eax
883         addl    $gdt, %eax
884         movl    %eax, (gdt_48+2)
885         lgdt    gdt_48                          # load gdt with whatever is
886                                                 # appropriate
887
888 # make sure any possible coprocessor is properly reset..
889         xorw    %ax, %ax
890         outb    %al, $0xf0
891         call    delay
892
893         outb    %al, $0xf1
894         call    delay
895
896 # well, that went ok, I hope. Now we mask all interrupts - the rest
897 # is done in init_IRQ().
898         movb    $0xFF, %al                      # mask all interrupts for now
899         outb    %al, $0xA1
900         call    delay
901         
902         movb    $0xFB, %al                      # mask all irq's but irq2 which
903         outb    %al, $0x21                      # is cascaded
904
905 # Well, that certainly wasn't fun :-(. Hopefully it works, and we don't
906 # need no steenking BIOS anyway (except for the initial loading :-).
907 # The BIOS-routine wants lots of unnecessary data, and it's less
908 # "interesting" anyway. This is how REAL programmers do it.
909 #
910 # Well, now's the time to actually move into protected mode. To make
911 # things as simple as possible, we do no register set-up or anything,
912 # we let the gnu-compiled 32-bit programs do that. We just jump to
913 # absolute address 0x1000 (or the loader supplied one),
914 # in 32-bit protected mode.
915 #
916 # Note that the short jump isn't strictly needed, although there are
917 # reasons why it might be a good idea. It won't hurt in any case.
918         movw    $1, %ax                         # protected mode (PE) bit
919         lmsw    %ax                             # This is it!
920         jmp     flush_instr
921
922 flush_instr:
923         xorw    %bx, %bx                        # Flag to indicate a boot
924         xorl    %esi, %esi                      # Pointer to real-mode code
925         movw    %cs, %si
926         subw    $DELTA_INITSEG, %si
927         shll    $4, %esi                        # Convert to 32-bit pointer
928
929 # jump to startup_32 in arch/i386/kernel/head.S
930 #       
931 # NOTE: For high loaded big kernels we need a
932 #       jmpi    0x100000,__BOOT_CS
933 #
934 #       but we yet haven't reloaded the CS register, so the default size 
935 #       of the target offset still is 16 bit.
936 #       However, using an operand prefix (0x66), the CPU will properly
937 #       take our 48 bit far pointer. (INTeL 80386 Programmer's Reference
938 #       Manual, Mixing 16-bit and 32-bit code, page 16-6)
939
940         .byte 0x66, 0xea                        # prefix + jmpi-opcode
941 code32: .long   0x1000                          # will be set to 0x100000
942                                                 # for big kernels
943         .word   __BOOT_CS
944
945 # Here's a bunch of information about your current kernel..
946 kernel_version: .ascii  UTS_RELEASE
947                 .ascii  " ("
948                 .ascii  LINUX_COMPILE_BY
949                 .ascii  "@"
950                 .ascii  LINUX_COMPILE_HOST
951                 .ascii  ") "
952                 .ascii  UTS_VERSION
953                 .byte   0
954
955 # This is the default real mode switch routine.
956 # to be called just before protected mode transition
957 default_switch:
958         cli                                     # no interrupts allowed !
959         movb    $0x80, %al                      # disable NMI for bootup
960                                                 # sequence
961         outb    %al, $0x70
962         lret
963
964 # This routine only gets called, if we get loaded by the simple
965 # bootsect loader _and_ have a bzImage to load.
966 # Because there is no place left in the 512 bytes of the boot sector,
967 # we must emigrate to code space here.
968 bootsect_helper:
969         cmpw    $0, %cs:bootsect_es
970         jnz     bootsect_second
971
972         movb    $0x20, %cs:type_of_loader
973         movw    %es, %ax
974         shrw    $4, %ax
975         movb    %ah, %cs:bootsect_src_base+2
976         movw    %es, %ax
977         movw    %ax, %cs:bootsect_es
978         subw    $SYSSEG, %ax
979         lret                                    # nothing else to do for now
980
981 bootsect_second:
982         pushw   %cx
983         pushw   %si
984         pushw   %bx
985         testw   %bx, %bx                        # 64K full?
986         jne     bootsect_ex
987
988         movw    $0x8000, %cx                    # full 64K, INT15 moves words
989         pushw   %cs
990         popw    %es
991         movw    $bootsect_gdt, %si
992         movw    $0x8700, %ax
993         int     $0x15
994         jc      bootsect_panic                  # this, if INT15 fails
995
996         movw    %cs:bootsect_es, %es            # we reset %es to always point
997         incb    %cs:bootsect_dst_base+2         # to 0x10000
998 bootsect_ex:
999         movb    %cs:bootsect_dst_base+2, %ah
1000         shlb    $4, %ah                         # we now have the number of
1001                                                 # moved frames in %ax
1002         xorb    %al, %al
1003         popw    %bx
1004         popw    %si
1005         popw    %cx
1006         lret
1007
1008 bootsect_gdt:
1009         .word   0, 0, 0, 0
1010         .word   0, 0, 0, 0
1011
1012 bootsect_src:
1013         .word   0xffff
1014
1015 bootsect_src_base:
1016         .byte   0x00, 0x00, 0x01                # base = 0x010000
1017         .byte   0x93                            # typbyte
1018         .word   0                               # limit16,base24 =0
1019
1020 bootsect_dst:
1021         .word   0xffff
1022
1023 bootsect_dst_base:
1024         .byte   0x00, 0x00, 0x10                # base = 0x100000
1025         .byte   0x93                            # typbyte
1026         .word   0                               # limit16,base24 =0
1027         .word   0, 0, 0, 0                      # BIOS CS
1028         .word   0, 0, 0, 0                      # BIOS DS
1029
1030 bootsect_es:
1031         .word   0
1032
1033 bootsect_panic:
1034         pushw   %cs
1035         popw    %ds
1036         cld
1037         leaw    bootsect_panic_mess, %si
1038         call    prtstr
1039         
1040 bootsect_panic_loop:
1041         jmp     bootsect_panic_loop
1042
1043 bootsect_panic_mess:
1044         .string "INT15 refuses to access high mem, giving up."
1045
1046
1047 #ifndef CONFIG_X86_VOYAGER
1048 # This routine tests whether or not A20 is enabled.  If so, it
1049 # exits with zf = 0.
1050 #
1051 # The memory address used, 0x200, is the int $0x80 vector, which
1052 # should be safe.
1053
1054 A20_TEST_ADDR = 4*0x80
1055
1056 a20_test:
1057         pushw   %cx
1058         pushw   %ax
1059         xorw    %cx, %cx
1060         movw    %cx, %fs                        # Low memory
1061         decw    %cx
1062         movw    %cx, %gs                        # High memory area
1063         movw    $A20_TEST_LOOPS, %cx
1064         movw    %fs:(A20_TEST_ADDR), %ax
1065         pushw   %ax
1066 a20_test_wait:
1067         incw    %ax
1068         movw    %ax, %fs:(A20_TEST_ADDR)
1069         call    delay                           # Serialize and make delay constant
1070         cmpw    %gs:(A20_TEST_ADDR+0x10), %ax
1071         loope   a20_test_wait
1072
1073         popw    %fs:(A20_TEST_ADDR)
1074         popw    %ax
1075         popw    %cx
1076         ret     
1077
1078 #endif /* CONFIG_X86_VOYAGER */
1079
1080 # This routine checks that the keyboard command queue is empty
1081 # (after emptying the output buffers)
1082 #
1083 # Some machines have delusions that the keyboard buffer is always full
1084 # with no keyboard attached...
1085 #
1086 # If there is no keyboard controller, we will usually get 0xff
1087 # to all the reads.  With each IO taking a microsecond and
1088 # a timeout of 100,000 iterations, this can take about half a
1089 # second ("delay" == outb to port 0x80). That should be ok,
1090 # and should also be plenty of time for a real keyboard controller
1091 # to empty.
1092 #
1093
1094 empty_8042:
1095         pushl   %ecx
1096         movl    $100000, %ecx
1097
1098 empty_8042_loop:
1099         decl    %ecx
1100         jz      empty_8042_end_loop
1101
1102         call    delay
1103
1104         inb     $0x64, %al                      # 8042 status port
1105         testb   $1, %al                         # output buffer?
1106         jz      no_output
1107
1108         call    delay
1109         inb     $0x60, %al                      # read it
1110         jmp     empty_8042_loop
1111
1112 no_output:
1113         testb   $2, %al                         # is input buffer full?
1114         jnz     empty_8042_loop                 # yes - loop
1115 empty_8042_end_loop:
1116         popl    %ecx
1117         ret
1118
1119 # Read the cmos clock. Return the seconds in al
1120 gettime:
1121         pushw   %cx
1122         movb    $0x02, %ah
1123         int     $0x1a
1124         movb    %dh, %al                        # %dh contains the seconds
1125         andb    $0x0f, %al
1126         movb    %dh, %ah
1127         movb    $0x04, %cl
1128         shrb    %cl, %ah
1129         aad
1130         popw    %cx
1131         ret
1132
1133 # Delay is needed after doing I/O
1134 delay:
1135         outb    %al,$0x80
1136         ret
1137
1138 # Descriptor tables
1139 #
1140 # NOTE: The intel manual says gdt should be sixteen bytes aligned for
1141 # efficiency reasons.  However, there are machines which are known not
1142 # to boot with misaligned GDTs, so alter this at your peril!  If you alter
1143 # GDT_ENTRY_BOOT_CS (in asm/segment.h) remember to leave at least two
1144 # empty GDT entries (one for NULL and one reserved).
1145 #
1146 # NOTE: On some CPUs, the GDT must be 8 byte aligned.  This is
1147 # true for the Voyager Quad CPU card which will not boot without
1148 # This directive.  16 byte aligment is recommended by intel.
1149 #
1150         .align 16
1151 gdt:
1152         .fill GDT_ENTRY_BOOT_CS,8,0
1153
1154         .word   0xFFFF                          # 4Gb - (0x100000*0x1000 = 4Gb)
1155         .word   0                               # base address = 0
1156         .word   0x9A00                          # code read/exec
1157         .word   0x00CF                          # granularity = 4096, 386
1158                                                 #  (+5th nibble of limit)
1159
1160         .word   0xFFFF                          # 4Gb - (0x100000*0x1000 = 4Gb)
1161         .word   0                               # base address = 0
1162         .word   0x9200                          # data read/write
1163         .word   0x00CF                          # granularity = 4096, 386
1164                                                 #  (+5th nibble of limit)
1165 gdt_end:
1166         .align  4
1167         
1168         .word   0                               # alignment byte
1169 idt_48:
1170         .word   0                               # idt limit = 0
1171         .word   0, 0                            # idt base = 0L
1172
1173         .word   0                               # alignment byte
1174 gdt_48:
1175         .word   gdt_end - gdt - 1               # gdt limit
1176         .word   0, 0                            # gdt base (filled in later)
1177
1178 # Include video setup & detection code
1179
1180 #include "video.S"
1181
1182 # Setup signature -- must be last
1183 setup_sig1:     .word   SIG1
1184 setup_sig2:     .word   SIG2
1185
1186 # After this point, there is some free space which is used by the video mode
1187 # handling code to store the temporary mode table (not used by the kernel).
1188
1189 modelist:
1190
1191 .text
1192 endtext:
1193 .data
1194 enddata:
1195 .bss
1196 endbss: