How to set system time in a Windows WDM driver? - c

We are developing a GPS time sychronization hardware device. And we want write a Windows WDM driver for the device. To avoid the user level latency, we'd like set windows( XP/Vista/7) system time in the kernel driver level. Does anyone know how to achive that?
Thanks
Xu Jun

Did you try NtSetSystemTime? I think it's undocumented.
http://undocumented.ntinternals.net/
See also http://undocumented.ntinternals.net/UserMode/Undocumented%20Functions/Time/NtQuerySystemTime.html
Yyou should use the Zw-prefix functions in your driver.

Related

Get system date/time via USB

Is there any way to query the system's date/time via USB without installing anything on the host computer (maybe just drivers)?
Background of the original problem
To avoid the XY problem, let me explain a bit what I'm trying to do.
To be able to calculate a TOTP token for 2FA (e.g. like Google Authenticator app does) you need a real-time clock to get the date and time.
There's this USB device called SC4-HSM that I would like to use to calculate the tokens, however it doesn't have a clock and according to the designer, adding one would be too expensive (needs a battery, etc).
Possible solution to the original problem
This device is going to be used with a computer which already has an RTC of course. Thus I had the idea of querying the system for a date/time which would solve the issue.
(Note: I know that a USB device can be connected to all sorts of hosts and not all hosts will have an RTC, but since this only needs to work with a computer, I thought this shouldn't be an issue)
My first thought was that there might be some USB device class that had date/time needs, so I could register the device as that type and then I would be able to query the values.
After going through the device class codes list (Internet Archive) nothing jumped at me as needing date/time. The closest ones I could think of were:
Content Security (PDF)
Personal Healthcare
Smart Card Class (PDF)
I skimmed the device class documents in the USB Implementers Forum but there's nothing in there even remotely related to date or time.
Current problem
Since the USB specs seemed like a dead-end I thought that maybe there was a way to write a very simple USB driver that can be auto-loaded when the device is plugged in to a computer and then we can use the driver to return the date/time when the device asks for it (unless I'm misunderstanding something).
I am now looking through USB development docs like Michael Opdenacker's Linux USB drivers course, I tried the Linux USB Project which seems dead. Skimmed through Driver Development for Windows NT just to get an idea, however I am still not able to figure out if this is possible or not, and how hard it would be.
I'm a complete beginner at this and maybe this is something out of my skill level, but I would like to figure out if will I need weird hacks and workarounds or is there a much more straightforward way to do this?
There seems to be little information about it or I'm just searching the wrong places.
Any ideas/or pointers on either solving the original problem or the current one?
system time is not necessarily the general time i.e. the 'atomic' time you get from a NTP server
the most obvious solution is to use autorun, this is also possible on linux but normally autorun is blocked so the user explicitely has to activate it
https://askubuntu.com/questions/642511/how-to-autorun-files-and-scripts-in-ubuntu-when-inserting-a-usb-stick-like-autor
the linux command to get the time is date or hwclock or if the computer is connected to the net it may be possible to contact a NTP server (if the firewall does not block this)
then your autorun program has to send the data to the SC4-HSM. i do not know what USB classes the SC4-HSM implements if it implements CDC ACM (virtual COM port) this is easy:
Unable to sync computer time to Arduino via USB
(something like echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/tty.usbmodemfa131)
maybe it is possible to access system time over the USB drivers, i do not know this right now

Disabling Linux driver for Arduino issue

I'm writing a kernel module which will be kind of a driver for my Chinese Arduino.
At one moment I had to disable existing arduino driver and make sure that arduino is not determined by system. I did it with rmmod command. Unplugged and plugged in my arduino. But it still determined in /dev/ as ttyUSB0, same as it was before.
Does it mean that I have more drivers on my system which I don't know about? Or every hardware plugged in usb port will be determined as a file regardless of driver existence? 0_0
Also, of course I have an arduino ide installed on my machine. Maybe there is a built-in driver? If so, how can I disable it without removing ide?
Thank you for the answers.
Does it mean that I have more drivers on my system which I don't know
about?
Kernel may be configured to use a particular driver while user space still might change that.
The manual way: rmmod your driver, unplug Arduino, check system log with dmesg. Plug in Arduino, check dmesg again. Automated way: use usb-devices script from usbutils package (apt-get install usbutils) to list usb devices and the drivers associated with them.
After you find the associated driver, most likely ch341, rmmod it, or add to /etc/modprobe.d/blacklist to disable loading of this module permanently.

How to reset system in wireless driver?

Can I use emergency_restart() ?
or If I use kill(1, SIGTERM);
I need to include "signal.h", but it seems not work in wireless driver
embedded linux system
device: wireless AP
code section: wireless driver
As Basile says, kill() is a user space syscall, so you can't use it from a kernel module. It's a crude way to accomplish what you are trying to do anyways. Unless you have a reason to use emergency_restart() over kernel_restart() I would use the latter. The options you have can be found in reboot.h.
BTW, this is opening a security hole in your system. Since the driver is interpreting the data packet to determine if it is time to restart, there the OS cannot help to determine if that source has authority to perform such an action on your system. You may be better off with a user-space daemon which is run with the appropriate permissions to reboot the computer, and can act as an authentication agent and a reboot agent rather than doing it in your driver.

how to automatically start a PC using c

hey guys, is there any way i can automatically turn a pc on without having to go to BIOS?
ie from windows using a language or the like
If you want to turn on a PC remotely (i.e. from another computer connected to the network), you can use "wake on lan" to do it. Here is a C implementation: http://www.gcd.org/sengoku/docs/wol.c I have not tested this C code. I use a script in Perl to switch PCs on which telnets into a router and tells the router to send the so-called magic packet.
"Wake on lan" is a feature of the network interface so it may or may not be present on your computer.
No, there isn't. BIOS is essential for your Input-Output operations between Software and Hardware. Without a properly-setup BIOS, no hardware in your system will work at all.
Note that many programs have already been written for this purpose.
See:
Wake on LAN
Of course when the computer is powered off, no code will run, so the the answer to the title of your question is no. The actual body of your question seems to be a different question; what does the BIOS have to do with anything?
Technically is is possible without an external stimulus such as "wake-up on LAN" if your hardware and OS support the ACPI "wake-up on RTC alarm" feature. The RTC is battery-backed, and has an alarm feature that can trigger a wake-up.
However the issues/variable are: Hardware support, BIOS support, OS support, API support to access the protected RTC hardware resource in order to set an alarm in the first instance.
It sounds like you're asking how to call some sort of OS restart or shutdown feature (system call in UNIX/Linux, or I guess it would be a "service" in MS Windows).
In any event the details of how to start, restart or shutdown a system are platform specific and differ considerably from one OS to another ... and sometimes a bit among different hardware models and OS versions even within any given OS).

How to implement a USB device driver for Windows?

How should I approach implementing a USB device driver for Windows? How should I take into account different versions of windows e.g:
- Windows XP
- Windows Vista
- Windows 7
Is there open source solutions which could be used as a starting point? I'm a total newbie to windows driver development.
We have an embedded device with USB device port and we would like to have as low latency communication from the application level to the device as possible without sacrificing the data throughput. The actual data transferred is ADC/DAC data. Basically there is a lot of data which we need to transfer to a Windows machine as fast as possible.
We need more information about the device to point you in the right direction, but here are a few steps to get you started:
register with Microsoft Connect so you can download the Windows Driver Kit
register with osr-online as you'll find great articles, plenty of information, and a newsgroup dediciated just to Windows drivers -- this place is a goldmine
buy Developing Drivers with WDF, which will help you make sense of driver development on Windows and give you a good foundation to read articles from OSR and Microsoft
Hope that you can use UMDF (user-mode drivers) as you can use C++ and just write COM code. If you're doing anything with USB that requires kernel-space....you've got a lot of reading and learning to do for the next year!
To answer your question on versions, the Driver Kit has tools that will help you manage creating different drivers. If you write a good driver, it should run on all three OS with no problems, and the differences will just be in the config area (not the binary)
Basically, it depends on how complex your device is. What type of driver are you trying to write? File system? MP3 player? Camera? Modem?
If you end up having to write a kernel mode driver, let me know and I can point you to some good articles and what not.
I should also add that for around US $5,000, you can buy a license for WinDriver, a tool that takes all of the hard stuff out of driver development. You can use C++ or C# user-mode code to communicate with their driver that is custom generated for your device. This is the way to go if you have a tight deadline.
You can take a look at windows variant of libusb *here*. There are wrappers for many programming languages on official libusb site and on the web.
Start here: Windows Driver Kit Introduction
If you have some form of control over the device side, have it implement an interface for which Windows already provides drivers. E.g. the USB HID class (literally Human Input Device, but neither the Human nor the Input is mandatory) already has Windows drivers, and there is a reasonable Win32 API on top. You're not going to get data rates anywhere near 480 Mbps, though.

Resources