Deleted Added
sdiff udiff text old ( 128716 ) new ( 129240 )
full compact
1/*
2 * Copyright (c) 2000 John Baldwin
3 * All rights reserved.
4 *
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
8 * such forms.
9 *
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
13 * purpose.
14 *
15 * $FreeBSD: head/sys/boot/i386/pxeldr/pxeldr.S 128716 2004-04-28 14:31:44Z ru $
16 */
17
18/*
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.
26 */
27
28/*
29 * Memory locations.
30 */
31 .set MEM_PAGE_SIZE,0x1000 // memory page size, 4k
32 .set MEM_ARG,0x900 // Arguments at start
33 .set MEM_ARG_BTX,0xa100 // Where we move them to so the
34 // BTX client can see them
35 .set MEM_ARG_SIZE,0x18 // Size of the arguments
36 .set MEM_BTX_ADDRESS,0x9000 // where BTX lives
37 .set MEM_BTX_ENTRY,0x9010 // where BTX starts to execute
38 .set MEM_BTX_OFFSET,MEM_PAGE_SIZE // offset of BTX in the loader
39 .set MEM_BTX_CLIENT,0xa000 // where BTX clients live
40 .set MEM_BIOS_KEYBOARD,0x496 // BDA byte with keyboard bit
41/*
42 * a.out header fields
43 */
44 .set AOUT_TEXT,0x04 // text segment size
45 .set AOUT_DATA,0x08 // data segment size
46 .set AOUT_BSS,0x0c // zero'd BSS size
47 .set AOUT_SYMBOLS,0x10 // symbol table
48 .set AOUT_ENTRY,0x14 // entry point
49 .set AOUT_HEADER,MEM_PAGE_SIZE // size of the a.out header
50/*
51 * Flags for kargs->bootflags
52 */
53 .set KARGS_FLAGS_PXE,0x2 // flag to indicate booting from
54 // PXE loader
55/*
56 * Boot howto bits
57 */
58 .set RB_SERIAL,0x1000 // serial console
59/*
60 * Segment selectors.
61 */
62 .set SEL_SDATA,0x8 // Supervisor data
63 .set SEL_RDATA,0x10 // Real mode data
64 .set SEL_SCODE,0x18 // PM-32 code
65 .set SEL_SCODE16,0x20 // PM-16 code
66/*
67 * BTX constants
68 */
69 .set INT_SYS,0x30 // BTX syscall interrupt
70/*
71 * Bit in MEM_BIOS_KEYBOARD that is set if an enhanced keyboard is present
72 */
73 .set KEYBOARD_BIT,0x10
74/*
75 * We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry
76 * point)
77 */
78 .code16
79 .globl start
80 .org 0x0, 0x0
81/*
82 * BTX program loader for PXE network booting
83 */
84start: cld // string ops inc
85 xorw %ax, %ax // zero %ax
86 movw %ax, %ss // setup the
87 movw $start, %sp // stack
88 movw %es, %cx // save PXENV+ segment
89 movw %ax, %ds // setup the
90 movw %ax, %es // data segments
91 andl $0xffff, %ecx // clear upper words
92 andl $0xffff, %ebx // of %ebx and %ecx
93 shll $4, %ecx // calculate the offset of
94 addl %ebx, %ecx // the PXENV+ struct and
95 pushl %ecx // save it on the stack
96 movw $welcome_msg, %si // %ds:(%si) -> welcome message
97 callw putstr // display the welcome message
98/*
99 * Setup the arguments that the loader is expecting from boot[12]
100 */
101 movw $bootinfo_msg, %si // %ds:(%si) -> boot args
102 // message
103 callw putstr // display the message
104 movw $MEM_ARG, %bx // %ds:(%bx) -> boot args
105 movw %bx, %di // %es:(%di) -> boot args
106 xorl %eax, %eax // zero %eax
107 movw $(MEM_ARG_SIZE/4), %cx // Size of arguments in 32-bit
108 // dwords
109 rep // Clear the arguments
110 stosl // to zero
111 orb $KARGS_FLAGS_PXE, 0x8(%bx) // kargs->bootflags |=
112 // KARGS_FLAGS_PXE
113 popl 0xc(%bx) // kargs->pxeinfo = *PXENV+
114#ifdef ALWAYS_SERIAL
115/*
116 * set the RBX_SERIAL bit in the howto byte.
117 */
118 orl $RB_SERIAL, (%bx) // enable serial console
119#endif
120#ifdef PROBE_KEYBOARD
121/*
122 * Look at the BIOS data area to see if we have an enhanced keyboard. If not,
123 * set the RBX_SERIAL bit in the howto byte.
124 */
125 testb $KEYBOARD_BIT, MEM_BIOS_KEYBOARD // keyboard present?
126 jnz keyb // yes, so skip
127 orl $RB_SERIAL, (%bx) // enable serial console
128keyb:
129#endif
130/*
131 * Turn on the A20 address line
132 */
133 callw seta20 // Turn A20 on
134/*
135 * Relocate the loader and BTX using a very lazy protected mode
136 */
137 movw $relocate_msg, %si // Display the
138 callw putstr // relocation message
139 movl end+AOUT_ENTRY, %edi // %edi is the destination
140 movl $(end+AOUT_HEADER), %esi // %esi is
141 // the start of the text
142 // segment
143 movl end+AOUT_TEXT, %ecx // %ecx = length of the text
144 // segment
145 lgdt gdtdesc // setup our own gdt
146 cli // turn off interrupts
147 movl %cr0, %eax // Turn on
148 orb $0x1, %al // protected
149 movl %eax, %cr0 // mode
150 ljmp $SEL_SCODE,$pm_start // long jump to clear the
151 // instruction pre-fetch queue
152 .code32
153pm_start: movw $SEL_SDATA, %ax // Initialize
154 movw %ax, %ds // %ds and
155 movw %ax, %es // %es to a flat selector
156 rep // Relocate the
157 movsb // text segment
158 addl $(MEM_PAGE_SIZE - 1), %edi // pad %edi out to a new page
159 andl $~(MEM_PAGE_SIZE - 1), %edi // for the data segment
160 movl end+AOUT_DATA, %ecx // size of the data segment
161 rep // Relocate the
162 movsb // data segment
163 movl end+AOUT_BSS, %ecx // size of the bss
164 xorl %eax, %eax // zero %eax
165 addb $3, %cl // round %ecx up to
166 shrl $2, %ecx // a multiple of 4
167 rep // zero the
168 stosl // bss
169 movl end+AOUT_ENTRY, %esi // %esi -> relocated loader
170 addl $MEM_BTX_OFFSET, %esi // %esi -> BTX in the loader
171 movl $MEM_BTX_ADDRESS, %edi // %edi -> where BTX needs to go
172 movzwl 0xa(%esi), %ecx // %ecx -> length of BTX
173 rep // Relocate
174 movsb // BTX
175 ljmp $SEL_SCODE16,$pm_16 // Jump to 16-bit PM
176 .code16
177pm_16: movw $SEL_RDATA, %ax // Initialize
178 movw %ax, %ds // %ds and
179 movw %ax, %es // %es to a real mode selector
180 movl %cr0, %eax // Turn off
181 andb $~0x1, %al // protected
182 movl %eax, %cr0 // mode
183 ljmp $0,$pm_end // Long jump to clear the
184 // instruction pre-fetch queue
185pm_end: sti // Turn interrupts back on now
186/*
187 * Copy the BTX client to MEM_BTX_CLIENT
188 */
189 xorw %ax, %ax // zero %ax and set
190 movw %ax, %ds // %ds and %es
191 movw %ax, %es // to segment 0
192 movw $MEM_BTX_CLIENT, %di // Prepare to relocate
193 movw $btx_client, %si // the simple btx client
194 movw $(btx_client_end-btx_client), %cx // length of btx client
195 rep // Relocate the
196 movsb // simple BTX client
197/*
198 * Copy the boot[12] args to where the BTX client can see them
199 */
200 movw $MEM_ARG, %si // where the args are at now
201 movw $MEM_ARG_BTX, %di // where the args are moving to
202 movw $(MEM_ARG_SIZE/4), %cx // size of the arguments in
203 // longs
204 rep // Relocate
205 movsl // the words
206/*
207 * Save the entry point so the client can get to it later on
208 */
209 movl end+AOUT_ENTRY, %eax // load the entry point
210 stosl // add it to the end of the
211 // arguments
212/*
213 * Now we just start up BTX and let it do the rest
214 */
215 movw $jump_message, %si // Display the
216 callw putstr // jump message
217 ljmp $0,$MEM_BTX_ENTRY // Jump to the BTX entry point
218
219/*
220 * Display a null-terminated string
221 */
222putstr: lodsb // load %al from %ds:(%si)
223 testb %al,%al // stop at null
224 jnz putc // if the char != null,
225 // output it
226 retw // return when null is hit
227putc: movw $0x7,%bx // attribute for output
228 movb $0xe,%ah // BIOS: put_char
229 int $0x10 // call BIOS, print char in %al
230 jmp putstr // keep looping
231
232/*
233 * Enable A20
234 */
235seta20: cli // Disable interrupts
236seta20.1: inb $0x64,%al // Get status
237 testb $0x2,%al // Busy?
238 jnz seta20.1 // Yes
239 movb $0xd1,%al // Command: Write
240 outb %al,$0x64 // output port
241seta20.2: inb $0x64,%al // Get status
242 testb $0x2,%al // Busy?
243 jnz seta20.2 // Yes
244 movb $0xdf,%al // Enable
245 outb %al,$0x60 // A20
246 sti // Enable interrupts
247 retw // To caller
248
249/*
250 * BTX client to start btxldr
251 */
252 .code32
253btx_client: movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi
254 // %ds:(%esi) -> end
255 // of boot[12] args
256 movl $(MEM_ARG_SIZE/4), %ecx // Number of words to push
257 std // Go backwards
258push_arg: lodsl // Read argument
259 pushl %eax // Push it onto the stack
260 loop push_arg // Push all of the arguments
261 cld // In case anyone depends
262 // on this
263 pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE // Entry point of
264 // the loader
265 pushl %eax // Emulate a near call
266 movl $0x1, %eax // 'exec' system call
267 int $INT_SYS // BTX system call
268btx_client_end:
269 .code16
270
271 .p2align 4
272/*
273 * Global descriptor table.
274 */
275gdt: .word 0x0,0x0,0x0,0x0 // Null entry
276 .word 0xffff,0x0,0x9200,0xcf // SEL_SDATA
277 .word 0xffff,0x0,0x9200,0x0 // SEL_RDATA
278 .word 0xffff,0x0,0x9a00,0xcf // SEL_SCODE (32-bit)
279 .word 0xffff,0x0,0x9a00,0x8f // SEL_SCODE16 (16-bit)
280gdt.1:
281/*
282 * Pseudo-descriptors.
283 */
284gdtdesc: .word gdt.1-gdt-1 // Limit
285 .long gdt // Base
286
287welcome_msg: .asciz "PXE Loader 1.00\r\n\n"
288bootinfo_msg: .asciz "Building the boot loader arguments\r\n"
289relocate_msg: .asciz "Relocating the loader and the BTX\r\n"
290jump_message: .asciz "Starting the BTX loader\r\n"
291
292 .p2align 4
293end: