cdboot.S revision 60821
10SN/A# 22362SN/A# Copyright (c) 2000 John Baldwin 30SN/A# All rights reserved. 40SN/A# 50SN/A# Redistribution and use in source and binary forms are freely 60SN/A# permitted provided that the above copyright notice and this 72362SN/A# paragraph and the following disclaimer are duplicated in all 80SN/A# such forms. 92362SN/A# 100SN/A# This software is provided "AS IS" and without any express or 110SN/A# implied warranties, including, without limitation, the implied 120SN/A# warranties of merchantability and fitness for a particular 130SN/A# purpose. 140SN/A# 150SN/A 160SN/A# $FreeBSD: head/sys/boot/i386/cdboot/cdboot.s 60821 2000-05-23 12:18:49Z jhb $ 170SN/A 180SN/A# 190SN/A# This simple program is a preloader for the normal boot3 loader. It is simply 200SN/A# prepended to the beginning of a fully built and btxld'd loader. It then 212362SN/A# copies the loader to the address boot2 normally loads it, emulates the 222362SN/A# boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps 232362SN/A# to the start of btxldr to start the boot process. This method allows a stock 240SN/A# /boot/loader to be used w/o having to fully rewrite boot[12] to handle the 250SN/A# cd9660 file system. 260SN/A# 270SN/A 280SN/A# 290SN/A# Memory locations. 300SN/A# 310SN/A .set MEM_PAGE_SIZE,0x1000 # memory page size, 4k 320SN/A .set MEM_ARG,0x900 # Arguments at start 330SN/A .set MEM_ARG_BTX,0xa100 # Where we move them to so the 340SN/A # BTX client can see them 350SN/A .set MEM_ARG_SIZE,0x18 # Size of the arguments 360SN/A .set MEM_BTX_ADDRESS,0x9000 # where BTX lives 370SN/A .set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute 380SN/A .set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader 390SN/A .set MEM_BTX_CLIENT,0xa000 # where BTX clients live 400SN/A# 410SN/A# a.out header fields 420SN/A# 430SN/A .set AOUT_TEXT,0x04 # text segment size 440SN/A .set AOUT_DATA,0x08 # data segment size 450SN/A .set AOUT_BSS,0x0c # zero'd BSS size 460SN/A .set AOUT_SYMBOLS,0x10 # symbol table 470SN/A .set AOUT_ENTRY,0x14 # entry point 480SN/A .set AOUT_HEADER,MEM_PAGE_SIZE # size of the a.out header 490SN/A# 500SN/A# Flags for kargs->bootflags 5110203SN/A# 5210203SN/A .set KARGS_FLAGS_CD,0x1 # flag to indicate booting from 5310203SN/A # CD loader 5410203SN/A# 5510203SN/A# Segment selectors. 5610203SN/A# 570SN/A .set SEL_SDATA,0x8 # Supervisor data 580SN/A .set SEL_RDATA,0x10 # Real mode data 590SN/A .set SEL_SCODE,0x18 # PM-32 code 600SN/A .set SEL_SCODE16,0x20 # PM-16 code 6110203SN/A# 6210203SN/A# BTX constants 6310203SN/A# 640SN/A .set INT_SYS,0x30 # BTX syscall interrupt 650SN/A# 660SN/A# We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry 670SN/A# point) 680SN/A# 690SN/A .code16 700SN/A .globl start 710SN/A .org 0x0, 0x0 720SN/A# 730SN/A# BTX program loader for CD booting 740SN/A# 750SN/Astart: cld # string ops inc 760SN/A xorw %ax, %ax # zero %ax 770SN/A movw %ax, %ss # setup the 780SN/A movw $start, %sp # stack 790SN/A pushw %dx # save the BIOS boot device in 800SN/A # %dl for later 810SN/A movw %ax, %ds # setup the 820SN/A movw %ax, %es # data segments 830SN/A movw $welcome_msg, %si # %ds:(%si) -> welcome message 840SN/A callw putstr # display the welcome message 850SN/A# 860SN/A# Setup the arguments that the loader is expecting from boot[12] 870SN/A# 880SN/A movw $bootinfo_msg, %si # %ds:(%si) -> boot args message 890SN/A callw putstr # display the message 900SN/A movl $MEM_ARG, %bx # %ds:(%bx) -> boot args 910SN/A movw %bx, %di # %es:(%di) -> boot args 920SN/A xorl %eax, %eax # zero %eax 930SN/A movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit 940SN/A # dwords 950SN/A rep # Clear the arguments 960SN/A stosl # to zero 970SN/A popw %dx # restore BIOS boot device 980SN/A movb %dl, 0x4(%bx) # set kargs->bootdev 990SN/A orb $KARGS_FLAGS_CD, 0x8(%bx) # kargs->bootflags |= 1000SN/A # KARGS_FLAGS_CD 1010SN/A# 1020SN/A# Turn on the A20 address line 1030SN/A# 1040SN/A callw seta20 # Turn A20 on 1050SN/A# 1060SN/A# Relocate the loader and BTX using a very lazy protected mode 1070SN/A# 1080SN/A movw $relocate_msg, %si # Display the 1090SN/A callw putstr # relocation message 1100SN/A movl end+AOUT_ENTRY, %edi # %edi is the destination 1110SN/A movl $(end+AOUT_HEADER), %esi # %esi is 1120SN/A # the start of the text 1130SN/A # segment 1140SN/A movl end+AOUT_TEXT, %ecx # %ecx = length of the text 1150SN/A # segment 1160SN/A lgdt gdtdesc # setup our own gdt 1170SN/A cli # turn off interrupts 1180SN/A movl %cr0, %eax # Turn on 1190SN/A orb $0x1, %al # protected 1200SN/A movl %eax, %cr0 # mode 1210SN/A ljmp $SEL_SCODE,$pm_start # long jump to clear the 1220SN/A # instruction pre-fetch queue 1230SN/A .code32 1240SN/Apm_start: movw $SEL_SDATA, %ax # Initialize 1250SN/A movw %ax, %ds # %ds and 1260SN/A movw %ax, %es # %es to a flat selector 1270SN/A rep # Relocate the 1280SN/A movsb # text segment 1290SN/A addl $(MEM_PAGE_SIZE - 1), %edi # pad %edi out to a new page 1300SN/A andl $~(MEM_PAGE_SIZE - 1), %edi # for the data segment 1310SN/A movl end+AOUT_DATA, %ecx # size of the data segment 1320SN/A rep # Relocate the 1330SN/A movsb # data segment 1340SN/A movl end+AOUT_BSS, %ecx # size of the bss 1350SN/A xorl %eax, %eax # zero %eax 1360SN/A addb $3, %cl # round %ecx up to 1370SN/A shrl $2, %ecx # a multiple of 4 1380SN/A rep # zero the 1390SN/A stosl # bss 1400SN/A movl end+AOUT_ENTRY, %esi # %esi -> relocated loader 1410SN/A addl $MEM_BTX_OFFSET, %esi # %esi -> BTX in the loader 1420SN/A movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go 1430SN/A movzwl 0xa(%esi), %ecx # %ecx -> length of BTX 1440SN/A rep # Relocate 1450SN/A movsb # BTX 1460SN/A ljmp $SEL_SCODE16,$pm_16 # Jump to 16-bit PM 1470SN/A .code16 1480SN/Apm_16: movw $SEL_RDATA, %ax # Initialize 1490SN/A movw %ax, %ds # %ds and 1500SN/A movw %ax, %es # %es to a real mode selector 1510SN/A movl %cr0, %eax # Turn off 1520SN/A andb $~0x1, %al # protected 1530SN/A movl %eax, %cr0 # mode 154 ljmp $0,$pm_end # Long jump to clear the 155 # instruction pre-fetch queue 156pm_end: sti # Turn interrupts back on now 157# 158# Copy the BTX client to MEM_BTX_CLIENT 159# 160 xorw %ax, %ax # zero %ax and set 161 movw %ax, %ds # %ds and %es 162 movw %ax, %es # to segment 0 163 movw $MEM_BTX_CLIENT, %di # Prepare to relocate 164 movw $btx_client, %si # the simple btx client 165 movw $(btx_client_end-btx_client), %cx # length of btx client 166 rep # Relocate the 167 movsb # simple BTX client 168# 169# Copy the boot[12] args to where the BTX client can see them 170# 171 movw $MEM_ARG, %si # where the args are at now 172 movw $MEM_ARG_BTX, %di # where the args are moving to 173 movw $(MEM_ARG_SIZE/4), %cx # size of the arguments in longs 174 rep # Relocate 175 movsl # the words 176# 177# Save the entry point so the client can get to it later on 178# 179 movl end+AOUT_ENTRY, %eax # load the entry point 180 stosl # add it to the end of the 181 # arguments 182# 183# Now we just start up BTX and let it do the rest 184# 185 movw $jump_message, %si # Display the 186 callw putstr # jump message 187 ljmp $0,$MEM_BTX_ENTRY # Jump to the BTX entry point 188 189# 190# Display a null-terminated string 191# 192putstr: lodsb # load %al from %ds:(%si) 193 testb %al,%al # stop at null 194 jnz putc # if the char != null, output it 195 retw # return when null is hit 196putc: movw $0x7,%bx # attribute for output 197 movb $0xe,%ah # BIOS: put_char 198 int $0x10 # call BIOS, print char in %al 199 jmp putstr # keep looping 200 201# 202# Enable A20 203# 204seta20: cli # Disable interrupts 205seta20.1: inb $0x64,%al # Get status 206 testb $0x2,%al # Busy? 207 jnz seta20.1 # Yes 208 movb $0xd1,%al # Command: Write 209 outb %al,$0x64 # output port 210seta20.2: inb $0x64,%al # Get status 211 testb $0x2,%al # Busy? 212 jnz seta20.2 # Yes 213 movb $0xdf,%al # Enable 214 outb %al,$0x60 # A20 215 sti # Enable interrupts 216 retw # To caller 217 218# 219# BTX client to start btxldr 220# 221 .code32 222btx_client: movl $(MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE-4), %esi 223 # %ds:(%esi) -> end 224 # of boot[12] args 225 movl $(MEM_ARG_SIZE/4), %ecx # Number of words to push 226 std # Go backwards 227push_arg: lodsl # Read argument 228 pushl %eax # Push it onto the stack 229 loop push_arg # Push all of the arguments 230 cld # In case anyone depends on this 231 pushl MEM_ARG_BTX-MEM_BTX_CLIENT+MEM_ARG_SIZE # Entry point of 232 # the loader 233 pushl %eax # Emulate a near call 234 movl $0x1, %eax # 'exec' system call 235 int $INT_SYS # BTX system call 236btx_client_end: 237 .code16 238 239 .p2align 4 240# 241# Global descriptor table. 242# 243gdt: .word 0x0,0x0,0x0,0x0 # Null entry 244 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA 245 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA 246 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE (32-bit) 247 .word 0xffff,0x0,0x9a00,0x8f # SEL_SCODE16 (16-bit) 248gdt.1: 249# 250# Pseudo-descriptors. 251# 252gdtdesc: .word gdt.1-gdt-1 # Limit 253 .long gdt # Base 254 255welcome_msg: .asciz "CD Loader 1.00\r\n\n" 256bootinfo_msg: .asciz "Building the boot loader arguments\r\n" 257relocate_msg: .asciz "Relocating the loader and the BTX\r\n" 258jump_message: .asciz "Starting the BTX loader\r\n" 259 260end: 261