2
0

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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. @@ -1263,6 +1263,74 @@ static struct mtd_notifier ubi_mtd_notif
  10. .remove = ubi_notify_remove,
  11. };
  12. +
  13. +/*
  14. + * This function tries attaching mtd partitions named either "ubi" or "data"
  15. + * during boot.
  16. + */
  17. +static void __init ubi_auto_attach(void)
  18. +{
  19. + int err;
  20. + struct mtd_info *mtd;
  21. + loff_t offset = 0;
  22. + size_t len;
  23. + char magic[4];
  24. +
  25. + /* try attaching mtd device named "ubi" or "data" */
  26. + mtd = open_mtd_device("ubi");
  27. + if (IS_ERR(mtd))
  28. + mtd = open_mtd_device("data");
  29. +
  30. + if (IS_ERR(mtd))
  31. + return;
  32. +
  33. + /* get the first not bad block */
  34. + if (mtd_can_have_bb(mtd))
  35. + while (mtd_block_isbad(mtd, offset)) {
  36. + offset += mtd->erasesize;
  37. +
  38. + if (offset > mtd->size) {
  39. + pr_err("UBI error: Failed to find a non-bad "
  40. + "block on mtd%d\n", mtd->index);
  41. + goto cleanup;
  42. + }
  43. + }
  44. +
  45. + /* check if the read from flash was successful */
  46. + err = mtd_read(mtd, offset, 4, &len, (void *) magic);
  47. + if ((err && !mtd_is_bitflip(err)) || len != 4) {
  48. + pr_err("UBI error: unable to read from mtd%d\n", mtd->index);
  49. + goto cleanup;
  50. + }
  51. +
  52. + /* check for a valid ubi magic */
  53. + if (strncmp(magic, "UBI#", 4)) {
  54. + pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
  55. + goto cleanup;
  56. + }
  57. +
  58. + /* don't auto-add media types where UBI doesn't makes sense */
  59. + if (mtd->type != MTD_NANDFLASH &&
  60. + mtd->type != MTD_NORFLASH &&
  61. + mtd->type != MTD_DATAFLASH &&
  62. + mtd->type != MTD_MLCNANDFLASH)
  63. + goto cleanup;
  64. +
  65. + mutex_lock(&ubi_devices_mutex);
  66. + pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
  67. + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0, false);
  68. + mutex_unlock(&ubi_devices_mutex);
  69. + if (err < 0) {
  70. + pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
  71. + goto cleanup;
  72. + }
  73. +
  74. + return;
  75. +
  76. +cleanup:
  77. + put_mtd_device(mtd);
  78. +}
  79. +
  80. static int __init ubi_init_attach(void)
  81. {
  82. int err, i, k;
  83. @@ -1313,6 +1381,12 @@ static int __init ubi_init_attach(void)
  84. }
  85. }
  86. + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
  87. + * parameter was given */
  88. + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
  89. + !ubi_is_module() && !mtd_devs)
  90. + ubi_auto_attach();
  91. +
  92. return 0;
  93. out_detach: