2 * Copyright (c) International Business Machines Corp., 2006
3 * Copyright (c) Nokia Corporation, 2007
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.
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.
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
19 * Author: Artem Bityutskiy (Битюцкий Артём),
24 * This file includes UBI initialization and building of UBI devices.
26 * When UBI is initialized, it attaches all the MTD devices specified as the
27 * module load parameters or the kernel boot parameters. If MTD devices were
28 * specified, UBI does not attach any MTD device, but it is possible to do
29 * later using the "UBI control device".
31 * At the moment we only attach UBI devices by scanning, which will become a
32 * bottleneck when flashes reach certain large size. Then one may improve UBI
33 * and add other methods, although it does not seem to be easy to do.
36 #include <linux/err.h>
37 #include <linux/module.h>
38 #include <linux/moduleparam.h>
39 #include <linux/stringify.h>
40 #include <linux/stat.h>
41 #include <linux/miscdevice.h>
42 #include <linux/log2.h>
45 /* Maximum length of the 'mtd=' parameter */
46 #define MTD_PARAM_LEN_MAX 64
49 * struct mtd_dev_param - MTD device parameter description data structure.
50 * @name: MTD device name or number string
51 * @vid_hdr_offs: VID header offset
52 * @data_offs: data offset
56 char name[MTD_PARAM_LEN_MAX];
61 /* Numbers of elements set in the @mtd_dev_param array */
62 static int mtd_devs = 0;
64 /* MTD devices specification parameters */
65 static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
67 /* All UBI devices in system */
68 struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
70 /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
71 struct class *ubi_class;
73 /* Slab cache for lock-tree entries */
74 struct kmem_cache *ubi_ltree_slab;
76 /* Slab cache for wear-leveling entries */
77 struct kmem_cache *ubi_wl_entry_slab;
79 /* UBI control character device */
80 static struct miscdevice ubi_ctrl_cdev = {
81 .minor = MISC_DYNAMIC_MINOR,
83 .fops = &ubi_ctrl_cdev_operations,
86 /* "Show" method for files in '/<sysfs>/class/ubi/' */
87 static ssize_t ubi_version_show(struct class *class, char *buf)
89 return sprintf(buf, "%d\n", UBI_VERSION);
92 /* UBI version attribute ('/<sysfs>/class/ubi/version') */
93 static struct class_attribute ubi_version =
94 __ATTR(version, S_IRUGO, ubi_version_show, NULL);
96 static ssize_t dev_attribute_show(struct device *dev,
97 struct device_attribute *attr, char *buf);
99 /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
100 static struct device_attribute dev_eraseblock_size =
101 __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
102 static struct device_attribute dev_avail_eraseblocks =
103 __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
104 static struct device_attribute dev_total_eraseblocks =
105 __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
106 static struct device_attribute dev_volumes_count =
107 __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
108 static struct device_attribute dev_max_ec =
109 __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
110 static struct device_attribute dev_reserved_for_bad =
111 __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
112 static struct device_attribute dev_bad_peb_count =
113 __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
114 static struct device_attribute dev_max_vol_count =
115 __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
116 static struct device_attribute dev_min_io_size =
117 __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
118 static struct device_attribute dev_bgt_enabled =
119 __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
121 /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
122 static ssize_t dev_attribute_show(struct device *dev,
123 struct device_attribute *attr, char *buf)
125 const struct ubi_device *ubi;
127 ubi = container_of(dev, struct ubi_device, dev);
128 if (attr == &dev_eraseblock_size)
129 return sprintf(buf, "%d\n", ubi->leb_size);
130 else if (attr == &dev_avail_eraseblocks)
131 return sprintf(buf, "%d\n", ubi->avail_pebs);
132 else if (attr == &dev_total_eraseblocks)
133 return sprintf(buf, "%d\n", ubi->good_peb_count);
134 else if (attr == &dev_volumes_count)
135 return sprintf(buf, "%d\n", ubi->vol_count);
136 else if (attr == &dev_max_ec)
137 return sprintf(buf, "%d\n", ubi->max_ec);
138 else if (attr == &dev_reserved_for_bad)
139 return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
140 else if (attr == &dev_bad_peb_count)
141 return sprintf(buf, "%d\n", ubi->bad_peb_count);
142 else if (attr == &dev_max_vol_count)
143 return sprintf(buf, "%d\n", ubi->vtbl_slots);
144 else if (attr == &dev_min_io_size)
145 return sprintf(buf, "%d\n", ubi->min_io_size);
146 else if (attr == &dev_bgt_enabled)
147 return sprintf(buf, "%d\n", ubi->thread_enabled);
154 /* Fake "release" method for UBI devices */
155 static void dev_release(struct device *dev) { }
158 * ubi_sysfs_init - initialize sysfs for an UBI device.
159 * @ubi: UBI device description object
161 * This function returns zero in case of success and a negative error code in
164 static int ubi_sysfs_init(struct ubi_device *ubi)
168 ubi->dev.release = dev_release;
169 ubi->dev.devt = ubi->cdev.dev;
170 ubi->dev.class = ubi_class;
171 sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
172 err = device_register(&ubi->dev);
176 err = device_create_file(&ubi->dev, &dev_eraseblock_size);
179 err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
182 err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
185 err = device_create_file(&ubi->dev, &dev_volumes_count);
188 err = device_create_file(&ubi->dev, &dev_max_ec);
191 err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
194 err = device_create_file(&ubi->dev, &dev_bad_peb_count);
197 err = device_create_file(&ubi->dev, &dev_max_vol_count);
200 err = device_create_file(&ubi->dev, &dev_min_io_size);
203 err = device_create_file(&ubi->dev, &dev_bgt_enabled);
208 * ubi_sysfs_close - close sysfs for an UBI device.
209 * @ubi: UBI device description object
211 static void ubi_sysfs_close(struct ubi_device *ubi)
213 device_remove_file(&ubi->dev, &dev_bgt_enabled);
214 device_remove_file(&ubi->dev, &dev_min_io_size);
215 device_remove_file(&ubi->dev, &dev_max_vol_count);
216 device_remove_file(&ubi->dev, &dev_bad_peb_count);
217 device_remove_file(&ubi->dev, &dev_reserved_for_bad);
218 device_remove_file(&ubi->dev, &dev_max_ec);
219 device_remove_file(&ubi->dev, &dev_volumes_count);
220 device_remove_file(&ubi->dev, &dev_total_eraseblocks);
221 device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
222 device_remove_file(&ubi->dev, &dev_eraseblock_size);
223 device_unregister(&ubi->dev);
227 * kill_volumes - destroy all volumes.
228 * @ubi: UBI device description object
230 static void kill_volumes(struct ubi_device *ubi)
234 for (i = 0; i < ubi->vtbl_slots; i++)
236 ubi_free_volume(ubi, ubi->volumes[i]);
240 * uif_init - initialize user interfaces for an UBI device.
241 * @ubi: UBI device description object
243 * This function returns zero in case of success and a negative error code in
246 static int uif_init(struct ubi_device *ubi)
251 mutex_init(&ubi->volumes_mutex);
252 spin_lock_init(&ubi->volumes_lock);
254 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
257 * Major numbers for the UBI character devices are allocated
258 * dynamically. Major numbers of volume character devices are
259 * equivalent to ones of the corresponding UBI character device. Minor
260 * numbers of UBI character devices are 0, while minor numbers of
261 * volume character devices start from 1. Thus, we allocate one major
262 * number and ubi->vtbl_slots + 1 minor numbers.
264 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
266 ubi_err("cannot register UBI character devices");
270 ubi_assert(MINOR(dev) == 0);
271 cdev_init(&ubi->cdev, &ubi_cdev_operations);
272 dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
273 ubi->cdev.owner = THIS_MODULE;
275 err = cdev_add(&ubi->cdev, dev, 1);
277 ubi_err("cannot add character device");
281 err = ubi_sysfs_init(ubi);
285 for (i = 0; i < ubi->vtbl_slots; i++)
286 if (ubi->volumes[i]) {
287 err = ubi_add_volume(ubi, ubi->volumes[i]);
289 ubi_err("cannot add volume %d", i);
299 ubi_sysfs_close(ubi);
300 cdev_del(&ubi->cdev);
302 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
303 ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
308 * uif_close - close user interfaces for an UBI device.
309 * @ubi: UBI device description object
311 static void uif_close(struct ubi_device *ubi)
314 ubi_sysfs_close(ubi);
315 cdev_del(&ubi->cdev);
316 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
320 * attach_by_scanning - attach an MTD device using scanning method.
321 * @ubi: UBI device descriptor
323 * This function returns zero in case of success and a negative error code in
326 * Note, currently this is the only method to attach UBI devices. Hopefully in
327 * the future we'll have more scalable attaching methods and avoid full media
328 * scanning. But even in this case scanning will be needed as a fall-back
329 * attaching method if there are some on-flash table corruptions.
331 static int attach_by_scanning(struct ubi_device *ubi)
334 struct ubi_scan_info *si;
340 ubi->bad_peb_count = si->bad_peb_count;
341 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
342 ubi->max_ec = si->max_ec;
343 ubi->mean_ec = si->mean_ec;
345 err = ubi_read_volume_table(ubi, si);
349 err = ubi_wl_init_scan(ubi, si);
353 err = ubi_eba_init_scan(ubi, si);
357 ubi_scan_destroy_si(si);
365 ubi_scan_destroy_si(si);
370 * io_init - initialize I/O unit for a given UBI device.
371 * @ubi: UBI device description object
373 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
375 * o EC header is always at offset zero - this cannot be changed;
376 * o VID header starts just after the EC header at the closest address
377 * aligned to @io->@hdrs_min_io_size;
378 * o data starts just after the VID header at the closest address aligned to
381 * This function returns zero in case of success and a negative error code in
384 static int io_init(struct ubi_device *ubi)
386 if (ubi->mtd->numeraseregions != 0) {
388 * Some flashes have several erase regions. Different regions
389 * may have different eraseblock size and other
390 * characteristics. It looks like mostly multi-region flashes
391 * have one "main" region and one or more small regions to
392 * store boot loader code or boot parameters or whatever. I
393 * guess we should just pick the largest region. But this is
396 ubi_err("multiple regions, not implemented");
401 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
402 * physical eraseblocks maximum.
405 ubi->peb_size = ubi->mtd->erasesize;
406 ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
407 ubi->flash_size = ubi->mtd->size;
409 if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
410 ubi->bad_allowed = 1;
412 ubi->min_io_size = ubi->mtd->writesize;
413 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
415 /* Make sure minimal I/O unit is power of 2 */
416 if (!is_power_of_2(ubi->min_io_size)) {
417 ubi_err("min. I/O unit (%d) is not power of 2",
422 ubi_assert(ubi->hdrs_min_io_size > 0);
423 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
424 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
426 /* Calculate default aligned sizes of EC and VID headers */
427 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
428 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
430 dbg_msg("min_io_size %d", ubi->min_io_size);
431 dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
432 dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
433 dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
435 if (ubi->vid_hdr_offset == 0)
437 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
440 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
441 ~(ubi->hdrs_min_io_size - 1);
442 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
443 ubi->vid_hdr_aloffset;
446 /* Similar for the data offset */
447 if (ubi->leb_start == 0) {
448 ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
449 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
452 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
453 dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
454 dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
455 dbg_msg("leb_start %d", ubi->leb_start);
457 /* The shift must be aligned to 32-bit boundary */
458 if (ubi->vid_hdr_shift % 4) {
459 ubi_err("unaligned VID header shift %d",
465 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
466 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
467 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
468 ubi->leb_start % ubi->min_io_size) {
469 ubi_err("bad VID header (%d) or data offsets (%d)",
470 ubi->vid_hdr_offset, ubi->leb_start);
475 * It may happen that EC and VID headers are situated in one minimal
476 * I/O unit. In this case we can only accept this UBI image in
479 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
480 ubi_warn("EC and VID headers are in the same minimal I/O unit, "
481 "switch to read-only mode");
485 ubi->leb_size = ubi->peb_size - ubi->leb_start;
487 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
488 ubi_msg("MTD device %d is write-protected, attach in "
489 "read-only mode", ubi->mtd->index);
493 dbg_msg("leb_size %d", ubi->leb_size);
494 dbg_msg("ro_mode %d", ubi->ro_mode);
497 * Note, ideally, we have to initialize ubi->bad_peb_count here. But
498 * unfortunately, MTD does not provide this information. We should loop
499 * over all physical eraseblocks and invoke mtd->block_is_bad() for
500 * each physical eraseblock. So, we skip ubi->bad_peb_count
501 * uninitialized and initialize it after scanning.
508 * attach_mtd_dev - attach an MTD device.
509 * @mtd_dev: MTD device name or number string
510 * @vid_hdr_offset: VID header offset
511 * @data_offset: data offset
513 * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
514 * MTD device name, and tries to open it by this name. If it is unable to open,
515 * it tries to convert @mtd_dev to an integer and open the MTD device by its
516 * number. Returns zero in case of success and a negative error code in case of
519 static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
522 struct ubi_device *ubi;
523 struct mtd_info *mtd;
526 mtd = get_mtd_device_nm(mtd_dev);
531 if (PTR_ERR(mtd) != -ENODEV)
535 * Probably this is not MTD device name but MTD device number -
538 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
539 if (*endp != '\0' || mtd_dev == endp) {
540 ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
544 mtd = get_mtd_device(NULL, mtd_num);
549 /* Check if we already have the same MTD device attached */
550 for (i = 0; i < UBI_MAX_DEVICES; i++)
551 ubi = ubi_devices[i];
552 if (ubi && ubi->mtd->index == mtd->index) {
553 ubi_err("mtd%d is already attached to ubi%d",
559 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
567 /* Search for an empty slot in the @ubi_devices array */
569 for (i = 0; i < UBI_MAX_DEVICES; i++)
570 if (!ubi_devices[i]) {
575 if (ubi->ubi_num == -1) {
576 ubi_err("only %d UBI devices may be created", UBI_MAX_DEVICES);
581 dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
582 ubi->mtd->index, ubi->ubi_num, vid_hdr_offset, data_offset);
584 ubi->vid_hdr_offset = vid_hdr_offset;
585 ubi->leb_start = data_offset;
590 mutex_init(&ubi->buf_mutex);
591 ubi->peb_buf1 = vmalloc(ubi->peb_size);
595 ubi->peb_buf2 = vmalloc(ubi->peb_size);
599 #ifdef CONFIG_MTD_UBI_DEBUG
600 mutex_init(&ubi->dbg_buf_mutex);
601 ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
602 if (!ubi->dbg_peb_buf)
606 err = attach_by_scanning(ubi);
608 dbg_err("failed to attach by scanning, error %d", err);
616 ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi->ubi_num);
617 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
618 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
619 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
620 ubi->peb_size, ubi->peb_size >> 10);
621 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
622 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
623 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
624 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
625 ubi_msg("VID header offset: %d (aligned %d)",
626 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
627 ubi_msg("data offset: %d", ubi->leb_start);
628 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
629 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
630 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
631 ubi_msg("number of user volumes: %d",
632 ubi->vol_count - UBI_INT_VOL_COUNT);
633 ubi_msg("available PEBs: %d", ubi->avail_pebs);
634 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
635 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
637 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
639 /* Enable the background thread */
640 if (!DBG_DISABLE_BGT) {
641 ubi->thread_enabled = 1;
642 wake_up_process(ubi->bgt_thread);
645 ubi_devices[ubi->ubi_num] = ubi;
653 vfree(ubi->peb_buf1);
654 vfree(ubi->peb_buf2);
655 #ifdef CONFIG_MTD_UBI_DEBUG
656 vfree(ubi->dbg_peb_buf);
665 * detach_mtd_dev - detach an MTD device.
666 * @ubi: UBI device description object
668 static void detach_mtd_dev(struct ubi_device *ubi)
670 int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
672 dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
677 put_mtd_device(ubi->mtd);
678 vfree(ubi->peb_buf1);
679 vfree(ubi->peb_buf2);
680 #ifdef CONFIG_MTD_UBI_DEBUG
681 vfree(ubi->dbg_peb_buf);
683 kfree(ubi_devices[ubi_num]);
684 ubi_devices[ubi_num] = NULL;
685 ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
689 * ltree_entry_ctor - lock tree entries slab cache constructor.
690 * @obj: the lock-tree entry to construct
691 * @cache: the lock tree entry slab cache
692 * @flags: constructor flags
694 static void ltree_entry_ctor(struct kmem_cache *cache, void *obj)
696 struct ubi_ltree_entry *le = obj;
699 init_rwsem(&le->mutex);
702 static int __init ubi_init(void)
706 /* Ensure that EC and VID headers have correct size */
707 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
708 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
710 if (mtd_devs > UBI_MAX_DEVICES) {
711 printk(KERN_ERR "UBI error: too many MTD devices, "
712 "maximum is %d\n", UBI_MAX_DEVICES);
716 /* Create base sysfs directory and sysfs files */
717 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
718 if (IS_ERR(ubi_class)) {
719 err = PTR_ERR(ubi_class);
720 printk(KERN_ERR "UBI error: cannot create UBI class\n");
724 err = class_create_file(ubi_class, &ubi_version);
726 printk(KERN_ERR "UBI error: cannot create sysfs file\n");
730 err = misc_register(&ubi_ctrl_cdev);
732 printk(KERN_ERR "UBI error: cannot register device\n");
736 ubi_ltree_slab = kmem_cache_create("ubi_ltree_slab",
737 sizeof(struct ubi_ltree_entry), 0,
738 0, <ree_entry_ctor);
742 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
743 sizeof(struct ubi_wl_entry),
745 if (!ubi_wl_entry_slab)
748 /* Attach MTD devices */
749 for (i = 0; i < mtd_devs; i++) {
750 struct mtd_dev_param *p = &mtd_dev_param[i];
753 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
755 printk(KERN_ERR "UBI error: cannot attach %s\n",
764 for (k = 0; k < i; k++)
765 detach_mtd_dev(ubi_devices[k]);
766 kmem_cache_destroy(ubi_wl_entry_slab);
768 kmem_cache_destroy(ubi_ltree_slab);
770 misc_deregister(&ubi_ctrl_cdev);
772 class_remove_file(ubi_class, &ubi_version);
774 class_destroy(ubi_class);
776 printk(KERN_ERR "UBI error: cannot initialize UBI, error %d\n", err);
779 module_init(ubi_init);
781 static void __exit ubi_exit(void)
785 for (i = 0; i < UBI_MAX_DEVICES; i++)
787 detach_mtd_dev(ubi_devices[i]);
788 kmem_cache_destroy(ubi_wl_entry_slab);
789 kmem_cache_destroy(ubi_ltree_slab);
790 misc_deregister(&ubi_ctrl_cdev);
791 class_remove_file(ubi_class, &ubi_version);
792 class_destroy(ubi_class);
794 module_exit(ubi_exit);
797 * bytes_str_to_int - convert a string representing number of bytes to an
799 * @str: the string to convert
801 * This function returns positive resulting integer in case of success and a
802 * negative error code in case of failure.
804 static int __init bytes_str_to_int(const char *str)
807 unsigned long result;
809 result = simple_strtoul(str, &endp, 0);
810 if (str == endp || result < 0) {
811 printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
824 if (endp[1] == 'i' && (endp[2] == '\0' ||
825 endp[2] == 'B' || endp[2] == 'b'))
830 printk(KERN_ERR "UBI error: incorrect bytes count: \"%s\"\n",
839 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
840 * @val: the parameter value to parse
843 * This function returns zero in case of success and a negative error code in
846 static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
849 struct mtd_dev_param *p;
850 char buf[MTD_PARAM_LEN_MAX];
851 char *pbuf = &buf[0];
852 char *tokens[3] = {NULL, NULL, NULL};
857 if (mtd_devs == UBI_MAX_DEVICES) {
858 printk(KERN_ERR "UBI error: too many parameters, max. is %d\n",
863 len = strnlen(val, MTD_PARAM_LEN_MAX);
864 if (len == MTD_PARAM_LEN_MAX) {
865 printk(KERN_ERR "UBI error: parameter \"%s\" is too long, "
866 "max. is %d\n", val, MTD_PARAM_LEN_MAX);
871 printk(KERN_WARNING "UBI warning: empty 'mtd=' parameter - "
878 /* Get rid of the final newline */
879 if (buf[len - 1] == '\n')
882 for (i = 0; i < 3; i++)
883 tokens[i] = strsep(&pbuf, ",");
886 printk(KERN_ERR "UBI error: too many arguments at \"%s\"\n",
891 p = &mtd_dev_param[mtd_devs];
892 strcpy(&p->name[0], tokens[0]);
895 p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
897 p->data_offs = bytes_str_to_int(tokens[2]);
899 if (p->vid_hdr_offs < 0)
900 return p->vid_hdr_offs;
901 if (p->data_offs < 0)
908 module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
909 MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
910 "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
911 "Multiple \"mtd\" parameters may be specified.\n"
912 "MTD devices may be specified by their number or name. "
913 "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
914 "specify UBI VID header position and data starting "
915 "position to be used by UBI.\n"
916 "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
917 "with name content using VID header offset 1984 and data "
918 "start 2048, and MTD device number 4 using default "
921 MODULE_VERSION(__stringify(UBI_VERSION));
922 MODULE_DESCRIPTION("UBI - Unsorted Block Images");
923 MODULE_AUTHOR("Artem Bityutskiy");
924 MODULE_LICENSE("GPL");