blob: a1941b8eb88ef8f2e37c73804f5c1d38ddc7244b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Park1d8dca62008-11-19 16:23:06 +01002/*
3 * Copyright (c) International Business Machines Corp., 2006
4 * Copyright (c) Nokia Corporation, 2007
5 *
Kyungmin Park1d8dca62008-11-19 16:23:06 +01006 * Author: Artem Bityutskiy (Битюцкий Артём),
7 * Frank Haverkamp
8 */
9
10/*
11 * This file includes UBI initialization and building of UBI devices.
12 *
13 * When UBI is initialized, it attaches all the MTD devices specified as the
14 * module load parameters or the kernel boot parameters. If MTD devices were
15 * specified, UBI does not attach any MTD device, but it is possible to do
16 * later using the "UBI control device".
Kyungmin Park1d8dca62008-11-19 16:23:06 +010017 */
18
Heiko Schocherf5895d12014-06-24 10:10:04 +020019#ifndef __UBOOT__
Simon Glass0f2af882020-05-10 11:40:05 -060020#include <log.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <dm/devres.h>
Kyungmin Park1d8dca62008-11-19 16:23:06 +010022#include <linux/module.h>
23#include <linux/moduleparam.h>
24#include <linux/stringify.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020025#include <linux/namei.h>
Kyungmin Park1d8dca62008-11-19 16:23:06 +010026#include <linux/stat.h>
27#include <linux/miscdevice.h>
28#include <linux/log2.h>
29#include <linux/kthread.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020030#include <linux/kernel.h>
31#include <linux/slab.h>
32#include <linux/major.h>
33#else
Masahiro Yamada78eeb912016-01-24 23:27:48 +090034#include <linux/bug.h>
Fabio Estevam0297d1e2015-11-05 12:43:39 -020035#include <linux/log2.h>
Simon Glassbdd5f812023-09-14 18:21:46 -060036#include <linux/printk.h>
Kyungmin Park1d8dca62008-11-19 16:23:06 +010037#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +020038#include <linux/err.h>
Kyungmin Park1d8dca62008-11-19 16:23:06 +010039#include <ubi_uboot.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020040#include <linux/mtd/partitions.h>
41
Kyungmin Park1d8dca62008-11-19 16:23:06 +010042#include "ubi.h"
43
Heiko Schocherf5895d12014-06-24 10:10:04 +020044/* Maximum length of the 'mtd=' parameter */
45#define MTD_PARAM_LEN_MAX 64
46
47/* Maximum number of comma-separated items in the 'mtd=' parameter */
48#define MTD_PARAM_MAX_COUNT 4
49
50/* Maximum value for the number of bad PEBs per 1024 PEBs */
51#define MAX_MTD_UBI_BEB_LIMIT 768
52
53#ifdef CONFIG_MTD_UBI_MODULE
54#define ubi_is_module() 1
55#else
56#define ubi_is_module() 0
57#endif
58
Stefan Roesecb65dc72009-06-04 16:55:34 +020059#if (CONFIG_SYS_MALLOC_LEN < (512 << 10))
60#error Malloc area too small for UBI, increase CONFIG_SYS_MALLOC_LEN to >= 512k
61#endif
62
Kyungmin Park1d8dca62008-11-19 16:23:06 +010063/**
64 * struct mtd_dev_param - MTD device parameter description data structure.
Heiko Schocherf5895d12014-06-24 10:10:04 +020065 * @name: MTD character device node path, MTD device name, or MTD device number
66 * string
Kyungmin Park1d8dca62008-11-19 16:23:06 +010067 * @vid_hdr_offs: VID header offset
Heiko Schocherf5895d12014-06-24 10:10:04 +020068 * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
Kyungmin Park1d8dca62008-11-19 16:23:06 +010069 */
Heiko Schocherf5895d12014-06-24 10:10:04 +020070struct mtd_dev_param {
Kyungmin Park1d8dca62008-11-19 16:23:06 +010071 char name[MTD_PARAM_LEN_MAX];
Heiko Schocherf5895d12014-06-24 10:10:04 +020072 int ubi_num;
Kyungmin Park1d8dca62008-11-19 16:23:06 +010073 int vid_hdr_offs;
Heiko Schocherf5895d12014-06-24 10:10:04 +020074 int max_beb_per1024;
Kyungmin Park1d8dca62008-11-19 16:23:06 +010075};
76
77/* Numbers of elements set in the @mtd_dev_param array */
Heiko Schocherf5895d12014-06-24 10:10:04 +020078static int __initdata mtd_devs;
Kyungmin Park1d8dca62008-11-19 16:23:06 +010079
80/* MTD devices specification parameters */
Heiko Schocherf5895d12014-06-24 10:10:04 +020081static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
82#ifndef __UBOOT__
83#ifdef CONFIG_MTD_UBI_FASTMAP
84/* UBI module parameter to enable fastmap automatically on non-fastmap images */
85static bool fm_autoconvert;
Heiko Schocher94b66de2015-10-22 06:19:21 +020086static bool fm_debug;
Heiko Schocherf5895d12014-06-24 10:10:04 +020087#endif
88#else
89#ifdef CONFIG_MTD_UBI_FASTMAP
Heiko Schocherf5895d12014-06-24 10:10:04 +020090static bool fm_autoconvert = CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT;
Heiko Schocher94b66de2015-10-22 06:19:21 +020091static bool fm_debug = CONFIG_MTD_UBI_FM_DEBUG;
Heiko Schocherf5895d12014-06-24 10:10:04 +020092#endif
Heiko Schocher94b66de2015-10-22 06:19:21 +020093#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +010094
Kyungmin Park1d8dca62008-11-19 16:23:06 +010095/* Slab cache for wear-leveling entries */
96struct kmem_cache *ubi_wl_entry_slab;
97
Heiko Schocherf5895d12014-06-24 10:10:04 +020098#ifndef __UBOOT__
Kyungmin Park1d8dca62008-11-19 16:23:06 +010099/* UBI control character device */
100static struct miscdevice ubi_ctrl_cdev = {
101 .minor = MISC_DYNAMIC_MINOR,
102 .name = "ubi_ctrl",
103 .fops = &ubi_ctrl_cdev_operations,
104};
105#endif
106
107/* All UBI devices in system */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200108#ifndef __UBOOT__
109static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
110#else
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100111struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
Heiko Schocherf5895d12014-06-24 10:10:04 +0200112#endif
Wolfgang Denk9d328a62021-09-27 17:42:38 +0200113
Heiko Schocherf5895d12014-06-24 10:10:04 +0200114#ifndef __UBOOT__
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100115/* Serializes UBI devices creations and removals */
116DEFINE_MUTEX(ubi_devices_mutex);
117
118/* Protects @ubi_devices and @ubi->ref_count */
119static DEFINE_SPINLOCK(ubi_devices_lock);
120
121/* "Show" method for files in '/<sysfs>/class/ubi/' */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200122static ssize_t ubi_version_show(struct class *class,
123 struct class_attribute *attr, char *buf)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100124{
125 return sprintf(buf, "%d\n", UBI_VERSION);
126}
127
128/* UBI version attribute ('/<sysfs>/class/ubi/version') */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200129static struct class_attribute ubi_class_attrs[] = {
130 __ATTR(version, S_IRUGO, ubi_version_show, NULL),
131 __ATTR_NULL
132};
133
134/* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
135struct class ubi_class = {
136 .name = UBI_NAME_STR,
137 .owner = THIS_MODULE,
138 .class_attrs = ubi_class_attrs,
139};
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100140
141static ssize_t dev_attribute_show(struct device *dev,
142 struct device_attribute *attr, char *buf);
143
144/* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
145static struct device_attribute dev_eraseblock_size =
146 __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
147static struct device_attribute dev_avail_eraseblocks =
148 __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
149static struct device_attribute dev_total_eraseblocks =
150 __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
151static struct device_attribute dev_volumes_count =
152 __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
153static struct device_attribute dev_max_ec =
154 __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
155static struct device_attribute dev_reserved_for_bad =
156 __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
157static struct device_attribute dev_bad_peb_count =
158 __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
159static struct device_attribute dev_max_vol_count =
160 __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
161static struct device_attribute dev_min_io_size =
162 __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
163static struct device_attribute dev_bgt_enabled =
164 __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
165static struct device_attribute dev_mtd_num =
166 __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
167#endif
168
169/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200170 * ubi_volume_notify - send a volume change notification.
171 * @ubi: UBI device description object
172 * @vol: volume description object of the changed volume
173 * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
174 *
175 * This is a helper function which notifies all subscribers about a volume
176 * change event (creation, removal, re-sizing, re-naming, updating). Returns
177 * zero in case of success and a negative error code in case of failure.
178 */
179int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
180{
Heiko Schocher94b66de2015-10-22 06:19:21 +0200181 int ret;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200182 struct ubi_notification nt;
183
184 ubi_do_get_device_info(ubi, &nt.di);
185 ubi_do_get_volume_info(ubi, vol, &nt.vi);
186
Heiko Schocherf5895d12014-06-24 10:10:04 +0200187 switch (ntype) {
188 case UBI_VOLUME_ADDED:
189 case UBI_VOLUME_REMOVED:
190 case UBI_VOLUME_RESIZED:
191 case UBI_VOLUME_RENAMED:
Heiko Schocher94b66de2015-10-22 06:19:21 +0200192 ret = ubi_update_fastmap(ubi);
193 if (ret)
194 ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200195 }
Heiko Schocher94b66de2015-10-22 06:19:21 +0200196
Heiko Schocherf5895d12014-06-24 10:10:04 +0200197 return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
198}
199
200/**
201 * ubi_notify_all - send a notification to all volumes.
202 * @ubi: UBI device description object
203 * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
204 * @nb: the notifier to call
205 *
206 * This function walks all volumes of UBI device @ubi and sends the @ntype
207 * notification for each volume. If @nb is %NULL, then all registered notifiers
208 * are called, otherwise only the @nb notifier is called. Returns the number of
209 * sent notifications.
210 */
211int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
212{
213 struct ubi_notification nt;
214 int i, count = 0;
215#ifndef __UBOOT__
216 int ret;
217#endif
218
219 ubi_do_get_device_info(ubi, &nt.di);
220
221 mutex_lock(&ubi->device_mutex);
222 for (i = 0; i < ubi->vtbl_slots; i++) {
223 /*
224 * Since the @ubi->device is locked, and we are not going to
225 * change @ubi->volumes, we do not have to lock
226 * @ubi->volumes_lock.
227 */
228 if (!ubi->volumes[i])
229 continue;
230
231 ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
232#ifndef __UBOOT__
233 if (nb)
234 nb->notifier_call(nb, ntype, &nt);
235 else
236 ret = blocking_notifier_call_chain(&ubi_notifiers, ntype,
237 &nt);
238#endif
239 count += 1;
240 }
241 mutex_unlock(&ubi->device_mutex);
242
243 return count;
244}
245
246/**
247 * ubi_enumerate_volumes - send "add" notification for all existing volumes.
248 * @nb: the notifier to call
249 *
250 * This function walks all UBI devices and volumes and sends the
251 * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
252 * registered notifiers are called, otherwise only the @nb notifier is called.
253 * Returns the number of sent notifications.
254 */
255int ubi_enumerate_volumes(struct notifier_block *nb)
256{
257 int i, count = 0;
258
259 /*
260 * Since the @ubi_devices_mutex is locked, and we are not going to
261 * change @ubi_devices, we do not have to lock @ubi_devices_lock.
262 */
263 for (i = 0; i < UBI_MAX_DEVICES; i++) {
264 struct ubi_device *ubi = ubi_devices[i];
265
266 if (!ubi)
267 continue;
268 count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
269 }
270
271 return count;
272}
273
274/**
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100275 * ubi_get_device - get UBI device.
276 * @ubi_num: UBI device number
277 *
278 * This function returns UBI device description object for UBI device number
279 * @ubi_num, or %NULL if the device does not exist. This function increases the
280 * device reference count to prevent removal of the device. In other words, the
281 * device cannot be removed if its reference count is not zero.
282 */
283struct ubi_device *ubi_get_device(int ubi_num)
284{
285 struct ubi_device *ubi;
286
287 spin_lock(&ubi_devices_lock);
288 ubi = ubi_devices[ubi_num];
289 if (ubi) {
290 ubi_assert(ubi->ref_count >= 0);
291 ubi->ref_count += 1;
292 get_device(&ubi->dev);
293 }
294 spin_unlock(&ubi_devices_lock);
295
296 return ubi;
297}
298
299/**
300 * ubi_put_device - drop an UBI device reference.
301 * @ubi: UBI device description object
302 */
303void ubi_put_device(struct ubi_device *ubi)
304{
305 spin_lock(&ubi_devices_lock);
306 ubi->ref_count -= 1;
307 put_device(&ubi->dev);
308 spin_unlock(&ubi_devices_lock);
309}
310
311/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312 * ubi_get_by_major - get UBI device by character device major number.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100313 * @major: major number
314 *
315 * This function is similar to 'ubi_get_device()', but it searches the device
316 * by its major number.
317 */
318struct ubi_device *ubi_get_by_major(int major)
319{
320 int i;
321 struct ubi_device *ubi;
322
323 spin_lock(&ubi_devices_lock);
324 for (i = 0; i < UBI_MAX_DEVICES; i++) {
325 ubi = ubi_devices[i];
326 if (ubi && MAJOR(ubi->cdev.dev) == major) {
327 ubi_assert(ubi->ref_count >= 0);
328 ubi->ref_count += 1;
329 get_device(&ubi->dev);
330 spin_unlock(&ubi_devices_lock);
331 return ubi;
332 }
333 }
334 spin_unlock(&ubi_devices_lock);
335
336 return NULL;
337}
338
339/**
340 * ubi_major2num - get UBI device number by character device major number.
341 * @major: major number
342 *
343 * This function searches UBI device number object by its major number. If UBI
344 * device was not found, this function returns -ENODEV, otherwise the UBI device
345 * number is returned.
346 */
347int ubi_major2num(int major)
348{
349 int i, ubi_num = -ENODEV;
350
351 spin_lock(&ubi_devices_lock);
352 for (i = 0; i < UBI_MAX_DEVICES; i++) {
353 struct ubi_device *ubi = ubi_devices[i];
354
355 if (ubi && MAJOR(ubi->cdev.dev) == major) {
356 ubi_num = ubi->ubi_num;
357 break;
358 }
359 }
360 spin_unlock(&ubi_devices_lock);
361
362 return ubi_num;
363}
364
Heiko Schocherf5895d12014-06-24 10:10:04 +0200365#ifndef __UBOOT__
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100366/* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
367static ssize_t dev_attribute_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
369{
370 ssize_t ret;
371 struct ubi_device *ubi;
372
373 /*
374 * The below code looks weird, but it actually makes sense. We get the
375 * UBI device reference from the contained 'struct ubi_device'. But it
376 * is unclear if the device was removed or not yet. Indeed, if the
377 * device was removed before we increased its reference count,
378 * 'ubi_get_device()' will return -ENODEV and we fail.
379 *
380 * Remember, 'struct ubi_device' is freed in the release function, so
381 * we still can use 'ubi->ubi_num'.
382 */
383 ubi = container_of(dev, struct ubi_device, dev);
384 ubi = ubi_get_device(ubi->ubi_num);
385 if (!ubi)
386 return -ENODEV;
387
388 if (attr == &dev_eraseblock_size)
389 ret = sprintf(buf, "%d\n", ubi->leb_size);
390 else if (attr == &dev_avail_eraseblocks)
391 ret = sprintf(buf, "%d\n", ubi->avail_pebs);
392 else if (attr == &dev_total_eraseblocks)
393 ret = sprintf(buf, "%d\n", ubi->good_peb_count);
394 else if (attr == &dev_volumes_count)
395 ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
396 else if (attr == &dev_max_ec)
397 ret = sprintf(buf, "%d\n", ubi->max_ec);
398 else if (attr == &dev_reserved_for_bad)
399 ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
400 else if (attr == &dev_bad_peb_count)
401 ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
402 else if (attr == &dev_max_vol_count)
403 ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
404 else if (attr == &dev_min_io_size)
405 ret = sprintf(buf, "%d\n", ubi->min_io_size);
406 else if (attr == &dev_bgt_enabled)
407 ret = sprintf(buf, "%d\n", ubi->thread_enabled);
408 else if (attr == &dev_mtd_num)
409 ret = sprintf(buf, "%d\n", ubi->mtd->index);
410 else
411 ret = -EINVAL;
412
413 ubi_put_device(ubi);
414 return ret;
415}
416
Heiko Schocher94b66de2015-10-22 06:19:21 +0200417static struct attribute *ubi_dev_attrs[] = {
418 &dev_eraseblock_size.attr,
419 &dev_avail_eraseblocks.attr,
420 &dev_total_eraseblocks.attr,
421 &dev_volumes_count.attr,
422 &dev_max_ec.attr,
423 &dev_reserved_for_bad.attr,
424 &dev_bad_peb_count.attr,
425 &dev_max_vol_count.attr,
426 &dev_min_io_size.attr,
427 &dev_bgt_enabled.attr,
428 &dev_mtd_num.attr,
429 NULL
430};
431ATTRIBUTE_GROUPS(ubi_dev);
432
Heiko Schocherf5895d12014-06-24 10:10:04 +0200433static void dev_release(struct device *dev)
434{
435 struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
436
437 kfree(ubi);
438}
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100439
440/**
441 * ubi_sysfs_init - initialize sysfs for an UBI device.
442 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200443 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
444 * taken
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100445 *
446 * This function returns zero in case of success and a negative error code in
447 * case of failure.
448 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200449static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100450{
451 int err;
452
453 ubi->dev.release = dev_release;
454 ubi->dev.devt = ubi->cdev.dev;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200455 ubi->dev.class = &ubi_class;
456 ubi->dev.groups = ubi_dev_groups;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200457 dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100458 err = device_register(&ubi->dev);
459 if (err)
460 return err;
461
Heiko Schocherf5895d12014-06-24 10:10:04 +0200462 *ref = 1;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200463 return 0;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100464}
465
466/**
467 * ubi_sysfs_close - close sysfs for an UBI device.
468 * @ubi: UBI device description object
469 */
470static void ubi_sysfs_close(struct ubi_device *ubi)
471{
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100472 device_unregister(&ubi->dev);
473}
474#endif
475
476/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200477 * kill_volumes - destroy all user volumes.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100478 * @ubi: UBI device description object
479 */
480static void kill_volumes(struct ubi_device *ubi)
481{
482 int i;
483
484 for (i = 0; i < ubi->vtbl_slots; i++)
485 if (ubi->volumes[i])
486 ubi_free_volume(ubi, ubi->volumes[i]);
487}
488
489/**
490 * uif_init - initialize user interfaces for an UBI device.
491 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200492 * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
493 * taken, otherwise set to %0
494 *
495 * This function initializes various user interfaces for an UBI device. If the
496 * initialization fails at an early stage, this function frees all the
497 * resources it allocated, returns an error, and @ref is set to %0. However,
498 * if the initialization fails after the UBI device was registered in the
499 * driver core subsystem, this function takes a reference to @ubi->dev, because
500 * otherwise the release function ('dev_release()') would free whole @ubi
501 * object. The @ref argument is set to %1 in this case. The caller has to put
502 * this reference.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100503 *
504 * This function returns zero in case of success and a negative error code in
505 * case of failure.
506 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200507static int uif_init(struct ubi_device *ubi, int *ref)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100508{
509 int i, err;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200510#ifndef __UBOOT__
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100511 dev_t dev;
512#endif
513
Heiko Schocherf5895d12014-06-24 10:10:04 +0200514 *ref = 0;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100515 sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
516
517 /*
518 * Major numbers for the UBI character devices are allocated
519 * dynamically. Major numbers of volume character devices are
520 * equivalent to ones of the corresponding UBI character device. Minor
521 * numbers of UBI character devices are 0, while minor numbers of
522 * volume character devices start from 1. Thus, we allocate one major
523 * number and ubi->vtbl_slots + 1 minor numbers.
524 */
525 err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
526 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200527 ubi_err(ubi, "cannot register UBI character devices");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100528 return err;
529 }
530
531 ubi_assert(MINOR(dev) == 0);
532 cdev_init(&ubi->cdev, &ubi_cdev_operations);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200533 dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100534 ubi->cdev.owner = THIS_MODULE;
535
536 err = cdev_add(&ubi->cdev, dev, 1);
537 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200538 ubi_err(ubi, "cannot add character device");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100539 goto out_unreg;
540 }
541
Heiko Schocherf5895d12014-06-24 10:10:04 +0200542 err = ubi_sysfs_init(ubi, ref);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100543 if (err)
544 goto out_sysfs;
545
546 for (i = 0; i < ubi->vtbl_slots; i++)
547 if (ubi->volumes[i]) {
548 err = ubi_add_volume(ubi, ubi->volumes[i]);
549 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200550 ubi_err(ubi, "cannot add volume %d", i);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100551 goto out_volumes;
552 }
553 }
554
555 return 0;
556
557out_volumes:
558 kill_volumes(ubi);
559out_sysfs:
Heiko Schocherf5895d12014-06-24 10:10:04 +0200560 if (*ref)
561 get_device(&ubi->dev);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100562 ubi_sysfs_close(ubi);
563 cdev_del(&ubi->cdev);
564out_unreg:
565 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200566 ubi_err(ubi, "cannot initialize UBI %s, error %d",
567 ubi->ubi_name, err);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100568 return err;
569}
570
571/**
572 * uif_close - close user interfaces for an UBI device.
573 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200574 *
575 * Note, since this function un-registers UBI volume device objects (@vol->dev),
576 * the memory allocated voe the volumes is freed as well (in the release
577 * function).
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100578 */
579static void uif_close(struct ubi_device *ubi)
580{
581 kill_volumes(ubi);
582 ubi_sysfs_close(ubi);
583 cdev_del(&ubi->cdev);
584 unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
585}
586
587/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200588 * ubi_free_internal_volumes - free internal volumes.
589 * @ubi: UBI device description object
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100590 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200591void ubi_free_internal_volumes(struct ubi_device *ubi)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100592{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200593 int i;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100594
Heiko Schocherf5895d12014-06-24 10:10:04 +0200595 for (i = ubi->vtbl_slots;
596 i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
597 kfree(ubi->volumes[i]->eba_tbl);
598 kfree(ubi->volumes[i]);
599 }
600}
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100601
Heiko Schocherf5895d12014-06-24 10:10:04 +0200602static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
603{
604 int limit, device_pebs;
605 uint64_t device_size;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100606
Heiko Schocherf5895d12014-06-24 10:10:04 +0200607 if (!max_beb_per1024)
608 return 0;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100609
Heiko Schocherf5895d12014-06-24 10:10:04 +0200610 /*
611 * Here we are using size of the entire flash chip and
612 * not just the MTD partition size because the maximum
613 * number of bad eraseblocks is a percentage of the
614 * whole device and bad eraseblocks are not fairly
615 * distributed over the flash chip. So the worst case
616 * is that all the bad eraseblocks of the chip are in
617 * the MTD partition we are attaching (ubi->mtd).
618 */
619 device_size = mtd_get_device_size(ubi->mtd);
620 device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
621 limit = mult_frac(device_pebs, max_beb_per1024, 1024);
Holger Bruncke5db8e72011-10-10 13:08:19 +0200622
Heiko Schocherf5895d12014-06-24 10:10:04 +0200623 /* Round it up */
624 if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
625 limit += 1;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100626
Heiko Schocherf5895d12014-06-24 10:10:04 +0200627 return limit;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100628}
629
630/**
Heiko Schocherf5895d12014-06-24 10:10:04 +0200631 * io_init - initialize I/O sub-system for a given UBI device.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100632 * @ubi: UBI device description object
Heiko Schocherf5895d12014-06-24 10:10:04 +0200633 * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100634 *
635 * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
636 * assumed:
637 * o EC header is always at offset zero - this cannot be changed;
638 * o VID header starts just after the EC header at the closest address
639 * aligned to @io->hdrs_min_io_size;
640 * o data starts just after the VID header at the closest address aligned to
641 * @io->min_io_size
642 *
643 * This function returns zero in case of success and a negative error code in
644 * case of failure.
645 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200646static int io_init(struct ubi_device *ubi, int max_beb_per1024)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100647{
Heiko Schocherf5895d12014-06-24 10:10:04 +0200648 dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
649 dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
650
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100651 if (ubi->mtd->numeraseregions != 0) {
652 /*
653 * Some flashes have several erase regions. Different regions
654 * may have different eraseblock size and other
655 * characteristics. It looks like mostly multi-region flashes
656 * have one "main" region and one or more small regions to
657 * store boot loader code or boot parameters or whatever. I
658 * guess we should just pick the largest region. But this is
659 * not implemented.
660 */
Heiko Schocher94b66de2015-10-22 06:19:21 +0200661 ubi_err(ubi, "multiple regions, not implemented");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100662 return -EINVAL;
663 }
664
665 if (ubi->vid_hdr_offset < 0)
666 return -EINVAL;
667
668 /*
669 * Note, in this implementation we support MTD devices with 0x7FFFFFFF
670 * physical eraseblocks maximum.
671 */
672
673 ubi->peb_size = ubi->mtd->erasesize;
Stefan Roesed4f92ac2009-06-29 13:30:50 +0200674 ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100675 ubi->flash_size = ubi->mtd->size;
676
Heiko Schocherf5895d12014-06-24 10:10:04 +0200677 if (mtd_can_have_bb(ubi->mtd)) {
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100678 ubi->bad_allowed = 1;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200679 ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
680 }
681
682 if (ubi->mtd->type == MTD_NORFLASH) {
683 ubi_assert(ubi->mtd->writesize == 1);
684 ubi->nor_flash = 1;
685 }
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100686
687 ubi->min_io_size = ubi->mtd->writesize;
688 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
689
690 /*
691 * Make sure minimal I/O unit is power of 2. Note, there is no
692 * fundamental reason for this assumption. It is just an optimization
693 * which allows us to avoid costly division operations.
694 */
695 if (!is_power_of_2(ubi->min_io_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200696 ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100697 ubi->min_io_size);
698 return -EINVAL;
699 }
700
701 ubi_assert(ubi->hdrs_min_io_size > 0);
702 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
703 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
704
Heiko Schocherf5895d12014-06-24 10:10:04 +0200705 ubi->max_write_size = ubi->mtd->writebufsize;
706 /*
707 * Maximum write size has to be greater or equivalent to min. I/O
708 * size, and be multiple of min. I/O size.
709 */
710 if (ubi->max_write_size < ubi->min_io_size ||
711 ubi->max_write_size % ubi->min_io_size ||
712 !is_power_of_2(ubi->max_write_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200713 ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200714 ubi->max_write_size, ubi->min_io_size);
715 return -EINVAL;
716 }
717
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100718 /* Calculate default aligned sizes of EC and VID headers */
719 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
720 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
721
Heiko Schocherf5895d12014-06-24 10:10:04 +0200722 dbg_gen("min_io_size %d", ubi->min_io_size);
723 dbg_gen("max_write_size %d", ubi->max_write_size);
724 dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
725 dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
726 dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100727
728 if (ubi->vid_hdr_offset == 0)
729 /* Default offset */
730 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
731 ubi->ec_hdr_alsize;
732 else {
733 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
734 ~(ubi->hdrs_min_io_size - 1);
735 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
736 ubi->vid_hdr_aloffset;
737 }
738
739 /* Similar for the data offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200740 ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100741 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
742
Heiko Schocherf5895d12014-06-24 10:10:04 +0200743 dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
744 dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
745 dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
746 dbg_gen("leb_start %d", ubi->leb_start);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100747
748 /* The shift must be aligned to 32-bit boundary */
749 if (ubi->vid_hdr_shift % 4) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200750 ubi_err(ubi, "unaligned VID header shift %d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100751 ubi->vid_hdr_shift);
752 return -EINVAL;
753 }
754
755 /* Check sanity */
756 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
757 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
758 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
759 ubi->leb_start & (ubi->min_io_size - 1)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200760 ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100761 ubi->vid_hdr_offset, ubi->leb_start);
762 return -EINVAL;
763 }
764
765 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200766 * Set maximum amount of physical erroneous eraseblocks to be 10%.
767 * Erroneous PEB are those which have read errors.
768 */
769 ubi->max_erroneous = ubi->peb_count / 10;
770 if (ubi->max_erroneous < 16)
771 ubi->max_erroneous = 16;
772 dbg_gen("max_erroneous %d", ubi->max_erroneous);
773
774 /*
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100775 * It may happen that EC and VID headers are situated in one minimal
776 * I/O unit. In this case we can only accept this UBI image in
777 * read-only mode.
778 */
779 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200780 ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100781 ubi->ro_mode = 1;
782 }
783
784 ubi->leb_size = ubi->peb_size - ubi->leb_start;
785
786 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200787 ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200788 ubi->mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100789 ubi->ro_mode = 1;
790 }
791
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100792 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200793 * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100794 * unfortunately, MTD does not provide this information. We should loop
795 * over all physical eraseblocks and invoke mtd->block_is_bad() for
Heiko Schocherf5895d12014-06-24 10:10:04 +0200796 * each physical eraseblock. So, we leave @ubi->bad_peb_count
797 * uninitialized so far.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100798 */
799
800 return 0;
801}
802
803/**
804 * autoresize - re-size the volume which has the "auto-resize" flag set.
805 * @ubi: UBI device description object
806 * @vol_id: ID of the volume to re-size
807 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200808 * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100809 * the volume table to the largest possible size. See comments in ubi-header.h
810 * for more description of the flag. Returns zero in case of success and a
811 * negative error code in case of failure.
812 */
813static int autoresize(struct ubi_device *ubi, int vol_id)
814{
815 struct ubi_volume_desc desc;
816 struct ubi_volume *vol = ubi->volumes[vol_id];
817 int err, old_reserved_pebs = vol->reserved_pebs;
818
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200820 ubi_warn(ubi, "skip auto-resize because of R/O mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200821 return 0;
822 }
823
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100824 /*
825 * Clear the auto-resize flag in the volume in-memory copy of the
Heiko Schocherf5895d12014-06-24 10:10:04 +0200826 * volume table, and 'ubi_resize_volume()' will propagate this change
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100827 * to the flash.
828 */
829 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
830
831 if (ubi->avail_pebs == 0) {
832 struct ubi_vtbl_record vtbl_rec;
833
834 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200835 * No available PEBs to re-size the volume, clear the flag on
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100836 * flash and exit.
837 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200838 vtbl_rec = ubi->vtbl[vol_id];
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100839 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
840 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200841 ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100842 vol_id);
843 } else {
844 desc.vol = vol;
845 err = ubi_resize_volume(&desc,
846 old_reserved_pebs + ubi->avail_pebs);
847 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200848 ubi_err(ubi, "cannot auto-resize volume %d",
849 vol_id);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100850 }
851
852 if (err)
853 return err;
854
Heiko Schocher94b66de2015-10-22 06:19:21 +0200855 ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
856 vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100857 return 0;
858}
859
860/**
861 * ubi_attach_mtd_dev - attach an MTD device.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200862 * @mtd: MTD device description object
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100863 * @ubi_num: number to assign to the new UBI device
864 * @vid_hdr_offset: VID header offset
Heiko Schocherf5895d12014-06-24 10:10:04 +0200865 * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100866 *
867 * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
868 * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
Heiko Schocherf5895d12014-06-24 10:10:04 +0200869 * which case this function finds a vacant device number and assigns it
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100870 * automatically. Returns the new UBI device number in case of success and a
871 * negative error code in case of failure.
872 *
873 * Note, the invocations of this function has to be serialized by the
874 * @ubi_devices_mutex.
875 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200876int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
877 int vid_hdr_offset, int max_beb_per1024)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100878{
879 struct ubi_device *ubi;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200880 int i, err, ref = 0;
881
882 if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
883 return -EINVAL;
884
885 if (!max_beb_per1024)
886 max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100887
888 /*
889 * Check if we already have the same MTD device attached.
890 *
891 * Note, this function assumes that UBI devices creations and deletions
892 * are serialized, so it does not take the &ubi_devices_lock.
893 */
894 for (i = 0; i < UBI_MAX_DEVICES; i++) {
895 ubi = ubi_devices[i];
896 if (ubi && mtd->index == ubi->mtd->index) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200897 ubi_err(ubi, "mtd%d is already attached to ubi%d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100898 mtd->index, i);
899 return -EEXIST;
900 }
901 }
902
903 /*
904 * Make sure this MTD device is not emulated on top of an UBI volume
905 * already. Well, generally this recursion works fine, but there are
906 * different problems like the UBI module takes a reference to itself
907 * by attaching (and thus, opening) the emulated MTD device. This
908 * results in inability to unload the module. And in general it makes
909 * no sense to attach emulated MTD devices, so we prohibit this.
910 */
911 if (mtd->type == MTD_UBIVOLUME) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200912 ubi_err(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200913 mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100914 return -EINVAL;
915 }
916
917 if (ubi_num == UBI_DEV_NUM_AUTO) {
918 /* Search for an empty slot in the @ubi_devices array */
919 for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
920 if (!ubi_devices[ubi_num])
921 break;
922 if (ubi_num == UBI_MAX_DEVICES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200923 ubi_err(ubi, "only %d UBI devices may be created",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200924 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100925 return -ENFILE;
926 }
927 } else {
928 if (ubi_num >= UBI_MAX_DEVICES)
929 return -EINVAL;
930
931 /* Make sure ubi_num is not busy */
932 if (ubi_devices[ubi_num]) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200933 ubi_err(ubi, "already exists");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100934 return -EEXIST;
935 }
936 }
937
938 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
939 if (!ubi)
940 return -ENOMEM;
941
942 ubi->mtd = mtd;
943 ubi->ubi_num = ubi_num;
944 ubi->vid_hdr_offset = vid_hdr_offset;
945 ubi->autoresize_vol_id = -1;
946
Heiko Schocherf5895d12014-06-24 10:10:04 +0200947#ifdef CONFIG_MTD_UBI_FASTMAP
948 ubi->fm_pool.used = ubi->fm_pool.size = 0;
949 ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
950
951 /*
952 * fm_pool.max_size is 5% of the total number of PEBs but it's also
953 * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
954 */
955 ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
956 ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200957 ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
958 UBI_FM_MIN_POOL_SIZE);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200959
Heiko Schocher94b66de2015-10-22 06:19:21 +0200960 ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200961 ubi->fm_disabled = !fm_autoconvert;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200962 if (fm_debug)
963 ubi_enable_dbg_chk_fastmap(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200964
965 if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
966 <= UBI_FM_MAX_START) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200967 ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200968 UBI_FM_MAX_START);
969 ubi->fm_disabled = 1;
970 }
971
Heiko Schocher94b66de2015-10-22 06:19:21 +0200972 ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
973 ubi_msg(ubi, "default fastmap WL pool size: %d",
974 ubi->fm_wl_pool.max_size);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200975#else
976 ubi->fm_disabled = 1;
977#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100978 mutex_init(&ubi->buf_mutex);
979 mutex_init(&ubi->ckvol_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200980 mutex_init(&ubi->device_mutex);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100981 spin_lock_init(&ubi->volumes_lock);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200982 init_rwsem(&ubi->fm_protect);
983 init_rwsem(&ubi->fm_eba_sem);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100984
Heiko Schocher94b66de2015-10-22 06:19:21 +0200985 ubi_msg(ubi, "attaching mtd%d", mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100986
Heiko Schocherf5895d12014-06-24 10:10:04 +0200987 err = io_init(ubi, max_beb_per1024);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100988 if (err)
989 goto out_free;
990
Stefan Roese920863d2008-12-10 10:28:33 +0100991 err = -ENOMEM;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200992 ubi->peb_buf = vmalloc(ubi->peb_size);
993 if (!ubi->peb_buf)
Stefan Roese920863d2008-12-10 10:28:33 +0100994 goto out_free;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100995
Heiko Schocherf5895d12014-06-24 10:10:04 +0200996#ifdef CONFIG_MTD_UBI_FASTMAP
997 ubi->fm_size = ubi_calc_fm_size(ubi);
998 ubi->fm_buf = vzalloc(ubi->fm_size);
999 if (!ubi->fm_buf)
Stefan Roese920863d2008-12-10 10:28:33 +01001000 goto out_free;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001001#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02001002 err = ubi_attach(ubi, 0);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001003 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001004 ubi_err(ubi, "failed to attach mtd%d, error %d",
1005 mtd->index, err);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001006 goto out_free;
1007 }
1008
1009 if (ubi->autoresize_vol_id != -1) {
1010 err = autoresize(ubi, ubi->autoresize_vol_id);
1011 if (err)
1012 goto out_detach;
1013 }
1014
Heiko Schocherf5895d12014-06-24 10:10:04 +02001015 err = uif_init(ubi, &ref);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001016 if (err)
1017 goto out_detach;
1018
Heiko Schocherf5895d12014-06-24 10:10:04 +02001019 err = ubi_debugfs_init_dev(ubi);
1020 if (err)
1021 goto out_uif;
1022
1023 ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001024 if (IS_ERR(ubi->bgt_thread)) {
1025 err = PTR_ERR(ubi->bgt_thread);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001026 ubi_err(ubi, "cannot spawn \"%s\", error %d",
1027 ubi->bgt_name, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001028 goto out_debugfs;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001029 }
1030
Heiko Schocher94b66de2015-10-22 06:19:21 +02001031 ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
1032 mtd->index, mtd->name, ubi->flash_size >> 20);
1033 ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001034 ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001035 ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001036 ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001037 ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001038 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001039 ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001040 ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001041 ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001042 ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
1043 ubi->vtbl_slots);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001044 ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001045 ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
1046 ubi->image_seq);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001047 ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001048 ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001049
Heiko Schocherf5895d12014-06-24 10:10:04 +02001050 /*
1051 * The below lock makes sure we do not race with 'ubi_thread()' which
1052 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
1053 */
1054 spin_lock(&ubi->wl_lock);
1055 ubi->thread_enabled = 1;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001056#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +02001057 wake_up_process(ubi->bgt_thread);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001058#else
Richard Weinbergerfc168682018-02-08 07:29:52 +01001059 ubi_do_worker(ubi);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001060#endif
1061
Heiko Schocherf5895d12014-06-24 10:10:04 +02001062 spin_unlock(&ubi->wl_lock);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001063
1064 ubi_devices[ubi_num] = ubi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001065 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001066 return ubi_num;
1067
Heiko Schocherf5895d12014-06-24 10:10:04 +02001068out_debugfs:
1069 ubi_debugfs_exit_dev(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001070out_uif:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001071 get_device(&ubi->dev);
1072 ubi_assert(ref);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001073 uif_close(ubi);
1074out_detach:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001075 ubi_wl_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001076 ubi_free_internal_volumes(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001077 vfree(ubi->vtbl);
1078out_free:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001079 vfree(ubi->peb_buf);
1080 vfree(ubi->fm_buf);
1081 if (ref)
1082 put_device(&ubi->dev);
1083 else
1084 kfree(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001085 return err;
1086}
1087
1088/**
1089 * ubi_detach_mtd_dev - detach an MTD device.
1090 * @ubi_num: UBI device number to detach from
1091 * @anyway: detach MTD even if device reference count is not zero
1092 *
1093 * This function destroys an UBI device number @ubi_num and detaches the
1094 * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1095 * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1096 * exist.
1097 *
1098 * Note, the invocations of this function has to be serialized by the
1099 * @ubi_devices_mutex.
1100 */
1101int ubi_detach_mtd_dev(int ubi_num, int anyway)
1102{
1103 struct ubi_device *ubi;
1104
1105 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1106 return -EINVAL;
1107
Heiko Schocherf5895d12014-06-24 10:10:04 +02001108 ubi = ubi_get_device(ubi_num);
1109 if (!ubi)
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001110 return -EINVAL;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001111
Heiko Schocherf5895d12014-06-24 10:10:04 +02001112 spin_lock(&ubi_devices_lock);
1113 put_device(&ubi->dev);
1114 ubi->ref_count -= 1;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001115 if (ubi->ref_count) {
1116 if (!anyway) {
1117 spin_unlock(&ubi_devices_lock);
1118 return -EBUSY;
1119 }
1120 /* This may only happen if there is a bug */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001121 ubi_err(ubi, "%s reference count %d, destroy anyway",
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001122 ubi->ubi_name, ubi->ref_count);
1123 }
1124 ubi_devices[ubi_num] = NULL;
1125 spin_unlock(&ubi_devices_lock);
1126
1127 ubi_assert(ubi_num == ubi->ubi_num);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001128 ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001129 ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001130#ifdef CONFIG_MTD_UBI_FASTMAP
1131 /* If we don't write a new fastmap at detach time we lose all
Heiko Schocher94b66de2015-10-22 06:19:21 +02001132 * EC updates that have been made since the last written fastmap.
1133 * In case of fastmap debugging we omit the update to simulate an
1134 * unclean shutdown. */
1135 if (!ubi_dbg_chk_fastmap(ubi))
1136 ubi_update_fastmap(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001137#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001138 /*
1139 * Before freeing anything, we have to stop the background thread to
1140 * prevent it from doing anything on this device while we are freeing.
1141 */
1142 if (ubi->bgt_thread)
1143 kthread_stop(ubi->bgt_thread);
1144
Heiko Schocherf5895d12014-06-24 10:10:04 +02001145 /*
1146 * Get a reference to the device in order to prevent 'dev_release()'
1147 * from freeing the @ubi object.
1148 */
1149 get_device(&ubi->dev);
1150
1151 ubi_debugfs_exit_dev(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001152 uif_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001153
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001154 ubi_wl_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001155 ubi_free_internal_volumes(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001156 vfree(ubi->vtbl);
1157 put_mtd_device(ubi->mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001158 vfree(ubi->peb_buf);
1159 vfree(ubi->fm_buf);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001160 ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001161 put_device(&ubi->dev);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001162 return 0;
1163}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001164
1165#ifndef __UBOOT__
1166/**
1167 * open_mtd_by_chdev - open an MTD device by its character device node path.
1168 * @mtd_dev: MTD character device node path
1169 *
1170 * This helper function opens an MTD device by its character node device path.
1171 * Returns MTD device description object in case of success and a negative
1172 * error code in case of failure.
1173 */
1174static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1175{
1176 int err, major, minor, mode;
1177 struct path path;
1178
1179 /* Probably this is an MTD character device node path */
1180 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1181 if (err)
1182 return ERR_PTR(err);
1183
1184 /* MTD device number is defined by the major / minor numbers */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001185 major = imajor(d_backing_inode(path.dentry));
1186 minor = iminor(d_backing_inode(path.dentry));
1187 mode = d_backing_inode(path.dentry)->i_mode;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001188 path_put(&path);
1189 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1190 return ERR_PTR(-EINVAL);
1191
1192 if (minor & 1)
1193 /*
1194 * Just do not think the "/dev/mtdrX" devices support is need,
1195 * so do not support them to avoid doing extra work.
1196 */
1197 return ERR_PTR(-EINVAL);
1198
1199 return get_mtd_device(NULL, minor / 2);
1200}
1201#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001202
1203/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001204 * open_mtd_device - open MTD device by name, character device path, or number.
1205 * @mtd_dev: name, character device node path, or MTD device device number
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001206 *
1207 * This function tries to open and MTD device described by @mtd_dev string,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001208 * which is first treated as ASCII MTD device number, and if it is not true, it
1209 * is treated as MTD device name, and if that is also not true, it is treated
1210 * as MTD character device node path. Returns MTD device description object in
1211 * case of success and a negative error code in case of failure.
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001212 */
1213static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1214{
1215 struct mtd_info *mtd;
1216 int mtd_num;
1217 char *endp;
1218
1219 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1220 if (*endp != '\0' || mtd_dev == endp) {
1221 /*
1222 * This does not look like an ASCII integer, probably this is
1223 * MTD device name.
1224 */
1225 mtd = get_mtd_device_nm(mtd_dev);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001226#ifndef __UBOOT__
1227 if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1228 /* Probably this is an MTD character device node path */
1229 mtd = open_mtd_by_chdev(mtd_dev);
1230#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001231 } else
1232 mtd = get_mtd_device(NULL, mtd_num);
1233
1234 return mtd;
1235}
1236
Heiko Schocherf5895d12014-06-24 10:10:04 +02001237#ifndef __UBOOT__
1238static int __init ubi_init(void)
1239#else
1240int ubi_init(void)
1241#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001242{
1243 int err, i, k;
1244
1245 /* Ensure that EC and VID headers have correct size */
1246 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
1247 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
1248
1249 if (mtd_devs > UBI_MAX_DEVICES) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001250 pr_err("UBI error: too many MTD devices, maximum is %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001251 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001252 return -EINVAL;
1253 }
1254
1255 /* Create base sysfs directory and sysfs files */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001256 err = class_register(&ubi_class);
1257 if (err < 0)
1258 return err;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001259
1260 err = misc_register(&ubi_ctrl_cdev);
1261 if (err) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001262 pr_err("UBI error: cannot register device\n");
Heiko Schocher94b66de2015-10-22 06:19:21 +02001263 goto out;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001264 }
1265
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001266 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
1267 sizeof(struct ubi_wl_entry),
1268 0, 0, NULL);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001269 if (!ubi_wl_entry_slab) {
1270 err = -ENOMEM;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001271 goto out_dev_unreg;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001272 }
1273
1274 err = ubi_debugfs_init();
1275 if (err)
1276 goto out_slab;
1277
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001278
1279 /* Attach MTD devices */
1280 for (i = 0; i < mtd_devs; i++) {
1281 struct mtd_dev_param *p = &mtd_dev_param[i];
1282 struct mtd_info *mtd;
1283
1284 cond_resched();
1285
1286 mtd = open_mtd_device(p->name);
1287 if (IS_ERR(mtd)) {
1288 err = PTR_ERR(mtd);
Stefan Roesed6d09012018-05-29 15:28:54 +02001289 pr_err("UBI error: cannot open mtd %s, error %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001290 p->name, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001291 /* See comment below re-ubi_is_module(). */
1292 if (ubi_is_module())
1293 goto out_detach;
1294 continue;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001295 }
1296
1297 mutex_lock(&ubi_devices_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001298 err = ubi_attach_mtd_dev(mtd, p->ubi_num,
1299 p->vid_hdr_offs, p->max_beb_per1024);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001300 mutex_unlock(&ubi_devices_mutex);
1301 if (err < 0) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001302 pr_err("UBI error: cannot attach mtd%d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001303 mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001304 put_mtd_device(mtd);
1305
1306 /*
1307 * Originally UBI stopped initializing on any error.
1308 * However, later on it was found out that this
1309 * behavior is not very good when UBI is compiled into
1310 * the kernel and the MTD devices to attach are passed
1311 * through the command line. Indeed, UBI failure
1312 * stopped whole boot sequence.
1313 *
1314 * To fix this, we changed the behavior for the
1315 * non-module case, but preserved the old behavior for
1316 * the module case, just for compatibility. This is a
1317 * little inconsistent, though.
1318 */
1319 if (ubi_is_module())
1320 goto out_detach;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001321 }
1322 }
1323
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001324 err = ubiblock_init();
1325 if (err) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001326 pr_err("UBI error: block: cannot initialize, error %d\n", err);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001327
1328 /* See comment above re-ubi_is_module(). */
1329 if (ubi_is_module())
1330 goto out_detach;
1331 }
1332
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001333 return 0;
1334
1335out_detach:
1336 for (k = 0; k < i; k++)
1337 if (ubi_devices[k]) {
1338 mutex_lock(&ubi_devices_mutex);
1339 ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
1340 mutex_unlock(&ubi_devices_mutex);
1341 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001342 ubi_debugfs_exit();
1343out_slab:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001344 kmem_cache_destroy(ubi_wl_entry_slab);
1345out_dev_unreg:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001346 misc_deregister(&ubi_ctrl_cdev);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001347out:
Heiko Schocher131a8242015-01-20 09:05:23 +01001348#ifdef __UBOOT__
1349 /* Reset any globals that the driver depends on being zeroed */
1350 mtd_devs = 0;
1351#endif
Heiko Schocher94b66de2015-10-22 06:19:21 +02001352 class_unregister(&ubi_class);
Stefan Roesed6d09012018-05-29 15:28:54 +02001353 pr_err("UBI error: cannot initialize UBI, error %d\n", err);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001354 return err;
1355}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001356late_initcall(ubi_init);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001357
Heiko Schocherf5895d12014-06-24 10:10:04 +02001358#ifndef __UBOOT__
1359static void __exit ubi_exit(void)
1360#else
1361void ubi_exit(void)
1362#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001363{
1364 int i;
1365
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001366 ubiblock_exit();
1367
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001368 for (i = 0; i < UBI_MAX_DEVICES; i++)
1369 if (ubi_devices[i]) {
1370 mutex_lock(&ubi_devices_mutex);
1371 ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
1372 mutex_unlock(&ubi_devices_mutex);
1373 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001374 ubi_debugfs_exit();
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001375 kmem_cache_destroy(ubi_wl_entry_slab);
1376 misc_deregister(&ubi_ctrl_cdev);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001377 class_unregister(&ubi_class);
Heiko Schocher131a8242015-01-20 09:05:23 +01001378#ifdef __UBOOT__
1379 /* Reset any globals that the driver depends on being zeroed */
1380 mtd_devs = 0;
1381#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001382}
1383module_exit(ubi_exit);
1384
1385/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001386 * bytes_str_to_int - convert a number of bytes string into an integer.
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001387 * @str: the string to convert
1388 *
1389 * This function returns positive resulting integer in case of success and a
1390 * negative error code in case of failure.
1391 */
1392static int __init bytes_str_to_int(const char *str)
1393{
1394 char *endp;
1395 unsigned long result;
1396
1397 result = simple_strtoul(str, &endp, 0);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001398 if (str == endp || result >= INT_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001399 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001400 return -EINVAL;
1401 }
1402
1403 switch (*endp) {
1404 case 'G':
1405 result *= 1024;
1406 case 'M':
1407 result *= 1024;
1408 case 'K':
1409 result *= 1024;
1410 if (endp[1] == 'i' && endp[2] == 'B')
1411 endp += 2;
1412 case '\0':
1413 break;
1414 default:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001415 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001416 return -EINVAL;
1417 }
1418
1419 return result;
1420}
1421
Heiko Schocherf5895d12014-06-24 10:10:04 +02001422int kstrtoint(const char *s, unsigned int base, int *res)
1423{
1424 unsigned long long tmp;
1425
1426 tmp = simple_strtoull(s, NULL, base);
1427 if (tmp != (unsigned long long)(int)tmp)
1428 return -ERANGE;
1429
1430 return (int)tmp;
1431}
1432
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001433/**
1434 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1435 * @val: the parameter value to parse
1436 * @kp: not used
1437 *
1438 * This function returns zero in case of success and a negative error code in
1439 * case of error.
1440 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001441#ifndef __UBOOT__
1442static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1443#else
1444int ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1445#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001446{
1447 int i, len;
1448 struct mtd_dev_param *p;
1449 char buf[MTD_PARAM_LEN_MAX];
1450 char *pbuf = &buf[0];
Heiko Schocherf5895d12014-06-24 10:10:04 +02001451 char *tokens[MTD_PARAM_MAX_COUNT], *token;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001452
1453 if (!val)
1454 return -EINVAL;
1455
1456 if (mtd_devs == UBI_MAX_DEVICES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001457 pr_err("UBI error: too many parameters, max. is %d\n",
1458 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001459 return -EINVAL;
1460 }
1461
1462 len = strnlen(val, MTD_PARAM_LEN_MAX);
1463 if (len == MTD_PARAM_LEN_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001464 pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1465 val, MTD_PARAM_LEN_MAX);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001466 return -EINVAL;
1467 }
1468
1469 if (len == 0) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001470 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001471 return 0;
1472 }
1473
1474 strcpy(buf, val);
1475
1476 /* Get rid of the final newline */
1477 if (buf[len - 1] == '\n')
1478 buf[len - 1] = '\0';
1479
Heiko Schocherf5895d12014-06-24 10:10:04 +02001480 for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001481 tokens[i] = strsep(&pbuf, ",");
1482
1483 if (pbuf) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001484 pr_err("UBI error: too many arguments at \"%s\"\n", val);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001485 return -EINVAL;
1486 }
1487
1488 p = &mtd_dev_param[mtd_devs];
1489 strcpy(&p->name[0], tokens[0]);
1490
Heiko Schocherf5895d12014-06-24 10:10:04 +02001491 token = tokens[1];
1492 if (token) {
1493 p->vid_hdr_offs = bytes_str_to_int(token);
1494
1495 if (p->vid_hdr_offs < 0)
1496 return p->vid_hdr_offs;
1497 }
1498
1499 token = tokens[2];
1500 if (token) {
1501 int err = kstrtoint(token, 10, &p->max_beb_per1024);
1502
1503 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001504 pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1505 token);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001506 return -EINVAL;
1507 }
1508 }
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001509
Heiko Schocherf5895d12014-06-24 10:10:04 +02001510 token = tokens[3];
1511 if (token) {
1512 int err = kstrtoint(token, 10, &p->ubi_num);
1513
1514 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001515 pr_err("UBI error: bad value for ubi_num parameter: %s",
1516 token);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001517 return -EINVAL;
1518 }
1519 } else
1520 p->ubi_num = UBI_DEV_NUM_AUTO;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001521
1522 mtd_devs += 1;
1523 return 0;
1524}
1525
1526module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001527MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001528 "Multiple \"mtd\" parameters may be specified.\n"
Heiko Schocherf5895d12014-06-24 10:10:04 +02001529 "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1530 "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1531 "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1532 __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
1533 "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
1534 "\n"
1535 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1536 "Example 2: mtd=content,1984 mtd=4 - attach MTD device with name \"content\" using VID header offset 1984, and MTD device number 4 with default VID header offset.\n"
1537 "Example 3: mtd=/dev/mtd1,0,25 - attach MTD device /dev/mtd1 using default VID header offset and reserve 25*nand_size_in_blocks/1024 erase blocks for bad block handling.\n"
1538 "Example 4: mtd=/dev/mtd1,0,0,5 - attach MTD device /dev/mtd1 to UBI 5 and using default values for the other fields.\n"
1539 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1540#ifdef CONFIG_MTD_UBI_FASTMAP
1541module_param(fm_autoconvert, bool, 0644);
1542MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
Heiko Schocher94b66de2015-10-22 06:19:21 +02001543module_param(fm_debug, bool, 0);
1544MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
Heiko Schocherf5895d12014-06-24 10:10:04 +02001545#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001546MODULE_VERSION(__stringify(UBI_VERSION));
1547MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1548MODULE_AUTHOR("Artem Bityutskiy");
1549MODULE_LICENSE("GPL");