build.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  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 = MKDEV(ubi->major, 0);
  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. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  252. ubi->major = MAJOR(dev);
  253. dbg_msg("%s major is %u", ubi->ubi_name, ubi->major);
  254. ubi->cdev.owner = THIS_MODULE;
  255. dev = MKDEV(ubi->major, 0);
  256. err = cdev_add(&ubi->cdev, dev, 1);
  257. if (err) {
  258. ubi_err("cannot add character device %s", ubi->ubi_name);
  259. goto out_unreg;
  260. }
  261. err = ubi_sysfs_init(ubi);
  262. if (err)
  263. goto out_cdev;
  264. for (i = 0; i < ubi->vtbl_slots; i++)
  265. if (ubi->volumes[i]) {
  266. err = ubi_add_volume(ubi, i);
  267. if (err)
  268. goto out_volumes;
  269. }
  270. return 0;
  271. out_volumes:
  272. kill_volumes(ubi);
  273. ubi_sysfs_close(ubi);
  274. out_cdev:
  275. cdev_del(&ubi->cdev);
  276. out_unreg:
  277. unregister_chrdev_region(MKDEV(ubi->major, 0),
  278. ubi->vtbl_slots + 1);
  279. return err;
  280. }
  281. /**
  282. * uif_close - close user interfaces for an UBI device.
  283. * @ubi: UBI device description object
  284. */
  285. static void uif_close(struct ubi_device *ubi)
  286. {
  287. kill_volumes(ubi);
  288. ubi_sysfs_close(ubi);
  289. cdev_del(&ubi->cdev);
  290. unregister_chrdev_region(MKDEV(ubi->major, 0), ubi->vtbl_slots + 1);
  291. }
  292. /**
  293. * attach_by_scanning - attach an MTD device using scanning method.
  294. * @ubi: UBI device descriptor
  295. *
  296. * This function returns zero in case of success and a negative error code in
  297. * case of failure.
  298. *
  299. * Note, currently this is the only method to attach UBI devices. Hopefully in
  300. * the future we'll have more scalable attaching methods and avoid full media
  301. * scanning. But even in this case scanning will be needed as a fall-back
  302. * attaching method if there are some on-flash table corruptions.
  303. */
  304. static int attach_by_scanning(struct ubi_device *ubi)
  305. {
  306. int err;
  307. struct ubi_scan_info *si;
  308. si = ubi_scan(ubi);
  309. if (IS_ERR(si))
  310. return PTR_ERR(si);
  311. ubi->bad_peb_count = si->bad_peb_count;
  312. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  313. ubi->max_ec = si->max_ec;
  314. ubi->mean_ec = si->mean_ec;
  315. err = ubi_read_volume_table(ubi, si);
  316. if (err)
  317. goto out_si;
  318. err = ubi_wl_init_scan(ubi, si);
  319. if (err)
  320. goto out_vtbl;
  321. err = ubi_eba_init_scan(ubi, si);
  322. if (err)
  323. goto out_wl;
  324. ubi_scan_destroy_si(si);
  325. return 0;
  326. out_wl:
  327. ubi_wl_close(ubi);
  328. out_vtbl:
  329. vfree(ubi->vtbl);
  330. out_si:
  331. ubi_scan_destroy_si(si);
  332. return err;
  333. }
  334. /**
  335. * io_init - initialize I/O unit for a given UBI device.
  336. * @ubi: UBI device description object
  337. *
  338. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  339. * assumed:
  340. * o EC header is always at offset zero - this cannot be changed;
  341. * o VID header starts just after the EC header at the closest address
  342. * aligned to @io->@hdrs_min_io_size;
  343. * o data starts just after the VID header at the closest address aligned to
  344. * @io->@min_io_size
  345. *
  346. * This function returns zero in case of success and a negative error code in
  347. * case of failure.
  348. */
  349. static int io_init(struct ubi_device *ubi)
  350. {
  351. if (ubi->mtd->numeraseregions != 0) {
  352. /*
  353. * Some flashes have several erase regions. Different regions
  354. * may have different eraseblock size and other
  355. * characteristics. It looks like mostly multi-region flashes
  356. * have one "main" region and one or more small regions to
  357. * store boot loader code or boot parameters or whatever. I
  358. * guess we should just pick the largest region. But this is
  359. * not implemented.
  360. */
  361. ubi_err("multiple regions, not implemented");
  362. return -EINVAL;
  363. }
  364. /*
  365. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  366. * physical eraseblocks maximum.
  367. */
  368. ubi->peb_size = ubi->mtd->erasesize;
  369. ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
  370. ubi->flash_size = ubi->mtd->size;
  371. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  372. ubi->bad_allowed = 1;
  373. ubi->min_io_size = ubi->mtd->writesize;
  374. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  375. /* Make sure minimal I/O unit is power of 2 */
  376. if (!is_power_of_2(ubi->min_io_size)) {
  377. ubi_err("bad min. I/O unit");
  378. return -EINVAL;
  379. }
  380. ubi_assert(ubi->hdrs_min_io_size > 0);
  381. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  382. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  383. /* Calculate default aligned sizes of EC and VID headers */
  384. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  385. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  386. dbg_msg("min_io_size %d", ubi->min_io_size);
  387. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  388. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  389. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  390. if (ubi->vid_hdr_offset == 0)
  391. /* Default offset */
  392. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  393. ubi->ec_hdr_alsize;
  394. else {
  395. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  396. ~(ubi->hdrs_min_io_size - 1);
  397. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  398. ubi->vid_hdr_aloffset;
  399. }
  400. /* Similar for the data offset */
  401. if (ubi->leb_start == 0) {
  402. ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
  403. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  404. }
  405. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  406. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  407. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  408. dbg_msg("leb_start %d", ubi->leb_start);
  409. /* The shift must be aligned to 32-bit boundary */
  410. if (ubi->vid_hdr_shift % 4) {
  411. ubi_err("unaligned VID header shift %d",
  412. ubi->vid_hdr_shift);
  413. return -EINVAL;
  414. }
  415. /* Check sanity */
  416. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  417. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  418. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  419. ubi->leb_start % ubi->min_io_size) {
  420. ubi_err("bad VID header (%d) or data offsets (%d)",
  421. ubi->vid_hdr_offset, ubi->leb_start);
  422. return -EINVAL;
  423. }
  424. /*
  425. * It may happen that EC and VID headers are situated in one minimal
  426. * I/O unit. In this case we can only accept this UBI image in
  427. * read-only mode.
  428. */
  429. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  430. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  431. "switch to read-only mode");
  432. ubi->ro_mode = 1;
  433. }
  434. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  435. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  436. ubi_msg("MTD device %d is write-protected, attach in "
  437. "read-only mode", ubi->mtd->index);
  438. ubi->ro_mode = 1;
  439. }
  440. dbg_msg("leb_size %d", ubi->leb_size);
  441. dbg_msg("ro_mode %d", ubi->ro_mode);
  442. /*
  443. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  444. * unfortunately, MTD does not provide this information. We should loop
  445. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  446. * each physical eraseblock. So, we skip ubi->bad_peb_count
  447. * uninitialized and initialize it after scanning.
  448. */
  449. return 0;
  450. }
  451. /**
  452. * attach_mtd_dev - attach an MTD device.
  453. * @mtd_dev: MTD device name or number string
  454. * @vid_hdr_offset: VID header offset
  455. * @data_offset: data offset
  456. *
  457. * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
  458. * MTD device name, and tries to open it by this name. If it is unable to open,
  459. * it tries to convert @mtd_dev to an integer and open the MTD device by its
  460. * number. Returns zero in case of success and a negative error code in case of
  461. * failure.
  462. */
  463. static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
  464. int data_offset)
  465. {
  466. struct ubi_device *ubi;
  467. struct mtd_info *mtd;
  468. int i, err;
  469. mtd = get_mtd_device_nm(mtd_dev);
  470. if (IS_ERR(mtd)) {
  471. int mtd_num;
  472. char *endp;
  473. if (PTR_ERR(mtd) != -ENODEV)
  474. return PTR_ERR(mtd);
  475. /*
  476. * Probably this is not MTD device name but MTD device number -
  477. * check this out.
  478. */
  479. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  480. if (*endp != '\0' || mtd_dev == endp) {
  481. ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
  482. return -ENODEV;
  483. }
  484. mtd = get_mtd_device(NULL, mtd_num);
  485. if (IS_ERR(mtd))
  486. return PTR_ERR(mtd);
  487. }
  488. /* Check if we already have the same MTD device attached */
  489. for (i = 0; i < ubi_devices_cnt; i++)
  490. if (ubi_devices[i]->mtd->index == mtd->index) {
  491. ubi_err("mtd%d is already attached to ubi%d",
  492. mtd->index, i);
  493. err = -EINVAL;
  494. goto out_mtd;
  495. }
  496. ubi = ubi_devices[ubi_devices_cnt] = kzalloc(sizeof(struct ubi_device),
  497. GFP_KERNEL);
  498. if (!ubi) {
  499. err = -ENOMEM;
  500. goto out_mtd;
  501. }
  502. ubi->ubi_num = ubi_devices_cnt;
  503. ubi->mtd = mtd;
  504. dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
  505. ubi->mtd->index, ubi_devices_cnt, vid_hdr_offset, data_offset);
  506. ubi->vid_hdr_offset = vid_hdr_offset;
  507. ubi->leb_start = data_offset;
  508. err = io_init(ubi);
  509. if (err)
  510. goto out_free;
  511. mutex_init(&ubi->buf_mutex);
  512. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  513. if (!ubi->peb_buf1)
  514. goto out_free;
  515. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  516. if (!ubi->peb_buf2)
  517. goto out_free;
  518. #ifdef CONFIG_MTD_UBI_DEBUG
  519. mutex_init(&ubi->dbg_buf_mutex);
  520. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  521. if (!ubi->dbg_peb_buf)
  522. goto out_free;
  523. #endif
  524. err = attach_by_scanning(ubi);
  525. if (err) {
  526. dbg_err("failed to attach by scanning, error %d", err);
  527. goto out_free;
  528. }
  529. err = uif_init(ubi);
  530. if (err)
  531. goto out_detach;
  532. ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt);
  533. ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
  534. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  535. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  536. ubi->peb_size, ubi->peb_size >> 10);
  537. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  538. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  539. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  540. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  541. ubi_msg("VID header offset: %d (aligned %d)",
  542. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  543. ubi_msg("data offset: %d", ubi->leb_start);
  544. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  545. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  546. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  547. ubi_msg("number of user volumes: %d",
  548. ubi->vol_count - UBI_INT_VOL_COUNT);
  549. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  550. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  551. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  552. ubi->beb_rsvd_pebs);
  553. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  554. /* Enable the background thread */
  555. if (!DBG_DISABLE_BGT) {
  556. ubi->thread_enabled = 1;
  557. wake_up_process(ubi->bgt_thread);
  558. }
  559. ubi_devices_cnt += 1;
  560. return 0;
  561. out_detach:
  562. ubi_eba_close(ubi);
  563. ubi_wl_close(ubi);
  564. vfree(ubi->vtbl);
  565. out_free:
  566. vfree(ubi->peb_buf1);
  567. vfree(ubi->peb_buf2);
  568. #ifdef CONFIG_MTD_UBI_DEBUG
  569. vfree(ubi->dbg_peb_buf);
  570. #endif
  571. kfree(ubi);
  572. out_mtd:
  573. put_mtd_device(mtd);
  574. ubi_devices[ubi_devices_cnt] = NULL;
  575. return err;
  576. }
  577. /**
  578. * detach_mtd_dev - detach an MTD device.
  579. * @ubi: UBI device description object
  580. */
  581. static void detach_mtd_dev(struct ubi_device *ubi)
  582. {
  583. int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
  584. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  585. uif_close(ubi);
  586. ubi_eba_close(ubi);
  587. ubi_wl_close(ubi);
  588. vfree(ubi->vtbl);
  589. put_mtd_device(ubi->mtd);
  590. vfree(ubi->peb_buf1);
  591. vfree(ubi->peb_buf2);
  592. #ifdef CONFIG_MTD_UBI_DEBUG
  593. vfree(ubi->dbg_peb_buf);
  594. #endif
  595. kfree(ubi_devices[ubi_num]);
  596. ubi_devices[ubi_num] = NULL;
  597. ubi_devices_cnt -= 1;
  598. ubi_assert(ubi_devices_cnt >= 0);
  599. ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
  600. }
  601. static int __init ubi_init(void)
  602. {
  603. int err, i, k;
  604. /* Ensure that EC and VID headers have correct size */
  605. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  606. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  607. if (mtd_devs > UBI_MAX_DEVICES) {
  608. printk("UBI error: too many MTD devices, maximum is %d\n",
  609. UBI_MAX_DEVICES);
  610. return -EINVAL;
  611. }
  612. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  613. if (IS_ERR(ubi_class))
  614. return PTR_ERR(ubi_class);
  615. err = class_create_file(ubi_class, &ubi_version);
  616. if (err)
  617. goto out_class;
  618. /* Attach MTD devices */
  619. for (i = 0; i < mtd_devs; i++) {
  620. struct mtd_dev_param *p = &mtd_dev_param[i];
  621. cond_resched();
  622. err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
  623. if (err)
  624. goto out_detach;
  625. }
  626. return 0;
  627. out_detach:
  628. for (k = 0; k < i; k++)
  629. detach_mtd_dev(ubi_devices[k]);
  630. class_remove_file(ubi_class, &ubi_version);
  631. out_class:
  632. class_destroy(ubi_class);
  633. return err;
  634. }
  635. module_init(ubi_init);
  636. static void __exit ubi_exit(void)
  637. {
  638. int i, n = ubi_devices_cnt;
  639. for (i = 0; i < n; i++)
  640. detach_mtd_dev(ubi_devices[i]);
  641. class_remove_file(ubi_class, &ubi_version);
  642. class_destroy(ubi_class);
  643. }
  644. module_exit(ubi_exit);
  645. /**
  646. * bytes_str_to_int - convert a string representing number of bytes to an
  647. * integer.
  648. * @str: the string to convert
  649. *
  650. * This function returns positive resulting integer in case of success and a
  651. * negative error code in case of failure.
  652. */
  653. static int __init bytes_str_to_int(const char *str)
  654. {
  655. char *endp;
  656. unsigned long result;
  657. result = simple_strtoul(str, &endp, 0);
  658. if (str == endp || result < 0) {
  659. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  660. return -EINVAL;
  661. }
  662. switch (*endp) {
  663. case 'G':
  664. result *= 1024;
  665. case 'M':
  666. result *= 1024;
  667. case 'K':
  668. case 'k':
  669. result *= 1024;
  670. if (endp[1] == 'i' && (endp[2] == '\0' ||
  671. endp[2] == 'B' || endp[2] == 'b'))
  672. endp += 2;
  673. case '\0':
  674. break;
  675. default:
  676. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  677. return -EINVAL;
  678. }
  679. return result;
  680. }
  681. /**
  682. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  683. * @val: the parameter value to parse
  684. * @kp: not used
  685. *
  686. * This function returns zero in case of success and a negative error code in
  687. * case of error.
  688. */
  689. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  690. {
  691. int i, len;
  692. struct mtd_dev_param *p;
  693. char buf[MTD_PARAM_LEN_MAX];
  694. char *pbuf = &buf[0];
  695. char *tokens[3] = {NULL, NULL, NULL};
  696. if (mtd_devs == UBI_MAX_DEVICES) {
  697. printk("UBI error: too many parameters, max. is %d\n",
  698. UBI_MAX_DEVICES);
  699. return -EINVAL;
  700. }
  701. len = strnlen(val, MTD_PARAM_LEN_MAX);
  702. if (len == MTD_PARAM_LEN_MAX) {
  703. printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
  704. val, MTD_PARAM_LEN_MAX);
  705. return -EINVAL;
  706. }
  707. if (len == 0) {
  708. printk("UBI warning: empty 'mtd=' parameter - ignored\n");
  709. return 0;
  710. }
  711. strcpy(buf, val);
  712. /* Get rid of the final newline */
  713. if (buf[len - 1] == '\n')
  714. buf[len - 1] = '\0';
  715. for (i = 0; i < 3; i++)
  716. tokens[i] = strsep(&pbuf, ",");
  717. if (pbuf) {
  718. printk("UBI error: too many arguments at \"%s\"\n", val);
  719. return -EINVAL;
  720. }
  721. p = &mtd_dev_param[mtd_devs];
  722. strcpy(&p->name[0], tokens[0]);
  723. if (tokens[1])
  724. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  725. if (tokens[2])
  726. p->data_offs = bytes_str_to_int(tokens[2]);
  727. if (p->vid_hdr_offs < 0)
  728. return p->vid_hdr_offs;
  729. if (p->data_offs < 0)
  730. return p->data_offs;
  731. mtd_devs += 1;
  732. return 0;
  733. }
  734. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  735. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  736. "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
  737. "Multiple \"mtd\" parameters may be specified.\n"
  738. "MTD devices may be specified by their number or name. "
  739. "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
  740. "specify UBI VID header position and data starting "
  741. "position to be used by UBI.\n"
  742. "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
  743. "with name content using VID header offset 1984 and data "
  744. "start 2048, and MTD device number 4 using default "
  745. "offsets");
  746. MODULE_VERSION(__stringify(UBI_VERSION));
  747. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  748. MODULE_AUTHOR("Artem Bityutskiy");
  749. MODULE_LICENSE("GPL");