Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 1 | /* |
| 2 | * Mailbox Daemon Flash Helpers |
| 3 | * |
| 4 | * Copyright 2016 IBM |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | * |
| 18 | */ |
| 19 | |
| 20 | #define _GNU_SOURCE |
| 21 | #include <assert.h> |
| 22 | #include <errno.h> |
| 23 | #include <fcntl.h> |
| 24 | #include <getopt.h> |
| 25 | #include <limits.h> |
| 26 | #include <poll.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <stdint.h> |
| 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
| 31 | #include <string.h> |
| 32 | #include <syslog.h> |
| 33 | #include <signal.h> |
| 34 | #include <sys/ioctl.h> |
| 35 | #include <sys/mman.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/timerfd.h> |
| 38 | #include <sys/types.h> |
| 39 | #include <time.h> |
| 40 | #include <unistd.h> |
| 41 | #include <inttypes.h> |
| 42 | #include <mtd/mtd-abi.h> |
| 43 | |
| 44 | #include "mbox.h" |
| 45 | #include "common.h" |
| 46 | #include "mboxd_flash.h" |
| 47 | |
| 48 | int init_flash_dev(struct mbox_context *context) |
| 49 | { |
| 50 | char *filename = get_dev_mtd(); |
| 51 | int fd, rc = 0; |
| 52 | |
| 53 | if (!filename) { |
| 54 | MSG_ERR("Couldn't find the PNOR /dev/mtd partition\n"); |
| 55 | return -1; |
| 56 | } |
| 57 | |
| 58 | MSG_OUT("Opening %s\n", filename); |
| 59 | |
| 60 | /* Open Flash Device */ |
| 61 | fd = open(filename, O_RDWR); |
| 62 | if (fd < 0) { |
| 63 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", |
| 64 | filename, strerror(errno)); |
| 65 | rc = -errno; |
| 66 | goto out; |
| 67 | } |
| 68 | context->fds[MTD_FD].fd = fd; |
| 69 | |
| 70 | /* Read the Flash Info */ |
| 71 | if (ioctl(fd, MEMGETINFO, &context->mtd_info) == -1) { |
| 72 | MSG_ERR("Couldn't get information about MTD: %s\n", |
| 73 | strerror(errno)); |
| 74 | rc = -1; |
| 75 | goto out; |
| 76 | } |
| 77 | |
Andrew Jeffery | 33640b3 | 2017-04-12 20:56:11 +0930 | [diff] [blame] | 78 | if (context->flash_size == 0) { |
| 79 | /* |
| 80 | * PNOR images for current OpenPOWER systems are at most 64MB |
| 81 | * despite the PNOR itself sometimes being as big as 128MB. To |
| 82 | * ensure the image read from the PNOR is exposed in the LPC |
| 83 | * address space at the location expected by the host firmware, |
| 84 | * it is required that the image size be used for |
| 85 | * context->flash_size, and not the size of the flash device. |
| 86 | * |
| 87 | * However, the test cases specify the flash size via special |
| 88 | * test APIs (controlling flash behaviour) which don't have |
| 89 | * access to the mbox context. Rather than requiring |
| 90 | * error-prone assignments in every test case, we instead rely |
| 91 | * on context->flash_size being set to the size reported by the |
| 92 | * MEMINFO ioctl(). |
| 93 | * |
| 94 | * As this case should never be hit in production (i.e. outside |
| 95 | * the test environment), log an error. As a consequence, this |
| 96 | * error is expected in the test case output. |
| 97 | */ |
| 98 | MSG_ERR("Flash size MUST be supplied on the commandline. However, continuing by assuming flash is %u bytes\n", |
| 99 | context->mtd_info.size); |
| 100 | context->flash_size = context->mtd_info.size; |
| 101 | } |
| 102 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 103 | /* We know the erase size so we can allocate the flash_erased bytemap */ |
| 104 | context->erase_size_shift = log_2(context->mtd_info.erasesize); |
| 105 | context->flash_bmap = calloc(context->flash_size >> |
| 106 | context->erase_size_shift, |
| 107 | sizeof(*context->flash_bmap)); |
| 108 | |
| 109 | out: |
| 110 | free(filename); |
| 111 | return rc; |
| 112 | } |
| 113 | |
| 114 | void free_flash_dev(struct mbox_context *context) |
| 115 | { |
| 116 | free(context->flash_bmap); |
| 117 | close(context->fds[MTD_FD].fd); |
| 118 | } |
| 119 | |
| 120 | /* Flash Functions */ |
| 121 | |
| 122 | #define CHUNKSIZE (64 * 1024) |
| 123 | |
| 124 | /* |
| 125 | * copy_flash() - Copy data from the flash device into a provided buffer |
| 126 | * @context: The mbox context pointer |
| 127 | * @offset: The flash offset to copy from (bytes) |
| 128 | * @mem: The buffer to copy into (must be of atleast size) |
| 129 | * @size: The number of bytes to copy |
| 130 | * |
| 131 | * Return: 0 on success otherwise negative error code |
| 132 | */ |
| 133 | int copy_flash(struct mbox_context *context, uint32_t offset, void *mem, |
| 134 | uint32_t size) |
| 135 | { |
Andrew Jeffery | 9aa5ff7 | 2017-04-12 00:16:56 +0930 | [diff] [blame] | 136 | int32_t size_read; |
| 137 | |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 138 | MSG_OUT("Loading flash at %p for 0x%08x bytes from offset 0x%.8x\n", |
| 139 | mem, size, offset); |
| 140 | if (lseek(context->fds[MTD_FD].fd, offset, SEEK_SET) != offset) { |
| 141 | MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset, |
| 142 | strerror(errno)); |
| 143 | return -MBOX_R_SYSTEM_ERROR; |
| 144 | } |
| 145 | |
Andrew Jeffery | 9aa5ff7 | 2017-04-12 00:16:56 +0930 | [diff] [blame] | 146 | do { |
| 147 | size_read = read(context->fds[MTD_FD].fd, mem, |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 148 | min_u32(CHUNKSIZE, size)); |
| 149 | if (size_read < 0) { |
Andrew Jeffery | 4f88bfb | 2017-04-24 12:34:15 +0930 | [diff] [blame] | 150 | MSG_ERR("Couldn't copy mtd into ram: %s\n", |
| 151 | strerror(errno)); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 152 | return -MBOX_R_SYSTEM_ERROR; |
| 153 | } |
| 154 | |
| 155 | size -= size_read; |
| 156 | mem += size_read; |
Andrew Jeffery | 9aa5ff7 | 2017-04-12 00:16:56 +0930 | [diff] [blame] | 157 | } while (size && size_read); |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 158 | |
Andrew Jeffery | 9aa5ff7 | 2017-04-12 00:16:56 +0930 | [diff] [blame] | 159 | return size ? -MBOX_R_SYSTEM_ERROR : 0; |
Suraj Jitindar Singh | e39c916 | 2017-03-28 10:47:43 +1100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | /* |
| 163 | * flash_is_erased() - Check if an offset into flash is erased |
| 164 | * @context: The mbox context pointer |
| 165 | * @offset: The flash offset to check (bytes) |
| 166 | * |
| 167 | * Return: true if erased otherwise false |
| 168 | */ |
| 169 | static inline bool flash_is_erased(struct mbox_context *context, |
| 170 | uint32_t offset) |
| 171 | { |
| 172 | return context->flash_bmap[offset >> context->erase_size_shift] |
| 173 | == FLASH_ERASED; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * set_flash_bytemap() - Set the flash erased bytemap |
| 178 | * @context: The mbox context pointer |
| 179 | * @offset: The flash offset to set (bytes) |
| 180 | * @count: Number of bytes to set |
| 181 | * @val: Value to set the bytemap to |
| 182 | * |
| 183 | * The flash bytemap only tracks the erased status at the erase block level so |
| 184 | * this will update the erased state for an (or many) erase blocks |
| 185 | * |
| 186 | * Return: 0 if success otherwise negative error code |
| 187 | */ |
| 188 | int set_flash_bytemap(struct mbox_context *context, uint32_t offset, |
| 189 | uint32_t count, uint8_t val) |
| 190 | { |
| 191 | if ((offset + count) > context->flash_size) { |
| 192 | return -MBOX_R_PARAM_ERROR; |
| 193 | } |
| 194 | |
| 195 | memset(context->flash_bmap + (offset >> context->erase_size_shift), |
| 196 | val, |
| 197 | align_up(count, 1 << context->erase_size_shift) >> |
| 198 | context->erase_size_shift); |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
| 203 | /* |
| 204 | * erase_flash() - Erase the flash |
| 205 | * @context: The mbox context pointer |
| 206 | * @offset: The flash offset to erase (bytes) |
| 207 | * @size: The number of bytes to erase |
| 208 | * |
| 209 | * Return: 0 on success otherwise negative error code |
| 210 | */ |
| 211 | int erase_flash(struct mbox_context *context, uint32_t offset, uint32_t count) |
| 212 | { |
| 213 | const uint32_t erase_size = 1 << context->erase_size_shift; |
| 214 | struct erase_info_user erase_info = { 0 }; |
| 215 | int rc; |
| 216 | |
| 217 | MSG_OUT("Erasing 0x%.8x for 0x%.8x\n", offset, count); |
| 218 | |
| 219 | /* |
| 220 | * We have an erased_bytemap for the flash so we want to avoid erasing |
| 221 | * blocks which we already know to be erased. Look for runs of blocks |
| 222 | * which aren't erased and erase the entire run at once to avoid how |
| 223 | * often we have to call the erase ioctl. If the block is already |
| 224 | * erased then there's nothing we need to do. |
| 225 | */ |
| 226 | while (count) { |
| 227 | if (!flash_is_erased(context, offset)) { /* Need to erase */ |
| 228 | if (!erase_info.length) { /* Start of not-erased run */ |
| 229 | erase_info.start = offset; |
| 230 | } |
| 231 | erase_info.length += erase_size; |
| 232 | } else if (erase_info.length) { /* Already erased|end of run? */ |
| 233 | /* Erase the previous run which just ended */ |
| 234 | rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, |
| 235 | &erase_info); |
| 236 | if (rc < 0) { |
| 237 | MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 238 | erase_info.start); |
| 239 | return -MBOX_R_SYSTEM_ERROR; |
| 240 | } |
| 241 | /* Mark ERASED where we just erased */ |
| 242 | set_flash_bytemap(context, erase_info.start, |
| 243 | erase_info.length, FLASH_ERASED); |
| 244 | erase_info.start = 0; |
| 245 | erase_info.length = 0; |
| 246 | } |
| 247 | |
| 248 | offset += erase_size; |
| 249 | count -= erase_size; |
| 250 | } |
| 251 | |
| 252 | if (erase_info.length) { |
| 253 | rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info); |
| 254 | if (rc < 0) { |
| 255 | MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 256 | erase_info.start); |
| 257 | return -MBOX_R_SYSTEM_ERROR; |
| 258 | } |
| 259 | /* Mark ERASED where we just erased */ |
| 260 | set_flash_bytemap(context, erase_info.start, erase_info.length, |
| 261 | FLASH_ERASED); |
| 262 | } |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | * write_flash() - Write the flash from a provided buffer |
| 269 | * @context: The mbox context pointer |
| 270 | * @offset: The flash offset to write to (bytes) |
| 271 | * @buf: The buffer to write from (must be of atleast size) |
| 272 | * @size: The number of bytes to write |
| 273 | * |
| 274 | * Return: 0 on success otherwise negative error code |
| 275 | */ |
| 276 | int write_flash(struct mbox_context *context, uint32_t offset, void *buf, |
| 277 | uint32_t count) |
| 278 | { |
| 279 | uint32_t buf_offset = 0; |
| 280 | int rc; |
| 281 | |
| 282 | MSG_OUT("Writing 0x%.8x for 0x%.8x from %p\n", offset, count, buf); |
| 283 | |
| 284 | if (lseek(context->fds[MTD_FD].fd, offset, SEEK_SET) != offset) { |
| 285 | MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset, |
| 286 | strerror(errno)); |
| 287 | return -MBOX_R_SYSTEM_ERROR; |
| 288 | } |
| 289 | |
| 290 | while (count) { |
| 291 | rc = write(context->fds[MTD_FD].fd, buf + buf_offset, count); |
| 292 | if (rc < 0) { |
| 293 | MSG_ERR("Couldn't write to flash, write lost: %s\n", |
| 294 | strerror(errno)); |
| 295 | return -MBOX_R_WRITE_ERROR; |
| 296 | } |
| 297 | /* Mark *NOT* erased where we just wrote */ |
| 298 | set_flash_bytemap(context, offset + buf_offset, rc, |
| 299 | FLASH_DIRTY); |
| 300 | count -= rc; |
| 301 | buf_offset += rc; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |