/*1* Copyright (c) 1998 Robert Nordier2* All rights reserved.3*4* Redistribution and use in source and binary forms are freely5* permitted provided that the above copyright notice and this6* paragraph and the following disclaimer are duplicated in all7* such forms.8*9* This software is provided "AS IS" and without any express or10* implied warranties, including, without limitation, the implied11* warranties of merchantability and fitness for a particular12* purpose.13*/1415/* Memory Locations */16.set MEM_REL,0x700 # Relocation address17.set MEM_ARG,0x900 # Arguments18.set MEM_ORG,0x7c00 # Origin19.set MEM_BUF,0x8c00 # Load area20.set MEM_BTX,0x9000 # BTX start21.set MEM_JMP,0x9010 # BTX entry point22.set MEM_USR,0xa000 # Client start23.set BDA_BOOT,0x472 # Boot howto flag2425/* Partition Constants */26.set PRT_OFF,0x1be # Partition offset27.set PRT_NUM,0x4 # Partitions28.set PRT_BSD,0xa5 # Partition type2930/* Flag Bits */31.set FL_PACKET,0x80 # Packet mode3233/* Misc. Constants */34.set SIZ_PAG,0x1000 # Page size35.set SIZ_SEC,0x200 # Sector size3637.set NSECT,0x1038.globl start39.globl xread40.code164142start: jmp main # Start recognizably4344/*45* This is the start of a standard BIOS Parameter Block (BPB). Most bootable46* FAT disks have this at the start of their MBR. While normal BIOS's will47* work fine without this section, IBM's El Torito emulation "fixes" up the48* BPB by writing into the memory copy of the MBR. Rather than have data49* written into our xread routine, we'll define a BPB to work around it.50* The data marked with (T) indicates a field required for a ThinkPad to51* recognize the disk and (W) indicates fields written from IBM BIOS code.52* The use of the BPB is based on what OpenBSD and NetBSD implemented in53* their boot code but the required fields were determined by trial and error.54*55* Note: If additional space is needed in boot1, one solution would be to56* move the "prompt" message data (below) to replace the OEM ID.57*/58.org 0x03, 0x0059oemid: .space 0x08, 0x00 # OEM ID6061.org 0x0b, 0x0062bpb: .word 512 # sector size (T)63.byte 0 # sectors/clustor64.word 0 # reserved sectors65.byte 0 # number of FATs66.word 0 # root entries67.word 0 # small sectors68.byte 0 # media type (W)69.word 0 # sectors/fat70.word 18 # sectors per track (T)71.word 2 # number of heads (T)72.long 0 # hidden sectors (W)73.long 0 # large sectors7475.org 0x24, 0x0076ebpb: .byte 0 # BIOS physical drive number (W)7778.org 0x25,0x9079/*80* Trampoline used by boot2 to call read to read data from the disk via81* the BIOS. Call with:82*83* %cx:%ax - long - LBA to read in84* %es:(%bx) - caddr_t - buffer to read data into85* %dl - byte - drive to read from86* %dh - byte - num sectors to read87*/8889xread: push %ss # Address90pop %ds # data91/*92* Setup an EDD disk packet and pass it to read93*/94xread.1: # Starting95pushl $0x0 # absolute96push %cx # block97push %ax # number98push %es # Address of99push %bx # transfer buffer100xor %ax,%ax # Number of101movb %dh,%al # blocks to102push %ax # transfer103push $0x10 # Size of packet104mov %sp,%bp # Packet pointer105callw read # Read from disk106lea 0x10(%bp),%sp # Clear stack107lret # To far caller108/*109* Load the rest of boot2 and BTX up, copy the parts to the right locations,110* and start it all up.111*/112113/*114* Setup the segment registers to flat addressing (segment 0) and setup the115* stack to end just below the start of our code.116*/117main: cld # String ops inc118xor %cx,%cx # Zero119mov %cx,%es # Address120mov %cx,%ds # data121mov %cx,%ss # Set up122mov $start,%sp # stack123/*124* Relocate ourself to MEM_REL. Since %cx == 0, the inc %ch sets125* %cx == 0x100. Note that boot1 does not use this relocated copy126* of itself while loading boot2; however, BTX reclaims the memory127* used by boot1 during its initialization. As a result, boot2 uses128* xread from the relocated copy.129*/130mov %sp,%si # Source131mov $MEM_REL,%di # Destination132incb %ch # Word count133rep # Copy134movsw # code135/*136* If we are on a hard drive, then load the MBR and look for the first137* FreeBSD slice. We use the fake partition entry below that points to138* the MBR when we call nread. The first pass looks for the first active139* FreeBSD slice. The second pass looks for the first non-active FreeBSD140* slice if the first one fails.141*/142mov $part4,%si # Partition143cmpb $0x80,%dl # Hard drive?144jb main.4 # No145movb $0x1,%dh # Block count146callw nread # Read MBR147mov $0x1,%cx # Two passes148main.1: mov $MEM_BUF+PRT_OFF,%si # Partition table149movb $0x1,%dh # Partition150main.2: cmpb $PRT_BSD,0x4(%si) # Our partition type?151jne main.3 # No152jcxz main.5 # If second pass153testb $0x80,(%si) # Active?154jnz main.5 # Yes155main.3: add $0x10,%si # Next entry156incb %dh # Partition157cmpb $0x1+PRT_NUM,%dh # In table?158jb main.2 # Yes159dec %cx # Do two160jcxz main.1 # passes161/*162* If we get here, we didn't find any FreeBSD slices at all, so print an163* error message and die.164*/165mov $msg_part,%si # Message166jmp error # Error167/*168* Floppies use partition 0 of drive 0.169*/170main.4: xor %dx,%dx # Partition:drive171/*172* Ok, we have a slice and drive in %dx now, so use that to locate and load173* boot2. %si references the start of the slice we are looking for, so go174* ahead and load up the first 16 sectors (boot1 + boot2) from that. When175* we read it in, we conveniently use 0x8c00 as our transfer buffer. Thus,176* boot1 ends up at 0x8c00, and boot2 starts at 0x8c00 + 0x200 = 0x8e00.177* The first part of boot2 is the disklabel, which is 0x200 bytes long.178* The second part is BTX, which is thus loaded into 0x9000, which is where179* it also runs from. The boot2.bin binary starts right after the end of180* BTX, so we have to figure out where the start of it is and then move the181* binary to 0xc000. Normally, BTX clients start at MEM_USR, or 0xa000, but182* when we use btxld to create boot2, we use an entry point of 0x2000. That183* entry point is relative to MEM_USR; thus boot2.bin starts at 0xc000.184*/185main.5: mov %dx,MEM_ARG # Save args186movb $NSECT,%dh # Sector count187callw nread # Read disk188mov $MEM_BTX,%bx # BTX189mov 0xa(%bx),%si # Get BTX length and set190add %bx,%si # %si to start of boot2.bin191mov $MEM_USR+SIZ_PAG*2,%di # Client page 2192mov $MEM_BTX+(NSECT-1)*SIZ_SEC,%cx # Byte193sub %si,%cx # count194rep # Relocate195movsb # client196197/*198* Enable A20 so we can access memory above 1 meg.199* Use the zero-valued %cx as a timeout for embedded hardware which do not200* have a keyboard controller.201*/202seta20: cli # Disable interrupts203seta20.1: dec %cx # Timeout?204jz seta20.3 # Yes205inb $0x64,%al # Get status206testb $0x2,%al # Busy?207jnz seta20.1 # Yes208movb $0xd1,%al # Command: Write209outb %al,$0x64 # output port210seta20.2: inb $0x64,%al # Get status211testb $0x2,%al # Busy?212jnz seta20.2 # Yes213movb $0xdf,%al # Enable214outb %al,$0x60 # A20215seta20.3: sti # Enable interrupts216217jmp start+MEM_JMP-MEM_ORG # Start BTX218219220/*221* Trampoline used to call read from within boot1.222*/223nread: mov $MEM_BUF,%bx # Transfer buffer224mov 0x8(%si),%ax # Get225mov 0xa(%si),%cx # LBA226push %cs # Read from227callw xread.1 # disk228jnc return # If success, return229mov $msg_read,%si # Otherwise, set the error230# message and fall through to231# the error routine232/*233* Print out the error message pointed to by %ds:(%si) followed234* by a prompt, wait for a keypress, and then reboot the machine.235*/236error: callw putstr # Display message237mov $prompt,%si # Display238callw putstr # prompt239xorb %ah,%ah # BIOS: Get240int $0x16 # keypress241movw $0x1234, BDA_BOOT # Do a warm boot242ljmp $0xf000,$0xfff0 # reboot the machine243/*244* Display a null-terminated string using the BIOS output.245*/246putstr.0: mov $0x7,%bx # Page:attribute247movb $0xe,%ah # BIOS: Display248int $0x10 # character249putstr: lodsb # Get char250testb %al,%al # End of string?251jne putstr.0 # No252253/*254* Overused return code. ereturn is used to return an error from the255* read function. Since we assume putstr succeeds, we (ab)use the256* same code when we return from putstr.257*/258ereturn: movb $0x1,%ah # Invalid259stc # argument260return: retw # To caller261/*262* Reads sectors from the disk. If EDD is enabled, then check if it is263* installed and use it if it is. If it is not installed or not enabled, then264* fall back to using CHS. Since we use a LBA, if we are using CHS, we have to265* fetch the drive parameters from the BIOS and divide it out ourselves.266* Call with:267*268* %dl - byte - drive number269* stack - 10 bytes - EDD Packet270*/271read: testb $FL_PACKET,%cs:MEM_REL+flags-start # LBA support enabled?272jz read.1 # No, use CHS273cmpb $0x80,%dl # Hard drive?274jb read.1 # No, use CHS275mov $0x55aa,%bx # Magic276push %dx # Save277movb $0x41,%ah # BIOS: Check278int $0x13 # extensions present279pop %dx # Restore280jc read.1 # If error, use CHS281cmp $0xaa55,%bx # Magic?282jne read.1 # No, so use CHS283testb $0x1,%cl # Packet interface?284jz read.1 # No, so use CHS285mov %bp,%si # Disk packet286movb $0x42,%ah # BIOS: Extended287int $0x13 # read288retw # To caller289read.1: push %dx # Save290movb $0x8,%ah # BIOS: Get drive291int $0x13 # parameters292movb %dh,%ch # Max head number293pop %dx # Restore294jc return # If error295andb $0x3f,%cl # Sectors per track296jz ereturn # If zero297cli # Disable interrupts298mov 0x8(%bp),%eax # Get LBA299push %dx # Save300movzbl %cl,%ebx # Divide by301xor %edx,%edx # sectors302div %ebx # per track303movb %ch,%bl # Max head number304movb %dl,%ch # Sector number305inc %bx # Divide by306xorb %dl,%dl # number307div %ebx # of heads308movb %dl,%bh # Head number309pop %dx # Restore310cmpl $0x3ff,%eax # Cylinder number supportable?311sti # Enable interrupts312ja ereturn # No, return an error313xchgb %al,%ah # Set up cylinder314rorb $0x2,%al # number315orb %ch,%al # Merge316inc %ax # sector317xchg %ax,%cx # number318movb %bh,%dh # Head number319subb %ah,%al # Sectors this track320mov 0x2(%bp),%ah # Blocks to read321cmpb %ah,%al # To read322jb read.2 # this323#ifdef TRACK_AT_A_TIME324movb %ah,%al # track325#else326movb $1,%al # one sector327#endif328read.2: mov $0x5,%di # Try count329read.3: les 0x4(%bp),%bx # Transfer buffer330push %ax # Save331movb $0x2,%ah # BIOS: Read332int $0x13 # from disk333pop %bx # Restore334jnc read.4 # If success335dec %di # Retry?336jz read.6 # No337xorb %ah,%ah # BIOS: Reset338int $0x13 # disk system339xchg %bx,%ax # Block count340jmp read.3 # Continue341read.4: movzbw %bl,%ax # Sectors read342add %ax,0x8(%bp) # Adjust343jnc read.5 # LBA,344incw 0xa(%bp) # transfer345read.5: shlb %bl # buffer346add %bl,0x5(%bp) # pointer,347sub %al,0x2(%bp) # block count348ja read.1 # If not done349read.6: retw # To caller350351/* Messages */352353msg_read: .asciz "Read"354msg_part: .asciz "Boot"355356prompt: .asciz " error\r\n"357358flags: .byte FLAGS # Flags359360.org PRT_OFF,0x90361362/* Partition table */363364.fill 0x30,0x1,0x0365part4: .byte 0x80, 0x00, 0x01, 0x00366.byte 0xa5, 0xfe, 0xff, 0xff367.byte 0x00, 0x00, 0x00, 0x00368.byte 0x50, 0xc3, 0x00, 0x00 # 50000 sectors long, bleh369370.word 0xaa55 # Magic number371372373