blob: 4b6eb674227451cf198690c9b8195574dd5d1b88 [file] [log] [blame]
Cyril Burc85e34d2016-11-15 11:50:41 +11001/* Copyright 2016 IBM
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 *
15 */
16
17#include <assert.h>
18#include <errno.h>
19#include <fcntl.h>
20#include <getopt.h>
21#include <limits.h>
22#include <poll.h>
23#include <stdbool.h>
24#include <stdint.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
28#include <syslog.h>
Michael Neuling899ebac2017-01-14 11:20:26 -060029#include <signal.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110030#include <sys/ioctl.h>
31#include <sys/mman.h>
32#include <sys/stat.h>
33#include <sys/timerfd.h>
34#include <sys/types.h>
35#include <time.h>
36#include <unistd.h>
Andrew Jeffery78210b92017-01-13 13:06:09 +103037#include <inttypes.h>
Cyril Burc85e34d2016-11-15 11:50:41 +110038
39#include <mtd/mtd-abi.h>
40
41#include <linux/aspeed-lpc-ctrl.h>
42
43#include "mbox.h"
44#include "common.h"
45
46#define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl"
47
Michael Neuling622992b2017-01-17 11:09:15 -060048
49/* Put pulled fds first */
Cyril Burc85e34d2016-11-15 11:50:41 +110050#define MBOX_FD 0
Michael Neuling622992b2017-01-17 11:09:15 -060051#define POLL_FDS 1
Cyril Burc85e34d2016-11-15 11:50:41 +110052#define LPC_CTRL_FD 1
53#define MTD_FD 2
54#define TOTAL_FDS 3
55
56#define ALIGN_UP(_v, _a) (((_v) + (_a) - 1) & ~((_a) - 1))
57
58#define MSG_OUT(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0)
59#define MSG_ERR(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0)
60
61#define BOOT_HICR7 0x30000e00U
62#define BOOT_HICR8 0xfe0001ffU
63
64struct mbox_context {
65 struct pollfd fds[TOTAL_FDS];
66 void *lpc_mem;
67 uint32_t base;
68 uint32_t size;
69 uint32_t pgsize;
70 bool dirty;
71 uint32_t dirtybase;
72 uint32_t dirtysize;
73 struct mtd_info_user mtd_info;
Cyril Bure8f2de12017-01-17 16:56:02 +110074 uint32_t flash_size;
Cyril Burc85e34d2016-11-15 11:50:41 +110075};
76
77static int running = 1;
Michael Neuling899ebac2017-01-14 11:20:26 -060078static int sighup = 0;
Cyril Burc85e34d2016-11-15 11:50:41 +110079
Cyril Bur46233672017-01-16 13:33:26 +110080static int point_to_flash(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +110081{
Cyril Bur46233672017-01-16 13:33:26 +110082 struct aspeed_lpc_ctrl_mapping map;
83 int r = 0;
84
Cyril Burc85e34d2016-11-15 11:50:41 +110085 /*
Cyril Bur46233672017-01-16 13:33:26 +110086 * Point it to the real flash for sanity.
Cyril Burc85e34d2016-11-15 11:50:41 +110087 *
Cyril Bur46233672017-01-16 13:33:26 +110088 * This function assumes 32MB of flash which means that that
89 * hostboot expects flash to be at 0x0e000000 - 0x0fffffff on the
90 * LPC bus. If the machine actually has 64MB of flash then the
91 * map.addr should be 0x0c000000. TODO
Cyril Burc85e34d2016-11-15 11:50:41 +110092 *
93 * Until hostboot learns how to talk to this daemon this hardcode will
94 * get hostboot going. Furthermore, when hostboot does learn to talk
95 * then this mapping is unnecessary and this code should be removed.
96 */
97
Cyril Bure8f2de12017-01-17 16:56:02 +110098 /*
99 * The mask is because the top nibble is the host LPC FW space, we
100 * want space 0
101 */
102 map.addr = (0UL - context->flash_size) & 0x0fffffff;
103 map.size = context->flash_size;
Cyril Bur46233672017-01-16 13:33:26 +1100104 map.offset = 0;
105 map.window_type = ASPEED_LPC_CTRL_WINDOW_FLASH;
106 map.window_id = 0; /* Theres only one */
Cyril Burc85e34d2016-11-15 11:50:41 +1100107
108 MSG_OUT("Pointing HOST LPC bus at the actual flash\n");
Cyril Bure8f2de12017-01-17 16:56:02 +1100109 MSG_OUT("Assuming %dMB of flash: HOST LPC 0x%08x\n", context->flash_size >> 20,
110 map.addr);
Cyril Burc85e34d2016-11-15 11:50:41 +1100111
Michael Neuling899ebac2017-01-14 11:20:26 -0600112 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) {
Cyril Bur46233672017-01-16 13:33:26 +1100113 r = -errno;
114 MSG_ERR("Couldn't MAP the host LPC bus to the platform flash\n");
115 }
116
Cyril Burc85e34d2016-11-15 11:50:41 +1100117 return r;
118}
119
120static int flash_write(struct mbox_context *context, uint32_t pos, uint32_t len)
121{
122 int rc;
123 struct erase_info_user erase_info = {
124 .start = pos,
125 };
126
127 assert(context);
128
129 erase_info.length = ALIGN_UP(len, context->mtd_info.erasesize);
130
131 MSG_OUT("Erasing 0x%08x for 0x%08x (aligned: 0x%08x)\n", pos, len, erase_info.length);
Michael Neuling622992b2017-01-17 11:09:15 -0600132 if (ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100133 MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno));
134 return -1;
135 }
136
Michael Neuling622992b2017-01-17 11:09:15 -0600137 if (lseek(context->fds[MTD_FD].fd, pos, SEEK_SET) == (off_t) -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100138 MSG_ERR("Couldn't seek to 0x%08x into MTD, flash write lost: %s\n", pos, strerror(errno));
139 return -1;
140 }
141
142 while (erase_info.length) {
Michael Neuling622992b2017-01-17 11:09:15 -0600143 rc = write(context->fds[MTD_FD].fd, context->lpc_mem + pos, erase_info.length);
Cyril Burc85e34d2016-11-15 11:50:41 +1100144 if (rc == -1) {
145 MSG_ERR("Couldn't write to flash! Flash write lost: %s\n", strerror(errno));
146 return -1;
147 }
148 erase_info.length -= rc;
149 pos += rc;
150 }
151
152 return 0;
153}
154
155/* TODO: Add come consistency around the daemon exiting and either
156 * way, ensuring it responds.
157 * I'm in favour of an approach where it does its best to stay alive
158 * and keep talking, the hacky prototype was written the other way.
159 * This function is now inconsistent
160 */
161static int dispatch_mbox(struct mbox_context *context)
162{
163 int r = 0;
164 int len;
165 off_t pos;
166 uint8_t byte;
167 union mbox_regs resp, req = { 0 };
168 uint16_t sizepg, basepg, dirtypg;
169 uint32_t dirtycount;
170 struct aspeed_lpc_ctrl_mapping map;
171
172 assert(context);
173
174 map.addr = context->base;
175 map.size = context->size;
176 map.offset = 0;
177 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
178 map.window_id = 0; /* Theres only one */
179
180 MSG_OUT("Dispatched to mbox\n");
181 r = read(context->fds[MBOX_FD].fd, &req, sizeof(req.raw));
182 if (r < 0) {
183 r = -errno;
184 MSG_ERR("Couldn't read: %s\n", strerror(errno));
185 goto out;
186 }
187 if (r < sizeof(req.msg)) {
188 MSG_ERR("Short read: %d expecting %zu\n", r, sizeof(req.msg));
189 r = -1;
190 goto out;
191 }
192
193 /* We are NOT going to update the last two 'status' bytes */
194 memcpy(&resp, &req, sizeof(req.msg));
195
196 sizepg = context->size >> context->pgsize;
197 basepg = context->base >> context->pgsize;
198 MSG_OUT("Got data in with command %d\n", req.msg.command);
199 switch (req.msg.command) {
200 case MBOX_C_RESET_STATE:
201 /* Called by early hostboot? TODO */
202 resp.msg.response = MBOX_R_SUCCESS;
Cyril Bur46233672017-01-16 13:33:26 +1100203 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100204 if (r) {
205 resp.msg.response = MBOX_R_SYSTEM_ERROR;
206 MSG_ERR("Couldn't point the LPC BUS back to actual flash\n");
207 }
208 break;
209 case MBOX_C_GET_MBOX_INFO:
210 /* TODO Freak if data.data[0] isn't 1 */
211 resp.msg.data[0] = 1;
212 put_u16(&resp.msg.data[1], sizepg);
213 put_u16(&resp.msg.data[3], sizepg);
214 resp.msg.response = MBOX_R_SUCCESS;
215 /* Wow that can't stay negated thats horrible */
216 MSG_OUT("LPC_CTRL_IOCTL_MAP to 0x%08x for 0x%08x\n", map.addr, map.size);
Michael Neuling622992b2017-01-17 11:09:15 -0600217 r = ioctl(context->fds[LPC_CTRL_FD].fd,
Cyril Burc85e34d2016-11-15 11:50:41 +1100218 ASPEED_LPC_CTRL_IOCTL_MAP, &map);
219 if (r < 0) {
220 r = -errno;
221 resp.msg.response = MBOX_R_SYSTEM_ERROR;
222 MSG_ERR("Couldn't MAP ioctl(): %s\n", strerror(errno));
223 }
224 break;
225 case MBOX_C_GET_FLASH_INFO:
226 put_u32(&resp.msg.data[0], context->mtd_info.size);
227 put_u32(&resp.msg.data[4], context->mtd_info.erasesize);
228 resp.msg.response = MBOX_R_SUCCESS;
229 break;
230 case MBOX_C_READ_WINDOW:
231 /*
232 * We could probably play tricks with LPC mapping.
233 * That would require kernel involvement.
234 * We could also always copy the relevant flash part to
235 * context->base even if it turns out that offset is in
236 * the window...
237 * This approach is easiest.
238 */
Andrew Jeffery78210b92017-01-13 13:06:09 +1030239 if (context->dirty) {
Michael Neuling622992b2017-01-17 11:09:15 -0600240 r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size);
Andrew Jeffery78210b92017-01-13 13:06:09 +1030241 if (r != context->size) {
242 MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size);
243 goto out;
244 }
245 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100246 basepg += get_u16(&req.msg.data[0]);
247 put_u16(&resp.msg.data[0], basepg);
248 resp.msg.response = MBOX_R_SUCCESS;
249 context->dirty = false;
250 break;
251 case MBOX_C_CLOSE_WINDOW:
252 context->dirty = true;
253 break;
254 case MBOX_C_WRITE_WINDOW:
255 basepg += get_u16(&req.msg.data[0]);
256 put_u16(&resp.msg.data[0], basepg);
257 resp.msg.response = MBOX_R_SUCCESS;
258 context->dirtybase = basepg << context->pgsize;
259 break;
260 /* Optimise these later */
261 case MBOX_C_WRITE_DIRTY:
262 case MBOX_C_WRITE_FENCE:
263 dirtypg = get_u16(&req.msg.data[0]);
264 dirtycount = get_u32(&req.msg.data[2]);
265 if (dirtycount == 0) {
266 resp.msg.response = MBOX_R_PARAM_ERROR;
267 break;
268 }
269 /*
270 * dirtypg is actually offset within window so we probs
271 * need to know if the window isn't at zero
272 */
273 if (flash_write(context, dirtypg << context->pgsize, dirtycount) != 0) {
274 resp.msg.response = MBOX_R_WRITE_ERROR;
275 break;
276 }
277 resp.msg.response = MBOX_R_SUCCESS;
278 break;
279 case MBOX_C_ACK:
280 resp.msg.response = MBOX_R_SUCCESS;
281 pos = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_BYTE, SEEK_SET);
282 if (pos != MBOX_BMC_BYTE) {
283 r = -errno;
284 MSG_ERR("Couldn't lseek() to byte %d: %s\n", MBOX_BMC_BYTE,
285 strerror(errno));
286 }
287 /*
288 * NAND what is in the hardware and the request.
289 * This prevents the host being able to SET bits, it can
290 * only request set ones be cleared.
291 */
292 byte = ~(req.msg.data[0] & req.raw[MBOX_BMC_BYTE]);
293 len = write(context->fds[MBOX_FD].fd, &byte, 1);
294 if (len != 1) {
295 r = -errno;
296 MSG_ERR("Couldn't write to BMC status reg: %s\n",
297 strerror(errno));
298 }
299 pos = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET);
300 if (pos != 0) {
301 r = -errno;
302 MSG_ERR("Couldn't reset MBOX offset to zero\n");
303 }
304 break;
305 case MBOX_C_COMPLETED_COMMANDS:
306 /* This implementation always completes before responding */
307 resp.msg.data[0] = 0;
308 resp.msg.response = MBOX_R_SUCCESS;
309 break;
310 default:
311 MSG_ERR("UNKNOWN MBOX COMMAND\n");
312 resp.msg.response = MBOX_R_PARAM_ERROR;
313 r = -1;
314 }
315
316 MSG_OUT("Writing response to MBOX regs\n");
317 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp.msg));
318 if (len < sizeof(resp.msg)) {
319 r = -errno;
320 MSG_ERR("Didn't write the full response\n");
321 }
322
323out:
324 return r;
325}
326
Michael Neuling899ebac2017-01-14 11:20:26 -0600327int copy_flash(struct mbox_context *context)
328{
329 int r;
330
331 /*
332 * Copy flash into RAM early, same time.
333 * The kernel has created the LPC->AHB mapping also, which means
334 * flash should work.
335 * Ideally we tell the kernel whats up and when to do stuff...
336 */
337 MSG_OUT("Loading flash into ram at %p for 0x%08x bytes\n",
338 context->lpc_mem, context->size);
339 if (lseek(context->fds[MTD_FD].fd, 0, SEEK_SET) != 0) {
340 r = -errno;
341 MSG_ERR("Couldn't reset MBOX pos to zero\n");
342 return r;
343 }
344 r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size);
345 if (r != context->size) {
346 r = -errno;
347 MSG_ERR("Couldn't copy mtd into ram: %d\n", r);
348 return r;
349 }
350 return 0;
351}
352
353void signal_hup(int signum, siginfo_t *info, void *uc)
354{
355 sighup = 1;
356}
357
Cyril Burc85e34d2016-11-15 11:50:41 +1100358static void usage(const char *name)
359{
Cyril Bure8f2de12017-01-17 16:56:02 +1100360 fprintf(stderr, "Usage %s [ -v[v] | --syslog ] --flash=size[K | M]\n", name);
361 fprintf(stderr, "\t--flash size[K | M]\t Map the flash for the according to 'size' in Kilobytes or Megabytes\n");
Cyril Burc85e34d2016-11-15 11:50:41 +1100362 fprintf(stderr, "\t--verbose\t Be [more] verbose\n");
363 fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without -v)\n\n");
364}
365
366int main(int argc, char *argv[])
367{
368 struct mbox_context *context;
369 const char *name = argv[0];
370 char *pnor_filename = NULL;
371 int opt, polled, r, i;
372 struct aspeed_lpc_ctrl_mapping map;
Michael Neuling899ebac2017-01-14 11:20:26 -0600373 struct sigaction act;
Cyril Bure8f2de12017-01-17 16:56:02 +1100374 char *endptr;
Cyril Burc85e34d2016-11-15 11:50:41 +1100375
376 static const struct option long_options[] = {
Cyril Bure8f2de12017-01-17 16:56:02 +1100377 { "flash", required_argument, 0, 'f' },
378 { "verbose", no_argument, 0, 'v' },
379 { "syslog", no_argument, 0, 's' },
380 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100381 };
382
383 context = calloc(1, sizeof(*context));
384 for (i = 0; i < TOTAL_FDS; i++)
385 context->fds[i].fd = -1;
386
387 mbox_vlog = &mbox_log_console;
Cyril Bure8f2de12017-01-17 16:56:02 +1100388 while ((opt = getopt_long(argc, argv, "fv", long_options, NULL)) != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100389 switch (opt) {
390 case 0:
391 break;
Cyril Bure8f2de12017-01-17 16:56:02 +1100392 case 'f':
393 context->flash_size = strtol(optarg, &endptr, 0);
394 if (optarg == endptr) {
395 fprintf(stderr, "Unparseable flash size\n");
396 usage(name);
397 exit(EXIT_FAILURE);
398 }
399 if (*endptr == 'K') {
400 context->flash_size <<= 10;
401 } else if (*endptr == 'M') {
402 context->flash_size <<= 20;
403 } else if (*endptr != '\0') { /* Unknown units */
404 fprintf(stderr, "Unknown units '%c'\n", *endptr);
405 usage(name);
406 exit(EXIT_FAILURE);
407 }
408 break;
Cyril Burc85e34d2016-11-15 11:50:41 +1100409 case 'v':
410 verbosity++;
411 break;
412 case 's':
413 /* Avoid a double openlog() */
414 if (mbox_vlog != &vsyslog) {
415 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
416 mbox_vlog = &vsyslog;
417 }
418 break;
419 default:
420 usage(name);
421 exit(EXIT_FAILURE);
422 }
423 }
424
Cyril Bure8f2de12017-01-17 16:56:02 +1100425 if (context->flash_size == 0) {
426 fprintf(stderr, "Must specify a non-zero flash size\n");
427 usage(name);
428 exit(EXIT_FAILURE);
429 }
430
Cyril Burc85e34d2016-11-15 11:50:41 +1100431 if (verbosity == MBOX_LOG_VERBOSE)
432 MSG_OUT("Verbose logging\n");
433
434 if (verbosity == MBOX_LOG_DEBUG)
435 MSG_OUT("Debug logging\n");
436
Michael Neuling899ebac2017-01-14 11:20:26 -0600437 MSG_OUT("Registering SigHUP hander\n");
438 act.sa_sigaction = signal_hup;
439 sigemptyset(&act.sa_mask);
440 act.sa_flags = SA_SIGINFO;
441 if (sigaction(SIGHUP, &act, NULL) < 0) {
442 perror("Registering SIGHUP");
443 exit(1);
444 }
445 sighup = 0;
446
Cyril Burc85e34d2016-11-15 11:50:41 +1100447 MSG_OUT("Starting\n");
448
449 MSG_OUT("Opening %s\n", MBOX_HOST_PATH);
450 context->fds[MBOX_FD].fd = open(MBOX_HOST_PATH, O_RDWR | O_NONBLOCK);
451 if (context->fds[MBOX_FD].fd < 0) {
452 r = -errno;
453 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
454 MBOX_HOST_PATH, strerror(errno));
455 goto finish;
456 }
457
458 MSG_OUT("Opening %s\n", LPC_CTRL_PATH);
459 context->fds[LPC_CTRL_FD].fd = open(LPC_CTRL_PATH, O_RDWR | O_SYNC);
460 if (context->fds[LPC_CTRL_FD].fd < 0) {
461 r = -errno;
462 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
463 LPC_CTRL_PATH, strerror(errno));
464 goto finish;
465 }
466
467 MSG_OUT("Getting buffer size...\n");
468 /* This may become more variable in the future */
469 context->pgsize = 12; /* 4K */
470 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
471 map.window_id = 0; /* Theres only one */
472 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE,
473 &map) < 0) {
474 r = -errno;
475 MSG_OUT("fail\n");
476 MSG_ERR("Couldn't get lpc control buffer size: %s\n", strerror(-r));
477 goto finish;
478 }
479 /* And strip the first nibble, LPC access speciality */
480 context->size = map.size;
481 context->base = -context->size & 0x0FFFFFFF;
482
483 /* READ THE COMMENT AT THE START OF THIS FUNCTION! */
Cyril Bur46233672017-01-16 13:33:26 +1100484 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100485 if (r) {
486 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
487 strerror(-r));
488 goto finish;
489 }
490
491 MSG_OUT("Mapping %s for %u\n", LPC_CTRL_PATH, context->size);
492 context->lpc_mem = mmap(NULL, context->size, PROT_READ | PROT_WRITE, MAP_SHARED,
493 context->fds[LPC_CTRL_FD].fd, 0);
494 if (context->lpc_mem == MAP_FAILED) {
495 r = -errno;
496 MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH, strerror(errno));
497 goto finish;
498 }
499
500 pnor_filename = get_dev_mtd();
501 if (!pnor_filename) {
502 MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n");
503 r = -1;
504 goto finish;
505 }
506
507 MSG_OUT("Opening %s\n", pnor_filename);
508 context->fds[MTD_FD].fd = open(pnor_filename, O_RDWR);
509 if (context->fds[MTD_FD].fd < 0) {
510 r = -errno;
511 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
512 pnor_filename, strerror(errno));
513 goto finish;
514 }
515
516 if (ioctl(context->fds[MTD_FD].fd, MEMGETINFO, &context->mtd_info) == -1) {
517 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
518 return -1;
519 }
520
Michael Neuling899ebac2017-01-14 11:20:26 -0600521 if (copy_flash(context))
Cyril Burc85e34d2016-11-15 11:50:41 +1100522 goto finish;
Cyril Burc85e34d2016-11-15 11:50:41 +1100523
524 context->fds[MBOX_FD].events = POLLIN;
Cyril Burc85e34d2016-11-15 11:50:41 +1100525
Cyril Burd8f6d7a2017-01-10 18:11:16 +1100526 /* Test the single write facility by setting all the regs to 0xFF */
527 MSG_OUT("Setting all MBOX regs to 0xff individually...\n");
528 for (i = 0; i < MBOX_REG_BYTES; i++) {
529 uint8_t byte = 0xff;
530 off_t pos;
531 int len;
532
533 pos = lseek(context->fds[MBOX_FD].fd, i, SEEK_SET);
534 if (pos != i) {
535 MSG_ERR("Couldn't lseek() to byte %d: %s\n", i,
536 strerror(errno));
537 break;
538 }
539 len = write(context->fds[MBOX_FD].fd, &byte, 1);
540 if (len != 1) {
541 MSG_ERR("Couldn't write MBOX reg %d: %s\n", i,
542 strerror(errno));
543 break;
544 }
545 }
546 if (lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET) != 0) {
547 r = -errno;
548 MSG_ERR("Couldn't reset MBOX pos to zero\n");
549 goto finish;
550 }
551
Cyril Burc85e34d2016-11-15 11:50:41 +1100552 MSG_OUT("Entering polling loop\n");
553 while (running) {
Michael Neuling622992b2017-01-17 11:09:15 -0600554 polled = poll(context->fds, POLL_FDS, 1000);
Cyril Burc85e34d2016-11-15 11:50:41 +1100555 if (polled == 0)
556 continue;
Michael Neuling899ebac2017-01-14 11:20:26 -0600557 if ((polled == -1) && (errno != -EINTR) && (sighup == 1)) {
558 /* Got sighup. reset to point to flash and
559 * reread flash */
Michael Neuling899ebac2017-01-14 11:20:26 -0600560 r = point_to_flash(context);
561 if (r) {
562 goto finish;
563 }
Michael Neuling899ebac2017-01-14 11:20:26 -0600564 r = copy_flash(context);
565 if (r)
566 goto finish;
Michael Neuling899ebac2017-01-14 11:20:26 -0600567 sighup = 0;
568 continue;
569 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100570 if (polled < 0) {
571 r = -errno;
572 MSG_ERR("Error from poll(): %s\n", strerror(errno));
573 break;
574 }
575 r = dispatch_mbox(context);
576 if (r < 0) {
577 MSG_ERR("Error handling MBOX event: %s\n", strerror(-r));
578 break;
579 }
580 }
581
582 MSG_OUT("Exiting\n");
583
Cyril Burc85e34d2016-11-15 11:50:41 +1100584finish:
585 if (context->lpc_mem)
586 munmap(context->lpc_mem, context->size);
587
588 free(pnor_filename);
589 close(context->fds[MTD_FD].fd);
590 close(context->fds[LPC_CTRL_FD].fd);
591 close(context->fds[MBOX_FD].fd);
592 free(context);
593
594 return r;
595}
596