mtdsplit_cfe_bootfs.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2021 Rafał Miłecki <[email protected]>
  4. */
  5. #include <linux/init.h>
  6. #include <linux/jffs2.h>
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/mtd/mtd.h>
  10. #include <linux/mtd/partitions.h>
  11. #include <linux/of.h>
  12. #include <linux/slab.h>
  13. #include "mtdsplit.h"
  14. #define je16_to_cpu(x) ((x).v16)
  15. #define je32_to_cpu(x) ((x).v32)
  16. #define NR_PARTS 2
  17. static int mtdsplit_cfe_bootfs_parse(struct mtd_info *mtd,
  18. const struct mtd_partition **pparts,
  19. struct mtd_part_parser_data *data)
  20. {
  21. struct jffs2_raw_dirent node;
  22. enum mtdsplit_part_type type;
  23. struct mtd_partition *parts;
  24. size_t rootfs_offset;
  25. size_t retlen;
  26. size_t offset;
  27. int err;
  28. /* Don't parse backup partitions */
  29. if (strcmp(mtd->name, "firmware"))
  30. return -EINVAL;
  31. /* Find the end of JFFS2 bootfs partition */
  32. offset = 0;
  33. do {
  34. err = mtd_read(mtd, offset, sizeof(node), &retlen, (void *)&node);
  35. if (err || retlen != sizeof(node))
  36. break;
  37. if (je16_to_cpu(node.magic) != JFFS2_MAGIC_BITMASK)
  38. break;
  39. offset += je32_to_cpu(node.totlen);
  40. offset = (offset + 0x3) & ~0x3;
  41. } while (offset < mtd->size);
  42. /* Find rootfs partition that follows the bootfs */
  43. err = mtd_find_rootfs_from(mtd, mtd->erasesize, mtd->size, &rootfs_offset, &type);
  44. if (err)
  45. return err;
  46. parts = kzalloc(NR_PARTS * sizeof(*parts), GFP_KERNEL);
  47. if (!parts)
  48. return -ENOMEM;
  49. parts[0].name = "bootfs";
  50. parts[0].offset = 0;
  51. parts[0].size = rootfs_offset;
  52. if (type == MTDSPLIT_PART_TYPE_UBI)
  53. parts[1].name = UBI_PART_NAME;
  54. else
  55. parts[1].name = ROOTFS_PART_NAME;
  56. parts[1].offset = rootfs_offset;
  57. parts[1].size = mtd->size - rootfs_offset;
  58. *pparts = parts;
  59. return NR_PARTS;
  60. }
  61. static const struct of_device_id mtdsplit_cfe_bootfs_of_match_table[] = {
  62. { .compatible = "brcm,bcm4908-firmware" },
  63. {},
  64. };
  65. MODULE_DEVICE_TABLE(of, mtdsplit_cfe_bootfs_of_match_table);
  66. static struct mtd_part_parser mtdsplit_cfe_bootfs_parser = {
  67. .owner = THIS_MODULE,
  68. .name = "cfe-bootfs",
  69. .of_match_table = mtdsplit_cfe_bootfs_of_match_table,
  70. .parse_fn = mtdsplit_cfe_bootfs_parse,
  71. };
  72. module_mtd_part_parser(mtdsplit_cfe_bootfs_parser);