Solved

Low - level disk access in W9x/NT

Posted on 2000-03-24
22
273 Views
Last Modified: 2010-04-04
How do I get access from Win9x *and NT* to the physical information (factory S/N) from a HDD ? In fact, I need to access from a 32-bit Delphi app some port adresses (01F7, 01F6 and 01F0). The first two for byte-reading and the last one for word-writing.

TNX
0
Comment
Question by:iatan
  • 13
  • 4
  • 3
  • +2
22 Comments
 
LVL 17

Expert Comment

by:inthe
ID: 2661447
try this:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

const
  kernel32  = 'kernel32.dll';

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Type
  TVolumeInfo = record
     Name               : String;
     SerialNumber       : DWORD;
     MaxComponentLength : DWORD;
     FileSystemFlags    : DWORD;
     FileSystemName     : String;
  end; // TVolumeInfo

function GetVolumeInformationA(lpRootPathName: PAnsiChar;
  lpVolumeNameBuffer: PAnsiChar; nVolumeNameSize: DWORD; lpVolumeSerialNumber: PDWORD;
  var lpMaximumComponentLength, lpFileSystemFlags: DWORD;
  lpFileSystemNameBuffer: PAnsiChar; nFileSystemNameSize: DWORD): BOOL; stdcall;
external 'kernel32.dll' name 'GetVolumeInformationA';
function GetVolumeInformationW(lpRootPathName: PWideChar;
  lpVolumeNameBuffer: PWideChar; nVolumeNameSize: DWORD; lpVolumeSerialNumber: PDWORD;
  var lpMaximumComponentLength, lpFileSystemFlags: DWORD;
  lpFileSystemNameBuffer: PWideChar; nFileSystemNameSize: DWORD): BOOL; stdcall;
external 'kernel32.dll' name 'GetVolumeInformationW';
function GetVolumeInformation(lpRootPathName: PChar;
  lpVolumeNameBuffer: PChar; nVolumeNameSize: DWORD; lpVolumeSerialNumber: PDWORD;
  var lpMaximumComponentLength, lpFileSystemFlags: DWORD;
  lpFileSystemNameBuffer: PChar; nFileSystemNameSize: DWORD): BOOL; stdcall;
external 'kernel32.dll' name 'GetVolumeInformationA';

var
  Form1: TForm1;

implementation

{$R *.DFM}

function MyGetVolumeInformation( const Drive : Char ) : TVolumeInfo;
var
   lpRootPathName           : PChar; // address of root directory of the file system
   lpVolumeNameBuffer       : PChar; // address of name of the volume
   nVolumeNameSize          : DWORD; // length of lpVolumeNameBuffer
   lpVolumeSerialNumber     : DWORD; // address of volume serial number
   lpMaximumComponentLength : DWORD; // address of system's maximum filename length
   lpFileSystemFlags        : DWORD; // address of file system flags
   lpFileSystemNameBuffer   : PChar; // address of name of file system
   nFileSystemNameSize      : DWORD; // length of lpFileSystemNameBuffer
begin
   GetMem( lpVolumeNameBuffer, MAX_PATH + 1 );
   GetMem( lpFileSystemNameBuffer, MAX_PATH + 1 );
   try
      nVolumeNameSize     := MAX_PATH + 1;
      nFileSystemNameSize := MAX_PATH + 1;

      lpRootPathName := PChar( Drive + ':\' );
      if GetVolumeInformation( lpRootPathName,
                               lpVolumeNameBuffer,
                               nVolumeNameSize,
                               @lpVolumeSerialNumber,
                               lpMaximumComponentLength,
                               lpFileSystemFlags,
                               lpFileSystemNameBuffer,
                               nFileSystemNameSize ) then
      begin
         with Result do
         begin
            Name               := lpVolumeNameBuffer;
            SerialNumber       := lpVolumeSerialNumber;
            MaxComponentLength := lpMaximumComponentLength;
            FileSystemFlags    := lpFileSystemFlags;
            FileSystemName     := lpFileSystemNameBuffer;
         end; // with Result
      end // if
      else
      begin
         with Result do
         begin
            Name               := '';
            SerialNumber       := 0;
            MaxComponentLength := 0;
            FileSystemFlags    := 0;
            FileSystemName     := '';
         end; // with Result
      end; // else
   finally
      FreeMem( lpVolumeNameBuffer );
      FreeMem( lpFileSystemNameBuffer );
   end; // try
end;

procedure TForm1.Button1Click(Sender: TObject);
var
   VolumeInfo : TVolumeInfo;
   flags      : String;
begin
   VolumeInfo := MyGetVolumeInformation( 'C' );
   with VolumeInfo do
   begin
      if (FileSystemFlags and FS_CASE_IS_PRESERVED) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_CASE_IS_PRESERVED'
            else
               flags := 'FS_CASE_IS_PRESERVED';

         if (FileSystemFlags and FS_CASE_SENSITIVE) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_CASE_SENSITIVE'
            else
               flags := 'FS_CASE_SENSITIVE';

         if (FileSystemFlags and FS_UNICODE_STORED_ON_DISK) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_UNICODE_STORED_ON_DISK'
            else
               flags := 'FS_UNICODE_STORED_ON_DISK';

         if (FileSystemFlags and FS_PERSISTENT_ACLS) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_PERSISTENT_ACLS'
            else
               flags := 'FS_PERSISTENT_ACLS';

         if (FileSystemFlags and FS_FILE_COMPRESSION) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_FILE_COMPRESSION'
            else
               flags := 'FS_FILE_COMPRESSION';

         if (FileSystemFlags and FS_VOL_IS_COMPRESSED) <> 0 then
            if Length( flags ) <> 0 then
               flags := flags + #13#10#9'FS_VOL_IS_COMPRESSED'
            else
               flags := 'FS_VOL_IS_COMPRESSED';

      ShowMessage( 'Volume Information For Drive C'#13#10#13#10 +
                   'Name:'#9 + Name + #13#10 +
                   'Serial Number:'#9 + Copy( IntToHex( SerialNumber, 0 ), 1, 4 ) + '-' + Copy( IntToHex( SerialNumber, 0 ), 5, 4 )+ #13#10 +
                   'Max Component Length:'#9 + IntToStr( MaxComponentLength ) + #13#10 +
                   'File System Flags:'#13#10#9 + Flags + #13#10 +
                   'File System:'#9 + FileSystemName );
   end; // with VolumeInfo
end;
end.


0
 

Author Comment

by:iatan
ID: 2668248
Thank you, inthe, for your answer, but it is not what I need. The source code you gave me reads the logical s/n from the volume, and I need to read the physical s/n from the HDD. The reason for this is that the logical s/n can be changed via software (the most obvious example is at the time of formatting), while the physical one cannot be changed in any way (except for a nail and a hammer :-) )
0
 

Author Comment

by:iatan
ID: 2668613
BTW, you don't need to explicitly declare the GetVolumeInformation function, because Delphi already has the prototype...
0
 
LVL 17

Expert Comment

by:inthe
ID: 2674388
it is old code from old version of delphi and GetVolumeInformation need to be declared in older version..

what serial number is this ?
is there a program for windows i can see this with ?



0
 

Author Comment

by:iatan
ID: 2679181
Ok, here comes the code : (see below). This is a 16-bit protected mode dll, and it will *not* work when compiled with Delphi (it will compile, but will not run properly). This code will run under W9x, but it will not run under NT/2000, so I need a 32-bit version. Unfortunately, the ports are not published.. I have tried to write a VxD/VDD, but I have not succeeded.
0
 

Author Comment

by:iatan
ID: 2679183
Unit DiskInfo;

Interface

Function GetDiskInformations : boolean;

Function GetModelNumber : string;
Function GetContrRevNumber : string;
Function GetSerialNumber : string;

                        {$Else - sectiune compilata pentru mod protejat (in general) }
                        Library DiskInfo;

                                Uses WinDOS;
{$EndIf}

{$IfDef MSDOS - sectiune compilata pentru modul real }

Implementation

Uses Dos;

{$EndIf}

{ Sectiunea de mai jos este independenta de tinta compilarii }

Var In_Data : array [0..255] of word;

Function GetAscii(off_start : integer; off_end : integer) : string;
Var ret_val : string;
    loop    : integer;
    LowV, HighV, Interv  : integer;
    loop1,i : integer;

Function HighWord(var i : word) : char;
var Sir : string[2];
Begin
  Move (i, Sir, 2);
  HighWord := Sir[1];
End;

Function LowWord(var i : word) : char;
var Sir : string[2];
Begin
  Move (i, Sir, 2);
  LowWord := Sir[0];
End;

Begin
  LowV := off_start*2;
  HighV:= off_end*2;
  Interv := HighV - LowV + 2;
  for i:= 1 to Interv  do begin
    ret_val[2*i] := LowWord(in_data[i-1+off_start]);
    ret_val[2*i-1] := HighWord(in_data[i-1+off_start]);
  end;
  ret_val[0] := Chr(i);
  GetAscii := ret_val
End;

Var SModelNumber : string[18];
    SSerialNumber : string[8];
    SRevision : string[8];

Function GetDiskInformations : boolean; {$IfNDef MSDOS} export; {$EndIf}
Var k, dd_of : word;
    TWord, Second : word;
Begin
    k := 0;
    GetTime(TWord, TWord, Second, TWord);
    Repeat
      GetTime(TWord, TWord, k, TWord);
      if k < Second then k := k + 60;
    until (Port[$01F7] = $50) or (k-Second > 10);
    if k-second > 10 then begin
      GetDiskInformations := FALSE;
      exit;
    end;
    Port[$01F6] := $00A0;
    {If loop = 0 then WritePort($01F6,$00A0)
                else WritePort($01F6,$00B0);}
    Port[$01F7] := $00EC;
    k := 0;
    GetTime(TWord, TWord, Second, TWord);
    Repeat
      GetTime(TWord, TWord, k, TWord);
      if k < Second Then k := k + 60;
    until (Port[$01F7] = $58) or (k-Second > 10);
    if k-second > 10 then begin
      GetDiskInformations := FALSE;
      exit;
    end;
    For dd_of := 0 to 255 do
       In_Data[dd_of] := PortW[$01F0];
    GetDiskInformations := TRUE;
    SModelNumber := GetAscii(27, 46);
    SSerialNumber := GetAscii(10, 19);
    SRevision := GetAscii(23, 26)
End;

{$IfDef MSDOS}
Function GetModelNumber : string;
{$Else}
Function GetModelNumber : char; export;
{$EndIf}
Begin
  {$IfDef MSDOS}
  GetModelNumber := SModelNumber
  {$Else}
  If SModelNumber <> '' then
    Begin
      GetModelNumber := SModelNumber[1];
      Delete(SModelNumber,1,1)
    End
  else GetModelNumber := #0
  {$EndIf}
End;

{$IfDef MSDOS}
Function GetSerialNumber : string;
{$Else MSDOS}
Function GetSerialNumber : char; export;
{$EndIf}
Begin
  {$IfDef MSDOS}
  GetSerialNumber := SSerialNumber
  {$Else}
  If SSerialNumber <> '' then
    Begin
      GetSerialNumber := SSerialNumber[1];
      Delete(SSerialNumber,1,1)
    End
  else GetSerialNumber := #0
  {$EndIf}
End;

{$IfDef MSDOS}
Function GetContrRevNumber : string;
{$Else MSDOS}
Function GetContrRevNumber : char; export;
{$EndIf}
Begin
  {$IfDef MSDOS}
  GetContrRevNumber := SRevision
  {$Else}
  If SRevision <> '' then
    Begin
      GetContrRevNumber := SRevision[1];
      Delete(SRevision,1,1)
    End
  else GetContrRevNumber := #0
  {$EndIf}
End;

{$IfNDef MSDOS}
Exports GetDiskInformations,
      GetModelNumber,
      GetContrRevNumber,
      GetSerialNumber;

Begin
{$EndIf}
End.
0
 

Author Comment

by:iatan
ID: 2679190
sorry about the comments...they slipped..
0
 
LVL 4

Expert Comment

by:Radler
ID: 2680395
Spying...
Õ¿Õ
/o/
0
 
LVL 2

Expert Comment

by:ivi
ID: 2682442
iatan, you code works fine ... but inside of the drivers only. This code will not work under Win32 even if you will have full port access from the application level because under Windows we must disable an interruptions before. It is possible at driver level (ring 0) only.
0
 
LVL 17

Expert Comment

by:inthe
ID: 2684978
yep vxd stuff which cant be done in delphi. hope you know c++ etc  ;-)
0
 

Author Comment

by:iatan
ID: 2688844
OK. Any other sugestions ?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:iatan
ID: 2688846
Adjusted points from 100 to 200
0
 

Author Comment

by:iatan
ID: 2726997
Adjusted points from 200 to 300
0
 
LVL 1

Expert Comment

by:sistudio041900
ID: 2730311
Bypass: Win9x VxD I/O Virtualization.

Win kernel mode drivers .386 .VXD do
catch the raw writes to I/O Space, but
there is a 'workaround': as the IO is decoded 10 bits and the driver registers
for 16 bit address then writing to I/O ports with bit 15 set to 1 will bypass any .386 or .VXD drivers that did virualize the hardware in question. This works for all Win platforms that support kernel dirvers (VXD) so just write to 0x81F7 and you land to real hardware at 0x01F7 :)
This will not work on NT (not tested on Win2000). On NT there is only one way to the hardware, and that is via NT Service.
0
 

Author Comment

by:iatan
ID: 2737208
OK, thanks, sistudio, for the tip (BTW, how much did you have to dig to get that kind of knowledge ?).
But my problem is this : as you can see, (source above) I have to read first from a port until I get a value. Well, that doesn't work and I have tried in the ordinary way and with the method you described. The rewritten piece of code is below...
0
 

Author Comment

by:iatan
ID: 2737213
Procedure WriteByteToPort(B : byte; P : word); Assembler;
ASM
          mov      al, B
          mov      dx, P
          or       dx, 1000000000000000B
          out      dx, al
End;

Function ReadByteFromPort(P : word) : byte; Assembler;
ASM
          mov      dx, P
          in       al, dx
End;

Procedure WriteWordToPort(W : word; P : word); Assembler;
ASM
          mov      ax, W
          mov      dx, P
          or       dx, 1000000000000000B
          out      dx, ax
End;

Function ReadWordFromPort(P : word) : word; Assembler;
ASM
          mov      dx, P
          in       ax, dx
End;

....and the call is like this :

<...>
Repeat
      DecodeTime(Now,TWord, TWord, k, TWord);
      if k < Second then k := k + 60;
      FTestW95.Label1.Caption := IntToStr(ReadByteFromPort($01F7));
      FTestW95.Label1.Refresh
    until (ReadByteFromPort($01F7) = $50) or (k-Second > 10);
    if k-second > 10 then begin
      GetDiskInformations := FALSE;
      exit;
    end;
    WriteByteToPort($00A0,$01F6);
    {If loop = 0 then WritePort($01F6,$00A0)
                else WritePort($01F6,$00B0);}
    WriteByteToPort($00EC,$01F7);
<...>
0
 
LVL 1

Accepted Solution

by:
sistudio041900 earned 300 total points
ID: 2742969
I'm in business since '79 :) an I have written VXD's so I do know whats behind the scenes. About the code, you wrote: first what you are doing is DANGEROUS if doing low level access to I/O specially to harddisk controller you must ensure no-one else does that. Maybe creating a critical section and boosting thread priority to maximum is sufficient, but here I am not 100% sure. But you cant to harddisk I/O in way you do, somebody else (some thread eg OS) may do the same access and then you get your harddisk corrupted. Well the in out asm commands as I described actually work, but well you should set bit 15 for input also. But again, this kind of programming is something I would never use for anything I distribute, be it commercial or free. This might be useful in some 'internal testing'... but to dangerous to use in release products. If all you are looking for is copyprotection I suggest you study other means of achieving that. Dongles are available starting at 15 USD (I have myself manufactured dongles and sold them for below 10 USD with profit). If you want copyportection just get a DB25 connector place a wire between some of the pins, or more wires some diodes I2C memory, etc between the pins and use the I/O methods as I described to write the dongle access code. :)







0
 

Author Comment

by:iatan
ID: 2743002
I hear you :) .. But, since I am not very fond of hardwired solutions, I will try to think of something else.

And yes, thank you for your answer

0
 

Author Comment

by:iatan
ID: 2743021
:( Sorry about the points mess, it's really not my fault...Tell me how to fix it..
0
 
LVL 1

Expert Comment

by:sistudio041900
ID: 2743023
Thanks for accepting the answer. If you are not going to distribute to Russia/China (and considering them as primary market) then I would suggest that a correct 'end user license agreement' is actually sufficient. In most cases primary sales region is US and as their laws are so strict that there is no reason to even consider any special 'no-copy' methods for software products sold to US. As of hardware dongles, I dont use them myself either :) and the US people? They just DONT buy anything that has a dongle. As of other means try:
 
http://www.previewsystems.com/vbox/
0
 
LVL 1

Expert Comment

by:sistudio041900
ID: 2743024
Thanks for accepting the answer. If you are not going to distribute to Russia/China (and considering them as primary market) then I would suggest that a correct 'end user license agreement' is actually sufficient. In most cases primary sales region is US and as their laws are so strict that there is no reason to even consider any special 'no-copy' methods for software products sold to US. As of hardware dongles, I dont use them myself either :) and the US people? They just DONT buy anything that has a dongle. As of other means try:
 
http://www.previewsystems.com/vbox/
0
 

Author Comment

by:iatan
ID: 2743037
I see I have to thank you every 10 minutes :) ..
And I still owe you 270 points...
About the market, it's Romania (unfortunately that's closer to Russia than US)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now