mtdsplit_brnimage.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
  3. * Copyright (C) 2015 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published
  7. * by the Free Software Foundation.
  8. *
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/kernel.h>
  13. #include <linux/slab.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/byteorder/generic.h>
  17. #include "mtdsplit.h"
  18. #define BRNIMAGE_NR_PARTS 2
  19. #define BRNIMAGE_ALIGN_BYTES 0x400
  20. #define BRNIMAGE_FOOTER_SIZE 12
  21. #define BRNIMAGE_MIN_OVERHEAD (BRNIMAGE_FOOTER_SIZE)
  22. #define BRNIMAGE_MAX_OVERHEAD (BRNIMAGE_ALIGN_BYTES + BRNIMAGE_FOOTER_SIZE)
  23. static int mtdsplit_parse_brnimage(struct mtd_info *master,
  24. struct mtd_partition **pparts,
  25. struct mtd_part_parser_data *data)
  26. {
  27. struct mtd_partition *parts;
  28. uint32_t buf;
  29. unsigned long rootfs_offset, rootfs_size, kernel_size;
  30. size_t len;
  31. int ret = 0;
  32. for (rootfs_offset = 0; rootfs_offset < master->size;
  33. rootfs_offset += BRNIMAGE_ALIGN_BYTES) {
  34. ret = mtd_check_rootfs_magic(master, rootfs_offset, NULL);
  35. if (!ret)
  36. break;
  37. }
  38. if (ret)
  39. return ret;
  40. if (rootfs_offset >= master->size)
  41. return -EINVAL;
  42. ret = mtd_read(master, rootfs_offset - BRNIMAGE_FOOTER_SIZE, 4, &len,
  43. (void *)&buf);
  44. if (ret)
  45. return ret;
  46. if (len != 4)
  47. return -EIO;
  48. kernel_size = le32_to_cpu(buf);
  49. if (kernel_size > (rootfs_offset - BRNIMAGE_MIN_OVERHEAD))
  50. return -EINVAL;
  51. if (kernel_size < (rootfs_offset - BRNIMAGE_MAX_OVERHEAD))
  52. return -EINVAL;
  53. /*
  54. * The footer must be untouched as it contains the checksum of the
  55. * original brnImage (kernel + squashfs)!
  56. */
  57. rootfs_size = master->size - rootfs_offset - BRNIMAGE_FOOTER_SIZE;
  58. parts = kzalloc(BRNIMAGE_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  59. if (!parts)
  60. return -ENOMEM;
  61. parts[0].name = KERNEL_PART_NAME;
  62. parts[0].offset = 0;
  63. parts[0].size = kernel_size;
  64. parts[1].name = ROOTFS_PART_NAME;
  65. parts[1].offset = rootfs_offset;
  66. parts[1].size = rootfs_size;
  67. *pparts = parts;
  68. return BRNIMAGE_NR_PARTS;
  69. }
  70. static struct mtd_part_parser mtdsplit_brnimage_parser = {
  71. .owner = THIS_MODULE,
  72. .name = "brnimage-fw",
  73. .parse_fn = mtdsplit_parse_brnimage,
  74. .type = MTD_PARSER_TYPE_FIRMWARE,
  75. };
  76. static int __init mtdsplit_brnimage_init(void)
  77. {
  78. register_mtd_parser(&mtdsplit_brnimage_parser);
  79. return 0;
  80. }
  81. subsys_initcall(mtdsplit_brnimage_init);