mtd-rootfs.patch 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. diff -Nur linux-2.6.39.4.orig/drivers/mtd/mtdpart.c linux-2.6.39.4/drivers/mtd/mtdpart.c
  2. --- linux-2.6.39.4.orig/drivers/mtd/mtdpart.c 2011-08-03 21:43:28.000000000 +0200
  3. +++ linux-2.6.39.4/drivers/mtd/mtdpart.c 2012-01-17 19:15:04.000000000 +0100
  4. @@ -30,6 +30,7 @@
  5. #include <linux/mtd/mtd.h>
  6. #include <linux/mtd/partitions.h>
  7. #include <linux/err.h>
  8. +#include <linux/root_dev.h>
  9. /* Our partition linked list */
  10. static LIST_HEAD(mtd_partitions);
  11. @@ -660,6 +661,14 @@
  12. if (IS_ERR(slave))
  13. return PTR_ERR(slave);
  14. + if (strcmp(parts[i].name, "rootfs") == 0) {
  15. + if (ROOT_DEV == 0) {
  16. + printk(KERN_NOTICE "mtd: partition \"rootfs\" "
  17. + "set to be root filesystem\n");
  18. + ROOT_DEV = MKDEV(MTD_BLOCK_MAJOR, i);
  19. + }
  20. + }
  21. +
  22. mutex_lock(&mtd_partitions_mutex);
  23. list_add(&slave->list, &mtd_partitions);
  24. mutex_unlock(&mtd_partitions_mutex);
  25. diff -Nur linux-2.6.39.4.orig/drivers/mtd/mtdpart.c.orig linux-2.6.39.4/drivers/mtd/mtdpart.c.orig
  26. --- linux-2.6.39.4.orig/drivers/mtd/mtdpart.c.orig 1970-01-01 01:00:00.000000000 +0100
  27. +++ linux-2.6.39.4/drivers/mtd/mtdpart.c.orig 2011-08-03 21:43:28.000000000 +0200
  28. @@ -0,0 +1,756 @@
  29. +/*
  30. + * Simple MTD partitioning layer
  31. + *
  32. + * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
  33. + * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
  34. + * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
  35. + *
  36. + * This program is free software; you can redistribute it and/or modify
  37. + * it under the terms of the GNU General Public License as published by
  38. + * the Free Software Foundation; either version 2 of the License, or
  39. + * (at your option) any later version.
  40. + *
  41. + * This program is distributed in the hope that it will be useful,
  42. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  43. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  44. + * GNU General Public License for more details.
  45. + *
  46. + * You should have received a copy of the GNU General Public License
  47. + * along with this program; if not, write to the Free Software
  48. + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  49. + *
  50. + */
  51. +
  52. +#include <linux/module.h>
  53. +#include <linux/types.h>
  54. +#include <linux/kernel.h>
  55. +#include <linux/slab.h>
  56. +#include <linux/list.h>
  57. +#include <linux/kmod.h>
  58. +#include <linux/mtd/mtd.h>
  59. +#include <linux/mtd/partitions.h>
  60. +#include <linux/err.h>
  61. +
  62. +/* Our partition linked list */
  63. +static LIST_HEAD(mtd_partitions);
  64. +static DEFINE_MUTEX(mtd_partitions_mutex);
  65. +
  66. +/* Our partition node structure */
  67. +struct mtd_part {
  68. + struct mtd_info mtd;
  69. + struct mtd_info *master;
  70. + uint64_t offset;
  71. + struct list_head list;
  72. +};
  73. +
  74. +/*
  75. + * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
  76. + * the pointer to that structure with this macro.
  77. + */
  78. +#define PART(x) ((struct mtd_part *)(x))
  79. +
  80. +
  81. +/*
  82. + * MTD methods which simply translate the effective address and pass through
  83. + * to the _real_ device.
  84. + */
  85. +
  86. +static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
  87. + size_t *retlen, u_char *buf)
  88. +{
  89. + struct mtd_part *part = PART(mtd);
  90. + struct mtd_ecc_stats stats;
  91. + int res;
  92. +
  93. + stats = part->master->ecc_stats;
  94. +
  95. + if (from >= mtd->size)
  96. + len = 0;
  97. + else if (from + len > mtd->size)
  98. + len = mtd->size - from;
  99. + res = part->master->read(part->master, from + part->offset,
  100. + len, retlen, buf);
  101. + if (unlikely(res)) {
  102. + if (res == -EUCLEAN)
  103. + mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
  104. + if (res == -EBADMSG)
  105. + mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
  106. + }
  107. + return res;
  108. +}
  109. +
  110. +static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
  111. + size_t *retlen, void **virt, resource_size_t *phys)
  112. +{
  113. + struct mtd_part *part = PART(mtd);
  114. + if (from >= mtd->size)
  115. + len = 0;
  116. + else if (from + len > mtd->size)
  117. + len = mtd->size - from;
  118. + return part->master->point (part->master, from + part->offset,
  119. + len, retlen, virt, phys);
  120. +}
  121. +
  122. +static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
  123. +{
  124. + struct mtd_part *part = PART(mtd);
  125. +
  126. + part->master->unpoint(part->master, from + part->offset, len);
  127. +}
  128. +
  129. +static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
  130. + unsigned long len,
  131. + unsigned long offset,
  132. + unsigned long flags)
  133. +{
  134. + struct mtd_part *part = PART(mtd);
  135. +
  136. + offset += part->offset;
  137. + return part->master->get_unmapped_area(part->master, len, offset,
  138. + flags);
  139. +}
  140. +
  141. +static int part_read_oob(struct mtd_info *mtd, loff_t from,
  142. + struct mtd_oob_ops *ops)
  143. +{
  144. + struct mtd_part *part = PART(mtd);
  145. + int res;
  146. +
  147. + if (from >= mtd->size)
  148. + return -EINVAL;
  149. + if (ops->datbuf && from + ops->len > mtd->size)
  150. + return -EINVAL;
  151. +
  152. + /*
  153. + * If OOB is also requested, make sure that we do not read past the end
  154. + * of this partition.
  155. + */
  156. + if (ops->oobbuf) {
  157. + size_t len, pages;
  158. +
  159. + if (ops->mode == MTD_OOB_AUTO)
  160. + len = mtd->oobavail;
  161. + else
  162. + len = mtd->oobsize;
  163. + pages = mtd_div_by_ws(mtd->size, mtd);
  164. + pages -= mtd_div_by_ws(from, mtd);
  165. + if (ops->ooboffs + ops->ooblen > pages * len)
  166. + return -EINVAL;
  167. + }
  168. +
  169. + res = part->master->read_oob(part->master, from + part->offset, ops);
  170. + if (unlikely(res)) {
  171. + if (res == -EUCLEAN)
  172. + mtd->ecc_stats.corrected++;
  173. + if (res == -EBADMSG)
  174. + mtd->ecc_stats.failed++;
  175. + }
  176. + return res;
  177. +}
  178. +
  179. +static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
  180. + size_t len, size_t *retlen, u_char *buf)
  181. +{
  182. + struct mtd_part *part = PART(mtd);
  183. + return part->master->read_user_prot_reg(part->master, from,
  184. + len, retlen, buf);
  185. +}
  186. +
  187. +static int part_get_user_prot_info(struct mtd_info *mtd,
  188. + struct otp_info *buf, size_t len)
  189. +{
  190. + struct mtd_part *part = PART(mtd);
  191. + return part->master->get_user_prot_info(part->master, buf, len);
  192. +}
  193. +
  194. +static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
  195. + size_t len, size_t *retlen, u_char *buf)
  196. +{
  197. + struct mtd_part *part = PART(mtd);
  198. + return part->master->read_fact_prot_reg(part->master, from,
  199. + len, retlen, buf);
  200. +}
  201. +
  202. +static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
  203. + size_t len)
  204. +{
  205. + struct mtd_part *part = PART(mtd);
  206. + return part->master->get_fact_prot_info(part->master, buf, len);
  207. +}
  208. +
  209. +static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
  210. + size_t *retlen, const u_char *buf)
  211. +{
  212. + struct mtd_part *part = PART(mtd);
  213. + if (!(mtd->flags & MTD_WRITEABLE))
  214. + return -EROFS;
  215. + if (to >= mtd->size)
  216. + len = 0;
  217. + else if (to + len > mtd->size)
  218. + len = mtd->size - to;
  219. + return part->master->write(part->master, to + part->offset,
  220. + len, retlen, buf);
  221. +}
  222. +
  223. +static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
  224. + size_t *retlen, const u_char *buf)
  225. +{
  226. + struct mtd_part *part = PART(mtd);
  227. + if (!(mtd->flags & MTD_WRITEABLE))
  228. + return -EROFS;
  229. + if (to >= mtd->size)
  230. + len = 0;
  231. + else if (to + len > mtd->size)
  232. + len = mtd->size - to;
  233. + return part->master->panic_write(part->master, to + part->offset,
  234. + len, retlen, buf);
  235. +}
  236. +
  237. +static int part_write_oob(struct mtd_info *mtd, loff_t to,
  238. + struct mtd_oob_ops *ops)
  239. +{
  240. + struct mtd_part *part = PART(mtd);
  241. +
  242. + if (!(mtd->flags & MTD_WRITEABLE))
  243. + return -EROFS;
  244. +
  245. + if (to >= mtd->size)
  246. + return -EINVAL;
  247. + if (ops->datbuf && to + ops->len > mtd->size)
  248. + return -EINVAL;
  249. + return part->master->write_oob(part->master, to + part->offset, ops);
  250. +}
  251. +
  252. +static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
  253. + size_t len, size_t *retlen, u_char *buf)
  254. +{
  255. + struct mtd_part *part = PART(mtd);
  256. + return part->master->write_user_prot_reg(part->master, from,
  257. + len, retlen, buf);
  258. +}
  259. +
  260. +static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
  261. + size_t len)
  262. +{
  263. + struct mtd_part *part = PART(mtd);
  264. + return part->master->lock_user_prot_reg(part->master, from, len);
  265. +}
  266. +
  267. +static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
  268. + unsigned long count, loff_t to, size_t *retlen)
  269. +{
  270. + struct mtd_part *part = PART(mtd);
  271. + if (!(mtd->flags & MTD_WRITEABLE))
  272. + return -EROFS;
  273. + return part->master->writev(part->master, vecs, count,
  274. + to + part->offset, retlen);
  275. +}
  276. +
  277. +static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
  278. +{
  279. + struct mtd_part *part = PART(mtd);
  280. + int ret;
  281. + if (!(mtd->flags & MTD_WRITEABLE))
  282. + return -EROFS;
  283. + if (instr->addr >= mtd->size)
  284. + return -EINVAL;
  285. + instr->addr += part->offset;
  286. + ret = part->master->erase(part->master, instr);
  287. + if (ret) {
  288. + if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  289. + instr->fail_addr -= part->offset;
  290. + instr->addr -= part->offset;
  291. + }
  292. + return ret;
  293. +}
  294. +
  295. +void mtd_erase_callback(struct erase_info *instr)
  296. +{
  297. + if (instr->mtd->erase == part_erase) {
  298. + struct mtd_part *part = PART(instr->mtd);
  299. +
  300. + if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  301. + instr->fail_addr -= part->offset;
  302. + instr->addr -= part->offset;
  303. + }
  304. + if (instr->callback)
  305. + instr->callback(instr);
  306. +}
  307. +EXPORT_SYMBOL_GPL(mtd_erase_callback);
  308. +
  309. +static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  310. +{
  311. + struct mtd_part *part = PART(mtd);
  312. + if ((len + ofs) > mtd->size)
  313. + return -EINVAL;
  314. + return part->master->lock(part->master, ofs + part->offset, len);
  315. +}
  316. +
  317. +static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  318. +{
  319. + struct mtd_part *part = PART(mtd);
  320. + if ((len + ofs) > mtd->size)
  321. + return -EINVAL;
  322. + return part->master->unlock(part->master, ofs + part->offset, len);
  323. +}
  324. +
  325. +static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
  326. +{
  327. + struct mtd_part *part = PART(mtd);
  328. + if ((len + ofs) > mtd->size)
  329. + return -EINVAL;
  330. + return part->master->is_locked(part->master, ofs + part->offset, len);
  331. +}
  332. +
  333. +static void part_sync(struct mtd_info *mtd)
  334. +{
  335. + struct mtd_part *part = PART(mtd);
  336. + part->master->sync(part->master);
  337. +}
  338. +
  339. +static int part_suspend(struct mtd_info *mtd)
  340. +{
  341. + struct mtd_part *part = PART(mtd);
  342. + return part->master->suspend(part->master);
  343. +}
  344. +
  345. +static void part_resume(struct mtd_info *mtd)
  346. +{
  347. + struct mtd_part *part = PART(mtd);
  348. + part->master->resume(part->master);
  349. +}
  350. +
  351. +static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
  352. +{
  353. + struct mtd_part *part = PART(mtd);
  354. + if (ofs >= mtd->size)
  355. + return -EINVAL;
  356. + ofs += part->offset;
  357. + return part->master->block_isbad(part->master, ofs);
  358. +}
  359. +
  360. +static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
  361. +{
  362. + struct mtd_part *part = PART(mtd);
  363. + int res;
  364. +
  365. + if (!(mtd->flags & MTD_WRITEABLE))
  366. + return -EROFS;
  367. + if (ofs >= mtd->size)
  368. + return -EINVAL;
  369. + ofs += part->offset;
  370. + res = part->master->block_markbad(part->master, ofs);
  371. + if (!res)
  372. + mtd->ecc_stats.badblocks++;
  373. + return res;
  374. +}
  375. +
  376. +static inline void free_partition(struct mtd_part *p)
  377. +{
  378. + kfree(p->mtd.name);
  379. + kfree(p);
  380. +}
  381. +
  382. +/*
  383. + * This function unregisters and destroy all slave MTD objects which are
  384. + * attached to the given master MTD object.
  385. + */
  386. +
  387. +int del_mtd_partitions(struct mtd_info *master)
  388. +{
  389. + struct mtd_part *slave, *next;
  390. + int ret, err = 0;
  391. +
  392. + mutex_lock(&mtd_partitions_mutex);
  393. + list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  394. + if (slave->master == master) {
  395. + ret = del_mtd_device(&slave->mtd);
  396. + if (ret < 0) {
  397. + err = ret;
  398. + continue;
  399. + }
  400. + list_del(&slave->list);
  401. + free_partition(slave);
  402. + }
  403. + mutex_unlock(&mtd_partitions_mutex);
  404. +
  405. + return err;
  406. +}
  407. +EXPORT_SYMBOL(del_mtd_partitions);
  408. +
  409. +static struct mtd_part *allocate_partition(struct mtd_info *master,
  410. + const struct mtd_partition *part, int partno,
  411. + uint64_t cur_offset)
  412. +{
  413. + struct mtd_part *slave;
  414. + char *name;
  415. +
  416. + /* allocate the partition structure */
  417. + slave = kzalloc(sizeof(*slave), GFP_KERNEL);
  418. + name = kstrdup(part->name, GFP_KERNEL);
  419. + if (!name || !slave) {
  420. + printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
  421. + master->name);
  422. + kfree(name);
  423. + kfree(slave);
  424. + return ERR_PTR(-ENOMEM);
  425. + }
  426. +
  427. + /* set up the MTD object for this partition */
  428. + slave->mtd.type = master->type;
  429. + slave->mtd.flags = master->flags & ~part->mask_flags;
  430. + slave->mtd.size = part->size;
  431. + slave->mtd.writesize = master->writesize;
  432. + slave->mtd.writebufsize = master->writebufsize;
  433. + slave->mtd.oobsize = master->oobsize;
  434. + slave->mtd.oobavail = master->oobavail;
  435. + slave->mtd.subpage_sft = master->subpage_sft;
  436. +
  437. + slave->mtd.name = name;
  438. + slave->mtd.owner = master->owner;
  439. + slave->mtd.backing_dev_info = master->backing_dev_info;
  440. +
  441. + /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
  442. + * to have the same data be in two different partitions.
  443. + */
  444. + slave->mtd.dev.parent = master->dev.parent;
  445. +
  446. + slave->mtd.read = part_read;
  447. + slave->mtd.write = part_write;
  448. +
  449. + if (master->panic_write)
  450. + slave->mtd.panic_write = part_panic_write;
  451. +
  452. + if (master->point && master->unpoint) {
  453. + slave->mtd.point = part_point;
  454. + slave->mtd.unpoint = part_unpoint;
  455. + }
  456. +
  457. + if (master->get_unmapped_area)
  458. + slave->mtd.get_unmapped_area = part_get_unmapped_area;
  459. + if (master->read_oob)
  460. + slave->mtd.read_oob = part_read_oob;
  461. + if (master->write_oob)
  462. + slave->mtd.write_oob = part_write_oob;
  463. + if (master->read_user_prot_reg)
  464. + slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
  465. + if (master->read_fact_prot_reg)
  466. + slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
  467. + if (master->write_user_prot_reg)
  468. + slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
  469. + if (master->lock_user_prot_reg)
  470. + slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
  471. + if (master->get_user_prot_info)
  472. + slave->mtd.get_user_prot_info = part_get_user_prot_info;
  473. + if (master->get_fact_prot_info)
  474. + slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
  475. + if (master->sync)
  476. + slave->mtd.sync = part_sync;
  477. + if (!partno && !master->dev.class && master->suspend && master->resume) {
  478. + slave->mtd.suspend = part_suspend;
  479. + slave->mtd.resume = part_resume;
  480. + }
  481. + if (master->writev)
  482. + slave->mtd.writev = part_writev;
  483. + if (master->lock)
  484. + slave->mtd.lock = part_lock;
  485. + if (master->unlock)
  486. + slave->mtd.unlock = part_unlock;
  487. + if (master->is_locked)
  488. + slave->mtd.is_locked = part_is_locked;
  489. + if (master->block_isbad)
  490. + slave->mtd.block_isbad = part_block_isbad;
  491. + if (master->block_markbad)
  492. + slave->mtd.block_markbad = part_block_markbad;
  493. + slave->mtd.erase = part_erase;
  494. + slave->master = master;
  495. + slave->offset = part->offset;
  496. +
  497. + if (slave->offset == MTDPART_OFS_APPEND)
  498. + slave->offset = cur_offset;
  499. + if (slave->offset == MTDPART_OFS_NXTBLK) {
  500. + slave->offset = cur_offset;
  501. + if (mtd_mod_by_eb(cur_offset, master) != 0) {
  502. + /* Round up to next erasesize */
  503. + slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
  504. + printk(KERN_NOTICE "Moving partition %d: "
  505. + "0x%012llx -> 0x%012llx\n", partno,
  506. + (unsigned long long)cur_offset, (unsigned long long)slave->offset);
  507. + }
  508. + }
  509. + if (slave->mtd.size == MTDPART_SIZ_FULL)
  510. + slave->mtd.size = master->size - slave->offset;
  511. +
  512. + printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
  513. + (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
  514. +
  515. + /* let's do some sanity checks */
  516. + if (slave->offset >= master->size) {
  517. + /* let's register it anyway to preserve ordering */
  518. + slave->offset = 0;
  519. + slave->mtd.size = 0;
  520. + printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
  521. + part->name);
  522. + goto out_register;
  523. + }
  524. + if (slave->offset + slave->mtd.size > master->size) {
  525. + slave->mtd.size = master->size - slave->offset;
  526. + printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
  527. + part->name, master->name, (unsigned long long)slave->mtd.size);
  528. + }
  529. + if (master->numeraseregions > 1) {
  530. + /* Deal with variable erase size stuff */
  531. + int i, max = master->numeraseregions;
  532. + u64 end = slave->offset + slave->mtd.size;
  533. + struct mtd_erase_region_info *regions = master->eraseregions;
  534. +
  535. + /* Find the first erase regions which is part of this
  536. + * partition. */
  537. + for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
  538. + ;
  539. + /* The loop searched for the region _behind_ the first one */
  540. + if (i > 0)
  541. + i--;
  542. +
  543. + /* Pick biggest erasesize */
  544. + for (; i < max && regions[i].offset < end; i++) {
  545. + if (slave->mtd.erasesize < regions[i].erasesize) {
  546. + slave->mtd.erasesize = regions[i].erasesize;
  547. + }
  548. + }
  549. + BUG_ON(slave->mtd.erasesize == 0);
  550. + } else {
  551. + /* Single erase size */
  552. + slave->mtd.erasesize = master->erasesize;
  553. + }
  554. +
  555. + if ((slave->mtd.flags & MTD_WRITEABLE) &&
  556. + mtd_mod_by_eb(slave->offset, &slave->mtd)) {
  557. + /* Doesn't start on a boundary of major erase size */
  558. + /* FIXME: Let it be writable if it is on a boundary of
  559. + * _minor_ erase size though */
  560. + slave->mtd.flags &= ~MTD_WRITEABLE;
  561. + printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
  562. + part->name);
  563. + }
  564. + if ((slave->mtd.flags & MTD_WRITEABLE) &&
  565. + mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
  566. + slave->mtd.flags &= ~MTD_WRITEABLE;
  567. + printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
  568. + part->name);
  569. + }
  570. +
  571. + slave->mtd.ecclayout = master->ecclayout;
  572. + if (master->block_isbad) {
  573. + uint64_t offs = 0;
  574. +
  575. + while (offs < slave->mtd.size) {
  576. + if (master->block_isbad(master,
  577. + offs + slave->offset))
  578. + slave->mtd.ecc_stats.badblocks++;
  579. + offs += slave->mtd.erasesize;
  580. + }
  581. + }
  582. +
  583. +out_register:
  584. + return slave;
  585. +}
  586. +
  587. +int mtd_add_partition(struct mtd_info *master, char *name,
  588. + long long offset, long long length)
  589. +{
  590. + struct mtd_partition part;
  591. + struct mtd_part *p, *new;
  592. + uint64_t start, end;
  593. + int ret = 0;
  594. +
  595. + /* the direct offset is expected */
  596. + if (offset == MTDPART_OFS_APPEND ||
  597. + offset == MTDPART_OFS_NXTBLK)
  598. + return -EINVAL;
  599. +
  600. + if (length == MTDPART_SIZ_FULL)
  601. + length = master->size - offset;
  602. +
  603. + if (length <= 0)
  604. + return -EINVAL;
  605. +
  606. + part.name = name;
  607. + part.size = length;
  608. + part.offset = offset;
  609. + part.mask_flags = 0;
  610. + part.ecclayout = NULL;
  611. +
  612. + new = allocate_partition(master, &part, -1, offset);
  613. + if (IS_ERR(new))
  614. + return PTR_ERR(new);
  615. +
  616. + start = offset;
  617. + end = offset + length;
  618. +
  619. + mutex_lock(&mtd_partitions_mutex);
  620. + list_for_each_entry(p, &mtd_partitions, list)
  621. + if (p->master == master) {
  622. + if ((start >= p->offset) &&
  623. + (start < (p->offset + p->mtd.size)))
  624. + goto err_inv;
  625. +
  626. + if ((end >= p->offset) &&
  627. + (end < (p->offset + p->mtd.size)))
  628. + goto err_inv;
  629. + }
  630. +
  631. + list_add(&new->list, &mtd_partitions);
  632. + mutex_unlock(&mtd_partitions_mutex);
  633. +
  634. + add_mtd_device(&new->mtd);
  635. +
  636. + return ret;
  637. +err_inv:
  638. + mutex_unlock(&mtd_partitions_mutex);
  639. + free_partition(new);
  640. + return -EINVAL;
  641. +}
  642. +EXPORT_SYMBOL_GPL(mtd_add_partition);
  643. +
  644. +int mtd_del_partition(struct mtd_info *master, int partno)
  645. +{
  646. + struct mtd_part *slave, *next;
  647. + int ret = -EINVAL;
  648. +
  649. + mutex_lock(&mtd_partitions_mutex);
  650. + list_for_each_entry_safe(slave, next, &mtd_partitions, list)
  651. + if ((slave->master == master) &&
  652. + (slave->mtd.index == partno)) {
  653. + ret = del_mtd_device(&slave->mtd);
  654. + if (ret < 0)
  655. + break;
  656. +
  657. + list_del(&slave->list);
  658. + free_partition(slave);
  659. + break;
  660. + }
  661. + mutex_unlock(&mtd_partitions_mutex);
  662. +
  663. + return ret;
  664. +}
  665. +EXPORT_SYMBOL_GPL(mtd_del_partition);
  666. +
  667. +/*
  668. + * This function, given a master MTD object and a partition table, creates
  669. + * and registers slave MTD objects which are bound to the master according to
  670. + * the partition definitions.
  671. + *
  672. + * We don't register the master, or expect the caller to have done so,
  673. + * for reasons of data integrity.
  674. + */
  675. +
  676. +int add_mtd_partitions(struct mtd_info *master,
  677. + const struct mtd_partition *parts,
  678. + int nbparts)
  679. +{
  680. + struct mtd_part *slave;
  681. + uint64_t cur_offset = 0;
  682. + int i;
  683. +
  684. + printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
  685. +
  686. + for (i = 0; i < nbparts; i++) {
  687. + slave = allocate_partition(master, parts + i, i, cur_offset);
  688. + if (IS_ERR(slave))
  689. + return PTR_ERR(slave);
  690. +
  691. + mutex_lock(&mtd_partitions_mutex);
  692. + list_add(&slave->list, &mtd_partitions);
  693. + mutex_unlock(&mtd_partitions_mutex);
  694. +
  695. + add_mtd_device(&slave->mtd);
  696. +
  697. + cur_offset = slave->offset + slave->mtd.size;
  698. + }
  699. +
  700. + return 0;
  701. +}
  702. +EXPORT_SYMBOL(add_mtd_partitions);
  703. +
  704. +static DEFINE_SPINLOCK(part_parser_lock);
  705. +static LIST_HEAD(part_parsers);
  706. +
  707. +static struct mtd_part_parser *get_partition_parser(const char *name)
  708. +{
  709. + struct mtd_part_parser *p, *ret = NULL;
  710. +
  711. + spin_lock(&part_parser_lock);
  712. +
  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. +
  719. + spin_unlock(&part_parser_lock);
  720. +
  721. + return ret;
  722. +}
  723. +
  724. +int register_mtd_parser(struct mtd_part_parser *p)
  725. +{
  726. + spin_lock(&part_parser_lock);
  727. + list_add(&p->list, &part_parsers);
  728. + spin_unlock(&part_parser_lock);
  729. +
  730. + return 0;
  731. +}
  732. +EXPORT_SYMBOL_GPL(register_mtd_parser);
  733. +
  734. +int deregister_mtd_parser(struct mtd_part_parser *p)
  735. +{
  736. + spin_lock(&part_parser_lock);
  737. + list_del(&p->list);
  738. + spin_unlock(&part_parser_lock);
  739. + return 0;
  740. +}
  741. +EXPORT_SYMBOL_GPL(deregister_mtd_parser);
  742. +
  743. +int parse_mtd_partitions(struct mtd_info *master, const char **types,
  744. + struct mtd_partition **pparts, unsigned long origin)
  745. +{
  746. + struct mtd_part_parser *parser;
  747. + int ret = 0;
  748. +
  749. + for ( ; ret <= 0 && *types; types++) {
  750. + parser = get_partition_parser(*types);
  751. + if (!parser && !request_module("%s", *types))
  752. + parser = get_partition_parser(*types);
  753. + if (!parser) {
  754. + printk(KERN_NOTICE "%s partition parsing not available\n",
  755. + *types);
  756. + continue;
  757. + }
  758. + ret = (*parser->parse_fn)(master, pparts, origin);
  759. + if (ret > 0) {
  760. + printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
  761. + ret, parser->name, master->name);
  762. + }
  763. + put_partition_parser(parser);
  764. + }
  765. + return ret;
  766. +}
  767. +EXPORT_SYMBOL_GPL(parse_mtd_partitions);
  768. +
  769. +int mtd_is_partition(struct mtd_info *mtd)
  770. +{
  771. + struct mtd_part *part;
  772. + int ispart = 0;
  773. +
  774. + mutex_lock(&mtd_partitions_mutex);
  775. + list_for_each_entry(part, &mtd_partitions, list)
  776. + if (&part->mtd == mtd) {
  777. + ispart = 1;
  778. + break;
  779. + }
  780. + mutex_unlock(&mtd_partitions_mutex);
  781. +
  782. + return ispart;
  783. +}
  784. +EXPORT_SYMBOL_GPL(mtd_is_partition);