2 * Copyright (c) 1998 Robert Nordier
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 * Prototype BTX loader program, written in a couple of hours. The
20 * real thing should probably be more flexible, and in C.
26 .set MEM_STUB,0x600 # Real mode stub
27 .set MEM_ESP,0x1000 # New stack pointer
28 .set MEM_TBL,0x5000 # BTX page tables
29 .set MEM_ENTRY,0x9010 # BTX entry point
30 .set MEM_DATA,start+0x1000 # Data segment
34 .set SEL_SCODE,0x8 # 4GB code
35 .set SEL_SDATA,0x10 # 4GB data
36 .set SEL_RCODE,0x18 # 64K code
37 .set SEL_RDATA,0x20 # 64K data
41 .set PAG_SIZ,0x1000 # Page size
42 .set PAG_ENT,0x4 # Page entry size
46 .set SCR_MAT,0xe1 # Mode/attribute
47 .set SCR_COL,0x50 # Columns per row
48 .set SCR_ROW,0x19 # Rows per screen
50 * BIOS Data Area locations.
52 .set BDA_MEM,0xa1501 # Free memory
53 .set BDA_POS,0xa153e # Cursor position
55 * Required by aout gas inadequacy.
57 .set SIZ_STUB,0x1a # Size of stub
59 * We expect to be loaded by boot2 at the origin defined in ./Makefile.
63 * BTX program loader for ELF clients.
65 start: cld # String ops inc
67 gdcwait.1: inb $0x60,%al
73 gdcwait.2: inb $0x60,%al
86 movl $m_logo,%esi # Identify
87 call putstr # ourselves
88 movzwl BDA_MEM,%eax # Get base memory
91 shll $0x11,%eax # in bytes
92 movl %eax,%ebp # Base of user stack
94 movl $m_mem,%esi # Display
95 call hexout # amount of
96 call putstr # base memory
98 lgdt gdtdesc # Load new GDT
100 * Relocate caller's arguments.
102 #ifdef BTXLDR_VERBOSE
103 movl $m_esp,%esi # Display
104 movl %esp,%eax # caller
106 call putstr # pointer
107 movl $m_args,%esi # Format string
108 leal 0x4(%esp,1),%ebx # First argument
109 movl $0x6,%ecx # Count
110 start.1: movl (%ebx),%eax # Get argument and
111 addl $0x4,%ebx # bump pointer
112 call hexout # Display it
113 loop start.1 # Till done
114 call putstr # End message
116 movl $0x48,%ecx # Allocate space
117 subl %ecx,%ebp # for bootinfo
118 movl 0x18(%esp,1),%esi # Source: bootinfo
119 cmpl $0x0, %esi # If the bootinfo pointer
120 je start_null_bi # is null, don't copy it
121 movl %ebp,%edi # Destination
124 movl %ebp,0x18(%esp,1) # Update pointer
125 #ifdef BTXLDR_VERBOSE
126 movl $m_rel_bi,%esi # Display
127 movl %ebp,%eax # bootinfo
128 call hexout # relocation
129 call putstr # message
131 start_null_bi: movl $0x18,%ecx # Allocate space
132 subl %ecx,%ebp # for arguments
133 leal 0x4(%esp,1),%esi # Source
134 movl %ebp,%edi # Destination
137 #ifdef BTXLDR_VERBOSE
138 movl $m_rel_args,%esi # Display
139 movl %ebp,%eax # argument
140 call hexout # relocation
141 call putstr # message
146 movl $MEM_ESP,%esp # Set up new stack
147 movl $MEM_DATA,%ebx # Data segment
148 movl $m_vers,%esi # Display BTX
149 call putstr # version message
150 movb 0x5(%ebx),%al # Get major version
151 addb $'0',%al # Display
153 movb $'.',%al # And a
155 movb 0x6(%ebx),%al # Get minor
156 xorb %ah,%ah # version
157 movb $0xa,%dl # Divide
159 addb $'0',%al # Display
161 movb %ah,%al # Get units
162 addb $'0',%al # Display
164 call putstr # End message
165 movl %ebx,%esi # BTX image
166 movzwl 0x8(%ebx),%edi # Compute
167 orl $PAG_SIZ/PAG_ENT-1,%edi # the
169 shll $0x2,%edi # load
170 addl $MEM_TBL,%edi # address
171 pushl %edi # Save load address
172 movzwl 0xa(%ebx),%ecx # Image size
173 #ifdef BTXLDR_VERBOSE
174 pushl %ecx # Save image size
178 movl %esi,%ebx # Keep place
179 #ifdef BTXLDR_VERBOSE
180 movl $m_rel_btx,%esi # Restore
181 popl %eax # parameters
185 #ifdef BTXLDR_VERBOSE
187 call hexout # relocation
188 call putstr # message
190 addl $PAG_SIZ,%ebp # Display
191 #ifdef BTXLDR_VERBOSE
192 movl $m_base,%esi # the
193 movl %ebp,%eax # user
195 call putstr # address
198 * Set up ELF-format client program.
200 cmpl $0x464c457f,(%ebx) # ELF magic number?
202 movl $e_fmt,%esi # Display error
203 call putstr # message
204 start.2: jmp start.2 # Hang
206 #ifdef BTXLDR_VERBOSE
207 movl $m_elf,%esi # Display ELF
208 call putstr # message
209 movl $m_segs,%esi # Format string
211 movl $0x2,%edi # Segment count
212 movl 0x1c(%ebx),%edx # Get e_phoff
213 addl %ebx,%edx # To pointer
214 movzwl 0x2c(%ebx),%ecx # Get e_phnum
215 start.4: cmpl $0x1,(%edx) # Is p_type PT_LOAD?
217 #ifdef BTXLDR_VERBOSE
218 movl 0x4(%edx),%eax # Display
219 call hexout # p_offset
220 movl 0x8(%edx),%eax # Display
221 call hexout # p_vaddr
222 movl 0x10(%edx),%eax # Display
223 call hexout # p_filesz
224 movl 0x14(%edx),%eax # Display
225 call hexout # p_memsz
226 call putstr # End message
230 pushl %ecx # registers
231 movl 0x4(%edx),%esi # Get p_offset
232 addl %ebx,%esi # as pointer
233 movl 0x8(%edx),%edi # Get p_vaddr
234 addl %ebp,%edi # as pointer
235 movl 0x10(%edx),%ecx # Get p_filesz
238 movl 0x14(%edx),%ecx # Any bytes
239 subl 0x10(%edx),%ecx # to zero?
244 start.5: popl %ecx # Restore
246 popl %esi # registers
247 decl %edi # Segments to do
249 start.6: addl $0x20,%edx # To next entry
250 loop start.4 # Till done
252 #ifdef BTXLDR_VERBOSE
253 movl $m_done,%esi # Display done
254 call putstr # message
256 movl $start.8,%esi # Real mode stub
257 movl $MEM_STUB,%edi # Destination
258 movl $start.9-start.8,%ecx # Size
261 ljmp $SEL_RCODE,$MEM_STUB # To 16-bit code
263 start.8: xorw %ax,%ax # Data
264 movb $SEL_RDATA,%al # selector
265 movw %ax,%ss # Reload SS
268 movw %ax,%fs # segment
269 movw %ax,%gs # limits
270 movl %cr0,%eax # Switch to
272 movl %eax,%cr0 # mode
273 ljmp $0,$MEM_ENTRY # Jump to BTX entry point
277 * Output message [ESI] followed by EAX in hex.
279 hexout: pushl %eax # Save
280 call putstr # Display message
283 pushl %edi # caller's
284 movl $buf,%edi # Buffer
287 xorb %al,%al # Terminate
290 hexout.1: lodsb # Get a char
291 cmpb $'0',%al # Leading zero?
293 testb %al,%al # End of string?
296 hexout.2: decl %esi # Adjust for inc
297 call putstr # Display hex
302 * Output zero-terminated string [ESI] to the console.
304 putstr.0: call putchr # Output char
305 putstr: lodsb # Load char
306 testb %al,%al # End of string?
310 * Output character AL to the console.
313 xorl %ecx,%ecx # Zero for loops
314 movb $SCR_MAT,%ah # Mode/attribute
315 movl $BDA_POS,%ebx # BDA pointer
316 movw (%ebx),%dx # Cursor position
317 movl $0xa0000,%edi # Regen buffer (color)
318 putchr.1: cmpb $0xa,%al # New line?
321 movb %al,(%edi,%ecx,1) # Write char
323 movb %ah,(%edi,%ecx,1) # Write attr
326 putchr.2: movw %dx,%ax
332 putchr.3: cmpw $SCR_COL*SCR_ROW*2,%dx
334 leal 2*SCR_COL(%edi),%esi # New top line
335 movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
338 movb $' ',%al # Space
340 movb $SCR_COL,%cl # Columns to clear
343 movw $(SCR_ROW-1)*SCR_COL*2,%dx
344 putchr.4: movw %dx,(%ebx) # Update position
346 gdcwait.3: inb $0x60,%al
358 * Convert EAX, AX, or AL to hex, saving the result to [EDI].
360 hex32: pushl %eax # Save
361 shrl $0x10,%eax # Do upper
364 hex16: call hex16.1 # Do upper 8
365 hex16.1: xchgb %ah,%al # Save/restore
366 hex8: pushl %eax # Save
367 shrb $0x4,%al # Do upper
370 hex8.1: andb $0xf,%al # Get lower 4
371 cmpb $0xa,%al # Convert
372 sbbb $0x69,%al # to hex
374 orb $0x20,%al # To lower case
381 * Global descriptor table.
383 gdt: .word 0x0,0x0,0x0,0x0 # Null entry
384 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE
385 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA
386 .word 0xffff,0x0,0x9a00,0x0 # SEL_RCODE
387 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA
389 gdtdesc: .word gdt.1-gdt-1 # Limit
394 m_logo: .asciz " \nBTX loader 1.00 "
395 m_vers: .asciz "BTX version is \0\n"
396 e_fmt: .asciz "Error: Client format not supported\n"
397 #ifdef BTXLDR_VERBOSE
398 m_mem: .asciz "Starting in protected mode (base mem=\0)\n"
399 m_esp: .asciz "Arguments passed (esp=\0):\n"
400 m_args: .asciz"<howto="
405 .asciz" bootinfo=\0>\n"
406 m_rel_bi: .asciz "Relocated bootinfo (size=48) to \0\n"
407 m_rel_args: .asciz "Relocated arguments (size=18) to \0\n"
408 m_rel_btx: .asciz "Relocated kernel (size=\0) to \0\n"
409 m_base: .asciz "Client base address is \0\n"
410 m_elf: .asciz "Client format is ELF\n"
411 m_segs: .asciz "text segment: offset="
415 .asciz "data segment: offset="
419 m_done: .asciz "Loading complete\n"
422 * Uninitialized data area.
424 buf: # Scratch buffer