Chapter 8 Access and Control of Hard Disk and Graphics Card (1)

Chapter 8 Access and Control of Hard Disk and Graphics Card

This chapter transforms the main boot sector into a program loader, the function is to load the user program and execute the program (hand over the control of the processor to the program)

8.2 Structure of the user program

Segment, segment assembly address, and segment assembly address

The NASM compiler uses the assembly directive "SECTION" or "SEGMENT" to define a segment. The segment is only used to separate different content in the program.
Intel requires that the starting physical address of the port in memory be at least 16 bytes aligned. The physical address must be divisible by 16.
There are also requirements for alignment within the segment. Use "align="

insert image description here

The assembly address of each segment is relative to the beginning (0) of the entire program.

In order to facilitate the assembly address of this segment, you can usesection.段名称.start

When a section definition statement contains vstart=0clauses, the assembly address of the label is calculated from the beginning of the section where it is located, and starts from 0. (i.e. start calculating addresses relative to the current segment)

Header of the user program [External link picture transfer failed, the source site may have an anti-leeching mechanism, it is recommended to save the picture and upload it directly

insert image description here

The user program header must contain at least the following information:
1. The size of the user program.
The loader needs to decide how many logical sectors to read based on this information.
8 lines, the label program_end indicates the length. In the compilation stage, fill in the representative assembly address in here
insert image description here

2. 应用程序的入口点,包括段地址和偏移地址。
11、12行,依次声明并初始化偏移地址和短地址。偏移地址取自代码段code_1标号“start”,段地址用表达式section.code_1.start得到
段地址是用伪指令dd声明的,是32位地址,它仅仅是编译阶段确定的汇编地址,在用户加载到内存后,需要根据加载的实际位置重新计算(浮动)。
3. 段重定位表
程序加载到内存后,每个段的地址必须重新确定。段的重定位是加载器的工作,它需要知道每个段在用户程序内的位置。需要一张段重定位表
声明并初始化段的重定位表的项目数,段重定位表在两个标号之间
即`(header_end - code_1_segment)/4`在编辑阶段计算,面是实际的段重定位表,每个表项用dd声明并初始化为1个双字

8.3 Workflow of the loader (device)

Initialize and decide where to load

For a loader to load a program and start executing it, it needs to decide two things. First, the physical memory address from which the user program is loaded; second, where the user program is located on the hard disk, and what is its starting logical sector number.

​ 8-1, line 6, a constant is declared at the beginning of the loader program, app_lba_start equ 100its function is similar to #define A 100 in C language. Unlike other pseudo-instructions db, dw, and dd, the value declared with equ does not occupy any assembly address, nor does it occupy any memory location at runtime.

phy_base dd 0x10000 ;The physical starting address where the user program is loaded , other locations can also be used.

insert image description here

Ready to load user program

The main boot sector is defined as a segment, lines vstar=0x7c00
8-1, 12-14 are used to initialize the stack segment register SS and the stack pointer SP.
Lines 16 and 17, the starting address is a double-word unit. On a 16-bit processor, only two registers can be used to store it.

insert image description here

Peripherals and their interfaces

The input and output control device concentrator (ICH) chip is used to connect different buses and coordinate the access of each I/O interface to the processor.
insert image description here

The processor is connected to the processing interface circuit inside the ICH through the local bus. Then, inside the ICH, it is connected with each I/O interface through the bus.

I/O ports and port access

Specifically, the processor communicates with peripheral devices through ports. Essentially, a port is just some registers, and the registers of the port are located in the I/O interface circuit.

Some ports are mapped to the memory address space by the port number, and some are independently compiled and have no relationship with the memory. In a stand-alone system, the address of the processor is connected to both the memory and each I/O interface. There is a special pin M/IO#. When the processor accesses the I/O port, the M/IO# pin is high, and the memory-related circuits are turned on; if the processor accesses the I/O port, the M/IO# pin is low, and the memory circuit Prohibited.

The in instruction reads from the port, the destination operand must be the register AL or AX, and the source operand is the register DX. The
out instruction sends data to the peripheral device through the port. The destination operand is an 8-bit immediate value or register DX, and the source operand must be a register AL or AX.

Read sector data through hard disk controller port

The basic unit of hard disk reading and writing is sector, and the data exchange between host and hard disk is in blocks, and hard disk is a typical block device.

Take LBA28 as an example to access the hard disk (28 bits to represent the logical sector number, which can manage a 128GB hard disk). The
main hard disk controller on the personal computer is assigned 8-bit ports, from 0x1f0 to 0x1f7. Suppose you want to read logical sectors from the hard disk now.

  1. Set the number of sectors to read, this number is written to the 0x1f2 port. This is an 8-bit port, intelligently read and write 255 sectors at a time
mov dx,0x1f2
mox al,0x01
out dx,al
  1. Set the starting LBA sector number. Sector reading and writing are continuous, so only the number of the first sector needs to be given. The 28-bit sector number should be divided into 4 segments, which are respectively written into ports 0x1f3 (0 7 bits), 0x1f4 (8 15 bits), 0x1f5 (16~23) and 0x1f6 (the last four bits). Assuming that the logical sector number for reading and writing is actually 0x02, you can write code:
mov dx,0x1f3
mov al,0x02
out dx,al
inc dx
mov al,0x00
out dx,al
inc dx
out dx,al
inc dx
mov al,0xe0
out dx,al

Under the current system, each PATA/SATAinterface allows two hard disks to be attached, namely the master disk and the slave disk.

Please add a picture description

The lower 4 bits of 0x1f6 are used to store 24 to 27 bits, the fourth bit is used to indicate the hard disk number, 0 means the master disk, and 1 means the slave disk. The upper 3 bits are "111", indicating the LBA mode

  1. Write 0x20 to port 0x1f7, request hard disk read
mov dx,0x1f7
mov al,0x20
out dx,al
  1. Wait for the read and write operations to complete. Port 0x1f7 is both a command port and a status port.

insert image description here

During its internal operation, it sets bit 7 of port 0x1f7 to 1,. Once the hard disk system is ready, it will clear this bit, and at the same time the third bit is 1, indicating that it is ready, requesting the host to send or receive data

   mov dx,0x1f7
.waits:
   in al,dx
   and al,0x88  ;10001000 表明我们想保留第7位和第3位,其他无关的清零
   cmp al,0x08 
   jnz .waits
  1. Continuously fetch data. 0x1f0 is the data port of the hard disk interface, and it is a 16-bit access port. Once the hard disk controller is free and ready, data can be continuously written or read from this port.
    Read a sector from hard disk:
	mov cx,256      ;总共要读取的字数
	mov dx,0x1f0
.readw:
	in ax,dx
	mov [bx],ax
	add bx,2
	loop .readw

The 0x1f1 port is the error register, which contains the state of the hard drive since the last command executed (the cause of the error)

Guess you like

Origin blog.csdn.net/weixin_61631200/article/details/129542227