mtdsplit_eva.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
  3. * Copyright (C) 2015 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  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. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/byteorder/generic.h>
  17. #include "mtdsplit.h"
  18. #define EVA_NR_PARTS 2
  19. #define EVA_MAGIC 0xfeed1281
  20. #define EVA_FOOTER_SIZE 0x18
  21. #define EVA_DUMMY_SQUASHFS_SIZE 0x100
  22. struct eva_image_header {
  23. uint32_t magic;
  24. uint32_t size;
  25. };
  26. static int mtdsplit_parse_eva(struct mtd_info *master,
  27. struct mtd_partition **pparts,
  28. struct mtd_part_parser_data *data)
  29. {
  30. struct mtd_partition *parts;
  31. struct eva_image_header hdr;
  32. size_t retlen;
  33. unsigned long kernel_size, rootfs_offset;
  34. int err;
  35. err = mtd_read(master, 0, sizeof(hdr), &retlen, (void *) &hdr);
  36. if (err)
  37. return err;
  38. if (retlen != sizeof(hdr))
  39. return -EIO;
  40. if (le32_to_cpu(hdr.magic) != EVA_MAGIC)
  41. return -EINVAL;
  42. kernel_size = le32_to_cpu(hdr.size) + EVA_FOOTER_SIZE;
  43. /* rootfs starts at the next 0x10000 boundary: */
  44. rootfs_offset = round_up(kernel_size, 0x10000);
  45. /* skip the dummy EVA squashfs partition (with wrong endianness): */
  46. rootfs_offset += EVA_DUMMY_SQUASHFS_SIZE;
  47. if (rootfs_offset >= master->size)
  48. return -EINVAL;
  49. err = mtd_check_rootfs_magic(master, rootfs_offset, NULL);
  50. if (err)
  51. return err;
  52. parts = kzalloc(EVA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  53. if (!parts)
  54. return -ENOMEM;
  55. parts[0].name = KERNEL_PART_NAME;
  56. parts[0].offset = 0;
  57. parts[0].size = kernel_size;
  58. parts[1].name = ROOTFS_PART_NAME;
  59. parts[1].offset = rootfs_offset;
  60. parts[1].size = master->size - rootfs_offset;
  61. *pparts = parts;
  62. return EVA_NR_PARTS;
  63. }
  64. static struct mtd_part_parser mtdsplit_eva_parser = {
  65. .owner = THIS_MODULE,
  66. .name = "eva-fw",
  67. .parse_fn = mtdsplit_parse_eva,
  68. .type = MTD_PARSER_TYPE_FIRMWARE,
  69. };
  70. static int __init mtdsplit_eva_init(void)
  71. {
  72. register_mtd_parser(&mtdsplit_eva_parser);
  73. return 0;
  74. }
  75. subsys_initcall(mtdsplit_eva_init);