wok-6.x view linld/stuff/src/_BEG.ASM @ rev 24159

Up ventoy (1.0.62)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Mon Dec 20 10:05:11 2021 +0000 (2021-12-20)
parents 072e91636362
children 530dacfd6585
line source
1 ;***************************************************************
2 ;****** This file is distributed under GPL
3 ;***************************************************************
4 ideal
5 %PAGESIZE 255
6 %crefref
7 %noincl
8 %nomacs
10 include "common.inc"
11 include "himem.inc"
12 include "isostate.inc"
14 CMDNUMCNT = 5
15 ifdef CPUTYPE
16 CMDNUMCNT = CMDNUMCNT+1
17 endif
18 ifdef QUICK_BOOT
19 CMDNUMCNT = CMDNUMCNT+2
20 endif
22 macro alloc_isostate
23 public _isostate
24 _isostate isostate <?>
25 org $-ISOSTATE_OVERLAP
26 endm
28 macro alloc_image
29 rm_size dw ? ; _imgs-4
30 rm_buf dw ? ; _imgs-2
31 global _imgs:byte
32 label _imgs byte
33 img_kernel image_himem ?
34 img_initrd image_himem ?
35 endm
37 macro alloc_cmdnum
38 global _cmdnum:word
39 label _cmdnum word
40 dd CMDNUMCNT dup(?)
41 endm
43 p8086
45 group DGROUP _TEXT,_DATA,_BSS
46 assume cs:DGROUP,ds:DGROUP
48 segment _TEXT byte public use16 'CODE'
49 ends _TEXT
51 segment _DATA byte public use16 'DATA'
52 ;global _data_start:byte
53 label _data_start byte
54 ifndef NO386
55 msg_badcpu db "I need a 386+ in real mode w/o paging"
56 ifdef VCPI
57 db " or "
58 msg_badmapping db "under VCPI 4.0+ manager with low 640k 1:1 mapping"
59 global _vcpi:byte
60 endif
61 _vcpi db 0
62 endif
64 ends _DATA
66 segment _BSS byte public use16 'BSS'
68 ifdef ISO9660
69 ifndef VCPI
70 alloc_isotate
71 else
72 room_for_isostate = 1
73 endif
74 endif
76 ifdef ISOHOOK
77 extrn _big_cmdline:byte
78 db 254 dup(?)
79 endif
80 ;global _bss_start:byte
81 label _bss_start byte
82 global stktop:byte
83 ifdef CPUTYPE
84 global _cputype:word
85 _cputype dw ?
86 endif
87 global _cpu386:byte
88 _cpu386 db ?
89 ifdef CPU64
90 org $-1
91 global _cpu_features:dword
92 _cpu_features dd ?
93 room_for_image = 1
94 else
95 alloc_image
96 endif
98 ;BSS_OVERLAP_BOOT = 1
100 ifndef BSS_OVERLAP_BOOT
102 ifdef room_for_isostate
103 alloc_isostate
104 endif
106 ifdef room_for_image
107 alloc_image
108 endif
110 alloc_cmdnum
112 endif
114 ends _BSS
116 segment _TEXT byte public use16 'CODE'
118 macro cpuid
119 db 0fh,0A2h
120 endm
122 org 100h
123 ;global _text_start:byte
124 label _text_start byte
126 ;***************************************************************
127 ; clear bss
128 ;***************************************************************
129 mov sp,offset stktop
130 mov bx, 0F000h ; cld ; cli & empty string
131 mov si,offset _bss_start
132 clearbss:
133 mov [si],bl ; clear bss + heap
134 inc si
135 jne clearbss
137 ;***************************************************************
138 ; check CPU
139 ;***************************************************************
141 ; Check for oldies
142 ifdef CPUTYPE
143 mov ax,bx
144 mov cl,32+7 ; 186+ masks with 31
145 shr ah,cl
146 endif
147 push bx ; < 286 : flags[12..15] are forced 1
148 popf ; = 286 : flags[12..15] are forced 0, cld, cli
149 pushf ; > 286 : only flags[15] is forced 0
150 pop dx
151 add dh,bh ; 160:NS=386+, 0F0:NC=286, 1E0:PO=86/186
152 ifndef NO386
153 mov bx,offset msg_badcpu
154 js godie ;it is not a 386+, die
155 else
156 ifdef CPUTYPE
157 jc not286
158 mov ah,2
159 not286:
160 js jmp_endcpu86 ;it is not a 386+, try ELKS & co
161 else
162 js endcpu86 ;it is not a 386+, try ELKS & co
163 endif
164 endif
165 p386
166 ifdef VCPI
167 mov edx,cs
168 shl edx,4 ; edx for prepare_vcpi (edx[31-20]=0) [1]
169 else
170 ifndef LARGE_ZIMAGE
171 mov edx,cs
172 shl edx,4 ; edx for memcpy32 (edx[31-20]=0) [1]
173 endif
174 endif
175 ifndef LARGE_ZIMAGE
176 extrn gdt_base_memcpy:word ; gdt_base for memcpy32
177 add [dword gdt_base_memcpy],edx
178 endif
180 ; Check for vm
181 smsw bx ;SMSW cannot be trapped! :-)
182 shr bx,1 ;MSW_PE
183 ; We're in vm
184 jnc check_vcpi
186 check_rm_paging:
187 ; It's a 386 in real mode, chk for paging (crazy but possible)
188 mov ebx,cr0
189 inc ebx ;CR0_PG to S
190 jns endcpu386
191 ifndef NO386
192 p8086
193 extrn die:near
194 godie:
195 call near die
196 else
197 jmp_endcpu86:
198 jmp endcpu86 ;386+ rm will fake 186+
199 endif
202 ;***************************************************************
203 ; checks for vcpi
204 ;***************************************************************
205 label check_vcpi near
206 p386
207 ifdef VCPI
208 ; Check whether it is safe to call 67h
209 xor eax,eax
210 mov es,ax
211 cmp [dword es:67h*4],eax
212 je no_vcpi
213 mov ah,0DEh ; check for vcpi present
214 int 67h
215 or ah,ah
216 jnz no_vcpi
217 is386vcpi:
218 extrn prepare_vcpi:near
219 call prepare_vcpi
220 ; get_vcpi_interface() || die("VCPI: low 640k: need 1:1 mapping");
221 ;extrn _get_vcpi_interface:near
222 ;call _get_vcpi_interface
223 ifndef NO386
224 mov bx,offset msg_badmapping
225 jnz no_vcpi
226 dec [byte bx+_vcpi-msg_badmapping]
227 else
228 jnz no_vcpi
229 extrn _vcpi:byte
230 dec [byte _vcpi]
231 endif
232 endif
233 no_vcpi:
234 endcpu386:
235 ifdef CPU64
236 pushfd
237 pop dx
238 pop ax
239 ifdef CPUTYPE
240 xor al,24h ; toggle CPUID feature bit & AC bit
241 else
242 xor al,20h ; toggle CPUID feature bit 21 (=> pentium+)
243 endif
244 push ax ; (toggle AC: bit 18 => 486+)
245 push dx
246 popfd
247 pushfd
248 pop dx
249 pop dx
250 xor al,dl ; clear C; 00=P5+=PE+Z 20=486=PO 24=386=PE
251 ifdef CPUTYPE
252 ifndef VCPI
253 ifdef LARGE_ZIMAGE
254 cdq ; 386 and 486 are not 64 bits
255 endif
256 endif
257 mov ax,0400h
258 jpo is486
259 mov ah,3
260 is486:
261 jne set_cputype ; edx[31-20]=0 see [1]; edx.26=lm=0
262 xchg ax,si ; si=0300h
263 basicCPUID:
264 xor eax,eax
265 inc ax
266 cpuid ; Basic cpuid (late 486 or Pentium)
267 inc si
268 jpo basicCPUID ; again cause of Nasty EMM386s
269 push eax
270 shr edx,1+26 ; test bit 26 (sse2), clear bit 29 (avoid tm/lm confusion)
271 jnc baseInfo
272 else
273 jne @@no_cpuid ; CPUID feature bit changed ?
274 endif
275 mov eax,80000001h ; Extended Processor Info and Feature Bits (Pentium 4 or newer)
276 cpuid ; Extended cpuid (Pentium 4)
277 ifdef CPUTYPE
278 baseInfo:
279 pop ax ; base FamilyID[11-8] & base ModelID[7-4]
280 pop bx ; extended FamilyID[11-4] & extended ModelID[3-0]
281 mov cx,0F04h
282 and ah,ch
283 and bh,ch
284 ror bx,cl ; extended FamilyID in bl, extended ModelID in bh[7-4]
285 shr al,cl ; set base ModelID
286 cmp ah,ch
287 jz SetModelID
288 cmp ah,06h
289 jnz ModelIDset
290 db 3Dh ; cmp ax,0E302h
291 SetModelID:
292 add ah,bl ; use extended FamilyID
293 or al,bh ; use extended ModelID
294 ModelIDset:
295 set_cputype:
296 endif
297 mov dl,-1 ; set 386 flag
298 ifdef NO386
299 db 66h ; mov [_cpu_features],edx
300 @@no_cpuid:
301 mov [word _cpu_features],dx ; dl != 0
302 else
303 mov [_cpu_features],edx
304 @@no_cpuid:
305 endif
306 else
307 dec [_cpu386]
308 endif
309 endcpu86:
310 ifdef CPUTYPE
311 mov [_cputype],ax ; _cputype: FFMM or 0[1-4]00=80[1-4]86 or 0000=8086/8088
312 endif
313 p8086
315 ;***************************************************************
316 ; build argv & argc
317 ;***************************************************************
318 mov si,80h
319 ifdef ISOHOOK
320 mov bx,offset _big_cmdline
321 cmp [byte si],2
322 jnb @@user_args
323 call @set_cmdline$qpxzc
324 @@user_args:
325 endif
326 lodsb ; size 0..127
327 cbw
328 ifdef ISOHOOK
329 inc ax
330 jnz short_cmdline
331 mov si,bx
332 lodsb ; size 0..254
333 short_cmdline:
334 dec ax
335 endif
336 xchg ax,bx
337 mov [bx+si],bh ; set eos
339 ;***************************************************************
341 ifdef BSS_OVERLAP_BOOT
342 mov di,offset clean-100h
344 org $-(CMDNUMCNT*4) ; alloc_cmdnum
346 ifdef room_for_image
347 org $-2-2-(2*size image_himem) ; alloc_image
348 endif
350 ifdef room_for_isostate
351 org $+ISOSTATE_OVERLAP-size isostate
352 alloc_isostate
353 endif
355 ifdef room_for_image
356 alloc_image
357 endif
359 alloc_cmdnum
360 clean:
361 mov [di+0FFh],bh
362 dec di
363 jnz clean
364 endif
366 org $-4 ; _himem_buf
367 global _himem_buf:dword
368 _himem_buf dd ?
370 ;***************************************************************
371 extrn _bss_end:word
372 mov di,offset _bss_end
373 global _heap_top
374 org $-2
375 _heap_top dw ?
376 ;xor dx,dx
377 ;push dx ; envp (already cleared)
378 ;mov [word di],dx ; argv[0] = 0 (idem)
379 argbuild:
380 mov bx,2 ; argc * 2
381 argeos:
382 mov cx,1 ; look for a start of string
383 mov [byte si-1],bh ; mark eos
384 mov ah,20h ; space will be eos
385 arglp:
386 lodsb
387 cmp al,0h
388 je argdone
389 cmp al,20h
390 jb argeos
391 cmp al,ah
392 je argeos
393 cmp al,27h
394 je isargstr
395 cmp al,22h
396 je isargstr
397 jcxz arglp ; not start of string
398 dec si
399 ;jmp newarg
400 db 0BAh ; mov dx,im opcode
401 isargstr:
402 mov ah,al ; expected eos
403 newarg:
404 mov [word bx+di],si ; argv[argc++] = si
405 inc bx
406 inc bx
407 dec cx
408 jmp arglp
410 ;***************************************************************
411 ;_fastcall void set_cmdline(bx:const char *filename);
412 ;***************************************************************
413 global @set_cmdline$qpxzc:near
414 proc @set_cmdline$qpxzc near
415 extrn openargs:near
416 call openargs
417 jc @ret
418 mov ch,15 ; cx<4096
419 mov di,[_heap_top]
420 jmp read_cmdline ; read_cmdline(ax,di,cx)
422 endp @set_cmdline$qpxzc
424 ifdef NO386
425 ;***************************************************************
426 ;u16 topseg();
427 ;***************************************************************
428 global _topseg:near
429 proc _topseg near
431 int 12h
432 jnc @@max640k
433 mov ax,640 ; 640k => 9000h
434 @@max640k:
435 dec ax
436 and al,0C0h
437 mov cl,6
438 shl ax,cl
439 @ret:
440 ret
442 endp _topseg
443 else
444 @ret:
445 ret
446 endif
448 ;***************************************************************
449 argdone:
450 ;mov [word bx+di],0 ; argv[argc] = 0
451 lea ax,[bx+di+2]
452 mov [_heap_top],ax
453 ;push di ; argv
454 ;shr bx,1
455 ;push bx ; argc
456 ifndef filearg
457 mov bx,[di+2] ; argv[1]
458 extrn openargs:near
459 call near openargs
460 jc argend
461 ;pop bx ; trash argc, argv >> 1Kb !
462 ;pop cx ; sizemax=argv
463 dec cx ; sizemax=0ffffh
464 read_cmdline:
465 mov dx,di
466 push dx
467 xchg ax,bx
468 extrn @read$cxdxbx:near ; read(fd=bx,buffer=dx,size=cx)
469 call near @read$cxdxbx
470 pop si ; si=buffer=argv
471 add di,ax
472 ifndef NO_CLOSE
473 extrn close:near
474 call near close
475 endif
476 jmp argbuild
477 argend:
478 endif
480 ;***************************************************************
481 ; extrn _main:near
482 ; call _main
483 ;never return
485 ;***************************************************************
488 ends _TEXT
491 end _text_start
493 ;###### END OF FILE ############################################