blob: 5490a73deca51617de88211a05e9971ebabec926 [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:
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200214 ubi_err("failed to initialize sysfs for %s, error %d",
215 ubi->ubi_name, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400216 return err;
217}
218
219/**
220 * ubi_sysfs_close - close sysfs for an UBI device.
221 * @ubi: UBI device description object
222 */
223static void ubi_sysfs_close(struct ubi_device *ubi)
224{
225 device_remove_file(&ubi->dev, &dev_bgt_enabled);
226 device_remove_file(&ubi->dev, &dev_min_io_size);
227 device_remove_file(&ubi->dev, &dev_max_vol_count);
228 device_remove_file(&ubi->dev, &dev_bad_peb_count);
229 device_remove_file(&ubi->dev, &dev_reserved_for_bad);
230 device_remove_file(&ubi->dev, &dev_max_ec);
231 device_remove_file(&ubi->dev, &dev_volumes_count);
232 device_remove_file(&ubi->dev, &dev_total_eraseblocks);
233 device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
234 device_remove_file(&ubi->dev, &dev_eraseblock_size);
235 device_unregister(&ubi->dev);
236}
237
238/**
239 * kill_volumes - destroy all volumes.
240 * @ubi: UBI device description object
241 */
242static void kill_volumes(struct ubi_device *ubi)
243{
244 int i;
245
246 for (i = 0; i < ubi->vtbl_slots; i++)
247 if (ubi->volumes[i])
248 ubi_free_volume(ubi, i);
249}
250
251/**
252 * uif_init - initialize user interfaces for an UBI device.
253 * @ubi: UBI device description object
254 *
255 * This function returns zero in case of success and a negative error code in
256 * case of failure.
257 */
258static int uif_init(struct ubi_device *ubi)
259{
260 int i, err;
261 dev_t dev;
262
263 mutex_init(&ubi->vtbl_mutex);
264 spin_lock_init(&ubi->volumes_lock);
265
266 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
267
268 /*
269 * Major numbers for the UBI character devices are allocated
270 * dynamically. Major numbers of volume character devices are
271 * equivalent to ones of the corresponding UBI character device. Minor
272 * numbers of UBI character devices are 0, while minor numbers of
273 * volume character devices start from 1. Thus, we allocate one major
274 * number and ubi->vtbl_slots + 1 minor numbers.
275 */
276 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
277 if (err) {
278 ubi_err("cannot register UBI character devices");
279 return err;
280 }
281
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200282 ubi_assert(MINOR(dev) == 0);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400283 cdev_init(&ubi->cdev, &ubi_cdev_operations);
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200284 dbg_msg("%s major is %u", ubi->ubi_name, MAJOR(dev));
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400285 ubi->cdev.owner = THIS_MODULE;
286
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400287 err = cdev_add(&ubi->cdev, dev, 1);
288 if (err) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200289 ubi_err("cannot add character device");
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400290 goto out_unreg;
291 }
292
293 err = ubi_sysfs_init(ubi);
294 if (err)
295 goto out_cdev;
296
297 for (i = 0; i < ubi->vtbl_slots; i++)
298 if (ubi->volumes[i]) {
299 err = ubi_add_volume(ubi, i);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200300 if (err) {
301 ubi_err("cannot add volume %d", i);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400302 goto out_volumes;
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200303 }
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400304 }
305
306 return 0;
307
308out_volumes:
309 kill_volumes(ubi);
310 ubi_sysfs_close(ubi);
311out_cdev:
312 cdev_del(&ubi->cdev);
313out_unreg:
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200314 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200315 ubi_err("cannot initialize UBI %s, error %d", ubi->ubi_name, err);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400316 return err;
317}
318
319/**
320 * uif_close - close user interfaces for an UBI device.
321 * @ubi: UBI device description object
322 */
323static void uif_close(struct ubi_device *ubi)
324{
325 kill_volumes(ubi);
326 ubi_sysfs_close(ubi);
327 cdev_del(&ubi->cdev);
Artem Bityutskiy49dfc292007-12-15 18:13:56 +0200328 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329}
330
331/**
332 * attach_by_scanning - attach an MTD device using scanning method.
333 * @ubi: UBI device descriptor
334 *
335 * This function returns zero in case of success and a negative error code in
336 * case of failure.
337 *
338 * Note, currently this is the only method to attach UBI devices. Hopefully in
339 * the future we'll have more scalable attaching methods and avoid full media
340 * scanning. But even in this case scanning will be needed as a fall-back
341 * attaching method if there are some on-flash table corruptions.
342 */
343static int attach_by_scanning(struct ubi_device *ubi)
344{
345 int err;
346 struct ubi_scan_info *si;
347
348 si = ubi_scan(ubi);
349 if (IS_ERR(si))
350 return PTR_ERR(si);
351
352 ubi->bad_peb_count = si->bad_peb_count;
353 ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
354 ubi->max_ec = si->max_ec;
355 ubi->mean_ec = si->mean_ec;
356
357 err = ubi_read_volume_table(ubi, si);
358 if (err)
359 goto out_si;
360
361 err = ubi_wl_init_scan(ubi, si);
362 if (err)
363 goto out_vtbl;
364
365 err = ubi_eba_init_scan(ubi, si);
366 if (err)
367 goto out_wl;
368
369 ubi_scan_destroy_si(si);
370 return 0;
371
372out_wl:
373 ubi_wl_close(ubi);
374out_vtbl:
Vinit Agnihotrid7f0c4d2007-06-15 15:31:22 +0530375 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400376out_si:
377 ubi_scan_destroy_si(si);
378 return err;
379}
380
381/**
382 * io_init - initialize I/O unit for a given UBI device.
383 * @ubi: UBI device description object
384 *
385 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
386 * assumed:
387 * o EC header is always at offset zero - this cannot be changed;
388 * o VID header starts just after the EC header at the closest address
389 * aligned to @io->@hdrs_min_io_size;
390 * o data starts just after the VID header at the closest address aligned to
391 * @io->@min_io_size
392 *
393 * This function returns zero in case of success and a negative error code in
394 * case of failure.
395 */
396static int io_init(struct ubi_device *ubi)
397{
398 if (ubi->mtd->numeraseregions != 0) {
399 /*
400 * Some flashes have several erase regions. Different regions
401 * may have different eraseblock size and other
402 * characteristics. It looks like mostly multi-region flashes
403 * have one "main" region and one or more small regions to
404 * store boot loader code or boot parameters or whatever. I
405 * guess we should just pick the largest region. But this is
406 * not implemented.
407 */
408 ubi_err("multiple regions, not implemented");
409 return -EINVAL;
410 }
411
412 /*
413 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
414 * physical eraseblocks maximum.
415 */
416
417 ubi->peb_size = ubi->mtd->erasesize;
418 ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
419 ubi->flash_size = ubi->mtd->size;
420
421 if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
422 ubi->bad_allowed = 1;
423
424 ubi->min_io_size = ubi->mtd->writesize;
425 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
426
427 /* Make sure minimal I/O unit is power of 2 */
Vignesh Babu7753f162007-06-12 10:31:05 +0530428 if (!is_power_of_2(ubi->min_io_size)) {
Artem Bityutskiy01f7b302007-12-15 19:56:51 +0200429 ubi_err("min. I/O unit (%d) is not power of 2",
430 ubi->min_io_size);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400431 return -EINVAL;
432 }
433
434 ubi_assert(ubi->hdrs_min_io_size > 0);
435 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
436 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
437
438 /* Calculate default aligned sizes of EC and VID headers */
439 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
440 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
441
442 dbg_msg("min_io_size %d", ubi->min_io_size);
443 dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
444 dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
445 dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
446
447 if (ubi->vid_hdr_offset == 0)
448 /* Default offset */
449 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
450 ubi->ec_hdr_alsize;
451 else {
452 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
453 ~(ubi->hdrs_min_io_size - 1);
454 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
455 ubi->vid_hdr_aloffset;
456 }
457
458 /* Similar for the data offset */
459 if (ubi->leb_start == 0) {
460 ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
461 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
462 }
463
464 dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
465 dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
466 dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
467 dbg_msg("leb_start %d", ubi->leb_start);
468
469 /* The shift must be aligned to 32-bit boundary */
470 if (ubi->vid_hdr_shift % 4) {
471 ubi_err("unaligned VID header shift %d",
472 ubi->vid_hdr_shift);
473 return -EINVAL;
474 }
475
476 /* Check sanity */
477 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
478 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
479 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
480 ubi->leb_start % ubi->min_io_size) {
481 ubi_err("bad VID header (%d) or data offsets (%d)",
482 ubi->vid_hdr_offset, ubi->leb_start);
483 return -EINVAL;
484 }
485
486 /*
487 * It may happen that EC and VID headers are situated in one minimal
488 * I/O unit. In this case we can only accept this UBI image in
489 * read-only mode.
490 */
491 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
492 ubi_warn("EC and VID headers are in the same minimal I/O unit, "
493 "switch to read-only mode");
494 ubi->ro_mode = 1;
495 }
496
497 ubi->leb_size = ubi->peb_size - ubi->leb_start;
498
499 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
500 ubi_msg("MTD device %d is write-protected, attach in "
501 "read-only mode", ubi->mtd->index);
502 ubi->ro_mode = 1;
503 }
504
505 dbg_msg("leb_size %d", ubi->leb_size);
506 dbg_msg("ro_mode %d", ubi->ro_mode);
507
508 /*
509 * Note, ideally, we have to initialize ubi->bad_peb_count here. But
510 * unfortunately, MTD does not provide this information. We should loop
511 * over all physical eraseblocks and invoke mtd->block_is_bad() for
512 * each physical eraseblock. So, we skip ubi->bad_peb_count
513 * uninitialized and initialize it after scanning.
514 */
515
516 return 0;
517}
518
519/**
520 * attach_mtd_dev - attach an MTD device.
521 * @mtd_dev: MTD device name or number string
522 * @vid_hdr_offset: VID header offset
523 * @data_offset: data offset
524 *
525 * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
526 * MTD device name, and tries to open it by this name. If it is unable to open,
527 * it tries to convert @mtd_dev to an integer and open the MTD device by its
528 * number. Returns zero in case of success and a negative error code in case of
529 * failure.
530 */
531static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
532 int data_offset)
533{
534 struct ubi_device *ubi;
535 struct mtd_info *mtd;
536 int i, err;
537
538 mtd = get_mtd_device_nm(mtd_dev);
539 if (IS_ERR(mtd)) {
540 int mtd_num;
541 char *endp;
542
543 if (PTR_ERR(mtd) != -ENODEV)
544 return PTR_ERR(mtd);
545
546 /*
547 * Probably this is not MTD device name but MTD device number -
548 * check this out.
549 */
550 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
551 if (*endp != '\0' || mtd_dev == endp) {
552 ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
553 return -ENODEV;
554 }
555
556 mtd = get_mtd_device(NULL, mtd_num);
557 if (IS_ERR(mtd))
558 return PTR_ERR(mtd);
559 }
560
561 /* Check if we already have the same MTD device attached */
562 for (i = 0; i < ubi_devices_cnt; i++)
563 if (ubi_devices[i]->mtd->index == mtd->index) {
564 ubi_err("mtd%d is already attached to ubi%d",
565 mtd->index, i);
566 err = -EINVAL;
567 goto out_mtd;
568 }
569
570 ubi = ubi_devices[ubi_devices_cnt] = kzalloc(sizeof(struct ubi_device),
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300571 GFP_KERNEL);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400572 if (!ubi) {
573 err = -ENOMEM;
574 goto out_mtd;
575 }
576
577 ubi->ubi_num = ubi_devices_cnt;
578 ubi->mtd = mtd;
579
580 dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
581 ubi->mtd->index, ubi_devices_cnt, vid_hdr_offset, data_offset);
582
583 ubi->vid_hdr_offset = vid_hdr_offset;
584 ubi->leb_start = data_offset;
585 err = io_init(ubi);
586 if (err)
587 goto out_free;
588
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300589 mutex_init(&ubi->buf_mutex);
590 ubi->peb_buf1 = vmalloc(ubi->peb_size);
591 if (!ubi->peb_buf1)
592 goto out_free;
593
594 ubi->peb_buf2 = vmalloc(ubi->peb_size);
595 if (!ubi->peb_buf2)
596 goto out_free;
597
598#ifdef CONFIG_MTD_UBI_DEBUG
599 mutex_init(&ubi->dbg_buf_mutex);
600 ubi->dbg_peb_buf = vmalloc(ubi->peb_size);
601 if (!ubi->dbg_peb_buf)
602 goto out_free;
603#endif
604
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400605 err = attach_by_scanning(ubi);
606 if (err) {
607 dbg_err("failed to attach by scanning, error %d", err);
608 goto out_free;
609 }
610
611 err = uif_init(ubi);
612 if (err)
613 goto out_detach;
614
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400615 ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt);
616 ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
617 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
618 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
619 ubi->peb_size, ubi->peb_size >> 10);
620 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
621 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
622 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
623 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
624 ubi_msg("VID header offset: %d (aligned %d)",
625 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
626 ubi_msg("data offset: %d", ubi->leb_start);
627 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
628 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
629 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
630 ubi_msg("number of user volumes: %d",
631 ubi->vol_count - UBI_INT_VOL_COUNT);
632 ubi_msg("available PEBs: %d", ubi->avail_pebs);
633 ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
634 ubi_msg("number of PEBs reserved for bad PEB handling: %d",
635 ubi->beb_rsvd_pebs);
636 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
637
638 /* Enable the background thread */
639 if (!DBG_DISABLE_BGT) {
640 ubi->thread_enabled = 1;
641 wake_up_process(ubi->bgt_thread);
642 }
643
Vinit Agnihotria6ded482007-07-04 16:35:56 +0300644 ubi_devices_cnt += 1;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400645 return 0;
646
647out_detach:
648 ubi_eba_close(ubi);
649 ubi_wl_close(ubi);
Vinit Agnihotrid7f0c4d2007-06-15 15:31:22 +0530650 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400651out_free:
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300652 vfree(ubi->peb_buf1);
653 vfree(ubi->peb_buf2);
654#ifdef CONFIG_MTD_UBI_DEBUG
655 vfree(ubi->dbg_peb_buf);
656#endif
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400657 kfree(ubi);
658out_mtd:
659 put_mtd_device(mtd);
660 ubi_devices[ubi_devices_cnt] = NULL;
661 return err;
662}
663
664/**
665 * detach_mtd_dev - detach an MTD device.
666 * @ubi: UBI device description object
667 */
668static void detach_mtd_dev(struct ubi_device *ubi)
669{
670 int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
671
672 dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
673 uif_close(ubi);
674 ubi_eba_close(ubi);
675 ubi_wl_close(ubi);
Artem Bityutskiy92ad8f32007-05-06 16:12:54 +0300676 vfree(ubi->vtbl);
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400677 put_mtd_device(ubi->mtd);
Artem Bityutskiye88d6e102007-08-29 14:51:52 +0300678 vfree(ubi->peb_buf1);
679 vfree(ubi->peb_buf2);
680#ifdef CONFIG_MTD_UBI_DEBUG
681 vfree(ubi->dbg_peb_buf);
682#endif
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400683 kfree(ubi_devices[ubi_num]);
684 ubi_devices[ubi_num] = NULL;
685 ubi_devices_cnt -= 1;
686 ubi_assert(ubi_devices_cnt >= 0);
687 ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
688}
689
690static int __init ubi_init(void)
691{
692 int err, i, k;
693
694 /* Ensure that EC and VID headers have correct size */
695 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
696 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
697
698 if (mtd_devs > UBI_MAX_DEVICES) {
699 printk("UBI error: too many MTD devices, maximum is %d\n",
700 UBI_MAX_DEVICES);
701 return -EINVAL;
702 }
703
704 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
705 if (IS_ERR(ubi_class))
706 return PTR_ERR(ubi_class);
707
708 err = class_create_file(ubi_class, &ubi_version);
709 if (err)
710 goto out_class;
711
712 /* Attach MTD devices */
713 for (i = 0; i < mtd_devs; i++) {
714 struct mtd_dev_param *p = &mtd_dev_param[i];
715
716 cond_resched();
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400717 err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
718 if (err)
719 goto out_detach;
720 }
721
722 return 0;
723
724out_detach:
725 for (k = 0; k < i; k++)
726 detach_mtd_dev(ubi_devices[k]);
727 class_remove_file(ubi_class, &ubi_version);
728out_class:
729 class_destroy(ubi_class);
730 return err;
731}
732module_init(ubi_init);
733
734static void __exit ubi_exit(void)
735{
736 int i, n = ubi_devices_cnt;
737
738 for (i = 0; i < n; i++)
739 detach_mtd_dev(ubi_devices[i]);
740 class_remove_file(ubi_class, &ubi_version);
741 class_destroy(ubi_class);
742}
743module_exit(ubi_exit);
744
745/**
746 * bytes_str_to_int - convert a string representing number of bytes to an
747 * integer.
748 * @str: the string to convert
749 *
750 * This function returns positive resulting integer in case of success and a
751 * negative error code in case of failure.
752 */
753static int __init bytes_str_to_int(const char *str)
754{
755 char *endp;
756 unsigned long result;
757
758 result = simple_strtoul(str, &endp, 0);
759 if (str == endp || result < 0) {
760 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
761 return -EINVAL;
762 }
763
764 switch (*endp) {
765 case 'G':
766 result *= 1024;
767 case 'M':
768 result *= 1024;
769 case 'K':
770 case 'k':
771 result *= 1024;
772 if (endp[1] == 'i' && (endp[2] == '\0' ||
773 endp[2] == 'B' || endp[2] == 'b'))
774 endp += 2;
775 case '\0':
776 break;
777 default:
778 printk("UBI error: incorrect bytes count: \"%s\"\n", str);
779 return -EINVAL;
780 }
781
782 return result;
783}
784
785/**
786 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
787 * @val: the parameter value to parse
788 * @kp: not used
789 *
790 * This function returns zero in case of success and a negative error code in
791 * case of error.
792 */
793static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
794{
795 int i, len;
796 struct mtd_dev_param *p;
797 char buf[MTD_PARAM_LEN_MAX];
798 char *pbuf = &buf[0];
799 char *tokens[3] = {NULL, NULL, NULL};
800
801 if (mtd_devs == UBI_MAX_DEVICES) {
802 printk("UBI error: too many parameters, max. is %d\n",
803 UBI_MAX_DEVICES);
804 return -EINVAL;
805 }
806
807 len = strnlen(val, MTD_PARAM_LEN_MAX);
808 if (len == MTD_PARAM_LEN_MAX) {
809 printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
810 val, MTD_PARAM_LEN_MAX);
811 return -EINVAL;
812 }
813
814 if (len == 0) {
815 printk("UBI warning: empty 'mtd=' parameter - ignored\n");
816 return 0;
817 }
818
819 strcpy(buf, val);
820
821 /* Get rid of the final newline */
822 if (buf[len - 1] == '\n')
Artem Bityutskiy503990e2007-07-11 16:03:29 +0300823 buf[len - 1] = '\0';
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400824
825 for (i = 0; i < 3; i++)
826 tokens[i] = strsep(&pbuf, ",");
827
828 if (pbuf) {
829 printk("UBI error: too many arguments at \"%s\"\n", val);
830 return -EINVAL;
831 }
832
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400833 p = &mtd_dev_param[mtd_devs];
834 strcpy(&p->name[0], tokens[0]);
835
836 if (tokens[1])
837 p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
838 if (tokens[2])
839 p->data_offs = bytes_str_to_int(tokens[2]);
840
841 if (p->vid_hdr_offs < 0)
842 return p->vid_hdr_offs;
843 if (p->data_offs < 0)
844 return p->data_offs;
845
846 mtd_devs += 1;
847 return 0;
848}
849
850module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
851MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
852 "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
853 "Multiple \"mtd\" parameters may be specified.\n"
854 "MTD devices may be specified by their number or name. "
855 "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
856 "specify UBI VID header position and data starting "
857 "position to be used by UBI.\n"
858 "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
859 "with name content using VID header offset 1984 and data "
860 "start 2048, and MTD device number 4 using default "
861 "offsets");
862
863MODULE_VERSION(__stringify(UBI_VERSION));
864MODULE_DESCRIPTION("UBI - Unsorted Block Images");
865MODULE_AUTHOR("Artem Bityutskiy");
866MODULE_LICENSE("GPL");