The boot sector of linux (1)

Last time I talked about the program between the operating system power-on and the boot sector, but at the end, I just used a test program to replace the boot program (that is, the program in the first sector). Today we will talk about the real boot program .

But before we talk about the boot program, we need to learn the FAT12 file system first, and then we will understand how the data is stored on the hard disk (note: it is the hard disk, at this time the data has not been loaded into the memory).

All file systems divide the disk into several levels to facilitate organization and management, including
1. Sector: the smallest data unit on the disk
2. Cluster: one or more sectors
3. Partition: usually refers to the entire file system

The boot sector is located in the 0 cylinder (track) of the entire disk and the first sector of the 0 head. In this sector, there is a very important data structure called BPB (BIOS Parameter
Write picture description here
Block ). The above is the internal format of the first sector.

Immediately after the first sector are FAT1 and FAT2, occupying 9 sectors respectively. After FAT2 is the first sector of the root directory area, which is followed by the data area. The entire file system is as follows
Write picture description here

Floppy disks (replace hard disks and U disks with the current era) are used to store data, which is simply to read and write data. Reading and writing are just a question of flag bits (permissions, not shown for the time being). Their common point is to find data, and data exists in the form of files, so the main responsibility of the entire file system is to find files , read and write files

First, analyze the root directory. It is located after FAT2. The starting sector is 19. It consists of several directory entries. The maximum number of entries is BPB_RootEntCnt (this data is in the boot sector), so the length is not fixed. Each entry in the root directory area occupies 32 bytes , the format is as follows

name Offset length description
DIR name 0 0xB Length name is 8 bytes, extension name is 3 bytes
DIR-Attr 0xB 1 File attributes
Reserved bit 0xC 10 Reserved bit
DIR_WrtTime 0x16 2 Last write time
DIR_WrtDate 0x18 2 Last write date
DIR_FstClus 0x1A 2 This entry corresponds to the starting cluster number
DIR_FileSize 0x1C 4 File size

By visiting the root directory, you can get the file name under the root directory, as well as the file attributes, file address, and the last read and write date.

The attribute represents the read and write permission of the file.
Cluster number lookup files from the beginning of the beginning
This completes the major task of the file system, find the file, read and write files.

Since the size of the root directory area is uncertain, so we need to calculate a first data region in the first several sectors
assumed root directory area occupy a total RootDirSectors = [(BPB_RootEntCnt * 32) + (BPB_BytsPerSec-1) ]/BPB_BytsPerSec is
to add BPB_BytsPerSec-1 to ensure that this formula can still be established when the root directory area cannot fill the entire sector.
Start sector number of data area = start sector number of root directory +14

Next, let's talk about the functions of FAT1 and FAT2. As mentioned above, data can be found directly through some formulas. Isn't the function of FAT redundant?

In fact, this is not the case. The method described above can only access files smaller than 512 bytes. If the file is larger than one sector, FAT is required at this time.

There are two FATs. FAT2 can be regarded as a backup of FAT1. They are usually the same.

In FAT, every 12 bits is called a FAT entry, which represents a cluster. The zeroth and first FAT items are not always used. Starting from the second FAT item, each cluster in the data area is indicated. That is to say, the second FAT item indicates the first cluster in the data area. This corresponds to the first cluster number of the data area mentioned above, which is 2.

Usually the value of the FAT item represents the next cluster number of the file , but if the value is greater than or equal to 0xFF8, it means that the current cluster is already the last cluster in the file, and if the value is 0xFF7, it means it is a bad cluster.

The process of accessing a file is as follows. First, look up the file name through the root directory file to determine which entry it is, then access DIR_FstClus in the entry, which has the corresponding starting cluster number. If the file is larger than 512 bytes, it needs to be divided into several sectors. Area to store data. After the first sector number is accessed, the next cluster number can be accessed through FAT, which corresponds to the next sector. In this way, the principle of file access is realized.

The principle of the FAT12 file system is finished. Now let’s implement it in detail. First, fill in the data. If we have reinstalled the operating system with a USB flash drive, we should know that this is the first code to boot from the USB flash drive. The function is naturally to know the relevant information of this disk, and prepare for the future to read data from the disk. After all, the operating system is still not loaded in the disk.

In this code, I continue to be lazy, just let the bootloader work, just print a string, and write the boot code in the next chapter (if I forget it, I won’t write it).

;======================================
;boot.asm
;编译方法:nasm boot.asm -o boot.bin
;======================================
;%define    _BOOT_DEBUG_  ;做Boot Sector时要注释掉 

%ifdef  _BOOT_DEBUG_
    org 0100h
%else
    org 07c00h      ;Boot状态 bios将把boot sector加载到0:07c00处开始执行
%endif

    jmp short   LABEL_START
    nop

    ; 下面是 FAT12 磁盘的头
    BS_OEMName  DB 'ForrestY'   ; OEM String, 必须 8 个字节
    BPB_BytsPerSec  DW 512      ; 每扇区字节数
    BPB_SecPerClus  DB 1        ; 每簇多少扇区
    BPB_RsvdSecCnt  DW 1        ; Boot 记录占用多少扇区
    BPB_NumFATs DB 2        ; 共有多少 FAT 表
    BPB_RootEntCnt  DW 224      ; 根目录文件数最大值
    BPB_TotSec16    DW 2880     ; 逻辑扇区总数
    BPB_Media   DB 0xF0     ; 媒体描述符
    BPB_FATSz16 DW 9        ; 每FAT扇区数
    BPB_SecPerTrk   DW 18       ; 每磁道扇区数
    BPB_NumHeads    DW 2        ; 磁头数(面数)
    BPB_HiddSec DD 0        ; 隐藏扇区数
    BPB_TotSec32    DD 0        ; wTotalSectorCount为0时这个值记录扇区数
    BS_DrvNum   DB 0        ; 中断 13 的驱动器号
    BS_Reserved1    DB 0        ; 未使用
    BS_BootSig  DB 29h      ; 扩展引导标记 (29h)
    BS_VolID    DD 0        ; 卷序列号
    BS_VolLab   DB 'OrangeS0.02'; 卷标, 必须 11 个字节
    BS_FileSysType  DB 'FAT12   '   ; 文件系统类型, 必须 8个字节  


LABEL_START:
    mov ax, cs
    mov ds, ax
    mov es, ax
    Call    DispStr         ; 调用显示字符串例程
    jmp $          ; 无限循环

DispStr:
    mov ax, BootMessage
    mov bp, ax          ; ES:BP = 串地址
    mov cx, 16          ; CX = 串长度
    mov ax, 01301h      ; AH = 13,  AL = 01h
    mov bx, 000ch       ; 页号为0(BH = 0) 黑底红字(BL = 0Ch,高亮)
    mov dl, 0
    int 10h         ; int 10h
    ret

BootMessage:        db  "Hello, OS world!"
times   510-($-$$)   db  0   ; 填充剩下的空间,使生成的二进制代码恰好为512字节
dw  0xaa55              ; 结束标志

After writing this code into the boot sector of the disk, the floppy disk has been recognized by DOS and Linux, and you can easily add or delete programs on it.

Guess you like

Origin blog.csdn.net/u012323667/article/details/79522783