pxeldr.S revision 237761
1128708Sru/* 2128708Sru * Copyright (c) 2000 John Baldwin 3128708Sru * All rights reserved. 4128708Sru * 5128708Sru * Redistribution and use in source and binary forms are freely 6128708Sru * permitted provided that the above copyright notice and this 7128708Sru * paragraph and the following disclaimer are duplicated in all 8128708Sru * such forms. 9128708Sru * 10128708Sru * This software is provided "AS IS" and without any express or 11128708Sru * implied warranties, including, without limitation, the implied 12128708Sru * warranties of merchantability and fitness for a particular 13128708Sru * purpose. 14128708Sru * 15128708Sru * $FreeBSD: stable/9/sys/boot/i386/pxeldr/pxeldr.S 237761 2012-06-29 10:10:16Z avg $ 16128708Sru */ 1758713Sjhb 18128708Sru/* 19128708Sru * This simple program is a preloader for the normal boot3 loader. It is simply 20128708Sru * prepended to the beginning of a fully built and btxld'd loader. It then 21128708Sru * copies the loader to the address boot2 normally loads it, emulates the 22129240Sru * boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps 23128708Sru * to the start of btxldr to start the boot process. This method allows a stock 24128708Sru * /boot/loader to be booted over the network via PXE w/o having to write a 25128708Sru * separate PXE-aware client just to load the loader. 26128708Sru */ 2758713Sjhb 28162046Sru#include <sys/reboot.h> 29237761Savg#include <bootargs.h> 30162046Sru 31128708Sru/* 32128708Sru * Memory locations. 33128708Sru */ 34129240Sru .set MEM_PAGE_SIZE,0x1000 # memory page size, 4k 35129240Sru .set MEM_ARG,0x900 # Arguments at start 36129240Sru .set MEM_ARG_BTX,0xa100 # Where we move them to so the 37129240Sru # BTX client can see them 38129240Sru .set MEM_ARG_SIZE,0x18 # Size of the arguments 39129240Sru .set MEM_BTX_ADDRESS,0x9000 # where BTX lives 40129240Sru .set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute 41129240Sru .set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader 42129240Sru .set MEM_BTX_CLIENT,0xa000 # where BTX clients live 43129240Sru .set MEM_BIOS_KEYBOARD,0x496 # BDA byte with keyboard bit 44128708Sru/* 45128708Sru * a.out header fields 46128708Sru */ 47129240Sru .set AOUT_TEXT,0x04 # text segment size 48129240Sru .set AOUT_DATA,0x08 # data segment size 49129240Sru .set AOUT_BSS,0x0c # zero'd BSS size 50129240Sru .set AOUT_SYMBOLS,0x10 # symbol table 51129240Sru .set AOUT_ENTRY,0x14 # entry point 52129240Sru .set AOUT_HEADER,MEM_PAGE_SIZE # size of the a.out header 53128708Sru/* 54128708Sru * Segment selectors. 55128708Sru */ 56129240Sru .set SEL_SDATA,0x8 # Supervisor data 57129240Sru .set SEL_RDATA,0x10 # Real mode data 58129240Sru .set SEL_SCODE,0x18 # PM-32 code 59129240Sru .set SEL_SCODE16,0x20 # PM-16 code 60128708Sru/* 61128708Sru * BTX constants 62128708Sru */ 63129240Sru .set INT_SYS,0x30 # BTX syscall interrupt 64128708Sru/* 65128708Sru * Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present 66128708Sru */ 6769391Sps .set KEYBOARD_BIT,0x10 68128708Sru/* 69128708Sru * We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry 70128708Sru * point) 71128708Sru */ 7258713Sjhb .code16 7358713Sjhb .globl start 7458713Sjhb .org 0x0, 0x0 75128708Sru/* 76128708Sru * BTX program loader for PXE network booting 77128708Sru */ 78129240Srustart: cld # string ops inc 79129240Sru xorw %ax, %ax # zero %ax 80129240Sru movw %ax, %ss # setup the 81129240Sru movw $start, %sp # stack 82129240Sru movw %es, %cx # save PXENV+ segment 83129240Sru movw %ax, %ds # setup the 84129240Sru movw %ax, %es # data segments 85129240Sru andl $0xffff, %ecx # clear upper words 86129240Sru andl $0xffff, %ebx # of %ebx and %ecx 87129240Sru shll $4, %ecx # calculate the offset of 88129240Sru addl %ebx, %ecx # the PXENV+ struct and 89129240Sru pushl %ecx # save it on the stack 90129240Sru movw $welcome_msg, %si # %ds:(%si) -> welcome message 91129240Sru callw putstr # display the welcome message 92128708Sru/* 93128708Sru * Setup the arguments that the loader is expecting from boot[12] 94128708Sru */ 95129240Sru movw $bootinfo_msg, %si # %ds:(%si) -> boot args message 96129240Sru callw putstr # display the message 97129240Sru movw $MEM_ARG, %bx # %ds:(%bx) -> boot args 98129240Sru movw %bx, %di # %es:(%di) -> boot args 99129240Sru xorl %eax, %eax # zero %eax 100129240Sru movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit 101129240Sru # dwords 102129240Sru rep # Clear the arguments 103129240Sru stosl # to zero 104129240Sru orb $KARGS_FLAGS_PXE, 0x8(%bx) # kargs->bootflags |= 105129240Sru # KARGS_FLAGS_PXE 106129240Sru popl 0xc(%bx) # kargs->pxeinfo = *PXENV+ 107125693Sru#ifdef ALWAYS_SERIAL 108128708Sru/* 109128708Sru * set the RBX_SERIAL bit in the howto byte. 110128708Sru */ 111129240Sru orl $RB_SERIAL, (%bx) # enable serial console 112125693Sru#endif 113125693Sru#ifdef PROBE_KEYBOARD 114128708Sru/* 115128708Sru * Look at the BIOS data area to see if we have an enhanced keyboard. If not, 116178037Sjhb * set the RBX_DUAL and RBX_SERIAL bits in the howto byte. 117128708Sru */ 118129240Sru testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD # keyboard present? 119129240Sru jnz keyb # yes, so skip 120178037Sjhb orl $(RB_MULTIPLE | RB_SERIAL), (%bx) # enable serial console 12169391Spskeyb: 122125693Sru#endif 123128708Sru/* 124128708Sru * Turn on the A20 address line 125128708Sru */ 126129240Sru callw seta20 # Turn A20 on 127128708Sru/* 128128708Sru * Relocate the loader and BTX using a very lazy protected mode 129128708Sru */ 130129240Sru movw $relocate_msg, %si # Display the 131129240Sru callw putstr # relocation message 132129240Sru movl end+AOUT_ENTRY, %edi # %edi is the destination 133129240Sru movl $(end+AOUT_HEADER), %esi # %esi is 134129240Sru # the start of the text 135129240Sru # segment 136129240Sru movl end+AOUT_TEXT, %ecx # %ecx = length of the text 137129240Sru # segment 138129240Sru lgdt gdtdesc # setup our own gdt 139129240Sru cli # turn off interrupts 140129240Sru movl %cr0, %eax # Turn on 141129240Sru orb $0x1, %al # protected 142129240Sru movl %eax, %cr0 # mode 143129240Sru ljmp $SEL_SCODE,$pm_start # long jump to clear the 144129240Sru # instruction pre-fetch queue 14558713Sjhb .code32 146129240Srupm_start: movw $SEL_SDATA, %ax # Initialize 147129240Sru movw %ax, %ds # %ds and 148129240Sru movw %ax, %es # %es to a flat selector 149129240Sru rep # Relocate the 150129240Sru movsb # text segment 151129240Sru addl $(MEM_PAGE_SIZE - 1), %edi # pad %edi out to a new page 152129240Sru andl $~(MEM_PAGE_SIZE - 1), %edi # for the data segment 153129240Sru movl end+AOUT_DATA, %ecx # size of the data segment 154129240Sru rep # Relocate the 155129240Sru movsb # data segment 156129240Sru movl end+AOUT_BSS, %ecx # size of the bss 157129240Sru xorl %eax, %eax # zero %eax 158129240Sru addb $3, %cl # round %ecx up to 159129240Sru shrl $2, %ecx # a multiple of 4 160129240Sru rep # zero the 161129240Sru stosl # bss 162129240Sru movl end+AOUT_ENTRY, %esi # %esi -> relocated loader 163129240Sru addl $MEM_BTX_OFFSET, %esi # %esi -> BTX in the loader 164129240Sru movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go 165129240Sru movzwl 0xa(%esi), %ecx # %ecx -> length of BTX 166129240Sru rep # Relocate 167129240Sru movsb # BTX 168129240Sru ljmp $SEL_SCODE16,$pm_16 # Jump to 16-bit PM 16958713Sjhb .code16 170129240Srupm_16: movw $SEL_RDATA, %ax # Initialize 171129240Sru movw %ax, %ds # %ds and 172129240Sru movw %ax, %es # %es to a real mode selector 173129240Sru movl %cr0, %eax # Turn off 174129240Sru andb $~0x1, %al # protected 175129240Sru movl %eax, %cr0 # mode 176129240Sru ljmp $0,$pm_end # Long jump to clear the 177129240Sru # instruction pre-fetch queue 178129240Srupm_end: sti # Turn interrupts back on now 179128708Sru/* 180128708Sru * Copy the BTX client to MEM_BTX_CLIENT 181128708Sru */ 182129240Sru xorw %ax, %ax # zero %ax and set 183129240Sru movw %ax, %ds # %ds and %es 184129240Sru movw %ax, %es # to segment 0 185129240Sru movw $MEM_BTX_CLIENT, %di # Prepare to relocate 186129240Sru movw $btx_client, %si # the simple btx client 187129240Sru movw $(btx_client_end-btx_client), %cx # length of btx client 188129240Sru rep # Relocate the 189129240Sru movsb # simple BTX client 190128708Sru/* 191128708Sru * Copy the boot[12] args to where the BTX client can see them 192128708Sru */ 193129240Sru movw $MEM_ARG, %si # where the args are at now 194129240Sru movw $MEM_ARG_BTX, %di # where the args are moving to 195129240Sru movw $(MEM_ARG_SIZE/4), %cx # size of the arguments in longs 196129240Sru rep # Relocate 197129240Sru movsl # the words 198128708Sru/* 199128708Sru * Save the entry point so the client can get to it later on 200128708Sru */ 201129240Sru movl end+AOUT_ENTRY, %eax # load the entry point 202129240Sru stosl # add it to the end of the 203129240Sru # arguments 204128708Sru/* 205128708Sru * Now we just start up BTX and let it do the rest 206128708Sru */ 207129240Sru movw $jump_message, %si # Display the 208129240Sru callw putstr # jump message 209129240Sru ljmp $0,$MEM_BTX_ENTRY # Jump to the BTX entry point 21058713Sjhb 211128708Sru/* 212128708Sru * Display a null-terminated string 213128708Sru */ 214129240Sruputstr: lodsb # load %al from %ds:(%si) 215129240Sru testb %al,%al # stop at null 216129240Sru jnz putc # if the char != null, output it 217129240Sru retw # return when null is hit 218129240Sruputc: movw $0x7,%bx # attribute for output 219129240Sru movb $0xe,%ah # BIOS: put_char 220129240Sru int $0x10 # call BIOS, print char in %al 221129240Sru jmp putstr # keep looping 22258713Sjhb 223128708Sru/* 224157669Ssobomax * Enable A20. Put an upper limit on the amount of time we wait for the 225157669Ssobomax * keyboard controller to get ready (65K x ISA access time). If 226157669Ssobomax * we wait more than that amount, the hardware is probably 227157669Ssobomax * legacy-free and simply doesn't have a keyboard controller. 228157669Ssobomax * Thus, the A20 line is already enabled. 229128708Sru */ 230129240Sruseta20: cli # Disable interrupts 231157669Ssobomax xor %cx,%cx # Clear 232157669Ssobomaxseta20.1: inc %cx # Increment, overflow? 233157669Ssobomax jz seta20.3 # Yes 234157669Ssobomax inb $0x64,%al # Get status 235129240Sru testb $0x2,%al # Busy? 236129240Sru jnz seta20.1 # Yes 237129240Sru movb $0xd1,%al # Command: Write 238129240Sru outb %al,$0x64 # output port 239129240Sruseta20.2: inb $0x64,%al # Get status 240129240Sru testb $0x2,%al # Busy? 241129240Sru jnz seta20.2 # Yes 242129240Sru movb $0xdf,%al # Enable 243129240Sru outb %al,$0x60 # A20 244157669Ssobomaxseta20.3: sti # Enable interrupts 245129240Sru retw # To caller 24658713Sjhb 247128708Sru/* 248128708Sru * BTX client to start btxldr 249128708Sru */ 25058713Sjhb .code32 25158713Sjhbbtx_client: movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi 252129240Sru # %ds:(%esi) -> end 253129240Sru # of boot[12] args 254129240Sru movl $(MEM_ARG_SIZE/4), %ecx # Number of words to push 255129240Sru std # Go backwards 256129240Srupush_arg: lodsl # Read argument 257129240Sru pushl %eax # Push it onto the stack 258129240Sru loop push_arg # Push all of the arguments 259129240Sru cld # In case anyone depends on this 260129240Sru pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of 261129240Sru # the loader 262129240Sru pushl %eax # Emulate a near call 263129240Sru movl $0x1, %eax # 'exec' system call 264129240Sru int $INT_SYS # BTX system call 26558713Sjhbbtx_client_end: 26658713Sjhb .code16 26781363Sjhb 26858713Sjhb .p2align 4 269128708Sru/* 270128708Sru * Global descriptor table. 271128708Sru */ 272129240Srugdt: .word 0x0,0x0,0x0,0x0 # Null entry 273129240Sru .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA 274129240Sru .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA 275129240Sru .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE (32-bit) 276129240Sru .word 0xffff,0x0,0x9a00,0x8f # SEL_SCODE16 (16-bit) 27758713Sjhbgdt.1: 278128708Sru/* 279128708Sru * Pseudo-descriptors. 280128708Sru */ 281129240Srugdtdesc: .word gdt.1-gdt-1 # Limit 282129240Sru .long gdt # Base 28381363Sjhb 28458713Sjhbwelcome_msg: .asciz "PXE Loader 1.00\r\n\n" 28558713Sjhbbootinfo_msg: .asciz "Building the boot loader arguments\r\n" 28658713Sjhbrelocate_msg: .asciz "Relocating the loader and the BTX\r\n" 28758713Sjhbjump_message: .asciz "Starting the BTX loader\r\n" 28881363Sjhb 28981367Smp .p2align 4 29058713Sjhbend: 291