Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
  • 3 commits
  • 14 files changed
  • 0 comments
  • 1 contributor
5  software/bios/Makefile
... ...
@@ -1,5 +1,5 @@
1 1
 M2DIR=../..
2  
-include $(M2DIR)/software/include.mak
  2
+include $(M2DIR)/software/common.mak
3 3
 
4 4
 OBJECTS=crt0.o isr.o ddrinit.o main.o microudp.o tftp.o boot-helper.o boot.o
5 5
 
@@ -18,12 +18,11 @@ bios.elf: linker.ld $(OBJECTS) libs
18 18
 bios-rescue.elf: linker-rescue.ld $(OBJECTS) libs
19 19
 
20 20
 %.elf:
21  
-	$(LD) $(LDFLAGS) -T $< -N -o $@ $(OBJECTS) -L$(M2DIR)/software/libbase -L$(M2DIR)/software/libextra -lbase -lextra
  21
+	$(LD) $(LDFLAGS) -T $< -N -o $@ $(OBJECTS) -L$(M2DIR)/software/libbase -lbase
22 22
 	chmod -x $@
23 23
 
24 24
 libs:
25 25
 	make -C $(M2DIR)/software/libbase
26  
-	make -C $(M2DIR)/software/libextra
27 26
 
28 27
 flash: bios.bin
29 28
 	m1nor bios.bin
2  software/bios/boot.c
@@ -20,7 +20,7 @@
20 20
 #include <uart.h>
21 21
 #include <system.h>
22 22
 #include <board.h>
23  
-#include <extra/crc.h>
  23
+#include <crc.h>
24 24
 #include <sfl.h>
25 25
 #include <string.h>
26 26
 #include <irq.h>
2  software/bios/main.c
@@ -23,7 +23,7 @@
23 23
 #include <board.h>
24 24
 #include <irq.h>
25 25
 #include <version.h>
26  
-#include <extra/crc.h>
  26
+#include <crc.h>
27 27
 #include <timer.h>
28 28
 
29 29
 #include <hw/flash.h>
2  software/bios/microudp.c
@@ -17,7 +17,7 @@
17 17
 
18 18
 #include <stdio.h>
19 19
 #include <system.h>
20  
-#include <extra/crc.h>
  20
+#include <crc.h>
21 21
 #include <hw/minimac.h>
22 22
 
23 23
 #include "microudp.h"
0  software/include.mak → software/common.mak
File renamed without changes
0  software/include/extra/crc.h → software/include/base/crc.h
File renamed without changes
31  software/include/extra/blockdev.h
... ...
@@ -1,31 +0,0 @@
1  
-/*
2  
- * Milkymist SoC (Software)
3  
- * Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
4  
- *
5  
- * This program is free software: you can redistribute it and/or modify
6  
- * it under the terms of the GNU General Public License as published by
7  
- * the Free Software Foundation, version 3 of the License.
8  
- *
9  
- * This program is distributed in the hope that it will be useful,
10  
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  
- * GNU General Public License for more details.
13  
- *
14  
- * You should have received a copy of the GNU General Public License
15  
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  
- */
17  
-
18  
-#ifndef __BLOCKDEV_H
19  
-#define __BLOCKDEV_H
20  
-
21  
-enum {
22  
-	BLOCKDEV_MEMORY_CARD
23  
-};
24  
-
25  
-int bd_init(int devnr);
26  
-int bd_readblock(unsigned int block, void *buffer);
27  
-void bd_done(void);
28  
-
29  
-int bd_has_part_table(int devnr);
30  
-
31  
-#endif /* __BLOCKDEV_H */
28  software/include/extra/fatfs.h
... ...
@@ -1,28 +0,0 @@
1  
-/*
2  
- * Milkymist SoC (Software)
3  
- * Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
4  
- *
5  
- * This program is free software: you can redistribute it and/or modify
6  
- * it under the terms of the GNU General Public License as published by
7  
- * the Free Software Foundation, version 3 of the License.
8  
- *
9  
- * This program is distributed in the hope that it will be useful,
10  
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  
- * GNU General Public License for more details.
13  
- *
14  
- * You should have received a copy of the GNU General Public License
15  
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  
- */
17  
-
18  
-#ifndef __FATFS_H
19  
-#define __FATFS_H
20  
-
21  
-typedef int (*fatfs_dir_callback)(const char *, const char *, void *);
22  
-
23  
-int fatfs_init(int devnr);
24  
-int fatfs_list_files(fatfs_dir_callback cb, void *param);
25  
-int fatfs_load(const char *filename, char *buffer, int size, int *realsize);
26  
-void fatfs_done(void);
27  
-
28  
-#endif /* __FATFS_H */
4  software/libbase/Makefile
... ...
@@ -1,7 +1,7 @@
1 1
 M2DIR=../..
2  
-include $(M2DIR)/software/include.mak
  2
+include $(M2DIR)/software/common.mak
3 3
 
4  
-OBJECTS=divsi3.o libc.o console.o timer.o system.o board.o uart.o softfloat.o softfloat-glue.o vsnprintf.o atof.o
  4
+OBJECTS=divsi3.o libc.o crc16.o crc32.o console.o timer.o system.o board.o uart.o softfloat.o softfloat-glue.o vsnprintf.o atof.o
5 5
 
6 6
 all: libbase.a
7 7
 
2  software/libextra/crc16.c → software/libbase/crc16.c
... ...
@@ -1,4 +1,4 @@
1  
-#include <extra/crc.h>
  1
+#include <crc.h>
2 2
 
3 3
 static const unsigned int crc16_table[256] = {
4 4
 	0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
2  software/libextra/crc32.c → software/libbase/crc32.c
@@ -3,7 +3,7 @@
3 3
  * For conditions of distribution and use, see copyright notice in zlib.h
4 4
  */
5 5
 
6  
-#include <extra/crc.h>
  6
+#include <crc.h>
7 7
 
8 8
 static const unsigned int crc_table[256] = {
9 9
 	0x00000000L, 0x77073096L, 0xee0e612cL, 0x990951baL, 0x076dc419L,
18  software/libextra/Makefile
... ...
@@ -1,18 +0,0 @@
1  
-M2DIR=../..
2  
-include $(M2DIR)/software/include.mak
3  
-
4  
-OBJECTS=crc16.o crc32.o
5  
-
6  
-all: libextra.a
7  
-
8  
-# pull in dependency info for *existing* .o files
9  
--include $(OBJECTS:.o=.d)
10  
-
11  
-libextra.a: $(OBJECTS)
12  
-	$(AR) clr libextra.a $(OBJECTS)
13  
-	$(RANLIB) libextra.a
14  
-
15  
-.PHONY: clean
16  
-
17  
-clean:
18  
-	rm -f $(OBJECTS) $(OBJECTS:.o=.d) libextra.a .*~ *~
273  software/libextra/blockdev.c
... ...
@@ -1,273 +0,0 @@
1  
-/*
2  
- * Milkymist SoC (Software)
3  
- * Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
4  
- *
5  
- * This program is free software: you can redistribute it and/or modify
6  
- * it under the terms of the GNU General Public License as published by
7  
- * the Free Software Foundation, version 3 of the License.
8  
- *
9  
- * This program is distributed in the hope that it will be useful,
10  
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  
- * GNU General Public License for more details.
13  
- *
14  
- * You should have received a copy of the GNU General Public License
15  
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  
- */
17  
-
18  
-#include <hw/flash.h>
19  
-#include <hw/memcard.h>
20  
-#include <string.h>
21  
-
22  
-#include <blockdev.h>
23  
-
24  
-//#define MEMCARD_DEBUG
25  
-
26  
-static void memcard_start_cmd_tx(void)
27  
-{
28  
-	CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_TX;
29  
-}
30  
-
31  
-static void memcard_start_cmd_rx(void)
32  
-{
33  
-	CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX;
34  
-	CSR_MEMCARD_START = MEMCARD_START_CMD_RX;
35  
-	CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_RX;
36  
-}
37  
-
38  
-static void memcard_start_cmd_dat_rx(void)
39  
-{
40  
-	CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX|MEMCARD_PENDING_DAT_RX;
41  
-	CSR_MEMCARD_START = MEMCARD_START_CMD_RX|MEMCARD_START_DAT_RX;
42  
-	CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_CMD_RX|MEMCARD_ENABLE_DAT_RX;
43  
-}
44  
-
45  
-static void memcard_send_command(unsigned char cmd, unsigned int arg)
46  
-{
47  
-	unsigned char packet[6];
48  
-	int a;
49  
-	int i;
50  
-	unsigned char data;
51  
-	unsigned char crc;
52  
-
53  
-	packet[0] = cmd | 0x40;
54  
-	packet[1] = ((arg >> 24) & 0xff);
55  
-	packet[2] = ((arg >> 16) & 0xff);
56  
-	packet[3] = ((arg >> 8) & 0xff);
57  
-	packet[4] = (arg & 0xff);
58  
-
59  
-	crc = 0;
60  
-	for(a=0;a<5;a++) {
61  
-		data = packet[a];
62  
-		for(i=0;i<8;i++) {
63  
-			crc <<= 1;
64  
-			if((data & 0x80) ^ (crc & 0x80))
65  
-				crc ^= 0x09;
66  
-			data <<= 1;
67  
-		}
68  
-	}
69  
-	crc = (crc<<1) | 1;
70  
-
71  
-	packet[5] = crc;
72  
-
73  
-#ifdef MEMCARD_DEBUG
74  
-	printf(">> %02x %02x %02x %02x %02x %02x\n", packet[0], packet[1], packet[2], packet[3], packet[4], packet[5]);
75  
-#endif
76  
-
77  
-	for(i=0;i<6;i++) {
78  
-		CSR_MEMCARD_CMD = packet[i];
79  
-		while(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_TX);
80  
-	}
81  
-}
82  
-
83  
-static void memcard_send_dummy(void)
84  
-{
85  
-	CSR_MEMCARD_CMD = 0xff;
86  
-	while(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_TX);
87  
-}
88  
-
89  
-static int memcard_receive_command(unsigned char *buffer, int len)
90  
-{
91  
-	int i;
92  
-	int timeout;
93  
-
94  
-	for(i=0;i<len;i++) {
95  
-		timeout = 2000000;
96  
-		while(!(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_RX)) {
97  
-			timeout--;
98  
-			if(timeout == 0) {
99  
-				#ifdef MEMCARD_DEBUG
100  
-				printf("Command receive timeout\n");
101  
-				#endif
102  
-				return 0;
103  
-			}
104  
-		}
105  
-		buffer[i] = CSR_MEMCARD_CMD;
106  
-		CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX;
107  
-	}
108  
-
109  
-	while(!(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_RX));
110  
-
111  
-	#ifdef MEMCARD_DEBUG
112  
-	printf("<< ");
113  
-	for(i=0;i<len;i++)
114  
-		printf("%02x ", buffer[i]);
115  
-	printf("\n");
116  
-	#endif
117  
-
118  
-	return 1;
119  
-}
120  
-
121  
-static int memcard_receive_command_data(unsigned char *command, unsigned int *data)
122  
-{
123  
-	int i, j;
124  
-	int timeout;
125  
-
126  
-	i = 0;
127  
-	j = 0;
128  
-	while(j < 128) {
129  
-		timeout = 2000000;
130  
-		while(!(CSR_MEMCARD_PENDING & (MEMCARD_PENDING_CMD_RX|MEMCARD_PENDING_DAT_RX))) {
131  
-			timeout--;
132  
-			if(timeout == 0) {
133  
-				#ifdef MEMCARD_DEBUG
134  
-				printf("Command receive timeout\n");
135  
-				#endif
136  
-				return 0;
137  
-			}
138  
-		}
139  
-		if(CSR_MEMCARD_PENDING & MEMCARD_PENDING_CMD_RX) {
140  
-			command[i++] = CSR_MEMCARD_CMD;
141  
-			CSR_MEMCARD_PENDING = MEMCARD_PENDING_CMD_RX;
142  
-			if(i == 6)
143  
-				CSR_MEMCARD_ENABLE = MEMCARD_ENABLE_DAT_RX; /* disable command RX */
144  
-		}
145  
-		if(CSR_MEMCARD_PENDING & MEMCARD_PENDING_DAT_RX) {
146  
-			data[j++] = CSR_MEMCARD_DAT;
147  
-			CSR_MEMCARD_PENDING = MEMCARD_PENDING_DAT_RX;
148  
-		}
149  
-	}
150  
-
151  
-	/* Get CRC (ignored) */
152  
-	for(i=0;i<2;i++) {
153  
-		while(!(CSR_MEMCARD_PENDING & MEMCARD_PENDING_DAT_RX));
154  
-		#ifdef MEMCARD_DEBUG
155  
-		printf("CRC: %08x\n", CSR_MEMCARD_DAT);
156  
-		#endif
157  
-		CSR_MEMCARD_PENDING = MEMCARD_PENDING_DAT_RX;
158  
-	}
159  
-
160  
-	while(!(CSR_MEMCARD_PENDING & MEMCARD_PENDING_DAT_RX));
161  
-
162  
-	#ifdef MEMCARD_DEBUG
163  
-	printf("<< %02x %02x %02x %02x %02x %02x\n", command[0], command[1], command[2], command[3], command[4], command[5]);
164  
-	#endif
165  
-
166  
-	//for(i=0;i<128;i++)
167  
-	//	printf("%08x ", data[i]);
168  
-	//printf("\n");
169  
-
170  
-	return 1;
171  
-}
172  
-
173  
-static int memcard_init(void)
174  
-{
175  
-	unsigned char b[17];
176  
-	unsigned int rca;
177  
-
178  
-	CSR_MEMCARD_CLK2XDIV = 250;
179  
-
180  
-	/* CMD0 */
181  
-	memcard_start_cmd_tx();
182  
-	memcard_send_command(0, 0);
183  
-
184  
-	memcard_send_dummy();
185  
-
186  
-	/* CMD8 */
187  
-	memcard_send_command(8, 0x1aa);
188  
-	memcard_start_cmd_rx();
189  
-	if(!memcard_receive_command(b, 6)) return 0;
190  
-
191  
-	/* ACMD41 - initialize */
192  
-	while(1) {
193  
-		memcard_start_cmd_tx();
194  
-		memcard_send_command(55, 0);
195  
-		memcard_start_cmd_rx();
196  
-		if(!memcard_receive_command(b, 6)) return 0;
197  
-		memcard_start_cmd_tx();
198  
-		memcard_send_command(41, 0x00300000);
199  
-		memcard_start_cmd_rx();
200  
-		if(!memcard_receive_command(b, 6)) return 0;
201  
-		if(b[1] & 0x80) break;
202  
-		#ifdef MEMCARD_DEBUG
203  
-		printf("Card is busy, retrying\n");
204  
-		#endif
205  
-	}
206  
-
207  
-	/* CMD2 - get CID */
208  
-	memcard_start_cmd_tx();
209  
-	memcard_send_command(2, 0);
210  
-	memcard_start_cmd_rx();
211  
-	if(!memcard_receive_command(b, 17)) return 0;
212  
-
213  
-	/* CMD3 - get RCA */
214  
-	memcard_start_cmd_tx();
215  
-	memcard_send_command(3, 0);
216  
-	memcard_start_cmd_rx();
217  
-	if(!memcard_receive_command(b, 6)) return 0;
218  
-	rca = (((unsigned int)b[1]) << 8)|((unsigned int)b[2]);
219  
-	#ifdef MEMCARD_DEBUG
220  
-	printf("RCA: %04x\n", rca);
221  
-	#endif
222  
-
223  
-	/* CMD7 - select card */
224  
-	memcard_start_cmd_tx();
225  
-	memcard_send_command(7, rca << 16);
226  
-	memcard_start_cmd_rx();
227  
-	if(!memcard_receive_command(b, 6)) return 0;
228  
-
229  
-	/* ACMD6 - set bus width */
230  
-	memcard_start_cmd_tx();
231  
-	memcard_send_command(55, rca << 16);
232  
-	memcard_start_cmd_rx();
233  
-	if(!memcard_receive_command(b, 6)) return 0;
234  
-	memcard_start_cmd_tx();
235  
-	memcard_send_command(6, 2);
236  
-	memcard_start_cmd_rx();
237  
-	if(!memcard_receive_command(b, 6)) return 0;
238  
-
239  
-	CSR_MEMCARD_CLK2XDIV = 3;
240  
-
241  
-	return 1;
242  
-}
243  
-
244  
-static int memcard_readblock(unsigned int block, void *buffer)
245  
-{
246  
-	unsigned char b[6];
247  
-
248  
-	/* CMD17 - read block */
249  
-	memcard_start_cmd_tx();
250  
-	memcard_send_command(17, block*512);
251  
-	memcard_start_cmd_dat_rx();
252  
-	if(!memcard_receive_command_data(b, (unsigned int *)buffer)) return 0;
253  
-	return 1;
254  
-}
255  
-
256  
-int bd_init(int devnr)
257  
-{
258  
-	return memcard_init();
259  
-}
260  
-
261  
-int bd_readblock(unsigned int block, void *buffer)
262  
-{
263  
-	return memcard_readblock(block, buffer);
264  
-}
265  
-
266  
-void bd_done(void)
267  
-{
268  
-}
269  
-
270  
-int bd_has_part_table(int devnr)
271  
-{
272  
-	return 1;
273  
-}
440  software/libextra/fatfs.c
... ...
@@ -1,440 +0,0 @@
1  
-/*
2  
- * Milkymist SoC (Software)
3  
- * Copyright (C) 2007, 2008, 2009, 2010 Sebastien Bourdeauducq
4  
- *
5  
- * This program is free software: you can redistribute it and/or modify
6  
- * it under the terms of the GNU General Public License as published by
7  
- * the Free Software Foundation, version 3 of the License.
8  
- *
9  
- * This program is distributed in the hope that it will be useful,
10  
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  
- * GNU General Public License for more details.
13  
- *
14  
- * You should have received a copy of the GNU General Public License
15  
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  
- */
17  
-
18  
-#include <stdlib.h>
19  
-#include <stdio.h>
20  
-#include <string.h>
21  
-#include <ctype.h>
22  
-#include <endian.h>
23  
-#include <console.h>
24  
-#include <blockdev.h>
25  
-
26  
-#include <fatfs.h>
27  
-
28  
-//#define DEBUG
29  
-
30  
-#define BLOCK_SIZE 512
31  
-
32  
-struct partition_descriptor {
33  
-	unsigned char flags;
34  
-	unsigned char start_head;
35  
-	unsigned short start_cylinder;
36  
-	unsigned char type;
37  
-	unsigned char end_head;
38  
-	unsigned short end_cylinder;
39  
-	unsigned int start_sector;
40  
-	unsigned int end_sector;
41  
-} __attribute__((packed));
42  
-
43  
-struct firstsector {
44  
-	unsigned char bootsector[446];
45  
-	struct partition_descriptor partitions[4];
46  
-	unsigned char signature[2];
47  
-} __attribute__((packed));
48  
-
49  
-
50  
-struct fat16_firstsector {
51  
-	/* Common to FATxx */
52  
-	char jmp[3];
53  
-	char oem[8];
54  
-	unsigned short bytes_per_sector;
55  
-	unsigned char sectors_per_cluster;
56  
-	unsigned short reserved_sectors;
57  
-	unsigned char number_of_fat;
58  
-	unsigned short max_root_entries;
59  
-	unsigned short total_sectors_short;
60  
-	unsigned char media_descriptor;
61  
-	unsigned short sectors_per_fat;
62  
-	unsigned short sectors_per_track;
63  
-	unsigned short head_count;
64  
-	unsigned int hidden_sectors;
65  
-	unsigned int total_sectors;
66  
-	
67  
-	/* FAT16 specific */
68  
-	unsigned char drive_nr;
69  
-	unsigned char reserved;
70  
-	unsigned char ext_boot_signature;
71  
-	unsigned int id;
72  
-	unsigned char volume_label[11];
73  
-	unsigned char fstype[8];
74  
-	unsigned char bootcode[448];
75  
-	unsigned char signature[2];
76  
-} __attribute__((packed));
77  
-
78  
-struct directory_entry {
79  
-	unsigned char filename[8];
80  
-	unsigned char extension[3];
81  
-	unsigned char attributes;
82  
-	unsigned char reserved;
83  
-	unsigned char create_time_ms;
84  
-	unsigned short create_time;
85  
-	unsigned short create_date;
86  
-	unsigned short last_access;
87  
-	unsigned short ea_index;
88  
-	unsigned short lastm_time;
89  
-	unsigned short lastm_date;
90  
-	unsigned short first_cluster;
91  
-	unsigned int file_size;
92  
-} __attribute__((packed));
93  
-
94  
-struct directory_entry_lfn {
95  
-	unsigned char seq;
96  
-	unsigned short name1[5]; /* UTF16 */
97  
-	unsigned char attributes;
98  
-	unsigned char reserved;
99  
-	unsigned char checksum;
100  
-	unsigned short name2[6];
101  
-	unsigned short first_cluster;
102  
-	unsigned short name3[2];
103  
-} __attribute__((packed));
104  
-
105  
-#define PARTITION_TYPE_FAT16		0x06
106  
-#define PARTITION_TYPE_FAT32		0x0b
107  
-
108  
-static int fatfs_partition_start_sector;	/* Sector# of the beginning of the FAT16 partition */
109  
-
110  
-static int fatfs_sectors_per_cluster;
111  
-static int fatfs_fat_sector;			/* Sector of the first FAT */
112  
-static int fatfs_fat_entries;			/* Number of entries in the FAT */
113  
-static int fatfs_max_root_entries;
114  
-static int fatfs_root_table_sector;		/* Sector# of the beginning of the root table */
115  
-
116  
-static int fatfs_fat_cached_sector;
117  
-static unsigned short int fatfs_fat_sector_cache[BLOCK_SIZE/2];
118  
-
119  
-static int fatfs_dir_cached_sector;
120  
-static struct directory_entry fatfs_dir_sector_cache[BLOCK_SIZE/sizeof(struct directory_entry)];
121  
-
122  
-static int fatfs_data_start_sector;
123  
-
124  
-int fatfs_init(int devnr)
125  
-{
126  
-	struct firstsector s0;
127  
-	struct fat16_firstsector s;
128  
-	int i;
129  
-
130  
-	if(!bd_init(devnr)) {
131  
-		printf("E: Unable to initialize memory card driver\n");
132  
-		return 0;
133  
-	}
134  
-
135  
-	if(bd_has_part_table(devnr)) {
136  
-		/* Read sector 0, with partition table */
137  
-		if(!bd_readblock(0, (void *)&s0)) {
138  
-			printf("E: Unable to read block 0\n");
139  
-			return 0;
140  
-		}
141  
-
142  
-		fatfs_partition_start_sector = -1;
143  
-		for(i=0;i<4;i++)
144  
-			if((s0.partitions[i].type == PARTITION_TYPE_FAT16)
145  
-			||(s0.partitions[i].type == PARTITION_TYPE_FAT32)) {
146  
-#ifdef DEBUG
147  
-				printf("I: Using partition #%d: start sector %08x, end sector %08x\n", i,
148  
-					le32toh(s0.partitions[i].start_sector), le32toh(s0.partitions[i].end_sector));
149  
-#endif
150  
-				fatfs_partition_start_sector = le32toh(s0.partitions[i].start_sector);
151  
-				break;
152  
-			}
153  
-		if(fatfs_partition_start_sector == -1) {
154  
-			printf("E: No FAT partition was found\n");
155  
-			return 0;
156  
-		}
157  
-	} else
158  
-		fatfs_partition_start_sector = 0;
159  
-	
160  
-	/* Read first FAT16 sector */
161  
-	if(!bd_readblock(fatfs_partition_start_sector, (void *)&s)) {
162  
-		printf("E: Unable to read first FAT sector\n");
163  
-		return 0;
164  
-	}
165  
-	
166  
-#ifdef DEBUG
167  
-	{
168  
-		char oem[9];
169  
-		char volume_label[12];
170  
-		memcpy(oem, s.oem, 8);
171  
-		oem[8] = 0;
172  
-		memcpy(volume_label, s.volume_label, 11);
173  
-		volume_label[11] = 0;
174  
-		printf("I: OEM name: %s\n", oem);
175  
-		printf("I: Volume label: %s\n", volume_label);
176  
-	}
177  
-#endif
178  
-	
179  
-	if(le16toh(s.bytes_per_sector) != BLOCK_SIZE) {
180  
-		printf("E: Unexpected number of bytes per sector (%d)\n", le16toh(s.bytes_per_sector));
181  
-		return 0;
182  
-	}
183  
-	fatfs_sectors_per_cluster = s.sectors_per_cluster;
184  
-	
185  
-	fatfs_fat_entries = (le16toh(s.sectors_per_fat)*BLOCK_SIZE)/2;
186  
-	fatfs_fat_sector = fatfs_partition_start_sector + 1;
187  
-	fatfs_fat_cached_sector = -1;
188  
-	
189  
-	fatfs_max_root_entries = le16toh(s.max_root_entries);
190  
-	fatfs_root_table_sector = fatfs_fat_sector + s.number_of_fat*le16toh(s.sectors_per_fat);
191  
-	fatfs_dir_cached_sector = -1;
192  
-	
193  
-	fatfs_data_start_sector = fatfs_root_table_sector + (fatfs_max_root_entries*sizeof(struct directory_entry))/BLOCK_SIZE;
194  
-
195  
-	if(fatfs_max_root_entries == 0) {
196  
-		printf("E: Your memory card uses FAT32, which is not supported.\n");
197  
-		printf("E: Please reformat your card using FAT16, e.g. use mkdosfs -F 16\n");
198  
-		printf("E: FAT32 support would be an appreciated contribution.\n");
199  
-		return 0;
200  
-	}
201  
-	
202  
-#ifdef DEBUG
203  
-	printf("I: Cluster is %d sectors, FAT has %d entries, FAT 1 is at sector %d,\nI: root table is at sector %d (max %d), data is at sector %d, nb of fat: %d\n",
204  
-		fatfs_sectors_per_cluster, fatfs_fat_entries, fatfs_fat_sector,
205  
-		fatfs_root_table_sector, fatfs_max_root_entries,
206  
-		fatfs_data_start_sector, s.number_of_fat);
207  
-#endif
208  
-	return 1;
209  
-}
210  
-
211  
-static int fatfs_read_fat(int offset)
212  
-{
213  
-	int wanted_sector;
214  
-	
215  
-	if((offset < 0) || (offset >= fatfs_fat_entries)) {
216  
-		printf("E: Incorrect offset %d in fatfs_read_fat\n", offset);
217  
-		return -1;
218  
-	}
219  
-		
220  
-	wanted_sector = fatfs_fat_sector + (offset*2)/BLOCK_SIZE;
221  
-	if(wanted_sector != fatfs_fat_cached_sector) {
222  
-		if(!bd_readblock(wanted_sector, (void *)&fatfs_fat_sector_cache)) {
223  
-			printf("E: Memory card failed (FAT), sector %d\n", wanted_sector);
224  
-			return -1;
225  
-		}
226  
-		fatfs_fat_cached_sector = wanted_sector;
227  
-	}
228  
-	
229  
-	return le16toh(fatfs_fat_sector_cache[offset % (BLOCK_SIZE/2)]);
230  
-}
231  
-
232  
-static const struct directory_entry *fatfs_read_root_directory(int offset)
233  
-{
234  
-	int wanted_sector;
235  
-	
236  
-	if((offset < 0) || (offset >= fatfs_max_root_entries))
237  
-		return NULL;
238  
-
239  
-	wanted_sector = fatfs_root_table_sector + (offset*sizeof(struct directory_entry))/BLOCK_SIZE;
240  
-
241  
-	if(wanted_sector != fatfs_dir_cached_sector) {
242  
-		if(!bd_readblock(wanted_sector, (void *)&fatfs_dir_sector_cache)) {
243  
-			printf("E: Memory card failed (Rootdir), sector %d\n", wanted_sector);
244  
-			return NULL;
245  
-		}
246  
-		fatfs_dir_cached_sector = wanted_sector;
247  
-	}
248  
-	return &fatfs_dir_sector_cache[offset % (BLOCK_SIZE/sizeof(struct directory_entry))];
249  
-}
250  
-
251  
-static void lfn_to_ascii(const struct directory_entry_lfn *entry, char *name, int terminate)
252  
-{
253  
-	int i;
254  
-	unsigned short c;
255  
-
256  
-	for(i=0;i<5;i++) {
257  
-		c = le16toh(entry->name1[i]);
258  
-		if(c <= 255) {
259  
-			*name = c;
260  
-			name++;
261  
-			if(c == 0) return;
262  
-		}
263  
-	}
264  
-	for(i=0;i<6;i++) {
265  
-		c = le16toh(entry->name2[i]);
266  
-		if(c <= 255) {
267  
-			*name = c;
268  
-			name++;
269  
-			if(c == 0) return;
270  
-		}
271  
-	}
272  
-	for(i=0;i<2;i++) {
273  
-		c = le16toh(entry->name3[i]);
274  
-		if(c <= 255) {
275  
-			*name = c;
276  
-			name++;
277  
-			if(c == 0) return;
278  
-		}
279  
-	}
280  
-
281  
-	if(terminate)
282  
-		*name = 0;
283  
-}
284  
-
285  
-static int fatfs_is_regular(const struct directory_entry *entry)
286  
-{
287  
-	return ((entry->attributes & 0x10) == 0)
288  
-		&& ((entry->attributes & 0x08) == 0)
289  
-		&& (entry->filename[0] != 0xe5);
290  
-}
291  
-
292  
-int fatfs_list_files(fatfs_dir_callback cb, void *param)
293  
-{
294  
-	const struct directory_entry *entry;
295  
-	char fmtbuf[8+1+3+1];
296  
-	char longname[131];
297  
-	int has_longname;
298  
-	int i, j, k;
299  
-
300  
-	has_longname = 0;
301  
-	longname[sizeof(longname)-1] = 0; /* avoid crashing when reading a corrupt FS */
302  
-	for(k=0;k<fatfs_max_root_entries;k++) {
303  
-		entry = fatfs_read_root_directory(k);
304  
-#ifdef DEBUG
305  
-		printf("I: Read entry with attribute %02x\n", entry->attributes);
306  
-#endif
307  
-		if(entry->attributes == 0x0f) {
308  
-			const struct directory_entry_lfn *entry_lfn;
309  
-			unsigned char frag;
310  
-			int terminate;
311  
-
312  
-			entry_lfn = (const struct directory_entry_lfn *)entry;
313  
-			frag = entry_lfn->seq & 0x3f;
314  
-			terminate = entry_lfn->seq & 0x40;
315  
-			if(frag*13 < sizeof(longname)) {
316  
-				lfn_to_ascii((const struct directory_entry_lfn *)entry, &longname[(frag-1)*13], terminate);
317  
-				if(frag == 1) has_longname = 1;
318  
-			}
319  
-			continue;
320  
-		} else {
321  
-			if(!fatfs_is_regular(entry)) {
322  
-				has_longname = 0;
323  
-				continue;
324  
-			}
325  
-		}
326  
-		if(entry == NULL) return 0;
327  
-		if(entry->filename[0] == 0) {
328  
-			has_longname = 0;
329  
-			break;
330  
-		}
331  
-		j = 0;
332  
-		for(i=0;i<8;i++) {
333  
-			if(entry->filename[i] == ' ') break;
334  
-			fmtbuf[j++] = entry->filename[i];
335  
-		}
336  
-		fmtbuf[j++] = '.';
337  
-		for(i=0;i<3;i++) {
338  
-			if(entry->extension[i] == ' ') break;
339  
-			fmtbuf[j++] = entry->extension[i];
340  
-		}
341  
-		fmtbuf[j++] = 0;
342  
-		if(!cb(fmtbuf, has_longname ? longname : fmtbuf, param)) return 0;
343  
-		has_longname = 0;
344  
-	}
345  
-	return 1;
346  
-}
347  
-
348  
-static const struct directory_entry *fatfs_find_file_by_name(const char *filename)
349  
-{
350  
-	char searched_filename[8];
351  
-	char searched_extension[3];
352  
-	char *dot;
353  
-	const char *c;
354  
-	int i;
355  
-	const struct directory_entry *entry;
356  
-	
357  
-	dot = strrchr(filename, '.');
358  
-	if(dot == NULL)
359  
-		return NULL;
360  
-	
361  
-	memset(searched_filename, ' ', 8);
362  
-	memset(searched_extension, ' ', 3);
363  
-	i = 0;
364  
-	for(c=filename;c<dot;c++)
365  
-		searched_filename[i++] = toupper(*c);
366  
-		
367  
-	i = 0;
368  
-	for(c=dot+1;*c!=0;c++)
369  
-		searched_extension[i++] = toupper(*c);
370  
-		
371  
-	for(i=0;i<fatfs_max_root_entries;i++) {
372  
-		entry = fatfs_read_root_directory(i);
373  
-		if(entry == NULL) break;
374  
-		if(entry->filename[0] == 0) break;
375  
-		if(!fatfs_is_regular(entry)) continue;
376  
-		if(!memcmp(searched_filename, entry->filename, 8)
377  
-		 &&!memcmp(searched_extension, entry->extension, 3))
378  
-		 	return entry;
379  
-	}
380  
-	return NULL;
381  
-}
382  
-
383  
-static int fatfs_load_cluster(int clustern, char *buffer, int maxsectors)
384  
-{
385  
-	int startsector;
386  
-	int i;
387  
-	int toread;
388  
-	
389  
-	clustern = clustern - 2;
390  
-	startsector = fatfs_data_start_sector + clustern*fatfs_sectors_per_cluster;
391  
-	if(maxsectors < fatfs_sectors_per_cluster)
392  
-		toread = maxsectors;
393  
-	else
394  
-		toread = fatfs_sectors_per_cluster;
395  
-	for(i=0;i<toread;i++)
396  
-		if(!bd_readblock(startsector+i, (unsigned char *)buffer+i*BLOCK_SIZE)) {
397  
-			printf("E: Memory card failed (Cluster), sector %d\n", startsector+i);
398  
-			return 0;
399  
-		}
400  
-	return 1;
401  
-}
402  
-
403  
-int fatfs_load(const char *filename, char *buffer, int size, int *realsize)
404  
-{
405  
-	const struct directory_entry *entry;
406  
-	int cluster_size;
407  
-	int cluster;
408  
-	int n;
409  
-	
410  
-	cluster_size = fatfs_sectors_per_cluster*BLOCK_SIZE;
411  
-	size /= BLOCK_SIZE;
412  
-	
413  
-	entry = fatfs_find_file_by_name(filename);
414  
-	if(entry == NULL) {
415  
-		printf("E: File not found: %s\n", filename);
416  
-		return 0;
417  
-	}
418  
-	
419  
-	if(realsize != NULL) *realsize = le32toh(entry->file_size);
420  
-	
421  
-	n = 0;
422  
-	cluster = le16toh(entry->first_cluster);
423  
-	while(size > 0) {
424  
-		if(!fatfs_load_cluster(cluster, buffer+n*cluster_size, size))
425  
-			return 0;
426  
-		size -= fatfs_sectors_per_cluster;
427  
-		n++;
428  
-		cluster = fatfs_read_fat(cluster);
429  
-		if(cluster >= 0xFFF8) break;
430  
-		if(cluster == -1) return 0;
431  
-	}
432  
-	//putsnonl("\n");
433  
-	
434  
-	return n*cluster_size;
435  
-}
436  
-
437  
-void fatfs_done(void)
438  
-{
439  
-	bd_done();
440  
-}

No commit comments for this range

Something went wrong with that request. Please try again.