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 | |
| 78 | /* We know the erase size so we can allocate the flash_erased bytemap */ |
| 79 | context->erase_size_shift = log_2(context->mtd_info.erasesize); |
| 80 | context->flash_bmap = calloc(context->flash_size >> |
| 81 | context->erase_size_shift, |
| 82 | sizeof(*context->flash_bmap)); |
| 83 | |
| 84 | out: |
| 85 | free(filename); |
| 86 | return rc; |
| 87 | } |
| 88 | |
| 89 | void free_flash_dev(struct mbox_context *context) |
| 90 | { |
| 91 | free(context->flash_bmap); |
| 92 | close(context->fds[MTD_FD].fd); |
| 93 | } |
| 94 | |
| 95 | /* Flash Functions */ |
| 96 | |
| 97 | #define CHUNKSIZE (64 * 1024) |
| 98 | |
| 99 | /* |
| 100 | * copy_flash() - Copy data from the flash device into a provided buffer |
| 101 | * @context: The mbox context pointer |
| 102 | * @offset: The flash offset to copy from (bytes) |
| 103 | * @mem: The buffer to copy into (must be of atleast size) |
| 104 | * @size: The number of bytes to copy |
| 105 | * |
| 106 | * Return: 0 on success otherwise negative error code |
| 107 | */ |
| 108 | int copy_flash(struct mbox_context *context, uint32_t offset, void *mem, |
| 109 | uint32_t size) |
| 110 | { |
| 111 | MSG_OUT("Loading flash at %p for 0x%08x bytes from offset 0x%.8x\n", |
| 112 | mem, size, offset); |
| 113 | if (lseek(context->fds[MTD_FD].fd, offset, SEEK_SET) != offset) { |
| 114 | MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset, |
| 115 | strerror(errno)); |
| 116 | return -MBOX_R_SYSTEM_ERROR; |
| 117 | } |
| 118 | |
| 119 | while (size) { |
| 120 | uint32_t size_read = read(context->fds[MTD_FD].fd, mem, |
| 121 | min_u32(CHUNKSIZE, size)); |
| 122 | if (size_read < 0) { |
| 123 | MSG_ERR("Couldn't copy mtd into ram: %d. %s\n", |
| 124 | size_read, strerror(size_read)); |
| 125 | return -MBOX_R_SYSTEM_ERROR; |
| 126 | } |
| 127 | |
| 128 | size -= size_read; |
| 129 | mem += size_read; |
| 130 | } |
| 131 | |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * flash_is_erased() - Check if an offset into flash is erased |
| 137 | * @context: The mbox context pointer |
| 138 | * @offset: The flash offset to check (bytes) |
| 139 | * |
| 140 | * Return: true if erased otherwise false |
| 141 | */ |
| 142 | static inline bool flash_is_erased(struct mbox_context *context, |
| 143 | uint32_t offset) |
| 144 | { |
| 145 | return context->flash_bmap[offset >> context->erase_size_shift] |
| 146 | == FLASH_ERASED; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * set_flash_bytemap() - Set the flash erased bytemap |
| 151 | * @context: The mbox context pointer |
| 152 | * @offset: The flash offset to set (bytes) |
| 153 | * @count: Number of bytes to set |
| 154 | * @val: Value to set the bytemap to |
| 155 | * |
| 156 | * The flash bytemap only tracks the erased status at the erase block level so |
| 157 | * this will update the erased state for an (or many) erase blocks |
| 158 | * |
| 159 | * Return: 0 if success otherwise negative error code |
| 160 | */ |
| 161 | int set_flash_bytemap(struct mbox_context *context, uint32_t offset, |
| 162 | uint32_t count, uint8_t val) |
| 163 | { |
| 164 | if ((offset + count) > context->flash_size) { |
| 165 | return -MBOX_R_PARAM_ERROR; |
| 166 | } |
| 167 | |
| 168 | memset(context->flash_bmap + (offset >> context->erase_size_shift), |
| 169 | val, |
| 170 | align_up(count, 1 << context->erase_size_shift) >> |
| 171 | context->erase_size_shift); |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | /* |
| 177 | * erase_flash() - Erase the flash |
| 178 | * @context: The mbox context pointer |
| 179 | * @offset: The flash offset to erase (bytes) |
| 180 | * @size: The number of bytes to erase |
| 181 | * |
| 182 | * Return: 0 on success otherwise negative error code |
| 183 | */ |
| 184 | int erase_flash(struct mbox_context *context, uint32_t offset, uint32_t count) |
| 185 | { |
| 186 | const uint32_t erase_size = 1 << context->erase_size_shift; |
| 187 | struct erase_info_user erase_info = { 0 }; |
| 188 | int rc; |
| 189 | |
| 190 | MSG_OUT("Erasing 0x%.8x for 0x%.8x\n", offset, count); |
| 191 | |
| 192 | /* |
| 193 | * We have an erased_bytemap for the flash so we want to avoid erasing |
| 194 | * blocks which we already know to be erased. Look for runs of blocks |
| 195 | * which aren't erased and erase the entire run at once to avoid how |
| 196 | * often we have to call the erase ioctl. If the block is already |
| 197 | * erased then there's nothing we need to do. |
| 198 | */ |
| 199 | while (count) { |
| 200 | if (!flash_is_erased(context, offset)) { /* Need to erase */ |
| 201 | if (!erase_info.length) { /* Start of not-erased run */ |
| 202 | erase_info.start = offset; |
| 203 | } |
| 204 | erase_info.length += erase_size; |
| 205 | } else if (erase_info.length) { /* Already erased|end of run? */ |
| 206 | /* Erase the previous run which just ended */ |
| 207 | rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, |
| 208 | &erase_info); |
| 209 | if (rc < 0) { |
| 210 | MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 211 | erase_info.start); |
| 212 | return -MBOX_R_SYSTEM_ERROR; |
| 213 | } |
| 214 | /* Mark ERASED where we just erased */ |
| 215 | set_flash_bytemap(context, erase_info.start, |
| 216 | erase_info.length, FLASH_ERASED); |
| 217 | erase_info.start = 0; |
| 218 | erase_info.length = 0; |
| 219 | } |
| 220 | |
| 221 | offset += erase_size; |
| 222 | count -= erase_size; |
| 223 | } |
| 224 | |
| 225 | if (erase_info.length) { |
| 226 | rc = ioctl(context->fds[MTD_FD].fd, MEMERASE, &erase_info); |
| 227 | if (rc < 0) { |
| 228 | MSG_ERR("Couldn't erase flash at 0x%.8x\n", |
| 229 | erase_info.start); |
| 230 | return -MBOX_R_SYSTEM_ERROR; |
| 231 | } |
| 232 | /* Mark ERASED where we just erased */ |
| 233 | set_flash_bytemap(context, erase_info.start, erase_info.length, |
| 234 | FLASH_ERASED); |
| 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * write_flash() - Write the flash from a provided buffer |
| 242 | * @context: The mbox context pointer |
| 243 | * @offset: The flash offset to write to (bytes) |
| 244 | * @buf: The buffer to write from (must be of atleast size) |
| 245 | * @size: The number of bytes to write |
| 246 | * |
| 247 | * Return: 0 on success otherwise negative error code |
| 248 | */ |
| 249 | int write_flash(struct mbox_context *context, uint32_t offset, void *buf, |
| 250 | uint32_t count) |
| 251 | { |
| 252 | uint32_t buf_offset = 0; |
| 253 | int rc; |
| 254 | |
| 255 | MSG_OUT("Writing 0x%.8x for 0x%.8x from %p\n", offset, count, buf); |
| 256 | |
| 257 | if (lseek(context->fds[MTD_FD].fd, offset, SEEK_SET) != offset) { |
| 258 | MSG_ERR("Couldn't seek flash at pos: %u %s\n", offset, |
| 259 | strerror(errno)); |
| 260 | return -MBOX_R_SYSTEM_ERROR; |
| 261 | } |
| 262 | |
| 263 | while (count) { |
| 264 | rc = write(context->fds[MTD_FD].fd, buf + buf_offset, count); |
| 265 | if (rc < 0) { |
| 266 | MSG_ERR("Couldn't write to flash, write lost: %s\n", |
| 267 | strerror(errno)); |
| 268 | return -MBOX_R_WRITE_ERROR; |
| 269 | } |
| 270 | /* Mark *NOT* erased where we just wrote */ |
| 271 | set_flash_bytemap(context, offset + buf_offset, rc, |
| 272 | FLASH_DIRTY); |
| 273 | count -= rc; |
| 274 | buf_offset += rc; |
| 275 | } |
| 276 | |
| 277 | return 0; |
| 278 | } |