wok view linld/stuff/src/_BEG.ASM @ rev 22183

linld: 8086 may load zImage (elks)
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sun Nov 10 18:20:50 2019 +0100 (2019-11-10)
parents 65ea21135647
children f3a4853e3eef
line source
1 ;***************************************************************
2 ;****** This file is distributed under GPL
3 ;***************************************************************
4 ideal
5 %PAGESIZE 1000
6 %crefref
7 %noincl
8 %nomacs
9 p8086
11 group DGROUP _TEXT,_DATA,_BSS
12 assume cs:DGROUP,ds:DGROUP
14 segment _TEXT byte public use16 'CODE'
15 ends _TEXT
17 segment _DATA byte public use16 'DATA'
18 global _data_start:byte
19 label _data_start byte
20 ifndef NO386
21 msg_badcpu db "I need 386+ CPU in real mode w/o paging"
22 db " or "
23 msg_badmapping db "under VCPI 4.0+ manager with low 640k 1:1 mapping"
24 global _vcpi:byte
25 _vcpi db 0
26 endif
28 ends _DATA
30 segment _BSS byte public use16 'BSS'
32 STACK_SIZE = 1024
34 global _bss_start:byte
35 label _bss_start byte
36 global _version_string:byte
37 label _version_string byte
38 db STACK_SIZE-2 dup(?)
39 stktop dw ?
40 ifdef NO386
41 global _cpu386:byte
42 _cpu386 db ?
43 org $-1
44 endif
45 global _cpu_features:dword
46 _cpu_features dd ?
47 ends _BSS
49 segment _TEXT byte public use16 'CODE'
51 macro cpuid
52 db 0fh,0A2h
53 endm
55 org 100h
56 global _text_start:byte
57 label _text_start byte
59 ;***************************************************************
60 ; clear bss
61 ;***************************************************************
62 mov sp,offset stktop
63 mov si,offset _bss_start
64 mov bx, 0F000h ; cld ; cli & empty string
65 clearbss:
66 mov [si],bl ; clear bss + heap + sp
67 inc si
68 jne clearbss
70 ;***************************************************************
71 ; check CPU
72 ;***************************************************************
74 ; Check for oldies
75 push bx ; < 286 : flags[12..15] are forced 1
76 popf ; = 286 : flags[12..15] are forced 0
77 pushf ; > 286 : only flags[15] is forced 0
78 pop dx
79 add dh,bh ; NS=386+, S+NC=286, S+C=86/186
80 ifndef NO386
81 mov bx,offset msg_badcpu
82 js godie ;it is not a 386+, die
83 else
84 js endcpu86 ;it is not a 386+, try ELKS & co
85 endif
86 p386
87 ; Check for vm
88 smsw ax ;SMSW cannot be trapped! :-)
89 and al,1 ;MSW_PE
90 ; We're in vm
91 jnz check_vcpi
93 check_rm_paging:
94 ; It's a 386 in real mode, chk for paging (crazy but possible)
95 mov eax,cr0
96 inc eax ;CR0_PG to S
97 jns endcpu386
98 ifndef NO386
99 p8086
100 extrn die:near
101 godie:
102 no_vcpi:
103 call near die
104 else
105 jmp endcpu86
106 endif
108 ;***************************************************************
109 ; checks for vcpi
110 ;***************************************************************
111 label check_vcpi near
112 p386
113 push ds
114 ; Check whether it is safe to call 67h (we trust only known EMM managers)
115 push si
116 pop ds
117 mov ds,[word 67h*4+2]
118 cmp [dword si+10+4],'0XXX'
119 jne skip
120 ;mov eax,'XMME'
121 ;xor eax,[dword si+10]
122 ; QMME also works (as told by <J.S.Peatfield@damtp.cambridge.ac.uk>)
123 ;shl eax,8
124 mov ax,'ME'
125 xor ax,[word si+10]
126 skip:
127 pop ds
128 jne no_vcpi
130 ; Check emm manager status and version
131 ;mov ah,40h ; get status
132 ;int 67h
133 ;test ah,ah
134 ;jnz no_vcpi
135 mov ah,46h ; get version
136 int 67h
137 test ah,ah
138 jnz no_vcpi
139 cmp al,40h ; version must be >= 4.0
140 jb no_vcpi
141 ; Check vcpi manager status
142 ;;mov ax,5A01h ; ALLOCATE RAW PAGES
143 ;;mov bx,4
144 ;;int 67h
145 ;;test ah,ah
146 ;;jnz no_vcpi
147 ;;push dx ;$ save handle
148 mov ax,0DE00h ; check for vcpi present
149 int 67h
150 test ah,ah
151 jnz no_vcpi
152 is386vcpi:
153 extrn prepare_vcpi:near
154 call prepare_vcpi
155 ; get_vcpi_interface() || die("VCPI: low 640k: need 1:1 mapping");
156 ;extrn _get_vcpi_interface:near
157 ;call _get_vcpi_interface
158 ifndef NO386
159 mov bx,offset msg_badmapping
160 jnz no_vcpi
161 dec [byte bx+_vcpi-msg_badmapping]
162 else
163 jnz no_vcpi
164 extrn _vcpi:byte
165 dec [byte _vcpi]
166 no_vcpi:
167 endif
168 endcpu386:
169 pushfd
170 pop dx
171 pop ax
172 mov bl,al
173 xor al,20h ; toggle CPUID feature bit 21
174 push ax
175 push dx
176 popfd
177 pushfd
178 pop dx
179 pop ax
180 xor al,bl ; clear C
181 je @@no_cpuid ; CPUID feature bit changed ?
182 mov eax,80000001h ; Extended Processor Info and Feature Bits
183 cpuid
184 ifdef NO386
185 mov dl,-1 ; set 386 flag
186 endif
187 mov [_cpu_features],edx
188 @@no_cpuid:
189 endcpu86:
190 p8086
192 ;***************************************************************
193 ; build argv & argc
194 ;***************************************************************
195 extrn _heap_top:word
196 mov di,[_heap_top]
197 ;xor ax,ax
198 ;push ax ; envp (already cleared)
199 ;mov [word di],ax ; argv[0] = 0 (idem)
200 mov si,80h
201 lodsb
202 cmp al,7Eh
203 jbe alok
204 mov al,7Eh
205 alok:
206 cbw
207 xchg ax,bx
208 mov [bx+si],bh ; set eos
209 argbuild:
210 ifdef DEBUG
211 p386
212 pusha
213 mov bx,si
214 extrn puts:near
215 call puts
216 popa
217 endif
218 mov bx,2 ; argc * 2
219 argeos:
220 mov cx,1 ; look for a start of string
221 mov [byte si-1],bh ; mark eos
222 mov ah,20h ; space will be eos
223 arglp:
224 lodsb
225 cmp al,0h
226 je argdone
227 cmp al,20h
228 jb argeos
229 cmp al,ah
230 je argeos
231 cmp al,27h
232 je isargstr
233 cmp al,22h
234 je isargstr
235 jcxz arglp ; not start of string
236 dec si
237 ;jmp newarg
238 db 0BAh ; mov dx,im opcode
239 isargstr:
240 mov ah,al ; expected eos
241 newarg:
242 mov [word bx+di],si ; argv[argc++] = si
243 inc bx
244 inc bx
245 dec cx
246 jmp arglp
247 argdone:
248 ;mov [word bx+di],0 ; argv[argc] = 0
249 lea ax,[bx+di+2]
250 mov [_heap_top],ax
251 push di ; argv
252 shr bx,1
253 push bx ; argc
254 ifndef filearg
255 mov bx,[di+2] ; argv[1]
256 extrn openargs:near
257 call near openargs
258 jc argend
259 pop bx ; trash argc, argv >> 1Kb !
260 global read_cmdline:near
261 pop cx ; sizemax=argv
262 read_cmdline:
263 mov dx,di
264 push dx
265 push ax
266 xchg ax,bx
267 extrn @read$cxdxbx:near ; read(fd=bx,buffer=dx,size=cx)
268 call near @read$cxdxbx
269 pop bx ; fd for close
270 pop si ; si=buffer=argv
271 add di,ax
272 extrn close:near
273 call near close
274 jmp argbuild
275 argend:
276 endif
278 ;***************************************************************
279 extrn _main:near
280 call _main
281 ;never return
283 ;***************************************************************
285 ends _TEXT
288 end _text_start
290 ;###### END OF FILE ############################################