mtdsplit.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (C) 2009-2013 Felix Fietkau <nbd@openwrt.org>
  3. * Copyright (C) 2009-2013 Gabor Juhos <juhosg@openwrt.org>
  4. * Copyright (C) 2012 Jonas Gorski <jogo@openwrt.org>
  5. * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License version 2 as published
  9. * by the Free Software Foundation.
  10. *
  11. */
  12. #define pr_fmt(fmt) "mtdsplit: " fmt
  13. #include <linux/export.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <linux/magic.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/byteorder/generic.h>
  20. #include "mtdsplit.h"
  21. struct squashfs_super_block {
  22. __le32 s_magic;
  23. __le32 pad0[9];
  24. __le64 bytes_used;
  25. };
  26. int mtd_get_squashfs_len(struct mtd_info *master,
  27. size_t offset,
  28. size_t *squashfs_len)
  29. {
  30. struct squashfs_super_block sb;
  31. size_t retlen;
  32. int err;
  33. err = mtd_read(master, offset, sizeof(sb), &retlen, (void *)&sb);
  34. if (err || (retlen != sizeof(sb))) {
  35. pr_alert("error occured while reading from \"%s\"\n",
  36. master->name);
  37. return -EIO;
  38. }
  39. if (le32_to_cpu(sb.s_magic) != SQUASHFS_MAGIC) {
  40. pr_alert("no squashfs found in \"%s\"\n", master->name);
  41. return -EINVAL;
  42. }
  43. retlen = le64_to_cpu(sb.bytes_used);
  44. if (retlen <= 0) {
  45. pr_alert("squashfs is empty in \"%s\"\n", master->name);
  46. return -ENODEV;
  47. }
  48. if (offset + retlen > master->size) {
  49. pr_alert("squashfs has invalid size in \"%s\"\n",
  50. master->name);
  51. return -EINVAL;
  52. }
  53. *squashfs_len = retlen;
  54. return 0;
  55. }
  56. EXPORT_SYMBOL_GPL(mtd_get_squashfs_len);
  57. static ssize_t mtd_next_eb(struct mtd_info *mtd, size_t offset)
  58. {
  59. return mtd_rounddown_to_eb(offset, mtd) + mtd->erasesize;
  60. }
  61. int mtd_check_rootfs_magic(struct mtd_info *mtd, size_t offset)
  62. {
  63. u32 magic;
  64. size_t retlen;
  65. int ret;
  66. ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
  67. (unsigned char *) &magic);
  68. if (ret)
  69. return ret;
  70. if (retlen != sizeof(magic))
  71. return -EIO;
  72. if (le32_to_cpu(magic) != SQUASHFS_MAGIC &&
  73. magic != 0x19852003)
  74. return -EINVAL;
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(mtd_check_rootfs_magic);
  78. int mtd_find_rootfs_from(struct mtd_info *mtd,
  79. size_t from,
  80. size_t limit,
  81. size_t *ret_offset)
  82. {
  83. size_t offset;
  84. int err;
  85. for (offset = from; offset < limit;
  86. offset = mtd_next_eb(mtd, offset)) {
  87. err = mtd_check_rootfs_magic(mtd, offset);
  88. if (err)
  89. continue;
  90. *ret_offset = offset;
  91. return 0;
  92. }
  93. return -ENODEV;
  94. }
  95. EXPORT_SYMBOL_GPL(mtd_find_rootfs_from);