]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Give checkvm some love.
authorVMware, Inc <>
Mon, 20 Dec 2010 22:21:55 +0000 (14:21 -0800)
committerMarcelo Vanzin <mvanzin@vmware.com>
Mon, 20 Dec 2010 22:21:55 +0000 (14:21 -0800)
Clean up our favorite useless app:
. use lib/backdoor and lib/checkvm instead of inline asm
. don't reimplement getopt on win32 since we already have it in lib/misc
. fix some code standard nits

Signed-off-by: Marcelo Vanzin <mvanzin@vmware.com>
open-vm-tools/checkvm/Makefile.am
open-vm-tools/checkvm/checkvm.c

index f4003a70a0badf0f7de41c7e2aca396094a6824f..24f25902e64115d6d6b59b9e1cf0bd8203b7df1f 100644 (file)
@@ -19,3 +19,17 @@ bin_PROGRAMS = vmware-checkvm
 
 vmware_checkvm_SOURCES =
 vmware_checkvm_SOURCES += checkvm.c
+
+vmware_checkvm_LDADD =
+vmware_checkvm_LDADD += @VMTOOLS_LIBS@
+
+if HAVE_ICU
+   vmware_checkvm_LDADD += @ICU_LIBS@
+   vmware_checkvm_LINK = $(LIBTOOL) --tag=CXX $(AM_LIBTOOLFLAGS)     \
+                            $(LIBTOOLFLAGS) --mode=link $(CXX)       \
+                            $(AM_CXXFLAGS) $(CXXFLAGS) $(AM_LDFLAGS) \
+                            $(LDFLAGS) -o $@
+else
+   vmware_checkvm_LINK = $(LINK)
+endif
+
index 3b001f3779bfeb76b25f729f82b13158a05fd44b..d4aa4e790a7a1a1215f412027bfcbe8adf3b5630 100644 (file)
  *      Check if we are running in a VM or not
  */
 
-
-#include <signal.h>
 #include <stdlib.h>
 #include <stdio.h>
 
-#ifdef _WIN32
-#include <wtypes.h>
-#include <winbase.h>
-#include <io.h>
-#else
+#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 "embed_version.h"
 VM_EMBED_VERSION(CHECKVM_VERSION_STRING);
 
 
-
-#ifdef __GNUC__
-
-/*
- *  outl and inl: Output or input a 32-bit word
- */
-static __inline__ void
-outl(
-    const uint32 port,
-    uint32       val
-)
-{
-  __asm__ volatile("out%L0 (%%dx)" : :"a" (val), "d" (port));
-}
-
-static __inline__ uint32
-inl(
-    const uint32 port
-)
-{
-  uint32 ret;
-
-  __asm__ volatile("in%L0 (%%dx)" : "=a" (ret) : "d" (port));
-  return ret;
-}
-
-
-/*
- *  getVersion  -  Read VM version & product code through backdoor
- */
-void
-getVersion(uint32 *version)
-{
-   uint32 eax, ebx, ecx, edx;
-   
-   __asm__ volatile("inl (%%dx)" :
-                   "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) :
-                   "0"(BDOOR_MAGIC), "1"(BDOOR_CMD_GETVERSION),
-                   "2"(BDOOR_PORT) : "memory");
-   version[0] = eax;
-   version[1] = ebx;
-   version[2] = ecx;
-}
-
 /*
  *  getHWVersion  -  Read VM HW version through backdoor
  */
 void
 getHWVersion(uint32 *hwVersion)
 {
-   uint32 eax, ebx, ecx, edx;
-   
-   __asm__ volatile("inl (%%dx)" :
-                   "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) :
-                   "0"(BDOOR_MAGIC), "1"(BDOOR_CMD_GETHWVERSION),
-                   "2"(BDOOR_PORT) : "memory");
-   *hwVersion = eax;
-}
-
-
-/*
- *  getScreenSize  -  Get screen size of the host
- */
-void
-getScreenSize(uint32 *screensize)
-{
-   uint32 eax, ebx, ecx, edx;
-   
-   __asm__ volatile("inl (%%dx)" :
-                   "=a"(eax), "=c"(ecx), "=d"(edx), "=b"(ebx) :
-                   "0"(BDOOR_MAGIC), "1"(BDOOR_CMD_GETSCREENSIZE),
-                   "2"(BDOOR_PORT) : "memory");
-   *screensize = eax;
-}
-
-
-
-#elif _WIN32
-
-
-/*
- *  getVersion  -  Read VM version & product code through backdoor
- */
-void
-getVersion(uint32 *version)
-{              
-   uint32 v;
-   uint32 m;
-   uint32 p;
-   
-   _asm {        
-         push eax
-         push ebx
-         push ecx
-         push edx
-         
-         mov eax, BDOOR_MAGIC
-         mov ecx, BDOOR_CMD_GETVERSION
-         mov dx, BDOOR_PORT
-         in eax, dx
-         mov v, eax
-         mov m, ebx
-        mov p, ecx
-         
-         pop edx
-         pop ecx
-         pop ebx
-         pop eax
-   }
-   
-   version[0] = v;
-   version[1] = m;
-   version[2] = p;
-}
-
-
-/*
- *  getHWVersion  -  Read VM HW Version through backdoor
- */
-void
-getHWVersion(uint32 *hwVersion)
-{              
-   uint32 v;
-
-   _asm {        
-         push eax
-         push ecx
-         push edx
-
-         mov eax, BDOOR_MAGIC
-         mov ecx, BDOOR_CMD_GETHWVERSION
-         mov dx, BDOOR_PORT
-        in eax, dx
-        mov v, eax
-
-         pop edx
-         pop ecx
-         pop eax
-   }
-   
-   *hwVersion = v;
+   Backdoor_proto bp;
+   bp.in.cx.halfs.low = BDOOR_CMD_GETHWVERSION;
+   Backdoor(&bp);
+   *hwVersion = bp.out.ax.word;
 }
 
 
@@ -193,75 +61,11 @@ getHWVersion(uint32 *hwVersion)
  */
 void
 getScreenSize(uint32 *screensize)
-{              
-   uint32 v;
-
-   _asm {        
-         push eax
-         push ecx
-         push edx
-
-         mov eax, BDOOR_MAGIC
-         mov ecx, BDOOR_CMD_GETSCREENSIZE
-         mov dx, BDOOR_PORT
-         in eax, dx
-        mov v, eax
-
-         pop edx
-         pop ecx
-         pop eax
-   }
-   
-   *screensize = v;
-}
-
-
-#endif
-
-/*
- *  Simulate the getopt function for windows
- */
-#ifdef _WIN32
-static char *optarg;
-static int optind = 1;
-
-static int
-getopt(int argc, char *argv[], char *opts)
 {
-   char *ptr = argv[optind];
-   int chr;
-   if ((ptr) && (*ptr == '-')) {
-      ptr++;
-      while (*opts) {
-         if (*ptr == *opts) {
-            if (*(opts+1) == ':') {
-               optarg = ptr+1;
-               if (*optarg == 0) {
-                  optind++;
-                  optarg = argv[optind];
-               }
-            }
-            chr = *ptr & 0xff;
-            optind++;
-            return chr;
-         }
-         opts++;
-      }
-
-      /*
-       *  Not a valid option
-       */
-      return '?';
-   }
-   return EOF;
-}
-#endif
-
-
-static void handler(int sig)
-{
-   fprintf (stdout, "Couldn't get version\n");
-   exit(1);
+   Backdoor_proto bp;
+   bp.in.cx.halfs.low = BDOOR_CMD_GETSCREENSIZE;
+   Backdoor(&bp);
+   *screensize = bp.out.ax.word;
 }
 
 
@@ -269,93 +73,77 @@ static void handler(int sig)
  *  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[])
+int
+main(int argc,
+     char *argv[])
 {
-   uint32 version[3];
+   uint32 version[2];
    int opt;
    int width, height;
    uint32 screensize = 0;
    uint32 hwVersion;
 
-#ifdef _WIN32
-   __try {
-      getVersion(&version[0]);
-   } __except(EXCEPTION_EXECUTE_HANDLER) {
-      fprintf (stdout, "Couldn't get version\n");
+   if (!VmCheck_IsVirtualWorld()) {
+      fprintf(stdout, "Not running in a virtual machine.\n");
       return 1;
    }
-#else
-#ifdef __FreeBSD__
-#  define ERROR_SIGNAL SIGBUS
-#else
-#  define ERROR_SIGNAL SIGSEGV
-#endif
-   signal(ERROR_SIGNAL, handler);
-   getVersion(&version[0]);
-   signal(ERROR_SIGNAL, SIG_DFL);
-#endif
 
-   if (version[1] != BDOOR_MAGIC) {
-      fprintf (stdout, "Incorrect virtual machine version\n");
-      return 1;
-   }
-
-   if (version[0] != VERSION_MAGIC) {
-      fprintf (stdout, "%s version %d (should be %d)\n",
-                       PRODUCT_LINE_NAME, version[0], VERSION_MAGIC);
+   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) {
+   while ((opt = getopt(argc, argv, "rph")) != EOF) {
       switch (opt) {
       case 'r':
-         getScreenSize (&screensize);
+         getScreenSize(&screensize);
          width = (screensize >> 16) & 0xffff;
          height = screensize & 0xffff;
          if ((width <= 0x7fff) && (height <= 0x7fff)) {
-           printf("%d %d\n", width, height);
+            printf("%d %d\n", width, height);
          } else {
-           printf("0 0\n");
+            printf("0 0\n");
          }
-        return 0;
+         return 0;
 
       case 'p':
-        /*
-         * Print out product that we're running on based on code
-         * obtained from getVersion().
-         */
-         switch (version[2]) {
-           case VMX_TYPE_EXPRESS:
-              printf("Express\n");
-              break;
-              
-           case VMX_TYPE_SCALABLE_SERVER:
-              printf("ESX Server\n");
-              break;
-              
-           case VMX_TYPE_WGS:
-              printf("VMware Server\n");
-              break;
-              
-           case VMX_TYPE_WORKSTATION:
-              printf("Workstation\n");
-              break;
-              
-            default:
-              printf("Unknown\n");
-              break;
-        }
-        return 0;
+         /*
+         * Print out product that we're running on based on code
+         * obtained from getVersion().
+         */
+         switch (version[1]) {
+         case VMX_TYPE_EXPRESS:
+            printf("Express\n");
+            break;
+
+         case VMX_TYPE_SCALABLE_SERVER:
+            printf("ESX Server\n");
+            break;
+
+         case VMX_TYPE_WGS:
+            printf("VMware 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;
+         getHWVersion(&hwVersion);
+         printf("VM's hw version is %u\n", hwVersion);
+         break;
+
       default:
-        break;
+         break;
       }
    }