mtdpart.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * Simple MTD partitioning layer
  3. *
  4. * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
  5. * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
  6. * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #include <linux/module.h>
  24. #include <linux/types.h>
  25. #include <linux/kernel.h>
  26. #include <linux/slab.h>
  27. #include <linux/list.h>
  28. #include <linux/kmod.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/err.h>
  32. #include "mtdcore.h"
  33. /* Our partition linked list */
  34. static LIST_HEAD(mtd_partitions);
  35. static DEFINE_MUTEX(mtd_partitions_mutex);
  36. /* Our partition node structure */
  37. struct mtd_part {
  38. struct mtd_info mtd;
  39. struct mtd_info *master;
  40. uint64_t offset;
  41. struct list_head list;
  42. };
  43. /*
  44. * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  45. * the pointer to that structure.
  46. */
  47. static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
  48. {
  49. return container_of(mtd, struct mtd_part, mtd);
  50. }
  51. /*
  52. * MTD methods which simply translate the effective address and pass through
  53. * to the _real_ device.
  54. */
  55. static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
  56. size_t *retlen, u_char *buf)
  57. {
  58. struct mtd_part *part = mtd_to_part(mtd);
  59. struct mtd_ecc_stats stats;
  60. int res;
  61. stats = part->master->ecc_stats;
  62. res = part->master->_read(part->master, from + part->offset, len,
  63. retlen, buf);
  64. if (unlikely(mtd_is_eccerr(res)))
  65. mtd->ecc_stats.failed +=
  66. part->master->ecc_stats.failed - stats.failed;
  67. else
  68. mtd->ecc_stats.corrected +=
  69. part->master->ecc_stats.corrected - stats.corrected;
  70. return res;
  71. }
  72. static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
  73. size_t *retlen, void **virt, resource_size_t *phys)
  74. {
  75. struct mtd_part *part = mtd_to_part(mtd);
  76. return part->master->_point(part->master, from + part->offset, len,
  77. retlen, virt, phys);
  78. }
  79. static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  80. {
  81. struct mtd_part *part = mtd_to_part(mtd);
  82. return part->master->_unpoint(part->master, from + part->offset, len);
  83. }
  84. static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
  85. unsigned long len,
  86. unsigned long offset,
  87. unsigned long flags)
  88. {
  89. struct mtd_part *part = mtd_to_part(mtd);
  90. offset += part->offset;
  91. return part->master->_get_unmapped_area(part->master, len, offset,
  92. flags);
  93. }
  94. static int part_read_oob(struct mtd_info *mtd, loff_t from,
  95. struct mtd_oob_ops *ops)
  96. {
  97. struct mtd_part *part = mtd_to_part(mtd);
  98. int res;
  99. if (from >= mtd->size)
  100. return -EINVAL;
  101. if (ops->datbuf && from + ops->len > mtd->size)
  102. return -EINVAL;
  103. /*
  104. * If OOB is also requested, make sure that we do not read past the end
  105. * of this partition.
  106. */
  107. if (ops->oobbuf) {
  108. size_t len, pages;
  109. len = mtd_oobavail(mtd, ops);
  110. pages = mtd_div_by_ws(mtd->size, mtd);
  111. pages -= mtd_div_by_ws(from, mtd);
  112. if (ops->ooboffs + ops->ooblen > pages * len)
  113. return -EINVAL;
  114. }
  115. res = part->master->_read_oob(part->master, from + part->offset, ops);
  116. if (unlikely(res)) {
  117. if (mtd_is_bitflip(res))
  118. mtd->ecc_stats.corrected++;
  119. if (mtd_is_eccerr(res))
  120. mtd->ecc_stats.failed++;
  121. }
  122. return res;
  123. }
  124. static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  125. size_t len, size_t *retlen, u_char *buf)
  126. {
  127. struct mtd_part *part = mtd_to_part(mtd);
  128. return part->master->_read_user_prot_reg(part->master, from, len,
  129. retlen, buf);
  130. }
  131. static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
  132. size_t *retlen, struct otp_info *buf)
  133. {
  134. struct mtd_part *part = mtd_to_part(mtd);
  135. return part->master->_get_user_prot_info(part->master, len, retlen,
  136. buf);
  137. }
  138. static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  139. size_t len, size_t *retlen, u_char *buf)
  140. {
  141. struct mtd_part *part = mtd_to_part(mtd);
  142. return part->master->_read_fact_prot_reg(part->master, from, len,
  143. retlen, buf);
  144. }
  145. static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  146. size_t *retlen, struct otp_info *buf)
  147. {
  148. struct mtd_part *part = mtd_to_part(mtd);
  149. return part->master->_get_fact_prot_info(part->master, len, retlen,
  150. buf);
  151. }
  152. static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
  153. size_t *retlen, const u_char *buf)
  154. {
  155. struct mtd_part *part = mtd_to_part(mtd);
  156. return part->master->_write(part->master, to + part->offset, len,
  157. retlen, buf);
  158. }
  159. static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  160. size_t *retlen, const u_char *buf)
  161. {
  162. struct mtd_part *part = mtd_to_part(mtd);
  163. return part->master->_panic_write(part->master, to + part->offset, len,
  164. retlen, buf);
  165. }
  166. static int part_write_oob(struct mtd_info *mtd, loff_t to,
  167. struct mtd_oob_ops *ops)
  168. {
  169. struct mtd_part *part = mtd_to_part(mtd);
  170. if (to >= mtd->size)
  171. return -EINVAL;
  172. if (ops->datbuf && to + ops->len > mtd->size)
  173. return -EINVAL;
  174. return part->master->_write_oob(part->master, to + part->offset, ops);
  175. }
  176. static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  177. size_t len, size_t *retlen, u_char *buf)
  178. {
  179. struct mtd_part *part = mtd_to_part(mtd);
  180. return part->master->_write_user_prot_reg(part->master, from, len,
  181. retlen, buf);
  182. }
  183. static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  184. size_t len)
  185. {
  186. struct mtd_part *part = mtd_to_part(mtd);
  187. return part->master->_lock_user_prot_reg(part->master, from, len);
  188. }
  189. static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
  190. unsigned long count, loff_t to, size_t *retlen)
  191. {
  192. struct mtd_part *part = mtd_to_part(mtd);
  193. return part->master->_writev(part->master, vecs, count,
  194. to + part->offset, retlen);
  195. }
  196. static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
  197. {
  198. struct mtd_part *part = mtd_to_part(mtd);
  199. int ret;
  200. instr->addr += part->offset;
  201. ret = part->master->_erase(part->master, instr);
  202. if (ret) {
  203. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  204. instr->fail_addr -= part->offset;
  205. instr->addr -= part->offset;
  206. }
  207. return ret;
  208. }
  209. void mtd_erase_callback(struct erase_info *instr)
  210. {
  211. if (instr->mtd->_erase == part_erase) {
  212. struct mtd_part *part = mtd_to_part(instr->mtd);
  213. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  214. instr->fail_addr -= part->offset;
  215. instr->addr -= part->offset;
  216. }
  217. if (instr->callback)
  218. instr->callback(instr);
  219. }
  220. EXPORT_SYMBOL_GPL(mtd_erase_callback);
  221. static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  222. {
  223. struct mtd_part *part = mtd_to_part(mtd);
  224. return part->master->_lock(part->master, ofs + part->offset, len);
  225. }
  226. static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  227. {
  228. struct mtd_part *part = mtd_to_part(mtd);
  229. return part->master->_unlock(part->master, ofs + part->offset, len);
  230. }
  231. static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  232. {
  233. struct mtd_part *part = mtd_to_part(mtd);
  234. return part->master->_is_locked(part->master, ofs + part->offset, len);
  235. }
  236. static void part_sync(struct mtd_info *mtd)
  237. {
  238. struct mtd_part *part = mtd_to_part(mtd);
  239. part->master->_sync(part->master);
  240. }
  241. static int part_suspend(struct mtd_info *mtd)
  242. {
  243. struct mtd_part *part = mtd_to_part(mtd);
  244. return part->master->_suspend(part->master);
  245. }
  246. static void part_resume(struct mtd_info *mtd)
  247. {
  248. struct mtd_part *part = mtd_to_part(mtd);
  249. part->master->_resume(part->master);
  250. }
  251. static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  252. {
  253. struct mtd_part *part = mtd_to_part(mtd);
  254. ofs += part->offset;
  255. return part->master->_block_isreserved(part->master, ofs);
  256. }
  257. static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
  258. {
  259. struct mtd_part *part = mtd_to_part(mtd);
  260. ofs += part->offset;
  261. return part->master->_block_isbad(part->master, ofs);
  262. }
  263. static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
  264. {
  265. struct mtd_part *part = mtd_to_part(mtd);
  266. int res;
  267. ofs += part->offset;
  268. res = part->master->_block_markbad(part->master, ofs);
  269. if (!res)
  270. mtd->ecc_stats.badblocks++;
  271. return res;
  272. }
  273. static int part_get_device(struct mtd_info *mtd)
  274. {
  275. struct mtd_part *part = mtd_to_part(mtd);
  276. return part->master->_get_device(part->master);
  277. }
  278. static void part_put_device(struct mtd_info *mtd)
  279. {
  280. struct mtd_part *part = mtd_to_part(mtd);
  281. part->master->_put_device(part->master);
  282. }
  283. static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
  284. struct mtd_oob_region *oobregion)
  285. {
  286. struct mtd_part *part = mtd_to_part(mtd);
  287. return mtd_ooblayout_ecc(part->master, section, oobregion);
  288. }
  289. static int part_ooblayout_free(struct mtd_info *mtd, int section,
  290. struct mtd_oob_region *oobregion)
  291. {
  292. struct mtd_part *part = mtd_to_part(mtd);
  293. return mtd_ooblayout_free(part->master, section, oobregion);
  294. }
  295. static const struct mtd_ooblayout_ops part_ooblayout_ops = {
  296. .ecc = part_ooblayout_ecc,
  297. .free = part_ooblayout_free,
  298. };
  299. static inline void free_partition(struct mtd_part *p)
  300. {
  301. kfree(p->mtd.name);
  302. kfree(p);
  303. }
  304. /*
  305. * This function unregisters and destroy all slave MTD objects which are
  306. * attached to the given master MTD object.
  307. */
  308. int del_mtd_partitions(struct mtd_info *master)
  309. {
  310. struct mtd_part *slave, *next;
  311. int ret, err = 0;
  312. mutex_lock(&mtd_partitions_mutex);
  313. list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  314. if (slave->master == master) {
  315. ret = del_mtd_device(&slave->mtd);
  316. if (ret < 0) {
  317. err = ret;
  318. continue;
  319. }
  320. list_del(&slave->list);
  321. free_partition(slave);
  322. }
  323. mutex_unlock(&mtd_partitions_mutex);
  324. return err;
  325. }
  326. static struct mtd_part *allocate_partition(struct mtd_info *master,
  327. const struct mtd_partition *part, int partno,
  328. uint64_t cur_offset)
  329. {
  330. struct mtd_part *slave;
  331. char *name;
  332. /* allocate the partition structure */
  333. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  334. name = kstrdup(part->name, GFP_KERNEL);
  335. if (!name || !slave) {
  336. printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
  337. master->name);
  338. kfree(name);
  339. kfree(slave);
  340. return ERR_PTR(-ENOMEM);
  341. }
  342. /* set up the MTD object for this partition */
  343. slave->mtd.type = master->type;
  344. slave->mtd.flags = master->flags & ~part->mask_flags;
  345. slave->mtd.size = part->size;
  346. slave->mtd.writesize = master->writesize;
  347. slave->mtd.writebufsize = master->writebufsize;
  348. slave->mtd.oobsize = master->oobsize;
  349. slave->mtd.oobavail = master->oobavail;
  350. slave->mtd.subpage_sft = master->subpage_sft;
  351. slave->mtd.pairing = master->pairing;
  352. slave->mtd.name = name;
  353. slave->mtd.owner = master->owner;
  354. /* NOTE: Historically, we didn't arrange MTDs as a tree out of
  355. * concern for showing the same data in multiple partitions.
  356. * However, it is very useful to have the master node present,
  357. * so the MTD_PARTITIONED_MASTER option allows that. The master
  358. * will have device nodes etc only if this is set, so make the
  359. * parent conditional on that option. Note, this is a way to
  360. * distinguish between the master and the partition in sysfs.
  361. */
  362. slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
  363. &master->dev :
  364. master->dev.parent;
  365. slave->mtd._read = part_read;
  366. slave->mtd._write = part_write;
  367. if (master->_panic_write)
  368. slave->mtd._panic_write = part_panic_write;
  369. if (master->_point && master->_unpoint) {
  370. slave->mtd._point = part_point;
  371. slave->mtd._unpoint = part_unpoint;
  372. }
  373. if (master->_get_unmapped_area)
  374. slave->mtd._get_unmapped_area = part_get_unmapped_area;
  375. if (master->_read_oob)
  376. slave->mtd._read_oob = part_read_oob;
  377. if (master->_write_oob)
  378. slave->mtd._write_oob = part_write_oob;
  379. if (master->_read_user_prot_reg)
  380. slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
  381. if (master->_read_fact_prot_reg)
  382. slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
  383. if (master->_write_user_prot_reg)
  384. slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
  385. if (master->_lock_user_prot_reg)
  386. slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
  387. if (master->_get_user_prot_info)
  388. slave->mtd._get_user_prot_info = part_get_user_prot_info;
  389. if (master->_get_fact_prot_info)
  390. slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
  391. if (master->_sync)
  392. slave->mtd._sync = part_sync;
  393. if (!partno && !master->dev.class && master->_suspend &&
  394. master->_resume) {
  395. slave->mtd._suspend = part_suspend;
  396. slave->mtd._resume = part_resume;
  397. }
  398. if (master->_writev)
  399. slave->mtd._writev = part_writev;
  400. if (master->_lock)
  401. slave->mtd._lock = part_lock;
  402. if (master->_unlock)
  403. slave->mtd._unlock = part_unlock;
  404. if (master->_is_locked)
  405. slave->mtd._is_locked = part_is_locked;
  406. if (master->_block_isreserved)
  407. slave->mtd._block_isreserved = part_block_isreserved;
  408. if (master->_block_isbad)
  409. slave->mtd._block_isbad = part_block_isbad;
  410. if (master->_block_markbad)
  411. slave->mtd._block_markbad = part_block_markbad;
  412. if (master->_get_device)
  413. slave->mtd._get_device = part_get_device;
  414. if (master->_put_device)
  415. slave->mtd._put_device = part_put_device;
  416. slave->mtd._erase = part_erase;
  417. slave->master = master;
  418. slave->offset = part->offset;
  419. if (slave->offset == MTDPART_OFS_APPEND)
  420. slave->offset = cur_offset;
  421. if (slave->offset == MTDPART_OFS_NXTBLK) {
  422. slave->offset = cur_offset;
  423. if (mtd_mod_by_eb(cur_offset, master) != 0) {
  424. /* Round up to next erasesize */
  425. slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
  426. printk(KERN_NOTICE "Moving partition %d: "
  427. "0x%012llx -> 0x%012llx\n", partno,
  428. (unsigned long long)cur_offset, (unsigned long long)slave->offset);
  429. }
  430. }
  431. if (slave->offset == MTDPART_OFS_RETAIN) {
  432. slave->offset = cur_offset;
  433. if (master->size - slave->offset >= slave->mtd.size) {
  434. slave->mtd.size = master->size - slave->offset
  435. - slave->mtd.size;
  436. } else {
  437. printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
  438. part->name, master->size - slave->offset,
  439. slave->mtd.size);
  440. /* register to preserve ordering */
  441. goto out_register;
  442. }
  443. }
  444. if (slave->mtd.size == MTDPART_SIZ_FULL)
  445. slave->mtd.size = master->size - slave->offset;
  446. printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
  447. (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
  448. /* let's do some sanity checks */
  449. if (slave->offset >= master->size) {
  450. /* let's register it anyway to preserve ordering */
  451. slave->offset = 0;
  452. slave->mtd.size = 0;
  453. printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
  454. part->name);
  455. goto out_register;
  456. }
  457. if (slave->offset + slave->mtd.size > master->size) {
  458. slave->mtd.size = master->size - slave->offset;
  459. printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
  460. part->name, master->name, (unsigned long long)slave->mtd.size);
  461. }
  462. if (master->numeraseregions > 1) {
  463. /* Deal with variable erase size stuff */
  464. int i, max = master->numeraseregions;
  465. u64 end = slave->offset + slave->mtd.size;
  466. struct mtd_erase_region_info *regions = master->eraseregions;
  467. /* Find the first erase regions which is part of this
  468. * partition. */
  469. for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
  470. ;
  471. /* The loop searched for the region _behind_ the first one */
  472. if (i > 0)
  473. i--;
  474. /* Pick biggest erasesize */
  475. for (; i < max && regions[i].offset < end; i++) {
  476. if (slave->mtd.erasesize < regions[i].erasesize) {
  477. slave->mtd.erasesize = regions[i].erasesize;
  478. }
  479. }
  480. BUG_ON(slave->mtd.erasesize == 0);
  481. } else {
  482. /* Single erase size */
  483. slave->mtd.erasesize = master->erasesize;
  484. }
  485. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  486. mtd_mod_by_eb(slave->offset, &slave->mtd)) {
  487. /* Doesn't start on a boundary of major erase size */
  488. /* FIXME: Let it be writable if it is on a boundary of
  489. * _minor_ erase size though */
  490. slave->mtd.flags &= ~MTD_WRITEABLE;
  491. printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
  492. part->name);
  493. }
  494. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  495. mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
  496. slave->mtd.flags &= ~MTD_WRITEABLE;
  497. printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
  498. part->name);
  499. }
  500. mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
  501. slave->mtd.ecc_step_size = master->ecc_step_size;
  502. slave->mtd.ecc_strength = master->ecc_strength;
  503. slave->mtd.bitflip_threshold = master->bitflip_threshold;
  504. if (master->_block_isbad) {
  505. uint64_t offs = 0;
  506. while (offs < slave->mtd.size) {
  507. if (mtd_block_isreserved(master, offs + slave->offset))
  508. slave->mtd.ecc_stats.bbtblocks++;
  509. else if (mtd_block_isbad(master, offs + slave->offset))
  510. slave->mtd.ecc_stats.badblocks++;
  511. offs += slave->mtd.erasesize;
  512. }
  513. }
  514. out_register:
  515. return slave;
  516. }
  517. static ssize_t mtd_partition_offset_show(struct device *dev,
  518. struct device_attribute *attr, char *buf)
  519. {
  520. struct mtd_info *mtd = dev_get_drvdata(dev);
  521. struct mtd_part *part = mtd_to_part(mtd);
  522. return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
  523. }
  524. static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
  525. static const struct attribute *mtd_partition_attrs[] = {
  526. &dev_attr_offset.attr,
  527. NULL
  528. };
  529. static int mtd_add_partition_attrs(struct mtd_part *new)
  530. {
  531. int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
  532. if (ret)
  533. printk(KERN_WARNING
  534. "mtd: failed to create partition attrs, err=%d\n", ret);
  535. return ret;
  536. }
  537. int mtd_add_partition(struct mtd_info *master, const char *name,
  538. long long offset, long long length)
  539. {
  540. struct mtd_partition part;
  541. struct mtd_part *new;
  542. int ret = 0;
  543. /* the direct offset is expected */
  544. if (offset == MTDPART_OFS_APPEND ||
  545. offset == MTDPART_OFS_NXTBLK)
  546. return -EINVAL;
  547. if (length == MTDPART_SIZ_FULL)
  548. length = master->size - offset;
  549. if (length <= 0)
  550. return -EINVAL;
  551. memset(&part, 0, sizeof(part));
  552. part.name = name;
  553. part.size = length;
  554. part.offset = offset;
  555. new = allocate_partition(master, &part, -1, offset);
  556. if (IS_ERR(new))
  557. return PTR_ERR(new);
  558. mutex_lock(&mtd_partitions_mutex);
  559. list_add(&new->list, &mtd_partitions);
  560. mutex_unlock(&mtd_partitions_mutex);
  561. add_mtd_device(&new->mtd);
  562. mtd_add_partition_attrs(new);
  563. return ret;
  564. }
  565. EXPORT_SYMBOL_GPL(mtd_add_partition);
  566. int mtd_del_partition(struct mtd_info *master, int partno)
  567. {
  568. struct mtd_part *slave, *next;
  569. int ret = -EINVAL;
  570. mutex_lock(&mtd_partitions_mutex);
  571. list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  572. if ((slave->master == master) &&
  573. (slave->mtd.index == partno)) {
  574. sysfs_remove_files(&slave->mtd.dev.kobj,
  575. mtd_partition_attrs);
  576. ret = del_mtd_device(&slave->mtd);
  577. if (ret < 0)
  578. break;
  579. list_del(&slave->list);
  580. free_partition(slave);
  581. break;
  582. }
  583. mutex_unlock(&mtd_partitions_mutex);
  584. return ret;
  585. }
  586. EXPORT_SYMBOL_GPL(mtd_del_partition);
  587. /*
  588. * This function, given a master MTD object and a partition table, creates
  589. * and registers slave MTD objects which are bound to the master according to
  590. * the partition definitions.
  591. *
  592. * For historical reasons, this function's caller only registers the master
  593. * if the MTD_PARTITIONED_MASTER config option is set.
  594. */
  595. int add_mtd_partitions(struct mtd_info *master,
  596. const struct mtd_partition *parts,
  597. int nbparts)
  598. {
  599. struct mtd_part *slave;
  600. uint64_t cur_offset = 0;
  601. int i;
  602. printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
  603. for (i = 0; i < nbparts; i++) {
  604. slave = allocate_partition(master, parts + i, i, cur_offset);
  605. if (IS_ERR(slave)) {
  606. del_mtd_partitions(master);
  607. return PTR_ERR(slave);
  608. }
  609. mutex_lock(&mtd_partitions_mutex);
  610. list_add(&slave->list, &mtd_partitions);
  611. mutex_unlock(&mtd_partitions_mutex);
  612. add_mtd_device(&slave->mtd);
  613. mtd_add_partition_attrs(slave);
  614. cur_offset = slave->offset + slave->mtd.size;
  615. }
  616. return 0;
  617. }
  618. static DEFINE_SPINLOCK(part_parser_lock);
  619. static LIST_HEAD(part_parsers);
  620. static struct mtd_part_parser *mtd_part_parser_get(const char *name)
  621. {
  622. struct mtd_part_parser *p, *ret = NULL;
  623. spin_lock(&part_parser_lock);
  624. list_for_each_entry(p, &part_parsers, list)
  625. if (!strcmp(p->name, name) && try_module_get(p->owner)) {
  626. ret = p;
  627. break;
  628. }
  629. spin_unlock(&part_parser_lock);
  630. return ret;
  631. }
  632. static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
  633. {
  634. module_put(p->owner);
  635. }
  636. /*
  637. * Many partition parsers just expected the core to kfree() all their data in
  638. * one chunk. Do that by default.
  639. */
  640. static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
  641. int nr_parts)
  642. {
  643. kfree(pparts);
  644. }
  645. int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
  646. {
  647. p->owner = owner;
  648. if (!p->cleanup)
  649. p->cleanup = &mtd_part_parser_cleanup_default;
  650. spin_lock(&part_parser_lock);
  651. list_add(&p->list, &part_parsers);
  652. spin_unlock(&part_parser_lock);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(__register_mtd_parser);
  656. void deregister_mtd_parser(struct mtd_part_parser *p)
  657. {
  658. spin_lock(&part_parser_lock);
  659. list_del(&p->list);
  660. spin_unlock(&part_parser_lock);
  661. }
  662. EXPORT_SYMBOL_GPL(deregister_mtd_parser);
  663. /*
  664. * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
  665. * are changing this array!
  666. */
  667. static const char * const default_mtd_part_types[] = {
  668. "cmdlinepart",
  669. "ofpart",
  670. NULL
  671. };
  672. /**
  673. * parse_mtd_partitions - parse MTD partitions
  674. * @master: the master partition (describes whole MTD device)
  675. * @types: names of partition parsers to try or %NULL
  676. * @pparts: info about partitions found is returned here
  677. * @data: MTD partition parser-specific data
  678. *
  679. * This function tries to find partition on MTD device @master. It uses MTD
  680. * partition parsers, specified in @types. However, if @types is %NULL, then
  681. * the default list of parsers is used. The default list contains only the
  682. * "cmdlinepart" and "ofpart" parsers ATM.
  683. * Note: If there are more then one parser in @types, the kernel only takes the
  684. * partitions parsed out by the first parser.
  685. *
  686. * This function may return:
  687. * o a negative error code in case of failure
  688. * o zero otherwise, and @pparts will describe the partitions, number of
  689. * partitions, and the parser which parsed them. Caller must release
  690. * resources with mtd_part_parser_cleanup() when finished with the returned
  691. * data.
  692. */
  693. int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
  694. struct mtd_partitions *pparts,
  695. struct mtd_part_parser_data *data)
  696. {
  697. struct mtd_part_parser *parser;
  698. int ret, err = 0;
  699. if (!types)
  700. types = default_mtd_part_types;
  701. for ( ; *types; types++) {
  702. pr_debug("%s: parsing partitions %s\n", master->name, *types);
  703. parser = mtd_part_parser_get(*types);
  704. if (!parser && !request_module("%s", *types))
  705. parser = mtd_part_parser_get(*types);
  706. pr_debug("%s: got parser %s\n", master->name,
  707. parser ? parser->name : NULL);
  708. if (!parser)
  709. continue;
  710. ret = (*parser->parse_fn)(master, &pparts->parts, data);
  711. pr_debug("%s: parser %s: %i\n",
  712. master->name, parser->name, ret);
  713. if (ret > 0) {
  714. printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
  715. ret, parser->name, master->name);
  716. pparts->nr_parts = ret;
  717. pparts->parser = parser;
  718. return 0;
  719. }
  720. mtd_part_parser_put(parser);
  721. /*
  722. * Stash the first error we see; only report it if no parser
  723. * succeeds
  724. */
  725. if (ret < 0 && !err)
  726. err = ret;
  727. }
  728. return err;
  729. }
  730. void mtd_part_parser_cleanup(struct mtd_partitions *parts)
  731. {
  732. const struct mtd_part_parser *parser;
  733. if (!parts)
  734. return;
  735. parser = parts->parser;
  736. if (parser) {
  737. if (parser->cleanup)
  738. parser->cleanup(parts->parts, parts->nr_parts);
  739. mtd_part_parser_put(parser);
  740. }
  741. }
  742. int mtd_is_partition(const struct mtd_info *mtd)
  743. {
  744. struct mtd_part *part;
  745. int ispart = 0;
  746. mutex_lock(&mtd_partitions_mutex);
  747. list_for_each_entry(part, &mtd_partitions, list)
  748. if (&part->mtd == mtd) {
  749. ispart = 1;
  750. break;
  751. }
  752. mutex_unlock(&mtd_partitions_mutex);
  753. return ispart;
  754. }
  755. EXPORT_SYMBOL_GPL(mtd_is_partition);
  756. /* Returns the size of the entire flash chip */
  757. uint64_t mtd_get_device_size(const struct mtd_info *mtd)
  758. {
  759. if (!mtd_is_partition(mtd))
  760. return mtd->size;
  761. return mtd_to_part(mtd)->master->size;
  762. }
  763. EXPORT_SYMBOL_GPL(mtd_get_device_size);