Link to home
Start Free TrialLog in
Avatar of astrix2
astrix2

asked on

Any Know a SOURCE CODE for Read Write Sectors on NTFS Disk, Scan Sectors, Mark Sector as Bad, etc...

Any Know a SOURCE CODE for Read Write Sectors on NTFS Disk, Scan Sectors, Mark Sector as Bad, etc...

Thanks in Advance...
Avatar of Shailesh Shinde
Shailesh Shinde
Flag of India image

Hi,

Once you have pointers to device objects for raw disks and partitions, reading and writing to those raw disks/partitions is not a difficult thing. You only have to do a IoCallDriver on the respective device object with IRP_MJ_READ/IRP_MJ_WRITE function codes initialized in the IRPs.

The following code will make things clear:

LARGET_INTEGER lDiskOffset; 

PDEVICE_OBJECT pDevObj; //Device object representing disk/partition

KEVENT Event; 

// Trying to read some arbitrary sector number 1169944 and 
// by default assuming sector size 

// 512 

..........

..........

        lDiskOffset.QuadPart = 1169944*512;
        sBuf = ExAllocatePool(NonPagedPool, size);
        
        if (!sBuf) {
            ObDereferenceObject(pFileObj);
            return STATUS_INSUFFICIENT_RESOURCES;
        }
        KeInitializeEvent(&Event, NotificationEvent, FALSE);
        memset(sBuf, '0x00', size);
        pIrp = IoBuildSynchronousFsdRequest(IRP_MJ_WRITE/*IRP_MJ_READ*/, 
			pDevObj, sBuf, size, &lDiskOffset, &Event, &ioStatus);
        
        
        if (!pIrp) {
            ExFreePool(sBuf);
            return STATUS_INSUFFICIENT_RESOURCES;
        }
        
        status = IoCallDriver(pDevObj, pIrp);

        if (status == STATUS_PENDING) {
            KeWaitForSingleObject(&Event, Executive, KernelMode, FALSE,    NULL);
            status = ioStatus.Status;
        }
        ExFreePool(sBuf);

.......... 

Open in new window

Given above is just a sample code for sending a write operation to sector number 1169944.

Thanks,
Shail
Avatar of astrix2
astrix2

ASKER

Thanks for your answer, but, how to mark as bad one arbitrary sector, have you the code???

In reality my idea is this, how I write here:

https://katyscode.wordpress.com/2007/04/16/ntfs-bad-sectors-resolution-the-badclus-metafile/comment-page-1/#comment-27421

////////////////////////////////
First of all, Sorry for my not very good English...

Well Katy, I have a doubt about bad sectors, and is:

Way not make any person a program that check sectors not one by one - take any time, normally 8 to 10 hours - but 100  to 100 sectors, or 1000 to 1000 sectors, and when find a bad sector mark from the last good sector all bad, scanning 1000 to 1000 sectors the time reduce many, and in almost all cases bad sectors are contiguous...

With this option of scan disk sectors 1000 by 1000 in 1 minute are checked almost all of the disk, and marking inmediately how bad the last 1000 sectors, yes, many sectors good are marked bad, but not lost many of the space of the disk, but the advantage of this method are that in 5 mnutes any person recover a hard disk with all bad sectors marked - with many of good sectors, but not lost many of the space -...

Is possible a programa how this???

For example, HDD Regenarator, take 8-10 hours in terminate scan, and HDD Regenarator attempt to recover bad sectors, but well, not is more better have the option - if any only want to recover the disk, not the disk information - check 1000 by 1000 sector, and not attempt to recover bad sector, when a sector are bad, mark inmediately the last 1000 sectors as bad, and continue, and in little little little time, HARD DISK recovered for use!!!

Is possible make a program how this???

Thanks in advance Katy if you answer...

Have you a nice day...

Javier...
////////////////////////////////

I not understand why not is make a programa how this, I have 4 hard disk with bad sectors, and not need the info in this disks, if any programa check not one by one, but 100 by 1000 sectors, the 10 Hours reduce to:

10 Hours * 60 Minutes = 600 Minutes
600 Minutes / 1000 = 0,6 Minutes!!!

And not attempt to recover, one sector are difficult to check? Inmediately mark bad this sectors and the 1000 previous not checked sectors, and continue...

With this, si possible lost any GB of the hard disk, but in 2 minutes aprox, Disk OK for use!!!

Not understand how any so simple not is make...

Or Shail or any person know a program that make this??? Is possible check 1000 by 1000 sector, not one by one sector, and directly mark bad - and the prev 1000 sectors not checked - when any sector delay many milliseconds in retrieve...???

Thanks in advance Shail and any, and sorry for possible errors in my writing, my English not is very good, sorry...

Good day at all!!!
Avatar of rindi
NTFS is legacy, closed source belonging to m$. So there is no source code available for it.
Microsoft did never open the code for NTFS. All the info available about NTFS is gathered by users all over the planet.
Avatar of astrix2

ASKER

Ok, thanks, but grrr with microsoft... hehe...

And, any know any program for mark one sector as bad, not scan, mark a sector how bad but the sector one wants...

Because I have an idea for make this, and is use the victoria program and with VB manage the program with sendkeys, I make other times this technique...

Victoria scan and put in a map if any sector delay times, how this image:

User generated image
And managing sending keys and simulate clicks putting the mouse in some coordinates (start, stop, capture the image of program, check if have red rectangles, or green, or, any want, save the results, and put new begin sector for scan, etc, appear to be complex, but not is very complex in reality, I make many times, one was to check an online roulette program, with sendkeys and capturing image of results I check if the casino program online have trap, and yes, HAVE, I leave the program in a secundary pc I not use working 2 or 3  days constantly and 3 or 4 (not remeber well at now) times do more than 12-14 black or red followed, in the real life not are probable) is possible make a quick scan and know the zones - not exact, many sectors are good, but lost 30 or 40 gb in a 2 TB hardisk not is very bad - that the disk fails...

But after is neccessary a program that mark the sectors, any know a program that mark a sector as bad, sector one wants?

Thanks in advance...

Good day....
As there is already proven software to do this, ie mark sectors as bad.

Explain why you want to do this again?
Avatar of astrix2

ASKER

Gerald Write:

///////////////////
As there is already proven software to do this, ie mark sectors as bad.

Explain why you want to do this again?
///////////////////

Where is the software for mark one want sector as bad???

Is the only thing I need, because with Software Victoria and a little program in VB with sendkeys and simulate clicks is possible for me check surface of disk in 100 by 100 sectors or 1000 by 1000 sectors, or the quantity I like sectors...

One good thing are have source code how victoria or hdd regenerator for scan disks sectors and if have source code I have the possibility of scan in steps, but because appear to be not exits that source code, I only need a program that MARK sectors as bad, sectors I like, not automatic...

Thanks in advance...
Bad sectors happen on the disk and don't have much to do with the file-system (NTFS). HDD Reg doesn't even care about what file-system is used or how the disk is partitioned. It looks at the disk at a lower level. Probably using commands that directly talk to the disk (SATA or SAS commands). So basically just ignore NTFS...
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.