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