mtdpart.c 22 KB

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