Read-Write hdd sectors - Embedded

This is a discussion on Read-Write hdd sectors - Embedded ; Hello to all, I m writing the Diagnostic code for the HDD. In that i want to do the following test 1. Locate the sectors 2. Reading the data from the sector 3. Saving the original data into some buffer ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Read-Write hdd sectors

  1. Read-Write hdd sectors

    Hello to all,

    I m writing the Diagnostic code for the HDD. In that i want to do the
    following test
    1. Locate the sectors
    2. Reading the data from the sector
    3. Saving the original data into some buffer
    4. Writing the new data into that sectors
    5. Again reading back the data from the sector
    6. Comparing the data
    7. Again putting the original data back to the sectors.

    I want to do all theses steps from the user application layer. For that
    i m using the read , write commands on the registers present inside the
    IDE controller.

    My code is
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include


    #define HDD_SECTOR_COUNT_REG 0x1f2
    #define HDD_CYLINDER_LOW_REG 0x1f4
    #define HDD_CYLINDER_HIGH_REG 0x1f5
    #define HDD_SECTOR_NUM_REG 0x1f3

    #define HDD_BASE 0x1f0
    #define HDD_ERROR_REG 0X1f1
    #define HDD_STATUS_COMM_REG 0x1f7
    #define HDD_HEAD_DRIVE_REG 0x1f6
    #define HDD_DATA_READY 0x08
    #define HDD_READ_SECTOR 0x20
    #define HDD_BUSY 0x80
    #define HDD_WRITE_SECTOR 0x30

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include

    int ReadSector();
    void WriteSector();


    int main()
    {

    int data2;
    ioperm(HDD_BASE,8,1);
    data2 = ReadSector();
    printf("the original data = %d\n",data2);
    WriteSector();
    ReadSector();
    return 0;
    }

    int ReadSector()
    {
    int status,status1,status2,status3,status4;
    int i,SectCnt;
    int Buffer[256];
    int LBA_Adress = 400;
    int val;
    int data=0;
    int data3=0;
    while(1)
    {
    val=inb(HDD_STATUS_COMM_REG);
    if(val != 0x80)
    {
    printf("not busy\n");

    break;
    }
    else
    printf("Busy\n");
    }
    outb((LBA_Adress & 0xff),HDD_SECTOR_NUM_REG);

    status=inb(HDD_SECTOR_NUM_REG);
    printf("%d\n",status);



    outb(((LBA_Adress>>16) & 0xff),HDD_CYLINDER_HIGH_REG);
    outb(((LBA_Adress>>8) & 0xff),HDD_CYLINDER_LOW_REG);
    outb(((((LBA_Adress>>24)&0x0f) | 0xE0) &
    0xff),HDD_HEAD_DRIVE_REG);
    outb(SectCnt,HDD_SECTOR_COUNT_REG);

    while(!(inb(HDD_STATUS_COMM_REG)&HDD_DATA_READY));

    printf("Data Ready\n");

    Buffer[0]=0;
    for(i=0;i<1;i++)
    {
    Buffer[i]=inb(HDD_BASE);
    data3=Buffer[i];
    printf("data3 = %d\n",data3);
    }

    return(data3);
    }



    void WriteSector()
    {

    int status,status1,status2,status3,status4;
    int i,SectCnt;
    int LBA_Adress = 400;
    int val=0;
    int data=0xff;
    while(1)
    {
    val=inb(HDD_STATUS_COMM_REG);
    if(val != 0x80)
    {
    printf("not busy\n");

    break;
    }
    else
    printf("Busy\n");
    }



    outb((LBA_Adress & 0xff),HDD_SECTOR_NUM_REG);
    outb(((LBA_Adress>>16) & 0xff),HDD_CYLINDER_HIGH_REG);
    outb(((LBA_Adress>>8) & 0xff),HDD_CYLINDER_LOW_REG);
    outb(((((LBA_Adress>>24)&0x0f) | 0xE0) &
    0xff),HDD_HEAD_DRIVE_REG);
    outb(HDD_READ_SECTOR,HDD_STATUS_COMM_REG);
    outb(HDD_READ_SECTOR,HDD_STATUS_COMM_REG);
    outb(SectCnt,HDD_SECTOR_COUNT_REG);
    outb(HDD_WRITE_SECTOR,HDD_STATUS_COMM_REG);

    while(!(inb(HDD_STATUS_COMM_REG)&HDD_DATA_READY));

    printf("Data Ready\n");
    outb(data,HDD_BASE);
    printf("%d\n",data);
    }

    I m using Linux SUSE OS.
    After execution i m getting the o/p as
    not busy
    Data Ready
    data3 = 0
    the original data = 0
    not busy
    Data Ready
    255
    not busy
    Data Ready
    data3 = 0

    That means after reading the Sector i get the first byte =0
    Then i m writing 255 at the same ocation and reading back. But this
    time also i m getting the same date =0 instead of 255

    At the same time after execution i m not able to login in Windows(i m
    working in duo mode--Linux and Wnddows)
    It is give me error that /system32/hal.dii file is missing or being
    currupted.....

    Can anyone please tell me whether i m going on the right direction or
    not. What is the problem in my code? I don't want to do the programming
    from the Kernel mode.

    Please guide me.....

    Thanks and Regards,
    Nutty.


  2. Re: Read-Write hdd sectors

    As discussed at some length in the other thread:

    If using Linux: This is not worth discussing, as the hdx driver does
    this for you.

    If not using Linux: find an appropriate discussion group.


    hope this helps,
    -Michael

  3. Re: Read-Write hdd sectors

    Michael Schnell wrote:
    > As discussed at some length in the other thread:
    >
    > If using Linux: This is not worth discussing, as the hdx driver does
    > this for you.
    >
    > If not using Linux: find an appropriate discussion group.
    >
    >
    > hope this helps,
    > -Michael


    This has been explained to him a couple of dozen times,
    along with explanations why. It seems that he's not:

    a) reading the responses, or
    b) understanding the responses.

    I'm suggesting to plonk him due to not reacting to
    the perfectly good responses.

    --

    Tauno Voipio
    tauno voipio (at) iki fi

+ Reply to Thread