mtdsplit_cfe_bootfs.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 1
  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. if (type == MTDSPLIT_PART_TYPE_UBI)
  50. parts[0].name = UBI_PART_NAME;
  51. else
  52. parts[0].name = ROOTFS_PART_NAME;
  53. parts[0].offset = rootfs_offset;
  54. parts[0].size = mtd->size - rootfs_offset;
  55. *pparts = parts;
  56. return NR_PARTS;
  57. }
  58. static const struct of_device_id mtdsplit_cfe_bootfs_of_match_table[] = {
  59. { .compatible = "brcm,bcm4908-firmware" },
  60. {},
  61. };
  62. MODULE_DEVICE_TABLE(of, mtdsplit_cfe_bootfs_of_match_table);
  63. static struct mtd_part_parser mtdsplit_cfe_bootfs_parser = {
  64. .owner = THIS_MODULE,
  65. .name = "cfe-bootfs",
  66. .of_match_table = mtdsplit_cfe_bootfs_of_match_table,
  67. .parse_fn = mtdsplit_cfe_bootfs_parse,
  68. };
  69. module_mtd_part_parser(mtdsplit_cfe_bootfs_parser);