summaryrefslogtreecommitdiffstats
path: root/fs/ratpfs.c
blob: 902289bab17da24ab08a4b281f72b477ba378ea9 (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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
 * 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.
 *
 * 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.
 */

#define pr_fmt(fmt) "barebox-ratpfs: " fmt

#include <common.h>
#include <command.h>
#include <init.h>
#include <malloc.h>
#include <fs.h>
#include <errno.h>
#include <linux/stat.h>
#include <asm/unaligned.h>
#include <ratp_bb.h>

#define RATPFS_TYPE_MOUNT_CALL       1
#define RATPFS_TYPE_MOUNT_RETURN     2
#define RATPFS_TYPE_READDIR_CALL     3
#define RATPFS_TYPE_READDIR_RETURN   4
#define RATPFS_TYPE_STAT_CALL        5
#define RATPFS_TYPE_STAT_RETURN      6
#define RATPFS_TYPE_OPEN_CALL        7
#define RATPFS_TYPE_OPEN_RETURN      8
#define RATPFS_TYPE_READ_CALL        9
#define RATPFS_TYPE_READ_RETURN     10
#define RATPFS_TYPE_WRITE_CALL      11
#define RATPFS_TYPE_WRITE_RETURN    12
#define RATPFS_TYPE_CLOSE_CALL      13
#define RATPFS_TYPE_CLOSE_RETURN    14
#define RATPFS_TYPE_TRUNCATE_CALL   15
#define RATPFS_TYPE_TRUNCATE_RETURN 16

struct ratpfs_file {
	uint32_t handle;
};

struct ratpfs_dir {
	char *entries;
	int len, off;
	DIR dir;
};

static int ratpfs_create(struct device_d __always_unused *dev,
			const char __always_unused *pathname,
			mode_t __always_unused mode)
{
	pr_debug("%s\n", __func__);

	return 0;
}

static int ratpfs_mkdir(struct device_d __always_unused *dev,
		       const char __always_unused *pathname)
{
	pr_debug("%s\n", __func__);

	return -ENOSYS;
}

static int ratpfs_rm(struct device_d __always_unused *dev,
		    const char *pathname)
{
	pr_debug("%s\n", __func__);

	/* Get rid of leading '/' */
	pathname = &pathname[1];

	return 0;
}

static int ratpfs_truncate(struct device_d __always_unused *dev,
			  FILE *f, ulong size)
{
	int len_tx = 1 /* type */
		+ 4 /* handle */
		+ 4 /* size */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx)+len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_file *rfile = f->priv;
	int ret;

	pr_debug("%s: len_tx=%i handle=%i size=%i\n", __func__,
		 len_tx, rfile->handle, (int)size);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_TRUNCATE_CALL;
	put_unaligned_be32(rfile->handle, &pkt_tx->data[1]);
	put_unaligned_be32(size, &pkt_tx->data[5]);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto out;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);

	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_TRUNCATE_RETURN) {
		pr_err("invalid truncate response\n");
		ret = -EIO;
		goto out;
	}

out:
	free(pkt_rx);
	return ret;
}

static int ratpfs_open(struct device_d __always_unused *dev,
		      FILE *file, const char *filename)
{
	int len_name = strlen(filename);
	int len_tx = 1 /* type */
		+ 4 /* flags */
		+ len_name /* path */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_file *rfile = xzalloc(sizeof(*rfile));
	int ret;

	pr_debug("%s: len_tx=%i filename='%s'\n", __func__, len_tx, filename);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_OPEN_CALL;
	put_unaligned_be32(file->flags, &pkt_tx->data[1]);
	memcpy(&pkt_tx->data[5], filename, len_name);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto err;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);
	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_OPEN_RETURN) {
		pr_err("invalid open response\n");
		ret = -EIO;
		goto err;
	}
	rfile->handle = get_unaligned_be32(&pkt_rx->data[1]);
	if (rfile->handle == 0) {
		ret = -get_unaligned_be32(&pkt_rx->data[5]); /* errno */
		goto err;
	}
	file->priv = rfile;
	file->size = get_unaligned_be32(&pkt_rx->data[5]);

	goto out;

err:
	file->priv = NULL;
	free(rfile);
out:
	free(pkt_rx);
	return ret;
}

static int ratpfs_close(struct device_d __always_unused *dev,
			FILE *f)
{
	int len_tx = 1 /* type */
		+ 4 /* handle */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_file *rfile = f->priv;
	int ret;

	pr_debug("%s: len_tx=%i handle=%i\n", __func__,
		 len_tx, rfile->handle);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_CLOSE_CALL;
	put_unaligned_be32(rfile->handle, &pkt_tx->data[1]);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto out;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);

	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_CLOSE_RETURN) {
		pr_err("invalid close response\n");
		goto out;
	}

out:
	free(pkt_rx);
	return ret;
}

static int ratpfs_write(struct device_d __always_unused *dev,
			FILE *f, const void *buf, size_t orig_size)
{
	int size = min((int)orig_size, 4096);
	int len_tx = 1 /* type */
		+ 4 /* handle */
		+ 4 /* pos */
		+ size /* data */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_file *rfile = f->priv;
	int ret;

	pr_debug("%s: len_tx=%i handle=%i pos=%i size=%i\n", __func__,
		 len_tx, rfile->handle, (int)f->pos, size);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_WRITE_CALL;
	put_unaligned_be32(rfile->handle, &pkt_tx->data[1]);
	put_unaligned_be32(f->pos, &pkt_tx->data[5]);
	memcpy(&pkt_tx->data[9], buf, size);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto out;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);

	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_WRITE_RETURN) {
		pr_err("invalid write response\n");
		ret = -EIO;
		goto out;
	}

	ret = size;
out:
	free(pkt_rx);

	return ret;
}

static int ratpfs_read(struct device_d __always_unused *dev,
		       FILE *f, void *buf, size_t orig_size)
{
	int size = min((int)orig_size, 4096);
	int len_tx = 1 /* type */
		+ 4 /* handle */
		+ 4 /* pos */
		+ 4 /* size */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_file *rfile = f->priv;
	int ret;

	pr_debug("%s: len_tx=%i handle=%i pos=%i size=%i\n", __func__,
		 len_tx, rfile->handle, (int)f->pos, size);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_READ_CALL;
	put_unaligned_be32(rfile->handle, &pkt_tx->data[1]);
	put_unaligned_be32(f->pos, &pkt_tx->data[5]);
	put_unaligned_be32(size, &pkt_tx->data[9]);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto out;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);
	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_READ_RETURN) {
		pr_err("invalid read response\n");
		ret = -EIO;
		goto out;
	}
	size = pkt_rx->len - 1;
	memcpy(buf, &pkt_rx->data[1], size);
	ret = size;

out:
	free(pkt_rx);
	return ret;
}

static loff_t ratpfs_lseek(struct device_d __always_unused *dev,
			  FILE *f, loff_t pos)
{
	pr_debug("%s\n", __func__);
	f->pos = pos;
	return f->pos;
}

static DIR* ratpfs_opendir(struct device_d __always_unused *dev,
			  const char *pathname)
{
	int len_name = strlen(pathname);
	int len_tx = 1 /* type */
		+ len_name /* path */;
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx)+len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	struct ratpfs_dir *rdir = xzalloc(sizeof(*rdir));
	int ret;

	pr_debug("%s: len_tx=%i pathname='%s'\n", __func__, len_tx, pathname);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_READDIR_CALL;
	memcpy(&pkt_tx->data[1], pathname, len_name);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (!ret) {
		pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);
		if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_READDIR_RETURN) {
			pr_err("invalid readdir response\n");
			free(pkt_rx);
			return NULL;
		}
		rdir->len = pkt_rx->len - 1;
		rdir->entries = xmemdup(&pkt_rx->data[1], rdir->len);
		free(pkt_rx);
		return &rdir->dir;
	} else {
		return NULL;
	}
}

static struct dirent *ratpfs_readdir(struct device_d *dev, DIR *dir)
{
	struct ratpfs_dir *rdir = container_of(dir, struct ratpfs_dir, dir);
	int i;

	pr_debug("%s\n", __func__);

	if (rdir->len <= rdir->off)
		return NULL;

	for (i = 0; rdir->off < rdir->len; rdir->off++, i++) {
		dir->d.d_name[i] = rdir->entries[rdir->off];
		if (dir->d.d_name[i] == 0)
			break;
	}
	rdir->off++;

	return &dir->d;
}

static int ratpfs_closedir(struct device_d *dev, DIR *dir)
{
	struct ratpfs_dir *rdir = container_of(dir, struct ratpfs_dir, dir);

	pr_debug("%s\n", __func__);

	free(rdir->entries);
	free(rdir);

	return 0;
}

static int ratpfs_stat(struct device_d __always_unused *dev,
		      const char *filename, struct stat *s)
{
	int len_name = strlen(filename);
	int len_tx = 1 /* type */
		+ len_name; /* path */
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	int ret;

	pr_debug("%s: len_tx=%i filename='%s'\n", __func__, len_tx, filename);

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_STAT_CALL;
	memcpy(&pkt_tx->data[1], filename, len_name);

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret) {
		ret = -EIO;
		goto out;
	}

	pr_debug("%s: len_rx=%i\n", __func__, pkt_rx->len);
	if (pkt_rx->len < 6 || pkt_rx->data[0] != RATPFS_TYPE_STAT_RETURN) {
		pr_err("invalid stat response\n");
		goto out;
	}
	switch (pkt_rx->data[1]) {
	case 0:
		ret = -ENOENT;
		break;
	case 1:
		s->st_mode = S_IFREG | S_IRWXU | S_IRWXG | S_IRWXO;
		break;
	case 2:
		s->st_mode = S_IFDIR | S_IRWXU | S_IRWXG | S_IRWXO;
		break;
	}
	s->st_size = get_unaligned_be32(&pkt_rx->data[2]);

out:
	free(pkt_rx);
	return ret;
}

static int ratpfs_probe(struct device_d *dev)
{
	int len_tx = 1; /* type */
	struct ratp_bb_pkt *pkt_tx = xzalloc(sizeof(*pkt_tx) + len_tx);
	struct ratp_bb_pkt *pkt_rx = NULL;
	int ret;
	struct fs_device_d *fsdev = dev_to_fs_device(dev);

	pr_debug("%s\n", __func__);

	ret = barebox_ratp_fs_mount(fsdev->path);
	if (ret)
		return ret;

	pkt_tx->len = len_tx;
	pkt_tx->data[0] = RATPFS_TYPE_MOUNT_CALL;

	ret = barebox_ratp_fs_call(pkt_tx, &pkt_rx);
	if (ret)
		goto out;

	if (pkt_rx->len < 1 || pkt_rx->data[0] != RATPFS_TYPE_MOUNT_RETURN) {
		pr_err("invalid mount response\n");
		ret = -EINVAL;
		goto out;
	}

out:
	free(pkt_rx);

	if (ret)
		barebox_ratp_fs_mount(NULL);

	return ret;
}

static void ratpfs_remove(struct device_d __always_unused *dev)
{
	pr_debug("%s\n", __func__);

	barebox_ratp_fs_mount(NULL);
}

static struct fs_driver_d ratpfs_driver = {
	.open      = ratpfs_open,
	.close     = ratpfs_close,
	.read      = ratpfs_read,
	.lseek     = ratpfs_lseek,
	.opendir   = ratpfs_opendir,
	.readdir   = ratpfs_readdir,
	.closedir  = ratpfs_closedir,
	.stat      = ratpfs_stat,
	.create    = ratpfs_create,
	.unlink    = ratpfs_rm,
	.mkdir     = ratpfs_mkdir,
	.rmdir     = ratpfs_rm,
	.write     = ratpfs_write,
	.truncate  = ratpfs_truncate,
	.flags     = FS_DRIVER_NO_DEV,
	.drv = {
		.probe  = ratpfs_probe,
		.remove = ratpfs_remove,
		.name = "ratpfs",
	}
};

static int ratpfs_init(void)
{
	return register_fs_driver(&ratpfs_driver);
}
coredevice_initcall(ratpfs_init);