mtdchar.c 26 KB

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