mtdchar.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258
  1. /*
  2. * Copyright © 1999-2010 David Woodhouse <dwmw2@infradead.org>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. *
  18. */
  19. #include <linux/device.h>
  20. #include <linux/fs.h>
  21. #include <linux/mm.h>
  22. #include <linux/err.h>
  23. #include <linux/init.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/mutex.h>
  29. #include <linux/backing-dev.h>
  30. #include <linux/compat.h>
  31. #include <linux/mount.h>
  32. #include <linux/blkpg.h>
  33. #include <linux/magic.h>
  34. #include <linux/mtd/mtd.h>
  35. #include <linux/mtd/partitions.h>
  36. #include <linux/mtd/map.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <asm/uaccess.h>
  39. static DEFINE_MUTEX(mtd_mutex);
  40. /*
  41. * Data structure to hold the pointer to the mtd device as well
  42. * as mode information of various use cases.
  43. */
  44. struct mtd_file_info {
  45. struct mtd_info *mtd;
  46. struct inode *ino;
  47. enum mtd_file_modes mode;
  48. };
  49. static loff_t mtdchar_lseek(struct file *file, loff_t offset, int orig)
  50. {
  51. struct mtd_file_info *mfi = file->private_data;
  52. struct mtd_info *mtd = mfi->mtd;
  53. switch (orig) {
  54. case SEEK_SET:
  55. break;
  56. case SEEK_CUR:
  57. offset += file->f_pos;
  58. break;
  59. case SEEK_END:
  60. offset += mtd->size;
  61. break;
  62. default:
  63. return -EINVAL;
  64. }
  65. if (offset >= 0 && offset <= mtd->size)
  66. return file->f_pos = offset;
  67. return -EINVAL;
  68. }
  69. static int count;
  70. static struct vfsmount *mnt;
  71. static struct file_system_type mtd_inodefs_type;
  72. static int mtdchar_open(struct inode *inode, struct file *file)
  73. {
  74. int minor = iminor(inode);
  75. int devnum = minor >> 1;
  76. int ret = 0;
  77. struct mtd_info *mtd;
  78. struct mtd_file_info *mfi;
  79. struct inode *mtd_ino;
  80. pr_debug("MTD_open\n");
  81. /* You can't open the RO devices RW */
  82. if ((file->f_mode & FMODE_WRITE) && (minor & 1))
  83. return -EACCES;
  84. ret = simple_pin_fs(&mtd_inodefs_type, &mnt, &count);
  85. if (ret)
  86. return ret;
  87. mutex_lock(&mtd_mutex);
  88. mtd = get_mtd_device(NULL, devnum);
  89. if (IS_ERR(mtd)) {
  90. ret = PTR_ERR(mtd);
  91. goto out;
  92. }
  93. if (mtd->type == MTD_ABSENT) {
  94. ret = -ENODEV;
  95. goto out1;
  96. }
  97. mtd_ino = iget_locked(mnt->mnt_sb, devnum);
  98. if (!mtd_ino) {
  99. ret = -ENOMEM;
  100. goto out1;
  101. }
  102. if (mtd_ino->i_state & I_NEW) {
  103. mtd_ino->i_private = mtd;
  104. mtd_ino->i_mode = S_IFCHR;
  105. mtd_ino->i_data.backing_dev_info = mtd->backing_dev_info;
  106. unlock_new_inode(mtd_ino);
  107. }
  108. file->f_mapping = mtd_ino->i_mapping;
  109. /* You can't open it RW if it's not a writeable device */
  110. if ((file->f_mode & FMODE_WRITE) && !(mtd->flags & MTD_WRITEABLE)) {
  111. ret = -EACCES;
  112. goto out2;
  113. }
  114. mfi = kzalloc(sizeof(*mfi), GFP_KERNEL);
  115. if (!mfi) {
  116. ret = -ENOMEM;
  117. goto out2;
  118. }
  119. mfi->ino = mtd_ino;
  120. mfi->mtd = mtd;
  121. file->private_data = mfi;
  122. mutex_unlock(&mtd_mutex);
  123. return 0;
  124. out2:
  125. iput(mtd_ino);
  126. out1:
  127. put_mtd_device(mtd);
  128. out:
  129. mutex_unlock(&mtd_mutex);
  130. simple_release_fs(&mnt, &count);
  131. return ret;
  132. } /* mtdchar_open */
  133. /*====================================================================*/
  134. static int mtdchar_close(struct inode *inode, struct file *file)
  135. {
  136. struct mtd_file_info *mfi = file->private_data;
  137. struct mtd_info *mtd = mfi->mtd;
  138. pr_debug("MTD_close\n");
  139. /* Only sync if opened RW */
  140. if ((file->f_mode & FMODE_WRITE))
  141. mtd_sync(mtd);
  142. iput(mfi->ino);
  143. put_mtd_device(mtd);
  144. file->private_data = NULL;
  145. kfree(mfi);
  146. simple_release_fs(&mnt, &count);
  147. return 0;
  148. } /* mtdchar_close */
  149. /* Back in June 2001, dwmw2 wrote:
  150. *
  151. * FIXME: This _really_ needs to die. In 2.5, we should lock the
  152. * userspace buffer down and use it directly with readv/writev.
  153. *
  154. * The implementation below, using mtd_kmalloc_up_to, mitigates
  155. * allocation failures when the system is under low-memory situations
  156. * or if memory is highly fragmented at the cost of reducing the
  157. * performance of the requested transfer due to a smaller buffer size.
  158. *
  159. * A more complex but more memory-efficient implementation based on
  160. * get_user_pages and iovecs to cover extents of those pages is a
  161. * longer-term goal, as intimated by dwmw2 above. However, for the
  162. * write case, this requires yet more complex head and tail transfer
  163. * handling when those head and tail offsets and sizes are such that
  164. * alignment requirements are not met in the NAND subdriver.
  165. */
  166. static ssize_t mtdchar_read(struct file *file, char __user *buf, size_t count,
  167. loff_t *ppos)
  168. {
  169. struct mtd_file_info *mfi = file->private_data;
  170. struct mtd_info *mtd = mfi->mtd;
  171. size_t retlen;
  172. size_t total_retlen=0;
  173. int ret=0;
  174. int len;
  175. size_t size = count;
  176. char *kbuf;
  177. pr_debug("MTD_read\n");
  178. if (*ppos + count > mtd->size)
  179. count = mtd->size - *ppos;
  180. if (!count)
  181. return 0;
  182. kbuf = mtd_kmalloc_up_to(mtd, &size);
  183. if (!kbuf)
  184. return -ENOMEM;
  185. while (count) {
  186. len = min_t(size_t, count, size);
  187. switch (mfi->mode) {
  188. case MTD_FILE_MODE_OTP_FACTORY:
  189. ret = mtd_read_fact_prot_reg(mtd, *ppos, len,
  190. &retlen, kbuf);
  191. break;
  192. case MTD_FILE_MODE_OTP_USER:
  193. ret = mtd_read_user_prot_reg(mtd, *ppos, len,
  194. &retlen, kbuf);
  195. break;
  196. case MTD_FILE_MODE_RAW:
  197. {
  198. struct mtd_oob_ops ops;
  199. ops.mode = MTD_OPS_RAW;
  200. ops.datbuf = kbuf;
  201. ops.oobbuf = NULL;
  202. ops.len = len;
  203. ret = mtd_read_oob(mtd, *ppos, &ops);
  204. retlen = ops.retlen;
  205. break;
  206. }
  207. default:
  208. ret = mtd_read(mtd, *ppos, len, &retlen, kbuf);
  209. }
  210. /* Nand returns -EBADMSG on ECC errors, but it returns
  211. * the data. For our userspace tools it is important
  212. * to dump areas with ECC errors!
  213. * For kernel internal usage it also might return -EUCLEAN
  214. * to signal the caller that a bitflip has occurred and has
  215. * been corrected by the ECC algorithm.
  216. * Userspace software which accesses NAND this way
  217. * must be aware of the fact that it deals with NAND
  218. */
  219. if (!ret || mtd_is_bitflip_or_eccerr(ret)) {
  220. *ppos += retlen;
  221. if (copy_to_user(buf, kbuf, retlen)) {
  222. kfree(kbuf);
  223. return -EFAULT;
  224. }
  225. else
  226. total_retlen += retlen;
  227. count -= retlen;
  228. buf += retlen;
  229. if (retlen == 0)
  230. count = 0;
  231. }
  232. else {
  233. kfree(kbuf);
  234. return ret;
  235. }
  236. }
  237. kfree(kbuf);
  238. return total_retlen;
  239. } /* mtdchar_read */
  240. static ssize_t mtdchar_write(struct file *file, const char __user *buf, size_t count,
  241. loff_t *ppos)
  242. {
  243. struct mtd_file_info *mfi = file->private_data;
  244. struct mtd_info *mtd = mfi->mtd;
  245. size_t size = count;
  246. char *kbuf;
  247. size_t retlen;
  248. size_t total_retlen=0;
  249. int ret=0;
  250. int len;
  251. pr_debug("MTD_write\n");
  252. if (*ppos == mtd->size)
  253. return -ENOSPC;
  254. if (*ppos + count > mtd->size)
  255. count = mtd->size - *ppos;
  256. if (!count)
  257. return 0;
  258. kbuf = mtd_kmalloc_up_to(mtd, &size);
  259. if (!kbuf)
  260. return -ENOMEM;
  261. while (count) {
  262. len = min_t(size_t, count, size);
  263. if (copy_from_user(kbuf, buf, len)) {
  264. kfree(kbuf);
  265. return -EFAULT;
  266. }
  267. switch (mfi->mode) {
  268. case MTD_FILE_MODE_OTP_FACTORY:
  269. ret = -EROFS;
  270. break;
  271. case MTD_FILE_MODE_OTP_USER:
  272. ret = mtd_write_user_prot_reg(mtd, *ppos, len,
  273. &retlen, kbuf);
  274. break;
  275. case MTD_FILE_MODE_RAW:
  276. {
  277. struct mtd_oob_ops ops;
  278. ops.mode = MTD_OPS_RAW;
  279. ops.datbuf = kbuf;
  280. ops.oobbuf = NULL;
  281. ops.ooboffs = 0;
  282. ops.len = len;
  283. ret = mtd_write_oob(mtd, *ppos, &ops);
  284. retlen = ops.retlen;
  285. break;
  286. }
  287. default:
  288. ret = mtd_write(mtd, *ppos, len, &retlen, kbuf);
  289. }
  290. if (!ret) {
  291. *ppos += retlen;
  292. total_retlen += retlen;
  293. count -= retlen;
  294. buf += retlen;
  295. }
  296. else {
  297. kfree(kbuf);
  298. return ret;
  299. }
  300. }
  301. kfree(kbuf);
  302. return total_retlen;
  303. } /* mtdchar_write */
  304. /*======================================================================
  305. IOCTL calls for getting device parameters.
  306. ======================================================================*/
  307. static void mtdchar_erase_callback (struct erase_info *instr)
  308. {
  309. wake_up((wait_queue_head_t *)instr->priv);
  310. }
  311. #ifdef CONFIG_HAVE_MTD_OTP
  312. static int otp_select_filemode(struct mtd_file_info *mfi, int mode)
  313. {
  314. struct mtd_info *mtd = mfi->mtd;
  315. size_t retlen;
  316. int ret = 0;
  317. /*
  318. * Make a fake call to mtd_read_fact_prot_reg() to check if OTP
  319. * operations are supported.
  320. */
  321. if (mtd_read_fact_prot_reg(mtd, -1, 0, &retlen, NULL) == -EOPNOTSUPP)
  322. return -EOPNOTSUPP;
  323. switch (mode) {
  324. case MTD_OTP_FACTORY:
  325. mfi->mode = MTD_FILE_MODE_OTP_FACTORY;
  326. break;
  327. case MTD_OTP_USER:
  328. mfi->mode = MTD_FILE_MODE_OTP_USER;
  329. break;
  330. default:
  331. ret = -EINVAL;
  332. case MTD_OTP_OFF:
  333. break;
  334. }
  335. return ret;
  336. }
  337. #else
  338. # define otp_select_filemode(f,m) -EOPNOTSUPP
  339. #endif
  340. static int mtdchar_writeoob(struct file *file, struct mtd_info *mtd,
  341. uint64_t start, uint32_t length, void __user *ptr,
  342. uint32_t __user *retp)
  343. {
  344. struct mtd_file_info *mfi = file->private_data;
  345. struct mtd_oob_ops ops;
  346. uint32_t retlen;
  347. int ret = 0;
  348. if (!(file->f_mode & FMODE_WRITE))
  349. return -EPERM;
  350. if (length > 4096)
  351. return -EINVAL;
  352. if (!mtd->_write_oob)
  353. ret = -EOPNOTSUPP;
  354. else
  355. ret = access_ok(VERIFY_READ, ptr, length) ? 0 : -EFAULT;
  356. if (ret)
  357. return ret;
  358. ops.ooblen = length;
  359. ops.ooboffs = start & (mtd->writesize - 1);
  360. ops.datbuf = NULL;
  361. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  362. MTD_OPS_PLACE_OOB;
  363. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  364. return -EINVAL;
  365. ops.oobbuf = memdup_user(ptr, length);
  366. if (IS_ERR(ops.oobbuf))
  367. return PTR_ERR(ops.oobbuf);
  368. start &= ~((uint64_t)mtd->writesize - 1);
  369. ret = mtd_write_oob(mtd, start, &ops);
  370. if (ops.oobretlen > 0xFFFFFFFFU)
  371. ret = -EOVERFLOW;
  372. retlen = ops.oobretlen;
  373. if (copy_to_user(retp, &retlen, sizeof(length)))
  374. ret = -EFAULT;
  375. kfree(ops.oobbuf);
  376. return ret;
  377. }
  378. static int mtdchar_readoob(struct file *file, struct mtd_info *mtd,
  379. uint64_t start, uint32_t length, void __user *ptr,
  380. uint32_t __user *retp)
  381. {
  382. struct mtd_file_info *mfi = file->private_data;
  383. struct mtd_oob_ops ops;
  384. int ret = 0;
  385. if (length > 4096)
  386. return -EINVAL;
  387. if (!access_ok(VERIFY_WRITE, ptr, length))
  388. return -EFAULT;
  389. ops.ooblen = length;
  390. ops.ooboffs = start & (mtd->writesize - 1);
  391. ops.datbuf = NULL;
  392. ops.mode = (mfi->mode == MTD_FILE_MODE_RAW) ? MTD_OPS_RAW :
  393. MTD_OPS_PLACE_OOB;
  394. if (ops.ooboffs && ops.ooblen > (mtd->oobsize - ops.ooboffs))
  395. return -EINVAL;
  396. ops.oobbuf = kmalloc(length, GFP_KERNEL);
  397. if (!ops.oobbuf)
  398. return -ENOMEM;
  399. start &= ~((uint64_t)mtd->writesize - 1);
  400. ret = mtd_read_oob(mtd, start, &ops);
  401. if (put_user(ops.oobretlen, retp))
  402. ret = -EFAULT;
  403. else if (ops.oobretlen && copy_to_user(ptr, ops.oobbuf,
  404. ops.oobretlen))
  405. ret = -EFAULT;
  406. kfree(ops.oobbuf);
  407. /*
  408. * NAND returns -EBADMSG on ECC errors, but it returns the OOB
  409. * data. For our userspace tools it is important to dump areas
  410. * with ECC errors!
  411. * For kernel internal usage it also might return -EUCLEAN
  412. * to signal the caller that a bitflip has occured and has
  413. * been corrected by the ECC algorithm.
  414. *
  415. * Note: currently the standard NAND function, nand_read_oob_std,
  416. * does not calculate ECC for the OOB area, so do not rely on
  417. * this behavior unless you have replaced it with your own.
  418. */
  419. if (mtd_is_bitflip_or_eccerr(ret))
  420. return 0;
  421. return ret;
  422. }
  423. /*
  424. * Copies (and truncates, if necessary) data from the larger struct,
  425. * nand_ecclayout, to the smaller, deprecated layout struct,
  426. * nand_ecclayout_user. This is necessary only to support the deprecated
  427. * API ioctl ECCGETLAYOUT while allowing all new functionality to use
  428. * nand_ecclayout flexibly (i.e. the struct may change size in new
  429. * releases without requiring major rewrites).
  430. */
  431. static int shrink_ecclayout(const struct nand_ecclayout *from,
  432. struct nand_ecclayout_user *to)
  433. {
  434. int i;
  435. if (!from || !to)
  436. return -EINVAL;
  437. memset(to, 0, sizeof(*to));
  438. to->eccbytes = min((int)from->eccbytes, MTD_MAX_ECCPOS_ENTRIES);
  439. for (i = 0; i < to->eccbytes; i++)
  440. to->eccpos[i] = from->eccpos[i];
  441. for (i = 0; i < MTD_MAX_OOBFREE_ENTRIES; i++) {
  442. if (from->oobfree[i].length == 0 &&
  443. from->oobfree[i].offset == 0)
  444. break;
  445. to->oobavail += from->oobfree[i].length;
  446. to->oobfree[i] = from->oobfree[i];
  447. }
  448. return 0;
  449. }
  450. static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
  451. struct blkpg_ioctl_arg __user *arg)
  452. {
  453. struct blkpg_ioctl_arg a;
  454. struct blkpg_partition p;
  455. if (!capable(CAP_SYS_ADMIN))
  456. return -EPERM;
  457. if (copy_from_user(&a, arg, sizeof(struct blkpg_ioctl_arg)))
  458. return -EFAULT;
  459. if (copy_from_user(&p, a.data, sizeof(struct blkpg_partition)))
  460. return -EFAULT;
  461. switch (a.op) {
  462. case BLKPG_ADD_PARTITION:
  463. /* Only master mtd device must be used to add partitions */
  464. if (mtd_is_partition(mtd))
  465. return -EINVAL;
  466. return mtd_add_partition(mtd, p.devname, p.start, p.length);
  467. case BLKPG_DEL_PARTITION:
  468. if (p.pno < 0)
  469. return -EINVAL;
  470. return mtd_del_partition(mtd, p.pno);
  471. default:
  472. return -EINVAL;
  473. }
  474. }
  475. static int mtdchar_write_ioctl(struct mtd_info *mtd,
  476. struct mtd_write_req __user *argp)
  477. {
  478. struct mtd_write_req req;
  479. struct mtd_oob_ops ops;
  480. void __user *usr_data, *usr_oob;
  481. int ret;
  482. if (copy_from_user(&req, argp, sizeof(req)) ||
  483. !access_ok(VERIFY_READ, req.usr_data, req.len) ||
  484. !access_ok(VERIFY_READ, req.usr_oob, req.ooblen))
  485. return -EFAULT;
  486. if (!mtd->_write_oob)
  487. return -EOPNOTSUPP;
  488. ops.mode = req.mode;
  489. ops.len = (size_t)req.len;
  490. ops.ooblen = (size_t)req.ooblen;
  491. ops.ooboffs = 0;
  492. usr_data = (void __user *)(uintptr_t)req.usr_data;
  493. usr_oob = (void __user *)(uintptr_t)req.usr_oob;
  494. if (req.usr_data) {
  495. ops.datbuf = memdup_user(usr_data, ops.len);
  496. if (IS_ERR(ops.datbuf))
  497. return PTR_ERR(ops.datbuf);
  498. } else {
  499. ops.datbuf = NULL;
  500. }
  501. if (req.usr_oob) {
  502. ops.oobbuf = memdup_user(usr_oob, ops.ooblen);
  503. if (IS_ERR(ops.oobbuf)) {
  504. kfree(ops.datbuf);
  505. return PTR_ERR(ops.oobbuf);
  506. }
  507. } else {
  508. ops.oobbuf = NULL;
  509. }
  510. ret = mtd_write_oob(mtd, (loff_t)req.start, &ops);
  511. kfree(ops.datbuf);
  512. kfree(ops.oobbuf);
  513. return ret;
  514. }
  515. static int mtdchar_ioctl(struct file *file, u_int cmd, u_long arg)
  516. {
  517. struct mtd_file_info *mfi = file->private_data;
  518. struct mtd_info *mtd = mfi->mtd;
  519. void __user *argp = (void __user *)arg;
  520. int ret = 0;
  521. u_long size;
  522. struct mtd_info_user info;
  523. pr_debug("MTD_ioctl\n");
  524. size = (cmd & IOCSIZE_MASK) >> IOCSIZE_SHIFT;
  525. if (cmd & IOC_IN) {
  526. if (!access_ok(VERIFY_READ, argp, size))
  527. return -EFAULT;
  528. }
  529. if (cmd & IOC_OUT) {
  530. if (!access_ok(VERIFY_WRITE, argp, size))
  531. return -EFAULT;
  532. }
  533. switch (cmd) {
  534. case MEMGETREGIONCOUNT:
  535. if (copy_to_user(argp, &(mtd->numeraseregions), sizeof(int)))
  536. return -EFAULT;
  537. break;
  538. case MEMGETREGIONINFO:
  539. {
  540. uint32_t ur_idx;
  541. struct mtd_erase_region_info *kr;
  542. struct region_info_user __user *ur = argp;
  543. if (get_user(ur_idx, &(ur->regionindex)))
  544. return -EFAULT;
  545. if (ur_idx >= mtd->numeraseregions)
  546. return -EINVAL;
  547. kr = &(mtd->eraseregions[ur_idx]);
  548. if (put_user(kr->offset, &(ur->offset))
  549. || put_user(kr->erasesize, &(ur->erasesize))
  550. || put_user(kr->numblocks, &(ur->numblocks)))
  551. return -EFAULT;
  552. break;
  553. }
  554. case MEMGETINFO:
  555. memset(&info, 0, sizeof(info));
  556. info.type = mtd->type;
  557. info.flags = mtd->flags;
  558. info.size = mtd->size;
  559. info.erasesize = mtd->erasesize;
  560. info.writesize = mtd->writesize;
  561. info.oobsize = mtd->oobsize;
  562. /* The below field is obsolete */
  563. info.padding = 0;
  564. if (copy_to_user(argp, &info, sizeof(struct mtd_info_user)))
  565. return -EFAULT;
  566. break;
  567. case MEMERASE:
  568. case MEMERASE64:
  569. {
  570. struct erase_info *erase;
  571. if(!(file->f_mode & FMODE_WRITE))
  572. return -EPERM;
  573. erase=kzalloc(sizeof(struct erase_info),GFP_KERNEL);
  574. if (!erase)
  575. ret = -ENOMEM;
  576. else {
  577. wait_queue_head_t waitq;
  578. DECLARE_WAITQUEUE(wait, current);
  579. init_waitqueue_head(&waitq);
  580. if (cmd == MEMERASE64) {
  581. struct erase_info_user64 einfo64;
  582. if (copy_from_user(&einfo64, argp,
  583. sizeof(struct erase_info_user64))) {
  584. kfree(erase);
  585. return -EFAULT;
  586. }
  587. erase->addr = einfo64.start;
  588. erase->len = einfo64.length;
  589. } else {
  590. struct erase_info_user einfo32;
  591. if (copy_from_user(&einfo32, argp,
  592. sizeof(struct erase_info_user))) {
  593. kfree(erase);
  594. return -EFAULT;
  595. }
  596. erase->addr = einfo32.start;
  597. erase->len = einfo32.length;
  598. }
  599. erase->mtd = mtd;
  600. erase->callback = mtdchar_erase_callback;
  601. erase->priv = (unsigned long)&waitq;
  602. /*
  603. FIXME: Allow INTERRUPTIBLE. Which means
  604. not having the wait_queue head on the stack.
  605. If the wq_head is on the stack, and we
  606. leave because we got interrupted, then the
  607. wq_head is no longer there when the
  608. callback routine tries to wake us up.
  609. */
  610. ret = mtd_erase(mtd, erase);
  611. if (!ret) {
  612. set_current_state(TASK_UNINTERRUPTIBLE);
  613. add_wait_queue(&waitq, &wait);
  614. if (erase->state != MTD_ERASE_DONE &&
  615. erase->state != MTD_ERASE_FAILED)
  616. schedule();
  617. remove_wait_queue(&waitq, &wait);
  618. set_current_state(TASK_RUNNING);
  619. ret = (erase->state == MTD_ERASE_FAILED)?-EIO:0;
  620. }
  621. kfree(erase);
  622. }
  623. break;
  624. }
  625. case MEMWRITEOOB:
  626. {
  627. struct mtd_oob_buf buf;
  628. struct mtd_oob_buf __user *buf_user = argp;
  629. /* NOTE: writes return length to buf_user->length */
  630. if (copy_from_user(&buf, argp, sizeof(buf)))
  631. ret = -EFAULT;
  632. else
  633. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  634. buf.ptr, &buf_user->length);
  635. break;
  636. }
  637. case MEMREADOOB:
  638. {
  639. struct mtd_oob_buf buf;
  640. struct mtd_oob_buf __user *buf_user = argp;
  641. /* NOTE: writes return length to buf_user->start */
  642. if (copy_from_user(&buf, argp, sizeof(buf)))
  643. ret = -EFAULT;
  644. else
  645. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  646. buf.ptr, &buf_user->start);
  647. break;
  648. }
  649. case MEMWRITEOOB64:
  650. {
  651. struct mtd_oob_buf64 buf;
  652. struct mtd_oob_buf64 __user *buf_user = argp;
  653. if (copy_from_user(&buf, argp, sizeof(buf)))
  654. ret = -EFAULT;
  655. else
  656. ret = mtdchar_writeoob(file, mtd, buf.start, buf.length,
  657. (void __user *)(uintptr_t)buf.usr_ptr,
  658. &buf_user->length);
  659. break;
  660. }
  661. case MEMREADOOB64:
  662. {
  663. struct mtd_oob_buf64 buf;
  664. struct mtd_oob_buf64 __user *buf_user = argp;
  665. if (copy_from_user(&buf, argp, sizeof(buf)))
  666. ret = -EFAULT;
  667. else
  668. ret = mtdchar_readoob(file, mtd, buf.start, buf.length,
  669. (void __user *)(uintptr_t)buf.usr_ptr,
  670. &buf_user->length);
  671. break;
  672. }
  673. case MEMWRITE:
  674. {
  675. ret = mtdchar_write_ioctl(mtd,
  676. (struct mtd_write_req __user *)arg);
  677. break;
  678. }
  679. case MEMLOCK:
  680. {
  681. struct erase_info_user einfo;
  682. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  683. return -EFAULT;
  684. ret = mtd_lock(mtd, einfo.start, einfo.length);
  685. break;
  686. }
  687. case MEMUNLOCK:
  688. {
  689. struct erase_info_user einfo;
  690. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  691. return -EFAULT;
  692. ret = mtd_unlock(mtd, einfo.start, einfo.length);
  693. break;
  694. }
  695. case MEMISLOCKED:
  696. {
  697. struct erase_info_user einfo;
  698. if (copy_from_user(&einfo, argp, sizeof(einfo)))
  699. return -EFAULT;
  700. ret = mtd_is_locked(mtd, einfo.start, einfo.length);
  701. break;
  702. }
  703. /* Legacy interface */
  704. case MEMGETOOBSEL:
  705. {
  706. struct nand_oobinfo oi;
  707. if (!mtd->ecclayout)
  708. return -EOPNOTSUPP;
  709. if (mtd->ecclayout->eccbytes > ARRAY_SIZE(oi.eccpos))
  710. return -EINVAL;
  711. oi.useecc = MTD_NANDECC_AUTOPLACE;
  712. memcpy(&oi.eccpos, mtd->ecclayout->eccpos, sizeof(oi.eccpos));
  713. memcpy(&oi.oobfree, mtd->ecclayout->oobfree,
  714. sizeof(oi.oobfree));
  715. oi.eccbytes = mtd->ecclayout->eccbytes;
  716. if (copy_to_user(argp, &oi, sizeof(struct nand_oobinfo)))
  717. return -EFAULT;
  718. break;
  719. }
  720. case MEMGETBADBLOCK:
  721. {
  722. loff_t offs;
  723. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  724. return -EFAULT;
  725. return mtd_block_isbad(mtd, offs);
  726. break;
  727. }
  728. case MEMSETBADBLOCK:
  729. {
  730. loff_t offs;
  731. if (copy_from_user(&offs, argp, sizeof(loff_t)))
  732. return -EFAULT;
  733. return mtd_block_markbad(mtd, offs);
  734. break;
  735. }
  736. #ifdef CONFIG_HAVE_MTD_OTP
  737. case OTPSELECT:
  738. {
  739. int mode;
  740. if (copy_from_user(&mode, argp, sizeof(int)))
  741. return -EFAULT;
  742. mfi->mode = MTD_FILE_MODE_NORMAL;
  743. ret = otp_select_filemode(mfi, mode);
  744. file->f_pos = 0;
  745. break;
  746. }
  747. case OTPGETREGIONCOUNT:
  748. case OTPGETREGIONINFO:
  749. {
  750. struct otp_info *buf = kmalloc(4096, GFP_KERNEL);
  751. if (!buf)
  752. return -ENOMEM;
  753. switch (mfi->mode) {
  754. case MTD_FILE_MODE_OTP_FACTORY:
  755. ret = mtd_get_fact_prot_info(mtd, buf, 4096);
  756. break;
  757. case MTD_FILE_MODE_OTP_USER:
  758. ret = mtd_get_user_prot_info(mtd, buf, 4096);
  759. break;
  760. default:
  761. ret = -EINVAL;
  762. break;
  763. }
  764. if (ret >= 0) {
  765. if (cmd == OTPGETREGIONCOUNT) {
  766. int nbr = ret / sizeof(struct otp_info);
  767. ret = copy_to_user(argp, &nbr, sizeof(int));
  768. } else
  769. ret = copy_to_user(argp, buf, ret);
  770. if (ret)
  771. ret = -EFAULT;
  772. }
  773. kfree(buf);
  774. break;
  775. }
  776. case OTPLOCK:
  777. {
  778. struct otp_info oinfo;
  779. if (mfi->mode != MTD_FILE_MODE_OTP_USER)
  780. return -EINVAL;
  781. if (copy_from_user(&oinfo, argp, sizeof(oinfo)))
  782. return -EFAULT;
  783. ret = mtd_lock_user_prot_reg(mtd, oinfo.start, oinfo.length);
  784. break;
  785. }
  786. #endif
  787. /* This ioctl is being deprecated - it truncates the ECC layout */
  788. case ECCGETLAYOUT:
  789. {
  790. struct nand_ecclayout_user *usrlay;
  791. if (!mtd->ecclayout)
  792. return -EOPNOTSUPP;
  793. usrlay = kmalloc(sizeof(*usrlay), GFP_KERNEL);
  794. if (!usrlay)
  795. return -ENOMEM;
  796. shrink_ecclayout(mtd->ecclayout, usrlay);
  797. if (copy_to_user(argp, usrlay, sizeof(*usrlay)))
  798. ret = -EFAULT;
  799. kfree(usrlay);
  800. break;
  801. }
  802. case ECCGETSTATS:
  803. {
  804. #ifdef CONFIG_MTD_LAZYECCSTATS
  805. part_fill_badblockstats(mtd);
  806. #endif
  807. if (copy_to_user(argp, &mtd->ecc_stats,
  808. sizeof(struct mtd_ecc_stats)))
  809. return -EFAULT;
  810. break;
  811. }
  812. case MTDFILEMODE:
  813. {
  814. mfi->mode = 0;
  815. switch(arg) {
  816. case MTD_FILE_MODE_OTP_FACTORY:
  817. case MTD_FILE_MODE_OTP_USER:
  818. ret = otp_select_filemode(mfi, arg);
  819. break;
  820. case MTD_FILE_MODE_RAW:
  821. if (!mtd_has_oob(mtd))
  822. return -EOPNOTSUPP;
  823. mfi->mode = arg;
  824. case MTD_FILE_MODE_NORMAL:
  825. break;
  826. default:
  827. ret = -EINVAL;
  828. }
  829. file->f_pos = 0;
  830. break;
  831. }
  832. case BLKPG:
  833. {
  834. ret = mtdchar_blkpg_ioctl(mtd,
  835. (struct blkpg_ioctl_arg __user *)arg);
  836. break;
  837. }
  838. case BLKRRPART:
  839. {
  840. /* No reread partition feature. Just return ok */
  841. ret = 0;
  842. break;
  843. }
  844. default:
  845. ret = -ENOTTY;
  846. }
  847. return ret;
  848. } /* memory_ioctl */
  849. static long mtdchar_unlocked_ioctl(struct file *file, u_int cmd, u_long arg)
  850. {
  851. int ret;
  852. mutex_lock(&mtd_mutex);
  853. ret = mtdchar_ioctl(file, cmd, arg);
  854. mutex_unlock(&mtd_mutex);
  855. return ret;
  856. }
  857. #ifdef CONFIG_COMPAT
  858. struct mtd_oob_buf32 {
  859. u_int32_t start;
  860. u_int32_t length;
  861. compat_caddr_t ptr; /* unsigned char* */
  862. };
  863. #define MEMWRITEOOB32 _IOWR('M', 3, struct mtd_oob_buf32)
  864. #define MEMREADOOB32 _IOWR('M', 4, struct mtd_oob_buf32)
  865. static long mtdchar_compat_ioctl(struct file *file, unsigned int cmd,
  866. unsigned long arg)
  867. {
  868. struct mtd_file_info *mfi = file->private_data;
  869. struct mtd_info *mtd = mfi->mtd;
  870. void __user *argp = compat_ptr(arg);
  871. int ret = 0;
  872. mutex_lock(&mtd_mutex);
  873. switch (cmd) {
  874. case MEMWRITEOOB32:
  875. {
  876. struct mtd_oob_buf32 buf;
  877. struct mtd_oob_buf32 __user *buf_user = argp;
  878. if (copy_from_user(&buf, argp, sizeof(buf)))
  879. ret = -EFAULT;
  880. else
  881. ret = mtdchar_writeoob(file, mtd, buf.start,
  882. buf.length, compat_ptr(buf.ptr),
  883. &buf_user->length);
  884. break;
  885. }
  886. case MEMREADOOB32:
  887. {
  888. struct mtd_oob_buf32 buf;
  889. struct mtd_oob_buf32 __user *buf_user = argp;
  890. /* NOTE: writes return length to buf->start */
  891. if (copy_from_user(&buf, argp, sizeof(buf)))
  892. ret = -EFAULT;
  893. else
  894. ret = mtdchar_readoob(file, mtd, buf.start,
  895. buf.length, compat_ptr(buf.ptr),
  896. &buf_user->start);
  897. break;
  898. }
  899. default:
  900. ret = mtdchar_ioctl(file, cmd, (unsigned long)argp);
  901. }
  902. mutex_unlock(&mtd_mutex);
  903. return ret;
  904. }
  905. #endif /* CONFIG_COMPAT */
  906. /*
  907. * try to determine where a shared mapping can be made
  908. * - only supported for NOMMU at the moment (MMU can't doesn't copy private
  909. * mappings)
  910. */
  911. #ifndef CONFIG_MMU
  912. static unsigned long mtdchar_get_unmapped_area(struct file *file,
  913. unsigned long addr,
  914. unsigned long len,
  915. unsigned long pgoff,
  916. unsigned long flags)
  917. {
  918. struct mtd_file_info *mfi = file->private_data;
  919. struct mtd_info *mtd = mfi->mtd;
  920. unsigned long offset;
  921. int ret;
  922. if (addr != 0)
  923. return (unsigned long) -EINVAL;
  924. if (len > mtd->size || pgoff >= (mtd->size >> PAGE_SHIFT))
  925. return (unsigned long) -EINVAL;
  926. offset = pgoff << PAGE_SHIFT;
  927. if (offset > mtd->size - len)
  928. return (unsigned long) -EINVAL;
  929. ret = mtd_get_unmapped_area(mtd, len, offset, flags);
  930. return ret == -EOPNOTSUPP ? -ENOSYS : ret;
  931. }
  932. #endif
  933. static inline unsigned long get_vm_size(struct vm_area_struct *vma)
  934. {
  935. return vma->vm_end - vma->vm_start;
  936. }
  937. static inline resource_size_t get_vm_offset(struct vm_area_struct *vma)
  938. {
  939. return (resource_size_t) vma->vm_pgoff << PAGE_SHIFT;
  940. }
  941. /*
  942. * Set a new vm offset.
  943. *
  944. * Verify that the incoming offset really works as a page offset,
  945. * and that the offset and size fit in a resource_size_t.
  946. */
  947. static inline int set_vm_offset(struct vm_area_struct *vma, resource_size_t off)
  948. {
  949. pgoff_t pgoff = off >> PAGE_SHIFT;
  950. if (off != (resource_size_t) pgoff << PAGE_SHIFT)
  951. return -EINVAL;
  952. if (off + get_vm_size(vma) - 1 < off)
  953. return -EINVAL;
  954. vma->vm_pgoff = pgoff;
  955. return 0;
  956. }
  957. /*
  958. * set up a mapping for shared memory segments
  959. */
  960. static int mtdchar_mmap(struct file *file, struct vm_area_struct *vma)
  961. {
  962. #ifdef CONFIG_MMU
  963. struct mtd_file_info *mfi = file->private_data;
  964. struct mtd_info *mtd = mfi->mtd;
  965. struct map_info *map = mtd->priv;
  966. /* This is broken because it assumes the MTD device is map-based
  967. and that mtd->priv is a valid struct map_info. It should be
  968. replaced with something that uses the mtd_get_unmapped_area()
  969. operation properly. */
  970. if (0 /*mtd->type == MTD_RAM || mtd->type == MTD_ROM*/) {
  971. #ifdef pgprot_noncached
  972. if (file->f_flags & O_DSYNC || map->phys >= __pa(high_memory))
  973. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  974. #endif
  975. return vm_iomap_memory(vma, map->phys, map->size);
  976. }
  977. return -ENOSYS;
  978. #else
  979. return vma->vm_flags & VM_SHARED ? 0 : -ENOSYS;
  980. #endif
  981. }
  982. static const struct file_operations mtd_fops = {
  983. .owner = THIS_MODULE,
  984. .llseek = mtdchar_lseek,
  985. .read = mtdchar_read,
  986. .write = mtdchar_write,
  987. .unlocked_ioctl = mtdchar_unlocked_ioctl,
  988. #ifdef CONFIG_COMPAT
  989. .compat_ioctl = mtdchar_compat_ioctl,
  990. #endif
  991. .open = mtdchar_open,
  992. .release = mtdchar_close,
  993. .mmap = mtdchar_mmap,
  994. #ifndef CONFIG_MMU
  995. .get_unmapped_area = mtdchar_get_unmapped_area,
  996. #endif
  997. };
  998. static const struct super_operations mtd_ops = {
  999. .drop_inode = generic_delete_inode,
  1000. .statfs = simple_statfs,
  1001. };
  1002. static struct dentry *mtd_inodefs_mount(struct file_system_type *fs_type,
  1003. int flags, const char *dev_name, void *data)
  1004. {
  1005. return mount_pseudo(fs_type, "mtd_inode:", &mtd_ops, NULL, MTD_INODE_FS_MAGIC);
  1006. }
  1007. static struct file_system_type mtd_inodefs_type = {
  1008. .name = "mtd_inodefs",
  1009. .mount = mtd_inodefs_mount,
  1010. .kill_sb = kill_anon_super,
  1011. };
  1012. MODULE_ALIAS_FS("mtd_inodefs");
  1013. static int __init init_mtdchar(void)
  1014. {
  1015. int ret;
  1016. ret = __register_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS,
  1017. "mtd", &mtd_fops);
  1018. if (ret < 0) {
  1019. pr_notice("Can't allocate major number %d for "
  1020. "Memory Technology Devices.\n", MTD_CHAR_MAJOR);
  1021. return ret;
  1022. }
  1023. ret = register_filesystem(&mtd_inodefs_type);
  1024. if (ret) {
  1025. pr_notice("Can't register mtd_inodefs filesystem: %d\n", ret);
  1026. goto err_unregister_chdev;
  1027. }
  1028. return ret;
  1029. err_unregister_chdev:
  1030. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1031. return ret;
  1032. }
  1033. static void __exit cleanup_mtdchar(void)
  1034. {
  1035. unregister_filesystem(&mtd_inodefs_type);
  1036. __unregister_chrdev(MTD_CHAR_MAJOR, 0, 1 << MINORBITS, "mtd");
  1037. }
  1038. module_init(init_mtdchar);
  1039. module_exit(cleanup_mtdchar);
  1040. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);
  1041. MODULE_LICENSE("GPL");
  1042. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  1043. MODULE_DESCRIPTION("Direct character-device access to MTD devices");
  1044. MODULE_ALIAS_CHARDEV_MAJOR(MTD_CHAR_MAJOR);