⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 readsector.cpp

📁 瑞星微公司RK27XX系列芯片的SDK开发包
💻 CPP
字号:
/* 2004.06.05
****************************************
**  Copyright  (C)  W.ch  1999-2004   **
**  Web:  http://www.winchiphead.com  **
****************************************
**  USB Host File Interface for CH375 **
**  VC5.0@PC                          **
****************************************
*/

#define PC_DEBUG_DRIVE "\\\\.\\PHYSICALDRIVE1"//"\\\\?\\usb#vid_071b&pid_3203#usbv1.00#{a5dcbf10-6530-11d2-901f-00c04fb951ed}"//  

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>


/*******************************************************************************/
HANDLE gPCHDiskDevice;

TestReadSector(ULONG argc, UCHAR *argv[])
{
    UCHAR c;
    ULONG i, j;
    ULONG lba;
    HANDLE mhDevice;
    UCHAR buffer[512];
    printf("Copyright (C) W.ch 1998-2005, http://wch.cn\n");
    printf("Show sector data in second physical disk\n");
    if (argc <= 1)
    {
        printf("This program only work under WINDOWS 2000/XP\n");
        printf("Command line:  CH375UD  sector_number\n");
        printf("Example:\n");
        printf("  to show    0# (0000H) sector: CH375UD  0\n");
        printf("  to show   63# (003FH) sector: CH375UD  63\n");
        printf("  to show 1758# (06DEH) sector: CH375UD  1758\n");
        printf("to show sector data and save to TXT file:\n");
        printf("  CH375UD  1758  > file_name_for_saving_text\n");
        printf("warning !!! to clear 1758# (06DEH) sector: CH375UD  1758 /C\n");
        exit(1);
    }
    lba = atol((const char*)argv[1]);
    mhDevice = CreateFile(PC_DEBUG_DRIVE, GENERIC_READ | GENERIC_WRITE,
                          FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                          NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (mhDevice == INVALID_HANDLE_VALUE)
    {
        printf("Please insert USB disk and retry\n");
        exit(2);
    }
    i = lba * 512;
    j = SetFilePointer(mhDevice, i, NULL, FILE_BEGIN);    // 指定扇区号
    if (i != j)
    {
        CloseHandle(mhDevice);
        printf("Error on set disk sector address\n");
        exit(3);
    }
    i = 512;
    if (! ReadFile(mhDevice, buffer, 512, &i, NULL))        // 读一个扇区512字节
    {
        CloseHandle(mhDevice);
        printf("Error on reading disk sector\n");
        exit(4);
    }
    if (i != 512)
    {
        CloseHandle(mhDevice);
        printf("Error on reading disk sector, return length = %d\n", i);
        exit(5);
    }
    for (i = 0; i < 512; i += 16)
    {
        printf("@%03X:", i);
        for (j = 0; j < 16; j ++)
        {
            if (j == 8) printf(" -");
            printf(" %02X", buffer[i+j]);
        }
        printf("   ");
        for (j = 0; j < 16; j ++)
        {
            c = buffer[i+j];
            if (c < 0x20) c = '.';
            if (c == 0x7f) c = '.';
            printf("%c", c);
        }
        printf("\n");
    }
    if (argc >= 3)
    {
        if (argv[2][0] == '/' && (argv[2][1] == 'C' || argv[2][1] == 'c'))
        {
            printf("Press TAB to confirm clear %ld# sector in second disk\n", lba);
            if (getch() == 9)
            {
                i = lba * 512;
                SetFilePointer(mhDevice, i, NULL, FILE_BEGIN);    // 指定扇区号
                for (i = 0; i < 512; i ++) buffer[i] = 0;
                if (! WriteFile(mhDevice, buffer, 512, &i, NULL))        // 写一个扇区512字节
                {
                    CloseHandle(mhDevice);
                    printf("Error on writing disk sector\n");
                    exit(6);
                }
                if (i == 512) printf("Clear successfully\n");
                else printf("Error return length = %d\n", i);
            }
            else printf("Canceled\n");
        }
    }
    CloseHandle(mhDevice);
    printf("  CH375UD  %ld | more   or  CH375UD  %ld > SECTOR.TXT  ", lba, lba);
    return(1);
}


extern FS_CACHE FlashUFatCache;
extern FS_CACHE FlashUFdtCache;
extern FS_CACHE FlashUDataCache;

int PC_HDiskGetStatus(void)
{
    return(0);
}

int PC_HDiskReadSec(uint32 LBA, uint32 SecCount, void *buf)
{
    ULONG i = LBA * 512;
    ULONG j;
    j = SetFilePointer(gPCHDiskDevice, i, NULL, FILE_BEGIN);    // 指定扇区号
    if (i != j)
    {
        return ERROR;
    }

    if (! ReadFile(gPCHDiskDevice, buf, SecCount*512, &i, NULL))
    {  // 读一个扇区512字节
        return ERROR;
    }

    if (i != SecCount*512)
    {
        return ERROR;
    }
    return OK;
}

int PC_HDiskWriteSec(uint32 LBA, uint32 SecCount, void *buf)
{
    //为了保护硬盘,不支持写操作.
    return ERROR;
}


int PC_HDiskIoCtl(uint32 cmd, uint32 arg, void *buf)
{
    uint32 *rbuf;
    rbuf = (uint32 *)buf;
    switch (cmd)
    {
        case IO_CTL_GET_CAPACITY:
            *rbuf = 0;
            break;
        case IO_CTL_GET_MEDIUM_START_SEC:
            *rbuf = 0;
            break;
        case IO_CTL_CACHE_WRITE_BACK:

            break;
        case IO_CTL_FLUSH_CACHE:

            break;
        default:
            break;
    }
    return(0);
}


void PC_HDiskCacheInit(void)
{

}
int PC_HDiskMediumInit(void)
{

    gPCHDiskDevice = CreateFile(PC_DEBUG_DRIVE, GENERIC_READ /*| GENERIC_WRITE*/,
                                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                                NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
    if (gPCHDiskDevice == INVALID_HANDLE_VALUE)
    {
        return ERROR;
    }
    return OK;
}

const FS_DEVICE_TYPE PCLIB_HDiskDevice =
{
    "D:\\",
    "PCDISK",
    PC_HDiskGetStatus,
    PC_HDiskReadSec,
    PC_HDiskWriteSec,
    PC_HDiskIoCtl,
    PC_HDiskMediumInit,
    &FlashUFatCache,
    &FlashUFdtCache,
    &FlashUDataCache
};


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -