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
18 #define RBX_MUTE 0x10 /* -m */
19 #define OPT_SET(opt) (1 << (opt))
22 * Prototype BTX loader program, written in a couple of hours. The
23 * real thing should probably be more flexible, and in C.
29 .set MEM_STUB,0x600 # Real mode stub
30 .set MEM_ESP,0x1000 # New stack pointer
31 .set MEM_TBL,0x5000 # BTX page tables
32 .set MEM_ENTRY,0x9010 # BTX entry point
33 .set MEM_DATA,start+0x1000 # Data segment
37 .set SEL_SCODE,0x8 # 4GB code
38 .set SEL_SDATA,0x10 # 4GB data
39 .set SEL_RCODE,0x18 # 64K code
40 .set SEL_RDATA,0x20 # 64K data
44 .set PAG_SIZ,0x1000 # Page size
45 .set PAG_ENT,0x4 # Page entry size
49 .set SCR_MAT,0x7 # Mode/attribute
50 .set SCR_COL,0x50 # Columns per row
51 .set SCR_ROW,0x19 # Rows per screen
53 * BIOS Data Area locations.
55 .set BDA_MEM,0x413 # Free memory
56 .set BDA_SCR,0x449 # Video mode
57 .set BDA_POS,0x450 # Cursor position
59 * Required by aout gas inadequacy.
61 .set SIZ_STUB,0x1a # Size of stub
63 * We expect to be loaded by boot2 at the origin defined in ./Makefile.
67 * BTX program loader for ELF clients.
69 start: cld # String ops inc
70 testl $OPT_SET(RBX_MUTE), 4(%esp) # Check first argument
71 setnz muted # for RBX_MUTE, set flag
72 movl $m_logo,%esi # Identify
73 call putstr # ourselves
74 movzwl BDA_MEM,%eax # Get base memory
75 shll $0xa,%eax # in bytes
76 movl %eax,%ebp # Base of user stack
78 movl $m_mem,%esi # Display
79 call hexout # amount of
80 call putstr # base memory
82 lgdt gdtdesc # Load new GDT
84 * Relocate caller's arguments.
87 movl $m_esp,%esi # Display
88 movl %esp,%eax # caller
91 movl $m_args,%esi # Format string
92 leal 0x4(%esp,1),%ebx # First argument
93 movl $0x6,%ecx # Count
94 start.1: movl (%ebx),%eax # Get argument and
95 addl $0x4,%ebx # bump pointer
96 call hexout # Display it
97 loop start.1 # Till done
98 call putstr # End message
100 movl $0x48,%ecx # Allocate space
101 subl %ecx,%ebp # for bootinfo
102 movl 0x18(%esp,1),%esi # Source: bootinfo
103 cmpl $0x0, %esi # If the bootinfo pointer
104 je start_null_bi # is null, don't copy it
105 movl %ebp,%edi # Destination
108 movl %ebp,0x18(%esp,1) # Update pointer
109 #ifdef BTXLDR_VERBOSE
110 movl $m_rel_bi,%esi # Display
111 movl %ebp,%eax # bootinfo
112 call hexout # relocation
113 call putstr # message
115 start_null_bi: movl $0x18,%ecx # Allocate space
116 subl %ecx,%ebp # for arguments
117 leal 0x4(%esp,1),%esi # Source
118 movl %ebp,%edi # Destination
121 #ifdef BTXLDR_VERBOSE
122 movl $m_rel_args,%esi # Display
123 movl %ebp,%eax # argument
124 call hexout # relocation
125 call putstr # message
130 movl $MEM_ESP,%esp # Set up new stack
131 movl $MEM_DATA,%ebx # Data segment
132 movl $m_vers,%esi # Display BTX
133 call putstr # version message
134 movb 0x5(%ebx),%al # Get major version
135 addb $'0',%al # Display
137 movb $'.',%al # And a
139 movb 0x6(%ebx),%al # Get minor
140 xorb %ah,%ah # version
141 movb $0xa,%dl # Divide
143 addb $'0',%al # Display
145 movb %ah,%al # Get units
146 addb $'0',%al # Display
148 call putstr # End message
149 movl %ebx,%esi # BTX image
150 movzwl 0x8(%ebx),%edi # Compute
151 orl $PAG_SIZ/PAG_ENT-1,%edi # the
153 shll $0x2,%edi # load
154 addl $MEM_TBL,%edi # address
155 pushl %edi # Save load address
156 movzwl 0xa(%ebx),%ecx # Image size
157 #ifdef BTXLDR_VERBOSE
158 pushl %ecx # Save image size
162 movl %esi,%ebx # Keep place
163 #ifdef BTXLDR_VERBOSE
164 movl $m_rel_btx,%esi # Restore
165 popl %eax # parameters
169 #ifdef BTXLDR_VERBOSE
171 call hexout # relocation
172 call putstr # message
174 addl $PAG_SIZ,%ebp # Display
175 #ifdef BTXLDR_VERBOSE
176 movl $m_base,%esi # the
177 movl %ebp,%eax # user
179 call putstr # address
182 * Set up ELF-format client program.
184 cmpl $0x464c457f,(%ebx) # ELF magic number?
186 movl $e_fmt,%esi # Display error
187 call putstr # message
188 start.2: jmp start.2 # Hang
190 #ifdef BTXLDR_VERBOSE
191 movl $m_elf,%esi # Display ELF
192 call putstr # message
193 movl $m_segs,%esi # Format string
195 movl $0x2,%edi # Segment count
196 movl 0x1c(%ebx),%edx # Get e_phoff
197 addl %ebx,%edx # To pointer
198 movzwl 0x2c(%ebx),%ecx # Get e_phnum
199 start.4: cmpl $0x1,(%edx) # Is p_type PT_LOAD?
201 #ifdef BTXLDR_VERBOSE
202 movl 0x4(%edx),%eax # Display
203 call hexout # p_offset
204 movl 0x8(%edx),%eax # Display
205 call hexout # p_vaddr
206 movl 0x10(%edx),%eax # Display
207 call hexout # p_filesz
208 movl 0x14(%edx),%eax # Display
209 call hexout # p_memsz
210 call putstr # End message
214 pushl %ecx # registers
215 movl 0x4(%edx),%esi # Get p_offset
216 addl %ebx,%esi # as pointer
217 movl 0x8(%edx),%edi # Get p_vaddr
218 addl %ebp,%edi # as pointer
219 movl 0x10(%edx),%ecx # Get p_filesz
222 movl 0x14(%edx),%ecx # Any bytes
223 subl 0x10(%edx),%ecx # to zero?
228 start.5: popl %ecx # Restore
230 popl %esi # registers
231 decl %edi # Segments to do
233 start.6: addl $0x20,%edx # To next entry
234 loop start.4 # Till done
236 #ifdef BTXLDR_VERBOSE
237 movl $m_done,%esi # Display done
238 call putstr # message
240 movl $start.8,%esi # Real mode stub
241 movl $MEM_STUB,%edi # Destination
242 movl $start.9-start.8,%ecx # Size
245 ljmp $SEL_RCODE,$MEM_STUB # To 16-bit code
247 start.8: xorw %ax,%ax # Data
248 movb $SEL_RDATA,%al # selector
249 movw %ax,%ss # Reload SS
252 movw %ax,%fs # segment
253 movw %ax,%gs # limits
254 movl %cr0,%eax # Switch to
256 movl %eax,%cr0 # mode
257 ljmp $0,$MEM_ENTRY # Jump to BTX entry point
261 * Output message [ESI] followed by EAX in hex.
263 hexout: pushl %eax # Save
264 call putstr # Display message
267 pushl %edi # caller's
268 movl $buf,%edi # Buffer
271 xorb %al,%al # Terminate
274 hexout.1: lodsb # Get a char
275 cmpb $'0',%al # Leading zero?
277 testb %al,%al # End of string?
280 hexout.2: decl %esi # Adjust for inc
281 call putstr # Display hex
286 * Output zero-terminated string [ESI] to the console.
288 putstr.0: call putchr # Output char
289 putstr: lodsb # Load char
290 testb %al,%al # End of string?
294 * Output character AL to the console.
296 putchr: testb $1,muted # Check muted
297 jnz putchr.5 # do a nop
299 xorl %ecx,%ecx # Zero for loops
300 movb $SCR_MAT,%ah # Mode/attribute
301 movl $BDA_POS,%ebx # BDA pointer
302 movw (%ebx),%dx # Cursor position
303 movl $0xb8000,%edi # Regen buffer (color)
304 cmpb %ah,BDA_SCR-BDA_POS(%ebx) # Mono mode?
306 xorw %di,%di # Regen buffer (mono)
307 putchr.1: cmpb $0xa,%al # New line?
309 xchgl %eax,%ecx # Save char
310 movb $SCR_COL,%al # Columns per row
311 mulb %dh # * row position
312 addb %dl,%al # + column
313 adcb $0x0,%ah # position
315 xchgl %eax,%ecx # Swap char, offset
316 movw %ax,(%edi,%ecx,1) # Write attr:char
317 incl %edx # Bump cursor
318 cmpb $SCR_COL,%dl # Beyond row?
320 putchr.2: xorb %dl,%dl # Zero column
322 putchr.3: cmpb $SCR_ROW,%dh # Beyond screen?
324 leal 2*SCR_COL(%edi),%esi # New top line
325 movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
328 movb $' ',%al # Space
329 movb $SCR_COL,%cl # Columns to clear
332 movb $SCR_ROW-1,%dh # Bottom line
333 putchr.4: movw %dx,(%ebx) # Update position
335 putchr.5: ret # To caller
337 * Convert EAX, AX, or AL to hex, saving the result to [EDI].
339 hex32: pushl %eax # Save
340 shrl $0x10,%eax # Do upper
343 hex16: call hex16.1 # Do upper 8
344 hex16.1: xchgb %ah,%al # Save/restore
345 hex8: pushl %eax # Save
346 shrb $0x4,%al # Do upper
349 hex8.1: andb $0xf,%al # Get lower 4
350 cmpb $0xa,%al # Convert
351 sbbb $0x69,%al # to hex
353 orb $0x20,%al # To lower case
360 * Global descriptor table.
362 gdt: .word 0x0,0x0,0x0,0x0 # Null entry
363 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE
364 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA
365 .word 0xffff,0x0,0x9a00,0x0 # SEL_RCODE
366 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA
368 gdtdesc: .word gdt.1-gdt-1 # Limit
373 m_logo: .asciz " \nBTX loader 1.00 "
374 m_vers: .asciz "BTX version is \0\n"
375 e_fmt: .asciz "Error: Client format not supported\n"
376 #ifdef BTXLDR_VERBOSE
377 m_mem: .asciz "Starting in protected mode (base mem=\0)\n"
378 m_esp: .asciz "Arguments passed (esp=\0):\n"
379 m_args: .asciz"<howto="
384 .asciz" bootinfo=\0>\n"
385 m_rel_bi: .asciz "Relocated bootinfo (size=48) to \0\n"
386 m_rel_args: .asciz "Relocated arguments (size=18) to \0\n"
387 m_rel_btx: .asciz "Relocated kernel (size=\0) to \0\n"
388 m_base: .asciz "Client base address is \0\n"
389 m_elf: .asciz "Client format is ELF\n"
390 m_segs: .asciz "text segment: offset="
394 .asciz "data segment: offset="
398 m_done: .asciz "Loading complete\n"
407 * Uninitialized data area.
409 buf: # Scratch buffer