From bbf7d44cf935538f5a353665f160e5cd3ed59da8 Mon Sep 17 00:00:00 2001
From: hackbard <hackbard@staubsauger.localdomain>
Date: Sun, 9 Sep 2007 21:54:45 +0200
Subject: [PATCH] playing around with pffs (NOT FINISHED, DOESNT COMPILE!)

---
 betty/Makefile |  2 +-
 betty/pffs.c   | 89 ++++++++++++++++++++++++++++++++++++++++++++++----
 betty/pffs.h   | 42 +++++++++++++++++++++---
 3 files changed, 120 insertions(+), 13 deletions(-)

diff --git a/betty/Makefile b/betty/Makefile
index b7a2616..0a35586 100644
--- a/betty/Makefile
+++ b/betty/Makefile
@@ -19,7 +19,7 @@ HOST_TARGET = lpcload fwdump
 CROSS_TARGET = fwbc.hex fwflash.hex betty.hex
 
 # betty deps
-BETTY_DEPS = system.o uart.o buttons.o spi.o display.o flash.o pffs.o
+BETTY_DEPS = system.o uart.o buttons.o spi.o display.o flash.o functions.o #pffs.o
 
 # all projects
 all: $(HOST_TARGET) $(CROSS_TARGET)
diff --git a/betty/pffs.c b/betty/pffs.c
index 7b3b5e8..1257d18 100644
--- a/betty/pffs.c
+++ b/betty/pffs.c
@@ -36,7 +36,8 @@ u32 pffs_get_ptr(t_pffs *pffs,u8 sector) {
 	pffs->data_ptr=daddr;
 
 	if(found) {
-		pffs->index_ptr=pffs->sec_addr[sector]+iaddr;
+		pffs->index_ptr[0]=pffs->sec_addr[sector];
+		pffs->index_ptr[1]=pffs->sec_addr[sector]+iaddr;
 		return 0;
 	}
 	else
@@ -62,7 +63,7 @@ int pffs_sec_empty(t_pffs *pffs,u8 sec) {
 
 #define pffs_sec_erase(pffs,sec) pffs->fe(pffs->base_addr|pffs->sec_addr[sec])
 
-int pffs_reorientate(t_pffs *pffs) {
+int pffs_orientate(t_pffs *pffs) {
 
 	u8 sec,sec0,sec1;
 
@@ -88,7 +89,7 @@ int pffs_reorientate(t_pffs *pffs) {
 	return 0;
 }
 
-int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
+int pffs_flash_register(t_pffs *pffs,u32 base_addr,u32 *sec_addr,
                         u8 sec_num_data_min,u8 sec_num_data_max,
 			u8 sec_num_index0,u8 sec_num_index1,
                         int (*fw)(u32 addr,u16 *buf,int len),
@@ -97,7 +98,6 @@ int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
 
 	/* assign physical flash specs */
 	pffs->base_addr=base_addr;
-	pffs->sec_num=sec_num;
 	pffs->sec_addr=sec_addr;
 
 	/* specified index and data sectors */
@@ -111,9 +111,6 @@ int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
 	pffs->fr=fr;
 	pffs->fe=fe;
 
-	/* orientate */
-	pffs_reorientate(pffs);
-
 	pffs->state|=PFFS_REGISTERED;
 
 	return 0;
@@ -121,9 +118,87 @@ int pffs_flash_register(t_pffs *pffs,u32 base_addr,u8 sec_num,u32 *sec_addr,
 
 int pffs_init(t_pffs *pffs) {
 
+	/* check whether a flash is registered */
 	if(!(pffs->state&PFFS_REGISTERED))
 		return -1;
 
+	/* orientate */
+	pffs_orientate(pffs);
+
+	return 0;
+}
+
+#define pffs_check_magic(data) (((data)&PFFS_INDEX_MAGIC_MASK)==PFFS_INDEX_MAGIC)
+#define pffs_fnlen(data) (((data)&PFFS_FNLEN_MASK)>>4)
+#define pffs_daddr_msb(data) (((data)&))
+
+int pffs_find_file(t_pffs *pffs,char *file,u32 *iaddr,u32 *daddr,u16 *len) {
+
+	u8 fnl;
+	u16 data[PFFS_MAX_FILENAME_SIZE+PFFS_HEADER_SIZE];
+
+	pffs->index_ptr[2]=pffs->base_addr|pffs->index_ptr[0];
+
+	while(pffs->index_ptr[2]<pffs->index_ptr[1])
+		pffs->fr(iaddr,data,3);
+
+		if(!pffs_check_magic(data[0]))
+			break;
+
+		fnl=pffs_fnlen(data[0]);
+		pffs->fr(iaddr+6,data+3,fnl+fnl);
+
+		if(!strncmp(fd->file,(char *)(data+3),fnl+fnl)) {
+			*daddr=((data[0]&0x000f)<<16)|data[1];
+			*len=data[2];
+			pffs->index_ptr[2]=*iaddr;
+			return PFFS_FILE_EXISTS;
+		}
+		pffs->fr(iaddr,data,3);
+	}
+	
+	return PFFS_FILE_NOT_FOUND;
+}
+
+int pffs_open_read(t_pffs *pffs,t_pffs_fd *fd) {
+
+	u32 iaddr;
+	u32 daddr;
+	u16 data[PFFS_MAX_FILENAME+PFFS_HEADER_SIZE];
+
+	iaddr=pffs->base_addr|pffs->index_ptr[0];
+
+	return 0x23;
+}
+
+int pffs_open(t_pffs *pffs,t_pffs_fd *fd,char *file,u8 mode) {
+
+	/* the pffs struct */
+	fd->pffs=pffs;
+
+	/* filename */
+	fd->fn_size=strlen(file);
+	if(fd->fn_size>30)
+		return PFFS_FILENAME_TOO_LONG;
+	strncpy(fd->file,file,fd->fn_size);
+	if(fd->fn_size&1)
+		fd->file[fd->fn_size++]='\0';
+
+	/* mode */
+	fd->mode=mode;
+
+	/* action */
+	switch(mode) {
+		case PFFS_READ:
+			pffs_open_read(pffs,fd);
+			break;
+		case PFFS_WRITE:
+			break;
+		case PFFS_RDWR:
+		default:
+			return PFFS_MODE_UNSUPPORTED;
+	}
+
 	return 0;
 }
 
diff --git a/betty/pffs.h b/betty/pffs.h
index 72ec880..faf0d38 100644
--- a/betty/pffs.h
+++ b/betty/pffs.h
@@ -10,19 +10,37 @@
 
 #include "lpc2xxx.h"
 #include "types.h"
+#include "functions.h"
 
 /* defines */
+#define PFFS_MAX_FILENAME_SIZE	15		// in words
+#define PFFS_HEADER_SIZE	3		// in words
 
 #define PFFS_REGISTERED		(1<<0)
 
 #define PFFS_INDEX_MAGIC	0x7000
 
+#define PFFS_INDEX_MAGIC_MASK	0xf000
+#define PFFS_RESERVED_MASK	0x0f00
+#define PFFS_FNLEN_MASK		0x00f0
+#define PFFS_LEN_MSB_MASK	0x000f
+
+/* file modes */
+#define PFFS_READ		0x01
+#define PFFS_WRITE		0x02
+#define PFFS_RDWR		0x03
+
+/* pffs write / read return codes */
+#define PFFS_FILE_EXISTS	0x01
+#define PFFS_FILE_NOT_FOUND	0x02
+#define PFFS_NO_SPACE_LEFT	0x04
+#define PFFS_FILENAME_TOO_LONG	0x08
+
 /* type definitions */
 
 typedef struct s_pffs {
 	/* flash specs */
 	u32 base_addr;
-	u8 sec_num;
 	u32 *sec_addr;
 	/* flash write, read and sector erase function pointers */
 	int (*fw)(u32 addr,u16 *buf,int len);
@@ -30,12 +48,26 @@ typedef struct s_pffs {
 	int (*fe)(u32 addr);
 	/* pffs internal variables */
 	u8 state;
-	u32 data_ptr;
-	u32 index_ptr;
-	u8 sec_num_data[2];
-	u8 sec_num_index[2];
+	u32 data_ptr;			// pointer where new data goes
+	u32 index_ptr[3];		// 0: start, 1: new, 2: current
+	u8 sec_num_data[2];		// data start/end sectors
+	u8 sec_num_index[2];		// 2 index sectors
 } t_pffs;
 
+typedef struct s_pffs_fd {
+	u32 daddr;
+	u32 iaddr;
+	char file[PFFS_MAX_FILENAME_SIZE+PFFS_MAX_FILENAME_SIZE];
+	u8 fn_size;
+	u8 mode;
+	t_pffs *pffs;
+} t_pffs_fd;
+
 /* function prototypes */
+int pffs_open(t_pffs *pffs,char *file,u8 mode);
+int pffs_write(t_pffs *pffs,int fd,u8 *buf,int len);
+int pffs_read(t_pffs *pffs,int fd,u8 *buf,int len);
+int pffs_close(t_pffs *pffs,int fd);
+int pffs_unlink(t_pffs *pffs,char *file);
 
 #endif
-- 
2.39.5