491-ubi-auto-create-ubiblock-device-for-rootfs.patch 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. From: Daniel Golle <daniel@makrotopia.org>
  2. Subject: ubi: auto-create ubiblock device for rootfs
  3. Signed-off-by: Daniel Golle <daniel@makrotopia.org>
  4. ---
  5. drivers/mtd/ubi/block.c | 42 ++++++++++++++++++++++++++++++++++++++++++
  6. 1 file changed, 42 insertions(+)
  7. --- a/drivers/mtd/ubi/block.c
  8. +++ b/drivers/mtd/ubi/block.c
  9. @@ -633,6 +633,44 @@ static void __init ubiblock_create_from_
  10. }
  11. }
  12. +#define UBIFS_NODE_MAGIC 0x06101831
  13. +static inline int ubi_vol_is_ubifs(struct ubi_volume_desc *desc)
  14. +{
  15. + int ret;
  16. + uint32_t magic_of, magic;
  17. + ret = ubi_read(desc, 0, (char *)&magic_of, 0, 4);
  18. + if (ret)
  19. + return 0;
  20. + magic = le32_to_cpu(magic_of);
  21. + return magic == UBIFS_NODE_MAGIC;
  22. +}
  23. +
  24. +static void __init ubiblock_create_auto_rootfs(void)
  25. +{
  26. + int ubi_num, ret, is_ubifs;
  27. + struct ubi_volume_desc *desc;
  28. + struct ubi_volume_info vi;
  29. +
  30. + for (ubi_num = 0; ubi_num < UBI_MAX_DEVICES; ubi_num++) {
  31. + desc = ubi_open_volume_nm(ubi_num, "rootfs", UBI_READONLY);
  32. + if (IS_ERR(desc))
  33. + continue;
  34. +
  35. + ubi_get_volume_info(desc, &vi);
  36. + is_ubifs = ubi_vol_is_ubifs(desc);
  37. + ubi_close_volume(desc);
  38. + if (is_ubifs)
  39. + break;
  40. +
  41. + ret = ubiblock_create(&vi);
  42. + if (ret)
  43. + pr_err("UBI error: block: can't add '%s' volume, err=%d\n",
  44. + vi.name, ret);
  45. + /* always break if we get here */
  46. + break;
  47. + }
  48. +}
  49. +
  50. static void ubiblock_remove_all(void)
  51. {
  52. struct ubiblock *next;
  53. @@ -665,6 +703,10 @@ int __init ubiblock_init(void)
  54. */
  55. ubiblock_create_from_param();
  56. + /* auto-attach "rootfs" volume if existing and non-ubifs */
  57. + if (IS_ENABLED(CONFIG_MTD_ROOTFS_ROOT_DEV))
  58. + ubiblock_create_auto_rootfs();
  59. +
  60. /*
  61. * Block devices are only created upon user requests, so we ignore
  62. * existing volumes.