1*d5c9a868SElliott Hughes /* Copyright 2021 Alain Knaff.
2*d5c9a868SElliott Hughes * This file is part of mtools.
3*d5c9a868SElliott Hughes *
4*d5c9a868SElliott Hughes * Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes * it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes * the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes * (at your option) any later version.
8*d5c9a868SElliott Hughes *
9*d5c9a868SElliott Hughes * Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12*d5c9a868SElliott Hughes * GNU General Public License for more details.
13*d5c9a868SElliott Hughes *
14*d5c9a868SElliott Hughes * You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes * along with Mtools. If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes *
17*d5c9a868SElliott Hughes * Buffer read/write module
18*d5c9a868SElliott Hughes */
19*d5c9a868SElliott Hughes
20*d5c9a868SElliott Hughes #include "sysincludes.h"
21*d5c9a868SElliott Hughes #include "msdos.h"
22*d5c9a868SElliott Hughes #include "mtools.h"
23*d5c9a868SElliott Hughes #include "partition.h"
24*d5c9a868SElliott Hughes
25*d5c9a868SElliott Hughes typedef struct Partition_t {
26*d5c9a868SElliott Hughes struct Stream_t head;
27*d5c9a868SElliott Hughes
28*d5c9a868SElliott Hughes mt_off_t offset; /* Offset, in bytes */
29*d5c9a868SElliott Hughes mt_off_t size; /* size, in bytes */
30*d5c9a868SElliott Hughes uint32_t nbSect; /* size, in sectors */
31*d5c9a868SElliott Hughes
32*d5c9a868SElliott Hughes uint8_t pos;
33*d5c9a868SElliott Hughes
34*d5c9a868SElliott Hughes uint8_t sectors;
35*d5c9a868SElliott Hughes uint8_t heads;
36*d5c9a868SElliott Hughes uint16_t cyclinders;
37*d5c9a868SElliott Hughes } Partition_t;
38*d5c9a868SElliott Hughes
print_hsc(hsc * h)39*d5c9a868SElliott Hughes static __inline__ void print_hsc(hsc *h)
40*d5c9a868SElliott Hughes {
41*d5c9a868SElliott Hughes printf(" h=%d s=%d c=%d\n",
42*d5c9a868SElliott Hughes head(*h), sector(*h), cyl(*h));
43*d5c9a868SElliott Hughes }
44*d5c9a868SElliott Hughes
45*d5c9a868SElliott Hughes /*
46*d5c9a868SElliott Hughes * Make sure range [ start, end ] does not overlap with partition i
47*d5c9a868SElliott Hughes */
overlapCheck(struct partition * partTable,unsigned int i,uint32_t start,uint32_t end)48*d5c9a868SElliott Hughes static int overlapCheck(struct partition *partTable, unsigned int i,
49*d5c9a868SElliott Hughes uint32_t start, uint32_t end) {
50*d5c9a868SElliott Hughes struct partition *partition = &partTable[i];
51*d5c9a868SElliott Hughes if(!partition->sys_ind)
52*d5c9a868SElliott Hughes return 0; /* Partition not allocated => ok */
53*d5c9a868SElliott Hughes if(end > BEGIN(partition) &&
54*d5c9a868SElliott Hughes (start < END(partition) || END(partition) < BEGIN(partition)))
55*d5c9a868SElliott Hughes /* overlap */
56*d5c9a868SElliott Hughes return -1;
57*d5c9a868SElliott Hughes return 0;
58*d5c9a868SElliott Hughes }
59*d5c9a868SElliott Hughes
findOverlap(struct partition * partTable,unsigned int until,uint32_t start,uint32_t end)60*d5c9a868SElliott Hughes unsigned int findOverlap(struct partition *partTable, unsigned int until,
61*d5c9a868SElliott Hughes uint32_t start, uint32_t end)
62*d5c9a868SElliott Hughes {
63*d5c9a868SElliott Hughes unsigned int i;
64*d5c9a868SElliott Hughes for(i=1; i <= until; i++)
65*d5c9a868SElliott Hughes if(overlapCheck(partTable, i, start, end))
66*d5c9a868SElliott Hughes return i;
67*d5c9a868SElliott Hughes return 0;
68*d5c9a868SElliott Hughes }
69*d5c9a868SElliott Hughes
70*d5c9a868SElliott Hughes
consistencyCheck(struct partition * partTable,int doprint,int verbose,int * has_activated,uint32_t tot_sectors,struct device * used_dev UNUSEDP,unsigned int target_partition)71*d5c9a868SElliott Hughes int consistencyCheck(struct partition *partTable, int doprint,
72*d5c9a868SElliott Hughes int verbose,
73*d5c9a868SElliott Hughes int *has_activated, uint32_t tot_sectors,
74*d5c9a868SElliott Hughes struct device *used_dev UNUSEDP,
75*d5c9a868SElliott Hughes unsigned int target_partition)
76*d5c9a868SElliott Hughes {
77*d5c9a868SElliott Hughes unsigned int i;
78*d5c9a868SElliott Hughes bool inconsistency;
79*d5c9a868SElliott Hughes
80*d5c9a868SElliott Hughes /* quick consistency check */
81*d5c9a868SElliott Hughes inconsistency = 0;
82*d5c9a868SElliott Hughes *has_activated = 0;
83*d5c9a868SElliott Hughes for(i=1; i<=4; i++){
84*d5c9a868SElliott Hughes unsigned int j;
85*d5c9a868SElliott Hughes struct partition *partition = &partTable[i];
86*d5c9a868SElliott Hughes if(!partition->sys_ind)
87*d5c9a868SElliott Hughes continue;
88*d5c9a868SElliott Hughes if(partition->boot_ind)
89*d5c9a868SElliott Hughes (*has_activated)++;
90*d5c9a868SElliott Hughes
91*d5c9a868SElliott Hughes if(END(partition) < BEGIN(partition)) {
92*d5c9a868SElliott Hughes fprintf(stderr,
93*d5c9a868SElliott Hughes "End of partition %d before its begin\n",
94*d5c9a868SElliott Hughes i);
95*d5c9a868SElliott Hughes }
96*d5c9a868SElliott Hughes
97*d5c9a868SElliott Hughes if((j = findOverlap(partTable, i-1,
98*d5c9a868SElliott Hughes BEGIN(partition), END(partition)))) {
99*d5c9a868SElliott Hughes fprintf(stderr,
100*d5c9a868SElliott Hughes "Partitions %d and %d overlap\n",
101*d5c9a868SElliott Hughes j, i);
102*d5c9a868SElliott Hughes inconsistency=1;
103*d5c9a868SElliott Hughes }
104*d5c9a868SElliott Hughes
105*d5c9a868SElliott Hughes if(tot_sectors && END(partition) >tot_sectors) {
106*d5c9a868SElliott Hughes fprintf(stderr,
107*d5c9a868SElliott Hughes "Partition %d extends beyond end of disk\n", i);
108*d5c9a868SElliott Hughes }
109*d5c9a868SElliott Hughes
110*d5c9a868SElliott Hughes if(doprint && verbose) {
111*d5c9a868SElliott Hughes if(i==target_partition)
112*d5c9a868SElliott Hughes putchar('*');
113*d5c9a868SElliott Hughes else
114*d5c9a868SElliott Hughes putchar(' ');
115*d5c9a868SElliott Hughes printf("Partition %d\n",i);
116*d5c9a868SElliott Hughes
117*d5c9a868SElliott Hughes printf(" active=%x\n", partition->boot_ind);
118*d5c9a868SElliott Hughes printf(" start:");
119*d5c9a868SElliott Hughes print_hsc(&partition->start);
120*d5c9a868SElliott Hughes printf(" type=0x%x\n", partition->sys_ind);
121*d5c9a868SElliott Hughes printf(" end:");
122*d5c9a868SElliott Hughes print_hsc(&partition->end);
123*d5c9a868SElliott Hughes printf(" start=%d\n", BEGIN(partition));
124*d5c9a868SElliott Hughes printf(" nr=%d\n", _DWORD(partition->nr_sects));
125*d5c9a868SElliott Hughes printf("\n");
126*d5c9a868SElliott Hughes }
127*d5c9a868SElliott Hughes }
128*d5c9a868SElliott Hughes return inconsistency;
129*d5c9a868SElliott Hughes }
130*d5c9a868SElliott Hughes
131*d5c9a868SElliott Hughes
limit_size(Partition_t * This,mt_off_t start,size_t * len)132*d5c9a868SElliott Hughes static int limit_size(Partition_t *This, mt_off_t start, size_t *len)
133*d5c9a868SElliott Hughes {
134*d5c9a868SElliott Hughes if(start > This->size)
135*d5c9a868SElliott Hughes return -1;
136*d5c9a868SElliott Hughes limitSizeToOffT(len, This->size - start);
137*d5c9a868SElliott Hughes return 0;
138*d5c9a868SElliott Hughes }
139*d5c9a868SElliott Hughes
partition_pread(Stream_t * Stream,char * buf,mt_off_t start,size_t len)140*d5c9a868SElliott Hughes static ssize_t partition_pread(Stream_t *Stream, char *buf,
141*d5c9a868SElliott Hughes mt_off_t start, size_t len)
142*d5c9a868SElliott Hughes {
143*d5c9a868SElliott Hughes DeclareThis(Partition_t);
144*d5c9a868SElliott Hughes if(limit_size(This, start, &len) < 0)
145*d5c9a868SElliott Hughes return -1;
146*d5c9a868SElliott Hughes return PREADS(This->head.Next, buf, start+This->offset, len);
147*d5c9a868SElliott Hughes }
148*d5c9a868SElliott Hughes
partition_pwrite(Stream_t * Stream,char * buf,mt_off_t start,size_t len)149*d5c9a868SElliott Hughes static ssize_t partition_pwrite(Stream_t *Stream, char *buf,
150*d5c9a868SElliott Hughes mt_off_t start, size_t len)
151*d5c9a868SElliott Hughes {
152*d5c9a868SElliott Hughes DeclareThis(Partition_t);
153*d5c9a868SElliott Hughes if(limit_size(This, start, &len) < 0)
154*d5c9a868SElliott Hughes return -1;
155*d5c9a868SElliott Hughes return PWRITES(This->head.Next, buf, start+This->offset, len);
156*d5c9a868SElliott Hughes }
157*d5c9a868SElliott Hughes
partition_data(Stream_t * Stream,time_t * date,mt_off_t * size,int * type,uint32_t * address)158*d5c9a868SElliott Hughes static int partition_data(Stream_t *Stream, time_t *date, mt_off_t *size,
159*d5c9a868SElliott Hughes int *type, uint32_t *address)
160*d5c9a868SElliott Hughes {
161*d5c9a868SElliott Hughes DeclareThis(Partition_t);
162*d5c9a868SElliott Hughes
163*d5c9a868SElliott Hughes if(date || type || address) {
164*d5c9a868SElliott Hughes int ret = GET_DATA(This->head.Next, date, NULL, type, address);
165*d5c9a868SElliott Hughes if(ret < 0)
166*d5c9a868SElliott Hughes return ret;
167*d5c9a868SElliott Hughes }
168*d5c9a868SElliott Hughes if(size)
169*d5c9a868SElliott Hughes *size = This->size * 512;
170*d5c9a868SElliott Hughes return 0;
171*d5c9a868SElliott Hughes }
172*d5c9a868SElliott Hughes
173*d5c9a868SElliott Hughes
partition_geom(Stream_t * Stream,struct device * dev,UNUSEDP struct device * orig_dev)174*d5c9a868SElliott Hughes static int partition_geom(Stream_t *Stream, struct device *dev,
175*d5c9a868SElliott Hughes UNUSEDP struct device *orig_dev)
176*d5c9a868SElliott Hughes {
177*d5c9a868SElliott Hughes DeclareThis(Partition_t);
178*d5c9a868SElliott Hughes
179*d5c9a868SElliott Hughes if(!dev->tot_sectors)
180*d5c9a868SElliott Hughes dev->tot_sectors = This->nbSect;
181*d5c9a868SElliott Hughes
182*d5c9a868SElliott Hughes return 0;
183*d5c9a868SElliott Hughes }
184*d5c9a868SElliott Hughes
185*d5c9a868SElliott Hughes static Class_t PartitionClass = {
186*d5c9a868SElliott Hughes 0,
187*d5c9a868SElliott Hughes 0,
188*d5c9a868SElliott Hughes partition_pread,
189*d5c9a868SElliott Hughes partition_pwrite,
190*d5c9a868SElliott Hughes 0, /* flush */
191*d5c9a868SElliott Hughes 0, /* free */
192*d5c9a868SElliott Hughes partition_geom, /* set_geom */
193*d5c9a868SElliott Hughes partition_data, /* get_data */
194*d5c9a868SElliott Hughes 0, /* pre-allocate */
195*d5c9a868SElliott Hughes get_dosConvert_pass_through, /* dos convert */
196*d5c9a868SElliott Hughes 0, /* discard */
197*d5c9a868SElliott Hughes };
198*d5c9a868SElliott Hughes
OpenPartition(Stream_t * Next,struct device * dev,char * errmsg,mt_off_t * maxSize)199*d5c9a868SElliott Hughes Stream_t *OpenPartition(Stream_t *Next, struct device *dev,
200*d5c9a868SElliott Hughes char *errmsg, mt_off_t *maxSize) {
201*d5c9a868SElliott Hughes Partition_t *This;
202*d5c9a868SElliott Hughes int has_activated;
203*d5c9a868SElliott Hughes unsigned char buf[2048];
204*d5c9a868SElliott Hughes struct partition *partTable=(struct partition *)(buf+ 0x1ae);
205*d5c9a868SElliott Hughes uint32_t partOff;
206*d5c9a868SElliott Hughes struct partition *partition;
207*d5c9a868SElliott Hughes
208*d5c9a868SElliott Hughes if(!dev || (dev->partition > 4) || (dev->partition <= 0)) {
209*d5c9a868SElliott Hughes fprintf(stderr,
210*d5c9a868SElliott Hughes "Invalid partition %d (must be between 1 and 4), ignoring it\n",
211*d5c9a868SElliott Hughes dev->partition);
212*d5c9a868SElliott Hughes return NULL;
213*d5c9a868SElliott Hughes }
214*d5c9a868SElliott Hughes
215*d5c9a868SElliott Hughes This = New(Partition_t);
216*d5c9a868SElliott Hughes if (!This){
217*d5c9a868SElliott Hughes printOom();
218*d5c9a868SElliott Hughes return 0;
219*d5c9a868SElliott Hughes }
220*d5c9a868SElliott Hughes memset((void*)This, 0, sizeof(Partition_t));
221*d5c9a868SElliott Hughes init_head(&This->head, &PartitionClass, Next);
222*d5c9a868SElliott Hughes
223*d5c9a868SElliott Hughes
224*d5c9a868SElliott Hughes /* read the first sector, or part of it */
225*d5c9a868SElliott Hughes if (force_pread(This->head.Next, (char*) buf, 0, 512) != 512)
226*d5c9a868SElliott Hughes goto exit_0;
227*d5c9a868SElliott Hughes if( _WORD(buf+510) != 0xaa55) {
228*d5c9a868SElliott Hughes /* Not a partition table */
229*d5c9a868SElliott Hughes if(errmsg)
230*d5c9a868SElliott Hughes sprintf(errmsg,
231*d5c9a868SElliott Hughes "Device does not have a BIOS partition table\n");
232*d5c9a868SElliott Hughes goto exit_0;
233*d5c9a868SElliott Hughes }
234*d5c9a868SElliott Hughes partition = &partTable[dev->partition];
235*d5c9a868SElliott Hughes if(!partition->sys_ind) {
236*d5c9a868SElliott Hughes if(errmsg)
237*d5c9a868SElliott Hughes sprintf(errmsg,
238*d5c9a868SElliott Hughes "Partition %d does not exist\n",
239*d5c9a868SElliott Hughes dev->partition);
240*d5c9a868SElliott Hughes goto exit_0;
241*d5c9a868SElliott Hughes }
242*d5c9a868SElliott Hughes
243*d5c9a868SElliott Hughes partOff = BEGIN(partition);
244*d5c9a868SElliott Hughes if (maxSize) {
245*d5c9a868SElliott Hughes if (partOff > (smt_off_t)(*maxSize >> 9)) {
246*d5c9a868SElliott Hughes if(errmsg)
247*d5c9a868SElliott Hughes sprintf(errmsg,"init: Big disks not supported");
248*d5c9a868SElliott Hughes goto exit_0;
249*d5c9a868SElliott Hughes }
250*d5c9a868SElliott Hughes *maxSize -= partOff << 9;
251*d5c9a868SElliott Hughes maximize(*maxSize, ((mt_off_t)PART_SIZE(partition)) << 9);
252*d5c9a868SElliott Hughes }
253*d5c9a868SElliott Hughes
254*d5c9a868SElliott Hughes This->offset = (mt_off_t) partOff << 9;
255*d5c9a868SElliott Hughes
256*d5c9a868SElliott Hughes if(!mtools_skip_check &&
257*d5c9a868SElliott Hughes consistencyCheck((struct partition *)(buf+0x1ae), 0, 0,
258*d5c9a868SElliott Hughes &has_activated, dev->tot_sectors, dev, 0)) {
259*d5c9a868SElliott Hughes fprintf(stderr,
260*d5c9a868SElliott Hughes "Warning: inconsistent partition table\n");
261*d5c9a868SElliott Hughes fprintf(stderr,
262*d5c9a868SElliott Hughes "Possibly unpartitioned device\n");
263*d5c9a868SElliott Hughes fprintf(stderr,
264*d5c9a868SElliott Hughes "\n*** Maybe try without partition=%d in "
265*d5c9a868SElliott Hughes "device definition ***\n\n",
266*d5c9a868SElliott Hughes dev->partition);
267*d5c9a868SElliott Hughes fprintf(stderr,
268*d5c9a868SElliott Hughes "If this is a PCMCIA card, or a disk "
269*d5c9a868SElliott Hughes "partitioned on another computer, this "
270*d5c9a868SElliott Hughes "message may be in error: add "
271*d5c9a868SElliott Hughes "mtools_skip_check=1 to your .mtoolsrc "
272*d5c9a868SElliott Hughes "file to suppress this warning\n");
273*d5c9a868SElliott Hughes }
274*d5c9a868SElliott Hughes dev->tot_sectors = This->nbSect = PART_SIZE(partition);
275*d5c9a868SElliott Hughes This->size = (mt_off_t) This->nbSect << 9;
276*d5c9a868SElliott Hughes return &This->head;
277*d5c9a868SElliott Hughes exit_0:
278*d5c9a868SElliott Hughes Free(This);
279*d5c9a868SElliott Hughes return NULL;
280*d5c9a868SElliott Hughes }
281*d5c9a868SElliott Hughes
282