mtdsplit_wrgg.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "mtdsplit.h"
  19. #define WRGG_NR_PARTS 2
  20. #define WRGG_MIN_ROOTFS_OFFS 0x80000 /* 512KiB */
  21. #define WRGG03_MAGIC 0x20080321
  22. #define WRG_MAGIC 0x20040220
  23. struct wrgg03_header {
  24. char signature[32];
  25. uint32_t magic1;
  26. uint32_t magic2;
  27. char version[16];
  28. char model[16];
  29. uint32_t flag[2];
  30. uint32_t reserve[2];
  31. char buildno[16];
  32. uint32_t size;
  33. uint32_t offset;
  34. char devname[32];
  35. char digest[16];
  36. } __attribute__ ((packed));
  37. struct wrg_header {
  38. char signature[32];
  39. uint32_t magic1;
  40. uint32_t magic2;
  41. uint32_t size;
  42. uint32_t offset;
  43. char devname[32];
  44. char digest[16];
  45. } __attribute__ ((packed));
  46. static int mtdsplit_parse_wrgg(struct mtd_info *master,
  47. const struct mtd_partition **pparts,
  48. struct mtd_part_parser_data *data)
  49. {
  50. struct wrgg03_header hdr;
  51. size_t hdr_len, retlen, kernel_ent_size;
  52. size_t rootfs_offset;
  53. struct mtd_partition *parts;
  54. enum mtdsplit_part_type type;
  55. int err;
  56. hdr_len = sizeof(hdr);
  57. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  58. if (err)
  59. return err;
  60. if (retlen != hdr_len)
  61. return -EIO;
  62. /* sanity checks */
  63. if (le32_to_cpu(hdr.magic1) == WRGG03_MAGIC) {
  64. kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
  65. /*
  66. * If this becomes silly big it's probably because the
  67. * WRGG image is little-endian.
  68. */
  69. if (kernel_ent_size > master->size)
  70. kernel_ent_size = hdr_len + le32_to_cpu(hdr.size);
  71. /* Now what ?! It's neither */
  72. if (kernel_ent_size > master->size)
  73. return -EINVAL;
  74. } else if (le32_to_cpu(hdr.magic1) == WRG_MAGIC) {
  75. kernel_ent_size = sizeof(struct wrg_header) + le32_to_cpu(
  76. ((struct wrg_header*)&hdr)->size);
  77. } else {
  78. return -EINVAL;
  79. }
  80. if (kernel_ent_size > master->size)
  81. return -EINVAL;
  82. /*
  83. * The size in the header covers the rootfs as well.
  84. * Start the search from an arbitrary offset.
  85. */
  86. err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
  87. master->size, &rootfs_offset, &type);
  88. if (err)
  89. return err;
  90. parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  91. if (!parts)
  92. return -ENOMEM;
  93. parts[0].name = KERNEL_PART_NAME;
  94. parts[0].offset = 0;
  95. parts[0].size = rootfs_offset;
  96. parts[1].name = ROOTFS_PART_NAME;
  97. parts[1].offset = rootfs_offset;
  98. parts[1].size = master->size - rootfs_offset;
  99. *pparts = parts;
  100. return WRGG_NR_PARTS;
  101. }
  102. static const struct of_device_id mtdsplit_wrgg_of_match_table[] = {
  103. { .compatible = "wrg" },
  104. {},
  105. };
  106. MODULE_DEVICE_TABLE(of, mtdsplit_wrgg_of_match_table);
  107. static struct mtd_part_parser mtdsplit_wrgg_parser = {
  108. .owner = THIS_MODULE,
  109. .name = "wrgg-fw",
  110. .of_match_table = mtdsplit_wrgg_of_match_table,
  111. .parse_fn = mtdsplit_parse_wrgg,
  112. .type = MTD_PARSER_TYPE_FIRMWARE,
  113. };
  114. static int __init mtdsplit_wrgg_init(void)
  115. {
  116. register_mtd_parser(&mtdsplit_wrgg_parser);
  117. return 0;
  118. }
  119. subsys_initcall(mtdsplit_wrgg_init);