-# Disable interrupts.
-# String instructions go upward.
-
- cli
- cld
-
-# Set up data segments and stack.
-
- subw %ax, %ax
- movw %ax, %es
- movw %ax, %ds
-
-# Stack grows downward starting from us.
-# We don't ever use the stack so this is strictly speaking
-# unnecessary.
-
- movw %ax, %ss
- movw $0x7c00, %sp
-
-#### Enable A20. Address line 20 is tied to low when the machine
-#### boots, which prevents addressing memory about 1 MB. This code
-#### fixes it.
-
-# Poll status register while busy.
-
-1: inb $0x64, %al
- testb $0x2, %al
- jnz 1b
-
-# Send command for writing output port.
-
- movb $0xd1, %al
- outb %al, $0x64
-
-# Poll status register while busy.
-
-1: inb $0x64, %al
- testb $0x2, %al
- jnz 1b
-
-# Enable A20 line.
-
- movb $0xdf, %al
- outb %al, $0x60
-
-#### 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. We cap memory at 64 MB because that's all we
-#### prepare page tables for, below.
-
- movb $0x88, %ah
- int $0x15
- jc panic
- addl $1024, %eax # Total kB memory
- cmp $0x10000, %eax # Cap at 64 MB
+# 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.
+
+load_kernel:
+ call puts
+ .string "\rLoading"
+
+ # Figure out number of sectors to read. A Pintos kernel is
+ # just an ELF format object, which doesn't have an
+ # easy-to-read field to identify its own size (see [ELF1]).
+ # But we limit Pintos kernels to 512 kB for other reasons, so
+ # it's easy enough to just read the entire contents of the
+ # partition or 512 kB from disk, whichever is smaller.
+ mov %es:12(%si), %ecx # EBP = number of sectors
+ cmp $1024, %ecx # Cap size at 512 kB