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

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