blob: ad2e2c7ae6e601f889394adccfc261e83ddfc4a4 [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
48#define MBOX_FD 0
49#define LPC_CTRL_FD 1
50#define MTD_FD 2
51#define TOTAL_FDS 3
52
53#define ALIGN_UP(_v, _a) (((_v) + (_a) - 1) & ~((_a) - 1))
54
55#define MSG_OUT(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0)
56#define MSG_ERR(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0)
57
58#define BOOT_HICR7 0x30000e00U
59#define BOOT_HICR8 0xfe0001ffU
60
61struct mbox_context {
62 struct pollfd fds[TOTAL_FDS];
63 void *lpc_mem;
64 uint32_t base;
65 uint32_t size;
66 uint32_t pgsize;
67 bool dirty;
68 uint32_t dirtybase;
69 uint32_t dirtysize;
70 struct mtd_info_user mtd_info;
Cyril Bure8f2de12017-01-17 16:56:02 +110071 uint32_t flash_size;
Cyril Burc85e34d2016-11-15 11:50:41 +110072};
73
74static int running = 1;
Michael Neuling899ebac2017-01-14 11:20:26 -060075static int sighup = 0;
Cyril Burc85e34d2016-11-15 11:50:41 +110076
Cyril Bur46233672017-01-16 13:33:26 +110077static int point_to_flash(struct mbox_context *context)
Cyril Burc85e34d2016-11-15 11:50:41 +110078{
Cyril Bur46233672017-01-16 13:33:26 +110079 struct aspeed_lpc_ctrl_mapping map;
80 int r = 0;
81
Cyril Burc85e34d2016-11-15 11:50:41 +110082 /*
Cyril Bur46233672017-01-16 13:33:26 +110083 * Point it to the real flash for sanity.
Cyril Burc85e34d2016-11-15 11:50:41 +110084 *
Cyril Bur46233672017-01-16 13:33:26 +110085 * This function assumes 32MB of flash which means that that
86 * hostboot expects flash to be at 0x0e000000 - 0x0fffffff on the
87 * LPC bus. If the machine actually has 64MB of flash then the
88 * map.addr should be 0x0c000000. TODO
Cyril Burc85e34d2016-11-15 11:50:41 +110089 *
90 * Until hostboot learns how to talk to this daemon this hardcode will
91 * get hostboot going. Furthermore, when hostboot does learn to talk
92 * then this mapping is unnecessary and this code should be removed.
93 */
94
Cyril Bure8f2de12017-01-17 16:56:02 +110095 /*
96 * The mask is because the top nibble is the host LPC FW space, we
97 * want space 0
98 */
99 map.addr = (0UL - context->flash_size) & 0x0fffffff;
100 map.size = context->flash_size;
Cyril Bur46233672017-01-16 13:33:26 +1100101 map.offset = 0;
102 map.window_type = ASPEED_LPC_CTRL_WINDOW_FLASH;
103 map.window_id = 0; /* Theres only one */
Cyril Burc85e34d2016-11-15 11:50:41 +1100104
105 MSG_OUT("Pointing HOST LPC bus at the actual flash\n");
Cyril Bure8f2de12017-01-17 16:56:02 +1100106 MSG_OUT("Assuming %dMB of flash: HOST LPC 0x%08x\n", context->flash_size >> 20,
107 map.addr);
Cyril Burc85e34d2016-11-15 11:50:41 +1100108
Michael Neuling899ebac2017-01-14 11:20:26 -0600109 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) {
Cyril Bur46233672017-01-16 13:33:26 +1100110 r = -errno;
111 MSG_ERR("Couldn't MAP the host LPC bus to the platform flash\n");
112 }
113
Cyril Burc85e34d2016-11-15 11:50:41 +1100114 return r;
115}
116
117static int flash_write(struct mbox_context *context, uint32_t pos, uint32_t len)
118{
119 int rc;
120 struct erase_info_user erase_info = {
121 .start = pos,
122 };
123
124 assert(context);
125
126 erase_info.length = ALIGN_UP(len, context->mtd_info.erasesize);
127
128 MSG_OUT("Erasing 0x%08x for 0x%08x (aligned: 0x%08x)\n", pos, len, erase_info.length);
129 if (ioctl(-context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) {
130 MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno));
131 return -1;
132 }
133
134 if (lseek(-context->fds[MTD_FD].fd, pos, SEEK_SET) == (off_t) -1) {
135 MSG_ERR("Couldn't seek to 0x%08x into MTD, flash write lost: %s\n", pos, strerror(errno));
136 return -1;
137 }
138
139 while (erase_info.length) {
140 rc = write(-context->fds[MTD_FD].fd, context->lpc_mem + pos, erase_info.length);
141 if (rc == -1) {
142 MSG_ERR("Couldn't write to flash! Flash write lost: %s\n", strerror(errno));
143 return -1;
144 }
145 erase_info.length -= rc;
146 pos += rc;
147 }
148
149 return 0;
150}
151
152/* TODO: Add come consistency around the daemon exiting and either
153 * way, ensuring it responds.
154 * I'm in favour of an approach where it does its best to stay alive
155 * and keep talking, the hacky prototype was written the other way.
156 * This function is now inconsistent
157 */
158static int dispatch_mbox(struct mbox_context *context)
159{
160 int r = 0;
161 int len;
162 off_t pos;
163 uint8_t byte;
164 union mbox_regs resp, req = { 0 };
165 uint16_t sizepg, basepg, dirtypg;
166 uint32_t dirtycount;
167 struct aspeed_lpc_ctrl_mapping map;
168
169 assert(context);
170
171 map.addr = context->base;
172 map.size = context->size;
173 map.offset = 0;
174 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
175 map.window_id = 0; /* Theres only one */
176
177 MSG_OUT("Dispatched to mbox\n");
178 r = read(context->fds[MBOX_FD].fd, &req, sizeof(req.raw));
179 if (r < 0) {
180 r = -errno;
181 MSG_ERR("Couldn't read: %s\n", strerror(errno));
182 goto out;
183 }
184 if (r < sizeof(req.msg)) {
185 MSG_ERR("Short read: %d expecting %zu\n", r, sizeof(req.msg));
186 r = -1;
187 goto out;
188 }
189
190 /* We are NOT going to update the last two 'status' bytes */
191 memcpy(&resp, &req, sizeof(req.msg));
192
193 sizepg = context->size >> context->pgsize;
194 basepg = context->base >> context->pgsize;
195 MSG_OUT("Got data in with command %d\n", req.msg.command);
196 switch (req.msg.command) {
197 case MBOX_C_RESET_STATE:
198 /* Called by early hostboot? TODO */
199 resp.msg.response = MBOX_R_SUCCESS;
Cyril Bur46233672017-01-16 13:33:26 +1100200 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100201 if (r) {
202 resp.msg.response = MBOX_R_SYSTEM_ERROR;
203 MSG_ERR("Couldn't point the LPC BUS back to actual flash\n");
204 }
205 break;
206 case MBOX_C_GET_MBOX_INFO:
207 /* TODO Freak if data.data[0] isn't 1 */
208 resp.msg.data[0] = 1;
209 put_u16(&resp.msg.data[1], sizepg);
210 put_u16(&resp.msg.data[3], sizepg);
211 resp.msg.response = MBOX_R_SUCCESS;
212 /* Wow that can't stay negated thats horrible */
213 MSG_OUT("LPC_CTRL_IOCTL_MAP to 0x%08x for 0x%08x\n", map.addr, map.size);
214 r = ioctl(-context->fds[LPC_CTRL_FD].fd,
215 ASPEED_LPC_CTRL_IOCTL_MAP, &map);
216 if (r < 0) {
217 r = -errno;
218 resp.msg.response = MBOX_R_SYSTEM_ERROR;
219 MSG_ERR("Couldn't MAP ioctl(): %s\n", strerror(errno));
220 }
221 break;
222 case MBOX_C_GET_FLASH_INFO:
223 put_u32(&resp.msg.data[0], context->mtd_info.size);
224 put_u32(&resp.msg.data[4], context->mtd_info.erasesize);
225 resp.msg.response = MBOX_R_SUCCESS;
226 break;
227 case MBOX_C_READ_WINDOW:
228 /*
229 * We could probably play tricks with LPC mapping.
230 * That would require kernel involvement.
231 * We could also always copy the relevant flash part to
232 * context->base even if it turns out that offset is in
233 * the window...
234 * This approach is easiest.
235 */
Andrew Jeffery78210b92017-01-13 13:06:09 +1030236 if (context->dirty) {
237 r = read(-context->fds[MTD_FD].fd, context->lpc_mem, context->size);
238 if (r != context->size) {
239 MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size);
240 goto out;
241 }
242 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100243 basepg += get_u16(&req.msg.data[0]);
244 put_u16(&resp.msg.data[0], basepg);
245 resp.msg.response = MBOX_R_SUCCESS;
246 context->dirty = false;
247 break;
248 case MBOX_C_CLOSE_WINDOW:
249 context->dirty = true;
250 break;
251 case MBOX_C_WRITE_WINDOW:
252 basepg += get_u16(&req.msg.data[0]);
253 put_u16(&resp.msg.data[0], basepg);
254 resp.msg.response = MBOX_R_SUCCESS;
255 context->dirtybase = basepg << context->pgsize;
256 break;
257 /* Optimise these later */
258 case MBOX_C_WRITE_DIRTY:
259 case MBOX_C_WRITE_FENCE:
260 dirtypg = get_u16(&req.msg.data[0]);
261 dirtycount = get_u32(&req.msg.data[2]);
262 if (dirtycount == 0) {
263 resp.msg.response = MBOX_R_PARAM_ERROR;
264 break;
265 }
266 /*
267 * dirtypg is actually offset within window so we probs
268 * need to know if the window isn't at zero
269 */
270 if (flash_write(context, dirtypg << context->pgsize, dirtycount) != 0) {
271 resp.msg.response = MBOX_R_WRITE_ERROR;
272 break;
273 }
274 resp.msg.response = MBOX_R_SUCCESS;
275 break;
276 case MBOX_C_ACK:
277 resp.msg.response = MBOX_R_SUCCESS;
278 pos = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_BYTE, SEEK_SET);
279 if (pos != MBOX_BMC_BYTE) {
280 r = -errno;
281 MSG_ERR("Couldn't lseek() to byte %d: %s\n", MBOX_BMC_BYTE,
282 strerror(errno));
283 }
284 /*
285 * NAND what is in the hardware and the request.
286 * This prevents the host being able to SET bits, it can
287 * only request set ones be cleared.
288 */
289 byte = ~(req.msg.data[0] & req.raw[MBOX_BMC_BYTE]);
290 len = write(context->fds[MBOX_FD].fd, &byte, 1);
291 if (len != 1) {
292 r = -errno;
293 MSG_ERR("Couldn't write to BMC status reg: %s\n",
294 strerror(errno));
295 }
296 pos = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET);
297 if (pos != 0) {
298 r = -errno;
299 MSG_ERR("Couldn't reset MBOX offset to zero\n");
300 }
301 break;
302 case MBOX_C_COMPLETED_COMMANDS:
303 /* This implementation always completes before responding */
304 resp.msg.data[0] = 0;
305 resp.msg.response = MBOX_R_SUCCESS;
306 break;
307 default:
308 MSG_ERR("UNKNOWN MBOX COMMAND\n");
309 resp.msg.response = MBOX_R_PARAM_ERROR;
310 r = -1;
311 }
312
313 MSG_OUT("Writing response to MBOX regs\n");
314 len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp.msg));
315 if (len < sizeof(resp.msg)) {
316 r = -errno;
317 MSG_ERR("Didn't write the full response\n");
318 }
319
320out:
321 return r;
322}
323
Michael Neuling899ebac2017-01-14 11:20:26 -0600324int copy_flash(struct mbox_context *context)
325{
326 int r;
327
328 /*
329 * Copy flash into RAM early, same time.
330 * The kernel has created the LPC->AHB mapping also, which means
331 * flash should work.
332 * Ideally we tell the kernel whats up and when to do stuff...
333 */
334 MSG_OUT("Loading flash into ram at %p for 0x%08x bytes\n",
335 context->lpc_mem, context->size);
336 if (lseek(context->fds[MTD_FD].fd, 0, SEEK_SET) != 0) {
337 r = -errno;
338 MSG_ERR("Couldn't reset MBOX pos to zero\n");
339 return r;
340 }
341 r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size);
342 if (r != context->size) {
343 r = -errno;
344 MSG_ERR("Couldn't copy mtd into ram: %d\n", r);
345 return r;
346 }
347 return 0;
348}
349
350void signal_hup(int signum, siginfo_t *info, void *uc)
351{
352 sighup = 1;
353}
354
Cyril Burc85e34d2016-11-15 11:50:41 +1100355static void usage(const char *name)
356{
Cyril Bure8f2de12017-01-17 16:56:02 +1100357 fprintf(stderr, "Usage %s [ -v[v] | --syslog ] --flash=size[K | M]\n", name);
358 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 +1100359 fprintf(stderr, "\t--verbose\t Be [more] verbose\n");
360 fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without -v)\n\n");
361}
362
363int main(int argc, char *argv[])
364{
365 struct mbox_context *context;
366 const char *name = argv[0];
367 char *pnor_filename = NULL;
368 int opt, polled, r, i;
369 struct aspeed_lpc_ctrl_mapping map;
Michael Neuling899ebac2017-01-14 11:20:26 -0600370 struct sigaction act;
Cyril Bure8f2de12017-01-17 16:56:02 +1100371 char *endptr;
Cyril Burc85e34d2016-11-15 11:50:41 +1100372
373 static const struct option long_options[] = {
Cyril Bure8f2de12017-01-17 16:56:02 +1100374 { "flash", required_argument, 0, 'f' },
375 { "verbose", no_argument, 0, 'v' },
376 { "syslog", no_argument, 0, 's' },
377 { 0, 0, 0, 0 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100378 };
379
380 context = calloc(1, sizeof(*context));
381 for (i = 0; i < TOTAL_FDS; i++)
382 context->fds[i].fd = -1;
383
384 mbox_vlog = &mbox_log_console;
Cyril Bure8f2de12017-01-17 16:56:02 +1100385 while ((opt = getopt_long(argc, argv, "fv", long_options, NULL)) != -1) {
Cyril Burc85e34d2016-11-15 11:50:41 +1100386 switch (opt) {
387 case 0:
388 break;
Cyril Bure8f2de12017-01-17 16:56:02 +1100389 case 'f':
390 context->flash_size = strtol(optarg, &endptr, 0);
391 if (optarg == endptr) {
392 fprintf(stderr, "Unparseable flash size\n");
393 usage(name);
394 exit(EXIT_FAILURE);
395 }
396 if (*endptr == 'K') {
397 context->flash_size <<= 10;
398 } else if (*endptr == 'M') {
399 context->flash_size <<= 20;
400 } else if (*endptr != '\0') { /* Unknown units */
401 fprintf(stderr, "Unknown units '%c'\n", *endptr);
402 usage(name);
403 exit(EXIT_FAILURE);
404 }
405 break;
Cyril Burc85e34d2016-11-15 11:50:41 +1100406 case 'v':
407 verbosity++;
408 break;
409 case 's':
410 /* Avoid a double openlog() */
411 if (mbox_vlog != &vsyslog) {
412 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
413 mbox_vlog = &vsyslog;
414 }
415 break;
416 default:
417 usage(name);
418 exit(EXIT_FAILURE);
419 }
420 }
421
Cyril Bure8f2de12017-01-17 16:56:02 +1100422 if (context->flash_size == 0) {
423 fprintf(stderr, "Must specify a non-zero flash size\n");
424 usage(name);
425 exit(EXIT_FAILURE);
426 }
427
Cyril Burc85e34d2016-11-15 11:50:41 +1100428 if (verbosity == MBOX_LOG_VERBOSE)
429 MSG_OUT("Verbose logging\n");
430
431 if (verbosity == MBOX_LOG_DEBUG)
432 MSG_OUT("Debug logging\n");
433
Michael Neuling899ebac2017-01-14 11:20:26 -0600434 MSG_OUT("Registering SigHUP hander\n");
435 act.sa_sigaction = signal_hup;
436 sigemptyset(&act.sa_mask);
437 act.sa_flags = SA_SIGINFO;
438 if (sigaction(SIGHUP, &act, NULL) < 0) {
439 perror("Registering SIGHUP");
440 exit(1);
441 }
442 sighup = 0;
443
Cyril Burc85e34d2016-11-15 11:50:41 +1100444 MSG_OUT("Starting\n");
445
446 MSG_OUT("Opening %s\n", MBOX_HOST_PATH);
447 context->fds[MBOX_FD].fd = open(MBOX_HOST_PATH, O_RDWR | O_NONBLOCK);
448 if (context->fds[MBOX_FD].fd < 0) {
449 r = -errno;
450 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
451 MBOX_HOST_PATH, strerror(errno));
452 goto finish;
453 }
454
455 MSG_OUT("Opening %s\n", LPC_CTRL_PATH);
456 context->fds[LPC_CTRL_FD].fd = open(LPC_CTRL_PATH, O_RDWR | O_SYNC);
457 if (context->fds[LPC_CTRL_FD].fd < 0) {
458 r = -errno;
459 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
460 LPC_CTRL_PATH, strerror(errno));
461 goto finish;
462 }
463
464 MSG_OUT("Getting buffer size...\n");
465 /* This may become more variable in the future */
466 context->pgsize = 12; /* 4K */
467 map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY;
468 map.window_id = 0; /* Theres only one */
469 if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE,
470 &map) < 0) {
471 r = -errno;
472 MSG_OUT("fail\n");
473 MSG_ERR("Couldn't get lpc control buffer size: %s\n", strerror(-r));
474 goto finish;
475 }
476 /* And strip the first nibble, LPC access speciality */
477 context->size = map.size;
478 context->base = -context->size & 0x0FFFFFFF;
479
480 /* READ THE COMMENT AT THE START OF THIS FUNCTION! */
Cyril Bur46233672017-01-16 13:33:26 +1100481 r = point_to_flash(context);
Cyril Burc85e34d2016-11-15 11:50:41 +1100482 if (r) {
483 MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n",
484 strerror(-r));
485 goto finish;
486 }
487
488 MSG_OUT("Mapping %s for %u\n", LPC_CTRL_PATH, context->size);
489 context->lpc_mem = mmap(NULL, context->size, PROT_READ | PROT_WRITE, MAP_SHARED,
490 context->fds[LPC_CTRL_FD].fd, 0);
491 if (context->lpc_mem == MAP_FAILED) {
492 r = -errno;
493 MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH, strerror(errno));
494 goto finish;
495 }
496
497 pnor_filename = get_dev_mtd();
498 if (!pnor_filename) {
499 MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n");
500 r = -1;
501 goto finish;
502 }
503
504 MSG_OUT("Opening %s\n", pnor_filename);
505 context->fds[MTD_FD].fd = open(pnor_filename, O_RDWR);
506 if (context->fds[MTD_FD].fd < 0) {
507 r = -errno;
508 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n",
509 pnor_filename, strerror(errno));
510 goto finish;
511 }
512
513 if (ioctl(context->fds[MTD_FD].fd, MEMGETINFO, &context->mtd_info) == -1) {
514 MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno));
515 return -1;
516 }
517
Michael Neuling899ebac2017-01-14 11:20:26 -0600518 if (copy_flash(context))
Cyril Burc85e34d2016-11-15 11:50:41 +1100519 goto finish;
Cyril Burc85e34d2016-11-15 11:50:41 +1100520
521 context->fds[MBOX_FD].events = POLLIN;
522 /* Ignore in poll() */
523 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
524 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
525
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) {
554 polled = poll(context->fds, TOTAL_FDS, 1000);
555 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 */
560 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
561 r = point_to_flash(context);
562 if (r) {
563 goto finish;
564 }
565 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
566 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
567 r = copy_flash(context);
568 if (r)
569 goto finish;
570 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
571 sighup = 0;
572 continue;
573 }
Cyril Burc85e34d2016-11-15 11:50:41 +1100574 if (polled < 0) {
575 r = -errno;
576 MSG_ERR("Error from poll(): %s\n", strerror(errno));
577 break;
578 }
579 r = dispatch_mbox(context);
580 if (r < 0) {
581 MSG_ERR("Error handling MBOX event: %s\n", strerror(-r));
582 break;
583 }
584 }
585
586 MSG_OUT("Exiting\n");
587
588 /* Unnegate so we can close it */
589 context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd;
590 context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd;
591
592finish:
593 if (context->lpc_mem)
594 munmap(context->lpc_mem, context->size);
595
596 free(pnor_filename);
597 close(context->fds[MTD_FD].fd);
598 close(context->fds[LPC_CTRL_FD].fd);
599 close(context->fds[MBOX_FD].fd);
600 free(context);
601
602 return r;
603}
604