Newsgroup: it.comp.os.linux.development
Titolo: c code that can retrieve "Drive Serial Number" from the Harddisk firmware.

hi all,
Any experts can help me to get the c ocde that can  retrieve "Drive Serial
Number" from the Harddisk firmware.
Thank you very much

Number" from the Harddisk firmware.
Look at the source for hdparm program - that does show the serial number
so it apparently contains the necessary code.
Then, if you're planning to use that for something like copy protection,
there are some issues you need to consider:
- what happens to your customer when s/he just decides to change the
  hard disk (after a disk crash, or just to get a bigger disk, or
  after a total system upgrade)? How does the customer re-brand
  the product?
  - even more importantly, what happens to the customer in the
    above case after your company has gone belly-up (or been acquired),
    or just has decided to drop all support for the product
- does this even work at all if the customer happens to have SCSI
  disks instead of IDE?
- as the query goes through many software layers, any of these
  software layers can be changed to return either falsified
  serial numbers, or to clean up the serial number information
  completely
So, most hw-based copy protection screws the honest customers rather
nastily, while performing its intended function sloppily.
What _might_ work, at least for some reasonable period of time, would be
to provide the customer a USB-connected hardware token, and have your
software authenticate against that. At a minimum the token might just
contain some ROM where an activation key is stored (but then, here the
key could be captured in transport, and thus makes this implementation
have the same vulnerabilities as the HD serial number has), so a better
would be to have the token contain some small crypto processor and some
key data. Your software would then need to send a message to the device,
and expect the returned message to be signed with some specific encryption
key.
But then, the encrypting tokens are not free - so this'll increase the
price of your software. And there's still the risk of losing or breaking
the token - so there would need to be a way for the licensees to acquire
new tokens to replace the lost/broken/malfunctioning ones, without paying
the full shelf price of the software.
-- 
Wolf  a.k.a.  Juha Laiho     Espoo, Finland
(GC 3.0) GIT d- s+: a C++ ULSH++++$ P++@ L+++ E- W+$@ N++ !K w !O !M V
         PS(+) PE Y+ PGP(+) t- 5 !X R !tv b+ !DI D G e+ h---- r+++ y++++
"...cancel my subscription to the resurrection!" (Jim Morrison)

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <linux/hdreg.h>
#include <fcntl.h>
#include <errno.h>
int main(int argc, char *argv[])
{
struct hd_driveid id;
int fd = open("/dev/hda", O_RDONLY|O_NONBLOCK);
if (fd < 0)
{
        perror("/dev/hda");
        exit(errno);
}
if(!ioctl(fd, HDIO_GET_IDENTITY, &id))
{
    printf("Serial Number=%s\n",id.serial_no);
     //printf("Model Number=%s\n",id.model);
     //printf("Firmware Revision=%.8s\n",id.fw_rev);
     //printf("Cylinders=%d\n",id.cyls);
     //printf("heads=%d\n",id.heads);
     //printf("Sectors/Track=%d\n",id.sectors);
}
return EXIT_SUCCESS;
}

nkl wrote:

Someone recently posted a simple source code to retrieve a serial number of
hard drives. If you do a search on google, you should be able to locate the
source code.