-#include "mmu.h"
-#include "loader.h"
-
-##############################################################################
-# Kernel loader.
-#
-# This code should be stored in the first sector of the hard disk. When the
-# BIOS runs, it loads this code at physical address 0x7c00-0x7e00 (512 bytes).
-# Then it jumps to the beginning of it, in real mode.
-# This code switches into protected mode (32-bit mode) so that all of
-# memory can accessed, loads the kernel into memory, and jumps to the
-# first byte of the kernel, where start.S is linked.
-##############################################################################
-
-.globl start # Entry point
-start: .code16 # This runs in real mode
- cli # Disable interrupts
- cld # String ops inc
- xorw %ax,%ax # Zero
- movw %ax,%es # Address
- movw %ax,%ds # data
- movw %ax,%ss # Set up
- movw $start,%sp # stack (grows down)
-
-#### Enable A20:
-#### Address line 20 is tied to low when the machine boots,
-#### obviously this a bit of a drag, such as when trying to
-#### address memory above 1MB. This code undoes this.
-
-1: inb $0x64,%al # Get status
- testb $0x2,%al # Busy?
- jnz 1b # Yes
- movb $0xd1,%al # Command: Write
- outb %al,$0x64 # output port
-2: inb $0x64,%al # Get status
- testb $0x2,%al # Busy?
- jnz 2b # Yes
- movb $0xdf,%al # Enable
- outb %al,$0x60 # A20
-
-#### Get memory size, via interrupt 15h function 88h.
-#### Returns CF clear if successful, with AX = (kB of physical memory) - 1024.
-#### This only works for memory sizes <= 65 MB, which should be fine for our purposes.
-
- movb $0x88,%ah
- int $0x15
- jc panic # Carry flag set on error
-2: addl $1024,%eax # Total kB
- shrl $2,%eax # Total 4 kB pages
- movl %eax, ram_pages
-
-#### switch from real to protected mode
-#### The segments in GDT allow all of physical memory to be accessed.
-#### Furthermore, the segments have base addresses of 0, so that the
-#### segment translation is a NOP, ie. virtual addresses are identical to
-#### their physical addresses. With this setup, it appears to this code
-#### that it is running directly on physical memory.
-
- cli # Mandatory since we dont set up an IDT
- lgdt gdtdesc # load GDT -- mandatory in protected mode
- movl %cr0, %eax # turn on protected mode
- orl $CR0_PE, %eax #
- movl %eax, %cr0 #
- ### CPU magic: jump to relocation, flush prefetch queue, and
- ### reload %cs Has the effect of just jmp to the next
- ### instruction, but simultaneous loads CS with
- ### $PROT_MODE_CSEG.
- ljmp $SEL_KCSEG, $protcseg
-
-#### We are in protected mode in a 32-bit segment (hence the .code32)
-protcseg:
- .code32
- movw $SEL_KDSEG, %ax # set up data segment registers
- movw %ax, %ds
- movw %ax, %es
- movw %ax, %fs
- movw %ax, %gs
- movw %ax, %ss
-
-#### Load kernel starting at physical address LOADER_KERN_BASE by
-#### frobbing the IDE controller directly.
-
- movl $1, %ebx
- movl $LOADER_KERN_BASE, %edi
-read_sector:
+#include "threads/loader.h"
+
+#### Kernel loader.
+
+#### This code should be stored in the first sector of a hard disk.
+#### When the BIOS runs, it loads this code at physical address
+#### 0x7c00-0x7e00 (512 bytes) and jumps to the beginning of it,
+#### in real mode. The loader loads the kernel into memory and jumps
+#### to its entry point, which is the start function in start.S.
+####
+#### The BIOS passes in the drive that the loader was read from as
+#### DL, with floppy drives numbered 0x00, 0x01, ... and hard drives
+#### numbered 0x80, 0x81, ... We want to support booting a kernel on
+#### a different drive from the loader, so we don't take advantage of
+#### this.
+
+# Runs in real mode, which is a 16-bit segment.
+ .code16
+
+# Set up segment registers.
+# Set stack to grow downward from 60 kB (after boot, the kernel
+# continues to use this stack for its initial thread).
+
+ sub %ax, %ax
+ mov %ax, %ds
+ mov %ax, %ss
+ mov $0xf000, %esp
+
+# Configure serial port so we can report progress without connected VGA.
+# See [IntrList] for details.
+ sub %dx, %dx # Serial port 0.
+ mov $0xe3, %al # 9600 bps, N-8-1.
+ # AH is already 0 (Initialize Port).
+ int $0x14 # Destroys AX.
+
+ call puts
+ .string "PiLo"
+
+#### Read the partition table on each system hard disk and scan for a
+#### partition of type 0x20, which is the type that we use for a
+#### Pintos kernel.
+####
+#### Read [Partitions] for a description of the partition table format
+#### that we parse.
+####
+#### We print out status messages to show the disk and partition being
+#### scanned, e.g. hda1234 as we scan four partitions on the first
+#### hard disk.
+
+ mov $0x80, %dl # Hard disk 0.
+read_mbr:
+ sub %ebx, %ebx # Sector 0.
+ mov $0x2000, %ax # Use 0x20000 for buffer.
+ mov %ax, %es
+ call read_sector
+ jc no_such_drive
+
+ # Print hd[a-z].
+ call puts
+ .string " hd"
+ mov %dl, %al
+ add $'a' - 0x80, %al
+ call putc
+
+ # Check for MBR signature--if not present, it's not a
+ # partitioned hard disk.
+ cmpw $0xaa55, %es:510
+ jne next_drive
+
+ mov $446, %si # Offset of partition table entry 1.
+ mov $'1', %al
+check_partition:
+ # Is it an unused partition?
+ cmpl $0, %es:(%si)
+ je next_partition
+
+ # Print [1-4].
+ call putc
+
+ # Is it a Pintos kernel partition?
+ cmpb $0x20, %es:4(%si)
+ jne next_partition
+
+ # Is it a bootable partition?
+ cmpb $0x80, %es:(%si)
+ je load_kernel
+
+next_partition:
+ # No match for this partition, go on to the next one.
+ add $16, %si # Offset to next partition table entry.
+ inc %al
+ cmp $510, %si
+ jb check_partition
+
+next_drive:
+ # No match on this drive, go on to the next one.
+ inc %dl
+ jnc read_mbr
+
+no_such_drive:
+no_boot_partition:
+ # Didn't find a Pintos kernel partition anywhere, give up.
+ call puts
+ .string "\rNot found\r"
+
+ # Notify BIOS that boot failed. See [IntrList].
+ int $0x18
+
+#### We found a kernel. The kernel's drive is in DL. The partition
+#### table entry for the kernel's partition is at ES:SI. Our job now
+#### is to read the kernel from disk and jump to its start address.