From 20503b61a7f73fad8ee97219f7a1e74de3a8a2ac Mon Sep 17 00:00:00 2001 From: Michael Brandt Date: Tue, 20 Jul 2010 15:08:22 +0200 Subject: Read/write VFAT support from Rockbox-3.3 FAT stack Add read/write VFAT support from Rockbox-3.3 FAT stack. It should be also applicable to the unmodified 2009.11 U-Boot release. Note that is was taken as is from Rockbox and from a older U-Boot Rockbox patch. "checkpatch" shows very many coding style errors and warnings, but it is tedious work to clean this up. To make this patch work an additional mmc_block_write() board support routine and the errno variable are needed. Furthermore following defines in the board config header file: #define CONFIG_ROCKBOX_FAT 1 #define CONFIG_U_BOOT 1 #define CONFIG_SUPPORT_VFAT 1 #define CONFIG_CMD_TREE_FAT This will be added in a follow-up patch. This patch is based on the patch from Etienne Carriere for the U671x U-Boot: This commit adds FAT write support to u-boot native read-only FAT code. Commit initially applied on u-boot-v2009.01 (SHA1: 72d15e705bc3983884105cb7755c7ba80e74a0a5) Based on FAT stack dumped from Rockbox package v3.1 (www.rockbox.org). Based on initial Rockbox FAT stack integration in u-boot by Keith Outwater (outwater@comcast.net). Current porting is aligned with Rockbox v3.3 FAT stack. Enable upon config switches: CONFIG_CMD_FAT CONFIG_ROCKBOX_FAT CONFIG_CMD_TREE_FAT (recommended) CONFIG_SUPPORT_VFAT (recommended) C code APIs (from U-boot native FAT support): int fat_register_device(block_dev_desc_t *dev_desc, int part_no); long file_fat_read(const char *path, void *buf, unsigned long maxsize); int file_fat_ls(const char *dirname); int file_fat_detectfs(void); C code APIs (added by Rockbox FAT support): long file_fat_write(const char *path, void *buf, unsigned long maxsize); int file_fat_rm(const char *path); int file_fat_rmdir(const char *path); int file_fat_mkdir(const char *path); int file_fat_cd(const char *path); int file_fat_pwd(void); int file_fat_mv(const char *oldpath, const char *newpath); unsigned int rockbox_fat_free(unsigned long size_kbyte); unsigned int rockbox_fat_size(void); Use "help fat" from u-boot console to see available commands. ST-Ericsson ID: WP264488 Change-Id: I9afc29ecb80f9152bd8534bbf11e47e54cfad796 Signed-off-by: Michael Brandt Reviewed-on: http://gerrit.lud.stericsson.com/gerrit/3009 --- include/rockbox_fat.h | 157 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 include/rockbox_fat.h (limited to 'include/rockbox_fat.h') diff --git a/include/rockbox_fat.h b/include/rockbox_fat.h new file mode 100644 index 000000000..1b0da44b4 --- /dev/null +++ b/include/rockbox_fat.h @@ -0,0 +1,157 @@ +/*************************************************************************** + * Copyright (C) 2002 by Linus Nielsen Feltzing + * + * 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. + * + * 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 + * + * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY + * KIND, either express or implied. + * + **************************************************************************** + * Source file dumped from rockbox-3.1 distribution. + * $Id: fat.h 18960 2008-11-01 16:14:28Z gevaerts $ + * Copyright (C) 2002 by Linus Nielsen Feltzing + **************************************************************************** + * See file CREDITS for list of people who contributed to the U-boot + * project. + * + * 01/17/2006 Keith Outwater (outwater4@comcast.net) - port to U-Boot using + * CVS version 1.13 of 'firmware/export/fat.h' from rockbox CVS server. + * 09-jan-2008 etienne.carriere@stnwireless.com - update from rockbox-3.1 + **************************************************************************** + */ +#ifndef ROCKBOX_FAT_H +#define ROCKBOX_FAT_H + +#include +#include "rockbox_mv.h" /* for volume definitions */ + +/* Check that sector size is 512 bytes */ +#ifndef SECTOR_SIZE +#define SECTOR_SIZE 512 +#elif (SECTOR_SIZE != 512) +#error "SECTOR_SIZE expected to be 512" +#endif +/* + * The max length of the current working dorectory. + */ +#define ROCKBOX_CWD_LEN 512 + +/* Number of bytes reserved for a file name (including the trailing \0). + Since names are stored in the entry as UTF-8, we won't be able to + store all names allowed by FAT. In FAT, a name can have max 255 + characters (not bytes!). Since the UTF-8 encoding of a char may take + up to 4 bytes, there will be names that we won't be able to store + completely. For such names, the short DOS name is used. */ +#define FAT_FILENAME_BYTES 256 + +struct fat_direntry +{ + unsigned char name[FAT_FILENAME_BYTES]; /* UTF-8 encoded name plus \0 */ + unsigned short attr; /* Attributes */ + unsigned char crttimetenth; /* Millisecond creation + time stamp (0-199) */ + unsigned short crttime; /* Creation time */ + unsigned short crtdate; /* Creation date */ + unsigned short lstaccdate; /* Last access date */ + unsigned short wrttime; /* Last write time */ + unsigned short wrtdate; /* Last write date */ + unsigned long filesize; /* File size in bytes */ + long firstcluster; /* fstclusterhi<<16 + fstcluslo */ +}; + +#define FAT_ATTR_READ_ONLY 0x01 +#define FAT_ATTR_HIDDEN 0x02 +#define FAT_ATTR_SYSTEM 0x04 +#define FAT_ATTR_VOLUME_ID 0x08 +#define FAT_ATTR_DIRECTORY 0x10 +#define FAT_ATTR_ARCHIVE 0x20 +#define FAT_ATTR_VOLUME 0x40 /* this is a volume, not a real directory */ + +struct fat_file +{ + long firstcluster; /* first cluster in file */ + long lastcluster; /* cluster of last access */ + long lastsector; /* sector of last access */ + long clusternum; /* current clusternum */ + long sectornum; /* sector number in this cluster */ + unsigned int direntry; /* short dir entry index from start of dir */ + unsigned int direntries; /* number of dir entries used by this file */ + long dircluster; /* first cluster of dir */ + bool eof; +#ifdef HAVE_MULTIVOLUME + int volume; /* file resides on which volume */ +#endif +}; + +struct fat_dir +{ + unsigned int entry; + unsigned int entrycount; + long sector; + struct fat_file file; + unsigned char sectorcache[3][SECTOR_SIZE]; +}; + +#ifdef HAVE_HOTSWAP +extern void fat_lock(void); +extern void fat_unlock(void); +#endif + +extern void fat_init(void); +extern int fat_mount(IF_MV2(int volume,) IF_MV2(int drive,) long startsector); +extern int fat_unmount(int volume, bool flush); +extern void fat_size(IF_MV2(int volume,) /* public for info */ + unsigned long* size, + unsigned long* free); +extern int fat_check_free(IF_MV2(int volume,) unsigned long size); +extern void fat_recalc_free(IF_MV_NONVOID(int volume)); /* public for debug info screen */ +extern int fat_create_dir(const char *name, + struct fat_dir* newdir, + struct fat_dir* dir); +extern int fat_open(IF_MV2(int volume,) + long cluster, + struct fat_file* ent, + const struct fat_dir* dir); +extern int fat_create_file(const char *name, + struct fat_file* ent, + struct fat_dir* dir); +extern long fat_readwrite(struct fat_file *ent, long sectorcount, + void *buf, bool write); +extern int fat_closewrite(struct fat_file *ent, long size, int attr); +extern int fat_seek(struct fat_file *ent, unsigned long sector); +extern int fat_remove(struct fat_file *ent); +extern int fat_truncate(const struct fat_file *ent); +extern int fat_rename(struct fat_file *file, + struct fat_dir *dir, + const unsigned char* newname, + long size, int attr); + +extern int fat_opendir(IF_MV2(int volume,) + struct fat_dir *ent, unsigned long currdir, + const struct fat_dir *parent_dir); +extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry); +extern unsigned int fat_get_cluster_size(IF_MV_NONVOID(int volume)); /* public for debug info screen */ +extern bool fat_ismounted(int volume); + +#ifndef _ROCKBOX_FILE_H_ +#include +#endif + +#ifndef _ROCKBOX_DIR_H +#include +#endif + +/* Added to rockbox_wrapper.c */ +unsigned int rockbox_fat_size(void); +unsigned int rockbox_fat_free(unsigned long size); + + +#endif /* #ifndef ROCKBOX_FAT_H */ -- cgit v1.2.3