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 push ax
82
83 mov ss,ax ! put stack at INITSEG:0x4000-12.
84 mov sp,dx
85 /* 86 * Many BIOS's default disk parameter tables will not 87 * recognize multi-sector reads beyond the maximum sector number 88 * specified in the default diskette parameter tables - this may 89 * mean 7 sectors in some cases. 90 * 91 * Since single sector reads are slow and out of the question, 92 * we must take care of this by creating new parameter tables 93 * (for the first disk) in RAM. We will set the maximum sector 94 * count to 18 - the most we will encounter on an HD 1.44. 95 * 96 * High doesn't hurt. Low does. 97 * 98 * Segments are as follows: ds=es=ss=cs - INITSEG, 99 * fs = 0, gs = parameter table segment 100 */ 101
102 push #0
103 pop fs
104 mov bx,#0x78 ! fs:bx is parameter table address
105 seg fs
106 lgs si,(bx) ! gs:si is source
107
108 mov di,dx ! es:di is destination
109 mov cx,#6 ! copy 12 bytes
110 cld
111
112 rep
113 seg gs
114 movsw
115
116 mov di,dx
117 movb 4(di),*18 ! patch sector count
118
119 seg fs
120 mov (bx),di
121 seg fs
122 mov 2(bx),es
123
124 pop ax
125 mov fs,ax
126 mov gs,ax
127
128 xor ah,ah ! reset FDC
129 xor dl,dl
130 int 0x13
131
132 ! load the setup-sectors directly after the bootblock.
133 ! Note that 'es' is already set up.
134
135 load_setup:
136 xor dx, dx ! drive 0, head 0
137 mov cx,#0x0002 ! sector 2, track 0
138 mov bx,#0x0200 ! address = 512, in INITSEG
139 mov ax,#0x0200+SETUPSECS ! service 2, nr of sectors
140 ! (assume all on head 0, track 0)
141 int 0x13 ! read it
142 jnc ok_load_setup ! ok - continue
143
144 push ax ! dump error code
145 call print_nl
146 mov bp, sp
147 call print_hex
148 pop ax
149
150 xor dl, dl ! reset FDC
151 xor ah, ah
152 int 0x13
153 jmp load_setup
154
155 ok_load_setup:
156
157 ! Get disk drive parameters, specifically nr of sectors/track
158
159 #if 0
160
161 ! bde - the Phoenix BIOS manual says function 0x08 only works for fixed
162 ! disks. It doesn't work for one of my BIOS's (1987 Award). It was
163 ! fatal not to check the error code.
164
165 xor dl,dl
166 mov ah,#0x08 ! AH=8 is get drive parameters
167 int 0x13
168 xor ch,ch
169 #else 170
171 ! It seems that there is no BIOS call to get the number of sectors. Guess
172 ! 18 sectors if sector 18 can be read, 15 if sector 15 can be read.
173 ! Otherwise guess 9.
174
175 xor dx, dx ! drive 0, head 0
176 mov cx,#0x0012 ! sector 18, track 0
177 mov bx,#0x0200+SETUPSECS*0x200 ! address after setup (es = cs)
178 mov ax,#0x0201 ! service 2, 1 sector
179 int 0x13
180 jnc got_sectors
181 mov cl,#0x0f ! sector 15
182 mov ax,#0x0201 ! service 2, 1 sector
183 int 0x13
184 jnc got_sectors
185 mov cl,#0x09
186
187 #endif 188
189 got_sectors:
190 seg cs
191 mov sectors,cx
192 mov ax,#INITSEG
193 mov es,ax
194
195 ! Print some inane message
196
197 mov ah,#0x03 ! read cursor pos
198 xor bh,bh
199 int 0x10
200
201 mov cx,#9
202 mov bx,#0x0007 ! page 0, attribute 7 (normal)
203 mov bp,#msg1
204 mov ax,#0x1301 ! write string, move cursor
205 int 0x10
206
207 ! ok, we've written the message, now
208 ! we want to load the system (at 0x10000)
209
210 mov ax,#SYSSEG
211 mov es,ax ! segment of 0x010000
212 call read_it
213 call kill_motor
214 call print_nl
215
216 ! After that we check which root-device to use. If the device is
217 ! defined (!= 0), nothing is done and the given device is used.
218 ! Otherwise, either /dev/PS0 (2,28) or /dev/at0 (2,8), depending
219 ! on the number of sectors that the BIOS reports currently.
220
221 seg cs
222 mov ax,root_dev
223 or ax,ax
224 jne root_defined
225 seg cs
226 mov bx,sectors
227 mov ax,#0x0208 ! /dev/ps0 - 1.2Mb
228 cmp bx,#15
229 je root_defined
230 mov ax,#0x021c ! /dev/PS0 - 1.44Mb
231 cmp bx,#18
232 je root_defined
233 mov ax,#0x0200 ! /dev/fd0 - autodetect
234 root_defined:
235 seg cs
236 mov root_dev,ax
237
238 ! after that (everyting loaded), we jump to
239 ! the setup-routine loaded directly after
240 ! the bootblock:
241
242 jmpi 0,SETUPSEG
243
244 ! This routine loads the system at address 0x10000, making sure
245 ! no 64kB boundaries are crossed. We try to load it as fast as
246 ! possible, loading whole tracks whenever we can.
247 !
248 ! in: es - starting address segment (normally 0x1000)
249 !
250 sread: .word 1+SETUPSECS ! sectors read of current track
251 head: .word 0 ! current head
252 track: .word 0 ! current track
253
254 read_it:
255 mov ax,es
256 test ax,#0x0fff
257 die: jne die ! es must be at 64kB boundary
258 xor bx,bx ! bx is starting address within segment
259 rp_read:
260 mov ax,es
261 cmp ax,#ENDSEG ! have we loaded all yet?
262 jb ok1_read
263 ret
264 ok1_read:
265 seg cs
266 mov ax,sectors
267 sub ax,sread
268 mov cx,ax
269 shl cx,#9
270 add cx,bx
271 jnc ok2_read
272 je ok2_read
273 xor ax,ax
274 sub ax,bx
275 shr ax,#9
276 ok2_read:
277 call read_track
278 mov cx,ax
279 add ax,sread
280 seg cs
281 cmp ax,sectors
282 jne ok3_read
283 mov ax,#1
284 sub ax,head
285 jne ok4_read
286 inc track
287 ok4_read:
288 mov head,ax
289 xor ax,ax
290 ok3_read:
291 mov sread,ax
292 shl cx,#9
293 add bx,cx
294 jnc rp_read
295 mov ax,es
296 add ah,#0x10
297 mov es,ax
298 xor bx,bx
299 jmp rp_read
300
301 read_track:
302 pusha
303 pusha
304 mov ax, #0xe2e ! loading... message 2e = .
305 mov bx, #7
306 int 0x10
307 popa
308
309 mov dx,track
310 mov cx,sread
311 inc cx
312 mov ch,dl
313 mov dx,head
314 mov dh,dl
315 and dx,#0x0100
316 mov ah,#2
317
318 push dx ! save for error dump
319 push cx
320 push bx
321 push ax
322
323 int 0x13
324 jc bad_rt
325 add sp, #8
326 popa
327 ret
328
329 bad_rt: push ax ! save error code
330 call print_all ! ah = error, al = read
331
332
333 xor ah,ah
334 xor dl,dl
335 int 0x13
336
337
338 add sp, #10
339 popa
340 jmp read_track
341
342 /* 343 * print_all is for debugging purposes. 344 * It will print out all of the registers. The assumption is that this is 345 * called from a routine, with a stack frame like 346 * dx 347 * cx 348 * bx 349 * ax 350 * error 351 * ret <- sp 352 * 353 */ 354
355 print_all:
356 mov cx, #5 ! error code + 4 registers
357 mov bp, sp
358
359 print_loop:
360 push cx ! save count left
361 call print_nl ! nl for readability
362
363 cmp cl, 5
364 jae no_reg ! see if register name is needed
365
366 mov ax, #0xe05 + 'A - 1
367 sub al, cl
368 int 0x10
369
370 mov al, #'X
371 int 0x10
372
373 mov al, #':
374 int 0x10
375
376 no_reg:
377 add bp, #2 ! next register
378 call print_hex ! print it
379 pop cx
380 loop print_loop
381 ret
382
383 print_nl:
384 mov ax, #0xe0d ! CR
385 int 0x10
386 mov al, #0xa ! LF
387 int 0x10
388 ret
389
390 /* 391 * print_hex is for debugging purposes, and prints the word 392 * pointed to by ss:bp in hexadecmial. 393 */ 394
395 print_hex:
396 mov cx, #4 ! 4 hex digits
397 mov dx, (bp) ! load word into dx
398 print_digit:
399 rol dx, #4 ! rotate so that lowest 4 bits are used
400 mov ah, #0xe
401 mov al, dl ! mask off so we have only next nibble
402 and al, #0xf
403 add al, #'0 ! convert to 0-based digit
404 cmp al, #'9 ! check for overflow
405 jbe good_digit
406 add al, #'A - '0 - 10
407
408 good_digit:
409 int 0x10
410 loop print_digit
411 ret
412
413
414 /* 415 * This procedure turns off the floppy drive motor, so 416 * that we enter the kernel in a known state, and 417 * don't have to worry about it later. 418 */ 419 kill_motor:
420 push dx
421 mov dx,#0x3f2
422 xor al, al
423 outb
424 pop dx
425 ret
426
427 sectors:
428 .word 0
429
430 msg1:
431 .byte 13,10
432 .ascii "Loading"
433
434 .org 502
435 swap_dev:
436 .word SWAP_DEV
437 ram_size:
438 .word RAMDISK
439 vid_mode:
440 .word SVGA_MODE
441 root_dev:
442 .word ROOT_DEV
443 boot_flag:
444 .word 0xAA55