usb ramdisk

Hi guys,

I need your advice :slight_smile:
There is usb device that when plugged in creates FAT partition in windows and app that is writing there some files.
Now, I need to tweak this app in a way I could use it without HW, data that is written there is temporary and doesn’t matter.

Question is how to simulate it?
I was thinking about extending ramdisk driver, but it may not work, as this is not really usb device. So maybe I should use emulated device and somehow extend it with ramdisk cfuctionality?

What you advice here?
Thanks a lot!

Sorry but I don’t understand your requirements at all. Why would you want a
“usb” ramdisk?
Mark Roddy

Hi, Point is that from weird reason moonted ramdisk doesn not work. My guess is that app is looking for usb device or so first before connecting to volume… i will reverse it today or tomoorow And will see what is goong on. But do you think that in such case regular ramdisk should be enough? Thanks

Sure, you will be the 10,000 person here to go off and implement some
variation on the ramdisk sample.

Mark Roddy

We’ve all been there.

Actually, a ramdisk is not a bad exercise to learn about the storage stack.
I’ve implemented them as disk class drivers, SCSI miniports, and SCSI
storports. ramdisk are useless for any sort of production environment
unless there is some VERY specific requirement. However, I would not
discourage anyone for trying to write one. Using any one of a plethora of
examples on GitHub, you can have a working driver in a day or two to start
tinkering with.

On Wed, Dec 12, 2018 at 12:47 PM Mark_Roddy
wrote:

> OSR http://osr.vanillacommunities.com/
> Mark_Roddy commented on usb ramdisk
>
> Sure, you will be the 10,000 person here to go off and implement some
> variation on the ramdisk sample.
>
> Mark Roddy
>
> –
> Reply to this email directly or follow the link below to check it out:
> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>
> Check it out:
> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>

Pavel:

How about writing the data to a file on a local disk until the device is
inserted?

On Wed, Dec 12, 2018 at 12:59 PM Jamey Kirby <kirby.jamey> wrote:

> We’ve all been there.
>
> Actually, a ramdisk is not a bad exercise to learn about the storage
> stack. I’ve implemented them as disk class drivers, SCSI miniports, and
> SCSI storports. ramdisk are useless for any sort of production environment
> unless there is some VERY specific requirement. However, I would not
> discourage anyone for trying to write one. Using any one of a plethora of
> examples on GitHub, you can have a working driver in a day or two to start
> tinkering with.
>
>
>
> On Wed, Dec 12, 2018 at 12:47 PM Mark_Roddy
> wrote:
>
>> OSR http://osr.vanillacommunities.com/
>> Mark_Roddy commented on usb ramdisk
>>
>> Sure, you will be the 10,000 person here to go off and implement some
>> variation on the ramdisk sample.
>>
>> Mark Roddy
>>
>> –
>> Reply to this email directly or follow the link below to check it out:
>> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>>
>> Check it out:
>> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>>
>
>
> –
> Jamey Kirby
> Disrupting the establishment since 1964
>
> This is a personal email account and as such, emails are not subject to
> archiving. Nothing else really matters.

></kirby.jamey>

Here are a couple of informative links:

https://stackoverflow.com/questions/5016363/creating-a-virtual-usb-device
http://usbip.sourceforge.net/

On Wed, Dec 12, 2018 at 1:04 PM Jamey Kirby <kirby.jamey> wrote:

> Pavel:
>
> How about writing the data to a file on a local disk until the device is
> inserted?
>
>
> On Wed, Dec 12, 2018 at 12:59 PM Jamey Kirby <kirby.jamey>
> wrote:
>
>> We’ve all been there.
>>
>> Actually, a ramdisk is not a bad exercise to learn about the storage
>> stack. I’ve implemented them as disk class drivers, SCSI miniports, and
>> SCSI storports. ramdisk are useless for any sort of production environment
>> unless there is some VERY specific requirement. However, I would not
>> discourage anyone for trying to write one. Using any one of a plethora of
>> examples on GitHub, you can have a working driver in a day or two to start
>> tinkering with.
>>
>>
>>
>> On Wed, Dec 12, 2018 at 12:47 PM Mark_Roddy
>> wrote:
>>
>>> OSR http://osr.vanillacommunities.com/
>>> Mark_Roddy commented on usb ramdisk
>>>
>>> Sure, you will be the 10,000 person here to go off and implement some
>>> variation on the ramdisk sample.
>>>
>>> Mark Roddy
>>>
>>> –
>>> Reply to this email directly or follow the link below to check it out:
>>>
>>> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>>>
>>> Check it out:
>>> http://osr.vanillacommunities.com/discussion/comment/291799#Comment_291799
>>>
>>
>>
>> –
>> Jamey Kirby
>> Disrupting the establishment since 1964
>>
>> This is a personal email account and as such, emails are not subject to
>> archiving. Nothing else really matters.

>>
>
>
> –
> Jamey Kirby
> Disrupting the establishment since 1964
>
> This is a personal email account and as such, emails are not subject to
> archiving. Nothing else really matters.

></kirby.jamey></kirby.jamey>

a ramdisk is not a bad exercise to learn about the storage stack

I emphatically disagree. Writing a RAMdisk driver is a terrible way to learn about the storage stack.

I would not discourage anyone for trying to write one

And I would.

Using any one of a plethora of examples on GitHub

Gad… Save me!

A ramdisk driver doesn’t fit in any specific category or group within the storage stack; It doesn’t follow any of the rules that the pieces of the storage stack follow. It’s basically an aberration and a hack. You learn nothing from such an exercise other than how to create an aberrant hack.

You want to learn about the Storage Stack? Write a WDF filter driver that filters the piece of the Storage Stack you’re interested in learning about. You’ll learn how to write a proper driver, while you write some code for something useful, that actually has a proper place in the stack. Not something that’s tacked-on to someplace where it doesn’t belong.

Here are a couple of informative links:

Both referencing WDM. Even worse, the project is a WDM bus driver. Did you actually look at the Power Management code in that driver?

Peter

Guys,
thank you for answers.
But getting back to my issue:

I need to hack 3rd party app to make it working with my virtual device.
To do this I need to have virtual USB stick - how to achieve it in easiest way?

@Jamey

How about writing the data to a file on a local disk until the device is inserted?
Well I was thinking about it, but unsure how to do it (I was never doing usb developement) …
Do you know some usb cdrom sample or other storage usb driver?
In the past I was creating kind of virtual disk driver, but it was long time ago and it was … wdm. Never developed usb stack, thus in this area Im newbe ;( and this is why Im asking for help, sample, advice…
Jamey, thank you for pointing me usbip sample I will read it and learn, but looking at it - this looks like bus, and my case is storage isn’t it?

@Peter,

Both referencing WDM. Even worse, the project is a WDM bus driver
Can you give me some sample or share some code etc how to do it in modern way?

Assuming I will create some usb vritual storage driver (still dont see even stub for such in windows driver samples ;() - how exactly usb storage drivers expose disk functionality> thru irp_read/irp_write? Sorry for dumb questions.

Thanks.

Please, OP… slow down. You’re jumping from “problem” to “write code” without going through “analyze problem” and “design solution”.

So, let’s start: HOW does the application know to open, specifically, a USB device? We can’t emulate a device, unless we know EXACTLY what the app is looking for. WHAT does the app try to open, when, and how does it choose the unit to open?

After we understand what the actual root problem is, we can design a solution… OK?

Peter

Peter,
Thank you. Will get this app soon, and this is what I will do first: will reverse it and check how it looking for device.

The USBIP is similar to what you may want to do.

Simplified: USBIP creates a virtual HBA, packages up the USB packet, and
sends it to the remote computer. In turn, the remote computer take the USB
packet from the network, sends it to the real USB device that is being
shared, and passes the results back over the network.

Now, take out the network part, put both the server side logic and the
client side logic in the same driver with your stuff in the middle.

On Thu, Dec 13, 2018 at 3:03 AM Pavel_S
wrote:

> OSR https://community.osr.com/
> Pavel_S commented on usb ramdisk
>
> Peter,
>
> Thank you. Will get this app soon, and this is what I will do first: will
> reverse it and check how it looking for device.
>
> –
> Reply to this email directly or follow the link below to check it out:
> https://community.osr.com/discussion/comment/291820#Comment_291820
>
> Check it out:
> https://community.osr.com/discussion/comment/291820#Comment_291820
>

Well no, that is not going to be trivial. USBIP assumes that there is a
functional USB device on the remote that is providing URBs for USBIP to
serve up. “Your stuff in the middle” will have to be a full emulation of a
usb storage device.

Mark Roddy

Of course. I assumed that part was obvious. Pav is no slacker.

Hi guys It appears this soft is using setupapi to get access to device with some vendor specifix settings, thus thing that seem to be needed is in fact virtual usb stick/disk with same sertings set (vid,pid) Does it clears you something? I was wondering if proper way of emulation of such device would be to create new system bus that apon receiving magic ioctl will add new PDO to bus with exposed USB interface. Do you think this would be good way? If so, then thing I dont know yet is how to create such PDO in a way usbstor would take care of rest… do you see such way to make virtual environment so that default mass storage stack would be loaded? Or maybe you will have some more suggestions? @Jamey Im inspecting usbip code in meantime. thank you

Pavel_S wrote:

It appears this soft is using setupapi to get access to device with some vendor specifix settings, thus thing that seem to be needed is in fact virtual usb stick/disk…

Does it clears you something?

I was wondering if proper way of emulation of such device would be to create new system bus that apon receiving magic ioctl will add new PDO to bus with exposed USB interface.

Do you think this would be good way?

If so, then thing I dont know yet is how to create such PDO in a way usbstor would take care of rest… do you see such way to make virtual environment so that default mass storage stack would be loaded?

No, it’s not a good way.  It’s certainly possible for you to create a
fake device with a device ID that matches the USB device, as well as a
“compatible ID” for USB mass storage class device that would cause
usbstor.sys to get loaded.  However, that means your driver has to
respond exactly like a USB mass storage device, sucking up wrapped SCSI
commands and responding with the correct data and the correct timing. 
It’s all the pain of a RAMdisk combined with the pain of USB.

What’s the point of all of this?  Wouldn’t it just be easier to buy
several of the target disk drive and let it do its thing?

Wouldn’t it just be easier to buy several of the target disk drive and let it do its thing?
unfortunately no, this will be too expensive solution.

From Win10there is also UDE - not sure however it suitable for this solution… what do you think?

However, that means your driver has to respond exactly like a USB mass storage device
is there a sample I could at least look for?

Thanks

Not really. There is a spec somewhere in the usb spec archive for it. You
can reverse engineer the urb flow using (I think, haven’t tried it) the
microsoft or wireshark usb trace features in the perf stuff from msft or
the usb option in wireshark.

Mark Roddy

Tim

Wouldn’t it just be easier to buy several of the target disk drive and let it do its thing
After reading Mark last comment I’m coming back to this idea.
So assume we will buy some sticks - do you guys think it would be easy to tweak usb stick in a way I could change vid/pid usb identifiers somehow to make it be recognizable by setupapi? Maybe I could do some filter driver attached to device and change some descriptors…?

Thanks,

Pavel_S wrote:

So assume we will buy some sticks - do you guys think it would be easy to tweak usb stick in a way I could change vid/pid usb identifiers somehow to make it be recognizable by setupapi? Maybe I could do some filter driver attached to device and change some descriptors…?

Frankly, it would be much easier to use something like Detours in user
mode to hook the application’s call to CreateFile and modify the file
name.  The app would get a handle it could use, and would never know
that it was diverted.