build.c 34 KB

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