An operating system implementation (11)-Put the operating system into protected mode

Source: Internet
Author: User

This section first introduced the breakthrough boot sector only 512 bytes of the principle, and then introduced the FAT12 file system, finally through the experiment Load loader and give control to loader to achieve break through 512 bytes of bondage.
512-byte limit exceeded

The boot sector used in the preceding section is only 512 bytes. However, there are actually a lot of things that the operating system needs to do during the startup process. So there is a way to break the 512-byte limit.

So how do you break the 512-byte limit? One way is to create a file, load it into memory through the boot sector, and then teach it the control. In this way, the 512-byte binding is gone.

This is not the kernel of the operating system where the boot sector is loaded into memory. Since booting to start, the operating system has undergone a process such as "boot → load kernel into memory → jump into protected mode → start executing kernel". That is, before the kernel starts to execute, not only does it load the kernel, but it also has a series of work to prepare for protection mode, and if all is given to the boot sector, 512 bytes is probably not enough. Therefore, it is not the kernel that loads into the memory, but another module called loader. The boot sector loads the loader into memory and gives it control. All the other work mentioned above is given to loader. Loader does not have a 512-byte limit. So it's a lot more flexible.

The next most important thing is how to find the loader file and load it into memory. First introduce the FAT12 file system FAT12

The full name of fat is file Allocation Table. It is the file system that started using the DOS era, and the file system is still used on floppy disks. Fat divides the disk into layers to facilitate organization and management, as follows: Sector (Sector): the smallest data unit on disk. Cluster (Cluster): one or more sectors. Partition (Partition): usually refers to the entire file system.

The following is the structure of the floppy disk in the FAT12 format:

Boot Sector

The first is a boot sector, which is located in the No. 0 sector. It's structured as shown below

The boot sector has a very important data structure called BPB (BIOS Parameterblock), which begins with Bpb_. Fields beginning with Bs_ do not belong to BPB, but are only part of the boot sector (boot Sector). FAT

You can see that there are two fat tables, FAT2 can be seen as FAT1 backups, they are usually the same. Fat is a bit like a bitmap. Each 12-bit is called a fat item (fatentry), which represents a cluster.

Typically, the value of a FAT item represents the next cluster number of the file. From here you can calculate the maximum cluster number of the data area in the FAT12 is 2^12=4k, if each cluster is 512 bytes, then the maximum amount of data is 4KX512B=2MB

When the value of a fat table entry is greater than or equal to 0XFF8, the current cluster is already the last cluster of the file. If the value is 0XFF7, it indicates that it is a bad cluster.

Where the No. 0 and 1th fat items are never used, starting with the 2nd Fat entry represents each cluster in the data area. That is, the second FAT item represents the first cluster in the data area, so the first cluster number in the data area is 2. root directory Area

After the root area is located in the second Fat table, the starting sector area code is 19, which consists of several directory entries (directory Entry) with a maximum of bpb_rootentcnt entries. Because the size of the root area is dependent on bpb_rootentcnt, the length is not fixed.

Each entry in the root area occupies 32 bytes, in the following format:

The root area primarily defines the name, attribute, time, start cluster number, and size. Data Area

The cluster number for the data area starts at 2. This is because the FAT table entries mentioned above start with the second one. Because the root directory area length is not fixed. So you need to calculate the location of the first cluster number in the data area. How to read a file

The first step is to go to the root directory to find the file based on the file name and attributes. After locating the file directory entry, read the first cluster of files according to the starting cluster number in the directory, and then look at the Fat table entries to find the next cluster number of the file. If less than 0XFF7, the data is not read, and if greater than or equal to 0XFF8, the end of the file read

Next, implement one of the simplest loader and implement the loading process. The main steps are as follows: making a DOS-aware boot disk

The boot sector needs to have BPB and other header information to be recognized by Microsoft, we first add it, the code is roughly as follows:

Now the floppy disk has been able to be recognized by DOS and Linux, we can easily add or remove files. Write a simple loader program

To load loader into memory, you first need to have a loader. So the next step is to write the simplest loader, the code is as follows:

Save this code in a large loader.asm file. This code is compiled into a. The COM file executes directly under DOS, and the effect is to output the character L in the center of the screen and then into the dead loop. Here, we compile with the following command line:

The compiled binary code is loaded into the memory anywhere and can be executed correctly, but we want to extend it, for future execution will not have a problem, make sure to put it in a paragraph offset 0x100 position. load Loader into memory Int 13h

Load a file on the floppy disk into memory, using the BIOS interrupt int 13h. It uses the following diagram:

As can be seen from the above figure, the interrupt requires a parameter other than the sector code starting from the No. 0 sector, but the cylinder number, the number of magnets, and the sector area code three components on the current cylinder. Therefore, the following diagram method to convert: floppy disk relative to the sector area code conversion

The principle of conversion is as follows:

First, the 1.44M floppy disk structure: A floppy disk consists of 2 disks (0 and 1), each with 80 tracks (magnetic column), each track has 18 sectors, each sector size bit 512Byte. So total capacity: 2X80X18X512BYTE=1474569BYTE=1.44MB

Then, starting with the No. 0 sector, the number is called the relative sector, and its relationship to the physical location is as follows:

0 sides, 0 lanes, 1 sectors             0 
0 sides, 0 lanes, 2 sectors             1 
0 faces, 0 lanes, 3 sectors             2 
...
0-side, 0-channel, 18-sector- 
1-side, 0-channel, 1-sector 
...
1 sides, 0 lanes, 18 sectors 
0 sides, 1 lanes, 1 sectors ...
0-side, 1-channel, 18-sector 
1-side, 1-channel, 1-Sector            54
read floppy disk sector

Because the loader may contain more than one sector, then write a function to read the floppy disk sector:

The above code uses the stack, so the program begins with the SS and ESP initialized:

Read the function of the sector is written, and then start looking for Loader.bin in the floppy disk to find loader

Mainly includes two search: the first sector in the root directory to look for loader in the fat table looking for the rest of the loader sector root area to look for Loader.bin

The logical process for the above code is to traverse all sectors of the root area, load each sector into memory, and then look for an entry with the file name Loader.bin, which will guide you to find it. At the moment of finding, Es:di is the character that points to the letter N of the entry. Some of these macros are defined as follows:

Some of the values for variables and strings are defined as follows:

Some characters are printed during the reading, and the function for printing the string is as follows:

The first sector of the loader is found, then the remaining sectors of the loader are searched for the next sector area code in the Fat table entries. find the value of a fat item by sector code

The job of looking for loader is finished, then loading loader:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.