Article Title: Obtain the CPUID, hard disk serial number, and MAC address in Linux. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.
In the development of many system software, some system uniqueness information is required. Therefore, obtaining the host's CPUID, hard disk serial number, and the MAC address of the NIC becomes a very important application.
After some research on google, I basically implemented these functions. The following preparations are required:
GCC Embedded Assembly. For more information about GCC Embedded Assembly, see the relevant manual.
Ioctl system call. For details about the call method, refer to the manual page.
Obtain CPUID
According to the instructions provided on the Internet, CPUID is not supported by all Intel CPUs. If supported, the Assembly call is eax set to ipv_0003 and cpuid is called.
The following is the implementation code (on my CPU, It is not obtained ):
# Define cpuid (in, a, B, c, d) asm ("cpuid": "= a" (a), "= B" (B ), "= c" (c), "= d" (d): "a" (in ));
Static int
Getcpuid (char * id, size_t max)
{
Int I;
Unsigned long li, maxi, maxei, ebx, ecx, edx, unused;
Cpuid (0, maxi, unused );
Maxi & = 0 xffff;
If (maxi <3)
{
Return-1;
}
Cpuid (3, eax, ebx, ecx, edx );
Snprintf (id, max, "% 08lx % 08lx % 08lx % 08lx", eax, ebx, ecx, edx );
Fprintf (stdout, "get cpu id: % s \ n", id );
Return 0;
}
Obtain the hard disk serial number
This implementation uses reading the/etc/mtab file, finding the device file mounted to the/(root directory), opening it, and then calling ioctl.
The second parameter of ioctl is HDIO_GET_IDENTITY to obtain the identifier of the specified file descriptor.
The third parameter of ioctl is struct hd_driveid. in linux/hdreg. h, the declaration of struct hd_driveid has
Struct hd_driveid {
Unsigned short config;/lots of obsolete bit flags */
Unsigned short cyls;/* Obsolete, "physical" cyls */
Unsigned short reserved2;/* reserved (word 2 )*/
Unsigned short heads;/* Obsolete, "physical" heads */
Unsigned short track_bytes;/* unformatted bytes per track */
Unsigned short sector_bytes;/* unformatted bytes per sector */
Unsigned short sectors;/* Obsolete, "physical" sectors per track */
Unsigned short vendor0;/* vendor unique */
Unsigned short vendor1;/* vendor unique */
Unsigned short vendor2;/* Retired vendor unique */
Unsigned char serial_no [20];/* 0 = not_specified */
Unsigned short buf_type;/* Retired */
Unsigned short buf_size;/* Retired, 512 byte increments
* 0 = not_specified
*/
......
};
In this example, serial_no is the serial number of the hard disk. If this item is 0, It is not provided.
The idea is clear. The following is the implementation code:
Static int
Getdiskid (char * id, size_t max)
{
Int fd;
Struct hd_driveid hid;
FILE * fp;
Char line [0x100], * disk, * root, * p;
Fp = fopen ("/etc/mtab", "r ");
If (fp = NULL)
{
Fprintf (stderr, "No/etc/mtab file. \ n ");
Return-1;
}
[1] [2] [3] Next page