mtdsplit_seama.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/partitions.h>
  15. #include <linux/byteorder/generic.h>
  16. #include "mtdsplit.h"
  17. #define SEAMA_MAGIC 0x5EA3A417
  18. #define SEAMA_NR_PARTS 2
  19. #define SEAMA_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
  20. struct seama_header {
  21. __be32 magic; /* should always be SEAMA_MAGIC. */
  22. __be16 reserved; /* reserved for */
  23. __be16 metasize; /* size of the META data */
  24. __be32 size; /* size of the image */
  25. u8 md5[16]; /* digest */
  26. };
  27. static int mtdsplit_parse_seama(struct mtd_info *master,
  28. const struct mtd_partition **pparts,
  29. struct mtd_part_parser_data *data)
  30. {
  31. struct seama_header hdr;
  32. size_t hdr_len, retlen, kernel_ent_size;
  33. size_t rootfs_offset;
  34. struct mtd_partition *parts;
  35. enum mtdsplit_part_type type;
  36. int err;
  37. hdr_len = sizeof(hdr);
  38. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  39. if (err)
  40. return err;
  41. if (retlen != hdr_len)
  42. return -EIO;
  43. /* sanity checks */
  44. if (be32_to_cpu(hdr.magic) != SEAMA_MAGIC)
  45. return -EINVAL;
  46. kernel_ent_size = hdr_len + be32_to_cpu(hdr.size) +
  47. be16_to_cpu(hdr.metasize);
  48. if (kernel_ent_size > master->size)
  49. return -EINVAL;
  50. /* Check for the rootfs right after Seama entity with a kernel. */
  51. err = mtd_check_rootfs_magic(master, kernel_ent_size, &type);
  52. if (!err) {
  53. rootfs_offset = kernel_ent_size;
  54. } else {
  55. /*
  56. * On some devices firmware entity might contain both: kernel
  57. * and rootfs. We can't determine kernel size so we just have to
  58. * look for rootfs magic.
  59. * Start the search from an arbitrary offset.
  60. */
  61. err = mtd_find_rootfs_from(master, SEAMA_MIN_ROOTFS_OFFS,
  62. master->size, &rootfs_offset, &type);
  63. if (err)
  64. return err;
  65. }
  66. parts = kzalloc(SEAMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  67. if (!parts)
  68. return -ENOMEM;
  69. parts[0].name = KERNEL_PART_NAME;
  70. parts[0].offset = sizeof hdr + be16_to_cpu(hdr.metasize);
  71. parts[0].size = rootfs_offset - parts[0].offset;
  72. if (type == MTDSPLIT_PART_TYPE_UBI)
  73. parts[1].name = UBI_PART_NAME;
  74. else
  75. parts[1].name = ROOTFS_PART_NAME;
  76. parts[1].offset = rootfs_offset;
  77. parts[1].size = master->size - rootfs_offset;
  78. *pparts = parts;
  79. return SEAMA_NR_PARTS;
  80. }
  81. #include <linux/version.h>
  82. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  83. static const struct of_device_id mtdsplit_seama_of_match_table[] = {
  84. { .compatible = "seama" },
  85. {},
  86. };
  87. MODULE_DEVICE_TABLE(of, mtdsplit_seama_of_match_table);
  88. #endif
  89. static struct mtd_part_parser mtdsplit_seama_parser = {
  90. .owner = THIS_MODULE,
  91. .name = "seama-fw",
  92. #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
  93. .of_match_table = mtdsplit_seama_of_match_table,
  94. #endif
  95. .parse_fn = mtdsplit_parse_seama,
  96. .type = MTD_PARSER_TYPE_FIRMWARE,
  97. };
  98. static int __init mtdsplit_seama_init(void)
  99. {
  100. register_mtd_parser(&mtdsplit_seama_parser);
  101. return 0;
  102. }
  103. subsys_initcall(mtdsplit_seama_init);