blob: b0791f79505683b80f134d3e58e4da14bc2b0bb2 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
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/*
24 * This file includes UBI initialization and building of UBI devices. At the
25 * moment UBI devices may only be added while UBI is initialized, but dynamic
26 * device add/remove functionality is planned. Also, at the moment we only
27 * attach UBI devices by scanning, which will become a bottleneck when flashes
28 * reach certain large size. Then one may improve UBI and add other methods.
29 */
30
31#include <linux/err.h>
32#include <linux/module.h>
33#include <linux/moduleparam.h>
34#include <linux/stringify.h>
35#include <linux/stat.h>
Vignesh Babu7753f162007-06-12 10:31:05 +053036#include <linux/log2.h>
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040037#include "ubi.h"
38
39/* Maximum length of the 'mtd=' parameter */
40#define MTD_PARAM_LEN_MAX 64
41
42/**
43 * struct mtd_dev_param - MTD device parameter description data structure.
44 * @name: MTD device name or number string
45 * @vid_hdr_offs: VID header offset
46 * @data_offs: data offset
47 */
48struct mtd_dev_param
49{
50 char name[MTD_PARAM_LEN_MAX];
51 int vid_hdr_offs;
52 int data_offs;
53};
54
55/* Numbers of elements set in the @mtd_dev_param array */
56static int mtd_devs = 0;
57
58/* MTD devices specification parameters */
59static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
60
61/* Number of UBI devices in system */
62int ubi_devices_cnt;
63
64/* All UBI devices in system */
65struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
66
67/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
68struct class *ubi_class;
69
70/* "Show" method for files in '/<sysfs>/class/ubi/' */
71static ssize_t ubi_version_show(struct class *class, char *buf)
72{
73 return sprintf(buf, "%d\n", UBI_VERSION);
74}
75
76/* UBI version attribute ('/<sysfs>/class/ubi/version') */
77static struct class_attribute ubi_version =
78 __ATTR(version, S_IRUGO, ubi_version_show, NULL);
79
80static ssize_t dev_attribute_show(struct device *dev,
81 struct device_attribute *attr, char *buf);
82
83/* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
84static struct device_attribute dev_eraseblock_size =
85 __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
86static struct device_attribute dev_avail_eraseblocks =
87 __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
88static struct device_attribute dev_total_eraseblocks =
89 __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
90static struct device_attribute dev_volumes_count =
91 __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
92static struct device_attribute dev_max_ec =
93 __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
94static struct device_attribute dev_reserved_for_bad =
95 __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
96static struct device_attribute dev_bad_peb_count =
97 __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
98static struct device_attribute dev_max_vol_count =
99 __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
100static struct device_attribute dev_min_io_size =
101 __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
102static struct device_attribute dev_bgt_enabled =
103 __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
104
105/* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
106static ssize_t dev_attribute_show(struct device *dev,
107 struct device_attribute *attr, char *buf)
108{
109 const struct ubi_device *ubi;
110
111 ubi = container_of(dev, struct ubi_device, dev);
112 if (attr == &dev_eraseblock_size)
113 return sprintf(buf, "%d\n", ubi->leb_size);
114 else if (attr == &dev_avail_eraseblocks)
115 return sprintf(buf, "%d\n", ubi->avail_pebs);
116 else if (attr == &dev_total_eraseblocks)
117 return sprintf(buf, "%d\n", ubi->good_peb_count);
118 else if (attr == &dev_volumes_count)
119 return sprintf(buf, "%d\n", ubi->vol_count);
120 else if (attr == &dev_max_ec)
121 return sprintf(buf, "%d\n", ubi->max_ec);
122 else if (attr == &dev_reserved_for_bad)
123 return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
124 else if (attr == &dev_bad_peb_count)
125 return sprintf(buf, "%d\n", ubi->bad_peb_count);
126 else if (attr == &dev_max_vol_count)
127 return sprintf(buf, "%d\n", ubi->vtbl_slots);
128 else if (attr == &dev_min_io_size)
129 return sprintf(buf, "%d\n", ubi->min_io_size);
130 else if (attr == &dev_bgt_enabled)
131 return sprintf(buf, "%d\n", ubi->thread_enabled);
132 else
133 BUG();
134
135 return 0;
136}
137
138/* Fake "release" method for UBI devices */
139static void dev_release(struct device *dev) { }
140
141/**
142 * ubi_sysfs_init - initialize sysfs for an UBI device.
143 * @ubi: UBI device description object
144 *
145 * This function returns zero in case of success and a negative error code in
146 * case of failure.
147 */
148static int ubi_sysfs_init(struct ubi_device *ubi)
149{
150 int err;
151
152 ubi->dev.release = dev_release;
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200153 ubi->dev.devt = ubi->cdev.dev;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400154 ubi->dev.class = ubi_class;
155 sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
156 err = device_register(&ubi->dev);
157 if (err)
158 goto out;
159
160 err = device_create_file(&ubi->dev, &dev_eraseblock_size);
161 if (err)
162 goto out_unregister;
163 err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
164 if (err)
165 goto out_eraseblock_size;
166 err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
167 if (err)
168 goto out_avail_eraseblocks;
169 err = device_create_file(&ubi->dev, &dev_volumes_count);
170 if (err)
171 goto out_total_eraseblocks;
172 err = device_create_file(&ubi->dev, &dev_max_ec);
173 if (err)
174 goto out_volumes_count;
175 err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
176 if (err)
177 goto out_volumes_max_ec;
178 err = device_create_file(&ubi->dev, &dev_bad_peb_count);
179 if (err)
180 goto out_reserved_for_bad;
181 err = device_create_file(&ubi->dev, &dev_max_vol_count);
182 if (err)
183 goto out_bad_peb_count;
184 err = device_create_file(&ubi->dev, &dev_min_io_size);
185 if (err)
186 goto out_max_vol_count;
187 err = device_create_file(&ubi->dev, &dev_bgt_enabled);
188 if (err)
189 goto out_min_io_size;
190
191 return 0;
192
193out_min_io_size:
194 device_remove_file(&ubi->dev, &dev_min_io_size);
195out_max_vol_count:
196 device_remove_file(&ubi->dev, &dev_max_vol_count);
197out_bad_peb_count:
198 device_remove_file(&ubi->dev, &dev_bad_peb_count);
199out_reserved_for_bad:
200 device_remove_file(&ubi->dev, &dev_reserved_for_bad);
201out_volumes_max_ec:
202 device_remove_file(&ubi->dev, &dev_max_ec);
203out_volumes_count:
204 device_remove_file(&ubi->dev, &dev_volumes_count);
205out_total_eraseblocks:
206 device_remove_file(&ubi->dev, &dev_total_eraseblocks);
207out_avail_eraseblocks:
208 device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
209out_eraseblock_size:
210 device_remove_file(&ubi->dev, &dev_eraseblock_size);
211out_unregister:
212 device_unregister(&ubi->dev);
213out:
214 ubi_err("failed to initialize sysfs for %s", ubi->ubi_name);
215 return err;
216}
217
218/**
219 * ubi_sysfs_close - close sysfs for an UBI device.
220 * @ubi: UBI device description object
221 */
222static void ubi_sysfs_close(struct ubi_device *ubi)
223{
224 device_remove_file(&ubi->dev, &dev_bgt_enabled);
225 device_remove_file(&ubi->dev, &dev_min_io_size);
226 device_remove_file(&ubi->dev, &dev_max_vol_count);
227 device_remove_file(&ubi->dev, &dev_bad_peb_count);
228 device_remove_file(&ubi->dev, &dev_reserved_for_bad);
229 device_remove_file(&ubi->dev, &dev_max_ec);
230 device_remove_file(&ubi->dev, &dev_volumes_count);
231 device_remove_file(&ubi->dev, &dev_total_eraseblocks);
232 device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
233 device_remove_file(&ubi->dev, &dev_eraseblock_size);
234 device_unregister(&ubi->dev);
235}
236
237/**
238 * kill_volumes - destroy all volumes.
239 * @ubi: UBI device description object
240 */
241static void kill_volumes(struct ubi_device *ubi)
242{
243 int i;
244
245 for (i = 0; i < ubi->vtbl_slots; i++)
246 if (ubi->volumes[i])
247 ubi_free_volume(ubi, i);
248}
249
250/**
251 * uif_init - initialize user interfaces for an UBI device.
252 * @ubi: UBI device description object
253 *
254 * This function returns zero in case of success and a negative error code in
255 * case of failure.
256 */
257static int uif_init(struct ubi_device *ubi)
258{
259 int i, err;
260 dev_t dev;
261
262 mutex_init(&ubi->vtbl_mutex);
263 spin_lock_init(&ubi->volumes_lock);
264
265 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
266
267 /*
268 * Major numbers for the UBI character devices are allocated
269 * dynamically. Major numbers of volume character devices are
270 * equivalent to ones of the corresponding UBI character device. Minor
271 * numbers of UBI character devices are 0, while minor numbers of
272 * volume character devices start from 1. Thus, we allocate one major
273 * number and ubi->vtbl_slots + 1 minor numbers.
274 */
275 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
276 if (err) {
277 ubi_err("cannot register UBI character devices");
278 return err;
279 }
280
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200281 ubi_assert(MINOR(dev) == 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400282 cdev_init(&ubi->cdev, &ubi_cdev_operations);
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200283 dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400284 ubi->cdev.owner = THIS_MODULE;
285
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400286 err = cdev_add(&ubi->cdev, dev, 1);
287 if (err) {
288 ubi_err("cannot add character device %s", ubi->ubi_name);
289 goto out_unreg;
290 }
291
292 err = ubi_sysfs_init(ubi);
293 if (err)
294 goto out_cdev;
295
296 for (i = 0; i < ubi->vtbl_slots; i++)
297 if (ubi->volumes[i]) {
298 err = ubi_add_volume(ubi, i);
299 if (err)
300 goto out_volumes;
301 }
302
303 return 0;
304
305out_volumes:
306 kill_volumes(ubi);
307 ubi_sysfs_close(ubi);
308out_cdev:
309 cdev_del(&ubi->cdev);
310out_unreg:
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200311 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400312 return err;
313}
314
315/**
316 * uif_close - close user interfaces for an UBI device.
317 * @ubi: UBI device description object
318 */
319static void uif_close(struct ubi_device *ubi)
320{
321 kill_volumes(ubi);
322 ubi_sysfs_close(ubi);
323 cdev_del(&ubi->cdev);
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200324 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400325}
326
327/**
328 * attach_by_scanning - attach an MTD device using scanning method.
329 * @ubi: UBI device descriptor
330 *
331 * This function returns zero in case of success and a negative error code in
332 * case of failure.
333 *
334 * Note, currently this is the only method to attach UBI devices. Hopefully in
335 * the future we'll have more scalable attaching methods and avoid full media
336 * scanning. But even in this case scanning will be needed as a fall-back
337 * attaching method if there are some on-flash table corruptions.
338 */
339static int attach_by_scanning(struct ubi_device *ubi)
340{
341 int err;
342 struct ubi_scan_info *si;
343
344 si = ubi_scan(ubi);
345 if (IS_ERR(si))
346 return PTR_ERR(si);
347
348 ubi->bad_peb_count = si->bad_peb_count;
349 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
350 ubi->max_ec = si->max_ec;
351 ubi->mean_ec = si->mean_ec;
352
353 err = ubi_read_volume_table(ubi, si);
354 if (err)
355 goto out_si;
356
357 err = ubi_wl_init_scan(ubi, si);
358 if (err)
359 goto out_vtbl;
360
361 err = ubi_eba_init_scan(ubi, si);
362 if (err)
363 goto out_wl;
364
365 ubi_scan_destroy_si(si);
366 return 0;
367
368out_wl:
369 ubi_wl_close(ubi);
370out_vtbl:
Vinit Agnihotrid7f0c4d2007-06-15 15:31:22 +0530371 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400372out_si:
373 ubi_scan_destroy_si(si);
374 return err;
375}
376
377/**
378 * io_init - initialize I/O unit for a given UBI device.
379 * @ubi: UBI device description object
380 *
381 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
382 * assumed:
383 * o EC header is always at offset zero - this cannot be changed;
384 * o VID header starts just after the EC header at the closest address
385 * aligned to @io->@hdrs_min_io_size;
386 * o data starts just after the VID header at the closest address aligned to
387 * @io->@min_io_size
388 *
389 * This function returns zero in case of success and a negative error code in
390 * case of failure.
391 */
392static int io_init(struct ubi_device *ubi)
393{
394 if (ubi->mtd->numeraseregions != 0) {
395 /*
396 * Some flashes have several erase regions. Different regions
397 * may have different eraseblock size and other
398 * characteristics. It looks like mostly multi-region flashes
399 * have one "main" region and one or more small regions to
400 * store boot loader code or boot parameters or whatever. I
401 * guess we should just pick the largest region. But this is
402 * not implemented.
403 */
404 ubi_err("multiple regions, not implemented");
405 return -EINVAL;
406 }
407
408 /*
409 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
410 * physical eraseblocks maximum.
411 */
412
413 ubi->peb_size = ubi->mtd->erasesize;
414 ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
415 ubi->flash_size = ubi->mtd->size;
416
417 if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
418 ubi->bad_allowed = 1;
419
420 ubi->min_io_size = ubi->mtd->writesize;
421 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
422
423 /* Make sure minimal I/O unit is power of 2 */
Vignesh Babu7753f162007-06-12 10:31:05 +0530424 if (!is_power_of_2(ubi->min_io_size)) {
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400425 ubi_err("bad min. I/O unit");
426 return -EINVAL;
427 }
428
429 ubi_assert(ubi->hdrs_min_io_size > 0);
430 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
431 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
432
433 /* Calculate default aligned sizes of EC and VID headers */
434 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
435 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
436
437 dbg_msg("min_io_size %d", ubi->min_io_size);
438 dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
439 dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
440 dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
441
442 if (ubi->vid_hdr_offset == 0)
443 /* Default offset */
444 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
445 ubi->ec_hdr_alsize;
446 else {
447 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
448 ~(ubi->hdrs_min_io_size - 1);
449 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
450 ubi->vid_hdr_aloffset;
451 }
452
453 /* Similar for the data offset */
454 if (ubi->leb_start == 0) {
455 ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
456 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
457 }
458
459 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
460 dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
461 dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
462 dbg_msg("leb_start %d", ubi->leb_start);
463
464 /* The shift must be aligned to 32-bit boundary */
465 if (ubi->vid_hdr_shift % 4) {
466 ubi_err("unaligned VID header shift %d",
467 ubi->vid_hdr_shift);
468 return -EINVAL;
469 }
470
471 /* Check sanity */
472 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
473 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
474 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
475 ubi->leb_start % ubi->min_io_size) {
476 ubi_err("bad VID header (%d) or data offsets (%d)",
477 ubi->vid_hdr_offset, ubi->leb_start);
478 return -EINVAL;
479 }
480
481 /*
482 * It may happen that EC and VID headers are situated in one minimal
483 * I/O unit. In this case we can only accept this UBI image in
484 * read-only mode.
485 */
486 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
487 ubi_warn("EC and VID headers are in the same minimal I/O unit, "
488 "switch to read-only mode");
489 ubi->ro_mode = 1;
490 }
491
492 ubi->leb_size = ubi->peb_size - ubi->leb_start;
493
494 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
495 ubi_msg("MTD device %d is write-protected, attach in "
496 "read-only mode", ubi->mtd->index);
497 ubi->ro_mode = 1;
498 }
499
500 dbg_msg("leb_size %d", ubi->leb_size);
501 dbg_msg("ro_mode %d", ubi->ro_mode);
502
503 /*
504 * Note, ideally, we have to initialize ubi->bad_peb_count here. But
505 * unfortunately, MTD does not provide this information. We should loop
506 * over all physical eraseblocks and invoke mtd->block_is_bad() for
507 * each physical eraseblock. So, we skip ubi->bad_peb_count
508 * uninitialized and initialize it after scanning.
509 */
510
511 return 0;
512}
513
514/**
515 * attach_mtd_dev - attach an MTD device.
516 * @mtd_dev: MTD device name or number string
517 * @vid_hdr_offset: VID header offset
518 * @data_offset: data offset
519 *
520 * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
521 * MTD device name, and tries to open it by this name. If it is unable to open,
522 * it tries to convert @mtd_dev to an integer and open the MTD device by its
523 * number. Returns zero in case of success and a negative error code in case of
524 * failure.
525 */
526static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
527 int data_offset)
528{
529 struct ubi_device *ubi;
530 struct mtd_info *mtd;
531 int i, err;
532
533 mtd = get_mtd_device_nm(mtd_dev);
534 if (IS_ERR(mtd)) {
535 int mtd_num;
536 char *endp;
537
538 if (PTR_ERR(mtd) != -ENODEV)
539 return PTR_ERR(mtd);
540
541 /*
542 * Probably this is not MTD device name but MTD device number -
543 * check this out.
544 */
545 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
546 if (*endp != '\0' || mtd_dev == endp) {
547 ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
548 return -ENODEV;
549 }
550
551 mtd = get_mtd_device(NULL, mtd_num);
552 if (IS_ERR(mtd))
553 return PTR_ERR(mtd);
554 }
555
556 /* Check if we already have the same MTD device attached */
557 for (i = 0; i < ubi_devices_cnt; i++)
558 if (ubi_devices[i]->mtd->index == mtd->index) {
559 ubi_err("mtd%d is already attached to ubi%d",
560 mtd->index, i);
561 err = -EINVAL;
562 goto out_mtd;
563 }
564
565 ubi = ubi_devices[ubi_devices_cnt] = kzalloc(sizeof(struct ubi_device),
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300566 GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400567 if (!ubi) {
568 err = -ENOMEM;
569 goto out_mtd;
570 }
571
572 ubi->ubi_num = ubi_devices_cnt;
573 ubi->mtd = mtd;
574
575 dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
576 ubi->mtd->index, ubi_devices_cnt, vid_hdr_offset, data_offset);
577
578 ubi->vid_hdr_offset = vid_hdr_offset;
579 ubi->leb_start = data_offset;
580 err = io_init(ubi);
581 if (err)
582 goto out_free;
583
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300584 mutex_init(&ubi->buf_mutex);
585 ubi->peb_buf1 = vmalloc(ubi->peb_size);
586 if (!ubi->peb_buf1)
587 goto out_free;
588
589 ubi->peb_buf2 = vmalloc(ubi->peb_size);
590 if (!ubi->peb_buf2)
591 goto out_free;
592
593#ifdef CONFIG_MTD_UBI_DEBUG
594 mutex_init(&ubi->dbg_buf_mutex);
595 ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
596 if (!ubi->dbg_peb_buf)
597 goto out_free;
598#endif
599
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400600 err = attach_by_scanning(ubi);
601 if (err) {
602 dbg_err("failed to attach by scanning, error %d", err);
603 goto out_free;
604 }
605
606 err = uif_init(ubi);
607 if (err)
608 goto out_detach;
609
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400610 ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt);
611 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
612 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
613 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
614 ubi->peb_size, ubi->peb_size >> 10);
615 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
616 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
617 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
618 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
619 ubi_msg("VID header offset: %d (aligned %d)",
620 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
621 ubi_msg("data offset: %d", ubi->leb_start);
622 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
623 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
624 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
625 ubi_msg("number of user volumes: %d",
626 ubi->vol_count - UBI_INT_VOL_COUNT);
627 ubi_msg("available PEBs: %d", ubi->avail_pebs);
628 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
629 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
630 ubi->beb_rsvd_pebs);
631 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
632
633 /* Enable the background thread */
634 if (!DBG_DISABLE_BGT) {
635 ubi->thread_enabled = 1;
636 wake_up_process(ubi->bgt_thread);
637 }
638
Vinit Agnihotria6ded482007-07-04 16:35:56 +0300639 ubi_devices_cnt += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400640 return 0;
641
642out_detach:
643 ubi_eba_close(ubi);
644 ubi_wl_close(ubi);
Vinit Agnihotrid7f0c4d2007-06-15 15:31:22 +0530645 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400646out_free:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300647 vfree(ubi->peb_buf1);
648 vfree(ubi->peb_buf2);
649#ifdef CONFIG_MTD_UBI_DEBUG
650 vfree(ubi->dbg_peb_buf);
651#endif
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400652 kfree(ubi);
653out_mtd:
654 put_mtd_device(mtd);
655 ubi_devices[ubi_devices_cnt] = NULL;
656 return err;
657}
658
659/**
660 * detach_mtd_dev - detach an MTD device.
661 * @ubi: UBI device description object
662 */
663static void detach_mtd_dev(struct ubi_device *ubi)
664{
665 int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
666
667 dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
668 uif_close(ubi);
669 ubi_eba_close(ubi);
670 ubi_wl_close(ubi);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300671 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400672 put_mtd_device(ubi->mtd);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300673 vfree(ubi->peb_buf1);
674 vfree(ubi->peb_buf2);
675#ifdef CONFIG_MTD_UBI_DEBUG
676 vfree(ubi->dbg_peb_buf);
677#endif
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400678 kfree(ubi_devices[ubi_num]);
679 ubi_devices[ubi_num] = NULL;
680 ubi_devices_cnt -= 1;
681 ubi_assert(ubi_devices_cnt >= 0);
682 ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
683}
684
685static int __init ubi_init(void)
686{
687 int err, i, k;
688
689 /* Ensure that EC and VID headers have correct size */
690 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
691 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
692
693 if (mtd_devs > UBI_MAX_DEVICES) {
694 printk("UBI error: too many MTD devices, maximum is %d\n",
695 UBI_MAX_DEVICES);
696 return -EINVAL;
697 }
698
699 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
700 if (IS_ERR(ubi_class))
701 return PTR_ERR(ubi_class);
702
703 err = class_create_file(ubi_class, &ubi_version);
704 if (err)
705 goto out_class;
706
707 /* Attach MTD devices */
708 for (i = 0; i < mtd_devs; i++) {
709 struct mtd_dev_param *p = &mtd_dev_param[i];
710
711 cond_resched();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400712 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
713 if (err)
714 goto out_detach;
715 }
716
717 return 0;
718
719out_detach:
720 for (k = 0; k < i; k++)
721 detach_mtd_dev(ubi_devices[k]);
722 class_remove_file(ubi_class, &ubi_version);
723out_class:
724 class_destroy(ubi_class);
725 return err;
726}
727module_init(ubi_init);
728
729static void __exit ubi_exit(void)
730{
731 int i, n = ubi_devices_cnt;
732
733 for (i = 0; i < n; i++)
734 detach_mtd_dev(ubi_devices[i]);
735 class_remove_file(ubi_class, &ubi_version);
736 class_destroy(ubi_class);
737}
738module_exit(ubi_exit);
739
740/**
741 * bytes_str_to_int - convert a string representing number of bytes to an
742 * integer.
743 * @str: the string to convert
744 *
745 * This function returns positive resulting integer in case of success and a
746 * negative error code in case of failure.
747 */
748static int __init bytes_str_to_int(const char *str)
749{
750 char *endp;
751 unsigned long result;
752
753 result = simple_strtoul(str, &endp, 0);
754 if (str == endp || result < 0) {
755 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
756 return -EINVAL;
757 }
758
759 switch (*endp) {
760 case 'G':
761 result *= 1024;
762 case 'M':
763 result *= 1024;
764 case 'K':
765 case 'k':
766 result *= 1024;
767 if (endp[1] == 'i' && (endp[2] == '\0' ||
768 endp[2] == 'B' || endp[2] == 'b'))
769 endp += 2;
770 case '\0':
771 break;
772 default:
773 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
774 return -EINVAL;
775 }
776
777 return result;
778}
779
780/**
781 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
782 * @val: the parameter value to parse
783 * @kp: not used
784 *
785 * This function returns zero in case of success and a negative error code in
786 * case of error.
787 */
788static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
789{
790 int i, len;
791 struct mtd_dev_param *p;
792 char buf[MTD_PARAM_LEN_MAX];
793 char *pbuf = &buf[0];
794 char *tokens[3] = {NULL, NULL, NULL};
795
796 if (mtd_devs == UBI_MAX_DEVICES) {
797 printk("UBI error: too many parameters, max. is %d\n",
798 UBI_MAX_DEVICES);
799 return -EINVAL;
800 }
801
802 len = strnlen(val, MTD_PARAM_LEN_MAX);
803 if (len == MTD_PARAM_LEN_MAX) {
804 printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
805 val, MTD_PARAM_LEN_MAX);
806 return -EINVAL;
807 }
808
809 if (len == 0) {
810 printk("UBI warning: empty 'mtd=' parameter - ignored\n");
811 return 0;
812 }
813
814 strcpy(buf, val);
815
816 /* Get rid of the final newline */
817 if (buf[len - 1] == '\n')
Artem Bityutskiy503990e2007-07-11 16:03:29 +0300818 buf[len - 1] = '\0';
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400819
820 for (i = 0; i < 3; i++)
821 tokens[i] = strsep(&pbuf, ",");
822
823 if (pbuf) {
824 printk("UBI error: too many arguments at \"%s\"\n", val);
825 return -EINVAL;
826 }
827
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400828 p = &mtd_dev_param[mtd_devs];
829 strcpy(&p->name[0], tokens[0]);
830
831 if (tokens[1])
832 p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
833 if (tokens[2])
834 p->data_offs = bytes_str_to_int(tokens[2]);
835
836 if (p->vid_hdr_offs < 0)
837 return p->vid_hdr_offs;
838 if (p->data_offs < 0)
839 return p->data_offs;
840
841 mtd_devs += 1;
842 return 0;
843}
844
845module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
846MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
847 "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
848 "Multiple \"mtd\" parameters may be specified.\n"
849 "MTD devices may be specified by their number or name. "
850 "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
851 "specify UBI VID header position and data starting "
852 "position to be used by UBI.\n"
853 "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
854 "with name content using VID header offset 1984 and data "
855 "start 2048, and MTD device number 4 using default "
856 "offsets");
857
858MODULE_VERSION(__stringify(UBI_VERSION));
859MODULE_DESCRIPTION("UBI - Unsorted Block Images");
860MODULE_AUTHOR("Artem Bityutskiy");
861MODULE_LICENSE("GPL");