summaryrefslogtreecommitdiff
path: root/srec.c
blob: 7c3aaf4e6f0894838eb54e10e837eef616f11330 (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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
 * 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 "nios2sim-ng.h"
#include "image.h"

#define SREC_CRC_COUNT		1

#define SREC_LINE_LENGTH	515
static char data_buf[SREC_LINE_LENGTH];

/**
 * @return  length of the line
 */
static ssize_t srec_read_line(FILE *fp, char *buf, size_t count)
{
	char c;
	ssize_t len = 0;

	while (count > 0 && !feof(fp)) {
		if (ferror(fp)) {
			err("ferror\n");
			goto err_out;
		}

		/* Read byte by byte */
		if (fread(&c, 1, 1, fp) == 1) {
			if (c == '\n' || c == '\r')
				break;	/* end of line */

			buf[len++] = c;
			count--;
		} else {
			if (feof(fp) && !ferror(fp))
				break;
			else
				goto err_out;
		}
	}

	return len;
err_out:
	return -1;
}

static int srec_load_S3(char *buf, size_t data_count,
		off_t offset, uint8_t *mem_base, size_t mem_size)
{
	size_t i;
	uint32_t *base = (uint32_t *) mem_base;

	for (i = 0; i < data_count; i += 4) {
		uint32_t tmp;

		if (sscanf(buf + i * 2, "%8x", &tmp) != 1) {
			err("sscanf() failed on S3 record\n");
			return -1;
		}
		/* SREC is Big Endian, NiosII is Little Endian */
		tmp = bswap_32(tmp);

		if (offset + i >= mem_size) {
			err("Image file too large for allocated memory of %zu bytes\n", mem_size);
			return -1;
		}

		/* Store in memory */
		base[offset / 4 + i] = tmp;
	}

	return 0;
}

static int srec_handle_line(char *buf, size_t len, uint8_t *mem_base, size_t mem_size)
{
	unsigned int data_count = 0;
	unsigned int start_addr;

	/* Minimum valid record size */
	if (len < 2) {
		err("Invalid line in SREC file\n");
		return -1;
	}

	if (buf[0] != 'S') {
		err("Invalid line in SREC file\n");
		return -1;
	}

	switch (buf[1]) {
	case '0':
		/* Ignore */
		break;
	case '1':
		dbg("handling S1\n");
		break;
	case '2':
		dbg("handling S2\n");
		break;
	case '3':
		if (sscanf(buf, "S3%2x%8x", &data_count, &start_addr) != 2) {
			err("Invalid S3 record\n");
			return -1;
		}

		if (srec_load_S3(buf + 12, data_count - 4, start_addr, mem_base, mem_size) != 0)
			return -1;
		break;
	case '4':
		dbg("handling S4\n");
		break;
	case '5':
		dbg("handling S5\n");
		break;
	case '6':
		dbg("handling S6\n");
		break;
	case '7':
		dbg("handling S7\n");
		break;
	case '8':
		dbg("handling S8\n");
		break;
	case '9':
		dbg("handling S9\n");
		break;
	default:
		err("Invalid SREC type: %c\n", buf[1]);
		return -1;
	}

	return 0;
}

int srec_load(FILE *fp, const char *name __unused, uint8_t *mem_base, size_t mem_size)
{
	ssize_t len;
	int ret = 0;

	while (1) {
		memset(data_buf, 0x00, SREC_LINE_LENGTH);
		len = srec_read_line(fp, data_buf, SREC_LINE_LENGTH);
		if (len < 0) {
			ret = -1;
			break;
		} else if (len == 0) {
			ret = 0;
			break;
		} else {
			ret = srec_handle_line(data_buf, len, mem_base, mem_size);
			if (ret < 0)
				break;
		}
	}

	return ret;
}