build.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  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. ubi->peb_buf1 = vmalloc(ubi->peb_size);
  721. if (!ubi->peb_buf1)
  722. goto out_free;
  723. ubi->peb_buf2 = vmalloc(ubi->peb_size);
  724. if (!ubi->peb_buf2)
  725. goto out_free;
  726. #ifdef CONFIG_MTD_UBI_DEBUG
  727. mutex_init(&ubi->dbg_buf_mutex);
  728. ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
  729. if (!ubi->dbg_peb_buf)
  730. goto out_free;
  731. #endif
  732. err = attach_by_scanning(ubi);
  733. if (err) {
  734. dbg_err("failed to attach by scanning, error %d", err);
  735. goto out_free;
  736. }
  737. if (ubi->autoresize_vol_id != -1) {
  738. err = autoresize(ubi, ubi->autoresize_vol_id);
  739. if (err)
  740. goto out_detach;
  741. }
  742. err = uif_init(ubi);
  743. if (err)
  744. goto out_nofree;
  745. ubi->bgt_thread = kthread_create(ubi_thread, ubi, ubi->bgt_name);
  746. if (IS_ERR(ubi->bgt_thread)) {
  747. err = PTR_ERR(ubi->bgt_thread);
  748. ubi_err("cannot spawn \"%s\", error %d", ubi->bgt_name,
  749. err);
  750. goto out_uif;
  751. }
  752. ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
  753. ubi_msg("MTD device name: \"%s\"", mtd->name);
  754. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  755. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  756. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  757. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  758. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  759. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  760. ubi_msg("number of user volumes: %d",
  761. ubi->vol_count - UBI_INT_VOL_COUNT);
  762. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  763. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  764. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  765. ubi->beb_rsvd_pebs);
  766. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  767. if (!DBG_DISABLE_BGT)
  768. ubi->thread_enabled = 1;
  769. wake_up_process(ubi->bgt_thread);
  770. ubi_devices[ubi_num] = ubi;
  771. return ubi_num;
  772. out_uif:
  773. uif_close(ubi);
  774. out_nofree:
  775. do_free = 0;
  776. out_detach:
  777. ubi_wl_close(ubi);
  778. if (do_free)
  779. free_user_volumes(ubi);
  780. free_internal_volumes(ubi);
  781. vfree(ubi->vtbl);
  782. out_free:
  783. vfree(ubi->peb_buf1);
  784. vfree(ubi->peb_buf2);
  785. #ifdef CONFIG_MTD_UBI_DEBUG
  786. vfree(ubi->dbg_peb_buf);
  787. #endif
  788. kfree(ubi);
  789. return err;
  790. }
  791. /**
  792. * ubi_detach_mtd_dev - detach an MTD device.
  793. * @ubi_num: UBI device number to detach from
  794. * @anyway: detach MTD even if device reference count is not zero
  795. *
  796. * This function destroys an UBI device number @ubi_num and detaches the
  797. * underlying MTD device. Returns zero in case of success and %-EBUSY if the
  798. * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
  799. * exist.
  800. *
  801. * Note, the invocations of this function has to be serialized by the
  802. * @ubi_devices_mutex.
  803. */
  804. int ubi_detach_mtd_dev(int ubi_num, int anyway)
  805. {
  806. struct ubi_device *ubi;
  807. if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
  808. return -EINVAL;
  809. spin_lock(&ubi_devices_lock);
  810. ubi = ubi_devices[ubi_num];
  811. if (!ubi) {
  812. spin_unlock(&ubi_devices_lock);
  813. return -EINVAL;
  814. }
  815. if (ubi->ref_count) {
  816. if (!anyway) {
  817. spin_unlock(&ubi_devices_lock);
  818. return -EBUSY;
  819. }
  820. /* This may only happen if there is a bug */
  821. ubi_err("%s reference count %d, destroy anyway",
  822. ubi->ubi_name, ubi->ref_count);
  823. }
  824. ubi_devices[ubi_num] = NULL;
  825. spin_unlock(&ubi_devices_lock);
  826. ubi_assert(ubi_num == ubi->ubi_num);
  827. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  828. /*
  829. * Before freeing anything, we have to stop the background thread to
  830. * prevent it from doing anything on this device while we are freeing.
  831. */
  832. if (ubi->bgt_thread)
  833. kthread_stop(ubi->bgt_thread);
  834. uif_close(ubi);
  835. ubi_wl_close(ubi);
  836. free_internal_volumes(ubi);
  837. vfree(ubi->vtbl);
  838. put_mtd_device(ubi->mtd);
  839. vfree(ubi->peb_buf1);
  840. vfree(ubi->peb_buf2);
  841. #ifdef CONFIG_MTD_UBI_DEBUG
  842. vfree(ubi->dbg_peb_buf);
  843. #endif
  844. ubi_msg("mtd%d is detached from ubi%d", ubi->mtd->index, ubi->ubi_num);
  845. kfree(ubi);
  846. return 0;
  847. }
  848. /**
  849. * find_mtd_device - open an MTD device by its name or number.
  850. * @mtd_dev: name or number of the device
  851. *
  852. * This function tries to open and MTD device described by @mtd_dev string,
  853. * which is first treated as an ASCII number, and if it is not true, it is
  854. * treated as MTD device name. Returns MTD device description object in case of
  855. * success and a negative error code in case of failure.
  856. */
  857. static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
  858. {
  859. struct mtd_info *mtd;
  860. int mtd_num;
  861. char *endp;
  862. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  863. if (*endp != '\0' || mtd_dev == endp) {
  864. /*
  865. * This does not look like an ASCII integer, probably this is
  866. * MTD device name.
  867. */
  868. mtd = get_mtd_device_nm(mtd_dev);
  869. } else
  870. mtd = get_mtd_device(NULL, mtd_num);
  871. return mtd;
  872. }
  873. static int __init ubi_init(void)
  874. {
  875. int err, i, k;
  876. /* Ensure that EC and VID headers have correct size */
  877. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  878. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  879. if (mtd_devs > UBI_MAX_DEVICES) {
  880. ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
  881. return -EINVAL;
  882. }
  883. /* Create base sysfs directory and sysfs files */
  884. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  885. if (IS_ERR(ubi_class)) {
  886. err = PTR_ERR(ubi_class);
  887. ubi_err("cannot create UBI class");
  888. goto out;
  889. }
  890. err = class_create_file(ubi_class, &ubi_version);
  891. if (err) {
  892. ubi_err("cannot create sysfs file");
  893. goto out_class;
  894. }
  895. err = misc_register(&ubi_ctrl_cdev);
  896. if (err) {
  897. ubi_err("cannot register device");
  898. goto out_version;
  899. }
  900. ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
  901. sizeof(struct ubi_wl_entry),
  902. 0, 0, NULL);
  903. if (!ubi_wl_entry_slab)
  904. goto out_dev_unreg;
  905. /* Attach MTD devices */
  906. for (i = 0; i < mtd_devs; i++) {
  907. struct mtd_dev_param *p = &mtd_dev_param[i];
  908. struct mtd_info *mtd;
  909. cond_resched();
  910. mtd = open_mtd_device(p->name);
  911. if (IS_ERR(mtd)) {
  912. err = PTR_ERR(mtd);
  913. goto out_detach;
  914. }
  915. mutex_lock(&ubi_devices_mutex);
  916. err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO,
  917. p->vid_hdr_offs);
  918. mutex_unlock(&ubi_devices_mutex);
  919. if (err < 0) {
  920. put_mtd_device(mtd);
  921. ubi_err("cannot attach mtd%d", mtd->index);
  922. goto out_detach;
  923. }
  924. }
  925. return 0;
  926. out_detach:
  927. for (k = 0; k < i; k++)
  928. if (ubi_devices[k]) {
  929. mutex_lock(&ubi_devices_mutex);
  930. ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
  931. mutex_unlock(&ubi_devices_mutex);
  932. }
  933. kmem_cache_destroy(ubi_wl_entry_slab);
  934. out_dev_unreg:
  935. misc_deregister(&ubi_ctrl_cdev);
  936. out_version:
  937. class_remove_file(ubi_class, &ubi_version);
  938. out_class:
  939. class_destroy(ubi_class);
  940. out:
  941. ubi_err("UBI error: cannot initialize UBI, error %d", err);
  942. return err;
  943. }
  944. module_init(ubi_init);
  945. static void __exit ubi_exit(void)
  946. {
  947. int i;
  948. for (i = 0; i < UBI_MAX_DEVICES; i++)
  949. if (ubi_devices[i]) {
  950. mutex_lock(&ubi_devices_mutex);
  951. ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
  952. mutex_unlock(&ubi_devices_mutex);
  953. }
  954. kmem_cache_destroy(ubi_wl_entry_slab);
  955. misc_deregister(&ubi_ctrl_cdev);
  956. class_remove_file(ubi_class, &ubi_version);
  957. class_destroy(ubi_class);
  958. }
  959. module_exit(ubi_exit);
  960. /**
  961. * bytes_str_to_int - convert a number of bytes string into an integer.
  962. * @str: the string to convert
  963. *
  964. * This function returns positive resulting integer in case of success and a
  965. * negative error code in case of failure.
  966. */
  967. static int __init bytes_str_to_int(const char *str)
  968. {
  969. char *endp;
  970. unsigned long result;
  971. result = simple_strtoul(str, &endp, 0);
  972. if (str == endp || result < 0) {
  973. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  974. str);
  975. return -EINVAL;
  976. }
  977. switch (*endp) {
  978. case 'G':
  979. result *= 1024;
  980. case 'M':
  981. result *= 1024;
  982. case 'K':
  983. result *= 1024;
  984. if (endp[1] == 'i' && endp[2] == 'B')
  985. endp += 2;
  986. case '\0':
  987. break;
  988. default:
  989. printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
  990. str);
  991. return -EINVAL;
  992. }
  993. return result;
  994. }
  995. /**
  996. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  997. * @val: the parameter value to parse
  998. * @kp: not used
  999. *
  1000. * This function returns zero in case of success and a negative error code in
  1001. * case of error.
  1002. */
  1003. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  1004. {
  1005. int i, len;
  1006. struct mtd_dev_param *p;
  1007. char buf[MTD_PARAM_LEN_MAX];
  1008. char *pbuf = &buf[0];
  1009. char *tokens[2] = {NULL, NULL};
  1010. if (!val)
  1011. return -EINVAL;
  1012. if (mtd_devs == UBI_MAX_DEVICES) {
  1013. printk(KERN_ERR "UBI error: too many parameters, max. is %d\n",
  1014. UBI_MAX_DEVICES);
  1015. return -EINVAL;
  1016. }
  1017. len = strnlen(val, MTD_PARAM_LEN_MAX);
  1018. if (len == MTD_PARAM_LEN_MAX) {
  1019. printk(KERN_ERR "UBI error: parameter \"%s\" is too long, "
  1020. "max. is %d\n", val, MTD_PARAM_LEN_MAX);
  1021. return -EINVAL;
  1022. }
  1023. if (len == 0) {
  1024. printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - "
  1025. "ignored\n");
  1026. return 0;
  1027. }
  1028. strcpy(buf, val);
  1029. /* Get rid of the final newline */
  1030. if (buf[len - 1] == '\n')
  1031. buf[len - 1] = '\0';
  1032. for (i = 0; i < 2; i++)
  1033. tokens[i] = strsep(&pbuf, ",");
  1034. if (pbuf) {
  1035. printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n",
  1036. val);
  1037. return -EINVAL;
  1038. }
  1039. p = &mtd_dev_param[mtd_devs];
  1040. strcpy(&p->name[0], tokens[0]);
  1041. if (tokens[1])
  1042. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  1043. if (p->vid_hdr_offs < 0)
  1044. return p->vid_hdr_offs;
  1045. mtd_devs += 1;
  1046. return 0;
  1047. }
  1048. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  1049. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  1050. "mtd=<name|num>[,<vid_hdr_offs>].\n"
  1051. "Multiple \"mtd\" parameters may be specified.\n"
  1052. "MTD devices may be specified by their number or name.\n"
  1053. "Optional \"vid_hdr_offs\" parameter specifies UBI VID "
  1054. "header position and data starting position to be used "
  1055. "by UBI.\n"
  1056. "Example: mtd=content,1984 mtd=4 - attach MTD device"
  1057. "with name \"content\" using VID header offset 1984, and "
  1058. "MTD device number 4 with default VID header offset.");
  1059. MODULE_VERSION(__stringify(UBI_VERSION));
  1060. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  1061. MODULE_AUTHOR("Artem Bityutskiy");
  1062. MODULE_LICENSE("GPL");