Merge commit 'origin/master' into adv
[profile/ivi/syslinux.git] / comboot.inc
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2007 H. Peter Anvin - All Rights Reserved
4 ;;
5 ;;   This program is free software; you can redistribute it and/or modify
6 ;;   it under the terms of the GNU General Public License as published by
7 ;;   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8 ;;   Boston MA 02111-1307, USA; either version 2 of the License, or
9 ;;   (at your option) any later version; incorporated herein by reference.
10 ;;
11 ;; -----------------------------------------------------------------------
12
13 ;;
14 ;; comboot.inc
15 ;;
16 ;; Common code for running a COMBOOT image
17 ;;
18
19                 section .text
20
21 ; Parameter registers definition; this is the definition
22 ; of the stack frame used by INT 21h and INT 22h.
23 %define         P_FLAGS         word [bp+44]
24 %define         P_FLAGSL        byte [bp+44]
25 %define         P_FLAGSH        byte [bp+45]
26 %define         P_CS            word [bp+42]
27 %define         P_IP            word [bp+40]
28 %define         P_DS            word [bp+38]
29 %define         P_ES            word [bp+36]
30 %define         P_FS            word [bp+34]
31 %define         P_GS            word [bp+32]
32 %define         P_EAX           dword [bp+28]
33 %define         P_AX            word [bp+28]
34 %define         P_HAX           word [bp+30]
35 %define         P_AL            byte [bp+28]
36 %define         P_AH            byte [bp+29]
37 %define         P_ECX           dword [bp+24]
38 %define         P_CX            word [bp+24]
39 %define         P_HCX           word [bp+26]
40 %define         P_CL            byte [bp+24]
41 %define         P_CH            byte [bp+25]
42 %define         P_EDX           dword [bp+20]
43 %define         P_DX            word [bp+20]
44 %define         P_HDX           word [bp+22]
45 %define         P_DL            byte [bp+20]
46 %define         P_DH            byte [bp+21]
47 %define         P_EBX           dword [bp+16]
48 %define         P_BX            word [bp+16]
49 %define         P_HBX           word [bp+18]
50 %define         P_BL            byte [bp+16]
51 %define         P_BH            byte [bp+17]
52 %define         P_EBP           dword [bp+8]
53 %define         P_BP            word [bp+8]
54 %define         P_HBP           word [bp+10]
55 %define         P_ESI           dword [bp+4]
56 %define         P_SI            word [bp+4]
57 %define         P_HSI           word [bp+6]
58 %define         P_EDI           dword [bp]
59 %define         P_DI            word [bp]
60 %define         P_HDI           word [bp+2]
61
62 ; Looks like a COMBOOT image but too large
63 comboot_too_large:
64                 mov si,err_comlarge
65                 call cwritestr
66                 jmp enter_command
67
68 ;
69 ; Load a COMBOOT image.  A COMBOOT image is basically a DOS .COM file,
70 ; except that it may, of course, not contain any DOS system calls.  We
71 ; do, however, allow the execution of INT 20h to return to SYSLINUX.
72 ;
73 is_comboot_image:
74                 and dx,dx
75                 jnz comboot_too_large
76                 cmp ax,0ff00h           ; Max size in bytes
77                 jae comboot_too_large
78
79                 push si                 ; Save file handle
80
81                 call make_plain_cmdline
82
83                 call comboot_setup_api
84
85                 mov cx,comboot_seg
86                 mov es,cx
87
88                 xor di,di
89                 mov cx,64               ; 256 bytes (size of PSP)
90                 xor eax,eax             ; Clear PSP
91                 rep stosd
92
93                 mov word [es:0], 020CDh ; INT 20h instruction
94                 ; First non-free paragraph
95                 ; This is valid because comboot_seg == real_mode_seg
96                 ; == the highest segment used by all derivatives
97                 int 12h                 ; Get DOS memory size
98                 shl ax,6                ; Kilobytes -> paragraphs
99                 mov word [es:02h],ax
100
101 %ifndef DEPEND
102 %if real_mode_seg != comboot_seg
103 %error "This code assumes real_mode_seg == comboot_seg"
104 %endif
105 %endif
106                 ; Copy the command line from high memory
107                 mov si,cmd_line_here
108                 mov cx,125              ; Max cmdline len (minus space and CR)
109                 mov di,081h             ; Offset in PSP for command line
110                 mov al,' '              ; DOS command lines begin with a space
111                 stosb
112
113 .loop:          es lodsb
114                 and al,al
115                 jz .done
116                 stosb
117                 loop .loop
118 .done:
119
120                 mov al,0Dh              ; CR after last character
121                 stosb
122                 mov ax,di
123                 sub al,82h              ; Include space but not CR
124                 mov [es:80h],al         ; Store command line length
125
126                 ; Now actually load the file...
127                 pop si                  ; File handle
128                 mov bx,100h             ; Load at <seg>:0100h
129                 mov cx,0FF00h >> SECTOR_SHIFT
130                                         ; Absolute maximum # of sectors
131                 call getfssec
132
133                 ; And invoke the program...
134                 mov ax,es
135                 mov ds,ax
136                 mov ss,ax
137                 xor sp,sp
138                 push word 0             ; Return to address 0 -> exit
139
140                 jmp comboot_seg:100h    ; Run it
141
142 ; Proper return vector
143 comboot_return: cli                     ; Don't trust anyone
144                 push enter_command      ; Normal return to command prompt
145                 jmp comboot_exit
146
147 ;
148 ; Set up the COMBOOT API interrupt vectors.  This is also used
149 ; by the COM32 code.
150 ;
151 comboot_setup_api:
152                 mov di,4*0x20           ; DOS interrupt vectors
153                 mov eax,comboot_return  ; INT 20h = exit
154                 stosd
155                 mov ax,comboot_int21    ; INT 21h = DOS-compatible syscalls
156                 stosd
157                 mov ax,comboot_int22    ; INT 22h = proprietary syscalls
158                 stosd
159                 mov ax,comboot_bogus
160                 mov cx,29               ; All remaining DOS vectors
161                 rep stosd
162                 ret
163
164 ; INT 21h: generic DOS system call
165 comboot_int21:  cli
166                 push ds
167                 push es
168                 push fs
169                 push gs
170                 pushad
171                 cld
172                 mov bp,cs
173                 mov ds,bp
174                 mov es,bp
175                 mov bp,sp                       ; Set up stack frame
176
177                 call adjust_screen              ; The COMBOOT program might have changed the screen
178
179                 mov cx,int21_count
180                 mov si,int21_table
181 .again:         lodsb
182                 cmp al,P_AH
183                 lodsw
184                 loopne .again
185                 ; The last function in the list is the
186                 ; "no such function" function
187                 clc
188                 call ax                 ; Call the invoked function
189 comboot_resume:
190                 mov bp,sp               ; In case the function clobbers BP
191                 setc P_FLAGSL           ; Propagate CF->error
192                 popad
193                 pop gs
194                 pop fs
195                 pop es
196                 pop ds
197                 iret
198
199 ; Attempted to execute non-21h DOS system call
200 comboot_bogus:  cli                     ; Don't trust anyone
201                 mov cx,err_notdos
202                 push enter_command
203                 jmp comboot_exit_msg
204
205 ;
206 ; Generic COMBOOT return to command line code
207 ;  stack -> where to go next
208 ;     CX -> message (for _msg version)
209 ;
210 comboot_exit:
211                 xor cx,cx
212 comboot_exit_msg:
213                 pop bx                  ; Return address
214                 RESET_STACK_AND_SEGS AX
215                 call adjust_screen      ; The COMBOOT program might have changed the screen
216                 jcxz .nomsg
217                 mov si,KernelCName
218                 call cwritestr
219                 mov si,cx
220                 call cwritestr
221 .nomsg:
222                 jmp bx
223
224 ;
225 ; INT 21h system calls
226 ;
227 comboot_getkey:                         ; 01 = get key with echo
228                 call vgashowcursor
229                 call comboot_getchar
230                 call vgahidecursor
231                 call writechr
232                 clc
233                 ret
234
235 comboot_writechr:                       ; 02 = writechr
236                 mov al,P_DL
237                 call writechr
238                 clc
239                 ret
240
241 comboot_writeserial:                    ; 04 = write serial port
242                 mov al,P_DL
243                 call write_serial
244                 clc
245                 ret
246
247 comboot_getkeynoecho:                   ; 08 = get key w/o echo
248                 call comboot_getchar
249                 clc
250                 ret
251
252 comboot_writestr:                       ; 09 = write DOS string
253                 mov es,P_DS
254                 mov si,P_DX
255 .loop:          es lodsb
256                 cmp al,'$'              ; End string with $ - bizarre
257                 je .done
258                 call writechr
259                 jmp short .loop
260 .done:          clc
261                 ret
262
263 comboot_checkkey:                       ; 0B = check keyboard status
264                 cmp byte [APIKeyFlag],00h
265                 jnz .waiting
266                 call pollchar
267 .waiting:       setz al
268                 dec al                  ; AL = 0FFh if present, 0 if not
269                 mov P_AL,al
270                 clc
271                 ret
272
273 comboot_checkver:                       ; 30 = check DOS version
274                 ; We return 0 in all DOS-compatible version registers,
275                 ; but the high part of eax-ebx-ecx-edx spell "SYSLINUX"
276                 mov P_EAX,'SY' << 16
277                 mov P_EBX,'SL' << 16
278                 mov P_ECX,'IN' << 16
279                 mov P_EDX,'UX' << 16
280                 ret
281
282 comboot_getchar:
283                 cmp byte [APIKeyFlag],00h
284                 jne .queued
285                 call getchar            ; If not queued get input
286                 and al,al               ; Function key?  (CF <- 0)
287                 jnz .done
288                 mov [APIKeyWait],ah     ; High part of key
289                 inc byte [APIKeyFlag]   ; Set flag
290 .done:          mov P_AL,al
291                 ret
292 .queued:        mov al,[APIKeyWait]
293                 dec byte [APIKeyFlag]
294                 jmp .done
295
296 ;
297 ; INT 22h - SYSLINUX-specific system calls
298 ;           System call number in ax
299 ;
300 comboot_int22:
301                 cli
302                 push ds
303                 push es
304                 push fs
305                 push gs
306                 pushad
307                 cld
308                 mov bp,cs
309                 mov ds,bp
310                 mov es,bp
311                 mov bp,sp                       ; Set up stack frame
312
313                 call adjust_screen              ; The COMBOOT program might have changed the screen
314
315                 cmp ax,int22_count
316                 jb .ok
317                 xor ax,ax                       ; Function 0 -> unimplemented
318 .ok:
319                 xchg ax,bx
320                 add bx,bx                       ; CF <- 0
321                 call [bx+int22_table]
322                 jmp comboot_resume              ; On return
323
324 ;
325 ; INT 22h AX=0000h      Unimplemented call
326 ;
327 comapi_err:
328                 stc
329                 ret
330
331 ;
332 ; INT 22h AX=0001h      Get SYSLINUX version
333 ;
334 comapi_get_version:
335                 ; Number of API functions supported
336                 mov P_AX,int22_count
337                 ; SYSLINUX version
338                 mov P_CX,(VER_MAJOR << 8)+VER_MINOR
339                 ; SYSLINUX derivative ID byte
340                 mov P_DX,my_id
341                 ; For future use
342                 mov P_BX,cs     ; cs == 0
343
344                 mov P_ES,ds
345                 ; ES:SI -> version banner
346                 mov P_SI,syslinux_banner
347                 ; ES:DI -> copyright string
348                 mov P_DI,copyright_str
349
350 comapi_nop:
351                 clc
352                 ret
353
354 ;
355 ; INT 22h AX=0002h      Write string
356 ;
357 ; Write null-terminated string in ES:BX
358 ;
359 comapi_writestr:
360                 mov ds,P_ES
361                 mov si,P_BX
362                 call writestr
363                 clc
364                 ret
365
366 ;
367 ; INT 22h AX=0003h      Run command
368 ;
369 ; Terminates the COMBOOT program and executes the command line in
370 ; ES:BX as if it had been entered by the user.
371 ;
372 comapi_run:
373                 mov ds,P_ES
374                 mov si,P_BX
375                 mov di,command_line
376                 call strcpy
377                 push load_kernel                ; Run a new kernel
378                 jmp comboot_exit                ; Terminate task, clean up
379
380 ;
381 ; INT 22h AX=0004h      Run default command
382 ;
383 ; Terminates the COMBOOT program and executes the default command line
384 ; as if a timeout had happened or the user pressed <Enter>.
385 ;
386 comapi_run_default:
387                 push auto_boot
388                 jmp comboot_exit
389
390 ;
391 ; INT 22h AX=0005h      Force text mode
392 ;
393 ; Puts the video in standard text mode
394 ;
395 comapi_textmode:
396                 call vgaclearmode
397                 clc
398                 ret
399
400 ;
401 ; INT 22h AX=0006h      Open file
402 ;
403 comapi_open:
404                 push ds
405                 mov ds,P_ES
406                 mov si,P_SI
407                 mov di,InitRD
408                 call mangle_name
409                 pop ds
410                 call searchdir
411                 jz comapi_err
412                 mov P_AX,ax
413                 mov P_HAX,dx
414                 mov P_CX,SECTOR_SIZE
415                 mov P_SI,si
416                 clc
417                 ret
418
419 ;
420 ; INT 22h AX=0007h      Read file
421 ;
422 comapi_read:
423                 mov es,P_ES
424                 mov bx,P_BX
425                 mov si,P_SI
426                 mov cx,P_CX
427                 call getfssec
428                 jnc .noteof
429                 xor si,si               ; SI <- 0 on EOF, CF <- 0
430 .noteof:        mov P_SI,si
431                 ret
432
433 ;
434 ; INT 22h AX=0008h      Close file
435 ;
436 comapi_close:
437                 mov si,P_SI
438                 call close_file
439                 clc
440                 ret
441
442 ;
443 ; INT 22h AX=0009h      Call PXE stack
444 ;
445 %if IS_PXELINUX
446 comapi_pxecall:
447                 mov bx,P_BX
448                 mov es,P_ES
449                 mov di,P_DI
450                 call pxenv
451                 mov P_AX,ax
452                 clc
453                 ret
454 %else
455 comapi_pxecall  equ comapi_err                  ; Not available
456 %endif
457
458 ;
459 ; INT 22h AX=000Ah      Get Derivative-Specific Info
460 ;
461 comapi_derinfo:
462                 mov P_AL,my_id
463 %if IS_PXELINUX
464                 mov ax,[APIVer]
465                 mov P_DX,ax
466                 mov ax,[StrucPtr]
467                 mov P_BX,ax
468                 mov ax,[StrucPtr+2]
469                 mov P_ES,ax
470                 mov ax,[InitStack]
471                 mov P_SI,ax
472                 mov ax,[InitStack+2]
473                 mov P_FS,ax
474 %else
475                 ; Physical medium...
476
477                 mov P_CL,SECTOR_SHIFT
478                 mov al,[DriveNumber]
479                 mov P_DL,al
480                 mov P_FS,cs
481                 mov P_SI,OrigESDI
482 %if IS_SYSLINUX || IS_MDSLINUX || IS_EXTLINUX
483                 mov P_ES,cs
484                 mov P_BX,PartInfo
485 %elif IS_ISOLINUX
486                 mov P_ES,cs
487                 mov P_BX,spec_packet
488 %endif
489 %endif
490                 clc
491                 ret
492
493 ;
494 ; INT 22h AX=000Bh      Get Serial Console Configuration
495 ;
496 comapi_serialcfg:
497                 mov ax,[SerialPort]
498                 mov P_DX,ax
499                 mov ax,[BaudDivisor]
500                 mov P_CX,ax
501                 mov ax,[FlowControl]
502                 or al,ah
503                 mov ah,[FlowIgnore]
504                 shr ah,4
505                 test byte [DisplayCon],01h
506                 jnz .normalconsole
507                 or ah,80h
508 .normalconsole:
509                 mov P_BX,ax
510                 clc
511                 ret
512
513 ;
514 ; INT 22h AX=000Ch      Perform final cleanup
515 ;
516 comapi_cleanup:
517 %if IS_PXELINUX
518                 ; Unload PXE if requested
519                 test dl,3
520                 setnz [KeepPXE]
521                 sub bp,sp               ; unload_pxe may move the stack around
522                 call unload_pxe
523                 add bp,sp               ; restore frame pointer...
524 %elif IS_SYSLINUX || IS_MDSLINUX || IS_EXTLINUX
525                 ; Restore original FDC table
526                 mov eax,[OrigFDCTabPtr]
527                 mov [fdctab],eax
528 %endif
529                 ; Reset the floppy disk subsystem
530                 xor ax,ax
531                 xor dx,dx
532                 int 13h
533                 clc
534                 ret
535
536 ;
537 ; INT 22h AX=000Dh      Clean up then replace bootstrap
538 ;
539 comapi_chainboot:
540                 call comapi_cleanup
541                 mov eax,P_EDI
542                 mov [trackbuf+4],eax            ; Copy from
543                 mov eax,P_ECX
544                 mov [trackbuf+8],eax            ; Total bytes
545                 mov eax,7C00h
546                 mov [trackbuf],eax              ; Copy to
547                 mov [EntryPoint],eax            ; CS:IP entry point
548                 mov esi,P_ESI
549                 mov edx,P_EBX
550                 mov bx,P_DS
551                 jmp replace_bootstrap_one
552
553
554 ;
555 ; INT 22h AX=000Eh      Get configuration file name
556 ;
557 comapi_configfile:
558                 mov P_ES,cs
559                 mov P_BX,ConfigName
560                 clc
561                 ret
562
563 ;
564 ; INT 22h AX=000Fh      Get IPAPPEND strings
565 ;
566 %if IS_PXELINUX
567 comapi_ipappend:
568                 mov P_ES,cs
569                 mov P_CX,numIPAppends
570                 mov P_BX,IPAppends
571                 clc
572                 ret
573
574                 section .data
575                 alignb 2, db 0
576 IPAppends       dw IPOption
577                 dw BOOTIFStr
578 numIPAppends    equ ($-IPAppends)/2
579
580 %else
581 comapi_ipappend equ comapi_err
582 %endif
583
584                 section .text
585
586 ;
587 ; INT 22h AX=0010h      Resolve hostname
588 ;
589 %if IS_PXELINUX
590 comapi_dnsresolv:
591                 mov ds,P_ES
592                 mov si,P_BX
593                 call dns_resolv
594                 mov P_EAX,eax
595                 clc
596                 ret
597 %else
598 comapi_dnsresolv equ comapi_err
599 %endif
600
601                 section .text
602
603 ;
604 ; INT 22h AX=0011h      Maximum number of shuffle descriptors
605 ;
606 comapi_maxshuffle:
607                 mov P_CX,(2*trackbufsize)/12
608                 ret
609
610 ;
611 ; INT 22h AX=0012h      Cleanup, shuffle and boot
612 ;
613 comapi_shuffle:
614                 cmp P_CX,(2*trackbufsize)/12
615                 ja .error
616
617                 call comapi_cleanup
618
619                 mov cx, P_CX
620                 push cx                         ; On stack: descriptor count
621
622                 lea cx,[ecx+ecx*2]              ; CX *= 3
623
624                 mov fs,P_ES
625                 mov si,P_DI
626                 mov di,trackbuf
627                 push di                         ; On stack: descriptor list address
628                 fs rep movsd                    ; Copy the list
629
630                 mov eax,P_EBP
631                 mov [EntryPoint],eax            ; CS:IP entry point
632                 mov esi,P_ESI
633                 mov edx,P_EBX
634                 mov bx,P_DS
635                 jmp replace_bootstrap
636 .error:
637                 stc
638                 ret
639
640 ;
641 ; INT 22h AX=0013h      Idle call
642 ;
643 ;
644 ; *** FIX THIS ***
645 ; The idle call seems to have detrimental effects on some machines when
646 ; called from a COM32 context (WHY?) --  disable it for now.
647 ;
648 %if 0 ; def HAVE_IDLE
649
650 comapi_idle:
651                 DO_IDLE
652                 clc
653                 ret
654
655 %else
656
657 comapi_idle     equ comapi_err
658
659 %endif
660
661 ;
662 ; INT 22h AX=0014h      Local boot
663 ;
664 %if IS_PXELINUX || IS_ISOLINUX
665 comapi_localboot:
666                 mov ax,P_DX
667                 jmp local_boot
668 %else
669 comapi_localboot equ comapi_err
670 %endif
671
672 ;
673 ; INT 22h AX=0015h      Feature flags
674 ;
675 comapi_features:
676                 mov P_ES,cs
677                 mov P_BX,feature_flags
678                 mov P_CX,feature_flags_len
679                 clc
680                 ret
681
682 ;
683 ; INT 22h AX=0016h      Run kernel image
684 ;
685 comapi_runkernel:
686                 mov al,P_DL
687                 cmp al,VK_TYPES-1
688                 ja .error
689                 mov [KernelType],al
690                 push ds
691                 mov ds,P_DS
692                 mov si,P_SI
693                 mov di,KernelName
694                 call mangle_name
695                 pop ds
696                 call searchdir
697                 jz comapi_err
698
699                 ; The kernel image was found, so we can load it...
700                 mov [Kernel_SI],si
701                 mov [Kernel_EAX],ax
702                 mov [Kernel_EAX+2],dx
703
704                 ; It's not just possible, but quite likely, that ES:BX
705                 ; points into real_mode_seg, so we need to exercise some
706                 ; special care here... use xfer_buf_seg as an intermediary
707                 push ds
708                 push es
709                 mov ax,xfer_buf_seg
710                 mov ds,P_ES
711                 mov si,P_BX
712                 mov es,ax
713                 xor di,di
714                 call strcpy
715                 pop es
716                 pop ds
717
718 %if IS_PXELINUX
719                 mov al,P_CL
720                 mov [IPAppend],al
721 %endif
722
723                 call comboot_exit
724
725 .finish:
726                 ; Copy the command line into its proper place
727                 push ds
728                 push es
729                 mov ax,xfer_buf_seg
730                 mov dx,real_mode_seg
731                 mov ds,ax
732                 mov es,dx
733                 xor si,si
734                 mov di,cmd_line_here
735                 call strcpy
736                 mov byte [es:di-1],' '          ; Simulate APPEND
737                 pop es
738                 pop ds
739                 mov [CmdLinePtr],di
740                 mov word [CmdOptPtr],zero_string
741                 jmp kernel_good_saved
742
743 .error          equ comapi_shuffle.error
744
745 ;
746 ; INT 22h AX=0017h  Report video mode change
747 ;
748 comapi_usingvga:
749                 mov ax,P_BX
750                 cmp ax,0Fh              ; Unknown flags = failure
751                 ja .error
752                 mov [UsingVGA],al
753                 mov cx,P_CX
754                 mov dx,P_DX
755                 mov [GXPixCols],cx
756                 mov [GXPixRows],dx
757                 test al,08h
758                 jnz .notext
759                 call adjust_screen
760 .notext:
761                 clc
762                 ret
763 .error:
764                 stc
765                 ret
766
767 ;
768 ; INT 22h AX=0018h  Query custom font
769 ;
770 comapi_userfont:
771                 mov al,[UserFont]
772                 and al,al
773                 jz .done
774                 mov al,[VGAFontSize]
775                 mov P_ES,ds
776                 mov P_BX,vgafontbuf
777
778 .done:          ; CF=0 here
779                 mov P_AL,al
780                 ret
781
782 ;
783 ; INT 22h AX=0019h  Read disk
784 ;
785 %if IS_SYSLINUX || IS_MDSLINUX || IS_ISOLINUX || IS_EXTLINUX
786 comapi_readdisk:
787                 mov esi,P_ESI           ; Enforce ESI == EDI == 0, these
788                 or esi,P_EDI            ; are reserved for future expansion
789                 jnz .err
790                 mov eax,P_EDX
791                 mov es,P_ES
792                 mov bx,P_BX
793                 mov bp,P_CX             ; WE CANNOT use P_* after touching bp!
794                 call getlinsec
795                 clc
796                 ret
797 .err:
798                 stc
799                 ret
800 %else
801 comapi_readdisk equ comapi_err
802 %endif
803
804 ;
805 ; INT 22h AX=001Ah      Cleanup, shuffle and boot to flat protected mode
806 ;
807 comapi_shufflepm:
808                 cmp P_CX,(2*trackbufsize)/12
809                 ja .error
810
811                 call comapi_cleanup
812
813                 mov cx, P_CX
814                 push cx                         ; On stack: descriptor count
815
816                 lea cx,[ecx+ecx*2]              ; CX *= 3
817
818                 mov fs,P_ES
819                 mov si,P_DI
820                 mov di,trackbuf
821                 push di                         ; On stack: descriptor list address
822                 fs rep movsd                    ; Copy the list
823
824                 mov fs,P_DS
825                 mov si,P_SI
826                 mov edi,TrampolineBuf
827                 mov al,0B8h                     ; MOV EAX opcode
828                 mov cl,9
829 .maketramp:
830                 stosb                           ; MOV opcode
831                 inc ax                          ; Next register opcode
832                 fs movsd                        ; immediate value
833                 loop .maketramp
834                 mov byte [di-5],0E9h            ; Last opcode is JMP
835                 sub [di-4],edi                  ; Make JMP target relative
836
837                 mov dword [EntryPoint],trampoline_to_pm
838                 xor bx,bx                       ; DS on entry
839                 jmp replace_bootstrap
840 .error:
841                 stc
842                 ret
843
844 ;
845 ; INT 22h AX=001Bh      Cleanup, shuffle and boot with register setting
846 ;
847 comapi_shufflerm:
848                 cmp P_CX,(2*trackbufsize)/12
849                 ja .error
850
851                 call comapi_cleanup
852
853                 mov cx, P_CX
854                 push cx                         ; On stack: descriptor count
855
856                 lea cx,[ecx+ecx*2]              ; CX *= 3
857
858                 mov fs,P_ES
859                 mov si,P_DI
860                 mov di,trackbuf
861                 push di                         ; On stack: descriptor list address
862                 fs rep movsd                    ; Copy the list
863
864                 mov fs,P_DS
865                 mov si,P_SI
866                 mov di,TrampolineBuf
867
868                 ; Generate segment-loading instructions
869                 mov bx,0C08Eh                   ; MOV ES,AX
870                 mov cl,6                        ; 6 segment registers (incl CS)
871 .segtramp:
872                 mov al,0B8h
873                 stosb                           ; MOV AX,imm16 opcode
874                 fs movsw                        ; imm16
875                 mov ax,bx
876                 add bh,8
877                 stosw                           ; MOV xS,AX
878                 loop .segtramp
879
880                 ; Clobber the MOV CS,AX instruction.
881                 mov word [di-22], 9090h         ; NOP NOP
882
883                 ; Generate GPR-loading instructions
884                 mov ax,0B866h                   ; MOV EAX,imm32
885                 mov cl,8                        ; 8 GPRs
886 .gprtramp:
887                 stosw                           ; MOV ExX,imm32 opcode
888                 fs movsd                        ; imm32
889                 inc ah
890                 loop .gprtramp
891
892                 mov al,0EAh                     ; JMP FAR imm16:imm16 opcode
893                 stosb
894                 fs movsd                        ; CS:IP
895
896                 mov dword [EntryPoint],TrampolineBuf
897                 jmp replace_bootstrap
898 .error:
899                 stc
900                 ret
901
902 ;
903 ; INT 22h AX=001Ch      Get pointer to auxillary data vector
904 ;
905 comapi_getadv:
906                 mov P_ES,ds
907                 mov P_BX,adv0.data
908                 mov P_CX,ADV_LEN
909                 ret
910
911 ;
912 ; INT 22h AX=001Dh      Write auxillary data vector
913 ;
914 comapi_writeadv equ adv_write
915
916                 section .data
917
918 %macro          int21 2
919                 db %1
920                 dw %2
921 %endmacro
922
923 int21_table:
924                 int21   00h, comboot_return
925                 int21   01h, comboot_getkey
926                 int21   02h, comboot_writechr
927                 int21   04h, comboot_writeserial
928                 int21   08h, comboot_getkeynoecho
929                 int21   09h, comboot_writestr
930                 int21   0Bh, comboot_checkkey
931                 int21   30h, comboot_checkver
932                 int21   4Ch, comboot_return
933                 int21    -1, comboot_bogus
934 int21_count     equ ($-int21_table)/3
935
936                 align 2, db 0
937 int22_table:
938                 dw comapi_err           ; 0000 unimplemented syscall
939                 dw comapi_get_version   ; 0001 get SYSLINUX version
940                 dw comapi_writestr      ; 0002 write string
941                 dw comapi_run           ; 0003 run specified command
942                 dw comapi_run_default   ; 0004 run default command
943                 dw comapi_textmode      ; 0005 force text mode
944                 dw comapi_open          ; 0006 open file
945                 dw comapi_read          ; 0007 read file
946                 dw comapi_close         ; 0008 close file
947                 dw comapi_pxecall       ; 0009 call PXE stack
948                 dw comapi_derinfo       ; 000A derivative-specific info
949                 dw comapi_serialcfg     ; 000B get serial port config
950                 dw comapi_cleanup       ; 000C perform final cleanup
951                 dw comapi_chainboot     ; 000D clean up then bootstrap
952                 dw comapi_configfile    ; 000E get name of config file
953                 dw comapi_ipappend      ; 000F get ipappend strings
954                 dw comapi_dnsresolv     ; 0010 resolve hostname
955                 dw comapi_maxshuffle    ; 0011 maximum shuffle descriptors
956                 dw comapi_shuffle       ; 0012 cleanup, shuffle and boot
957                 dw comapi_idle          ; 0013 idle call
958                 dw comapi_localboot     ; 0014 local boot
959                 dw comapi_features      ; 0015 feature flags
960                 dw comapi_runkernel     ; 0016 run kernel image
961                 dw comapi_usingvga      ; 0017 report video mode change
962                 dw comapi_userfont      ; 0018 query custom font
963                 dw comapi_readdisk      ; 0019 read disk
964                 dw comapi_shufflepm     ; 001A cleanup, shuffle and boot to pm
965                 dw comapi_shufflerm     ; 001B cleanup, shuffle and boot to rm
966                 dw comapi_getadv        ; 001C get pointer to ADV
967                 dw comapi_writeadv      ; 001D write ADV to disk
968 int22_count     equ ($-int22_table)/2
969
970 APIKeyWait      db 0
971 APIKeyFlag      db 0
972
973 zero_string     db 0                    ; Empty, null-terminated string
974
975 ;
976 ; This is the feature flag array for INT 22h AX=0015h
977 feature_flags:
978 %if IS_PXELINUX
979                 db 1                    ; Have local boot, idle not noop
980 %elif IS_ISOLINUX
981                 db 3                    ; Have local boot, idle is noop
982 %else
983                 db 2                    ; No local boot, idle is noop
984 %endif
985 feature_flags_len equ ($-feature_flags)
986
987 err_notdos      db ': attempted DOS system call', CR, LF, 0
988 err_comlarge    db 'COMBOOT image too large.', CR, LF, 0
989
990                 section .bss1
991 ConfigName      resb    FILENAME_MAX