mtdsplit.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * Copyright (C) 2009-2013 Felix Fietkau <[email protected]>
  3. * Copyright (C) 2009-2013 Gabor Juhos <[email protected]>
  4. * Copyright (C) 2012 Jonas Gorski <[email protected]>
  5. * Copyright (C) 2013 Hauke Mehrtens <[email protected]>
  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);