1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (c) International Business Machines Corp., 2006
3*4882a593Smuzhiyun * Copyright (c) Nokia Corporation, 2007
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0+
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Artem Bityutskiy (Битюцкий Артём),
8*4882a593Smuzhiyun * Frank Haverkamp
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun /*
12*4882a593Smuzhiyun * This file includes UBI initialization and building of UBI devices.
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * When UBI is initialized, it attaches all the MTD devices specified as the
15*4882a593Smuzhiyun * module load parameters or the kernel boot parameters. If MTD devices were
16*4882a593Smuzhiyun * specified, UBI does not attach any MTD device, but it is possible to do
17*4882a593Smuzhiyun * later using the "UBI control device".
18*4882a593Smuzhiyun */
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #ifndef __UBOOT__
21*4882a593Smuzhiyun #include <linux/module.h>
22*4882a593Smuzhiyun #include <linux/moduleparam.h>
23*4882a593Smuzhiyun #include <linux/stringify.h>
24*4882a593Smuzhiyun #include <linux/namei.h>
25*4882a593Smuzhiyun #include <linux/stat.h>
26*4882a593Smuzhiyun #include <linux/miscdevice.h>
27*4882a593Smuzhiyun #include <linux/log2.h>
28*4882a593Smuzhiyun #include <linux/kthread.h>
29*4882a593Smuzhiyun #include <linux/kernel.h>
30*4882a593Smuzhiyun #include <linux/slab.h>
31*4882a593Smuzhiyun #include <linux/major.h>
32*4882a593Smuzhiyun #else
33*4882a593Smuzhiyun #include <linux/bug.h>
34*4882a593Smuzhiyun #include <linux/log2.h>
35*4882a593Smuzhiyun #endif
36*4882a593Smuzhiyun #include <linux/err.h>
37*4882a593Smuzhiyun #include <ubi_uboot.h>
38*4882a593Smuzhiyun #include <linux/mtd/partitions.h>
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun #include "ubi.h"
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun /* Maximum length of the 'mtd=' parameter */
43*4882a593Smuzhiyun #define MTD_PARAM_LEN_MAX 64
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun /* Maximum number of comma-separated items in the 'mtd=' parameter */
46*4882a593Smuzhiyun #define MTD_PARAM_MAX_COUNT 4
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* Maximum value for the number of bad PEBs per 1024 PEBs */
49*4882a593Smuzhiyun #define MAX_MTD_UBI_BEB_LIMIT 768
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_MODULE
52*4882a593Smuzhiyun #define ubi_is_module() 1
53*4882a593Smuzhiyun #else
54*4882a593Smuzhiyun #define ubi_is_module() 0
55*4882a593Smuzhiyun #endif
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun #if (CONFIG_SYS_MALLOC_LEN < (512 << 10))
58*4882a593Smuzhiyun #error Malloc area too small for UBI, increase CONFIG_SYS_MALLOC_LEN to >= 512k
59*4882a593Smuzhiyun #endif
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun /**
62*4882a593Smuzhiyun * struct mtd_dev_param - MTD device parameter description data structure.
63*4882a593Smuzhiyun * @name: MTD character device node path, MTD device name, or MTD device number
64*4882a593Smuzhiyun * string
65*4882a593Smuzhiyun * @vid_hdr_offs: VID header offset
66*4882a593Smuzhiyun * @max_beb_per1024: maximum expected number of bad PEBs per 1024 PEBs
67*4882a593Smuzhiyun */
68*4882a593Smuzhiyun struct mtd_dev_param {
69*4882a593Smuzhiyun char name[MTD_PARAM_LEN_MAX];
70*4882a593Smuzhiyun int ubi_num;
71*4882a593Smuzhiyun int vid_hdr_offs;
72*4882a593Smuzhiyun int max_beb_per1024;
73*4882a593Smuzhiyun };
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Numbers of elements set in the @mtd_dev_param array */
76*4882a593Smuzhiyun static int __initdata mtd_devs;
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun /* MTD devices specification parameters */
79*4882a593Smuzhiyun static struct mtd_dev_param __initdata mtd_dev_param[UBI_MAX_DEVICES];
80*4882a593Smuzhiyun #ifndef __UBOOT__
81*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
82*4882a593Smuzhiyun /* UBI module parameter to enable fastmap automatically on non-fastmap images */
83*4882a593Smuzhiyun static bool fm_autoconvert;
84*4882a593Smuzhiyun static bool fm_debug;
85*4882a593Smuzhiyun #endif
86*4882a593Smuzhiyun #else
87*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
88*4882a593Smuzhiyun #if !defined(CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT)
89*4882a593Smuzhiyun #define CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT 0
90*4882a593Smuzhiyun #endif
91*4882a593Smuzhiyun static bool fm_autoconvert = CONFIG_MTD_UBI_FASTMAP_AUTOCONVERT;
92*4882a593Smuzhiyun #if !defined(CONFIG_MTD_UBI_FM_DEBUG)
93*4882a593Smuzhiyun #define CONFIG_MTD_UBI_FM_DEBUG 0
94*4882a593Smuzhiyun #endif
95*4882a593Smuzhiyun static bool fm_debug = CONFIG_MTD_UBI_FM_DEBUG;
96*4882a593Smuzhiyun #endif
97*4882a593Smuzhiyun #endif
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun /* Slab cache for wear-leveling entries */
100*4882a593Smuzhiyun struct kmem_cache *ubi_wl_entry_slab;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun #ifndef __UBOOT__
103*4882a593Smuzhiyun /* UBI control character device */
104*4882a593Smuzhiyun static struct miscdevice ubi_ctrl_cdev = {
105*4882a593Smuzhiyun .minor = MISC_DYNAMIC_MINOR,
106*4882a593Smuzhiyun .name = "ubi_ctrl",
107*4882a593Smuzhiyun .fops = &ubi_ctrl_cdev_operations,
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* All UBI devices in system */
112*4882a593Smuzhiyun #ifndef __UBOOT__
113*4882a593Smuzhiyun static struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
114*4882a593Smuzhiyun #else
115*4882a593Smuzhiyun struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
116*4882a593Smuzhiyun #endif
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun #ifndef __UBOOT__
119*4882a593Smuzhiyun /* Serializes UBI devices creations and removals */
120*4882a593Smuzhiyun DEFINE_MUTEX(ubi_devices_mutex);
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /* Protects @ubi_devices and @ubi->ref_count */
123*4882a593Smuzhiyun static DEFINE_SPINLOCK(ubi_devices_lock);
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun /* "Show" method for files in '/<sysfs>/class/ubi/' */
ubi_version_show(struct class * class,struct class_attribute * attr,char * buf)126*4882a593Smuzhiyun static ssize_t ubi_version_show(struct class *class,
127*4882a593Smuzhiyun struct class_attribute *attr, char *buf)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun return sprintf(buf, "%d\n", UBI_VERSION);
130*4882a593Smuzhiyun }
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun /* UBI version attribute ('/<sysfs>/class/ubi/version') */
133*4882a593Smuzhiyun static struct class_attribute ubi_class_attrs[] = {
134*4882a593Smuzhiyun __ATTR(version, S_IRUGO, ubi_version_show, NULL),
135*4882a593Smuzhiyun __ATTR_NULL
136*4882a593Smuzhiyun };
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
139*4882a593Smuzhiyun struct class ubi_class = {
140*4882a593Smuzhiyun .name = UBI_NAME_STR,
141*4882a593Smuzhiyun .owner = THIS_MODULE,
142*4882a593Smuzhiyun .class_attrs = ubi_class_attrs,
143*4882a593Smuzhiyun };
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun static ssize_t dev_attribute_show(struct device *dev,
146*4882a593Smuzhiyun struct device_attribute *attr, char *buf);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
149*4882a593Smuzhiyun static struct device_attribute dev_eraseblock_size =
150*4882a593Smuzhiyun __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
151*4882a593Smuzhiyun static struct device_attribute dev_avail_eraseblocks =
152*4882a593Smuzhiyun __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
153*4882a593Smuzhiyun static struct device_attribute dev_total_eraseblocks =
154*4882a593Smuzhiyun __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
155*4882a593Smuzhiyun static struct device_attribute dev_volumes_count =
156*4882a593Smuzhiyun __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
157*4882a593Smuzhiyun static struct device_attribute dev_max_ec =
158*4882a593Smuzhiyun __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
159*4882a593Smuzhiyun static struct device_attribute dev_reserved_for_bad =
160*4882a593Smuzhiyun __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
161*4882a593Smuzhiyun static struct device_attribute dev_bad_peb_count =
162*4882a593Smuzhiyun __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
163*4882a593Smuzhiyun static struct device_attribute dev_max_vol_count =
164*4882a593Smuzhiyun __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
165*4882a593Smuzhiyun static struct device_attribute dev_min_io_size =
166*4882a593Smuzhiyun __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
167*4882a593Smuzhiyun static struct device_attribute dev_bgt_enabled =
168*4882a593Smuzhiyun __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
169*4882a593Smuzhiyun static struct device_attribute dev_mtd_num =
170*4882a593Smuzhiyun __ATTR(mtd_num, S_IRUGO, dev_attribute_show, NULL);
171*4882a593Smuzhiyun #endif
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun /**
174*4882a593Smuzhiyun * ubi_volume_notify - send a volume change notification.
175*4882a593Smuzhiyun * @ubi: UBI device description object
176*4882a593Smuzhiyun * @vol: volume description object of the changed volume
177*4882a593Smuzhiyun * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
178*4882a593Smuzhiyun *
179*4882a593Smuzhiyun * This is a helper function which notifies all subscribers about a volume
180*4882a593Smuzhiyun * change event (creation, removal, re-sizing, re-naming, updating). Returns
181*4882a593Smuzhiyun * zero in case of success and a negative error code in case of failure.
182*4882a593Smuzhiyun */
ubi_volume_notify(struct ubi_device * ubi,struct ubi_volume * vol,int ntype)183*4882a593Smuzhiyun int ubi_volume_notify(struct ubi_device *ubi, struct ubi_volume *vol, int ntype)
184*4882a593Smuzhiyun {
185*4882a593Smuzhiyun int ret;
186*4882a593Smuzhiyun struct ubi_notification nt;
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun ubi_do_get_device_info(ubi, &nt.di);
189*4882a593Smuzhiyun ubi_do_get_volume_info(ubi, vol, &nt.vi);
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun switch (ntype) {
192*4882a593Smuzhiyun case UBI_VOLUME_ADDED:
193*4882a593Smuzhiyun case UBI_VOLUME_REMOVED:
194*4882a593Smuzhiyun case UBI_VOLUME_RESIZED:
195*4882a593Smuzhiyun case UBI_VOLUME_RENAMED:
196*4882a593Smuzhiyun ret = ubi_update_fastmap(ubi);
197*4882a593Smuzhiyun if (ret)
198*4882a593Smuzhiyun ubi_msg(ubi, "Unable to write a new fastmap: %i", ret);
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
201*4882a593Smuzhiyun return blocking_notifier_call_chain(&ubi_notifiers, ntype, &nt);
202*4882a593Smuzhiyun }
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun /**
205*4882a593Smuzhiyun * ubi_notify_all - send a notification to all volumes.
206*4882a593Smuzhiyun * @ubi: UBI device description object
207*4882a593Smuzhiyun * @ntype: notification type to send (%UBI_VOLUME_ADDED, etc)
208*4882a593Smuzhiyun * @nb: the notifier to call
209*4882a593Smuzhiyun *
210*4882a593Smuzhiyun * This function walks all volumes of UBI device @ubi and sends the @ntype
211*4882a593Smuzhiyun * notification for each volume. If @nb is %NULL, then all registered notifiers
212*4882a593Smuzhiyun * are called, otherwise only the @nb notifier is called. Returns the number of
213*4882a593Smuzhiyun * sent notifications.
214*4882a593Smuzhiyun */
ubi_notify_all(struct ubi_device * ubi,int ntype,struct notifier_block * nb)215*4882a593Smuzhiyun int ubi_notify_all(struct ubi_device *ubi, int ntype, struct notifier_block *nb)
216*4882a593Smuzhiyun {
217*4882a593Smuzhiyun struct ubi_notification nt;
218*4882a593Smuzhiyun int i, count = 0;
219*4882a593Smuzhiyun #ifndef __UBOOT__
220*4882a593Smuzhiyun int ret;
221*4882a593Smuzhiyun #endif
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun ubi_do_get_device_info(ubi, &nt.di);
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun mutex_lock(&ubi->device_mutex);
226*4882a593Smuzhiyun for (i = 0; i < ubi->vtbl_slots; i++) {
227*4882a593Smuzhiyun /*
228*4882a593Smuzhiyun * Since the @ubi->device is locked, and we are not going to
229*4882a593Smuzhiyun * change @ubi->volumes, we do not have to lock
230*4882a593Smuzhiyun * @ubi->volumes_lock.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun if (!ubi->volumes[i])
233*4882a593Smuzhiyun continue;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun ubi_do_get_volume_info(ubi, ubi->volumes[i], &nt.vi);
236*4882a593Smuzhiyun #ifndef __UBOOT__
237*4882a593Smuzhiyun if (nb)
238*4882a593Smuzhiyun nb->notifier_call(nb, ntype, &nt);
239*4882a593Smuzhiyun else
240*4882a593Smuzhiyun ret = blocking_notifier_call_chain(&ubi_notifiers, ntype,
241*4882a593Smuzhiyun &nt);
242*4882a593Smuzhiyun #endif
243*4882a593Smuzhiyun count += 1;
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun mutex_unlock(&ubi->device_mutex);
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun return count;
248*4882a593Smuzhiyun }
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun /**
251*4882a593Smuzhiyun * ubi_enumerate_volumes - send "add" notification for all existing volumes.
252*4882a593Smuzhiyun * @nb: the notifier to call
253*4882a593Smuzhiyun *
254*4882a593Smuzhiyun * This function walks all UBI devices and volumes and sends the
255*4882a593Smuzhiyun * %UBI_VOLUME_ADDED notification for each volume. If @nb is %NULL, then all
256*4882a593Smuzhiyun * registered notifiers are called, otherwise only the @nb notifier is called.
257*4882a593Smuzhiyun * Returns the number of sent notifications.
258*4882a593Smuzhiyun */
ubi_enumerate_volumes(struct notifier_block * nb)259*4882a593Smuzhiyun int ubi_enumerate_volumes(struct notifier_block *nb)
260*4882a593Smuzhiyun {
261*4882a593Smuzhiyun int i, count = 0;
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /*
264*4882a593Smuzhiyun * Since the @ubi_devices_mutex is locked, and we are not going to
265*4882a593Smuzhiyun * change @ubi_devices, we do not have to lock @ubi_devices_lock.
266*4882a593Smuzhiyun */
267*4882a593Smuzhiyun for (i = 0; i < UBI_MAX_DEVICES; i++) {
268*4882a593Smuzhiyun struct ubi_device *ubi = ubi_devices[i];
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if (!ubi)
271*4882a593Smuzhiyun continue;
272*4882a593Smuzhiyun count += ubi_notify_all(ubi, UBI_VOLUME_ADDED, nb);
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun return count;
276*4882a593Smuzhiyun }
277*4882a593Smuzhiyun
278*4882a593Smuzhiyun /**
279*4882a593Smuzhiyun * ubi_get_device - get UBI device.
280*4882a593Smuzhiyun * @ubi_num: UBI device number
281*4882a593Smuzhiyun *
282*4882a593Smuzhiyun * This function returns UBI device description object for UBI device number
283*4882a593Smuzhiyun * @ubi_num, or %NULL if the device does not exist. This function increases the
284*4882a593Smuzhiyun * device reference count to prevent removal of the device. In other words, the
285*4882a593Smuzhiyun * device cannot be removed if its reference count is not zero.
286*4882a593Smuzhiyun */
ubi_get_device(int ubi_num)287*4882a593Smuzhiyun struct ubi_device *ubi_get_device(int ubi_num)
288*4882a593Smuzhiyun {
289*4882a593Smuzhiyun struct ubi_device *ubi;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun spin_lock(&ubi_devices_lock);
292*4882a593Smuzhiyun ubi = ubi_devices[ubi_num];
293*4882a593Smuzhiyun if (ubi) {
294*4882a593Smuzhiyun ubi_assert(ubi->ref_count >= 0);
295*4882a593Smuzhiyun ubi->ref_count += 1;
296*4882a593Smuzhiyun get_device(&ubi->dev);
297*4882a593Smuzhiyun }
298*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun return ubi;
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun /**
304*4882a593Smuzhiyun * ubi_put_device - drop an UBI device reference.
305*4882a593Smuzhiyun * @ubi: UBI device description object
306*4882a593Smuzhiyun */
ubi_put_device(struct ubi_device * ubi)307*4882a593Smuzhiyun void ubi_put_device(struct ubi_device *ubi)
308*4882a593Smuzhiyun {
309*4882a593Smuzhiyun spin_lock(&ubi_devices_lock);
310*4882a593Smuzhiyun ubi->ref_count -= 1;
311*4882a593Smuzhiyun put_device(&ubi->dev);
312*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
313*4882a593Smuzhiyun }
314*4882a593Smuzhiyun
315*4882a593Smuzhiyun /**
316*4882a593Smuzhiyun * ubi_get_by_major - get UBI device by character device major number.
317*4882a593Smuzhiyun * @major: major number
318*4882a593Smuzhiyun *
319*4882a593Smuzhiyun * This function is similar to 'ubi_get_device()', but it searches the device
320*4882a593Smuzhiyun * by its major number.
321*4882a593Smuzhiyun */
ubi_get_by_major(int major)322*4882a593Smuzhiyun struct ubi_device *ubi_get_by_major(int major)
323*4882a593Smuzhiyun {
324*4882a593Smuzhiyun int i;
325*4882a593Smuzhiyun struct ubi_device *ubi;
326*4882a593Smuzhiyun
327*4882a593Smuzhiyun spin_lock(&ubi_devices_lock);
328*4882a593Smuzhiyun for (i = 0; i < UBI_MAX_DEVICES; i++) {
329*4882a593Smuzhiyun ubi = ubi_devices[i];
330*4882a593Smuzhiyun if (ubi && MAJOR(ubi->cdev.dev) == major) {
331*4882a593Smuzhiyun ubi_assert(ubi->ref_count >= 0);
332*4882a593Smuzhiyun ubi->ref_count += 1;
333*4882a593Smuzhiyun get_device(&ubi->dev);
334*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
335*4882a593Smuzhiyun return ubi;
336*4882a593Smuzhiyun }
337*4882a593Smuzhiyun }
338*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun return NULL;
341*4882a593Smuzhiyun }
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun /**
344*4882a593Smuzhiyun * ubi_major2num - get UBI device number by character device major number.
345*4882a593Smuzhiyun * @major: major number
346*4882a593Smuzhiyun *
347*4882a593Smuzhiyun * This function searches UBI device number object by its major number. If UBI
348*4882a593Smuzhiyun * device was not found, this function returns -ENODEV, otherwise the UBI device
349*4882a593Smuzhiyun * number is returned.
350*4882a593Smuzhiyun */
ubi_major2num(int major)351*4882a593Smuzhiyun int ubi_major2num(int major)
352*4882a593Smuzhiyun {
353*4882a593Smuzhiyun int i, ubi_num = -ENODEV;
354*4882a593Smuzhiyun
355*4882a593Smuzhiyun spin_lock(&ubi_devices_lock);
356*4882a593Smuzhiyun for (i = 0; i < UBI_MAX_DEVICES; i++) {
357*4882a593Smuzhiyun struct ubi_device *ubi = ubi_devices[i];
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun if (ubi && MAJOR(ubi->cdev.dev) == major) {
360*4882a593Smuzhiyun ubi_num = ubi->ubi_num;
361*4882a593Smuzhiyun break;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun return ubi_num;
367*4882a593Smuzhiyun }
368*4882a593Smuzhiyun
369*4882a593Smuzhiyun #ifndef __UBOOT__
370*4882a593Smuzhiyun /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
dev_attribute_show(struct device * dev,struct device_attribute * attr,char * buf)371*4882a593Smuzhiyun static ssize_t dev_attribute_show(struct device *dev,
372*4882a593Smuzhiyun struct device_attribute *attr, char *buf)
373*4882a593Smuzhiyun {
374*4882a593Smuzhiyun ssize_t ret;
375*4882a593Smuzhiyun struct ubi_device *ubi;
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun /*
378*4882a593Smuzhiyun * The below code looks weird, but it actually makes sense. We get the
379*4882a593Smuzhiyun * UBI device reference from the contained 'struct ubi_device'. But it
380*4882a593Smuzhiyun * is unclear if the device was removed or not yet. Indeed, if the
381*4882a593Smuzhiyun * device was removed before we increased its reference count,
382*4882a593Smuzhiyun * 'ubi_get_device()' will return -ENODEV and we fail.
383*4882a593Smuzhiyun *
384*4882a593Smuzhiyun * Remember, 'struct ubi_device' is freed in the release function, so
385*4882a593Smuzhiyun * we still can use 'ubi->ubi_num'.
386*4882a593Smuzhiyun */
387*4882a593Smuzhiyun ubi = container_of(dev, struct ubi_device, dev);
388*4882a593Smuzhiyun ubi = ubi_get_device(ubi->ubi_num);
389*4882a593Smuzhiyun if (!ubi)
390*4882a593Smuzhiyun return -ENODEV;
391*4882a593Smuzhiyun
392*4882a593Smuzhiyun if (attr == &dev_eraseblock_size)
393*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->leb_size);
394*4882a593Smuzhiyun else if (attr == &dev_avail_eraseblocks)
395*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->avail_pebs);
396*4882a593Smuzhiyun else if (attr == &dev_total_eraseblocks)
397*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->good_peb_count);
398*4882a593Smuzhiyun else if (attr == &dev_volumes_count)
399*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->vol_count - UBI_INT_VOL_COUNT);
400*4882a593Smuzhiyun else if (attr == &dev_max_ec)
401*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->max_ec);
402*4882a593Smuzhiyun else if (attr == &dev_reserved_for_bad)
403*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
404*4882a593Smuzhiyun else if (attr == &dev_bad_peb_count)
405*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->bad_peb_count);
406*4882a593Smuzhiyun else if (attr == &dev_max_vol_count)
407*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->vtbl_slots);
408*4882a593Smuzhiyun else if (attr == &dev_min_io_size)
409*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->min_io_size);
410*4882a593Smuzhiyun else if (attr == &dev_bgt_enabled)
411*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->thread_enabled);
412*4882a593Smuzhiyun else if (attr == &dev_mtd_num)
413*4882a593Smuzhiyun ret = sprintf(buf, "%d\n", ubi->mtd->index);
414*4882a593Smuzhiyun else
415*4882a593Smuzhiyun ret = -EINVAL;
416*4882a593Smuzhiyun
417*4882a593Smuzhiyun ubi_put_device(ubi);
418*4882a593Smuzhiyun return ret;
419*4882a593Smuzhiyun }
420*4882a593Smuzhiyun
421*4882a593Smuzhiyun static struct attribute *ubi_dev_attrs[] = {
422*4882a593Smuzhiyun &dev_eraseblock_size.attr,
423*4882a593Smuzhiyun &dev_avail_eraseblocks.attr,
424*4882a593Smuzhiyun &dev_total_eraseblocks.attr,
425*4882a593Smuzhiyun &dev_volumes_count.attr,
426*4882a593Smuzhiyun &dev_max_ec.attr,
427*4882a593Smuzhiyun &dev_reserved_for_bad.attr,
428*4882a593Smuzhiyun &dev_bad_peb_count.attr,
429*4882a593Smuzhiyun &dev_max_vol_count.attr,
430*4882a593Smuzhiyun &dev_min_io_size.attr,
431*4882a593Smuzhiyun &dev_bgt_enabled.attr,
432*4882a593Smuzhiyun &dev_mtd_num.attr,
433*4882a593Smuzhiyun NULL
434*4882a593Smuzhiyun };
435*4882a593Smuzhiyun ATTRIBUTE_GROUPS(ubi_dev);
436*4882a593Smuzhiyun
dev_release(struct device * dev)437*4882a593Smuzhiyun static void dev_release(struct device *dev)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun struct ubi_device *ubi = container_of(dev, struct ubi_device, dev);
440*4882a593Smuzhiyun
441*4882a593Smuzhiyun kfree(ubi);
442*4882a593Smuzhiyun }
443*4882a593Smuzhiyun
444*4882a593Smuzhiyun /**
445*4882a593Smuzhiyun * ubi_sysfs_init - initialize sysfs for an UBI device.
446*4882a593Smuzhiyun * @ubi: UBI device description object
447*4882a593Smuzhiyun * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
448*4882a593Smuzhiyun * taken
449*4882a593Smuzhiyun *
450*4882a593Smuzhiyun * This function returns zero in case of success and a negative error code in
451*4882a593Smuzhiyun * case of failure.
452*4882a593Smuzhiyun */
ubi_sysfs_init(struct ubi_device * ubi,int * ref)453*4882a593Smuzhiyun static int ubi_sysfs_init(struct ubi_device *ubi, int *ref)
454*4882a593Smuzhiyun {
455*4882a593Smuzhiyun int err;
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun ubi->dev.release = dev_release;
458*4882a593Smuzhiyun ubi->dev.devt = ubi->cdev.dev;
459*4882a593Smuzhiyun ubi->dev.class = &ubi_class;
460*4882a593Smuzhiyun ubi->dev.groups = ubi_dev_groups;
461*4882a593Smuzhiyun dev_set_name(&ubi->dev, UBI_NAME_STR"%d", ubi->ubi_num);
462*4882a593Smuzhiyun err = device_register(&ubi->dev);
463*4882a593Smuzhiyun if (err)
464*4882a593Smuzhiyun return err;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun *ref = 1;
467*4882a593Smuzhiyun return 0;
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun /**
471*4882a593Smuzhiyun * ubi_sysfs_close - close sysfs for an UBI device.
472*4882a593Smuzhiyun * @ubi: UBI device description object
473*4882a593Smuzhiyun */
ubi_sysfs_close(struct ubi_device * ubi)474*4882a593Smuzhiyun static void ubi_sysfs_close(struct ubi_device *ubi)
475*4882a593Smuzhiyun {
476*4882a593Smuzhiyun device_unregister(&ubi->dev);
477*4882a593Smuzhiyun }
478*4882a593Smuzhiyun #endif
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun /**
481*4882a593Smuzhiyun * kill_volumes - destroy all user volumes.
482*4882a593Smuzhiyun * @ubi: UBI device description object
483*4882a593Smuzhiyun */
kill_volumes(struct ubi_device * ubi)484*4882a593Smuzhiyun static void kill_volumes(struct ubi_device *ubi)
485*4882a593Smuzhiyun {
486*4882a593Smuzhiyun int i;
487*4882a593Smuzhiyun
488*4882a593Smuzhiyun for (i = 0; i < ubi->vtbl_slots; i++)
489*4882a593Smuzhiyun if (ubi->volumes[i])
490*4882a593Smuzhiyun ubi_free_volume(ubi, ubi->volumes[i]);
491*4882a593Smuzhiyun }
492*4882a593Smuzhiyun
493*4882a593Smuzhiyun /**
494*4882a593Smuzhiyun * uif_init - initialize user interfaces for an UBI device.
495*4882a593Smuzhiyun * @ubi: UBI device description object
496*4882a593Smuzhiyun * @ref: set to %1 on exit in case of failure if a reference to @ubi->dev was
497*4882a593Smuzhiyun * taken, otherwise set to %0
498*4882a593Smuzhiyun *
499*4882a593Smuzhiyun * This function initializes various user interfaces for an UBI device. If the
500*4882a593Smuzhiyun * initialization fails at an early stage, this function frees all the
501*4882a593Smuzhiyun * resources it allocated, returns an error, and @ref is set to %0. However,
502*4882a593Smuzhiyun * if the initialization fails after the UBI device was registered in the
503*4882a593Smuzhiyun * driver core subsystem, this function takes a reference to @ubi->dev, because
504*4882a593Smuzhiyun * otherwise the release function ('dev_release()') would free whole @ubi
505*4882a593Smuzhiyun * object. The @ref argument is set to %1 in this case. The caller has to put
506*4882a593Smuzhiyun * this reference.
507*4882a593Smuzhiyun *
508*4882a593Smuzhiyun * This function returns zero in case of success and a negative error code in
509*4882a593Smuzhiyun * case of failure.
510*4882a593Smuzhiyun */
uif_init(struct ubi_device * ubi,int * ref)511*4882a593Smuzhiyun static int uif_init(struct ubi_device *ubi, int *ref)
512*4882a593Smuzhiyun {
513*4882a593Smuzhiyun int i, err;
514*4882a593Smuzhiyun #ifndef __UBOOT__
515*4882a593Smuzhiyun dev_t dev;
516*4882a593Smuzhiyun #endif
517*4882a593Smuzhiyun
518*4882a593Smuzhiyun *ref = 0;
519*4882a593Smuzhiyun sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
520*4882a593Smuzhiyun
521*4882a593Smuzhiyun /*
522*4882a593Smuzhiyun * Major numbers for the UBI character devices are allocated
523*4882a593Smuzhiyun * dynamically. Major numbers of volume character devices are
524*4882a593Smuzhiyun * equivalent to ones of the corresponding UBI character device. Minor
525*4882a593Smuzhiyun * numbers of UBI character devices are 0, while minor numbers of
526*4882a593Smuzhiyun * volume character devices start from 1. Thus, we allocate one major
527*4882a593Smuzhiyun * number and ubi->vtbl_slots + 1 minor numbers.
528*4882a593Smuzhiyun */
529*4882a593Smuzhiyun err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
530*4882a593Smuzhiyun if (err) {
531*4882a593Smuzhiyun ubi_err(ubi, "cannot register UBI character devices");
532*4882a593Smuzhiyun return err;
533*4882a593Smuzhiyun }
534*4882a593Smuzhiyun
535*4882a593Smuzhiyun ubi_assert(MINOR(dev) == 0);
536*4882a593Smuzhiyun cdev_init(&ubi->cdev, &ubi_cdev_operations);
537*4882a593Smuzhiyun dbg_gen("%s major is %u", ubi->ubi_name, MAJOR(dev));
538*4882a593Smuzhiyun ubi->cdev.owner = THIS_MODULE;
539*4882a593Smuzhiyun
540*4882a593Smuzhiyun err = cdev_add(&ubi->cdev, dev, 1);
541*4882a593Smuzhiyun if (err) {
542*4882a593Smuzhiyun ubi_err(ubi, "cannot add character device");
543*4882a593Smuzhiyun goto out_unreg;
544*4882a593Smuzhiyun }
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun err = ubi_sysfs_init(ubi, ref);
547*4882a593Smuzhiyun if (err)
548*4882a593Smuzhiyun goto out_sysfs;
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun for (i = 0; i < ubi->vtbl_slots; i++)
551*4882a593Smuzhiyun if (ubi->volumes[i]) {
552*4882a593Smuzhiyun err = ubi_add_volume(ubi, ubi->volumes[i]);
553*4882a593Smuzhiyun if (err) {
554*4882a593Smuzhiyun ubi_err(ubi, "cannot add volume %d", i);
555*4882a593Smuzhiyun goto out_volumes;
556*4882a593Smuzhiyun }
557*4882a593Smuzhiyun }
558*4882a593Smuzhiyun
559*4882a593Smuzhiyun return 0;
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun out_volumes:
562*4882a593Smuzhiyun kill_volumes(ubi);
563*4882a593Smuzhiyun out_sysfs:
564*4882a593Smuzhiyun if (*ref)
565*4882a593Smuzhiyun get_device(&ubi->dev);
566*4882a593Smuzhiyun ubi_sysfs_close(ubi);
567*4882a593Smuzhiyun cdev_del(&ubi->cdev);
568*4882a593Smuzhiyun out_unreg:
569*4882a593Smuzhiyun unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
570*4882a593Smuzhiyun ubi_err(ubi, "cannot initialize UBI %s, error %d",
571*4882a593Smuzhiyun ubi->ubi_name, err);
572*4882a593Smuzhiyun return err;
573*4882a593Smuzhiyun }
574*4882a593Smuzhiyun
575*4882a593Smuzhiyun /**
576*4882a593Smuzhiyun * uif_close - close user interfaces for an UBI device.
577*4882a593Smuzhiyun * @ubi: UBI device description object
578*4882a593Smuzhiyun *
579*4882a593Smuzhiyun * Note, since this function un-registers UBI volume device objects (@vol->dev),
580*4882a593Smuzhiyun * the memory allocated voe the volumes is freed as well (in the release
581*4882a593Smuzhiyun * function).
582*4882a593Smuzhiyun */
uif_close(struct ubi_device * ubi)583*4882a593Smuzhiyun static void uif_close(struct ubi_device *ubi)
584*4882a593Smuzhiyun {
585*4882a593Smuzhiyun kill_volumes(ubi);
586*4882a593Smuzhiyun ubi_sysfs_close(ubi);
587*4882a593Smuzhiyun cdev_del(&ubi->cdev);
588*4882a593Smuzhiyun unregister_chrdev_region(ubi->cdev.dev, ubi->vtbl_slots + 1);
589*4882a593Smuzhiyun }
590*4882a593Smuzhiyun
591*4882a593Smuzhiyun /**
592*4882a593Smuzhiyun * ubi_free_internal_volumes - free internal volumes.
593*4882a593Smuzhiyun * @ubi: UBI device description object
594*4882a593Smuzhiyun */
ubi_free_internal_volumes(struct ubi_device * ubi)595*4882a593Smuzhiyun void ubi_free_internal_volumes(struct ubi_device *ubi)
596*4882a593Smuzhiyun {
597*4882a593Smuzhiyun int i;
598*4882a593Smuzhiyun
599*4882a593Smuzhiyun for (i = ubi->vtbl_slots;
600*4882a593Smuzhiyun i < ubi->vtbl_slots + UBI_INT_VOL_COUNT; i++) {
601*4882a593Smuzhiyun kfree(ubi->volumes[i]->eba_tbl);
602*4882a593Smuzhiyun kfree(ubi->volumes[i]);
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun }
605*4882a593Smuzhiyun
get_bad_peb_limit(const struct ubi_device * ubi,int max_beb_per1024)606*4882a593Smuzhiyun static int get_bad_peb_limit(const struct ubi_device *ubi, int max_beb_per1024)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun int limit, device_pebs;
609*4882a593Smuzhiyun uint64_t device_size;
610*4882a593Smuzhiyun
611*4882a593Smuzhiyun if (!max_beb_per1024)
612*4882a593Smuzhiyun return 0;
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun /*
615*4882a593Smuzhiyun * Here we are using size of the entire flash chip and
616*4882a593Smuzhiyun * not just the MTD partition size because the maximum
617*4882a593Smuzhiyun * number of bad eraseblocks is a percentage of the
618*4882a593Smuzhiyun * whole device and bad eraseblocks are not fairly
619*4882a593Smuzhiyun * distributed over the flash chip. So the worst case
620*4882a593Smuzhiyun * is that all the bad eraseblocks of the chip are in
621*4882a593Smuzhiyun * the MTD partition we are attaching (ubi->mtd).
622*4882a593Smuzhiyun */
623*4882a593Smuzhiyun device_size = mtd_get_device_size(ubi->mtd);
624*4882a593Smuzhiyun device_pebs = mtd_div_by_eb(device_size, ubi->mtd);
625*4882a593Smuzhiyun limit = mult_frac(device_pebs, max_beb_per1024, 1024);
626*4882a593Smuzhiyun
627*4882a593Smuzhiyun /* Round it up */
628*4882a593Smuzhiyun if (mult_frac(limit, 1024, max_beb_per1024) < device_pebs)
629*4882a593Smuzhiyun limit += 1;
630*4882a593Smuzhiyun
631*4882a593Smuzhiyun return limit;
632*4882a593Smuzhiyun }
633*4882a593Smuzhiyun
634*4882a593Smuzhiyun /**
635*4882a593Smuzhiyun * io_init - initialize I/O sub-system for a given UBI device.
636*4882a593Smuzhiyun * @ubi: UBI device description object
637*4882a593Smuzhiyun * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
638*4882a593Smuzhiyun *
639*4882a593Smuzhiyun * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
640*4882a593Smuzhiyun * assumed:
641*4882a593Smuzhiyun * o EC header is always at offset zero - this cannot be changed;
642*4882a593Smuzhiyun * o VID header starts just after the EC header at the closest address
643*4882a593Smuzhiyun * aligned to @io->hdrs_min_io_size;
644*4882a593Smuzhiyun * o data starts just after the VID header at the closest address aligned to
645*4882a593Smuzhiyun * @io->min_io_size
646*4882a593Smuzhiyun *
647*4882a593Smuzhiyun * This function returns zero in case of success and a negative error code in
648*4882a593Smuzhiyun * case of failure.
649*4882a593Smuzhiyun */
io_init(struct ubi_device * ubi,int max_beb_per1024)650*4882a593Smuzhiyun static int io_init(struct ubi_device *ubi, int max_beb_per1024)
651*4882a593Smuzhiyun {
652*4882a593Smuzhiyun dbg_gen("sizeof(struct ubi_ainf_peb) %zu", sizeof(struct ubi_ainf_peb));
653*4882a593Smuzhiyun dbg_gen("sizeof(struct ubi_wl_entry) %zu", sizeof(struct ubi_wl_entry));
654*4882a593Smuzhiyun
655*4882a593Smuzhiyun if (ubi->mtd->numeraseregions != 0) {
656*4882a593Smuzhiyun /*
657*4882a593Smuzhiyun * Some flashes have several erase regions. Different regions
658*4882a593Smuzhiyun * may have different eraseblock size and other
659*4882a593Smuzhiyun * characteristics. It looks like mostly multi-region flashes
660*4882a593Smuzhiyun * have one "main" region and one or more small regions to
661*4882a593Smuzhiyun * store boot loader code or boot parameters or whatever. I
662*4882a593Smuzhiyun * guess we should just pick the largest region. But this is
663*4882a593Smuzhiyun * not implemented.
664*4882a593Smuzhiyun */
665*4882a593Smuzhiyun ubi_err(ubi, "multiple regions, not implemented");
666*4882a593Smuzhiyun return -EINVAL;
667*4882a593Smuzhiyun }
668*4882a593Smuzhiyun
669*4882a593Smuzhiyun if (ubi->vid_hdr_offset < 0)
670*4882a593Smuzhiyun return -EINVAL;
671*4882a593Smuzhiyun
672*4882a593Smuzhiyun /*
673*4882a593Smuzhiyun * Note, in this implementation we support MTD devices with 0x7FFFFFFF
674*4882a593Smuzhiyun * physical eraseblocks maximum.
675*4882a593Smuzhiyun */
676*4882a593Smuzhiyun
677*4882a593Smuzhiyun ubi->peb_size = ubi->mtd->erasesize;
678*4882a593Smuzhiyun ubi->peb_count = mtd_div_by_eb(ubi->mtd->size, ubi->mtd);
679*4882a593Smuzhiyun ubi->flash_size = ubi->mtd->size;
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (mtd_can_have_bb(ubi->mtd)) {
682*4882a593Smuzhiyun ubi->bad_allowed = 1;
683*4882a593Smuzhiyun ubi->bad_peb_limit = get_bad_peb_limit(ubi, max_beb_per1024);
684*4882a593Smuzhiyun }
685*4882a593Smuzhiyun
686*4882a593Smuzhiyun if (ubi->mtd->type == MTD_NORFLASH) {
687*4882a593Smuzhiyun ubi_assert(ubi->mtd->writesize == 1);
688*4882a593Smuzhiyun ubi->nor_flash = 1;
689*4882a593Smuzhiyun }
690*4882a593Smuzhiyun
691*4882a593Smuzhiyun ubi->min_io_size = ubi->mtd->writesize;
692*4882a593Smuzhiyun ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
693*4882a593Smuzhiyun
694*4882a593Smuzhiyun /*
695*4882a593Smuzhiyun * Make sure minimal I/O unit is power of 2. Note, there is no
696*4882a593Smuzhiyun * fundamental reason for this assumption. It is just an optimization
697*4882a593Smuzhiyun * which allows us to avoid costly division operations.
698*4882a593Smuzhiyun */
699*4882a593Smuzhiyun if (!is_power_of_2(ubi->min_io_size)) {
700*4882a593Smuzhiyun ubi_err(ubi, "min. I/O unit (%d) is not power of 2",
701*4882a593Smuzhiyun ubi->min_io_size);
702*4882a593Smuzhiyun return -EINVAL;
703*4882a593Smuzhiyun }
704*4882a593Smuzhiyun
705*4882a593Smuzhiyun ubi_assert(ubi->hdrs_min_io_size > 0);
706*4882a593Smuzhiyun ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
707*4882a593Smuzhiyun ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
708*4882a593Smuzhiyun
709*4882a593Smuzhiyun ubi->max_write_size = ubi->mtd->writebufsize;
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * Maximum write size has to be greater or equivalent to min. I/O
712*4882a593Smuzhiyun * size, and be multiple of min. I/O size.
713*4882a593Smuzhiyun */
714*4882a593Smuzhiyun if (ubi->max_write_size < ubi->min_io_size ||
715*4882a593Smuzhiyun ubi->max_write_size % ubi->min_io_size ||
716*4882a593Smuzhiyun !is_power_of_2(ubi->max_write_size)) {
717*4882a593Smuzhiyun ubi_err(ubi, "bad write buffer size %d for %d min. I/O unit",
718*4882a593Smuzhiyun ubi->max_write_size, ubi->min_io_size);
719*4882a593Smuzhiyun return -EINVAL;
720*4882a593Smuzhiyun }
721*4882a593Smuzhiyun
722*4882a593Smuzhiyun /* Calculate default aligned sizes of EC and VID headers */
723*4882a593Smuzhiyun ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
724*4882a593Smuzhiyun ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun dbg_gen("min_io_size %d", ubi->min_io_size);
727*4882a593Smuzhiyun dbg_gen("max_write_size %d", ubi->max_write_size);
728*4882a593Smuzhiyun dbg_gen("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
729*4882a593Smuzhiyun dbg_gen("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
730*4882a593Smuzhiyun dbg_gen("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
731*4882a593Smuzhiyun
732*4882a593Smuzhiyun if (ubi->vid_hdr_offset == 0)
733*4882a593Smuzhiyun /* Default offset */
734*4882a593Smuzhiyun ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
735*4882a593Smuzhiyun ubi->ec_hdr_alsize;
736*4882a593Smuzhiyun else {
737*4882a593Smuzhiyun ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
738*4882a593Smuzhiyun ~(ubi->hdrs_min_io_size - 1);
739*4882a593Smuzhiyun ubi->vid_hdr_shift = ubi->vid_hdr_offset -
740*4882a593Smuzhiyun ubi->vid_hdr_aloffset;
741*4882a593Smuzhiyun }
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /* Similar for the data offset */
744*4882a593Smuzhiyun ubi->leb_start = ubi->vid_hdr_offset + UBI_VID_HDR_SIZE;
745*4882a593Smuzhiyun ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
746*4882a593Smuzhiyun
747*4882a593Smuzhiyun dbg_gen("vid_hdr_offset %d", ubi->vid_hdr_offset);
748*4882a593Smuzhiyun dbg_gen("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
749*4882a593Smuzhiyun dbg_gen("vid_hdr_shift %d", ubi->vid_hdr_shift);
750*4882a593Smuzhiyun dbg_gen("leb_start %d", ubi->leb_start);
751*4882a593Smuzhiyun
752*4882a593Smuzhiyun /* The shift must be aligned to 32-bit boundary */
753*4882a593Smuzhiyun if (ubi->vid_hdr_shift % 4) {
754*4882a593Smuzhiyun ubi_err(ubi, "unaligned VID header shift %d",
755*4882a593Smuzhiyun ubi->vid_hdr_shift);
756*4882a593Smuzhiyun return -EINVAL;
757*4882a593Smuzhiyun }
758*4882a593Smuzhiyun
759*4882a593Smuzhiyun /* Check sanity */
760*4882a593Smuzhiyun if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
761*4882a593Smuzhiyun ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
762*4882a593Smuzhiyun ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
763*4882a593Smuzhiyun ubi->leb_start & (ubi->min_io_size - 1)) {
764*4882a593Smuzhiyun ubi_err(ubi, "bad VID header (%d) or data offsets (%d)",
765*4882a593Smuzhiyun ubi->vid_hdr_offset, ubi->leb_start);
766*4882a593Smuzhiyun return -EINVAL;
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun /*
770*4882a593Smuzhiyun * Set maximum amount of physical erroneous eraseblocks to be 10%.
771*4882a593Smuzhiyun * Erroneous PEB are those which have read errors.
772*4882a593Smuzhiyun */
773*4882a593Smuzhiyun ubi->max_erroneous = ubi->peb_count / 10;
774*4882a593Smuzhiyun if (ubi->max_erroneous < 16)
775*4882a593Smuzhiyun ubi->max_erroneous = 16;
776*4882a593Smuzhiyun dbg_gen("max_erroneous %d", ubi->max_erroneous);
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun /*
779*4882a593Smuzhiyun * It may happen that EC and VID headers are situated in one minimal
780*4882a593Smuzhiyun * I/O unit. In this case we can only accept this UBI image in
781*4882a593Smuzhiyun * read-only mode.
782*4882a593Smuzhiyun */
783*4882a593Smuzhiyun if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
784*4882a593Smuzhiyun ubi_warn(ubi, "EC and VID headers are in the same minimal I/O unit, switch to read-only mode");
785*4882a593Smuzhiyun ubi->ro_mode = 1;
786*4882a593Smuzhiyun }
787*4882a593Smuzhiyun
788*4882a593Smuzhiyun ubi->leb_size = ubi->peb_size - ubi->leb_start;
789*4882a593Smuzhiyun
790*4882a593Smuzhiyun if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
791*4882a593Smuzhiyun ubi_msg(ubi, "MTD device %d is write-protected, attach in read-only mode",
792*4882a593Smuzhiyun ubi->mtd->index);
793*4882a593Smuzhiyun ubi->ro_mode = 1;
794*4882a593Smuzhiyun }
795*4882a593Smuzhiyun
796*4882a593Smuzhiyun /*
797*4882a593Smuzhiyun * Note, ideally, we have to initialize @ubi->bad_peb_count here. But
798*4882a593Smuzhiyun * unfortunately, MTD does not provide this information. We should loop
799*4882a593Smuzhiyun * over all physical eraseblocks and invoke mtd->block_is_bad() for
800*4882a593Smuzhiyun * each physical eraseblock. So, we leave @ubi->bad_peb_count
801*4882a593Smuzhiyun * uninitialized so far.
802*4882a593Smuzhiyun */
803*4882a593Smuzhiyun
804*4882a593Smuzhiyun return 0;
805*4882a593Smuzhiyun }
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun /**
808*4882a593Smuzhiyun * autoresize - re-size the volume which has the "auto-resize" flag set.
809*4882a593Smuzhiyun * @ubi: UBI device description object
810*4882a593Smuzhiyun * @vol_id: ID of the volume to re-size
811*4882a593Smuzhiyun *
812*4882a593Smuzhiyun * This function re-sizes the volume marked by the %UBI_VTBL_AUTORESIZE_FLG in
813*4882a593Smuzhiyun * the volume table to the largest possible size. See comments in ubi-header.h
814*4882a593Smuzhiyun * for more description of the flag. Returns zero in case of success and a
815*4882a593Smuzhiyun * negative error code in case of failure.
816*4882a593Smuzhiyun */
autoresize(struct ubi_device * ubi,int vol_id)817*4882a593Smuzhiyun static int autoresize(struct ubi_device *ubi, int vol_id)
818*4882a593Smuzhiyun {
819*4882a593Smuzhiyun struct ubi_volume_desc desc;
820*4882a593Smuzhiyun struct ubi_volume *vol = ubi->volumes[vol_id];
821*4882a593Smuzhiyun int err, old_reserved_pebs = vol->reserved_pebs;
822*4882a593Smuzhiyun
823*4882a593Smuzhiyun if (ubi->ro_mode) {
824*4882a593Smuzhiyun ubi_warn(ubi, "skip auto-resize because of R/O mode");
825*4882a593Smuzhiyun return 0;
826*4882a593Smuzhiyun }
827*4882a593Smuzhiyun
828*4882a593Smuzhiyun /*
829*4882a593Smuzhiyun * Clear the auto-resize flag in the volume in-memory copy of the
830*4882a593Smuzhiyun * volume table, and 'ubi_resize_volume()' will propagate this change
831*4882a593Smuzhiyun * to the flash.
832*4882a593Smuzhiyun */
833*4882a593Smuzhiyun ubi->vtbl[vol_id].flags &= ~UBI_VTBL_AUTORESIZE_FLG;
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun if (ubi->avail_pebs == 0) {
836*4882a593Smuzhiyun struct ubi_vtbl_record vtbl_rec;
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun /*
839*4882a593Smuzhiyun * No available PEBs to re-size the volume, clear the flag on
840*4882a593Smuzhiyun * flash and exit.
841*4882a593Smuzhiyun */
842*4882a593Smuzhiyun vtbl_rec = ubi->vtbl[vol_id];
843*4882a593Smuzhiyun err = ubi_change_vtbl_record(ubi, vol_id, &vtbl_rec);
844*4882a593Smuzhiyun if (err)
845*4882a593Smuzhiyun ubi_err(ubi, "cannot clean auto-resize flag for volume %d",
846*4882a593Smuzhiyun vol_id);
847*4882a593Smuzhiyun } else {
848*4882a593Smuzhiyun desc.vol = vol;
849*4882a593Smuzhiyun err = ubi_resize_volume(&desc,
850*4882a593Smuzhiyun old_reserved_pebs + ubi->avail_pebs);
851*4882a593Smuzhiyun if (err)
852*4882a593Smuzhiyun ubi_err(ubi, "cannot auto-resize volume %d",
853*4882a593Smuzhiyun vol_id);
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun
856*4882a593Smuzhiyun if (err)
857*4882a593Smuzhiyun return err;
858*4882a593Smuzhiyun
859*4882a593Smuzhiyun ubi_msg(ubi, "volume %d (\"%s\") re-sized from %d to %d LEBs",
860*4882a593Smuzhiyun vol_id, vol->name, old_reserved_pebs, vol->reserved_pebs);
861*4882a593Smuzhiyun return 0;
862*4882a593Smuzhiyun }
863*4882a593Smuzhiyun
864*4882a593Smuzhiyun /**
865*4882a593Smuzhiyun * ubi_attach_mtd_dev - attach an MTD device.
866*4882a593Smuzhiyun * @mtd: MTD device description object
867*4882a593Smuzhiyun * @ubi_num: number to assign to the new UBI device
868*4882a593Smuzhiyun * @vid_hdr_offset: VID header offset
869*4882a593Smuzhiyun * @max_beb_per1024: maximum expected number of bad PEB per 1024 PEBs
870*4882a593Smuzhiyun *
871*4882a593Smuzhiyun * This function attaches MTD device @mtd_dev to UBI and assign @ubi_num number
872*4882a593Smuzhiyun * to the newly created UBI device, unless @ubi_num is %UBI_DEV_NUM_AUTO, in
873*4882a593Smuzhiyun * which case this function finds a vacant device number and assigns it
874*4882a593Smuzhiyun * automatically. Returns the new UBI device number in case of success and a
875*4882a593Smuzhiyun * negative error code in case of failure.
876*4882a593Smuzhiyun *
877*4882a593Smuzhiyun * Note, the invocations of this function has to be serialized by the
878*4882a593Smuzhiyun * @ubi_devices_mutex.
879*4882a593Smuzhiyun */
ubi_attach_mtd_dev(struct mtd_info * mtd,int ubi_num,int vid_hdr_offset,int max_beb_per1024)880*4882a593Smuzhiyun int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num,
881*4882a593Smuzhiyun int vid_hdr_offset, int max_beb_per1024)
882*4882a593Smuzhiyun {
883*4882a593Smuzhiyun struct ubi_device *ubi;
884*4882a593Smuzhiyun int i, err, ref = 0;
885*4882a593Smuzhiyun
886*4882a593Smuzhiyun if (max_beb_per1024 < 0 || max_beb_per1024 > MAX_MTD_UBI_BEB_LIMIT)
887*4882a593Smuzhiyun return -EINVAL;
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun if (!max_beb_per1024)
890*4882a593Smuzhiyun max_beb_per1024 = CONFIG_MTD_UBI_BEB_LIMIT;
891*4882a593Smuzhiyun
892*4882a593Smuzhiyun /*
893*4882a593Smuzhiyun * Check if we already have the same MTD device attached.
894*4882a593Smuzhiyun *
895*4882a593Smuzhiyun * Note, this function assumes that UBI devices creations and deletions
896*4882a593Smuzhiyun * are serialized, so it does not take the &ubi_devices_lock.
897*4882a593Smuzhiyun */
898*4882a593Smuzhiyun for (i = 0; i < UBI_MAX_DEVICES; i++) {
899*4882a593Smuzhiyun ubi = ubi_devices[i];
900*4882a593Smuzhiyun if (ubi && mtd->index == ubi->mtd->index) {
901*4882a593Smuzhiyun ubi_err(ubi, "mtd%d is already attached to ubi%d",
902*4882a593Smuzhiyun mtd->index, i);
903*4882a593Smuzhiyun return -EEXIST;
904*4882a593Smuzhiyun }
905*4882a593Smuzhiyun }
906*4882a593Smuzhiyun
907*4882a593Smuzhiyun /*
908*4882a593Smuzhiyun * Make sure this MTD device is not emulated on top of an UBI volume
909*4882a593Smuzhiyun * already. Well, generally this recursion works fine, but there are
910*4882a593Smuzhiyun * different problems like the UBI module takes a reference to itself
911*4882a593Smuzhiyun * by attaching (and thus, opening) the emulated MTD device. This
912*4882a593Smuzhiyun * results in inability to unload the module. And in general it makes
913*4882a593Smuzhiyun * no sense to attach emulated MTD devices, so we prohibit this.
914*4882a593Smuzhiyun */
915*4882a593Smuzhiyun if (mtd->type == MTD_UBIVOLUME) {
916*4882a593Smuzhiyun ubi_err(ubi, "refuse attaching mtd%d - it is already emulated on top of UBI",
917*4882a593Smuzhiyun mtd->index);
918*4882a593Smuzhiyun return -EINVAL;
919*4882a593Smuzhiyun }
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun if (ubi_num == UBI_DEV_NUM_AUTO) {
922*4882a593Smuzhiyun /* Search for an empty slot in the @ubi_devices array */
923*4882a593Smuzhiyun for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++)
924*4882a593Smuzhiyun if (!ubi_devices[ubi_num])
925*4882a593Smuzhiyun break;
926*4882a593Smuzhiyun if (ubi_num == UBI_MAX_DEVICES) {
927*4882a593Smuzhiyun ubi_err(ubi, "only %d UBI devices may be created",
928*4882a593Smuzhiyun UBI_MAX_DEVICES);
929*4882a593Smuzhiyun return -ENFILE;
930*4882a593Smuzhiyun }
931*4882a593Smuzhiyun } else {
932*4882a593Smuzhiyun if (ubi_num >= UBI_MAX_DEVICES)
933*4882a593Smuzhiyun return -EINVAL;
934*4882a593Smuzhiyun
935*4882a593Smuzhiyun /* Make sure ubi_num is not busy */
936*4882a593Smuzhiyun if (ubi_devices[ubi_num]) {
937*4882a593Smuzhiyun ubi_err(ubi, "already exists");
938*4882a593Smuzhiyun return -EEXIST;
939*4882a593Smuzhiyun }
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun ubi = kzalloc(sizeof(struct ubi_device), GFP_KERNEL);
943*4882a593Smuzhiyun if (!ubi)
944*4882a593Smuzhiyun return -ENOMEM;
945*4882a593Smuzhiyun
946*4882a593Smuzhiyun ubi->mtd = mtd;
947*4882a593Smuzhiyun ubi->ubi_num = ubi_num;
948*4882a593Smuzhiyun ubi->vid_hdr_offset = vid_hdr_offset;
949*4882a593Smuzhiyun ubi->autoresize_vol_id = -1;
950*4882a593Smuzhiyun
951*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
952*4882a593Smuzhiyun ubi->fm_pool.used = ubi->fm_pool.size = 0;
953*4882a593Smuzhiyun ubi->fm_wl_pool.used = ubi->fm_wl_pool.size = 0;
954*4882a593Smuzhiyun
955*4882a593Smuzhiyun /*
956*4882a593Smuzhiyun * fm_pool.max_size is 5% of the total number of PEBs but it's also
957*4882a593Smuzhiyun * between UBI_FM_MAX_POOL_SIZE and UBI_FM_MIN_POOL_SIZE.
958*4882a593Smuzhiyun */
959*4882a593Smuzhiyun ubi->fm_pool.max_size = min(((int)mtd_div_by_eb(ubi->mtd->size,
960*4882a593Smuzhiyun ubi->mtd) / 100) * 5, UBI_FM_MAX_POOL_SIZE);
961*4882a593Smuzhiyun ubi->fm_pool.max_size = max(ubi->fm_pool.max_size,
962*4882a593Smuzhiyun UBI_FM_MIN_POOL_SIZE);
963*4882a593Smuzhiyun
964*4882a593Smuzhiyun ubi->fm_wl_pool.max_size = ubi->fm_pool.max_size / 2;
965*4882a593Smuzhiyun ubi->fm_disabled = !fm_autoconvert;
966*4882a593Smuzhiyun if (fm_debug)
967*4882a593Smuzhiyun ubi_enable_dbg_chk_fastmap(ubi);
968*4882a593Smuzhiyun
969*4882a593Smuzhiyun if (!ubi->fm_disabled && (int)mtd_div_by_eb(ubi->mtd->size, ubi->mtd)
970*4882a593Smuzhiyun <= UBI_FM_MAX_START) {
971*4882a593Smuzhiyun ubi_err(ubi, "More than %i PEBs are needed for fastmap, sorry.",
972*4882a593Smuzhiyun UBI_FM_MAX_START);
973*4882a593Smuzhiyun ubi->fm_disabled = 1;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun
976*4882a593Smuzhiyun ubi_msg(ubi, "default fastmap pool size: %d", ubi->fm_pool.max_size);
977*4882a593Smuzhiyun ubi_msg(ubi, "default fastmap WL pool size: %d",
978*4882a593Smuzhiyun ubi->fm_wl_pool.max_size);
979*4882a593Smuzhiyun #else
980*4882a593Smuzhiyun ubi->fm_disabled = 1;
981*4882a593Smuzhiyun #endif
982*4882a593Smuzhiyun mutex_init(&ubi->buf_mutex);
983*4882a593Smuzhiyun mutex_init(&ubi->ckvol_mutex);
984*4882a593Smuzhiyun mutex_init(&ubi->device_mutex);
985*4882a593Smuzhiyun spin_lock_init(&ubi->volumes_lock);
986*4882a593Smuzhiyun init_rwsem(&ubi->fm_protect);
987*4882a593Smuzhiyun init_rwsem(&ubi->fm_eba_sem);
988*4882a593Smuzhiyun
989*4882a593Smuzhiyun ubi_msg(ubi, "attaching mtd%d", mtd->index);
990*4882a593Smuzhiyun
991*4882a593Smuzhiyun err = io_init(ubi, max_beb_per1024);
992*4882a593Smuzhiyun if (err)
993*4882a593Smuzhiyun goto out_free;
994*4882a593Smuzhiyun
995*4882a593Smuzhiyun err = -ENOMEM;
996*4882a593Smuzhiyun ubi->peb_buf = vmalloc(ubi->peb_size);
997*4882a593Smuzhiyun if (!ubi->peb_buf)
998*4882a593Smuzhiyun goto out_free;
999*4882a593Smuzhiyun
1000*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
1001*4882a593Smuzhiyun ubi->fm_size = ubi_calc_fm_size(ubi);
1002*4882a593Smuzhiyun ubi->fm_buf = vzalloc(ubi->fm_size);
1003*4882a593Smuzhiyun if (!ubi->fm_buf)
1004*4882a593Smuzhiyun goto out_free;
1005*4882a593Smuzhiyun #endif
1006*4882a593Smuzhiyun err = ubi_attach(ubi, 0);
1007*4882a593Smuzhiyun if (err) {
1008*4882a593Smuzhiyun ubi_err(ubi, "failed to attach mtd%d, error %d",
1009*4882a593Smuzhiyun mtd->index, err);
1010*4882a593Smuzhiyun goto out_free;
1011*4882a593Smuzhiyun }
1012*4882a593Smuzhiyun
1013*4882a593Smuzhiyun if (ubi->autoresize_vol_id != -1) {
1014*4882a593Smuzhiyun err = autoresize(ubi, ubi->autoresize_vol_id);
1015*4882a593Smuzhiyun if (err)
1016*4882a593Smuzhiyun goto out_detach;
1017*4882a593Smuzhiyun }
1018*4882a593Smuzhiyun
1019*4882a593Smuzhiyun err = uif_init(ubi, &ref);
1020*4882a593Smuzhiyun if (err)
1021*4882a593Smuzhiyun goto out_detach;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun err = ubi_debugfs_init_dev(ubi);
1024*4882a593Smuzhiyun if (err)
1025*4882a593Smuzhiyun goto out_uif;
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun ubi->bgt_thread = kthread_create(ubi_thread, ubi, "%s", ubi->bgt_name);
1028*4882a593Smuzhiyun if (IS_ERR(ubi->bgt_thread)) {
1029*4882a593Smuzhiyun err = PTR_ERR(ubi->bgt_thread);
1030*4882a593Smuzhiyun ubi_err(ubi, "cannot spawn \"%s\", error %d",
1031*4882a593Smuzhiyun ubi->bgt_name, err);
1032*4882a593Smuzhiyun goto out_debugfs;
1033*4882a593Smuzhiyun }
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun ubi_msg(ubi, "attached mtd%d (name \"%s\", size %llu MiB)",
1036*4882a593Smuzhiyun mtd->index, mtd->name, ubi->flash_size >> 20);
1037*4882a593Smuzhiyun ubi_msg(ubi, "PEB size: %d bytes (%d KiB), LEB size: %d bytes",
1038*4882a593Smuzhiyun ubi->peb_size, ubi->peb_size >> 10, ubi->leb_size);
1039*4882a593Smuzhiyun ubi_msg(ubi, "min./max. I/O unit sizes: %d/%d, sub-page size %d",
1040*4882a593Smuzhiyun ubi->min_io_size, ubi->max_write_size, ubi->hdrs_min_io_size);
1041*4882a593Smuzhiyun ubi_msg(ubi, "VID header offset: %d (aligned %d), data offset: %d",
1042*4882a593Smuzhiyun ubi->vid_hdr_offset, ubi->vid_hdr_aloffset, ubi->leb_start);
1043*4882a593Smuzhiyun ubi_msg(ubi, "good PEBs: %d, bad PEBs: %d, corrupted PEBs: %d",
1044*4882a593Smuzhiyun ubi->good_peb_count, ubi->bad_peb_count, ubi->corr_peb_count);
1045*4882a593Smuzhiyun ubi_msg(ubi, "user volume: %d, internal volumes: %d, max. volumes count: %d",
1046*4882a593Smuzhiyun ubi->vol_count - UBI_INT_VOL_COUNT, UBI_INT_VOL_COUNT,
1047*4882a593Smuzhiyun ubi->vtbl_slots);
1048*4882a593Smuzhiyun ubi_msg(ubi, "max/mean erase counter: %d/%d, WL threshold: %d, image sequence number: %u",
1049*4882a593Smuzhiyun ubi->max_ec, ubi->mean_ec, CONFIG_MTD_UBI_WL_THRESHOLD,
1050*4882a593Smuzhiyun ubi->image_seq);
1051*4882a593Smuzhiyun ubi_msg(ubi, "available PEBs: %d, total reserved PEBs: %d, PEBs reserved for bad PEB handling: %d",
1052*4882a593Smuzhiyun ubi->avail_pebs, ubi->rsvd_pebs, ubi->beb_rsvd_pebs);
1053*4882a593Smuzhiyun
1054*4882a593Smuzhiyun /*
1055*4882a593Smuzhiyun * The below lock makes sure we do not race with 'ubi_thread()' which
1056*4882a593Smuzhiyun * checks @ubi->thread_enabled. Otherwise we may fail to wake it up.
1057*4882a593Smuzhiyun */
1058*4882a593Smuzhiyun spin_lock(&ubi->wl_lock);
1059*4882a593Smuzhiyun ubi->thread_enabled = 1;
1060*4882a593Smuzhiyun #ifndef __UBOOT__
1061*4882a593Smuzhiyun wake_up_process(ubi->bgt_thread);
1062*4882a593Smuzhiyun #else
1063*4882a593Smuzhiyun ubi_do_worker(ubi);
1064*4882a593Smuzhiyun #endif
1065*4882a593Smuzhiyun
1066*4882a593Smuzhiyun spin_unlock(&ubi->wl_lock);
1067*4882a593Smuzhiyun
1068*4882a593Smuzhiyun ubi_devices[ubi_num] = ubi;
1069*4882a593Smuzhiyun ubi_notify_all(ubi, UBI_VOLUME_ADDED, NULL);
1070*4882a593Smuzhiyun return ubi_num;
1071*4882a593Smuzhiyun
1072*4882a593Smuzhiyun out_debugfs:
1073*4882a593Smuzhiyun ubi_debugfs_exit_dev(ubi);
1074*4882a593Smuzhiyun out_uif:
1075*4882a593Smuzhiyun get_device(&ubi->dev);
1076*4882a593Smuzhiyun ubi_assert(ref);
1077*4882a593Smuzhiyun uif_close(ubi);
1078*4882a593Smuzhiyun out_detach:
1079*4882a593Smuzhiyun ubi_wl_close(ubi);
1080*4882a593Smuzhiyun ubi_free_internal_volumes(ubi);
1081*4882a593Smuzhiyun vfree(ubi->vtbl);
1082*4882a593Smuzhiyun out_free:
1083*4882a593Smuzhiyun vfree(ubi->peb_buf);
1084*4882a593Smuzhiyun vfree(ubi->fm_buf);
1085*4882a593Smuzhiyun if (ref)
1086*4882a593Smuzhiyun put_device(&ubi->dev);
1087*4882a593Smuzhiyun else
1088*4882a593Smuzhiyun kfree(ubi);
1089*4882a593Smuzhiyun return err;
1090*4882a593Smuzhiyun }
1091*4882a593Smuzhiyun
1092*4882a593Smuzhiyun /**
1093*4882a593Smuzhiyun * ubi_detach_mtd_dev - detach an MTD device.
1094*4882a593Smuzhiyun * @ubi_num: UBI device number to detach from
1095*4882a593Smuzhiyun * @anyway: detach MTD even if device reference count is not zero
1096*4882a593Smuzhiyun *
1097*4882a593Smuzhiyun * This function destroys an UBI device number @ubi_num and detaches the
1098*4882a593Smuzhiyun * underlying MTD device. Returns zero in case of success and %-EBUSY if the
1099*4882a593Smuzhiyun * UBI device is busy and cannot be destroyed, and %-EINVAL if it does not
1100*4882a593Smuzhiyun * exist.
1101*4882a593Smuzhiyun *
1102*4882a593Smuzhiyun * Note, the invocations of this function has to be serialized by the
1103*4882a593Smuzhiyun * @ubi_devices_mutex.
1104*4882a593Smuzhiyun */
ubi_detach_mtd_dev(int ubi_num,int anyway)1105*4882a593Smuzhiyun int ubi_detach_mtd_dev(int ubi_num, int anyway)
1106*4882a593Smuzhiyun {
1107*4882a593Smuzhiyun struct ubi_device *ubi;
1108*4882a593Smuzhiyun
1109*4882a593Smuzhiyun if (ubi_num < 0 || ubi_num >= UBI_MAX_DEVICES)
1110*4882a593Smuzhiyun return -EINVAL;
1111*4882a593Smuzhiyun
1112*4882a593Smuzhiyun ubi = ubi_get_device(ubi_num);
1113*4882a593Smuzhiyun if (!ubi)
1114*4882a593Smuzhiyun return -EINVAL;
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun spin_lock(&ubi_devices_lock);
1117*4882a593Smuzhiyun put_device(&ubi->dev);
1118*4882a593Smuzhiyun ubi->ref_count -= 1;
1119*4882a593Smuzhiyun if (ubi->ref_count) {
1120*4882a593Smuzhiyun if (!anyway) {
1121*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
1122*4882a593Smuzhiyun return -EBUSY;
1123*4882a593Smuzhiyun }
1124*4882a593Smuzhiyun /* This may only happen if there is a bug */
1125*4882a593Smuzhiyun ubi_err(ubi, "%s reference count %d, destroy anyway",
1126*4882a593Smuzhiyun ubi->ubi_name, ubi->ref_count);
1127*4882a593Smuzhiyun }
1128*4882a593Smuzhiyun ubi_devices[ubi_num] = NULL;
1129*4882a593Smuzhiyun spin_unlock(&ubi_devices_lock);
1130*4882a593Smuzhiyun
1131*4882a593Smuzhiyun ubi_assert(ubi_num == ubi->ubi_num);
1132*4882a593Smuzhiyun ubi_notify_all(ubi, UBI_VOLUME_REMOVED, NULL);
1133*4882a593Smuzhiyun ubi_msg(ubi, "detaching mtd%d", ubi->mtd->index);
1134*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
1135*4882a593Smuzhiyun /* If we don't write a new fastmap at detach time we lose all
1136*4882a593Smuzhiyun * EC updates that have been made since the last written fastmap.
1137*4882a593Smuzhiyun * In case of fastmap debugging we omit the update to simulate an
1138*4882a593Smuzhiyun * unclean shutdown. */
1139*4882a593Smuzhiyun if (!ubi_dbg_chk_fastmap(ubi))
1140*4882a593Smuzhiyun ubi_update_fastmap(ubi);
1141*4882a593Smuzhiyun #endif
1142*4882a593Smuzhiyun /*
1143*4882a593Smuzhiyun * Before freeing anything, we have to stop the background thread to
1144*4882a593Smuzhiyun * prevent it from doing anything on this device while we are freeing.
1145*4882a593Smuzhiyun */
1146*4882a593Smuzhiyun if (ubi->bgt_thread)
1147*4882a593Smuzhiyun kthread_stop(ubi->bgt_thread);
1148*4882a593Smuzhiyun
1149*4882a593Smuzhiyun /*
1150*4882a593Smuzhiyun * Get a reference to the device in order to prevent 'dev_release()'
1151*4882a593Smuzhiyun * from freeing the @ubi object.
1152*4882a593Smuzhiyun */
1153*4882a593Smuzhiyun get_device(&ubi->dev);
1154*4882a593Smuzhiyun
1155*4882a593Smuzhiyun ubi_debugfs_exit_dev(ubi);
1156*4882a593Smuzhiyun uif_close(ubi);
1157*4882a593Smuzhiyun
1158*4882a593Smuzhiyun ubi_wl_close(ubi);
1159*4882a593Smuzhiyun ubi_free_internal_volumes(ubi);
1160*4882a593Smuzhiyun vfree(ubi->vtbl);
1161*4882a593Smuzhiyun put_mtd_device(ubi->mtd);
1162*4882a593Smuzhiyun vfree(ubi->peb_buf);
1163*4882a593Smuzhiyun vfree(ubi->fm_buf);
1164*4882a593Smuzhiyun ubi_msg(ubi, "mtd%d is detached", ubi->mtd->index);
1165*4882a593Smuzhiyun put_device(&ubi->dev);
1166*4882a593Smuzhiyun return 0;
1167*4882a593Smuzhiyun }
1168*4882a593Smuzhiyun
1169*4882a593Smuzhiyun #ifndef __UBOOT__
1170*4882a593Smuzhiyun /**
1171*4882a593Smuzhiyun * open_mtd_by_chdev - open an MTD device by its character device node path.
1172*4882a593Smuzhiyun * @mtd_dev: MTD character device node path
1173*4882a593Smuzhiyun *
1174*4882a593Smuzhiyun * This helper function opens an MTD device by its character node device path.
1175*4882a593Smuzhiyun * Returns MTD device description object in case of success and a negative
1176*4882a593Smuzhiyun * error code in case of failure.
1177*4882a593Smuzhiyun */
open_mtd_by_chdev(const char * mtd_dev)1178*4882a593Smuzhiyun static struct mtd_info * __init open_mtd_by_chdev(const char *mtd_dev)
1179*4882a593Smuzhiyun {
1180*4882a593Smuzhiyun int err, major, minor, mode;
1181*4882a593Smuzhiyun struct path path;
1182*4882a593Smuzhiyun
1183*4882a593Smuzhiyun /* Probably this is an MTD character device node path */
1184*4882a593Smuzhiyun err = kern_path(mtd_dev, LOOKUP_FOLLOW, &path);
1185*4882a593Smuzhiyun if (err)
1186*4882a593Smuzhiyun return ERR_PTR(err);
1187*4882a593Smuzhiyun
1188*4882a593Smuzhiyun /* MTD device number is defined by the major / minor numbers */
1189*4882a593Smuzhiyun major = imajor(d_backing_inode(path.dentry));
1190*4882a593Smuzhiyun minor = iminor(d_backing_inode(path.dentry));
1191*4882a593Smuzhiyun mode = d_backing_inode(path.dentry)->i_mode;
1192*4882a593Smuzhiyun path_put(&path);
1193*4882a593Smuzhiyun if (major != MTD_CHAR_MAJOR || !S_ISCHR(mode))
1194*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
1195*4882a593Smuzhiyun
1196*4882a593Smuzhiyun if (minor & 1)
1197*4882a593Smuzhiyun /*
1198*4882a593Smuzhiyun * Just do not think the "/dev/mtdrX" devices support is need,
1199*4882a593Smuzhiyun * so do not support them to avoid doing extra work.
1200*4882a593Smuzhiyun */
1201*4882a593Smuzhiyun return ERR_PTR(-EINVAL);
1202*4882a593Smuzhiyun
1203*4882a593Smuzhiyun return get_mtd_device(NULL, minor / 2);
1204*4882a593Smuzhiyun }
1205*4882a593Smuzhiyun #endif
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun /**
1208*4882a593Smuzhiyun * open_mtd_device - open MTD device by name, character device path, or number.
1209*4882a593Smuzhiyun * @mtd_dev: name, character device node path, or MTD device device number
1210*4882a593Smuzhiyun *
1211*4882a593Smuzhiyun * This function tries to open and MTD device described by @mtd_dev string,
1212*4882a593Smuzhiyun * which is first treated as ASCII MTD device number, and if it is not true, it
1213*4882a593Smuzhiyun * is treated as MTD device name, and if that is also not true, it is treated
1214*4882a593Smuzhiyun * as MTD character device node path. Returns MTD device description object in
1215*4882a593Smuzhiyun * case of success and a negative error code in case of failure.
1216*4882a593Smuzhiyun */
open_mtd_device(const char * mtd_dev)1217*4882a593Smuzhiyun static struct mtd_info * __init open_mtd_device(const char *mtd_dev)
1218*4882a593Smuzhiyun {
1219*4882a593Smuzhiyun struct mtd_info *mtd;
1220*4882a593Smuzhiyun int mtd_num;
1221*4882a593Smuzhiyun char *endp;
1222*4882a593Smuzhiyun
1223*4882a593Smuzhiyun mtd_num = simple_strtoul(mtd_dev, &endp, 0);
1224*4882a593Smuzhiyun if (*endp != '\0' || mtd_dev == endp) {
1225*4882a593Smuzhiyun /*
1226*4882a593Smuzhiyun * This does not look like an ASCII integer, probably this is
1227*4882a593Smuzhiyun * MTD device name.
1228*4882a593Smuzhiyun */
1229*4882a593Smuzhiyun mtd = get_mtd_device_nm(mtd_dev);
1230*4882a593Smuzhiyun #ifndef __UBOOT__
1231*4882a593Smuzhiyun if (IS_ERR(mtd) && PTR_ERR(mtd) == -ENODEV)
1232*4882a593Smuzhiyun /* Probably this is an MTD character device node path */
1233*4882a593Smuzhiyun mtd = open_mtd_by_chdev(mtd_dev);
1234*4882a593Smuzhiyun #endif
1235*4882a593Smuzhiyun } else
1236*4882a593Smuzhiyun mtd = get_mtd_device(NULL, mtd_num);
1237*4882a593Smuzhiyun
1238*4882a593Smuzhiyun return mtd;
1239*4882a593Smuzhiyun }
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun #ifndef __UBOOT__
ubi_init(void)1242*4882a593Smuzhiyun static int __init ubi_init(void)
1243*4882a593Smuzhiyun #else
1244*4882a593Smuzhiyun int ubi_init(void)
1245*4882a593Smuzhiyun #endif
1246*4882a593Smuzhiyun {
1247*4882a593Smuzhiyun int err, i, k;
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun /* Ensure that EC and VID headers have correct size */
1250*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
1251*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun if (mtd_devs > UBI_MAX_DEVICES) {
1254*4882a593Smuzhiyun pr_err("UBI error: too many MTD devices, maximum is %d\n",
1255*4882a593Smuzhiyun UBI_MAX_DEVICES);
1256*4882a593Smuzhiyun return -EINVAL;
1257*4882a593Smuzhiyun }
1258*4882a593Smuzhiyun
1259*4882a593Smuzhiyun /* Create base sysfs directory and sysfs files */
1260*4882a593Smuzhiyun err = class_register(&ubi_class);
1261*4882a593Smuzhiyun if (err < 0)
1262*4882a593Smuzhiyun return err;
1263*4882a593Smuzhiyun
1264*4882a593Smuzhiyun err = misc_register(&ubi_ctrl_cdev);
1265*4882a593Smuzhiyun if (err) {
1266*4882a593Smuzhiyun pr_err("UBI error: cannot register device\n");
1267*4882a593Smuzhiyun goto out;
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
1271*4882a593Smuzhiyun sizeof(struct ubi_wl_entry),
1272*4882a593Smuzhiyun 0, 0, NULL);
1273*4882a593Smuzhiyun if (!ubi_wl_entry_slab) {
1274*4882a593Smuzhiyun err = -ENOMEM;
1275*4882a593Smuzhiyun goto out_dev_unreg;
1276*4882a593Smuzhiyun }
1277*4882a593Smuzhiyun
1278*4882a593Smuzhiyun err = ubi_debugfs_init();
1279*4882a593Smuzhiyun if (err)
1280*4882a593Smuzhiyun goto out_slab;
1281*4882a593Smuzhiyun
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun /* Attach MTD devices */
1284*4882a593Smuzhiyun for (i = 0; i < mtd_devs; i++) {
1285*4882a593Smuzhiyun struct mtd_dev_param *p = &mtd_dev_param[i];
1286*4882a593Smuzhiyun struct mtd_info *mtd;
1287*4882a593Smuzhiyun
1288*4882a593Smuzhiyun cond_resched();
1289*4882a593Smuzhiyun
1290*4882a593Smuzhiyun mtd = open_mtd_device(p->name);
1291*4882a593Smuzhiyun if (IS_ERR(mtd)) {
1292*4882a593Smuzhiyun err = PTR_ERR(mtd);
1293*4882a593Smuzhiyun pr_err("UBI error: cannot open mtd %s, error %d\n",
1294*4882a593Smuzhiyun p->name, err);
1295*4882a593Smuzhiyun /* See comment below re-ubi_is_module(). */
1296*4882a593Smuzhiyun if (ubi_is_module())
1297*4882a593Smuzhiyun goto out_detach;
1298*4882a593Smuzhiyun continue;
1299*4882a593Smuzhiyun }
1300*4882a593Smuzhiyun
1301*4882a593Smuzhiyun mutex_lock(&ubi_devices_mutex);
1302*4882a593Smuzhiyun err = ubi_attach_mtd_dev(mtd, p->ubi_num,
1303*4882a593Smuzhiyun p->vid_hdr_offs, p->max_beb_per1024);
1304*4882a593Smuzhiyun mutex_unlock(&ubi_devices_mutex);
1305*4882a593Smuzhiyun if (err < 0) {
1306*4882a593Smuzhiyun pr_err("UBI error: cannot attach mtd%d\n",
1307*4882a593Smuzhiyun mtd->index);
1308*4882a593Smuzhiyun put_mtd_device(mtd);
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun /*
1311*4882a593Smuzhiyun * Originally UBI stopped initializing on any error.
1312*4882a593Smuzhiyun * However, later on it was found out that this
1313*4882a593Smuzhiyun * behavior is not very good when UBI is compiled into
1314*4882a593Smuzhiyun * the kernel and the MTD devices to attach are passed
1315*4882a593Smuzhiyun * through the command line. Indeed, UBI failure
1316*4882a593Smuzhiyun * stopped whole boot sequence.
1317*4882a593Smuzhiyun *
1318*4882a593Smuzhiyun * To fix this, we changed the behavior for the
1319*4882a593Smuzhiyun * non-module case, but preserved the old behavior for
1320*4882a593Smuzhiyun * the module case, just for compatibility. This is a
1321*4882a593Smuzhiyun * little inconsistent, though.
1322*4882a593Smuzhiyun */
1323*4882a593Smuzhiyun if (ubi_is_module())
1324*4882a593Smuzhiyun goto out_detach;
1325*4882a593Smuzhiyun }
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun err = ubiblock_init();
1329*4882a593Smuzhiyun if (err) {
1330*4882a593Smuzhiyun pr_err("UBI error: block: cannot initialize, error %d\n", err);
1331*4882a593Smuzhiyun
1332*4882a593Smuzhiyun /* See comment above re-ubi_is_module(). */
1333*4882a593Smuzhiyun if (ubi_is_module())
1334*4882a593Smuzhiyun goto out_detach;
1335*4882a593Smuzhiyun }
1336*4882a593Smuzhiyun
1337*4882a593Smuzhiyun return 0;
1338*4882a593Smuzhiyun
1339*4882a593Smuzhiyun out_detach:
1340*4882a593Smuzhiyun for (k = 0; k < i; k++)
1341*4882a593Smuzhiyun if (ubi_devices[k]) {
1342*4882a593Smuzhiyun mutex_lock(&ubi_devices_mutex);
1343*4882a593Smuzhiyun ubi_detach_mtd_dev(ubi_devices[k]->ubi_num, 1);
1344*4882a593Smuzhiyun mutex_unlock(&ubi_devices_mutex);
1345*4882a593Smuzhiyun }
1346*4882a593Smuzhiyun ubi_debugfs_exit();
1347*4882a593Smuzhiyun out_slab:
1348*4882a593Smuzhiyun kmem_cache_destroy(ubi_wl_entry_slab);
1349*4882a593Smuzhiyun out_dev_unreg:
1350*4882a593Smuzhiyun misc_deregister(&ubi_ctrl_cdev);
1351*4882a593Smuzhiyun out:
1352*4882a593Smuzhiyun #ifdef __UBOOT__
1353*4882a593Smuzhiyun /* Reset any globals that the driver depends on being zeroed */
1354*4882a593Smuzhiyun mtd_devs = 0;
1355*4882a593Smuzhiyun #endif
1356*4882a593Smuzhiyun class_unregister(&ubi_class);
1357*4882a593Smuzhiyun pr_err("UBI error: cannot initialize UBI, error %d\n", err);
1358*4882a593Smuzhiyun return err;
1359*4882a593Smuzhiyun }
1360*4882a593Smuzhiyun late_initcall(ubi_init);
1361*4882a593Smuzhiyun
1362*4882a593Smuzhiyun #ifndef __UBOOT__
ubi_exit(void)1363*4882a593Smuzhiyun static void __exit ubi_exit(void)
1364*4882a593Smuzhiyun #else
1365*4882a593Smuzhiyun void ubi_exit(void)
1366*4882a593Smuzhiyun #endif
1367*4882a593Smuzhiyun {
1368*4882a593Smuzhiyun int i;
1369*4882a593Smuzhiyun
1370*4882a593Smuzhiyun ubiblock_exit();
1371*4882a593Smuzhiyun
1372*4882a593Smuzhiyun for (i = 0; i < UBI_MAX_DEVICES; i++)
1373*4882a593Smuzhiyun if (ubi_devices[i]) {
1374*4882a593Smuzhiyun mutex_lock(&ubi_devices_mutex);
1375*4882a593Smuzhiyun ubi_detach_mtd_dev(ubi_devices[i]->ubi_num, 1);
1376*4882a593Smuzhiyun mutex_unlock(&ubi_devices_mutex);
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun ubi_debugfs_exit();
1379*4882a593Smuzhiyun kmem_cache_destroy(ubi_wl_entry_slab);
1380*4882a593Smuzhiyun misc_deregister(&ubi_ctrl_cdev);
1381*4882a593Smuzhiyun class_unregister(&ubi_class);
1382*4882a593Smuzhiyun #ifdef __UBOOT__
1383*4882a593Smuzhiyun /* Reset any globals that the driver depends on being zeroed */
1384*4882a593Smuzhiyun mtd_devs = 0;
1385*4882a593Smuzhiyun #endif
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun module_exit(ubi_exit);
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun /**
1390*4882a593Smuzhiyun * bytes_str_to_int - convert a number of bytes string into an integer.
1391*4882a593Smuzhiyun * @str: the string to convert
1392*4882a593Smuzhiyun *
1393*4882a593Smuzhiyun * This function returns positive resulting integer in case of success and a
1394*4882a593Smuzhiyun * negative error code in case of failure.
1395*4882a593Smuzhiyun */
bytes_str_to_int(const char * str)1396*4882a593Smuzhiyun static int __init bytes_str_to_int(const char *str)
1397*4882a593Smuzhiyun {
1398*4882a593Smuzhiyun char *endp;
1399*4882a593Smuzhiyun unsigned long result;
1400*4882a593Smuzhiyun
1401*4882a593Smuzhiyun result = simple_strtoul(str, &endp, 0);
1402*4882a593Smuzhiyun if (str == endp || result >= INT_MAX) {
1403*4882a593Smuzhiyun pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1404*4882a593Smuzhiyun return -EINVAL;
1405*4882a593Smuzhiyun }
1406*4882a593Smuzhiyun
1407*4882a593Smuzhiyun switch (*endp) {
1408*4882a593Smuzhiyun case 'G':
1409*4882a593Smuzhiyun result *= 1024;
1410*4882a593Smuzhiyun case 'M':
1411*4882a593Smuzhiyun result *= 1024;
1412*4882a593Smuzhiyun case 'K':
1413*4882a593Smuzhiyun result *= 1024;
1414*4882a593Smuzhiyun if (endp[1] == 'i' && endp[2] == 'B')
1415*4882a593Smuzhiyun endp += 2;
1416*4882a593Smuzhiyun case '\0':
1417*4882a593Smuzhiyun break;
1418*4882a593Smuzhiyun default:
1419*4882a593Smuzhiyun pr_err("UBI error: incorrect bytes count: \"%s\"\n", str);
1420*4882a593Smuzhiyun return -EINVAL;
1421*4882a593Smuzhiyun }
1422*4882a593Smuzhiyun
1423*4882a593Smuzhiyun return result;
1424*4882a593Smuzhiyun }
1425*4882a593Smuzhiyun
kstrtoint(const char * s,unsigned int base,int * res)1426*4882a593Smuzhiyun int kstrtoint(const char *s, unsigned int base, int *res)
1427*4882a593Smuzhiyun {
1428*4882a593Smuzhiyun unsigned long long tmp;
1429*4882a593Smuzhiyun
1430*4882a593Smuzhiyun tmp = simple_strtoull(s, NULL, base);
1431*4882a593Smuzhiyun if (tmp != (unsigned long long)(int)tmp)
1432*4882a593Smuzhiyun return -ERANGE;
1433*4882a593Smuzhiyun
1434*4882a593Smuzhiyun return (int)tmp;
1435*4882a593Smuzhiyun }
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun /**
1438*4882a593Smuzhiyun * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
1439*4882a593Smuzhiyun * @val: the parameter value to parse
1440*4882a593Smuzhiyun * @kp: not used
1441*4882a593Smuzhiyun *
1442*4882a593Smuzhiyun * This function returns zero in case of success and a negative error code in
1443*4882a593Smuzhiyun * case of error.
1444*4882a593Smuzhiyun */
1445*4882a593Smuzhiyun #ifndef __UBOOT__
ubi_mtd_param_parse(const char * val,struct kernel_param * kp)1446*4882a593Smuzhiyun static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1447*4882a593Smuzhiyun #else
1448*4882a593Smuzhiyun int ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
1449*4882a593Smuzhiyun #endif
1450*4882a593Smuzhiyun {
1451*4882a593Smuzhiyun int i, len;
1452*4882a593Smuzhiyun struct mtd_dev_param *p;
1453*4882a593Smuzhiyun char buf[MTD_PARAM_LEN_MAX];
1454*4882a593Smuzhiyun char *pbuf = &buf[0];
1455*4882a593Smuzhiyun char *tokens[MTD_PARAM_MAX_COUNT], *token;
1456*4882a593Smuzhiyun
1457*4882a593Smuzhiyun if (!val)
1458*4882a593Smuzhiyun return -EINVAL;
1459*4882a593Smuzhiyun
1460*4882a593Smuzhiyun if (mtd_devs == UBI_MAX_DEVICES) {
1461*4882a593Smuzhiyun pr_err("UBI error: too many parameters, max. is %d\n",
1462*4882a593Smuzhiyun UBI_MAX_DEVICES);
1463*4882a593Smuzhiyun return -EINVAL;
1464*4882a593Smuzhiyun }
1465*4882a593Smuzhiyun
1466*4882a593Smuzhiyun len = strnlen(val, MTD_PARAM_LEN_MAX);
1467*4882a593Smuzhiyun if (len == MTD_PARAM_LEN_MAX) {
1468*4882a593Smuzhiyun pr_err("UBI error: parameter \"%s\" is too long, max. is %d\n",
1469*4882a593Smuzhiyun val, MTD_PARAM_LEN_MAX);
1470*4882a593Smuzhiyun return -EINVAL;
1471*4882a593Smuzhiyun }
1472*4882a593Smuzhiyun
1473*4882a593Smuzhiyun if (len == 0) {
1474*4882a593Smuzhiyun pr_warn("UBI warning: empty 'mtd=' parameter - ignored\n");
1475*4882a593Smuzhiyun return 0;
1476*4882a593Smuzhiyun }
1477*4882a593Smuzhiyun
1478*4882a593Smuzhiyun strcpy(buf, val);
1479*4882a593Smuzhiyun
1480*4882a593Smuzhiyun /* Get rid of the final newline */
1481*4882a593Smuzhiyun if (buf[len - 1] == '\n')
1482*4882a593Smuzhiyun buf[len - 1] = '\0';
1483*4882a593Smuzhiyun
1484*4882a593Smuzhiyun for (i = 0; i < MTD_PARAM_MAX_COUNT; i++)
1485*4882a593Smuzhiyun tokens[i] = strsep(&pbuf, ",");
1486*4882a593Smuzhiyun
1487*4882a593Smuzhiyun if (pbuf) {
1488*4882a593Smuzhiyun pr_err("UBI error: too many arguments at \"%s\"\n", val);
1489*4882a593Smuzhiyun return -EINVAL;
1490*4882a593Smuzhiyun }
1491*4882a593Smuzhiyun
1492*4882a593Smuzhiyun p = &mtd_dev_param[mtd_devs];
1493*4882a593Smuzhiyun strcpy(&p->name[0], tokens[0]);
1494*4882a593Smuzhiyun
1495*4882a593Smuzhiyun token = tokens[1];
1496*4882a593Smuzhiyun if (token) {
1497*4882a593Smuzhiyun p->vid_hdr_offs = bytes_str_to_int(token);
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun if (p->vid_hdr_offs < 0)
1500*4882a593Smuzhiyun return p->vid_hdr_offs;
1501*4882a593Smuzhiyun }
1502*4882a593Smuzhiyun
1503*4882a593Smuzhiyun token = tokens[2];
1504*4882a593Smuzhiyun if (token) {
1505*4882a593Smuzhiyun int err = kstrtoint(token, 10, &p->max_beb_per1024);
1506*4882a593Smuzhiyun
1507*4882a593Smuzhiyun if (err) {
1508*4882a593Smuzhiyun pr_err("UBI error: bad value for max_beb_per1024 parameter: %s",
1509*4882a593Smuzhiyun token);
1510*4882a593Smuzhiyun return -EINVAL;
1511*4882a593Smuzhiyun }
1512*4882a593Smuzhiyun }
1513*4882a593Smuzhiyun
1514*4882a593Smuzhiyun token = tokens[3];
1515*4882a593Smuzhiyun if (token) {
1516*4882a593Smuzhiyun int err = kstrtoint(token, 10, &p->ubi_num);
1517*4882a593Smuzhiyun
1518*4882a593Smuzhiyun if (err) {
1519*4882a593Smuzhiyun pr_err("UBI error: bad value for ubi_num parameter: %s",
1520*4882a593Smuzhiyun token);
1521*4882a593Smuzhiyun return -EINVAL;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun } else
1524*4882a593Smuzhiyun p->ubi_num = UBI_DEV_NUM_AUTO;
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun mtd_devs += 1;
1527*4882a593Smuzhiyun return 0;
1528*4882a593Smuzhiyun }
1529*4882a593Smuzhiyun
1530*4882a593Smuzhiyun module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
1531*4882a593Smuzhiyun MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: mtd=<name|num|path>[,<vid_hdr_offs>[,max_beb_per1024[,ubi_num]]].\n"
1532*4882a593Smuzhiyun "Multiple \"mtd\" parameters may be specified.\n"
1533*4882a593Smuzhiyun "MTD devices may be specified by their number, name, or path to the MTD character device node.\n"
1534*4882a593Smuzhiyun "Optional \"vid_hdr_offs\" parameter specifies UBI VID header position to be used by UBI. (default value if 0)\n"
1535*4882a593Smuzhiyun "Optional \"max_beb_per1024\" parameter specifies the maximum expected bad eraseblock per 1024 eraseblocks. (default value ("
1536*4882a593Smuzhiyun __stringify(CONFIG_MTD_UBI_BEB_LIMIT) ") if 0)\n"
1537*4882a593Smuzhiyun "Optional \"ubi_num\" parameter specifies UBI device number which have to be assigned to the newly created UBI device (assigned automatically by default)\n"
1538*4882a593Smuzhiyun "\n"
1539*4882a593Smuzhiyun "Example 1: mtd=/dev/mtd0 - attach MTD device /dev/mtd0.\n"
1540*4882a593Smuzhiyun "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"
1541*4882a593Smuzhiyun "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"
1542*4882a593Smuzhiyun "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"
1543*4882a593Smuzhiyun "\t(e.g. if the NAND *chipset* has 4096 PEB, 100 will be reserved for this UBI device).");
1544*4882a593Smuzhiyun #ifdef CONFIG_MTD_UBI_FASTMAP
1545*4882a593Smuzhiyun module_param(fm_autoconvert, bool, 0644);
1546*4882a593Smuzhiyun MODULE_PARM_DESC(fm_autoconvert, "Set this parameter to enable fastmap automatically on images without a fastmap.");
1547*4882a593Smuzhiyun module_param(fm_debug, bool, 0);
1548*4882a593Smuzhiyun MODULE_PARM_DESC(fm_debug, "Set this parameter to enable fastmap debugging by default. Warning, this will make fastmap slow!");
1549*4882a593Smuzhiyun #endif
1550*4882a593Smuzhiyun MODULE_VERSION(__stringify(UBI_VERSION));
1551*4882a593Smuzhiyun MODULE_DESCRIPTION("UBI - Unsorted Block Images");
1552*4882a593Smuzhiyun MODULE_AUTHOR("Artem Bityutskiy");
1553*4882a593Smuzhiyun MODULE_LICENSE("GPL");
1554