cdboot.S revision 81363
1# 2# Copyright (c) 2000 John Baldwin 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 16# $FreeBSD: head/sys/boot/i386/cdboot/cdboot.s 81363 2001-08-09 18:07:45Z jhb $ 17 18# 19# This simple program is a preloader for the normal boot3 loader. It is simply 20# prepended to the beginning of a fully built and btxld'd loader. It then 21# copies the loader to the address boot2 normally loads it, emulates the 22# boot[12] environment (protected mode, a bootinfo struct, etc.), and then jumps 23# to the start of btxldr to start the boot process. This method allows a stock 24# /boot/loader to be used w/o having to fully rewrite boot[12] to handle the 25# cd9660 file system. 26# 27 28# 29# Memory locations. 30# 31 .set MEM_PAGE_SIZE,0x1000 # memory page size, 4k 32 .set MEM_ARG,0x900 # Arguments at start 33 .set MEM_ARG_BTX,0xa100 # Where we move them to so the 34 # BTX client can see them 35 .set MEM_ARG_SIZE,0x18 # Size of the arguments 36 .set MEM_BTX_ADDRESS,0x9000 # where BTX lives 37 .set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute 38 .set MEM_BTX_OFFSET,MEM_PAGE_SIZE # offset of BTX in the loader 39 .set MEM_BTX_CLIENT,0xa000 # where BTX clients live 40# 41# a.out header fields 42# 43 .set AOUT_TEXT,0x04 # text segment size 44 .set AOUT_DATA,0x08 # data segment size 45 .set AOUT_BSS,0x0c # zero'd BSS size 46 .set AOUT_SYMBOLS,0x10 # symbol table 47 .set AOUT_ENTRY,0x14 # entry point 48 .set AOUT_HEADER,MEM_PAGE_SIZE # size of the a.out header 49# 50# Flags for kargs->bootflags 51# 52 .set KARGS_FLAGS_CD,0x1 # flag to indicate booting from 53 # CD loader 54# 55# Segment selectors. 56# 57 .set SEL_SDATA,0x8 # Supervisor data 58 .set SEL_RDATA,0x10 # Real mode data 59 .set SEL_SCODE,0x18 # PM-32 code 60 .set SEL_SCODE16,0x20 # PM-16 code 61# 62# BTX constants 63# 64 .set INT_SYS,0x30 # BTX syscall interrupt 65# 66# We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry 67# point) 68# 69 .code16 70 .globl start 71 .org 0x0, 0x0 72# 73# BTX program loader for CD booting 74# 75start: cld # string ops inc 76 xorw %ax, %ax # zero %ax 77 movw %ax, %ss # setup the 78 movw $start, %sp # stack 79 pushw %dx # save the BIOS boot device in 80 # %dl for later 81 movw %ax, %ds # setup the 82 movw %ax, %es # data segments 83 movw $welcome_msg, %si # %ds:(%si) -> welcome message 84 callw putstr # display the welcome message 85# 86# Setup the arguments that the loader is expecting from boot[12] 87# 88 movw $bootinfo_msg, %si # %ds:(%si) -> boot args message 89 callw putstr # display the message 90 movw $MEM_ARG, %bx # %ds:(%bx) -> boot args 91 movw %bx, %di # %es:(%di) -> boot args 92 xorl %eax, %eax # zero %eax 93 movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit 94 # dwords 95 rep # Clear the arguments 96 stosl # to zero 97 popw %dx # restore BIOS boot device 98 movb %dl, 0x4(%bx) # set kargs->bootdev 99 orb $KARGS_FLAGS_CD, 0x8(%bx) # kargs->bootflags |= 100 # KARGS_FLAGS_CD 101# 102# Turn on the A20 address line 103# 104 callw seta20 # Turn A20 on 105# 106# Relocate the loader and BTX using a very lazy protected mode 107# 108 movw $relocate_msg, %si # Display the 109 callw putstr # relocation message 110 movl end+AOUT_ENTRY, %edi # %edi is the destination 111 movl $(end+AOUT_HEADER), %esi # %esi is 112 # the start of the text 113 # segment 114 movl end+AOUT_TEXT, %ecx # %ecx = length of the text 115 # segment 116 lgdt gdtdesc # setup our own gdt 117 cli # turn off interrupts 118 movl %cr0, %eax # Turn on 119 orb $0x1, %al # protected 120 movl %eax, %cr0 # mode 121 ljmp $SEL_SCODE,$pm_start # long jump to clear the 122 # instruction pre-fetch queue 123 .code32 124pm_start: movw $SEL_SDATA, %ax # Initialize 125 movw %ax, %ds # %ds and 126 movw %ax, %es # %es to a flat selector 127 rep # Relocate the 128 movsb # text segment 129 addl $(MEM_PAGE_SIZE - 1), %edi # pad %edi out to a new page 130 andl $~(MEM_PAGE_SIZE - 1), %edi # for the data segment 131 movl end+AOUT_DATA, %ecx # size of the data segment 132 rep # Relocate the 133 movsb # data segment 134 movl end+AOUT_BSS, %ecx # size of the bss 135 xorl %eax, %eax # zero %eax 136 addb $3, %cl # round %ecx up to 137 shrl $2, %ecx # a multiple of 4 138 rep # zero the 139 stosl # bss 140 movl end+AOUT_ENTRY, %esi # %esi -> relocated loader 141 addl $MEM_BTX_OFFSET, %esi # %esi -> BTX in the loader 142 movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go 143 movzwl 0xa(%esi), %ecx # %ecx -> length of BTX 144 rep # Relocate 145 movsb # BTX 146 ljmp $SEL_SCODE16,$pm_16 # Jump to 16-bit PM 147 .code16 148pm_16: movw $SEL_RDATA, %ax # Initialize 149 movw %ax, %ds # %ds and 150 movw %ax, %es # %es to a real mode selector 151 movl %cr0, %eax # Turn off 152 andb $~0x1, %al # protected 153 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