mtdpart.c 28 KB

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