Guest User

Full code

a guest
Feb 27th, 2012
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;;
  2.  ; schwann.1.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 (I didn't want my real name on pastebin.com).
  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.  ;
  44.  ; And here is a list of the ones we will use and what they mean:
  45.  ;    Byte  Meaning
  46.  ;    0x12  Error parsing ELF
  47.  ;
  48. ;;
  49.  
  50. %define ELF_ERROR   0x12    ; Error parsing ELF.
  51. %define STAGE2_SEG_ADDR 0x1000  ; Stage 2 segment address.
  52. %define STAGE2_OFF_ADDR 0x0000  ; Stage 2 offset address.
  53. %define STAGE2_BYTES    32768   ; Bytes to load for stage 2 (32 kiB).
  54. %define STAGE2_SECTORS  64  ; CHS sectors to load (64 sec / 32 kiB).
  55. %define STAGE2_CHS_SEC  2   ; CHS sector for stage 2 (2nd sector).
  56. %define STAGE2_LBA_BLK  1   ; LBA block for stage 2 (1st block).
  57. ; There's no STAGE2_BLOCKS because with LBA we have to calculate the number of
  58. ; blocks because we can't assume the block size is 512 as with CHS.
  59.  
  60. [bits   16]
  61. [org    0x7c00]
  62.  
  63. ;
  64. ; Here's what we need to do:
  65. ; 1. Set up a (temporary) stack, save some variables (such as the value of DL)
  66. ; 2. a) If DL denotes an unpartitioned disk (DL <= 0x01) then we load the 32 kiB
  67. ;       that follow LBA 0 (which will be the block containing this file).
  68. ;    b) If DL denotes a partitioned disk then we check whether LBA is supported.
  69. ;       i) If LBA is supported, use int 0x13/0x42 to load 32 kiB.
  70. ;      ii) If LBA is unsupported, use int 0x13/0x02 to load 32 kiB.
  71. ; 3. Parse the 32 kiB we parse as an ELF executable.
  72. ; 4. Jump to the ELF executable's entry point.
  73. ;
  74.  
  75. ; The entry point.
  76. start:
  77.     cli
  78.     ; Set up segments.
  79.     xor ax, ax
  80.     mov ds, ax
  81.     mov ss, ax
  82.     mov sp, 0x9c00
  83.     ; Save dl.
  84.     mov [device],   dl
  85.     sti
  86. ; Here's where we check the value of the device variable and decide how to load
  87. ; stage 2.
  88. branch:
  89.     cmp byte [device],  0x01
  90.     jle is_fdd          ; If device <= 0x01 then it's an fdd.
  91. ; Check whether LBA is supported and decide whether to use LBA or CHS.
  92. is_hdd:
  93.     mov ah, 0x41
  94.     mov bx, 0x55aa
  95.     mov dl, [device]
  96.     int 0x13
  97.     and cx, 0   ; CX bitflags bit 0 -> LBA supported?
  98.     cmp cx, 0   ; CX = value of bit 0.
  99.     je  use_chs     ; CX = 0 therefore LBA not supported.
  100.     ; CX =/= 0 therefore LBA is supported.
  101. ; Use LBA.
  102. use_lba:
  103.     ; Get the drive parameters.
  104.     mov ah, 0x48
  105.     mov si, 0x1000      ; This is where stage2 will be parse,
  106.                     ; but we won't need this data by then.
  107.     mov dl, [device]    ; Use the BIOS device.
  108.     int 0x13
  109.     cmp ah, 0
  110.     je  lba_ctd         ; Success.
  111.     ; Error... Print the error bytes and hang the machine.
  112.     mov al, ah
  113.     call    puterr
  114.     jmp hang
  115. lba_ctd:
  116.     ; Find the LBA of the active partition
  117.     mov bx, 0x7dbe      ; Address of partition table in memory
  118.                     ; (0x7c00 + 446).
  119.     .ptable_loop:
  120.         mov ah, [bx]    ; Partition status byte.
  121.         cmp ah, 0x80
  122.         je  .bootable_found ; Found bootable partition.
  123.         add bx, 16  ; Next entry.
  124.         jmp .ptable_loop
  125.     .bootable_found:
  126.         mov ax,     [bx + 0x0c]
  127.         mov [dap.offset],   ax
  128.     ; Get the number of bytes per block
  129.     mov ax, [si + 0x18]
  130.     ; Calculate the number of blocks to read (STAGE2_BYTES / block size).
  131.     push    ax
  132.     mov ax, STAGE2_BYTES
  133.     pop bx
  134.     div bx
  135.     mov [dap.block_count],  ax
  136.     ; Now we can actually perform the read.
  137.     mov ah, 0x42        ; Extended read.
  138.     mov dl, [device]    ; Use the BIOS device.
  139.     mov si, dap     ; DS:SI = Disk Address Packet (DAP)
  140.     int 0x13
  141.     cmp ah, 0
  142.     je  parse           ; Success.
  143.     ; Error... Print the error bytes and hang the machine.
  144.     mov al, ah
  145.     call    puterr
  146.     jmp hang
  147. ; Use CHS - load the sectors using CHS instead of LBA.
  148. use_chs:
  149.     ; Find the CHS of the active partition.
  150.     mov bx, 0x7dbe      ; Address of partition table in memory
  151.     .ptable_loop:
  152.         mov ah, [bx]    ; Partition status byte.
  153.         cmp ah, 0x80
  154.         je  .bootable_found ; Found bootable partition.
  155.         add bx, 16  ; Next entry.
  156.         jmp .ptable_loop
  157.     .bootable_found:
  158.         mov ax,     [bx + 0x01]
  159.         mov [stage2_chs],   ax
  160.     ; Reset the device.
  161.     mov ah, 0x00        ; Reset.
  162.     mov dl, [device]    ; Use the BIOS device.
  163.     int 0x13
  164.     ; Set ES:BX to STAGE2_SEG_ADDR:STAGE2_OFF_ADDR.
  165.     mov bx, STAGE2_SEG_ADDR
  166.     mov es, bx
  167.     xor bx, bx
  168.     ; Try to read the sectors.
  169.     mov ah, 0x02            ; Read sectors.
  170.     mov al, STAGE2_SECTORS      ; Sectors 0 .. 63.
  171.     mov ch, [stage2_chs + 0x02] ; Cylinder 0.
  172.     mov cl, [stage2_chs + 0x01] ; Initial sector.
  173.     mov dh, [stage2_chs + 0x00] ; Head 0.
  174.     mov dl, [device]        ; Use the BIOS device.
  175.     int 0x13
  176.     cmp ah, 0
  177.     je  parse
  178.     mov al, ah
  179.     call    puterr
  180.     jmp hang
  181. ; Once the data is loaded to ES:BX, we try to parse it as an ELF executable.
  182. ; We know the ELF is <= 32 kiB in length and that it starts at ES:BX = 0000:1000
  183. ; This parser is going to be really basic and not robust at all because of the
  184. ; size limits (I have 192 bytes left).
  185. parse:
  186.     ; TODO
  187. ; Once the ELF executable has been parsed we jump to it.
  188. jump:
  189.     ; TODO
  190. ; Should an error occur, we go here and hang to prevent the CPU from executing
  191. ; garbage.
  192. hang:
  193.     cli
  194.     .hang:
  195.         hlt
  196.         jmp .hang
  197. ; Prints the character in AL.
  198. putchar:
  199.     mov ah, 0x0e
  200.     int 0x10
  201.     ret
  202. ; Prints the error (byte) in AL as two hexadecimal characters and a space.
  203. ; This procedure could probably be much shorter and simpler but I'm not that
  204. ; good with assembly.
  205. puterr:
  206.     push ax
  207.     ; Zero pad if necessary.
  208.     .pad:
  209.         cmp al, 0x0F
  210.         jg  .char
  211.         mov ah, 0x0e
  212.         mov al, '0'
  213.         int 0x10
  214.     ; Write the character itself.
  215.     .char:
  216.         pop ax
  217.         cmp al, 0x0A
  218.         jl  .l
  219.         ; >= 0x0A - add 'A'.
  220.         add al, 'A'
  221.         mov ah, 0x0e
  222.         int 0x10
  223.         jmp .space
  224.         ; < 0x0A - add '0'.
  225.         .l:
  226.             add al, '0'
  227.             mov ah, 0x0e
  228.             int 0x10
  229.     ; Append a space.
  230.     .space:
  231.         mov al, ' '
  232.         call    putchar
  233.     ret
  234. ; Don't execute data.
  235. jmp hang
  236. ; Data.
  237. stage2_chs      dd  0       ; Stage 2 CHS (for non-LBA HDDs)
  238. device          db  0       ; The BIOS device ID.
  239. ctr         db  3       ; The counter for .*_loop.
  240. dap:
  241.     .size       db  0x10        ; Size of packet (0x10 or 0x18).
  242.     .reserved   db  0       ; Reserved.
  243.     .block_count    dw  0       ; Number of blocks.
  244.     .bufoffptr  dw  STAGE2_OFF_ADDR ; Buffer offset address.
  245.     .bufsegptr  dw  STAGE2_SEG_ADDR ; Buffer segment address.
  246.     .offset     dq  STAGE2_LBA_BLK  ; Initial LBA offset.
  247.  
  248.  
  249. ; Pad with zeros up to the 446th byte.
  250. times   446 - ($ - $$)  db  0
  251. ; Code for handling the floppy disks. We fit this into the 64 bytes that will be
  252. ; taken up by the partition table on partitioned devices, since we don't need
  253. ; floppy disk boot code on partitioned devices. This is basically the same as
  254. ; use_chs but with more error checking due to the nature of floppy disks.
  255. is_fdd:
  256.     ; 1. Reset the floppy disk.
  257.     mov ah, 0x00        ; Reset.
  258.     mov dl, [device]    ; Use the BIOS device.
  259.     int 0x13
  260.     ; 2. Set ES:BX to STAGE2_SEG_ADDR:STAGE2_OFF_ADDR.
  261.     mov bx, STAGE2_SEG_ADDR
  262.     mov es, bx
  263.     xor bx, bx
  264.     ; 3. Try to read the sectors.
  265.     mov ah, 0x02        ; Read sectors.
  266.     mov al, STAGE2_SECTORS  ; Sectors 0 .. 63.
  267.     mov ch, 0       ; Cylinder 0.
  268.     mov cl, 2       ; Sector 2.
  269.     mov dh, 0       ; Head 0.
  270.     mov dl, [device]    ; Use the BIOS device.
  271.     int 0x13
  272.     push    ax          ; Store exit status on stack.
  273.     ; 4. If there is an error, display it and then try again (up to
  274.     ;    3 times)
  275.     ;    a) Check for an error.
  276.     cmp ah, 0
  277.     je  parse           ; Success.
  278.     ;    b) Decrement the counter.
  279.     dec byte [ctr]
  280.     ;    c) Display the error.
  281.     pop ax          ; Exit status in AH.
  282.     mov al, ah      ; AH -> AL.
  283.     call    puterr
  284.     ;    d) If counter <= 0, give up; otherwise, retry.
  285.     cmp byte [ctr], 0
  286.     jle hang            ; Give up.
  287.     jmp is_fdd          ; Try again.
  288. ; End of the sector.
  289. times   510 - ($ - $$)  db  0   ; Pad with zeros up to the 510th byte.
  290.             dw  0xaa55  ; Boot signature.
Advertisement
Add Comment
Please, Sign In to add comment