build.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. * Copyright (c) Nokia Corporation, 2007
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. * Author: Artem Bityutskiy (Битюцкий Артём),
  20. * Frank Haverkamp
  21. */
  22. /*
  23. * This file includes UBI initialization and building of UBI devices. At the
  24. * moment UBI devices may only be added while UBI is initialized, but dynamic
  25. * device add/remove functionality is planned. Also, at the moment we only
  26. * attach UBI devices by scanning, which will become a bottleneck when flashes
  27. * reach certain large size. Then one may improve UBI and add other methods.
  28. */
  29. #include <linux/err.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/stringify.h>
  33. #include <linux/stat.h>
  34. #include <linux/log2.h>
  35. #include "ubi.h"
  36. /* Maximum length of the 'mtd=' parameter */
  37. #define MTD_PARAM_LEN_MAX 64
  38. /**
  39. * struct mtd_dev_param - MTD device parameter description data structure.
  40. * @name: MTD device name or number string
  41. * @vid_hdr_offs: VID header offset
  42. * @data_offs: data offset
  43. */
  44. struct mtd_dev_param
  45. {
  46. char name[MTD_PARAM_LEN_MAX];
  47. int vid_hdr_offs;
  48. int data_offs;
  49. };
  50. /* Numbers of elements set in the @mtd_dev_param array */
  51. static int mtd_devs = 0;
  52. /* MTD devices specification parameters */
  53. static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
  54. /* All UBI devices in system */
  55. struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  56. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  57. struct class *ubi_class;
  58. /* Slab cache for lock-tree entries */
  59. struct kmem_cache *ubi_ltree_slab;
  60. /* Slab cache for wear-leveling entries */
  61. struct kmem_cache *ubi_wl_entry_slab;
  62. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  63. static ssize_t ubi_version_show(struct class *class, char *buf)
  64. {
  65. return sprintf(buf, "%d\n", UBI_VERSION);
  66. }
  67. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  68. static struct class_attribute ubi_version =
  69. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  70. static ssize_t dev_attribute_show(struct device *dev,
  71. struct device_attribute *attr, char *buf);
  72. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  73. static struct device_attribute dev_eraseblock_size =
  74. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  75. static struct device_attribute dev_avail_eraseblocks =
  76. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  77. static struct device_attribute dev_total_eraseblocks =
  78. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  79. static struct device_attribute dev_volumes_count =
  80. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  81. static struct device_attribute dev_max_ec =
  82. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  83. static struct device_attribute dev_reserved_for_bad =
  84. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  85. static struct device_attribute dev_bad_peb_count =
  86. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  87. static struct device_attribute dev_max_vol_count =
  88. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  89. static struct device_attribute dev_min_io_size =
  90. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  91. static struct device_attribute dev_bgt_enabled =
  92. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  93. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  94. static ssize_t dev_attribute_show(struct device *dev,
  95. struct device_attribute *attr, char *buf)
  96. {
  97. const struct ubi_device *ubi;
  98. ubi = container_of(dev, struct ubi_device, dev);
  99. if (attr == &dev_eraseblock_size)
  100. return sprintf(buf, "%d\n", ubi->leb_size);
  101. else if (attr == &dev_avail_eraseblocks)
  102. return sprintf(buf, "%d\n", ubi->avail_pebs);
  103. else if (attr == &dev_total_eraseblocks)
  104. return sprintf(buf, "%d\n", ubi->good_peb_count);
  105. else if (attr == &dev_volumes_count)
  106. return sprintf(buf, "%d\n", ubi->vol_count);
  107. else if (attr == &dev_max_ec)
  108. return sprintf(buf, "%d\n", ubi->max_ec);
  109. else if (attr == &dev_reserved_for_bad)
  110. return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  111. else if (attr == &dev_bad_peb_count)
  112. return sprintf(buf, "%d\n", ubi->bad_peb_count);
  113. else if (attr == &dev_max_vol_count)
  114. return sprintf(buf, "%d\n", ubi->vtbl_slots);
  115. else if (attr == &dev_min_io_size)
  116. return sprintf(buf, "%d\n", ubi->min_io_size);
  117. else if (attr == &dev_bgt_enabled)
  118. return sprintf(buf, "%d\n", ubi->thread_enabled);
  119. else
  120. BUG();
  121. return 0;
  122. }
  123. /* Fake "release" method for UBI devices */
  124. static void dev_release(struct device *dev) { }
  125. /**
  126. * ubi_sysfs_init - initialize sysfs for an UBI device.
  127. * @ubi: UBI device description object
  128. *
  129. * This function returns zero in case of success and a negative error code in
  130. * case of failure.
  131. */
  132. static int ubi_sysfs_init(struct ubi_device *ubi)
  133. {
  134. int err;
  135. ubi->dev.release = dev_release;
  136. ubi->dev.devt = ubi->cdev.dev;
  137. ubi->dev.class = ubi_class;
  138. sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
  139. err = device_register(&ubi->dev);
  140. if (err)
  141. goto out;
  142. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  143. if (err)
  144. goto out_unregister;
  145. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  146. if (err)
  147. goto out_eraseblock_size;
  148. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  149. if (err)
  150. goto out_avail_eraseblocks;
  151. err = device_create_file(&ubi->dev, &dev_volumes_count);
  152. if (err)
  153. goto out_total_eraseblocks;
  154. err = device_create_file(&ubi->dev, &dev_max_ec);
  155. if (err)
  156. goto out_volumes_count;
  157. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  158. if (err)
  159. goto out_volumes_max_ec;
  160. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  161. if (err)
  162. goto out_reserved_for_bad;
  163. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  164. if (err)
  165. goto out_bad_peb_count;
  166. err = device_create_file(&ubi->dev, &dev_min_io_size);
  167. if (err)
  168. goto out_max_vol_count;
  169. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  170. if (err)
  171. goto out_min_io_size;
  172. return 0;
  173. out_min_io_size:
  174. device_remove_file(&ubi->dev, &dev_min_io_size);
  175. out_max_vol_count:
  176. device_remove_file(&ubi->dev, &dev_max_vol_count);
  177. out_bad_peb_count:
  178. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  179. out_reserved_for_bad:
  180. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  181. out_volumes_max_ec:
  182. device_remove_file(&ubi->dev, &dev_max_ec);
  183. out_volumes_count:
  184. device_remove_file(&ubi->dev, &dev_volumes_count);
  185. out_total_eraseblocks:
  186. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  187. out_avail_eraseblocks:
  188. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  189. out_eraseblock_size:
  190. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  191. out_unregister:
  192. device_unregister(&ubi->dev);
  193. out:
  194. ubi_err("failed to initialize sysfs for %s, error %d",
  195. ubi->ubi_name, err);
  196. return err;
  197. }
  198. /**
  199. * ubi_sysfs_close - close sysfs for an UBI device.
  200. * @ubi: UBI device description object
  201. */
  202. static void ubi_sysfs_close(struct ubi_device *ubi)
  203. {
  204. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  205. device_remove_file(&ubi->dev, &dev_min_io_size);
  206. device_remove_file(&ubi->dev, &dev_max_vol_count);
  207. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  208. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  209. device_remove_file(&ubi->dev, &dev_max_ec);
  210. device_remove_file(&ubi->dev, &dev_volumes_count);
  211. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  212. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  213. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  214. device_unregister(&ubi->dev);
  215. }
  216. /**
  217. * kill_volumes - destroy all volumes.
  218. * @ubi: UBI device description object
  219. */
  220. static void kill_volumes(struct ubi_device *ubi)
  221. {
  222. int i;
  223. for (i = 0; i < ubi->vtbl_slots; i++)
  224. if (ubi->volumes[i])
  225. ubi_free_volume(ubi, ubi->volumes[i]);
  226. }
  227. /**
  228. * uif_init - initialize user interfaces for an UBI device.
  229. * @ubi: UBI device description object
  230. *
  231. * This function returns zero in case of success and a negative error code in
  232. * case of failure.
  233. */
  234. static int uif_init(struct ubi_device *ubi)
  235. {
  236. int i, err;
  237. dev_t dev;
  238. mutex_init(&ubi->vtbl_mutex);
  239. spin_lock_init(&ubi->volumes_lock);
  240. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  241. /*
  242. * Major numbers for the UBI character devices are allocated
  243. * dynamically. Major numbers of volume character devices are
  244. * equivalent to ones of the corresponding UBI character device. Minor
  245. * numbers of UBI character devices are 0, while minor numbers of
  246. * volume character devices start from 1. Thus, we allocate one major
  247. * number and ubi->vtbl_slots + 1 minor numbers.
  248. */
  249. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  250. if (err) {
  251. ubi_err("cannot register UBI character devices");
  252. return err;
  253. }
  254. ubi_assert(MINOR(dev) == 0);
  255. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  256. dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
  257. ubi->cdev.owner = THIS_MODULE;
  258. err = cdev_add(&ubi->cdev, dev, 1);
  259. if (err) {
  260. ubi_err("cannot add character device");
  261. goto out_unreg;
  262. }
  263. err = ubi_sysfs_init(ubi);
  264. if (err)
  265. goto out_cdev;
  266. for (i = 0; i < ubi->vtbl_slots; i++)
  267. if (ubi->volumes[i]) {
  268. err = ubi_add_volume(ubi, ubi->volumes[i]);
  269. if (err) {
  270. ubi_err("cannot add volume %d", i);
  271. goto out_volumes;
  272. }
  273. }
  274. return 0;
  275. out_volumes:
  276. kill_volumes(ubi);
  277. ubi_sysfs_close(ubi);
  278. out_cdev:
  279. cdev_del(&ubi->cdev);
  280. out_unreg:
  281. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  282. ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
  283. return err;
  284. }
  285. /**
  286. * uif_close - close user interfaces for an UBI device.
  287. * @ubi: UBI device description object
  288. */
  289. static void uif_close(struct ubi_device *ubi)
  290. {
  291. kill_volumes(ubi);
  292. ubi_sysfs_close(ubi);
  293. cdev_del(&ubi->cdev);
  294. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  295. }
  296. /**
  297. * attach_by_scanning - attach an MTD device using scanning method.
  298. * @ubi: UBI device descriptor
  299. *
  300. * This function returns zero in case of success and a negative error code in
  301. * case of failure.
  302. *
  303. * Note, currently this is the only method to attach UBI devices. Hopefully in
  304. * the future we'll have more scalable attaching methods and avoid full media
  305. * scanning. But even in this case scanning will be needed as a fall-back
  306. * attaching method if there are some on-flash table corruptions.
  307. */
  308. static int attach_by_scanning(struct ubi_device *ubi)
  309. {
  310. int err;
  311. struct ubi_scan_info *si;
  312. si = ubi_scan(ubi);
  313. if (IS_ERR(si))
  314. return PTR_ERR(si);
  315. ubi->bad_peb_count = si->bad_peb_count;
  316. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  317. ubi->max_ec = si->max_ec;
  318. ubi->mean_ec = si->mean_ec;
  319. err = ubi_read_volume_table(ubi, si);
  320. if (err)
  321. goto out_si;
  322. err = ubi_wl_init_scan(ubi, si);
  323. if (err)
  324. goto out_vtbl;
  325. err = ubi_eba_init_scan(ubi, si);
  326. if (err)
  327. goto out_wl;
  328. ubi_scan_destroy_si(si);
  329. return 0;
  330. out_wl:
  331. ubi_wl_close(ubi);
  332. out_vtbl:
  333. vfree(ubi->vtbl);
  334. out_si:
  335. ubi_scan_destroy_si(si);
  336. return err;
  337. }
  338. /**
  339. * io_init - initialize I/O unit for a given UBI device.
  340. * @ubi: UBI device description object
  341. *
  342. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  343. * assumed:
  344. * o EC header is always at offset zero - this cannot be changed;
  345. * o VID header starts just after the EC header at the closest address
  346. * aligned to @io->@hdrs_min_io_size;
  347. * o data starts just after the VID header at the closest address aligned to
  348. * @io->@min_io_size
  349. *
  350. * This function returns zero in case of success and a negative error code in
  351. * case of failure.
  352. */
  353. static int io_init(struct ubi_device *ubi)
  354. {
  355. if (ubi->mtd->numeraseregions != 0) {
  356. /*
  357. * Some flashes have several erase regions. Different regions
  358. * may have different eraseblock size and other
  359. * characteristics. It looks like mostly multi-region flashes
  360. * have one "main" region and one or more small regions to
  361. * store boot loader code or boot parameters or whatever. I
  362. * guess we should just pick the largest region. But this is
  363. * not implemented.
  364. */
  365. ubi_err("multiple regions, not implemented");
  366. return -EINVAL;
  367. }
  368. /*
  369. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  370. * physical eraseblocks maximum.
  371. */
  372. ubi->peb_size = ubi->mtd->erasesize;
  373. ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
  374. ubi->flash_size = ubi->mtd->size;
  375. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  376. ubi->bad_allowed = 1;
  377. ubi->min_io_size = ubi->mtd->writesize;
  378. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  379. /* Make sure minimal I/O unit is power of 2 */
  380. if (!is_power_of_2(ubi->min_io_size)) {
  381. ubi_err("min. I/O unit (%d) is not power of 2",
  382. ubi->min_io_size);
  383. return -EINVAL;
  384. }
  385. ubi_assert(ubi->hdrs_min_io_size > 0);
  386. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  387. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  388. /* Calculate default aligned sizes of EC and VID headers */
  389. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  390. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  391. dbg_msg("min_io_size %d", ubi->min_io_size);
  392. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  393. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  394. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  395. if (ubi->vid_hdr_offset == 0)
  396. /* Default offset */
  397. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  398. ubi->ec_hdr_alsize;
  399. else {
  400. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  401. ~(ubi->hdrs_min_io_size - 1);
  402. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  403. ubi->vid_hdr_aloffset;
  404. }
  405. /* Similar for the data offset */
  406. if (ubi->leb_start == 0) {
  407. ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
  408. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  409. }
  410. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  411. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  412. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  413. dbg_msg("leb_start %d", ubi->leb_start);
  414. /* The shift must be aligned to 32-bit boundary */
  415. if (ubi->vid_hdr_shift % 4) {
  416. ubi_err("unaligned VID header shift %d",
  417. ubi->vid_hdr_shift);
  418. return -EINVAL;
  419. }
  420. /* Check sanity */
  421. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  422. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  423. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  424. ubi->leb_start % ubi->min_io_size) {
  425. ubi_err("bad VID header (%d) or data offsets (%d)",
  426. ubi->vid_hdr_offset, ubi->leb_start);
  427. return -EINVAL;
  428. }
  429. /*
  430. * It may happen that EC and VID headers are situated in one minimal
  431. * I/O unit. In this case we can only accept this UBI image in
  432. * read-only mode.
  433. */
  434. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  435. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  436. "switch to read-only mode");
  437. ubi->ro_mode = 1;
  438. }
  439. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  440. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  441. ubi_msg("MTD device %d is write-protected, attach in "
  442. "read-only mode", ubi->mtd->index);
  443. ubi->ro_mode = 1;
  444. }
  445. dbg_msg("leb_size %d", ubi->leb_size);
  446. dbg_msg("ro_mode %d", ubi->ro_mode);
  447. /*
  448. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  449. * unfortunately, MTD does not provide this information. We should loop
  450. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  451. * each physical eraseblock. So, we skip ubi->bad_peb_count
  452. * uninitialized and initialize it after scanning.
  453. */
  454. return 0;
  455. }
  456. /**
  457. * attach_mtd_dev - attach an MTD device.
  458. * @mtd_dev: MTD device name or number string
  459. * @vid_hdr_offset: VID header offset
  460. * @data_offset: data offset
  461. *
  462. * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
  463. * MTD device name, and tries to open it by this name. If it is unable to open,
  464. * it tries to convert @mtd_dev to an integer and open the MTD device by its
  465. * number. Returns zero in case of success and a negative error code in case of
  466. * failure.
  467. */
  468. static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
  469. int data_offset)
  470. {
  471. struct ubi_device *ubi;
  472. struct mtd_info *mtd;
  473. int i, err;
  474. mtd = get_mtd_device_nm(mtd_dev);
  475. if (IS_ERR(mtd)) {
  476. int mtd_num;
  477. char *endp;
  478. if (PTR_ERR(mtd) != -ENODEV)
  479. return PTR_ERR(mtd);
  480. /*
  481. * Probably this is not MTD device name but MTD device number -
  482. * check this out.
  483. */
  484. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  485. if (*endp != '\0' || mtd_dev == endp) {
  486. ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
  487. return -ENODEV;
  488. }
  489. mtd = get_mtd_device(NULL, mtd_num);
  490. if (IS_ERR(mtd))
  491. return PTR_ERR(mtd);
  492. }
  493. /* Check if we already have the same MTD device attached */
  494. for (i = 0; i < UBI_MAX_DEVICES; i++)
  495. ubi = ubi_devices[i];
  496. if (ubi && ubi->mtd->index == mtd->index) {
  497. ubi_err("mtd%d is already attached to ubi%d",
  498. mtd->index, i);
  499. err = -EINVAL;
  500. goto out_mtd;
  501. }
  502. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  503. if (!ubi) {
  504. err = -ENOMEM;
  505. goto out_mtd;
  506. }
  507. ubi->mtd = mtd;
  508. /* Search for an empty slot in the @ubi_devices array */
  509. ubi->ubi_num = -1;
  510. for (i = 0; i < UBI_MAX_DEVICES; i++)
  511. if (!ubi_devices[i]) {
  512. ubi->ubi_num = i;
  513. break;
  514. }
  515. if (ubi->ubi_num == -1) {
  516. ubi_err("only %d UBI devices may be created", UBI_MAX_DEVICES);
  517. err = -ENFILE;
  518. goto out_free;
  519. }
  520. dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
  521. ubi->mtd->index, ubi->ubi_num, vid_hdr_offset, data_offset);
  522. ubi->vid_hdr_offset = vid_hdr_offset;
  523. ubi->leb_start = data_offset;
  524. err = io_init(ubi);
  525. if (err)
  526. goto out_free;
  527. mutex_init(&ubi->buf_mutex);
  528. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  529. if (!ubi->peb_buf1)
  530. goto out_free;
  531. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  532. if (!ubi->peb_buf2)
  533. goto out_free;
  534. #ifdef CONFIG_MTD_UBI_DEBUG
  535. mutex_init(&ubi->dbg_buf_mutex);
  536. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  537. if (!ubi->dbg_peb_buf)
  538. goto out_free;
  539. #endif
  540. err = attach_by_scanning(ubi);
  541. if (err) {
  542. dbg_err("failed to attach by scanning, error %d", err);
  543. goto out_free;
  544. }
  545. err = uif_init(ubi);
  546. if (err)
  547. goto out_detach;
  548. ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi->ubi_num);
  549. ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
  550. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  551. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  552. ubi->peb_size, ubi->peb_size >> 10);
  553. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  554. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  555. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  556. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  557. ubi_msg("VID header offset: %d (aligned %d)",
  558. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  559. ubi_msg("data offset: %d", ubi->leb_start);
  560. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  561. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  562. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  563. ubi_msg("number of user volumes: %d",
  564. ubi->vol_count - UBI_INT_VOL_COUNT);
  565. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  566. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  567. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  568. ubi->beb_rsvd_pebs);
  569. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  570. /* Enable the background thread */
  571. if (!DBG_DISABLE_BGT) {
  572. ubi->thread_enabled = 1;
  573. wake_up_process(ubi->bgt_thread);
  574. }
  575. ubi_devices[ubi->ubi_num] = ubi;
  576. return 0;
  577. out_detach:
  578. ubi_eba_close(ubi);
  579. ubi_wl_close(ubi);
  580. vfree(ubi->vtbl);
  581. out_free:
  582. vfree(ubi->peb_buf1);
  583. vfree(ubi->peb_buf2);
  584. #ifdef CONFIG_MTD_UBI_DEBUG
  585. vfree(ubi->dbg_peb_buf);
  586. #endif
  587. kfree(ubi);
  588. out_mtd:
  589. put_mtd_device(mtd);
  590. return err;
  591. }
  592. /**
  593. * detach_mtd_dev - detach an MTD device.
  594. * @ubi: UBI device description object
  595. */
  596. static void detach_mtd_dev(struct ubi_device *ubi)
  597. {
  598. int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
  599. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  600. uif_close(ubi);
  601. ubi_eba_close(ubi);
  602. ubi_wl_close(ubi);
  603. vfree(ubi->vtbl);
  604. put_mtd_device(ubi->mtd);
  605. vfree(ubi->peb_buf1);
  606. vfree(ubi->peb_buf2);
  607. #ifdef CONFIG_MTD_UBI_DEBUG
  608. vfree(ubi->dbg_peb_buf);
  609. #endif
  610. kfree(ubi_devices[ubi_num]);
  611. ubi_devices[ubi_num] = NULL;
  612. ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
  613. }
  614. /**
  615. * ltree_entry_ctor - lock tree entries slab cache constructor.
  616. * @obj: the lock-tree entry to construct
  617. * @cache: the lock tree entry slab cache
  618. * @flags: constructor flags
  619. */
  620. static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
  621. {
  622. struct ubi_ltree_entry *le = obj;
  623. le->users = 0;
  624. init_rwsem(&le->mutex);
  625. }
  626. static int __init ubi_init(void)
  627. {
  628. int err, i, k;
  629. /* Ensure that EC and VID headers have correct size */
  630. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  631. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  632. if (mtd_devs > UBI_MAX_DEVICES) {
  633. printk("UBI error: too many MTD devices, maximum is %d\n",
  634. UBI_MAX_DEVICES);
  635. return -EINVAL;
  636. }
  637. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  638. if (IS_ERR(ubi_class))
  639. return PTR_ERR(ubi_class);
  640. err = class_create_file(ubi_class, &ubi_version);
  641. if (err)
  642. goto out_class;
  643. ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",
  644. sizeof(struct ubi_ltree_entry), 0,
  645. 0, &ltree_entry_ctor);
  646. if (!ubi_ltree_slab)
  647. goto out_version;
  648. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  649. sizeof(struct ubi_wl_entry),
  650. 0, 0, NULL);
  651. if (!ubi_wl_entry_slab)
  652. goto out_ltree;
  653. /* Attach MTD devices */
  654. for (i = 0; i < mtd_devs; i++) {
  655. struct mtd_dev_param *p = &mtd_dev_param[i];
  656. cond_resched();
  657. err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
  658. if (err)
  659. goto out_detach;
  660. }
  661. return 0;
  662. out_detach:
  663. for (k = 0; k < i; k++)
  664. detach_mtd_dev(ubi_devices[k]);
  665. kmem_cache_destroy(ubi_wl_entry_slab);
  666. out_ltree:
  667. kmem_cache_destroy(ubi_ltree_slab);
  668. out_version:
  669. class_remove_file(ubi_class, &ubi_version);
  670. out_class:
  671. class_destroy(ubi_class);
  672. return err;
  673. }
  674. module_init(ubi_init);
  675. static void __exit ubi_exit(void)
  676. {
  677. int i;
  678. for (i = 0; i < UBI_MAX_DEVICES; i++)
  679. if (ubi_devices[i])
  680. detach_mtd_dev(ubi_devices[i]);
  681. kmem_cache_destroy(ubi_wl_entry_slab);
  682. kmem_cache_destroy(ubi_ltree_slab);
  683. class_remove_file(ubi_class, &ubi_version);
  684. class_destroy(ubi_class);
  685. }
  686. module_exit(ubi_exit);
  687. /**
  688. * bytes_str_to_int - convert a string representing number of bytes to an
  689. * integer.
  690. * @str: the string to convert
  691. *
  692. * This function returns positive resulting integer in case of success and a
  693. * negative error code in case of failure.
  694. */
  695. static int __init bytes_str_to_int(const char *str)
  696. {
  697. char *endp;
  698. unsigned long result;
  699. result = simple_strtoul(str, &endp, 0);
  700. if (str == endp || result < 0) {
  701. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  702. return -EINVAL;
  703. }
  704. switch (*endp) {
  705. case 'G':
  706. result *= 1024;
  707. case 'M':
  708. result *= 1024;
  709. case 'K':
  710. case 'k':
  711. result *= 1024;
  712. if (endp[1] == 'i' && (endp[2] == '\0' ||
  713. endp[2] == 'B' || endp[2] == 'b'))
  714. endp += 2;
  715. case '\0':
  716. break;
  717. default:
  718. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  719. return -EINVAL;
  720. }
  721. return result;
  722. }
  723. /**
  724. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  725. * @val: the parameter value to parse
  726. * @kp: not used
  727. *
  728. * This function returns zero in case of success and a negative error code in
  729. * case of error.
  730. */
  731. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  732. {
  733. int i, len;
  734. struct mtd_dev_param *p;
  735. char buf[MTD_PARAM_LEN_MAX];
  736. char *pbuf = &buf[0];
  737. char *tokens[3] = {NULL, NULL, NULL};
  738. if (!val)
  739. return -EINVAL;
  740. if (mtd_devs == UBI_MAX_DEVICES) {
  741. printk("UBI error: too many parameters, max. is %d\n",
  742. UBI_MAX_DEVICES);
  743. return -EINVAL;
  744. }
  745. len = strnlen(val, MTD_PARAM_LEN_MAX);
  746. if (len == MTD_PARAM_LEN_MAX) {
  747. printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
  748. val, MTD_PARAM_LEN_MAX);
  749. return -EINVAL;
  750. }
  751. if (len == 0) {
  752. printk("UBI warning: empty 'mtd=' parameter - ignored\n");
  753. return 0;
  754. }
  755. strcpy(buf, val);
  756. /* Get rid of the final newline */
  757. if (buf[len - 1] == '\n')
  758. buf[len - 1] = '\0';
  759. for (i = 0; i < 3; i++)
  760. tokens[i] = strsep(&pbuf, ",");
  761. if (pbuf) {
  762. printk("UBI error: too many arguments at \"%s\"\n", val);
  763. return -EINVAL;
  764. }
  765. p = &mtd_dev_param[mtd_devs];
  766. strcpy(&p->name[0], tokens[0]);
  767. if (tokens[1])
  768. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  769. if (tokens[2])
  770. p->data_offs = bytes_str_to_int(tokens[2]);
  771. if (p->vid_hdr_offs < 0)
  772. return p->vid_hdr_offs;
  773. if (p->data_offs < 0)
  774. return p->data_offs;
  775. mtd_devs += 1;
  776. return 0;
  777. }
  778. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  779. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  780. "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
  781. "Multiple \"mtd\" parameters may be specified.\n"
  782. "MTD devices may be specified by their number or name. "
  783. "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
  784. "specify UBI VID header position and data starting "
  785. "position to be used by UBI.\n"
  786. "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
  787. "with name content using VID header offset 1984 and data "
  788. "start 2048, and MTD device number 4 using default "
  789. "offsets");
  790. MODULE_VERSION(__stringify(UBI_VERSION));
  791. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  792. MODULE_AUTHOR("Artem Bityutskiy");
  793. MODULE_LICENSE("GPL");