Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 1 | /* 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> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <sys/mman.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/timerfd.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <time.h> |
| 35 | #include <unistd.h> |
Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame^] | 36 | #include <inttypes.h> |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 37 | |
| 38 | #include <mtd/mtd-abi.h> |
| 39 | |
| 40 | #include <linux/aspeed-lpc-ctrl.h> |
| 41 | |
| 42 | #include "mbox.h" |
| 43 | #include "common.h" |
| 44 | |
| 45 | #define LPC_CTRL_PATH "/dev/aspeed-lpc-ctrl" |
| 46 | |
| 47 | #define MBOX_FD 0 |
| 48 | #define LPC_CTRL_FD 1 |
| 49 | #define MTD_FD 2 |
| 50 | #define TOTAL_FDS 3 |
| 51 | |
| 52 | #define ALIGN_UP(_v, _a) (((_v) + (_a) - 1) & ~((_a) - 1)) |
| 53 | |
| 54 | #define MSG_OUT(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0) |
| 55 | #define MSG_ERR(f_, ...) do { if (verbosity != MBOX_LOG_NONE) { mbox_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0) |
| 56 | |
| 57 | #define BOOT_HICR7 0x30000e00U |
| 58 | #define BOOT_HICR8 0xfe0001ffU |
| 59 | |
| 60 | struct mbox_context { |
| 61 | struct pollfd fds[TOTAL_FDS]; |
| 62 | void *lpc_mem; |
| 63 | uint32_t base; |
| 64 | uint32_t size; |
| 65 | uint32_t pgsize; |
| 66 | bool dirty; |
| 67 | uint32_t dirtybase; |
| 68 | uint32_t dirtysize; |
| 69 | struct mtd_info_user mtd_info; |
| 70 | }; |
| 71 | |
| 72 | static int running = 1; |
| 73 | |
| 74 | static int point_to_flash(void) |
| 75 | { |
| 76 | /* |
| 77 | * Point it to the real flash for sanity. Because hostboot has |
| 78 | * expectations as to where the flash is we can't use the kernel |
| 79 | * provided UNMAP ioctl(). |
| 80 | * |
| 81 | * That that ioctl() does is detect the size of the flash and map it |
| 82 | * appropriately on the LPC bus on the host. The issue with this is that |
| 83 | * if a machine has a different flash size to what hostboot expects the |
| 84 | * mapping will be incorrect. |
| 85 | * |
| 86 | * For example 32MB of flash for a platform would mean that hostboot |
| 87 | * expects flash to be at 0x0e000000 - 0x0fffffff on the LPC bus. If |
| 88 | * the machine actually has 64MB of flash then the UNMAP ioctl() would |
| 89 | * map it 0x0c000000 - 0x0fffffff but hostboot will still read at |
| 90 | * 0x0e000000. |
| 91 | * |
| 92 | * Until hostboot learns how to talk to this daemon this hardcode will |
| 93 | * get hostboot going. Furthermore, when hostboot does learn to talk |
| 94 | * then this mapping is unnecessary and this code should be removed. |
| 95 | */ |
| 96 | |
| 97 | int r = 0, devmem_fd; |
| 98 | char *devmem_ptr; |
| 99 | |
| 100 | MSG_OUT("Pointing HOST LPC bus at the actual flash\n"); |
| 101 | MSG_OUT("Assuming 32MB of flash: HOST LPC 0x%08x -> BMC 0x%08x\n", |
| 102 | BOOT_HICR7 & 0xffff0000, BOOT_HICR7 << 16); |
| 103 | devmem_fd = open("/dev/mem", O_RDWR); |
| 104 | if (devmem_fd == -1) { |
| 105 | r = -errno; |
| 106 | MSG_ERR("Couldn't open /dev/mem: %s\n", strerror(-r)); |
| 107 | goto out; |
| 108 | } |
| 109 | devmem_ptr = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 110 | devmem_fd, 0x1e789000); |
| 111 | if (devmem_ptr == MAP_FAILED) { |
| 112 | r = -errno; |
| 113 | MSG_ERR("Couldn't mmap() /dev/mem at 0x1e789000 for 0x1000: %s\n", |
| 114 | strerror(-r)); |
| 115 | goto out; |
| 116 | } |
| 117 | *(uint32_t *)&devmem_ptr[0x88] = BOOT_HICR7; |
| 118 | *(uint32_t *)&devmem_ptr[0x8c] = BOOT_HICR8; |
| 119 | munmap(devmem_ptr, 0x1000); |
| 120 | close(devmem_fd); |
| 121 | /* Sigh */ |
| 122 | |
| 123 | out: |
| 124 | return r; |
| 125 | } |
| 126 | |
| 127 | static int flash_write(struct mbox_context *context, uint32_t pos, uint32_t len) |
| 128 | { |
| 129 | int rc; |
| 130 | struct erase_info_user erase_info = { |
| 131 | .start = pos, |
| 132 | }; |
| 133 | |
| 134 | assert(context); |
| 135 | |
| 136 | erase_info.length = ALIGN_UP(len, context->mtd_info.erasesize); |
| 137 | |
| 138 | MSG_OUT("Erasing 0x%08x for 0x%08x (aligned: 0x%08x)\n", pos, len, erase_info.length); |
| 139 | if (ioctl(-context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) { |
| 140 | MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno)); |
| 141 | return -1; |
| 142 | } |
| 143 | |
| 144 | if (lseek(-context->fds[MTD_FD].fd, pos, SEEK_SET) == (off_t) -1) { |
| 145 | MSG_ERR("Couldn't seek to 0x%08x into MTD, flash write lost: %s\n", pos, strerror(errno)); |
| 146 | return -1; |
| 147 | } |
| 148 | |
| 149 | while (erase_info.length) { |
| 150 | rc = write(-context->fds[MTD_FD].fd, context->lpc_mem + pos, erase_info.length); |
| 151 | if (rc == -1) { |
| 152 | MSG_ERR("Couldn't write to flash! Flash write lost: %s\n", strerror(errno)); |
| 153 | return -1; |
| 154 | } |
| 155 | erase_info.length -= rc; |
| 156 | pos += rc; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* TODO: Add come consistency around the daemon exiting and either |
| 163 | * way, ensuring it responds. |
| 164 | * I'm in favour of an approach where it does its best to stay alive |
| 165 | * and keep talking, the hacky prototype was written the other way. |
| 166 | * This function is now inconsistent |
| 167 | */ |
| 168 | static int dispatch_mbox(struct mbox_context *context) |
| 169 | { |
| 170 | int r = 0; |
| 171 | int len; |
| 172 | off_t pos; |
| 173 | uint8_t byte; |
| 174 | union mbox_regs resp, req = { 0 }; |
| 175 | uint16_t sizepg, basepg, dirtypg; |
| 176 | uint32_t dirtycount; |
| 177 | struct aspeed_lpc_ctrl_mapping map; |
| 178 | |
| 179 | assert(context); |
| 180 | |
| 181 | map.addr = context->base; |
| 182 | map.size = context->size; |
| 183 | map.offset = 0; |
| 184 | map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY; |
| 185 | map.window_id = 0; /* Theres only one */ |
| 186 | |
| 187 | MSG_OUT("Dispatched to mbox\n"); |
| 188 | r = read(context->fds[MBOX_FD].fd, &req, sizeof(req.raw)); |
| 189 | if (r < 0) { |
| 190 | r = -errno; |
| 191 | MSG_ERR("Couldn't read: %s\n", strerror(errno)); |
| 192 | goto out; |
| 193 | } |
| 194 | if (r < sizeof(req.msg)) { |
| 195 | MSG_ERR("Short read: %d expecting %zu\n", r, sizeof(req.msg)); |
| 196 | r = -1; |
| 197 | goto out; |
| 198 | } |
| 199 | |
| 200 | /* We are NOT going to update the last two 'status' bytes */ |
| 201 | memcpy(&resp, &req, sizeof(req.msg)); |
| 202 | |
| 203 | sizepg = context->size >> context->pgsize; |
| 204 | basepg = context->base >> context->pgsize; |
| 205 | MSG_OUT("Got data in with command %d\n", req.msg.command); |
| 206 | switch (req.msg.command) { |
| 207 | case MBOX_C_RESET_STATE: |
| 208 | /* Called by early hostboot? TODO */ |
| 209 | resp.msg.response = MBOX_R_SUCCESS; |
| 210 | r = point_to_flash(); |
| 211 | if (r) { |
| 212 | resp.msg.response = MBOX_R_SYSTEM_ERROR; |
| 213 | MSG_ERR("Couldn't point the LPC BUS back to actual flash\n"); |
| 214 | } |
| 215 | break; |
| 216 | case MBOX_C_GET_MBOX_INFO: |
| 217 | /* TODO Freak if data.data[0] isn't 1 */ |
| 218 | resp.msg.data[0] = 1; |
| 219 | put_u16(&resp.msg.data[1], sizepg); |
| 220 | put_u16(&resp.msg.data[3], sizepg); |
| 221 | resp.msg.response = MBOX_R_SUCCESS; |
| 222 | /* Wow that can't stay negated thats horrible */ |
| 223 | MSG_OUT("LPC_CTRL_IOCTL_MAP to 0x%08x for 0x%08x\n", map.addr, map.size); |
| 224 | r = ioctl(-context->fds[LPC_CTRL_FD].fd, |
| 225 | ASPEED_LPC_CTRL_IOCTL_MAP, &map); |
| 226 | if (r < 0) { |
| 227 | r = -errno; |
| 228 | resp.msg.response = MBOX_R_SYSTEM_ERROR; |
| 229 | MSG_ERR("Couldn't MAP ioctl(): %s\n", strerror(errno)); |
| 230 | } |
| 231 | break; |
| 232 | case MBOX_C_GET_FLASH_INFO: |
| 233 | put_u32(&resp.msg.data[0], context->mtd_info.size); |
| 234 | put_u32(&resp.msg.data[4], context->mtd_info.erasesize); |
| 235 | resp.msg.response = MBOX_R_SUCCESS; |
| 236 | break; |
| 237 | case MBOX_C_READ_WINDOW: |
| 238 | /* |
| 239 | * We could probably play tricks with LPC mapping. |
| 240 | * That would require kernel involvement. |
| 241 | * We could also always copy the relevant flash part to |
| 242 | * context->base even if it turns out that offset is in |
| 243 | * the window... |
| 244 | * This approach is easiest. |
| 245 | */ |
Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame^] | 246 | if (context->dirty) { |
| 247 | r = read(-context->fds[MTD_FD].fd, context->lpc_mem, context->size); |
| 248 | if (r != context->size) { |
| 249 | MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size); |
| 250 | goto out; |
| 251 | } |
| 252 | } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 253 | basepg += get_u16(&req.msg.data[0]); |
| 254 | put_u16(&resp.msg.data[0], basepg); |
| 255 | resp.msg.response = MBOX_R_SUCCESS; |
| 256 | context->dirty = false; |
| 257 | break; |
| 258 | case MBOX_C_CLOSE_WINDOW: |
| 259 | context->dirty = true; |
| 260 | break; |
| 261 | case MBOX_C_WRITE_WINDOW: |
| 262 | basepg += get_u16(&req.msg.data[0]); |
| 263 | put_u16(&resp.msg.data[0], basepg); |
| 264 | resp.msg.response = MBOX_R_SUCCESS; |
| 265 | context->dirtybase = basepg << context->pgsize; |
| 266 | break; |
| 267 | /* Optimise these later */ |
| 268 | case MBOX_C_WRITE_DIRTY: |
| 269 | case MBOX_C_WRITE_FENCE: |
| 270 | dirtypg = get_u16(&req.msg.data[0]); |
| 271 | dirtycount = get_u32(&req.msg.data[2]); |
| 272 | if (dirtycount == 0) { |
| 273 | resp.msg.response = MBOX_R_PARAM_ERROR; |
| 274 | break; |
| 275 | } |
| 276 | /* |
| 277 | * dirtypg is actually offset within window so we probs |
| 278 | * need to know if the window isn't at zero |
| 279 | */ |
| 280 | if (flash_write(context, dirtypg << context->pgsize, dirtycount) != 0) { |
| 281 | resp.msg.response = MBOX_R_WRITE_ERROR; |
| 282 | break; |
| 283 | } |
| 284 | resp.msg.response = MBOX_R_SUCCESS; |
| 285 | break; |
| 286 | case MBOX_C_ACK: |
| 287 | resp.msg.response = MBOX_R_SUCCESS; |
| 288 | pos = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_BYTE, SEEK_SET); |
| 289 | if (pos != MBOX_BMC_BYTE) { |
| 290 | r = -errno; |
| 291 | MSG_ERR("Couldn't lseek() to byte %d: %s\n", MBOX_BMC_BYTE, |
| 292 | strerror(errno)); |
| 293 | } |
| 294 | /* |
| 295 | * NAND what is in the hardware and the request. |
| 296 | * This prevents the host being able to SET bits, it can |
| 297 | * only request set ones be cleared. |
| 298 | */ |
| 299 | byte = ~(req.msg.data[0] & req.raw[MBOX_BMC_BYTE]); |
| 300 | len = write(context->fds[MBOX_FD].fd, &byte, 1); |
| 301 | if (len != 1) { |
| 302 | r = -errno; |
| 303 | MSG_ERR("Couldn't write to BMC status reg: %s\n", |
| 304 | strerror(errno)); |
| 305 | } |
| 306 | pos = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET); |
| 307 | if (pos != 0) { |
| 308 | r = -errno; |
| 309 | MSG_ERR("Couldn't reset MBOX offset to zero\n"); |
| 310 | } |
| 311 | break; |
| 312 | case MBOX_C_COMPLETED_COMMANDS: |
| 313 | /* This implementation always completes before responding */ |
| 314 | resp.msg.data[0] = 0; |
| 315 | resp.msg.response = MBOX_R_SUCCESS; |
| 316 | break; |
| 317 | default: |
| 318 | MSG_ERR("UNKNOWN MBOX COMMAND\n"); |
| 319 | resp.msg.response = MBOX_R_PARAM_ERROR; |
| 320 | r = -1; |
| 321 | } |
| 322 | |
| 323 | MSG_OUT("Writing response to MBOX regs\n"); |
| 324 | len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp.msg)); |
| 325 | if (len < sizeof(resp.msg)) { |
| 326 | r = -errno; |
| 327 | MSG_ERR("Didn't write the full response\n"); |
| 328 | } |
| 329 | |
| 330 | out: |
| 331 | return r; |
| 332 | } |
| 333 | |
| 334 | static void usage(const char *name) |
| 335 | { |
| 336 | fprintf(stderr, "Usage %s [ -v[v] | --syslog ]\n", name); |
| 337 | fprintf(stderr, "\t--verbose\t Be [more] verbose\n"); |
| 338 | fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without -v)\n\n"); |
| 339 | } |
| 340 | |
| 341 | int main(int argc, char *argv[]) |
| 342 | { |
| 343 | struct mbox_context *context; |
| 344 | const char *name = argv[0]; |
| 345 | char *pnor_filename = NULL; |
| 346 | int opt, polled, r, i; |
| 347 | struct aspeed_lpc_ctrl_mapping map; |
| 348 | |
| 349 | static const struct option long_options[] = { |
| 350 | { "verbose", no_argument, 0, 'v' }, |
| 351 | { "syslog", no_argument, 0, 's' }, |
| 352 | { 0, 0, 0, 0 } |
| 353 | }; |
| 354 | |
| 355 | context = calloc(1, sizeof(*context)); |
| 356 | for (i = 0; i < TOTAL_FDS; i++) |
| 357 | context->fds[i].fd = -1; |
| 358 | |
| 359 | mbox_vlog = &mbox_log_console; |
| 360 | while ((opt = getopt_long(argc, argv, "v", long_options, NULL)) != -1) { |
| 361 | switch (opt) { |
| 362 | case 0: |
| 363 | break; |
| 364 | case 'v': |
| 365 | verbosity++; |
| 366 | break; |
| 367 | case 's': |
| 368 | /* Avoid a double openlog() */ |
| 369 | if (mbox_vlog != &vsyslog) { |
| 370 | openlog(PREFIX, LOG_ODELAY, LOG_DAEMON); |
| 371 | mbox_vlog = &vsyslog; |
| 372 | } |
| 373 | break; |
| 374 | default: |
| 375 | usage(name); |
| 376 | exit(EXIT_FAILURE); |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (verbosity == MBOX_LOG_VERBOSE) |
| 381 | MSG_OUT("Verbose logging\n"); |
| 382 | |
| 383 | if (verbosity == MBOX_LOG_DEBUG) |
| 384 | MSG_OUT("Debug logging\n"); |
| 385 | |
| 386 | MSG_OUT("Starting\n"); |
| 387 | |
| 388 | MSG_OUT("Opening %s\n", MBOX_HOST_PATH); |
| 389 | context->fds[MBOX_FD].fd = open(MBOX_HOST_PATH, O_RDWR | O_NONBLOCK); |
| 390 | if (context->fds[MBOX_FD].fd < 0) { |
| 391 | r = -errno; |
| 392 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 393 | MBOX_HOST_PATH, strerror(errno)); |
| 394 | goto finish; |
| 395 | } |
| 396 | |
| 397 | MSG_OUT("Opening %s\n", LPC_CTRL_PATH); |
| 398 | context->fds[LPC_CTRL_FD].fd = open(LPC_CTRL_PATH, O_RDWR | O_SYNC); |
| 399 | if (context->fds[LPC_CTRL_FD].fd < 0) { |
| 400 | r = -errno; |
| 401 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 402 | LPC_CTRL_PATH, strerror(errno)); |
| 403 | goto finish; |
| 404 | } |
| 405 | |
| 406 | MSG_OUT("Getting buffer size...\n"); |
| 407 | /* This may become more variable in the future */ |
| 408 | context->pgsize = 12; /* 4K */ |
| 409 | map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY; |
| 410 | map.window_id = 0; /* Theres only one */ |
| 411 | if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, |
| 412 | &map) < 0) { |
| 413 | r = -errno; |
| 414 | MSG_OUT("fail\n"); |
| 415 | MSG_ERR("Couldn't get lpc control buffer size: %s\n", strerror(-r)); |
| 416 | goto finish; |
| 417 | } |
| 418 | /* And strip the first nibble, LPC access speciality */ |
| 419 | context->size = map.size; |
| 420 | context->base = -context->size & 0x0FFFFFFF; |
| 421 | |
| 422 | /* READ THE COMMENT AT THE START OF THIS FUNCTION! */ |
| 423 | r = point_to_flash(); |
| 424 | if (r) { |
| 425 | MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n", |
| 426 | strerror(-r)); |
| 427 | goto finish; |
| 428 | } |
| 429 | |
| 430 | MSG_OUT("Mapping %s for %u\n", LPC_CTRL_PATH, context->size); |
| 431 | context->lpc_mem = mmap(NULL, context->size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 432 | context->fds[LPC_CTRL_FD].fd, 0); |
| 433 | if (context->lpc_mem == MAP_FAILED) { |
| 434 | r = -errno; |
| 435 | MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH, strerror(errno)); |
| 436 | goto finish; |
| 437 | } |
| 438 | |
| 439 | pnor_filename = get_dev_mtd(); |
| 440 | if (!pnor_filename) { |
| 441 | MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n"); |
| 442 | r = -1; |
| 443 | goto finish; |
| 444 | } |
| 445 | |
| 446 | MSG_OUT("Opening %s\n", pnor_filename); |
| 447 | context->fds[MTD_FD].fd = open(pnor_filename, O_RDWR); |
| 448 | if (context->fds[MTD_FD].fd < 0) { |
| 449 | r = -errno; |
| 450 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 451 | pnor_filename, strerror(errno)); |
| 452 | goto finish; |
| 453 | } |
| 454 | |
| 455 | if (ioctl(context->fds[MTD_FD].fd, MEMGETINFO, &context->mtd_info) == -1) { |
| 456 | MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno)); |
| 457 | return -1; |
| 458 | } |
| 459 | |
| 460 | /* |
| 461 | * Copy flash into RAM early, same time. |
| 462 | * The kernel has created the LPC->AHB mapping also, which means |
| 463 | * flash should work. |
| 464 | * Ideally we tell the kernel whats up and when to do stuff... |
| 465 | */ |
| 466 | MSG_OUT("Loading flash into ram at %p for 0x%08x bytes\n", |
| 467 | context->lpc_mem, context->size); |
| 468 | r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size); |
| 469 | if (r != context->size) { |
| 470 | MSG_ERR("Couldn't copy mtd into ram: %d\n", r); |
| 471 | goto finish; |
| 472 | } |
| 473 | |
| 474 | context->fds[MBOX_FD].events = POLLIN; |
| 475 | /* Ignore in poll() */ |
| 476 | context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd; |
| 477 | context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd; |
| 478 | |
Cyril Bur | d8f6d7a | 2017-01-10 18:11:16 +1100 | [diff] [blame] | 479 | /* Test the single write facility by setting all the regs to 0xFF */ |
| 480 | MSG_OUT("Setting all MBOX regs to 0xff individually...\n"); |
| 481 | for (i = 0; i < MBOX_REG_BYTES; i++) { |
| 482 | uint8_t byte = 0xff; |
| 483 | off_t pos; |
| 484 | int len; |
| 485 | |
| 486 | pos = lseek(context->fds[MBOX_FD].fd, i, SEEK_SET); |
| 487 | if (pos != i) { |
| 488 | MSG_ERR("Couldn't lseek() to byte %d: %s\n", i, |
| 489 | strerror(errno)); |
| 490 | break; |
| 491 | } |
| 492 | len = write(context->fds[MBOX_FD].fd, &byte, 1); |
| 493 | if (len != 1) { |
| 494 | MSG_ERR("Couldn't write MBOX reg %d: %s\n", i, |
| 495 | strerror(errno)); |
| 496 | break; |
| 497 | } |
| 498 | } |
| 499 | if (lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET) != 0) { |
| 500 | r = -errno; |
| 501 | MSG_ERR("Couldn't reset MBOX pos to zero\n"); |
| 502 | goto finish; |
| 503 | } |
| 504 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 505 | MSG_OUT("Entering polling loop\n"); |
| 506 | while (running) { |
| 507 | polled = poll(context->fds, TOTAL_FDS, 1000); |
| 508 | if (polled == 0) |
| 509 | continue; |
| 510 | if (polled < 0) { |
| 511 | r = -errno; |
| 512 | MSG_ERR("Error from poll(): %s\n", strerror(errno)); |
| 513 | break; |
| 514 | } |
| 515 | r = dispatch_mbox(context); |
| 516 | if (r < 0) { |
| 517 | MSG_ERR("Error handling MBOX event: %s\n", strerror(-r)); |
| 518 | break; |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | MSG_OUT("Exiting\n"); |
| 523 | |
| 524 | /* Unnegate so we can close it */ |
| 525 | context->fds[LPC_CTRL_FD].fd = -context->fds[LPC_CTRL_FD].fd; |
| 526 | context->fds[MTD_FD].fd = -context->fds[MTD_FD].fd; |
| 527 | |
| 528 | finish: |
| 529 | if (context->lpc_mem) |
| 530 | munmap(context->lpc_mem, context->size); |
| 531 | |
| 532 | free(pnor_filename); |
| 533 | close(context->fds[MTD_FD].fd); |
| 534 | close(context->fds[LPC_CTRL_FD].fd); |
| 535 | close(context->fds[MBOX_FD].fd); |
| 536 | free(context); |
| 537 | |
| 538 | return r; |
| 539 | } |
| 540 | |