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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. From: Daniel Golle <[email protected]>
  2. Subject: ubi: auto-attach mtd device named "ubi" or "data" on boot
  3. Signed-off-by: Daniel Golle <[email protected]>
  4. ---
  5. drivers/mtd/ubi/build.c | 36 ++++++++++++++++++++++++++++++++++++
  6. 1 file changed, 36 insertions(+)
  7. --- a/drivers/mtd/ubi/build.c
  8. +++ b/drivers/mtd/ubi/build.c
  9. @@ -1226,6 +1226,54 @@ static struct mtd_info * __init open_mtd
  10. return mtd;
  11. }
  12. +/*
  13. + * This function tries attaching mtd partitions named either "ubi" or "data"
  14. + * during boot.
  15. + */
  16. +static void __init ubi_auto_attach(void)
  17. +{
  18. + int err;
  19. + struct mtd_info *mtd;
  20. + size_t len;
  21. + char magic[4];
  22. +
  23. + /* try attaching mtd device named "ubi" or "data" */
  24. + mtd = open_mtd_device("ubi");
  25. + if (IS_ERR(mtd))
  26. + mtd = open_mtd_device("data");
  27. +
  28. + if (IS_ERR(mtd))
  29. + return;
  30. +
  31. + /* check for a valid ubi magic if read from flash was successful */
  32. + err = mtd_read(mtd, 0, 4, &len, (void *) magic);
  33. + if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
  34. + pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
  35. + goto cleanup;
  36. + }
  37. +
  38. + /* don't auto-add media types where UBI doesn't makes sense */
  39. + if (mtd->type != MTD_NANDFLASH &&
  40. + mtd->type != MTD_NORFLASH &&
  41. + mtd->type != MTD_DATAFLASH &&
  42. + mtd->type != MTD_MLCNANDFLASH)
  43. + goto cleanup;
  44. +
  45. + mutex_lock(&ubi_devices_mutex);
  46. + pr_notice("UBI: auto-attach mtd%d\n", 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. + pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
  51. + goto cleanup;
  52. + }
  53. +
  54. + return;
  55. +
  56. +cleanup:
  57. + put_mtd_device(mtd);
  58. +}
  59. +
  60. static int __init ubi_init(void)
  61. {
  62. int err, i, k;
  63. @@ -1309,6 +1357,12 @@ static int __init ubi_init(void)
  64. }
  65. }
  66. + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
  67. + * parameter was given */
  68. + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
  69. + !ubi_is_module() && !mtd_devs)
  70. + ubi_auto_attach();
  71. +
  72. err = ubiblock_init();
  73. if (err) {
  74. pr_err("UBI error: block: cannot initialize, error %d", err);