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