/*1* Copyright (c) 2000 John Baldwin2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions5* are met:6* 1. Redistributions of source code must retain the above copyright7* notice, this list of conditions and the following disclaimer.8* 2. Redistributions in binary form must reproduce the above copyright9* notice, this list of conditions and the following disclaimer in the10* documentation and/or other materials provided with the distribution.11*12* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND13* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE14* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE15* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE16* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL17* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS18* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)19* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT20* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY21* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF22* SUCH DAMAGE.23*/2425/*26* This simple program is a preloader for the normal boot3 loader. It is simply27* prepended to the beginning of a fully built and btxld'd loader. It then28* copies the loader to the address boot2 normally loads it, emulates the29* boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps30* to the start of btxldr to start the boot process. This method allows a stock31* /boot/loader to be booted over the network via PXE w/o having to write a32* separate PXE-aware client just to load the loader.33*/3435#include <sys/reboot.h>36#include <bootargs.h>3738/*39* Memory locations.40*/41.set MEM_PAGE_SIZE,0x1000 # memory page size, 4k42.set MEM_ARG,0x900 # Arguments at start43.set MEM_ARG_BTX,0xa100 # Where we move them to so the44# BTX client can see them45.set MEM_ARG_SIZE,0x18 # Size of the arguments46.set MEM_BTX_ADDRESS,0x9000 # where BTX lives47.set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute48.set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader49.set MEM_BTX_CLIENT,0xa000 # where BTX clients live50.set MEM_BIOS_KEYBOARD,0x496 # BDA byte with keyboard bit51/*52* a.out header fields53*/54.set AOUT_TEXT,0x04 # text segment size55.set AOUT_DATA,0x08 # data segment size56.set AOUT_BSS,0x0c # zero'd BSS size57.set AOUT_SYMBOLS,0x10 # symbol table58.set AOUT_ENTRY,0x14 # entry point59.set AOUT_HEADER,MEM_PAGE_SIZE # size of the a.out header60/*61* Segment selectors.62*/63.set SEL_SDATA,0x8 # Supervisor data64.set SEL_RDATA,0x10 # Real mode data65.set SEL_SCODE,0x18 # PM-32 code66.set SEL_SCODE16,0x20 # PM-16 code67/*68* BTX constants69*/70.set INT_SYS,0x30 # BTX syscall interrupt71/*72* Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present73*/74.set KEYBOARD_BIT,0x1075/*76* We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry77* point)78*/79.code1680.globl start81.org 0x0, 0x082/*83* BTX program loader for PXE network booting84*/85start: cld # string ops inc86xorw %ax, %ax # zero %ax87movw %ax, %ss # setup the88movw $start, %sp # stack89movw %es, %cx # save PXENV+ segment90movw %ax, %ds # setup the91movw %ax, %es # data segments92andl $0xffff, %ecx # clear upper words93andl $0xffff, %ebx # of %ebx and %ecx94shll $4, %ecx # calculate the offset of95addl %ebx, %ecx # the PXENV+ struct and96pushl %ecx # save it on the stack97movw $welcome_msg, %si # %ds:(%si) -> welcome message98callw putstr # display the welcome message99/*100* Setup the arguments that the loader is expecting from boot[12]101*/102movw $bootinfo_msg, %si # %ds:(%si) -> boot args message103callw putstr # display the message104movw $MEM_ARG, %bx # %ds:(%bx) -> boot args105movw %bx, %di # %es:(%di) -> boot args106xorl %eax, %eax # zero %eax107movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit108# dwords109rep # Clear the arguments110stosl # to zero111orb $KARGS_FLAGS_PXE, 0x8(%bx) # kargs->bootflags |=112# KARGS_FLAGS_PXE113popl 0xc(%bx) # kargs->pxeinfo = *PXENV+114#ifdef ALWAYS_SERIAL115/*116* set the RBX_SERIAL bit in the howto byte.117*/118orl $RB_SERIAL, (%bx) # enable serial console119#endif120#ifdef PROBE_KEYBOARD121/*122* Look at the BIOS data area to see if we have an enhanced keyboard. If not,123* set the RBX_DUAL and RBX_SERIAL bits in the howto byte.124*/125testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD # keyboard present?126jnz keyb # yes, so skip127orl $(RB_MULTIPLE | RB_SERIAL), (%bx) # enable serial console128keyb:129#endif130/*131* Turn on the A20 address line132*/133callw seta20 # Turn A20 on134/*135* Relocate the loader and BTX using a very lazy protected mode136*/137movw $relocate_msg, %si # Display the138callw putstr # relocation message139movl end+AOUT_ENTRY, %edi # %edi is the destination140movl $(end+AOUT_HEADER), %esi # %esi is141# the start of the text142# segment143movl end+AOUT_TEXT, %ecx # %ecx = length of the text144# segment145lgdt gdtdesc # setup our own gdt146cli # turn off interrupts147movl %cr0, %eax # Turn on148orb $0x1, %al # protected149movl %eax, %cr0 # mode150ljmp $SEL_SCODE,$pm_start # long jump to clear the151# instruction pre-fetch queue152.code32153pm_start: movw $SEL_SDATA, %ax # Initialize154movw %ax, %ds # %ds and155movw %ax, %es # %es to a flat selector156rep # Relocate the157movsb # text segment158addl $(MEM_PAGE_SIZE - 1), %edi # pad %edi out to a new page159andl $~(MEM_PAGE_SIZE - 1), %edi # for the data segment160movl end+AOUT_DATA, %ecx # size of the data segment161rep # Relocate the162movsb # data segment163movl end+AOUT_BSS, %ecx # size of the bss164xorl %eax, %eax # zero %eax165addb $3, %cl # round %ecx up to166shrl $2, %ecx # a multiple of 4167rep # zero the168stosl # bss169movl end+AOUT_ENTRY, %esi # %esi -> relocated loader170addl $MEM_BTX_OFFSET, %esi # %esi -> BTX in the loader171movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go172movzwl 0xa(%esi), %ecx # %ecx -> length of BTX173rep # Relocate174movsb # BTX175ljmp $SEL_SCODE16,$pm_16 # Jump to 16-bit PM176.code16177pm_16: movw $SEL_RDATA, %ax # Initialize178movw %ax, %ds # %ds and179movw %ax, %es # %es to a real mode selector180movl %cr0, %eax # Turn off181andb $~0x1, %al # protected182movl %eax, %cr0 # mode183ljmp $0,$pm_end # Long jump to clear the184# instruction pre-fetch queue185pm_end: sti # Turn interrupts back on now186/*187* Copy the BTX client to MEM_BTX_CLIENT188*/189xorw %ax, %ax # zero %ax and set190movw %ax, %ds # %ds and %es191movw %ax, %es # to segment 0192movw $MEM_BTX_CLIENT, %di # Prepare to relocate193movw $btx_client, %si # the simple btx client194movw $(btx_client_end-btx_client), %cx # length of btx client195rep # Relocate the196movsb # simple BTX client197/*198* Copy the boot[12] args to where the BTX client can see them199*/200movw $MEM_ARG, %si # where the args are at now201movw $MEM_ARG_BTX, %di # where the args are moving to202movw $(MEM_ARG_SIZE/4), %cx # size of the arguments in longs203rep # Relocate204movsl # the words205/*206* Save the entry point so the client can get to it later on207*/208movl end+AOUT_ENTRY, %eax # load the entry point209stosl # add it to the end of the210# arguments211/*212* Now we just start up BTX and let it do the rest213*/214movw $jump_message, %si # Display the215callw putstr # jump message216ljmp $0,$MEM_BTX_ENTRY # Jump to the BTX entry point217218/*219* Display a null-terminated string220*/221putstr: lodsb # load %al from %ds:(%si)222testb %al,%al # stop at null223jnz putc # if the char != null, output it224retw # return when null is hit225putc: movw $0x7,%bx # attribute for output226movb $0xe,%ah # BIOS: put_char227int $0x10 # call BIOS, print char in %al228jmp putstr # keep looping229230/*231* Enable A20. Put an upper limit on the amount of time we wait for the232* keyboard controller to get ready (65K x ISA access time). If233* we wait more than that amount, the hardware is probably234* legacy-free and simply doesn't have a keyboard controller.235* Thus, the A20 line is already enabled.236*/237seta20: cli # Disable interrupts238xor %cx,%cx # Clear239seta20.1: inc %cx # Increment, overflow?240jz seta20.3 # Yes241inb $0x64,%al # Get status242testb $0x2,%al # Busy?243jnz seta20.1 # Yes244movb $0xd1,%al # Command: Write245outb %al,$0x64 # output port246seta20.2: inb $0x64,%al # Get status247testb $0x2,%al # Busy?248jnz seta20.2 # Yes249movb $0xdf,%al # Enable250outb %al,$0x60 # A20251seta20.3: sti # Enable interrupts252retw # To caller253254/*255* BTX client to start btxldr256*/257.code32258btx_client: movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi259# %ds:(%esi) -> end260# of boot[12] args261movl $(MEM_ARG_SIZE/4), %ecx # Number of words to push262std # Go backwards263push_arg: lodsl # Read argument264pushl %eax # Push it onto the stack265loop push_arg # Push all of the arguments266cld # In case anyone depends on this267pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of268# the loader269pushl %eax # Emulate a near call270movl $0x1, %eax # 'exec' system call271int $INT_SYS # BTX system call272btx_client_end:273.code16274275.p2align 4276/*277* Global descriptor table.278*/279gdt: .word 0x0,0x0,0x0,0x0 # Null entry280.word 0xffff,0x0,0x9200,0xcf # SEL_SDATA281.word 0xffff,0x0,0x9200,0x0 # SEL_RDATA282.word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE (32-bit)283.word 0xffff,0x0,0x9a00,0x8f # SEL_SCODE16 (16-bit)284gdt.1:285/*286* Pseudo-descriptors.287*/288gdtdesc: .word gdt.1-gdt-1 # Limit289.long gdt # Base290291welcome_msg: .asciz "PXE Loader 1.00\r\n\n"292bootinfo_msg: .asciz "Building the boot loader arguments\r\n"293relocate_msg: .asciz "Relocating the loader and the BTX\r\n"294jump_message: .asciz "Starting the BTX loader\r\n"295296.p2align 4297end:298299300