Merge commit 'origin/nolen' into gpxe-support
[syslinux.git] / ui.inc
blob06777332b1b1d595e14d33823a800f3b12af7ac3
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2008 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.
11 ;; -----------------------------------------------------------------------
14 ; This file should be entered with the config file open (for getc)
16 load_config_file:
17                 call parse_config               ; Parse configuration file
18 no_config_file:
20                 call adv_init
22 ; Check for an ADV boot-once entry
24                 mov dl,ADV_BOOTONCE
25                 call adv_get
26                 jcxz .no_bootonce
28 .have_bootone:
29                 ; We apparently have a boot-once set; clear it and
30                 ; then execute the boot-once...
32                 ; Save the boot-once data; SI = data, CX = length
33                 mov di,command_line
34                 rep movsb
35                 xor ax,ax
36                 stosb
38                 ; Clear the boot-once data from the ADV
39                 xor cx,cx                       ; Set to zero = delete
40                 call adv_set
41                 jc .err
42                 call adv_write
43 .err:           jmp load_kernel
45 .no_bootonce:
48 ; Check whether or not we are supposed to display the boot prompt.
50 check_for_key:
51                 cmp word [ForcePrompt],0        ; Force prompt?
52                 jnz enter_command
53                 test byte [KbdFlags],5Bh        ; Shift Alt Caps Scroll
54                 jz auto_boot                    ; If neither, default boot
56 enter_command:
57                 cmp word [NoEscape],0           ; If NOESCAPE, no prompt,
58                 jne auto_boot                   ; always run default cmd
60                 mov si,boot_prompt
61                 call cwritestr
63                 mov byte [FuncFlag],0           ; <Ctrl-F> not pressed
64                 mov di,command_line
67 ; get the very first character -- we can either time
68 ; out, or receive a character press at this time.  Some dorky BIOSes stuff
69 ; a return in the buffer on bootup, so wipe the keyboard buffer first.
71 clear_buffer:   mov ah,11h                      ; Check for pending char
72                 int 16h
73                 jz get_char_time
74                 mov ah,10h                      ; Get char
75                 int 16h
76                 jmp short clear_buffer
78                 ; For the first character, both KbdTimeout and
79                 ; TotalTimeout apply; after that, only TotalTimeout.
81 get_char_time:
82                 mov eax,[TotalTimeout]
83                 mov [ThisTotalTo],eax
84                 mov eax,[KbdTimeout]
85                 mov [ThisKbdTo],eax
87 get_char:
88                 call getchar_timeout
89                 and dword [ThisKbdTo],0         ; For the next time...
91                 and al,al
92                 jz func_key
94 got_ascii:      cmp al,7Fh                      ; <DEL> == <BS>
95                 je backspace
96                 cmp al,' '                      ; ASCII?
97                 jb not_ascii
98                 ja enter_char
99                 cmp di,command_line             ; Space must not be first
100                 je short get_char
101 enter_char:     test byte [FuncFlag],1
102                 jnz ctrl_f                      ; Keystroke after <Ctrl-F>
103                 cmp di,max_cmd_len+command_line ; Check there's space
104                 jnb short get_char
105                 stosb                           ; Save it
106                 call writechr                   ; Echo to screen
107                 jmp short get_char
108 not_ascii:
109                 cmp al,0Dh                      ; Enter
110                 je command_done
111                 cmp al,'F' & 1Fh                ; <Ctrl-F>
112                 je set_func_flag
113 %if IS_PXELINUX
114                 cmp al,'N' & 1Fh                ; <Ctrl-N>
115                 je show_network_info
116 %endif
117                 cmp al,'U' & 1Fh                ; <Ctrl-U>
118                 je kill_command                 ; Kill input line
119                 cmp al,'V' & 1Fh                ; <Ctrl-V>
120                 je print_version
121                 cmp al,'X' & 1Fh                ; <Ctrl-X>
122                 je force_text_mode
123                 cmp al,08h                      ; Backspace
124                 jne get_char
125 backspace:      cmp di,command_line             ; Make sure there is anything
126                 je get_char                     ; to erase
127                 dec di                          ; Unstore one character
128                 mov si,wipe_char                ; and erase it from the screen
129                 call cwritestr
130 get_char_2:
131                 jmp short get_char
133 kill_command:
134                 call crlf
135                 jmp enter_command
137 force_text_mode:
138                 call vgaclearmode
139                 jmp enter_command
141 set_func_flag:
142                 mov byte [FuncFlag],1
143                 jmp short get_char_2
145 ctrl_f:
146                 xor ah,ah
147                 mov [FuncFlag],ah
148                 cmp al,'0'
149                 jb get_char_2
150                 je .zero                        ; <Ctrl-F>0 = F10
151                 or al,20h                       ; Lower case
152                 cmp al,'9'
153                 jna .digit
154                 cmp al,'a'                      ; F10-F12 = <Ctrl-F>A, B, C
155                 jb get_char_2
156                 cmp al,'c'
157                 ja get_char_2
158                 sub al,'a'-10
159                 jmp show_help
160 .zero:
161                 mov al,10
162                 jmp show_help
163 .digit:
164                 sub al,'1'
165                 jmp show_help
167 func_key:
168                 ; AL = 0 if we get here
169                 xchg al,ah
170                 cmp al,44h                      ; F10
171                 ja .f11_f12
172                 sub al,3Bh                      ; F1
173                 jb get_char_2
174                 jmp show_help
175 .f11_f12:
176                 cmp al,85h                      ; F11
177                 jb get_char_2
178                 cmp al,86h                      ; F12
179                 ja get_char_2
180                 sub al,85h-10
182 show_help:      ; AX = func key # (0 = F1, 9 = F10, 11 = F12)
183                 push di                         ; Save end-of-cmdline pointer
184                 shl ax,FILENAME_MAX_LG2         ; Convert to pointer
185                 add ax,FKeyName
186                 xchg di,ax
187                 cmp byte [di+NULLOFFSET],NULLFILE
188                 je short fk_nofile              ; Undefined F-key
189                 call open
190                 jz short fk_nofile              ; File not found
191                 call crlf
192                 call get_msg_file
193                 jmp short fk_wrcmd
195 print_version:
196                 push di                         ; Command line write pointer
197                 mov si,syslinux_banner
198                 call cwritestr
199 %ifdef HAVE_BIOSNAME
200                 mov si,[BIOSName]
201                 call cwritestr
202 %endif
203                 mov si,copyright_str
204                 call cwritestr
206                 ; ... fall through ...
208                 ; Write the boot prompt and command line again and
209                 ; wait for input.  Note that this expects the cursor
210                 ; to already have been CRLF'd, and that the old value
211                 ; of DI (the command line write pointer) is on the stack.
212 fk_wrcmd:
213                 mov si,boot_prompt
214                 call cwritestr
215                 pop di                          ; Command line write pointer
216                 push di
217                 mov byte [di],0                 ; Null-terminate command line
218                 mov si,command_line
219                 call cwritestr                  ; Write command line so far
220 fk_nofile:      pop di
221                 jmp get_char
224 ; Show network info (in the form of the ipappend strings)
226 %if IS_PXELINUX
227 show_network_info:
228                 push di                         ; Command line write pointer
229                 call crlf
230                 mov si,IPAppends                ; See comboot.doc
231                 mov cx,numIPAppends
232 .loop:
233                 lodsw
234                 push si
235                 mov si,ax
236                 call cwritestr
237                 call crlf
238                 pop si
239                 loop .loop
240                 jmp fk_wrcmd
241 %endif
244 ; Jump here to run the default command line
246 auto_boot:
247                 mov si,default_cmd
248                 mov di,command_line
249                 mov cx,(max_cmd_len+4) >> 2
250                 rep movsd
251                 jmp short load_kernel
254 ; Jump here when the command line is completed
256 command_done:
257                 call crlf
258                 cmp di,command_line             ; Did we just hit return?
259                 je auto_boot
260                 xor al,al                       ; Store a final null
261                 stosb
263 load_kernel:                                    ; Load the kernel now
265 ; First we need to mangle the kernel name the way DOS would...
267                 mov si,command_line
268                 mov di,KernelName
269                 push si
270                 call mangle_name
271                 pop si
273 ; Fast-forward to first option (we start over from the beginning, since
274 ; mangle_name doesn't necessarily return a consistent ending state.)
276 clin_non_wsp:   lodsb
277                 cmp al,' '
278                 ja clin_non_wsp
279 clin_is_wsp:    and al,al
280                 jz clin_opt_ptr
281                 lodsb
282                 cmp al,' '
283                 jbe clin_is_wsp
284 clin_opt_ptr:   dec si                          ; Point to first nonblank
285                 mov [CmdOptPtr],si              ; Save ptr to first option
287 ; If "allowoptions 0", put a null character here in order to ignore any
288 ; user-specified options.
290                 mov ax,[AllowOptions]
291                 and ax,ax
292                 jnz clin_opt_ok
293                 mov [si],al
294 clin_opt_ok:
297 ; Now check if it is a "virtual kernel"
299 vk_check:
300                 mov esi,[HighMemSize]           ; Start from top of memory
301 .scan:
302                 cmp esi,[VKernelEnd]
303                 jbe .not_vk
305                 mov di,VKernelBuf
306                 call rllunpack
307                 ; ESI updated on return
309                 sub di,cx                       ; Return to beginning of buf
310                 push si
311                 mov si,KernelName
312                 mov cx,FILENAME_MAX
313                 es repe cmpsb
314                 pop si
315                 je .found
316                 jmp .scan
319 ; We *are* using a "virtual kernel"
321 .found:
322                 push es
323                 push word real_mode_seg
324                 pop es
325                 mov di,cmd_line_here
326                 mov si,VKernelBuf+vk_append
327                 mov cx,[VKernelBuf+vk_appendlen]
328                 rep movsb
329                 mov [CmdLinePtr],di             ; Where to add rest of cmd
330                 pop es
331                 mov di,KernelName
332                 push di
333                 mov si,VKernelBuf+vk_rname
334                 mov cx,FILENAME_MAX             ; We need ECX == CX later
335                 rep movsb
336                 pop di
337 %if IS_PXELINUX
338                 mov al,[VKernelBuf+vk_ipappend]
339                 mov [IPAppend],al
340 %endif
341                 xor bx,bx                       ; Try only one version
343                 mov al, [VKernelBuf+vk_type]
344                 mov [KernelType], al
346 %if IS_PXELINUX || IS_ISOLINUX
347                 ; Is this a "localboot" pseudo-kernel?
348 %if IS_PXELINUX
349                 cmp byte [VKernelBuf+vk_rname+4], 0
350 %else
351                 cmp byte [VKernelBuf+vk_rname], 0
352 %endif
353                 jne get_kernel          ; No, it's real, go get it
355                 mov ax, [VKernelBuf+vk_rname+1]
356                 jmp local_boot
357 %else
358                 jmp get_kernel
359 %endif
361 .not_vk:
364 ; Not a "virtual kernel" - check that's OK and construct the command line
366                 cmp word [AllowImplicit],byte 0
367                 je bad_implicit
368                 push es
369                 push si
370                 push di
371                 mov di,real_mode_seg
372                 mov es,di
373                 mov si,AppendBuf
374                 mov di,cmd_line_here
375                 mov cx,[AppendLen]
376                 rep movsb
377                 mov [CmdLinePtr],di
378                 pop di
379                 pop si
380                 pop es
382                 mov [KernelType], cl            ; CL == 0 here
385 ; Find the kernel on disk
387 get_kernel:     mov byte [KernelName+FILENAME_MAX],0    ; Zero-terminate filename/extension
388                 mov di,KernelName+4*IS_PXELINUX
389                 xor al,al
390                 mov cx,FILENAME_MAX-5           ; Need 4 chars + null
391                 repne scasb                     ; Scan for final null
392                 jne .no_skip
393                 dec di                          ; Point to final null
394 .no_skip:       mov [KernelExtPtr],di
395                 mov bx,exten_table
396 .search_loop:   push bx
397                 mov di,KernelName               ; Search on disk
398                 call searchdir
399                 pop bx
400                 jnz kernel_good
401                 mov eax,[bx]                    ; Try a different extension
402                 mov si,[KernelExtPtr]
403                 mov [si],eax
404                 mov byte [si+4],0
405                 add bx,byte 4
406                 cmp bx,exten_table_end
407                 jna .search_loop                ; allow == case (final case)
408                 ; Fall into bad_kernel
410 ; bad_kernel: Kernel image not found
411 ; bad_implicit: The user entered a nonvirtual kernel name, with "implicit 0"
413 bad_implicit:
414 bad_kernel:
415                 mov cx,[OnerrorLen]
416                 and cx,cx
417                 jnz on_error
418 .really:
419                 mov si,KernelName
420                 mov di,KernelCName
421                 push di
422                 call unmangle_name              ; Get human form
423                 mov si,err_notfound             ; Complain about missing kernel
424                 call cwritestr
425                 pop si                          ; KernelCName
426                 call cwritestr
427                 mov si,crlf_msg
428                 jmp abort_load                  ; Ask user for clue
431 ; on_error: bad kernel, but we have onerror set; CX = OnerrorLen
433 on_error:
434                 mov si,Onerror
435                 mov di,command_line
436                 push si                         ; <A>
437                 push di                         ; <B>
438                 push cx                         ; <C>
439                 push cx                         ; <D>
440                 push di                         ; <E>
441                 repe cmpsb
442                 pop di                          ; <E> di == command_line
443                 pop bx                          ; <D> bx == [OnerrorLen]
444                 je bad_kernel.really            ; Onerror matches command_line already
445                 neg bx                          ; bx == -[OnerrorLen]
446                 lea cx,[max_cmd_len+bx]
447                 ; CX == max_cmd_len-[OnerrorLen]
448                 mov di,command_line+max_cmd_len-1
449                 mov byte [di+1],0               ; Enforce null-termination
450                 lea si,[di+bx]
451                 std
452                 rep movsb                       ; Make space in command_line
453                 cld
454                 pop cx                          ; <C> cx == [OnerrorLen]
455                 pop di                          ; <B> di == command_line
456                 pop si                          ; <A> si  == Onerror
457                 rep movsb
458                 jmp load_kernel
461 ; kernel_corrupt: Called if the kernel file does not seem healthy
463 kernel_corrupt: mov si,err_notkernel
464                 jmp abort_load
467 ; Get a key, observing ThisKbdTO and ThisTotalTO -- those are timeouts
468 ; which can be adjusted by the caller based on the corresponding
469 ; master variables; on return they're updated.
471 ; This cheats.  If we say "no timeout" we actually get a timeout of
472 ; 7.5 years.
474 getchar_timeout:
475                 call vgashowcursor
476                 RESET_IDLE
478 .loop:
479                 push word [BIOS_timer]
480                 call pollchar
481                 jnz .got_char
482                 pop ax
483                 cmp ax,[BIOS_timer]             ; Has the timer advanced?
484                 je .loop
485                 DO_IDLE
487                 dec dword [ThisKbdTo]
488                 jz .timeout
489                 dec dword [ThisTotalTo]
490                 jnz .loop
492 .timeout:
493                 ; Timeout!!!!
494                 pop cx                          ; Discard return address
495                 call vgahidecursor
496                 mov si,Ontimeout                ; Copy ontimeout command
497                 mov di,command_line
498                 mov cx,[OntimeoutLen]           ; if we have one...
499                 rep movsb
500                 jmp command_done
502 .got_char:
503                 pop cx                          ; Discard
504                 call getchar
505                 call vgahidecursor
506                 ret
509 ; This is it!  We have a name (and location on the disk)... let's load
510 ; that sucker!!  First we have to decide what kind of file this is; base
511 ; that decision on the file extension.  The following extensions are
512 ; recognized; case insensitive:
514 ; .com  - COMBOOT image
515 ; .cbt  - COMBOOT image
516 ; .c32  - COM32 image
517 ; .bs   - Boot sector
518 ; .0    - PXE bootstrap program (PXELINUX only)
519 ; .bin  - Boot sector
520 ; .bss  - Boot sector, but transfer over DOS superblock (SYSLINUX only)
521 ; .img  - Floppy image (ISOLINUX only)
523 ; Anything else is assumed to be a Linux kernel.
525                 section .bss
526                 alignb 4
527 Kernel_EAX      resd 1
528 Kernel_SI       resw 1
530                 section .text
531 kernel_good_saved:
532                 ; Alternate entry point for which the return from
533                 ; searchdir is stored in memory.  This is used for
534                 ; COMBOOT function INT 22h, AX=0016h.
535                 mov si,[Kernel_SI]
536                 mov eax,[Kernel_EAX]
538 kernel_good:
539                 pushad
541                 mov si,KernelName
542                 mov di,KernelCName
543                 call unmangle_name
544                 sub di,KernelCName
545                 mov [KernelCNameLen],di
547                 ; Default memory limit, can be overridden by image loaders
548                 mov eax,[HighMemRsvd]
549                 mov [MyHighMemSize],eax
551                 popad
553                 push di
554                 push ax
555                 mov di,KernelName+4*IS_PXELINUX
556                 xor al,al
557                 mov cx,FILENAME_MAX
558                 repne scasb
559                 jne .one_step
560                 dec di
561 .one_step:      mov ecx,[di-4]                  ; 4 bytes before end
562                 pop ax
563                 pop di
566 ; At this point, EAX contains the size of the kernel, SI contains
567 ; the file handle/cluster pointer, and ECX contains the extension (if any.)
569                 movzx di,byte [KernelType]
570                 add di,di
571                 jmp [kerneltype_table+di]
573 is_unknown_filetype:
574                 or ecx,20202000h                ; Force lower case (except dot)
576                 cmp ecx,'.com'
577                 je is_comboot_image
578                 cmp ecx,'.cbt'
579                 je is_comboot_image
580                 cmp ecx,'.c32'
581                 je is_com32_image
582 %if IS_ISOLINUX
583                 cmp ecx,'.img'
584                 je is_disk_image
585 %endif
586                 cmp ecx,'.bss'
587                 je is_bss_sector
588                 cmp ecx,'.bin'
589                 je is_bootsector
590                 shr ecx,8
591                 cmp ecx,'.bs'
592                 je is_bootsector
593                 shr ecx,8
594                 cmp cx,'.0'
595                 je is_bootsector
597                 ; Otherwise Linux kernel
598                 jmp is_linux_kernel
600 is_config_file:
601                 pusha
602                 mov si,KernelCName              ; Save the config file name, for posterity
603                 mov di,ConfigName
604                 call strcpy
605                 popa
606                 call openfd
607                 call reset_config
608                 jmp load_config_file
610 ; This is an image type we can't deal with
611 is_bad_image:
612                 mov si,err_badimage
613                 call cwritestr
614                 jmp enter_command
616 %if IS_SYSLINUX || IS_MDSLINUX
617                 ; ok
618 %else
619 is_bss_sector   equ is_bad_image
620 %endif
621 %if IS_ISOLINUX
622                 ; ok
623 %else
624 is_disk_image   equ is_bad_image
625 %endif
627                 section .data
628 boot_prompt     db 'boot: ', 0
629 wipe_char       db BS, ' ', BS, 0
630 err_badimage    db 'Invalid image type for this media type!', CR, LF, 0
631 err_notfound    db 'Could not find kernel image: ',0
632 err_notkernel   db CR, LF, 'Invalid or corrupt kernel image.', CR, LF, 0
635                 align 2, db 0
636 kerneltype_table:
637                 dw is_unknown_filetype  ; VK_KERNEL
638                 dw is_linux_kernel      ; VK_LINUX
639                 dw is_bootsector        ; VK_BOOT
640                 dw is_bss_sector        ; VK_BSS
641                 dw is_bootsector        ; VK_PXE
642                 dw is_disk_image        ; VK_FDIMAGE
643                 dw is_comboot_image     ; VK_COMBOOT
644                 dw is_com32_image       ; VK_COM32
645                 dw is_config_file       ; VK_CONFIG
647                 section .bss
648                 alignb 4
649 ThisKbdTo       resd 1                  ; Temporary holder for KbdTimeout
650 ThisTotalTo     resd 1                  ; Temporary holder for TotalTimeout
651 KernelExtPtr    resw 1                  ; During search, final null pointer
652 CmdOptPtr       resw 1                  ; Pointer to first option on cmd line
653 KbdFlags        resb 1                  ; Check for keyboard escapes
654 FuncFlag        resb 1                  ; Escape sequences received from keyboard
655 KernelType      resb 1                  ; Kernel type, from vkernel, if known
657                 section .text
659 ; Linux kernel loading code is common.
661 %include "runkernel.inc"
664 ; COMBOOT-loading code
666 %include "comboot.inc"
667 %include "com32.inc"
668 %include "cmdline.inc"
671 ; Boot sector loading code
673 %include "bootsect.inc"
676 ; Abort loading code
678 %include "abort.inc"
681 ; Hardware cleanup common code
683 %include "cleanup.inc"