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
16 # $FreeBSD: src/sys/boot/pc98/btx/btxldr/btxldr.s,v 1.3.2.2 2000/07/06 23:04:33 obrien Exp $
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
47 .set SCR_MAT,0xe1 # Mode/attribute
49 .set SCR_MAT,0x7 # Mode/attribute
51 .set SCR_COL,0x50 # Columns per row
52 .set SCR_ROW,0x19 # Rows per screen
54 # BIOS Data Area locations.
57 .set BDA_MEM,0xa1501 # Free memory
58 .set BDA_POS,0xa153e # Cursor position
60 .set BDA_MEM,0x413 # Free memory
61 .set BDA_SCR,0x449 # Video mode
62 .set BDA_POS,0x450 # Cursor position
65 # Required by aout gas inadequacy.
67 .set SIZ_STUB,0x1a # Size of stub
69 # We expect to be loaded by boot2 at the origin defined in ./Makefile.
73 # BTX program loader for ELF clients.
75 start: cld # String ops inc
78 gdcwait.1: inb $0x60,%al
84 gdcwait.2: inb $0x60,%al
98 movl $m_logo,%esi # Identify
99 call putstr # ourselves
100 movzwl BDA_MEM,%eax # Get base memory
104 shll $0x11,%eax # in bytes
106 shll $0xa,%eax # in bytes
108 movl %eax,%ebp # Base of user stack
109 ifdef(`BTXLDR_VERBOSE',`
110 movl $m_mem,%esi # Display
111 call hexout # amount of
112 call putstr # base memory
114 lgdt gdtdesc # Load new GDT
116 # Relocate caller's arguments.
118 ifdef('BTXLDR_VERBOSE',`
119 movl $m_esp,%esi # Display
120 movl %esp,%eax # caller
122 call putstr # pointer
123 movl $m_args,%esi # Format string
124 leal 0x4(%esp,1),%ebx # First argument
125 movl $0x6,%ecx # Count
126 start.1: movl (%ebx),%eax # Get argument and
127 addl $0x4,%ebx # bump pointer
128 call hexout # Display it
129 loop start.1 # Till done
130 call putstr # End message
132 movl $0x48,%ecx # Allocate space
133 subl %ecx,%ebp # for bootinfo
134 movl 0x18(%esp,1),%esi # Source: bootinfo
135 cmpl $0x0, %esi # If the bootinfo pointer
136 je start_null_bi # is null, don't copy it
137 movl %ebp,%edi # Destination
140 movl %ebp,0x18(%esp,1) # Update pointer
141 ifdef(`BTXLDR_VERBOSE',`
142 movl $m_rel_bi,%esi # Display
143 movl %ebp,%eax # bootinfo
144 call hexout # relocation
145 call putstr # message
147 start_null_bi: movl $0x18,%ecx # Allocate space
148 subl %ecx,%ebp # for arguments
149 leal 0x4(%esp,1),%esi # Source
150 movl %ebp,%edi # Destination
153 ifdef(`BTXLDR_VERBOSE',`
154 movl $m_rel_args,%esi # Display
155 movl %ebp,%eax # argument
156 call hexout # relocation
157 call putstr # message
162 movl $MEM_ESP,%esp # Set up new stack
163 movl $MEM_DATA,%ebx # Data segment
164 movl $m_vers,%esi # Display BTX
165 call putstr # version message
166 movb 0x5(%ebx),%al # Get major version
167 addb $'0',%al # Display
169 movb $'.',%al # And a
171 movb 0x6(%ebx),%al # Get minor
172 xorb %ah,%ah # version
173 movb $0xa,%dl # Divide
175 addb $'0',%al # Display
177 movb %ah,%al # Get units
178 addb $'0',%al # Display
180 call putstr # End message
181 movl %ebx,%esi # BTX image
182 movzwl 0x8(%ebx),%edi # Compute
183 orl $PAG_SIZ/PAG_ENT-1,%edi # the
185 shll $0x2,%edi # load
186 addl $MEM_TBL,%edi # address
187 pushl %edi # Save load address
188 movzwl 0xa(%ebx),%ecx # Image size
189 ifdef(`BTXLDR_VERBOSE',`
190 pushl %ecx # Save image size
194 movl %esi,%ebx # Keep place
195 ifdef(`BTXLDR_VERBOSE',`
196 movl $m_rel_btx,%esi # Restore
197 popl %eax # parameters
201 ifdef(`BTXLDR_VERBOSE',`
203 call hexout # relocation
204 call putstr # message
206 addl $PAG_SIZ,%ebp # Display
207 ifdef(`BTXLDR_VERBOSE',`
208 movl $m_base,%esi # the
209 movl %ebp,%eax # user
211 call putstr # address
214 # Set up ELF-format client program.
216 cmpl $0x464c457f,(%ebx) # ELF magic number?
218 movl $e_fmt,%esi # Display error
219 call putstr # message
220 start.2: jmp start.2 # Hang
222 ifdef(`BTXLDR_VERBOSE',`
223 movl $m_elf,%esi # Display ELF
224 call putstr # message
225 movl $m_segs,%esi # Format string
227 movl $0x2,%edi # Segment count
228 movl 0x1c(%ebx),%edx # Get e_phoff
229 addl %ebx,%edx # To pointer
230 movzwl 0x2c(%ebx),%ecx # Get e_phnum
231 start.4: cmpl $0x1,(%edx) # Is p_type PT_LOAD?
233 ifdef(`BTXLDR_VERBOSE',`
234 movl 0x4(%edx),%eax # Display
235 call hexout # p_offset
236 movl 0x8(%edx),%eax # Display
237 call hexout # p_vaddr
238 movl 0x10(%edx),%eax # Display
239 call hexout # p_filesz
240 movl 0x14(%edx),%eax # Display
241 call hexout # p_memsz
242 call putstr # End message
246 pushl %ecx # registers
247 movl 0x4(%edx),%esi # Get p_offset
248 addl %ebx,%esi # as pointer
249 movl 0x8(%edx),%edi # Get p_vaddr
250 addl %ebp,%edi # as pointer
251 movl 0x10(%edx),%ecx # Get p_filesz
254 movl 0x14(%edx),%ecx # Any bytes
255 subl 0x10(%edx),%ecx # to zero?
260 start.5: popl %ecx # Restore
262 popl %esi # registers
263 decl %edi # Segments to do
265 start.6: addl $0x20,%edx # To next entry
266 loop start.4 # Till done
268 ifdef(`BTXLDR_VERBOSE',`
269 movl $m_done,%esi # Display done
270 call putstr # message
272 movl $start.8,%esi # Real mode stub
273 movl $MEM_STUB,%edi # Destination
274 movl $start.9-start.8,%ecx # Size
277 ljmp $SEL_RCODE,$MEM_STUB # To 16-bit code
279 start.8: xorw %ax,%ax # Data
280 movb $SEL_RDATA,%al # selector
281 movw %ax,%ss # Reload SS
284 movw %ax,%fs # segment
285 movw %ax,%gs # limits
286 movl %cr0,%eax # Switch to
288 movl %eax,%cr0 # mode
289 ljmp $0,$MEM_ENTRY # Jump to BTX entry point
293 # Output message [ESI] followed by EAX in hex.
295 hexout: pushl %eax # Save
296 call putstr # Display message
299 pushl %edi # caller's
300 movl $buf,%edi # Buffer
303 xorb %al,%al # Terminate
306 hexout.1: lodsb # Get a char
307 cmpb $'0',%al # Leading zero?
309 testb %al,%al # End of string?
312 hexout.2: decl %esi # Adjust for inc
313 call putstr # Display hex
318 # Output zero-terminated string [ESI] to the console.
320 putstr.0: call putchr # Output char
321 putstr: lodsb # Load char
322 testb %al,%al # End of string?
326 # Output character AL to the console.
329 xorl %ecx,%ecx # Zero for loops
330 movb $SCR_MAT,%ah # Mode/attribute
331 movl $BDA_POS,%ebx # BDA pointer
332 movw (%ebx),%dx # Cursor position
334 movl $0xa0000,%edi # Regen buffer (color)
336 movl $0xb8000,%edi # Regen buffer (color)
337 cmpb %ah,BDA_SCR-BDA_POS(%ebx) # Mono mode?
339 xorw %di,%di # Regen buffer (mono)
341 putchr.1: cmpb $0xa,%al # New line?
345 movb %al,(%edi,%ecx,1) # Write char
347 movb %ah,(%edi,%ecx,1) # Write attr
350 putchr.2: movw %dx,%ax
356 putchr.3: cmpw $SCR_COL*SCR_ROW*2,%dx
358 xchgl %eax,%ecx # Save char
359 movb $SCR_COL,%al # Columns per row
360 mulb %dh # * row position
361 addb %dl,%al # + column
362 adcb $0x0,%ah # position
364 xchgl %eax,%ecx # Swap char, offset
365 movw %ax,(%edi,%ecx,1) # Write attr:char
366 incl %edx # Bump cursor
367 cmpb $SCR_COL,%dl # Beyond row?
369 putchr.2: xorb %dl,%dl # Zero column
371 putchr.3: cmpb $SCR_ROW,%dh # Beyond screen?
374 leal 2*SCR_COL(%edi),%esi # New top line
375 movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
378 movb $' ',%al # Space
382 movb $SCR_COL,%cl # Columns to clear
386 movw $(SCR_ROW-1)*SCR_COL*2,%dx
387 putchr.4: movw %dx,(%ebx) # Update position
389 gdcwait.3: inb $0x60,%al
399 movb $SCR_ROW-1,%dh # Bottom line
400 putchr.4: movw %dx,(%ebx) # Update position
405 # Convert EAX, AX, or AL to hex, saving the result to [EDI].
407 hex32: pushl %eax # Save
408 shrl $0x10,%eax # Do upper
411 hex16: call hex16.1 # Do upper 8
412 hex16.1: xchgb %ah,%al # Save/restore
413 hex8: pushl %eax # Save
414 shrb $0x4,%al # Do upper
417 hex8.1: andb $0xf,%al # Get lower 4
418 cmpb $0xa,%al # Convert
419 sbbb $0x69,%al # to hex
421 orb $0x20,%al # To lower case
428 # Global descriptor table.
430 gdt: .word 0x0,0x0,0x0,0x0 # Null entry
431 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE
432 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA
433 .word 0xffff,0x0,0x9a00,0x0 # SEL_RCODE
434 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA
436 gdtdesc: .word gdt.1-gdt-1 # Limit
441 m_logo: .asciz " \nBTX loader 1.00 "
442 m_vers: .asciz "BTX version is \0\n"
443 e_fmt: .asciz "Error: Client format not supported\n"
444 ifdef(`BTXLDR_VERBOSE',`
445 m_mem: .asciz "Starting in protected mode (base mem=\0)\n"
446 m_esp: .asciz "Arguments passed (esp=\0):\n"
447 m_args: .asciz"<howto="
452 .asciz" bootinfo=\0>\n"
453 m_rel_bi: .asciz "Relocated bootinfo (size=48) to \0\n"
454 m_rel_args: .asciz "Relocated arguments (size=18) to \0\n"
455 m_rel_btx: .asciz "Relocated kernel (size=\0) to \0\n"
456 m_base: .asciz "Client base address is \0\n"
457 m_elf: .asciz "Client format is ELF\n"
458 m_segs: .asciz "text segment: offset="
462 .asciz "data segment: offset="
466 m_done: .asciz "Loading complete\n"
469 # Uninitialized data area.
471 buf: # Scratch buffer