Skip to content

Commit

Permalink
implement support for overriding processor vendor string on command line
Browse files Browse the repository at this point in the history
Signed-off-by: Steven Noonan <steven@uplinklabs.net>
  • Loading branch information
tycho committed Mar 17, 2016
1 parent 4f62998 commit 591550a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
57 changes: 45 additions & 12 deletions handlers.c
Expand Up @@ -130,6 +130,44 @@ void handle_dump_base(struct cpu_regs_t *regs, struct cpuid_state_t *state)
state->cpuid_print(regs, state, FALSE);
}

typedef struct _vendor_map_t {
const char *name;
int id;
} vendor_map_t, *pvendor_map_t;

vendor_map_t vendors[] = {
{ "GenuineIntel", VENDOR_INTEL },
{ "AuthenticAMD", VENDOR_AMD },
{ "GenuineTMx86", VENDOR_TRANSMETA },
{ "CyrixInstead", VENDOR_CYRIX },
{ NULL, VENDOR_UNKNOWN },
};


int vendor_id(const char *name)
{
pvendor_map_t pvendor = vendors;

while (pvendor->name) {
if (strcmp(name, pvendor->name) == 0)
break;
pvendor++;
}

return pvendor->id;
}

const char *vendor_name(int vendor_id)
{
pvendor_map_t pvendor = vendors;
while (pvendor->name) {
if (pvendor->id == vendor_id)
break;
pvendor++;
}
return pvendor->name;
}

/* EAX = 0000 0000 */
void handle_std_base(struct cpu_regs_t *regs, struct cpuid_state_t *state)
{
Expand Down Expand Up @@ -157,18 +195,13 @@ void handle_std_base(struct cpu_regs_t *regs, struct cpuid_state_t *state)

buf[12] = 0;

printf("CPU vendor string: '%s'\n\n", buf);

if (strcmp(buf, "GenuineIntel") == 0)
state->vendor = VENDOR_INTEL;
else if (strcmp(buf, "AuthenticAMD") == 0)
state->vendor = VENDOR_AMD;
else if (strcmp(buf, "GenuineTMx86") == 0)
state->vendor = VENDOR_TRANSMETA;
else if (strcmp(buf, "CyrixInstead") == 0)
state->vendor = VENDOR_CYRIX;
else
state->vendor = VENDOR_UNKNOWN;
printf("CPU vendor string: '%s'", buf);
if (state->vendor == VENDOR_UNKNOWN ) {
state->vendor = vendor_id(buf);
} else {
printf(" (overridden as '%s')", vendor_name(state->vendor));
}
printf("\n\n");
}

/* EAX = 8000 0001 | EAX = 0000 0001 */
Expand Down
3 changes: 3 additions & 0 deletions handlers.h
Expand Up @@ -32,4 +32,7 @@ struct cpuid_leaf_handler_index_t {
extern const struct cpuid_leaf_handler_index_t dump_handlers[];
extern const struct cpuid_leaf_handler_index_t decode_handlers[];

int vendor_id(const char *vendor);
const char *vendor_name(int vendor_id);

#endif
14 changes: 10 additions & 4 deletions main.c
Expand Up @@ -130,11 +130,12 @@ static void run_cpuid(struct cpuid_state_t *state, int dump)

static void usage(const char *argv0)
{
printf("usage: %s [--help] [--dump] [--ignore-vendor] [--parse <filename>]\n\n", argv0);
printf("usage: %s [--help] [--dump] [--vendor <name>] [--ignore-vendor] [--parse <filename>]\n\n", argv0);
printf(" %-18s %s\n", "-h, --help", "Print this list of options");
printf(" %-18s %s\n", "-c, --cpu", "Index (starting at 0) of CPU to get info from");
printf(" %-18s %s\n", "-d, --dump", "Dump a raw CPUID table");
printf(" %-18s %s\n", "--ignore-vendor", "Show feature flags from all vendors");
printf(" %-18s %s\n", "--vendor", "Override the processor vendor string");
printf(" %-18s %s\n", "-f, --parse", "Read and decode a raw cpuid table from the file specified");
printf(" %-18s %s\n", "--sanity", "Do a sanity check of the CPUID data");
printf("\n");
Expand Down Expand Up @@ -181,6 +182,8 @@ int main(int argc, char **argv)
int c, ret = 0;
int cpu_start = -2, cpu_end = -2;

INIT_CPUID_STATE(&state);

while (TRUE) {
static struct option long_options[] = {
{"version", no_argument, 0, 'v'},
Expand All @@ -190,14 +193,15 @@ int main(int argc, char **argv)
{"cpu", required_argument, 0, 'c'},
{"kernel", no_argument, &do_kernel, 'k'},
{"ignore-vendor", no_argument, &ignore_vendor, 1},
{"vendor", required_argument, 0, 'V'},
{"parse", required_argument, 0, 'f'},
{"format", required_argument, 0, 'o'},
{"scan-to", required_argument, 0, 2},
{0, 0, 0, 0}
};
int option_index = 0;

c = getopt_long(argc, argv, "c:hdvo:f:", long_options, &option_index);
c = getopt_long(argc, argv, "c:hdvV:o:f:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
Expand Down Expand Up @@ -243,6 +247,10 @@ int main(int argc, char **argv)
exit(1);
}
break;
case 'V':
assert(optarg);
state.vendor = vendor_id(optarg);
break;
case 'v':
version();
case 'h':
Expand All @@ -252,8 +260,6 @@ int main(int argc, char **argv)
}
}

INIT_CPUID_STATE(&state);

if (cpu_start == -2)
cpu_start = cpu_end = 0;

Expand Down

0 comments on commit 591550a

Please sign in to comment.