1 ; ****************************************************************************
5 ; A program to boot Linux kernels off a CD-ROM using the El Torito
6 ; boot standard in "no emulation" mode, making the entire filesystem
7 ; available. It is based on the SYSLINUX boot loader for MS-DOS
10 ; Copyright (C) 1994-2001 H. Peter Anvin
12 ; This program is free software; you can redistribute it and/or modify
13 ; it under the terms of the GNU General Public License as published by
14 ; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
15 ; USA; either version 2 of the License, or (at your option) any later
16 ; version; incorporated herein by reference.
18 ; ****************************************************************************
20 ; THIS FILE IS A MODIFIED VERSION OF ISOLINUX.ASM
21 ; MODIFICATION DONE BY MICHAEL K TER LOUW
22 ; LAST UPDATED 3-9-2002
23 ; SEE "COPYING" FOR INFORMATION ABOUT THE LICENSE THAT APPLIES TO THIS RELEASE
25 ; ****************************************************************************
27 ; This file is a modified version of ISOLINUX.ASM.
28 ; Modification done by Eric Kohl
29 ; Last update 04-25-2002
31 ; ****************************************************************************
33 ; Note: The Makefile builds one version with DEBUG_MESSAGES automatically.
34 ;%define DEBUG_MESSAGES ; Uncomment to get debugging messages
39 ; ---------------------------------------------------------------------------
40 ; BEGIN THE BIOS/CODE/DATA SEGMENT
41 ; ---------------------------------------------------------------------------
44 serial_base resw 4 ; Base addresses for 4 serial ports
46 BIOS_fbm resw 1 ; Free Base Memory (kilobytes)
48 BIOS_timer resw 1 ; Timer ticks
50 BIOS_magic resw 1 ; BIOS reset magic
52 BIOS_vidrows resb 1 ; Number of screen rows
55 ; Memory below this point is reserved for the BIOS and the MBR
58 trackbuf resb 8192 ; Track buffer goes here
59 trackbufsize equ $-trackbuf
60 ; trackbuf ends at 3000h
63 file_sector resd 1 ; Sector pointer (0 = structure free)
64 file_left resd 1 ; Number of sectors left
68 dir_lba resd 1 ; Directory start (LBA)
69 dir_len resd 1 ; Length in bytes
70 dir_clust resd 1 ; Length in clusters
74 MAX_OPEN_LG2 equ 2 ; log2(Max number of open files)
75 MAX_OPEN equ (1 << MAX_OPEN_LG2)
76 SECTORSIZE_LG2 equ 11 ; 2048 bytes/sector (El Torito requirement)
77 SECTORSIZE equ (1 << SECTORSIZE_LG2)
78 CR equ 13 ; Carriage Return
80 retry_count equ 6 ; How patient are we with the BIOS?
84 absolute 5000h ; Here we keep our BSS stuff
86 DriveNo resb 1 ; CD-ROM BIOS drive number
87 DiskError resb 1 ; Error code for disk I/O
88 RetryCount resb 1 ; Used for disk access retries
89 TimeoutCount resb 1 ; Timeout counter
90 ISOFlags resb 1 ; Flags for ISO directory search
91 RootDir resb dir_t_size ; Root directory
92 CurDir resb dir_t_size ; Current directory
93 ISOFileName resb 64 ; ISO filename canonicalization buffer
97 alignb open_file_t_size
98 Files resb MAX_OPEN*open_file_t_size
106 cli ; Disable interrupts
107 xor ax, ax ; ax = segment zero
108 mov ss, ax ; Initialize stack segment
109 mov sp, start ; Set up stack
110 mov ds, ax ; Initialize other segment registers
114 sti ; Enable interrupts
115 cld ; Increment pointers
117 mov cx, 2048 >> 2 ; Copy the bootsector
118 mov si, 0x7C00 ; from 0000:7C00
119 mov di, 0x7000 ; to 0000:7000
120 rep movsd ; copy the program
121 jmp 0:relocate ; jump into relocated code
124 ; Display the banner and copyright
125 %ifdef DEBUG_MESSAGES
126 mov si, isolinux_banner ; si points to hello message
127 call writestr ; display the message
133 ; Make sure the keyboard buffer is empty
135 call pollchar_and_empty
137 ; Check for MBR on harddisk
145 jc .boot_cdrom ; could not read hdd
148 mov ax, word [trackbuf]
150 je .boot_cdrom ; no boot sector found (hopefully there are no weird bootsectors which begin with 0)
153 ; Display the 'Press key' message and wait for a maximum of 5 seconds
155 mov si, presskey_msg ; si points to 'Press key' message
156 call writestr ; display the message
158 mov byte [TimeoutCount], 5
160 mov eax, [BIOS_timer] ; load current tick counter
164 call pollchar_and_empty
167 mov ebx, [BIOS_timer]
171 mov si, dot_msg ; print '.'
173 dec byte [TimeoutCount] ; decrement timeout counter
180 ; Boot first harddisk (drive 0x80)
205 ; Save and display the boot drive number
207 %ifdef DEBUG_MESSAGES
215 ; Now figure out what we're actually doing
216 ; Note: use passed-in DL value rather than 7Fh because
217 ; at least some BIOSes will get the wrong value otherwise
218 mov ax, 4B01h ; Get disk emulation status
222 jc near spec_query_failed ; Shouldn't happen (BIOS bug)
224 cmp [sp_drive], dl ; Should contain the drive number
225 jne near spec_query_failed
227 %ifdef DEBUG_MESSAGES
230 mov al, byte [sp_drive]
236 ; Get drive information
243 ; mov si, nosecsize_msg No use in reporting this
247 ; Check for the sector size (should be 2048, but
248 ; some BIOSes apparently think we're 512-byte media)
250 ; FIX: We need to check what the proper behaviour
251 ; is for getlinsec when the BIOS thinks the sector
252 ; size is 512!!! For that, we need such a BIOS, though...
253 %ifdef DEBUG_MESSAGES
263 ; Clear Files structures
266 mov cx, (MAX_OPEN*open_file_t_size)/4
271 ; Now, we need to sniff out the actual filesystem data structures.
272 ; mkisofs gave us a pointer to the primary volume descriptor
273 ; (which will be at 16 only for a single-session disk!); from the PVD
274 ; we should be able to find the rest of what we need to know.
277 mov eax, 16 ; Primary Volume Descriptor (sector 16)
281 mov eax, [trackbuf+156+2]
282 mov [RootDir+dir_lba],eax
283 mov [CurDir+dir_lba],eax
284 %ifdef DEBUG_MESSAGES
291 mov eax,[trackbuf+156+10]
292 mov [RootDir+dir_len],eax
293 mov [CurDir+dir_len],eax
294 %ifdef DEBUG_MESSAGES
301 shr eax,SECTORSIZE_LG2
302 mov [RootDir+dir_clust],eax
303 mov [CurDir+dir_clust],eax
304 %ifdef DEBUG_MESSAGES
311 ; Look for the "REACTOS" directory, and if found,
312 ; make it the current directory instead of the root
315 mov al,02h ; Search for a directory
323 mov [CurDir+dir_len],eax
324 mov eax,[si+file_left]
325 mov [CurDir+dir_clust],eax
326 xor eax,eax ; Free this file pointer entry
327 xchg eax,[si+file_sector]
328 mov [CurDir+dir_lba],eax
331 mov di, isolinux_bin ; di points to Isolinux filename
332 call searchdir ; look for the file
333 jnz .isolinux_opened ; got the file
334 mov si, no_isolinux_msg ; si points to error message
335 call writemsg ; display the message
336 jmp kaboom ; fail boot
339 mov di, si ; save file pointer
341 %ifdef DEBUG_MESSAGES
348 mov ecx, eax ; calculate sector count
355 %ifdef DEBUG_MESSAGES
363 ; use high segment, as some bios can fail, when offset is too big
364 mov bx, 0x0F80 ; FREELDR_BASE / 16 ; es = load segment
366 xor ebx, ebx ; bx = load offset
367 mov si, di ; restore file pointer
368 mov cx, 0xFFFF ; load the whole file
369 call getfssec ; get the whole file
371 %ifdef DEBUG_MESSAGES
377 mov dl, [DriveNo] ; dl = boot drive
378 mov dh, 0 ; dh = boot partition
380 ; Transfer execution to the bootloader
381 ;ljmp16 0, FREELDR_BASE
396 ; DX:AX or EAX = file length in bytes
402 ; searchdir_iso is a special entry point for ISOLINUX only. In addition
403 ; to the above, searchdir_iso passes a file flag mask in AL. This is useful
404 ; for searching for directories.
414 call allocate_file ; Temporary file structure for directory
420 cmp byte [di],'\' ; If filename begins with slash
422 inc di ; Skip leading slash
423 mov si,RootDir ; Reference root directory instead
425 mov eax,[si+dir_clust]
426 mov [bx+file_left],eax
428 mov [bx+file_sector],eax
440 mov [di-1],byte 0 ; Terminate at directory name
441 mov cl,02h ; Search for directory
445 push word .resume ; Where to "return" to
451 ; Get a chunk of the directory
455 mov cx,1 ; load one sector
460 movzx eax, byte [si] ; Length of directory entry
465 test cl, byte 8Eh ; Unwanted file attributes!
468 movzx cx, byte [si+32] ; File identifier length
469 add si, byte 33 ; File identifier offset
470 call iso_compare_names
474 sub edx, eax ; Decrease bytes left
476 add si, ax ; Advance pointer
479 ; Did we finish the buffer?
480 cmp si, trackbuf+trackbufsize
481 jb .compare ; No, keep going
483 jmp short .getsome ; Get some more directory
486 ; Advance to the beginning of next sector
487 lea ax, [si+SECTORSIZE-1]
488 and ax, ~(SECTORSIZE-1)
490 jmp short .not_file ; We still need to do length checks
493 %ifdef DEBUG_MESSAGES
498 xor eax, eax ; ZF = 1
499 mov [bx+file_sector], eax
504 mov eax, [si+2] ; Location of extent
505 mov [bx+file_sector], eax
506 mov eax, [si+10] ; Data length
508 add eax, SECTORSIZE-1
509 shr eax, SECTORSIZE_LG2
510 mov [bx+file_left], eax
520 ; We get here if we were only doing part of a lookup
521 ; This relies on the fact that .success returns bx == si
522 xchg edx, eax ; Directory length in edx
523 pop cx ; Old ISOFlags
524 pop di ; Next filename pointer
526 mov byte [di-1], '\' ; restore the backslash in the filename
528 mov [ISOFlags], cl ; Restore the flags
529 jz .failure ; Did we fail? If so fail for real!
530 jmp .look_for_slash ; Otherwise, next level
533 ; allocate_file: Allocate a file structure
546 cmp dword [bx], byte 0
548 add bx, open_file_t_size ; ZF = 0
550 ; ZF = 0 if we fell out of the loop
557 ; Compare the names DS:SI and DS:DI and report if they are
558 ; equal from an ISO 9660 perspective. SI is the name from
559 ; the filesystem; CX indicates its length, and ';' terminates.
560 ; DI is expected to end with a null.
562 ; Note: clobbers AX, CX, SI, DI; assumes DS == ES == base segment
565 ; First, terminate and canonicalize input filename
577 cmp di, ISOFileNameEnd-1 ; Guard against buffer overrun
582 cmp byte [di-1], '.' ; Remove terminal dots
587 mov [di], byte 0 ; Null-terminate string
595 jz .success ; End of string for both
596 and al, al ; Is either one end of string?
597 jz .failure ; If so, failure
600 or ax, 2020h ; Convert to lower case
604 and ax, ax ; ZF = 0 (at least one will be nonzero)
615 ; getfssec: Get multiple clusters from a file, given the file pointer.
620 ; CX -> Cluster count; 0FFFFh = until end of file
622 ; SI -> File pointer (or 0 on EOF)
626 cmp cx, [si+file_left]
628 mov cx, [si+file_left]
634 mov eax, [si+file_sector]
640 add [si+file_sector], ecx
641 sub [si+file_left], ecx
645 mov [si+file_sector], ecx ; Mark as unused
654 ; INT 13h, AX=4B01h, DL=<passed in value> failed.
655 ; Try to scan the entire 80h-FFh from the end.
665 mov byte [si], 13 ; Size of buffer
676 cmp byte [sp_drive], dl
679 ; Okay, good enough...
686 ; Award BIOS 4.51 apparently passes garbage in sp_drive,
687 ; but if this was the drive number originally passed in
688 ; DL then consider it "good enough"
690 cmp byte [DriveNo], dl
707 ; Information message (DS:SI) output
708 ; Prefix with "isolinux: "
721 ; crlf: Print a newline
728 ; writestr: write a null-terminated string to the console, saving
729 ; registers on entry.
747 ; writehex[248]: Write a hex number in (AL, AX, EAX) to the console
754 jmp short writehex_common
760 jmp short writehex_common
786 ; Write a character to the screen. There is a more "sophisticated"
787 ; version of this in the subsequent code, so we patch the pointer
802 ; Get one sector. Convenience entry point.
806 ; Fall through to getlinsec
809 ; Get linear sectors - EBIOS LBA addressing, 2048-byte sectors.
811 ; Note that we can't always do this as a single request, because at least
812 ; Phoenix BIOSes has a 127-sector limit. To be on the safe side, stick
813 ; to 32 sectors (64K) per request.
816 ; EAX - Linear sector number
817 ; ES:BX - Target buffer
821 mov si,dapa ; Load up the DAPA
827 push bp ; Sectors left
835 mov ah,42h ; Extended Read
839 movzx eax,word [si+2] ; Sectors we read
840 add [si+8],eax ; Advance sector pointer
841 sub bp,ax ; Sectors left
842 shl ax,SECTORSIZE_LG2-4 ; 2048-byte sectors -> segment
843 add [si+6],ax ; Advance buffer pointer
846 mov eax,[si+8] ; Next sector
851 mov byte [RetryCount], retry_count
856 add sp, byte 8*4 ; Clean up stack
859 mov [DiskError], ah ; Save error code
861 dec byte [RetryCount]
865 mov ah,[dapa+2] ; Sector transfer count
866 cmp al,2 ; Only 2 attempts left
868 mov ah,1 ; Drop transfer size to 1
872 ja .again ; First time, just try again
873 shr ah,1 ; Otherwise, try to reduce
874 adc ah,0 ; the max transfer size, but not to 0
892 ; Fall through to kaboom
895 ; kaboom: write a message and bail out. Wait for a user keypress,
896 ; then do a hard reboot.
905 mov si, err_bootfailed
907 xor ax, ax ; Wait for keypress
910 mov word [BIOS_magic], 0 ; Cold reboot
911 jmp 0F000h:0FFF0h ; Reset vector address
915 ; pollchar_and_empty: check if we have an input character pending (ZF = 0) and empty the input buffer afterwards
919 mov ah, 1 ; Did the user press a key?
921 jz .end ; No, then we're done
922 mov ah, 0 ; Otherwise empty the buffer by reading it
930 isolinux_banner db CR, LF, 'Loading IsoBoot...', CR, LF, 0
931 copyright_str db ' (C) 1994-2002 H. Peter Anvin', CR, LF, 0
932 presskey_msg db 'Press any key to boot from CD', 0
935 %ifdef DEBUG_MESSAGES
936 startup_msg: db 'Startup, DL = ', 0
937 spec_ok_msg: db 'packet OK, drive = ', 0
938 secsize_msg: db 'size appears to be ', 0
939 rootloc_msg: db 'Root dir loc: ', 0
940 rootlen_msg: db 'Root dir len: ', 0
941 rootsect_msg: db 'Root dir len(sect): ', 0
942 fileloc_msg: db 'SETUPLDR loc: ', 0
943 filelen_msg: db 'SETUPLDR len: ', 0
944 filesect_msg: db 'SETUPLDR len(sect): ', 0
945 findfail_msg: db 'Failed to find file!', 0
946 startldr_msg: db 'Starting SETUPLDR.SYS', 0
949 ; nosecsize_msg: db 'No sector size, assume 0800', CR, LF, 0
950 spec_err_msg: db 'Load spec failed, trying wing ...', CR, LF, 0
951 maybe_msg: db 'Found smth at drive = ', 0
952 alright_msg: db 'might be ok, continuing...', CR, LF, 0
953 nothing_msg: db 'Failed locate CD-ROM; boot failed.', CR, LF, 0
954 isolinux_str db 'IsoBoot: ', 0
955 crlf_msg db CR, LF, 0
956 diskerr_msg: db 'Disk error ', 0
957 ondrive_str: db ', drive ', 0
958 err_bootfailed db CR, LF, 'failed..', 0
959 isolinux_dir db '\LOADER', 0
960 no_dir_msg db 'LOADER dir not found.', CR, LF, 0
961 isolinux_bin db 'SETUPLDR.SYS', 0
962 no_isolinux_msg db 'SETUPLDR not found.', CR, LF, 0
965 ; El Torito spec packet
968 spec_packet: db 13h ; Size of packet
969 sp_media: db 0 ; Media type
970 sp_drive: db 0 ; Drive number
971 sp_controller: db 0 ; Controller index
972 sp_lba: dd 0 ; LBA for emulated disk image
973 sp_devspec: dw 0 ; IDE/SCSI information
974 sp_buffer: dw 0 ; User-provided buffer
975 sp_loadseg: dw 0 ; Load segment
976 sp_sectors: dw 0 ; Sector count
977 sp_chs: db 0,0,0 ; Simulated CHS geometry
978 sp_dummy: db 0 ; Scratch, safe to overwrite
981 ; EBIOS drive parameter packet
984 drive_params: dw 30 ; Buffer size
985 dp_flags: dw 0 ; Information flags
986 dp_cyl: dd 0 ; Physical cylinders
987 dp_head: dd 0 ; Physical heads
988 dp_sec: dd 0 ; Physical sectors/track
989 dp_totalsec: dd 0,0 ; Total sectors
990 dp_secsize: dw 0 ; Bytes per sector
991 dp_dpte: dd 0 ; Device Parameter Table
992 dp_dpi_key: dw 0 ; 0BEDDh if rest valid
993 dp_dpi_len: db 0 ; DPI len
996 dp_bus: times 4 db 0 ; Host bus type
997 dp_interface: times 8 db 0 ; Interface type
998 db_i_path: dd 0,0 ; Interface path
999 db_d_path: dd 0,0 ; Device path
1001 db_dpi_csum: db 0 ; Checksum for DPI info
1004 ; EBIOS disk address packet
1007 dapa: dw 16 ; Packet size
1008 .count: dw 0 ; Block count
1009 .off: dw 0 ; Offset of buffer
1010 .seg: dw 0 ; Segment of buffer
1011 .lba: dd 0 ; LBA (LSW)
1015 MaxTransfer dw 2 ;32 ; Max sectors per transfer
1017 times 2046-($-$$) db 0 ; Pad to file offset 2046
1018 dw 0aa55h ; BootSector signature