mtdpart.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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 with this macro.
  46. */
  47. #define PART(x) ((struct mtd_part *)(x))
  48. /*
  49. * MTD methods which simply translate the effective address and pass through
  50. * to the _real_ device.
  51. */
  52. static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
  53. size_t *retlen, u_char *buf)
  54. {
  55. struct mtd_part *part = PART(mtd);
  56. struct mtd_ecc_stats stats;
  57. int res;
  58. stats = part->master->ecc_stats;
  59. res = part->master->_read(part->master, from + part->offset, len,
  60. retlen, buf);
  61. if (unlikely(res)) {
  62. if (mtd_is_bitflip(res))
  63. mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
  64. if (mtd_is_eccerr(res))
  65. mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
  66. }
  67. return res;
  68. }
  69. static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
  70. size_t *retlen, void **virt, resource_size_t *phys)
  71. {
  72. struct mtd_part *part = PART(mtd);
  73. return part->master->_point(part->master, from + part->offset, len,
  74. retlen, virt, phys);
  75. }
  76. static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  77. {
  78. struct mtd_part *part = PART(mtd);
  79. return part->master->_unpoint(part->master, from + part->offset, len);
  80. }
  81. static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
  82. unsigned long len,
  83. unsigned long offset,
  84. unsigned long flags)
  85. {
  86. struct mtd_part *part = PART(mtd);
  87. offset += part->offset;
  88. return part->master->_get_unmapped_area(part->master, len, offset,
  89. flags);
  90. }
  91. static int part_read_oob(struct mtd_info *mtd, loff_t from,
  92. struct mtd_oob_ops *ops)
  93. {
  94. struct mtd_part *part = PART(mtd);
  95. int res;
  96. if (from >= mtd->size)
  97. return -EINVAL;
  98. if (ops->datbuf && from + ops->len > mtd->size)
  99. return -EINVAL;
  100. /*
  101. * If OOB is also requested, make sure that we do not read past the end
  102. * of this partition.
  103. */
  104. if (ops->oobbuf) {
  105. size_t len, pages;
  106. if (ops->mode == MTD_OPS_AUTO_OOB)
  107. len = mtd->oobavail;
  108. else
  109. len = mtd->oobsize;
  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 = 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,
  132. struct otp_info *buf, size_t len)
  133. {
  134. struct mtd_part *part = PART(mtd);
  135. return part->master->_get_user_prot_info(part->master, buf, len);
  136. }
  137. static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  138. size_t len, size_t *retlen, u_char *buf)
  139. {
  140. struct mtd_part *part = PART(mtd);
  141. return part->master->_read_fact_prot_reg(part->master, from, len,
  142. retlen, buf);
  143. }
  144. static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
  145. size_t len)
  146. {
  147. struct mtd_part *part = PART(mtd);
  148. return part->master->_get_fact_prot_info(part->master, buf, len);
  149. }
  150. static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
  151. size_t *retlen, const u_char *buf)
  152. {
  153. struct mtd_part *part = PART(mtd);
  154. return part->master->_write(part->master, to + part->offset, len,
  155. retlen, buf);
  156. }
  157. static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  158. size_t *retlen, const u_char *buf)
  159. {
  160. struct mtd_part *part = PART(mtd);
  161. return part->master->_panic_write(part->master, to + part->offset, len,
  162. retlen, buf);
  163. }
  164. static int part_write_oob(struct mtd_info *mtd, loff_t to,
  165. struct mtd_oob_ops *ops)
  166. {
  167. struct mtd_part *part = PART(mtd);
  168. if (to >= mtd->size)
  169. return -EINVAL;
  170. if (ops->datbuf && to + ops->len > mtd->size)
  171. return -EINVAL;
  172. return part->master->_write_oob(part->master, to + part->offset, ops);
  173. }
  174. static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  175. size_t len, size_t *retlen, u_char *buf)
  176. {
  177. struct mtd_part *part = PART(mtd);
  178. return part->master->_write_user_prot_reg(part->master, from, len,
  179. retlen, buf);
  180. }
  181. static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  182. size_t len)
  183. {
  184. struct mtd_part *part = PART(mtd);
  185. return part->master->_lock_user_prot_reg(part->master, from, len);
  186. }
  187. static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
  188. unsigned long count, loff_t to, size_t *retlen)
  189. {
  190. struct mtd_part *part = PART(mtd);
  191. return part->master->_writev(part->master, vecs, count,
  192. to + part->offset, retlen);
  193. }
  194. static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
  195. {
  196. struct mtd_part *part = PART(mtd);
  197. int ret;
  198. instr->addr += part->offset;
  199. ret = part->master->_erase(part->master, instr);
  200. if (ret) {
  201. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  202. instr->fail_addr -= part->offset;
  203. instr->addr -= part->offset;
  204. }
  205. return ret;
  206. }
  207. void mtd_erase_callback(struct erase_info *instr)
  208. {
  209. if (instr->mtd->_erase == part_erase) {
  210. struct mtd_part *part = PART(instr->mtd);
  211. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  212. instr->fail_addr -= part->offset;
  213. instr->addr -= part->offset;
  214. }
  215. if (instr->callback)
  216. instr->callback(instr);
  217. }
  218. EXPORT_SYMBOL_GPL(mtd_erase_callback);
  219. static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  220. {
  221. struct mtd_part *part = PART(mtd);
  222. return part->master->_lock(part->master, ofs + part->offset, len);
  223. }
  224. static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  225. {
  226. struct mtd_part *part = PART(mtd);
  227. return part->master->_unlock(part->master, ofs + part->offset, len);
  228. }
  229. static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  230. {
  231. struct mtd_part *part = PART(mtd);
  232. return part->master->_is_locked(part->master, ofs + part->offset, len);
  233. }
  234. static void part_sync(struct mtd_info *mtd)
  235. {
  236. struct mtd_part *part = PART(mtd);
  237. part->master->_sync(part->master);
  238. }
  239. static int part_suspend(struct mtd_info *mtd)
  240. {
  241. struct mtd_part *part = PART(mtd);
  242. return part->master->_suspend(part->master);
  243. }
  244. static void part_resume(struct mtd_info *mtd)
  245. {
  246. struct mtd_part *part = PART(mtd);
  247. part->master->_resume(part->master);
  248. }
  249. static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
  250. {
  251. struct mtd_part *part = PART(mtd);
  252. ofs += part->offset;
  253. return part->master->_block_isbad(part->master, ofs);
  254. }
  255. static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
  256. {
  257. struct mtd_part *part = PART(mtd);
  258. int res;
  259. ofs += part->offset;
  260. res = part->master->_block_markbad(part->master, ofs);
  261. if (!res)
  262. mtd->ecc_stats.badblocks++;
  263. return res;
  264. }
  265. static inline void free_partition(struct mtd_part *p)
  266. {
  267. kfree(p->mtd.name);
  268. kfree(p);
  269. }
  270. void part_fill_badblockstats(struct mtd_info *mtd)
  271. {
  272. struct mtd_part *part = PART(mtd);
  273. if (part->master->_block_isbad) {
  274. uint64_t offs = 0;
  275. mtd->ecc_stats.badblocks = 0;
  276. while (offs < mtd->size) {
  277. if (mtd_block_isbad(part->master,
  278. offs + part->offset))
  279. mtd->ecc_stats.badblocks++;
  280. offs += mtd->erasesize;
  281. }
  282. }
  283. }
  284. /*
  285. * This function unregisters and destroy all slave MTD objects which are
  286. * attached to the given master MTD object.
  287. */
  288. int del_mtd_partitions(struct mtd_info *master)
  289. {
  290. struct mtd_part *slave, *next;
  291. int ret, err = 0;
  292. mutex_lock(&mtd_partitions_mutex);
  293. list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  294. if (slave->master == master) {
  295. ret = del_mtd_device(&slave->mtd);
  296. if (ret < 0) {
  297. err = ret;
  298. continue;
  299. }
  300. list_del(&slave->list);
  301. free_partition(slave);
  302. }
  303. mutex_unlock(&mtd_partitions_mutex);
  304. return err;
  305. }
  306. static struct mtd_part *allocate_partition(struct mtd_info *master,
  307. const struct mtd_partition *part, int partno,
  308. uint64_t cur_offset)
  309. {
  310. struct mtd_part *slave;
  311. char *name;
  312. /* allocate the partition structure */
  313. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  314. name = kstrdup(part->name, GFP_KERNEL);
  315. if (!name || !slave) {
  316. printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
  317. master->name);
  318. kfree(name);
  319. kfree(slave);
  320. return ERR_PTR(-ENOMEM);
  321. }
  322. /* set up the MTD object for this partition */
  323. slave->mtd.type = master->type;
  324. slave->mtd.flags = master->flags & ~part->mask_flags;
  325. slave->mtd.size = part->size;
  326. slave->mtd.writesize = master->writesize;
  327. slave->mtd.writebufsize = master->writebufsize;
  328. slave->mtd.oobsize = master->oobsize;
  329. slave->mtd.oobavail = master->oobavail;
  330. slave->mtd.subpage_sft = master->subpage_sft;
  331. slave->mtd.name = name;
  332. slave->mtd.owner = master->owner;
  333. slave->mtd.backing_dev_info = master->backing_dev_info;
  334. /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
  335. * to have the same data be in two different partitions.
  336. */
  337. slave->mtd.dev.parent = master->dev.parent;
  338. slave->mtd._read = part_read;
  339. slave->mtd._write = part_write;
  340. if (master->_panic_write)
  341. slave->mtd._panic_write = part_panic_write;
  342. if (master->_point && master->_unpoint) {
  343. slave->mtd._point = part_point;
  344. slave->mtd._unpoint = part_unpoint;
  345. }
  346. if (master->_get_unmapped_area)
  347. slave->mtd._get_unmapped_area = part_get_unmapped_area;
  348. if (master->_read_oob)
  349. slave->mtd._read_oob = part_read_oob;
  350. if (master->_write_oob)
  351. slave->mtd._write_oob = part_write_oob;
  352. if (master->_read_user_prot_reg)
  353. slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
  354. if (master->_read_fact_prot_reg)
  355. slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
  356. if (master->_write_user_prot_reg)
  357. slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
  358. if (master->_lock_user_prot_reg)
  359. slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
  360. if (master->_get_user_prot_info)
  361. slave->mtd._get_user_prot_info = part_get_user_prot_info;
  362. if (master->_get_fact_prot_info)
  363. slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
  364. if (master->_sync)
  365. slave->mtd._sync = part_sync;
  366. if (!partno && !master->dev.class && master->_suspend &&
  367. master->_resume) {
  368. slave->mtd._suspend = part_suspend;
  369. slave->mtd._resume = part_resume;
  370. }
  371. if (master->_writev)
  372. slave->mtd._writev = part_writev;
  373. if (master->_lock)
  374. slave->mtd._lock = part_lock;
  375. if (master->_unlock)
  376. slave->mtd._unlock = part_unlock;
  377. if (master->_is_locked)
  378. slave->mtd._is_locked = part_is_locked;
  379. if (master->_block_isbad)
  380. slave->mtd._block_isbad = part_block_isbad;
  381. if (master->_block_markbad)
  382. slave->mtd._block_markbad = part_block_markbad;
  383. slave->mtd._erase = part_erase;
  384. slave->master = master;
  385. slave->offset = part->offset;
  386. if (slave->offset == MTDPART_OFS_APPEND)
  387. slave->offset = cur_offset;
  388. if (slave->offset == MTDPART_OFS_NXTBLK) {
  389. slave->offset = cur_offset;
  390. if (mtd_mod_by_eb(cur_offset, master) != 0) {
  391. /* Round up to next erasesize */
  392. slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
  393. printk(KERN_NOTICE "Moving partition %d: "
  394. "0x%012llx -> 0x%012llx\n", partno,
  395. (unsigned long long)cur_offset, (unsigned long long)slave->offset);
  396. }
  397. }
  398. if (slave->offset == MTDPART_OFS_RETAIN) {
  399. slave->offset = cur_offset;
  400. if (master->size - slave->offset >= slave->mtd.size) {
  401. slave->mtd.size = master->size - slave->offset
  402. - slave->mtd.size;
  403. } else {
  404. printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
  405. part->name, master->size - slave->offset,
  406. slave->mtd.size);
  407. /* register to preserve ordering */
  408. goto out_register;
  409. }
  410. }
  411. if (slave->mtd.size == MTDPART_SIZ_FULL)
  412. slave->mtd.size = master->size - slave->offset;
  413. printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
  414. (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
  415. /* let's do some sanity checks */
  416. if (slave->offset >= master->size) {
  417. /* let's register it anyway to preserve ordering */
  418. slave->offset = 0;
  419. slave->mtd.size = 0;
  420. printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
  421. part->name);
  422. goto out_register;
  423. }
  424. if (slave->offset + slave->mtd.size > master->size) {
  425. slave->mtd.size = master->size - slave->offset;
  426. printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
  427. part->name, master->name, (unsigned long long)slave->mtd.size);
  428. }
  429. if (master->numeraseregions > 1) {
  430. /* Deal with variable erase size stuff */
  431. int i, max = master->numeraseregions;
  432. u64 end = slave->offset + slave->mtd.size;
  433. struct mtd_erase_region_info *regions = master->eraseregions;
  434. /* Find the first erase regions which is part of this
  435. * partition. */
  436. for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
  437. ;
  438. /* The loop searched for the region _behind_ the first one */
  439. if (i > 0)
  440. i--;
  441. /* Pick biggest erasesize */
  442. for (; i < max && regions[i].offset < end; i++) {
  443. if (slave->mtd.erasesize < regions[i].erasesize) {
  444. slave->mtd.erasesize = regions[i].erasesize;
  445. }
  446. }
  447. BUG_ON(slave->mtd.erasesize == 0);
  448. } else {
  449. /* Single erase size */
  450. slave->mtd.erasesize = master->erasesize;
  451. }
  452. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  453. mtd_mod_by_eb(slave->offset, &slave->mtd)) {
  454. /* Doesn't start on a boundary of major erase size */
  455. /* FIXME: Let it be writable if it is on a boundary of
  456. * _minor_ erase size though */
  457. slave->mtd.flags &= ~MTD_WRITEABLE;
  458. printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
  459. part->name);
  460. }
  461. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  462. mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
  463. slave->mtd.flags &= ~MTD_WRITEABLE;
  464. printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
  465. part->name);
  466. }
  467. slave->mtd.ecclayout = master->ecclayout;
  468. slave->mtd.ecc_strength = master->ecc_strength;
  469. #ifndef CONFIG_MTD_LAZYECCSTATS
  470. part_fill_badblockstats(&(slave->mtd));
  471. #endif
  472. out_register:
  473. return slave;
  474. }
  475. int mtd_add_partition(struct mtd_info *master, char *name,
  476. long long offset, long long length)
  477. {
  478. struct mtd_partition part;
  479. struct mtd_part *p, *new;
  480. uint64_t start, end;
  481. int ret = 0;
  482. /* the direct offset is expected */
  483. if (offset == MTDPART_OFS_APPEND ||
  484. offset == MTDPART_OFS_NXTBLK)
  485. return -EINVAL;
  486. if (length == MTDPART_SIZ_FULL)
  487. length = master->size - offset;
  488. if (length <= 0)
  489. return -EINVAL;
  490. part.name = name;
  491. part.size = length;
  492. part.offset = offset;
  493. part.mask_flags = 0;
  494. part.ecclayout = NULL;
  495. new = allocate_partition(master, &part, -1, offset);
  496. if (IS_ERR(new))
  497. return PTR_ERR(new);
  498. start = offset;
  499. end = offset + length;
  500. mutex_lock(&mtd_partitions_mutex);
  501. list_for_each_entry(p, &mtd_partitions, list)
  502. if (p->master == master) {
  503. if ((start >= p->offset) &&
  504. (start < (p->offset + p->mtd.size)))
  505. goto err_inv;
  506. if ((end >= p->offset) &&
  507. (end < (p->offset + p->mtd.size)))
  508. goto err_inv;
  509. }
  510. list_add(&new->list, &mtd_partitions);
  511. mutex_unlock(&mtd_partitions_mutex);
  512. add_mtd_device(&new->mtd);
  513. return ret;
  514. err_inv:
  515. mutex_unlock(&mtd_partitions_mutex);
  516. free_partition(new);
  517. return -EINVAL;
  518. }
  519. EXPORT_SYMBOL_GPL(mtd_add_partition);
  520. int mtd_del_partition(struct mtd_info *master, int partno)
  521. {
  522. struct mtd_part *slave, *next;
  523. int ret = -EINVAL;
  524. mutex_lock(&mtd_partitions_mutex);
  525. list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  526. if ((slave->master == master) &&
  527. (slave->mtd.index == partno)) {
  528. ret = del_mtd_device(&slave->mtd);
  529. if (ret < 0)
  530. break;
  531. list_del(&slave->list);
  532. free_partition(slave);
  533. break;
  534. }
  535. mutex_unlock(&mtd_partitions_mutex);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL_GPL(mtd_del_partition);
  539. /*
  540. * This function, given a master MTD object and a partition table, creates
  541. * and registers slave MTD objects which are bound to the master according to
  542. * the partition definitions.
  543. *
  544. * We don't register the master, or expect the caller to have done so,
  545. * for reasons of data integrity.
  546. */
  547. int add_mtd_partitions(struct mtd_info *master,
  548. const struct mtd_partition *parts,
  549. int nbparts)
  550. {
  551. struct mtd_part *slave;
  552. uint64_t cur_offset = 0;
  553. int i;
  554. printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
  555. for (i = 0; i < nbparts; i++) {
  556. slave = allocate_partition(master, parts + i, i, cur_offset);
  557. if (IS_ERR(slave)) {
  558. del_mtd_partitions(master);
  559. return PTR_ERR(slave);
  560. }
  561. mutex_lock(&mtd_partitions_mutex);
  562. list_add(&slave->list, &mtd_partitions);
  563. mutex_unlock(&mtd_partitions_mutex);
  564. add_mtd_device(&slave->mtd);
  565. cur_offset = slave->offset + slave->mtd.size;
  566. }
  567. return 0;
  568. }
  569. static DEFINE_SPINLOCK(part_parser_lock);
  570. static LIST_HEAD(part_parsers);
  571. static struct mtd_part_parser *get_partition_parser(const char *name)
  572. {
  573. struct mtd_part_parser *p, *ret = NULL;
  574. spin_lock(&part_parser_lock);
  575. list_for_each_entry(p, &part_parsers, list)
  576. if (!strcmp(p->name, name) && try_module_get(p->owner)) {
  577. ret = p;
  578. break;
  579. }
  580. spin_unlock(&part_parser_lock);
  581. return ret;
  582. }
  583. #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
  584. int register_mtd_parser(struct mtd_part_parser *p)
  585. {
  586. spin_lock(&part_parser_lock);
  587. list_add(&p->list, &part_parsers);
  588. spin_unlock(&part_parser_lock);
  589. return 0;
  590. }
  591. EXPORT_SYMBOL_GPL(register_mtd_parser);
  592. int deregister_mtd_parser(struct mtd_part_parser *p)
  593. {
  594. spin_lock(&part_parser_lock);
  595. list_del(&p->list);
  596. spin_unlock(&part_parser_lock);
  597. return 0;
  598. }
  599. EXPORT_SYMBOL_GPL(deregister_mtd_parser);
  600. /*
  601. * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
  602. * are changing this array!
  603. */
  604. static const char *default_mtd_part_types[] = {
  605. "cmdlinepart",
  606. "ofpart",
  607. NULL
  608. };
  609. /**
  610. * parse_mtd_partitions - parse MTD partitions
  611. * @master: the master partition (describes whole MTD device)
  612. * @types: names of partition parsers to try or %NULL
  613. * @pparts: array of partitions found is returned here
  614. * @data: MTD partition parser-specific data
  615. *
  616. * This function tries to find partition on MTD device @master. It uses MTD
  617. * partition parsers, specified in @types. However, if @types is %NULL, then
  618. * the default list of parsers is used. The default list contains only the
  619. * "cmdlinepart" and "ofpart" parsers ATM.
  620. * Note: If there are more then one parser in @types, the kernel only takes the
  621. * partitions parsed out by the first parser.
  622. *
  623. * This function may return:
  624. * o a negative error code in case of failure
  625. * o zero if no partitions were found
  626. * o a positive number of found partitions, in which case on exit @pparts will
  627. * point to an array containing this number of &struct mtd_info objects.
  628. */
  629. int parse_mtd_partitions(struct mtd_info *master, const char **types,
  630. struct mtd_partition **pparts,
  631. struct mtd_part_parser_data *data)
  632. {
  633. struct mtd_part_parser *parser;
  634. int ret = 0;
  635. if (!types)
  636. types = default_mtd_part_types;
  637. for ( ; ret <= 0 && *types; types++) {
  638. parser = get_partition_parser(*types);
  639. if (!parser && !request_module("%s", *types))
  640. parser = get_partition_parser(*types);
  641. if (!parser)
  642. continue;
  643. ret = (*parser->parse_fn)(master, pparts, data);
  644. put_partition_parser(parser);
  645. if (ret > 0) {
  646. printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
  647. ret, parser->name, master->name);
  648. break;
  649. }
  650. }
  651. return ret;
  652. }
  653. int mtd_is_partition(struct mtd_info *mtd)
  654. {
  655. struct mtd_part *part;
  656. int ispart = 0;
  657. mutex_lock(&mtd_partitions_mutex);
  658. list_for_each_entry(part, &mtd_partitions, list)
  659. if (&part->mtd == mtd) {
  660. ispart = 1;
  661. break;
  662. }
  663. mutex_unlock(&mtd_partitions_mutex);
  664. return ispart;
  665. }
  666. EXPORT_SYMBOL_GPL(mtd_is_partition);