Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Programming and Hacking > Development

Reply
 
Thread Tools Search this Thread Display Modes
Old 17th December 2022, 14:59   #1  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,167
Reading serial number of SD cards on Windows

When some person or company use cheap SD cards for content storage (like field camera production) it is sometime required to read serial number of SD cards to identify card inserted into cardreader. Like to read data from registered card or count number of records to card or to have database of the cards used without ugly attempts to paint some ID on card by hands.

SD card have serial number (like 32bit) placed into 16bytes CID register. There is a hardware protocol to read CID from card but it generally not directly supported by most widely used cardreaders via USB.

Standard Windows driver support only something like USB Mass Storage protocol. It is media independent and looks do not have any additions to support special flash cards protocols in its minimum specification.

So any flash card USB cardreader have a special processor to interface with host using USB and USBMS protocol and the special flash card protocol with the card inserted.

For SD cards or may be other cards Microsoft design SFFDISK series of IO control codes for user-space applications to exchange data with sort of Small Form Factor Disks devices. That is SD cards and may be other. It allow to send command to device and get reply. It looks default Microsoft USBMS driver do not support these IOCTLS at all.

So to read SD card serial number at user side we need:
1. Hardware cardreader supporting SD card commands (command 10 for reading CID).
2. Device driver for hardware and supporting SFFDISK IOCTLs for sending command (command 10) and getting data back.
3. Some user application to call DeviceIoControl() WinAPI function with data structure for command 10 and displaying result.

Now started some quest to find 1+2+3 working.

3. User application in C is the simplest small console software for getting drive/device handle from drive letter with SD card inserted (work with user rights) or physical drive number (require administrator access level).

After getting 3. working we can start to search cardreader supporting required IOCTLs. It was found only Realtek-chips based cardreaders support these IOCTLs without errors. But return good (non-zero) data only for commands 9 and 13. For command 10 return non-error but zeroes only. From Realtek chips tested - many USB2 and USB3 tested cardreaders work with most of released drivers. So with devices like Transcend RDF9K you can set nice USB3+ read speed with serial number of SD card reading in single device. (RDF8 also USB3 is NOT Realtek-based and not read CID). But you need to download Realtek USB cardreader driver from device manufacturer website and install manually. Unfortunately Realtek do not have official drivers for cardreader chips on its website and we only can get drivers from sites like Lenovo or may be Dell and other end-user hardware vendors. Or driver-helpers sites.

Now is the last but not completely solved step:
Realtek USB cardreader drivers (most of versions from 201x and 202x years) tested only send commands 9 and 13 (like card status and some other data requiests) to cardreader via USB bus. It may be some internal politics of Realtek not read CID with officially released drivers ? I send many e-mails to Realtek but got zero reply at all. It looks they not only release any drivers to end-users but also not provide any support to end-users.

So the next ideas were looking into USB sniffer output: Commands 9 and 13 cause some packets walking to cardreader and back and getting SD card commands reply via USB bus back. So Realtek have some additional protocol implemented over USBMS or near to support SFFDISK IOCTLs for sending SD card commands and receive reply. But it is driver that not send command 10 and only return non-error reply and zeroes.

So using WinDbg and IDA opensource disassembler and some nicely error messages commented Realtek driver version 10/26/2021,10.0.22000.31271 I quickly enough found function where SD card commands processed. But it was found only commands 9 and 13 are really send downstream to the cardreader. As the response type and size for commands 9 and 10 are equal I currently replace command 9 processing to command 10 and it finally start to work as CID reading setup. So it is 2-bytes driver patching for replacing integer compare instruction and operand to register loading from byte 0x9 to 0xA.

The resulted state - we lost CMD9 response (return zeroes) but got CMD10 with CID and serial number response now. Also as driver patched it lost compatibility with Microsoft driver signing and can not be loaded in the full security enabled Windows10 boot. Only can be started in test driver signing mode or in the enabled non-signed drivers mode.
I write many e-mails to support of Lenovo (built-in Realtek USB2 cardreader) and to Kingston and to Transcend external cardreader manufacturers support about this strange driver issue. But all report something like 'reading CID and serial number of SD card is not announced as product feature so no support possible'.

The compiled command line tool and patched driver is https://drive.google.com/file/d/1yZC...ew?usp=sharing for checking. To run it require 2 params - drive letter and command number. So if SD card in driver letter E it is
Code:
sdcard E 10
As practice shows most new Realtek driver work with most old cardreaders so single driver can be used as one for all products. Typical user-side difference from standard Microsoft driver is displaying some new/other/nice drive icons in File Explorer (and may be other file managing dialogs windows) when card inserted.

So to making last step to read serial number of SD cards in Windows in 'normal boot mode' we need either finally signed or fixed for command 10 driver from Realtek or signing of patched driver with Microsoft root-certificate signing. Test free certificate signing not work in standard boot mode of Windows10 too. To work with full security enabled.

Last edited by DTL; 17th December 2022 at 15:04.
DTL is offline   Reply With Quote
Old 17th December 2022, 18:22   #2  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
Dont know if this of any help,
NirSoft has a gigantic collection of quality system tools[A bit like SysInternals collection]:- https://www.nirsoft.net/
The Full Index:- https://www.nirsoft.net/utils/index.html#system_utils

UsbDeview:- https://www.nirsoft.net/utils/usb_devices_view.html
DevManView:- https://www.nirsoft.net/utils/device_manager_view.html

DevManView shows this for Card Reader Device:


DevManView shows this for a specific SD_Card in Card Reader Device:


UsbDeview shows this for a the same SD_Card in Card Reader Device:


Perhaps the Serial Number shown in UsbDeview image is the same thingy that you seek [or maybe for the card reader].

I once made an enquiry to NirSoft guy [Nir Sofer], but got no reply,
Perhaps if you ask for source, and sound like you have some idea of what you are doing, he might assist.
[He does provide source code for one or two of his utilities]

Hope this helps, good luck.

EDIT: OOooops No, has same serial number in the DebugView device Instance ID, so must be for the card reader, Sorry.

EDIT:
Quote:
USBLogView v1.26
USBLogView is a small utility that runs in the background and records the details of any USB device that is plugged or unplugged into your system. For every log line created by USBLogView, the following information is displayed: Event Type (Plug/Unplug), Event Time, Device Name, Description, Device Type, Drive Letter (For storage devices), Serial Number (Only for some types of devices), Vendor ID, Product ID, Vendor Name, Product Name, and more...
You can easily select one or more log records and then export them into csv/tab-delimited/xml/html file.
I'll check if above of any good.

EDIT: NOPE, dont work for my internal card reader, only USB device plug/unplug.
EDIT And UsbDriveLog dont work either.

EDIT: DriveLetterView does show Volume Serial Number but that aint what you are looking for.

Its not looking good, sorry.
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 17th December 2022 at 19:01.
StainlessS is offline   Reply With Quote
Old 17th December 2022, 20:09   #3  |  Link
DTL
Registered User
 
Join Date: Jul 2018
Posts: 1,167
"DriveLetterView does show Volume Serial Number but that aint what you are looking for."

Yes - it is logical filesystem storage volume serial number and it is changed after media formatting by Windows or in camera.

I see using search over Internet some USB cardreaders priced about $100 with software for reading and displaying CID of SD card. It may be used in the car service because some software for cars or databases may use this number somehow for product copyprotection. So may be Realtek and other vendors hide command 10 or any SFFDISK IOCTLs processing in windows drivers to not allow user to read CID and serial number of the SD card for some security reasons. Though if user legally buy Windows and USB card reader and SD card I do not see if it may be non legal to read CID and serial number of SD card described in the open documentation of SD cards protocol on the SCA website.
Though I read many of UNIX/Android devices with non-USB cardreader connection read and display CID of SD cards by default as virtual filesystem object. The Microsoft Windows still not have some system management data for reading of SD memorycard serial number yet but provide an API for user application to send command to SD card and process the reply. The only required support from device driver and hardware of SD cardreader. Also it looks this support not mandatory for SD cardreader manufacturer and for driver WHQL checking by Microsoft. As I read from Lenovo support - they not test Realtek cardreader drivers for processing informative SD cards commands at driver testing and not ask Realtek to support CMD10 as required. So it looks supporting of Microsoft API of SFFDISK IOCTLs is optional for cardreader manufacturer and typically not officially announced if hardware product support it or not or partial support only.

So practically at the 2022 we have tons of USB cardreader products on the market for Microsoft Windows with limited or no support of current Microsoft API for SD (and may be other type) of cards. Also as standard Microsoft driver for USBMS device do not support SFFDISK IOCTLs it looks it is not mandatory for hardware device to support it an only an option for vendor driver to support or not and which commands to support.

So Internet is loaded with many examples of attempts to read CID of SD card using open Microsoft API of DeviceIoControl with SFFDISK_DEVICE_COMMAND IOCTL but it usually not work for USB cardreaders because typically it is not implemented by hardware and/or driver of the device. Like this blog post https://jo0ls-dotnet-stuff.blogspot....mentation.html . The API looks like dated about beginning of 200x with Windows2000 or XP but still not completely supported by hardware manufacturers. Realtek support it partially in drivers (only CMD9 and CMD13) and many other vendors like Genesys Logic (Transcend RDF8 cardreader as example) not support at all.

Last edited by DTL; 17th December 2022 at 20:22.
DTL is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 13:25.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.