Part 2:the Boot Loader
For a PC, a floppy disk can be divided into 512 bytes of space, called a sector. A sector is the minimum granularity of a disk operation. Each read or write operation must be one or more sectors. If a disk can be used to start the operating system, the first sector of the disk is called the boot sector. The boot loader program described in this section is located in this boot sector. When the BIOS finds a bootable floppy disk or hard disk, it loads the 512-byte boot sector into the memory address 0X7C00~0X7DFF this area.
For 6.828, we will use the traditional hard drive boot mechanism, which means that our boot loader program must be less than 512 bytes in size. The entire boot loader is made up of a compilation file, Boot/boot. S, as well as a C language file, Boot/main.c. The Boot loader must complete two main functions.
1. First, boot loader to convert the processor from the actual mode to 32bit protection mode, because only in this mode the software can access more than 1MB space content.
2. The boot loader can then access the IDE disk device registers directly from the disk by using x86 's specific IO instructions to read the kernel.
For boot loader, it is important to have a file, obj/boot/boot.asm. This file is the disassembly version of the boot loader program that we are actually running. So we can put it and its source code is boot. S and main.c comparison.
Exercise 3:
Set a breakpoint at address 0x7c00, which is the location where the boot sector is loaded. Then let the program continue running until this breakpoint. Trace/boot/boot. s file, and use boot at the same time. s files and systems for you to disassemble the file obj/boot/boot.asm. You can also use GDB's x/i command to get disassembly instructions for any machine instruction and boot the source file. s files and boot.asm files and the instructions in GDB disassembly are compared.
Trace to the Bootmain function, but also to the Readsect () sub-function. Find and Readsect () the assembly instruction corresponding to each statement of the C language program, go back to Bootmain (), and then find the assembly statement corresponding to the for loop that reads the kernel file from disk to memory. Find out what statement will be executed when the loop ends, set the breakpoint there, continue running to the breakpoint, and run through all the remaining statements.
The answer to this question is in this blog:
Http://www.cnblogs.com/fatsheep9146/p/5115086.html below to answer the four questions raised in the article:1. When did the processor start running in 32bit mode? What exactly is switching the CPU from 16-bit to 32-bit mode of operation? Answer: at boot. s file, the computer works first in real mode, and this is the 16bit operation mode. When the "ljmp $PROT _mode_cseg, $protcseg" statement is completed, it enters the 32-bit mode of operation. The root cause is that the CPU is working in protected mode at this time.2. What is the last statement executed in boot loader? What is the first statement executed after the kernel is loaded into memory? A: The last statement executed by boot loader is the last statement in the Bootmain subroutine ((Void (*) (void)) (elfhdr->e_entry)) (), which jumps to the starting instruction of the operating system kernel program. This first instruction is located in/kern/entry. s file, the first sentence MOVW $0x1234, 0x4723. Where is the first instruction of the kernel? A: This question has been answered in the previous question, and the first instruction is located in/kern/entry. S file.4. How does boot loader know how many sectors it reads to get the entire kernel into memory? Where do you find this information? A: First about how many segments the operating system has, and how many sectors of each segment are in the Program Header table in the operating system file. Each table entry in this table corresponds to a segment of the operating system. and the contents of each table item include information such as the size of the segment, the start address offset of the segment, and so on. So if we can find this table, we can use the information provided by the table entry to determine how many sectors the kernel occupies. The information about where the table is stored is the ELF header information stored in the operating system kernel image file. In the next blog post we continue to welcome your comments and Guidance ~
[email protected]
MIT 6.828 Jos Study Note 4. Lab 1 part 2.1:the Boot Loader