Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 31 additions & 18 deletions src/bios_dev_name.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

static struct bios_dev_name_opts opts;
int nopirq;
int allow_vm;
int smver_mjr;
int smver_mnr;
int is_valid_smbios = 0;
Expand All @@ -29,6 +30,7 @@ static void usage(void)
fprintf(stderr, " -P or --prefix [string] string use for embedded NICs (default='em')\n");
fprintf(stderr, " -s or --smbios [x.y] Require SMBIOS x.y or greater\n");
fprintf(stderr, " -x or --nopirq Don't use $PIR table for slot numbers\n");
fprintf(stderr, " -V or --allow-vm Allow running in virtual machines\n");
fprintf(stderr, " -v or --version Show biosdevname version\n");
fprintf(stderr, " Example: biosdevname -i eth0\n");
fprintf(stderr, " returns: em1\n");
Expand Down Expand Up @@ -64,11 +66,12 @@ parse_opts(int argc, char **argv)
{"prefix", required_argument, 0, 'P'},
{"nopirq", no_argument, 0, 'x'},
{"smbios", required_argument, 0, 's'},
{"allow-vm", no_argument, 0, 'V'},
{"version", no_argument, 0, 'v'},
{0, 0, 0, 0}
};
c = getopt_long(argc, argv,
"dip:P:xs:v",
"dip:P:xs:Vv",
long_options, &option_index);
if (c == -1)
break;
Expand All @@ -91,6 +94,9 @@ parse_opts(int argc, char **argv)
case 'x':
nopirq = 1;
break;
case 'V':
allow_vm = 1;
break;
case 'v':
fprintf(stderr, "biosdevname version %s\n", BIOSDEVNAME_VERSION);
exit(0);
Expand All @@ -113,28 +119,33 @@ parse_opts(int argc, char **argv)
static u_int32_t
cpuid (u_int32_t eax, u_int32_t ecx)
{
asm volatile (
"xor %%ebx, %%ebx; cpuid"
: "=a" (eax), "=c" (ecx)
: "a" (eax)
: "%ebx", "%edx");
return ecx;
#if defined(__x86_64__) || defined(__i386__)
asm volatile (
"xor %%ebx, %%ebx; cpuid"
: "=a" (eax), "=c" (ecx)
: "a" (eax)
: "%ebx", "%edx");
return ecx;
#else
return 0;
#endif
}

/*
Algorithm suggested by:
http://kb.vmware.com/selfservice/microsites/search.do?language=en_US&cmd=displayKC&externalId=1009458
*/

static int
running_in_virtual_machine (void)
{
u_int32_t eax=1U, ecx=0U;

ecx = cpuid (eax, ecx);
if (ecx & 0x80000000U)
return 1;
#if defined(__x86_64__) || defined(__i386__)
/* Algorithm suggested by https://kb.vmware.com/s/article/1009458 */
u_int32_t eax=1U, ecx=0U;

ecx = cpuid (eax, ecx);
if (ecx & 0x80000000U)
return 1;
else
return 0;
#else
return 0;
#endif
}

static int
Expand All @@ -158,8 +169,10 @@ int main(int argc, char *argv[])

if (!running_as_root())
exit(3);
if (running_in_virtual_machine())
if (!allow_vm && running_in_virtual_machine()) {
fprintf(stderr, "Refusing to run in a virtual machine without --allow-vm option\n");
exit(4);
}
cookie = setup_bios_devices(opts.namingpolicy, opts.prefix);
if (!cookie) {
rc = 1;
Expand Down