mtdsplit_wrgg.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. } else if (le32_to_cpu(hdr.magic1) == WRG_MAGIC) {
  66. kernel_ent_size = sizeof(struct wrg_header) + le32_to_cpu(
  67. ((struct wrg_header*)&hdr)->size);
  68. } else {
  69. return -EINVAL;
  70. }
  71. if (kernel_ent_size > master->size)
  72. return -EINVAL;
  73. /*
  74. * The size in the header covers the rootfs as well.
  75. * Start the search from an arbitrary offset.
  76. */
  77. err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
  78. master->size, &rootfs_offset, &type);
  79. if (err)
  80. return err;
  81. parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  82. if (!parts)
  83. return -ENOMEM;
  84. parts[0].name = KERNEL_PART_NAME;
  85. parts[0].offset = 0;
  86. parts[0].size = rootfs_offset;
  87. parts[1].name = ROOTFS_PART_NAME;
  88. parts[1].offset = rootfs_offset;
  89. parts[1].size = master->size - rootfs_offset;
  90. *pparts = parts;
  91. return WRGG_NR_PARTS;
  92. }
  93. static struct mtd_part_parser mtdsplit_wrgg_parser = {
  94. .owner = THIS_MODULE,
  95. .name = "wrgg-fw",
  96. .parse_fn = mtdsplit_parse_wrgg,
  97. .type = MTD_PARSER_TYPE_FIRMWARE,
  98. };
  99. static int __init mtdsplit_wrgg_init(void)
  100. {
  101. register_mtd_parser(&mtdsplit_wrgg_parser);
  102. return 0;
  103. }
  104. subsys_initcall(mtdsplit_wrgg_init);