mtdpart.c 27 KB

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