1 ; Boot sector for GeekOS
2 ; Copyright (c) 2001,2004 David H. Hovemeyer <daveho@cs.umd.edu>
3 ; Copyright (c) 2003, Jeffrey K. Hollingsworth <hollings@cs.umd.edu>
4 ; Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu
5 ; Copyright (c) 2008, Peter Dinda <pdinda@northwestern.edu
8 ; This is free software. You are permitted to use,
9 ; redistribute, and modify it as specified in the file "COPYING".
11 ; Loads setup code and a program image from sectors 1..n of a floppy
12 ; and executes the setup code (which will in turn execute
15 ; Some of this code is adapted from Kernel Toolkit 0.2
16 ; and Linux version 2.2.x, so the following copyrights apply:
18 ; Copyright (C) 1991, 1992 Linus Torvalds
19 ; modified by Drew Eckhardt
20 ; modified by Bruce Evans (bde)
21 ; adapted for Kernel Toolkit by Luigi Sgro
25 ; Pad to desired offset from start symbol.
26 ; Usage: Pad_From_Symbol offset, symbol
27 %macro Pad_From_Symbol 2
28 times (%1 - ($ - %2)) db 0
31 ; ----------------------------------------------------------------------
33 ; ----------------------------------------------------------------------
38 BeginText: ; needed to calculate padding bytes to fill the sector
39 ; Copy the boot sector into INITSEG.
41 mov ds, ax ; source segment for string copy
42 xor si, si ; source index for string copy
44 mov es, ax ; destination segment for string copy
45 xor di, di ; destination index for string copy
46 cld ; clear direction flag
47 mov cx, 256 ; number of words to copy
48 rep movsw ; copy 512 bytes
50 jmp INITSEG:after_move
53 ; Now we're executing in INITSEG
56 ; We want the data segment to refer to INITSEG
57 ; (since we've defined variables in the same place as the code)
58 mov ds, ax ; ax still contains INITSEG
60 ; Put the stack in the place where we were originally loaded.
61 ; By definition, there is nothing important there now.
64 mov sp, (BOOTSEG << 4) + 512 - 2
68 ; Load the setup code.
69 mov ax, word [setupStart]
70 mov word [sec_count], ax
72 mov word [max_sector], ax
75 push ax ; 1st param to ReadSector (log sec num)
76 push word SETUPSEG ; 2nd param to ReadSector (seg base)
77 sub ax, [setupStart] ; convert to 0-indexed
78 shl ax, 9 ; multiply by 512
79 push ax ; ...to get 3rd param (byte offset)
81 ; read the sector from the floppy
83 add sp, 6 ; clear 3 word params
89 mov bx, word [max_sector]
90 cmp word [sec_count], bx
96 ; Load the kernel image from sectors KERN_START_SEC..n of the
97 ; floppy into memory at KERNSEG. Note that there are 128 sectors
98 ; per 64K segment. So, when figuring out which segment to
99 ; load the sector into, we shift right by 7 bits (which is
100 ; equivalent to dividing by 128).
102 ; Figure out start sector and max sector
104 mov ax, word [kernelStart]
105 mov word [sec_count], aX
106 add ax, word [kernelSize]
107 mov word [max_sector], ax
112 mov ax, [sec_count] ; logical sector on the floppy
113 push ax ; 1st param to ReadSector (log sec num)
119 mov ax, VMSEG ; ...to get base relative to VMSEG
120 push ax ; 2nd param to ReadSector (seg base)
126 mov ax, 2000h ; Always write at the start of the segment
127 push ax ; 3rd parameter
133 ; read the sector from the floppy
135 add sp, 6 ; clear 3 word params
140 ; mov dx, word [es:2000h] ;
179 ; update the low->high copy table for the bios
180 ; mov ax, word [bootsect_src_base] ;
184 ; adc byte [bootsect_src_base+2], 0
185 ; mov word [bootsect_src_base],ax
187 mov ax, word [bootsect_dst_base] ;
189 adc byte [bootsect_dst_base+2], 0
190 mov word [bootsect_dst_base],ax
192 ; have we loaded all of the sectors?
194 mov bx, word [max_sector]
195 cmp word [sec_count], bx
203 ; Now we've loaded the setup code and the kernel image.
204 ; Jump to setup code.
207 ; Read a sector from the floppy drive.
208 ; This code (and the rest of this boot sector) will have to
209 ; be re-written at some point so it reads more than one
213 ; - "logical" sector number [bp+8]
214 ; - destination segment [bp+6]
215 ; - destination offset [bp+4]
217 push bp ; set up stack frame
219 pusha ; save all registers
234 ; Sector = log_sec % SECTORS_PER_TRACK
235 ; Head = (log_sec / SECTORS_PER_TRACK) % HEADS
236 mov ax, [bp+8] ; get logical sector number from stack
237 xor dx, dx ; dx is high part of dividend (== 0)
238 mov bx, SECTORS_PER_TRACK ; divisor
239 div bx ; do the division
240 mov [sec], dx ; sector is the remainder
241 and ax, 1 ; same as mod by HEADS==2 (slight hack)
244 ; Track = log_sec / (SECTORS_PER_TRACK*HEADS)
245 mov ax, [bp+8] ; get logical sector number again
246 xor dx, dx ; dx is high part of dividend
247 mov bx, SECTORS_PER_TRACK*2 ; divisor
248 div bx ; do the division
249 mov [track], ax ; track is quotient
264 ; Now, try to actually read the sector from the floppy,
265 ; retrying up to 3 times.
267 mov [num_retries], byte 0
270 mov ax, [bp+6] ; dest segment...
271 mov es, ax ; goes in es
272 mov ax, (0x02 << 8) | 1 ; function = 02h in ah,
274 mov bx, [track] ; track number...
275 mov ch, bl ; goes in ch
276 mov bx, [sec] ; sector number...
277 mov cl, bl ; goes in cl...
278 inc cl ; but it must be 1-based, not 0-based
279 mov bx, [head] ; head number...
280 mov dh, bl ; goes in dh
281 xor dl, dl ; hard code drive=0
282 mov bx, [bp+4] ; offset goes in bx
283 ; (es:bx points to buffer)
284 ; Call the BIOS Read Diskette Sectors service
287 ; If the carry flag is NOT set, then there was no error
291 ; Error - code stored in ah
294 inc byte [num_retries]
295 cmp byte [num_retries], 3
298 ; If we got here, we failed thrice, so we give up
304 popa ; restore all regisiters
305 pop bp ; leave stack frame
308 ; Include utility routines:
310 ; REPLACED WITH FOLLOWING WHICH MUST BE COMPILED 16 FOR USE IN THIS CODE
313 mov cx, 4 ; 4 hex digits
315 rol dx, 4 ; rotate so that lowest 4 bits are used
316 mov ax, 0E0Fh ; ah = request, al = mask for nybble
318 add al, 90h ; convert al to ascii hex (four instructions)
319 daa ; I've spent 1 hour to understand how it works..
328 PrintNL: ; print CR and NL
339 ; ----------------------------------------------------------------------
341 ; ----------------------------------------------------------------------
343 ; These are used by ReadSector
349 ; Used for loops reading sectors from floppy
362 db 0,0x20,0x08 ; ! base = 0x082000
364 dw 0 ; ! limit16,base24 =0
368 db 0,0,0x10 ; ! base = 0x100000
370 dw 0 ; ! limit16,base24 =0
371 dw 0,0,0,0 ; ! BIOS CS
372 dw 0,0,0,0 ; ! BIOS DS
375 ; Padding to make the PFAT Boot Record sit just before the BIOS signature.
376 ;Pad_From_Symbol PFAT_BOOT_RECORD_OFFSET, BeginText
379 ; Describes how to load the setup program and kernel.
380 ; The default values are appropriate for creating a boot
381 ; floppy by concatenating the boot sector, setup program,
382 ; and kernel image. The buildFat program will change
383 ; these values if the boot floppy is formatted as a PFAT
400 ;; part of pfat boot record
402 dw 1 ; by default, setup is at first sector
404 ;; part of pfat boot record
406 dw NUM_SETUP_SECTORS ; number of sectors in setup
408 ;; part of pfat boot record
410 dw 1+NUM_SETUP_SECTORS ; default start sector for kernel
412 ;; part of pfat boot record
419 ; Finish by writing the BIOS signature to mark this as
420 ; a valid boot sector.
421 Pad_From_Symbol BIOS_SIGNATURE_OFFSET, BeginText
422 Signature dw 0xAA55 ; BIOS controls this to ensure this is a boot sector