wok-next view memtest/stuff/bootloader.S @ rev 19615

Up adwaita-icon-theme.
author Aleksej Bobylev <al.bobylev@gmail.com>
date Sat Feb 04 15:52:11 2017 +0200 (2017-02-04)
parents 50b423f7711b
children
line source
1 SYSSEG = 0x1000
2 INITSEG = 0x9000
3 SETUPSEG = 0x9020
5 .text
6 .code16
7 .org 0
8 .globl _start
9 _start:
11 #define CODESZ 512 /* patched by installer */
13 /* some extra features */
14 #define EXE_SUPPORT real mode dos .exe file support
15 #define CMDLINE 0x9E00
16 #define HELP store help message for /? argument
17 #define CHECK_REALMODE does not support vm86
18 #define SHUTDOWNDOS shutdown DOS services
20 /* some contraints to reduce the size */
21 //#define FLOPPY_1440K_ONLY 1.44M floppies support only
22 #define NO_CURSOR_DEFINITION
24 #ifdef EXE_SUPPORT
25 #define EXEADRS(x) (x+0xE0)
26 stacktop = 0x9E00 # in 0x8000 .. 0xA000
27 decw %bp // Magic number: MZ
28 popw %dx
29 jmp start // Bytes on last page of file
30 .word (CODESZ+511)/512 // Pages in file
31 .word 0 // Relocations
32 .word (end_header-_start)/16 // Size of header in paragraphs
33 .word 4096 // Minimum extra paragraphs needed
34 .word -1 // Maximum extra paragraphs needed
35 .word (CODESZ+15)/16 // Initial (relative) SS value
36 .word stacktop // Initial SP value
37 .word 0 // Checksum
38 .word EXEADRS(comstart) // Initial IP value
39 .word 0xFFF0 // Initial (relative) CS value
40 // .word 0x001C // File address of relocation table
41 // .word 0,0,0 // Overlay number
42 .ascii "(SliTaz)"
43 end_header:
44 comstart:
45 cld # assume nothing
46 #ifdef CMDLINE
47 movw $stacktop, %di # cmdline offset at 0x22
48 #else
49 #undef HELP
50 #endif
51 pushw $INITSEG
52 popw %es
53 #ifdef CMDLINE
54 movw $0x80, %si
55 lodsb
56 cbw
57 xchgw %ax, %cx
58 jcxz nocmdline
59 movw $0xA33F, 0x7F(%si)
60 skipspace:
61 lodsb
62 cmpb $0x20, %al
63 je skipspace
64 decw %si
65 rep
66 movsb
67 # ifdef HELP
68 # define PUTS
69 movw $EXEADRS(helpmsg), %si
70 cmpb $'/'+1, %al
71 # ifdef CHECK_REALMODE
72 js jsputs
73 # else
74 js puts
75 # endif
76 # endif
77 nocmdline:
78 #endif
79 movw $SYSSEG, %ax
80 movb $(512-(end_header-_start))/2, %cl
81 movw $end_header, %di
82 #ifdef CHECK_REALMODE
83 #define PUTS
84 movw $EXEADRS(realmode_expected), %si
85 //movb setup_sects-realmode_expected(%si), %ch
86 .byte 0x8A, 0x6C, setup_sects-realmode_expected
87 // bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
88 // flags 0 NT IOPL OF DF IF TF SF ZF 0 AF 0 PF 1 CF
89 //movb $0x10, %ah // DF = IF = TF = 0
90 pushw %ax
91 popfw // < 286 : flags[12..15] are forced 1
92 pushfw // = 286 : flags[12..15] are forced 0
93 popw %dx // > 286 : only flags[15] is forced 0
94 cmpb %ah, %dh // test Fx and 0x cases: S= < 386, C= < 286
95 jsputs:
96 js puts // S= not 386+
97 smsww %dx
98 shrw $1, %dx
99 jc puts // protected mode: vm86
100 #else
101 movb EXEADRS(setup_sects), %ch
102 #endif
103 movw $0x100, %si
104 rep
105 movsw
106 movw %ds, %bx
107 pushw %es
108 popw %ss
109 #ifndef SHUTDOWNDOS
110 ljmp $INITSEG, $movesys
111 #else
112 pushaw
113 movw %cx, %ds # %ds = 0
114 movw $4, %si
115 pushl (%si)
116 movw $step19, (%si)
117 movw %es, 2(%si)
118 pushfw
119 popw %ax
120 incb %ah # set TF
121 pushw %ax
122 popfw
123 ljmp *4*0x19-4(%si)
124 #endif
125 start:
126 pushw %dx
127 #else
128 #undef HELP
129 #undef CMDLINE
130 #undef CHECK_REALMODE
131 #endif
132 cld # assume nothing
133 stacktop = 0x9E00 # in 0x8000 .. 0xA000
134 zeroed = 12 # zeroed registers
135 movw $stacktop-12-zeroed, %di # stacktop is an arbitrary value >=
136 # length of bootsect + length of
137 # setup + room for stack;
138 # 12 is disk parm size.
139 pushw $INITSEG
140 popw %ss # %ss contain INITSEG
141 movw %di, %sp # put stack at INITSEG:stacktop-...
143 # Many BIOS's default disk parameter tables will not recognize
144 # multi-sector reads beyond the maximum sector number specified
145 # in the default diskette parameter tables - this may mean 7
146 # sectors in some cases.
147 #
148 # Since single sector reads are slow and out of the question,
149 # we must take care of this by creating new parameter tables
150 # (for the first disk) in RAM. We can set the maximum sector
151 # count to 36 - the most we will encounter on an ED 2.88.
152 #
153 # High doesn't hurt. Low does. Let's use the max: 63
155 pushw %ss
156 popw %es # %es = %ss = INITSEG
157 xorw %ax, %ax # %ax = 0
158 #ifdef EXE_SUPPORT
159 cwd # %dx = 0
160 #endif
161 movw $zeroed/2, %cx # clear gdt + offset, %ds, limits
162 rep # don't worry about cld
163 stosw # already done above
164 popw %bx # offset = 0
165 popw %ds # %ds = 0
166 popw %fs # %fs = 0
168 movb setup_sects+0x7C00, %al # read bootsector + setup (%ds = 0)
169 incw %ax
171 ldsw 0x78(%bx), %si # %ds:%bx+0x78 is parameter table address
172 pushw %es
173 pushw %di
174 movb $6, %cl # copy 12 bytes
175 rep # don't worry about cld
176 movsw # already done above
177 pushw %ss
178 popw %ds # now %ds = %es = %ss = INITSEG
179 popl %fs:0x78(%bx) # update parameter table address
180 movb $63, 0x4-12(%di) # patch sector count, %di = stacktop
181 cli
183 xchg %ax, %di # sector count
184 popw %ax # limits = 0
185 incw %cx # cylinder 0, sector 1, clear Z
186 call read_first_sectors # read setup
188 # This routine loads the system at address LOADSEG, making sure
189 # no 64kB boundaries are crossed. We try to load it as fast as
190 # possible, loading whole tracks whenever we can.
192 popw %bx # clear %bx
193 movw syssize, %di
194 decw %di
195 shrw $9-4, %di
196 incw %di
197 movw $SYSSEG, %cx
198 call read_sectorsCX
200 # This procedure turns off the floppy drive motor, so
201 # that we enter the kernel in a known state, and
202 # don't have to worry about it later.
204 kill_motor:
205 xchgw %ax, %di # reset FDC (%di < 128)
206 int $0x13
208 # After that (everything loaded), we jump to the setup-routine
209 # loaded directly after the bootblock:
210 # Segments are as follows: %ds = %ss = INITSEG
212 jmp_setup:
213 ljmp $SETUPSEG, $0
215 #ifdef PUTS
216 #define PUTC
217 putslp:
218 call putc
219 puts:
220 lodsb
221 orb %al, %al
222 jne putslp
223 int $0x20 // dos exit
224 #endif
225 #ifdef EXE_SUPPORT
226 #ifdef SHUTDOWNDOS
227 doiret:
228 iret
229 step19:
230 pushw %si
231 pushw %ds
232 movw %sp, %si
233 ldsw %ss:4(%si), %si
234 cmpw $0x19CD, (%si)
235 popw %ds
236 popw %si
237 jne doiret
238 xorw %si, %si
239 movw %si, %ds
240 pushw %cs
241 popw %ss
242 movw $stacktop-4-16, %sp
243 popl 4(%si)
244 popaw
245 #endif
246 movesys: // %ax = SYSSEG
247 movw %cs:syssize, %bp
248 shrw $4, %si
249 addw %si, %bx
250 subw %ax, %bx
251 jnc forward
252 addw %bp, %ax
253 forward:
254 movw %ax, %es
255 movw %ax, %dx
256 addw %bx, %dx
257 movw %dx, %ds
258 sbbw %dx, %dx // %dx = 0 : -1
259 cmc // C = 1 : 0
260 adcw %dx, %ax
261 xorw %si, %si
262 xorw %di, %di
263 movb $8, %cl
264 rep
265 movsw
266 decw %bp
267 jns forward
268 #ifndef NO_CURSOR_DEFINITION
269 movb $1, %ah
270 movb $0, %bh
271 movb $0x20, %ch // 0x2000
272 int $0x10
273 #endif
274 pushw %ss
275 popw %ds
276 jmp jmp_setup
277 #endif
278 putcdot:
279 #ifdef PUTC
280 movb $0x2E, %al
281 putc:
282 movb $0xE, %ah
283 movw $7, %bx
284 int $0x10
285 #endif
286 ret
289 # read_sectors reads %di sectors into %es:0 buffer.
290 # %es:0 is updated to the next memory location.
291 # First, sectors are read sector by sector until
292 # sector per track count is known. Then they are
293 # read track by track.
294 # Assume no error on first track.
296 #ifdef FLOPPY_1440K_ONLY
297 #define FLOPPY_HEADS 2 /* 2 heads */
298 #define FLOPPY_SECTORS 18 /* 18 sectors */
299 #else
300 #define FLOPPY_HEADS 2 /* 2 heads minimum */
301 #define FLOPPY_SECTORS 9 /* 9 sectors minimum */
302 #endif
304 check_limits:
305 #ifndef FLOPPY_1440K_ONLY
306 popw %dx
307 #ifdef FLOPPY_SECTORS
308 cmpb $FLOPPY_SECTORS+1, %cl # minimum sector count
309 jb check_head
310 #endif
311 cmpb %al, %cl # max sector known ?
312 ja next_head # no -> store it
313 check_head:
314 #ifdef FLOPPY_HEADS
315 cmpb $FLOPPY_HEADS, %dh # 2 heads minimum
316 jb check_cylinder
317 #endif
318 cmpb %ah, %dh # max head known ?
319 ja next_cylinder # no -> store it
320 check_cylinder:
321 #endif
322 pushaw
323 #ifndef FLOPPY_1440K_ONLY
324 cbw # %ah = 0
325 #endif
326 int $0x13 # reset controler
327 popaw
328 movb $1, %al # sector by sector...
329 read_sectorslp:
330 pushw %dx # some bios break dx...
331 #ifndef FLOPPY_1440K_ONLY
332 pushw %ax # limits
333 subb %cl, %al # sectors remaining in track
334 ja tolastsect
335 movb $1, %al # 1 sector mini
336 tolastsect:
337 #else
338 movb $FLOPPY_SECTORS+1, %al
339 subb %cl, %al # sectors remaining in track
340 #endif
341 cbw
342 cmpw %di, %ax
343 jb more1trk
344 movw %di, %ax # sectors to read
345 more1trk:
346 pushw %ax # save context
347 movb $2, %ah # cmd: read chs
348 int $0x13
349 #ifndef FLOPPY_1440K_ONLY
350 popw %dx # save %ax
351 popw %ax # limits
352 #else
353 popw %ax # restore context
354 popw %dx
355 #endif
356 jc check_limits
357 #ifndef FLOPPY_1440K_ONLY
358 xchgw %ax, %bp
359 addw %dx,%cx # next sector
360 movw %cx, %gs
361 movw %es, %cx
362 pushw %dx
363 shlw $5, %dx
364 addw %dx, %cx
365 popw %dx
366 subw %dx,%di # update sector counter
367 popw %dx
368 read_sectorsCX:
369 movw %cx, %es # next location
370 jz putcdot
371 #else
372 addw %ax,%cx # next sector
373 movw %cx, %gs
374 movw %es, %cx
375 pushw %ax
376 shlw $5, %ax
377 addw %ax, %cx
378 popw %ax
379 subw %ax,%di # update sector counter
380 read_sectorsCX:
381 movw %cx, %es # next location
382 jz putcdot
383 #endif
384 read_sectors:
385 movw %gs, %cx
386 #ifndef FLOPPY_1440K_ONLY
387 # al is last sector+1
388 # ah is last cylinder+1
389 xchgw %ax, %bp
390 #endif
391 #ifndef FLOPPY_1440K_ONLY
392 cmpb %al,%cl # reach sector limit ?
393 jne bdendlp
394 next_head:
395 movb %cl,%al
396 #else
397 cmpb $FLOPPY_SECTORS+1,%cl # reach sector limit ?
398 jne bdendlp
399 #endif
400 incb %dh # next head
401 movb $1,%cl # first sector
402 #ifndef FLOPPY_1440K_ONLY
403 cmpb %ah, %dh # reach head limit ?
404 jne bdendlp
405 next_cylinder:
406 movb %dh,%ah
407 #else
408 cmpb %cl,%dh # reach head limit ?
409 je bdendlp
410 #endif
411 # NOTE : support 256 cylinders max
412 incb %ch # next cylinder
413 movb $0,%dh # first head
414 read_first_sectors:
415 bdendlp:
416 jmp read_sectorslp
418 #ifdef HELP
419 helpmsg:
420 .ascii "No help available."
421 .byte 13,10
422 .byte 0
423 #endif
424 #ifdef CHECK_REALMODE
425 .org 475
426 realmode_expected:
427 helpend:
428 .ascii "386 real mode only."
429 .byte 13,10,0
430 #endif
431 .org 497
432 setup_sects:
433 .byte 0
434 .org 500
435 syssize:
436 .word 0