blob: c5935b2f9cd1579733c459ed9cb7cfd4fc574ea8 [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
David Woodhousea1452a32010-08-08 20:58:20 +01003 * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
Thomas Gleixner15fdc522005-11-07 00:14:42 +01006#include <linux/device.h>
7#include <linux/fs.h>
Andrew Morton0c1eafd2007-08-10 13:01:06 -07008#include <linux/mm.h>
Artem Bityutskiy9c740342006-10-11 14:52:47 +03009#include <linux/err.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010010#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/kernel.h>
12#include <linux/module.h>
Thomas Gleixner15fdc522005-11-07 00:14:42 +010013#include <linux/slab.h>
14#include <linux/sched.h>
Arnd Bergmann5aa82942010-06-02 14:28:52 +020015#include <linux/mutex.h>
David Howells402d3262009-02-12 10:40:00 +000016#include <linux/backing-dev.h>
Kevin Cernekee97718542009-04-08 22:53:13 -070017#include <linux/compat.h>
Kirill A. Shutemovcd874232010-05-17 16:55:47 +030018#include <linux/mount.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030019#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070020#include <linux/magic.h>
Ezequiel Garciaf83c3832013-10-13 18:05:23 -030021#include <linux/major.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mtd/mtd.h>
Roman Tereshonkovd0f79592010-09-17 13:31:42 +030023#include <linux/mtd/partitions.h>
Anatolij Gustschindd02b672010-06-15 09:30:15 +020024#include <linux/mtd/map.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080026#include <linux/uaccess.h>
Todd Poynor9bc7b382005-06-30 01:23:27 +010027
Artem Bityutskiy660685d2013-03-14 13:27:40 +020028#include "mtdcore.h"
29
Arnd Bergmann5aa82942010-06-02 14:28:52 +020030static DEFINE_MUTEX(mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Nicolas Pitre045e9a52005-02-08 19:12:53 +000032/*
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020033 * Data structure to hold the pointer to the mtd device as well
Brian Norris92394b5c2011-07-20 09:53:42 -070034 * as mode information of various use cases.
Nicolas Pitre045e9a52005-02-08 19:12:53 +000035 */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020036struct mtd_file_info {
37 struct mtd_info *mtd;
38 enum mtd_file_modes mode;
39};
Nicolas Pitre31f42332005-02-08 17:45:55 +000040
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020041static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020043 struct mtd_file_info *mfi = file->private_data;
Al Virob9599572013-06-16 20:27:42 +040044 return fixed_size_llseek(file, offset, orig, mfi->mtd->size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045}
46
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020047static int mtdchar_open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
49 int minor = iminor(inode);
50 int devnum = minor >> 1;
Jonathan Corbet60712392008-05-15 10:10:37 -060051 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct mtd_info *mtd;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020053 struct mtd_file_info *mfi;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Brian Norris289c0522011-07-19 10:06:09 -070055 pr_debug("MTD_open\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 /* You can't open the RO devices RW */
Al Viroaeb5d722008-09-02 15:28:45 -040058 if ((file->f_mode & FMODE_WRITE) && (minor & 1))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return -EACCES;
60
Arnd Bergmann5aa82942010-06-02 14:28:52 +020061 mutex_lock(&mtd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 mtd = get_mtd_device(NULL, devnum);
Thomas Gleixner97894cd2005-11-07 11:15:26 +000063
Jonathan Corbet60712392008-05-15 10:10:37 -060064 if (IS_ERR(mtd)) {
65 ret = PTR_ERR(mtd);
66 goto out;
67 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000068
David Howells402d3262009-02-12 10:40:00 +000069 if (mtd->type == MTD_ABSENT) {
Jonathan Corbet60712392008-05-15 10:10:37 -060070 ret = -ENODEV;
Al Viroc65390f2012-04-09 01:36:28 -040071 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 }
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 /* You can't open it RW if it's not a writeable device */
Al Viroaeb5d722008-09-02 15:28:45 -040075 if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
Jonathan Corbet60712392008-05-15 10:10:37 -060076 ret = -EACCES;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010077 goto out1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +000079
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020080 mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
81 if (!mfi) {
Jonathan Corbet60712392008-05-15 10:10:37 -060082 ret = -ENOMEM;
Christoph Hellwigb4caecd2015-01-14 10:42:32 +010083 goto out1;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020084 }
85 mfi->mtd = mtd;
86 file->private_data = mfi;
Al Viroc65390f2012-04-09 01:36:28 -040087 mutex_unlock(&mtd_mutex);
88 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020089
Al Viroc65390f2012-04-09 01:36:28 -040090out1:
91 put_mtd_device(mtd);
Jonathan Corbet60712392008-05-15 10:10:37 -060092out:
Arnd Bergmann5aa82942010-06-02 14:28:52 +020093 mutex_unlock(&mtd_mutex);
Jonathan Corbet60712392008-05-15 10:10:37 -060094 return ret;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020095} /* mtdchar_open */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*====================================================================*/
98
Artem Bityutskiy969e57a2011-12-23 17:27:46 +020099static int mtdchar_close(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200101 struct mtd_file_info *mfi = file->private_data;
102 struct mtd_info *mtd = mfi->mtd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Brian Norris289c0522011-07-19 10:06:09 -0700104 pr_debug("MTD_close\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Joakim Tjernlund7eafaed2007-06-27 00:56:40 +0200106 /* Only sync if opened RW */
Artem Bityutskiy327cf292011-12-30 16:35:35 +0200107 if ((file->f_mode & FMODE_WRITE))
Artem Bityutskiy85f2f2a2011-12-23 19:03:12 +0200108 mtd_sync(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 put_mtd_device(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200111 file->private_data = NULL;
112 kfree(mfi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 return 0;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200115} /* mtdchar_close */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Grant Erickson3e45cf52011-04-08 08:51:33 -0700117/* Back in June 2001, dwmw2 wrote:
118 *
119 * FIXME: This _really_ needs to die. In 2.5, we should lock the
120 * userspace buffer down and use it directly with readv/writev.
121 *
122 * The implementation below, using mtd_kmalloc_up_to, mitigates
123 * allocation failures when the system is under low-memory situations
124 * or if memory is highly fragmented at the cost of reducing the
125 * performance of the requested transfer due to a smaller buffer size.
126 *
127 * A more complex but more memory-efficient implementation based on
128 * get_user_pages and iovecs to cover extents of those pages is a
129 * longer-term goal, as intimated by dwmw2 above. However, for the
130 * write case, this requires yet more complex head and tail transfer
131 * handling when those head and tail offsets and sizes are such that
132 * alignment requirements are not met in the NAND subdriver.
133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200135static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
136 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200138 struct mtd_file_info *mfi = file->private_data;
139 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiy30fa9842011-12-29 15:16:28 +0200140 size_t retlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 size_t total_retlen=0;
142 int ret=0;
143 int len;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700144 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 char *kbuf;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000146
Brian Norris289c0522011-07-19 10:06:09 -0700147 pr_debug("MTD_read\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Jann Horn6c6bc9e2018-07-07 05:37:22 +0200149 if (*ppos + count > mtd->size) {
150 if (*ppos < mtd->size)
151 count = mtd->size - *ppos;
152 else
153 count = 0;
154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!count)
157 return 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000158
Grant Erickson3e45cf52011-04-08 08:51:33 -0700159 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100160 if (!kbuf)
161 return -ENOMEM;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700164 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200166 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700167 case MTD_FILE_MODE_OTP_FACTORY:
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200168 ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
169 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000170 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700171 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy4ea1cab2011-12-23 18:47:59 +0200172 ret = mtd_read_user_prot_reg(mtd, *ppos, len,
173 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000174 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700175 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200176 {
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200177 struct mtd_oob_ops ops = {};
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200178
Brian Norris0612b9d2011-08-30 18:45:40 -0700179 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200180 ops.datbuf = kbuf;
181 ops.oobbuf = NULL;
182 ops.len = len;
183
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200184 ret = mtd_read_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200185 retlen = ops.retlen;
186 break;
187 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000188 default:
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200189 ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000190 }
Brian Norris7854d3f2011-06-23 14:12:08 -0700191 /* Nand returns -EBADMSG on ECC errors, but it returns
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 * the data. For our userspace tools it is important
Brian Norris7854d3f2011-06-23 14:12:08 -0700193 * to dump areas with ECC errors!
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200194 * For kernel internal usage it also might return -EUCLEAN
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300195 * to signal the caller that a bitflip has occurred and has
Thomas Gleixner9a1fcdf2006-05-29 14:56:39 +0200196 * been corrected by the ECC algorithm.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 * Userspace software which accesses NAND this way
198 * must be aware of the fact that it deals with NAND
199 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700200 if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 *ppos += retlen;
202 if (copy_to_user(buf, kbuf, retlen)) {
Thomas Gleixnerf4a43cf2006-05-28 11:01:53 +0200203 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EFAULT;
205 }
206 else
207 total_retlen += retlen;
208
209 count -= retlen;
210 buf += retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000211 if (retlen == 0)
212 count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
214 else {
215 kfree(kbuf);
216 return ret;
217 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
220
Thago Galesib802c072006-04-17 17:38:15 +0100221 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200223} /* mtdchar_read */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200225static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
226 loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200228 struct mtd_file_info *mfi = file->private_data;
229 struct mtd_info *mtd = mfi->mtd;
Grant Erickson3e45cf52011-04-08 08:51:33 -0700230 size_t size = count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 char *kbuf;
232 size_t retlen;
233 size_t total_retlen=0;
234 int ret=0;
235 int len;
236
Brian Norris289c0522011-07-19 10:06:09 -0700237 pr_debug("MTD_write\n");
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000238
Jann Horn6c6bc9e2018-07-07 05:37:22 +0200239 if (*ppos >= mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return -ENOSPC;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 if (*ppos + count > mtd->size)
243 count = mtd->size - *ppos;
244
245 if (!count)
246 return 0;
247
Grant Erickson3e45cf52011-04-08 08:51:33 -0700248 kbuf = mtd_kmalloc_up_to(mtd, &size);
Thago Galesib802c072006-04-17 17:38:15 +0100249 if (!kbuf)
250 return -ENOMEM;
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 while (count) {
Grant Erickson3e45cf52011-04-08 08:51:33 -0700253 len = min_t(size_t, count, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 if (copy_from_user(kbuf, buf, len)) {
256 kfree(kbuf);
257 return -EFAULT;
258 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000259
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200260 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700261 case MTD_FILE_MODE_OTP_FACTORY:
Nicolas Pitre31f42332005-02-08 17:45:55 +0000262 ret = -EROFS;
263 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700264 case MTD_FILE_MODE_OTP_USER:
Artem Bityutskiy482b43a2011-12-23 18:50:04 +0200265 ret = mtd_write_user_prot_reg(mtd, *ppos, len,
266 &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000267 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200268
Brian Norrisbeb133f2011-08-30 18:45:41 -0700269 case MTD_FILE_MODE_RAW:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200270 {
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200271 struct mtd_oob_ops ops = {};
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200272
Brian Norris0612b9d2011-08-30 18:45:40 -0700273 ops.mode = MTD_OPS_RAW;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200274 ops.datbuf = kbuf;
275 ops.oobbuf = NULL;
Peter Wippichbf514082011-06-06 15:50:58 +0200276 ops.ooboffs = 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200277 ops.len = len;
278
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200279 ret = mtd_write_oob(mtd, *ppos, &ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200280 retlen = ops.retlen;
281 break;
282 }
283
Nicolas Pitre31f42332005-02-08 17:45:55 +0000284 default:
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200285 ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000286 }
Christian Riesch9a78bc82014-03-06 12:42:37 +0100287
288 /*
289 * Return -ENOSPC only if no data could be written at all.
290 * Otherwise just return the number of bytes that actually
291 * have been written.
292 */
293 if ((ret == -ENOSPC) && (total_retlen))
294 break;
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!ret) {
297 *ppos += retlen;
298 total_retlen += retlen;
299 count -= retlen;
300 buf += retlen;
301 }
302 else {
303 kfree(kbuf);
304 return ret;
305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 }
307
Thago Galesib802c072006-04-17 17:38:15 +0100308 kfree(kbuf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return total_retlen;
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200310} /* mtdchar_write */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/*======================================================================
313
314 IOCTL calls for getting device parameters.
315
316======================================================================*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200318static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
319{
320 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200321 size_t retlen;
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200322
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200323 switch (mode) {
324 case MTD_OTP_FACTORY:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100325 if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) ==
326 -EOPNOTSUPP)
327 return -EOPNOTSUPP;
328
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200329 mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200330 break;
331 case MTD_OTP_USER:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100332 if (mtd_read_user_prot_reg(mtd, -1, 0, &retlen, NULL) ==
333 -EOPNOTSUPP)
334 return -EOPNOTSUPP;
335
Artem Bityutskiyb6de3d62011-12-29 10:06:32 +0200336 mfi->mode = MTD_FILE_MODE_OTP_USER;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200337 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200338 case MTD_OTP_OFF:
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100339 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200340 break;
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100341 default:
342 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200343 }
Uwe Kleine-König5dc63fa2013-03-04 17:35:24 +0100344
345 return 0;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200346}
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200347
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200348static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
Kevin Cernekee97718542009-04-08 22:53:13 -0700349 uint64_t start, uint32_t length, void __user *ptr,
350 uint32_t __user *retp)
351{
Miquel Raynal46b58892020-01-14 10:09:52 +0100352 struct mtd_info *master = mtd_get_master(mtd);
Brian Norris9ce244b2011-08-30 18:45:37 -0700353 struct mtd_file_info *mfi = file->private_data;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200354 struct mtd_oob_ops ops = {};
Kevin Cernekee97718542009-04-08 22:53:13 -0700355 uint32_t retlen;
356 int ret = 0;
357
358 if (!(file->f_mode & FMODE_WRITE))
359 return -EPERM;
360
361 if (length > 4096)
362 return -EINVAL;
363
Miquel Raynal46b58892020-01-14 10:09:52 +0100364 if (!master->_write_oob)
Al Viro0db188f2017-09-29 13:55:19 -0400365 return -EOPNOTSUPP;
Kevin Cernekee97718542009-04-08 22:53:13 -0700366
367 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700368 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700369 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700370 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700371 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700372
373 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
374 return -EINVAL;
375
Julia Lawalldf1f1d12010-05-22 10:22:49 +0200376 ops.oobbuf = memdup_user(ptr, length);
377 if (IS_ERR(ops.oobbuf))
378 return PTR_ERR(ops.oobbuf);
Kevin Cernekee97718542009-04-08 22:53:13 -0700379
Brian Norris305b93f2011-08-23 17:17:32 -0700380 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200381 ret = mtd_write_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700382
383 if (ops.oobretlen > 0xFFFFFFFFU)
384 ret = -EOVERFLOW;
385 retlen = ops.oobretlen;
386 if (copy_to_user(retp, &retlen, sizeof(length)))
387 ret = -EFAULT;
388
389 kfree(ops.oobbuf);
390 return ret;
391}
392
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200393static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
Brian Norrisc46f6482011-08-30 18:45:38 -0700394 uint64_t start, uint32_t length, void __user *ptr,
395 uint32_t __user *retp)
Kevin Cernekee97718542009-04-08 22:53:13 -0700396{
Brian Norrisc46f6482011-08-30 18:45:38 -0700397 struct mtd_file_info *mfi = file->private_data;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200398 struct mtd_oob_ops ops = {};
Kevin Cernekee97718542009-04-08 22:53:13 -0700399 int ret = 0;
400
401 if (length > 4096)
402 return -EINVAL;
403
Kevin Cernekee97718542009-04-08 22:53:13 -0700404 ops.ooblen = length;
Brian Norris305b93f2011-08-23 17:17:32 -0700405 ops.ooboffs = start & (mtd->writesize - 1);
Kevin Cernekee97718542009-04-08 22:53:13 -0700406 ops.datbuf = NULL;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700407 ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
Brian Norris0612b9d2011-08-30 18:45:40 -0700408 MTD_OPS_PLACE_OOB;
Kevin Cernekee97718542009-04-08 22:53:13 -0700409
410 if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
411 return -EINVAL;
412
413 ops.oobbuf = kmalloc(length, GFP_KERNEL);
414 if (!ops.oobbuf)
415 return -ENOMEM;
416
Brian Norris305b93f2011-08-23 17:17:32 -0700417 start &= ~((uint64_t)mtd->writesize - 1);
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200418 ret = mtd_read_oob(mtd, start, &ops);
Kevin Cernekee97718542009-04-08 22:53:13 -0700419
420 if (put_user(ops.oobretlen, retp))
421 ret = -EFAULT;
422 else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
423 ops.oobretlen))
424 ret = -EFAULT;
425
426 kfree(ops.oobbuf);
Brian Norris041e4572011-06-23 16:45:24 -0700427
428 /*
429 * NAND returns -EBADMSG on ECC errors, but it returns the OOB
430 * data. For our userspace tools it is important to dump areas
431 * with ECC errors!
432 * For kernel internal usage it also might return -EUCLEAN
Nobuhiro Iwamatsu5d708ec2017-01-27 10:36:38 +0900433 * to signal the caller that a bitflip has occurred and has
Brian Norris041e4572011-06-23 16:45:24 -0700434 * been corrected by the ECC algorithm.
435 *
Brian Norrisc478d7e2011-06-28 16:29:00 -0700436 * Note: currently the standard NAND function, nand_read_oob_std,
437 * does not calculate ECC for the OOB area, so do not rely on
438 * this behavior unless you have replaced it with your own.
Brian Norris041e4572011-06-23 16:45:24 -0700439 */
Brian Norrisd57f40542011-09-20 18:34:25 -0700440 if (mtd_is_bitflip_or_eccerr(ret))
Brian Norris041e4572011-06-23 16:45:24 -0700441 return 0;
442
Kevin Cernekee97718542009-04-08 22:53:13 -0700443 return ret;
444}
445
Brian Norriscc26c3c2010-08-24 18:12:00 -0700446/*
Boris Brezillonaab616e2016-02-04 10:16:18 +0100447 * Copies (and truncates, if necessary) OOB layout information to the
448 * deprecated layout struct, nand_ecclayout_user. This is necessary only to
449 * support the deprecated API ioctl ECCGETLAYOUT while allowing all new
450 * functionality to use mtd_ooblayout_ops flexibly (i.e. mtd_ooblayout_ops
451 * can describe any kind of OOB layout with almost zero overhead from a
452 * memory usage point of view).
Brian Norriscc26c3c2010-08-24 18:12:00 -0700453 */
Boris Brezillonc2b78452016-02-03 20:10:30 +0100454static int shrink_ecclayout(struct mtd_info *mtd,
455 struct nand_ecclayout_user *to)
Brian Norriscc26c3c2010-08-24 18:12:00 -0700456{
Boris Brezillonc2b78452016-02-03 20:10:30 +0100457 struct mtd_oob_region oobregion;
458 int i, section = 0, ret;
Brian Norriscc26c3c2010-08-24 18:12:00 -0700459
Boris Brezillonc2b78452016-02-03 20:10:30 +0100460 if (!mtd || !to)
Brian Norriscc26c3c2010-08-24 18:12:00 -0700461 return -EINVAL;
462
463 memset(to, 0, sizeof(*to));
464
Boris Brezillonc2b78452016-02-03 20:10:30 +0100465 to->eccbytes = 0;
466 for (i = 0; i < MTD_MAX_ECCPOS_ENTRIES;) {
467 u32 eccpos;
468
OuYang ZhiZhong6de56492018-03-11 15:59:07 +0800469 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
Boris Brezillonc2b78452016-02-03 20:10:30 +0100470 if (ret < 0) {
471 if (ret != -ERANGE)
472 return ret;
473
474 break;
475 }
476
477 eccpos = oobregion.offset;
478 for (; i < MTD_MAX_ECCPOS_ENTRIES &&
479 eccpos < oobregion.offset + oobregion.length; i++) {
480 to->eccpos[i] = eccpos++;
481 to->eccbytes++;
482 }
483 }
Brian Norriscc26c3c2010-08-24 18:12:00 -0700484
485 for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
Boris Brezillonc2b78452016-02-03 20:10:30 +0100486 ret = mtd_ooblayout_free(mtd, i, &oobregion);
487 if (ret < 0) {
488 if (ret != -ERANGE)
489 return ret;
490
Brian Norriscc26c3c2010-08-24 18:12:00 -0700491 break;
Boris Brezillonc2b78452016-02-03 20:10:30 +0100492 }
493
494 to->oobfree[i].offset = oobregion.offset;
495 to->oobfree[i].length = oobregion.length;
496 to->oobavail += to->oobfree[i].length;
Brian Norriscc26c3c2010-08-24 18:12:00 -0700497 }
498
499 return 0;
500}
501
Boris Brezillonc2b78452016-02-03 20:10:30 +0100502static int get_oobinfo(struct mtd_info *mtd, struct nand_oobinfo *to)
503{
504 struct mtd_oob_region oobregion;
505 int i, section = 0, ret;
506
507 if (!mtd || !to)
508 return -EINVAL;
509
510 memset(to, 0, sizeof(*to));
511
512 to->eccbytes = 0;
513 for (i = 0; i < ARRAY_SIZE(to->eccpos);) {
514 u32 eccpos;
515
OuYang ZhiZhong6de56492018-03-11 15:59:07 +0800516 ret = mtd_ooblayout_ecc(mtd, section++, &oobregion);
Boris Brezillonc2b78452016-02-03 20:10:30 +0100517 if (ret < 0) {
518 if (ret != -ERANGE)
519 return ret;
520
521 break;
522 }
523
524 if (oobregion.length + i > ARRAY_SIZE(to->eccpos))
525 return -EINVAL;
526
527 eccpos = oobregion.offset;
528 for (; eccpos < oobregion.offset + oobregion.length; i++) {
529 to->eccpos[i] = eccpos++;
530 to->eccbytes++;
531 }
532 }
533
534 for (i = 0; i < 8; i++) {
535 ret = mtd_ooblayout_free(mtd, i, &oobregion);
536 if (ret < 0) {
537 if (ret != -ERANGE)
538 return ret;
539
540 break;
541 }
542
543 to->oobfree[i][0] = oobregion.offset;
544 to->oobfree[i][1] = oobregion.length;
545 }
546
547 to->useecc = MTD_NANDECC_AUTOPLACE;
548
549 return 0;
550}
551
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200552static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
Brian Norris53bb7242015-09-21 13:26:59 -0700553 struct blkpg_ioctl_arg *arg)
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300554{
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300555 struct blkpg_partition p;
556
557 if (!capable(CAP_SYS_ADMIN))
558 return -EPERM;
559
Brian Norris53bb7242015-09-21 13:26:59 -0700560 if (copy_from_user(&p, arg->data, sizeof(p)))
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300561 return -EFAULT;
562
Brian Norris53bb7242015-09-21 13:26:59 -0700563 switch (arg->op) {
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300564 case BLKPG_ADD_PARTITION:
565
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200566 /* Only master mtd device must be used to add partitions */
567 if (mtd_is_partition(mtd))
568 return -EINVAL;
569
Brian Norris1cc8d842014-07-21 19:08:13 -0700570 /* Sanitize user input */
571 p.devname[BLKPG_DEVNAMELTH - 1] = '\0';
572
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300573 return mtd_add_partition(mtd, p.devname, p.start, p.length);
574
575 case BLKPG_DEL_PARTITION:
576
577 if (p.pno < 0)
578 return -EINVAL;
579
580 return mtd_del_partition(mtd, p.pno);
581
582 default:
583 return -EINVAL;
584 }
585}
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300586
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200587static int mtdchar_write_ioctl(struct mtd_info *mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700588 struct mtd_write_req __user *argp)
589{
Miquel Raynal46b58892020-01-14 10:09:52 +0100590 struct mtd_info *master = mtd_get_master(mtd);
Brian Norrise99d8b02011-09-09 09:59:03 -0700591 struct mtd_write_req req;
Miquel Raynal717bc8a2019-09-19 21:06:21 +0200592 struct mtd_oob_ops ops = {};
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200593 const void __user *usr_data, *usr_oob;
Brian Norrise99d8b02011-09-09 09:59:03 -0700594 int ret;
595
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200596 if (copy_from_user(&req, argp, sizeof(req)))
Brian Norrise99d8b02011-09-09 09:59:03 -0700597 return -EFAULT;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200598
599 usr_data = (const void __user *)(uintptr_t)req.usr_data;
600 usr_oob = (const void __user *)(uintptr_t)req.usr_oob;
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200601
Miquel Raynal46b58892020-01-14 10:09:52 +0100602 if (!master->_write_oob)
Brian Norrise99d8b02011-09-09 09:59:03 -0700603 return -EOPNOTSUPP;
Brian Norrise99d8b02011-09-09 09:59:03 -0700604 ops.mode = req.mode;
605 ops.len = (size_t)req.len;
606 ops.ooblen = (size_t)req.ooblen;
607 ops.ooboffs = 0;
608
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200609 if (usr_data) {
Brian Norrise99d8b02011-09-09 09:59:03 -0700610 ops.datbuf = memdup_user(usr_data, ops.len);
611 if (IS_ERR(ops.datbuf))
612 return PTR_ERR(ops.datbuf);
613 } else {
614 ops.datbuf = NULL;
615 }
616
Geert Uytterhoevenf62cde42014-05-01 20:40:54 +0200617 if (usr_oob) {
Brian Norrise99d8b02011-09-09 09:59:03 -0700618 ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
619 if (IS_ERR(ops.oobbuf)) {
620 kfree(ops.datbuf);
621 return PTR_ERR(ops.oobbuf);
622 }
623 } else {
624 ops.oobbuf = NULL;
625 }
626
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200627 ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
Brian Norrise99d8b02011-09-09 09:59:03 -0700628
629 kfree(ops.datbuf);
630 kfree(ops.oobbuf);
631
632 return ret;
633}
634
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200635static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200637 struct mtd_file_info *mfi = file->private_data;
638 struct mtd_info *mtd = mfi->mtd;
Miquel Raynal46b58892020-01-14 10:09:52 +0100639 struct mtd_info *master = mtd_get_master(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 void __user *argp = (void __user *)arg;
641 int ret = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200642 struct mtd_info_user info;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000643
Brian Norris289c0522011-07-19 10:06:09 -0700644 pr_debug("MTD_ioctl\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 switch (cmd) {
647 case MEMGETREGIONCOUNT:
648 if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
649 return -EFAULT;
650 break;
651
652 case MEMGETREGIONINFO:
653 {
Zev Weissb67c5f82008-09-01 05:02:12 -0700654 uint32_t ur_idx;
655 struct mtd_erase_region_info *kr;
H Hartley Sweetenbcc98a42010-01-15 11:25:38 -0700656 struct region_info_user __user *ur = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
Zev Weissb67c5f82008-09-01 05:02:12 -0700658 if (get_user(ur_idx, &(ur->regionindex)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 return -EFAULT;
660
Dan Carpenter5e59be12010-09-08 21:39:56 +0200661 if (ur_idx >= mtd->numeraseregions)
662 return -EINVAL;
663
Zev Weissb67c5f82008-09-01 05:02:12 -0700664 kr = &(mtd->eraseregions[ur_idx]);
665
666 if (put_user(kr->offset, &(ur->offset))
667 || put_user(kr->erasesize, &(ur->erasesize))
668 || put_user(kr->numblocks, &(ur->numblocks)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return -EFAULT;
Zev Weissb67c5f82008-09-01 05:02:12 -0700670
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 break;
672 }
673
674 case MEMGETINFO:
Vasiliy Kulikova0c5a392010-11-06 17:41:24 +0300675 memset(&info, 0, sizeof(info));
Joern Engel73c619e2006-05-30 14:25:35 +0200676 info.type = mtd->type;
677 info.flags = mtd->flags;
678 info.size = mtd->size;
679 info.erasesize = mtd->erasesize;
680 info.writesize = mtd->writesize;
681 info.oobsize = mtd->oobsize;
Brian Norris19fb4342011-08-30 18:45:46 -0700682 /* The below field is obsolete */
683 info.padding = 0;
Joern Engel73c619e2006-05-30 14:25:35 +0200684 if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return -EFAULT;
686 break;
687
688 case MEMERASE:
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700689 case MEMERASE64:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 {
691 struct erase_info *erase;
692
Al Viroaeb5d722008-09-02 15:28:45 -0400693 if(!(file->f_mode & FMODE_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return -EPERM;
695
Burman Yan95b93a02006-11-15 21:10:29 +0200696 erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!erase)
698 ret = -ENOMEM;
699 else {
Kevin Cernekee0dc54e92009-04-08 22:52:28 -0700700 if (cmd == MEMERASE64) {
701 struct erase_info_user64 einfo64;
702
703 if (copy_from_user(&einfo64, argp,
704 sizeof(struct erase_info_user64))) {
705 kfree(erase);
706 return -EFAULT;
707 }
708 erase->addr = einfo64.start;
709 erase->len = einfo64.length;
710 } else {
711 struct erase_info_user einfo32;
712
713 if (copy_from_user(&einfo32, argp,
714 sizeof(struct erase_info_user))) {
715 kfree(erase);
716 return -EFAULT;
717 }
718 erase->addr = einfo32.start;
719 erase->len = einfo32.length;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 }
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000721
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200722 ret = mtd_erase(mtd, erase);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 kfree(erase);
724 }
725 break;
726 }
727
728 case MEMWRITEOOB:
729 {
730 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700731 struct mtd_oob_buf __user *buf_user = argp;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000732
Kevin Cernekee97718542009-04-08 22:53:13 -0700733 /* NOTE: writes return length to buf_user->length */
734 if (copy_from_user(&buf, argp, sizeof(buf)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ret = -EFAULT;
Kevin Cernekee97718542009-04-08 22:53:13 -0700736 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200737 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700738 buf.ptr, &buf_user->length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
742 case MEMREADOOB:
743 {
744 struct mtd_oob_buf buf;
Kevin Cernekee97718542009-04-08 22:53:13 -0700745 struct mtd_oob_buf __user *buf_user = argp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Kevin Cernekee97718542009-04-08 22:53:13 -0700747 /* NOTE: writes return length to buf_user->start */
748 if (copy_from_user(&buf, argp, sizeof(buf)))
749 ret = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200751 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekee97718542009-04-08 22:53:13 -0700752 buf.ptr, &buf_user->start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 break;
754 }
755
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700756 case MEMWRITEOOB64:
757 {
758 struct mtd_oob_buf64 buf;
759 struct mtd_oob_buf64 __user *buf_user = argp;
760
761 if (copy_from_user(&buf, argp, sizeof(buf)))
762 ret = -EFAULT;
763 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200764 ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700765 (void __user *)(uintptr_t)buf.usr_ptr,
766 &buf_user->length);
767 break;
768 }
769
770 case MEMREADOOB64:
771 {
772 struct mtd_oob_buf64 buf;
773 struct mtd_oob_buf64 __user *buf_user = argp;
774
775 if (copy_from_user(&buf, argp, sizeof(buf)))
776 ret = -EFAULT;
777 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200778 ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
Kevin Cernekeeaea7cea2009-04-08 22:53:49 -0700779 (void __user *)(uintptr_t)buf.usr_ptr,
780 &buf_user->length);
781 break;
782 }
783
Brian Norrise99d8b02011-09-09 09:59:03 -0700784 case MEMWRITE:
785 {
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200786 ret = mtdchar_write_ioctl(mtd,
Brian Norrise99d8b02011-09-09 09:59:03 -0700787 (struct mtd_write_req __user *)arg);
788 break;
789 }
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 case MEMLOCK:
792 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700793 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Harvey Harrison175428b2008-07-03 23:40:14 -0700795 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 return -EFAULT;
797
Artem Bityutskiy38134562011-12-30 17:00:35 +0200798 ret = mtd_lock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 break;
800 }
801
802 case MEMUNLOCK:
803 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700804 struct erase_info_user einfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Harvey Harrison175428b2008-07-03 23:40:14 -0700806 if (copy_from_user(&einfo, argp, sizeof(einfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return -EFAULT;
808
Artem Bityutskiy38134562011-12-30 17:00:35 +0200809 ret = mtd_unlock(mtd, einfo.start, einfo.length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 break;
811 }
812
Richard Cochran99384242010-06-14 18:10:33 +0200813 case MEMISLOCKED:
814 {
815 struct erase_info_user einfo;
816
817 if (copy_from_user(&einfo, argp, sizeof(einfo)))
818 return -EFAULT;
819
Artem Bityutskiy38134562011-12-30 17:00:35 +0200820 ret = mtd_is_locked(mtd, einfo.start, einfo.length);
Richard Cochran99384242010-06-14 18:10:33 +0200821 break;
822 }
823
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200824 /* Legacy interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 case MEMGETOOBSEL:
826 {
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200827 struct nand_oobinfo oi;
828
Miquel Raynal46b58892020-01-14 10:09:52 +0100829 if (!master->ooblayout)
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200830 return -EOPNOTSUPP;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200831
Boris Brezillonc2b78452016-02-03 20:10:30 +0100832 ret = get_oobinfo(mtd, &oi);
833 if (ret)
834 return ret;
Thomas Gleixner5bd34c02006-05-27 22:16:10 +0200835
836 if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 return -EFAULT;
838 break;
839 }
840
841 case MEMGETBADBLOCK:
842 {
843 loff_t offs;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 if (copy_from_user(&offs, argp, sizeof(loff_t)))
846 return -EFAULT;
Artem Bityutskiy8f461a72012-01-02 13:48:54 +0200847 return mtd_block_isbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 break;
849 }
850
851 case MEMSETBADBLOCK:
852 {
853 loff_t offs;
854
855 if (copy_from_user(&offs, argp, sizeof(loff_t)))
856 return -EFAULT;
Artem Bityutskiy800ffd32012-01-02 13:59:12 +0200857 return mtd_block_markbad(mtd, offs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 break;
859 }
860
Nicolas Pitre31f42332005-02-08 17:45:55 +0000861 case OTPSELECT:
862 {
863 int mode;
864 if (copy_from_user(&mode, argp, sizeof(int)))
865 return -EFAULT;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200866
Brian Norrisbeb133f2011-08-30 18:45:41 -0700867 mfi->mode = MTD_FILE_MODE_NORMAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200868
869 ret = otp_select_filemode(mfi, mode);
870
Nicolas Pitre81dba482005-04-01 16:36:15 +0100871 file->f_pos = 0;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000872 break;
873 }
874
875 case OTPGETREGIONCOUNT:
876 case OTPGETREGIONINFO:
877 {
878 struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100879 size_t retlen;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000880 if (!buf)
881 return -ENOMEM;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200882 switch (mfi->mode) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700883 case MTD_FILE_MODE_OTP_FACTORY:
Christian Riesch4b78fc42014-01-28 09:29:44 +0100884 ret = mtd_get_fact_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000885 break;
Brian Norrisbeb133f2011-08-30 18:45:41 -0700886 case MTD_FILE_MODE_OTP_USER:
Christian Riesch4b78fc42014-01-28 09:29:44 +0100887 ret = mtd_get_user_prot_info(mtd, 4096, &retlen, buf);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000888 break;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200889 default:
Artem Bityutskiy87e858a92011-12-28 18:47:46 +0200890 ret = -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200891 break;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000892 }
Christian Riesch4b78fc42014-01-28 09:29:44 +0100893 if (!ret) {
Nicolas Pitre31f42332005-02-08 17:45:55 +0000894 if (cmd == OTPGETREGIONCOUNT) {
Christian Riesch4b78fc42014-01-28 09:29:44 +0100895 int nbr = retlen / sizeof(struct otp_info);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000896 ret = copy_to_user(argp, &nbr, sizeof(int));
897 } else
Christian Riesch4b78fc42014-01-28 09:29:44 +0100898 ret = copy_to_user(argp, buf, retlen);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000899 if (ret)
900 ret = -EFAULT;
901 }
902 kfree(buf);
903 break;
904 }
905
906 case OTPLOCK:
907 {
Harvey Harrison175428b2008-07-03 23:40:14 -0700908 struct otp_info oinfo;
Nicolas Pitre31f42332005-02-08 17:45:55 +0000909
Brian Norrisbeb133f2011-08-30 18:45:41 -0700910 if (mfi->mode != MTD_FILE_MODE_OTP_USER)
Nicolas Pitre31f42332005-02-08 17:45:55 +0000911 return -EINVAL;
Harvey Harrison175428b2008-07-03 23:40:14 -0700912 if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
Nicolas Pitre31f42332005-02-08 17:45:55 +0000913 return -EFAULT;
Artem Bityutskiy4403dbfb2011-12-23 18:55:49 +0200914 ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
Nicolas Pitre31f42332005-02-08 17:45:55 +0000915 break;
916 }
Nicolas Pitre31f42332005-02-08 17:45:55 +0000917
Brian Norris7854d3f2011-06-23 14:12:08 -0700918 /* This ioctl is being deprecated - it truncates the ECC layout */
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200919 case ECCGETLAYOUT:
920 {
Brian Norriscc26c3c2010-08-24 18:12:00 -0700921 struct nand_ecclayout_user *usrlay;
922
Miquel Raynal46b58892020-01-14 10:09:52 +0100923 if (!master->ooblayout)
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200924 return -EOPNOTSUPP;
925
Brian Norriscc26c3c2010-08-24 18:12:00 -0700926 usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
927 if (!usrlay)
928 return -ENOMEM;
929
Boris Brezillonc2b78452016-02-03 20:10:30 +0100930 shrink_ecclayout(mtd, usrlay);
Brian Norriscc26c3c2010-08-24 18:12:00 -0700931
932 if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
933 ret = -EFAULT;
934 kfree(usrlay);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200935 break;
936 }
937
938 case ECCGETSTATS:
939 {
940 if (copy_to_user(argp, &mtd->ecc_stats,
941 sizeof(struct mtd_ecc_stats)))
942 return -EFAULT;
943 break;
944 }
945
946 case MTDFILEMODE:
947 {
948 mfi->mode = 0;
949
950 switch(arg) {
Brian Norrisbeb133f2011-08-30 18:45:41 -0700951 case MTD_FILE_MODE_OTP_FACTORY:
952 case MTD_FILE_MODE_OTP_USER:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200953 ret = otp_select_filemode(mfi, arg);
954 break;
955
Brian Norrisbeb133f2011-08-30 18:45:41 -0700956 case MTD_FILE_MODE_RAW:
Artem Bityutskiyfc002e32011-12-28 18:35:07 +0200957 if (!mtd_has_oob(mtd))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200958 return -EOPNOTSUPP;
959 mfi->mode = arg;
960
Brian Norrisbeb133f2011-08-30 18:45:41 -0700961 case MTD_FILE_MODE_NORMAL:
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200962 break;
963 default:
964 ret = -EINVAL;
965 }
966 file->f_pos = 0;
967 break;
968 }
969
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300970 case BLKPG:
971 {
Brian Norris53bb7242015-09-21 13:26:59 -0700972 struct blkpg_ioctl_arg __user *blk_arg = argp;
973 struct blkpg_ioctl_arg a;
974
975 if (copy_from_user(&a, blk_arg, sizeof(a)))
976 ret = -EFAULT;
977 else
978 ret = mtdchar_blkpg_ioctl(mtd, &a);
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300979 break;
980 }
981
982 case BLKRRPART:
983 {
984 /* No reread partition feature. Just return ok */
985 ret = 0;
986 break;
987 }
Roman Tereshonkovd0f79592010-09-17 13:31:42 +0300988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 default:
990 ret = -ENOTTY;
991 }
992
993 return ret;
994} /* memory_ioctl */
995
Artem Bityutskiy969e57a2011-12-23 17:27:46 +0200996static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
Arnd Bergmann55929332010-04-27 00:24:05 +0200997{
998 int ret;
999
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001000 mutex_lock(&mtd_mutex);
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001001 ret = mtdchar_ioctl(file, cmd, arg);
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001002 mutex_unlock(&mtd_mutex);
Arnd Bergmann55929332010-04-27 00:24:05 +02001003
1004 return ret;
1005}
1006
Kevin Cernekee97718542009-04-08 22:53:13 -07001007#ifdef CONFIG_COMPAT
1008
1009struct mtd_oob_buf32 {
1010 u_int32_t start;
1011 u_int32_t length;
1012 compat_caddr_t ptr; /* unsigned char* */
1013};
1014
1015#define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
1016#define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
1017
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001018static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
Kevin Cernekee97718542009-04-08 22:53:13 -07001019 unsigned long arg)
1020{
1021 struct mtd_file_info *mfi = file->private_data;
1022 struct mtd_info *mtd = mfi->mtd;
David Woodhouse0b6585c2009-05-29 16:09:08 +01001023 void __user *argp = compat_ptr(arg);
Kevin Cernekee97718542009-04-08 22:53:13 -07001024 int ret = 0;
1025
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001026 mutex_lock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001027
1028 switch (cmd) {
1029 case MEMWRITEOOB32:
1030 {
1031 struct mtd_oob_buf32 buf;
1032 struct mtd_oob_buf32 __user *buf_user = argp;
1033
1034 if (copy_from_user(&buf, argp, sizeof(buf)))
1035 ret = -EFAULT;
1036 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001037 ret = mtdchar_writeoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001038 buf.length, compat_ptr(buf.ptr),
1039 &buf_user->length);
1040 break;
1041 }
1042
1043 case MEMREADOOB32:
1044 {
1045 struct mtd_oob_buf32 buf;
1046 struct mtd_oob_buf32 __user *buf_user = argp;
1047
1048 /* NOTE: writes return length to buf->start */
1049 if (copy_from_user(&buf, argp, sizeof(buf)))
1050 ret = -EFAULT;
1051 else
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001052 ret = mtdchar_readoob(file, mtd, buf.start,
Kevin Cernekee97718542009-04-08 22:53:13 -07001053 buf.length, compat_ptr(buf.ptr),
1054 &buf_user->start);
1055 break;
1056 }
Brian Norris53bb7242015-09-21 13:26:59 -07001057
1058 case BLKPG:
1059 {
1060 /* Convert from blkpg_compat_ioctl_arg to blkpg_ioctl_arg */
1061 struct blkpg_compat_ioctl_arg __user *uarg = argp;
1062 struct blkpg_compat_ioctl_arg compat_arg;
1063 struct blkpg_ioctl_arg a;
1064
1065 if (copy_from_user(&compat_arg, uarg, sizeof(compat_arg))) {
1066 ret = -EFAULT;
1067 break;
1068 }
1069
1070 memset(&a, 0, sizeof(a));
1071 a.op = compat_arg.op;
1072 a.flags = compat_arg.flags;
1073 a.datalen = compat_arg.datalen;
1074 a.data = compat_ptr(compat_arg.data);
1075
1076 ret = mtdchar_blkpg_ioctl(mtd, &a);
1077 break;
1078 }
1079
Kevin Cernekee97718542009-04-08 22:53:13 -07001080 default:
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001081 ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
Kevin Cernekee97718542009-04-08 22:53:13 -07001082 }
1083
Arnd Bergmann5aa82942010-06-02 14:28:52 +02001084 mutex_unlock(&mtd_mutex);
Kevin Cernekee97718542009-04-08 22:53:13 -07001085
1086 return ret;
1087}
1088
1089#endif /* CONFIG_COMPAT */
1090
David Howells402d3262009-02-12 10:40:00 +00001091/*
1092 * try to determine where a shared mapping can be made
1093 * - only supported for NOMMU at the moment (MMU can't doesn't copy private
1094 * mappings)
1095 */
1096#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001097static unsigned long mtdchar_get_unmapped_area(struct file *file,
David Howells402d3262009-02-12 10:40:00 +00001098 unsigned long addr,
1099 unsigned long len,
1100 unsigned long pgoff,
1101 unsigned long flags)
1102{
1103 struct mtd_file_info *mfi = file->private_data;
1104 struct mtd_info *mtd = mfi->mtd;
Artem Bityutskiycd621272011-12-30 14:31:57 +02001105 unsigned long offset;
1106 int ret;
David Howells402d3262009-02-12 10:40:00 +00001107
Artem Bityutskiycd621272011-12-30 14:31:57 +02001108 if (addr != 0)
1109 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001110
Artem Bityutskiycd621272011-12-30 14:31:57 +02001111 if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
1112 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001113
Artem Bityutskiycd621272011-12-30 14:31:57 +02001114 offset = pgoff << PAGE_SHIFT;
1115 if (offset > mtd->size - len)
1116 return (unsigned long) -EINVAL;
David Howells402d3262009-02-12 10:40:00 +00001117
Artem Bityutskiycd621272011-12-30 14:31:57 +02001118 ret = mtd_get_unmapped_area(mtd, len, offset, flags);
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001119 return ret == -EOPNOTSUPP ? -ENODEV : ret;
David Howells402d3262009-02-12 10:40:00 +00001120}
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001121
1122static unsigned mtdchar_mmap_capabilities(struct file *file)
1123{
1124 struct mtd_file_info *mfi = file->private_data;
1125
1126 return mtd_mmap_capabilities(mfi->mtd);
1127}
David Howells402d3262009-02-12 10:40:00 +00001128#endif
1129
1130/*
1131 * set up a mapping for shared memory segments
1132 */
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001133static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
David Howells402d3262009-02-12 10:40:00 +00001134{
1135#ifdef CONFIG_MMU
1136 struct mtd_file_info *mfi = file->private_data;
1137 struct mtd_info *mtd = mfi->mtd;
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001138 struct map_info *map = mtd->priv;
David Howells402d3262009-02-12 10:40:00 +00001139
David Woodhousef5cf8f02012-10-09 15:08:10 +01001140 /* This is broken because it assumes the MTD device is map-based
1141 and that mtd->priv is a valid struct map_info. It should be
1142 replaced with something that uses the mtd_get_unmapped_area()
1143 operation properly. */
1144 if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001145#ifdef pgprot_noncached
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001146 if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001147 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
1148#endif
Linus Torvalds8558e4a2013-04-19 09:53:07 -07001149 return vm_iomap_memory(vma, map->phys, map->size);
Anatolij Gustschindd02b672010-06-15 09:30:15 +02001150 }
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001151 return -ENODEV;
David Howells402d3262009-02-12 10:40:00 +00001152#else
Vladimir Zapolskiyb9995932013-10-28 18:08:15 +02001153 return vma->vm_flags & VM_SHARED ? 0 : -EACCES;
David Howells402d3262009-02-12 10:40:00 +00001154#endif
1155}
1156
Arjan van de Vend54b1fd2007-02-12 00:55:34 -08001157static const struct file_operations mtd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 .owner = THIS_MODULE,
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001159 .llseek = mtdchar_lseek,
1160 .read = mtdchar_read,
1161 .write = mtdchar_write,
1162 .unlocked_ioctl = mtdchar_unlocked_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001163#ifdef CONFIG_COMPAT
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001164 .compat_ioctl = mtdchar_compat_ioctl,
Kevin Cernekee97718542009-04-08 22:53:13 -07001165#endif
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001166 .open = mtdchar_open,
1167 .release = mtdchar_close,
1168 .mmap = mtdchar_mmap,
David Howells402d3262009-02-12 10:40:00 +00001169#ifndef CONFIG_MMU
Artem Bityutskiy969e57a2011-12-23 17:27:46 +02001170 .get_unmapped_area = mtdchar_get_unmapped_area,
Christoph Hellwigb4caecd2015-01-14 10:42:32 +01001171 .mmap_capabilities = mtdchar_mmap_capabilities,
David Howells402d3262009-02-12 10:40:00 +00001172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173};
1174
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001175int __init init_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001177 int ret;
David Brownell1f24b5a2009-03-26 00:42:41 -07001178
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001179 ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001180 "mtd", &mtd_fops);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001181 if (ret < 0) {
Artem Bityutskiy57ae2b62013-03-15 12:59:36 +02001182 pr_err("Can't allocate major number %d for MTD\n",
1183 MTD_CHAR_MAJOR);
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001184 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 }
1186
Kirill A. Shutemovcd874232010-05-17 16:55:47 +03001187 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
Artem Bityutskiy660685d2013-03-14 13:27:40 +02001190void __exit cleanup_mtdchar(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Ben Hutchingsdad0db32010-01-29 21:00:04 +00001192 __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
Scott James Remnant90160e12009-03-02 18:42:39 +00001195MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);