xref: /openbmc/hiomapd/file/backend.c (revision 68a24c9ea5ce11c87fab22a3f4648c7d88c98fee)
1a042978bSEvan Lojewski // SPDX-License-Identifier: Apache-2.0
2a042978bSEvan Lojewski // Copyright (C) 2018 IBM Corp.
3a042978bSEvan Lojewski // Copyright (C) 2018 Evan Lojewski.
4a042978bSEvan Lojewski 
5a042978bSEvan Lojewski #define _GNU_SOURCE
6a042978bSEvan Lojewski #include <assert.h>
7a042978bSEvan Lojewski #include <errno.h>
8a042978bSEvan Lojewski #include <fcntl.h>
9a042978bSEvan Lojewski #include <getopt.h>
10a042978bSEvan Lojewski #include <inttypes.h>
11a042978bSEvan Lojewski #include <limits.h>
12a042978bSEvan Lojewski #include <mtd/mtd-abi.h>
13a042978bSEvan Lojewski #include <poll.h>
14a042978bSEvan Lojewski #include <signal.h>
15a042978bSEvan Lojewski #include <stdbool.h>
16a042978bSEvan Lojewski #include <stdint.h>
17a042978bSEvan Lojewski #include <stdio.h>
18a042978bSEvan Lojewski #include <stdlib.h>
19a042978bSEvan Lojewski #include <string.h>
20a042978bSEvan Lojewski #include <sys/ioctl.h>
21a042978bSEvan Lojewski #include <sys/mman.h>
22a042978bSEvan Lojewski #include <sys/stat.h>
23a042978bSEvan Lojewski #include <sys/timerfd.h>
24a042978bSEvan Lojewski #include <sys/types.h>
25a042978bSEvan Lojewski #include <syslog.h>
26a042978bSEvan Lojewski #include <time.h>
27a042978bSEvan Lojewski #include <unistd.h>
28a042978bSEvan Lojewski 
29a042978bSEvan Lojewski #include "common.h"
30a042978bSEvan Lojewski #include "backend.h"
31a042978bSEvan Lojewski #include "lpc.h"
32a042978bSEvan Lojewski #include "mboxd.h"
33a042978bSEvan Lojewski 
34a042978bSEvan Lojewski #define MIN(__x__, __y__)  (((__x__) < (__y__)) ? (__x__) : (__y__))
35a042978bSEvan Lojewski 
36a042978bSEvan Lojewski #define FILE_ERASE_SIZE (4 * 1024)
37a042978bSEvan Lojewski 
38*68a24c9eSPatrick Williams #pragma GCC diagnostic push
39*68a24c9eSPatrick Williams #pragma GCC diagnostic ignored "-Wpointer-arith"
40*68a24c9eSPatrick Williams 
41a042978bSEvan Lojewski struct file_data {
42a042978bSEvan Lojewski 	int fd;
43a042978bSEvan Lojewski };
44a042978bSEvan Lojewski 
file_dev_init(struct backend * backend,void * data)45a042978bSEvan Lojewski static int file_dev_init(struct backend *backend, void *data)
46a042978bSEvan Lojewski {
47a042978bSEvan Lojewski 	struct mtd_info_user info;
48a042978bSEvan Lojewski 	const char *path = data;
49a042978bSEvan Lojewski 	struct file_data *priv;
50a042978bSEvan Lojewski 	struct stat statbuf;
51a042978bSEvan Lojewski 	int rc = 0;
52a042978bSEvan Lojewski 
53a042978bSEvan Lojewski 	if (!path) {
54a042978bSEvan Lojewski 		MSG_ERR("No PNOR file specified\n");
55a042978bSEvan Lojewski 		return -EINVAL;
56a042978bSEvan Lojewski 	}
57a042978bSEvan Lojewski 
58a042978bSEvan Lojewski 	priv = malloc(sizeof(*priv));
59a042978bSEvan Lojewski 	if (!priv)
60a042978bSEvan Lojewski 		return -errno;
61a042978bSEvan Lojewski 
62a042978bSEvan Lojewski 	MSG_DBG("Opening %s\n", path);
63a042978bSEvan Lojewski 
64a042978bSEvan Lojewski 	priv->fd = open(path, O_RDWR);
65a042978bSEvan Lojewski 	if (priv->fd < 0) {
66a042978bSEvan Lojewski 		MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", path,
67a042978bSEvan Lojewski 			strerror(errno));
68a042978bSEvan Lojewski 		rc = -errno;
69a042978bSEvan Lojewski 		goto cleanup_data;
70a042978bSEvan Lojewski 	}
71a042978bSEvan Lojewski 
72a042978bSEvan Lojewski 	/* Don't attach to mtd devices. */
73a042978bSEvan Lojewski 	rc = ioctl(priv->fd, MEMGETINFO, &info);
74a042978bSEvan Lojewski 	if (rc != -1) {
75a042978bSEvan Lojewski 		rc = -errno;
76a042978bSEvan Lojewski 		goto cleanup_fd;
77a042978bSEvan Lojewski 	}
78a042978bSEvan Lojewski 
79a042978bSEvan Lojewski 	rc = fstat(priv->fd, &statbuf);
80a042978bSEvan Lojewski 	if (rc < 0) {
81a042978bSEvan Lojewski 		rc = -errno;
82a042978bSEvan Lojewski 		goto cleanup_fd;
83a042978bSEvan Lojewski 	}
84a042978bSEvan Lojewski 
85a042978bSEvan Lojewski 	if (backend->flash_size == 0) {
86a042978bSEvan Lojewski 		MSG_INFO("Flash size should be supplied on the commandline.\n");
87a042978bSEvan Lojewski 		backend->flash_size = statbuf.st_size;
88a042978bSEvan Lojewski 	}
89a042978bSEvan Lojewski 
90a042978bSEvan Lojewski 	/* Pick an erase size congruent with partition alignment */
91a042978bSEvan Lojewski 	backend->erase_size_shift = log_2(FILE_ERASE_SIZE);
92a042978bSEvan Lojewski 	backend->block_size_shift = backend->erase_size_shift;
93a042978bSEvan Lojewski 	MSG_DBG("Flash erase size: 0x%.8x\n", FILE_ERASE_SIZE);
94a042978bSEvan Lojewski 
95a042978bSEvan Lojewski 	backend->priv = priv;
96a042978bSEvan Lojewski 	return rc;
97a042978bSEvan Lojewski 
98a042978bSEvan Lojewski cleanup_fd:
99a042978bSEvan Lojewski 	close(priv->fd);
100a042978bSEvan Lojewski cleanup_data:
101a042978bSEvan Lojewski 	free(priv);
102a042978bSEvan Lojewski 	return rc;
103a042978bSEvan Lojewski }
104a042978bSEvan Lojewski 
file_dev_free(struct backend * backend)105a042978bSEvan Lojewski static void file_dev_free(struct backend *backend)
106a042978bSEvan Lojewski {
107a042978bSEvan Lojewski 	struct file_data *priv = backend->priv;
108a042978bSEvan Lojewski 
109a042978bSEvan Lojewski 	close(priv->fd);
110a042978bSEvan Lojewski 	free(priv);
111a042978bSEvan Lojewski }
112a042978bSEvan Lojewski 
113a042978bSEvan Lojewski /* Flash Functions */
114a042978bSEvan Lojewski 
115a042978bSEvan Lojewski /*
116a042978bSEvan Lojewski  * file_erase() - Erase the flash
117a042978bSEvan Lojewski  * @context:	The mbox context pointer
118a042978bSEvan Lojewski  * @offset:	The flash offset to erase (bytes)
119a042978bSEvan Lojewski  * @size:	The number of bytes to erase
120a042978bSEvan Lojewski  *
121a042978bSEvan Lojewski  * Return:	0 on success otherwise negative error code
122a042978bSEvan Lojewski  */
file_erase(struct backend * backend,uint32_t offset,uint32_t count)123a042978bSEvan Lojewski static int file_erase(struct backend *backend, uint32_t offset, uint32_t count)
124a042978bSEvan Lojewski {
125a042978bSEvan Lojewski 	const uint32_t erase_size = 1 << backend->erase_size_shift;
126a042978bSEvan Lojewski 	struct file_data *priv = backend->priv;
127a042978bSEvan Lojewski 	struct erase_info_user erase_info = {0};
128a042978bSEvan Lojewski 	int rc;
129a042978bSEvan Lojewski 
130a042978bSEvan Lojewski 	MSG_DBG("Erase flash @ 0x%.8x for 0x%.8x\n", offset, count);
131a042978bSEvan Lojewski 
132a042978bSEvan Lojewski 	uint8_t* erase_buf = (uint8_t*)malloc(count);
133a042978bSEvan Lojewski 	if (!erase_buf) {
134a042978bSEvan Lojewski 		MSG_ERR("Couldn't malloc erase buffer. %s\n", strerror(errno));
135a042978bSEvan Lojewski 		return -1;
136a042978bSEvan Lojewski 	}
137a042978bSEvan Lojewski 	memset(erase_buf, 0xFF, erase_size);
138a042978bSEvan Lojewski 	rc = pwrite(priv->fd, erase_buf, count, offset);
139a042978bSEvan Lojewski 	free(erase_buf);
140a042978bSEvan Lojewski 
141a042978bSEvan Lojewski 	if (rc < 0) {
142a042978bSEvan Lojewski 		MSG_ERR("Couldn't erase flash at 0x%.8x\n", erase_info.start);
143a042978bSEvan Lojewski 		return -errno;
144a042978bSEvan Lojewski 	}
145a042978bSEvan Lojewski 
146a042978bSEvan Lojewski 
147a042978bSEvan Lojewski 	return 0;
148a042978bSEvan Lojewski }
149a042978bSEvan Lojewski 
150a042978bSEvan Lojewski #define CHUNKSIZE (64 * 1024)
151a042978bSEvan Lojewski 
152a042978bSEvan Lojewski /*
153a042978bSEvan Lojewski  * file_copy() - Copy data from the flash device into a provided buffer
154a042978bSEvan Lojewski  * @context:	The backend context pointer
155a042978bSEvan Lojewski  * @offset:	The flash offset to copy from (bytes)
156a042978bSEvan Lojewski  * @mem:	The buffer to copy into (must be of atleast 'size' bytes)
157a042978bSEvan Lojewski  * @size:	The number of bytes to copy
158a042978bSEvan Lojewski  * Return:	Number of bytes copied on success, otherwise negative error
159a042978bSEvan Lojewski  *		code. file_copy will copy at most 'size' bytes, but it may
160a042978bSEvan Lojewski  *		copy less.
161a042978bSEvan Lojewski  */
file_copy(struct backend * backend,uint32_t offset,void * mem,uint32_t size)162a042978bSEvan Lojewski static int64_t file_copy(struct backend *backend, uint32_t offset,
163a042978bSEvan Lojewski 			  void *mem, uint32_t size)
164a042978bSEvan Lojewski {
165a042978bSEvan Lojewski 	struct file_data *priv = backend->priv;
166a042978bSEvan Lojewski 	int32_t size_read;
167a042978bSEvan Lojewski 	void *start = mem;
168a042978bSEvan Lojewski 
169a042978bSEvan Lojewski 	MSG_DBG("Copy flash to %p for size 0x%.8x from offset 0x%.8x\n", mem,
170a042978bSEvan Lojewski 		size, offset);
171a042978bSEvan Lojewski 	if (lseek(priv->fd, offset, SEEK_SET) != offset) {
172a042978bSEvan Lojewski 		MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset,
173a042978bSEvan Lojewski 			strerror(errno));
174a042978bSEvan Lojewski 		return -errno;
175a042978bSEvan Lojewski 	}
176a042978bSEvan Lojewski 
177a042978bSEvan Lojewski 	do {
178a042978bSEvan Lojewski 		size_read = read(priv->fd, mem, min_u32(CHUNKSIZE, size));
179a042978bSEvan Lojewski 		if (size_read < 0) {
180a042978bSEvan Lojewski 			MSG_ERR("Couldn't copy file into ram: %s\n",
181a042978bSEvan Lojewski 				strerror(errno));
182a042978bSEvan Lojewski 			return -errno;
183a042978bSEvan Lojewski 		}
184a042978bSEvan Lojewski 
185a042978bSEvan Lojewski 		size -= size_read;
186a042978bSEvan Lojewski 		mem += size_read;
187a042978bSEvan Lojewski 	} while (size && size_read);
188a042978bSEvan Lojewski 
189a042978bSEvan Lojewski 	return size_read ? mem - start : -EIO;
190a042978bSEvan Lojewski }
191a042978bSEvan Lojewski 
192a042978bSEvan Lojewski /*
193a042978bSEvan Lojewski  * file_write() - Write the flash from a provided buffer
194a042978bSEvan Lojewski  * @context:	The mbox context pointer
195a042978bSEvan Lojewski  * @offset:	The flash offset to write to (bytes)
196a042978bSEvan Lojewski  * @buf:	The buffer to write from (must be of atleast size)
197a042978bSEvan Lojewski  * @size:	The number of bytes to write
198a042978bSEvan Lojewski  *
199a042978bSEvan Lojewski  * Return:	0 on success otherwise negative error code
200a042978bSEvan Lojewski  */
file_write(struct backend * backend,uint32_t offset,void * buf,uint32_t count)201a042978bSEvan Lojewski static int file_write(struct backend *backend, uint32_t offset, void *buf,
202a042978bSEvan Lojewski 		       uint32_t count)
203a042978bSEvan Lojewski {
204a042978bSEvan Lojewski 	struct file_data *priv = backend->priv;
205a042978bSEvan Lojewski 	uint32_t buf_offset = 0;
206a042978bSEvan Lojewski 	int rc;
207a042978bSEvan Lojewski 
208a042978bSEvan Lojewski 	MSG_DBG("Write flash @ 0x%.8x for 0x%.8x from %p\n", offset, count,
209a042978bSEvan Lojewski 		buf);
210a042978bSEvan Lojewski 
211a042978bSEvan Lojewski 	if (lseek(priv->fd, offset, SEEK_SET) != offset) {
212a042978bSEvan Lojewski 		MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset,
213a042978bSEvan Lojewski 			strerror(errno));
214a042978bSEvan Lojewski 		return -errno;
215a042978bSEvan Lojewski 	}
216a042978bSEvan Lojewski 
217a042978bSEvan Lojewski 	while (count) {
218a042978bSEvan Lojewski 		rc = write(priv->fd, buf + buf_offset, count);
219a042978bSEvan Lojewski 		if (rc < 0) {
220a042978bSEvan Lojewski 			MSG_ERR("Couldn't write to file, write lost: %s\n",
221a042978bSEvan Lojewski 				strerror(errno));
222a042978bSEvan Lojewski 			return -errno;
223a042978bSEvan Lojewski 		}
224a042978bSEvan Lojewski 		count -= rc;
225a042978bSEvan Lojewski 		buf_offset += rc;
226a042978bSEvan Lojewski 	}
227a042978bSEvan Lojewski 
228a042978bSEvan Lojewski 	return 0;
229a042978bSEvan Lojewski }
230a042978bSEvan Lojewski 
231a042978bSEvan Lojewski /*
232a042978bSEvan Lojewski  * file_reset() - Reset the lpc bus mapping
233a042978bSEvan Lojewski  * @context:    The backend context pointer
234a042978bSEvan Lojewski  * @buf:	Pointer to the LPC reserved memory
235a042978bSEvan Lojewski  * @count:	The size of the LPC reserved memory
236a042978bSEvan Lojewski  *
237a042978bSEvan Lojewski  * Return:      0 on success otherwise negative error code
238a042978bSEvan Lojewski  */
file_reset(struct backend * backend,void * buf,uint32_t count)239a042978bSEvan Lojewski static int file_reset(struct backend *backend, void *buf, uint32_t count)
240a042978bSEvan Lojewski {
241a042978bSEvan Lojewski 	struct file_data *priv = backend->priv;
242a042978bSEvan Lojewski 	size_t len;
243a042978bSEvan Lojewski 	int rc;
244a042978bSEvan Lojewski 
245a042978bSEvan Lojewski 	len = MIN(backend->flash_size, count);
246a042978bSEvan Lojewski 
247a042978bSEvan Lojewski 	/* Ugh, otherwise we need to parse the FFS image */
248a042978bSEvan Lojewski 	assert(len == backend->flash_size);
249a042978bSEvan Lojewski 
250a042978bSEvan Lojewski 	/* Preload Flash contents into memory window */
251a042978bSEvan Lojewski 	rc = pread(priv->fd, buf, len, 0);
252a042978bSEvan Lojewski 	if (rc < 0)
253a042978bSEvan Lojewski 		return -errno;
254a042978bSEvan Lojewski 
255a042978bSEvan Lojewski 	return reset_lpc_memory;
256a042978bSEvan Lojewski }
257a042978bSEvan Lojewski 
258a042978bSEvan Lojewski static const struct backend_ops file_ops = {
259a042978bSEvan Lojewski 	.init = file_dev_init,
260a042978bSEvan Lojewski 	.free = file_dev_free,
261a042978bSEvan Lojewski 	.copy = file_copy,
262a042978bSEvan Lojewski 	.erase = file_erase,
263a042978bSEvan Lojewski 	.write = file_write,
264a042978bSEvan Lojewski 	.reset = file_reset,
265a042978bSEvan Lojewski 	.validate = NULL,
2668cef63e3SAlvin Wang 	.align_offset = NULL,
267a042978bSEvan Lojewski };
268a042978bSEvan Lojewski 
backend_get_file(void)269a042978bSEvan Lojewski struct backend backend_get_file(void)
270a042978bSEvan Lojewski {
271a042978bSEvan Lojewski 	struct backend be = {0};
272a042978bSEvan Lojewski 
273a042978bSEvan Lojewski 	be.ops = &file_ops;
274a042978bSEvan Lojewski 
275a042978bSEvan Lojewski 	return be;
276a042978bSEvan Lojewski }
277a042978bSEvan Lojewski 
backend_probe_file(struct backend * master,const char * path)278a042978bSEvan Lojewski int backend_probe_file(struct backend *master, const char *path)
279a042978bSEvan Lojewski {
280a042978bSEvan Lojewski 	struct backend with;
281a042978bSEvan Lojewski 
282a042978bSEvan Lojewski 	assert(master);
283a042978bSEvan Lojewski 	with = backend_get_file();
284a042978bSEvan Lojewski 
285a042978bSEvan Lojewski 	return backend_init(master, &with, (void *)path);
286a042978bSEvan Lojewski }
287*68a24c9eSPatrick Williams 
288*68a24c9eSPatrick Williams #pragma GCC diagnostic pop
289