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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. @@ -1168,6 +1168,49 @@ 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. +
  21. + /* try attaching mtd device named "ubi" or "data" */
  22. + mtd = open_mtd_device("ubi");
  23. + if (IS_ERR(mtd))
  24. + mtd = open_mtd_device("data");
  25. +
  26. + if (!IS_ERR(mtd)) {
  27. + size_t len;
  28. + char magic[4];
  29. +
  30. + /* check for a valid ubi magic */
  31. + err = mtd_read(mtd, 0, 4, &len, (void *) magic);
  32. + if (!err && len == 4 && strncmp(magic, "UBI#", 4)) {
  33. + pr_err("UBI error: no valid UBI magic found inside mtd%d\n", mtd->index);
  34. + put_mtd_device(mtd);
  35. + return;
  36. + }
  37. +
  38. + /* auto-add only media types where UBI makes sense */
  39. + if (mtd->type == MTD_NANDFLASH ||
  40. + mtd->type == MTD_NORFLASH ||
  41. + mtd->type == MTD_DATAFLASH ||
  42. + mtd->type == MTD_MLCNANDFLASH) {
  43. + mutex_lock(&ubi_devices_mutex);
  44. + pr_notice("UBI: auto-attach mtd%d\n", mtd->index);
  45. + err = ubi_attach_mtd_dev(mtd, UBI_DEV_NUM_AUTO, 0, 0);
  46. + mutex_unlock(&ubi_devices_mutex);
  47. + if (err < 0) {
  48. + pr_err("UBI error: cannot attach mtd%d\n", mtd->index);
  49. + put_mtd_device(mtd);
  50. + }
  51. + }
  52. + }
  53. +}
  54. +
  55. static int __init ubi_init(void)
  56. {
  57. int err, i, k;
  58. @@ -1251,6 +1294,12 @@ static int __init ubi_init(void)
  59. }
  60. }
  61. + /* auto-attach mtd devices only if built-in to the kernel and no ubi.mtd
  62. + * parameter was given */
  63. + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV) &&
  64. + !ubi_is_module() && !mtd_devs)
  65. + ubi_auto_attach();
  66. +
  67. err = ubiblock_init();
  68. if (err) {
  69. pr_err("UBI error: block: cannot initialize, error %d\n", err);