symbian-qemu-0.9.1-12/qemu-symbian-svp/plugins/syborg_nvmemorydevice.py
branchGCC_SURGE
changeset 93 07b904f40417
parent 74 eb3d0111f868
equal deleted inserted replaced
90:73ba398cc7f4 93:07b904f40417
    26 import ctypes
    26 import ctypes
    27 import qemu
    27 import qemu
    28 import sys
    28 import sys
    29 import os
    29 import os
    30 import array
    30 import array
       
    31 import platform
       
    32 import re
    31 
    33 
    32 class syborg_nvmemorydevice(qemu.devclass):
    34 class syborg_nvmemorydevice(qemu.devclass):
    33     # 256 MB default empty drive size if there is no readymade image available
    35     # 256 MB default empty drive size if there is no readymade image available
    34     DEFAULT_DRIVE_SIZE = 0x10000000
    36     DEFAULT_DRIVE_SIZE = 0x10000000
    35     DEVICE_SECTOR_SIZE = 0x200
    37     DEVICE_SECTOR_SIZE = 0x200
    64     nvmemory_sector_count                = 0
    66     nvmemory_sector_count                = 0
    65     
    67     
    66     drive_size                          = 0
    68     drive_size                          = 0
    67     sector_size                         = 0
    69     sector_size                         = 0
    68     drive_image_name                    = ""
    70     drive_image_name                    = ""
       
    71     host_plat = platform.platform();
    69 
    72 
    70     def create(self):
    73     def create(self):
    71         print "syborg_nvmemorydevice: create\n"
    74         print "syborg_nvmemorydevice: create\n"
    72         
    75         
    73         # Get properties
    76         # Get properties
    77         
    80         
    78         print "drive size: ", self.drive_size
    81         print "drive size: ", self.drive_size
    79         print "sector size: ", self.sector_size
    82         print "sector size: ", self.sector_size
    80         print "drive name: ", self.drive_image_name
    83         print "drive name: ", self.drive_image_name
    81 
    84 
    82         drive_path_and_name = self.DRIVE_PATH + "\\" + self.drive_image_name 
    85         drive_path_and_name = os.path.join(self.DRIVE_PATH, self.drive_image_name)  
    83         # Save working directory
    86         # Save working directory
    84         self.working_dir = os.getcwd()
    87         self.working_dir = os.getcwd()
       
    88         nvmem_lib = ""
       
    89         open_mode = 0
       
    90         if re.match('^linux',self.host_plat,re.I):
       
    91 			nvmemlib_name = "libnvmemmory.so"
       
    92 			open_mode = os.O_RDWR
       
    93         else:
       
    94 			nvmemlib_name = "nvmemmory.dll"
       
    95 			open_mode = os.O_RDWR|os.O_BINARY			
    85         
    96         
    86         # Open the nvmemory library
    97         # Open the nvmemory library
    87         try:
    98         try:
    88             self.nvmemlib = ctypes.CDLL("nvmemmory.dll")
    99             self.nvmemlib = ctypes.CDLL(nvmemlib_name)
    89         except:
   100         except Exception, e:
    90             sys.exit("syborg_nvmemorydevice: nvmemmory.dll load failed")
   101             print repr(e)
       
   102             sys.exit("syborg_nvmemorydevice: nvmemmory load failed")
    91         
   103         
    92         # Create an instance of non volatile memory handler class
   104         # Create an instance of non volatile memory handler class
    93         self.obj = self.nvmemlib.nvmem_create( self.sector_size )
   105         self.obj = self.nvmemlib.nvmem_create( self.sector_size )
    94         self.nvmemlib.nvmem_reset( self.obj )
   106         self.nvmemlib.nvmem_reset( self.obj )
    95         
   107         
    99             os.mkdir( self.DRIVE_PATH )
   111             os.mkdir( self.DRIVE_PATH )
   100         except:
   112         except:
   101             # Here we could check why we failed - usually because the path already exists \n"
   113             # Here we could check why we failed - usually because the path already exists \n"
   102             pass
   114             pass
   103         try:
   115         try:
   104             self.filehandle = os.open( drive_path_and_name, os.O_RDWR|os.O_BINARY )
   116             self.filehandle = os.open( drive_path_and_name, open_mode )
   105             os.close( self.filehandle )
   117             os.close( self.filehandle )
   106         except:
   118         except:
   107             print "syborg_nvmemorydevice: drive image not found - create\n"
   119             print "syborg_nvmemorydevice: drive image not found - create\n"
   108             self.filehandle = open( drive_path_and_name, "wb" )
   120             self.filehandle = open( drive_path_and_name, "wb" )
   109             # Initialize file with zeroes. This may take a while
   121             # Initialize file with zeroes. This may take a while
   117             while index < 128:
   129             while index < 128:
   118                 temparray.tofile(self.filehandle)
   130                 temparray.tofile(self.filehandle)
   119                 index = index+1
   131                 index = index+1
   120         
   132         
   121         # Create path and get handle to the raw memory array
   133         # Create path and get handle to the raw memory array
   122         imagepath = self.working_dir + "\\" + drive_path_and_name
   134         imagepath = os.path.join(self.working_dir, drive_path_and_name)
   123         print "imagepath: ", imagepath
   135         print "imagepath: ", imagepath
   124         self.nvmemhandle = self.nvmemlib.nvmem_open( self.obj, imagepath )
   136         self.nvmemhandle = self.nvmemlib.nvmem_open( self.obj, imagepath )
   125         if( self.nvmemhandle < 0 ):
   137         if( self.nvmemhandle < 0 ):
   126             error_msg = "syborg_nvmemorydevice: nvmem_open error: ", self.nvmemhandle
   138             error_msg = "syborg_nvmemorydevice: nvmem_open error: ", self.nvmemhandle
   127             sys.exit( error_msg )
   139             sys.exit( error_msg )