blob: f7cd82b0b5216d6cf8f097e88133f04dd32acce8 [file] [log] [blame]
David Hendricksf9a30552015-05-23 20:30:30 -07001/*
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>
Felix Singerbd82fa92022-08-19 02:40:39 +020020#include <stdbool.h>
David Hendricksf9a30552015-05-23 20:30:30 -070021#include <stdio.h>
22#include <stdlib.h>
23#include <mtd/mtd-user.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/stat.h>
27#include <unistd.h>
28
29#include "flash.h"
30#include "programmer.h"
31
32#define LINUX_DEV_ROOT "/dev"
33#define LINUX_MTD_SYSFS_ROOT "/sys/class/mtd"
34
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +100035struct linux_mtd_data {
36 FILE *dev_fp;
Felix Singerbd82fa92022-08-19 02:40:39 +020037 bool device_is_writeable;
38 bool no_erase;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +100039 /* Size info is presented in bytes in sysfs. */
Nikolai Artemiev6c331852021-05-09 11:37:38 +100040 unsigned long int total_size;
41 unsigned long int numeraseregions;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +100042 /* only valid if numeraseregions is 0 */
Nikolai Artemiev6c331852021-05-09 11:37:38 +100043 unsigned long int erasesize;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +100044};
David Hendricksf9a30552015-05-23 20:30:30 -070045
46/* read a string from a sysfs file and sanitize it */
47static int read_sysfs_string(const char *sysfs_path, const char *filename, char *buf, int len)
48{
49 int i;
50 size_t bytes_read;
51 FILE *fp;
52 char path[strlen(LINUX_MTD_SYSFS_ROOT) + 32];
53
54 snprintf(path, sizeof(path), "%s/%s", sysfs_path, filename);
55
56 if ((fp = fopen(path, "r")) == NULL) {
57 msg_perr("Cannot open %s\n", path);
58 return 1;
59 }
60
61 clearerr(fp);
62 bytes_read = fread(buf, 1, (size_t)len, fp);
63 if (!feof(fp) && ferror(fp)) {
64 msg_perr("Error occurred when reading %s\n", path);
65 fclose(fp);
66 return 1;
67 }
68
69 buf[bytes_read] = '\0';
70
71 /*
72 * Files from sysfs sometimes contain a newline or other garbage that
73 * can confuse functions like strtoul() and ruin formatting in print
74 * statements. Replace the first non-printable character (space is
75 * considered printable) with a proper string terminator.
76 */
77 for (i = 0; i < len; i++) {
78 if (!isprint(buf[i])) {
79 buf[i] = '\0';
80 break;
81 }
82 }
83
84 fclose(fp);
85 return 0;
86}
87
88static int read_sysfs_int(const char *sysfs_path, const char *filename, unsigned long int *val)
89{
90 char buf[32];
91 char *endptr;
92
93 if (read_sysfs_string(sysfs_path, filename, buf, sizeof(buf)))
94 return 1;
95
96 errno = 0;
97 *val = strtoul(buf, &endptr, 0);
98 if (*endptr != '\0') {
99 msg_perr("Error reading %s\n", filename);
100 return 1;
101 }
102
103 if (errno) {
104 msg_perr("Error reading %s: %s\n", filename, strerror(errno));
105 return 1;
106 }
107
108 return 0;
109}
110
111static int popcnt(unsigned int u)
112{
113 int count = 0;
114
115 while (u) {
116 u &= u - 1;
117 count++;
118 }
119
120 return count;
121}
122
123/* returns 0 to indicate success, non-zero to indicate error */
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000124static int get_mtd_info(const char *sysfs_path, struct linux_mtd_data *data)
David Hendricksf9a30552015-05-23 20:30:30 -0700125{
126 unsigned long int tmp;
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000127 char device_name[32];
David Hendricksf9a30552015-05-23 20:30:30 -0700128
129 /* Flags */
130 if (read_sysfs_int(sysfs_path, "flags", &tmp))
131 return 1;
132 if (tmp & MTD_WRITEABLE) {
133 /* cache for later use by write function */
Felix Singerbd82fa92022-08-19 02:40:39 +0200134 data->device_is_writeable = true;
David Hendricksf9a30552015-05-23 20:30:30 -0700135 }
136 if (tmp & MTD_NO_ERASE) {
Felix Singerbd82fa92022-08-19 02:40:39 +0200137 data->no_erase = true;
David Hendricksf9a30552015-05-23 20:30:30 -0700138 }
139
140 /* Device name */
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000141 if (read_sysfs_string(sysfs_path, "name", device_name, sizeof(device_name)))
David Hendricksf9a30552015-05-23 20:30:30 -0700142 return 1;
143
144 /* Total size */
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000145 if (read_sysfs_int(sysfs_path, "size", &data->total_size))
David Hendricksf9a30552015-05-23 20:30:30 -0700146 return 1;
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000147 if (popcnt(data->total_size) != 1) {
David Hendricksf9a30552015-05-23 20:30:30 -0700148 msg_perr("MTD size is not a power of 2\n");
149 return 1;
150 }
151
152 /* Erase size */
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000153 if (read_sysfs_int(sysfs_path, "erasesize", &data->erasesize))
David Hendricksf9a30552015-05-23 20:30:30 -0700154 return 1;
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000155 if (popcnt(data->erasesize) != 1) {
David Hendricksf9a30552015-05-23 20:30:30 -0700156 msg_perr("MTD erase size is not a power of 2\n");
157 return 1;
158 }
159
160 /* Erase regions */
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000161 if (read_sysfs_int(sysfs_path, "numeraseregions", &data->numeraseregions))
David Hendricksf9a30552015-05-23 20:30:30 -0700162 return 1;
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000163 if (data->numeraseregions != 0) {
David Hendricksf9a30552015-05-23 20:30:30 -0700164 msg_perr("Non-uniform eraseblock size is unsupported.\n");
165 return 1;
166 }
167
168 msg_pdbg("%s: device_name: \"%s\", is_writeable: %d, "
169 "numeraseregions: %lu, total_size: %lu, erasesize: %lu\n",
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000170 __func__, device_name, data->device_is_writeable,
171 data->numeraseregions, data->total_size, data->erasesize);
David Hendricksf9a30552015-05-23 20:30:30 -0700172
173 return 0;
174}
175
176static int linux_mtd_probe(struct flashctx *flash)
177{
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000178 struct linux_mtd_data *data = flash->mst->opaque.data;
179
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000180 if (data->no_erase)
David Hendricksf9a30552015-05-23 20:30:30 -0700181 flash->chip->feature_bits |= FEATURE_NO_ERASE;
182 flash->chip->tested = TEST_OK_PREW;
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000183 flash->chip->total_size = data->total_size / 1024; /* bytes -> kB */
184 flash->chip->block_erasers[0].eraseblocks[0].size = data->erasesize;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000185 flash->chip->block_erasers[0].eraseblocks[0].count =
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000186 data->total_size / data->erasesize;
David Hendricksf9a30552015-05-23 20:30:30 -0700187 return 1;
188}
189
190static int linux_mtd_read(struct flashctx *flash, uint8_t *buf,
191 unsigned int start, unsigned int len)
192{
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000193 struct linux_mtd_data *data = flash->mst->opaque.data;
David Hendricksf9a30552015-05-23 20:30:30 -0700194 unsigned int eb_size = flash->chip->block_erasers[0].eraseblocks[0].size;
195 unsigned int i;
196
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000197 if (fseek(data->dev_fp, start, SEEK_SET) != 0) {
David Hendricksf9a30552015-05-23 20:30:30 -0700198 msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno));
199 return 1;
200 }
201
202 for (i = 0; i < len; ) {
203 /*
204 * Try to align reads to eraseblock size.
205 * FIXME: Shouldn't actually be necessary, but not all MTD
206 * drivers handle arbitrary large reads well.
207 */
208 unsigned int step = eb_size - ((start + i) % eb_size);
209 step = min(step, len - i);
210
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000211 if (fread(buf + i, step, 1, data->dev_fp) != 1) {
David Hendricksf9a30552015-05-23 20:30:30 -0700212 msg_perr("Cannot read 0x%06x bytes at 0x%06x: %s\n",
213 step, start + i, strerror(errno));
214 return 1;
215 }
216
217 i += step;
218 }
219
220 return 0;
221}
222
223/* this version assumes we must divide the write request into chunks ourselves */
224static int linux_mtd_write(struct flashctx *flash, const uint8_t *buf,
225 unsigned int start, unsigned int len)
226{
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000227 struct linux_mtd_data *data = flash->mst->opaque.data;
David Hendricksf9a30552015-05-23 20:30:30 -0700228 unsigned int chunksize = flash->chip->block_erasers[0].eraseblocks[0].size;
229 unsigned int i;
230
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000231 if (!data->device_is_writeable)
David Hendricksf9a30552015-05-23 20:30:30 -0700232 return 1;
233
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000234 if (fseek(data->dev_fp, start, SEEK_SET) != 0) {
David Hendricksf9a30552015-05-23 20:30:30 -0700235 msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno));
236 return 1;
237 }
238
239 /*
240 * Try to align writes to eraseblock size. We want these large enough
241 * to give MTD room for optimizing performance.
242 * FIXME: Shouldn't need to divide this up at all, but not all MTD
243 * drivers handle arbitrary large writes well.
244 */
245 for (i = 0; i < len; ) {
246 unsigned int step = chunksize - ((start + i) % chunksize);
247 step = min(step, len - i);
248
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000249 if (fwrite(buf + i, step, 1, data->dev_fp) != 1) {
David Hendricksf9a30552015-05-23 20:30:30 -0700250 msg_perr("Cannot write 0x%06x bytes at 0x%06x\n", step, start + i);
251 return 1;
252 }
253
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000254 if (fflush(data->dev_fp) == EOF) {
David Hendricksf9a30552015-05-23 20:30:30 -0700255 msg_perr("Failed to flush buffer: %s\n", strerror(errno));
256 return 1;
257 }
258
259 i += step;
260 }
261
262 return 0;
263}
264
265static int linux_mtd_erase(struct flashctx *flash,
266 unsigned int start, unsigned int len)
267{
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000268 struct linux_mtd_data *data = flash->mst->opaque.data;
David Hendricksf9a30552015-05-23 20:30:30 -0700269 uint32_t u;
270
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000271 if (data->no_erase) {
Nico Huberac90af62022-12-18 00:22:47 +0000272 msg_perr("%s: device does not support erasing.\n"
273 "Please file a bug report at flashrom-stable@flashrom.org\n",
274 __func__);
David Hendricksf9a30552015-05-23 20:30:30 -0700275 return 1;
276 }
277
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000278 if (data->numeraseregions != 0) {
David Hendricksf9a30552015-05-23 20:30:30 -0700279 /* TODO: Support non-uniform eraseblock size using
280 use MEMGETREGIONCOUNT/MEMGETREGIONINFO ioctls */
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000281 msg_perr("%s: numeraseregions must be 0\n", __func__);
David Hendricksf9a30552015-05-23 20:30:30 -0700282 return 1;
283 }
284
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000285 for (u = 0; u < len; u += data->erasesize) {
David Hendricksf9a30552015-05-23 20:30:30 -0700286 struct erase_info_user erase_info = {
287 .start = start + u,
Nikolai Artemiev6c331852021-05-09 11:37:38 +1000288 .length = data->erasesize,
David Hendricksf9a30552015-05-23 20:30:30 -0700289 };
290
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000291 int ret = ioctl(fileno(data->dev_fp), MEMERASE, &erase_info);
Nikolai Artemiev04fce472022-01-11 18:26:48 +1100292 if (ret < 0) {
293 msg_perr("%s: MEMERASE ioctl call returned %d, error: %s\n",
294 __func__, ret, strerror(errno));
295 return 1;
David Hendricksf9a30552015-05-23 20:30:30 -0700296 }
297 }
298
299 return 0;
300}
301
Nico Huber72c02ff2023-01-08 02:00:06 +0100302static int linux_mtd_shutdown(void *data);
303
Anastasia Klimchuk842b4ee2021-05-13 12:56:51 +1000304static const struct opaque_master linux_mtd_opaque_master = {
David Hendricksf9a30552015-05-23 20:30:30 -0700305 /* max_data_{read,write} don't have any effect for this programmer */
306 .max_data_read = MAX_DATA_UNSPECIFIED,
307 .max_data_write = MAX_DATA_UNSPECIFIED,
308 .probe = linux_mtd_probe,
309 .read = linux_mtd_read,
310 .write = linux_mtd_write,
311 .erase = linux_mtd_erase,
Nico Huber72c02ff2023-01-08 02:00:06 +0100312 .shutdown = linux_mtd_shutdown,
David Hendricksf9a30552015-05-23 20:30:30 -0700313};
314
315/* Returns 0 if setup is successful, non-zero to indicate error */
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000316static int linux_mtd_setup(int dev_num, struct linux_mtd_data *data)
David Hendricksf9a30552015-05-23 20:30:30 -0700317{
318 char sysfs_path[32];
319 int ret = 1;
320
321 /* Start by checking /sys/class/mtd/mtdN/type which should be "nor" for NOR flash */
322 if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0)
323 goto linux_mtd_setup_exit;
324
Angel Pons7e134562021-06-07 13:29:13 +0200325 char buf[4] = { 0 };
David Hendricksf9a30552015-05-23 20:30:30 -0700326 if (read_sysfs_string(sysfs_path, "type", buf, sizeof(buf)))
327 return 1;
328
329 if (strcmp(buf, "nor")) {
330 msg_perr("MTD device %d type is not \"nor\"\n", dev_num);
331 goto linux_mtd_setup_exit;
332 }
333
334 /* sysfs shows the correct device type, see if corresponding device node exists */
335 char dev_path[32];
336 struct stat s;
337 snprintf(dev_path, sizeof(dev_path), "%s/mtd%d", LINUX_DEV_ROOT, dev_num);
338 errno = 0;
339 if (stat(dev_path, &s) < 0) {
340 msg_pdbg("Cannot stat \"%s\": %s\n", dev_path, strerror(errno));
341 goto linux_mtd_setup_exit;
342 }
343
344 /* so far so good, get more info from other files in this dir */
345 if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d/", LINUX_MTD_SYSFS_ROOT, dev_num) < 0)
346 goto linux_mtd_setup_exit;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000347 if (get_mtd_info(sysfs_path, data))
David Hendricksf9a30552015-05-23 20:30:30 -0700348 goto linux_mtd_setup_exit;
349
350 /* open file stream and go! */
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000351 if ((data->dev_fp = fopen(dev_path, "r+")) == NULL) {
David Hendricksf9a30552015-05-23 20:30:30 -0700352 msg_perr("Cannot open file stream for %s\n", dev_path);
353 goto linux_mtd_setup_exit;
354 }
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000355 ret = setvbuf(data->dev_fp, NULL, _IONBF, 0);
Douglas Anderson595c5d02021-01-29 16:35:24 -0800356 if (ret)
357 msg_pwarn("Failed to set MTD device to unbuffered: %d\n", ret);
358
David Hendricksf9a30552015-05-23 20:30:30 -0700359 msg_pinfo("Opened %s successfully\n", dev_path);
360
361 ret = 0;
362linux_mtd_setup_exit:
363 return ret;
364}
365
366static int linux_mtd_shutdown(void *data)
367{
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000368 struct linux_mtd_data *mtd_data = data;
369 if (mtd_data->dev_fp != NULL) {
370 fclose(mtd_data->dev_fp);
David Hendricksf9a30552015-05-23 20:30:30 -0700371 }
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000372 free(data);
David Hendricksf9a30552015-05-23 20:30:30 -0700373
374 return 0;
375}
376
Thomas Heijligencc853d82021-05-04 15:32:17 +0200377static int linux_mtd_init(void)
David Hendricksf9a30552015-05-23 20:30:30 -0700378{
379 char *param;
380 int dev_num = 0;
381 int ret = 1;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000382 struct linux_mtd_data *data = NULL;
David Hendricksf9a30552015-05-23 20:30:30 -0700383
384 param = extract_programmer_param("dev");
385 if (param) {
386 char *endptr;
387
388 dev_num = strtol(param, &endptr, 0);
389 if ((*endptr != '\0') || (dev_num < 0)) {
390 msg_perr("Invalid device number %s. Use flashrom -p "
391 "linux_mtd:dev=N where N is a valid MTD\n"
392 "device number.\n", param);
393 goto linux_mtd_init_exit;
394 }
395 }
396
David Hendricksb0247b32018-05-23 21:50:18 -0700397 /*
398 * If user specified the MTD device number then error out if it doesn't
399 * appear to exist. Otherwise assume the error is benign and print a
400 * debug message. Bail out in either case.
401 */
402 char sysfs_path[32];
403 if (snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d", LINUX_MTD_SYSFS_ROOT, dev_num) < 0)
404 goto linux_mtd_init_exit;
405
406 struct stat s;
407 if (stat(sysfs_path, &s) < 0) {
408 if (param)
409 msg_perr("%s does not exist\n", sysfs_path);
410 else
411 msg_pdbg("%s does not exist\n", sysfs_path);
412 goto linux_mtd_init_exit;
413 }
Anastasia Klimchukfd3a2252021-08-03 15:26:19 +1000414 free(param);
David Hendricksb0247b32018-05-23 21:50:18 -0700415
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000416 data = calloc(1, sizeof(*data));
417 if (!data) {
418 msg_perr("Unable to allocate memory for linux_mtd_data\n");
Anastasia Klimchukfd3a2252021-08-03 15:26:19 +1000419 return 1;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000420 }
David Hendricksf9a30552015-05-23 20:30:30 -0700421
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000422 /* Get MTD info and store it in `data` */
423 if (linux_mtd_setup(dev_num, data)) {
424 free(data);
Anastasia Klimchukfd3a2252021-08-03 15:26:19 +1000425 return 1;
Nikolai Artemiev3b32edb2021-05-08 19:00:06 +1000426 }
427
Nico Huber72c02ff2023-01-08 02:00:06 +0100428 return register_opaque_master(&linux_mtd_opaque_master, data);
Anastasia Klimchukfd3a2252021-08-03 15:26:19 +1000429
David Hendricksf9a30552015-05-23 20:30:30 -0700430linux_mtd_init_exit:
Jacob Garberba719992019-08-12 12:07:03 -0600431 free(param);
David Hendricksf9a30552015-05-23 20:30:30 -0700432 return ret;
433}
Thomas Heijligencc853d82021-05-04 15:32:17 +0200434
435const struct programmer_entry programmer_linux_mtd = {
436 .name = "linux_mtd",
437 .type = OTHER,
438 .devs.note = "Device files /dev/mtd*\n",
439 .init = linux_mtd_init,
Thomas Heijligencc853d82021-05-04 15:32:17 +0200440};