490-ubi-auto-attach-mtd-device-named-ubi-or-data-on-boot.patch 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. From 8a52e4100d7c3a4a1dfddfa02b8864a9b0068c13 Mon Sep 17 00:00:00 2001
  2. From: Daniel Golle <daniel@makrotopia.org>
  3. Date: Sat, 17 May 2014 03:36:18 +0200
  4. Subject: [PATCH 1/5] ubi: auto-attach mtd device named "ubi" or "data" on boot
  5. To: librecmc-devel@lists.openwrt.org
  6. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  7. ---
  8. drivers/mtd/ubi/build.c | 36 ++++++++++++++++++++++++++++++++++++
  9. 1 file changed, 36 insertions(+)
  10. --- a/drivers/mtd/ubi/build.c
  11. +++ b/drivers/mtd/ubi/build.c
  12. @@ -1209,6 +1209,48 @@ static struct mtd_info * __init open_mtd
  13. return mtd;
  14. }
  15. +/*
  16. + * This function tries attaching mtd partitions named either "ubi" or "data"
  17. + * during boot.
  18. + */
  19. +static void __init ubi_auto_attach(void)
  20. +{
  21. + int err;
  22. + struct mtd_info *mtd;
  23. +
  24. + /* try attaching mtd device named "ubi" or "data" */
  25. + mtd = open_mtd_device("ubi");
  26. + if (IS_ERR(mtd))
  27. + mtd = open_mtd_device("data");
  28. +
  29. + if (!IS_ERR(mtd)) {
  30. + size_t len;
  31. + char magic[4];
  32. +
  33. + /* check for a valid ubi magic */
  34. + err = mtd_read(mtd, 0, 4, &len, (void *) magic);
  35. + if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
  36. + ubi_err("no valid UBI magic found inside mtd%d", mtd->index);
  37. + put_mtd_device(mtd);
  38. + return;
  39. + }
  40. +
  41. + /* auto-add only media types where UBI makes sense */
  42. + if (mtd->type == MTD_NANDFLASH ||
  43. + mtd->type == MTD_DATAFLASH ||
  44. + mtd->type == MTD_MLCNANDFLASH) {
  45. + mutex_lock(&ubi_devices_mutex);
  46. + ubi_msg("auto-attach mtd%d", mtd->index);
  47. + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
  48. + mutex_unlock(&ubi_devices_mutex);
  49. + if (err < 0) {
  50. + ubi_err("cannot attach mtd%d", mtd->index);
  51. + put_mtd_device(mtd);
  52. + }
  53. + }
  54. + }
  55. +}
  56. +
  57. static int __init ubi_init(void)
  58. {
  59. int err, i, k;
  60. @@ -1298,6 +1340,12 @@ static int __init ubi_init(void)
  61. }
  62. }
  63. + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
  64. + * parameter was given */
  65. + if (config_enabled(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
  66. + !ubi_is_module() && !mtd_devs)
  67. + ubi_auto_attach();
  68. +
  69. err = ubiblock_init();
  70. if (err) {
  71. ubi_err("block: cannot initialize, error %d", err);