aboutsummaryrefslogtreecommitdiff
path: root/remap.c
blob: ab96d04e5c3db3fc764c3dcc01b86f82bc385134 (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
/*  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/>.
 *
 * Remapping shim
 */

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

enum map_type_t {
		 DATA,
		 ZERO,
		 SKIP,
		 POS
};

struct map {
	mt_off_t orig;
	mt_off_t remapped;
	enum map_type_t type;
};

typedef struct Remap_t {
	struct Stream_t head;

	struct map *map;
	int mapSize;

	mt_off_t net_offset;
} Remap_t;

static enum map_type_t remap(Remap_t *This, mt_off_t *start, size_t *len) {
	int i;
	for(i=0; i < This->mapSize - 1; i++)
		if(*start < This->map[i+1].remapped) {
			limitSizeToOffT(len, This->map[i+1].remapped - *start);
			break;
		}
	*start = *start - This->map[i].remapped + This->map[i].orig;
	return This->map[i].type;
}

static ssize_t remap_pread(Stream_t *Stream, char *buf,
			  mt_off_t start, size_t len)
{
	DeclareThis(Remap_t);
	if(remap(This, &start, &len)==DATA)
		return PREADS(This->head.Next, buf, start, len);
	else {
		memset(buf, 0, len);
		return (ssize_t) len;
	}
}

static ssize_t remap_pwrite(Stream_t *Stream, char *buf,
			   mt_off_t start, size_t len)
{
	DeclareThis(Remap_t);
	if(remap(This, &start, &len)==DATA)
		return PWRITES(This->head.Next, buf, start, len);
	else {
		unsigned int i;
		/* When writing to a "zero" sector, make sure that we
		   indeed only write zeroes back to there. Helps catch
		   putting filesystems with parameters unsuitable to
		   the particular mapping */
		for(i=0; i<len; i++) {
			if(buf[i]) {
				fprintf(stderr, "Bad data written to unmapped sectors\n");
				errno = EFAULT;
				return -1;
			}
		}
		return (ssize_t) len;
	}
}

static int remap_free(Stream_t *Stream)
{
	DeclareThis(Remap_t);
	if(This->map)
		free(This->map);
	return 0;
}

static Class_t RemapClass = {
	0,
	0,
	remap_pread,
	remap_pwrite,
	0, /* flush */
	remap_free, /* free */
	set_geom_pass_through, /* set_geom */
	0, /* get_data */
	0, /* pre-allocate */
	get_dosConvert_pass_through, /* dos convert */
	0, /* discard */
};

static int process_map(Remap_t *This, const char *ptr,
		       int countOnly, char *errmsg) {
	mt_off_t orig=0;
	mt_off_t remapped=0;
	int count=0;
	int atEnd=0;
	char *eptr;
	while(!atEnd) {
		mt_off_t len;
		enum map_type_t type;
		if(*ptr=='\0') {
			type=DATA;
			atEnd=1;
		} else if(!strncmp(ptr, "skip", 4)) {
			type=SKIP;
			ptr+=4;
		} else if(!strncmp(ptr, "zero", 4)) {
			type=ZERO;
			ptr+=4;
		} else if(!strncmp(ptr, "pos", 3)) {
			type=POS;
			ptr+=3;
		} else {
			type=DATA;
		}

		len=str_to_off_with_end(ptr,&eptr);
		ptr=eptr;
		switch(*ptr) {
		case '\0':
			/* End of string */
			break;
		case ',':
			/* Move on to next item */
			ptr++;
			break;
		default:
			sprintf(errmsg, "Bad number %s\n", ptr);
			return -1;
		}

		if(type == POS) {
			orig = len;
			continue;
		}
		if(type != SKIP) {
			if(!countOnly) {
				struct map *m = This->map+count;
				m->orig = orig;
				m->remapped = remapped;
				m->type = type;
			}
			remapped+=len;
			count++;
		}
		if(type != ZERO) {
			orig+=len;
		}

	}
	This->net_offset = orig-remapped;
	return count;
}


Stream_t *Remap(Stream_t *Next, struct device *dev, char *errmsg) {
	Remap_t *This;
	int nrItems=0;
	const char *map = dev->data_map;

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

	/* First count number of items */
	nrItems=process_map(This, map, 1, errmsg);
	if(nrItems < 0) {
		free(This);
		return NULL;
	}

	This->map = calloc((size_t)nrItems, sizeof(struct map));
	if(!This->map) {
		printOom();
		goto exit_0;
	}

	process_map(This, map, 0, errmsg);

	if(adjust_tot_sectors(dev, This->net_offset, errmsg) < 0)
		goto exit_1;

	This->mapSize=nrItems;
	return &This->head;
 exit_1:
	free(This->map);
 exit_0:
	free(This);
	printOom();
	return NULL;
}