Unique ID for removeble hardware

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
User avatar
Holus
Member
Member
Posts: 51
Joined: Thu Jan 27, 2011 5:57 pm

Unique ID for removeble hardware

Post by Holus »

My OS must be bootable from HDD and USB.
But some hardware (like USB) is easy to remove and replace.

Is there a Unique hardware of the device I boot from so I can check before destroing a USB-stick with OS info. I can make a function that checks if the bootdevice is unpluged and tell te user to put it back. But I want to check-it befor writing to it.

Is there a way to check it's the same device with-out reading from the device (and check some signatures on a sector. (that would be to slow))

I found that INT 0x13 function AH=48 tells me the PCI port that was used for the device. So I think I can check the VENDOR and DEVICE_ID. But I have 10 USB sticks with the same VENDOR and DEVICE_ID. Is there a other way?
Computer says NOOOO
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Unique ID for removeble hardware

Post by Solar »

Holus wrote:I want to check-it befor writing to it.

Is there a way to check it's the same device with-out reading from the device (and check some signatures on a sector. (that would be to slow))
Since you are going to write to it anyway, I don't see why reading the device first would be "too slow"...?
Every good solution is obvious once you've found it.
User avatar
Holus
Member
Member
Posts: 51
Joined: Thu Jan 27, 2011 5:57 pm

Re: Unique ID for removeble hardware

Post by Holus »

Solar wrote: Since you are going to write to it anyway, I don't see why reading the device first would be "too slow"...?
Yes. But I do not want an write action to be 1 read and 1 write action.
Waiting and checking the status 2x. Wait for the IRQ 2x. Process te information 2x.
Is there an other way??
Computer says NOOOO
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Unique ID for removeble hardware

Post by Combuster »

For USB: write a proper driver so you can actually detect plug events.

In general there's a certain amount of time needed for a user to physically switch USB sticks (the same goes for floppy drives where it's generally not possible to tell if it's present), if you haven't written or read the device in the past n seconds then you may want to check the signature again. If the device gets unplugged live, the read or write will return a failure so that you know it's not there.

In other words, there should not be a read for every write
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
User avatar
Holus
Member
Member
Posts: 51
Joined: Thu Jan 27, 2011 5:57 pm

Re: Unique ID for removeble hardware

Post by Holus »

Combuster wrote:For USB: write a proper driver so you can actually detect plug events.

In general there's a certain amount of time needed for a user to physically switch USB sticks (the same goes for floppy drives where it's generally not possible to tell if it's present), if you haven't written or read the device in the past n seconds then you may want to check the signature again. If the device gets unplugged live, the read or write will return a failure so that you know it's not there.

In other words, there should not be a read for every write
Thanks. Read 1x on plug-in and detect. That sounds like a nice simple solution.
Is there a unplugsignal to an IRQ?
Computer says NOOOO
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re: Unique ID for removeble hardware

Post by Brendan »

Hi,
Holus wrote:Thanks. Read 1x on plug-in and detect. That sounds like a nice simple solution.
What are you reading? If you read a magic signature from a sector, then the user can clone the raw data (e.g. copy the raw data onto other USB sticks), and you won't be able to tell the difference between the clones.

For all USB devices; when a USB device is plugged in you first have to find out what the device is (get the class and subclass codes to determine if it's a storage device or a keyboard or mouse or whatever). To do this you ask the device for a "Device Descriptor". In addition to class and subclass codes, the "Device Descriptor" includes a vendor ID, a product ID and a serial number. The serial number is a string of at least 12 Unicode characters that must be unique for the vendor ID and product ID; and by concatenating "vendor + product + serial" you end up with a globally unique identifier.

Basically, you have to get this data from the device anyway (to determine what sort of device it is), and can use it to determine if the device is the same one.

Note: For hard disks it's slightly similar. You have to get device information (e.g. the "identify device" command), and (for modern drives) this data includes an (up to) 40 byte serial number.


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply