VC、Linux、vxWorks读写物理扇区小结

来源:互联网 发布:刘雯怎么培养气质知乎 编辑:程序博客网 时间:2024/06/07 03:17

直接上程序:

1. Windows下的VC:

[cpp] view plain copy
  1. HANDLE hDevice = CreateFile("\\\\.\\I:", GENERIC_READ|GENERIC_WRITE,      
  2.             FILE_SHARE_READ | FILE_SHARE_WRITE,     
  3.             NULL, OPEN_EXISTING, 0, NULL    
  4.             );     
  5.         if (hDevice == INVALID_HANDLE_VALUE)     
  6.         {    
  7.             printf("打开磁盘错误\n");  
  8.             return 0;    
  9.         }    
  10.   
  11.         //读扇区  
  12.         DWORD bytesread = 0;   
  13.         unsigned char Buffer[1000] = {0};  
  14.         int SectorNumber=0;  
  15.         //for (int SectorNumber=0; SectorNumber<100; SectorNumber++)  
  16.         {  
  17.             SetFilePointer (hDevice, SectorNumber*512, 0, FILE_BEGIN);    
  18.             ReadFile (hDevice, Buffer, 512, &bytesread, NULL);    
  19.   
  20.             getch();  
  21.   
  22.         }  
  23.   
  24.         //写操作  
  25.         memset(Buffer, 0x35, 512);  
  26.         SetFilePointer (hDevice, SectorNumber*512, 0, FILE_BEGIN);    
  27.         WriteFile (hDevice, Buffer, 512, &bytesread, NULL);  

上面的CreateFile函数的第一个参数,是C: ,D:类似的盘符


2. vxWorks读扇区:参考mkboot.c源程序得到

[cpp] view plain copy
  1. char lbaSectorZero[1024] = {0};  
  2.     int line, j;  
  3.     ATA_RAW ataRaw = {0};  
  4.       
  5.     ataRaw.cylinder   = 0;  
  6.     ataRaw.head       = 0;  
  7.     ataRaw.sector     = 1;  
  8.     ataRaw.pBuf       = (char *)lbaSectorZero;  
  9.     ataRaw.nSecs      = 1;   
  10.     ataRaw.direction  = O_RDONLY;  
  11.     ataRawio (0, 0, &ataRaw);  
  12.       
  13.     for(line=0; line<32; line++)  
  14.     {  
  15.         for(j=0; j<16; j++)  
  16.         {  
  17.             printf("%02x ", (unsigned char)lbaSectorZero[line*16+j]);  
  18.         }  
  19.         printf("\n");  
  20.     }  


ataRawio.direction为0时是读,为1时是写,其他参考 target/h/drv/hdisk/ataDrv.h的ATA_RAW定义。



3. Linux:下面的/dev/sde,是U盘插上去后显示的设备符合:

[cpp] view plain copy
  1.   #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <stdio.h>   
  4. #include <errno.h>   
  5. #include <unistd.h>   
  6. #include <sys/types.h>   
  7. #include <sys/stat.h>   
  8. #include <fcntl.h>   
  9.   
  10. #include <linux/fs.h>   
  11. #include <sys/ioctl.h>  
  12.   
  13.   
  14. /* 
  15.  *  
  16.  */  
  17. int main(int argc, char** argv)   
  18. {  
  19.     int fd=0;  
  20.     int sizes = 0;  
  21.     char buf[1000] = {0};  
  22.         int line, j;  
  23.   
  24.     fd = open("/dev/sde", O_RDONLY);  
  25.     if(fd !=-1)  
  26.     {     
  27.           
  28.         ioctl(fd, BLKSSZGET, &sizes);  
  29.         printf("sector size=%d\n", sizes);  
  30.   
  31.         lseek(fd, 0, SEEK_SET);  
  32.           
  33.         read(fd, buf, sizes);  
  34.                   
  35.                 for(line=0; line<32; line++)  
  36.                 {  
  37.                     for(j=0; j<16; j++)  
  38.                     {  
  39.                         printf("%02x ", (unsigned char)buf[line*16+j]);  
  40.                     }  
  41.                     printf("\n");  
  42.                 }  
  43.   
  44.     }  
  45.   
  46.     return (EXIT_SUCCESS);  
  47. }