mtdpart.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Simple MTD partitioning layer
  4. *
  5. * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
  6. * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
  7. * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
  8. *
  9. */
  10. #ifndef __UBOOT__
  11. #include <log.h>
  12. #include <dm/devres.h>
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/kernel.h>
  16. #include <linux/slab.h>
  17. #include <linux/list.h>
  18. #include <linux/kmod.h>
  19. #endif
  20. #include <common.h>
  21. #include <malloc.h>
  22. #include <linux/bug.h>
  23. #include <linux/errno.h>
  24. #include <linux/compat.h>
  25. #include <ubi_uboot.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/mtd/partitions.h>
  28. #include <linux/err.h>
  29. #include <linux/sizes.h>
  30. #include "mtdcore.h"
  31. #ifndef __UBOOT__
  32. static DEFINE_MUTEX(mtd_partitions_mutex);
  33. #else
  34. DEFINE_MUTEX(mtd_partitions_mutex);
  35. #endif
  36. #ifdef __UBOOT__
  37. /* from mm/util.c */
  38. /**
  39. * kstrdup - allocate space for and copy an existing string
  40. * @s: the string to duplicate
  41. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  42. */
  43. char *kstrdup(const char *s, gfp_t gfp)
  44. {
  45. size_t len;
  46. char *buf;
  47. if (!s)
  48. return NULL;
  49. len = strlen(s) + 1;
  50. buf = kmalloc(len, gfp);
  51. if (buf)
  52. memcpy(buf, s, len);
  53. return buf;
  54. }
  55. #endif
  56. #define MTD_SIZE_REMAINING (~0LLU)
  57. #define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
  58. bool mtd_partitions_used(struct mtd_info *master)
  59. {
  60. struct mtd_info *slave;
  61. list_for_each_entry(slave, &master->partitions, node) {
  62. if (slave->usecount)
  63. return true;
  64. }
  65. return false;
  66. }
  67. /**
  68. * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
  69. * with it and update the @mtdparts string pointer.
  70. *
  71. * The partition name is allocated and must be freed by the caller.
  72. *
  73. * This function is widely inspired from part_parse (mtdparts.c).
  74. *
  75. * @mtdparts: String describing the partition with mtdparts command syntax
  76. * @partition: MTD partition structure to fill
  77. *
  78. * @return 0 on success, an error otherwise.
  79. */
  80. static int mtd_parse_partition(const char **_mtdparts,
  81. struct mtd_partition *partition)
  82. {
  83. const char *mtdparts = *_mtdparts;
  84. const char *name = NULL;
  85. int name_len;
  86. char *buf;
  87. /* Ensure the partition structure is empty */
  88. memset(partition, 0, sizeof(struct mtd_partition));
  89. /* Fetch the partition size */
  90. if (*mtdparts == '-') {
  91. /* Assign all remaining space to this partition */
  92. partition->size = MTD_SIZE_REMAINING;
  93. mtdparts++;
  94. } else {
  95. partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
  96. if (partition->size < SZ_4K) {
  97. printf("Minimum partition size 4kiB, %lldB requested\n",
  98. partition->size);
  99. return -EINVAL;
  100. }
  101. }
  102. /* Check for the offset */
  103. partition->offset = MTD_OFFSET_NOT_SPECIFIED;
  104. if (*mtdparts == '@') {
  105. mtdparts++;
  106. partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
  107. }
  108. /* Now look for the name */
  109. if (*mtdparts == '(') {
  110. name = ++mtdparts;
  111. mtdparts = strchr(name, ')');
  112. if (!mtdparts) {
  113. printf("No closing ')' found in partition name\n");
  114. return -EINVAL;
  115. }
  116. name_len = mtdparts - name + 1;
  117. if ((name_len - 1) == 0) {
  118. printf("Empty partition name\n");
  119. return -EINVAL;
  120. }
  121. mtdparts++;
  122. } else {
  123. /* Name will be of the form size@offset */
  124. name_len = 22;
  125. }
  126. /* Check if the partition is read-only */
  127. if (strncmp(mtdparts, "ro", 2) == 0) {
  128. partition->mask_flags |= MTD_WRITEABLE;
  129. mtdparts += 2;
  130. }
  131. /* Check for a potential next partition definition */
  132. if (*mtdparts == ',') {
  133. if (partition->size == MTD_SIZE_REMAINING) {
  134. printf("No partitions allowed after a fill-up\n");
  135. return -EINVAL;
  136. }
  137. ++mtdparts;
  138. } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
  139. /* NOP */
  140. } else {
  141. printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
  142. return -EINVAL;
  143. }
  144. /*
  145. * Allocate a buffer for the name and either copy the provided name or
  146. * auto-generate it with the form 'size@offset'.
  147. */
  148. buf = malloc(name_len);
  149. if (!buf)
  150. return -ENOMEM;
  151. if (name)
  152. strncpy(buf, name, name_len - 1);
  153. else
  154. snprintf(buf, name_len, "0x%08llx@0x%08llx",
  155. partition->size, partition->offset);
  156. buf[name_len - 1] = '\0';
  157. partition->name = buf;
  158. *_mtdparts = mtdparts;
  159. return 0;
  160. }
  161. /**
  162. * mtd_parse_partitions - Create a partition array from an mtdparts definition
  163. *
  164. * Stateless function that takes a @parent MTD device, a string @_mtdparts
  165. * describing the partitions (with the "mtdparts" command syntax) and creates
  166. * the corresponding MTD partition structure array @_parts. Both the name and
  167. * the structure partition itself must be freed freed, the caller may use
  168. * @mtd_free_parsed_partitions() for this purpose.
  169. *
  170. * @parent: MTD device which contains the partitions
  171. * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
  172. * command syntax.
  173. * @_parts: Allocated array containing the partitions, must be freed by the
  174. * caller.
  175. * @_nparts: Size of @_parts array.
  176. *
  177. * @return 0 on success, an error otherwise.
  178. */
  179. int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
  180. struct mtd_partition **_parts, int *_nparts)
  181. {
  182. struct mtd_partition partition = {}, *parts;
  183. const char *mtdparts = *_mtdparts;
  184. int cur_off = 0, cur_sz = 0;
  185. int nparts = 0;
  186. int ret, idx;
  187. u64 sz;
  188. /* First, iterate over the partitions until we know their number */
  189. while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
  190. ret = mtd_parse_partition(&mtdparts, &partition);
  191. if (ret)
  192. return ret;
  193. free((char *)partition.name);
  194. nparts++;
  195. }
  196. /* Allocate an array of partitions to give back to the caller */
  197. parts = malloc(sizeof(*parts) * nparts);
  198. if (!parts) {
  199. printf("Not enough space to save partitions meta-data\n");
  200. return -ENOMEM;
  201. }
  202. /* Iterate again over each partition to save the data in our array */
  203. for (idx = 0; idx < nparts; idx++) {
  204. ret = mtd_parse_partition(_mtdparts, &parts[idx]);
  205. if (ret)
  206. return ret;
  207. if (parts[idx].size == MTD_SIZE_REMAINING)
  208. parts[idx].size = parent->size - cur_sz;
  209. cur_sz += parts[idx].size;
  210. sz = parts[idx].size;
  211. if (sz < parent->writesize || do_div(sz, parent->writesize)) {
  212. printf("Partition size must be a multiple of %d\n",
  213. parent->writesize);
  214. return -EINVAL;
  215. }
  216. if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
  217. parts[idx].offset = cur_off;
  218. cur_off += parts[idx].size;
  219. parts[idx].ecclayout = parent->ecclayout;
  220. }
  221. /* Offset by one mtdparts to point to the next device if any */
  222. if (*_mtdparts[0] == ';')
  223. (*_mtdparts)++;
  224. *_parts = parts;
  225. *_nparts = nparts;
  226. return 0;
  227. }
  228. /**
  229. * mtd_free_parsed_partitions - Free dynamically allocated partitions
  230. *
  231. * Each successful call to @mtd_parse_partitions must be followed by a call to
  232. * @mtd_free_parsed_partitions to free any allocated array during the parsing
  233. * process.
  234. *
  235. * @parts: Array containing the partitions that will be freed.
  236. * @nparts: Size of @parts array.
  237. */
  238. void mtd_free_parsed_partitions(struct mtd_partition *parts,
  239. unsigned int nparts)
  240. {
  241. int i;
  242. for (i = 0; i < nparts; i++)
  243. free((char *)parts[i].name);
  244. free(parts);
  245. }
  246. /*
  247. * MTD methods which simply translate the effective address and pass through
  248. * to the _real_ device.
  249. */
  250. static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
  251. size_t *retlen, u_char *buf)
  252. {
  253. struct mtd_ecc_stats stats;
  254. int res;
  255. stats = mtd->parent->ecc_stats;
  256. res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
  257. retlen, buf);
  258. if (unlikely(mtd_is_eccerr(res)))
  259. mtd->ecc_stats.failed +=
  260. mtd->parent->ecc_stats.failed - stats.failed;
  261. else
  262. mtd->ecc_stats.corrected +=
  263. mtd->parent->ecc_stats.corrected - stats.corrected;
  264. return res;
  265. }
  266. #ifndef __UBOOT__
  267. static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
  268. size_t *retlen, void **virt, resource_size_t *phys)
  269. {
  270. return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
  271. retlen, virt, phys);
  272. }
  273. static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  274. {
  275. return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
  276. }
  277. #endif
  278. static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
  279. unsigned long len,
  280. unsigned long offset,
  281. unsigned long flags)
  282. {
  283. offset += mtd->offset;
  284. return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
  285. }
  286. static int part_read_oob(struct mtd_info *mtd, loff_t from,
  287. struct mtd_oob_ops *ops)
  288. {
  289. int res;
  290. if (from >= mtd->size)
  291. return -EINVAL;
  292. if (ops->datbuf && from + ops->len > mtd->size)
  293. return -EINVAL;
  294. /*
  295. * If OOB is also requested, make sure that we do not read past the end
  296. * of this partition.
  297. */
  298. if (ops->oobbuf) {
  299. size_t len, pages;
  300. if (ops->mode == MTD_OPS_AUTO_OOB)
  301. len = mtd->oobavail;
  302. else
  303. len = mtd->oobsize;
  304. pages = mtd_div_by_ws(mtd->size, mtd);
  305. pages -= mtd_div_by_ws(from, mtd);
  306. if (ops->ooboffs + ops->ooblen > pages * len)
  307. return -EINVAL;
  308. }
  309. res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
  310. if (unlikely(res)) {
  311. if (mtd_is_bitflip(res))
  312. mtd->ecc_stats.corrected++;
  313. if (mtd_is_eccerr(res))
  314. mtd->ecc_stats.failed++;
  315. }
  316. return res;
  317. }
  318. static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  319. size_t len, size_t *retlen, u_char *buf)
  320. {
  321. return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
  322. retlen, buf);
  323. }
  324. static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
  325. size_t *retlen, struct otp_info *buf)
  326. {
  327. return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
  328. buf);
  329. }
  330. static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  331. size_t len, size_t *retlen, u_char *buf)
  332. {
  333. return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
  334. retlen, buf);
  335. }
  336. static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
  337. size_t *retlen, struct otp_info *buf)
  338. {
  339. return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
  340. buf);
  341. }
  342. static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
  343. size_t *retlen, const u_char *buf)
  344. {
  345. return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
  346. retlen, buf);
  347. }
  348. static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  349. size_t *retlen, const u_char *buf)
  350. {
  351. return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
  352. retlen, buf);
  353. }
  354. static int part_write_oob(struct mtd_info *mtd, loff_t to,
  355. struct mtd_oob_ops *ops)
  356. {
  357. if (to >= mtd->size)
  358. return -EINVAL;
  359. if (ops->datbuf && to + ops->len > mtd->size)
  360. return -EINVAL;
  361. return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
  362. }
  363. static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  364. size_t len, size_t *retlen, u_char *buf)
  365. {
  366. return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
  367. retlen, buf);
  368. }
  369. static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  370. size_t len)
  371. {
  372. return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
  373. }
  374. #ifndef __UBOOT__
  375. static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
  376. unsigned long count, loff_t to, size_t *retlen)
  377. {
  378. return mtd->parent->_writev(mtd->parent, vecs, count,
  379. to + mtd->offset, retlen);
  380. }
  381. #endif
  382. static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
  383. {
  384. int ret;
  385. instr->addr += mtd->offset;
  386. ret = mtd->parent->_erase(mtd->parent, instr);
  387. if (ret) {
  388. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  389. instr->fail_addr -= mtd->offset;
  390. instr->addr -= mtd->offset;
  391. }
  392. return ret;
  393. }
  394. void mtd_erase_callback(struct erase_info *instr)
  395. {
  396. if (instr->mtd->_erase == part_erase) {
  397. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  398. instr->fail_addr -= instr->mtd->offset;
  399. instr->addr -= instr->mtd->offset;
  400. }
  401. if (instr->callback)
  402. instr->callback(instr);
  403. }
  404. EXPORT_SYMBOL_GPL(mtd_erase_callback);
  405. static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  406. {
  407. return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
  408. }
  409. static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  410. {
  411. return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
  412. }
  413. static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  414. {
  415. return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
  416. }
  417. static void part_sync(struct mtd_info *mtd)
  418. {
  419. mtd->parent->_sync(mtd->parent);
  420. }
  421. #ifndef __UBOOT__
  422. static int part_suspend(struct mtd_info *mtd)
  423. {
  424. return mtd->parent->_suspend(mtd->parent);
  425. }
  426. static void part_resume(struct mtd_info *mtd)
  427. {
  428. mtd->parent->_resume(mtd->parent);
  429. }
  430. #endif
  431. static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
  432. {
  433. ofs += mtd->offset;
  434. return mtd->parent->_block_isreserved(mtd->parent, ofs);
  435. }
  436. static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
  437. {
  438. ofs += mtd->offset;
  439. return mtd->parent->_block_isbad(mtd->parent, ofs);
  440. }
  441. static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
  442. {
  443. int res;
  444. ofs += mtd->offset;
  445. res = mtd->parent->_block_markbad(mtd->parent, ofs);
  446. if (!res)
  447. mtd->ecc_stats.badblocks++;
  448. return res;
  449. }
  450. static inline void free_partition(struct mtd_info *p)
  451. {
  452. kfree(p->name);
  453. kfree(p);
  454. }
  455. /*
  456. * This function unregisters and destroy all slave MTD objects which are
  457. * attached to the given master MTD object, recursively.
  458. */
  459. static int do_del_mtd_partitions(struct mtd_info *master)
  460. {
  461. struct mtd_info *slave, *next;
  462. int ret, err = 0;
  463. list_for_each_entry_safe(slave, next, &master->partitions, node) {
  464. if (mtd_has_partitions(slave))
  465. del_mtd_partitions(slave);
  466. debug("Deleting %s MTD partition\n", slave->name);
  467. ret = del_mtd_device(slave);
  468. if (ret < 0) {
  469. printf("Error when deleting partition \"%s\" (%d)\n",
  470. slave->name, ret);
  471. err = ret;
  472. continue;
  473. }
  474. list_del(&slave->node);
  475. free_partition(slave);
  476. }
  477. return err;
  478. }
  479. int del_mtd_partitions(struct mtd_info *master)
  480. {
  481. int ret;
  482. debug("Deleting MTD partitions on \"%s\":\n", master->name);
  483. mutex_lock(&mtd_partitions_mutex);
  484. ret = do_del_mtd_partitions(master);
  485. mutex_unlock(&mtd_partitions_mutex);
  486. return ret;
  487. }
  488. static struct mtd_info *allocate_partition(struct mtd_info *master,
  489. const struct mtd_partition *part,
  490. int partno, uint64_t cur_offset)
  491. {
  492. struct mtd_info *slave;
  493. char *name;
  494. /* allocate the partition structure */
  495. slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  496. name = kstrdup(part->name, GFP_KERNEL);
  497. if (!name || !slave) {
  498. printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
  499. master->name);
  500. kfree(name);
  501. kfree(slave);
  502. return ERR_PTR(-ENOMEM);
  503. }
  504. /* set up the MTD object for this partition */
  505. slave->type = master->type;
  506. slave->flags = master->flags & ~part->mask_flags;
  507. slave->size = part->size;
  508. slave->writesize = master->writesize;
  509. slave->writebufsize = master->writebufsize;
  510. slave->oobsize = master->oobsize;
  511. slave->oobavail = master->oobavail;
  512. slave->subpage_sft = master->subpage_sft;
  513. slave->name = name;
  514. slave->owner = master->owner;
  515. #ifndef __UBOOT__
  516. slave->backing_dev_info = master->backing_dev_info;
  517. /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
  518. * to have the same data be in two different partitions.
  519. */
  520. slave->dev.parent = master->dev.parent;
  521. #endif
  522. if (master->_read)
  523. slave->_read = part_read;
  524. if (master->_write)
  525. slave->_write = part_write;
  526. if (master->_panic_write)
  527. slave->_panic_write = part_panic_write;
  528. #ifndef __UBOOT__
  529. if (master->_point && master->_unpoint) {
  530. slave->_point = part_point;
  531. slave->_unpoint = part_unpoint;
  532. }
  533. #endif
  534. if (master->_get_unmapped_area)
  535. slave->_get_unmapped_area = part_get_unmapped_area;
  536. if (master->_read_oob)
  537. slave->_read_oob = part_read_oob;
  538. if (master->_write_oob)
  539. slave->_write_oob = part_write_oob;
  540. if (master->_read_user_prot_reg)
  541. slave->_read_user_prot_reg = part_read_user_prot_reg;
  542. if (master->_read_fact_prot_reg)
  543. slave->_read_fact_prot_reg = part_read_fact_prot_reg;
  544. if (master->_write_user_prot_reg)
  545. slave->_write_user_prot_reg = part_write_user_prot_reg;
  546. if (master->_lock_user_prot_reg)
  547. slave->_lock_user_prot_reg = part_lock_user_prot_reg;
  548. if (master->_get_user_prot_info)
  549. slave->_get_user_prot_info = part_get_user_prot_info;
  550. if (master->_get_fact_prot_info)
  551. slave->_get_fact_prot_info = part_get_fact_prot_info;
  552. if (master->_sync)
  553. slave->_sync = part_sync;
  554. #ifndef __UBOOT__
  555. if (!partno && !master->dev.class && master->_suspend &&
  556. master->_resume) {
  557. slave->_suspend = part_suspend;
  558. slave->_resume = part_resume;
  559. }
  560. if (master->_writev)
  561. slave->_writev = part_writev;
  562. #endif
  563. if (master->_lock)
  564. slave->_lock = part_lock;
  565. if (master->_unlock)
  566. slave->_unlock = part_unlock;
  567. if (master->_is_locked)
  568. slave->_is_locked = part_is_locked;
  569. if (master->_block_isreserved)
  570. slave->_block_isreserved = part_block_isreserved;
  571. if (master->_block_isbad)
  572. slave->_block_isbad = part_block_isbad;
  573. if (master->_block_markbad)
  574. slave->_block_markbad = part_block_markbad;
  575. slave->_erase = part_erase;
  576. slave->parent = master;
  577. slave->offset = part->offset;
  578. INIT_LIST_HEAD(&slave->partitions);
  579. INIT_LIST_HEAD(&slave->node);
  580. if (slave->offset == MTDPART_OFS_APPEND)
  581. slave->offset = cur_offset;
  582. if (slave->offset == MTDPART_OFS_NXTBLK) {
  583. slave->offset = cur_offset;
  584. if (mtd_mod_by_eb(cur_offset, master) != 0) {
  585. /* Round up to next erasesize */
  586. slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
  587. debug("Moving partition %d: "
  588. "0x%012llx -> 0x%012llx\n", partno,
  589. (unsigned long long)cur_offset, (unsigned long long)slave->offset);
  590. }
  591. }
  592. if (slave->offset == MTDPART_OFS_RETAIN) {
  593. slave->offset = cur_offset;
  594. if (master->size - slave->offset >= slave->size) {
  595. slave->size = master->size - slave->offset
  596. - slave->size;
  597. } else {
  598. debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
  599. part->name, master->size - slave->offset,
  600. slave->size);
  601. /* register to preserve ordering */
  602. goto out_register;
  603. }
  604. }
  605. if (slave->size == MTDPART_SIZ_FULL)
  606. slave->size = master->size - slave->offset;
  607. debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
  608. (unsigned long long)(slave->offset + slave->size), slave->name);
  609. /* let's do some sanity checks */
  610. if (slave->offset >= master->size) {
  611. /* let's register it anyway to preserve ordering */
  612. slave->offset = 0;
  613. slave->size = 0;
  614. printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
  615. part->name);
  616. goto out_register;
  617. }
  618. if (slave->offset + slave->size > master->size) {
  619. slave->size = master->size - slave->offset;
  620. printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
  621. part->name, master->name, slave->size);
  622. }
  623. if (master->numeraseregions > 1) {
  624. /* Deal with variable erase size stuff */
  625. int i, max = master->numeraseregions;
  626. u64 end = slave->offset + slave->size;
  627. struct mtd_erase_region_info *regions = master->eraseregions;
  628. /* Find the first erase regions which is part of this
  629. * partition. */
  630. for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
  631. ;
  632. /* The loop searched for the region _behind_ the first one */
  633. if (i > 0)
  634. i--;
  635. /* Pick biggest erasesize */
  636. for (; i < max && regions[i].offset < end; i++) {
  637. if (slave->erasesize < regions[i].erasesize)
  638. slave->erasesize = regions[i].erasesize;
  639. }
  640. WARN_ON(slave->erasesize == 0);
  641. } else {
  642. /* Single erase size */
  643. slave->erasesize = master->erasesize;
  644. }
  645. if ((slave->flags & MTD_WRITEABLE) &&
  646. mtd_mod_by_eb(slave->offset, slave)) {
  647. /* Doesn't start on a boundary of major erase size */
  648. /* FIXME: Let it be writable if it is on a boundary of
  649. * _minor_ erase size though */
  650. slave->flags &= ~MTD_WRITEABLE;
  651. printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
  652. part->name);
  653. }
  654. if ((slave->flags & MTD_WRITEABLE) &&
  655. mtd_mod_by_eb(slave->size, slave)) {
  656. slave->flags &= ~MTD_WRITEABLE;
  657. printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
  658. part->name);
  659. }
  660. slave->ecclayout = master->ecclayout;
  661. slave->ecc_step_size = master->ecc_step_size;
  662. slave->ecc_strength = master->ecc_strength;
  663. slave->bitflip_threshold = master->bitflip_threshold;
  664. if (master->_block_isbad) {
  665. uint64_t offs = 0;
  666. while (offs < slave->size) {
  667. if (mtd_block_isbad(master, offs + slave->offset))
  668. slave->ecc_stats.badblocks++;
  669. offs += slave->erasesize;
  670. }
  671. }
  672. out_register:
  673. return slave;
  674. }
  675. #ifndef __UBOOT__
  676. int mtd_add_partition(struct mtd_info *master, const char *name,
  677. long long offset, long long length)
  678. {
  679. struct mtd_partition part;
  680. struct mtd_info *p, *new;
  681. uint64_t start, end;
  682. int ret = 0;
  683. /* the direct offset is expected */
  684. if (offset == MTDPART_OFS_APPEND ||
  685. offset == MTDPART_OFS_NXTBLK)
  686. return -EINVAL;
  687. if (length == MTDPART_SIZ_FULL)
  688. length = master->size - offset;
  689. if (length <= 0)
  690. return -EINVAL;
  691. part.name = name;
  692. part.size = length;
  693. part.offset = offset;
  694. part.mask_flags = 0;
  695. part.ecclayout = NULL;
  696. new = allocate_partition(master, &part, -1, offset);
  697. if (IS_ERR(new))
  698. return PTR_ERR(new);
  699. start = offset;
  700. end = offset + length;
  701. mutex_lock(&mtd_partitions_mutex);
  702. list_for_each_entry(p, &master->partitions, node) {
  703. if (start >= p->offset &&
  704. (start < (p->offset + p->size)))
  705. goto err_inv;
  706. if (end >= p->offset &&
  707. (end < (p->offset + p->size)))
  708. goto err_inv;
  709. }
  710. list_add_tail(&new->node, &master->partitions);
  711. mutex_unlock(&mtd_partitions_mutex);
  712. add_mtd_device(new);
  713. return ret;
  714. err_inv:
  715. mutex_unlock(&mtd_partitions_mutex);
  716. free_partition(new);
  717. return -EINVAL;
  718. }
  719. EXPORT_SYMBOL_GPL(mtd_add_partition);
  720. int mtd_del_partition(struct mtd_info *master, int partno)
  721. {
  722. struct mtd_info *slave, *next;
  723. int ret = -EINVAL;
  724. mutex_lock(&mtd_partitions_mutex);
  725. list_for_each_entry_safe(slave, next, &master->partitions, node)
  726. if (slave->index == partno) {
  727. ret = del_mtd_device(slave);
  728. if (ret < 0)
  729. break;
  730. list_del(&slave->node);
  731. free_partition(slave);
  732. break;
  733. }
  734. mutex_unlock(&mtd_partitions_mutex);
  735. return ret;
  736. }
  737. EXPORT_SYMBOL_GPL(mtd_del_partition);
  738. #endif
  739. /*
  740. * This function, given a master MTD object and a partition table, creates
  741. * and registers slave MTD objects which are bound to the master according to
  742. * the partition definitions.
  743. *
  744. * We don't register the master, or expect the caller to have done so,
  745. * for reasons of data integrity.
  746. */
  747. int add_mtd_partitions(struct mtd_info *master,
  748. const struct mtd_partition *parts,
  749. int nbparts)
  750. {
  751. struct mtd_info *slave;
  752. uint64_t cur_offset = 0;
  753. int i;
  754. debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
  755. for (i = 0; i < nbparts; i++) {
  756. slave = allocate_partition(master, parts + i, i, cur_offset);
  757. if (IS_ERR(slave))
  758. return PTR_ERR(slave);
  759. mutex_lock(&mtd_partitions_mutex);
  760. list_add_tail(&slave->node, &master->partitions);
  761. mutex_unlock(&mtd_partitions_mutex);
  762. add_mtd_device(slave);
  763. cur_offset = slave->offset + slave->size;
  764. }
  765. return 0;
  766. }
  767. #ifndef __UBOOT__
  768. static DEFINE_SPINLOCK(part_parser_lock);
  769. static LIST_HEAD(part_parsers);
  770. static struct mtd_part_parser *get_partition_parser(const char *name)
  771. {
  772. struct mtd_part_parser *p, *ret = NULL;
  773. spin_lock(&part_parser_lock);
  774. list_for_each_entry(p, &part_parsers, list)
  775. if (!strcmp(p->name, name) && try_module_get(p->owner)) {
  776. ret = p;
  777. break;
  778. }
  779. spin_unlock(&part_parser_lock);
  780. return ret;
  781. }
  782. #define put_partition_parser(p) do { module_put((p)->owner); } while (0)
  783. void register_mtd_parser(struct mtd_part_parser *p)
  784. {
  785. spin_lock(&part_parser_lock);
  786. list_add(&p->list, &part_parsers);
  787. spin_unlock(&part_parser_lock);
  788. }
  789. EXPORT_SYMBOL_GPL(register_mtd_parser);
  790. void deregister_mtd_parser(struct mtd_part_parser *p)
  791. {
  792. spin_lock(&part_parser_lock);
  793. list_del(&p->list);
  794. spin_unlock(&part_parser_lock);
  795. }
  796. EXPORT_SYMBOL_GPL(deregister_mtd_parser);
  797. /*
  798. * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
  799. * are changing this array!
  800. */
  801. static const char * const default_mtd_part_types[] = {
  802. "cmdlinepart",
  803. "ofpart",
  804. NULL
  805. };
  806. /**
  807. * parse_mtd_partitions - parse MTD partitions
  808. * @master: the master partition (describes whole MTD device)
  809. * @types: names of partition parsers to try or %NULL
  810. * @pparts: array of partitions found is returned here
  811. * @data: MTD partition parser-specific data
  812. *
  813. * This function tries to find partition on MTD device @master. It uses MTD
  814. * partition parsers, specified in @types. However, if @types is %NULL, then
  815. * the default list of parsers is used. The default list contains only the
  816. * "cmdlinepart" and "ofpart" parsers ATM.
  817. * Note: If there are more then one parser in @types, the kernel only takes the
  818. * partitions parsed out by the first parser.
  819. *
  820. * This function may return:
  821. * o a negative error code in case of failure
  822. * o zero if no partitions were found
  823. * o a positive number of found partitions, in which case on exit @pparts will
  824. * point to an array containing this number of &struct mtd_info objects.
  825. */
  826. int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
  827. struct mtd_partition **pparts,
  828. struct mtd_part_parser_data *data)
  829. {
  830. struct mtd_part_parser *parser;
  831. int ret = 0;
  832. if (!types)
  833. types = default_mtd_part_types;
  834. for ( ; ret <= 0 && *types; types++) {
  835. parser = get_partition_parser(*types);
  836. if (!parser && !request_module("%s", *types))
  837. parser = get_partition_parser(*types);
  838. if (!parser)
  839. continue;
  840. ret = (*parser->parse_fn)(master, pparts, data);
  841. put_partition_parser(parser);
  842. if (ret > 0) {
  843. printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
  844. ret, parser->name, master->name);
  845. break;
  846. }
  847. }
  848. return ret;
  849. }
  850. #endif
  851. /* Returns the size of the entire flash chip */
  852. uint64_t mtd_get_device_size(const struct mtd_info *mtd)
  853. {
  854. if (mtd_is_partition(mtd))
  855. return mtd->parent->size;
  856. return mtd->size;
  857. }
  858. EXPORT_SYMBOL_GPL(mtd_get_device_size);