1 #include "threads/loader.h"
5 #### This code should be stored in the first sector of a hard disk.
6 #### When the BIOS runs, it loads this code at physical address
7 #### 0x7c00-0x7e00 (512 bytes) and jumps to the beginning of it,
8 #### in real mode. The loader loads the kernel into memory and jumps
9 #### to its entry point, which is the start function in start.S.
11 #### The BIOS passes in the drive that the loader was read from as
12 #### DL, with floppy drives numbered 0x00, 0x01, ... and hard drives
13 #### numbered 0x80, 0x81, ... We want to support booting a kernel on
14 #### a different drive from the loader, so we don't take advantage of
17 # Runs in real mode, which is a 16-bit segment.
20 # Set up segment registers.
21 # Set stack to grow downward from 60 kB (after boot, the kernel
22 # continues to use this stack for its initial thread).
29 # Configure serial port so we can report progress without connected VGA.
30 # See [IntrList] for details.
31 sub %dx, %dx # Serial port 0.
32 mov $0xe3, %al # 9600 bps, N-8-1.
33 # AH is already 0 (Initialize Port).
34 int $0x14 # Destroys AX.
39 #### Read the partition table on each system hard disk and scan for a
40 #### partition of type 0x20, which is the type that we use for a
43 #### Read [Partitions] for a description of the partition table format
46 #### We print out status messages to show the disk and partition being
47 #### scanned, e.g. hda1234 as we scan four partitions on the first
50 mov $0x80, %dl # Hard disk 0.
52 sub %ebx, %ebx # Sector 0.
53 mov $0x2000, %ax # Use 0x20000 for buffer.
65 # Check for MBR signature--if not present, it's not a
66 # partitioned hard disk.
70 mov $446, %si # Offset of partition table entry 1.
73 # Is it an unused partition?
80 # Is it a Pintos kernel partition?
81 cmpb $0x20, %es:4(%si)
84 # Is it a bootable partition?
89 # No match for this partition, go on to the next one.
90 add $16, %si # Offset to next partition table entry.
96 # No match on this drive, go on to the next one.
102 # Didn't find a Pintos kernel partition anywhere, give up.
104 .string "\rNot found\r"
106 # Notify BIOS that boot failed. See [IntrList].
109 #### We found a kernel. The kernel's drive is in DL. The partition
110 #### table entry for the kernel's partition is at ES:SI. Our job now
111 #### is to read the kernel from disk and jump to its start address.
117 # Figure out number of sectors to read. A Pintos kernel is
118 # just an ELF format object, which doesn't have an
119 # easy-to-read field to identify its own size (see [ELF1]).
120 # But we limit Pintos kernels to 512 kB for other reasons, so
121 # it's easy enough to just read the entire contents of the
122 # partition or 512 kB from disk, whichever is smaller.
123 mov %es:12(%si), %ecx # EBP = number of sectors
124 cmp $1024, %ecx # Cap size at 512 kB
129 mov %es:8(%si), %ebx # EBX = first sector
130 mov $0x2000, %ax # Start load address: 0x20000
133 # Read one sector into memory.
134 mov %ax, %es # ES:0000 -> load address
138 # Print '.' as progress indicator once every 16 sectors == 8 kB.
145 # Advance memory pointer and disk sector.
153 #### Transfer control to the kernel that we loaded. We read the start
154 #### address out of the ELF header (see [ELF1]) and convert it from a
155 #### 32-bit linear address into a 16:16 segment:offset address for
156 #### real mode, then jump to the converted address. The 80x86 doesn't
157 #### have an instruction to jump to an absolute segment:offset kept in
158 #### registers, so in fact we store the address in a temporary memory
159 #### location, then jump indirectly through that location. To save 4
160 #### bytes in the loader, we reuse 4 bytes of the loader's code for
161 #### this temporary pointer.
167 movw $0x2000, start + 2
172 # Disk sector read failed.
174 1: .string "\rBad read\r"
176 # Notify BIOS that boot failed. See [IntrList].
179 #### Print string subroutine. To save space in the loader, this
180 #### subroutine takes its null-terminated string argument from the
181 #### code stream just after the call, and then returns to the byte
182 #### just after the terminating null. This subroutine preserves all
183 #### general-purpose registers.
185 puts: xchg %si, %ss:(%esp)
198 #### Character output subroutine. Prints the character in AL to the
199 #### VGA display and serial port 0, using BIOS services (see
200 #### [IntrList]). Preserves all general-purpose registers.
202 #### If called upon to output a carriage return, this subroutine
203 #### automatically supplies the following line feed.
207 1: sub %bh, %bh # Page 0.
208 mov $0x0e, %ah # Teletype output service.
211 mov $0x01, %ah # Serial port output service.
212 sub %dx, %dx # Serial port 0.
213 2: int $0x14 # Destroys AH.
214 test $0x80, %ah # Output timed out?
224 #### Sector read subroutine. Takes a drive number in DL (0x80 = hard
225 #### disk 0, 0x81 = hard disk 1, ...) and a sector number in EBX, and
226 #### reads the specified sector into memory at ES:0000. Returns with
227 #### carry set on error, clear otherwise. Preserves all
228 #### general-purpose registers.
233 push %ax # LBA sector number [48:63]
234 push %ax # LBA sector number [32:47]
235 push %ebx # LBA sector number [0:31]
236 push %es # Buffer segment
237 push %ax # Buffer offset (always 0)
238 push $1 # Number of sectors to read
239 push $16 # Packet size
240 mov $0x42, %ah # Extended read
241 mov %sp, %si # DS:SI -> packet
242 int $0x13 # Error code in CF
243 popa # Pop 16 bytes, preserve flags
246 ret # Error code still in CF
248 #### Command-line arguments and their count.
249 #### This is written by the `pintos' utility and read by the kernel.
250 #### The loader itself does not do anything with the command line.
251 .org LOADER_ARG_CNT - LOADER_BASE
252 .fill LOADER_ARG_CNT_LEN, 1, 0
254 .org LOADER_ARGS - LOADER_BASE
255 .fill LOADER_ARGS_LEN, 1, 0
257 #### Partition table.
258 .org LOADER_PARTS - LOADER_BASE
259 .fill LOADER_PARTS_LEN, 1, 0
261 #### Boot-sector signature for BIOS inspection.
262 .org LOADER_SIG - LOADER_BASE