mtdsplit_wrgg.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <[email protected]>
  3. * Copyright (C) 2014 Felix Fietkau <[email protected]>
  4. * Copyright (C) 2016 Stijn Tintel <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License version 2 as published
  8. * by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/slab.h>
  15. #include <linux/mtd/mtd.h>
  16. #include <linux/mtd/partitions.h>
  17. #include <linux/byteorder/generic.h>
  18. #include <linux/of.h>
  19. #include "mtdsplit.h"
  20. #define WRGG_NR_PARTS 2
  21. #define WRGG_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
  22. #define WRGG03_MAGIC 0x20080321
  23. #define WRG_MAGIC 0x20040220
  24. struct wrgg03_header {
  25. char signature[32];
  26. uint32_t magic1;
  27. uint32_t magic2;
  28. char version[16];
  29. char model[16];
  30. uint32_t flag[2];
  31. uint32_t reserve[2];
  32. char buildno[16];
  33. uint32_t size;
  34. uint32_t offset;
  35. char devname[32];
  36. char digest[16];
  37. } __attribute__ ((packed));
  38. struct wrg_header {
  39. char signature[32];
  40. uint32_t magic1;
  41. uint32_t magic2;
  42. uint32_t size;
  43. uint32_t offset;
  44. char devname[32];
  45. char digest[16];
  46. } __attribute__ ((packed));
  47. static int mtdsplit_parse_wrgg(struct mtd_info *master,
  48. const struct mtd_partition **pparts,
  49. struct mtd_part_parser_data *data)
  50. {
  51. struct wrgg03_header hdr;
  52. size_t hdr_len, retlen, kernel_ent_size;
  53. size_t rootfs_offset;
  54. struct mtd_partition *parts;
  55. enum mtdsplit_part_type type;
  56. int err;
  57. hdr_len = sizeof(hdr);
  58. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  59. if (err)
  60. return err;
  61. if (retlen != hdr_len)
  62. return -EIO;
  63. /* sanity checks */
  64. if (le32_to_cpu(hdr.magic1) == WRGG03_MAGIC) {
  65. kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
  66. /*
  67. * If this becomes silly big it's probably because the
  68. * WRGG image is little-endian.
  69. */
  70. if (kernel_ent_size > master->size)
  71. kernel_ent_size = hdr_len + le32_to_cpu(hdr.size);
  72. /* Now what ?! It's neither */
  73. if (kernel_ent_size > master->size)
  74. return -EINVAL;
  75. } else if (le32_to_cpu(hdr.magic1) == WRG_MAGIC) {
  76. kernel_ent_size = sizeof(struct wrg_header) + le32_to_cpu(
  77. ((struct wrg_header*)&hdr)->size);
  78. } else {
  79. return -EINVAL;
  80. }
  81. if (kernel_ent_size > master->size)
  82. return -EINVAL;
  83. /*
  84. * The size in the header covers the rootfs as well.
  85. * Start the search from an arbitrary offset.
  86. */
  87. err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
  88. master->size, &rootfs_offset, &type);
  89. if (err)
  90. return err;
  91. parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  92. if (!parts)
  93. return -ENOMEM;
  94. parts[0].name = KERNEL_PART_NAME;
  95. parts[0].offset = 0;
  96. parts[0].size = rootfs_offset;
  97. parts[1].name = ROOTFS_PART_NAME;
  98. parts[1].offset = rootfs_offset;
  99. parts[1].size = master->size - rootfs_offset;
  100. *pparts = parts;
  101. return WRGG_NR_PARTS;
  102. }
  103. static const struct of_device_id mtdsplit_wrgg_of_match_table[] = {
  104. { .compatible = "wrg" },
  105. {},
  106. };
  107. MODULE_DEVICE_TABLE(of, mtdsplit_wrgg_of_match_table);
  108. static struct mtd_part_parser mtdsplit_wrgg_parser = {
  109. .owner = THIS_MODULE,
  110. .name = "wrgg-fw",
  111. .of_match_table = mtdsplit_wrgg_of_match_table,
  112. .parse_fn = mtdsplit_parse_wrgg,
  113. .type = MTD_PARSER_TYPE_FIRMWARE,
  114. };
  115. static int __init mtdsplit_wrgg_init(void)
  116. {
  117. register_mtd_parser(&mtdsplit_wrgg_parser);
  118. return 0;
  119. }
  120. subsys_initcall(mtdsplit_wrgg_init);