cdboot.S revision 56693
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 56693 2000-01-27 21:21:01Z 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_LOADER_ADDRESS,0x100000 # where the loader lives 32 .set MEM_LDR_ENTRY,0x7c00 # our entry point 33 .set MEM_ARG,0x900 # Arguments at start 34 .set MEM_ARG_BTX,0xa100 # Where we move them to so the 35 # BTX client can see them 36 .set MEM_ARG_SIZE,0x18 # Size of the arguments 37 .set MEM_BTX_ADDRESS,0x9000 # where BTX lives 38 .set MEM_BTX_ENTRY,0x9010 # where BTX starts to execute 39 .set MEM_AOUT_HEADER,0x1000 # size of the a.out header 40 .set MEM_BTX_OFFSET,0x1000 # offset of BTX in the loader 41 .set MEM_BTX_IMAGE,MEM_LOADER_ADDRESS+MEM_BTX_OFFSET # where 42 # BTX is in the loader 43 .set MEM_BTX_CLIENT,0xa000 # where BTX clients live 44# 45# Flags for kargs->bootflags 46# 47 .set KARGS_FLAGS_CD,0x1 # flag to indicate booting from 48 # CD loader 49# 50# Segment selectors. 51# 52 .set SEL_SDATA,0x8 # Supervisor data 53 .set SEL_RDATA,0x10 # Real mode data 54 .set SEL_SCODE,0x18 # PM-32 code 55 .set SEL_SCODE16,0x20 # PM-16 code 56# 57# BTX constants 58# 59 .set INT_SYS,0x30 # BTX syscall interrupt 60# 61# We expect to be loaded by the BIOS at 0x7c00 (standard boot loader entry point) 62# 63 .code16 64 .globl start 65 .org 0x0, 0x0 66# 67# BTX program loader for CD booting 68# 69start: jmp begin # skip the boot info table 70 .org 0x8, 0x90 # fill with nops up to the table 71# 72# Boot information table that is filled in by mkisofs(8), see the man page for 73# details 74# 75boot_info_table: 76bi_pvd_LBA: .long 0x0 77bi_file_LBA: .long 0x0 78bi_file_length: .long 0x0 79bi_checksum: .long 0x0 80bi_reserved: .byte 0x0 81 .org 0x40, 0x0 82# 83# Actual start of execution 84# 85begin: cld # string ops inc 86 xorw %ax, %ax # zero %ax 87 movw %ax, %ss # setup the 88 movw $MEM_LDR_ENTRY, %sp # stack 89 pushw %dx # save the BIOS boot device in 90 # %dl for later 91 movw $(MEM_LDR_ENTRY/0x10), %ax # setup the 92 movw %ax, %ds # data segment 93 movl $welcome_msg, %si # %ds:(%si) -> welcome message 94 call putstr # display the welcome message 95# 96# Setup the arguments that the loader is expecting from boot[12] 97# 98 movl $bootinfo_msg, %si # %ds:(%si) -> boot args message 99 call putstr # display the message 100 pushw %ss # Copy %ss 101 popw %es # to %es 102 movl $MEM_ARG, %ebx # %es:(%ebx) -> boot args 103 movw %bx, %di # %es:(%di) -> boot args 104 xorl %eax, %eax # zero %eax 105 movw $(MEM_ARG_SIZE/4), %cx # Size of arguments in 32-bit 106 # dwords 107 rep # Clear the arguments 108 stosl # to zero 109 popw %dx # restore BIOS boot device 110 movb %dl, %es:0x4(%ebx) # set kargs->bootdev 111 orb $KARGS_FLAGS_CD, %es:0x8(%ebx) # kargs->bootflags |= KARGS_FLAGS_CD 112# 113# Turn on the A20 address line 114# 115 call seta20 # Turn A20 on 116# 117# Relocate the loader and BTX using a very lazy protected mode 118# 119 movw $relocate_msg, %si # Display the 120 call putstr # relocation message 121 movl $MEM_LOADER_ADDRESS, %edi # %edi is the destination 122 movl $(MEM_LDR_ENTRY+end-start+MEM_AOUT_HEADER), %esi # %esi is 123 # the start of the raw loader 124 movl bi_file_length, %ecx # Set %ecx to the length 125 subl $(end-start+MEM_AOUT_HEADER), %ecx # of the raw loader 126 lgdt gdtdesc # setup our own gdt 127 cli # turn off interrupts 128 movl %cr0, %eax # Turn on 129 orl $0x1, %eax # protected 130 movl %eax, %cr0 # mode 131 .byte 0xea # long jump to 132 .word MEM_LDR_ENTRY+pm_start # clear the instruction 133 .word SEL_SCODE # pre-fetch 134 .code32 135pm_start: movw $SEL_SDATA, %ax # Initialize 136 movw %ax, %ds # %ds and 137 movw %ax, %es # %es to a flat selector 138 rep # Relocate 139 movsb # the loader 140 movl $MEM_BTX_IMAGE, %esi # %esi -> BTX in the loader 141 movl $MEM_BTX_ADDRESS, %edi # %edi -> where BTX needs to go 142 movzwl 0xa(%esi), %ecx # %ecx -> length of BTX 143 rep # Relocate 144 movsb # BTX 145 ljmp $SEL_SCODE16,$(MEM_LDR_ENTRY+pm_16) # Jump to 16-bit PM 146 .code16 147pm_16: movw $SEL_RDATA, %ax # Initialize 148 movw %ax, %ds # %ds and 149 movw %ax, %es # %es to a real mode selector 150 movl %cr0, %eax # Turn off 151 andl $~0x1, %eax # protected 152 movl %eax, %cr0 # mode 153 .byte 0xea # Long jump to 154 .word pm_end # clear the instruction 155 .word MEM_LDR_ENTRY/0x10 # pre-fetch 156pm_end: sti # Turn interrupts back on now 157# 158# Copy the BTX client to MEM_BTX_CLIENT 159# 160 movw $(MEM_LDR_ENTRY/0x10), %ax # Initialize 161 movw %ax, %ds # %ds to local data segment 162 xorw %ax, %ax # zero %ax and initialize 163 movw %ax, %es # %es to segment 0 164 movw $MEM_BTX_CLIENT, %di # Prepare to relocate 165 movw $btx_client, %si # the simple btx client 166 movw $(btx_client_end-btx_client), %cx # length of btx client 167 rep # Relocate the 168 movsb # simple BTX client 169# 170# Copy the boot[12] args to where the BTX client can see them 171# 172 movw $MEM_ARG, %si # where the args are at now 173 movw %ax, %ds # need segment 0 in %ds 174 movw $MEM_ARG_BTX, %di # where the args are moving to 175 movw $(MEM_ARG_SIZE/4), %cx # size of the arguments in longs 176 rep # Relocate 177 movsl # the words 178# 179# Now we just start up BTX and let it do the rest 180# 181 movw $(MEM_LDR_ENTRY/0x10), %ax # Initialize 182 movw %ax, %ds # %ds to the local data segment 183 movl $jump_message, %si # Display the 184 call putstr # jump message 185 .byte 0xea # Jump to 186 .word MEM_BTX_ENTRY # BTX entry 187 .word 0x0 # 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 ret # 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 ret # To caller 217 218# 219# BTX client to start btxld 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_LOADER_ADDRESS) # Address to jump to 232 pushl %eax # Emulate a near call 233 movl $0x1, %eax # 'exec' system call 234 int $INT_SYS # BTX system call 235btx_client_end: 236 .code16 237 238 .p2align 4 239# 240# Global descriptor table. 241# 242gdt: .word 0x0,0x0,0x0,0x0 # Null entry 243 .word 0xffff,0x0,0x9200,0xcf # SEL_SDATA 244 .word 0xffff,0x0,0x9200,0x0 # SEL_RDATA 245 .word 0xffff,0x0,0x9a00,0xcf # SEL_SCODE (32-bit) 246 .word 0xffff,0x0,0x9a00,0x8f # SEL_SCODE16 (16-bit) 247gdt.1: 248# 249# Pseudo-descriptors. 250# 251gdtdesc: .word gdt.1-gdt-1 # Limit 252 .long gdt+MEM_LDR_ENTRY # Base 253 254welcome_msg: .asciz "CD Loader 1.00\r\n\n" 255bootinfo_msg: .asciz "Building the boot loader arguments\r\n" 256relocate_msg: .asciz "Relocating the loader and the BTX\r\n" 257jump_message: .asciz "Starting the BTX loader\r\n" 258 259end: 260