mtdsplit_squashfs.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (C) 2013 Felix Fietkau <nbd@nbd.name>
  3. * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. */
  10. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/magic.h>
  16. #include <linux/mtd/mtd.h>
  17. #include <linux/mtd/partitions.h>
  18. #include <linux/byteorder/generic.h>
  19. #include "mtdsplit.h"
  20. static int
  21. mtdsplit_parse_squashfs(struct mtd_info *master,
  22. struct mtd_partition **pparts,
  23. struct mtd_part_parser_data *data)
  24. {
  25. struct mtd_partition *part;
  26. struct mtd_info *parent_mtd;
  27. size_t part_offset;
  28. size_t squashfs_len;
  29. int err;
  30. err = mtd_get_squashfs_len(master, 0, &squashfs_len);
  31. if (err)
  32. return err;
  33. parent_mtd = mtdpart_get_master(master);
  34. part_offset = mtdpart_get_offset(master);
  35. part = kzalloc(sizeof(*part), GFP_KERNEL);
  36. if (!part) {
  37. pr_alert("unable to allocate memory for \"%s\" partition\n",
  38. ROOTFS_SPLIT_NAME);
  39. return -ENOMEM;
  40. }
  41. part->name = ROOTFS_SPLIT_NAME;
  42. part->offset = mtd_roundup_to_eb(part_offset + squashfs_len,
  43. parent_mtd) - part_offset;
  44. part->size = mtd_rounddown_to_eb(master->size - part->offset, master);
  45. *pparts = part;
  46. return 1;
  47. }
  48. static struct mtd_part_parser mtdsplit_squashfs_parser = {
  49. .owner = THIS_MODULE,
  50. .name = "squashfs-split",
  51. .parse_fn = mtdsplit_parse_squashfs,
  52. .type = MTD_PARSER_TYPE_ROOTFS,
  53. };
  54. static int __init mtdsplit_squashfs_init(void)
  55. {
  56. register_mtd_parser(&mtdsplit_squashfs_parser);
  57. return 0;
  58. }
  59. subsys_initcall(mtdsplit_squashfs_init);