rllpack: make all pointers 32 bits wide
[syslinux.git] / core / ui.inc
blobcb6e03bda47b18c64002d9a207aff223c7624062
1 ;; -----------------------------------------------------------------------
2 ;;
3 ;;   Copyright 1994-2009 H. Peter Anvin - All Rights Reserved
4 ;;   Copyright 2009 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.
12 ;; -----------------------------------------------------------------------
15 ; This file should be entered with the config file open (for getc)
17 load_config_file:
18                 call parse_config               ; Parse configuration file
19 no_config_file:
21                 call adv_init
23 ; Check for an ADV boot-once entry
25                 mov dl,ADV_BOOTONCE
26                 call adv_get
27                 jcxz .no_bootonce
29 .have_bootone:
30                 ; We apparently have a boot-once set; clear it and
31                 ; then execute the boot-once...
33                 ; Save the boot-once data; SI = data, CX = length
34                 mov di,command_line
35                 rep movsb
36                 xor ax,ax
37                 stosb
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
46 .no_bootonce:
49 ; Check whether or not we are supposed to display the boot prompt.
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
59 enter_command:
60                 cmp word [NoEscape],0           ; If NOESCAPE, no prompt,
61                 jne auto_boot                   ; always run default cmd
63                 mov si,boot_prompt
64                 call writestr
66                 mov byte [FuncFlag],0           ; <Ctrl-F> not pressed
67                 mov di,command_line
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.
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
81                 ; For the first character, both KbdTimeout and
82                 ; TotalTimeout apply; after that, only TotalTimeout.
84 get_char_time:
85                 mov eax,[TotalTimeout]
86                 mov [ThisTotalTo],eax
87                 mov eax,[KbdTimeout]
88                 mov [ThisKbdTo],eax
90 get_char:
91                 call getchar_timeout
92                 and dword [ThisKbdTo],0         ; For the next time...
94                 and al,al
95                 jz func_key
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
138 kill_command:
139                 call crlf
140                 jmp enter_command
142 force_text_mode:
143                 call vgaclearmode
144                 jmp enter_command
146 set_func_flag:
147                 mov byte [FuncFlag],1
148                 jmp short get_char_2
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
162                 push cx                         ; save command line size
164                 mov edi,VKernelBuf
165                 call rllunpack
166                 ; ESI updated on return
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
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
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
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
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 open
238                 jz short fk_nofile              ; File not found
239                 call crlf
240                 call get_msg_file
241                 jmp short fk_wrcmd
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
254                 ; ... fall through ...
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
272 ; Show network info (in the form of the ipappend strings)
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
292 ; Jump here to run the default command line
294 auto_boot:
295                 mov si,default_cmd
296                 mov di,command_line
297                 mov cx,(max_cmd_len+4) >> 2
298                 rep movsd
299                 jmp short load_kernel
302 ; Jump here when the command line is completed
304 command_done:
305                 call crlf
306                 cmp di,command_line             ; Did we just hit return?
307                 je auto_boot
308                 xor al,al                       ; Store a final null
309                 stosb
311 load_kernel:                                    ; Load the kernel now
313 ; First we need to mangle the kernel name the way DOS would...
315                 mov si,command_line
316                 mov di,KernelName
317                 push si
318                 call mangle_name
319                 pop si
321 ; Fast-forward to first option (we start over from the beginning, since
322 ; mangle_name doesn't necessarily return a consistent ending state.)
324 clin_non_wsp:   lodsb
325                 cmp al,' '
326                 ja clin_non_wsp
327 clin_is_wsp:    and al,al
328                 jz clin_opt_ptr
329                 lodsb
330                 cmp al,' '
331                 jbe clin_is_wsp
332 clin_opt_ptr:   dec si                          ; Point to first nonblank
333                 mov [CmdOptPtr],si              ; Save ptr to first option
335 ; If "allowoptions 0", put a null character here in order to ignore any
336 ; user-specified options.
338                 mov ax,[AllowOptions]
339                 and ax,ax
340                 jnz clin_opt_ok
341                 mov [si],al
342 clin_opt_ok:
345 ; Now check if it is a "virtual kernel"
347 vk_check:
348                 mov esi,[HighMemSize]           ; Start from top of memory
349 .scan:
350                 cmp esi,[VKernelEnd]
351                 jbe .not_vk
353                 mov edi,VKernelBuf
354                 call rllunpack
355                 ; ESI updated on return
357                 sub di,cx                       ; Return to beginning of buf
358                 push si
359                 mov si,command_line
360 .loop:
361                 lodsb
362                 cmp al,' '
363                 jbe .done
364                 scasb
365                 je .loop
366 .nomatch:
367                 pop si
368                 jmp .scan
369 .done:
370                 cmp byte [di],0                 ; Must match end of string
371                 jne .nomatch
372                 pop si
375 ; We *are* using a "virtual kernel"
377 .found:
378                 push es
379                 push word real_mode_seg
380                 pop es
381                 mov di,cmd_line_here
382                 mov si,VKernelBuf+vk_append
383                 mov cx,[VKernelBuf+vk_appendlen]
384                 rep movsb
385                 mov [CmdLinePtr],di             ; Where to add rest of cmd
386                 pop es
387                 mov di,KernelName
388                 push di
389                 mov si,VKernelBuf+vk_rname
390                 mov cx,FILENAME_MAX             ; We need ECX == CX later
391                 rep movsb
392                 pop di
393 %if IS_PXELINUX
394                 mov al,[VKernelBuf+vk_ipappend]
395                 mov [IPAppend],al
396 %endif
397                 xor bx,bx                       ; Try only one version
399                 mov al, [VKernelBuf+vk_type]
400                 mov [KernelType], al
402 %if HAS_LOCALBOOT
403                 ; Is this a "localboot" pseudo-kernel?
404 %if IS_PXELINUX
405                 cmp byte [VKernelBuf+vk_rname+4], 0
406 %else
407                 cmp byte [VKernelBuf+vk_rname], 0
408 %endif
409                 jne get_kernel          ; No, it's real, go get it
411                 mov ax, [VKernelBuf+vk_rname+1]
412                 jmp local_boot
413 %else
414                 jmp get_kernel
415 %endif
417 .not_vk:
420 ; Not a "virtual kernel" - check that's OK and construct the command line
422                 cmp word [AllowImplicit],byte 0
423                 je bad_implicit
424                 push es
425                 push si
426                 push di
427                 mov di,real_mode_seg
428                 mov es,di
429                 mov si,AppendBuf
430                 mov di,cmd_line_here
431                 mov cx,[AppendLen]
432                 rep movsb
433                 mov [CmdLinePtr],di
434                 pop di
435                 pop si
436                 pop es
438                 mov [KernelType], cl            ; CL == 0 here
441 ; Find the kernel on disk
443 get_kernel:     mov byte [KernelName+FILENAME_MAX],0    ; Zero-terminate filename/extension
444                 mov di,KernelName+4*IS_PXELINUX
445                 xor al,al
446                 mov cx,FILENAME_MAX-5           ; Need 4 chars + null
447                 repne scasb                     ; Scan for final null
448                 jne .no_skip
449                 dec di                          ; Point to final null
450 .no_skip:       mov [KernelExtPtr],di
451                 mov bx,exten_table
452 .search_loop:   push bx
453                 mov di,KernelName               ; Search on disk
454                 call searchdir
455                 pop bx
456                 jnz kernel_good
457                 mov eax,[bx]                    ; Try a different extension
458                 mov si,[KernelExtPtr]
459                 mov [si],eax
460                 mov byte [si+4],0
461                 add bx,byte 4
462                 cmp bx,exten_table_end
463                 jna .search_loop                ; allow == case (final case)
464                 ; Fall into bad_kernel
466 ; bad_kernel: Kernel image not found
467 ; bad_implicit: The user entered a nonvirtual kernel name, with "implicit 0"
469 bad_implicit:
470 bad_kernel:
471                 mov cx,[OnerrorLen]
472                 and cx,cx
473                 jnz on_error
474 .really:
475                 mov si,KernelName
476                 mov di,KernelCName
477                 push di
478                 call unmangle_name              ; Get human form
479                 mov si,err_notfound             ; Complain about missing kernel
480                 call writestr
481                 pop si                          ; KernelCName
482                 call writestr
483                 mov si,crlf_msg
484                 jmp abort_load                  ; Ask user for clue
487 ; on_error: bad kernel, but we have onerror set; CX = OnerrorLen
489 on_error:
490                 mov si,Onerror
491                 mov di,command_line
492                 push si                         ; <A>
493                 push di                         ; <B>
494                 push cx                         ; <C>
495                 push cx                         ; <D>
496                 push di                         ; <E>
497                 repe cmpsb
498                 pop di                          ; <E> di == command_line
499                 pop bx                          ; <D> bx == [OnerrorLen]
500                 je bad_kernel.really            ; Onerror matches command_line already
501                 neg bx                          ; bx == -[OnerrorLen]
502                 lea cx,[max_cmd_len+bx]
503                 ; CX == max_cmd_len-[OnerrorLen]
504                 mov di,command_line+max_cmd_len-1
505                 mov byte [di+1],0               ; Enforce null-termination
506                 lea si,[di+bx]
507                 std
508                 rep movsb                       ; Make space in command_line
509                 cld
510                 pop cx                          ; <C> cx == [OnerrorLen]
511                 pop di                          ; <B> di == command_line
512                 pop si                          ; <A> si  == Onerror
513                 rep movsb
514                 jmp load_kernel
517 ; kernel_corrupt: Called if the kernel file does not seem healthy
519 kernel_corrupt: mov si,err_notkernel
520                 jmp abort_load
523 ; Get a key, observing ThisKbdTO and ThisTotalTO -- those are timeouts
524 ; which can be adjusted by the caller based on the corresponding
525 ; master variables; on return they're updated.
527 ; This cheats.  If we say "no timeout" we actually get a timeout of
528 ; 7.5 years.
530 getchar_timeout:
531                 call vgashowcursor
532                 RESET_IDLE
534 .loop:
535                 push word [BIOS_timer]
536                 call pollchar
537                 jnz .got_char
538                 pop ax
539                 cmp ax,[BIOS_timer]             ; Has the timer advanced?
540                 je .loop
541                 DO_IDLE
543                 dec dword [ThisKbdTo]
544                 jz .timeout
545                 dec dword [ThisTotalTo]
546                 jnz .loop
548 .timeout:
549                 ; Timeout!!!!
550                 pop cx                          ; Discard return address
551                 call vgahidecursor
552                 mov si,Ontimeout                ; Copy ontimeout command
553                 mov di,command_line
554                 mov cx,[OntimeoutLen]           ; if we have one...
555                 rep movsb
556                 jmp command_done
558 .got_char:
559                 pop cx                          ; Discard
560                 call getchar
561                 call vgahidecursor
562                 ret
565 ; This is it!  We have a name (and location on the disk)... let's load
566 ; that sucker!!  First we have to decide what kind of file this is; base
567 ; that decision on the file extension.  The following extensions are
568 ; recognized; case insensitive:
570 ; .com  - COMBOOT image
571 ; .cbt  - COMBOOT image
572 ; .c32  - COM32 image
573 ; .bs   - Boot sector
574 ; .0    - PXE bootstrap program (PXELINUX only)
575 ; .bin  - Boot sector
576 ; .bss  - Boot sector, but transfer over DOS superblock (SYSLINUX only)
577 ; .img  - Floppy image (ISOLINUX only)
579 ; Anything else is assumed to be a Linux kernel.
581                 section .bss
582                 alignb 4
583 Kernel_EAX      resd 1
584 Kernel_SI       resw 1
586                 section .text
587 kernel_good_saved:
588                 ; Alternate entry point for which the return from
589                 ; searchdir is stored in memory.  This is used for
590                 ; COMBOOT function INT 22h, AX=0016h.
591                 mov si,[Kernel_SI]
592                 mov eax,[Kernel_EAX]
594 kernel_good:
595                 pushad
596                 ;
597                 ; Common initialization for all kernel types
598                 ;
599                 xor ax,ax
600                 mov [InitRDPtr],ax
601                 mov [QuietBoot],al
602 %if IS_PXELINUX
603                 mov [KeepPXE],al
604 %endif
606                 mov si,KernelName
607                 mov di,KernelCName
608                 call unmangle_name
609                 sub di,KernelCName
610                 mov [KernelCNameLen],di
612                 ; Default memory limit, can be overridden by image loaders
613                 mov eax,[HighMemRsvd]
614                 mov [MyHighMemSize],eax
616                 popad
618                 push di
619                 push ax
620                 mov di,KernelName+4*IS_PXELINUX
621                 xor al,al
622                 mov cx,FILENAME_MAX
623                 repne scasb
624                 jne .one_step
625                 dec di
626 .one_step:      mov ecx,[di-4]                  ; 4 bytes before end
627                 pop ax
628                 pop di
631 ; At this point, EAX contains the size of the kernel, SI contains
632 ; the file handle/cluster pointer, and ECX contains the extension (if any.)
634                 movzx di,byte [KernelType]
635                 add di,di
636                 jmp [kerneltype_table+di]
638 is_unknown_filetype:
639                 or ecx,20202000h                ; Force lower case (except dot)
641                 cmp ecx,'.com'
642                 je is_comboot_image
643                 cmp ecx,'.cbt'
644                 je is_comboot_image
645                 cmp ecx,'.c32'
646                 je is_com32_image
647 %if IS_ISOLINUX
648                 cmp ecx,'.img'
649                 je is_disk_image
650 %endif
651                 cmp ecx,'.bss'
652                 je is_bss_sector
653                 cmp ecx,'.bin'
654                 je is_bootsector
655                 shr ecx,8
656                 cmp ecx,'.bs'
657                 je is_bootsector
658                 shr ecx,8
659                 cmp cx,'.0'
660                 je is_bootsector
662                 ; Otherwise Linux kernel
663                 jmp is_linux_kernel
665 is_config_file:
666                 pusha
667                 mov si,KernelCName              ; Save the config file name, for posterity
668                 mov di,ConfigName
669                 call strcpy
670                 popa
671                 call openfd
672                 call reset_config
673                 jmp load_config_file
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
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
692                 section .data
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
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
712                 section .bss
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
722                 section .text
724 ; Linux kernel loading code is common.
726 %include "runkernel.inc"
729 ; COMBOOT-loading code
731 %include "comboot.inc"
732 %include "com32.inc"
733 %include "cmdline.inc"
736 ; Boot sector loading code
738 %include "bootsect.inc"
741 ; Abort loading code
743 %include "abort.inc"
746 ; Hardware cleanup common code
748 %include "cleanup.inc"