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