Guest User

Untitled

a guest
Mar 2nd, 2012
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;
  2.  ; stage1.s - Schwann stage 1. Loads 32 kiB from the bootable partition and
  3.  ; parses it as an ELF executable, then jumps to the ELF's entry point.
  4.  ;
  5.  ; This file is part of the Schwann bootloader and is distributed under the
  6.  ; terms of the X11 license below.
  7.  ;
  8.  ; Copyright (C) 2012 Synon.
  9.  ;
  10.  ; Permission is hereby granted, free of charge, to any person obtaining a copy
  11.  ; of this software and associated documentation files (the "Software"), to deal
  12.  ; in the Software without restriction, including without limitation the rights
  13.  ; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14.  ; copies of the Software, and to permit persons to whom the Software is
  15.  ; furnished to do so, subject to the following conditions:
  16.  ;
  17.  ; The above copyright notice and this permission notice shall be included in
  18.  ; all copies or substantial portions of the Software.
  19.  ;
  20.  ; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21.  ; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22.  ; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23.  ; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24.  ; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25.  ; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26.  ; SOFTWARE.
  27.  ;
  28.  ; For error reporting we usually just print the BIOS error status as two hex
  29.  ; bytes followed by a space (e.g., "00 ").
  30.  ;
  31.  ; However, according to Ralph Brown's Interrupt List (RBIL), not all of the
  32.  ; status bytes are in use. Here is a(n inclusive) list of free ones:
  33.  ;    0x12 - 0x19
  34.  ;    0x21 - 0x30
  35.  ;    0x33 - 0x39
  36.  ;    0x41 - 0x79
  37.  ;    0x81 - 0xA9
  38.  ;    0xAB - 0xAF
  39.  ;    0xB7 - 0xBA
  40.  ;    0xBC - 0xCB
  41.  ;    0xCD - 0xDF
  42.  ;    0xE1 - 0xFE
  43.  ; The ones we use can be found below.
  44. ;;
  45. [bits   16]
  46. [org    0x7c00]
  47.  
  48. ; Error codes.
  49. ENOACTIVE   equ 0x12            ; No active partition.
  50. EELFMAGIC   equ 0x21            ; ELF header error.
  51. ; Stage 2 constants.
  52. STAGE2_SEGMENT  equ 0x0000          ; Stage 2 load segment.
  53. STAGE2_OFFSET   equ 0x1000 - 24     ; Stage 2 load offset.
  54. STAGE2_BYTES    equ 0x8000          ; 32768 bytes / 32 kiB.
  55. STAGE2_SECTORS  equ 54 ; Stage 2 sectors (CHS HDDs).
  56. ; ELF constants.
  57. ELF_MAGIC_0 equ 0x7F
  58. ELF_MAGIC_1 equ 0x45
  59. ELF_MAGIC_2 equ 0x4C
  60. ELF_MAGIC_3 equ 0x46
  61. ; Other constants.
  62. BUFFER_OFFSET   equ 0x1000          ; Drive parameters buffer.
  63.  
  64. ; Set up segments & stack, then save the device ID in DL register.
  65. _start:
  66.     cli             ; Interrupts off.
  67.     mov ax, 0       ; AX = 0.
  68.     mov ds, ax      ; DS = 0.
  69.     mov ss, ax      ; SS = 0.
  70.     mov sp, 0x9c00      ; SP = 0x7c00 + 0x200 = 0x9c00.
  71.     mov [device],   dl  ; [device] = DL (device ID).
  72.     sti             ; Interrupts on.
  73. ; Decide whether we are booting from hard disk (HDD) or floppy (FDD).
  74. is_hdd_or_fdd:
  75.     cmp byte [device],  0x01
  76.     jle read_fdd
  77. ; Decide whether to use LBA or CHS for the hard disk.
  78. hdd_is_lba_or_chs:
  79.     mov ah, 0x41        ; Check extensions.
  80.     mov bx, 0x55aa
  81.     mov dl, [device]
  82.     int 0x13
  83.     and cx, 0       ; CX bitflags bit 0 -> LBA supported?
  84.     cmp cx, 0
  85.     je  read_hdd_chs
  86. ; Read data from the hard disk (LBA).
  87. read_hdd_lba:
  88.     ; Get the drive parameters (INT 0x13/AH=0x48).
  89.     .get_params:
  90.         mov ah, 0x48        ; Extended drive parameters.
  91.         mov si, BUFFER_OFFSET   ; Drive parameters buffer.
  92.         mov dl, [device]    ; BIOS device.
  93.         int 0x13
  94.         cmp ah, 0
  95.         jne .error
  96.     ; Get the LBA of the first block of the active partition.
  97.     .get_active:
  98.         mov bx, 0x7dbe      ; Partition table address.
  99.         mov byte [ctr], 4   ; Only 4 partitions.
  100.         .get_active_loop:
  101.             mov ah, [bx]
  102.             cmp ah, 0x80
  103.             je  .setup_dap
  104.             add bx, 16
  105.             dec byte [ctr]
  106.             cmp byte [ctr], 0
  107.             jg  .get_active_loop
  108.             mov al, ENOACTIVE   ; No bootable partitions.
  109.             call    putbyte
  110.             jmp hang
  111.     ; Set up the Disk Address Packet (DAP).
  112.     .setup_dap:
  113.         ; Store LBA of first block in active partition to the DAP.
  114.         ; This is one quadword (where 1 qword = 8 bytes).
  115.         ; First word.
  116.         mov ax, [bx]
  117.         push    bx
  118.         mov bx, dap.offset
  119.         pop bx
  120.         mov [bx],   ax
  121.         ; Second word.
  122.         mov ax, [bx]
  123.         push    bx
  124.         mov bx, dap.offset + 2
  125.         pop bx
  126.         mov [bx],   ax
  127.         ; Third word.
  128.         mov ax, [bx]
  129.         push    bx
  130.         mov bx, dap.offset + 4
  131.         pop bx
  132.         mov [bx],   ax
  133.         ; Fourth word.
  134.         mov ax, [bx]
  135.         push    bx
  136.         mov bx, dap.offset + 6
  137.         pop bx
  138.         mov [bx],   ax
  139.         ; Calculate the number of blocks to be read.
  140.         mov ax, STAGE2_BYTES    ; Bytes to load.
  141.         mov bx, [si + 0x18] ; Bytes per block from the disk
  142.         div bx          ; parameters.
  143.         mov [dap.blocks],   ax  ; AX = block count.
  144.     ; Use the DAP to read the data (INT 0x13/AX=0x42).
  145.     .lba_read:
  146.         mov ah, 0x42        ; Read sectors (extended).
  147.         mov dl, [device]    ; BIOS device.
  148.         mov si, dap     ; DS:SI = Disk Address Packet.
  149.         int 0x13
  150.         cmp ah, 0
  151.         jne .error
  152.         ; ES:BX = STAGE2_SEGMENT:STAGE2_OFFSET
  153.         mov bx, STAGE2_SEGMENT
  154.         mov es, bx
  155.         mov bx, STAGE2_OFFSET
  156.         jmp parse_elf
  157.     ; Error - print error code and hang.
  158.     .error:
  159.         mov al, ah
  160.         call    putbyte
  161.         jmp hang
  162. ; Read data from the hard disk (CHS).
  163. read_hdd_chs:
  164.     ; Find the CHS of the active partition.
  165.     .get_active:
  166.         mov bx, 0x7dbe      ; Partition table address.
  167.         mov byte [ctr], 4
  168.         .get_active_loop:
  169.             mov ah, [bx]    ; Partition status byte.
  170.             cmp ah, 0x80
  171.             je  .setup_chs  ; Found bootable partition.
  172.             add bx, 16  ; Next entry.
  173.             dec byte [ctr]
  174.             cmp byte [ctr], 0
  175.             jg  .get_active_loop
  176.             ; No bootable partitions.
  177.             mov al, ENOACTIVE
  178.             call    putbyte
  179.             jmp hang
  180.     ; Store the CHS of the active partition's first sector.
  181.     .setup_chs:
  182.         mov ax,     [bx + 0x01]
  183.         mov [stage2_chs],   ax
  184.     ; Read CHS (INT 0x13/AH=0x02).
  185.     .chs_read:
  186.         mov ah, 0x02        ; Read sectors.
  187.         mov al, STAGE2_SECTORS  ; Number of sectors to read.
  188.         mov bx, STAGE2_SEGMENT  ; Stage2 buffer segment address.
  189.         mov es, bx
  190.         mov bx, STAGE2_OFFSET   ; Stage2 buffer offset address.
  191.         mov ch, [stage2_chs + 2]; Cylinder.
  192.         mov cl, [stage2_chs + 1]; Sector.
  193.         mov dh, [stage2_chs + 0]; Head.
  194.         mov dl, [device]    ; BIOS device.
  195.         int 0x13
  196.         cmp ah, 0
  197.         je  parse_elf
  198.     ; Error - print error code and hang.
  199.     .error:
  200.         mov al, ah
  201.         call    putbyte
  202. ; Hang the CPU in an idle loop.
  203. hang:
  204.     cli             ; Interrupts off.
  205.     hlt             ; Wait for interrupt.
  206.     jmp hang            ; Go back to 'hang'.
  207.     ; Technically speaking after the CLI no interrupts should be occurring
  208.     ; unless an error happens, so the JMP after the HLT probably doesn't do
  209.     ; anything in most cases.
  210. ; Check the ELF header.
  211. parse_elf:
  212.     ; Check the ELF magic number.
  213.     .check:
  214.         ; First byte.
  215.         mov ah, [es:bx  + 0]
  216.         mov al, ELF_MAGIC_0
  217.         cmp ah, al
  218.         jne .error
  219.         ; Second byte.
  220.         mov ah, [es:bx  + 1]
  221.         mov al, ELF_MAGIC_1
  222.         cmp ah, al
  223.         jne .error
  224.         ; Third byte.
  225.         mov ah, [es:bx  + 2]
  226.         mov al, ELF_MAGIC_2
  227.         cmp ah, al
  228.         jne .error
  229.         ; Fourth byte.
  230.         mov ah, [es:bx  + 3]
  231.         mov al, ELF_MAGIC_3
  232.         cmp ah, al
  233.         jne .error
  234.     ; Get the entry point and jump to it.
  235.     .get_elf_entry:
  236.         mov ebx,    [bx + 24]
  237.         mov eax,    0x1BADB002
  238.         jmp [ebx]
  239.     ; Print error code and hang.
  240.     .error:
  241.         mov al, EELFMAGIC
  242.         call    putbyte
  243.         jmp hang
  244. ; Prints the character in AL.
  245. putchar:
  246.     mov ah, 0x0e
  247.     int 0x10
  248.     ret
  249. ; Prints the first nibble in AL.
  250. putnibble:
  251.     cmp al, 10
  252.     jl  .lt10
  253.     add al, 'A' - 10
  254.     call    putchar
  255.     jmp .done
  256.     .lt10:
  257.         add al, '0'
  258.         call    putchar
  259.     .done:
  260.         ret
  261. ; Prints the byte in AL as two hexadecimal characters (two nibbles) and a space.
  262. putbyte:
  263.     ; The first nibble
  264.     push    ax
  265.     shr al, 4
  266.     call    putnibble
  267.     ; The second nibble
  268.     pop ax
  269.     and al, 0x0f
  270.     call    putnibble
  271.     ; Append a space.
  272.     cmp bx, 0
  273.     je  .done
  274.     mov al, ' '
  275.     call    putchar
  276.     .done:
  277.         ret
  278. ; Prints the word in AX as four hexadecimal characters (four nibbles) and a
  279. ; space.
  280. putword:
  281.     push    bx
  282.     ; The first byte.
  283.     push    ax
  284.     mov al, ah
  285.     mov bx, 0
  286.     call    putbyte
  287.     ; The second byte.
  288.     pop ax
  289.     mov bx, 0
  290.     call    putbyte
  291.     ; The space.
  292.     mov al, ' '
  293.     call    putchar
  294.     pop bx
  295.     ret
  296. ; The data section.
  297. device          db  0   ; Stores the BIOS device in DL at boot.
  298. ctr         dw  0   ; Counter for functions that need one.
  299. stage2_chs      db  0, 0, 0 ; Stage 2 CHS (for non-LBA HDDs).
  300. dap:                        ; Disk address packet.
  301.     .size       db  0x10        ; Size of packet (0x10 or 0x18).
  302.     .reserved   db  0       ; Reserved.
  303.     .blocks     dw  0       ; Number of blocks.
  304.     .bufoffptr  dw  STAGE2_OFFSET   ; Buffer offset address.
  305.     .bufsegptr  dw  STAGE2_SEGMENT  ; Buffer segment address.
  306.     .offset     dq  0       ; Initial LBA offset.
  307. ; The partition table. On floppy disks since they have no partition table we put
  308. ; the floppy disk boot code here instead. The installer will just skip this code
  309. ; on hard disks.
  310. times   446 - ($ - $$)  db  0   ; Pad to the 446th byte.
  311. ; Reads from the FDD.
  312. read_fdd:
  313.     ; Reset the floppy disk.
  314.     mov ah, 0x00        ; Reset.
  315.     mov dl, [device]    ; Use the BIOS device.
  316.     int 0x13
  317.     ; Set ES:BX to STAGE2_SEGMENT:STAGE2_OFFSET
  318.     mov bx, STAGE2_SEGMENT
  319.     mov es, bx
  320.     mov bx, STAGE2_OFFSET
  321.     ; Try to read the sectors.
  322.     mov ah, 0x02        ; Read sectors.
  323.     mov al, STAGE2_SECTORS  ; Sectors 0 .. 63.
  324.     mov ch, 0       ; Cylinder 0.
  325.     mov cl, 2       ; Sector 2.
  326.     mov dh, 0       ; Head 0.
  327.     mov dl, [device]    ; Use the BIOS device.
  328.     int 0x13
  329.     ; If there is an error, display it and then try again (up to 3 times)
  330.     cmp ah, 0
  331.     je  parse_elf
  332.     inc word [ctr]
  333.     mov al, ah      ; AH -> AL.
  334.     call    putbyte
  335.     cmp word [ctr], 3
  336.     jl  read_fdd        ; Try again.
  337.     jmp hang            ; Give up.
  338. ; End of the boot sector.
  339. times   510 - ($ - $$)  db  0   ; Pad to the 510th byte.
  340.             dw  0xAA55  ; Add the boot signature.
Advertisement
Add Comment
Please, Sign In to add comment