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 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 34 | struct linux_mtd_data { |
| 35 | FILE *dev_fp; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 36 | int device_is_writeable; |
| 37 | int no_erase; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 38 | /* Size info is presented in bytes in sysfs. */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 39 | unsigned long int total_size; |
| 40 | unsigned long int numeraseregions; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 41 | /* only valid if numeraseregions is 0 */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 42 | unsigned long int erasesize; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 43 | }; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 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 */ |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 123 | static int get_mtd_info(const char *sysfs_path, struct linux_mtd_data *data) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned long int tmp; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 126 | char device_name[32]; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 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 */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 133 | data->device_is_writeable = 1; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 134 | } |
| 135 | if (tmp & MTD_NO_ERASE) { |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 136 | data->no_erase = 1; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Device name */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 140 | if (read_sysfs_string(sysfs_path, "name", device_name, sizeof(device_name))) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 141 | return 1; |
| 142 | |
| 143 | /* Total size */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 144 | if (read_sysfs_int(sysfs_path, "size", &data->total_size)) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 145 | return 1; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 146 | if (popcnt(data->total_size) != 1) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 147 | msg_perr("MTD size is not a power of 2\n"); |
| 148 | return 1; |
| 149 | } |
| 150 | |
| 151 | /* Erase size */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 152 | if (read_sysfs_int(sysfs_path, "erasesize", &data->erasesize)) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 153 | return 1; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 154 | if (popcnt(data->erasesize) != 1) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 155 | msg_perr("MTD erase size is not a power of 2\n"); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | /* Erase regions */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 160 | if (read_sysfs_int(sysfs_path, "numeraseregions", &data->numeraseregions)) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 161 | return 1; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 162 | if (data->numeraseregions != 0) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 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", |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 169 | __func__, device_name, data->device_is_writeable, |
| 170 | data->numeraseregions, data->total_size, data->erasesize); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 171 | |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | static int linux_mtd_probe(struct flashctx *flash) |
| 176 | { |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 177 | struct linux_mtd_data *data = flash->mst->opaque.data; |
| 178 | |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 179 | if (data->no_erase) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 180 | flash->chip->feature_bits |= FEATURE_NO_ERASE; |
| 181 | flash->chip->tested = TEST_OK_PREW; |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 182 | flash->chip->total_size = data->total_size / 1024; /* bytes -> kB */ |
| 183 | flash->chip->block_erasers[0].eraseblocks[0].size = data->erasesize; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 184 | flash->chip->block_erasers[0].eraseblocks[0].count = |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 185 | data->total_size / data->erasesize; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 186 | return 1; |
| 187 | } |
| 188 | |
| 189 | static int linux_mtd_read(struct flashctx *flash, uint8_t *buf, |
| 190 | unsigned int start, unsigned int len) |
| 191 | { |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 192 | struct linux_mtd_data *data = flash->mst->opaque.data; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 193 | unsigned int eb_size = flash->chip->block_erasers[0].eraseblocks[0].size; |
| 194 | unsigned int i; |
| 195 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 196 | if (fseek(data->dev_fp, start, SEEK_SET) != 0) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 197 | msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno)); |
| 198 | return 1; |
| 199 | } |
| 200 | |
| 201 | for (i = 0; i < len; ) { |
| 202 | /* |
| 203 | * Try to align reads to eraseblock size. |
| 204 | * FIXME: Shouldn't actually be necessary, but not all MTD |
| 205 | * drivers handle arbitrary large reads well. |
| 206 | */ |
| 207 | unsigned int step = eb_size - ((start + i) % eb_size); |
| 208 | step = min(step, len - i); |
| 209 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 210 | if (fread(buf + i, step, 1, data->dev_fp) != 1) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 211 | msg_perr("Cannot read 0x%06x bytes at 0x%06x: %s\n", |
| 212 | step, start + i, strerror(errno)); |
| 213 | return 1; |
| 214 | } |
| 215 | |
| 216 | i += step; |
| 217 | } |
| 218 | |
| 219 | return 0; |
| 220 | } |
| 221 | |
| 222 | /* this version assumes we must divide the write request into chunks ourselves */ |
| 223 | static int linux_mtd_write(struct flashctx *flash, const uint8_t *buf, |
| 224 | unsigned int start, unsigned int len) |
| 225 | { |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 226 | struct linux_mtd_data *data = flash->mst->opaque.data; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 227 | unsigned int chunksize = flash->chip->block_erasers[0].eraseblocks[0].size; |
| 228 | unsigned int i; |
| 229 | |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 230 | if (!data->device_is_writeable) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 231 | return 1; |
| 232 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 233 | if (fseek(data->dev_fp, start, SEEK_SET) != 0) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 234 | msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno)); |
| 235 | return 1; |
| 236 | } |
| 237 | |
| 238 | /* |
| 239 | * Try to align writes to eraseblock size. We want these large enough |
| 240 | * to give MTD room for optimizing performance. |
| 241 | * FIXME: Shouldn't need to divide this up at all, but not all MTD |
| 242 | * drivers handle arbitrary large writes well. |
| 243 | */ |
| 244 | for (i = 0; i < len; ) { |
| 245 | unsigned int step = chunksize - ((start + i) % chunksize); |
| 246 | step = min(step, len - i); |
| 247 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 248 | if (fwrite(buf + i, step, 1, data->dev_fp) != 1) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 249 | msg_perr("Cannot write 0x%06x bytes at 0x%06x\n", step, start + i); |
| 250 | return 1; |
| 251 | } |
| 252 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 253 | if (fflush(data->dev_fp) == EOF) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 254 | msg_perr("Failed to flush buffer: %s\n", strerror(errno)); |
| 255 | return 1; |
| 256 | } |
| 257 | |
| 258 | i += step; |
| 259 | } |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | static int linux_mtd_erase(struct flashctx *flash, |
| 265 | unsigned int start, unsigned int len) |
| 266 | { |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 267 | struct linux_mtd_data *data = flash->mst->opaque.data; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 268 | uint32_t u; |
| 269 | |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 270 | if (data->no_erase) { |
Nico Huber | ac90af6 | 2022-12-18 00:22:47 +0000 | [diff] [blame] | 271 | msg_perr("%s: device does not support erasing.\n" |
| 272 | "Please file a bug report at flashrom-stable@flashrom.org\n", |
| 273 | __func__); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 274 | return 1; |
| 275 | } |
| 276 | |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 277 | if (data->numeraseregions != 0) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 278 | /* TODO: Support non-uniform eraseblock size using |
| 279 | use MEMGETREGIONCOUNT/MEMGETREGIONINFO ioctls */ |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 280 | msg_perr("%s: numeraseregions must be 0\n", __func__); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 281 | return 1; |
| 282 | } |
| 283 | |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 284 | for (u = 0; u < len; u += data->erasesize) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 285 | struct erase_info_user erase_info = { |
| 286 | .start = start + u, |
Nikolai Artemiev | 6c33185 | 2021-05-09 11:37:38 +1000 | [diff] [blame] | 287 | .length = data->erasesize, |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 288 | }; |
| 289 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 290 | int ret = ioctl(fileno(data->dev_fp), MEMERASE, &erase_info); |
Nikolai Artemiev | 04fce47 | 2022-01-11 18:26:48 +1100 | [diff] [blame] | 291 | if (ret < 0) { |
| 292 | msg_perr("%s: MEMERASE ioctl call returned %d, error: %s\n", |
| 293 | __func__, ret, strerror(errno)); |
| 294 | return 1; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | return 0; |
| 299 | } |
| 300 | |
Nico Huber | 72c02ff | 2023-01-08 02:00:06 +0100 | [diff] [blame] | 301 | static int linux_mtd_shutdown(void *data); |
| 302 | |
Anastasia Klimchuk | 842b4ee | 2021-05-13 12:56:51 +1000 | [diff] [blame] | 303 | static const struct opaque_master linux_mtd_opaque_master = { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 304 | /* max_data_{read,write} don't have any effect for this programmer */ |
| 305 | .max_data_read = MAX_DATA_UNSPECIFIED, |
| 306 | .max_data_write = MAX_DATA_UNSPECIFIED, |
| 307 | .probe = linux_mtd_probe, |
| 308 | .read = linux_mtd_read, |
| 309 | .write = linux_mtd_write, |
| 310 | .erase = linux_mtd_erase, |
Nico Huber | 72c02ff | 2023-01-08 02:00:06 +0100 | [diff] [blame] | 311 | .shutdown = linux_mtd_shutdown, |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 312 | }; |
| 313 | |
| 314 | /* Returns 0 if setup is successful, non-zero to indicate error */ |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 315 | static int linux_mtd_setup(int dev_num, struct linux_mtd_data *data) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 316 | { |
| 317 | char sysfs_path[32]; |
| 318 | int ret = 1; |
| 319 | |
| 320 | /* Start by checking /sys/class/mtd/mtdN/type which should be "nor" for NOR flash */ |
| 321 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 322 | goto linux_mtd_setup_exit; |
| 323 | |
Angel Pons | 7e13456 | 2021-06-07 13:29:13 +0200 | [diff] [blame] | 324 | char buf[4] = { 0 }; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 325 | if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf))) |
| 326 | return 1; |
| 327 | |
| 328 | if (strcmp(buf, "nor")) { |
| 329 | msg_perr("MTD device %d type is not \"nor\"\n", dev_num); |
| 330 | goto linux_mtd_setup_exit; |
| 331 | } |
| 332 | |
| 333 | /* sysfs shows the correct device type, see if corresponding device node exists */ |
| 334 | char dev_path[32]; |
| 335 | struct stat s; |
| 336 | snprintf(dev_path, sizeof(dev_path), "%s/mtd%d", LINUX_DEV_ROOT, dev_num); |
| 337 | errno = 0; |
| 338 | if (stat(dev_path, &s) < 0) { |
| 339 | msg_pdbg("Cannot stat \"%s\": %s\n", dev_path, strerror(errno)); |
| 340 | goto linux_mtd_setup_exit; |
| 341 | } |
| 342 | |
| 343 | /* so far so good, get more info from other files in this dir */ |
| 344 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 345 | goto linux_mtd_setup_exit; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 346 | if (get_mtd_info(sysfs_path, data)) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 347 | goto linux_mtd_setup_exit; |
| 348 | |
| 349 | /* open file stream and go! */ |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 350 | if ((data->dev_fp = fopen(dev_path, "r+")) == NULL) { |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 351 | msg_perr("Cannot open file stream for %s\n", dev_path); |
| 352 | goto linux_mtd_setup_exit; |
| 353 | } |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 354 | ret = setvbuf(data->dev_fp, NULL, _IONBF, 0); |
Douglas Anderson | 595c5d0 | 2021-01-29 16:35:24 -0800 | [diff] [blame] | 355 | if (ret) |
| 356 | msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret); |
| 357 | |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 358 | msg_pinfo("Opened %s successfully\n", dev_path); |
| 359 | |
| 360 | ret = 0; |
| 361 | linux_mtd_setup_exit: |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | static int linux_mtd_shutdown(void *data) |
| 366 | { |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 367 | struct linux_mtd_data *mtd_data = data; |
| 368 | if (mtd_data->dev_fp != NULL) { |
| 369 | fclose(mtd_data->dev_fp); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 370 | } |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 371 | free(data); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 376 | static int linux_mtd_init(void) |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 377 | { |
| 378 | char *param; |
| 379 | int dev_num = 0; |
| 380 | int ret = 1; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 381 | struct linux_mtd_data *data = NULL; |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 382 | |
| 383 | param = extract_programmer_param("dev"); |
| 384 | if (param) { |
| 385 | char *endptr; |
| 386 | |
| 387 | dev_num = strtol(param, &endptr, 0); |
| 388 | if ((*endptr != '\0') || (dev_num < 0)) { |
| 389 | msg_perr("Invalid device number %s. Use flashrom -p " |
| 390 | "linux_mtd:dev=N where N is a valid MTD\n" |
| 391 | "device number.\n", param); |
| 392 | goto linux_mtd_init_exit; |
| 393 | } |
| 394 | } |
| 395 | |
David Hendricks | b0247b3 | 2018-05-23 21:50:18 -0700 | [diff] [blame] | 396 | /* |
| 397 | * If user specified the MTD device number then error out if it doesn't |
| 398 | * appear to exist. Otherwise assume the error is benign and print a |
| 399 | * debug message. Bail out in either case. |
| 400 | */ |
| 401 | char sysfs_path[32]; |
| 402 | if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0) |
| 403 | goto linux_mtd_init_exit; |
| 404 | |
| 405 | struct stat s; |
| 406 | if (stat(sysfs_path, &s) < 0) { |
| 407 | if (param) |
| 408 | msg_perr("%s does not exist\n", sysfs_path); |
| 409 | else |
| 410 | msg_pdbg("%s does not exist\n", sysfs_path); |
| 411 | goto linux_mtd_init_exit; |
| 412 | } |
Anastasia Klimchuk | fd3a225 | 2021-08-03 15:26:19 +1000 | [diff] [blame] | 413 | free(param); |
David Hendricks | b0247b3 | 2018-05-23 21:50:18 -0700 | [diff] [blame] | 414 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 415 | data = calloc(1, sizeof(*data)); |
| 416 | if (!data) { |
| 417 | msg_perr("Unable to allocate memory for linux_mtd_data\n"); |
Anastasia Klimchuk | fd3a225 | 2021-08-03 15:26:19 +1000 | [diff] [blame] | 418 | return 1; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 419 | } |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 420 | |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 421 | /* Get MTD info and store it in `data` */ |
| 422 | if (linux_mtd_setup(dev_num, data)) { |
| 423 | free(data); |
Anastasia Klimchuk | fd3a225 | 2021-08-03 15:26:19 +1000 | [diff] [blame] | 424 | return 1; |
Nikolai Artemiev | 3b32edb | 2021-05-08 19:00:06 +1000 | [diff] [blame] | 425 | } |
| 426 | |
Nico Huber | 72c02ff | 2023-01-08 02:00:06 +0100 | [diff] [blame] | 427 | return register_opaque_master(&linux_mtd_opaque_master, data); |
Anastasia Klimchuk | fd3a225 | 2021-08-03 15:26:19 +1000 | [diff] [blame] | 428 | |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 429 | linux_mtd_init_exit: |
Jacob Garber | ba71999 | 2019-08-12 12:07:03 -0600 | [diff] [blame] | 430 | free(param); |
David Hendricks | f9a3055 | 2015-05-23 20:30:30 -0700 | [diff] [blame] | 431 | return ret; |
| 432 | } |
Thomas Heijligen | cc853d8 | 2021-05-04 15:32:17 +0200 | [diff] [blame] | 433 | |
| 434 | const struct programmer_entry programmer_linux_mtd = { |
| 435 | .name = "linux_mtd", |
| 436 | .type = OTHER, |
| 437 | .devs.note = "Device files /dev/mtd*\n", |
| 438 | .init = linux_mtd_init, |
| 439 | .map_flash_region = fallback_map, |
| 440 | .unmap_flash_region = fallback_unmap, |
| 441 | .delay = internal_delay, |
| 442 | }; |