Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: m-labs/misp
base: 6707cd1
...
head fork: m-labs/misp
compare: 9c84387
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 1 contributor
5 Makefile
View
@@ -40,4 +40,7 @@ clean:
make -C $(MISPDIR)/lib$$lib clean; \
done
-.PHONY: clean libs
+netboot: misp.bin
+ cp misp.bin /var/lib/tftpboot/boot.bin
+
+.PHONY: clean libs netboot
4 libglue/Makefile
View
@@ -1,8 +1,8 @@
MISPDIR=..
include $(MISPDIR)/common.mak
-CFLAGS+=-I$(YAFFSDIR)/direct -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_DEFINES_TYPES -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES
-OBJECTS=file.o getenv.o
+CFLAGS+=-I$(YAFFSDIR)/direct -DCONFIG_YAFFS_DIRECT -DCONFIG_YAFFS_DEFINES_TYPES -DCONFIG_YAFFS_PROVIDE_DEFS -DCONFIG_YAFFSFS_PROVIDE_VALUES -I$(MISPDIR)/libglue/include
+OBJECTS=yaffs.o asprintf.o file.o getenv.o
all: libglue.a
47 libglue/asprintf.c
View
@@ -0,0 +1,47 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <asprintf.h>
+
+int vasprintf(char **strp, const char *fmt, va_list ap)
+{
+ va_list aq;
+ int alloclen;
+ int len;
+ char *newbuf;
+
+ alloclen = 256;
+ *strp = malloc(alloclen);
+ if(!*strp)
+ return -1;
+
+ while(1) {
+ va_copy(aq, ap);
+ len = vscnprintf(*strp, alloclen, fmt, aq);
+ va_end(aq);
+ if(len < 0) {
+ free(*strp);
+ return len;
+ }
+ if(len < alloclen)
+ return len;
+ alloclen *= 2;
+ newbuf = realloc(*strp, alloclen);
+ if(!newbuf) {
+ free(*strp);
+ return -1;
+ }
+ *strp = newbuf;
+ }
+}
+
+int asprintf(char **strp, const char *fmt, ...)
+{
+ va_list args;
+ int len;
+
+ va_start(args, fmt);
+ len = vasprintf(strp, fmt, args);
+ va_end(args);
+ return len;
+}
292 libglue/file.c
View
@@ -1,79 +1,225 @@
#include <stdio.h>
+#include <stdlib.h>
#include <errno.h>
#include <stdarg.h>
+#include <asprintf.h>
#include <console.h>
-#include <yaffs_osglue.h>
+#include <yaffsfs.h>
-/* TODO */
+#define VAL_STDIN 1
+#define VAL_STDOUT 2
+#define VAL_STDERR 3
-unsigned int yaffs_trace_mask;
+FILE *stdin = (FILE *)VAL_STDIN;
+FILE *stdout = (FILE *)VAL_STDOUT;
+FILE *stderr = (FILE *)VAL_STDERR;
-void yaffsfs_Lock(void)
+static int is_std_stream(FILE *fd)
{
+ int f;
+
+ f = (int)fd;
+ return (f == VAL_STDIN) || (f == VAL_STDOUT) || (f == VAL_STDERR);
}
-void yaffsfs_Unlock(void)
-{
-}
+/*
+ * Mode conversion function based on NuttX
+ *
+ * Copyright (C) 2007-2012 Gregory Nutt. All rights reserved.
+ * Author: Gregory Nutt <gnutt@nuttx.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in
+ * the documentation and/or other materials provided with the
+ * distribution.
+ * 3. Neither the name NuttX nor the names of its contributors may be
+ * used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
+ * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
+ * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
+ * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
+ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
-u32 yaffsfs_CurrentTime(void)
-{
- return 0;
-}
+enum open_mode_e {
+ MODE_NONE = 0, /* No access mode determined */
+ MODE_R, /* "r" or "rb" open for reading */
+ MODE_W, /* "w" or "wb" open for writing, truncating or creating file */
+ MODE_A, /* "a" or "ab" open for writing, appending to file */
+ MODE_RPLUS, /* "r+", "rb+", or "r+b" open for update (reading and writing) */
+ MODE_WPLUS, /* "w+", "wb+", or "w+b" open for update, truncating or creating file */
+ MODE_APLUS, /* "a+", "ab+", or "a+b" open for update, appending to file */
+};
-void yaffsfs_SetError(int err)
+static int mode2oflags(const char *mode)
{
- errno = err;
-}
+ enum open_mode_e state;
+ int oflags;
-int yaffsfs_GetLastError(void)
-{
- return errno;
-}
+ /* Verify that a mode string was provided. */
+ if(!mode)
+ goto errout;
-void *yaffsfs_malloc(size_t size)
-{
- return NULL;
+ /* Parse the mode string to determine the corresponding open flags */
+ state = MODE_NONE;
+ oflags = 0;
+
+ for(;*mode;mode++) {
+ switch(*mode) {
+ /* Open for read access ("r", "r[+]", "r[b]", "r[b+]", or "r[+b]") */
+ case 'r':
+ if(state == MODE_NONE) {
+ /* Open for read access */
+ oflags = O_RDONLY;
+ state = MODE_R;
+ } else
+ goto errout;
+ break;
+
+ /* Open for write access ("w", "w[+]", "w[b]", "w[b+]", or "w[+b]") */
+ case 'w':
+ if(state == MODE_NONE) {
+ /* Open for write access, truncating any existing file */
+ oflags = O_WRONLY|O_CREAT|O_TRUNC;
+ state = MODE_W;
+ } else
+ goto errout;
+ break;
+
+ /* Open for write/append access ("a", "a[+]", "a[b]", "a[b+]", or "a[+b]") */
+ case 'a' :
+ if(state == MODE_NONE) {
+ /* Write to the end of the file */
+ oflags = O_WRONLY|O_CREAT|O_APPEND;
+ state = MODE_A;
+ } else
+ goto errout;
+ break;
+
+ /* Open for update access ("[r]+", "[rb]+]", "[r]+[b]", "[w]+",
+ * "[wb]+]", "[w]+[b]", "[a]+", "[ab]+]", "[a]+[b]")
+ */
+ case '+' :
+ switch(state) {
+ case MODE_R:
+ /* Open for read/write access */
+ oflags = O_RDWR;
+ state = MODE_RPLUS;
+ break;
+
+ case MODE_W:
+ /* Open for write read/access, truncating any existing file */
+ oflags = O_RDWR|O_CREAT|O_TRUNC;
+ state = MODE_WPLUS;
+ break;
+
+ case MODE_A:
+ /* Read from the beginning of the file; write to the end */
+ oflags = O_RDWR|O_CREAT|O_APPEND;
+ state = MODE_APLUS;
+ break;
+
+ default:
+ goto errout;
+ break;
+ }
+ break;
+
+ /* Open for binary access ("[r]b", "[r]b[+]", "[r+]b", "[w]b",
+ * "[w]b[+]", "[w+]b", "[a]b", "[a]b[+]", "[a+]b")
+ */
+ case 'b' :
+ if(state != MODE_NONE) {
+ /* nothing to do */
+ } else
+ goto errout;
+ break;
+
+ /* Unrecognized or unsupported mode */
+ default:
+ goto errout;
+ break;
+ }
+ }
+
+ return oflags;
+
+errout:
+ errno = EINVAL;
+ return -1;
}
-void yaffsfs_free(void *ptr)
+
+FILE *fopen(const char *path, const char *mode)
{
+ int oflags;
+ int *fd;
+
+ oflags = mode2oflags(mode);
+ if(oflags < 0)
+ return NULL;
+ fd = malloc(sizeof(int));
+ if(!fd)
+ return NULL;
+ *fd = yaffs_open(path, oflags, 0666);
+ if(*fd < 0) {
+ free(fd);
+ return NULL;
+ }
+ return (FILE *)fd;
}
-void yaffs_bug_fn(const char *file_name, int line_no)
+int fclose(FILE *fd)
{
+ return yaffs_close(*(int *)fd);
}
-
-FILE *stdin;
-FILE *stdout;
-FILE *stderr;
-
int fprintf(FILE *stream, const char *format, ...)
{
va_list args;
int len;
- char outbuf[256];
+ char *outbuf;
va_start(args, format);
- len = vscnprintf(outbuf, sizeof(outbuf), format, args);
+ len = vasprintf(&outbuf, format, args);
va_end(args);
- outbuf[len] = 0;
- putsnonl(outbuf);
+ if(len < 0)
+ return len;
+
+ if(is_std_stream(stream)) {
+ putsnonl(outbuf);
+ free(outbuf);
+ } else {
+ int fd = *(int *)stream;
+
+ len = yaffs_write(fd, outbuf, len);
+ free(outbuf);
+ }
return len;
}
int fflush(FILE *stream)
{
+ /* nothing to do */
return 0;
}
-FILE *fopen(const char *path, const char *mode)
-{
- return NULL;
-}
-
char *fgets(char *s, int size, FILE *stream)
{
return NULL;
@@ -81,43 +227,81 @@ char *fgets(char *s, int size, FILE *stream)
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{
- return 0;
+ if(is_std_stream(stream))
+ return 0;
+ else {
+ int fd = *(int *)stream;
+ int r;
+
+ r = yaffs_read(fd, ptr, size*nmemb);
+ if(r < 0)
+ return r;
+ return r/size;
+ }
}
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
{
- const char *str = ptr;
- int i;
-
- for(i=0;i<size*nmemb;i++)
- putchar(str[i]);
- return nmemb;
+ if(is_std_stream(stream)) {
+ const char *str = ptr;
+ int i;
+
+ for(i=0;i<size*nmemb;i++)
+ putchar(str[i]);
+ return nmemb;
+ } else {
+ int fd = *(int *)stream;
+ int r;
+
+ r = yaffs_write(fd, ptr, size*nmemb);
+ if(r < 0)
+ return r;
+ return r/size;
+ }
}
int getc(FILE *stream)
{
- return 0;
+ if(is_std_stream(stream))
+ return EOF;
+ else {
+ int fd = *(int *)stream;
+ int r;
+ char c;
+
+ r = yaffs_read(fd, &c, 1);
+ if(r < 0)
+ return EOF;
+ return c;
+ }
}
int fputc(int c, FILE *stream)
{
- return 0;
+ if(is_std_stream(stream)) {
+ putchar(c);
+ return c;
+ } else {
+ int fd = *(int *)stream;
+ int r;
+ char c2;
+
+ c2 = c;
+ r = yaffs_write(fd, &c2, 1);
+ if(r < 0)
+ return EOF;
+ return c;
+ }
}
-
int ferror(FILE *stream)
{
- return 0;
+ return 0; /* TODO */
}
int feof(FILE *stream)
{
- return 0;
-}
-
-int fclose(FILE *fp)
-{
- return 0;
+ return 0; /* TODO */
}
FILE *freopen(const char *path, const char *mode, FILE *stream)
9 libglue/include/asprintf.h
View
@@ -0,0 +1,9 @@
+#ifndef __ASPRINTF_H
+#define __ASPRINTF_H
+
+#include <stdarg.h>
+
+int asprintf(char **strp, const char *fmt, ...);
+int vasprintf(char **strp, const char *fmt, va_list ap);
+
+#endif /* __ASPRINTF_H */
59 libglue/yaffs.c
View
@@ -0,0 +1,59 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <console.h>
+
+#include <hw/mem.h>
+
+#include <yaffsfs.h>
+
+/*
+ * YAFFS callbacks
+ */
+
+unsigned int yaffs_trace_mask;
+
+void yaffsfs_Lock(void)
+{
+ /* nothing to do */
+}
+
+void yaffsfs_Unlock(void)
+{
+ /* nothing to do */
+}
+
+u32 yaffsfs_CurrentTime(void)
+{
+ return 0;
+}
+
+void yaffsfs_SetError(int err)
+{
+ errno = -err;
+}
+
+int yaffsfs_GetLastError(void)
+{
+ return -errno;
+}
+
+void *yaffsfs_malloc(size_t size)
+{
+ return malloc(size);
+}
+
+void yaffsfs_free(void *ptr)
+{
+ free(ptr);
+}
+
+void yaffs_bug_fn(const char *file_name, int line_no)
+{
+ printf("YAFFS BUG in %s line %d\n", file_name, line_no);
+}
+
+/*
+ * YAFFS init and flash access
+ */

No commit comments for this range

Something went wrong with that request. Please try again.