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 early-bootstrap code for harddisk-based Syslinux derivatives.
20 Sect1Ptr0_VAL equ 0xdeadbeef
21 Sect1Ptr1_VAL equ 0xfeedface
23 %include "diskboot.inc"
25 ; ===========================================================================
26 ; Start of LDLINUX.SYS
27 ; ===========================================================================
29 LDLINUX_SYS equ ($-$$)+TEXT_START
32 early_banner db CR, LF, MY_NAME, ' ', VERSION_STR, ' ', 0
33 db CR, LF, 1Ah ; EOF if we "type" this in DOS
36 ldlinux_magic dd LDLINUX_MAGIC
37 dd LDLINUX_MAGIC^HEXDATE
40 ; This area is patched by the installer. It is found by looking for
41 ; LDLINUX_MAGIC, plus 8 bytes.
44 CURRENTDIR_MAX equ FILENAME_MAX
47 DataSectors dw 0 ; Number of sectors (not including bootsec)
48 ADVSectors dw 0 ; Additional sectors for ADVs
49 LDLDwords dd 0 ; Total dwords starting at ldlinux_sys,
50 CheckSum dd 0 ; Checksum starting at ldlinux_sys
51 ; value = LDLINUX_MAGIC - [sum of dwords]
52 MaxTransfer dw 127 ; Max sectors to transfer
53 EPAPtr dw EPA - LDLINUX_SYS ; Pointer to the extended patch area
56 ; Extended patch area -- this is in .data16 so it doesn't occupy space in
57 ; the first sector. Use this structure for anything that isn't used by
58 ; the first sector itself.
63 ADVSecPtr dw ADVSec0 - LDLINUX_SYS
64 CurrentDirPtr dw CurrentDirName-LDLINUX_SYS ; Current directory name string
65 CurrentDirLen dw CURRENTDIR_MAX
66 SubvolPtr dw SubvolName-LDLINUX_SYS
67 SubvolLen dw SUBVOL_MAX
68 SecPtrOffset dw SectorPtrs-LDLINUX_SYS
69 SecPtrCnt dw (SectorPtrsEnd - SectorPtrs)/10
72 ; Boot sector patch pointers
74 Sect1Ptr0Ptr dw Sect1Ptr0 - bootsec ; Pointers to Sector 1 location
75 Sect1Ptr1Ptr dw Sect1Ptr1 - bootsec
76 RAIDPatchPtr dw kaboom.again - bootsec ; Patch to INT 18h in RAID mode
79 ; Pointer to the Syslinux banner
81 BannerPtr dw syslinux_banner - LDLINUX_SYS
84 ; Base directory name and subvolume, if applicable.
86 %define HAVE_CURRENTDIRNAME
87 global CurrentDirName, SubvolName
88 CurrentDirName times CURRENTDIR_MAX db 0
89 SubvolName times SUBVOL_MAX db 0
94 ; Note that some BIOSes are buggy and run the boot sector at 07C0:0000
95 ; instead of 0000:7C00 and the like. We don't want to add anything
96 ; more to the boot sector, so it is written to not assume a fixed
97 ; value in CS, but we don't want to deal with that anymore from now
100 jmp 0:.next ; Normalize CS:IP
101 .next: sti ; In case of broken INT 13h BIOSes
104 ; Tell the user we got this far
110 ; Checksum data thus far
113 mov cx,SECTOR_SIZE >> 2
114 mov edx,-LDLINUX_MAGIC
119 mov [CheckSum],edx ; Save intermediate result
122 ; Tell the user if we're using EBIOS or CBIOS
126 cmp byte [getonesec.jmp+1],(getonesec_ebios-(getonesec.jmp+2))
129 mov byte [getlinsec.jmp+1],(getlinsec_ebios-(getlinsec.jmp+2))
135 %define HAVE_BIOSNAME 1
140 ; Now we read the rest of LDLINUX.SYS.
144 mov ebx,TEXT_START+2*SECTOR_SIZE ; Where we start loading
146 dec cx ; Minus this sector
152 movzx ebp,word [si+8]
155 shr ebx,4 ; Convert to a segment
168 ; All loaded up, verify that we got what we needed.
169 ; Note: the checksum field is embedded in the checksum region, so
170 ; by the time we get to the end it should all cancel out.
173 mov si,ldlinux_sys + SECTOR_SIZE
175 sub ecx,SECTOR_SIZE >> 2
181 ; Handle segment wrap
191 and eax,eax ; Should be zero
192 jz all_read ; We're cool, go for it!
195 ; Uh-oh, something went bad...
197 mov si,checksumerr_msg
202 ; -----------------------------------------------------------------------------
203 ; Subroutines that have to be in the first sector
204 ; -----------------------------------------------------------------------------
209 ; getlinsec: load a sequence of BP floppy sector given by the linear sector
210 ; number in EAX into the buffer at ES:BX. We try to optimize
211 ; by loading up to a whole track at a time, but the user
212 ; is responsible for not crossing a 64K boundary.
213 ; (Yes, BP is weird for a count, but it was available...)
215 ; On return, BX points to the first byte after the transferred
218 ; This routine assumes CS == DS.
223 add eax,[Hidden] ; Add partition offset
225 .jmp: jmp strict short getlinsec_cbios
230 ; getlinsec implementation for EBIOS (EDD)
234 push bp ; Sectors left
236 call maxtrans ; Enforce maximum transfer size
237 movzx edi,bp ; Sectors we are about to read
250 mov ah,42h ; Extended Read
257 lea sp,[si+16] ; Remove DAPA
260 add eax,edi ; Advance sector pointer
262 sub bp,di ; Sectors left
263 shl di,SECTOR_SHIFT ; 512-byte sectors
264 add bx,di ; Advance buffer pointer
272 ; Some systems seem to get "stuck" in an error state when
273 ; using EBIOS. Doesn't happen when using CBIOS, which is
274 ; good, since some other systems get timeout failures
275 ; waiting for the floppy disk to spin up.
277 pushad ; Try resetting the device
281 loop .retry ; CX-- and jump if not zero
283 ;shr word [MaxTransfer],1 ; Reduce the transfer size
286 ; Total failure. Try falling back to CBIOS.
287 mov byte [getlinsec.jmp+1],(getlinsec_cbios-(getlinsec.jmp+2))
288 ;mov byte [MaxTransfer],63 ; Max possibe CBIOS transfer
291 ; ... fall through ...
296 ; getlinsec implementation for legacy CBIOS
305 movzx esi,word [bsSecPerTrack]
306 movzx edi,word [bsHeads]
308 ; Dividing by sectors to get (track,sector): we may have
309 ; up to 2^18 tracks, so we need to use 32-bit arithmetric.
313 xchg cx,dx ; CX <- sector index (0-based)
316 div edi ; Convert track to head/cyl
318 cmp eax,1023 ; Outside the CHS range?
322 ; Now we have AX = cyl, DX = head, CX = sector (0-based),
323 ; BP = sectors to transfer, SI = bsSecPerTrack,
324 ; ES:BX = data target
327 call maxtrans ; Enforce maximum transfer size
329 ; Must not cross track boundaries, so BP <= SI-CX
336 shl ah,6 ; Because IBM was STOOPID
337 ; and thought 8 bits were enough
338 ; then thought 10 bits were enough...
339 inc cx ; Sector numbers are 1-based, sigh
343 xchg ax,bp ; Sector to transfer count
344 mov ah,02h ; Read sectors
352 movzx ecx,al ; ECX <- sectors transferred
353 shl ax,SECTOR_SHIFT ; Convert sectors in AL to bytes in AX
369 xchg ax,bp ; Sectors transferred <- 0
370 shr word [MaxTransfer],1
382 ; writestr_early: write a null-terminated string to the console
383 ; This assumes we're on page 0. This is only used for early
384 ; messages, so it should be OK.
391 mov ah,0Eh ; Write to screen as TTY
392 mov bx,0007h ; Attribute
399 ; Checksum error message
401 checksumerr_msg db ' Load error - ', 0 ; Boot failed appended
406 cbios_name db 'CHS', 0 ; CHS/CBIOS
407 ebios_name db 'EDD', 0 ; EDD/EBIOS
414 cmp word [Debug_Magic],0D00Dh
419 rl_checkpt equ $ ; Must be <= 8000h
421 rl_checkpt_off equ ($-$$)
423 %if rl_checkpt_off > 3F6h ; Need one extent
424 %assign rl_checkpt_overflow rl_checkpt_off - 3F6h
425 %error Sector 1 overflow by rl_checkpt_overflow bytes
430 ; Extent pointers... each extent contains an 8-byte LBA and an 2-byte
431 ; sector count. In most cases, we will only ever need a handful of
432 ; extents, but we have to assume a maximally fragmented system where each
433 ; extent contains only one sector.
436 MaxInitDataSize equ 96 << 10
437 MaxLMA equ TEXT_START+SECTOR_SIZE+MaxInitDataSize
438 SectorPtrs zb 10*(MaxInitDataSize >> SECTOR_SHIFT)
441 ; ----------------------------------------------------------------------------
442 ; End of code and data that have to be in the first sector
443 ; ----------------------------------------------------------------------------
447 ; We enter here with ES scrambled...
451 ; Let the user (and programmer!) know we got this far. This used to be
452 ; in Sector 1, but makes a lot more sense here.
462 ; Insane hack to expand the DOS superblock to dwords
468 mov cx,superinfo_size
472 stosd ; Store expanded word
474 stosd ; Store expanded byte
479 ; Common initialization code
485 movzx dx,byte [DriveNumber]
486 ; DH = 0: we are boot from disk not CDROM
490 mov di,[bsSecPerTrack]
491 movzx ebp,word [MaxTransfer]
496 SuperInfo resq 16 ; The first 16 bytes expanded 8 times
499 ; Banner information not needed in sector 1
502 syslinux_banner db CR, LF, MY_NAME, ' ', VERSION_STR
503 late_banner db ' ', DATE_STR, 0