summaryrefslogtreecommitdiffstats
path: root/file/backend.c
blob: 224719d3542caf8b230726d2a66af4693d1cbf9d (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// SPDX-License-Identifier: Apache-2.0
// Copyright (C) 2018 IBM Corp.
// Copyright (C) 2018 Evan Lojewski.

#define _GNU_SOURCE
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <getopt.h>
#include <inttypes.h>
#include <limits.h>
#include <mtd/mtd-abi.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include "common.h"
#include "backend.h"
#include "lpc.h"
#include "mboxd.h"

#define MIN(__x__, __y__)  (((__x__) < (__y__)) ? (__x__) : (__y__))

#define FILE_ERASE_SIZE (4 * 1024)

struct file_data {
	int fd;
};

static int file_dev_init(struct backend *backend, void *data)
{
	struct mtd_info_user info;
	const char *path = data;
	struct file_data *priv;
	struct stat statbuf;
	int rc = 0;

	if (!path) {
		MSG_ERR("No PNOR file specified\n");
		return -EINVAL;
	}

	priv = malloc(sizeof(*priv));
	if (!priv)
		return -errno;

	MSG_DBG("Opening %s\n", path);

	priv->fd = open(path, O_RDWR);
	if (priv->fd < 0) {
		MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", path,
			strerror(errno));
		rc = -errno;
		goto cleanup_data;
	}

	/* Don't attach to mtd devices. */
	rc = ioctl(priv->fd, MEMGETINFO, &info);
	if (rc != -1) {
		rc = -errno;
		goto cleanup_fd;
	}

	rc = fstat(priv->fd, &statbuf);
	if (rc < 0) {
		rc = -errno;
		goto cleanup_fd;
	}

	if (backend->flash_size == 0) {
		MSG_INFO("Flash size should be supplied on the commandline.\n");
		backend->flash_size = statbuf.st_size;
	}

	/* Pick an erase size congruent with partition alignment */
	backend->erase_size_shift = log_2(FILE_ERASE_SIZE);
	backend->block_size_shift = backend->erase_size_shift;
	MSG_DBG("Flash erase size: 0x%.8x\n", FILE_ERASE_SIZE);

	backend->priv = priv;
	return rc;

cleanup_fd:
	close(priv->fd);
cleanup_data:
	free(priv);
	return rc;
}

static void file_dev_free(struct backend *backend)
{
	struct file_data *priv = backend->priv;

	close(priv->fd);
	free(priv);
}

/* Flash Functions */

/*
 * file_erase() - Erase the flash
 * @context:	The mbox context pointer
 * @offset:	The flash offset to erase (bytes)
 * @size:	The number of bytes to erase
 *
 * Return:	0 on success otherwise negative error code
 */
static int file_erase(struct backend *backend, uint32_t offset, uint32_t count)
{
	const uint32_t erase_size = 1 << backend->erase_size_shift;
	struct file_data *priv = backend->priv;
	struct erase_info_user erase_info = {0};
	int rc;

	MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n", offset, count);

	uint8_t* erase_buf = (uint8_t*)malloc(count);
	if (!erase_buf) {
		MSG_ERR("Couldn't malloc erase buffer. %s\n", strerror(errno));
		return -1;
	}
	memset(erase_buf, 0xFF, erase_size);
	rc = pwrite(priv->fd, erase_buf, count, offset);
	free(erase_buf);

	if (rc < 0) {
		MSG_ERR("Couldn't erase flash at 0x%.8x\n", erase_info.start);
		return -errno;
	}


	return 0;
}

#define CHUNKSIZE (64 * 1024)

/*
 * file_copy() - Copy data from the flash device into a provided buffer
 * @context:	The backend context pointer
 * @offset:	The flash offset to copy from (bytes)
 * @mem:	The buffer to copy into (must be of atleast 'size' bytes)
 * @size:	The number of bytes to copy
 * Return:	Number of bytes copied on success, otherwise negative error
 *		code. file_copy will copy at most 'size' bytes, but it may
 *		copy less.
 */
static int64_t file_copy(struct backend *backend, uint32_t offset,
			  void *mem, uint32_t size)
{
	struct file_data *priv = backend->priv;
	int32_t size_read;
	void *start = mem;

	MSG_DBG("Copy flash to %p for size 0x%.8x from offset 0x%.8x\n", mem,
		size, offset);
	if (lseek(priv->fd, offset, SEEK_SET) != offset) {
		MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset,
			strerror(errno));
		return -errno;
	}

	do {
		size_read = read(priv->fd, mem, min_u32(CHUNKSIZE, size));
		if (size_read < 0) {
			MSG_ERR("Couldn't copy file into ram: %s\n",
				strerror(errno));
			return -errno;
		}

		size -= size_read;
		mem += size_read;
	} while (size && size_read);

	return size_read ? mem - start : -EIO;
}

/*
 * file_write() - Write the flash from a provided buffer
 * @context:	The mbox context pointer
 * @offset:	The flash offset to write to (bytes)
 * @buf:	The buffer to write from (must be of atleast size)
 * @size:	The number of bytes to write
 *
 * Return:	0 on success otherwise negative error code
 */
static int file_write(struct backend *backend, uint32_t offset, void *buf,
		       uint32_t count)
{
	struct file_data *priv = backend->priv;
	uint32_t buf_offset = 0;
	int rc;

	MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count,
		buf);

	if (lseek(priv->fd, offset, SEEK_SET) != offset) {
		MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset,
			strerror(errno));
		return -errno;
	}

	while (count) {
		rc = write(priv->fd, buf + buf_offset, count);
		if (rc < 0) {
			MSG_ERR("Couldn't write to file, write lost: %s\n",
				strerror(errno));
			return -errno;
		}
		count -= rc;
		buf_offset += rc;
	}

	return 0;
}

/*
 * file_reset() - Reset the lpc bus mapping
 * @context:    The backend context pointer
 * @buf:	Pointer to the LPC reserved memory
 * @count:	The size of the LPC reserved memory
 *
 * Return:      0 on success otherwise negative error code
 */
static int file_reset(struct backend *backend, void *buf, uint32_t count)
{
	struct file_data *priv = backend->priv;
	size_t len;
	int rc;

	len = MIN(backend->flash_size, count);

	/* Ugh, otherwise we need to parse the FFS image */
	assert(len == backend->flash_size);

	/* Preload Flash contents into memory window */
	rc = pread(priv->fd, buf, len, 0);
	if (rc < 0)
		return -errno;

	return reset_lpc_memory;
}

static const struct backend_ops file_ops = {
	.init = file_dev_init,
	.free = file_dev_free,
	.copy = file_copy,
	.erase = file_erase,
	.write = file_write,
	.reset = file_reset,
	.validate = NULL,
};

struct backend backend_get_file(void)
{
	struct backend be = {0};

	be.ops = &file_ops;

	return be;
}

int backend_probe_file(struct backend *master, const char *path)
{
	struct backend with;

	assert(master);
	with = backend_get_file();

	return backend_init(master, &with, (void *)path);
}
OpenPOWER on IntegriCloud