MMC karty a RAW zapis

Jiri Beranek hw_konference@profisite.cz
Pondělí Červen 5 13:16:59 CEST 2006


Posilam par odkazu a fragmentu kodu ktery urcite po par upravach i
zkompilujete, snad to pomuze.
Abyste si neprepsal disk ktery nechcete prepsat, je vhodne napr. pri
otevreni zkontrolovat velikost
oteviraneho disku a zakazat pristup na "velke disky", nebo zkontrolovat zda
jsou na nejakem sektoru,
nebo sektorech zapsana urcita identifikacni data, ktera si zvolite.

#include "stdafx.h"
#include "AlarmRead.h"
#include "AlarmReadDlg.h"
#include "winioctl.h"
#include <stdlib.h>
#include <io.h>
#include "stdafx.h"
#include "windows.h"
#include <direct.h>

unsigned char read_sd (unsigned char *buf, unsigned long sector) {
  DWORD res;

  res = SetFilePointer(
  dh,          // handle of file
  sector*512,  // number of bytes to move file pointer
  NULL,        // pointer to high-order DWORD of distance to move
  FILE_BEGIN   // how to move
  );

  if ((res==0xFFFFFFFF)||(res!=sector*512))
   return (0);

  if (!ReadFile(
       dh,                          // handle of file to read
       buf,                         // pointer to buffer that receives data
       512,                         // number of bytes to read
       &res,                        // pointer to number of bytes read
       NULL                         // pointer to structure for data
  ))
      return (0);

  if (res!=512) return (0);

  return (1);
}


unsigned char read_block_sd (unsigned char *buf, unsigned long sector,
unsigned long size)
{
  DWORD res;

  res = SetFilePointer(
  dh,          // handle of file
  sector*512,  // number of bytes to move file pointer
  NULL,        // pointer to high-order DWORD of distance to move
  FILE_BEGIN   // how to move
  );

  if ((res==0xFFFFFFFF)||(res!=sector*512))
   return (0);

  if (!ReadFile(
       dh,                          // handle of file to read
       buf,                         // pointer to buffer that receives data
       (size+1)*512,                        // number of bytes to read
       &res,                        // pointer to number of bytes read
       NULL                         // pointer to structure for data
  ))
      return (0);

  if (res!=(size+1)*512) return (0);

  return (1);
}


unsigned char write_sd (unsigned char *buf, unsigned long sector, unsigned
long size) {
  DWORD res;

  res = SetFilePointer(
  dh,          // handle of file
  sector*512,  // number of bytes to move file pointer
  NULL,        // pointer to high-order DWORD of distance to move
  FILE_BEGIN   // how to move
  );

  if ((res==0xFFFFFFFF)||(res!=sector*512))
   return (0);

  if (!WriteFile(
       dh,                          // handle of file to write
       buf,                         // pointer to buffer that receives data
       size*512,                    // number of bytes to write
       &res,                        // pointer to number of bytes writed
       NULL                         // pointer to structure for data
  ))
      return (0);

  if (res!=size*512) return (0);

  return (1);
}


bool get_drive_size (unsigned long *ds) {
  DISK_GEOMETRY dg;
  DWORD br;

  if (!DeviceIoControl(
  dh,                            // handle to device of interest
  IOCTL_DISK_GET_DRIVE_GEOMETRY, // dwIoControlCode, control code of
operation to perform
  NULL,                          // lpInBuffer is not used; must be NULL
  0,                             // nInBufferSize is not used; must be zero
  &dg,                           // pointer to output buffer
  sizeof(dg),                    // size, in bytes, of lpOutBuffer
  &br,                           // pointer to variable to receive output
byte count
  NULL                           // pointer to OVERLAPPED structure for
asynchronous operation
  )) return (false);

  if (dg.BytesPerSector!=512) return (false);


*ds=/*(dg.BytesPerSector)**/(dg.SectorsPerTrack)*(dg.TracksPerCylinder)*(dg.
Cylinders.LowPart);
  *ds=*ds-1;


  return (true);
}

char drivet[]="\\\\.\\PHYSICALDRIVEX";
char selected_drive;

bool open_disc (unsigned char fmode) {
 DWORD dw, le;

 drivet[17]=selected_drive+'0';
 if (fmode==0)
 {
     dh = CreateFile(
      drivet,                            // pointer to name of the file
      GENERIC_READ,                      // access mode
      FILE_SHARE_READ|FILE_SHARE_WRITE,  // share mode
      NULL,                              // pointer to security attributes
      OPEN_EXISTING,                     // how to create
      0,                                 // file attributes
      NULL                               // handle to file with attributes
to copy
     );
 }
 else
 {
     dh = CreateFile(
      drivet,                            // pointer to name of the file
      GENERIC_READ|GENERIC_WRITE,        // access mode
      FILE_SHARE_READ|FILE_SHARE_WRITE,  // share mode
      NULL,                              // pointer to security attributes
      OPEN_EXISTING,                     // how to create
      0,                                 // file attributes
      NULL                               // handle to file with attributes
to copy
     );
 }

 if (dh==INVALID_HANDLE_VALUE) {
  le=GetLastError ();
  return (false);
 }

 dw = GetFileType (dh);
 if (dw!=FILE_TYPE_DISK) {
  CloseHandle (dh);
  return (false);
    }


    if (!get_drive_size (&sd_tot_size)) {
  CloseHandle (dh);
  return (false);
    }

 return (true);
}

>Jestli by to nevadilo, tak bych byl za kousek kodu rad :).
>Tou vetou jsem chtel rict ze casto se CreateFile pouziva pro pristup na
>disk, ale ne primo RAW, ale souborovy zapis v ramci filesystemu.

>Zatim me nenapada co pouzit jako "pointer to name of the file" (/*???*/)
>, je to hned prvni parametr funkce createfile. Mluvim o pripadu kdy chci
>pristup na USB ctecku karet. Abych treba omylem nehrabnul na pevny disk
>a pak by byl problem na svete.

>hDevice = CreateFile( /*???*/ , GENERIC_READ | GENERIC_WRITE,
>FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
>NULL);

>Kdyz jsme u toho, podobny pristup by sel pouzit i na seriovy a LPT port?

http://forum.builder.cz/read.php?123,1555971,1557011#msg-1557011
http://forum.builder.cz/read.php?23,1472592,1477895#msg-1477895
http://forum.builder.cz/read.php?23,1447448,1448208#msg-1448208

J.Ber

>DeviceIOControl je spis na nastavovani nejakych specifickych vlastnosti.
>Ano pomoci funkci (CreateFile, ReadFile, WriteFile, CloseHandle)
>zapise/prectete
>raw data do/z jakehokoliv sektroru.

>Nevim co si predstavit pod touto vetou ?:
>"mel jsem za to ze v pripade disku se pouziva session pres FAT nebo NTFS"

>Kdyz to bude nutny, tak muzu poslat i kus
>kodu na ukazku, ale fakt to neni veda.

>J.Ber


_______________________________________________
HW-list mailing list  -  sponsored by www.HW.cz
Hw-list@list.hw.cz
http://list.hw.cz/mailman/listinfo/hw-list





Další informace o konferenci Hw-list