2 * Copyright (c) 2000 John Baldwin
5 * Redistribution and use in source and binary forms are freely
6 * permitted provided that the above copyright notice and this
7 * paragraph and the following disclaimer are duplicated in all
10 * This software is provided "AS IS" and without any express or
11 * implied warranties, including, without limitation, the implied
12 * warranties of merchantability and fitness for a particular
19 * This simple program is a preloader for the normal boot3 loader. It is simply
20 * prepended to the beginning of a fully built and btxld'd loader. It then
21 * copies the loader to the address boot2 normally loads it, emulates the
22 * boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps
23 * to the start of btxldr to start the boot process. This method allows a stock
24 * /boot/loader to be booted over the network via PXE w/o having to write a
25 * separate PXE-aware client just to load the loader.
28 #include <sys/reboot.h>
33 .set MEM_PAGE_SIZE,0x1000 # memory page size, 4k
34 .set MEM_ARG,0x900 # Arguments at start
35 .set MEM_ARG_BTX,0xa100 # Where we move them to so the
36 # BTX client can see them
37 .set MEM_ARG_SIZE,0x18 # Size of the arguments
38 .set MEM_BTX_ADDRESS,0x9000 # where BTX lives
39 .set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute
40 .set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader
41 .set MEM_BTX_CLIENT,0xa000 # where BTX clients live
42 .set MEM_BIOS_KEYBOARD,0x496 # BDA byte with keyboard bit
46 .set AOUT_TEXT,0x04 # text segment size
47 .set AOUT_DATA,0x08 # data segment size
48 .set AOUT_BSS,0x0c # zero'd BSS size
49 .set AOUT_SYMBOLS,0x10 # symbol table
50 .set AOUT_ENTRY,0x14 # entry point
51 .set AOUT_HEADER,MEM_PAGE_SIZE # size of the a.out header
53 * Flags for kargs->bootflags
55 .set KARGS_FLAGS_PXE,0x2 # flag to indicate booting from
60 .set SEL_SDATA,0x8 # Supervisor data
61 .set SEL_RDATA,0x10 # Real mode data
62 .set SEL_SCODE,0x18 # PM-32 code
63 .set SEL_SCODE16,0x20 # PM-16 code
67 .set INT_SYS,0x30 # BTX syscall interrupt
69 * Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present
71 .set KEYBOARD_BIT,0x10
73 * We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry
80 * BTX program loader for PXE network booting
82 start: cld # string ops inc
83 xorw %ax, %ax # zero %ax
84 movw %ax, %ss # setup the
85 movw $start, %sp # stack
86 movw %es, %cx # save PXENV+ segment
87 movw %ax, %ds # setup the
88 movw %ax, %es # data segments
89 andl $0xffff, %ecx # clear upper words
90 andl $0xffff, %ebx # of %ebx and %ecx
91 shll $4, %ecx # calculate the offset of
92 addl %ebx, %ecx # the PXENV+ struct and
93 pushl %ecx # save it on the stack
94 movw $welcome_msg, %si # %ds:(%si) -> welcome message
95 callw putstr # display the welcome message
97 * Setup the arguments that the loader is expecting from boot[12]
99 movw $bootinfo_msg, %si # %ds:(%si) -> boot args message
100 callw putstr # display the message
101 movw $MEM_ARG, %bx # %ds:(%bx) -> boot args
102 movw %bx, %di # %es:(%di) -> boot args
103 xorl %eax, %eax # zero %eax
104 movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit
106 rep # Clear the arguments
108 orb $KARGS_FLAGS_PXE, 0x8(%bx) # kargs->bootflags |=
110 popl 0xc(%bx) # kargs->pxeinfo = *PXENV+
113 * set the RBX_SERIAL bit in the howto byte.
115 orl $RB_SERIAL, (%bx) # enable serial console
117 #ifdef PROBE_KEYBOARD
119 * Look at the BIOS data area to see if we have an enhanced keyboard. If not,
120 * set the RBX_DUAL and RBX_SERIAL bits in the howto byte.
122 testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD # keyboard present?
123 jnz keyb # yes, so skip
124 orl $(RB_MULTIPLE | RB_SERIAL), (%bx) # enable serial console
128 * Turn on the A20 address line
130 callw seta20 # Turn A20 on
132 * Relocate the loader and BTX using a very lazy protected mode
134 movw $relocate_msg, %si # Display the
135 callw putstr # relocation message
136 movl end+AOUT_ENTRY, %edi # %edi is the destination
137 movl $(end+AOUT_HEADER), %esi # %esi is
138 # the start of the text
140 movl end+AOUT_TEXT, %ecx # %ecx = length of the text
142 lgdt gdtdesc # setup our own gdt
143 cli # turn off interrupts
144 movl %cr0, %eax # Turn on
145 orb $0x1, %al # protected
146 movl %eax, %cr0 # mode
147 ljmp $SEL_SCODE,$pm_start # long jump to clear the
148 # instruction pre-fetch queue
150 pm_start: movw $SEL_SDATA, %ax # Initialize
151 movw %ax, %ds # %ds and
152 movw %ax, %es # %es to a flat selector
155 addl $(MEM_PAGE_SIZE - 1), %edi # pad %edi out to a new page
156 andl $~(MEM_PAGE_SIZE - 1), %edi # for the data segment
157 movl end+AOUT_DATA, %ecx # size of the data segment
160 movl end+AOUT_BSS, %ecx # size of the bss
161 xorl %eax, %eax # zero %eax
162 addb $3, %cl # round %ecx up to
163 shrl $2, %ecx # a multiple of 4
166 movl end+AOUT_ENTRY, %esi # %esi -> relocated loader
167 addl $MEM_BTX_OFFSET, %esi # %esi -> BTX in the loader
168 movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go
169 movzwl 0xa(%esi), %ecx # %ecx -> length of BTX
172 ljmp $SEL_SCODE16,$pm_16 # Jump to 16-bit PM
174 pm_16: movw $SEL_RDATA, %ax # Initialize
175 movw %ax, %ds # %ds and
176 movw %ax, %es # %es to a real mode selector
177 movl %cr0, %eax # Turn off
178 andb $~0x1, %al # protected
179 movl %eax, %cr0 # mode
180 ljmp $0,$pm_end # Long jump to clear the
181 # instruction pre-fetch queue
182 pm_end: sti # Turn interrupts back on now
184 * Copy the BTX client to MEM_BTX_CLIENT
186 xorw %ax, %ax # zero %ax and set
187 movw %ax, %ds # %ds and %es
188 movw %ax, %es # to segment 0
189 movw $MEM_BTX_CLIENT, %di # Prepare to relocate
190 movw $btx_client, %si # the simple btx client
191 movw $(btx_client_end-btx_client), %cx # length of btx client
193 movsb # simple BTX client
195 * Copy the boot[12] args to where the BTX client can see them
197 movw $MEM_ARG, %si # where the args are at now
198 movw $MEM_ARG_BTX, %di # where the args are moving to
199 movw $(MEM_ARG_SIZE/4), %cx # size of the arguments in longs
203 * Save the entry point so the client can get to it later on
205 movl end+AOUT_ENTRY, %eax # load the entry point
206 stosl # add it to the end of the
209 * Now we just start up BTX and let it do the rest
211 movw $jump_message, %si # Display the
212 callw putstr # jump message
213 ljmp $0,$MEM_BTX_ENTRY # Jump to the BTX entry point
216 * Display a null-terminated string
218 putstr: lodsb # load %al from %ds:(%si)
219 testb %al,%al # stop at null
220 jnz putc # if the char != null, output it
221 retw # return when null is hit
222 putc: movw $0x7,%bx # attribute for output
223 movb $0xe,%ah # BIOS: put_char
224 int $0x10 # call BIOS, print char in %al
225 jmp putstr # keep looping
228 * Enable A20. Put an upper limit on the amount of time we wait for the
229 * keyboard controller to get ready (65K x ISA access time). If
230 * we wait more than that amount, the hardware is probably
231 * legacy-free and simply doesn't have a keyboard controller.
232 * Thus, the A20 line is already enabled.
234 seta20: cli # Disable interrupts
236 seta20.1: inc %cx # Increment, overflow?
238 inb $0x64,%al # Get status
239 testb $0x2,%al # Busy?
241 movb $0xd1,%al # Command: Write
242 outb %al,$0x64 # output port
243 seta20.2: inb $0x64,%al # Get status
244 testb $0x2,%al # Busy?
246 movb $0xdf,%al # Enable
248 seta20.3: sti # Enable interrupts
252 * BTX client to start btxldr
255 btx_client: movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi
258 movl $(MEM_ARG_SIZE/4), %ecx # Number of words to push
260 push_arg: lodsl # Read argument
261 pushl %eax # Push it onto the stack
262 loop push_arg # Push all of the arguments
263 cld # In case anyone depends on this
264 pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of
266 pushl %eax # Emulate a near call
267 movl $0x1, %eax # 'exec' system call
268 int $INT_SYS # BTX system call
274 * Global descriptor table.
276 gdt: .word 0x0,0x0,0x0,0x0 # Null entry
277 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA
278 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA
279 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE (32-bit)
280 .word 0xffff,0x0,0x9a00,0x8f # SEL_SCODE16 (16-bit)
283 * Pseudo-descriptors.
285 gdtdesc: .word gdt.1-gdt-1 # Limit
288 welcome_msg: .asciz "PXE Loader 1.00\r\n\n"
289 bootinfo_msg: .asciz "Building the boot loader arguments\r\n"
290 relocate_msg: .asciz "Relocating the loader and the BTX\r\n"
291 jump_message: .asciz "Starting the BTX loader\r\n"