Add API call to determine the serial console configuration.
[profile/ivi/syslinux.git] / comboot.inc
1 ;; $Id$
2 ;; -----------------------------------------------------------------------
3 ;;   
4 ;;   Copyright 1994-2002 H. Peter Anvin - All Rights Reserved
5 ;;
6 ;;   This program is free software; you can redistribute it and/or modify
7 ;;   it under the terms of the GNU General Public License as published by
8 ;;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 ;;   Bostom MA 02111-1307, USA; either version 2 of the License, or
10 ;;   (at your option) any later version; incorporated herein by reference.
11 ;;
12 ;; -----------------------------------------------------------------------
13
14 ;;
15 ;; comboot.inc
16 ;; 
17 ;; Common code for running a COMBOOT image
18 ;;
19
20 ; Parameter registers definition; this is the definition
21 ; of the stack frame used by INT 21h and INT 22h.
22 %define         P_FLAGS         word [bp+40]
23 %define         P_FLAGSL        byte [bp+40]
24 %define         P_FLAGSH        byte [bp+41]
25 %define         P_CS            word [bp+38]
26 %define         P_IP            word [bp+36]
27 %define         P_DS            word [bp+34]
28 %define         P_ES            word [bp+32]
29 %define         P_EAX           dword [bp+28]
30 %define         P_AX            word [bp+28]
31 %define         P_HAX           word [bp+30]
32 %define         P_AL            byte [bp+28]
33 %define         P_AH            byte [bp+29]
34 %define         P_ECX           dword [bp+24]
35 %define         P_CX            word [bp+24]
36 %define         P_HCX           word [bp+26]
37 %define         P_CL            byte [bp+24]
38 %define         P_CH            byte [bp+25]
39 %define         P_EDX           dword [bp+20]
40 %define         P_DX            word [bp+20]
41 %define         P_HDX           word [bp+22]
42 %define         P_DL            byte [bp+20]
43 %define         P_DH            byte [bp+21]
44 %define         P_EBX           dword [bp+16]
45 %define         P_BX            word [bp+16]
46 %define         P_HBX           word [bp+18]
47 %define         P_BL            byte [bp+16]
48 %define         P_BH            byte [bp+17]
49 %define         P_EBP           dword [bp+8]
50 %define         P_BP            word [bp+8]
51 %define         P_HBP           word [bp+10]
52 %define         P_ESI           dword [bp+4]
53 %define         P_SI            word [bp+4]
54 %define         P_HSI           word [bp+6]
55 %define         P_EDI           dword [bp]
56 %define         P_DI            word [bp]
57 %define         P_HDI           word [bp+2]
58
59 ; Looks like a COMBOOT image but too large
60 comboot_too_large:
61                 mov si,err_comlarge
62                 call cwritestr
63                 jmp enter_command
64
65 ;
66 ; Load a COMBOOT image.  A COMBOOT image is basically a DOS .COM file,
67 ; except that it may, of course, not contain any DOS system calls.  We
68 ; do, however, allow the execution of INT 20h to return to SYSLINUX.
69 ;
70 is_comboot_image:
71                 and dx,dx
72                 jnz comboot_too_large
73                 cmp ax,0ff00h           ; Max size in bytes
74                 jae comboot_too_large
75
76                 call comboot_setup_api
77
78                 mov cx,comboot_seg
79                 mov es,cx
80
81                 mov bx,100h             ; Load at <seg>:0100h
82
83                 mov cx,[ClustPerMoby]   ; Absolute maximum # of clusters
84                 call getfssec
85
86                 xor di,di
87                 mov cx,64               ; 256 bytes (size of PSP)
88                 xor eax,eax             ; Clear PSP
89                 rep stosd
90
91                 mov word [es:0], 020CDh ; INT 20h instruction
92                 ; First non-free paragraph
93                 mov word [es:02h], comboot_seg+1000h
94
95                 ; Copy the command line from high memory
96                 mov cx,125              ; Max cmdline len (minus space and CR)
97                 mov si,[CmdOptPtr]
98                 mov di,081h             ; Offset in PSP for command line
99                 mov al,' '              ; DOS command lines begin with a space
100                 stosb
101
102 comboot_cmd_cp: lodsb
103                 and al,al
104                 jz comboot_end_cmd
105                 stosb
106                 loop comboot_cmd_cp
107 comboot_end_cmd: mov al,0Dh             ; CR after last character
108                 stosb
109                 mov al,126              ; Include space but not CR
110                 sub al,cl
111                 mov [es:80h], al        ; Store command line length
112
113                 mov [SavedSSSP],sp
114                 mov [SavedSSSP+2],ss    ; Save away SS:SP
115
116                 mov ax,es
117                 mov ds,ax
118                 mov ss,ax
119                 xor sp,sp
120                 push word 0             ; Return to address 0 -> exit
121
122                 jmp comboot_seg:100h    ; Run it
123
124 ; Proper return vector
125 comboot_return: cli                     ; Don't trust anyone
126                 xor ax,ax
127                 jmp comboot_exit
128
129 ;
130 ; Set up the COMBOOT API interrupt vectors.  This is also used
131 ; by the COM32 code.
132 ;
133 comboot_setup_api:
134                 mov di,4*0x20           ; DOS interrupt vectors
135                 mov eax,comboot_return  ; INT 20h = exit
136                 stosd
137                 mov ax,comboot_int21    ; INT 21h = DOS-compatible syscalls
138                 stosd
139                 mov ax,comboot_int22    ; INT 22h = proprietary syscalls
140                 stosd
141                 mov ax,comboot_bogus
142                 mov cx,29               ; All remaining DOS vectors
143                 rep stosd
144                 ret
145
146 ; INT 21h: generic DOS system call
147 comboot_int21:  cli
148                 push ds
149                 push es
150                 pushad
151                 cld
152                 mov bp,cs
153                 mov ds,bp
154                 mov es,bp
155                 mov bp,sp                       ; Set up stack frame
156
157                 mov cx,int21_count
158                 mov si,int21_table
159 .again:         lodsb
160                 cmp al,P_AH
161                 lodsw
162                 loopne .again
163                 ; The last function in the list is the
164                 ; "no such function" function
165
166                 call ax                 ; Call the invoked function
167 comboot_resume:
168                 setc P_FLAGSL           ; Propagate CF->error
169                 popad
170                 pop es
171                 pop ds
172                 iret
173
174 ; Attempted to execute non-21h DOS system call
175 comboot_bogus:  cli                     ; Don't trust anyone
176                 mov ax,err_notdos
177 ;
178 ; Generic COMBOOT return to command line code
179 ;  AX -> message (if any)
180 ;  BX -> where to go next
181 ;
182 comboot_exit:
183                 mov bx,enter_command    ; Normal return to command prompt
184 comboot_exit_special:
185                 xor dx,dx
186                 mov ds,dx
187                 mov es,dx
188                 lss sp,[SavedSSSP]
189                 sti
190                 cld
191                 and ax,ax
192                 je .nomsg
193                 mov si,KernelCName
194                 call cwritestr
195                 xchg si,ax
196                 call cwritestr
197 .nomsg:         jmp bx
198
199 ;
200 ; INT 21h system calls
201 ;
202 comboot_getkey:                         ; 01 = get key with echo
203                 call vgashowcursor
204                 call comboot_getchar
205                 call vgahidecursor
206                 call writechr
207                 clc
208                 ret
209
210 comboot_writechr:                       ; 02 = writechr
211                 mov al,P_DL
212                 call writechr
213                 clc
214                 ret
215
216 comboot_writeserial:                    ; 04 = write serial port
217                 mov al,P_DL
218                 call write_serial
219                 clc
220                 ret
221
222 comboot_getkeynoecho:                   ; 08 = get key w/o echo
223                 call comboot_getchar
224                 clc
225                 ret
226
227 comboot_writestr:                       ; 09 = write string
228                 mov es,P_DS
229                 mov si,P_DX
230 .loop:          es lodsb
231                 cmp al,'$'              ; End string with $ - bizarre
232                 je .done
233                 call writechr
234                 jmp short .loop
235 .done:          clc
236                 ret
237
238 comboot_checkkey:                       ; 0B = check keyboard status
239                 cmp byte [APIKeyFlag],00h
240                 jnz .waiting
241                 call pollchar
242 .waiting:       setz al
243                 dec al                  ; AL = 0FFh if present, 0 if not
244                 mov P_AL,al
245                 clc
246                 ret
247
248 comboot_checkver:                       ; 30 = check DOS version
249                 ; We return 0 in all DOS-compatible version registers,
250                 ; but the high part of eax-ebx-ecx-edx spell "SYSLINUX"
251                 mov P_EAX,'SY' << 16
252                 mov P_EBX,'SL' << 16
253                 mov P_ECX,'IN' << 16
254                 mov P_EDX,'UX' << 16
255                 clc
256                 ret
257
258 comboot_getchar:
259                 cmp byte [APIKeyFlag],00h
260                 jne .queued
261                 call getchar            ; If not queued get input
262                 and al,al               ; Function key?
263                 jnz .done
264                 mov [APIKeyWait],ah     ; High part of key
265                 inc byte [APIKeyFlag]   ; Set flag
266 .done:          mov P_AL,al
267                 ret
268 .queued:        mov al,[APIKeyWait]
269                 dec byte [APIKeyFlag]
270                 jmp .done
271
272 ;
273 ; INT 22h - SYSLINUX-specific system calls
274 ;           System call number in ax
275 ;
276 comboot_int22:
277                 cli
278                 push ds
279                 push es
280                 pushad
281                 cld
282                 mov bp,cs
283                 mov ds,bp
284                 mov es,bp
285                 mov bp,sp                       ; Set up stack frame
286
287                 cmp ax,int22_count
288                 jb .ok
289                 xor ax,ax                       ; Function 0 -> unimplemented
290 .ok:
291                 xchg ax,bx
292                 add bx,bx
293                 call [bx+int22_table]
294                 jmp comboot_resume              ; On return
295
296 ;
297 ; INT 22h AX=0000h      Unimplemented call
298 ;
299 comapi_err:
300                 stc
301                 ret
302
303 ;
304 ; INT 22h AX=0001h      Get SYSLINUX version
305 ;
306 comapi_get_version:
307                 ; Number of API functions supported
308                 mov P_AX,int22_count
309                 ; SYSLINUX version
310                 mov P_CX,(VER_MAJOR << 8)+VER_MINOR
311                 ; SYSLINUX derivative ID byte
312                 mov P_DX,my_id
313                 ; For future use
314                 mov P_BX,cs     ; cs == 0
315
316                 mov P_ES,ds
317                 ; ES:SI -> version banner
318                 mov P_SI,syslinux_banner
319                 ; ES:DI -> copyright string
320                 mov P_DI,copyright_str
321
322 comapi_nop:
323                 clc
324                 ret
325
326 ;
327 ; INT 22h AX=0002h      Write string
328 ;
329 ; Write null-terminated string in ES:BX
330 ;
331 comapi_writestr:
332                 mov ds,P_ES
333                 mov si,P_BX
334                 call writestr
335                 clc
336                 ret
337
338 ;
339 ; INT 22h AX=0003h      Run command
340 ;
341 ; Terminates the COMBOOT program and executes the command line in
342 ; ES:BX as if it had been entered by the user.
343 ;
344 comapi_run:
345                 mov ds,P_ES
346                 mov si,P_BX
347                 mov di,command_line
348 .copyloop:
349                 lodsb
350                 stosb
351                 and al,al
352                 jnz .copyloop
353                 xor ax,ax
354                 mov bx,load_kernel              ; Run a new kernel
355                 jmp comboot_exit_special        ; Terminate task, clean up
356
357 ;
358 ; INT 22h AX=0004h      Run default command             
359 ;
360 ; Terminates the COMBOOT program and executes the default command line
361 ; as if a timeout had happened or the user pressed <Enter>.
362 ;
363 comapi_run_default:
364                 mov bx,auto_boot
365                 jmp comboot_exit_special
366
367 ;
368 ; INT 22h AX=0005h      Force text mode
369 ;
370 ; Puts the video in standard text mode
371 ;
372 comapi_textmode:
373                 call vgaclearmode
374                 clc
375                 ret
376
377 ;
378 ; INT 22h AX=0006h      Open file
379 ;
380 comapi_open:
381                 push ds
382                 mov ds,P_ES
383                 mov si,P_SI
384                 mov di,InitRD
385                 push di
386                 call mangle_name
387                 pop di
388                 pop ds
389                 call searchdir
390                 jz .err
391                 mov P_AX,ax
392                 mov P_HAX,dx
393                 mov ax,[ClustSize]
394                 mov P_CX,ax
395                 clc
396                 ret
397 .err:
398                 stc
399                 ret
400
401
402 ;
403 ; INT 22h AX=0007h      Read file
404 ;
405 comapi_read:
406                 mov ax,P_BX
407                 mov si,P_SI
408                 call getfssec
409                 jnc .noteof
410                 xor si,si               ; SI <- 0 on EOF, CF <- 0
411 .noteof:        mov P_SI,si
412                 ret
413
414 ;
415 ; INT 22h AX=0008h      Close file
416 ;
417 comapi_close:
418                 ; Do nothing for now.  Eventually implement
419                 ; an internal API for this.
420                 clc
421                 ret
422
423 ;
424 ; INT 22h AX=0009h      Call PXE stack
425 ;
426 %if IS_PXELINUX
427 comapi_pxecall:
428                 mov bx,P_BX
429                 mov es,P_ES
430                 mov di,P_DI
431                 call far [PXENVEntry]
432                 clc
433                 ret
434 %else
435 comapi_pxecall  equ comapi_err                  ; Not available
436 %endif
437
438 ;
439 ; INT 22h AX=000Ah      Get Derivative-Specific Info
440 ;
441 comapi_derinfo:
442                 mov P_AL,my_id
443 %if IS_SYSLINUX
444                 mov al,[bsDriveNumber]
445                 mov P_DL,al
446                 mov P_ES,cs
447                 mov P_BX,PartInfo
448 %elif IS_PXELINUX
449                 mov ax,[APIVer]
450                 mov P_DX,ax
451                 mov ax,[StrucPtr]
452                 mov P_BX,ax
453                 mov ax,[StrucPtr+2]
454                 mov P_ES,ax
455 %elif IS_ISOLINUX
456                 mov al,[DriveNo]
457                 mov P_DL,al
458                 mov P_ES,cs
459                 mov P_BX,spec_packet
460 %endif
461                 clc
462                 ret
463
464 ;
465 ; INT 22h AX=000Bh      Get Serial Console Configuration
466 ;
467 comapi_serialcfg:
468                 mov ax,[SerialPort]
469                 mov P_DX,ax
470                 mov ax,[BaudDivisor]
471                 mov P_CX,ax
472                 mov ax,[FlowControl]
473                 or al,ah
474                 mov ah,[FlowIgnore]
475                 shr ah,4
476                 mov P_BX,ax
477                 clc
478                 ret
479
480 ;
481 ; This stuff should really be in the data section...
482 ;
483 %macro          int21 2
484                 db %1
485                 dw %2
486 %endmacro
487
488 int21_table:
489                 int21   00h, comboot_return
490                 int21   01h, comboot_getkey
491                 int21   02h, comboot_writechr
492                 int21   04h, comboot_writeserial
493                 int21   08h, comboot_getkey
494                 int21   09h, comboot_writestr
495                 int21   0Bh, comboot_checkkey
496                 int21   30h, comboot_checkver
497                 int21   4Ch, comboot_return
498                 int21   00h, comboot_bogus
499 int21_count     equ ($-int21_table)/3
500
501                 align 2, db 0
502 int22_table:
503                 dw comapi_err                   ; 0000 unimplemented syscall
504                 dw comapi_get_version           ; 0001 get SYSLINUX version
505                 dw comapi_writestr              ; 0002 write string
506                 dw comapi_run                   ; 0003 run specified command
507                 dw comapi_run_default           ; 0004 run default command
508                 dw comapi_textmode              ; 0005 force text mode
509                 dw comapi_open                  ; 0006 open file
510                 dw comapi_read                  ; 0007 read file
511                 dw comapi_close                 ; 0008 close file
512                 dw comapi_pxecall               ; 0009 call PXE stack
513                 dw comapi_derinfo               ; 000A derivative-specific info
514                 dw comapi_serialcfg             ; 000B get serial port config
515 int22_count     equ ($-int22_table)/2
516
517 APIKeyWait      db 0
518 APIKeyFlag      db 0