build.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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. sprintf(&ubi->dev.bus_id[0], 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, do_free = 0;
  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. do_free = 0;
  382. out_sysfs:
  383. ubi_sysfs_close(ubi);
  384. cdev_del(&ubi->cdev);
  385. out_unreg:
  386. if (do_free)
  387. free_user_volumes(ubi);
  388. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  389. ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
  390. return err;
  391. }
  392. /**
  393. * uif_close - close user interfaces for an UBI device.
  394. * @ubi: UBI device description object
  395. *
  396. * Note, since this function un-registers UBI volume device objects (@vol->dev),
  397. * the memory allocated voe the volumes is freed as well (in the release
  398. * function).
  399. */
  400. static void uif_close(struct ubi_device *ubi)
  401. {
  402. kill_volumes(ubi);
  403. ubi_sysfs_close(ubi);
  404. cdev_del(&ubi->cdev);
  405. unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
  406. }
  407. /**
  408. * free_internal_volumes - free internal volumes.
  409. * @ubi: UBI device description object
  410. */
  411. static void free_internal_volumes(struct ubi_device *ubi)
  412. {
  413. int i;
  414. for (i = ubi->vtbl_slots;
  415. i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
  416. kfree(ubi->volumes[i]->eba_tbl);
  417. kfree(ubi->volumes[i]);
  418. }
  419. }
  420. /**
  421. * attach_by_scanning - attach an MTD device using scanning method.
  422. * @ubi: UBI device descriptor
  423. *
  424. * This function returns zero in case of success and a negative error code in
  425. * case of failure.
  426. *
  427. * Note, currently this is the only method to attach UBI devices. Hopefully in
  428. * the future we'll have more scalable attaching methods and avoid full media
  429. * scanning. But even in this case scanning will be needed as a fall-back
  430. * attaching method if there are some on-flash table corruptions.
  431. */
  432. static int attach_by_scanning(struct ubi_device *ubi)
  433. {
  434. int err;
  435. struct ubi_scan_info *si;
  436. si = ubi_scan(ubi);
  437. if (IS_ERR(si))
  438. return PTR_ERR(si);
  439. ubi->bad_peb_count = si->bad_peb_count;
  440. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  441. ubi->max_ec = si->max_ec;
  442. ubi->mean_ec = si->mean_ec;
  443. err = ubi_read_volume_table(ubi, si);
  444. if (err)
  445. goto out_si;
  446. err = ubi_wl_init_scan(ubi, si);
  447. if (err)
  448. goto out_vtbl;
  449. err = ubi_eba_init_scan(ubi, si);
  450. if (err)
  451. goto out_wl;
  452. ubi_scan_destroy_si(si);
  453. return 0;
  454. out_wl:
  455. ubi_wl_close(ubi);
  456. out_vtbl:
  457. free_internal_volumes(ubi);
  458. vfree(ubi->vtbl);
  459. out_si:
  460. ubi_scan_destroy_si(si);
  461. return err;
  462. }
  463. /**
  464. * io_init - initialize I/O sub-system for a given UBI device.
  465. * @ubi: UBI device description object
  466. *
  467. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  468. * assumed:
  469. * o EC header is always at offset zero - this cannot be changed;
  470. * o VID header starts just after the EC header at the closest address
  471. * aligned to @io->hdrs_min_io_size;
  472. * o data starts just after the VID header at the closest address aligned to
  473. * @io->min_io_size
  474. *
  475. * This function returns zero in case of success and a negative error code in
  476. * case of failure.
  477. */
  478. static int io_init(struct ubi_device *ubi)
  479. {
  480. if (ubi->mtd->numeraseregions != 0) {
  481. /*
  482. * Some flashes have several erase regions. Different regions
  483. * may have different eraseblock size and other
  484. * characteristics. It looks like mostly multi-region flashes
  485. * have one "main" region and one or more small regions to
  486. * store boot loader code or boot parameters or whatever. I
  487. * guess we should just pick the largest region. But this is
  488. * not implemented.
  489. */
  490. ubi_err("multiple regions, not implemented");
  491. return -EINVAL;
  492. }
  493. if (ubi->vid_hdr_offset < 0)
  494. return -EINVAL;
  495. /*
  496. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  497. * physical eraseblocks maximum.
  498. */
  499. ubi->peb_size = ubi->mtd->erasesize;
  500. ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
  501. ubi->flash_size = ubi->mtd->size;
  502. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  503. ubi->bad_allowed = 1;
  504. ubi->min_io_size = ubi->mtd->writesize;
  505. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  506. /*
  507. * Make sure minimal I/O unit is power of 2. Note, there is no
  508. * fundamental reason for this assumption. It is just an optimization
  509. * which allows us to avoid costly division operations.
  510. */
  511. if (!is_power_of_2(ubi->min_io_size)) {
  512. ubi_err("min. I/O unit (%d) is not power of 2",
  513. ubi->min_io_size);
  514. return -EINVAL;
  515. }
  516. ubi_assert(ubi->hdrs_min_io_size > 0);
  517. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  518. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  519. /* Calculate default aligned sizes of EC and VID headers */
  520. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  521. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  522. dbg_msg("min_io_size %d", ubi->min_io_size);
  523. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  524. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  525. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  526. if (ubi->vid_hdr_offset == 0)
  527. /* Default offset */
  528. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  529. ubi->ec_hdr_alsize;
  530. else {
  531. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  532. ~(ubi->hdrs_min_io_size - 1);
  533. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  534. ubi->vid_hdr_aloffset;
  535. }
  536. /* Similar for the data offset */
  537. ubi->leb_start = ubi->vid_hdr_offset + UBI_EC_HDR_SIZE;
  538. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  539. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  540. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  541. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  542. dbg_msg("leb_start %d", ubi->leb_start);
  543. /* The shift must be aligned to 32-bit boundary */
  544. if (ubi->vid_hdr_shift % 4) {
  545. ubi_err("unaligned VID header shift %d",
  546. ubi->vid_hdr_shift);
  547. return -EINVAL;
  548. }
  549. /* Check sanity */
  550. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  551. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  552. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  553. ubi->leb_start & (ubi->min_io_size - 1)) {
  554. ubi_err("bad VID header (%d) or data offsets (%d)",
  555. ubi->vid_hdr_offset, ubi->leb_start);
  556. return -EINVAL;
  557. }
  558. /*
  559. * It may happen that EC and VID headers are situated in one minimal
  560. * I/O unit. In this case we can only accept this UBI image in
  561. * read-only mode.
  562. */
  563. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  564. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  565. "switch to read-only mode");
  566. ubi->ro_mode = 1;
  567. }
  568. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  569. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  570. ubi_msg("MTD device %d is write-protected, attach in "
  571. "read-only mode", ubi->mtd->index);
  572. ubi->ro_mode = 1;
  573. }
  574. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  575. ubi->peb_size, ubi->peb_size >> 10);
  576. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  577. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  578. if (ubi->hdrs_min_io_size != ubi->min_io_size)
  579. ubi_msg("sub-page size: %d",
  580. ubi->hdrs_min_io_size);
  581. ubi_msg("VID header offset: %d (aligned %d)",
  582. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  583. ubi_msg("data offset: %d", ubi->leb_start);
  584. /*
  585. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  586. * unfortunately, MTD does not provide this information. We should loop
  587. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  588. * each physical eraseblock. So, we skip ubi->bad_peb_count
  589. * uninitialized and initialize it after scanning.
  590. */
  591. return 0;
  592. }
  593. /**
  594. * autoresize - re-size the volume which has the "auto-resize" flag set.
  595. * @ubi: UBI device description object
  596. * @vol_id: ID of the volume to re-size
  597. *
  598. * This function re-sizes the volume marked by the @UBI_VTBL_AUTORESIZE_FLG in
  599. * the volume table to the largest possible size. See comments in ubi-header.h
  600. * for more description of the flag. Returns zero in case of success and a
  601. * negative error code in case of failure.
  602. */
  603. static int autoresize(struct ubi_device *ubi, int vol_id)
  604. {
  605. struct ubi_volume_desc desc;
  606. struct ubi_volume *vol = ubi->volumes[vol_id];
  607. int err, old_reserved_pebs = vol->reserved_pebs;
  608. /*
  609. * Clear the auto-resize flag in the volume in-memory copy of the
  610. * volume table, and 'ubi_resize_volume()' will propagate this change
  611. * to the flash.
  612. */
  613. ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
  614. if (ubi->avail_pebs == 0) {
  615. struct ubi_vtbl_record vtbl_rec;
  616. /*
  617. * No available PEBs to re-size the volume, clear the flag on
  618. * flash and exit.
  619. */
  620. memcpy(&vtbl_rec, &ubi->vtbl[vol_id],
  621. sizeof(struct ubi_vtbl_record));
  622. err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
  623. if (err)
  624. ubi_err("cannot clean auto-resize flag for volume %d",
  625. vol_id);
  626. } else {
  627. desc.vol = vol;
  628. err = ubi_resize_volume(&desc,
  629. old_reserved_pebs + ubi->avail_pebs);
  630. if (err)
  631. ubi_err("cannot auto-resize volume %d", vol_id);
  632. }
  633. if (err)
  634. return err;
  635. ubi_msg("volume %d (\"%s\") re-sized from %d to %d LEBs", vol_id,
  636. vol->name, old_reserved_pebs, vol->reserved_pebs);
  637. return 0;
  638. }
  639. /**
  640. * ubi_attach_mtd_dev - attach an MTD device.
  641. * @mtd: MTD device description object
  642. * @ubi_num: number to assign to the new UBI device
  643. * @vid_hdr_offset: VID header offset
  644. *
  645. * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
  646. * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
  647. * which case this function finds a vacant device number and assigns it
  648. * automatically. Returns the new UBI device number in case of success and a
  649. * negative error code in case of failure.
  650. *
  651. * Note, the invocations of this function has to be serialized by the
  652. * @ubi_devices_mutex.
  653. */
  654. int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
  655. {
  656. struct ubi_device *ubi;
  657. int i, err, do_free = 1;
  658. /*
  659. * Check if we already have the same MTD device attached.
  660. *
  661. * Note, this function assumes that UBI devices creations and deletions
  662. * are serialized, so it does not take the &ubi_devices_lock.
  663. */
  664. for (i = 0; i < UBI_MAX_DEVICES; i++) {
  665. ubi = ubi_devices[i];
  666. if (ubi && mtd->index == ubi->mtd->index) {
  667. dbg_err("mtd%d is already attached to ubi%d",
  668. mtd->index, i);
  669. return -EEXIST;
  670. }
  671. }
  672. /*
  673. * Make sure this MTD device is not emulated on top of an UBI volume
  674. * already. Well, generally this recursion works fine, but there are
  675. * different problems like the UBI module takes a reference to itself
  676. * by attaching (and thus, opening) the emulated MTD device. This
  677. * results in inability to unload the module. And in general it makes
  678. * no sense to attach emulated MTD devices, so we prohibit this.
  679. */
  680. if (mtd->type == MTD_UBIVOLUME) {
  681. ubi_err("refuse attaching mtd%d - it is already emulated on "
  682. "top of UBI", mtd->index);
  683. return -EINVAL;
  684. }
  685. if (ubi_num == UBI_DEV_NUM_AUTO) {
  686. /* Search for an empty slot in the @ubi_devices array */
  687. for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
  688. if (!ubi_devices[ubi_num])
  689. break;
  690. if (ubi_num == UBI_MAX_DEVICES) {
  691. dbg_err("only %d UBI devices may be created",
  692. UBI_MAX_DEVICES);
  693. return -ENFILE;
  694. }
  695. } else {
  696. if (ubi_num >= UBI_MAX_DEVICES)
  697. return -EINVAL;
  698. /* Make sure ubi_num is not busy */
  699. if (ubi_devices[ubi_num]) {
  700. dbg_err("ubi%d already exists", ubi_num);
  701. return -EEXIST;
  702. }
  703. }
  704. ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
  705. if (!ubi)
  706. return -ENOMEM;
  707. ubi->mtd = mtd;
  708. ubi->ubi_num = ubi_num;
  709. ubi->vid_hdr_offset = vid_hdr_offset;
  710. ubi->autoresize_vol_id = -1;
  711. mutex_init(&ubi->buf_mutex);
  712. mutex_init(&ubi->ckvol_mutex);
  713. mutex_init(&ubi->mult_mutex);
  714. mutex_init(&ubi->volumes_mutex);
  715. spin_lock_init(&ubi->volumes_lock);
  716. ubi_msg("attaching mtd%d to ubi%d", mtd->index, ubi_num);
  717. err = io_init(ubi);
  718. if (err)
  719. goto out_free;
  720. err = -ENOMEM;
  721. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  722. if (!ubi->peb_buf1)
  723. goto out_free;
  724. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  725. if (!ubi->peb_buf2)
  726. goto out_free;
  727. #ifdef CONFIG_MTD_UBI_DEBUG
  728. mutex_init(&ubi->dbg_buf_mutex);
  729. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  730. if (!ubi->dbg_peb_buf)
  731. goto out_free;
  732. #endif
  733. err = attach_by_scanning(ubi);
  734. if (err) {
  735. dbg_err("failed to attach by scanning, error %d", err);
  736. goto out_free;
  737. }
  738. if (ubi->autoresize_vol_id != -1) {
  739. err = autoresize(ubi, ubi->autoresize_vol_id);
  740. if (err)
  741. goto out_detach;
  742. }
  743. err = uif_init(ubi);
  744. if (err)
  745. goto out_nofree;
  746. ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
  747. if (IS_ERR(ubi->bgt_thread)) {
  748. err = PTR_ERR(ubi->bgt_thread);
  749. ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
  750. err);
  751. goto out_uif;
  752. }
  753. ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
  754. ubi_msg("MTD device name: \"%s\"", mtd->name);
  755. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  756. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  757. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  758. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  759. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  760. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  761. ubi_msg("number of user volumes: %d",
  762. ubi->vol_count - UBI_INT_VOL_COUNT);
  763. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  764. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  765. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  766. ubi->beb_rsvd_pebs);
  767. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  768. if (!DBG_DISABLE_BGT)
  769. ubi->thread_enabled = 1;
  770. wake_up_process(ubi->bgt_thread);
  771. ubi_devices[ubi_num] = ubi;
  772. return ubi_num;
  773. out_uif:
  774. uif_close(ubi);
  775. out_nofree:
  776. do_free = 0;
  777. out_detach:
  778. ubi_wl_close(ubi);
  779. if (do_free)
  780. free_user_volumes(ubi);
  781. free_internal_volumes(ubi);
  782. vfree(ubi->vtbl);
  783. out_free:
  784. vfree(ubi->peb_buf1);
  785. vfree(ubi->peb_buf2);
  786. #ifdef CONFIG_MTD_UBI_DEBUG
  787. vfree(ubi->dbg_peb_buf);
  788. #endif
  789. kfree(ubi);
  790. return err;
  791. }
  792. /**
  793. * ubi_detach_mtd_dev - detach an MTD device.
  794. * @ubi_num: UBI device number to detach from
  795. * @anyway: detach MTD even if device reference count is not zero
  796. *
  797. * This function destroys an UBI device number @ubi_num and detaches the
  798. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  799. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  800. * exist.
  801. *
  802. * Note, the invocations of this function has to be serialized by the
  803. * @ubi_devices_mutex.
  804. */
  805. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  806. {
  807. struct ubi_device *ubi;
  808. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  809. return -EINVAL;
  810. spin_lock(&ubi_devices_lock);
  811. ubi = ubi_devices[ubi_num];
  812. if (!ubi) {
  813. spin_unlock(&ubi_devices_lock);
  814. return -EINVAL;
  815. }
  816. if (ubi->ref_count) {
  817. if (!anyway) {
  818. spin_unlock(&ubi_devices_lock);
  819. return -EBUSY;
  820. }
  821. /* This may only happen if there is a bug */
  822. ubi_err("%s reference count %d, destroy anyway",
  823. ubi->ubi_name, ubi->ref_count);
  824. }
  825. ubi_devices[ubi_num] = NULL;
  826. spin_unlock(&ubi_devices_lock);
  827. ubi_assert(ubi_num == ubi->ubi_num);
  828. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  829. /*
  830. * Before freeing anything, we have to stop the background thread to
  831. * prevent it from doing anything on this device while we are freeing.
  832. */
  833. if (ubi->bgt_thread)
  834. kthread_stop(ubi->bgt_thread);
  835. uif_close(ubi);
  836. ubi_wl_close(ubi);
  837. free_internal_volumes(ubi);
  838. vfree(ubi->vtbl);
  839. put_mtd_device(ubi->mtd);
  840. vfree(ubi->peb_buf1);
  841. vfree(ubi->peb_buf2);
  842. #ifdef CONFIG_MTD_UBI_DEBUG
  843. vfree(ubi->dbg_peb_buf);
  844. #endif
  845. ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
  846. kfree(ubi);
  847. return 0;
  848. }
  849. /**
  850. * find_mtd_device - open an MTD device by its name or number.
  851. * @mtd_dev: name or number of the device
  852. *
  853. * This function tries to open and MTD device described by @mtd_dev string,
  854. * which is first treated as an ASCII number, and if it is not true, it is
  855. * treated as MTD device name. Returns MTD device description object in case of
  856. * success and a negative error code in case of failure.
  857. */
  858. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  859. {
  860. struct mtd_info *mtd;
  861. int mtd_num;
  862. char *endp;
  863. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  864. if (*endp != '\0' || mtd_dev == endp) {
  865. /*
  866. * This does not look like an ASCII integer, probably this is
  867. * MTD device name.
  868. */
  869. mtd = get_mtd_device_nm(mtd_dev);
  870. } else
  871. mtd = get_mtd_device(NULL, mtd_num);
  872. return mtd;
  873. }
  874. static int __init ubi_init(void)
  875. {
  876. int err, i, k;
  877. /* Ensure that EC and VID headers have correct size */
  878. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  879. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  880. if (mtd_devs > UBI_MAX_DEVICES) {
  881. ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
  882. return -EINVAL;
  883. }
  884. /* Create base sysfs directory and sysfs files */
  885. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  886. if (IS_ERR(ubi_class)) {
  887. err = PTR_ERR(ubi_class);
  888. ubi_err("cannot create UBI class");
  889. goto out;
  890. }
  891. err = class_create_file(ubi_class, &ubi_version);
  892. if (err) {
  893. ubi_err("cannot create sysfs file");
  894. goto out_class;
  895. }
  896. err = misc_register(&ubi_ctrl_cdev);
  897. if (err) {
  898. ubi_err("cannot register device");
  899. goto out_version;
  900. }
  901. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  902. sizeof(struct ubi_wl_entry),
  903. 0, 0, NULL);
  904. if (!ubi_wl_entry_slab)
  905. goto out_dev_unreg;
  906. /* Attach MTD devices */
  907. for (i = 0; i < mtd_devs; i++) {
  908. struct mtd_dev_param *p = &mtd_dev_param[i];
  909. struct mtd_info *mtd;
  910. cond_resched();
  911. mtd = open_mtd_device(p->name);
  912. if (IS_ERR(mtd)) {
  913. err = PTR_ERR(mtd);
  914. goto out_detach;
  915. }
  916. mutex_lock(&ubi_devices_mutex);
  917. err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO,
  918. p->vid_hdr_offs);
  919. mutex_unlock(&ubi_devices_mutex);
  920. if (err < 0) {
  921. put_mtd_device(mtd);
  922. ubi_err("cannot attach mtd%d", mtd->index);
  923. goto out_detach;
  924. }
  925. }
  926. return 0;
  927. out_detach:
  928. for (k = 0; k < i; k++)
  929. if (ubi_devices[k]) {
  930. mutex_lock(&ubi_devices_mutex);
  931. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  932. mutex_unlock(&ubi_devices_mutex);
  933. }
  934. kmem_cache_destroy(ubi_wl_entry_slab);
  935. out_dev_unreg:
  936. misc_deregister(&ubi_ctrl_cdev);
  937. out_version:
  938. class_remove_file(ubi_class, &ubi_version);
  939. out_class:
  940. class_destroy(ubi_class);
  941. out:
  942. ubi_err("UBI error: cannot initialize UBI, error %d", err);
  943. return err;
  944. }
  945. module_init(ubi_init);
  946. static void __exit ubi_exit(void)
  947. {
  948. int i;
  949. for (i = 0; i < UBI_MAX_DEVICES; i++)
  950. if (ubi_devices[i]) {
  951. mutex_lock(&ubi_devices_mutex);
  952. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  953. mutex_unlock(&ubi_devices_mutex);
  954. }
  955. kmem_cache_destroy(ubi_wl_entry_slab);
  956. misc_deregister(&ubi_ctrl_cdev);
  957. class_remove_file(ubi_class, &ubi_version);
  958. class_destroy(ubi_class);
  959. }
  960. module_exit(ubi_exit);
  961. /**
  962. * bytes_str_to_int - convert a number of bytes string into an integer.
  963. * @str: the string to convert
  964. *
  965. * This function returns positive resulting integer in case of success and a
  966. * negative error code in case of failure.
  967. */
  968. static int __init bytes_str_to_int(const char *str)
  969. {
  970. char *endp;
  971. unsigned long result;
  972. result = simple_strtoul(str, &endp, 0);
  973. if (str == endp || result < 0) {
  974. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  975. str);
  976. return -EINVAL;
  977. }
  978. switch (*endp) {
  979. case 'G':
  980. result *= 1024;
  981. case 'M':
  982. result *= 1024;
  983. case 'K':
  984. result *= 1024;
  985. if (endp[1] == 'i' && endp[2] == 'B')
  986. endp += 2;
  987. case '\0':
  988. break;
  989. default:
  990. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  991. str);
  992. return -EINVAL;
  993. }
  994. return result;
  995. }
  996. /**
  997. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  998. * @val: the parameter value to parse
  999. * @kp: not used
  1000. *
  1001. * This function returns zero in case of success and a negative error code in
  1002. * case of error.
  1003. */
  1004. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1005. {
  1006. int i, len;
  1007. struct mtd_dev_param *p;
  1008. char buf[MTD_PARAM_LEN_MAX];
  1009. char *pbuf = &buf[0];
  1010. char *tokens[2] = {NULL, NULL};
  1011. if (!val)
  1012. return -EINVAL;
  1013. if (mtd_devs == UBI_MAX_DEVICES) {
  1014. printk(KERN_ERR "UBI error: too many parameters, max. is %d\n",
  1015. UBI_MAX_DEVICES);
  1016. return -EINVAL;
  1017. }
  1018. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1019. if (len == MTD_PARAM_LEN_MAX) {
  1020. printk(KERN_ERR "UBI error: parameter \"%s\" is too long, "
  1021. "max. is %d\n", val, MTD_PARAM_LEN_MAX);
  1022. return -EINVAL;
  1023. }
  1024. if (len == 0) {
  1025. printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - "
  1026. "ignored\n");
  1027. return 0;
  1028. }
  1029. strcpy(buf, val);
  1030. /* Get rid of the final newline */
  1031. if (buf[len - 1] == '\n')
  1032. buf[len - 1] = '\0';
  1033. for (i = 0; i < 2; i++)
  1034. tokens[i] = strsep(&pbuf, ",");
  1035. if (pbuf) {
  1036. printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n",
  1037. val);
  1038. return -EINVAL;
  1039. }
  1040. p = &mtd_dev_param[mtd_devs];
  1041. strcpy(&p->name[0], tokens[0]);
  1042. if (tokens[1])
  1043. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  1044. if (p->vid_hdr_offs < 0)
  1045. return p->vid_hdr_offs;
  1046. mtd_devs += 1;
  1047. return 0;
  1048. }
  1049. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1050. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  1051. "mtd=<name|num>[,<vid_hdr_offs>].\n"
  1052. "Multiple \"mtd\" parameters may be specified.\n"
  1053. "MTD devices may be specified by their number or name.\n"
  1054. "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
  1055. "header position and data starting position to be used "
  1056. "by UBI.\n"
  1057. "Example: mtd=content,1984 mtd=4 - attach MTD device"
  1058. "with name \"content\" using VID header offset 1984, and "
  1059. "MTD device number 4 with default VID header offset.");
  1060. MODULE_VERSION(__stringify(UBI_VERSION));
  1061. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1062. MODULE_AUTHOR("Artem Bityutskiy");
  1063. MODULE_LICENSE("GPL");