aboutsummaryrefslogtreecommitdiff
path: root/partition.c
blob: 240f0f59619c1606ceafdaffdd1c46dfc4ea13a1 (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
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
/*  Copyright 2021 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools 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, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools 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 Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Buffer read/write module
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "partition.h"

typedef struct Partition_t {
	struct Stream_t head;

	mt_off_t offset; /* Offset, in bytes */
	mt_off_t size; /* size, in bytes */
	uint32_t nbSect; /* size, in sectors */
	
	uint8_t pos;

	uint8_t sectors;
	uint8_t heads;
	uint16_t cyclinders;
} Partition_t;

static __inline__ void print_hsc(hsc *h)
{
	printf(" h=%d s=%d c=%d\n",
	       head(*h), sector(*h), cyl(*h));
}

/*
 * Make sure range [ start, end ] does not overlap with partition i
 */
static int overlapCheck(struct partition *partTable, unsigned int i,
			uint32_t start, uint32_t end) {
	struct partition *partition = &partTable[i];
	if(!partition->sys_ind)
		return 0; /* Partition not allocated => ok */
	if(end > BEGIN(partition) &&
	   (start < END(partition) || END(partition) < BEGIN(partition)))
		/* overlap */
		return -1;
	return 0;
}

unsigned int findOverlap(struct partition *partTable, unsigned int until,
			 uint32_t start, uint32_t end)
{
	unsigned int i;
	for(i=1; i <= until; i++)
		if(overlapCheck(partTable, i, start, end))
			return i;
	return 0;
}


int consistencyCheck(struct partition *partTable, int doprint,
		     int verbose,
		     int *has_activated, uint32_t tot_sectors,
		     struct device *used_dev UNUSEDP,
		     unsigned int target_partition)
{
	unsigned int i;
	bool inconsistency;

	/* quick consistency check */
	inconsistency = 0;
	*has_activated = 0;
	for(i=1; i<=4; i++){
		unsigned int j;
		struct partition *partition = &partTable[i];
		if(!partition->sys_ind)
			continue;
		if(partition->boot_ind)
			(*has_activated)++;

		if(END(partition) < BEGIN(partition)) {
			fprintf(stderr,
				"End of partition %d before its begin\n",
				i);
		}

		if((j = findOverlap(partTable, i-1,
				    BEGIN(partition), END(partition)))) {
			fprintf(stderr,
				"Partitions %d and %d overlap\n",
				j, i);
			inconsistency=1;
		}

		if(tot_sectors && END(partition) >tot_sectors) {
			fprintf(stderr,
				"Partition %d extends beyond end of disk\n", i);
		}

		if(doprint && verbose) {
			if(i==target_partition)
				putchar('*');
			else
				putchar(' ');
			printf("Partition %d\n",i);

			printf("  active=%x\n", partition->boot_ind);
			printf("  start:");
			print_hsc(&partition->start);
			printf("  type=0x%x\n", partition->sys_ind);
			printf("  end:");
			print_hsc(&partition->end);
			printf("  start=%d\n", BEGIN(partition));
			printf("  nr=%d\n", _DWORD(partition->nr_sects));
			printf("\n");
		}
	}
	return inconsistency;
}


static int limit_size(Partition_t *This, mt_off_t start, size_t *len)
{
	if(start > This->size)
		return -1;
	limitSizeToOffT(len, This->size - start);
	return 0;
}

static ssize_t partition_pread(Stream_t *Stream, char *buf,
			       mt_off_t start, size_t len)
{
	DeclareThis(Partition_t);
	if(limit_size(This, start, &len) < 0)
		return -1;
	return PREADS(This->head.Next, buf, start+This->offset, len);
}

static ssize_t partition_pwrite(Stream_t *Stream, char *buf,
				mt_off_t start, size_t len)
{
	DeclareThis(Partition_t);
	if(limit_size(This, start, &len) < 0)
		return -1;
	return PWRITES(This->head.Next, buf, start+This->offset, len);
}

static int partition_data(Stream_t *Stream, time_t *date, mt_off_t *size,
			  int *type, uint32_t *address)
{
	DeclareThis(Partition_t);

	if(date || type || address) {
		int ret = GET_DATA(This->head.Next, date, NULL, type, address);
		if(ret < 0)
			return ret;
	}
	if(size)
		*size = This->size * 512;
	return 0;
}


static int partition_geom(Stream_t *Stream, struct device *dev,
			  UNUSEDP struct device *orig_dev)
{
	DeclareThis(Partition_t);

	if(!dev->tot_sectors)
		dev->tot_sectors = This->nbSect;

	return 0;
}

static Class_t PartitionClass = {
	0,
	0,
	partition_pread,
	partition_pwrite,
	0, /* flush */
	0, /* free */
	partition_geom, /* set_geom */
	partition_data, /* get_data */
	0, /* pre-allocate */
	get_dosConvert_pass_through, /* dos convert */
	0, /* discard */
};

Stream_t *OpenPartition(Stream_t *Next, struct device *dev,
			char *errmsg, mt_off_t *maxSize) {
	Partition_t *This;
	int has_activated;
	unsigned char buf[2048];
	struct partition *partTable=(struct partition *)(buf+ 0x1ae);
	uint32_t partOff;
	struct partition *partition;

	if(!dev || (dev->partition > 4) || (dev->partition <= 0)) {
	    fprintf(stderr,
		    "Invalid partition %d (must be between 1 and 4), ignoring it\n",
		    dev->partition);
	    return NULL;
	}

	This = New(Partition_t);
	if (!This){
		printOom();
		return 0;
	}
	memset((void*)This, 0, sizeof(Partition_t));
	init_head(&This->head, &PartitionClass, Next);


	/* read the first sector, or part of it */
	if (force_pread(This->head.Next, (char*) buf, 0, 512) != 512)
		goto exit_0;
	if( _WORD(buf+510) != 0xaa55) {
		/* Not a partition table */
		if(errmsg)
			sprintf(errmsg,
				"Device does not have a BIOS partition table\n");
		goto exit_0;
	}
	partition = &partTable[dev->partition];
	if(!partition->sys_ind) {
		if(errmsg)
			sprintf(errmsg,
				"Partition %d does not exist\n",
				dev->partition);
		goto exit_0;
	}

	partOff = BEGIN(partition);
	if (maxSize) {
		if (partOff > (smt_off_t)(*maxSize >> 9)) {
			if(errmsg)
				sprintf(errmsg,"init: Big disks not supported");
			goto exit_0;
		}
		*maxSize -= partOff << 9;
		maximize(*maxSize, ((mt_off_t)PART_SIZE(partition)) << 9);
	}

	This->offset = (mt_off_t) partOff << 9;

	if(!mtools_skip_check &&
	   consistencyCheck((struct partition *)(buf+0x1ae), 0, 0,
			    &has_activated, dev->tot_sectors, dev, 0)) {
		fprintf(stderr,
			"Warning: inconsistent partition table\n");
		fprintf(stderr,
			"Possibly unpartitioned device\n");
		fprintf(stderr,
			"\n*** Maybe try without partition=%d in "
			"device definition ***\n\n",
			dev->partition);
		fprintf(stderr,
			"If this is a PCMCIA card, or a disk "
			"partitioned on another computer, this "
			"message may be in error: add "
			"mtools_skip_check=1 to your .mtoolsrc "
			"file to suppress this warning\n");
	}
	dev->tot_sectors = This->nbSect = PART_SIZE(partition);
	This->size = (mt_off_t) This->nbSect << 9;
	return &This->head;
 exit_0:
	Free(This);
	return NULL;
}