blob: 42e89b4e102ef2b928205b91cb9019f429ec9c22 [file] [log] [blame]
David Hendrickscebee892015-05-23 20:30:30 -07001/*
2 * This file is part of the flashrom project.
3 *
4 * Copyright 2015 Google Inc.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#include <ctype.h>
21#include <errno.h>
22#include <fcntl.h>
23#include <inttypes.h>
24#include <libgen.h>
25#include <stdio.h>
26#include <stdlib.h>
27#include <mtd/mtd-user.h>
28#include <string.h>
29#include <sys/ioctl.h>
30#include <sys/stat.h>
31#include <unistd.h>
32
33#include "file.h"
34#include "flash.h"
35#include "programmer.h"
David Hendricks85f61c52015-06-04 19:52:59 -070036#include "writeprotect.h"
David Hendrickscebee892015-05-23 20:30:30 -070037
38#define LINUX_DEV_ROOT "/dev"
39#define LINUX_MTD_SYSFS_ROOT "/sys/class/mtd"
40
41/* enough space for LINUX_MTD_SYSFS_ROOT + directory name + filename */
42static char sysfs_path[PATH_MAX];
43
44static int dev_fd = -1;
45
David Hendrickscebee892015-05-23 20:30:30 -070046static int mtd_device_is_writeable;
47
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -070048static int mtd_no_erase;
49
David Hendrickscebee892015-05-23 20:30:30 -070050/* Size info is presented in bytes in sysfs. */
51static unsigned long int mtd_total_size;
52static unsigned long int mtd_numeraseregions;
53static unsigned long int mtd_erasesize; /* only valid if numeraseregions is 0 */
54
David Hendricks85f61c52015-06-04 19:52:59 -070055static struct wp wp_mtd; /* forward declaration */
56
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -070057static int stat_mtd_files(char *dev_path)
David Hendrickscebee892015-05-23 20:30:30 -070058{
59 struct stat s;
60
61 errno = 0;
62 if (stat(dev_path, &s) < 0) {
63 msg_pdbg("Cannot stat \"%s\": %s\n", dev_path, strerror(errno));
64 return 1;
65 }
66
67 if (lstat(sysfs_path, &s) < 0) {
68 msg_pdbg("Cannot stat \"%s\" : %s\n",
69 sysfs_path, strerror(errno));
70 return 1;
71 }
72
73 return 0;
74}
75
76/* read a string from a sysfs file and sanitize it */
77static int read_sysfs_string(const char *filename, char *buf, int len)
78{
79 int fd, bytes_read, i;
80 char path[strlen(LINUX_MTD_SYSFS_ROOT) + 32];
81
82 snprintf(path, sizeof(path), "%s/%s", sysfs_path, filename);
83
84 if ((fd = open(path, O_RDONLY)) < 0) {
85 msg_perr("Cannot open %s\n", path);
86 return 1;
87 }
88
89 if ((bytes_read = read(fd, buf, len - 1)) < 0) {
90 msg_perr("Cannot read %s\n", path);
91 close(fd);
92 return 1;
93 }
94
95 buf[bytes_read] = '\0';
96
97 /*
98 * Files from sysfs sometimes contain a newline or other garbage that
99 * can confuse functions like strtoul() and ruin formatting in print
100 * statements. Replace the first non-printable character (space is
101 * considered printable) with a proper string terminator.
102 */
103 for (i = 0; i < len; i++) {
104 if (!isprint(buf[i])) {
105 buf[i] = '\0';
106 break;
107 }
108 }
109
110 close(fd);
111 return 0;
112}
113
114static int read_sysfs_int(const char *filename, unsigned long int *val)
115{
David Hendrickscebee892015-05-23 20:30:30 -0700116 char buf[32];
117 char *endptr;
118
119 if (read_sysfs_string(filename, buf, sizeof(buf)))
120 return 1;
121
122 errno = 0;
123 *val = strtoul(buf, &endptr, 0);
124 if (endptr != &buf[strlen(buf)]) {
125 msg_perr("Error reading %s\n", filename);
126 return 1;
127 }
128
129 if (errno) {
130 msg_perr("Error reading %s: %s\n", filename, strerror(errno));
131 return 1;
132 }
133
134 return 0;
135}
136
137/* returns 0 to indicate success, non-zero to indicate error */
138static int get_mtd_info(void)
139{
140 unsigned long int tmp;
141 char mtd_device_name[32];
142
143 /* Flags */
144 if (read_sysfs_int("flags", &tmp))
145 return 1;
146 if (tmp & MTD_WRITEABLE) {
147 /* cache for later use by write function */
148 mtd_device_is_writeable = 1;
149 }
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700150 if (tmp & MTD_NO_ERASE) {
151 mtd_no_erase = 1;
152 }
David Hendrickscebee892015-05-23 20:30:30 -0700153
154 /* Device name */
155 if (read_sysfs_string("name", mtd_device_name, sizeof(mtd_device_name)))
156 return 1;
157
158 /* Total size */
159 if (read_sysfs_int("size", &mtd_total_size))
160 return 1;
161 if (__builtin_popcount(mtd_total_size) != 1) {
162 msg_perr("MTD size is not a power of 2\n");
163 return 1;
164 }
165
166 /* Erase size */
167 if (read_sysfs_int("erasesize", &mtd_erasesize))
168 return 1;
169 if (__builtin_popcount(mtd_erasesize) != 1) {
170 msg_perr("MTD erase size is not a power of 2\n");
171 return 1;
172 }
173
174 /* Erase regions */
175 if (read_sysfs_int("numeraseregions", &mtd_numeraseregions))
176 return 1;
177 if (mtd_numeraseregions != 0) {
178 msg_perr("Non-uniform eraseblock size is unsupported.\n");
179 return 1;
180 }
181
182 msg_pspew("%s: device_name: \"%s\", is_writeable: %d, "
183 "numeraseregions: %lu, total_size: %lu, erasesize: %lu\n",
184 __func__, mtd_device_name, mtd_device_is_writeable,
185 mtd_numeraseregions, mtd_total_size, mtd_erasesize);
186
187 return 0;
188}
189
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700190static int linux_mtd_probe(struct flashctx *flash)
David Hendrickscebee892015-05-23 20:30:30 -0700191{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100192 flash->chip->wp = &wp_mtd;
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700193 if (mtd_no_erase)
194 flash->chip->feature_bits |= FEATURE_NO_ERASE;
Patrick Georgif3fa2992017-02-02 16:24:44 +0100195 flash->chip->tested = TEST_OK_PREW;
196 flash->chip->total_size = mtd_total_size / 1024; /* bytes -> kB */
197 flash->chip->block_erasers[0].eraseblocks[0].size = mtd_erasesize;
198 flash->chip->block_erasers[0].eraseblocks[0].count =
David Hendrickscebee892015-05-23 20:30:30 -0700199 mtd_total_size / mtd_erasesize;
200 return 1;
201}
202
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700203static int linux_mtd_read(struct flashctx *flash, uint8_t *buf,
David Hendrickscebee892015-05-23 20:30:30 -0700204 unsigned int start, unsigned int len)
205{
Patrick Georgif3fa2992017-02-02 16:24:44 +0100206 unsigned int eb_size = flash->chip->block_erasers[0].eraseblocks[0].size;
Brian Norris99c8ad22016-05-10 15:36:02 -0700207 unsigned int i;
208
David Hendrickscebee892015-05-23 20:30:30 -0700209 if (lseek(dev_fd, start, SEEK_SET) != start) {
210 msg_perr("Cannot seek to 0x%06x: %s\n", start, strerror(errno));
211 return 1;
212 }
213
Brian Norris99c8ad22016-05-10 15:36:02 -0700214 for (i = 0; i < len; ) {
215 /* Try to align reads to eraseblock size */
216 unsigned int step = eb_size - ((start + i) % eb_size);
217 step = min(step, len - i);
218
219 if (read(dev_fd, buf + i, step) != step) {
220 msg_perr("Cannot read 0x%06x bytes at 0x%06x: %s\n",
221 step, start + i, strerror(errno));
222 return 1;
223 }
224
225 i += step;
David Hendrickscebee892015-05-23 20:30:30 -0700226 }
227
228 return 0;
229}
230
231/* this version assumes we must divide the write request into pages ourselves */
Patrick Georgiab8353e2017-02-03 18:32:01 +0100232static int linux_mtd_write(struct flashctx *flash, const uint8_t *buf,
David Hendrickscebee892015-05-23 20:30:30 -0700233 unsigned int start, unsigned int len)
234{
235 unsigned int page;
236 unsigned int chunksize, page_size;
237
Patrick Georgif3fa2992017-02-02 16:24:44 +0100238 chunksize = page_size = flash->chip->page_size;
David Hendrickscebee892015-05-23 20:30:30 -0700239
240 if (!mtd_device_is_writeable)
241 return 1;
242
243 for (page = start / page_size;
244 page <= (start + len - 1) / page_size; page++) {
245 unsigned int i, starthere, lenhere;
246
247 starthere = max(start, page * page_size);
248 lenhere = min(start + len, (page + 1) * page_size) - starthere;
249 for (i = 0; i < lenhere; i += chunksize) {
250 unsigned int towrite = min(chunksize, lenhere - i);
251
252 if (lseek(dev_fd, starthere, SEEK_SET) != starthere) {
253 msg_perr("Cannot seek to 0x%06x: %s\n",
254 start, strerror(errno));
255 return 1;
256 }
257
258 if (write(dev_fd, &buf[starthere - start], towrite) != towrite) {
259 msg_perr("Cannot read 0x%06x bytes at 0x%06x: "
260 "%s\n", start, len, strerror(errno));
261 return 1;
262 }
263 }
264 }
265
266 return 0;
267}
268
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700269static int linux_mtd_erase(struct flashctx *flash,
David Hendrickscebee892015-05-23 20:30:30 -0700270 unsigned int start, unsigned int len)
271{
David Hendrickscebee892015-05-23 20:30:30 -0700272 uint32_t u;
273
William A. Kennington IIIf15c2fa2017-04-07 17:38:42 -0700274 if (mtd_no_erase) {
275 msg_perr("%s: device does not support erasing. Please file a "
276 "bug report at flashrom@flashrom.org\n", __func__);
277 return 1;
278 }
279
David Hendrickscebee892015-05-23 20:30:30 -0700280 if (mtd_numeraseregions != 0) {
281 /* TODO: Support non-uniform eraseblock size using
282 use MEMGETREGIONCOUNT/MEMGETREGIONINFO ioctls */
283 }
284
285 for (u = 0; u < len; u += mtd_erasesize) {
286 struct erase_info_user erase_info = {
287 .start = start + u,
288 .length = mtd_erasesize,
289 };
290
291 if (ioctl(dev_fd, MEMERASE, &erase_info) == -1) {
292 msg_perr("%s: ioctl: %s\n", __func__, strerror(errno));
293 return 1;
294 }
295 }
296
297 return 0;
298}
299
300static struct opaque_programmer programmer_linux_mtd = {
Brian Norris99c8ad22016-05-10 15:36:02 -0700301 /* max_data_{read,write} don't have any effect for this programmer */
David Hendrickscebee892015-05-23 20:30:30 -0700302 .max_data_read = MAX_DATA_UNSPECIFIED,
303 .max_data_write = MAX_DATA_UNSPECIFIED,
304 .probe = linux_mtd_probe,
305 .read = linux_mtd_read,
306 .write = linux_mtd_write,
307 .erase = linux_mtd_erase,
308};
309
310/* Returns 0 if setup is successful, non-zero to indicate error */
311static int linux_mtd_setup(int dev_num)
312{
313 char dev_path[16]; /* "/dev/mtdN" */
314 int ret = 1;
315
316 if (dev_num < 0) {
317 char *tmp, *p;
318
319 tmp = (char *)scanft(LINUX_MTD_SYSFS_ROOT, "type", "nor", 1);
320 if (!tmp) {
David Hendrickscf6fbe62015-09-24 14:22:39 -0700321 msg_pdbg("%s: NOR type device not found.\n", __func__);
David Hendrickscebee892015-05-23 20:30:30 -0700322 goto linux_mtd_setup_exit;
323 }
324
325 /* "tmp" should be something like "/sys/blah/mtdN/type" */
326 p = tmp + strlen(LINUX_MTD_SYSFS_ROOT);
327 while (p[0] == '/')
328 p++;
329
330 if (sscanf(p, "mtd%d", &dev_num) != 1) {
331 msg_perr("Can't obtain device number from \"%s\"\n", p);
332 free(tmp);
333 goto linux_mtd_setup_exit;
334 }
335 free(tmp);
336 }
337
338 snprintf(sysfs_path, sizeof(sysfs_path), "%s/mtd%d",
339 LINUX_MTD_SYSFS_ROOT, dev_num);
340 snprintf(dev_path, sizeof(dev_path), "%s/mtd%d",
341 LINUX_DEV_ROOT, dev_num);
342 msg_pdbg("%s: sysfs_path: \"%s\", dev_path: \"%s\"\n",
343 __func__, sysfs_path, dev_path);
344
Souvik Ghosh5cdb7e52016-06-23 12:57:38 -0700345 if (stat_mtd_files(dev_path))
David Hendrickscebee892015-05-23 20:30:30 -0700346 goto linux_mtd_setup_exit;
347
348 if (get_mtd_info())
349 goto linux_mtd_setup_exit;
350
351 if ((dev_fd = open(dev_path, O_RDWR)) == -1) {
352 msg_pdbg("%s: failed to open %s: %s\n", __func__,
353 dev_path, strerror(errno));
354 goto linux_mtd_setup_exit;
355 }
356
357 ret = 0;
358linux_mtd_setup_exit:
359 return ret;
360}
361
David Hendricks93784b42016-08-09 17:00:38 -0700362static int linux_mtd_shutdown(void *data)
David Hendrickscebee892015-05-23 20:30:30 -0700363{
364 if (dev_fd != -1) {
365 close(dev_fd);
366 dev_fd = -1;
367 }
368
369 return 0;
370}
371
David Hendricksac1d25c2016-08-09 17:00:58 -0700372int linux_mtd_init(void)
David Hendrickscebee892015-05-23 20:30:30 -0700373{
374 char *param;
375 int dev_num = -1; /* linux_mtd_setup will search if dev_num < 0 */
376 int ret = 1;
377
378 if (alias && alias->type != ALIAS_HOST)
379 return 1;
380
381 param = extract_programmer_param("dev");
382 if (param) {
383 char *endptr;
384
385 dev_num = strtol(param, &endptr, 0);
386 if ((param == endptr) || (dev_num < 0)) {
387 msg_perr("Invalid device number %s. Use flashrom -p "
388 "linux_mtd:dev=N where N is a valid MTD "
389 "device number\n", param);
390 goto linux_mtd_init_exit;
391 }
392 }
393
394 if (linux_mtd_setup(dev_num))
395 goto linux_mtd_init_exit;
396
397 if (register_shutdown(linux_mtd_shutdown, NULL))
398 goto linux_mtd_init_exit;
399
400 register_opaque_programmer(&programmer_linux_mtd);
401
402 ret = 0;
403linux_mtd_init_exit:
404 msg_pdbg("%s: %s\n", __func__, ret == 0 ? "success." : "failed.");
405 return ret;
406}
David Hendricks85f61c52015-06-04 19:52:59 -0700407
408/*
409 * Write-protect functions.
410 */
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700411static int mtd_wp_list_ranges(const struct flashctx *flash)
David Hendricks85f61c52015-06-04 19:52:59 -0700412{
413 /* TODO: implement this */
414 msg_perr("--wp-list is not currently implemented for MTD.\n");
415 return 1;
416}
417
418/*
419 * We only have MEMLOCK to enable write-protection for a particular block,
420 * so we need to do force the user to use --wp-range and --wp-enable
421 * command-line arguments simultaneously. (Fortunately, CrOS factory
422 * installer does this already).
423 *
424 * The --wp-range argument is processed first and will set these variables
425 * which --wp-enable will use afterward.
426 */
427static unsigned int wp_range_start;
428static unsigned int wp_range_len;
429static int wp_set_range_called = 0;
430
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700431static int mtd_wp_set_range(const struct flashctx *flash,
David Hendricks85f61c52015-06-04 19:52:59 -0700432 unsigned int start, unsigned int len)
433{
434 wp_range_start = start;
435 wp_range_len = len;
436
437 wp_set_range_called = 1;
438 return 0;
439}
440
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700441static int mtd_wp_enable_writeprotect(const struct flashctx *flash, enum wp_mode mode)
David Hendricks85f61c52015-06-04 19:52:59 -0700442{
443 struct erase_info_user entire_chip = {
444 .start = 0,
445 .length = mtd_total_size,
446 };
447 struct erase_info_user desired_range = {
448 .start = wp_range_start,
449 .length = wp_range_len,
450 };
451
452 if (!wp_set_range_called) {
453 msg_perr("For MTD, --wp-range and --wp-enable must be "
454 "used simultaneously.\n");
455 return 1;
456 }
457
458 /*
459 * MTD handles write-protection additively, so whatever new range is
460 * specified is added to the range which is currently protected. To be
461 * consistent with flashrom behavior with other programmer interfaces,
462 * we need to disable the current write protection and then enable
463 * it for the desired range.
464 */
465 if (ioctl(dev_fd, MEMUNLOCK, &entire_chip) == -1) {
466 msg_perr("%s: Failed to disable write-protection, ioctl: %s\n",
467 __func__, strerror(errno));
Brian Norris4df096b2016-07-27 18:36:38 -0700468 msg_perr("Did you disable WP#?\n");
David Hendricks85f61c52015-06-04 19:52:59 -0700469 return 1;
470 }
471
472 if (ioctl(dev_fd, MEMLOCK, &desired_range) == -1) {
473 msg_perr("%s: Failed to enable write-protection, ioctl: %s\n",
474 __func__, strerror(errno));
475 return 1;
476 }
477
478 return 0;
479}
480
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700481static int mtd_wp_disable_writeprotect(const struct flashctx *flash)
David Hendricks85f61c52015-06-04 19:52:59 -0700482{
483 struct erase_info_user erase_info;
484
485 if (wp_set_range_called) {
486 erase_info.start = wp_range_start;
487 erase_info.length = wp_range_len;
488 } else {
489 erase_info.start = 0;
490 erase_info.length = mtd_total_size;
491 }
492
493 if (ioctl(dev_fd, MEMUNLOCK, &erase_info) == -1) {
494 msg_perr("%s: ioctl: %s\n", __func__, strerror(errno));
Brian Norris4df096b2016-07-27 18:36:38 -0700495 msg_perr("Did you disable WP#?\n");
David Hendricks85f61c52015-06-04 19:52:59 -0700496 return 1;
497 }
498
499 return 0;
500}
501
Souvik Ghoshd75cd672016-06-17 14:21:39 -0700502static int mtd_wp_status(const struct flashctx *flash)
David Hendricks85f61c52015-06-04 19:52:59 -0700503{
Brian Norris3ce4c472016-06-02 16:40:14 -0700504 uint32_t start = 0, len = 0;
505 int start_found = 0;
David Hendricks85f61c52015-06-04 19:52:59 -0700506 unsigned int u;
507
508 /* For now, assume only one contiguous region can be locked (NOR) */
509 /* FIXME: use flash struct members instead of raw MTD values here */
Wei-Ning Huang699269b2016-03-02 22:08:05 +0800510 for (u = 0; u < mtd_total_size; u += mtd_erasesize) {
David Hendricks85f61c52015-06-04 19:52:59 -0700511 int rc;
512 struct erase_info_user erase_info = {
513 .start = u,
514 .length = mtd_erasesize,
515 };
516
517 rc = ioctl(dev_fd, MEMISLOCKED, &erase_info);
518 if (rc < 0) {
519 msg_perr("%s: ioctl: %s\n", __func__, strerror(errno));
520 return 1;
521 } else if (rc == 1) {
Wei-Ning Huangbf285ee2016-03-04 11:36:14 +0800522 if (!start_found) {
David Hendricks85f61c52015-06-04 19:52:59 -0700523 start = erase_info.start;
Wei-Ning Huangbf285ee2016-03-04 11:36:14 +0800524 start_found = 1;
525 }
Brian Norris3ce4c472016-06-02 16:40:14 -0700526 len += mtd_erasesize;
David Hendricks85f61c52015-06-04 19:52:59 -0700527 } else if (rc == 0) {
Wei-Ning Huangbf285ee2016-03-04 11:36:14 +0800528 if (start_found) {
Brian Norris3ce4c472016-06-02 16:40:14 -0700529 /* TODO: changes required for supporting non-contiguous locked regions */
530 break;
Wei-Ning Huangbf285ee2016-03-04 11:36:14 +0800531 }
David Hendricks85f61c52015-06-04 19:52:59 -0700532 }
533
David Hendricks85f61c52015-06-04 19:52:59 -0700534 }
535
Wei-Ning Huangca907072016-04-27 11:30:17 +0800536 msg_cinfo("WP: write protect is %s.\n",
Brian Norris3ce4c472016-06-02 16:40:14 -0700537 start_found ? "enabled": "disabled");
Wei-Ning Huangca907072016-04-27 11:30:17 +0800538 msg_pinfo("WP: write protect range: start=0x%08x, "
Brian Norris3ce4c472016-06-02 16:40:14 -0700539 "len=0x%08x\n", start, len);
Wei-Ning Huangca907072016-04-27 11:30:17 +0800540
David Hendricks85f61c52015-06-04 19:52:59 -0700541 return 0;
542}
543
544static struct wp wp_mtd = {
545 .list_ranges = mtd_wp_list_ranges,
546 .set_range = mtd_wp_set_range,
547 .enable = mtd_wp_enable_writeprotect,
548 .disable = mtd_wp_disable_writeprotect,
549 .wp_status = mtd_wp_status,
550};