mtdsplit.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (C) 2009-2013 Felix Fietkau <nbd@nbd.name>
  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. #define UBI_EC_MAGIC 0x55424923 /* UBI# */
  22. struct squashfs_super_block {
  23. __le32 s_magic;
  24. __le32 pad0[9];
  25. __le64 bytes_used;
  26. };
  27. int mtd_get_squashfs_len(struct mtd_info *master,
  28. size_t offset,
  29. size_t *squashfs_len)
  30. {
  31. struct squashfs_super_block sb;
  32. size_t retlen;
  33. int err;
  34. err = mtd_read(master, offset, sizeof(sb), &retlen, (void *)&sb);
  35. if (err || (retlen != sizeof(sb))) {
  36. pr_alert("error occured while reading from \"%s\"\n",
  37. master->name);
  38. return -EIO;
  39. }
  40. if (le32_to_cpu(sb.s_magic) != SQUASHFS_MAGIC) {
  41. pr_alert("no squashfs found in \"%s\"\n", master->name);
  42. return -EINVAL;
  43. }
  44. retlen = le64_to_cpu(sb.bytes_used);
  45. if (retlen <= 0) {
  46. pr_alert("squashfs is empty in \"%s\"\n", master->name);
  47. return -ENODEV;
  48. }
  49. if (offset + retlen > master->size) {
  50. pr_alert("squashfs has invalid size in \"%s\"\n",
  51. master->name);
  52. return -EINVAL;
  53. }
  54. *squashfs_len = retlen;
  55. return 0;
  56. }
  57. EXPORT_SYMBOL_GPL(mtd_get_squashfs_len);
  58. static ssize_t mtd_next_eb(struct mtd_info *mtd, size_t offset)
  59. {
  60. return mtd_rounddown_to_eb(offset, mtd) + mtd->erasesize;
  61. }
  62. int mtd_check_rootfs_magic(struct mtd_info *mtd, size_t offset,
  63. enum mtdsplit_part_type *type)
  64. {
  65. u32 magic;
  66. size_t retlen;
  67. int ret;
  68. ret = mtd_read(mtd, offset, sizeof(magic), &retlen,
  69. (unsigned char *) &magic);
  70. if (ret)
  71. return ret;
  72. if (retlen != sizeof(magic))
  73. return -EIO;
  74. if (le32_to_cpu(magic) == SQUASHFS_MAGIC) {
  75. if (type)
  76. *type = MTDSPLIT_PART_TYPE_SQUASHFS;
  77. return 0;
  78. } else if (magic == 0x19852003) {
  79. if (type)
  80. *type = MTDSPLIT_PART_TYPE_JFFS2;
  81. return 0;
  82. } else if (be32_to_cpu(magic) == UBI_EC_MAGIC) {
  83. if (type)
  84. *type = MTDSPLIT_PART_TYPE_UBI;
  85. return 0;
  86. }
  87. return -EINVAL;
  88. }
  89. EXPORT_SYMBOL_GPL(mtd_check_rootfs_magic);
  90. int mtd_find_rootfs_from(struct mtd_info *mtd,
  91. size_t from,
  92. size_t limit,
  93. size_t *ret_offset,
  94. enum mtdsplit_part_type *type)
  95. {
  96. size_t offset;
  97. int err;
  98. for (offset = from; offset < limit;
  99. offset = mtd_next_eb(mtd, offset)) {
  100. err = mtd_check_rootfs_magic(mtd, offset, type);
  101. if (err)
  102. continue;
  103. *ret_offset = offset;
  104. return 0;
  105. }
  106. return -ENODEV;
  107. }
  108. EXPORT_SYMBOL_GPL(mtd_find_rootfs_from);