summaryrefslogtreecommitdiff
path: root/open-vm-tools/checkvm/checkvm.c
blob: e4bd98578f355987d40630f2e38d0a0e4e296379 (plain)
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
/*********************************************************
 * Copyright (C) 2007 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/*
 * checkvm.c --
 *
 *      Check if we are running in a VM or not
 */

#include <stdlib.h>
#include <stdio.h>

#if !defined(_WIN32)
#include <unistd.h>
#endif

#include "vm_version.h"
#include "backdoor.h"
#include "backdoor_def.h"
#include "vm_basic_types.h"
#include "vmcheck.h"
#if defined(_WIN32)
#include "getoptwin32.h"
#endif

#include "checkvm_version.h"
#include "vm_version.h"
#include "embed_version.h"
VM_EMBED_VERSION(CHECKVM_VERSION_STRING);


/*
 *  getHWVersion  -  Read VM HW version through backdoor
 */
void
getHWVersion(uint32 *hwVersion)
{
   Backdoor_proto bp;
   bp.in.cx.halfs.low = BDOOR_CMD_GETHWVERSION;
   Backdoor(&bp);
   *hwVersion = bp.out.ax.word;
}


/*
 *  getScreenSize  -  Get screen size of the host
 */
void
getScreenSize(uint32 *screensize)
{
   Backdoor_proto bp;
   bp.in.cx.halfs.low = BDOOR_CMD_GETSCREENSIZE;
   Backdoor(&bp);
   *screensize = bp.out.ax.word;
}


/*
 *  Start of main program.  Check if we are in a VM, by reading
 *  a backdoor port.  Then process any other commands.
 */
int
main(int argc,
     char *argv[])
{
   uint32 version[2];
   int opt;
   int width, height;
   uint32 screensize = 0;
   uint32 hwVersion;

   if (!VmCheck_IsVirtualWorld()) {
      fprintf(stdout, "Not running in a virtual machine.\n");
      return 1;
   }

   if (!VmCheck_GetVersion(&version[0], &version[1])) {
      fprintf(stdout, "Couldn't get version\n");
      return 1;
   }

   /*
    *  OK, we're in a VM, check if there are any other requests
    */
   while ((opt = getopt(argc, argv, "rph")) != EOF) {
      switch (opt) {
      case 'r':
         getScreenSize(&screensize);
         width = (screensize >> 16) & 0xffff;
         height = screensize & 0xffff;
         if ((width <= 0x7fff) && (height <= 0x7fff)) {
            printf("%d %d\n", width, height);
         } else {
            printf("0 0\n");
         }
         return 0;

      case 'p':
         /*
         * Print out product that we're running on based on code
         * obtained from getVersion().
         */
         switch (version[1]) {
         case VMX_TYPE_SCALABLE_SERVER:
            printf("ESX Server\n");
            break;

         case VMX_TYPE_WORKSTATION:
            printf("Workstation\n");
            break;

         default:
            printf("Unknown\n");
            break;
         }
         return 0;

      case 'h':
         getHWVersion(&hwVersion);
         printf("VM's hw version is %u\n", hwVersion);
         break;

      default:
         break;
      }
   }

   printf("%s version %d (good)\n", PRODUCT_LINE_NAME, version[0]);
   return 0;
}