Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;
- ; stage1.s - Schwann stage 1. Loads 32 kiB from the bootable partition and
- ; parses it as an ELF executable, then jumps to the ELF's entry point.
- ;
- ; This file is part of the Schwann bootloader and is distributed under the
- ; terms of the X11 license below.
- ;
- ; Copyright (C) 2012 Synon.
- ;
- ; Permission is hereby granted, free of charge, to any person obtaining a copy
- ; of this software and associated documentation files (the "Software"), to deal
- ; in the Software without restriction, including without limitation the rights
- ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- ; copies of the Software, and to permit persons to whom the Software is
- ; furnished to do so, subject to the following conditions:
- ;
- ; The above copyright notice and this permission notice shall be included in
- ; all copies or substantial portions of the Software.
- ;
- ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- ; SOFTWARE.
- ;
- ; For error reporting we usually just print the BIOS error status as two hex
- ; bytes followed by a space (e.g., "00 ").
- ;
- ; However, according to Ralph Brown's Interrupt List (RBIL), not all of the
- ; status bytes are in use. Here is a(n inclusive) list of free ones:
- ; 0x12 - 0x19
- ; 0x21 - 0x30
- ; 0x33 - 0x39
- ; 0x41 - 0x79
- ; 0x81 - 0xA9
- ; 0xAB - 0xAF
- ; 0xB7 - 0xBA
- ; 0xBC - 0xCB
- ; 0xCD - 0xDF
- ; 0xE1 - 0xFE
- ; The ones we use can be found below.
- ;;
- [bits 16]
- [org 0x7c00]
- ; Error codes.
- ENOACTIVE equ 0x12 ; No active partition.
- EELFMAGIC equ 0x21 ; ELF header error.
- ; Stage 2 constants.
- STAGE2_SEGMENT equ 0x0000 ; Stage 2 load segment.
- STAGE2_OFFSET equ 0x1000 - 24 ; Stage 2 load offset.
- STAGE2_BYTES equ 0x8000 ; 32768 bytes / 32 kiB.
- STAGE2_SECTORS equ 54 ; Stage 2 sectors (CHS HDDs).
- ; ELF constants.
- ELF_MAGIC_0 equ 0x7F
- ELF_MAGIC_1 equ 0x45
- ELF_MAGIC_2 equ 0x4C
- ELF_MAGIC_3 equ 0x46
- ; Other constants.
- BUFFER_OFFSET equ 0x1000 ; Drive parameters buffer.
- ; Set up segments & stack, then save the device ID in DL register.
- _start:
- cli ; Interrupts off.
- mov ax, 0 ; AX = 0.
- mov ds, ax ; DS = 0.
- mov ss, ax ; SS = 0.
- mov sp, 0x9c00 ; SP = 0x7c00 + 0x200 = 0x9c00.
- mov [device], dl ; [device] = DL (device ID).
- sti ; Interrupts on.
- ; Decide whether we are booting from hard disk (HDD) or floppy (FDD).
- is_hdd_or_fdd:
- cmp byte [device], 0x01
- jle read_fdd
- ; Decide whether to use LBA or CHS for the hard disk.
- hdd_is_lba_or_chs:
- mov ah, 0x41 ; Check extensions.
- mov bx, 0x55aa
- mov dl, [device]
- int 0x13
- and cx, 0 ; CX bitflags bit 0 -> LBA supported?
- cmp cx, 0
- je read_hdd_chs
- ; Read data from the hard disk (LBA).
- read_hdd_lba:
- ; Get the drive parameters (INT 0x13/AH=0x48).
- .get_params:
- mov ah, 0x48 ; Extended drive parameters.
- mov si, BUFFER_OFFSET ; Drive parameters buffer.
- mov dl, [device] ; BIOS device.
- int 0x13
- cmp ah, 0
- jne .error
- ; Get the LBA of the first block of the active partition.
- .get_active:
- mov bx, 0x7dbe ; Partition table address.
- mov byte [ctr], 4 ; Only 4 partitions.
- .get_active_loop:
- mov ah, [bx]
- cmp ah, 0x80
- je .setup_dap
- add bx, 16
- dec byte [ctr]
- cmp byte [ctr], 0
- jg .get_active_loop
- mov al, ENOACTIVE ; No bootable partitions.
- call putbyte
- jmp hang
- ; Set up the Disk Address Packet (DAP).
- .setup_dap:
- ; Store LBA of first block in active partition to the DAP.
- ; This is one quadword (where 1 qword = 8 bytes).
- ; First word.
- mov ax, [bx]
- push bx
- mov bx, dap.offset
- pop bx
- mov [bx], ax
- ; Second word.
- mov ax, [bx]
- push bx
- mov bx, dap.offset + 2
- pop bx
- mov [bx], ax
- ; Third word.
- mov ax, [bx]
- push bx
- mov bx, dap.offset + 4
- pop bx
- mov [bx], ax
- ; Fourth word.
- mov ax, [bx]
- push bx
- mov bx, dap.offset + 6
- pop bx
- mov [bx], ax
- ; Calculate the number of blocks to be read.
- mov ax, STAGE2_BYTES ; Bytes to load.
- mov bx, [si + 0x18] ; Bytes per block from the disk
- div bx ; parameters.
- mov [dap.blocks], ax ; AX = block count.
- ; Use the DAP to read the data (INT 0x13/AX=0x42).
- .lba_read:
- mov ah, 0x42 ; Read sectors (extended).
- mov dl, [device] ; BIOS device.
- mov si, dap ; DS:SI = Disk Address Packet.
- int 0x13
- cmp ah, 0
- jne .error
- ; ES:BX = STAGE2_SEGMENT:STAGE2_OFFSET
- mov bx, STAGE2_SEGMENT
- mov es, bx
- mov bx, STAGE2_OFFSET
- jmp parse_elf
- ; Error - print error code and hang.
- .error:
- mov al, ah
- call putbyte
- jmp hang
- ; Read data from the hard disk (CHS).
- read_hdd_chs:
- ; Find the CHS of the active partition.
- .get_active:
- mov bx, 0x7dbe ; Partition table address.
- mov byte [ctr], 4
- .get_active_loop:
- mov ah, [bx] ; Partition status byte.
- cmp ah, 0x80
- je .setup_chs ; Found bootable partition.
- add bx, 16 ; Next entry.
- dec byte [ctr]
- cmp byte [ctr], 0
- jg .get_active_loop
- ; No bootable partitions.
- mov al, ENOACTIVE
- call putbyte
- jmp hang
- ; Store the CHS of the active partition's first sector.
- .setup_chs:
- mov ax, [bx + 0x01]
- mov [stage2_chs], ax
- ; Read CHS (INT 0x13/AH=0x02).
- .chs_read:
- mov ah, 0x02 ; Read sectors.
- mov al, STAGE2_SECTORS ; Number of sectors to read.
- mov bx, STAGE2_SEGMENT ; Stage2 buffer segment address.
- mov es, bx
- mov bx, STAGE2_OFFSET ; Stage2 buffer offset address.
- mov ch, [stage2_chs + 2]; Cylinder.
- mov cl, [stage2_chs + 1]; Sector.
- mov dh, [stage2_chs + 0]; Head.
- mov dl, [device] ; BIOS device.
- int 0x13
- cmp ah, 0
- je parse_elf
- ; Error - print error code and hang.
- .error:
- mov al, ah
- call putbyte
- ; Hang the CPU in an idle loop.
- hang:
- cli ; Interrupts off.
- hlt ; Wait for interrupt.
- jmp hang ; Go back to 'hang'.
- ; Technically speaking after the CLI no interrupts should be occurring
- ; unless an error happens, so the JMP after the HLT probably doesn't do
- ; anything in most cases.
- ; Check the ELF header.
- parse_elf:
- ; Check the ELF magic number.
- .check:
- ; First byte.
- mov ah, [es:bx + 0]
- mov al, ELF_MAGIC_0
- cmp ah, al
- jne .error
- ; Second byte.
- mov ah, [es:bx + 1]
- mov al, ELF_MAGIC_1
- cmp ah, al
- jne .error
- ; Third byte.
- mov ah, [es:bx + 2]
- mov al, ELF_MAGIC_2
- cmp ah, al
- jne .error
- ; Fourth byte.
- mov ah, [es:bx + 3]
- mov al, ELF_MAGIC_3
- cmp ah, al
- jne .error
- ; Get the entry point and jump to it.
- .get_elf_entry:
- mov ebx, [bx + 24]
- mov eax, 0x1BADB002
- jmp [ebx]
- ; Print error code and hang.
- .error:
- mov al, EELFMAGIC
- call putbyte
- jmp hang
- ; Prints the character in AL.
- putchar:
- mov ah, 0x0e
- int 0x10
- ret
- ; Prints the first nibble in AL.
- putnibble:
- cmp al, 10
- jl .lt10
- add al, 'A' - 10
- call putchar
- jmp .done
- .lt10:
- add al, '0'
- call putchar
- .done:
- ret
- ; Prints the byte in AL as two hexadecimal characters (two nibbles) and a space.
- putbyte:
- ; The first nibble
- push ax
- shr al, 4
- call putnibble
- ; The second nibble
- pop ax
- and al, 0x0f
- call putnibble
- ; Append a space.
- cmp bx, 0
- je .done
- mov al, ' '
- call putchar
- .done:
- ret
- ; Prints the word in AX as four hexadecimal characters (four nibbles) and a
- ; space.
- putword:
- push bx
- ; The first byte.
- push ax
- mov al, ah
- mov bx, 0
- call putbyte
- ; The second byte.
- pop ax
- mov bx, 0
- call putbyte
- ; The space.
- mov al, ' '
- call putchar
- pop bx
- ret
- ; The data section.
- device db 0 ; Stores the BIOS device in DL at boot.
- ctr dw 0 ; Counter for functions that need one.
- stage2_chs db 0, 0, 0 ; Stage 2 CHS (for non-LBA HDDs).
- dap: ; Disk address packet.
- .size db 0x10 ; Size of packet (0x10 or 0x18).
- .reserved db 0 ; Reserved.
- .blocks dw 0 ; Number of blocks.
- .bufoffptr dw STAGE2_OFFSET ; Buffer offset address.
- .bufsegptr dw STAGE2_SEGMENT ; Buffer segment address.
- .offset dq 0 ; Initial LBA offset.
- ; The partition table. On floppy disks since they have no partition table we put
- ; the floppy disk boot code here instead. The installer will just skip this code
- ; on hard disks.
- times 446 - ($ - $$) db 0 ; Pad to the 446th byte.
- ; Reads from the FDD.
- read_fdd:
- ; Reset the floppy disk.
- mov ah, 0x00 ; Reset.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- ; Set ES:BX to STAGE2_SEGMENT:STAGE2_OFFSET
- mov bx, STAGE2_SEGMENT
- mov es, bx
- mov bx, STAGE2_OFFSET
- ; Try to read the sectors.
- mov ah, 0x02 ; Read sectors.
- mov al, STAGE2_SECTORS ; Sectors 0 .. 63.
- mov ch, 0 ; Cylinder 0.
- mov cl, 2 ; Sector 2.
- mov dh, 0 ; Head 0.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- ; If there is an error, display it and then try again (up to 3 times)
- cmp ah, 0
- je parse_elf
- inc word [ctr]
- mov al, ah ; AH -> AL.
- call putbyte
- cmp word [ctr], 3
- jl read_fdd ; Try again.
- jmp hang ; Give up.
- ; End of the boot sector.
- times 510 - ($ - $$) db 0 ; Pad to the 510th byte.
- dw 0xAA55 ; Add the boot signature.
Advertisement
Add Comment
Please, Sign In to add comment