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 */

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值