1 /* This file is derived from source code used in MIT's 6.828
2 course. The original copyright notice is reproduced in full
6 * Copyright (C) 1997 Massachusetts Institute of Technology
8 * This software is being provided by the copyright holders under the
9 * following license. By obtaining, using and/or copying this software,
10 * you agree that you have read, understood, and will comply with the
11 * following terms and conditions:
13 * Permission to use, copy, modify, distribute, and sell this software
14 * and its documentation for any purpose and without fee or royalty is
15 * hereby granted, provided that the full text of this NOTICE appears on
16 * ALL copies of the software and documentation or portions thereof,
17 * including modifications, that you make.
19 * THIS SOFTWARE IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. BY WAY OF EXAMPLE,
21 * BUT NOT LIMITATION, COPYRIGHT HOLDERS MAKE NO REPRESENTATIONS OR
22 * WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR
23 * THAT THE USE OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY
24 * THIRD PARTY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS. COPYRIGHT
25 * HOLDERS WILL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE OR
28 * The name and trademarks of copyright holders may NOT be used in
29 * advertising or publicity pertaining to the software without specific,
30 * written prior permission. Title to copyright in this software and any
31 * associated documentation will at all times remain with copyright
32 * holders. See the file AUTHORS which should have accompanied this software
33 * for a list of all copyright holders.
35 * This file may be derived from previously copyrighted software. This
36 * copyright applies only to those changes made by the copyright
37 * holders listed in the AUTHORS file. The rest of this file is covered by
38 * the copyright notices, if any, listed below.
41 #include "threads/loader.h"
43 .intel_syntax noprefix
47 #### This code should be stored in the first sector of the hard disk.
48 #### When the BIOS runs, it loads this code at physical address
49 #### 0x7c00-0x7e00 (512 bytes). Then it jumps to the beginning of it,
50 #### in real mode. This code switches into protected mode (32-bit
51 #### mode) so that all of memory can accessed, loads the kernel into
52 #### memory, and jumps to the first byte of the kernel, where start.S
55 /* Flags in control register 0. */
56 #define CR0_PE 0x00000001 /* Protection Enable. */
57 #define CR0_EM 0x00000004 /* (Floating-point) Emulation. */
58 #define CR0_PG 0x80000000 /* Paging. */
59 #define CR0_WP 0x00010000 /* Write-Protect enable in kernel mode. */
61 # Code runs in real mode, which is a 16-bit segment.
67 # Set up segment registers.
68 # Stack grows downward starting from us.
81 1: call scan_partitions
87 1: call scan_partitions
92 .asciz "No boot partition"
95 # EBX = sector number of partition table
97 # ES:0000 -> buffer for partition table
98 # Returns CF set if drive exists, CF clear otherwise.
101 2: cmp word ptr [es:510], 0xaa55
102 jnz no_boot_partition
107 jz found_boot_partition
109 jz found_extended_partition
111 jz found_extended_partition
113 jz found_extended_partition
115 jz found_extended_partition
128 found_extended_partition:
130 # ES:SI -> partition table entry for extended partition.
131 # Recursively examine it.
139 jmp next_parttbl_entry
141 found_boot_partition:
142 mov ebx, [es:si+8] # EBX = first sector
143 mov cx, [es:si+12] # CX = number of sectors
144 mov ax, 0x1000 # ES:0000 -> load address
155 # es:0000: destination buffer
156 # returns error flag in CF
159 or dl, dl # Floppy drives: DL < 0
160 js read_floppy_sector
164 push eax # LBA sector number [32:63]
165 push ebx # LBA sector number [0:31]
168 push eax # Buffer linear address
169 push 1 # Transfer one sector
170 push 16 # Packet size
171 mov ah, 0x42 # Extended read
172 mov si, sp # DS:SI -> packet
173 int 0x13 # Error code in CF
176 ret # Error code in CF
182 # In: BX = LBA sector number, DL = drive.
183 # Out: BL = drive, DX = cylinder, AL = head, AH = sector.
185 xchg dx, bx # DX = LBA sector number, BL = drive
186 mov cx, HEADS * SECTORS
187 div cx # AX = cyl, DX = hd + (sec-1) * SECTORS
188 xchg ax, dx # DX = cyl, AX = hd + (sec-1) * SECTORS
190 div cl # AL = head, AH = sector - 1
194 mov ch, dl # CH = cylinder
195 mov cl, ah # CL = sector
196 mov dh, al # DH = head
197 mov dl, bl # DL = drive
198 mov ax, 0x0201 # AH = function, AL = sectors to read
199 sub bx, bx # ES:BX -> buffer
205 # Reset floppy drive motor, try again.
211 #### The partition table goes here.
215 #### Boot-sector signature for BIOS inspection.