Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;
- ; schwann.1.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 (I didn't want my real name on pastebin.com).
- ;
- ; 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
- ;
- ; And here is a list of the ones we will use and what they mean:
- ; Byte Meaning
- ; 0x12 Error parsing ELF
- ;
- ;;
- %define ELF_ERROR 0x12 ; Error parsing ELF.
- %define STAGE2_SEG_ADDR 0x1000 ; Stage 2 segment address.
- %define STAGE2_OFF_ADDR 0x0000 ; Stage 2 offset address.
- %define STAGE2_BYTES 32768 ; Bytes to load for stage 2 (32 kiB).
- %define STAGE2_SECTORS 64 ; CHS sectors to load (64 sec / 32 kiB).
- %define STAGE2_CHS_SEC 2 ; CHS sector for stage 2 (2nd sector).
- %define STAGE2_LBA_BLK 1 ; LBA block for stage 2 (1st block).
- ; There's no STAGE2_BLOCKS because with LBA we have to calculate the number of
- ; blocks because we can't assume the block size is 512 as with CHS.
- [bits 16]
- [org 0x7c00]
- ;
- ; Here's what we need to do:
- ; 1. Set up a (temporary) stack, save some variables (such as the value of DL)
- ; 2. a) If DL denotes an unpartitioned disk (DL <= 0x01) then we load the 32 kiB
- ; that follow LBA 0 (which will be the block containing this file).
- ; b) If DL denotes a partitioned disk then we check whether LBA is supported.
- ; i) If LBA is supported, use int 0x13/0x42 to load 32 kiB.
- ; ii) If LBA is unsupported, use int 0x13/0x02 to load 32 kiB.
- ; 3. Parse the 32 kiB we parse as an ELF executable.
- ; 4. Jump to the ELF executable's entry point.
- ;
- ; The entry point.
- start:
- cli
- ; Set up segments.
- xor ax, ax
- mov ds, ax
- mov ss, ax
- mov sp, 0x9c00
- ; Save dl.
- mov [device], dl
- sti
- ; Here's where we check the value of the device variable and decide how to load
- ; stage 2.
- branch:
- cmp byte [device], 0x01
- jle is_fdd ; If device <= 0x01 then it's an fdd.
- ; Check whether LBA is supported and decide whether to use LBA or CHS.
- is_hdd:
- mov ah, 0x41
- mov bx, 0x55aa
- mov dl, [device]
- int 0x13
- and cx, 0 ; CX bitflags bit 0 -> LBA supported?
- cmp cx, 0 ; CX = value of bit 0.
- je use_chs ; CX = 0 therefore LBA not supported.
- ; CX =/= 0 therefore LBA is supported.
- ; Use LBA.
- use_lba:
- ; Get the drive parameters.
- mov ah, 0x48
- mov si, 0x1000 ; This is where stage2 will be parse,
- ; but we won't need this data by then.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- cmp ah, 0
- je lba_ctd ; Success.
- ; Error... Print the error bytes and hang the machine.
- mov al, ah
- call puterr
- jmp hang
- lba_ctd:
- ; Find the LBA of the active partition
- mov bx, 0x7dbe ; Address of partition table in memory
- ; (0x7c00 + 446).
- .ptable_loop:
- mov ah, [bx] ; Partition status byte.
- cmp ah, 0x80
- je .bootable_found ; Found bootable partition.
- add bx, 16 ; Next entry.
- jmp .ptable_loop
- .bootable_found:
- mov ax, [bx + 0x0c]
- mov [dap.offset], ax
- ; Get the number of bytes per block
- mov ax, [si + 0x18]
- ; Calculate the number of blocks to read (STAGE2_BYTES / block size).
- push ax
- mov ax, STAGE2_BYTES
- pop bx
- div bx
- mov [dap.block_count], ax
- ; Now we can actually perform the read.
- mov ah, 0x42 ; Extended read.
- mov dl, [device] ; Use the BIOS device.
- mov si, dap ; DS:SI = Disk Address Packet (DAP)
- int 0x13
- cmp ah, 0
- je parse ; Success.
- ; Error... Print the error bytes and hang the machine.
- mov al, ah
- call puterr
- jmp hang
- ; Use CHS - load the sectors using CHS instead of LBA.
- use_chs:
- ; Find the CHS of the active partition.
- mov bx, 0x7dbe ; Address of partition table in memory
- .ptable_loop:
- mov ah, [bx] ; Partition status byte.
- cmp ah, 0x80
- je .bootable_found ; Found bootable partition.
- add bx, 16 ; Next entry.
- jmp .ptable_loop
- .bootable_found:
- mov ax, [bx + 0x01]
- mov [stage2_chs], ax
- ; Reset the device.
- mov ah, 0x00 ; Reset.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- ; Set ES:BX to STAGE2_SEG_ADDR:STAGE2_OFF_ADDR.
- mov bx, STAGE2_SEG_ADDR
- mov es, bx
- xor bx, bx
- ; Try to read the sectors.
- mov ah, 0x02 ; Read sectors.
- mov al, STAGE2_SECTORS ; Sectors 0 .. 63.
- mov ch, [stage2_chs + 0x02] ; Cylinder 0.
- mov cl, [stage2_chs + 0x01] ; Initial sector.
- mov dh, [stage2_chs + 0x00] ; Head 0.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- cmp ah, 0
- je parse
- mov al, ah
- call puterr
- jmp hang
- ; Once the data is loaded to ES:BX, we try to parse it as an ELF executable.
- ; We know the ELF is <= 32 kiB in length and that it starts at ES:BX = 0000:1000
- ; This parser is going to be really basic and not robust at all because of the
- ; size limits (I have 192 bytes left).
- parse:
- ; TODO
- ; Once the ELF executable has been parsed we jump to it.
- jump:
- ; TODO
- ; Should an error occur, we go here and hang to prevent the CPU from executing
- ; garbage.
- hang:
- cli
- .hang:
- hlt
- jmp .hang
- ; Prints the character in AL.
- putchar:
- mov ah, 0x0e
- int 0x10
- ret
- ; Prints the error (byte) in AL as two hexadecimal characters and a space.
- ; This procedure could probably be much shorter and simpler but I'm not that
- ; good with assembly.
- puterr:
- push ax
- ; Zero pad if necessary.
- .pad:
- cmp al, 0x0F
- jg .char
- mov ah, 0x0e
- mov al, '0'
- int 0x10
- ; Write the character itself.
- .char:
- pop ax
- cmp al, 0x0A
- jl .l
- ; >= 0x0A - add 'A'.
- add al, 'A'
- mov ah, 0x0e
- int 0x10
- jmp .space
- ; < 0x0A - add '0'.
- .l:
- add al, '0'
- mov ah, 0x0e
- int 0x10
- ; Append a space.
- .space:
- mov al, ' '
- call putchar
- ret
- ; Don't execute data.
- jmp hang
- ; Data.
- stage2_chs dd 0 ; Stage 2 CHS (for non-LBA HDDs)
- device db 0 ; The BIOS device ID.
- ctr db 3 ; The counter for .*_loop.
- dap:
- .size db 0x10 ; Size of packet (0x10 or 0x18).
- .reserved db 0 ; Reserved.
- .block_count dw 0 ; Number of blocks.
- .bufoffptr dw STAGE2_OFF_ADDR ; Buffer offset address.
- .bufsegptr dw STAGE2_SEG_ADDR ; Buffer segment address.
- .offset dq STAGE2_LBA_BLK ; Initial LBA offset.
- ; Pad with zeros up to the 446th byte.
- times 446 - ($ - $$) db 0
- ; Code for handling the floppy disks. We fit this into the 64 bytes that will be
- ; taken up by the partition table on partitioned devices, since we don't need
- ; floppy disk boot code on partitioned devices. This is basically the same as
- ; use_chs but with more error checking due to the nature of floppy disks.
- is_fdd:
- ; 1. Reset the floppy disk.
- mov ah, 0x00 ; Reset.
- mov dl, [device] ; Use the BIOS device.
- int 0x13
- ; 2. Set ES:BX to STAGE2_SEG_ADDR:STAGE2_OFF_ADDR.
- mov bx, STAGE2_SEG_ADDR
- mov es, bx
- xor bx, bx
- ; 3. 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
- push ax ; Store exit status on stack.
- ; 4. If there is an error, display it and then try again (up to
- ; 3 times)
- ; a) Check for an error.
- cmp ah, 0
- je parse ; Success.
- ; b) Decrement the counter.
- dec byte [ctr]
- ; c) Display the error.
- pop ax ; Exit status in AH.
- mov al, ah ; AH -> AL.
- call puterr
- ; d) If counter <= 0, give up; otherwise, retry.
- cmp byte [ctr], 0
- jle hang ; Give up.
- jmp is_fdd ; Try again.
- ; End of the sector.
- times 510 - ($ - $$) db 0 ; Pad with zeros up to the 510th byte.
- dw 0xaa55 ; Boot signature.
Advertisement
Add Comment
Please, Sign In to add comment