Dingus hard drive (simplified)

Source: Internet
Author: User

If you search for a Linux hard drive on the Internet, you can find a lot of relevant information, but no specific code. After some efforts, I realized the hard disk read and write code. I 'd like to share it with you. This code may fail to be used in some new laptops due to disk controller updates. But it can be used on Vmware. Git address of this project: git: // code.csdn.net/hanjianqiao/dingus.git. The image directly compiled in commit for "add harddisk" is the following.


The dense Hex data is obtained from the first sector of the hard disk, which has been written to the hard disk before. The last 55 AA is a sector tag that everyone is familiar.

I wanted to use a picture to express the process of the hard drive, but I really didn't think how to draw this picture better. I just used plain text:

Hard Disk Writing:

1. Output Control Data to the hard drive control port, and then send the write command 0x30

2. Wait until the hard disk controller is ready (available in polling mode) for the next step.

3. Write 256 words (dubyte) data to the data port 0x1f0 in sequence

4. After one round robin, the interruption mode waits until hard disk write data is completed.


Read Hard Disk:

1. Wait until the hard disk controller is ready (available in polling mode) for the next step.

2. Output Control Data to the hard drive control port, and then send the write command 0x20

3. Waiting for hard disk interruption

4. When a hard disk is interrupted, 256 words are continuously read from the data port.

The hard drive controller is like a salesman. When you buy something, you need to tell her what kind of product you want first. Then, when the salesperson asks what you want, it will trigger an interruption: This is the product you want, and then you can get what you want. When you return the goods (equivalent to writing the sector), you need to tell the salesperson that you want to return the goods, and then give it to him (writing data into the buffer ), then the salesperson puts the returned goods to the storage location and triggers an interruption. Then you will know that the goods have been successfully returned.

The code below:

void init_harddisk(){unsigned char cw;struct GATE_DESCRIPTOR *idt = (struct GATE_DESCRIPTOR *) ADR_IDT;cw = io_in8(PIC1_IMR);cw = cw & 0xbf;io_out8(PIC1_IMR, cw);set_gatedesc(idt + 0x2e, (int) asm_inthandler2e, 2 * 8, AR_INTGATE32);return;}
This code is similar to the previous mouse driver and keyboard driver initialization. It enables interruption and configures the interrupt response function.


Static int controller_ready (void) {int retries = 10000; while (-- retries & (io_in8 (0x1f7) & 0xc0 )! = 0x40); Return (retries); // return the number of waiting cycles .}
This is equivalent to a latency function.

Then there are the main functions:

void readHardDisk(unsigned char nsector, unsigned char sector, unsigned int cyl, unsigned char current){struct BOOTINFO*binfo = (struct BOOTINFO*) ADR_BOOTINFO;inti;i = controller_ready();if(i == 0){putfonts8_asc(binfo->vram, binfo->scrnx, 400,400, COL8_00FF00,"Read Busy");putHex(binfo->vram, binfo->scrnx, 480,400, COL8_00FF00,io_in8(0x1f7));putHex(binfo->vram, binfo->scrnx, 520,400, COL8_00FF00,io_in8(0x1f1));return;}io_out8(0x3f6, 0x00);io_out8(0x1f2, nsector);io_out8(0x1f3, sector);io_out8(0x1f4, cyl);io_out8(0x1f5, cyl>>8);io_out8(0x1f6, current | 0xa0);io_out8(0x1f7, 0x20);}void writeHardDisk(unsigned char nsector, unsigned char sector, unsigned int cyl, unsigned char current, short* buf){struct BOOTINFO*binfo = (struct BOOTINFO*) ADR_BOOTINFO;inti;i = controller_ready();if(i == 0){putfonts8_asc(binfo->vram, binfo->scrnx, 400,400, COL8_00FF00,"Busy");return;}io_out8(0x1f2, nsector);io_out8(0x1f3, sector);io_out8(0x1f4, cyl);io_out8(0x1f5, cyl>>8);io_out8(0x1f6, current | 0xa0);io_out8(0x1f7, 0x30);i = controller_ready();if(i == 0){putfonts8_asc(binfo->vram, binfo->scrnx, 400,400, COL8_00FF00,"Busy in writing");return;}for(i=0; i < 256;i++){io_out16(0x1f0, buf[i]);}}void inthandler2e(int*esp){struct BOOTINFO*binfo = (struct BOOTINFO*) ADR_BOOTINFO;inti;shorthd_data[256];for(i=0; i < 256;i++){hd_data[i] = io_in16(0x1f0);}//*((unsigned char *)hd_data) = io_in8(0x1f7);putHexs(binfo->vram, binfo->scrnx, 0,0, COL8_00FF00,(unsignedchar *)hd_data,512);io_out8(PIC1_OCW2, 0x66);io_out8(PIC0_OCW2, 0x62);return;}
It may be confusing to see 0x1f0. The following is an introduction to these ports. For more details, refer to the "Linux kernel full note" book written by Dr. Zhao.



1. Output Control Data to the hard drive control port, and then send the write command 0x30

2. Wait until the hard disk controller is ready (available in polling mode) for the next step.

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.