1
0

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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. From: Daniel Golle <daniel@makrotopia.org>
  2. Subject: ubi: auto-attach mtd device named "ubi" or "data" on boot
  3. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  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,73 @@ 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. + loff_t offset = 0;
  21. + size_t len;
  22. + char magic[4];
  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. + return;
  31. +
  32. + /* get the first not bad block */
  33. + if (mtd_can_have_bb(mtd))
  34. + while (mtd_block_isbad(mtd, offset)) {
  35. + offset += mtd->erasesize;
  36. +
  37. + if (offset > mtd->size) {
  38. + pr_err("UBI error: Failed to find a non-bad "
  39. + "block on mtd%d\n", mtd->index);
  40. + goto cleanup;
  41. + }
  42. + }
  43. +
  44. + /* check if the read from flash was successful */
  45. + err = mtd_read(mtd, offset, 4, &len, (void *) magic);
  46. + if ((err && !mtd_is_bitflip(err)) || len != 4) {
  47. + pr_err("UBI error: unable to read from mtd%d\n", mtd->index);
  48. + goto cleanup;
  49. + }
  50. +
  51. + /* check for a valid ubi magic */
  52. + if (strncmp(magic, "UBI#", 4)) {
  53. + pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
  54. + goto cleanup;
  55. + }
  56. +
  57. + /* don't auto-add media types where UBI doesn't makes sense */
  58. + if (mtd->type != MTD_NANDFLASH &&
  59. + mtd->type != MTD_NORFLASH &&
  60. + mtd->type != MTD_DATAFLASH &&
  61. + mtd->type != MTD_MLCNANDFLASH)
  62. + goto cleanup;
  63. +
  64. + mutex_lock(&ubi_devices_mutex);
  65. + pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
  66. + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
  67. + mutex_unlock(&ubi_devices_mutex);
  68. + if (err < 0) {
  69. + pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
  70. + goto cleanup;
  71. + }
  72. +
  73. + return;
  74. +
  75. +cleanup:
  76. + put_mtd_device(mtd);
  77. +}
  78. +
  79. static int __init ubi_init(void)
  80. {
  81. int err, i, k;
  82. @@ -1309,6 +1376,12 @@ static int __init ubi_init(void)
  83. }
  84. }
  85. + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
  86. + * parameter was given */
  87. + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
  88. + !ubi_is_module() && !mtd_devs)
  89. + ubi_auto_attach();
  90. +
  91. err = ubiblock_init();
  92. if (err) {
  93. pr_err("UBI error: block: cannot initialize, error %d", err);