-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcpu.cpp
More file actions
executable file
·180 lines (140 loc) · 5.24 KB
/
cpu.cpp
File metadata and controls
executable file
·180 lines (140 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <cstdio>
#include <memory>
#include <string>
#include <sstream>
#include <fstream> // getUUID
#include <iostream>
#include <stdexcept> // executeCommand
#include <stdio.h>
#include <cpuid.h>
#define print(x) std::cout << x << std::endl;
#include <random>
#include <chrono>
#include <iomanip>
std::string generateUUID() {
// Get current time
auto now = std::chrono::system_clock::now();
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count();
// Generate a random number
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<uint64_t> distribution(0, std::numeric_limits<uint64_t>::max());
uint64_t randomNum = distribution(generator);
// Combine timestamp and random number
uint64_t combined = (timestamp << 32) | (randomNum & 0xFFFFFFFF);
// Format as UUID
std::stringstream ss;
ss << std::hex << std::setfill('0') << std::setw(16) << combined;
std::string uuid = ss.str();
// Insert hyphens to match UUID format
uuid.insert(8, "-");
uuid.insert(13, "-");
uuid.insert(18, "-");
uuid.insert(23, "-");
return uuid;
}
std::string executeCommand(const char* cmd) {
std::array<char, 128> buffer;
std::string result;
std::shared_ptr<FILE> pipe(popen(cmd, "r"), pclose);
if (!pipe) {
throw std::runtime_error("popen() failed!");
}
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr) {
result += buffer.data();
}
return result;
}
std::string getUUID()
{
// std::ifstream uuidFile("sys/class/dmi/id/product_uuid");
std::ifstream uuidFile("/proc/sys/kernel/random/uuid");
std::string uuid;
if (uuidFile.is_open()) {
std:getline(uuidFile, uuid);
uuidFile.close();
}
return uuid;
}
std::string GetCPUId()
{
std::string strCPUId;
unsigned int level = 1;
unsigned eax = 3 /* processor serial number */, ebx = 0, ecx = 0, edx = 0;
__get_cpuid(level, &eax, &ebx, &ecx, &edx);
// byte swap
int first = ((eax >> 24) & 0xff) | ((eax << 8) & 0xff0000) | ((eax >> 8) & 0xff00) | ((eax << 24) & 0xff000000);
int last = ((edx >> 24) & 0xff) | ((edx << 8) & 0xff0000) | ((edx >> 8) & 0xff00) | ((edx << 24) & 0xff000000);
// tranfer to string
std::stringstream ss;
ss << std::hex << first;
ss << std::hex << last;
ss >> strCPUId;
return strCPUId;
}
// [!] Useless
// static inline void native_cpuid(unsigned int *eax, unsigned int *ebx,
// unsigned int *ecx, unsigned int *edx)
// {
// /* ecx is often an input as well as an output. */
// asm volatile("cpuid"
// : "=a" (*eax),
// "=b" (*ebx),
// "=c" (*ecx),
// "=d" (*edx)
// : "0" (*eax), "2" (*ecx));
// }
// int cpuid2()
// {
// unsigned eax, ebx, ecx, edx;
// eax = 1; /* processor info and feature bits */
// native_cpuid(&eax, &ebx, &ecx, &edx);
// printf("stepping %d\n", eax & 0xF);
// printf("model %d\n", (eax >> 4) & 0xF);
// printf("family %d\n", (eax >> 8) & 0xF);
// printf("processor type %d\n", (eax >> 12) & 0x3);
// printf("extended model %d\n", (eax >> 16) & 0xF);
// printf("extended family %d\n", (eax >> 20) & 0xFF);
// /* EDIT */
// eax = 3; /* processor serial number */
// native_cpuid(&eax, &ebx, &ecx, &edx);
// /** see the CPUID Wikipedia article on which models return the serial
// number in which registers. The example here is for
// Pentium III */
// printf("serial number 0x%08x%08x\n", edx, ecx);
// return 0;
// }
void getPSN(char *PSN)
{
int varEAX, varEBX, varECX, varEDX;
char str[9];
//%eax=1 gives most significant 32 bits in eax
__asm__ __volatile__ ("cpuid" : "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (1));
sprintf(str, "%08X", varEAX); //i.e. XXXX-XXXX-xxxx-xxxx-xxxx-xxxx
sprintf(PSN, "%C%C%C%C-%C%C%C%C", str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
//%eax=3 gives least significant 64 bits in edx and ecx [if PN is enabled]
__asm__ __volatile__ ("cpuid" : "=a" (varEAX), "=b" (varEBX), "=c" (varECX), "=d" (varEDX) : "a" (3));
sprintf(str, "%08X", varEDX); //i.e. xxxx-xxxx-XXXX-XXXX-xxxx-xxxx
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
sprintf(str, "%08X", varECX); //i.e. xxxx-xxxx-xxxx-xxxx-XXXX-XXXX
sprintf(PSN, "%s-%C%C%C%C-%C%C%C%C", PSN, str[0], str[1], str[2], str[3], str[4], str[5], str[6], str[7]);
}
int main()
{
char PSN[30]; //24 Hex digits, 5 '-' separators, and a '\0'
getPSN(PSN);
printf("%s\n", PSN); //compare with: lshw | grep serial:
// [!] Useless
// cpuid2();
// std::string s = GetCPUId();
// print("cpu id: " << s);
// std::string uuid = getUUID();
// print("random uuid: " << uuid);
// std::string uuidt = executeCommand("uuidgen -t");
// print("time-base uuid: " << uuidt);
// std::string output = executeCommand("sudo dmidecode -s system-uuid");
// print("system uuid: " << output);
// std::string genuuid = generateUUID();
// print("Generated UUID: " << genuuid);
return 0;
}