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

Up slitaz-boot-scripts (446)
author Richard Dunbar <mojo@slitaz.org>
date Fri Oct 27 12:29:47 2017 -0400 (2017-10-27)
parents 9e463ebef079
children d74b8b5a78d0
line source
1 // Image/zImage boot sector
3 SYSSEG = 0x1000
4 INITSEG = 0x9000
5 SETUPSEG = 0x9020
6 setup_sects = 497
7 syssize = 500
9 .text
10 .code16
11 .org 0
12 .globl _start
13 _start:
15 #define CODESZ 512 /* patched by installer */
17 /* some extra features */
18 #define EXE_SUPPORT real mode dos .exe file support
19 #define CMDLINE 0x9E00
20 #define VCPI VCPI 4.0 support
21 #define SHUTDOWNDOS shutdown DOS services
23 /* some contraints to reduce the size */
24 //#define FLOPPY_1440K_ONLY 1.44M floppies support only -33
25 //#define FLOPPY_HAS_2_SIDES hardcoded heads count to 2 -13
26 //#define MOVE_MAX_SYSSIZE always memcpy 512Kb -2
27 //#define NO_CURSOR_DEFINITION -8
28 //#define NO_CMDLINE_SHRINK remove heading spaces ? -6
29 //#define NO_CMDLINE_FILE remove @cmdline file support ? -20
30 //#define NO_DOTS show progression dots ? -8
32 #ifdef EXE_SUPPORT
33 #define EXEADRS(x) (x+0xE0)
34 #define FLAT20(x) (x+16*INITSEG)
36 .macro trace_int19
37 pushl $4
38 popw %si
39 popw %ds
40 pushl (%si)
41 movl $step19+(INITSEG<<16), (%si)
42 pushfw
43 popw %ax
44 incb %ah # set TF
45 pushw %ax
46 popfw
47 ljmp *4*0x19-4(%si)
48 .endm
50 stacktop = 0x9E00 # in 0x8000 .. 0xA000
51 decw %bp // Magic number: MZ
52 popw %dx
53 jmp start // Bytes on last page of file
54 .word (CODESZ+511)/512 // Pages in file INSTALLER
55 .word 0 // Relocations
56 .word (end_header-_start)/16 // Size of header in paragraphs
57 .word 4096 // Minimum extra paragraphs needed
58 .word -1 // Maximum extra paragraphs needed
59 .word (CODESZ+15)/16 // Initial (relative) SS value INSTALLER
60 .word stacktop // Initial SP value
61 .word 0 // Checksum INSTALLER?
62 .word EXEADRS(comstart) // Initial IP value
63 .word 0xFFF0 // Initial (relative) CS value
64 // .word 0x001C // File address of relocation table
65 // .word 0,0,0 // Overlay number
66 #endif
67 start:
68 cld # assume nothing
69 xorw %ax, %ax # %ax = 0
70 zeroed = 12 # zeroed registers
71 stacktop = 0x9E00 # in 0x8000 .. 0xA000 (+zeroed+12)
72 pushw $INITSEG
73 popw %ss # %ss contain INITSEG
74 pushw %ss
75 end_header:
76 popw %es # %es = %ss = INITSEG
77 # cmdline offset at 0x22
78 movw $stacktop, %di # stacktop is an arbitrary value >=
79 # length of bootsect + length of
80 # setup + room for stack;
81 # 12 is disk parm size.
82 movw %di, %sp # put stack at INITSEG:stacktop-...
83 #ifdef EXE_SUPPORT
84 cwd # %dx = 0
85 #endif
87 # Many BIOS's default disk parameter tables will not recognize
88 # multi-sector reads beyond the maximum sector number specified
89 # in the default diskette parameter tables - this may mean 7
90 # sectors in some cases.
91 #
92 # Since single sector reads are slow and out of the question,
93 # we must take care of this by creating new parameter tables
94 # (for the first disk) in RAM. We can set the maximum sector
95 # count to 36 - the most we will encounter on an ED 2.88.
96 #
97 # High doesn't hurt. Low does. Let's use the max: 63
99 movw $zeroed/2, %cx # clear gdt + offset, %ds, limits
100 rep # don't worry about cld
101 stosw # already done above
102 popw %bx # offset = 0
103 popw %ds # %ds = 0
104 popw %fs # %fs = 0
106 movb setup_sects+0x7C00, %al # read bootsector + setup (%ds = 0)
107 incw %ax
109 ldsw 0x78(%bx), %si # %ds:%bx+0x78 is parameter table address
110 pushw %es
111 pushw %di
112 movb $6, %cl # copy 12 bytes
113 rep # don't worry about cld
114 movsw # already done above
115 pushw %ss
116 popw %ds # now %ds = %es = %ss = INITSEG
117 popl %fs:0x78(%bx) # update parameter table address
118 movb $63, 0x4-12(%di) # patch sector count, %di = stacktop
119 cli
121 xchg %ax, %di # sector count
122 popw %ax # limits = 0
123 incw %cx # cylinder 0, sector 1, clear Z
124 call read_first_sectors # read setup
126 # This routine loads the system at address LOADSEG, making sure
127 # no 64kB boundaries are crossed. We try to load it as fast as
128 # possible, loading whole tracks whenever we can.
130 #ifndef NO_DOTS
131 popw %bx # clear %bx
132 #endif
133 movw syssize, %di
134 decw %di
135 shrw $9-4, %di
136 incw %di
137 movw $SYSSEG, %cx
138 call read_sectorsCX
140 # This procedure turns off the floppy drive motor, so
141 # that we enter the kernel in a known state, and
142 # don't have to worry about it later.
144 kill_motor:
145 xchgw %ax, %di # reset FDC (%di < 128)
146 int $0x13
148 # After that (everything loaded), we jump to the setup-routine
149 # loaded directly after the bootblock:
150 # Segments are as follows: %ds = %ss = INITSEG
152 jmp_setup:
153 ljmp $SETUPSEG, $0
155 #ifdef EXE_SUPPORT
156 #ifdef SHUTDOWNDOS
157 doiret:
158 iret
159 step19:
160 pushw %si
161 pushw %ds
162 movw %sp, %si
163 ldsw %ss:4(%si), %si
164 cmpw $0x19CD, (%si)
165 popw %ds
166 popw %si
167 jne doiret
168 xorw %si, %si
169 movw %si, %ds
170 pushw %cs
171 popw %ss
172 movw $stacktop-4-16, %sp
173 popl 4(%si)
174 popaw
175 #endif
176 movesys: // %ax = SYSSEG, %bx = DS, %si
177 //movw %cs:syssize, %bp
178 movw $0x8000, %bp
179 shrw $4, %si
180 addw %si, %bx
181 subw %ax, %bx
182 jnc forward
183 addw %bp, %ax
184 forward:
185 movw %ax, %es
186 movw %ax, %dx
187 addw %bx, %dx
188 movw %dx, %ds
189 sbbw %dx, %dx // %dx = 0 : -1
190 cmc // C = 1 : 0
191 adcw %dx, %ax
192 xorw %si, %si
193 xorw %di, %di
194 movb $8, %cl
195 rep
196 movsw
197 decw %bp
198 jns forward
199 #ifndef NO_CURSOR_DEFINITION
200 movb $1, %ah
201 movb $0, %bh
202 movb $0x20, %ch // 0x2000
203 int $0x10
204 #endif
205 pushw %ss
206 popw %ds
207 jmp jmp_setup
208 comstart:
209 cld # assume nothing
210 pushw $INITSEG
211 popw %es
212 #ifdef CMDLINE
213 movw %sp, %di
214 movw $0x80, %si
215 lodsb
216 cbw
217 xchgw %ax, %cx
218 jcxz nocmdline
219 movw $0xA33F, 0x7F(%si)
220 # ifndef NO_CMDLINE_SHRINK
221 skipspace:
222 lodsb
223 cmpb $0x20, %al
224 je skipspace
225 # ifndef NO_CMDLINE_FILE
226 movw %si,%dx
227 decw %si
228 subb $'@',%al
229 jne notafile
230 movb $0x3D,%ah
231 int $0x21
232 jc notafile
233 xchgw %ax,%bx
234 //movw %si,%dx // ~320 bytes max
235 movw $EXEADRS(notafile)-130,%cx
236 movb $0x3F,%ah
237 int $0x21
238 xchgw %ax,%cx
239 notafile:
240 # else
241 decw %si
242 # endif
243 # endif
244 rep
245 movsb
246 nocmdline:
247 #endif
248 movb $(512-(end_header-_start))/2, %cl
249 movb EXEADRS(setup_sects), %ch
250 movw $0x100, %si
251 movw $end_header, %di
252 rep
253 movsw
254 movw $SYSSEG, %ax
255 movw %ds, %bx
256 pushw %es
257 popw %ss
258 #ifndef SHUTDOWNDOS
259 pushw %es
260 pushw $movesys
261 #endif
262 #ifdef VCPI
263 # ifndef SHUTDOWNDOS
264 pushw %es
265 pushw %ds
266 # endif
267 pushaw
268 smsww %ax
269 andb $1, %al
270 je isrm
271 movw $EXEADRS(gdt_vcpi),%si
272 pushw $pagebuf/16
273 popw %es
274 movl $pagebuf+3,%es:0x1000
275 xorw %di,%di
276 call_vcpi:
277 movb $0xDE,%ah // DE01, EBX = getiface(DS:SI, ES:DI)
278 int $0x67
279 movl $FLAT20(sw2pm_params),%esi
280 movb $0x0C,%al // DE0C switchpm(ESI)
281 cli
282 jmp call_vcpi
283 pm_code:
284 movl %cr0,%eax
285 andl $0x7FFFFFFE,%eax
286 movl %eax,%cr0
287 movl %eax,%cr3
288 isrm:
289 # ifdef SHUTDOWNDOS
290 trace_int19
291 # else
292 lssw %cs:EXEADRS(saved_ss_sp),%sp
293 popaw
294 popw %ds
295 popw %es
296 retf
297 # endif
298 #else
299 # ifdef SHUTDOWNDOS
300 pushaw
301 trace_int19
302 # endif
303 retf
304 #endif
305 #endif
307 # read_sectors reads %di sectors into %es:0 buffer.
308 # %es:0 is updated to the next memory location.
309 # First, sectors are read sector by sector until
310 # sector per track count is known. Then they are
311 # read track by track.
312 # Assume no error on first track.
314 #ifdef FLOPPY_1440K_ONLY
315 #define FLOPPY_HAS_2_SIDES hardcore heads count to 2
316 #define FLOPPY_SECTORS 18 /* 18 sectors */
317 #else
318 #define FLOPPY_HEADS 2 /* 2 heads minimum */
319 #define FLOPPY_SECTORS 9 /* 9 sectors minimum */
320 #endif
322 return:
323 #ifndef NO_DOTS
324 movw $0xE2E,%ax
325 movb $7,%bl
326 int $0x10
327 #endif
328 ret
330 check_limits:
331 #ifndef FLOPPY_1440K_ONLY
332 popw %dx
333 #ifdef FLOPPY_SECTORS
334 cmpb $FLOPPY_SECTORS+1, %cl # minimum sector count
335 jb check_head
336 #endif
337 cmpb %al, %cl # max sector known ?
338 ja next_head # no -> store it
339 #ifndef FLOPPY_HAS_2_SIDES
340 check_head:
341 #ifdef FLOPPY_HEADS
342 cmpb $FLOPPY_HEADS, %dh # 2 heads minimum
343 jb check_cylinder
344 #endif
345 cmpb %ah, %dh # max head known ?
346 ja next_cylinder # no -> store it
347 check_cylinder:
348 #endif
349 #endif
350 pushaw
351 #ifndef FLOPPY_1440K_ONLY
352 cbw # %ah = 0
353 #endif
354 int $0x13 # reset controler
355 popaw
356 movb $1, %al # sector by sector...
357 read_sectorslp:
358 pushw %dx # some bios break dx...
359 #ifndef FLOPPY_1440K_ONLY
360 pushw %ax # limits
361 subb %cl, %al # sectors remaining in track
362 ja tolastsect
363 movb $1, %al # 1 sector mini
364 tolastsect:
365 #else
366 movb $FLOPPY_SECTORS+1, %al
367 subb %cl, %al # sectors remaining in track
368 #endif
369 cbw
370 cmpw %di, %ax
371 jb more1trk
372 movw %di, %ax # sectors to read
373 more1trk:
374 pushw %ax # save context
375 movb $2, %ah # cmd: read chs
376 int $0x13
377 #ifndef FLOPPY_1440K_ONLY
378 popw %dx # save %ax
379 popw %ax # limits
380 #else
381 popw %ax # restore context
382 popw %dx
383 #endif
384 jc check_limits
385 #ifndef FLOPPY_1440K_ONLY
386 xchgw %ax, %bp
387 addw %dx,%cx # next sector
388 movw %cx, %gs
389 movw %es, %cx
390 pushw %dx
391 shlw $5, %dx
392 addw %dx, %cx
393 popw %dx
394 subw %dx,%di # update sector counter
395 popw %dx
396 #else
397 addw %ax,%cx # next sector
398 movw %cx, %gs
399 movw %es, %cx
400 pushw %ax
401 shlw $5, %ax
402 addw %ax, %cx
403 popw %ax
404 subw %ax,%di # update sector counter
405 #endif
406 read_sectorsCX:
407 movw %cx, %es # next location
408 jz return
409 read_sectors:
410 movw %gs, %cx
411 #ifndef FLOPPY_1440K_ONLY
412 # al is last sector+1
413 # ah is last cylinder+1
414 xchgw %ax, %bp
415 #endif
416 #ifndef FLOPPY_1440K_ONLY
417 cmpb %al,%cl # reach sector limit ?
418 jne bdendlp
419 next_head:
420 movb %cl,%al
421 #else
422 cmpb $FLOPPY_SECTORS+1,%cl # reach sector limit ?
423 jne bdendlp
424 #endif
425 movb $1,%cl # first sector
426 #ifndef FLOPPY_HAS_2_SIDES
427 incb %dh # next head
428 cmpb %ah, %dh # reach head limit ?
429 jne bdendlp
430 next_cylinder:
431 movb %dh,%ah
432 movb $0,%dh # first head
433 #else
434 xorb %cl,%dh # next head
435 jne bdendlp # reach head limit ?
436 #endif
437 # NOTE : support 256 cylinders max
438 incb %ch # next cylinder
439 read_first_sectors:
440 bdendlp:
441 jmp read_sectorslp
443 #ifdef VCPI
444 pagebuf = 0x98000
445 tss = gdt_abs-40
446 gdt = gdt_abs-32
447 gdt_null = gdt_abs-32
448 gdt_vcpi = gdt_abs-24
449 gdt_vcpi2 = gdt_abs-16
450 gdt_vcpi3 = gdt_abs-8
451 gdt_abs:
452 .word 0xFFFF
453 .long 0x92000000
454 .byte 0xCF,0
455 gdt_code:
456 .word 0xFFFF
457 gdt_code_base:
458 .long 0x9A000000+FLAT20(0)
459 .byte 0x8F,0
460 gdt_tss:
461 .word 0x00FF
462 gdt_tss_base:
463 .long 0x89000000+FLAT20(tss)
464 .byte 0,0
465 gdtr:
466 gdt_lim:
467 .word 0xFFFF
468 gdt_base:
469 .long FLAT20(gdt)
470 sw2pm_params:
471 sw2pm_cr3:
472 .long pagebuf+0x1000
473 sw2pm_gdtr_ptr:
474 .long FLAT20(gdtr)
475 sw2pm_idtr_ptr:
476 .long FLAT20(idtr)
477 sw2pm_ldtr:
478 .word 0
479 sw2pm_tr:
480 SEL_TSS = gdt_tss-gdt_null
481 .word SEL_TSS
482 sw2pm_jumpaddr:
483 .long pm_code
484 SEL_CODE = gdt_code-gdt_null
485 .word SEL_CODE
486 idtr:
487 idt_lim:
488 .word 0x03FF
489 idt_base:
490 .long 0
491 # ifndef SHUTDOWNDOS
492 saved_ss_sp:
493 .word stacktop-4-16-4,INITSEG
494 # endif
495 #endif