build.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212
  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.
  24. *
  25. * When UBI is initialized, it attaches all the MTD devices specified as the
  26. * module load parameters or the kernel boot parameters. If MTD devices were
  27. * specified, UBI does not attach any MTD device, but it is possible to do
  28. * later using the "UBI control device".
  29. *
  30. * At the moment we only attach UBI devices by scanning, which will become a
  31. * bottleneck when flashes reach certain large size. Then one may improve UBI
  32. * and add other methods, although it does not seem to be easy to do.
  33. */
  34. #include <linux/err.h>
  35. #include <linux/module.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/stringify.h>
  38. #include <linux/stat.h>
  39. #include <linux/miscdevice.h>
  40. #include <linux/log2.h>
  41. #include <linux/kthread.h>
  42. #include "ubi.h"
  43. /* Maximum length of the 'mtd=' parameter */
  44. #define MTD_PARAM_LEN_MAX 64
  45. /**
  46. * struct mtd_dev_param - MTD device parameter description data structure.
  47. * @name: MTD device name or number string
  48. * @vid_hdr_offs: VID header offset
  49. */
  50. struct mtd_dev_param {
  51. char name[MTD_PARAM_LEN_MAX];
  52. int vid_hdr_offs;
  53. };
  54. /* Numbers of elements set in the @mtd_dev_param array */
  55. static int mtd_devs;
  56. /* MTD devices specification parameters */
  57. static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
  58. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  59. struct class *ubi_class;
  60. /* Slab cache for wear-leveling entries */
  61. struct kmem_cache *ubi_wl_entry_slab;
  62. /* UBI control character device */
  63. static struct miscdevice ubi_ctrl_cdev = {
  64. .minor = MISC_DYNAMIC_MINOR,
  65. .name = "ubi_ctrl",
  66. .fops = &ubi_ctrl_cdev_operations,
  67. };
  68. /* All UBI devices in system */
  69. static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  70. /* Serializes UBI devices creations and removals */
  71. DEFINE_MUTEX(ubi_devices_mutex);
  72. /* Protects @ubi_devices and @ubi->ref_count */
  73. static DEFINE_SPINLOCK(ubi_devices_lock);
  74. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  75. static ssize_t ubi_version_show(struct class *class, char *buf)
  76. {
  77. return sprintf(buf, "%d\n", UBI_VERSION);
  78. }
  79. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  80. static struct class_attribute ubi_version =
  81. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  82. static ssize_t dev_attribute_show(struct device *dev,
  83. struct device_attribute *attr, char *buf);
  84. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  85. static struct device_attribute dev_eraseblock_size =
  86. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  87. static struct device_attribute dev_avail_eraseblocks =
  88. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  89. static struct device_attribute dev_total_eraseblocks =
  90. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  91. static struct device_attribute dev_volumes_count =
  92. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  93. static struct device_attribute dev_max_ec =
  94. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  95. static struct device_attribute dev_reserved_for_bad =
  96. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  97. static struct device_attribute dev_bad_peb_count =
  98. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  99. static struct device_attribute dev_max_vol_count =
  100. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  101. static struct device_attribute dev_min_io_size =
  102. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  103. static struct device_attribute dev_bgt_enabled =
  104. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  105. static struct device_attribute dev_mtd_num =
  106. __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
  107. /**
  108. * ubi_get_device - get UBI device.
  109. * @ubi_num: UBI device number
  110. *
  111. * This function returns UBI device description object for UBI device number
  112. * @ubi_num, or %NULL if the device does not exist. This function increases the
  113. * device reference count to prevent removal of the device. In other words, the
  114. * device cannot be removed if its reference count is not zero.
  115. */
  116. struct ubi_device *ubi_get_device(int ubi_num)
  117. {
  118. struct ubi_device *ubi;
  119. spin_lock(&ubi_devices_lock);
  120. ubi = ubi_devices[ubi_num];
  121. if (ubi) {
  122. ubi_assert(ubi->ref_count >= 0);
  123. ubi->ref_count += 1;
  124. get_device(&ubi->dev);
  125. }
  126. spin_unlock(&ubi_devices_lock);
  127. return ubi;
  128. }
  129. /**
  130. * ubi_put_device - drop an UBI device reference.
  131. * @ubi: UBI device description object
  132. */
  133. void ubi_put_device(struct ubi_device *ubi)
  134. {
  135. spin_lock(&ubi_devices_lock);
  136. ubi->ref_count -= 1;
  137. put_device(&ubi->dev);
  138. spin_unlock(&ubi_devices_lock);
  139. }
  140. /**
  141. * ubi_get_by_major - get UBI device by character device major number.
  142. * @major: major number
  143. *
  144. * This function is similar to 'ubi_get_device()', but it searches the device
  145. * by its major number.
  146. */
  147. struct ubi_device *ubi_get_by_major(int major)
  148. {
  149. int i;
  150. struct ubi_device *ubi;
  151. spin_lock(&ubi_devices_lock);
  152. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  153. ubi = ubi_devices[i];
  154. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  155. ubi_assert(ubi->ref_count >= 0);
  156. ubi->ref_count += 1;
  157. get_device(&ubi->dev);
  158. spin_unlock(&ubi_devices_lock);
  159. return ubi;
  160. }
  161. }
  162. spin_unlock(&ubi_devices_lock);
  163. return NULL;
  164. }
  165. /**
  166. * ubi_major2num - get UBI device number by character device major number.
  167. * @major: major number
  168. *
  169. * This function searches UBI device number object by its major number. If UBI
  170. * device was not found, this function returns -ENODEV, otherwise the UBI device
  171. * number is returned.
  172. */
  173. int ubi_major2num(int major)
  174. {
  175. int i, ubi_num = -ENODEV;
  176. spin_lock(&ubi_devices_lock);
  177. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  178. struct ubi_device *ubi = ubi_devices[i];
  179. if (ubi && MAJOR(ubi->cdev.dev) == major) {
  180. ubi_num = ubi->ubi_num;
  181. break;
  182. }
  183. }
  184. spin_unlock(&ubi_devices_lock);
  185. return ubi_num;
  186. }
  187. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  188. static ssize_t dev_attribute_show(struct device *dev,
  189. struct device_attribute *attr, char *buf)
  190. {
  191. ssize_t ret;
  192. struct ubi_device *ubi;
  193. /*
  194. * The below code looks weird, but it actually makes sense. We get the
  195. * UBI device reference from the contained 'struct ubi_device'. But it
  196. * is unclear if the device was removed or not yet. Indeed, if the
  197. * device was removed before we increased its reference count,
  198. * 'ubi_get_device()' will return -ENODEV and we fail.
  199. *
  200. * Remember, 'struct ubi_device' is freed in the release function, so
  201. * we still can use 'ubi->ubi_num'.
  202. */
  203. ubi = container_of(dev, struct ubi_device, dev);
  204. ubi = ubi_get_device(ubi->ubi_num);
  205. if (!ubi)
  206. return -ENODEV;
  207. if (attr == &dev_eraseblock_size)
  208. ret = sprintf(buf, "%d\n", ubi->leb_size);
  209. else if (attr == &dev_avail_eraseblocks)
  210. ret = sprintf(buf, "%d\n", ubi->avail_pebs);
  211. else if (attr == &dev_total_eraseblocks)
  212. ret = sprintf(buf, "%d\n", ubi->good_peb_count);
  213. else if (attr == &dev_volumes_count)
  214. ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
  215. else if (attr == &dev_max_ec)
  216. ret = sprintf(buf, "%d\n", ubi->max_ec);
  217. else if (attr == &dev_reserved_for_bad)
  218. ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  219. else if (attr == &dev_bad_peb_count)
  220. ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
  221. else if (attr == &dev_max_vol_count)
  222. ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
  223. else if (attr == &dev_min_io_size)
  224. ret = sprintf(buf, "%d\n", ubi->min_io_size);
  225. else if (attr == &dev_bgt_enabled)
  226. ret = sprintf(buf, "%d\n", ubi->thread_enabled);
  227. else if (attr == &dev_mtd_num)
  228. ret = sprintf(buf, "%d\n", ubi->mtd->index);
  229. else
  230. ret = -EINVAL;
  231. ubi_put_device(ubi);
  232. return ret;
  233. }
  234. /* Fake "release" method for UBI devices */
  235. static void dev_release(struct device *dev) { }
  236. /**
  237. * ubi_sysfs_init - initialize sysfs for an UBI device.
  238. * @ubi: UBI device description object
  239. *
  240. * This function returns zero in case of success and a negative error code in
  241. * case of failure.
  242. */
  243. static int ubi_sysfs_init(struct ubi_device *ubi)
  244. {
  245. int err;
  246. ubi->dev.release = dev_release;
  247. ubi->dev.devt = ubi->cdev.dev;
  248. ubi->dev.class = ubi_class;
  249. dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
  250. err = device_register(&ubi->dev);
  251. if (err)
  252. return err;
  253. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  254. if (err)
  255. return err;
  256. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  257. if (err)
  258. return err;
  259. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  260. if (err)
  261. return err;
  262. err = device_create_file(&ubi->dev, &dev_volumes_count);
  263. if (err)
  264. return err;
  265. err = device_create_file(&ubi->dev, &dev_max_ec);
  266. if (err)
  267. return err;
  268. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  269. if (err)
  270. return err;
  271. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  272. if (err)
  273. return err;
  274. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  275. if (err)
  276. return err;
  277. err = device_create_file(&ubi->dev, &dev_min_io_size);
  278. if (err)
  279. return err;
  280. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  281. if (err)
  282. return err;
  283. err = device_create_file(&ubi->dev, &dev_mtd_num);
  284. return err;
  285. }
  286. /**
  287. * ubi_sysfs_close - close sysfs for an UBI device.
  288. * @ubi: UBI device description object
  289. */
  290. static void ubi_sysfs_close(struct ubi_device *ubi)
  291. {
  292. device_remove_file(&ubi->dev, &dev_mtd_num);
  293. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  294. device_remove_file(&ubi->dev, &dev_min_io_size);
  295. device_remove_file(&ubi->dev, &dev_max_vol_count);
  296. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  297. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  298. device_remove_file(&ubi->dev, &dev_max_ec);
  299. device_remove_file(&ubi->dev, &dev_volumes_count);
  300. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  301. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  302. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  303. device_unregister(&ubi->dev);
  304. }
  305. /**
  306. * kill_volumes - destroy all volumes.
  307. * @ubi: UBI device description object
  308. */
  309. static void kill_volumes(struct ubi_device *ubi)
  310. {
  311. int i;
  312. for (i = 0; i < ubi->vtbl_slots; i++)
  313. if (ubi->volumes[i])
  314. ubi_free_volume(ubi, ubi->volumes[i]);
  315. }
  316. /**
  317. * free_user_volumes - free all user volumes.
  318. * @ubi: UBI device description object
  319. *
  320. * Normally the volumes are freed at the release function of the volume device
  321. * objects. However, on error paths the volumes have to be freed before the
  322. * device objects have been initialized.
  323. */
  324. static void free_user_volumes(struct ubi_device *ubi)
  325. {
  326. int i;
  327. for (i = 0; i < ubi->vtbl_slots; i++)
  328. if (ubi->volumes[i]) {
  329. kfree(ubi->volumes[i]->eba_tbl);
  330. kfree(ubi->volumes[i]);
  331. }
  332. }
  333. /**
  334. * uif_init - initialize user interfaces for an UBI device.
  335. * @ubi: UBI device description object
  336. *
  337. * This function returns zero in case of success and a negative error code in
  338. * case of failure. Note, this function destroys all volumes if it failes.
  339. */
  340. static int uif_init(struct ubi_device *ubi)
  341. {
  342. int i, err;
  343. dev_t dev;
  344. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  345. /*
  346. * Major numbers for the UBI character devices are allocated
  347. * dynamically. Major numbers of volume character devices are
  348. * equivalent to ones of the corresponding UBI character device. Minor
  349. * numbers of UBI character devices are 0, while minor numbers of
  350. * volume character devices start from 1. Thus, we allocate one major
  351. * number and ubi->vtbl_slots + 1 minor numbers.
  352. */
  353. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  354. if (err) {
  355. ubi_err("cannot register UBI character devices");
  356. return err;
  357. }
  358. ubi_assert(MINOR(dev) == 0);
  359. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  360. dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
  361. ubi->cdev.owner = THIS_MODULE;
  362. err = cdev_add(&ubi->cdev, dev, 1);
  363. if (err) {
  364. ubi_err("cannot add character device");
  365. goto out_unreg;
  366. }
  367. err = ubi_sysfs_init(ubi);
  368. if (err)
  369. goto out_sysfs;
  370. for (i = 0; i < ubi->vtbl_slots; i++)
  371. if (ubi->volumes[i]) {
  372. err = ubi_add_volume(ubi, ubi->volumes[i]);
  373. if (err) {
  374. ubi_err("cannot add volume %d", i);
  375. goto out_volumes;
  376. }
  377. }
  378. return 0;
  379. out_volumes:
  380. kill_volumes(ubi);
  381. out_sysfs:
  382. ubi_sysfs_close(ubi);
  383. cdev_del(&ubi->cdev);
  384. out_unreg:
  385. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  386. ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
  387. return err;
  388. }
  389. /**
  390. * uif_close - close user interfaces for an UBI device.
  391. * @ubi: UBI device description object
  392. *
  393. * Note, since this function un-registers UBI volume device objects (@vol->dev),
  394. * the memory allocated voe the volumes is freed as well (in the release
  395. * function).
  396. */
  397. static void uif_close(struct ubi_device *ubi)
  398. {
  399. kill_volumes(ubi);
  400. ubi_sysfs_close(ubi);
  401. cdev_del(&ubi->cdev);
  402. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  403. }
  404. /**
  405. * free_internal_volumes - free internal volumes.
  406. * @ubi: UBI device description object
  407. */
  408. static void free_internal_volumes(struct ubi_device *ubi)
  409. {
  410. int i;
  411. for (i = ubi->vtbl_slots;
  412. i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  413. kfree(ubi->volumes[i]->eba_tbl);
  414. kfree(ubi->volumes[i]);
  415. }
  416. }
  417. /**
  418. * attach_by_scanning - attach an MTD device using scanning method.
  419. * @ubi: UBI device descriptor
  420. *
  421. * This function returns zero in case of success and a negative error code in
  422. * case of failure.
  423. *
  424. * Note, currently this is the only method to attach UBI devices. Hopefully in
  425. * the future we'll have more scalable attaching methods and avoid full media
  426. * scanning. But even in this case scanning will be needed as a fall-back
  427. * attaching method if there are some on-flash table corruptions.
  428. */
  429. static int attach_by_scanning(struct ubi_device *ubi)
  430. {
  431. int err;
  432. struct ubi_scan_info *si;
  433. si = ubi_scan(ubi);
  434. if (IS_ERR(si))
  435. return PTR_ERR(si);
  436. ubi->bad_peb_count = si->bad_peb_count;
  437. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  438. ubi->max_ec = si->max_ec;
  439. ubi->mean_ec = si->mean_ec;
  440. err = ubi_read_volume_table(ubi, si);
  441. if (err)
  442. goto out_si;
  443. err = ubi_wl_init_scan(ubi, si);
  444. if (err)
  445. goto out_vtbl;
  446. err = ubi_eba_init_scan(ubi, si);
  447. if (err)
  448. goto out_wl;
  449. ubi_scan_destroy_si(si);
  450. return 0;
  451. out_wl:
  452. ubi_wl_close(ubi);
  453. out_vtbl:
  454. free_internal_volumes(ubi);
  455. vfree(ubi->vtbl);
  456. out_si:
  457. ubi_scan_destroy_si(si);
  458. return err;
  459. }
  460. /**
  461. * io_init - initialize I/O sub-system for a given UBI device.
  462. * @ubi: UBI device description object
  463. *
  464. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  465. * assumed:
  466. * o EC header is always at offset zero - this cannot be changed;
  467. * o VID header starts just after the EC header at the closest address
  468. * aligned to @io->hdrs_min_io_size;
  469. * o data starts just after the VID header at the closest address aligned to
  470. * @io->min_io_size
  471. *
  472. * This function returns zero in case of success and a negative error code in
  473. * case of failure.
  474. */
  475. static int io_init(struct ubi_device *ubi)
  476. {
  477. if (ubi->mtd->numeraseregions != 0) {
  478. /*
  479. * Some flashes have several erase regions. Different regions
  480. * may have different eraseblock size and other
  481. * characteristics. It looks like mostly multi-region flashes
  482. * have one "main" region and one or more small regions to
  483. * store boot loader code or boot parameters or whatever. I
  484. * guess we should just pick the largest region. But this is
  485. * not implemented.
  486. */
  487. ubi_err("multiple regions, not implemented");
  488. return -EINVAL;
  489. }
  490. if (ubi->vid_hdr_offset < 0)
  491. return -EINVAL;
  492. /*
  493. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  494. * physical eraseblocks maximum.
  495. */
  496. ubi->peb_size = ubi->mtd->erasesize;
  497. ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
  498. ubi->flash_size = ubi->mtd->size;
  499. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  500. ubi->bad_allowed = 1;
  501. ubi->min_io_size = ubi->mtd->writesize;
  502. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  503. /*
  504. * Make sure minimal I/O unit is power of 2. Note, there is no
  505. * fundamental reason for this assumption. It is just an optimization
  506. * which allows us to avoid costly division operations.
  507. */
  508. if (!is_power_of_2(ubi->min_io_size)) {
  509. ubi_err("min. I/O unit (%d) is not power of 2",
  510. ubi->min_io_size);
  511. return -EINVAL;
  512. }
  513. ubi_assert(ubi->hdrs_min_io_size > 0);
  514. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  515. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  516. /* Calculate default aligned sizes of EC and VID headers */
  517. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  518. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  519. dbg_msg("min_io_size %d", ubi->min_io_size);
  520. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  521. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  522. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  523. if (ubi->vid_hdr_offset == 0)
  524. /* Default offset */
  525. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  526. ubi->ec_hdr_alsize;
  527. else {
  528. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  529. ~(ubi->hdrs_min_io_size - 1);
  530. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  531. ubi->vid_hdr_aloffset;
  532. }
  533. /* Similar for the data offset */
  534. ubi->leb_start = ubi->vid_hdr_offset + UBI_EC_HDR_SIZE;
  535. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  536. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  537. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  538. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  539. dbg_msg("leb_start %d", ubi->leb_start);
  540. /* The shift must be aligned to 32-bit boundary */
  541. if (ubi->vid_hdr_shift % 4) {
  542. ubi_err("unaligned VID header shift %d",
  543. ubi->vid_hdr_shift);
  544. return -EINVAL;
  545. }
  546. /* Check sanity */
  547. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  548. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  549. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  550. ubi->leb_start & (ubi->min_io_size - 1)) {
  551. ubi_err("bad VID header (%d) or data offsets (%d)",
  552. ubi->vid_hdr_offset, ubi->leb_start);
  553. return -EINVAL;
  554. }
  555. /*
  556. * It may happen that EC and VID headers are situated in one minimal
  557. * I/O unit. In this case we can only accept this UBI image in
  558. * read-only mode.
  559. */
  560. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  561. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  562. "switch to read-only mode");
  563. ubi->ro_mode = 1;
  564. }
  565. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  566. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  567. ubi_msg("MTD device %d is write-protected, attach in "
  568. "read-only mode", ubi->mtd->index);
  569. ubi->ro_mode = 1;
  570. }
  571. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  572. ubi->peb_size, ubi->peb_size >> 10);
  573. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  574. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  575. if (ubi->hdrs_min_io_size != ubi->min_io_size)
  576. ubi_msg("sub-page size: %d",
  577. ubi->hdrs_min_io_size);
  578. ubi_msg("VID header offset: %d (aligned %d)",
  579. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  580. ubi_msg("data offset: %d", ubi->leb_start);
  581. /*
  582. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  583. * unfortunately, MTD does not provide this information. We should loop
  584. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  585. * each physical eraseblock. So, we skip ubi->bad_peb_count
  586. * uninitialized and initialize it after scanning.
  587. */
  588. return 0;
  589. }
  590. /**
  591. * autoresize - re-size the volume which has the "auto-resize" flag set.
  592. * @ubi: UBI device description object
  593. * @vol_id: ID of the volume to re-size
  594. *
  595. * This function re-sizes the volume marked by the @UBI_VTBL_AUTORESIZE_FLG in
  596. * the volume table to the largest possible size. See comments in ubi-header.h
  597. * for more description of the flag. Returns zero in case of success and a
  598. * negative error code in case of failure.
  599. */
  600. static int autoresize(struct ubi_device *ubi, int vol_id)
  601. {
  602. struct ubi_volume_desc desc;
  603. struct ubi_volume *vol = ubi->volumes[vol_id];
  604. int err, old_reserved_pebs = vol->reserved_pebs;
  605. /*
  606. * Clear the auto-resize flag in the volume in-memory copy of the
  607. * volume table, and 'ubi_resize_volume()' will propagate this change
  608. * to the flash.
  609. */
  610. ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
  611. if (ubi->avail_pebs == 0) {
  612. struct ubi_vtbl_record vtbl_rec;
  613. /*
  614. * No available PEBs to re-size the volume, clear the flag on
  615. * flash and exit.
  616. */
  617. memcpy(&vtbl_rec, &ubi->vtbl[vol_id],
  618. sizeof(struct ubi_vtbl_record));
  619. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  620. if (err)
  621. ubi_err("cannot clean auto-resize flag for volume %d",
  622. vol_id);
  623. } else {
  624. desc.vol = vol;
  625. err = ubi_resize_volume(&desc,
  626. old_reserved_pebs + ubi->avail_pebs);
  627. if (err)
  628. ubi_err("cannot auto-resize volume %d", vol_id);
  629. }
  630. if (err)
  631. return err;
  632. ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
  633. vol->name, old_reserved_pebs, vol->reserved_pebs);
  634. return 0;
  635. }
  636. /**
  637. * ubi_attach_mtd_dev - attach an MTD device.
  638. * @mtd: MTD device description object
  639. * @ubi_num: number to assign to the new UBI device
  640. * @vid_hdr_offset: VID header offset
  641. *
  642. * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
  643. * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
  644. * which case this function finds a vacant device number and assigns it
  645. * automatically. Returns the new UBI device number in case of success and a
  646. * negative error code in case of failure.
  647. *
  648. * Note, the invocations of this function has to be serialized by the
  649. * @ubi_devices_mutex.
  650. */
  651. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
  652. {
  653. struct ubi_device *ubi;
  654. int i, err, do_free = 1;
  655. /*
  656. * Check if we already have the same MTD device attached.
  657. *
  658. * Note, this function assumes that UBI devices creations and deletions
  659. * are serialized, so it does not take the &ubi_devices_lock.
  660. */
  661. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  662. ubi = ubi_devices[i];
  663. if (ubi && mtd->index == ubi->mtd->index) {
  664. dbg_err("mtd%d is already attached to ubi%d",
  665. mtd->index, i);
  666. return -EEXIST;
  667. }
  668. }
  669. /*
  670. * Make sure this MTD device is not emulated on top of an UBI volume
  671. * already. Well, generally this recursion works fine, but there are
  672. * different problems like the UBI module takes a reference to itself
  673. * by attaching (and thus, opening) the emulated MTD device. This
  674. * results in inability to unload the module. And in general it makes
  675. * no sense to attach emulated MTD devices, so we prohibit this.
  676. */
  677. if (mtd->type == MTD_UBIVOLUME) {
  678. ubi_err("refuse attaching mtd%d - it is already emulated on "
  679. "top of UBI", mtd->index);
  680. return -EINVAL;
  681. }
  682. if (ubi_num == UBI_DEV_NUM_AUTO) {
  683. /* Search for an empty slot in the @ubi_devices array */
  684. for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
  685. if (!ubi_devices[ubi_num])
  686. break;
  687. if (ubi_num == UBI_MAX_DEVICES) {
  688. dbg_err("only %d UBI devices may be created",
  689. UBI_MAX_DEVICES);
  690. return -ENFILE;
  691. }
  692. } else {
  693. if (ubi_num >= UBI_MAX_DEVICES)
  694. return -EINVAL;
  695. /* Make sure ubi_num is not busy */
  696. if (ubi_devices[ubi_num]) {
  697. dbg_err("ubi%d already exists", ubi_num);
  698. return -EEXIST;
  699. }
  700. }
  701. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  702. if (!ubi)
  703. return -ENOMEM;
  704. ubi->mtd = mtd;
  705. ubi->ubi_num = ubi_num;
  706. ubi->vid_hdr_offset = vid_hdr_offset;
  707. ubi->autoresize_vol_id = -1;
  708. mutex_init(&ubi->buf_mutex);
  709. mutex_init(&ubi->ckvol_mutex);
  710. mutex_init(&ubi->mult_mutex);
  711. mutex_init(&ubi->volumes_mutex);
  712. spin_lock_init(&ubi->volumes_lock);
  713. ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
  714. err = io_init(ubi);
  715. if (err)
  716. goto out_free;
  717. err = -ENOMEM;
  718. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  719. if (!ubi->peb_buf1)
  720. goto out_free;
  721. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  722. if (!ubi->peb_buf2)
  723. goto out_free;
  724. #ifdef CONFIG_MTD_UBI_DEBUG
  725. mutex_init(&ubi->dbg_buf_mutex);
  726. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  727. if (!ubi->dbg_peb_buf)
  728. goto out_free;
  729. #endif
  730. err = attach_by_scanning(ubi);
  731. if (err) {
  732. dbg_err("failed to attach by scanning, error %d", err);
  733. goto out_free;
  734. }
  735. if (ubi->autoresize_vol_id != -1) {
  736. err = autoresize(ubi, ubi->autoresize_vol_id);
  737. if (err)
  738. goto out_detach;
  739. }
  740. err = uif_init(ubi);
  741. if (err)
  742. goto out_nofree;
  743. ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
  744. if (IS_ERR(ubi->bgt_thread)) {
  745. err = PTR_ERR(ubi->bgt_thread);
  746. ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
  747. err);
  748. goto out_uif;
  749. }
  750. ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
  751. ubi_msg("MTD device name: \"%s\"", mtd->name);
  752. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  753. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  754. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  755. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  756. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  757. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  758. ubi_msg("number of user volumes: %d",
  759. ubi->vol_count - UBI_INT_VOL_COUNT);
  760. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  761. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  762. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  763. ubi->beb_rsvd_pebs);
  764. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  765. if (!DBG_DISABLE_BGT)
  766. ubi->thread_enabled = 1;
  767. wake_up_process(ubi->bgt_thread);
  768. ubi_devices[ubi_num] = ubi;
  769. return ubi_num;
  770. out_uif:
  771. uif_close(ubi);
  772. out_nofree:
  773. do_free = 0;
  774. out_detach:
  775. ubi_wl_close(ubi);
  776. if (do_free)
  777. free_user_volumes(ubi);
  778. free_internal_volumes(ubi);
  779. vfree(ubi->vtbl);
  780. out_free:
  781. vfree(ubi->peb_buf1);
  782. vfree(ubi->peb_buf2);
  783. #ifdef CONFIG_MTD_UBI_DEBUG
  784. vfree(ubi->dbg_peb_buf);
  785. #endif
  786. kfree(ubi);
  787. return err;
  788. }
  789. /**
  790. * ubi_detach_mtd_dev - detach an MTD device.
  791. * @ubi_num: UBI device number to detach from
  792. * @anyway: detach MTD even if device reference count is not zero
  793. *
  794. * This function destroys an UBI device number @ubi_num and detaches the
  795. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  796. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  797. * exist.
  798. *
  799. * Note, the invocations of this function has to be serialized by the
  800. * @ubi_devices_mutex.
  801. */
  802. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  803. {
  804. struct ubi_device *ubi;
  805. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  806. return -EINVAL;
  807. spin_lock(&ubi_devices_lock);
  808. ubi = ubi_devices[ubi_num];
  809. if (!ubi) {
  810. spin_unlock(&ubi_devices_lock);
  811. return -EINVAL;
  812. }
  813. if (ubi->ref_count) {
  814. if (!anyway) {
  815. spin_unlock(&ubi_devices_lock);
  816. return -EBUSY;
  817. }
  818. /* This may only happen if there is a bug */
  819. ubi_err("%s reference count %d, destroy anyway",
  820. ubi->ubi_name, ubi->ref_count);
  821. }
  822. ubi_devices[ubi_num] = NULL;
  823. spin_unlock(&ubi_devices_lock);
  824. ubi_assert(ubi_num == ubi->ubi_num);
  825. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  826. /*
  827. * Before freeing anything, we have to stop the background thread to
  828. * prevent it from doing anything on this device while we are freeing.
  829. */
  830. if (ubi->bgt_thread)
  831. kthread_stop(ubi->bgt_thread);
  832. uif_close(ubi);
  833. ubi_wl_close(ubi);
  834. free_internal_volumes(ubi);
  835. vfree(ubi->vtbl);
  836. put_mtd_device(ubi->mtd);
  837. vfree(ubi->peb_buf1);
  838. vfree(ubi->peb_buf2);
  839. #ifdef CONFIG_MTD_UBI_DEBUG
  840. vfree(ubi->dbg_peb_buf);
  841. #endif
  842. ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
  843. kfree(ubi);
  844. return 0;
  845. }
  846. /**
  847. * find_mtd_device - open an MTD device by its name or number.
  848. * @mtd_dev: name or number of the device
  849. *
  850. * This function tries to open and MTD device described by @mtd_dev string,
  851. * which is first treated as an ASCII number, and if it is not true, it is
  852. * treated as MTD device name. Returns MTD device description object in case of
  853. * success and a negative error code in case of failure.
  854. */
  855. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  856. {
  857. struct mtd_info *mtd;
  858. int mtd_num;
  859. char *endp;
  860. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  861. if (*endp != '\0' || mtd_dev == endp) {
  862. /*
  863. * This does not look like an ASCII integer, probably this is
  864. * MTD device name.
  865. */
  866. mtd = get_mtd_device_nm(mtd_dev);
  867. } else
  868. mtd = get_mtd_device(NULL, mtd_num);
  869. return mtd;
  870. }
  871. static int __init ubi_init(void)
  872. {
  873. int err, i, k;
  874. /* Ensure that EC and VID headers have correct size */
  875. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  876. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  877. if (mtd_devs > UBI_MAX_DEVICES) {
  878. ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
  879. return -EINVAL;
  880. }
  881. /* Create base sysfs directory and sysfs files */
  882. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  883. if (IS_ERR(ubi_class)) {
  884. err = PTR_ERR(ubi_class);
  885. ubi_err("cannot create UBI class");
  886. goto out;
  887. }
  888. err = class_create_file(ubi_class, &ubi_version);
  889. if (err) {
  890. ubi_err("cannot create sysfs file");
  891. goto out_class;
  892. }
  893. err = misc_register(&ubi_ctrl_cdev);
  894. if (err) {
  895. ubi_err("cannot register device");
  896. goto out_version;
  897. }
  898. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  899. sizeof(struct ubi_wl_entry),
  900. 0, 0, NULL);
  901. if (!ubi_wl_entry_slab)
  902. goto out_dev_unreg;
  903. /* Attach MTD devices */
  904. for (i = 0; i < mtd_devs; i++) {
  905. struct mtd_dev_param *p = &mtd_dev_param[i];
  906. struct mtd_info *mtd;
  907. cond_resched();
  908. mtd = open_mtd_device(p->name);
  909. if (IS_ERR(mtd)) {
  910. err = PTR_ERR(mtd);
  911. goto out_detach;
  912. }
  913. mutex_lock(&ubi_devices_mutex);
  914. err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO,
  915. p->vid_hdr_offs);
  916. mutex_unlock(&ubi_devices_mutex);
  917. if (err < 0) {
  918. put_mtd_device(mtd);
  919. ubi_err("cannot attach mtd%d", mtd->index);
  920. goto out_detach;
  921. }
  922. }
  923. return 0;
  924. out_detach:
  925. for (k = 0; k < i; k++)
  926. if (ubi_devices[k]) {
  927. mutex_lock(&ubi_devices_mutex);
  928. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  929. mutex_unlock(&ubi_devices_mutex);
  930. }
  931. kmem_cache_destroy(ubi_wl_entry_slab);
  932. out_dev_unreg:
  933. misc_deregister(&ubi_ctrl_cdev);
  934. out_version:
  935. class_remove_file(ubi_class, &ubi_version);
  936. out_class:
  937. class_destroy(ubi_class);
  938. out:
  939. ubi_err("UBI error: cannot initialize UBI, error %d", err);
  940. return err;
  941. }
  942. module_init(ubi_init);
  943. static void __exit ubi_exit(void)
  944. {
  945. int i;
  946. for (i = 0; i < UBI_MAX_DEVICES; i++)
  947. if (ubi_devices[i]) {
  948. mutex_lock(&ubi_devices_mutex);
  949. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  950. mutex_unlock(&ubi_devices_mutex);
  951. }
  952. kmem_cache_destroy(ubi_wl_entry_slab);
  953. misc_deregister(&ubi_ctrl_cdev);
  954. class_remove_file(ubi_class, &ubi_version);
  955. class_destroy(ubi_class);
  956. }
  957. module_exit(ubi_exit);
  958. /**
  959. * bytes_str_to_int - convert a number of bytes string into an integer.
  960. * @str: the string to convert
  961. *
  962. * This function returns positive resulting integer in case of success and a
  963. * negative error code in case of failure.
  964. */
  965. static int __init bytes_str_to_int(const char *str)
  966. {
  967. char *endp;
  968. unsigned long result;
  969. result = simple_strtoul(str, &endp, 0);
  970. if (str == endp || result < 0) {
  971. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  972. str);
  973. return -EINVAL;
  974. }
  975. switch (*endp) {
  976. case 'G':
  977. result *= 1024;
  978. case 'M':
  979. result *= 1024;
  980. case 'K':
  981. result *= 1024;
  982. if (endp[1] == 'i' && endp[2] == 'B')
  983. endp += 2;
  984. case '\0':
  985. break;
  986. default:
  987. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  988. str);
  989. return -EINVAL;
  990. }
  991. return result;
  992. }
  993. /**
  994. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  995. * @val: the parameter value to parse
  996. * @kp: not used
  997. *
  998. * This function returns zero in case of success and a negative error code in
  999. * case of error.
  1000. */
  1001. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1002. {
  1003. int i, len;
  1004. struct mtd_dev_param *p;
  1005. char buf[MTD_PARAM_LEN_MAX];
  1006. char *pbuf = &buf[0];
  1007. char *tokens[2] = {NULL, NULL};
  1008. if (!val)
  1009. return -EINVAL;
  1010. if (mtd_devs == UBI_MAX_DEVICES) {
  1011. printk(KERN_ERR "UBI error: too many parameters, max. is %d\n",
  1012. UBI_MAX_DEVICES);
  1013. return -EINVAL;
  1014. }
  1015. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1016. if (len == MTD_PARAM_LEN_MAX) {
  1017. printk(KERN_ERR "UBI error: parameter \"%s\" is too long, "
  1018. "max. is %d\n", val, MTD_PARAM_LEN_MAX);
  1019. return -EINVAL;
  1020. }
  1021. if (len == 0) {
  1022. printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - "
  1023. "ignored\n");
  1024. return 0;
  1025. }
  1026. strcpy(buf, val);
  1027. /* Get rid of the final newline */
  1028. if (buf[len - 1] == '\n')
  1029. buf[len - 1] = '\0';
  1030. for (i = 0; i < 2; i++)
  1031. tokens[i] = strsep(&pbuf, ",");
  1032. if (pbuf) {
  1033. printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n",
  1034. val);
  1035. return -EINVAL;
  1036. }
  1037. p = &mtd_dev_param[mtd_devs];
  1038. strcpy(&p->name[0], tokens[0]);
  1039. if (tokens[1])
  1040. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  1041. if (p->vid_hdr_offs < 0)
  1042. return p->vid_hdr_offs;
  1043. mtd_devs += 1;
  1044. return 0;
  1045. }
  1046. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1047. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  1048. "mtd=<name|num>[,<vid_hdr_offs>].\n"
  1049. "Multiple \"mtd\" parameters may be specified.\n"
  1050. "MTD devices may be specified by their number or name.\n"
  1051. "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
  1052. "header position and data starting position to be used "
  1053. "by UBI.\n"
  1054. "Example: mtd=content,1984 mtd=4 - attach MTD device"
  1055. "with name \"content\" using VID header offset 1984, and "
  1056. "MTD device number 4 with default VID header offset.");
  1057. MODULE_VERSION(__stringify(UBI_VERSION));
  1058. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1059. MODULE_AUTHOR("Artem Bityutskiy");
  1060. MODULE_LICENSE("GPL");