blob: 104537784abb2ae2f9618c755286a446267a31bd [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
Takahiro Kuwano8d653ff2024-10-15 13:08:31 +0900682 if (ubi->mtd->type == MTD_NORFLASH)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200683 ubi->nor_flash = 1;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100684
685 ubi->min_io_size = ubi->mtd->writesize;
686 ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
687
688 /*
689 * Make sure minimal I/O unit is power of 2. Note, there is no
690 * fundamental reason for this assumption. It is just an optimization
691 * which allows us to avoid costly division operations.
692 */
693 if (!is_power_of_2(ubi->min_io_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200694 ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100695 ubi->min_io_size);
696 return -EINVAL;
697 }
698
699 ubi_assert(ubi->hdrs_min_io_size > 0);
700 ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
701 ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
702
Heiko Schocherf5895d12014-06-24 10:10:04 +0200703 ubi->max_write_size = ubi->mtd->writebufsize;
704 /*
705 * Maximum write size has to be greater or equivalent to min. I/O
706 * size, and be multiple of min. I/O size.
707 */
708 if (ubi->max_write_size < ubi->min_io_size ||
709 ubi->max_write_size % ubi->min_io_size ||
710 !is_power_of_2(ubi->max_write_size)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200711 ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200712 ubi->max_write_size, ubi->min_io_size);
713 return -EINVAL;
714 }
715
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100716 /* Calculate default aligned sizes of EC and VID headers */
717 ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
718 ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
719
Heiko Schocherf5895d12014-06-24 10:10:04 +0200720 dbg_gen("min_io_size %d", ubi->min_io_size);
721 dbg_gen("max_write_size %d", ubi->max_write_size);
722 dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
723 dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
724 dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100725
726 if (ubi->vid_hdr_offset == 0)
727 /* Default offset */
728 ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
729 ubi->ec_hdr_alsize;
730 else {
731 ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
732 ~(ubi->hdrs_min_io_size - 1);
733 ubi->vid_hdr_shift = ubi->vid_hdr_offset -
734 ubi->vid_hdr_aloffset;
735 }
736
737 /* Similar for the data offset */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200738 ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100739 ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
740
Heiko Schocherf5895d12014-06-24 10:10:04 +0200741 dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
742 dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
743 dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
744 dbg_gen("leb_start %d", ubi->leb_start);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100745
746 /* The shift must be aligned to 32-bit boundary */
747 if (ubi->vid_hdr_shift % 4) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200748 ubi_err(ubi, "unaligned VID header shift %d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100749 ubi->vid_hdr_shift);
750 return -EINVAL;
751 }
752
753 /* Check sanity */
754 if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
755 ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
756 ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
757 ubi->leb_start & (ubi->min_io_size - 1)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200758 ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100759 ubi->vid_hdr_offset, ubi->leb_start);
760 return -EINVAL;
761 }
762
763 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200764 * Set maximum amount of physical erroneous eraseblocks to be 10%.
765 * Erroneous PEB are those which have read errors.
766 */
767 ubi->max_erroneous = ubi->peb_count / 10;
768 if (ubi->max_erroneous < 16)
769 ubi->max_erroneous = 16;
770 dbg_gen("max_erroneous %d", ubi->max_erroneous);
771
772 /*
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100773 * It may happen that EC and VID headers are situated in one minimal
774 * I/O unit. In this case we can only accept this UBI image in
775 * read-only mode.
776 */
777 if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200778 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 +0100779 ubi->ro_mode = 1;
780 }
781
782 ubi->leb_size = ubi->peb_size - ubi->leb_start;
783
784 if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200785 ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200786 ubi->mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100787 ubi->ro_mode = 1;
788 }
789
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100790 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200791 * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100792 * unfortunately, MTD does not provide this information. We should loop
793 * over all physical eraseblocks and invoke mtd->block_is_bad() for
Heiko Schocherf5895d12014-06-24 10:10:04 +0200794 * each physical eraseblock. So, we leave @ubi->bad_peb_count
795 * uninitialized so far.
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100796 */
797
798 return 0;
799}
800
801/**
802 * autoresize - re-size the volume which has the "auto-resize" flag set.
803 * @ubi: UBI device description object
804 * @vol_id: ID of the volume to re-size
805 *
Heiko Schocherf5895d12014-06-24 10:10:04 +0200806 * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100807 * the volume table to the largest possible size. See comments in ubi-header.h
808 * for more description of the flag. Returns zero in case of success and a
809 * negative error code in case of failure.
810 */
811static int autoresize(struct ubi_device *ubi, int vol_id)
812{
813 struct ubi_volume_desc desc;
814 struct ubi_volume *vol = ubi->volumes[vol_id];
815 int err, old_reserved_pebs = vol->reserved_pebs;
816
Heiko Schocherf5895d12014-06-24 10:10:04 +0200817 if (ubi->ro_mode) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200818 ubi_warn(ubi, "skip auto-resize because of R/O mode");
Heiko Schocherf5895d12014-06-24 10:10:04 +0200819 return 0;
820 }
821
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100822 /*
823 * Clear the auto-resize flag in the volume in-memory copy of the
Heiko Schocherf5895d12014-06-24 10:10:04 +0200824 * volume table, and 'ubi_resize_volume()' will propagate this change
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100825 * to the flash.
826 */
827 ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
828
829 if (ubi->avail_pebs == 0) {
830 struct ubi_vtbl_record vtbl_rec;
831
832 /*
Heiko Schocherf5895d12014-06-24 10:10:04 +0200833 * No available PEBs to re-size the volume, clear the flag on
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100834 * flash and exit.
835 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200836 vtbl_rec = ubi->vtbl[vol_id];
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100837 err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
838 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200839 ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100840 vol_id);
841 } else {
842 desc.vol = vol;
843 err = ubi_resize_volume(&desc,
844 old_reserved_pebs + ubi->avail_pebs);
845 if (err)
Heiko Schocher94b66de2015-10-22 06:19:21 +0200846 ubi_err(ubi, "cannot auto-resize volume %d",
847 vol_id);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100848 }
849
850 if (err)
851 return err;
852
Heiko Schocher94b66de2015-10-22 06:19:21 +0200853 ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
854 vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100855 return 0;
856}
857
858/**
859 * ubi_attach_mtd_dev - attach an MTD device.
Heiko Schocherf5895d12014-06-24 10:10:04 +0200860 * @mtd: MTD device description object
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100861 * @ubi_num: number to assign to the new UBI device
862 * @vid_hdr_offset: VID header offset
Heiko Schocherf5895d12014-06-24 10:10:04 +0200863 * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100864 *
865 * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
866 * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
Heiko Schocherf5895d12014-06-24 10:10:04 +0200867 * which case this function finds a vacant device number and assigns it
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100868 * automatically. Returns the new UBI device number in case of success and a
869 * negative error code in case of failure.
870 *
871 * Note, the invocations of this function has to be serialized by the
872 * @ubi_devices_mutex.
873 */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200874int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
875 int vid_hdr_offset, int max_beb_per1024)
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100876{
877 struct ubi_device *ubi;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200878 int i, err, ref = 0;
879
880 if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
881 return -EINVAL;
882
883 if (!max_beb_per1024)
884 max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100885
886 /*
887 * Check if we already have the same MTD device attached.
888 *
889 * Note, this function assumes that UBI devices creations and deletions
890 * are serialized, so it does not take the &ubi_devices_lock.
891 */
892 for (i = 0; i < UBI_MAX_DEVICES; i++) {
893 ubi = ubi_devices[i];
894 if (ubi && mtd->index == ubi->mtd->index) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200895 ubi_err(ubi, "mtd%d is already attached to ubi%d",
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100896 mtd->index, i);
897 return -EEXIST;
898 }
899 }
900
901 /*
902 * Make sure this MTD device is not emulated on top of an UBI volume
903 * already. Well, generally this recursion works fine, but there are
904 * different problems like the UBI module takes a reference to itself
905 * by attaching (and thus, opening) the emulated MTD device. This
906 * results in inability to unload the module. And in general it makes
907 * no sense to attach emulated MTD devices, so we prohibit this.
908 */
909 if (mtd->type == MTD_UBIVOLUME) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200910 ubi_err(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200911 mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100912 return -EINVAL;
913 }
914
915 if (ubi_num == UBI_DEV_NUM_AUTO) {
916 /* Search for an empty slot in the @ubi_devices array */
917 for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
918 if (!ubi_devices[ubi_num])
919 break;
920 if (ubi_num == UBI_MAX_DEVICES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200921 ubi_err(ubi, "only %d UBI devices may be created",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200922 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100923 return -ENFILE;
924 }
925 } else {
926 if (ubi_num >= UBI_MAX_DEVICES)
927 return -EINVAL;
928
929 /* Make sure ubi_num is not busy */
930 if (ubi_devices[ubi_num]) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200931 ubi_err(ubi, "already exists");
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100932 return -EEXIST;
933 }
934 }
935
936 ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
937 if (!ubi)
938 return -ENOMEM;
939
940 ubi->mtd = mtd;
941 ubi->ubi_num = ubi_num;
942 ubi->vid_hdr_offset = vid_hdr_offset;
943 ubi->autoresize_vol_id = -1;
944
Heiko Schocherf5895d12014-06-24 10:10:04 +0200945#ifdef CONFIG_MTD_UBI_FASTMAP
946 ubi->fm_pool.used = ubi->fm_pool.size = 0;
947 ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
948
949 /*
950 * fm_pool.max_size is 5% of the total number of PEBs but it's also
951 * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
952 */
953 ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
954 ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200955 ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
956 UBI_FM_MIN_POOL_SIZE);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200957
Heiko Schocher94b66de2015-10-22 06:19:21 +0200958 ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200959 ubi->fm_disabled = !fm_autoconvert;
Heiko Schocher94b66de2015-10-22 06:19:21 +0200960 if (fm_debug)
961 ubi_enable_dbg_chk_fastmap(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200962
963 if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
964 <= UBI_FM_MAX_START) {
Heiko Schocher94b66de2015-10-22 06:19:21 +0200965 ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200966 UBI_FM_MAX_START);
967 ubi->fm_disabled = 1;
968 }
969
Heiko Schocher94b66de2015-10-22 06:19:21 +0200970 ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
971 ubi_msg(ubi, "default fastmap WL pool size: %d",
972 ubi->fm_wl_pool.max_size);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200973#else
974 ubi->fm_disabled = 1;
975#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100976 mutex_init(&ubi->buf_mutex);
977 mutex_init(&ubi->ckvol_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200978 mutex_init(&ubi->device_mutex);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100979 spin_lock_init(&ubi->volumes_lock);
Heiko Schocher94b66de2015-10-22 06:19:21 +0200980 init_rwsem(&ubi->fm_protect);
981 init_rwsem(&ubi->fm_eba_sem);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100982
Heiko Schocher94b66de2015-10-22 06:19:21 +0200983 ubi_msg(ubi, "attaching mtd%d", mtd->index);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100984
Heiko Schocherf5895d12014-06-24 10:10:04 +0200985 err = io_init(ubi, max_beb_per1024);
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100986 if (err)
987 goto out_free;
988
Stefan Roese920863d2008-12-10 10:28:33 +0100989 err = -ENOMEM;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200990 ubi->peb_buf = vmalloc(ubi->peb_size);
991 if (!ubi->peb_buf)
Stefan Roese920863d2008-12-10 10:28:33 +0100992 goto out_free;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100993
Heiko Schocherf5895d12014-06-24 10:10:04 +0200994#ifdef CONFIG_MTD_UBI_FASTMAP
995 ubi->fm_size = ubi_calc_fm_size(ubi);
996 ubi->fm_buf = vzalloc(ubi->fm_size);
997 if (!ubi->fm_buf)
Stefan Roese920863d2008-12-10 10:28:33 +0100998 goto out_free;
Kyungmin Park1d8dca62008-11-19 16:23:06 +0100999#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +02001000 err = ubi_attach(ubi, 0);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001001 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001002 ubi_err(ubi, "failed to attach mtd%d, error %d",
1003 mtd->index, err);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001004 goto out_free;
1005 }
1006
1007 if (ubi->autoresize_vol_id != -1) {
1008 err = autoresize(ubi, ubi->autoresize_vol_id);
1009 if (err)
1010 goto out_detach;
1011 }
1012
Heiko Schocherf5895d12014-06-24 10:10:04 +02001013 err = uif_init(ubi, &ref);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001014 if (err)
1015 goto out_detach;
1016
Heiko Schocherf5895d12014-06-24 10:10:04 +02001017 err = ubi_debugfs_init_dev(ubi);
1018 if (err)
1019 goto out_uif;
1020
1021 ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001022 if (IS_ERR(ubi->bgt_thread)) {
1023 err = PTR_ERR(ubi->bgt_thread);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001024 ubi_err(ubi, "cannot spawn \"%s\", error %d",
1025 ubi->bgt_name, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001026 goto out_debugfs;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001027 }
1028
Heiko Schocher94b66de2015-10-22 06:19:21 +02001029 ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
1030 mtd->index, mtd->name, ubi->flash_size >> 20);
1031 ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001032 ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001033 ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001034 ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001035 ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001036 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001037 ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001038 ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001039 ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001040 ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
1041 ubi->vtbl_slots);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001042 ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001043 ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
1044 ubi->image_seq);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001045 ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
Heiko Schocherf5895d12014-06-24 10:10:04 +02001046 ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001047
Heiko Schocherf5895d12014-06-24 10:10:04 +02001048 /*
1049 * The below lock makes sure we do not race with 'ubi_thread()' which
1050 * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
1051 */
1052 spin_lock(&ubi->wl_lock);
1053 ubi->thread_enabled = 1;
Heiko Schocher94b66de2015-10-22 06:19:21 +02001054#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +02001055 wake_up_process(ubi->bgt_thread);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001056#else
Richard Weinbergerfc168682018-02-08 07:29:52 +01001057 ubi_do_worker(ubi);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001058#endif
1059
Heiko Schocherf5895d12014-06-24 10:10:04 +02001060 spin_unlock(&ubi->wl_lock);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001061
1062 ubi_devices[ubi_num] = ubi;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001063 ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001064 return ubi_num;
1065
Heiko Schocherf5895d12014-06-24 10:10:04 +02001066out_debugfs:
1067 ubi_debugfs_exit_dev(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001068out_uif:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001069 get_device(&ubi->dev);
1070 ubi_assert(ref);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001071 uif_close(ubi);
1072out_detach:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001073 ubi_wl_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001074 ubi_free_internal_volumes(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001075 vfree(ubi->vtbl);
1076out_free:
Heiko Schocherf5895d12014-06-24 10:10:04 +02001077 vfree(ubi->peb_buf);
1078 vfree(ubi->fm_buf);
1079 if (ref)
1080 put_device(&ubi->dev);
1081 else
1082 kfree(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001083 return err;
1084}
1085
1086/**
1087 * ubi_detach_mtd_dev - detach an MTD device.
1088 * @ubi_num: UBI device number to detach from
1089 * @anyway: detach MTD even if device reference count is not zero
1090 *
1091 * This function destroys an UBI device number @ubi_num and detaches the
1092 * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1093 * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1094 * exist.
1095 *
1096 * Note, the invocations of this function has to be serialized by the
1097 * @ubi_devices_mutex.
1098 */
1099int ubi_detach_mtd_dev(int ubi_num, int anyway)
1100{
1101 struct ubi_device *ubi;
1102
1103 if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1104 return -EINVAL;
1105
Heiko Schocherf5895d12014-06-24 10:10:04 +02001106 ubi = ubi_get_device(ubi_num);
1107 if (!ubi)
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001108 return -EINVAL;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001109
Heiko Schocherf5895d12014-06-24 10:10:04 +02001110 spin_lock(&ubi_devices_lock);
1111 put_device(&ubi->dev);
1112 ubi->ref_count -= 1;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001113 if (ubi->ref_count) {
1114 if (!anyway) {
1115 spin_unlock(&ubi_devices_lock);
1116 return -EBUSY;
1117 }
1118 /* This may only happen if there is a bug */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001119 ubi_err(ubi, "%s reference count %d, destroy anyway",
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001120 ubi->ubi_name, ubi->ref_count);
1121 }
1122 ubi_devices[ubi_num] = NULL;
1123 spin_unlock(&ubi_devices_lock);
1124
1125 ubi_assert(ubi_num == ubi->ubi_num);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001126 ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001127 ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001128#ifdef CONFIG_MTD_UBI_FASTMAP
1129 /* If we don't write a new fastmap at detach time we lose all
Heiko Schocher94b66de2015-10-22 06:19:21 +02001130 * EC updates that have been made since the last written fastmap.
1131 * In case of fastmap debugging we omit the update to simulate an
1132 * unclean shutdown. */
1133 if (!ubi_dbg_chk_fastmap(ubi))
1134 ubi_update_fastmap(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001135#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001136 /*
1137 * Before freeing anything, we have to stop the background thread to
1138 * prevent it from doing anything on this device while we are freeing.
1139 */
1140 if (ubi->bgt_thread)
1141 kthread_stop(ubi->bgt_thread);
1142
Heiko Schocherf5895d12014-06-24 10:10:04 +02001143 /*
1144 * Get a reference to the device in order to prevent 'dev_release()'
1145 * from freeing the @ubi object.
1146 */
1147 get_device(&ubi->dev);
1148
1149 ubi_debugfs_exit_dev(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001150 uif_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001151
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001152 ubi_wl_close(ubi);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001153 ubi_free_internal_volumes(ubi);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001154 vfree(ubi->vtbl);
1155 put_mtd_device(ubi->mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001156 vfree(ubi->peb_buf);
1157 vfree(ubi->fm_buf);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001158 ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001159 put_device(&ubi->dev);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001160 return 0;
1161}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001162
1163#ifndef __UBOOT__
1164/**
1165 * open_mtd_by_chdev - open an MTD device by its character device node path.
1166 * @mtd_dev: MTD character device node path
1167 *
1168 * This helper function opens an MTD device by its character node device path.
1169 * Returns MTD device description object in case of success and a negative
1170 * error code in case of failure.
1171 */
1172static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1173{
1174 int err, major, minor, mode;
1175 struct path path;
1176
1177 /* Probably this is an MTD character device node path */
1178 err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1179 if (err)
1180 return ERR_PTR(err);
1181
1182 /* MTD device number is defined by the major / minor numbers */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001183 major = imajor(d_backing_inode(path.dentry));
1184 minor = iminor(d_backing_inode(path.dentry));
1185 mode = d_backing_inode(path.dentry)->i_mode;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001186 path_put(&path);
1187 if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1188 return ERR_PTR(-EINVAL);
1189
1190 if (minor & 1)
1191 /*
1192 * Just do not think the "/dev/mtdrX" devices support is need,
1193 * so do not support them to avoid doing extra work.
1194 */
1195 return ERR_PTR(-EINVAL);
1196
1197 return get_mtd_device(NULL, minor / 2);
1198}
1199#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001200
1201/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001202 * open_mtd_device - open MTD device by name, character device path, or number.
1203 * @mtd_dev: name, character device node path, or MTD device device number
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001204 *
1205 * This function tries to open and MTD device described by @mtd_dev string,
Heiko Schocherf5895d12014-06-24 10:10:04 +02001206 * which is first treated as ASCII MTD device number, and if it is not true, it
1207 * is treated as MTD device name, and if that is also not true, it is treated
1208 * as MTD character device node path. Returns MTD device description object in
1209 * case of success and a negative error code in case of failure.
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001210 */
1211static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1212{
1213 struct mtd_info *mtd;
1214 int mtd_num;
1215 char *endp;
1216
1217 mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1218 if (*endp != '\0' || mtd_dev == endp) {
1219 /*
1220 * This does not look like an ASCII integer, probably this is
1221 * MTD device name.
1222 */
1223 mtd = get_mtd_device_nm(mtd_dev);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001224#ifndef __UBOOT__
1225 if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1226 /* Probably this is an MTD character device node path */
1227 mtd = open_mtd_by_chdev(mtd_dev);
1228#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001229 } else
1230 mtd = get_mtd_device(NULL, mtd_num);
1231
1232 return mtd;
1233}
1234
Heiko Schocherf5895d12014-06-24 10:10:04 +02001235#ifndef __UBOOT__
1236static int __init ubi_init(void)
1237#else
1238int ubi_init(void)
1239#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001240{
1241 int err, i, k;
1242
1243 /* Ensure that EC and VID headers have correct size */
1244 BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
1245 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
1246
1247 if (mtd_devs > UBI_MAX_DEVICES) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001248 pr_err("UBI error: too many MTD devices, maximum is %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001249 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001250 return -EINVAL;
1251 }
1252
1253 /* Create base sysfs directory and sysfs files */
Heiko Schocher94b66de2015-10-22 06:19:21 +02001254 err = class_register(&ubi_class);
1255 if (err < 0)
1256 return err;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001257
1258 err = misc_register(&ubi_ctrl_cdev);
1259 if (err) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001260 pr_err("UBI error: cannot register device\n");
Heiko Schocher94b66de2015-10-22 06:19:21 +02001261 goto out;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001262 }
1263
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001264 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
1265 sizeof(struct ubi_wl_entry),
1266 0, 0, NULL);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001267 if (!ubi_wl_entry_slab) {
1268 err = -ENOMEM;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001269 goto out_dev_unreg;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001270 }
1271
1272 err = ubi_debugfs_init();
1273 if (err)
1274 goto out_slab;
1275
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001276 /* Attach MTD devices */
1277 for (i = 0; i < mtd_devs; i++) {
1278 struct mtd_dev_param *p = &mtd_dev_param[i];
1279 struct mtd_info *mtd;
1280
1281 cond_resched();
1282
1283 mtd = open_mtd_device(p->name);
1284 if (IS_ERR(mtd)) {
1285 err = PTR_ERR(mtd);
Stefan Roesed6d09012018-05-29 15:28:54 +02001286 pr_err("UBI error: cannot open mtd %s, error %d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001287 p->name, err);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001288 /* See comment below re-ubi_is_module(). */
1289 if (ubi_is_module())
1290 goto out_detach;
1291 continue;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001292 }
1293
1294 mutex_lock(&ubi_devices_mutex);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001295 err = ubi_attach_mtd_dev(mtd, p->ubi_num,
1296 p->vid_hdr_offs, p->max_beb_per1024);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001297 mutex_unlock(&ubi_devices_mutex);
1298 if (err < 0) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001299 pr_err("UBI error: cannot attach mtd%d\n",
Heiko Schocher94b66de2015-10-22 06:19:21 +02001300 mtd->index);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001301 put_mtd_device(mtd);
1302
1303 /*
1304 * Originally UBI stopped initializing on any error.
1305 * However, later on it was found out that this
1306 * behavior is not very good when UBI is compiled into
1307 * the kernel and the MTD devices to attach are passed
1308 * through the command line. Indeed, UBI failure
1309 * stopped whole boot sequence.
1310 *
1311 * To fix this, we changed the behavior for the
1312 * non-module case, but preserved the old behavior for
1313 * the module case, just for compatibility. This is a
1314 * little inconsistent, though.
1315 */
1316 if (ubi_is_module())
1317 goto out_detach;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001318 }
1319 }
1320
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001321 err = ubiblock_init();
1322 if (err) {
Stefan Roesed6d09012018-05-29 15:28:54 +02001323 pr_err("UBI error: block: cannot initialize, error %d\n", err);
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001324
1325 /* See comment above re-ubi_is_module(). */
1326 if (ubi_is_module())
1327 goto out_detach;
1328 }
1329
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001330 return 0;
1331
1332out_detach:
1333 for (k = 0; k < i; k++)
1334 if (ubi_devices[k]) {
1335 mutex_lock(&ubi_devices_mutex);
1336 ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
1337 mutex_unlock(&ubi_devices_mutex);
1338 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001339 ubi_debugfs_exit();
1340out_slab:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001341 kmem_cache_destroy(ubi_wl_entry_slab);
1342out_dev_unreg:
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001343 misc_deregister(&ubi_ctrl_cdev);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001344out:
Heiko Schocher131a8242015-01-20 09:05:23 +01001345#ifdef __UBOOT__
1346 /* Reset any globals that the driver depends on being zeroed */
1347 mtd_devs = 0;
1348#endif
Heiko Schocher94b66de2015-10-22 06:19:21 +02001349 class_unregister(&ubi_class);
Stefan Roesed6d09012018-05-29 15:28:54 +02001350 pr_err("UBI error: cannot initialize UBI, error %d\n", err);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001351 return err;
1352}
Heiko Schocherf5895d12014-06-24 10:10:04 +02001353late_initcall(ubi_init);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001354
Heiko Schocherf5895d12014-06-24 10:10:04 +02001355#ifndef __UBOOT__
1356static void __exit ubi_exit(void)
1357#else
1358void ubi_exit(void)
1359#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001360{
1361 int i;
1362
Heiko Schocher081fe9e2014-07-15 16:08:43 +02001363 ubiblock_exit();
1364
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001365 for (i = 0; i < UBI_MAX_DEVICES; i++)
1366 if (ubi_devices[i]) {
1367 mutex_lock(&ubi_devices_mutex);
1368 ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
1369 mutex_unlock(&ubi_devices_mutex);
1370 }
Heiko Schocherf5895d12014-06-24 10:10:04 +02001371 ubi_debugfs_exit();
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001372 kmem_cache_destroy(ubi_wl_entry_slab);
1373 misc_deregister(&ubi_ctrl_cdev);
Heiko Schocher94b66de2015-10-22 06:19:21 +02001374 class_unregister(&ubi_class);
Heiko Schocher131a8242015-01-20 09:05:23 +01001375#ifdef __UBOOT__
1376 /* Reset any globals that the driver depends on being zeroed */
1377 mtd_devs = 0;
1378#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001379}
1380module_exit(ubi_exit);
1381
1382/**
Heiko Schocherf5895d12014-06-24 10:10:04 +02001383 * bytes_str_to_int - convert a number of bytes string into an integer.
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001384 * @str: the string to convert
1385 *
1386 * This function returns positive resulting integer in case of success and a
1387 * negative error code in case of failure.
1388 */
1389static int __init bytes_str_to_int(const char *str)
1390{
1391 char *endp;
1392 unsigned long result;
1393
1394 result = simple_strtoul(str, &endp, 0);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001395 if (str == endp || result >= INT_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001396 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001397 return -EINVAL;
1398 }
1399
1400 switch (*endp) {
1401 case 'G':
1402 result *= 1024;
1403 case 'M':
1404 result *= 1024;
1405 case 'K':
1406 result *= 1024;
1407 if (endp[1] == 'i' && endp[2] == 'B')
1408 endp += 2;
1409 case '\0':
1410 break;
1411 default:
Heiko Schocher94b66de2015-10-22 06:19:21 +02001412 pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001413 return -EINVAL;
1414 }
1415
1416 return result;
1417}
1418
Heiko Schocherf5895d12014-06-24 10:10:04 +02001419int kstrtoint(const char *s, unsigned int base, int *res)
1420{
1421 unsigned long long tmp;
1422
1423 tmp = simple_strtoull(s, NULL, base);
1424 if (tmp != (unsigned long long)(int)tmp)
1425 return -ERANGE;
1426
1427 return (int)tmp;
1428}
1429
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001430/**
1431 * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1432 * @val: the parameter value to parse
1433 * @kp: not used
1434 *
1435 * This function returns zero in case of success and a negative error code in
1436 * case of error.
1437 */
Heiko Schocherf5895d12014-06-24 10:10:04 +02001438#ifndef __UBOOT__
1439static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1440#else
1441int ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1442#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001443{
1444 int i, len;
1445 struct mtd_dev_param *p;
1446 char buf[MTD_PARAM_LEN_MAX];
1447 char *pbuf = &buf[0];
Heiko Schocherf5895d12014-06-24 10:10:04 +02001448 char *tokens[MTD_PARAM_MAX_COUNT], *token;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001449
1450 if (!val)
1451 return -EINVAL;
1452
1453 if (mtd_devs == UBI_MAX_DEVICES) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001454 pr_err("UBI error: too many parameters, max. is %d\n",
1455 UBI_MAX_DEVICES);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001456 return -EINVAL;
1457 }
1458
1459 len = strnlen(val, MTD_PARAM_LEN_MAX);
1460 if (len == MTD_PARAM_LEN_MAX) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001461 pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1462 val, MTD_PARAM_LEN_MAX);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001463 return -EINVAL;
1464 }
1465
1466 if (len == 0) {
Heiko Schocherf5895d12014-06-24 10:10:04 +02001467 pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001468 return 0;
1469 }
1470
1471 strcpy(buf, val);
1472
1473 /* Get rid of the final newline */
1474 if (buf[len - 1] == '\n')
1475 buf[len - 1] = '\0';
1476
Heiko Schocherf5895d12014-06-24 10:10:04 +02001477 for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001478 tokens[i] = strsep(&pbuf, ",");
1479
1480 if (pbuf) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001481 pr_err("UBI error: too many arguments at \"%s\"\n", val);
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001482 return -EINVAL;
1483 }
1484
1485 p = &mtd_dev_param[mtd_devs];
1486 strcpy(&p->name[0], tokens[0]);
1487
Heiko Schocherf5895d12014-06-24 10:10:04 +02001488 token = tokens[1];
1489 if (token) {
1490 p->vid_hdr_offs = bytes_str_to_int(token);
1491
1492 if (p->vid_hdr_offs < 0)
1493 return p->vid_hdr_offs;
1494 }
1495
1496 token = tokens[2];
1497 if (token) {
1498 int err = kstrtoint(token, 10, &p->max_beb_per1024);
1499
1500 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001501 pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1502 token);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001503 return -EINVAL;
1504 }
1505 }
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001506
Heiko Schocherf5895d12014-06-24 10:10:04 +02001507 token = tokens[3];
1508 if (token) {
1509 int err = kstrtoint(token, 10, &p->ubi_num);
1510
1511 if (err) {
Heiko Schocher94b66de2015-10-22 06:19:21 +02001512 pr_err("UBI error: bad value for ubi_num parameter: %s",
1513 token);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001514 return -EINVAL;
1515 }
1516 } else
1517 p->ubi_num = UBI_DEV_NUM_AUTO;
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001518
1519 mtd_devs += 1;
1520 return 0;
1521}
1522
1523module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
Heiko Schocherf5895d12014-06-24 10:10:04 +02001524MODULE_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 +01001525 "Multiple \"mtd\" parameters may be specified.\n"
Heiko Schocherf5895d12014-06-24 10:10:04 +02001526 "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1527 "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1528 "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1529 __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
1530 "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
1531 "\n"
1532 "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1533 "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"
1534 "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"
1535 "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"
1536 "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1537#ifdef CONFIG_MTD_UBI_FASTMAP
1538module_param(fm_autoconvert, bool, 0644);
1539MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
Heiko Schocher94b66de2015-10-22 06:19:21 +02001540module_param(fm_debug, bool, 0);
1541MODULE_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 +02001542#endif
Kyungmin Park1d8dca62008-11-19 16:23:06 +01001543MODULE_VERSION(__stringify(UBI_VERSION));
1544MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1545MODULE_AUTHOR("Artem Bityutskiy");
1546MODULE_LICENSE("GPL");