summaryrefslogtreecommitdiff
path: root/image.c
blob: 1f12238111a61489129655bde0d20131c71fc67b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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;
}