wok view memtest/stuff/bootloader.S @ rev 15471

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