build.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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. * This file includes UBI initialization and building of UBI devices. At the
  24. * moment UBI devices may only be added while UBI is initialized, but dynamic
  25. * device add/remove functionality is planned. Also, at the moment we only
  26. * attach UBI devices by scanning, which will become a bottleneck when flashes
  27. * reach certain large size. Then one may improve UBI and add other methods.
  28. */
  29. #include <linux/err.h>
  30. #include <linux/module.h>
  31. #include <linux/moduleparam.h>
  32. #include <linux/stringify.h>
  33. #include <linux/stat.h>
  34. #include "ubi.h"
  35. /* Maximum length of the 'mtd=' parameter */
  36. #define MTD_PARAM_LEN_MAX 64
  37. /**
  38. * struct mtd_dev_param - MTD device parameter description data structure.
  39. * @name: MTD device name or number string
  40. * @vid_hdr_offs: VID header offset
  41. * @data_offs: data offset
  42. */
  43. struct mtd_dev_param
  44. {
  45. char name[MTD_PARAM_LEN_MAX];
  46. int vid_hdr_offs;
  47. int data_offs;
  48. };
  49. /* Numbers of elements set in the @mtd_dev_param array */
  50. static int mtd_devs = 0;
  51. /* MTD devices specification parameters */
  52. static struct mtd_dev_param mtd_dev_param[UBI_MAX_DEVICES];
  53. /* Number of UBI devices in system */
  54. int ubi_devices_cnt;
  55. /* All UBI devices in system */
  56. struct ubi_device *ubi_devices[UBI_MAX_DEVICES];
  57. /* Root UBI "class" object (corresponds to '/<sysfs>/class/ubi/') */
  58. struct class *ubi_class;
  59. /* "Show" method for files in '/<sysfs>/class/ubi/' */
  60. static ssize_t ubi_version_show(struct class *class, char *buf)
  61. {
  62. return sprintf(buf, "%d\n", UBI_VERSION);
  63. }
  64. /* UBI version attribute ('/<sysfs>/class/ubi/version') */
  65. static struct class_attribute ubi_version =
  66. __ATTR(version, S_IRUGO, ubi_version_show, NULL);
  67. static ssize_t dev_attribute_show(struct device *dev,
  68. struct device_attribute *attr, char *buf);
  69. /* UBI device attributes (correspond to files in '/<sysfs>/class/ubi/ubiX') */
  70. static struct device_attribute dev_eraseblock_size =
  71. __ATTR(eraseblock_size, S_IRUGO, dev_attribute_show, NULL);
  72. static struct device_attribute dev_avail_eraseblocks =
  73. __ATTR(avail_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  74. static struct device_attribute dev_total_eraseblocks =
  75. __ATTR(total_eraseblocks, S_IRUGO, dev_attribute_show, NULL);
  76. static struct device_attribute dev_volumes_count =
  77. __ATTR(volumes_count, S_IRUGO, dev_attribute_show, NULL);
  78. static struct device_attribute dev_max_ec =
  79. __ATTR(max_ec, S_IRUGO, dev_attribute_show, NULL);
  80. static struct device_attribute dev_reserved_for_bad =
  81. __ATTR(reserved_for_bad, S_IRUGO, dev_attribute_show, NULL);
  82. static struct device_attribute dev_bad_peb_count =
  83. __ATTR(bad_peb_count, S_IRUGO, dev_attribute_show, NULL);
  84. static struct device_attribute dev_max_vol_count =
  85. __ATTR(max_vol_count, S_IRUGO, dev_attribute_show, NULL);
  86. static struct device_attribute dev_min_io_size =
  87. __ATTR(min_io_size, S_IRUGO, dev_attribute_show, NULL);
  88. static struct device_attribute dev_bgt_enabled =
  89. __ATTR(bgt_enabled, S_IRUGO, dev_attribute_show, NULL);
  90. /* "Show" method for files in '/<sysfs>/class/ubi/ubiX/' */
  91. static ssize_t dev_attribute_show(struct device *dev,
  92. struct device_attribute *attr, char *buf)
  93. {
  94. const struct ubi_device *ubi;
  95. ubi = container_of(dev, struct ubi_device, dev);
  96. if (attr == &dev_eraseblock_size)
  97. return sprintf(buf, "%d\n", ubi->leb_size);
  98. else if (attr == &dev_avail_eraseblocks)
  99. return sprintf(buf, "%d\n", ubi->avail_pebs);
  100. else if (attr == &dev_total_eraseblocks)
  101. return sprintf(buf, "%d\n", ubi->good_peb_count);
  102. else if (attr == &dev_volumes_count)
  103. return sprintf(buf, "%d\n", ubi->vol_count);
  104. else if (attr == &dev_max_ec)
  105. return sprintf(buf, "%d\n", ubi->max_ec);
  106. else if (attr == &dev_reserved_for_bad)
  107. return sprintf(buf, "%d\n", ubi->beb_rsvd_pebs);
  108. else if (attr == &dev_bad_peb_count)
  109. return sprintf(buf, "%d\n", ubi->bad_peb_count);
  110. else if (attr == &dev_max_vol_count)
  111. return sprintf(buf, "%d\n", ubi->vtbl_slots);
  112. else if (attr == &dev_min_io_size)
  113. return sprintf(buf, "%d\n", ubi->min_io_size);
  114. else if (attr == &dev_bgt_enabled)
  115. return sprintf(buf, "%d\n", ubi->thread_enabled);
  116. else
  117. BUG();
  118. return 0;
  119. }
  120. /* Fake "release" method for UBI devices */
  121. static void dev_release(struct device *dev) { }
  122. /**
  123. * ubi_sysfs_init - initialize sysfs for an UBI device.
  124. * @ubi: UBI device description object
  125. *
  126. * This function returns zero in case of success and a negative error code in
  127. * case of failure.
  128. */
  129. static int ubi_sysfs_init(struct ubi_device *ubi)
  130. {
  131. int err;
  132. ubi->dev.release = dev_release;
  133. ubi->dev.devt = MKDEV(ubi->major, 0);
  134. ubi->dev.class = ubi_class;
  135. sprintf(&ubi->dev.bus_id[0], UBI_NAME_STR"%d", ubi->ubi_num);
  136. err = device_register(&ubi->dev);
  137. if (err)
  138. goto out;
  139. err = device_create_file(&ubi->dev, &dev_eraseblock_size);
  140. if (err)
  141. goto out_unregister;
  142. err = device_create_file(&ubi->dev, &dev_avail_eraseblocks);
  143. if (err)
  144. goto out_eraseblock_size;
  145. err = device_create_file(&ubi->dev, &dev_total_eraseblocks);
  146. if (err)
  147. goto out_avail_eraseblocks;
  148. err = device_create_file(&ubi->dev, &dev_volumes_count);
  149. if (err)
  150. goto out_total_eraseblocks;
  151. err = device_create_file(&ubi->dev, &dev_max_ec);
  152. if (err)
  153. goto out_volumes_count;
  154. err = device_create_file(&ubi->dev, &dev_reserved_for_bad);
  155. if (err)
  156. goto out_volumes_max_ec;
  157. err = device_create_file(&ubi->dev, &dev_bad_peb_count);
  158. if (err)
  159. goto out_reserved_for_bad;
  160. err = device_create_file(&ubi->dev, &dev_max_vol_count);
  161. if (err)
  162. goto out_bad_peb_count;
  163. err = device_create_file(&ubi->dev, &dev_min_io_size);
  164. if (err)
  165. goto out_max_vol_count;
  166. err = device_create_file(&ubi->dev, &dev_bgt_enabled);
  167. if (err)
  168. goto out_min_io_size;
  169. return 0;
  170. out_min_io_size:
  171. device_remove_file(&ubi->dev, &dev_min_io_size);
  172. out_max_vol_count:
  173. device_remove_file(&ubi->dev, &dev_max_vol_count);
  174. out_bad_peb_count:
  175. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  176. out_reserved_for_bad:
  177. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  178. out_volumes_max_ec:
  179. device_remove_file(&ubi->dev, &dev_max_ec);
  180. out_volumes_count:
  181. device_remove_file(&ubi->dev, &dev_volumes_count);
  182. out_total_eraseblocks:
  183. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  184. out_avail_eraseblocks:
  185. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  186. out_eraseblock_size:
  187. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  188. out_unregister:
  189. device_unregister(&ubi->dev);
  190. out:
  191. ubi_err("failed to initialize sysfs for %s", ubi->ubi_name);
  192. return err;
  193. }
  194. /**
  195. * ubi_sysfs_close - close sysfs for an UBI device.
  196. * @ubi: UBI device description object
  197. */
  198. static void ubi_sysfs_close(struct ubi_device *ubi)
  199. {
  200. device_remove_file(&ubi->dev, &dev_bgt_enabled);
  201. device_remove_file(&ubi->dev, &dev_min_io_size);
  202. device_remove_file(&ubi->dev, &dev_max_vol_count);
  203. device_remove_file(&ubi->dev, &dev_bad_peb_count);
  204. device_remove_file(&ubi->dev, &dev_reserved_for_bad);
  205. device_remove_file(&ubi->dev, &dev_max_ec);
  206. device_remove_file(&ubi->dev, &dev_volumes_count);
  207. device_remove_file(&ubi->dev, &dev_total_eraseblocks);
  208. device_remove_file(&ubi->dev, &dev_avail_eraseblocks);
  209. device_remove_file(&ubi->dev, &dev_eraseblock_size);
  210. device_unregister(&ubi->dev);
  211. }
  212. /**
  213. * kill_volumes - destroy all volumes.
  214. * @ubi: UBI device description object
  215. */
  216. static void kill_volumes(struct ubi_device *ubi)
  217. {
  218. int i;
  219. for (i = 0; i < ubi->vtbl_slots; i++)
  220. if (ubi->volumes[i])
  221. ubi_free_volume(ubi, i);
  222. }
  223. /**
  224. * uif_init - initialize user interfaces for an UBI device.
  225. * @ubi: UBI device description object
  226. *
  227. * This function returns zero in case of success and a negative error code in
  228. * case of failure.
  229. */
  230. static int uif_init(struct ubi_device *ubi)
  231. {
  232. int i, err;
  233. dev_t dev;
  234. mutex_init(&ubi->vtbl_mutex);
  235. spin_lock_init(&ubi->volumes_lock);
  236. sprintf(ubi->ubi_name, UBI_NAME_STR "%d", ubi->ubi_num);
  237. /*
  238. * Major numbers for the UBI character devices are allocated
  239. * dynamically. Major numbers of volume character devices are
  240. * equivalent to ones of the corresponding UBI character device. Minor
  241. * numbers of UBI character devices are 0, while minor numbers of
  242. * volume character devices start from 1. Thus, we allocate one major
  243. * number and ubi->vtbl_slots + 1 minor numbers.
  244. */
  245. err = alloc_chrdev_region(&dev, 0, ubi->vtbl_slots + 1, ubi->ubi_name);
  246. if (err) {
  247. ubi_err("cannot register UBI character devices");
  248. return err;
  249. }
  250. cdev_init(&ubi->cdev, &ubi_cdev_operations);
  251. ubi->major = MAJOR(dev);
  252. dbg_msg("%s major is %u", ubi->ubi_name, ubi->major);
  253. ubi->cdev.owner = THIS_MODULE;
  254. dev = MKDEV(ubi->major, 0);
  255. err = cdev_add(&ubi->cdev, dev, 1);
  256. if (err) {
  257. ubi_err("cannot add character device %s", ubi->ubi_name);
  258. goto out_unreg;
  259. }
  260. err = ubi_sysfs_init(ubi);
  261. if (err)
  262. goto out_cdev;
  263. for (i = 0; i < ubi->vtbl_slots; i++)
  264. if (ubi->volumes[i]) {
  265. err = ubi_add_volume(ubi, i);
  266. if (err)
  267. goto out_volumes;
  268. }
  269. return 0;
  270. out_volumes:
  271. kill_volumes(ubi);
  272. ubi_sysfs_close(ubi);
  273. out_cdev:
  274. cdev_del(&ubi->cdev);
  275. out_unreg:
  276. unregister_chrdev_region(MKDEV(ubi->major, 0),
  277. ubi->vtbl_slots + 1);
  278. return err;
  279. }
  280. /**
  281. * uif_close - close user interfaces for an UBI device.
  282. * @ubi: UBI device description object
  283. */
  284. static void uif_close(struct ubi_device *ubi)
  285. {
  286. kill_volumes(ubi);
  287. ubi_sysfs_close(ubi);
  288. cdev_del(&ubi->cdev);
  289. unregister_chrdev_region(MKDEV(ubi->major, 0), ubi->vtbl_slots + 1);
  290. }
  291. /**
  292. * attach_by_scanning - attach an MTD device using scanning method.
  293. * @ubi: UBI device descriptor
  294. *
  295. * This function returns zero in case of success and a negative error code in
  296. * case of failure.
  297. *
  298. * Note, currently this is the only method to attach UBI devices. Hopefully in
  299. * the future we'll have more scalable attaching methods and avoid full media
  300. * scanning. But even in this case scanning will be needed as a fall-back
  301. * attaching method if there are some on-flash table corruptions.
  302. */
  303. static int attach_by_scanning(struct ubi_device *ubi)
  304. {
  305. int err;
  306. struct ubi_scan_info *si;
  307. si = ubi_scan(ubi);
  308. if (IS_ERR(si))
  309. return PTR_ERR(si);
  310. ubi->bad_peb_count = si->bad_peb_count;
  311. ubi->good_peb_count = ubi->peb_count - ubi->bad_peb_count;
  312. ubi->max_ec = si->max_ec;
  313. ubi->mean_ec = si->mean_ec;
  314. err = ubi_read_volume_table(ubi, si);
  315. if (err)
  316. goto out_si;
  317. err = ubi_wl_init_scan(ubi, si);
  318. if (err)
  319. goto out_vtbl;
  320. err = ubi_eba_init_scan(ubi, si);
  321. if (err)
  322. goto out_wl;
  323. ubi_scan_destroy_si(si);
  324. return 0;
  325. out_wl:
  326. ubi_wl_close(ubi);
  327. out_vtbl:
  328. kfree(ubi->vtbl);
  329. out_si:
  330. ubi_scan_destroy_si(si);
  331. return err;
  332. }
  333. /**
  334. * io_init - initialize I/O unit for a given UBI device.
  335. * @ubi: UBI device description object
  336. *
  337. * If @ubi->vid_hdr_offset or @ubi->leb_start is zero, default offsets are
  338. * assumed:
  339. * o EC header is always at offset zero - this cannot be changed;
  340. * o VID header starts just after the EC header at the closest address
  341. * aligned to @io->@hdrs_min_io_size;
  342. * o data starts just after the VID header at the closest address aligned to
  343. * @io->@min_io_size
  344. *
  345. * This function returns zero in case of success and a negative error code in
  346. * case of failure.
  347. */
  348. static int io_init(struct ubi_device *ubi)
  349. {
  350. if (ubi->mtd->numeraseregions != 0) {
  351. /*
  352. * Some flashes have several erase regions. Different regions
  353. * may have different eraseblock size and other
  354. * characteristics. It looks like mostly multi-region flashes
  355. * have one "main" region and one or more small regions to
  356. * store boot loader code or boot parameters or whatever. I
  357. * guess we should just pick the largest region. But this is
  358. * not implemented.
  359. */
  360. ubi_err("multiple regions, not implemented");
  361. return -EINVAL;
  362. }
  363. /*
  364. * Note, in this implementation we support MTD devices with 0x7FFFFFFF
  365. * physical eraseblocks maximum.
  366. */
  367. ubi->peb_size = ubi->mtd->erasesize;
  368. ubi->peb_count = ubi->mtd->size / ubi->mtd->erasesize;
  369. ubi->flash_size = ubi->mtd->size;
  370. if (ubi->mtd->block_isbad && ubi->mtd->block_markbad)
  371. ubi->bad_allowed = 1;
  372. ubi->min_io_size = ubi->mtd->writesize;
  373. ubi->hdrs_min_io_size = ubi->mtd->writesize >> ubi->mtd->subpage_sft;
  374. /* Make sure minimal I/O unit is power of 2 */
  375. if (ubi->min_io_size == 0 ||
  376. (ubi->min_io_size & (ubi->min_io_size - 1))) {
  377. ubi_err("bad min. I/O unit");
  378. return -EINVAL;
  379. }
  380. ubi_assert(ubi->hdrs_min_io_size > 0);
  381. ubi_assert(ubi->hdrs_min_io_size <= ubi->min_io_size);
  382. ubi_assert(ubi->min_io_size % ubi->hdrs_min_io_size == 0);
  383. /* Calculate default aligned sizes of EC and VID headers */
  384. ubi->ec_hdr_alsize = ALIGN(UBI_EC_HDR_SIZE, ubi->hdrs_min_io_size);
  385. ubi->vid_hdr_alsize = ALIGN(UBI_VID_HDR_SIZE, ubi->hdrs_min_io_size);
  386. dbg_msg("min_io_size %d", ubi->min_io_size);
  387. dbg_msg("hdrs_min_io_size %d", ubi->hdrs_min_io_size);
  388. dbg_msg("ec_hdr_alsize %d", ubi->ec_hdr_alsize);
  389. dbg_msg("vid_hdr_alsize %d", ubi->vid_hdr_alsize);
  390. if (ubi->vid_hdr_offset == 0)
  391. /* Default offset */
  392. ubi->vid_hdr_offset = ubi->vid_hdr_aloffset =
  393. ubi->ec_hdr_alsize;
  394. else {
  395. ubi->vid_hdr_aloffset = ubi->vid_hdr_offset &
  396. ~(ubi->hdrs_min_io_size - 1);
  397. ubi->vid_hdr_shift = ubi->vid_hdr_offset -
  398. ubi->vid_hdr_aloffset;
  399. }
  400. /* Similar for the data offset */
  401. if (ubi->leb_start == 0) {
  402. ubi->leb_start = ubi->vid_hdr_offset + ubi->vid_hdr_alsize;
  403. ubi->leb_start = ALIGN(ubi->leb_start, ubi->min_io_size);
  404. }
  405. dbg_msg("vid_hdr_offset %d", ubi->vid_hdr_offset);
  406. dbg_msg("vid_hdr_aloffset %d", ubi->vid_hdr_aloffset);
  407. dbg_msg("vid_hdr_shift %d", ubi->vid_hdr_shift);
  408. dbg_msg("leb_start %d", ubi->leb_start);
  409. /* The shift must be aligned to 32-bit boundary */
  410. if (ubi->vid_hdr_shift % 4) {
  411. ubi_err("unaligned VID header shift %d",
  412. ubi->vid_hdr_shift);
  413. return -EINVAL;
  414. }
  415. /* Check sanity */
  416. if (ubi->vid_hdr_offset < UBI_EC_HDR_SIZE ||
  417. ubi->leb_start < ubi->vid_hdr_offset + UBI_VID_HDR_SIZE ||
  418. ubi->leb_start > ubi->peb_size - UBI_VID_HDR_SIZE ||
  419. ubi->leb_start % ubi->min_io_size) {
  420. ubi_err("bad VID header (%d) or data offsets (%d)",
  421. ubi->vid_hdr_offset, ubi->leb_start);
  422. return -EINVAL;
  423. }
  424. /*
  425. * It may happen that EC and VID headers are situated in one minimal
  426. * I/O unit. In this case we can only accept this UBI image in
  427. * read-only mode.
  428. */
  429. if (ubi->vid_hdr_offset + UBI_VID_HDR_SIZE <= ubi->hdrs_min_io_size) {
  430. ubi_warn("EC and VID headers are in the same minimal I/O unit, "
  431. "switch to read-only mode");
  432. ubi->ro_mode = 1;
  433. }
  434. ubi->leb_size = ubi->peb_size - ubi->leb_start;
  435. if (!(ubi->mtd->flags & MTD_WRITEABLE)) {
  436. ubi_msg("MTD device %d is write-protected, attach in "
  437. "read-only mode", ubi->mtd->index);
  438. ubi->ro_mode = 1;
  439. }
  440. dbg_msg("leb_size %d", ubi->leb_size);
  441. dbg_msg("ro_mode %d", ubi->ro_mode);
  442. /*
  443. * Note, ideally, we have to initialize ubi->bad_peb_count here. But
  444. * unfortunately, MTD does not provide this information. We should loop
  445. * over all physical eraseblocks and invoke mtd->block_is_bad() for
  446. * each physical eraseblock. So, we skip ubi->bad_peb_count
  447. * uninitialized and initialize it after scanning.
  448. */
  449. return 0;
  450. }
  451. /**
  452. * attach_mtd_dev - attach an MTD device.
  453. * @mtd_dev: MTD device name or number string
  454. * @vid_hdr_offset: VID header offset
  455. * @data_offset: data offset
  456. *
  457. * This function attaches an MTD device to UBI. It first treats @mtd_dev as the
  458. * MTD device name, and tries to open it by this name. If it is unable to open,
  459. * it tries to convert @mtd_dev to an integer and open the MTD device by its
  460. * number. Returns zero in case of success and a negative error code in case of
  461. * failure.
  462. */
  463. static int attach_mtd_dev(const char *mtd_dev, int vid_hdr_offset,
  464. int data_offset)
  465. {
  466. struct ubi_device *ubi;
  467. struct mtd_info *mtd;
  468. int i, err;
  469. mtd = get_mtd_device_nm(mtd_dev);
  470. if (IS_ERR(mtd)) {
  471. int mtd_num;
  472. char *endp;
  473. if (PTR_ERR(mtd) != -ENODEV)
  474. return PTR_ERR(mtd);
  475. /*
  476. * Probably this is not MTD device name but MTD device number -
  477. * check this out.
  478. */
  479. mtd_num = simple_strtoul(mtd_dev, &endp, 0);
  480. if (*endp != '\0' || mtd_dev == endp) {
  481. ubi_err("incorrect MTD device: \"%s\"", mtd_dev);
  482. return -ENODEV;
  483. }
  484. mtd = get_mtd_device(NULL, mtd_num);
  485. if (IS_ERR(mtd))
  486. return PTR_ERR(mtd);
  487. }
  488. /* Check if we already have the same MTD device attached */
  489. for (i = 0; i < ubi_devices_cnt; i++)
  490. if (ubi_devices[i]->mtd->index == mtd->index) {
  491. ubi_err("mtd%d is already attached to ubi%d",
  492. mtd->index, i);
  493. err = -EINVAL;
  494. goto out_mtd;
  495. }
  496. ubi = ubi_devices[ubi_devices_cnt] = kzalloc(sizeof(struct ubi_device),
  497. GFP_KERNEL);
  498. if (!ubi) {
  499. err = -ENOMEM;
  500. goto out_mtd;
  501. }
  502. ubi->ubi_num = ubi_devices_cnt;
  503. ubi->mtd = mtd;
  504. dbg_msg("attaching mtd%d to ubi%d: VID header offset %d data offset %d",
  505. ubi->mtd->index, ubi_devices_cnt, vid_hdr_offset, data_offset);
  506. ubi->vid_hdr_offset = vid_hdr_offset;
  507. ubi->leb_start = data_offset;
  508. err = io_init(ubi);
  509. if (err)
  510. goto out_free;
  511. err = attach_by_scanning(ubi);
  512. if (err) {
  513. dbg_err("failed to attach by scanning, error %d", err);
  514. goto out_free;
  515. }
  516. err = uif_init(ubi);
  517. if (err)
  518. goto out_detach;
  519. ubi_devices_cnt += 1;
  520. ubi_msg("attached mtd%d to ubi%d", ubi->mtd->index, ubi_devices_cnt);
  521. ubi_msg("MTD device name: \"%s\"", ubi->mtd->name);
  522. ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
  523. ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
  524. ubi->peb_size, ubi->peb_size >> 10);
  525. ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
  526. ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
  527. ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
  528. ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
  529. ubi_msg("VID header offset: %d (aligned %d)",
  530. ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
  531. ubi_msg("data offset: %d", ubi->leb_start);
  532. ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
  533. ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
  534. ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
  535. ubi_msg("number of user volumes: %d",
  536. ubi->vol_count - UBI_INT_VOL_COUNT);
  537. ubi_msg("available PEBs: %d", ubi->avail_pebs);
  538. ubi_msg("total number of reserved PEBs: %d", ubi->rsvd_pebs);
  539. ubi_msg("number of PEBs reserved for bad PEB handling: %d",
  540. ubi->beb_rsvd_pebs);
  541. ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec);
  542. /* Enable the background thread */
  543. if (!DBG_DISABLE_BGT) {
  544. ubi->thread_enabled = 1;
  545. wake_up_process(ubi->bgt_thread);
  546. }
  547. return 0;
  548. out_detach:
  549. ubi_eba_close(ubi);
  550. ubi_wl_close(ubi);
  551. kfree(ubi->vtbl);
  552. out_free:
  553. kfree(ubi);
  554. out_mtd:
  555. put_mtd_device(mtd);
  556. ubi_devices[ubi_devices_cnt] = NULL;
  557. return err;
  558. }
  559. /**
  560. * detach_mtd_dev - detach an MTD device.
  561. * @ubi: UBI device description object
  562. */
  563. static void detach_mtd_dev(struct ubi_device *ubi)
  564. {
  565. int ubi_num = ubi->ubi_num, mtd_num = ubi->mtd->index;
  566. dbg_msg("detaching mtd%d from ubi%d", ubi->mtd->index, ubi_num);
  567. uif_close(ubi);
  568. ubi_eba_close(ubi);
  569. ubi_wl_close(ubi);
  570. kfree(ubi->vtbl);
  571. put_mtd_device(ubi->mtd);
  572. kfree(ubi_devices[ubi_num]);
  573. ubi_devices[ubi_num] = NULL;
  574. ubi_devices_cnt -= 1;
  575. ubi_assert(ubi_devices_cnt >= 0);
  576. ubi_msg("mtd%d is detached from ubi%d", mtd_num, ubi_num);
  577. }
  578. static int __init ubi_init(void)
  579. {
  580. int err, i, k;
  581. /* Ensure that EC and VID headers have correct size */
  582. BUILD_BUG_ON(sizeof(struct ubi_ec_hdr) != 64);
  583. BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
  584. if (mtd_devs > UBI_MAX_DEVICES) {
  585. printk("UBI error: too many MTD devices, maximum is %d\n",
  586. UBI_MAX_DEVICES);
  587. return -EINVAL;
  588. }
  589. ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
  590. if (IS_ERR(ubi_class))
  591. return PTR_ERR(ubi_class);
  592. err = class_create_file(ubi_class, &ubi_version);
  593. if (err)
  594. goto out_class;
  595. /* Attach MTD devices */
  596. for (i = 0; i < mtd_devs; i++) {
  597. struct mtd_dev_param *p = &mtd_dev_param[i];
  598. cond_resched();
  599. if (!p->name) {
  600. dbg_err("empty name");
  601. err = -EINVAL;
  602. goto out_detach;
  603. }
  604. err = attach_mtd_dev(p->name, p->vid_hdr_offs, p->data_offs);
  605. if (err)
  606. goto out_detach;
  607. }
  608. return 0;
  609. out_detach:
  610. for (k = 0; k < i; k++)
  611. detach_mtd_dev(ubi_devices[k]);
  612. class_remove_file(ubi_class, &ubi_version);
  613. out_class:
  614. class_destroy(ubi_class);
  615. return err;
  616. }
  617. module_init(ubi_init);
  618. static void __exit ubi_exit(void)
  619. {
  620. int i, n = ubi_devices_cnt;
  621. for (i = 0; i < n; i++)
  622. detach_mtd_dev(ubi_devices[i]);
  623. class_remove_file(ubi_class, &ubi_version);
  624. class_destroy(ubi_class);
  625. }
  626. module_exit(ubi_exit);
  627. /**
  628. * bytes_str_to_int - convert a string representing number of bytes to an
  629. * integer.
  630. * @str: the string to convert
  631. *
  632. * This function returns positive resulting integer in case of success and a
  633. * negative error code in case of failure.
  634. */
  635. static int __init bytes_str_to_int(const char *str)
  636. {
  637. char *endp;
  638. unsigned long result;
  639. result = simple_strtoul(str, &endp, 0);
  640. if (str == endp || result < 0) {
  641. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  642. return -EINVAL;
  643. }
  644. switch (*endp) {
  645. case 'G':
  646. result *= 1024;
  647. case 'M':
  648. result *= 1024;
  649. case 'K':
  650. case 'k':
  651. result *= 1024;
  652. if (endp[1] == 'i' && (endp[2] == '\0' ||
  653. endp[2] == 'B' || endp[2] == 'b'))
  654. endp += 2;
  655. case '\0':
  656. break;
  657. default:
  658. printk("UBI error: incorrect bytes count: \"%s\"\n", str);
  659. return -EINVAL;
  660. }
  661. return result;
  662. }
  663. /**
  664. * ubi_mtd_param_parse - parse the 'mtd=' UBI parameter.
  665. * @val: the parameter value to parse
  666. * @kp: not used
  667. *
  668. * This function returns zero in case of success and a negative error code in
  669. * case of error.
  670. */
  671. static int __init ubi_mtd_param_parse(const char *val, struct kernel_param *kp)
  672. {
  673. int i, len;
  674. struct mtd_dev_param *p;
  675. char buf[MTD_PARAM_LEN_MAX];
  676. char *pbuf = &buf[0];
  677. char *tokens[3] = {NULL, NULL, NULL};
  678. if (mtd_devs == UBI_MAX_DEVICES) {
  679. printk("UBI error: too many parameters, max. is %d\n",
  680. UBI_MAX_DEVICES);
  681. return -EINVAL;
  682. }
  683. len = strnlen(val, MTD_PARAM_LEN_MAX);
  684. if (len == MTD_PARAM_LEN_MAX) {
  685. printk("UBI error: parameter \"%s\" is too long, max. is %d\n",
  686. val, MTD_PARAM_LEN_MAX);
  687. return -EINVAL;
  688. }
  689. if (len == 0) {
  690. printk("UBI warning: empty 'mtd=' parameter - ignored\n");
  691. return 0;
  692. }
  693. strcpy(buf, val);
  694. /* Get rid of the final newline */
  695. if (buf[len - 1] == '\n')
  696. buf[len - 1] = 0;
  697. for (i = 0; i < 3; i++)
  698. tokens[i] = strsep(&pbuf, ",");
  699. if (pbuf) {
  700. printk("UBI error: too many arguments at \"%s\"\n", val);
  701. return -EINVAL;
  702. }
  703. if (tokens[0] == '\0')
  704. return -EINVAL;
  705. p = &mtd_dev_param[mtd_devs];
  706. strcpy(&p->name[0], tokens[0]);
  707. if (tokens[1])
  708. p->vid_hdr_offs = bytes_str_to_int(tokens[1]);
  709. if (tokens[2])
  710. p->data_offs = bytes_str_to_int(tokens[2]);
  711. if (p->vid_hdr_offs < 0)
  712. return p->vid_hdr_offs;
  713. if (p->data_offs < 0)
  714. return p->data_offs;
  715. mtd_devs += 1;
  716. return 0;
  717. }
  718. module_param_call(mtd, ubi_mtd_param_parse, NULL, NULL, 000);
  719. MODULE_PARM_DESC(mtd, "MTD devices to attach. Parameter format: "
  720. "mtd=<name|num>[,<vid_hdr_offs>,<data_offs>]. "
  721. "Multiple \"mtd\" parameters may be specified.\n"
  722. "MTD devices may be specified by their number or name. "
  723. "Optional \"vid_hdr_offs\" and \"data_offs\" parameters "
  724. "specify UBI VID header position and data starting "
  725. "position to be used by UBI.\n"
  726. "Example: mtd=content,1984,2048 mtd=4 - attach MTD device"
  727. "with name content using VID header offset 1984 and data "
  728. "start 2048, and MTD device number 4 using default "
  729. "offsets");
  730. MODULE_VERSION(__stringify(UBI_VERSION));
  731. MODULE_DESCRIPTION("UBI - Unsorted Block Images");
  732. MODULE_AUTHOR("Artem Bityutskiy");
  733. MODULE_LICENSE("GPL");