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