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