wok-next view memtest/stuff/bootloader.S @ rev 16977
backup-manager: update bdeps
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Wed Aug 06 11:39:11 2014 +0200 (2014-08-06) |
parents | bd4321dee983 |
children | 99c5dab1a371 |
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 # ifdef HELP
70 # define PUTS
71 movw $EXEADRS(helpmsg), %si
72 cmpb $'/'+1, %al
73 # ifdef CHECK_REALMODE
74 js jsputs
75 # else
76 js puts
77 # endif
78 # endif
79 nocmdline:
80 #endif
81 movw $SYSSEG, %ax
82 #ifdef CHECK_REALMODE
83 #define PUTS
84 movw $EXEADRS(realmode_expected), %si
85 pushfw // save flags
86 // bits 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
87 // flags 0 NT IOPL OF DF IF TF SF ZF 0 AF 0 PF 1 CF
88 //movb $0x10, %ah // DF = IF = TF = 0
89 pushw %ax
90 popfw // < 286 : flags[12..15] are forced 1
91 pushfw // = 286 : flags[12..15] are forced 0
92 popw %dx // > 286 : only flags[15] is forced 0
93 popfw // restore flags
94 cmpb %ah, %dh // test Fx and 0x cases
95 jsputs:
96 js puts // S= not 386+
97 smsww %dx
98 shrw $1, %dx
99 jc puts
100 #endif
101 movw $0x100, %si
102 movw $end_header, %di
103 movb EXEADRS(setup_sects), %ch
104 movb $(512-(end_header-_start))/2, %cl
105 rep
106 movsw
107 ljmp $INITSEG, $movesys
108 start:
109 pushw %dx
110 #else
111 #undef HELP
112 #undef CMDLINE
113 #undef CHECK_REALMODE
114 #endif
115 cld # assume nothing
116 stacktop = 0x9E00 # in 0x8000 .. 0xA000
117 zeroed = 12 # zeroed registers
118 movw $stacktop-12-zeroed, %di # stacktop is an arbitrary value >=
119 # length of bootsect + length of
120 # setup + room for stack;
121 # 12 is disk parm size.
122 pushw $INITSEG
123 popw %ss # %ss contain INITSEG
124 movw %di, %sp # put stack at INITSEG:stacktop-...
126 # Many BIOS's default disk parameter tables will not recognize
127 # multi-sector reads beyond the maximum sector number specified
128 # in the default diskette parameter tables - this may mean 7
129 # sectors in some cases.
130 #
131 # Since single sector reads are slow and out of the question,
132 # we must take care of this by creating new parameter tables
133 # (for the first disk) in RAM. We can set the maximum sector
134 # count to 36 - the most we will encounter on an ED 2.88.
135 #
136 # High doesn't hurt. Low does. Let's use the max: 63
138 pushw %ss
139 popw %es # %es = %ss = INITSEG
140 xorw %ax, %ax # %ax = 0
141 #ifdef EXE_SUPPORT
142 cwd # %dx = 0
143 #endif
144 movw $zeroed/2, %cx # clear gdt + offset, %ds, limits
145 rep # don't worry about cld
146 stosw # already done above
147 popw %bx # offset = 0
148 popw %ds # %ds = 0
149 popw %fs # %fs = 0
151 movb setup_sects+0x7C00, %al # read bootsector + setup (%ds = 0)
152 incw %ax
154 ldsw 0x78(%bx), %si # %ds:%bx+0x78 is parameter table address
155 pushw %es
156 pushw %di
157 movb $6, %cl # copy 12 bytes
158 rep # don't worry about cld
159 movsw # already done above
160 pushw %ss
161 popw %ds # now %ds = %es = %ss = INITSEG
162 popl %fs:0x78(%bx) # update parameter table address
163 movb $63, 0x4-12(%di) # patch sector count, %di = stacktop
164 cli
166 xchg %ax, %di # sector count
167 popw %ax # limits = 0
168 incw %cx # cylinder 0, sector 1, clear Z
169 call read_first_sectors # read setup
171 # This routine loads the system at address LOADSEG, making sure
172 # no 64kB boundaries are crossed. We try to load it as fast as
173 # possible, loading whole tracks whenever we can.
175 popw %bx # clear %bx
176 movw syssize, %di
177 addw $(512/16)-1, %di
178 shrw $9-4, %di
179 movw $SYSSEG, %cx
180 call read_sectorsCX
182 # This procedure turns off the floppy drive motor, so
183 # that we enter the kernel in a known state, and
184 # don't have to worry about it later.
186 kill_motor:
187 xchgw %ax, %di # reset FDC (%di < 128)
188 int $0x13
190 # After that (everything loaded), we jump to the setup-routine
191 # loaded directly after the bootblock:
192 # Segments are as follows: %ds = %ss = INITSEG
194 jmp_setup:
195 ljmp $SETUPSEG, $0
197 #ifdef PUTS
198 #define PUTC
199 putslp:
200 call putc
201 puts:
202 lodsb
203 orb %al, %al
204 jne putslp
205 int $0x20 // dos exit
206 #endif
207 #ifdef EXE_SUPPORT
208 movesys:
209 pushw %es
210 popw %ss
211 movw EXEADRS(syssize), %bp // %ds untouched
212 movw %ds, %bx
213 cwd
214 incw %dx
215 cmpw %ax, %bx
216 jnc forward
217 negw %dx
218 addw %bp, %ax
219 addw %bp, %bx
220 forward:
221 movw %ax, %es
222 movw %bx, %ds
223 xorw %di, %di
224 movb $8, %cl
225 pushw %si
226 rep
227 movsw
228 popw %si
229 addw %dx, %ax
230 addw %dx, %bx
231 decw %bp
232 jns forward
233 #ifndef NO_CURSOR_DEFINITION
234 movb $1, %ah
235 movb $0, %bh
236 movb $0x20, %ch // 0x2000
237 int $0x10
238 #endif
239 pushw %ss
240 popw %ds
241 jmp jmp_setup
242 #endif
243 putcdot:
244 #ifdef PUTC
245 movb $0x2E, %al
246 putc:
247 movb $0xE, %ah
248 movw $7, %bx
249 int $0x10
250 #endif
251 ret
254 # read_sectors reads %di sectors into %es:0 buffer.
255 # %es:0 is updated to the next memory location.
256 # First, sectors are read sector by sector until
257 # sector per track count is known. Then they are
258 # read track by track.
259 # Assume no error on first track.
261 #ifdef FLOPPY_1440K_ONLY
262 #define FLOPPY_HEADS 2 /* 2 heads */
263 #define FLOPPY_SECTORS 18 /* 18 sectors */
264 #else
265 #define FLOPPY_HEADS 2 /* 2 heads minimum */
266 #define FLOPPY_SECTORS 9 /* 9 sectors minimum */
267 #endif
269 check_limits:
270 #ifndef FLOPPY_1440K_ONLY
271 popw %dx
272 #ifdef FLOPPY_SECTORS
273 cmpb $FLOPPY_SECTORS+1, %cl # minimum sector count
274 jb check_head
275 #endif
276 cmpb %al, %cl # max sector known ?
277 ja next_head # no -> store it
278 check_head:
279 #ifdef FLOPPY_HEADS
280 cmpb $FLOPPY_HEADS, %dh # 2 heads minimum
281 jb check_cylinder
282 #endif
283 cmpb %ah, %dh # max head known ?
284 ja next_cylinder # no -> store it
285 check_cylinder:
286 #endif
287 pushaw
288 #ifndef FLOPPY_1440K_ONLY
289 cbw # %ah = 0
290 #endif
291 int $0x13 # reset controler
292 popaw
293 movb $1, %al # sector by sector...
294 read_sectorslp:
295 pushw %dx # some bios break dx...
296 #ifndef FLOPPY_1440K_ONLY
297 pushw %ax # limits
298 subb %cl, %al # sectors remaining in track
299 ja tolastsect
300 movb $1, %al # 1 sector mini
301 tolastsect:
302 #else
303 mov $FLOPPY_SECTORS+1, %al
304 subb %cl, %al # sectors remaining in track
305 #endif
306 cbw
307 cmpw %di, %ax
308 jb more1trk
309 movw %di, %ax # sectors to read
310 more1trk:
311 pushw %ax # save context
312 movb $2, %ah # cmd: read chs
313 int $0x13
314 #ifndef FLOPPY_1440K_ONLY
315 popw %dx # save %ax
316 popw %ax # limits
317 #else
318 popw %ax # restore context
319 popw %dx
320 #endif
321 jc check_limits
322 #ifndef FLOPPY_1440K_ONLY
323 xchgw %ax, %bp
324 addw %dx,%cx # next sector
325 movw %cx, %gs
326 movw %es, %cx
327 pushw %dx
328 shlw $5, %dx
329 addw %dx, %cx
330 popw %dx
331 subw %dx,%di # update sector counter
332 popw %dx
333 read_sectorsCX:
334 movw %cx, %es # next location
335 jz putcdot
336 #else
337 addw %ax,%cx # next sector
338 movw %cx, %gs
339 movw %es, %cx
340 pushw %ax
341 shlw $5, %ax
342 addw %ax, %cx
343 popw %ax
344 subw %ax,%di # update sector counter
345 read_sectorsCX:
346 movw %cx, %es # next location
347 jz putcdot
348 #endif
349 read_sectors:
350 movw %gs, %cx
351 #ifndef FLOPPY_1440K_ONLY
352 # al is last sector+1
353 # ah is last cylinder+1
354 xchgw %ax, %bp
355 #endif
356 #ifndef FLOPPY_1440K_ONLY
357 cmpb %al,%cl # reach sector limit ?
358 jne bdendlp
359 next_head:
360 movb %cl,%al
361 #else
362 cmpb $FLOPPY_SECTORS+1,%cl # reach sector limit ?
363 jne bdendlp
364 #endif
365 incb %dh # next head
366 movb $1,%cl # first sector
367 #ifndef FLOPPY_1440K_ONLY
368 cmpb %ah, %dh # reach head limit ?
369 jne bdendlp
370 next_cylinder:
371 movb %dh,%ah
372 #else
373 cmpb %cl,%dh # reach head limit ?
374 je bdendlp
375 #endif
376 # NOTE : support 256 cylinders max
377 incb %ch # next cylinder
378 read_first_sectors:
379 movb $0,%dh # first head
380 bdendlp:
381 jmp read_sectorslp
383 #ifdef CHECK_REALMODE
384 realmode_expected:
385 .ascii "386+ real mode only."
386 .byte 13,10,0
387 #endif
388 #ifdef HELP
389 helpmsg:
390 .ascii "No help available."
391 .byte 13,10
392 .byte 0
393 #endif
394 .org 497