David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the flashrom project. |
| 3 | * |
| 4 | * Copyright 2015 Google Inc. |
| 5 | * Copyright 2018-present Facebook, Inc. |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; version 2 of the License. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <ctype.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <mtd/mtd-user.h> |
| 23 | #include <string.h> |
| 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "flash.h" |
| 29 | #include "programmer.h" |
| 30 | |
| 31 | #define LINUX_DEV_ROOT "/dev" |
| 32 | #define LINUX_MTD_SYSFS_ROOT "/sys/class/mtd" |
| 33 | |
| 34 | static FILE *dev_fp = NULL; |
| 35 | |
| 36 | static int mtd_device_is_writeable; |
| 37 | |
| 38 | static int mtd_no_erase; |
| 39 | |
| 40 | /* Size info is presented in bytes in sysfs. */ |
| 41 | static unsigned long int mtd_total_size; |
| 42 | static unsigned long int mtd_numeraseregions; |
| 43 | static unsigned long int mtd_erasesize; /* only valid if numeraseregions is 0 */ |
| 44 | |
| 45 | /* read a string from a sysfs file and sanitize it */ |
| 46 | static int read_sysfs_string(const char *sysfs_path, const char *filename, char *buf, int len) |
| 47 | { |
| 48 | int i; |
| 49 | size_t bytes_read; |
| 50 | FILE *fp; |
| 51 | char path[strlen(LINUX_MTD_SYSFS_ROOT) + 32]; |
| 52 | |
| 53 | snprintf(path, sizeof(path), "%s/%s", sysfs_path, filename); |
| 54 | |
| 55 | if ((fp = fopen(path, "r")) == NULL) { |
| 56 | msg_perr("Cannot open %s\n", path); |
| 57 | return 1; |
| 58 | } |
| 59 | |
| 60 | clearerr(fp); |
| 61 | bytes_read = fread(buf, 1, (size_t)len, fp); |
| 62 | if (!feof(fp) && ferror(fp)) { |
| 63 | msg_perr("Error occurred when reading %s\n", path); |
| 64 | fclose(fp); |
| 65 | return 1; |
| 66 | } |
| 67 | |
| 68 | buf[bytes_read] = '\0'; |
| 69 | |
| 70 | /* |
| 71 | * Files from sysfs sometimes contain a newline or other garbage that |
| 72 | * can confuse functions like strtoul() and ruin formatting in print |
| 73 | * statements. Replace the first non-printable character (space is |
| 74 | * considered printable) with a proper string terminator. |
| 75 | */ |
| 76 | for (i = 0; i < len; i++) { |
| 77 | if (!isprint(buf[i])) { |
| 78 | buf[i] = '\0'; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fclose(fp); |
| 84 | return 0; |
| 85 | } |
| 86 | |
| 87 | static int read_sysfs_int(const char *sysfs_path, const char *filename, unsigned long int *val) |
| 88 | { |
| 89 | char buf[32]; |
| 90 | char *endptr; |
| 91 | |
| 92 | if (read_sysfs_string(sysfs_path, filename, buf, sizeof(buf))) |
| 93 | return 1; |
| 94 | |
| 95 | errno = 0; |
| 96 | *val = strtoul(buf, &endptr, 0); |
| 97 | if (*endptr != '\0') { |
| 98 | msg_perr("Error reading %s\n", filename); |
| 99 | return 1; |
| 100 | } |
| 101 | |
| 102 | if (errno) { |
| 103 | msg_perr("Error reading %s: %s\n", filename, strerror(errno)); |
| 104 | return 1; |
| 105 | } |
| 106 | |
| 107 | return 0; |
| 108 | } |
| 109 | |
| 110 | static int popcnt(unsigned int u) |
| 111 | { |
| 112 | int count = 0; |
| 113 | |
| 114 | while (u) { |
| 115 | u &= u - 1; |
| 116 | count++; |
| 117 | } |
| 118 | |
| 119 | return count; |
| 120 | } |
| 121 | |
| 122 | /* returns 0 to indicate success, non-zero to indicate error */ |
| 123 | static int get_mtd_info(const char *sysfs_path) |
| 124 | { |
| 125 | unsigned long int tmp; |
| 126 | char mtd_device_name[32]; |
| 127 | |
| 128 | /* Flags */ |
| 129 | if (read_sysfs_int(sysfs_path, "flags", &tmp)) |
| 130 | return 1; |
| 131 | if (tmp & MTD_WRITEABLE) { |
| 132 | /* cache for later use by write function */ |
| 133 | mtd_device_is_writeable = 1; |
| 134 | } |
| 135 | if (tmp & MTD_NO_ERASE) { |
| 136 | mtd_no_erase = 1; |
| 137 | } |
| 138 | |
| 139 | /* Device name */ |
| 140 | if (read_sysfs_string(sysfs_path, "name", mtd_device_name, sizeof(mtd_device_name))) |
| 141 | return 1; |
| 142 | |
| 143 | /* Total size */ |
| 144 | if (read_sysfs_int(sysfs_path, "size", &mtd_total_size)) |
| 145 | return 1; |
| 146 | if (popcnt(mtd_total_size) != 1) { |
| 147 | msg_perr("MTD size is not a power of 2\n"); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /* Erase size */ |
| 152 | if (read_sysfs_int(sysfs_path, "erasesize", &mtd_erasesize)) |
| 153 | return 1; |
| 154 | if (popcnt(mtd_erasesize) != 1) { |
| 155 | msg_perr("MTD erase size is not a power of 2\n"); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | /* Erase regions */ |
| 160 | if (read_sysfs_int(sysfs_path, "numeraseregions", &mtd_numeraseregions)) |
| 161 | return 1; |
| 162 | if (mtd_numeraseregions != 0) { |
| 163 | msg_perr("Non-uniform eraseblock size is unsupported.\n"); |
| 164 | return 1; |
| 165 | } |
| 166 | |
| 167 | msg_pdbg("%s: device_name: \"%s\", is_writeable: %d, " |
| 168 | "numeraseregions: %lu, total_size: %lu, erasesize: %lu\n", |
| 169 | __func__, mtd_device_name, mtd_device_is_writeable, |
| 170 | mtd_numeraseregions, mtd_total_size, mtd_erasesize); |
| 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int linux_mtd_probe(struct flashctx *flash) |
| 176 | { |
| 177 | if (mtd_no_erase) |
| 178 | flash->chip->feature_bits |= FEATURE_NO_ERASE; |
| 179 | flash->chip->tested = TEST_OK_PREW; |
| 180 | flash->chip->total_size = mtd_total_size / 1024; /* bytes -> kB */ |
| 181 | flash->chip->block_erasers[0].eraseblocks[0].size = mtd_erasesize; |
| 182 | flash->chip->block_erasers[0].eraseblocks[0].count = mtd_total_size / mtd_erasesize; |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | static int linux_mtd_read(struct flashctx *flash, uint8_t *buf, |
| 187 | unsigned int start, unsigned int len) |
| 188 | { |
| 189 | unsigned int eb_size = flash->chip->block_erasers[0].eraseblocks[0].size; |
| 190 | unsigned int i; |
| 191 | |
| 192 | if (fseek(dev_fp, start, SEEK_SET) != 0) { |
| 193 | msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno)); |
| 194 | return 1; |
| 195 | } |
| 196 | |
| 197 | for (i = 0; i < len; ) { |
| 198 | /* |
| 199 | * Try to align reads to eraseblock size. |
| 200 | * FIXME: Shouldn't actually be necessary, but not all MTD |
| 201 | * drivers handle arbitrary large reads well. |
| 202 | */ |
| 203 | unsigned int step = eb_size - ((start + i) % eb_size); |
| 204 | step = min(step, len - i); |
| 205 | |
| 206 | if (fread(buf + i, step, 1, dev_fp) != 1) { |
| 207 | msg_perr("Cannot read 0x%06x bytes at 0x%06x: %s\n", |
| 208 | step, start + i, strerror(errno)); |
| 209 | return 1; |
| 210 | } |
| 211 | |
| 212 | i += step; |
| 213 | } |
| 214 | |
| 215 | return 0; |
| 216 | } |
| 217 | |
| 218 | /* this version assumes we must divide the write request into chunks ourselves */ |
| 219 | static int linux_mtd_write(struct flashctx *flash, const uint8_t *buf, |
| 220 | unsigned int start, unsigned int len) |
| 221 | { |
| 222 | unsigned int chunksize = flash->chip->block_erasers[0].eraseblocks[0].size; |
| 223 | unsigned int i; |
| 224 | |
| 225 | if (!mtd_device_is_writeable) |
| 226 | return 1; |
| 227 | |
| 228 | if (fseek(dev_fp, start, SEEK_SET) != 0) { |
| 229 | msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno)); |
| 230 | return 1; |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * Try to align writes to eraseblock size. We want these large enough |
| 235 | * to give MTD room for optimizing performance. |
| 236 | * FIXME: Shouldn't need to divide this up at all, but not all MTD |
| 237 | * drivers handle arbitrary large writes well. |
| 238 | */ |
| 239 | for (i = 0; i < len; ) { |
| 240 | unsigned int step = chunksize - ((start + i) % chunksize); |
| 241 | step = min(step, len - i); |
| 242 | |
| 243 | if (fwrite(buf + i, step, 1, dev_fp) != 1) { |
| 244 | msg_perr("Cannot write 0x%06x bytes at 0x%06x\n", step, start + i); |
| 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | if (fflush(dev_fp) == EOF) { |
| 249 | msg_perr("Failed to flush buffer: %s\n", strerror(errno)); |
| 250 | return 1; |
| 251 | } |
| 252 | |
| 253 | i += step; |
| 254 | } |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int linux_mtd_erase(struct flashctx *flash, |
| 260 | unsigned int start, unsigned int len) |
| 261 | { |
| 262 | uint32_t u; |
| 263 | |
| 264 | if (mtd_no_erase) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 265 | msg_perr("%s: device does not support erasing.\n" |
| 266 | "Please file a bug report at flashrom-stable@flashrom.org\n", |
| 267 | __func__); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 268 | return 1; |
| 269 | } |
| 270 | |
| 271 | if (mtd_numeraseregions != 0) { |
| 272 | /* TODO: Support non-uniform eraseblock size using |
| 273 | use MEMGETREGIONCOUNT/MEMGETREGIONINFO ioctls */ |
| 274 | msg_perr("%s: mtd_numeraseregions must be 0\n", __func__); |
| 275 | return 1; |
| 276 | } |
| 277 | |
| 278 | for (u = 0; u < len; u += mtd_erasesize) { |
| 279 | struct erase_info_user erase_info = { |
| 280 | .start = start + u, |
| 281 | .length = mtd_erasesize, |
| 282 | }; |
| 283 | |
Nikolai Artemiev | 04fce47 | 2022-01-11 18:26:48 +1100 | [diff] [blame] | 284 | int ret = ioctl(fileno(dev_fp), MEMERASE, &erase_info); |
| 285 | if (ret < 0) { |
| 286 | msg_perr("%s: MEMERASE ioctl call returned %d, error: %s\n", |
| 287 | __func__, ret, strerror(errno)); |
| 288 | return 1; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 295 | static const struct opaque_master linux_mtd_opaque_master = { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 296 | /* max_data_{read,write} don't have any effect for this programmer */ |
| 297 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 298 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 299 | .probe = linux_mtd_probe, |
| 300 | .read = linux_mtd_read, |
| 301 | .write = linux_mtd_write, |
| 302 | .erase = linux_mtd_erase, |
| 303 | }; |
| 304 | |
| 305 | /* Returns 0 if setup is successful, non-zero to indicate error */ |
| 306 | static int linux_mtd_setup(int dev_num) |
| 307 | { |
| 308 | char sysfs_path[32]; |
| 309 | int ret = 1; |
| 310 | |
| 311 | /* Start by checking /sys/class/mtd/mtdN/type which should be "nor" for NOR flash */ |
| 312 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 313 | goto linux_mtd_setup_exit; |
| 314 | |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 315 | char buf[4] = { 0 }; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 316 | if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf))) |
| 317 | return 1; |
| 318 | |
| 319 | if (strcmp(buf, "nor")) { |
| 320 | msg_perr("MTD device %d type is not \"nor\"\n", dev_num); |
| 321 | goto linux_mtd_setup_exit; |
| 322 | } |
| 323 | |
| 324 | /* sysfs shows the correct device type, see if corresponding device node exists */ |
| 325 | char dev_path[32]; |
| 326 | struct stat s; |
| 327 | snprintf(dev_path, sizeof(dev_path), "%s/mtd%d", LINUX_DEV_ROOT, dev_num); |
| 328 | errno = 0; |
| 329 | if (stat(dev_path, &s) < 0) { |
| 330 | msg_pdbg("Cannot stat \"%s\": %s\n", dev_path, strerror(errno)); |
| 331 | goto linux_mtd_setup_exit; |
| 332 | } |
| 333 | |
| 334 | /* so far so good, get more info from other files in this dir */ |
| 335 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 336 | goto linux_mtd_setup_exit; |
| 337 | if (get_mtd_info(sysfs_path)) |
| 338 | goto linux_mtd_setup_exit; |
| 339 | |
| 340 | /* open file stream and go! */ |
| 341 | if ((dev_fp = fopen(dev_path, "r+")) == NULL) { |
| 342 | msg_perr("Cannot open file stream for %s\n", dev_path); |
| 343 | goto linux_mtd_setup_exit; |
| 344 | } |
Douglas Anderson | 595c5d0 | 2021-01-29 16:35:24 -0800 | [diff] [blame] | 345 | ret = setvbuf(dev_fp, NULL, _IONBF, 0); |
| 346 | if (ret) |
| 347 | msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret); |
| 348 | |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 349 | msg_pinfo("Opened %s successfully\n", dev_path); |
| 350 | |
| 351 | ret = 0; |
| 352 | linux_mtd_setup_exit: |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int linux_mtd_shutdown(void *data) |
| 357 | { |
| 358 | if (dev_fp != NULL) { |
| 359 | fclose(dev_fp); |
| 360 | dev_fp = NULL; |
| 361 | } |
| 362 | |
| 363 | return 0; |
| 364 | } |
| 365 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 366 | static int linux_mtd_init(void) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 367 | { |
| 368 | char *param; |
| 369 | int dev_num = 0; |
| 370 | int ret = 1; |
| 371 | |
| 372 | param = extract_programmer_param("dev"); |
| 373 | if (param) { |
| 374 | char *endptr; |
| 375 | |
| 376 | dev_num = strtol(param, &endptr, 0); |
| 377 | if ((*endptr != '\0') || (dev_num < 0)) { |
| 378 | msg_perr("Invalid device number %s. Use flashrom -p " |
| 379 | "linux_mtd:dev=N where N is a valid MTD\n" |
| 380 | "device number.\n", param); |
| 381 | goto linux_mtd_init_exit; |
| 382 | } |
| 383 | } |
| 384 | |
David Hendricks | b0247b3 | 2018-05-23 21:50:18 -0700 | [diff] [blame] | 385 | /* |
| 386 | * If user specified the MTD device number then error out if it doesn't |
| 387 | * appear to exist. Otherwise assume the error is benign and print a |
| 388 | * debug message. Bail out in either case. |
| 389 | */ |
| 390 | char sysfs_path[32]; |
| 391 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 392 | goto linux_mtd_init_exit; |
| 393 | |
| 394 | struct stat s; |
| 395 | if (stat(sysfs_path, &s) < 0) { |
| 396 | if (param) |
| 397 | msg_perr("%s does not exist\n", sysfs_path); |
| 398 | else |
| 399 | msg_pdbg("%s does not exist\n", sysfs_path); |
| 400 | goto linux_mtd_init_exit; |
| 401 | } |
| 402 | |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 403 | if (linux_mtd_setup(dev_num)) |
| 404 | goto linux_mtd_init_exit; |
| 405 | |
| 406 | if (register_shutdown(linux_mtd_shutdown, NULL)) |
| 407 | goto linux_mtd_init_exit; |
| 408 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 409 | register_opaque_master(&linux_mtd_opaque_master); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 410 | |
| 411 | ret = 0; |
| 412 | linux_mtd_init_exit: |
Jacob Garber | ba71999 | 2019-08-12 12:07:03 -0600 | [diff] [blame] | 413 | free(param); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 414 | return ret; |
| 415 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 416 | |
| 417 | const struct programmer_entry programmer_linux_mtd = { |
| 418 | .name = "linux_mtd", |
| 419 | .type = OTHER, |
| 420 | .devs.note = "Device files /dev/mtd*\n", |
| 421 | .init = linux_mtd_init, |
| 422 | .map_flash_region = fallback_map, |
| 423 | .unmap_flash_region = fallback_unmap, |
| 424 | .delay = internal_delay, |
| 425 | }; |