build.c 24 KB

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