1 ; -----------------------------------------------------------------------
3 ; Copyright 1994-2009 H. Peter Anvin - All Rights Reserved
4 ; Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
6 ; This program is free software; you can redistribute it and/or modify
7 ; it under the terms of the GNU General Public License as published by
8 ; the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
9 ; Boston MA 02110-1301, USA; either version 2 of the License, or
10 ; (at your option) any later version; incorporated herein by reference.
12 ; -----------------------------------------------------------------------
17 ; Common boot sector code for harddisk-based Syslinux derivatives.
19 ; Requires macros z[bwd], labels ldlinux_ent, ldlinux_magic, ldlinux_sys
20 ; and constants BS_MAGIC_VER, LDLINUX_MAGIC, retry_count, Sect1Ptr[01]_VAL,
26 ; Some of the things that have to be saved very early are saved
27 ; "close" to the initial stack pointer offset, in order to
28 ; reduce the code size...
31 global StackBuf, PartInfo, Hidden, OrigESDI, DriveNumber
32 StackBuf equ STACK_TOP-44-92 ; Start the stack here (grow down - 4K)
35 .gptlen equ PartInfo+16
37 FloppyTable equ PartInfo+76
38 ; Total size of PartInfo + FloppyTable == 76+16 = 92 bytes
39 Hidden equ StackBuf-24 ; Partition offset (qword)
40 OrigFDCTabPtr equ StackBuf-16 ; Original FDC table
41 OrigDSSI equ StackBuf-12 ; DS:SI -> partinfo
42 OrigESDI equ StackBuf-8 ; ES:DI -> $PnP structure
43 DriveNumber equ StackBuf-4 ; Drive number
44 StackHome equ Hidden ; The start of the canonical stack
47 ; Primary entry point. Tempting as though it may be, we can't put the
48 ; initial "cli" here; the jmp opcode in the first byte is part of the
49 ; "magic number" (using the term very loosely) for the DOS superblock.
52 _start: jmp short start ; 2 bytes
55 ; "Superblock" follows -- it's in the boot sector, so it's already
56 ; loaded and ready for us
58 bsOemName db MY_NAME ; The SYS command sets this, so...
62 ; These are the fields we actually care about. We end up expanding them
63 ; all to dword size early in the code, so generate labels for both
64 ; the expanded and unexpanded versions.
67 bx %+ %1 equ SuperInfo+($-superblock)*8+4
72 bx %+ %1 equ SuperInfo+($-superblock)*8
77 bx %+ %1 equ $ ; no expansion for dwords
92 superinfo_size equ ($-superblock)-1 ; How much to expand
96 ; This is as far as FAT12/16 and FAT32 are consistent
98 ; FAT12/16 need 26 more bytes,
99 ; FAT32 need 54 more bytes
101 superblock_len_fat16 equ $-superblock+26
102 superblock_len_fat32 equ $-superblock+54
103 zb 54 ; Maximum needed size
104 superblock_max equ $-superblock
106 SecPerClust equ bxSecPerClust
109 ; Note we don't check the constraints above now; we did that at install
113 cli ; No interrupts yet, please
120 mov sp,StackBuf-2 ; Just below BSS (-2 for alignment)
121 push dx ; Save drive number (in DL)
122 push es ; Save initial ES:DI -> $PnP pointer
124 push ds ; Save original DS:SI -> partinfo
129 ; DS:SI may contain a partition table entry and possibly a GPT entry.
130 ; Preserve it for us. This saves 56 bytes of the GPT entry, which is
131 ; currently the maximum we care about. Total is 76 bytes.
133 mov cl,(16+4+56)/2 ; Save partition info
135 rep movsw ; This puts CX back to zero
137 mov ds,cx ; Now we can initialize DS...
140 ; Now sautee the BIOS floppy info block to that it will support decent-
141 ; size transfers; the floppy block is 11 bytes and is stored in the
142 ; INT 1Eh vector (brilliant waste of resources, eh?)
144 ; Of course, if BIOSes had been properly programmed, we wouldn't have
145 ; had to waste precious space with this code.
148 lfs si,[bx] ; FS:SI -> original fdctab
149 push fs ; Save on stack in case we need to bail
152 ; Save the old fdctab even if hard disk so the stack layout
153 ; is the same. The instructions above do not change the flags
154 and dl,dl ; If floppy disk (00-7F), assume no
160 mov cl,6 ; 12 bytes (CX == 0)
161 ; es:di -> FloppyTable already
162 ; This should be safe to do now, interrupts are off...
163 mov [bx],di ; FloppyTable
164 mov [bx+2],ax ; Segment 0
165 fs rep movsw ; Faster to move words
166 mov cl,[bsSecPerTrack] ; Patch the sector count
169 push ax ; Partition offset == 0
174 int 13h ; Some BIOSes need this
175 ; Using xint13 costs +1B
176 jmp short not_harddisk
178 ; The drive number and possibly partition information was passed to us
179 ; by the BIOS or previous boot loader (MBR). Current "best practice" is to
180 ; trust that rather than what the superblock contains.
182 ; Note: di points to beyond the end of PartInfo
183 ; Note: false negatives might slip through the handover area's sanity checks,
184 ; if the region is very close (less than a paragraph) to
185 ; PartInfo ; no false positives are possible though
188 mov dx,[di-76-10] ; Original DS
189 mov si,[di-76-12] ; Original SI
192 cmp dx,4fh ; DS:SI < 50h:0 (BDA or IVT) ?
194 cmp dx,(PartInfo-75)>>4 ; DS:SI in overwritten memory?
196 test byte [di-76],7Fh ; Sanity check: "active flag" should
197 jnz .no_partition ; be 00 or 80
198 cmp [di-76+4],cl ; Sanity check: partition type != 0
200 cmp eax,'!GPT' ; !GPT signature?
202 cmp byte [di-76+4],0EDh ; Synthetic GPT partition entry?
204 .gpt: ; GPT-style partition info
205 push dword [di-76+20+36]
206 push dword [di-76+20+32]
208 .mbr: ; MBR-style partition info
209 push cx ; Upper half partition offset == 0
211 push dword [di-76+8] ; Partition offset (dword)
215 ; No partition table given... assume that the Hidden field in the boot sector
216 ; tells the truth (in particular, is zero if this is an unpartitioned disk.)
220 push dword [bsHidden]
223 ; Get disk drive parameters (don't trust the superblock.) Don't do this for
224 ; floppy drives -- INT 13:08 on floppy drives will (may?) return info about
225 ; what the *drive* supports, not about the *media*. Fortunately floppy disks
226 ; tend to have a fixed, well-defined geometry which is stored in the superblock.
228 ; DL == drive # still
235 inc dx ; Contains # of heads - 1
238 mov [bsSecPerTrack],cx
242 ; Ready to enable interrupts, captain
247 ; Do we have EBIOS (EDD)?
251 mov ah,41h ; EDD existence query
256 test cl,1 ; Extended disk access functionality set
259 ; We have EDD support...
261 mov byte [getonesec.jmp+1],(getonesec_ebios-(getonesec.jmp+2))
265 ; Load the first sector of LDLINUX.SYS; this used to be all proper
266 ; with parsing the superblock and root directory; it doesn't fit
267 ; together with EBIOS support, unfortunately.
270 mov eax,strict dword Sect1Ptr0_VAL ; 0xdeadbeef
272 mov edx,strict dword Sect1Ptr1_VAL ; 0xfeedface
274 mov bx,ldlinux_sys ; Where to load it
277 ; Some modicum of integrity checking
278 cmp dword [ldlinux_magic+4],LDLINUX_MAGIC^HEXDATE
285 ; getonesec: load a single disk linear sector EDX:EAX into the buffer
288 ; This routine assumes CS == DS == SS, and trashes most registers.
290 ; Stylistic note: use "xchg" instead of "mov" when the source is a register
291 ; that is dead from that point; this saves space. However, please keep
292 ; the order to dst,src to keep things sane.
295 add eax,[Hidden] ; Add partition offset
298 .jmp: jmp strict short getonesec_cbios
303 ; getonesec implementation for EBIOS (EDD)
316 mov ah,42h ; Extended Read
319 lea sp,[si+16] ; Remove DAPA
324 ; Some systems seem to get "stuck" in an error state when
325 ; using EBIOS. Doesn't happen when using CBIOS, which is
326 ; good, since some other systems get timeout failures
327 ; waiting for the floppy disk to spin up.
329 pushad ; Try resetting the device
333 loop .retry ; CX-- and jump if not zero
335 ; Total failure. Try falling back to CBIOS.
336 mov byte [getonesec.jmp+1],(getonesec_cbios-(getonesec.jmp+2))
341 ; getlinsec implementation for legacy CBIOS
347 movzx esi,word [bsSecPerTrack]
348 movzx edi,word [bsHeads]
350 ; Dividing by sectors to get (track,sector): we may have
351 ; up to 2^18 tracks, so we need to use 32-bit arithmetric.
355 xchg cx,dx ; CX <- sector index (0-based)
358 div edi ; Convert track to head/cyl
360 cmp eax,1023 ; Outside the CHS range?
364 ; Now we have AX = cyl, DX = head, CX = sector (0-based),
365 ; SI = bsSecPerTrack, ES:BX = data target
367 shl ah,6 ; Because IBM was STOOPID
368 ; and thought 8 bits were enough
369 ; then thought 10 bits were enough...
370 inc cx ; Sector numbers are 1-based, sigh
374 mov ax,0201h ; Read one sector
382 ; Fall through to disk_error
385 ; kaboom: write a message and bail out.
390 global kaboom:function hidden
396 mov sp,OrigFDCTabPtr ; Reset stack
397 mov ds,si ; Reset data segment
398 pop dword [fdctab] ; Restore FDC table
399 .patch: ; When we have full code, intercept here
404 mov ah,0Eh ; Write to screen as TTY
405 mov bx,0007h ; Attribute
411 .again: int 16h ; Wait for keypress
412 ; NB: replaced by int 18h if
413 ; chosen at install time..
414 int 19h ; And try once more to boot...
415 .norge: hlt ; If int 19h returned; this is the end
419 ; INT 13h wrapper function
423 push es ; ES destroyed by INT 13h AH 08h
429 ; Error message on failure
431 bailmsg: db 'Boot error', 0Dh, 0Ah, 0
433 ; This fails if the boot sector overflowsg
436 bs_magic dd LDLINUX_MAGIC
437 bs_link dw (Sect1Load - bootsec) | BS_MAGIC_VER
438 bootsignature dw 0xAA55
441 ; ===========================================================================
443 ; ===========================================================================