summaryrefslogtreecommitdiff
path: root/fs/fat/rockbox_wrapper.c
blob: dd91d3831d8da273c55931fc9aa4ea5a04884d64 (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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/*------------------------------------------------------------------------------
 * (C) Copyright 2006
 * Keith Outwater, (outwater4@comcast.net)
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * 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; either version 2 of
 * the License, or (at your option) any later version.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

#include <common.h>
#include <config.h>

/*
 * FIXME: The pwd, cd and file path builder code needs to be fixed/implemented.
 */

#include <asm/errno.h>
#include <asm/string.h>
#include <fat.h>
#include <linux/string.h>
#include "rockbox_debug.h"
#include <rockbox_mv.h>

/* Routines, exported or not... */
long file_fat_size(const char *filename);

extern int errno;	/* see board/<board>/<board>.c */

const char *month[] = {
	"(null)",
	"Jan", "Feb", "Mar", "Apr", "May", "Jun",
	"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

/*
 * The current working directory.
 * Should we keep track of this on a per-partiton basis?
 */
char file_cwd[ROCKBOX_CWD_LEN + 1] = "/";

#ifndef __HAVE_ARCH_STRNICMP
/*
 * Convert a character to lower case
 */
__inline__ static char
_tolower(char c)
{
	if ((c >= 'A') && (c <= 'Z')) {
		c = (c - 'A') + 'a';
	}
	return c;
}

/**
 * strnicmp - Case insensitive, length-limited string comparison
 * @s1: One string
 * @s2: The other string
 * @len: the maximum number of characters to compare
 *
 * FIXME: replaces strnicmp() in rockbox-3.1. check is keep best one
 */
int
strnicmp(const char *s1, const char *s2, size_t len)
{
	/*
	 * Yes, Virginia, it had better be unsigned 
	 */
	unsigned char c1, c2;

	c1 = 0;
	c2 = 0;
	if (len) {
		do {
			c1 = *s1;
			c2 = *s2;
			s1++;
			s2++;
			if (!c1)
				break;
			if (!c2)
				break;
			if (c1 == c2)
				continue;
			c1 = _tolower(c1);
			c2 = _tolower(c2);
			if (c1 != c2)
				break;
		} while (--len);
	}
	return (int) c1 - (int) c2;
}
#endif

/**
 * strcasecmp - Case insensitive string comparison
 * @s1: One string
 * @s2: The other string
 */
int strcasecmp(const char *s1, const char *s2)
{
    while (*s1 != '\0' && _tolower(*s1) == _tolower(*s2)) {
        s1++;
        s2++;
    }

    return _tolower(*(unsigned char *) s1) - _tolower(*(unsigned char *) s2);
}


/**
 * strncasecmp - Case insensitive, length-limited string comparison
 * @s1: One string
 * @s2: The other string
 * @len: the maximum number of characters to compare
 *
 * FIXME: replaces strnicmp() in rockbox-3.1. check is keep best one
 */
int strncasecmp(const char *s1, const char *s2, size_t n)
{
    if(!n)
      return 0;

    while (n-- != 0 && _tolower(*s1) == _tolower(*s2)) {
        if(n == 0 || *s1 == '\0')
          break;
        s1++;
        s2++;
    }

    return _tolower(*(unsigned char *) s1) - _tolower(*(unsigned char *) s2);
}


/*
 * U-Boot 'fat ls' wrapper. Code lifted from rockbox "filetree.c"
 * Return number of bytes read on success, -1 on error.
 */
int
file_fat_ls(const char *dirname)
{
	uint i, max_entries;
	int name_buffer_used = 0;
	DIR *dir;

	struct tree_context {
		int filesindir;
		int dirsindir;
		int dirlength;
		void *dircache;   /* unused, see below */
		char name_buffer[2048];
		int name_buffer_size;
		bool dirfull;
	} c;

	struct entry {
		short attr;	/* FAT attributes + file type flags */
		unsigned long time_write;	/* Last write time */
		char *name;
	};

	if (*dirname == '/') {
		while(*(dirname+1) == '/') dirname++;
	}
	if (strlen(dirname) > ROCKBOX_CWD_LEN) {
		errno = ENAMETOOLONG;
		return -1;
	}

	dir = opendir(dirname);
	if (!dir) {
		/* maybe it's a regular file */
		i = file_fat_size(dirname);
		if (i != -1) {
			printf("%9lu  %s\n", (ulong) i, dirname);
			return 0;
		}
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Error: ls() of \"%s\" failed: %s\n",
			dirname, strerror(errno));
#endif
		return -1;
	}

	c.dirsindir = 0;
	c.dirfull = false;
	c.name_buffer_size = sizeof (c.name_buffer) - 1;

	/*
	 * FIXME: figure out max i 
	 */
	max_entries = (strncmp("/", dirname, 2)==0) ? 512 : 0xFFFFFFFF;
	for (i = 0; i < max_entries; i++) {
		int len;
		struct dirent *entry = readdir(dir);
#if 0	/* dircache unused and not initialized ! */
		struct entry *dptr =
		    (struct entry *) (c.dircache + i * sizeof (struct entry));
#endif
		if (!entry)
			break;

		len = strlen((char*)entry->d_name);
		printf("%9lu %s %02d %4d %02d:%02d:%02d [%s] %s\n",
		       entry->size,
		       month[(entry->wrtdate >> 5) & 0xf],
		       entry->wrtdate & 0xf,
		       ((entry->wrtdate >> 9) & 0x7f) + 1980,
		       (entry->wrttime >> 11) & 0x1f,
		       (entry->wrttime >> 5) & 0x3f, entry->wrttime & 0x1f,
			   (entry->attribute & FAT_ATTR_DIRECTORY) ? "dir " : "file",
		       entry->d_name);
#if 0
		/*
		 * skip directories . and ..
		 */
		if ((entry->attribute & FAT_ATTR_DIRECTORY) &&
		    (((len == 1) &&
		      (!strncmp(entry->d_name, ".", 1))) ||
		     ((len == 2) && (!strncmp(entry->d_name, "..", 2))))) {
			i--;
			continue;
		}

		/*
		 * Skip FAT volume ID
		 */
		if (entry->attribute & FAT_ATTR_VOLUME_ID) {
			i--;
			continue;
		}
#endif

#if 0	/* dircache unused and not initialized ! */
		dptr->attr = entry->attribute;
#endif
		if (len > c.name_buffer_size - name_buffer_used - 1) {
			/*
			 * Tell the world that we ran out of buffer space 
			 */
			c.dirfull = true;
			fat_dprintf("Dir buffer is full\n");
			break;
		}

#if 0	/* dircache unused and not initialized ! */
		dptr->name = &c.name_buffer[name_buffer_used];
		dptr->time_write =
		    (long) entry->wrtdate << 16 | (long) entry->wrttime;
		strcpy(dptr->name, (char*)entry->d_name);
#endif
		name_buffer_used += len + 1;

		/*
		 * count the remaining dirs 
		 */
#if 0	/* dircache unused and not initialized ! */
		if (dptr->attr & FAT_ATTR_DIRECTORY)
#else
		if (entry->attribute & FAT_ATTR_DIRECTORY)
#endif
			c.dirsindir++;
	}

	c.filesindir = i;
	c.dirlength = i;
	closedir(dir);
	printf("\n%d file%s %d dir%s",
	       c.filesindir, c.filesindir == 1 ? "," : "s,",
	       c.dirsindir, c.dirsindir == 1 ? "\n" : "s\n");
	return 0;
}

/*
 * return target file size (negative if failure)
 * Return number of bytes of the target file, -1 on error.
 */
long
file_fat_size(const char *filename)
{
	int fd;
	long file_size;

	printf("Reading %s\n", filename);
	fd = open(filename, O_RDONLY);
	if (fd < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Error: open() of \"%s\" failed: %s\n",
			filename, strerror(errno));
#endif
		return -1;
	}

	file_size = filesize(fd);
	if (file_size < 0) {
		fat_eprintf("Call to filesize() failed\n");
		close(fd);
		return -1;
	}

	close(fd);
	return file_size;
}


/*
 * U-Boot 'file read' wrapper.
 * Return number of bytes read on success, -1 on error.
 */
long
file_fat_read(const char *filename, void *buffer, unsigned long maxsize)
{
	int fd;
	long bytes;
	long file_size;

	printf("Reading %s\n", filename);
	fd = open(filename, O_RDONLY);
	if (fd < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Error: open() of \"%s\" failed: %s\n",
			filename, strerror(errno));
#endif
		return -1;
	}

	file_size = filesize(fd);
	if (file_size < 0) {
		fat_eprintf("Call to filesize() failed\n");
		return -1;
	}

	/*
	 * If the number of bytes to read is zero, read the entire file.
	 */
	if ((maxsize != 0) && (maxsize < file_size))
		file_size = maxsize;

	bytes = (long) read(fd, buffer, (size_t) file_size);
	if ((unsigned long) bytes != file_size) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Read error: %s\n", strerror(errno));
#endif
		close(fd);
		return -1;
	}

	close(fd);
	return bytes;
}

/*
 * U-Boot 'file write' wrapper.
 * Return number of bytes written on success, -1 on error.
 */
long
file_fat_write(const char *filename, void *buffer, unsigned long maxsize)
{
	int fd;
	long bytes;

	printf("Writing %s\n", filename);
	fd = open(filename, O_WRONLY | O_CREAT);
	if (fd < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Open of \"%s\" failed: %s\n",
			filename, strerror(errno));
#endif
		return -1;
	}

	bytes = (long) write(fd, buffer, (size_t) maxsize);
	if ((unsigned long) bytes != maxsize) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Write failed: %s\n", strerror(errno));
#endif
		close(fd);
		return -1;
	}

	close(fd);
	return bytes;
}

int
file_fat_pwd(void)
{
	printf("%s\n", file_cwd);
	return 1;
}

/*
 * U-Boot 'file rm' wrapper.
 * Return 1 on success, -1 on error and set 'errno'.
 */
int
file_fat_rm(const char *filename)
{
	int rc;

	fat_dprintf("Remove \"%s\"\n", filename);
	rc = remove(filename);
	if (rc < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Removal of \"%s\" failed: %s\n",
			filename, strerror(errno));
#endif
		return -1;
	}

	return 1;
}

/*
 * U-Boot 'file mkdir' wrapper.
 * Return 1 on success, -1 on error and set 'errno'.
 */
int
file_fat_mkdir(const char *dirname)
{
	int rc;

	fat_dprintf("Make dir \"%s\"\n", dirname);
	rc = mkdir(dirname);
	if (rc < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Creation of \"%s\" failed: %s\n",
			dirname, strerror(errno));
#endif
		return -1;
	}

	return 1;
}

/*
 * U-Boot 'file rmdir' wrapper.
 * Return 1 on success, -1 on error and set 'errno'.
 */
int
file_fat_rmdir(const char *dirname)
{
	int rc;

	fat_dprintf("Remove dir \"%s\"\n", dirname);
	rc = rmdir(dirname);
	if (rc < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Removal of \"%s\" failed: %s\n",
			dirname, strerror(errno));
#endif
		return -1;
	}

	return 1;
}

static void
pathcpy(char *dest, const char *src)
{
	char *origdest = dest;

	do {
		if (dest - file_cwd >= CWD_LEN) {
			*dest = '\0';
			return;
		}

		*(dest) = *(src);
		if (*src == '\0') {
			if (dest-- != origdest && ISDIRDELIM(*dest)) {
				*dest = '\0';
			}
			return;
		}

		++dest;
		if (ISDIRDELIM(*src)) {
			while (ISDIRDELIM(*src))
				src++;
		} else {
			src++;
		}
	} while (1);
}

/*
 * U-Boot 'file cd' wrapper.
 */
int
file_fat_cd(const char *path)
{
	if (ISDIRDELIM(*path)) {
		while (ISDIRDELIM(*path))
			path++;
		strncpy(file_cwd + 1, path, CWD_LEN - 1);
	} else {
		const char *origpath = path;
		char *tmpstr = file_cwd;
		int back = 0;

		while (*tmpstr != '\0')
			tmpstr++;

		do {
			tmpstr--;
		} while (ISDIRDELIM(*tmpstr));

		while (*path == '.') {
			path++;
			while (*path == '.') {
				path++;
				back++;
			}

			if (*path != '\0' && !ISDIRDELIM(*path)) {
				path = origpath;
				back = 0;
				break;
			}

			while (ISDIRDELIM(*path))
				path++;

			origpath = path;
		}

		while (back--) {
			/*
			 * Strip off path component 
			 */
			while (!ISDIRDELIM(*tmpstr)) {
				tmpstr--;
			}

			if (tmpstr == file_cwd) {
				/*
				 * Incremented again right after the loop. 
				 */
				tmpstr--;
				break;
			}

			/*
			 * Skip delimiters 
			 */
			while (ISDIRDELIM(*tmpstr))
				tmpstr--;
		}

		tmpstr++;
		if (*path == '\0') {
			if (tmpstr == file_cwd) {
				*tmpstr = '/';
				tmpstr++;
			}

			*tmpstr = '\0';
			return 0;
		}

		*tmpstr = '/';
		pathcpy(tmpstr + 1, path);
	}

	printf("New CWD is '%s'\n", file_cwd);
	return 0;
}

/*
 * U-Boot 'file mv' wrapper.
 * Return 1 on success, -1 on error and set 'errno'.
 */
int
file_fat_mv(const char *oldname, const char *newpath)
{
	int rc;

	fat_dprintf("Move \'%s\" to \"%s\"\n", oldname, newpath);
	rc = rename(oldname, newpath);
	if (rc < 0) {
#ifdef CONFIG_STRERROR
		fprintf(stderr, "Failed to move \"%s\" to \"%s\" : %s\n",
			oldname, newpath, strerror(errno));
#endif
		return -1;
	}

	return 1;
}


extern int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector);
int rockbox_fat_mount(long startsector);
/* @brief Map rockbox_fat_mount() (fs/fat/fat.c) on fat_mount() (fs/fat/rockbox_fat.c)
 */
int rockbox_fat_mount(long startsector)
{
#ifdef HAVE_MULTIVOLUME
#error "MULTIVOLUME from rockbox package not ported to U-boot"
#else
    return fat_mount(IF_MV2(volume,) IF_MV2(drive,) startsector);
#endif
}

/* @brief Get filesystem size and free left space
 * @return partition size in kByte 
 */
unsigned int rockbox_fat_size(void)
{
#ifdef HAVE_MULTIVOLUME
#error "MULTIVOLUME from rockbox package not ported to U-boot"
#else
	unsigned long tsize;
    fat_size(IF_MV2(int volume,) &tsize, NULL);
	return (unsigned int) tsize;
#endif
}

/* @brief Return available free space.
 * @param[in]  if size==0, compute free space, else check if enougth free space
 * @return     if size==0, ret free space, else return true if enougth free space
 * @internals
 *     Input and returned size values are formated in kByte.
 */
unsigned int rockbox_fat_free(unsigned long size)
{
#ifdef HAVE_MULTIVOLUME
#error "MULTIVOLUME from rockbox package not ported to U-boot"
#else
	if (size) { 
		return fat_check_free(IF_MV2(int volume,) size);
	} else {
		unsigned long tfree;
		fat_recalc_free(IF_MV2(int volume,));
	    fat_size(IF_MV2(int volume,) NULL, &tfree);
		return (unsigned int) tfree;
	}
#endif
}