mtdsplit_seama.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 <linux/of.h>
  17. #include "mtdsplit.h"
  18. #define SEAMA_MAGIC 0x5EA3A417
  19. #define SEAMA_NR_PARTS 2
  20. #define SEAMA_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
  21. struct seama_header {
  22. __be32 magic; /* should always be SEAMA_MAGIC. */
  23. __be16 reserved; /* reserved for */
  24. __be16 metasize; /* size of the META data */
  25. __be32 size; /* size of the image */
  26. u8 md5[16]; /* digest */
  27. };
  28. static int mtdsplit_parse_seama(struct mtd_info *master,
  29. const struct mtd_partition **pparts,
  30. struct mtd_part_parser_data *data)
  31. {
  32. struct seama_header hdr;
  33. size_t hdr_len, retlen, kernel_ent_size;
  34. size_t rootfs_offset;
  35. struct mtd_partition *parts;
  36. enum mtdsplit_part_type type;
  37. int err;
  38. hdr_len = sizeof(hdr);
  39. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  40. if (err)
  41. return err;
  42. if (retlen != hdr_len)
  43. return -EIO;
  44. /* sanity checks */
  45. if (be32_to_cpu(hdr.magic) != SEAMA_MAGIC)
  46. return -EINVAL;
  47. kernel_ent_size = hdr_len + be32_to_cpu(hdr.size) +
  48. be16_to_cpu(hdr.metasize);
  49. if (kernel_ent_size > master->size)
  50. return -EINVAL;
  51. /* Check for the rootfs right after Seama entity with a kernel. */
  52. err = mtd_check_rootfs_magic(master, kernel_ent_size, &type);
  53. if (!err) {
  54. rootfs_offset = kernel_ent_size;
  55. } else {
  56. /*
  57. * On some devices firmware entity might contain both: kernel
  58. * and rootfs. We can't determine kernel size so we just have to
  59. * look for rootfs magic.
  60. * Start the search from an arbitrary offset.
  61. */
  62. err = mtd_find_rootfs_from(master, SEAMA_MIN_ROOTFS_OFFS,
  63. master->size, &rootfs_offset, &type);
  64. if (err)
  65. return err;
  66. }
  67. parts = kzalloc(SEAMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  68. if (!parts)
  69. return -ENOMEM;
  70. parts[0].name = KERNEL_PART_NAME;
  71. parts[0].offset = sizeof hdr + be16_to_cpu(hdr.metasize);
  72. parts[0].size = rootfs_offset - parts[0].offset;
  73. if (type == MTDSPLIT_PART_TYPE_UBI)
  74. parts[1].name = UBI_PART_NAME;
  75. else
  76. parts[1].name = ROOTFS_PART_NAME;
  77. parts[1].offset = rootfs_offset;
  78. parts[1].size = master->size - rootfs_offset;
  79. *pparts = parts;
  80. return SEAMA_NR_PARTS;
  81. }
  82. static const struct of_device_id mtdsplit_seama_of_match_table[] = {
  83. { .compatible = "seama" },
  84. {},
  85. };
  86. MODULE_DEVICE_TABLE(of, mtdsplit_seama_of_match_table);
  87. static struct mtd_part_parser mtdsplit_seama_parser = {
  88. .owner = THIS_MODULE,
  89. .name = "seama-fw",
  90. .of_match_table = mtdsplit_seama_of_match_table,
  91. .parse_fn = mtdsplit_parse_seama,
  92. .type = MTD_PARSER_TYPE_FIRMWARE,
  93. };
  94. static int __init mtdsplit_seama_init(void)
  95. {
  96. register_mtd_parser(&mtdsplit_seama_parser);
  97. return 0;
  98. }
  99. subsys_initcall(mtdsplit_seama_init);