1 !
2 ! SYS_SIZE is the number of clicks (16 bytes) to be loaded.
3 ! 0x3000 is 0x30000 bytes = 196kB, more than enough for current
4 ! versions of linux
5 !
6 #include <linux/config.h>
7 SYSSIZE = DEF_SYSSIZE
8 !
9 ! bootsect.s Copyright (C) 1991, 1992 Linus Torvalds
10 ! modified by Drew Eckhardt
11 ! modified by Bruce Evans (bde)
12 !
13 ! bootsect.s is loaded at 0x7c00 by the bios-startup routines, and moves
14 ! itself out of the way to address 0x90000, and jumps there.
15 !
16 ! bde - should not jump blindly, there may be systems with only 512K low
17 ! memory. Use int 0x12 to get the top of memory, etc.
18 !
19 ! It then loads 'setup' directly after itself (0x90200), and the system
20 ! at 0x10000, using BIOS interrupts.
21 !
22 ! NOTE! currently system is at most 8*65536 bytes long. This should be no
23 ! problem, even in the future. I want to keep it simple. This 512 kB
24 ! kernel size should be enough, especially as this doesn't contain the
25 ! buffer cache as in minix
26 !
27 ! The loader has been made as simple as possible, and continuos
28 ! read errors will result in a unbreakable loop. Reboot by hand. It
29 ! loads pretty fast by getting whole tracks at a time whenever possible.
30
31 .text
32
33 SETUPSECS = 4 ! nr of setup-sectors
34 BOOTSEG = 0x07C0 ! original address of boot-sector
35 INITSEG = DEF_INITSEG ! we move boot here - out of the way
36 SETUPSEG = DEF_SETUPSEG ! setup starts here
37 SYSSEG = DEF_SYSSEG ! system loaded at 0x10000 (65536).
38 ENDSEG = SYSSEG + SYSSIZE ! where to stop loading
39
40 ! ROOT_DEV & SWAP_DEV are now written by "build".
41 ROOT_DEV = 0
42 SWAP_DEV = 0
43 #ifndefSVGA_MODE 44 #defineSVGA_MODE ASK_VGA
45 #endif 46 #ifndefRAMDISK 47 #defineRAMDISK 0
48 #endif 49
50 ! ld86 requires an entry symbol. This may as well be the usual one.
51 .globl _main
52 _main:
53 #if 0 /* hook for debugger, harmless unless BIOS is fussy (old HP) */ 54 int 3
55 #endif 56 mov ax,#BOOTSEG
57 mov ds,ax
58 mov ax,#INITSEG
59 mov es,ax
60 mov cx,#256
61 sub si,si
62 sub di,di
63 cld
64 rep
65 movsw
66 jmpi go,INITSEG
67
68 go: mov ax,cs
69 mov dx,#0x4000-12 ! 0x4000 is arbitrary value >= length of
70 ! bootsect + length of setup + room for stack
71 ! 12 is disk parm size
72
73 ! bde - changed 0xff00 to 0x4000 to use debugger at 0x6400 up (bde). We
74 ! wouldn't have to worry about this if we checked the top of memory. Also
75 ! my BIOS can be configured to put the wini drive tables in high memory
76 ! instead of in the vector table. The old stack might have clobbered the
77 ! drive table.
78
79 mov ds,ax
80 mov es,ax
81 mov ss,ax ! put stack at INITSEG:0x4000-12.
82 mov sp,dx
83 /* 84 * Many BIOS's default disk parameter tables will not 85 * recognize multi-sector reads beyond the maximum sector number 86 * specified in the default diskette parameter tables - this may 87 * mean 7 sectors in some cases. 88 * 89 * Since single sector reads are slow and out of the question, 90 * we must take care of this by creating new parameter tables 91 * (for the first disk) in RAM. We will set the maximum sector 92 * count to 18 - the most we will encounter on an HD 1.44. 93 * 94 * High doesn't hurt. Low does. 95 * 96 * Segments are as follows: ds=es=ss=cs - INITSEG, 97 * fs = 0, gs = parameter table segment 98 */ 99
100 push #0
101 pop fs
102 mov bx,#0x78 ! fs:bx is parameter table address
103 seg fs
104 lgs si,(bx) ! gs:si is source
105
106 mov di,dx ! es:di is destination
107 mov cx,#6 ! copy 12 bytes
108 cld
109
110 rep
111 seg gs
112 movsw
113
114 mov di,dx
115 movb 4(di),*18 ! patch sector count
116
117 seg fs
118 mov (bx),di
119 seg fs
120 mov 2(bx),es
121
122 mov ax,cs
123 mov fs,ax
124 mov gs,ax
125
126 xor ah,ah ! reset FDC
127 xor dl,dl
128 int 0x13
129
130 ! load the setup-sectors directly after the bootblock.
131 ! Note that 'es' is already set up.
132
133 load_setup:
134 xor dx, dx ! drive 0, head 0
135 mov cx,#0x0002 ! sector 2, track 0
136 mov bx,#0x0200 ! address = 512, in INITSEG
137 mov ax,#0x0200+SETUPSECS ! service 2, nr of sectors
138 ! (assume all on head 0, track 0)
139 int 0x13 ! read it
140 jnc ok_load_setup ! ok - continue
141
142 push ax ! dump error code
143 call print_nl
144 mov bp, sp
145 call print_hex
146 pop ax
147
148 xor dl, dl ! reset FDC
149 xor ah, ah
150 int 0x13
151 jmp load_setup
152
153 ok_load_setup:
154
155 ! Get disk drive parameters, specifically nr of sectors/track
156
157 #if 0
158
159 ! bde - the Phoenix BIOS manual says function 0x08 only works for fixed
160 ! disks. It doesn't work for one of my BIOS's (1987 Award). It was
161 ! fatal not to check the error code.
162
163 xor dl,dl
164 mov ah,#0x08 ! AH=8 is get drive parameters
165 int 0x13
166 xor ch,ch
167 #else 168
169 ! It seems that there is no BIOS call to get the number of sectors. Guess
170 ! 18 sectors if sector 18 can be read, 15 if sector 15 can be read.
171 ! Otherwise guess 9.
172
173 xor dx, dx ! drive 0, head 0
174 mov cx,#0x0012 ! sector 18, track 0
175 mov bx,#0x0200+SETUPSECS*0x200 ! address after setup (es = cs)
176 mov ax,#0x0201 ! service 2, 1 sector
177 int 0x13
178 jnc got_sectors
179 mov cl,#0x0f ! sector 15
180 mov ax,#0x0201 ! service 2, 1 sector
181 int 0x13
182 jnc got_sectors
183 mov cl,#0x09
184
185 #endif 186
187 got_sectors:
188 seg cs
189 mov sectors,cx
190 mov ax,#INITSEG
191 mov es,ax
192
193 ! Print some inane message
194
195 mov ah,#0x03 ! read cursor pos
196 xor bh,bh
197 int 0x10
198
199 mov cx,#9
200 mov bx,#0x0007 ! page 0, attribute 7 (normal)
201 mov bp,#msg1
202 mov ax,#0x1301 ! write string, move cursor
203 int 0x10
204
205 ! ok, we've written the message, now
206 ! we want to load the system (at 0x10000)
207
208 mov ax,#SYSSEG
209 mov es,ax ! segment of 0x010000
210 call read_it
211 call kill_motor
212 call print_nl
213
214 ! After that we check which root-device to use. If the device is
215 ! defined (!= 0), nothing is done and the given device is used.
216 ! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
217 ! on the number of sectors that the BIOS reports currently.
218
219 seg cs
220 mov ax,root_dev
221 or ax,ax
222 jne root_defined
223 seg cs
224 mov bx,sectors
225 mov ax,#0x0208 ! /dev/ps0 - 1.2Mb
226 cmp bx,#15
227 je root_defined
228 mov ax,#0x021c ! /dev/PS0 - 1.44Mb
229 cmp bx,#18
230 je root_defined
231 mov ax,#0x0200 ! /dev/fd0 - autodetect
232 root_defined:
233 seg cs
234 mov root_dev,ax
235
236 ! after that (everyting loaded), we jump to
237 ! the setup-routine loaded directly after
238 ! the bootblock:
239
240 jmpi 0,SETUPSEG
241
242 ! This routine loads the system at address 0x10000, making sure
243 ! no 64kB boundaries are crossed. We try to load it as fast as
244 ! possible, loading whole tracks whenever we can.
245 !
246 ! in: es - starting address segment (normally 0x1000)
247 !
248 sread: .word 1+SETUPSECS ! sectors read of current track
249 head: .word 0 ! current head
250 track: .word 0 ! current track
251
252 read_it:
253 mov ax,es
254 test ax,#0x0fff
255 die: jne die ! es must be at 64kB boundary
256 xor bx,bx ! bx is starting address within segment
257 rp_read:
258 mov ax,es
259 cmp ax,#ENDSEG ! have we loaded all yet?
260 jb ok1_read
261 ret
262 ok1_read:
263 seg cs
264 mov ax,sectors
265 sub ax,sread
266 mov cx,ax
267 shl cx,#9
268 add cx,bx
269 jnc ok2_read
270 je ok2_read
271 xor ax,ax
272 sub ax,bx
273 shr ax,#9
274 ok2_read:
275 call read_track
276 mov cx,ax
277 add ax,sread
278 seg cs
279 cmp ax,sectors
280 jne ok3_read
281 mov ax,#1
282 sub ax,head
283 jne ok4_read
284 inc track
285 ok4_read:
286 mov head,ax
287 xor ax,ax
288 ok3_read:
289 mov sread,ax
290 shl cx,#9
291 add bx,cx
292 jnc rp_read
293 mov ax,es
294 add ah,#0x10
295 mov es,ax
296 xor bx,bx
297 jmp rp_read
298
299 read_track:
300 pusha
301 pusha
302 mov ax, #0xe2e ! loading... message 2e = .
303 mov bx, #7
304 int 0x10
305 popa
306
307 mov dx,track
308 mov cx,sread
309 inc cx
310 mov ch,dl
311 mov dx,head
312 mov dh,dl
313 and dx,#0x0100
314 mov ah,#2
315
316 push dx ! save for error dump
317 push cx
318 push bx
319 push ax
320
321 int 0x13
322 jc bad_rt
323 add sp, #8
324 popa
325 ret
326
327 bad_rt: push ax ! save error code
328 call print_all ! ah = error, al = read
329
330
331 xor ah,ah
332 xor dl,dl
333 int 0x13
334
335
336 add sp, #10
337 popa
338 jmp read_track
339
340 /* 341 * print_all is for debugging purposes. 342 * It will print out all of the registers. The assumption is that this is 343 * called from a routine, with a stack frame like 344 * dx 345 * cx 346 * bx 347 * ax 348 * error 349 * ret <- sp 350 * 351 */ 352
353 print_all:
354 mov cx, #5 ! error code + 4 registers
355 mov bp, sp
356
357 print_loop:
358 push cx ! save count left
359 call print_nl ! nl for readability
360
361 cmp cl, 5
362 jae no_reg ! see if register name is needed
363
364 mov ax, #0xe05 + 'A - 1
365 sub al, cl
366 int 0x10
367
368 mov al, #'X
369 int 0x10
370
371 mov al, #':
372 int 0x10
373
374 no_reg:
375 add bp, #2 ! next register
376 call print_hex ! print it
377 pop cx
378 loop print_loop
379 ret
380
381 print_nl:
382 mov ax, #0xe0d ! CR
383 int 0x10
384 mov al, #0xa ! LF
385 int 0x10
386 ret
387
388 /* 389 * print_hex is for debugging purposes, and prints the word 390 * pointed to by ss:bp in hexadecmial. 391 */ 392
393 print_hex:
394 mov cx, #4 ! 4 hex digits
395 mov dx, (bp) ! load word into dx
396 print_digit:
397 rol dx, #4 ! rotate so that lowest 4 bits are used
398 mov ah, #0xe
399 mov al, dl ! mask off so we have only next nibble
400 and al, #0xf
401 add al, #'0 ! convert to 0-based digit
402 cmp al, #'9 ! check for overflow
403 jbe good_digit
404 add al, #'A - '0 - 10
405
406 good_digit:
407 int 0x10
408 loop print_digit
409 ret
410
411
412 /* 413 * This procedure turns off the floppy drive motor, so 414 * that we enter the kernel in a known state, and 415 * don't have to worry about it later. 416 */ 417 kill_motor:
418 push dx
419 mov dx,#0x3f2
420 xor al, al
421 outb
422 pop dx
423 ret
424
425 sectors:
426 .word 0
427
428 msg1:
429 .byte 13,10
430 .ascii "Loading"
431
432 .org 502
433 swap_dev:
434 .word SWAP_DEV
435 ram_size:
436 .word RAMDISK
437 vid_mode:
438 .word SVGA_MODE
439 root_dev:
440 .word ROOT_DEV
441 boot_flag:
442 .word 0xAA55