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> |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 29 | #include <signal.h> |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 30 | #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 Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 37 | #include <inttypes.h> |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 38 | |
| 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 Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 48 | |
| 49 | /* Put pulled fds first */ |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 50 | #define MBOX_FD 0 |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 51 | #define POLL_FDS 1 |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 52 | #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 | |
| 64 | struct 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 Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 74 | uint32_t flash_size; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | static int running = 1; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 78 | static int sighup = 0; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 79 | |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 80 | static int point_to_flash(struct mbox_context *context) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 81 | { |
Cyril Bur | ef315c1 | 2017-01-19 10:49:34 +1100 | [diff] [blame^] | 82 | struct aspeed_lpc_ctrl_mapping map = { 0 }; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 83 | int r = 0; |
| 84 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 85 | /* |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 86 | * Point it to the real flash for sanity. |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 87 | * |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 88 | * 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 92 | * |
| 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 Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 98 | /* |
| 99 | * The mask is because the top nibble is the host LPC FW space, we |
Cyril Bur | ef315c1 | 2017-01-19 10:49:34 +1100 | [diff] [blame^] | 100 | * want space 0. The struct has been zeroed, best to be explicit |
| 101 | * though. |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 102 | */ |
| 103 | map.addr = (0UL - context->flash_size) & 0x0fffffff; |
| 104 | map.size = context->flash_size; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 105 | map.offset = 0; |
| 106 | map.window_type = ASPEED_LPC_CTRL_WINDOW_FLASH; |
| 107 | map.window_id = 0; /* Theres only one */ |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 108 | |
| 109 | MSG_OUT("Pointing HOST LPC bus at the actual flash\n"); |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 110 | MSG_OUT("Assuming %dMB of flash: HOST LPC 0x%08x\n", context->flash_size >> 20, |
| 111 | map.addr); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 112 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 113 | if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_MAP, &map) == -1) { |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 114 | r = -errno; |
| 115 | MSG_ERR("Couldn't MAP the host LPC bus to the platform flash\n"); |
| 116 | } |
| 117 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 118 | return r; |
| 119 | } |
| 120 | |
| 121 | static int flash_write(struct mbox_context *context, uint32_t pos, uint32_t len) |
| 122 | { |
| 123 | int rc; |
| 124 | struct erase_info_user erase_info = { |
| 125 | .start = pos, |
| 126 | }; |
| 127 | |
| 128 | assert(context); |
| 129 | |
| 130 | erase_info.length = ALIGN_UP(len, context->mtd_info.erasesize); |
| 131 | |
| 132 | MSG_OUT("Erasing 0x%08x for 0x%08x (aligned: 0x%08x)\n", pos, len, erase_info.length); |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 133 | if (ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 134 | MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno)); |
| 135 | return -1; |
| 136 | } |
| 137 | |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 138 | if (lseek(context->fds[MTD_FD].fd, pos, SEEK_SET) == (off_t) -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 139 | MSG_ERR("Couldn't seek to 0x%08x into MTD, flash write lost: %s\n", pos, strerror(errno)); |
| 140 | return -1; |
| 141 | } |
| 142 | |
| 143 | while (erase_info.length) { |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 144 | rc = write(context->fds[MTD_FD].fd, context->lpc_mem + pos, erase_info.length); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 145 | if (rc == -1) { |
| 146 | MSG_ERR("Couldn't write to flash! Flash write lost: %s\n", strerror(errno)); |
| 147 | return -1; |
| 148 | } |
| 149 | erase_info.length -= rc; |
| 150 | pos += rc; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | /* TODO: Add come consistency around the daemon exiting and either |
| 157 | * way, ensuring it responds. |
| 158 | * I'm in favour of an approach where it does its best to stay alive |
| 159 | * and keep talking, the hacky prototype was written the other way. |
| 160 | * This function is now inconsistent |
| 161 | */ |
| 162 | static int dispatch_mbox(struct mbox_context *context) |
| 163 | { |
| 164 | int r = 0; |
| 165 | int len; |
| 166 | off_t pos; |
| 167 | uint8_t byte; |
| 168 | union mbox_regs resp, req = { 0 }; |
| 169 | uint16_t sizepg, basepg, dirtypg; |
| 170 | uint32_t dirtycount; |
Cyril Bur | ef315c1 | 2017-01-19 10:49:34 +1100 | [diff] [blame^] | 171 | struct aspeed_lpc_ctrl_mapping map = { 0 }; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 172 | |
| 173 | assert(context); |
| 174 | |
| 175 | map.addr = context->base; |
| 176 | map.size = context->size; |
| 177 | map.offset = 0; |
| 178 | map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY; |
| 179 | map.window_id = 0; /* Theres only one */ |
| 180 | |
| 181 | MSG_OUT("Dispatched to mbox\n"); |
| 182 | r = read(context->fds[MBOX_FD].fd, &req, sizeof(req.raw)); |
| 183 | if (r < 0) { |
| 184 | r = -errno; |
| 185 | MSG_ERR("Couldn't read: %s\n", strerror(errno)); |
| 186 | goto out; |
| 187 | } |
| 188 | if (r < sizeof(req.msg)) { |
| 189 | MSG_ERR("Short read: %d expecting %zu\n", r, sizeof(req.msg)); |
| 190 | r = -1; |
| 191 | goto out; |
| 192 | } |
| 193 | |
| 194 | /* We are NOT going to update the last two 'status' bytes */ |
| 195 | memcpy(&resp, &req, sizeof(req.msg)); |
| 196 | |
| 197 | sizepg = context->size >> context->pgsize; |
| 198 | basepg = context->base >> context->pgsize; |
| 199 | MSG_OUT("Got data in with command %d\n", req.msg.command); |
| 200 | switch (req.msg.command) { |
| 201 | case MBOX_C_RESET_STATE: |
| 202 | /* Called by early hostboot? TODO */ |
| 203 | resp.msg.response = MBOX_R_SUCCESS; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 204 | r = point_to_flash(context); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 205 | if (r) { |
| 206 | resp.msg.response = MBOX_R_SYSTEM_ERROR; |
| 207 | MSG_ERR("Couldn't point the LPC BUS back to actual flash\n"); |
| 208 | } |
| 209 | break; |
| 210 | case MBOX_C_GET_MBOX_INFO: |
| 211 | /* TODO Freak if data.data[0] isn't 1 */ |
| 212 | resp.msg.data[0] = 1; |
| 213 | put_u16(&resp.msg.data[1], sizepg); |
| 214 | put_u16(&resp.msg.data[3], sizepg); |
| 215 | resp.msg.response = MBOX_R_SUCCESS; |
| 216 | /* Wow that can't stay negated thats horrible */ |
| 217 | MSG_OUT("LPC_CTRL_IOCTL_MAP to 0x%08x for 0x%08x\n", map.addr, map.size); |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 218 | r = ioctl(context->fds[LPC_CTRL_FD].fd, |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 219 | ASPEED_LPC_CTRL_IOCTL_MAP, &map); |
| 220 | if (r < 0) { |
| 221 | r = -errno; |
| 222 | resp.msg.response = MBOX_R_SYSTEM_ERROR; |
| 223 | MSG_ERR("Couldn't MAP ioctl(): %s\n", strerror(errno)); |
| 224 | } |
| 225 | break; |
| 226 | case MBOX_C_GET_FLASH_INFO: |
| 227 | put_u32(&resp.msg.data[0], context->mtd_info.size); |
| 228 | put_u32(&resp.msg.data[4], context->mtd_info.erasesize); |
| 229 | resp.msg.response = MBOX_R_SUCCESS; |
| 230 | break; |
| 231 | case MBOX_C_READ_WINDOW: |
| 232 | /* |
| 233 | * We could probably play tricks with LPC mapping. |
| 234 | * That would require kernel involvement. |
| 235 | * We could also always copy the relevant flash part to |
| 236 | * context->base even if it turns out that offset is in |
| 237 | * the window... |
| 238 | * This approach is easiest. |
| 239 | */ |
Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 240 | if (context->dirty) { |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 241 | r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size); |
Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 242 | if (r != context->size) { |
| 243 | MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size); |
| 244 | goto out; |
| 245 | } |
| 246 | } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 247 | basepg += get_u16(&req.msg.data[0]); |
| 248 | put_u16(&resp.msg.data[0], basepg); |
| 249 | resp.msg.response = MBOX_R_SUCCESS; |
| 250 | context->dirty = false; |
| 251 | break; |
| 252 | case MBOX_C_CLOSE_WINDOW: |
| 253 | context->dirty = true; |
| 254 | break; |
| 255 | case MBOX_C_WRITE_WINDOW: |
| 256 | basepg += get_u16(&req.msg.data[0]); |
| 257 | put_u16(&resp.msg.data[0], basepg); |
| 258 | resp.msg.response = MBOX_R_SUCCESS; |
| 259 | context->dirtybase = basepg << context->pgsize; |
| 260 | break; |
| 261 | /* Optimise these later */ |
| 262 | case MBOX_C_WRITE_DIRTY: |
| 263 | case MBOX_C_WRITE_FENCE: |
| 264 | dirtypg = get_u16(&req.msg.data[0]); |
| 265 | dirtycount = get_u32(&req.msg.data[2]); |
| 266 | if (dirtycount == 0) { |
| 267 | resp.msg.response = MBOX_R_PARAM_ERROR; |
| 268 | break; |
| 269 | } |
| 270 | /* |
| 271 | * dirtypg is actually offset within window so we probs |
| 272 | * need to know if the window isn't at zero |
| 273 | */ |
| 274 | if (flash_write(context, dirtypg << context->pgsize, dirtycount) != 0) { |
| 275 | resp.msg.response = MBOX_R_WRITE_ERROR; |
| 276 | break; |
| 277 | } |
| 278 | resp.msg.response = MBOX_R_SUCCESS; |
| 279 | break; |
| 280 | case MBOX_C_ACK: |
| 281 | resp.msg.response = MBOX_R_SUCCESS; |
| 282 | pos = lseek(context->fds[MBOX_FD].fd, MBOX_BMC_BYTE, SEEK_SET); |
| 283 | if (pos != MBOX_BMC_BYTE) { |
| 284 | r = -errno; |
| 285 | MSG_ERR("Couldn't lseek() to byte %d: %s\n", MBOX_BMC_BYTE, |
| 286 | strerror(errno)); |
| 287 | } |
| 288 | /* |
| 289 | * NAND what is in the hardware and the request. |
| 290 | * This prevents the host being able to SET bits, it can |
| 291 | * only request set ones be cleared. |
| 292 | */ |
| 293 | byte = ~(req.msg.data[0] & req.raw[MBOX_BMC_BYTE]); |
| 294 | len = write(context->fds[MBOX_FD].fd, &byte, 1); |
| 295 | if (len != 1) { |
| 296 | r = -errno; |
| 297 | MSG_ERR("Couldn't write to BMC status reg: %s\n", |
| 298 | strerror(errno)); |
| 299 | } |
| 300 | pos = lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET); |
| 301 | if (pos != 0) { |
| 302 | r = -errno; |
| 303 | MSG_ERR("Couldn't reset MBOX offset to zero\n"); |
| 304 | } |
| 305 | break; |
| 306 | case MBOX_C_COMPLETED_COMMANDS: |
| 307 | /* This implementation always completes before responding */ |
| 308 | resp.msg.data[0] = 0; |
| 309 | resp.msg.response = MBOX_R_SUCCESS; |
| 310 | break; |
| 311 | default: |
| 312 | MSG_ERR("UNKNOWN MBOX COMMAND\n"); |
| 313 | resp.msg.response = MBOX_R_PARAM_ERROR; |
| 314 | r = -1; |
| 315 | } |
| 316 | |
| 317 | MSG_OUT("Writing response to MBOX regs\n"); |
| 318 | len = write(context->fds[MBOX_FD].fd, &resp, sizeof(resp.msg)); |
| 319 | if (len < sizeof(resp.msg)) { |
| 320 | r = -errno; |
| 321 | MSG_ERR("Didn't write the full response\n"); |
| 322 | } |
| 323 | |
| 324 | out: |
| 325 | return r; |
| 326 | } |
| 327 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 328 | int copy_flash(struct mbox_context *context) |
| 329 | { |
| 330 | int r; |
| 331 | |
| 332 | /* |
| 333 | * Copy flash into RAM early, same time. |
| 334 | * The kernel has created the LPC->AHB mapping also, which means |
| 335 | * flash should work. |
| 336 | * Ideally we tell the kernel whats up and when to do stuff... |
| 337 | */ |
| 338 | MSG_OUT("Loading flash into ram at %p for 0x%08x bytes\n", |
| 339 | context->lpc_mem, context->size); |
| 340 | if (lseek(context->fds[MTD_FD].fd, 0, SEEK_SET) != 0) { |
| 341 | r = -errno; |
| 342 | MSG_ERR("Couldn't reset MBOX pos to zero\n"); |
| 343 | return r; |
| 344 | } |
| 345 | r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size); |
| 346 | if (r != context->size) { |
| 347 | r = -errno; |
| 348 | MSG_ERR("Couldn't copy mtd into ram: %d\n", r); |
| 349 | return r; |
| 350 | } |
| 351 | return 0; |
| 352 | } |
| 353 | |
| 354 | void signal_hup(int signum, siginfo_t *info, void *uc) |
| 355 | { |
| 356 | sighup = 1; |
| 357 | } |
| 358 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 359 | static void usage(const char *name) |
| 360 | { |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 361 | fprintf(stderr, "Usage %s [ -v[v] | --syslog ] --flash=size[K | M]\n", name); |
| 362 | fprintf(stderr, "\t--flash size[K | M]\t Map the flash for the according to 'size' in Kilobytes or Megabytes\n"); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 363 | fprintf(stderr, "\t--verbose\t Be [more] verbose\n"); |
| 364 | fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without -v)\n\n"); |
| 365 | } |
| 366 | |
| 367 | int main(int argc, char *argv[]) |
| 368 | { |
| 369 | struct mbox_context *context; |
| 370 | const char *name = argv[0]; |
| 371 | char *pnor_filename = NULL; |
| 372 | int opt, polled, r, i; |
Cyril Bur | ef315c1 | 2017-01-19 10:49:34 +1100 | [diff] [blame^] | 373 | struct aspeed_lpc_ctrl_mapping map = { 0 }; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 374 | struct sigaction act; |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 375 | char *endptr; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 376 | |
| 377 | static const struct option long_options[] = { |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 378 | { "flash", required_argument, 0, 'f' }, |
| 379 | { "verbose", no_argument, 0, 'v' }, |
| 380 | { "syslog", no_argument, 0, 's' }, |
| 381 | { 0, 0, 0, 0 } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 382 | }; |
| 383 | |
| 384 | context = calloc(1, sizeof(*context)); |
| 385 | for (i = 0; i < TOTAL_FDS; i++) |
| 386 | context->fds[i].fd = -1; |
| 387 | |
| 388 | mbox_vlog = &mbox_log_console; |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 389 | while ((opt = getopt_long(argc, argv, "fv", long_options, NULL)) != -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 390 | switch (opt) { |
| 391 | case 0: |
| 392 | break; |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 393 | case 'f': |
| 394 | context->flash_size = strtol(optarg, &endptr, 0); |
| 395 | if (optarg == endptr) { |
| 396 | fprintf(stderr, "Unparseable flash size\n"); |
| 397 | usage(name); |
| 398 | exit(EXIT_FAILURE); |
| 399 | } |
| 400 | if (*endptr == 'K') { |
| 401 | context->flash_size <<= 10; |
| 402 | } else if (*endptr == 'M') { |
| 403 | context->flash_size <<= 20; |
| 404 | } else if (*endptr != '\0') { /* Unknown units */ |
| 405 | fprintf(stderr, "Unknown units '%c'\n", *endptr); |
| 406 | usage(name); |
| 407 | exit(EXIT_FAILURE); |
| 408 | } |
| 409 | break; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 410 | case 'v': |
| 411 | verbosity++; |
| 412 | break; |
| 413 | case 's': |
| 414 | /* Avoid a double openlog() */ |
| 415 | if (mbox_vlog != &vsyslog) { |
| 416 | openlog(PREFIX, LOG_ODELAY, LOG_DAEMON); |
| 417 | mbox_vlog = &vsyslog; |
| 418 | } |
| 419 | break; |
| 420 | default: |
| 421 | usage(name); |
| 422 | exit(EXIT_FAILURE); |
| 423 | } |
| 424 | } |
| 425 | |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 426 | if (context->flash_size == 0) { |
| 427 | fprintf(stderr, "Must specify a non-zero flash size\n"); |
| 428 | usage(name); |
| 429 | exit(EXIT_FAILURE); |
| 430 | } |
| 431 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 432 | if (verbosity == MBOX_LOG_VERBOSE) |
| 433 | MSG_OUT("Verbose logging\n"); |
| 434 | |
| 435 | if (verbosity == MBOX_LOG_DEBUG) |
| 436 | MSG_OUT("Debug logging\n"); |
| 437 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 438 | MSG_OUT("Registering SigHUP hander\n"); |
| 439 | act.sa_sigaction = signal_hup; |
| 440 | sigemptyset(&act.sa_mask); |
| 441 | act.sa_flags = SA_SIGINFO; |
| 442 | if (sigaction(SIGHUP, &act, NULL) < 0) { |
| 443 | perror("Registering SIGHUP"); |
| 444 | exit(1); |
| 445 | } |
| 446 | sighup = 0; |
| 447 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 448 | MSG_OUT("Starting\n"); |
| 449 | |
| 450 | MSG_OUT("Opening %s\n", MBOX_HOST_PATH); |
| 451 | context->fds[MBOX_FD].fd = open(MBOX_HOST_PATH, O_RDWR | O_NONBLOCK); |
| 452 | if (context->fds[MBOX_FD].fd < 0) { |
| 453 | r = -errno; |
| 454 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 455 | MBOX_HOST_PATH, strerror(errno)); |
| 456 | goto finish; |
| 457 | } |
| 458 | |
| 459 | MSG_OUT("Opening %s\n", LPC_CTRL_PATH); |
| 460 | context->fds[LPC_CTRL_FD].fd = open(LPC_CTRL_PATH, O_RDWR | O_SYNC); |
| 461 | if (context->fds[LPC_CTRL_FD].fd < 0) { |
| 462 | r = -errno; |
| 463 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 464 | LPC_CTRL_PATH, strerror(errno)); |
| 465 | goto finish; |
| 466 | } |
| 467 | |
| 468 | MSG_OUT("Getting buffer size...\n"); |
| 469 | /* This may become more variable in the future */ |
| 470 | context->pgsize = 12; /* 4K */ |
| 471 | map.window_type = ASPEED_LPC_CTRL_WINDOW_MEMORY; |
| 472 | map.window_id = 0; /* Theres only one */ |
| 473 | if (ioctl(context->fds[LPC_CTRL_FD].fd, ASPEED_LPC_CTRL_IOCTL_GET_SIZE, |
| 474 | &map) < 0) { |
| 475 | r = -errno; |
| 476 | MSG_OUT("fail\n"); |
| 477 | MSG_ERR("Couldn't get lpc control buffer size: %s\n", strerror(-r)); |
| 478 | goto finish; |
| 479 | } |
| 480 | /* And strip the first nibble, LPC access speciality */ |
| 481 | context->size = map.size; |
| 482 | context->base = -context->size & 0x0FFFFFFF; |
| 483 | |
| 484 | /* READ THE COMMENT AT THE START OF THIS FUNCTION! */ |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 485 | r = point_to_flash(context); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 486 | if (r) { |
| 487 | MSG_ERR("Failed to point the LPC BUS at the actual flash: %s\n", |
| 488 | strerror(-r)); |
| 489 | goto finish; |
| 490 | } |
| 491 | |
| 492 | MSG_OUT("Mapping %s for %u\n", LPC_CTRL_PATH, context->size); |
| 493 | context->lpc_mem = mmap(NULL, context->size, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 494 | context->fds[LPC_CTRL_FD].fd, 0); |
| 495 | if (context->lpc_mem == MAP_FAILED) { |
| 496 | r = -errno; |
| 497 | MSG_ERR("Didn't manage to mmap %s: %s\n", LPC_CTRL_PATH, strerror(errno)); |
| 498 | goto finish; |
| 499 | } |
| 500 | |
| 501 | pnor_filename = get_dev_mtd(); |
| 502 | if (!pnor_filename) { |
| 503 | MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n"); |
| 504 | r = -1; |
| 505 | goto finish; |
| 506 | } |
| 507 | |
| 508 | MSG_OUT("Opening %s\n", pnor_filename); |
| 509 | context->fds[MTD_FD].fd = open(pnor_filename, O_RDWR); |
| 510 | if (context->fds[MTD_FD].fd < 0) { |
| 511 | r = -errno; |
| 512 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 513 | pnor_filename, strerror(errno)); |
| 514 | goto finish; |
| 515 | } |
| 516 | |
| 517 | if (ioctl(context->fds[MTD_FD].fd, MEMGETINFO, &context->mtd_info) == -1) { |
| 518 | MSG_ERR("Couldn't get information about MTD: %s\n", strerror(errno)); |
| 519 | return -1; |
| 520 | } |
| 521 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 522 | if (copy_flash(context)) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 523 | goto finish; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 524 | |
| 525 | context->fds[MBOX_FD].events = POLLIN; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 526 | |
Cyril Bur | d8f6d7a | 2017-01-10 18:11:16 +1100 | [diff] [blame] | 527 | /* Test the single write facility by setting all the regs to 0xFF */ |
| 528 | MSG_OUT("Setting all MBOX regs to 0xff individually...\n"); |
| 529 | for (i = 0; i < MBOX_REG_BYTES; i++) { |
| 530 | uint8_t byte = 0xff; |
| 531 | off_t pos; |
| 532 | int len; |
| 533 | |
| 534 | pos = lseek(context->fds[MBOX_FD].fd, i, SEEK_SET); |
| 535 | if (pos != i) { |
| 536 | MSG_ERR("Couldn't lseek() to byte %d: %s\n", i, |
| 537 | strerror(errno)); |
| 538 | break; |
| 539 | } |
| 540 | len = write(context->fds[MBOX_FD].fd, &byte, 1); |
| 541 | if (len != 1) { |
| 542 | MSG_ERR("Couldn't write MBOX reg %d: %s\n", i, |
| 543 | strerror(errno)); |
| 544 | break; |
| 545 | } |
| 546 | } |
| 547 | if (lseek(context->fds[MBOX_FD].fd, 0, SEEK_SET) != 0) { |
| 548 | r = -errno; |
| 549 | MSG_ERR("Couldn't reset MBOX pos to zero\n"); |
| 550 | goto finish; |
| 551 | } |
| 552 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 553 | MSG_OUT("Entering polling loop\n"); |
| 554 | while (running) { |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 555 | polled = poll(context->fds, POLL_FDS, 1000); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 556 | if (polled == 0) |
| 557 | continue; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 558 | if ((polled == -1) && (errno != -EINTR) && (sighup == 1)) { |
| 559 | /* Got sighup. reset to point to flash and |
| 560 | * reread flash */ |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 561 | r = point_to_flash(context); |
| 562 | if (r) { |
| 563 | goto finish; |
| 564 | } |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 565 | r = copy_flash(context); |
| 566 | if (r) |
| 567 | goto finish; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 568 | sighup = 0; |
| 569 | continue; |
| 570 | } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 571 | if (polled < 0) { |
| 572 | r = -errno; |
| 573 | MSG_ERR("Error from poll(): %s\n", strerror(errno)); |
| 574 | break; |
| 575 | } |
| 576 | r = dispatch_mbox(context); |
| 577 | if (r < 0) { |
| 578 | MSG_ERR("Error handling MBOX event: %s\n", strerror(-r)); |
| 579 | break; |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | MSG_OUT("Exiting\n"); |
| 584 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 585 | finish: |
| 586 | if (context->lpc_mem) |
| 587 | munmap(context->lpc_mem, context->size); |
| 588 | |
| 589 | free(pnor_filename); |
| 590 | close(context->fds[MTD_FD].fd); |
| 591 | close(context->fds[LPC_CTRL_FD].fd); |
| 592 | close(context->fds[MBOX_FD].fd); |
| 593 | free(context); |
| 594 | |
| 595 | return r; |
| 596 | } |
| 597 | |