Index: EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c
===================================================================
--- EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c	(revision 278)
+++ EcnlProtoTool/trunk/ntshell/fatfs/ffarch.c	(revision 279)
@@ -56,8 +56,7 @@
 #include <kernel.h>
 #include <stdlib.h>
-#include <errno.h>
-#include <sys/types.h>
 #include <string.h>
 #include "t_stdlib.h"
+#include "syssvc/serial.h"
 #include "syssvc/syslog.h"
 #include "kernel_cfg.h"
@@ -65,4 +64,16 @@
 #include "sdfs.h"
 #include "ff.h"
+#include "tlsf.h"
+#include "ntstdio.h"
+
+#define SIO_PORTID 1
+extern ntstdio_t ntstdio;
+
+static tlsf_t sys_tlsf;
+static pool_t sys_pool;
+
+uint32_t  __HeapBase;
+uint32_t  __HeapLimit;
+FATFS RomDisk;
 
 gpio_t ins;
@@ -70,12 +81,20 @@
 #define WP() false
 
-extern FATFS RomDisk;
-extern unsigned char RamDisk[SZ_RAMDISK * 1024];
-
+typedef struct SD {
+	DSTATUS dst;
+	BYTE type;
+	FATFS FatFs;
+} SD;
+SD Sd;
+
+void sys_init(void);
 bool_t romdisk_init();
+bool_t SD_begin();
 
 int mruby_arduino_init()
 {
 	int result = -1;
+
+	sys_init();
 
 	/* SD_CD */
@@ -85,26 +104,65 @@
 	sdfs_init(&sdfs, P8_5, P8_6, P8_3, P8_4, "sd");
 
-	SD_begin();
-
-	FIL fd;
-	UINT rlen = 0;
-	memset(&fd, 0, sizeof(fd));
-	if (f_open(&fd, "1:/httpd-fs.bin", FA_OPEN_EXISTING | FA_READ) == FR_OK) {
-		f_read(&fd, RamDisk, SZ_RAMDISK * 1024, &rlen);
-		f_close(&fd);
-		if (romdisk_init())
-			result = 0;
-	}
-
+	result = SD_begin() ? 0 : -1;
 	if (result == 0) {
-		syslog(LOG_NOTICE, "ramdisk ok!");
+		ntstdio_printf(&ntstdio, "SD card (1:) OK!\n");
 	}
 	else {
-		syslog(LOG_NOTICE, "ramdisk ng!");
-	}
-
-	arduino_init();
+		ntstdio_printf(&ntstdio, "SD card (1:) NG!\n");
+	}
+	sta_cyc(SDFS_CYC);
+
+	if (romdisk_init())
+		result = 0;
+
+	if (result == 0) {
+		ntstdio_printf(&ntstdio, "ROM disk (0:) OK!\n");
+	}
+	else {
+		ntstdio_printf(&ntstdio, "ROM disk (0:) NG!\n");
+	}
+
+	/* uploadãã£ã¬ã¯ããªãä½æãã¦ãã */
+	f_mkdir("1:/upload");
+
+	serial_ctl_por(SIO_PORTID, IOCTL_FCSND | IOCTL_FCRCV);
 
 	return result;
+}
+
+void sys_init(void)
+{
+	sys_tlsf = tlsf_create(&__HeapBase);
+	if (sys_tlsf == NULL)
+		return;
+
+	sys_pool = tlsf_add_pool(sys_tlsf, ((uint8_t *)&__HeapBase) + tlsf_size(), ((size_t)&__HeapLimit - (size_t)&__HeapBase) - tlsf_size());
+}
+
+void sys_fini(void)
+{
+	tlsf_destroy(sys_tlsf);
+}
+
+void *sys_malloc(size_t size)
+{
+	return tlsf_malloc(sys_tlsf, size);
+}
+
+void *sys_calloc(size_t size, size_t count)
+{
+	void *result = tlsf_malloc(sys_tlsf, count * size);
+	memset(result, 0, count * size);
+	return result;
+}
+
+void *sys_realloc(void *ptr, size_t size)
+{
+	return tlsf_realloc(sys_tlsf, ptr, size);
+}
+
+void sys_free(void *ptr)
+{
+	tlsf_free(sys_tlsf, ptr);
 }
 
@@ -122,4 +180,33 @@
 
 	if ((res = f_mount(&RomDisk, "0:", 1)) != FR_OK) {
+		return false;
+	}
+
+	return true;
+}
+
+bool_t SD_begin()
+{
+	DSTATUS dst;
+	FRESULT res;
+	BYTE pdrv = 1, type;
+
+	if (Sd.FatFs.fs_type != 0)
+		return true;
+
+	if ((dst = disk_initialize(pdrv)) != RES_OK) {
+		return false;
+	}
+
+	if ((dst = disk_ioctl(pdrv, MMC_GET_TYPE, &type)) != RES_OK) {
+		Sd.dst = dst;
+		Sd.type = 0;
+	}
+	else {
+		Sd.dst = RES_OK;
+		Sd.type = type;
+	}
+
+	if ((res = f_mount(&Sd.FatFs, "1:", 1)) != FR_OK) {
 		return false;
 	}
@@ -207,122 +294,33 @@
 }
 
-static int lock[TNUM_TSKID];
-static int stack_pos[TNUM_TSKID];
-
-void __malloc_lock(struct _reent *a)
-{
-	ID tskid;
+int ff_cre_syncobj(BYTE vol, _SYNC_t* sobj)
+{
+	return 1;
+}
+
+int ff_req_grant(_SYNC_t sobj)
+{
 	ER ret;
-	int count, sp;
-
-	ret = get_tid(&tskid);
-	if (ret != E_OK) {
-		/*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
-		Asm("bkpt #0");
-	}
-
-	Asm("mov %0, sp" : "=r"(sp) :);
-	if ((stack_pos[tskid - 1] == 0) || (stack_pos[tskid - 1] < sp))
-		stack_pos[tskid - 1] = sp;
-
-	count = ++lock[tskid - 1];
-	if (count != 1)
-		return;
-
-	ret = wai_sem(SEM_MALLOC);
-	if (ret != E_OK) {
-		/*syslog(LOG_DEBUG, "wai_sem %s", itron_strerror(ret));*/
-		Asm("bkpt #0");
-	}
-}
-
-void __malloc_unlock(struct _reent *a)
-{
-	ID tskid;
-	ER ret;
-	int count;
-
-	ret = get_tid(&tskid);
-	if (ret != E_OK) {
-		/*syslog(LOG_DEBUG, "get_tid %s", itron_strerror(ret));*/
-		Asm("bkpt #0");
-	}
-
-	count = --lock[tskid - 1];
-	if(count != 0)
-		return;
-
-	ret = sig_sem(SEM_MALLOC);
-	if (ret != E_OK) {
-		/*syslog(LOG_DEBUG, "sig_sem %s", itron_strerror(ret));*/
-		Asm("bkpt #0");
-	}
-}
-
-// Provide implementation of _sbrk (low-level dynamic memory allocation
-// routine) for GCC_ARM which compares new heap pointer with MSP instead of
-// SP.  This make it compatible with RTX RTOS thread stacks.
-
-// Linker defined symbol used by _sbrk to indicate where heap should start.
-int __end__;
-uint32_t  __HeapLimit;
-
-// Turn off the errno macro and use actual global variable instead.
-#undef errno
-int errno;
-
-static unsigned char* heap = (unsigned char*)&__end__;
-
-// Dynamic memory allocation related syscall.
-caddr_t _sbrk(int incr) {
-	unsigned char*        prev_heap = heap;
-	unsigned char*        new_heap = heap + incr;
-
-	if (new_heap >= (unsigned char*)&__HeapLimit) {     /* __HeapLimit is end of heap section */
-		errno = ENOMEM;
-		return (caddr_t)-1;
-	}
-
-	heap = new_heap;
-	return (caddr_t) prev_heap;
-}
-
-void reset_heap()
-{
-	// .data
-	extern uint8_t __malloc_av_[0x408];
-	extern uint8_t __malloc_sbrk_base[0x4];
-	extern uint8_t __malloc_trim_threshold[0x4];
-	// .bss
-	extern uint8_t __malloc_top_pad[0x4];
-	extern uint8_t __malloc_current_mallinfo[0x28];
-	extern uint8_t __malloc_max_sbrked_mem[0x4];
-	extern uint8_t __malloc_max_total_mem[0x4];
-	//
-	extern void *__etext;
-	extern void *__data_start__;
-
-	int offset = (int)&__etext - (int)&__data_start__;
-
-	__malloc_lock(_REENT);
-
-	memcpy(__malloc_av_, &__malloc_av_[offset], sizeof(__malloc_av_));
-	memcpy(__malloc_sbrk_base, &__malloc_sbrk_base[offset], sizeof(__malloc_sbrk_base));
-	memcpy(__malloc_trim_threshold, &__malloc_trim_threshold[offset], sizeof(__malloc_trim_threshold));
-
-	memset(__malloc_top_pad, 0, sizeof(__malloc_top_pad));
-	memset(__malloc_current_mallinfo, 0, sizeof(__malloc_current_mallinfo));
-	memset(__malloc_max_sbrked_mem, 0, sizeof(__malloc_max_sbrked_mem));
-	memset(__malloc_max_total_mem, 0, sizeof(__malloc_max_total_mem));
-
-	heap = (unsigned char*)&__end__;
-
-	__malloc_unlock(_REENT);
-}
-
-void exit(int return_code) {
-	Asm("bkpt #0");
-	reset_heap();
-	ext_tsk();
-	for(;;);
-}
+	ret = wai_sem(SEM_FILESYSTEM);
+	return ret == E_OK;
+}
+
+void ff_rel_grant(_SYNC_t sobj)
+{
+	sig_sem(SEM_FILESYSTEM);
+}
+
+int ff_del_syncobj(_SYNC_t sobj)
+{
+	return 1;
+}
+
+void* ff_memalloc (UINT msize)
+{
+	return sys_malloc(msize);
+}
+
+void ff_memfree (void* mblock)
+{
+	sys_free(mblock);
+}
