1
0

mtdsplit_seama.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
  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. 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. static struct mtd_part_parser mtdsplit_seama_parser = {
  82. .owner = THIS_MODULE,
  83. .name = "seama-fw",
  84. .parse_fn = mtdsplit_parse_seama,
  85. .type = MTD_PARSER_TYPE_FIRMWARE,
  86. };
  87. static int __init mtdsplit_seama_init(void)
  88. {
  89. register_mtd_parser(&mtdsplit_seama_parser);
  90. return 0;
  91. }
  92. subsys_initcall(mtdsplit_seama_init);