File: umdevvd.c

package info (click to toggle)
umview 0.8.2-3
  • links: PTS
  • area: main
  • in suites: bookworm, bullseye
  • size: 5,472 kB
  • sloc: ansic: 67,309; sh: 11,160; ruby: 914; makefile: 424; python: 141
file content (394 lines) | stat: -rw-r--r-- 10,060 bytes parent folder | download | duplicates (4)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*   This is part of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   UMDEVVD: Virtual device for VDI, VMDK, VHD disks.
 *   (It requires the VBoxDD library at run time).
 *
 *    Copyright (C) 2010  Renzo Davoli <renzo@cs.unibo.it>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; version 2 of the License
 *
 *   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
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU 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.
 *  
 */

#include <limits.h>
#include <errno.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <dlfcn.h>
#include "umdev.h"
#include <linux/fs.h>
#include <linux/hdreg.h>
#include "module.h"

#define STD_SECTORSIZE 512
#define STD_SECTORSIZE_OFFSET_MASK 0x1ff

#ifdef __GNUC__
#define UNUSED __attribute__ ((unused))
#else
#define UNUSED
#endif

#define RT_BIT(bit)                             (1UL << (bit))
#define VD_OPEN_FLAGS_NORMAL        0
#define VD_OPEN_FLAGS_READONLY      RT_BIT(0)

typedef struct PDMMEDIAGEOMETRY
{
	uint32_t    cCylinders;
	uint32_t    cHeads;
	uint32_t    cSectors;
} PDMMEDIAGEOMETRY;

typedef int (* intfun)();
typedef uint64_t (* uint64_tfun)();

static int VBoxDD_count;
static void *VBoxDD_handle;
static intfun pVDFlush;
static intfun pVDRead;
static intfun pVDWrite;
static uint64_tfun pVDGetSize;
static intfun pVDCreate;
static intfun pVDOpen;
static intfun pVDClose;
static intfun pVDGetLCHSGeometry;

struct vddisk {
	void         *hdDisk;
	unsigned long flags;
	loff_t size;
};

static int load_VBoxDD()
{
	if (VBoxDD_count == 0) {
		VBoxDD_handle=dlopen("VBoxDD.so",RTLD_LAZY);
		if (VBoxDD_handle == NULL) {
			VBoxDD_handle=dlopen("/usr/lib/virtualbox/VBoxDD.so",RTLD_LAZY);
			if (VBoxDD_handle == NULL) {
				printk("VBoxDD library: not found\n");
				return -ENOENT;
			}
		}
		pVDFlush=dlsym(VBoxDD_handle,"VDFlush");
		pVDRead=dlsym(VBoxDD_handle,"VDRead");
		pVDWrite=dlsym(VBoxDD_handle,"VDWrite");
		pVDGetSize=dlsym(VBoxDD_handle,"VDGetSize");
		pVDCreate=dlsym(VBoxDD_handle,"VDCreate");
		pVDOpen=dlsym(VBoxDD_handle,"VDOpen");
		pVDClose=dlsym(VBoxDD_handle,"VDClose");
		pVDGetLCHSGeometry=dlsym(VBoxDD_handle,"VDGetLCHSGeometry");
		/*printk("%p %p %p %p %p %p %p %p \n",
				pVDFlush,
				pVDRead,
				pVDWrite,
				pVDGetSize,
				pVDCreate,
				pVDOpen,
				pVDClose,
				pVDGetLCHSGeometry);*/
	}
	VBoxDD_count++;
	return 0;
}

static void unload_VBoxDD()
{
	VBoxDD_count--;
	if (VBoxDD_count == 0) {
		dlclose(VBoxDD_handle);
		VBoxDD_handle=NULL;
	}
}

static int vd_open(char type, dev_t device, struct dev_info *di)
{
	return 0;
}

static int vd_release(char type, dev_t device, struct dev_info *di)
{
	struct vddisk *vddisk = umdev_getprivatedata(di->devhandle);
	if (vddisk) 
		pVDFlush(vddisk->hdDisk);
	return 0;
}

static inline int aligned_read(void *disk,loff_t pos, char *buf, size_t len)
{
	if ((pos & STD_SECTORSIZE_OFFSET_MASK)==0 && 
			(len & STD_SECTORSIZE_OFFSET_MASK)==0) {
		int ret = pVDRead(disk,pos,buf,len);
		return (ret >= 0) ? (signed) len : -EIO;
	} else {
		int rv;
		char tbuf[STD_SECTORSIZE];
		size_t toff,tlen,llen=len;
		toff=pos & STD_SECTORSIZE_OFFSET_MASK;
		if (toff) {
			rv=pVDRead(disk,pos - toff,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
			tlen=STD_SECTORSIZE-toff;
			if (len<tlen) tlen=len;
			memcpy(buf,tbuf+toff,tlen);
			pos+=tlen;
			buf+=tlen;
			llen-=tlen;
		}
		tlen=llen & ~STD_SECTORSIZE_OFFSET_MASK;
		if (tlen) {
			rv=pVDRead(disk,pos,buf,tlen);
			if (rv<0) return -EIO;
			pos+=tlen;
			buf+=tlen;
			llen-=tlen;
		}
		if (llen > 0) {
			rv=pVDRead(disk,pos,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
			memcpy(buf,tbuf,llen);
		}
		return len;
	}
}

static int vd_read(char type, dev_t device, char *buf, size_t len, loff_t pos, struct dev_info *di)
{
	struct vddisk *vddisk = umdev_getprivatedata(di->devhandle);
	if (vddisk) {
		loff_t size=vddisk->size;
		if (pos > size) pos=size;
		if (pos+len > size) len=size-pos;
		if (len > 0) 
			return aligned_read(vddisk->hdDisk,pos,buf,len);
		else
			return 0;
	}
	else
		return -ENODEV;
}

static inline int aligned_write(void *disk,loff_t pos, const char *buf, size_t len)
{
	if ((pos & STD_SECTORSIZE_OFFSET_MASK)==0 && 
			(len & STD_SECTORSIZE_OFFSET_MASK)==0) {
		int ret = pVDWrite(disk,pos,buf,len);
		return (ret >= 0) ? (signed) len : -EIO; 
	} else {
		int rv;
		char tbuf[STD_SECTORSIZE];
		size_t toff,tlen,llen=len;
		toff=pos & STD_SECTORSIZE_OFFSET_MASK;
		if (toff) {
			rv=pVDRead(disk,pos - toff,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
			tlen=STD_SECTORSIZE-toff;
			if (len<tlen) tlen=len;
			memcpy(tbuf+toff,buf,tlen);
			rv=pVDWrite(disk,pos - toff,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
			pos+=tlen;
			buf+=tlen;
			llen-=tlen;
		}
		tlen=llen & ~STD_SECTORSIZE_OFFSET_MASK;
		if (tlen) {
			rv=pVDWrite(disk,pos,buf,tlen);
			if (rv<0) return -EIO;
			pos+=tlen;
			buf+=tlen;
			llen-=tlen;
		}
		if (llen > 0) {
			rv=pVDRead(disk,pos,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
			memcpy(tbuf,buf,llen);
			rv=pVDWrite(disk,pos,tbuf,STD_SECTORSIZE);
			if (rv<0) return -EIO;
		}
		return len;
	}
}

static int vd_write(char type, dev_t device, const char *buf, size_t len, loff_t pos, struct dev_info *di)
{
	struct vddisk *vddisk = umdev_getprivatedata(di->devhandle);
	if (vddisk) {
		loff_t size=vddisk->size;
		if (vddisk->flags & MS_RDONLY)
			return -EACCES;
		else {
			if (pos > size) pos=size;
			if (pos+len > size) len=size-pos;
			if (len > 0)
				return aligned_write(vddisk->hdDisk,pos,buf,len);
			else
				return 0;
		}
	} else
		return -ENODEV;
}

static loff_t vd_lseek(char type, dev_t device, loff_t offset, int whence, loff_t pos, struct dev_info *di)
{
	struct vddisk *vddisk = umdev_getprivatedata(di->devhandle);
	if (vddisk) {
		loff_t size=vddisk->size;
		loff_t rv;
		switch (whence) {
			case SEEK_SET:
				rv=offset;
				break;
			case SEEK_CUR:
				rv=pos+offset;
				break;
			case SEEK_END:
				rv=size+offset;
				break;
		}
		if (rv<0) rv=0;
		if (rv>size) rv=size;
		return rv;
	}
	else
		return -ENODEV;
}

// detects type of virtual image
int detectDiskType (char **disktype, char *filename) {
	char buf[8];
	int fd = open (filename, O_RDONLY);
	read (fd, buf, sizeof (buf));

	if (strncmp (buf, "conectix", 8) == 0)  *disktype = "VHD";
	else if (strncmp (buf, "VMDK", 4) == 0)  *disktype = "VMDK";
	else if (strncmp (buf, "KDMV", 4) == 0)  *disktype = "VMDK";
	else if (strncmp (buf, "<<<",  3) == 0)  *disktype = "VDI";
	else {
		printk("cannot autodetect disk type\n");
		close(fd);
		return -ENODEV;
	}
	printk ("disktype is %s\n", *disktype);
	close(fd);
	return 0;
}

static int vd_init(char type, dev_t device, char *path, unsigned long flags, char *args,struct umdev *devhandle)
{
	struct vddisk *vddisk;
	char *diskType = "auto";
	if (load_VBoxDD() != 0)
		return -ENODEV;
	vddisk=calloc(1,sizeof (struct vddisk));
	if (vddisk==NULL)
		return -ENOMEM;
	vddisk->flags=flags;
	if (pVDCreate(NULL, &vddisk->hdDisk) < 0) {
		printk("invalid initialisation of VD interface\n");
		goto enodev;
	}
	if (detectDiskType (&diskType, path) < 0)
		goto enodev;
	if (pVDOpen(vddisk->hdDisk,diskType, path, 
					(flags & MS_RDONLY)?VD_OPEN_FLAGS_READONLY:VD_OPEN_FLAGS_NORMAL, NULL) < 0) {
		printk("opening vbox image failed\n");
		goto enodev;
	}
	vddisk->size=pVDGetSize(vddisk->hdDisk, 0);
	mode_t mode=umdev_getmode(devhandle);
	mode = (mode & ~S_IFMT) | S_IFBLK;
	umdev_setmode(devhandle, mode);
	umdev_setprivatedata(devhandle,vddisk);
	return 0;
enodev:
	free(vddisk);
	return -ENODEV;
}

static int vd_fini(char type, dev_t device, struct umdev *devhandle)
{
	struct vddisk *vddisk = umdev_getprivatedata(devhandle);
	if (vddisk) {
		pVDClose(vddisk->hdDisk,0);
		free(vddisk);
		unload_VBoxDD();
	}
	return 0;
}

static int vd_ioctl(char type, dev_t device, int req, void * arg, struct dev_info *di)
{
	struct vddisk *vddisk = umdev_getprivatedata(di->devhandle);
	if (vddisk) {
		switch (req) {
			case BLKROGET: *(int *)arg = ((vddisk->flags & MS_RDONLY) != 0);
										 break;
			case BLKSSZGET: *(int *)arg = STD_SECTORSIZE;
											break;
			case BLKRRPART: break;
			case BLKGETSIZE: *(int *)arg = vddisk->size / STD_SECTORSIZE;
											 break;
			case BLKGETSIZE64: *(long long *)arg = vddisk->size;
												 break;
			case HDIO_GETGEO: {
													struct hd_geometry *hdg = arg;
													PDMMEDIAGEOMETRY vdgeom;
													pVDGetLCHSGeometry(vddisk->hdDisk,0,&vdgeom);
													/*char*/hdg->heads=vdgeom.cHeads;
													/*char*/hdg->sectors=vdgeom.cSectors;
													/*short*/hdg->cylinders=vdgeom.cCylinders;
													hdg->start=0;
												}
												break;
			default: return -EINVAL;
		}
		return 0;
	} else
		return -ENODEV;
}
static int vd_ioctl_params(char type, dev_t device, int req, struct dev_info *di)
{
	switch (req) {
		case BLKROSET: return (sizeof(int) | IOCTL_R);
		case BLKROGET: return (sizeof(int) | IOCTL_W);
		case BLKSSZGET: return (sizeof(int) | IOCTL_W);
		case BLKRRPART: return 0;
		case BLKGETSIZE: return (sizeof(int) | IOCTL_W);
		case BLKGETSIZE64: return (sizeof(long long) | IOCTL_W);
		case HDIO_GETGEO: return (sizeof(struct hd_geometry) | IOCTL_W);
		default: return 0;
	}
}

struct umdev_operations umdev_ops={
	.open=vd_open,
	.read=vd_read,
	.write=vd_write,
	.release=vd_release,
	.lseek=vd_lseek,
	.init=vd_init,
	.ioctl=vd_ioctl,
	.ioctlparms=vd_ioctl_params,
	.fini=vd_fini,
};