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 | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 82 | struct aspeed_lpc_ctrl_mapping map; |
| 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 |
| 100 | * want space 0 |
| 101 | */ |
| 102 | map.addr = (0UL - context->flash_size) & 0x0fffffff; |
| 103 | map.size = context->flash_size; |
Cyril Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 104 | map.offset = 0; |
| 105 | map.window_type = ASPEED_LPC_CTRL_WINDOW_FLASH; |
| 106 | map.window_id = 0; /* Theres only one */ |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 107 | |
| 108 | MSG_OUT("Pointing HOST LPC bus at the actual flash\n"); |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 109 | MSG_OUT("Assuming %dMB of flash: HOST LPC 0x%08x\n", context->flash_size >> 20, |
| 110 | map.addr); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 111 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 112 | 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] | 113 | r = -errno; |
| 114 | MSG_ERR("Couldn't MAP the host LPC bus to the platform flash\n"); |
| 115 | } |
| 116 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 117 | return r; |
| 118 | } |
| 119 | |
| 120 | static 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 Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 132 | if (ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info) == -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 133 | MSG_ERR("Couldn't MEMERASE ioctl, flash write lost: %s\n", strerror(errno)); |
| 134 | return -1; |
| 135 | } |
| 136 | |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 137 | 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] | 138 | 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 Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 143 | 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] | 144 | 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 | */ |
| 161 | static 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 Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 203 | r = point_to_flash(context); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 204 | 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 Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 217 | r = ioctl(context->fds[LPC_CTRL_FD].fd, |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 218 | 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 Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 239 | if (context->dirty) { |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 240 | r = read(context->fds[MTD_FD].fd, context->lpc_mem, context->size); |
Andrew Jeffery | 78210b9 | 2017-01-13 13:06:09 +1030 | [diff] [blame] | 241 | if (r != context->size) { |
| 242 | MSG_ERR("Short read: %d expecting %"PRIu32"\n", r, context->size); |
| 243 | goto out; |
| 244 | } |
| 245 | } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 246 | 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 | |
| 323 | out: |
| 324 | return r; |
| 325 | } |
| 326 | |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 327 | int 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 | |
| 353 | void signal_hup(int signum, siginfo_t *info, void *uc) |
| 354 | { |
| 355 | sighup = 1; |
| 356 | } |
| 357 | |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 358 | static void usage(const char *name) |
| 359 | { |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 360 | 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 362 | 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 | |
| 366 | int 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 Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 373 | struct sigaction act; |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 374 | char *endptr; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 375 | |
| 376 | static const struct option long_options[] = { |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 377 | { "flash", required_argument, 0, 'f' }, |
| 378 | { "verbose", no_argument, 0, 'v' }, |
| 379 | { "syslog", no_argument, 0, 's' }, |
| 380 | { 0, 0, 0, 0 } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 381 | }; |
| 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 Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 388 | while ((opt = getopt_long(argc, argv, "fv", long_options, NULL)) != -1) { |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 389 | switch (opt) { |
| 390 | case 0: |
| 391 | break; |
Cyril Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 392 | 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 409 | 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 Bur | e8f2de1 | 2017-01-17 16:56:02 +1100 | [diff] [blame] | 425 | 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 431 | 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 Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 437 | 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 447 | 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 Bur | 4623367 | 2017-01-16 13:33:26 +1100 | [diff] [blame] | 484 | r = point_to_flash(context); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 485 | 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 Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 521 | if (copy_flash(context)) |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 522 | goto finish; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 523 | |
| 524 | context->fds[MBOX_FD].events = POLLIN; |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 525 | |
Cyril Bur | d8f6d7a | 2017-01-10 18:11:16 +1100 | [diff] [blame] | 526 | /* 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 552 | MSG_OUT("Entering polling loop\n"); |
| 553 | while (running) { |
Michael Neuling | 622992b | 2017-01-17 11:09:15 -0600 | [diff] [blame] | 554 | polled = poll(context->fds, POLL_FDS, 1000); |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 555 | if (polled == 0) |
| 556 | continue; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 557 | if ((polled == -1) && (errno != -EINTR) && (sighup == 1)) { |
| 558 | /* Got sighup. reset to point to flash and |
| 559 | * reread flash */ |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 560 | r = point_to_flash(context); |
| 561 | if (r) { |
| 562 | goto finish; |
| 563 | } |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 564 | r = copy_flash(context); |
| 565 | if (r) |
| 566 | goto finish; |
Michael Neuling | 899ebac | 2017-01-14 11:20:26 -0600 | [diff] [blame] | 567 | sighup = 0; |
| 568 | continue; |
| 569 | } |
Cyril Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 570 | 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 Bur | c85e34d | 2016-11-15 11:50:41 +1100 | [diff] [blame] | 584 | finish: |
| 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 | |