Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 2 commits
  • 6 files changed
  • 0 comments
  • 1 contributor
2  Makefile
@@ -4,7 +4,7 @@ include $(MISPDIR)/common.mak
4 4
 OBJECTS=crt0.o isr.o luainit.o main.o
5 5
 OURLIBS=m mm yaffs2 glue lua
6 6
 
7  
-CFLAGS+=-I$(MISPDIR)/libm/include -I$(MISPDIR)/libmm/include -I$(LUADIR)/src
  7
+CFLAGS+=-I$(MISPDIR)/libm/include -I$(MISPDIR)/libmm/include -I$(MISPDIR)/libglue/include -I$(LUADIR)/src
8 8
 
9 9
 all: misp.bin
10 10
 
2  libglue/Makefile
... ...
@@ -1,7 +1,7 @@
1 1
 MISPDIR=..
2 2
 include $(MISPDIR)/common.mak
3 3
 
4  
-CFLAGS+=-I$(YAFFSDIR)/direct -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_DEFINES_TYPES -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES -I$(MISPDIR)/libglue/include
  4
+CFLAGS+=-I$(YAFFSDIR)/direct -I$(YAFFSDIR) -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_DEFINES_TYPES -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES -I$(MISPDIR)/libglue/include
5 5
 OBJECTS=yaffs.o asprintf.o file.o getenv.o
6 6
 
7 7
 all: libglue.a
21  libglue/file.c
@@ -187,7 +187,11 @@ FILE *fopen(const char *path, const char *mode)
187 187
 
188 188
 int fclose(FILE *fd)
189 189
 {
190  
-	return yaffs_close(*(int *)fd);
  190
+	int r;
  191
+	
  192
+	r = yaffs_close(*(int *)fd);
  193
+	free(fd);
  194
+	return r;
191 195
 }
192 196
 
193 197
 int fprintf(FILE *stream, const char *format, ...)
@@ -306,5 +310,18 @@ int feof(FILE *stream)
306 310
 
307 311
 FILE *freopen(const char *path, const char *mode, FILE *stream)
308 312
 {
309  
-	return NULL;
  313
+	FILE *newfd;
  314
+	
  315
+	if(is_std_stream(stream))
  316
+		return NULL; /* unsupported */
  317
+	
  318
+	yaffs_close(*(int *)stream);
  319
+	newfd = fopen(path, mode);
  320
+	if(newfd == NULL) {
  321
+		free(stream);
  322
+		return NULL;
  323
+	}
  324
+	*(int *)stream = *(int *)newfd;
  325
+	free(newfd);
  326
+	return stream;
310 327
 }
6  libglue/include/glue.h
... ...
@@ -0,0 +1,6 @@
  1
+#ifndef __GLUE_H
  2
+#define __GLUE_H
  3
+
  4
+void fs_init(void);
  5
+
  6
+#endif /* __GLUE_H */
141  libglue/yaffs.c
@@ -7,6 +7,10 @@
7 7
 #include <hw/mem.h>
8 8
 
9 9
 #include <yaffsfs.h>
  10
+#include <yaffs_guts.h>
  11
+#include <yaffs_packedtags2.h>
  12
+
  13
+#include <glue.h>
10 14
 
11 15
 /*
12 16
  * YAFFS callbacks
@@ -56,4 +60,139 @@ void yaffs_bug_fn(const char *file_name, int line_no)
56 60
 
57 61
 /*
58 62
  * YAFFS init and flash access
59  
- */
  63
+ */
  64
+
  65
+#define NOR_SIZE		(32*1024*1024)
  66
+#define NOR_BLOCKSIZE		(128*1024)
  67
+
  68
+#define NOR_CHUNK_DATA_SIZE	512
  69
+#define NOR_CHUNK_TAGS_SIZE	16
  70
+#define NOR_CHUNK_WHOLE_SIZE	(NOR_CHUNK_DATA_SIZE+NOR_CHUNK_TAGS_SIZE)
  71
+
  72
+static void read_flash(void *data, int len, int offset)
  73
+{
  74
+	memcpy(data, (char *)(0x80000000 | FLASH_OFFSET_FILESYSTEM) + offset, len);
  75
+}
  76
+
  77
+static void write_flash(const void *data, int len, int offset)
  78
+{
  79
+	/* TODO */
  80
+}
  81
+
  82
+static unsigned int chunk_address(struct yaffs_dev *dev, int c)
  83
+{
  84
+	unsigned int chunks_per_block = dev->param.chunks_per_block;
  85
+	return NOR_BLOCKSIZE*(c/chunks_per_block)
  86
+		+ NOR_CHUNK_WHOLE_SIZE*(c%chunks_per_block);
  87
+}
  88
+
  89
+static int read_chunk_tags(struct yaffs_dev *dev, int nand_chunk, u8 *data, struct yaffs_ext_tags *tags)
  90
+{
  91
+	unsigned int address;
  92
+	
  93
+	//printf("%s %d (data=%p tags=%p)\n", __func__, nand_chunk, data, tags);
  94
+	address = chunk_address(dev, nand_chunk);
  95
+	if(data)
  96
+		read_flash(data, NOR_CHUNK_DATA_SIZE, address);
  97
+	if(tags) {
  98
+		struct yaffs_packed_tags2_tags_only x;
  99
+		read_flash(&x, NOR_CHUNK_TAGS_SIZE, address+NOR_CHUNK_DATA_SIZE);
  100
+		yaffs_unpack_tags2_tags_only(tags, &x);
  101
+	}
  102
+	return YAFFS_OK;
  103
+}
  104
+
  105
+static int write_chunk_tags(struct yaffs_dev *dev, int nand_chunk, const u8 *data, const struct yaffs_ext_tags *tags)
  106
+{
  107
+	unsigned int address;
  108
+	
  109
+	//printf("%s %d (data=%p tags=%p)\n", __func__, nand_chunk, data, tags);
  110
+	address = chunk_address(dev, nand_chunk);
  111
+	if(data)
  112
+		write_flash(data, NOR_CHUNK_DATA_SIZE, address);
  113
+	if(tags) {
  114
+		struct yaffs_packed_tags2_tags_only x;
  115
+		yaffs_pack_tags2_tags_only(&x, tags);
  116
+		write_flash(&x, NOR_CHUNK_TAGS_SIZE, address+NOR_CHUNK_DATA_SIZE);
  117
+	}
  118
+	return YAFFS_OK;
  119
+}
  120
+
  121
+static int bad_block(struct yaffs_dev *dev, int blockId)
  122
+{
  123
+	struct yaffs_ext_tags tags;
  124
+	int chunk_nr;
  125
+
  126
+	chunk_nr = blockId * dev->param.chunks_per_block;
  127
+
  128
+	read_chunk_tags(dev, chunk_nr, NULL, &tags);
  129
+	tags.block_bad = 1;
  130
+	write_chunk_tags(dev, chunk_nr, NULL, &tags);
  131
+	
  132
+	return YAFFS_OK;
  133
+}
  134
+
  135
+static int query_block(struct yaffs_dev *dev, int blockId, enum yaffs_block_state *state, u32 *seq_number)
  136
+{
  137
+	struct yaffs_ext_tags tags;
  138
+	int chunk_nr;
  139
+
  140
+	*seq_number = 0;
  141
+
  142
+	chunk_nr = blockId * dev->param.chunks_per_block;
  143
+
  144
+	read_chunk_tags(dev, chunk_nr, NULL, &tags);
  145
+	if(tags.block_bad)
  146
+		*state = YAFFS_BLOCK_STATE_DEAD;
  147
+	else if(!tags.chunk_used)
  148
+		*state = YAFFS_BLOCK_STATE_EMPTY;
  149
+	else if(tags.chunk_used) {
  150
+		*state = YAFFS_BLOCK_STATE_NEEDS_SCAN;
  151
+		*seq_number = tags.seq_number;
  152
+	}
  153
+	
  154
+	return YAFFS_OK;
  155
+}
  156
+
  157
+static int erase(struct yaffs_dev *dev, int blockId)
  158
+{
  159
+	// TODO my_ioctl(sc->flashdev, FLASH_ERASE_BLOCK, (void *)(blockId*sc->blocksize));
  160
+	
  161
+	return YAFFS_OK;
  162
+}
  163
+
  164
+static int initialise(struct yaffs_dev *dev)
  165
+{
  166
+	return YAFFS_OK;
  167
+}
  168
+
  169
+static struct yaffs_dev flash_dev = {
  170
+	.read_only = 0,
  171
+	.param = {
  172
+		.name = "/",
  173
+	
  174
+		.start_block = 0,
  175
+		.end_block = NOR_SIZE/NOR_BLOCKSIZE - 1,
  176
+		.chunks_per_block = NOR_BLOCKSIZE/NOR_CHUNK_WHOLE_SIZE,
  177
+		.total_bytes_per_chunk = NOR_CHUNK_WHOLE_SIZE,
  178
+		.n_reserved_blocks = 5,
  179
+		.n_caches = 15,
  180
+		.inband_tags = 1,
  181
+		.is_yaffs2 = 1,
  182
+		.no_tags_ecc = 1,
  183
+	
  184
+		.write_chunk_tags_fn = write_chunk_tags,
  185
+		.read_chunk_tags_fn = read_chunk_tags,
  186
+		.bad_block_fn = bad_block,
  187
+		.query_block_fn = query_block,
  188
+		.erase_fn = erase,
  189
+		.initialise_flash_fn = initialise
  190
+	}
  191
+};
  192
+
  193
+
  194
+void fs_init(void)
  195
+{
  196
+	yaffs_add_device(&flash_dev);
  197
+	yaffs_mount("/");
  198
+}
3  main.c
@@ -5,6 +5,8 @@
5 5
 #include <irq.h>
6 6
 #include <uart.h>
7 7
 
  8
+#include <glue.h>
  9
+
8 10
 #include <lua.h>
9 11
 #include <lauxlib.h>
10 12
 #include <lualib.h>
@@ -27,6 +29,7 @@ int main()
27 29
 	irq_setie(1);
28 30
 	uart_init();
29 31
 	mm_initialize(&_heapstart, 64*1024*1024);
  32
+	fs_init();
30 33
 	
31 34
 	printf("Hello World\n");
32 35
 	test_lua();

No commit comments for this range

Something went wrong with that request. Please try again.