xref: /illumos-gate/usr/src/boot/i386/btx/btxldr/btxldr.S (revision fc910014e8a32a65612105835a10995f2c13d942)
1/*
2 * Copyright (c) 1998 Robert Nordier
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$
16 */
17
18#include <bootargs.h>
19
20#define	RBX_MUTE	0x10	/* -m */
21#define	OPT_SET(opt)	(1 << (opt))
22
23/*
24 * Prototype BTX loader program, written in a couple of hours.  The
25 * real thing should probably be more flexible, and in C.
26 */
27
28/*
29 * Memory locations.
30 */
31		.set MEM_STUB,0x600		# Real mode stub
32		.set MEM_ESP,0x1000		# New stack pointer
33		.set MEM_TBL,0x5000		# BTX page tables
34		.set MEM_ENTRY,0x9010		# BTX entry point
35		.set MEM_DATA,start+0x1000	# Data segment
36/*
37 * Segment selectors.
38 */
39		.set SEL_SCODE,0x8		# 4GB code
40		.set SEL_SDATA,0x10		# 4GB data
41		.set SEL_RCODE,0x18		# 64K code
42		.set SEL_RDATA,0x20		# 64K data
43/*
44 * Paging constants.
45 */
46		.set PAG_SIZ,0x1000		# Page size
47		.set PAG_ENT,0x4		# Page entry size
48/*
49 * Screen constants.
50 */
51		.set SCR_MAT,0x7		# Mode/attribute
52		.set SCR_COL,0x50		# Columns per row
53		.set SCR_ROW,0x19		# Rows per screen
54/*
55 * BIOS Data Area locations.
56 */
57		.set BDA_MEM,0x413		# Free memory
58		.set BDA_SCR,0x449		# Video mode
59		.set BDA_POS,0x450		# Cursor position
60/*
61 * Required by aout gas inadequacy.
62 */
63		.set SIZ_STUB,0x1a		# Size of stub
64/*
65 * We expect to be loaded by boot2 at the origin defined in ./Makefile.
66 */
67		.globl start
68/*
69 * BTX program loader for ELF clients.
70 */
71start:		cld				# String ops inc
72		testl $OPT_SET(RBX_MUTE), 4(%esp) # Check first argument
73		setnz muted			#  for RBX_MUTE, set flag
74		movl $m_logo,%esi		# Identify
75		call putstr			#  ourselves
76		movzwl BDA_MEM,%eax		# Get base memory
77		shll $0xa,%eax			#  in bytes
78		movl %eax,%ebp			# Base of user stack
79#ifdef BTXLDR_VERBOSE
80		movl $m_mem,%esi		# Display
81		call hexout			#  amount of
82		call putstr			#  base memory
83#endif
84		lgdt gdtdesc			# Load new GDT
85/*
86 * Relocate caller's arguments.
87 */
88#ifdef BTXLDR_VERBOSE
89		movl $m_esp,%esi		# Display
90		movl %esp,%eax			#  caller
91		call hexout			#  stack
92		call putstr			#  pointer
93		movl $m_args,%esi		# Format string
94		leal 0x4(%esp),%ebx		# First argument
95		movl $0x6,%ecx			# Count
96start.1:	movl (%ebx),%eax		# Get argument and
97		addl $0x4,%ebx			#  bump pointer
98		call hexout			# Display it
99		loop start.1			# Till done
100		call putstr			# End message
101#endif
102		movl BA_BOOTINFO+4(%esp),%esi	# Source: bootinfo
103		cmpl $0x0, %esi			# If the bootinfo pointer
104		je start_null_bi		#  is null, don't copy it
105		movl BI_SIZE(%esi),%ecx 	# Allocate space
106		subl %ecx,%ebp			#  for bootinfo
107		movl %ebp,%edi			# Destination
108		rep				# Copy
109		movsb				#  it
110		movl %ebp,BA_BOOTINFO+4(%esp)	# Update pointer
111		movl %edi,%ebp			# Restore base pointer
112#ifdef BTXLDR_VERBOSE
113		movl $m_rel_bi,%esi		# Display
114		movl %ebp,%eax			#  bootinfo
115		call hexout			#  relocation
116		call putstr			#  message
117#endif
118start_null_bi:	movl $BOOTARGS_SIZE,%ecx 	# Fixed size of arguments
119		testl $KARGS_FLAGS_EXTARG, BA_BOOTFLAGS+4(%esp) # Check for extra data
120		jz start_fixed			# Skip if the flag is not set
121		addl BOOTARGS_SIZE+4(%esp),%ecx	# Add size of variable args
122start_fixed:	subl $ARGOFF,%ebp		# Place args at fixed offset
123		leal 0x4(%esp),%esi		# Source
124		movl %ebp,%edi			# Destination
125		rep				# Copy
126		movsb				#  them
127#ifdef BTXLDR_VERBOSE
128		movl $m_rel_args,%esi		# Display
129		movl %ebp,%eax			#  argument
130		call hexout			#  relocation
131		call putstr			#  message
132#endif
133/*
134 * Set up BTX kernel.
135 */
136		movl $MEM_ESP,%esp		# Set up new stack
137		movl $MEM_DATA,%ebx		# Data segment
138		movl $m_vers,%esi		# Display BTX
139		call putstr			#  version message
140		movb 0x5(%ebx),%al		# Get major version
141		addb $'0',%al			# Display
142		call putchr			#  it
143		movb $'.',%al			# And a
144		call putchr			#  dot
145		movb 0x6(%ebx),%al		# Get minor
146		xorb %ah,%ah			#  version
147		movb $0xa,%dl			# Divide
148		divb %dl,%al			#  by 10
149		addb $'0',%al			# Display
150		call putchr			#  tens
151		movb %ah,%al			# Get units
152		addb $'0',%al			# Display
153		call putchr			#  units
154		call putstr			# End message
155		movl %ebx,%esi			# BTX image
156		movzwl 0x8(%ebx),%edi		# Compute
157		orl $PAG_SIZ/PAG_ENT-1,%edi	#  the
158		incl %edi			#  BTX
159		shll $0x2,%edi			#  load
160		addl $MEM_TBL,%edi		#  address
161		pushl %edi			# Save load address
162		movzwl 0xa(%ebx),%ecx		# Image size
163#ifdef BTXLDR_VERBOSE
164		pushl %ecx			# Save image size
165#endif
166		rep				# Relocate
167		movsb				#  BTX
168		movl %esi,%ebx			# Keep place
169#ifdef BTXLDR_VERBOSE
170		movl $m_rel_btx,%esi		# Restore
171		popl %eax			#  parameters
172		call hexout			#  and
173#endif
174		popl %ebp			#  display
175#ifdef BTXLDR_VERBOSE
176		movl %ebp,%eax			#  the
177		call hexout			#  relocation
178		call putstr			#  message
179#endif
180		addl $PAG_SIZ,%ebp		# Display
181#ifdef BTXLDR_VERBOSE
182		movl $m_base,%esi		#  the
183		movl %ebp,%eax			#  user
184		call hexout			#  base
185		call putstr			#  address
186#endif
187/*
188 * Set up ELF-format client program.
189 */
190		cmpl $0x464c457f,(%ebx) 	# ELF magic number?
191		je start.3			# Yes
192		movl $e_fmt,%esi		# Display error
193		call putstr			#  message
194start.2:	jmp start.2			# Hang
195start.3:
196#ifdef BTXLDR_VERBOSE
197		movl $m_elf,%esi		# Display ELF
198		call putstr			#  message
199		movl $m_segs,%esi		# Format string
200#endif
201		movl 0x1c(%ebx),%edx		# Get e_phoff
202		addl %ebx,%edx			# To pointer
203		movzwl 0x2c(%ebx),%ecx		# Get e_phnum
204start.4:	cmpl $0x1,(%edx)		# Is p_type PT_LOAD?
205		jne start.6			# No
206#ifdef BTXLDR_VERBOSE
207		movl 0x4(%edx),%eax		# Display
208		call hexout			#  p_offset
209		movl 0x8(%edx),%eax		# Display
210		call hexout			#  p_vaddr
211		movl 0x10(%edx),%eax		# Display
212		call hexout			#  p_filesz
213		movl 0x14(%edx),%eax		# Display
214		call hexout			#  p_memsz
215		call putstr			# End message
216#endif
217		pushl %esi			# Save
218		pushl %ecx			#  working registers
219		movl 0x4(%edx),%esi		# Get p_offset
220		addl %ebx,%esi			#  as pointer
221		movl 0x8(%edx),%edi		# Get p_vaddr
222		addl %ebp,%edi			#  as pointer
223		movl 0x10(%edx),%ecx		# Get p_filesz
224		rep				# Set up
225		movsb				#  segment
226		movl 0x14(%edx),%ecx		# Any bytes
227		subl 0x10(%edx),%ecx		#  to zero?
228		jz start.5			# No
229		xorb %al,%al			# Then
230		rep				#  zero
231		stosb				#  them
232start.5:	popl %ecx			# Restore
233		popl %esi			#  registers
234start.6:	addl $0x20,%edx 		# To next entry
235		loop start.4			# Till done
236#ifdef BTXLDR_VERBOSE
237		movl $m_done,%esi		# Display done
238		call putstr			#  message
239#endif
240		movl $start.8,%esi		# Real mode stub
241		movl $MEM_STUB,%edi		# Destination
242		movl $start.9-start.8,%ecx	# Size
243		rep				# Relocate
244		movsb				#  it
245		ljmp $SEL_RCODE,$MEM_STUB	# To 16-bit code
246		.code16
247start.8:	xorw %ax,%ax			# Data
248		movb $SEL_RDATA,%al		#  selector
249		movw %ax,%ss			# Reload SS
250		movw %ax,%ds			# Reset
251		movw %ax,%es			#  other
252		movw %ax,%fs			#  segment
253		movw %ax,%gs			#  limits
254		movl %cr0,%eax			# Switch to
255		decw %ax			#  real
256		movl %eax,%cr0			#  mode
257		ljmp $0,$MEM_ENTRY		# Jump to BTX entry point
258start.9:
259		.code32
260/*
261 * Output message [ESI] followed by EAX in hex.
262 */
263hexout: 	pushl %eax			# Save
264		call putstr			# Display message
265		popl %eax			# Restore
266		pushl %esi			# Save
267		pushl %edi			#  caller's
268		movl $buf,%edi			# Buffer
269		pushl %edi			# Save
270		call hex32			# To hex
271		xorb %al,%al			# Terminate
272		stosb				#  string
273		popl %esi			# Restore
274hexout.1:	lodsb				# Get a char
275		cmpb $'0',%al			# Leading zero?
276		je hexout.1			# Yes
277		testb %al,%al			# End of string?
278		jne hexout.2			# No
279		decl %esi			# Undo
280hexout.2:	decl %esi			# Adjust for inc
281		call putstr			# Display hex
282		popl %edi			# Restore
283		popl %esi			#  caller's
284		ret				# To caller
285/*
286 * Output zero-terminated string [ESI] to the console.
287 */
288putstr.0:	call putchr			# Output char
289putstr: 	lodsb				# Load char
290		testb %al,%al			# End of string?
291		jne putstr.0			# No
292		ret				# To caller
293/*
294 * Output character AL to the console.
295 */
296putchr:		testb $1,muted			# Check muted
297		jnz putchr.5			#  do a nop
298		pusha				# Save
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?
305		jne putchr.1			# No
306		xorw %di,%di			# Regen buffer (mono)
307putchr.1:	cmpb $0xa,%al			# New line?
308		je putchr.2			# Yes
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
314		shll %eax			#  * 2
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?
319		jb putchr.3			# No
320putchr.2:	xorb %dl,%dl			# Zero column
321		incb %dh			# Bump row
322putchr.3:	cmpb $SCR_ROW,%dh		# Beyond screen?
323		jb putchr.4			# No
324		leal 2*SCR_COL(%edi),%esi	# New top line
325		movw $(SCR_ROW-1)*SCR_COL/2,%cx # Words to move
326		rep				# Scroll
327		movsl				#  screen
328		movb $' ',%al			# Space
329		movb $SCR_COL,%cl		# Columns to clear
330		rep				# Clear
331		stosw				#  line
332		movb $SCR_ROW-1,%dh		# Bottom line
333putchr.4:	movw %dx,(%ebx) 		# Update position
334		popa				# Restore
335putchr.5:	ret				# To caller
336/*
337 * Convert EAX, AX, or AL to hex, saving the result to [EDI].
338 */
339hex32:		pushl %eax			# Save
340		shrl $0x10,%eax 		# Do upper
341		call hex16			#  16
342		popl %eax			# Restore
343hex16:		call hex16.1			# Do upper 8
344hex16.1:	xchgb %ah,%al			# Save/restore
345hex8:		pushl %eax			# Save
346		shrb $0x4,%al			# Do upper
347		call hex8.1			#  4
348		popl %eax			# Restore
349hex8.1: 	andb $0xf,%al			# Get lower 4
350		cmpb $0xa,%al			# Convert
351		sbbb $0x69,%al			#  to hex
352		das				#  digit
353		orb $0x20,%al			# To lower case
354		stosb				# Save char
355		ret				# (Recursive)
356
357		.data
358		.p2align 4
359/*
360 * Global descriptor table.
361 */
362gdt:		.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
367gdt.1:
368gdtdesc:	.word gdt.1-gdt-1		# Limit
369		.long gdt			# Base
370/*
371 * Messages.
372 */
373m_logo: 	.asciz " \nBTX loader 1.00  "
374m_vers: 	.asciz "BTX version is \0\n"
375e_fmt:		.asciz "Error: Client format not supported\n"
376#ifdef BTXLDR_VERBOSE
377m_mem:		.asciz "Starting in protected mode (base mem=\0)\n"
378m_esp:		.asciz "Arguments passed (esp=\0):\n"
379m_args: 	.asciz "<howto="
380		.asciz " bootdev="
381		.asciz " junk="
382		.asciz " "
383		.asciz " "
384		.asciz " bootinfo=\0>\n"
385m_rel_bi:	.asciz "Relocated bootinfo (size=48) to \0\n"
386m_rel_args:	.asciz "Relocated arguments (size=18) to \0\n"
387m_rel_btx:	.asciz "Relocated kernel (size=\0) to \0\n"
388m_base: 	.asciz "Client base address is \0\n"
389m_elf:		.asciz "Client format is ELF\n"
390m_segs: 	.asciz "text segment: offset="
391		.asciz " vaddr="
392		.asciz " filesz="
393		.asciz " memsz=\0\n"
394		.asciz "data segment: offset="
395		.asciz " vaddr="
396		.asciz " filesz="
397		.asciz " memsz=\0\n"
398m_done: 	.asciz "Loading complete\n"
399#endif
400
401/*
402 * Flags
403 */
404muted:		.byte 0x0
405
406/*
407 * Uninitialized data area.
408 */
409buf:						# Scratch buffer
410