summaryrefslogtreecommitdiff
path: root/image.c
diff options
context:
space:
mode:
Diffstat (limited to 'image.c')
-rw-r--r--image.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/image.c b/image.c
new file mode 100644
index 0000000..1f12238
--- /dev/null
+++ b/image.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (C) 2010 Tobias Klauser <tklauser@distanz.ch>
+ *
+ * This file is part of nios2sim-ng.
+ *
+ * This file is subject to the terms and conditions of the GNU General Public
+ * License. See the file "COPYING" in the main directory of this archive
+ * for more details.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <errno.h>
+
+#include "nios2sim-ng.h"
+#include "image.h"
+
+int image_load(const char *image_path, int image_format, uint8_t *mem_base,
+ size_t mem_size)
+{
+ FILE *fp;
+ loader_func_t loader;
+ int ret;
+
+ switch(image_format) {
+ case FORMAT_ELF:
+ loader = elf_load;
+ break;
+ case FORMAT_SREC:
+ loader = srec_load;
+ break;
+ default:
+ err("Invalid image format\n");
+ return -1;
+ }
+
+ fp = fopen(image_path, "r");
+ if (unlikely(fp == NULL)) {
+ err("Could not open image file '%s': %s\n", image_path, strerror(errno));
+ return -1;
+ }
+
+ ret = loader(fp, image_path, mem_base, mem_size);
+
+ fclose(fp);
+
+ return ret;
+}