Linux下读硬盘序列号的程序

Linux下读硬盘序列号的程序

/*
* gethddsn.c
*
* Get serial number of ide hard disk.
* example: my Maxtor 15G 's s/n is K306S04C.
*
* Compile with: gcc -O2 gethddsn.c
*
* Xiaoming DONG <xmdong@263.net>
* Aug 24, 2000
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>  /* ioperm() */
#include <asm/io.h>  /* outb(), inb() */

/* get serial number */
int gethddsn(char *ide)
{
  unsigned int ide_info[257];
  unsigned int info_off;
  unsigned long loop2=0;
  int loop, loop1=0;

  if (ioperm(0x1f6, 1, 1)) {
    perror("ioperm"); exit(-1);
  }
  outb(0xa0, 0x1f6);
  if (ioperm(0x1f7, 1, 1)) {
    perror("ioperm"); exit(-1);
  }
  outb(0xec, 0x1f7);
  do {
    if (ioperm(0x1f7, 1, 1)) {
       perror("ioperm"); exit(-1);
    }
  } while (inb(0x1f7) != 0x58 && loop2++ < 0xffff);
  for (info_off=0; info_off != 256; info_off++) {
    if (ioperm(0x1f0, 2, 1)) {
     perror("ioperm"); exit(-1);
    }
    ide_info[info_off] = inw(0x1f0);
  }
  for (loop=10, loop1=0; loop<=19; loop++)
  {
    ide[loop1++] = (char)(ide_info[loop] / 256);
    ide[loop1++] = (char)(ide_info[loop] % 256);
  }
  ide[loop1] = 0;
  if (loop1 > 40) printf("*error*/n");
  return 0;
}

void main()  /* print hard disk number */
{
  static char serial[41];
  gethddsn(serial);
  printf("IDE hard disk s/n: %s/n", serial);
}
/* end of gethddsn.c */
阅读更多
文章标签: linux ide disk .net gcc
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭