mtdsplit_wrgg.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
  3. * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
  4. * Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
  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. struct wrgg03_header {
  23. char signature[32];
  24. uint32_t magic1;
  25. uint32_t magic2;
  26. char version[16];
  27. char model[16];
  28. uint32_t flag[2];
  29. uint32_t reserve[2];
  30. char buildno[16];
  31. uint32_t size;
  32. uint32_t offset;
  33. char devname[32];
  34. char digest[16];
  35. } __attribute__ ((packed));
  36. static int mtdsplit_parse_wrgg(struct mtd_info *master,
  37. struct mtd_partition **pparts,
  38. struct mtd_part_parser_data *data)
  39. {
  40. struct wrgg03_header hdr;
  41. size_t hdr_len, retlen, kernel_ent_size;
  42. size_t rootfs_offset;
  43. struct mtd_partition *parts;
  44. enum mtdsplit_part_type type;
  45. int err;
  46. hdr_len = sizeof(hdr);
  47. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  48. if (err)
  49. return err;
  50. if (retlen != hdr_len)
  51. return -EIO;
  52. /* sanity checks */
  53. if (le32_to_cpu(hdr.magic1) != WRGG03_MAGIC)
  54. return -EINVAL;
  55. kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
  56. if (kernel_ent_size > master->size)
  57. return -EINVAL;
  58. /*
  59. * The size in the header covers the rootfs as well.
  60. * Start the search from an arbitrary offset.
  61. */
  62. err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
  63. master->size, &rootfs_offset, &type);
  64. if (err)
  65. return err;
  66. parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  67. if (!parts)
  68. return -ENOMEM;
  69. parts[0].name = KERNEL_PART_NAME;
  70. parts[0].offset = 0;
  71. parts[0].size = rootfs_offset;
  72. parts[1].name = ROOTFS_PART_NAME;
  73. parts[1].offset = rootfs_offset;
  74. parts[1].size = master->size - rootfs_offset;
  75. *pparts = parts;
  76. return WRGG_NR_PARTS;
  77. }
  78. static struct mtd_part_parser mtdsplit_wrgg_parser = {
  79. .owner = THIS_MODULE,
  80. .name = "wrgg-fw",
  81. .parse_fn = mtdsplit_parse_wrgg,
  82. .type = MTD_PARSER_TYPE_FIRMWARE,
  83. };
  84. static int __init mtdsplit_wrgg_init(void)
  85. {
  86. register_mtd_parser(&mtdsplit_wrgg_parser);
  87. return 0;
  88. }
  89. subsys_initcall(mtdsplit_wrgg_init);