File: umdevmbr.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 (227 lines) | stat: -rw-r--r-- 6,703 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
/*   This is part of um-ViewOS
 *   The user-mode implementation of OSVIEW -- A Process with a View
 *
 *   UMDEVMBR: Virtual Device to access Disk Images
 *   (using the standard IBM-PC partition scheme based on MBR/Extended MBR)
 *
 *    Copyright (C) 2006  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 <stdio.h>
#include <string.h>
#include "umdev.h"
#include <stdlib.h>
#include <errno.h>
#include <linux/fs.h>
#include <linux/hdreg.h>
#include <config.h>
#include <sys/ioctl.h>
#include "mbr.h"

//char readonly=0;

static int hdmbr_open(char type, dev_t device, struct dev_info *di)
{
	struct mbr *mbr=umdev_getprivatedata(di->devhandle);
	int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
	if (partno==0 || mbr->part[partno-1] != NULL)
		return 0;
	else
		return -ENODEV;
}

static int hdmbr_read(char type, dev_t device, char *buf, size_t len, loff_t pos, struct dev_info *di)
{
	struct mbr *mbr=umdev_getprivatedata(di->devhandle);
	int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
	int rv;
	if (partno==0) /* partno==0, the disk as a whole */ {
		rv=pread64(mbr->fd,buf,len,pos);
		return (rv<0)?-errno:rv;
	} else { /* access a partition */
		struct partition *partition=mbr->part[partno-1];
		if (partition) {
			if ((pos >> IDE_BLOCKSIZE_LOG) <= partition->LBAnoblocks) {
				pos += ((off_t) partition->LBAbegin) << IDE_BLOCKSIZE_LOG;
				rv=pread64(mbr->fd,buf,len,pos);
				return (rv<0)?-errno:rv;
			} else
				return 0;
		} else
			return -ENODEV;
	}
}

static int hdmbr_write(char type, dev_t device, const char *buf, size_t len, loff_t pos, struct dev_info *di)
{
	struct mbr *mbr=umdev_getprivatedata(di->devhandle);
	int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
	int rv;
	if (partno==0) /* partno==0, the disk as a whole */ {
		rv=pwrite64(mbr->fd,buf,len,pos);
		return (rv<0)?-errno:rv;
	} else { /* access a partition */
		struct partition *partition=mbr->part[partno-1];
		if (partition) {
			if ((pos >> IDE_BLOCKSIZE_LOG) <= partition->LBAnoblocks) {
				pos += ((off_t) partition->LBAbegin) << IDE_BLOCKSIZE_LOG;
				rv=pwrite64(mbr->fd,buf,len,pos);
				return (rv<0)?-errno:rv;
			} else
				return -EINVAL;
		} else
			return -ENODEV;
	}
}

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

static loff_t hdmbr_lseek(char type, dev_t device, loff_t offset, int whence, loff_t pos, struct dev_info *di)
{
	loff_t rv;
	switch (whence) {
		case SEEK_SET:
			rv=offset;
			break;
		case SEEK_CUR:
			rv=pos+offset;
			break;
		case SEEK_END: {
										 struct mbr *mbr=umdev_getprivatedata(di->devhandle);
										 int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
										 if (partno==0)
											 rv=mbr->size + offset;
										 else { /* access a partition */
											 struct partition *partition=mbr->part[partno-1];
											 if (partition) 
												 rv=((partition->LBAnoblocks)<< IDE_BLOCKSIZE_LOG) +offset;
											 else
												 return -ENODEV;

										 }
									 }
			break;
	}
	if (rv<0) rv=0;
	return rv;
}

static int hdmbr_init(char type, dev_t device, char *path, unsigned long flags, char *args, struct umdev *devhandle)
{
	int fd=open(path,O_RDWR);
	if (fd < 0) 
		return -1;
	else {
		struct mbr *mbr=mbr_open(fd);
		if (mbr != NULL) {
			mode_t mode=umdev_getmode(devhandle);
			mode = (mode & ~S_IFMT) | S_IFBLK;
			umdev_setmode(devhandle, mode);
			umdev_setprivatedata(devhandle,mbr);
			umdev_setnsubdev(devhandle, IDE_MAXPART);
			return 0;
		} else
			return -1;
	}
}

static int hdmbr_fini(char type, dev_t device, struct umdev *devhandle)
{
	struct mbr *mbr=umdev_getprivatedata(devhandle);
	mbr_close(mbr);
	return 0;
}

static int hdmbr_ioctl(char type, dev_t device, int req, void * arg, struct dev_info *di)
{
	struct mbr *mbr=umdev_getprivatedata(di->devhandle);
	switch (req) {
		/*case BLKROSET: if (*(int *)arg != 0)
										 readonly=1;
									 break;
		case BLKROGET: *(int *)arg = readonly;
									 break;*/
		case BLKSSZGET: *(int *)arg = IDE_BLOCKSIZE;
										break;
		case BLKRRPART: mbr_reread(mbr);
										break;
		case BLKGETSIZE: {
											 int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
											 if (partno==0)
												 *(int *)arg = (mbr->size >> IDE_BLOCKSIZE_LOG);
											 else { /* access a partition */
												 struct partition *partition=mbr->part[partno-1];
												 if (partition) 
													 *(int *)arg = (partition->LBAnoblocks) << IDE_BLOCKSIZE_LOG;
												 else
													 return -ENODEV;
											 }
										 }
										 break;
		case BLKGETSIZE64: {
												 int partno=minor(device)-minor(umdev_getbasedev(di->devhandle));
												 if (partno==0)
													 *(long long *)arg = mbr->size;
												 else { /* access a partition */
													 struct partition *partition=mbr->part[partno-1];
													 if (partition) 
														 *(long long *)arg = (partition->LBAnoblocks) << IDE_BLOCKSIZE_LOG;
													 else
														 return -ENODEV;
												 }
											 }
											 break;
		case HDIO_GETGEO: {
												struct hd_geometry *hdg = arg;
												*hdg=mbr->geometry;
											}
										 break;
		default: return -EINVAL;
	}
	return 0;
}

static int hdmbr_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=hdmbr_open,
	.read=hdmbr_read,
	.write=hdmbr_write,
	.release=hdmbr_release,
	.lseek=hdmbr_lseek,
	.init=hdmbr_init,
	.fini=hdmbr_fini,
	.ioctl=hdmbr_ioctl,
	.ioctlparms=hdmbr_ioctl_params,
};