mtdsplit_eva.c 2.4 KB

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