none
Create file memory mapping in a driver? RRS feed

  • Question

  • Is there a driver level API to map a disk file to memory? Similar to CreateFileMapping() and friends for user space apps.

    If the answer is positive, than any remarks on the subjects are also welcome. And any links to relevant docs/discussions.

    The context is that there is a "firmware library file" that the driver should open, parse and handle requests to upload firmware modules to the underlying device. Modules are nicely alligned in the binary and uploaded on power state transitions and on specific requests. And it seems like a good idea to handle the file access by mapping it to memory. But obviously there may be concerns...

    The driver is a KMDF driver.

    Friday, July 5, 2013 1:55 PM

Answers

  • You can do it with ZwCreateSection and ZwMapViewOfSection.  But before you run off and do this, several questions are in order:

         1.  Why is the driver the one opening and parsing the firmware file?   Much of this could be done in a user space program, and putting the code in the kernel may not adding anything but more chances to crash.

         2.  Depending on the size of these firmware files could you pre-parse it and put it in the registry?  Putting configuration data in files is not the normal Windows model.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Friday, July 5, 2013 2:03 PM

All replies

  • You can do it with ZwCreateSection and ZwMapViewOfSection.  But before you run off and do this, several questions are in order:

         1.  Why is the driver the one opening and parsing the firmware file?   Much of this could be done in a user space program, and putting the code in the kernel may not adding anything but more chances to crash.

         2.  Depending on the size of these firmware files could you pre-parse it and put it in the registry?  Putting configuration data in files is not the normal Windows model.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Friday, July 5, 2013 2:03 PM
  • Don, thanks for your prompt response. I'll study the Zw* API you've refered me to and, well, possibly come up with more questions :-)

    And yes, your concerns are valid and I'm also strongly against perfoming this kind of processing in kernel. But I'm afraid that in this case there is no other way. Due to strict timing requirements basically. And this is not about configuration data, this is binary code to be executed by the device's hardware. The total size may be quite large, like tens of MB.

    So, are there any additional concerns associated with mapping the file into kernel memory, compared to plain opening for reading with ZwCreateFile()?

    Friday, July 5, 2013 3:58 PM
  • On the other hand, why go through the memory mapping pain at all? Open the file, get the size, allocate a buffet and just read the entire contents. If this a boot driver, you must go the registry route since the file system is not up yet

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, July 5, 2013 4:04 PM
  • True, this would be a default approach. But then I'd have to keep the entire file contents in the memory all the time. OK, finally it will be paged to disk, and than it's all the same. But at the very start memory mapping consumes less resources and seems more efficient. Just considering options... 

    Krzysztof

    Friday, July 5, 2013 4:24 PM
  • How big is the file? "it is more efficient and consumes less resources" is purely a guess without measurement

    d -- This posting is provided "AS IS" with no warranties, and confers no rights.

    Friday, July 5, 2013 5:48 PM