mtdsplit_lzma.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (C) 2014 Gabor Juhos <[email protected]>
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published
  6. * by the Free Software Foundation.
  7. *
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/partitions.h>
  15. #include <linux/of.h>
  16. #include <linux/version.h>
  17. #if LINUX_VERSION_CODE < KERNEL_VERSION(6,12,0)
  18. #include <asm/unaligned.h>
  19. #else
  20. #include <linux/unaligned.h>
  21. #endif
  22. #include "mtdsplit.h"
  23. #define LZMA_NR_PARTS 2
  24. #define LZMA_PROPERTIES_SIZE 5
  25. struct lzma_header {
  26. u8 props[LZMA_PROPERTIES_SIZE];
  27. u8 size_low[4];
  28. u8 size_high[4];
  29. };
  30. static int mtdsplit_parse_lzma(struct mtd_info *master,
  31. const struct mtd_partition **pparts,
  32. struct mtd_part_parser_data *data)
  33. {
  34. struct lzma_header hdr;
  35. size_t hdr_len, retlen;
  36. size_t rootfs_offset;
  37. u32 t;
  38. struct mtd_partition *parts;
  39. int err;
  40. hdr_len = sizeof(hdr);
  41. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  42. if (err)
  43. return err;
  44. if (retlen != hdr_len)
  45. return -EIO;
  46. /* verify LZMA properties */
  47. if (hdr.props[0] >= (9 * 5 * 5))
  48. return -EINVAL;
  49. t = get_unaligned_le32(&hdr.props[1]);
  50. if (!is_power_of_2(t))
  51. return -EINVAL;
  52. t = get_unaligned_le32(&hdr.size_high);
  53. if (t)
  54. return -EINVAL;
  55. err = mtd_find_rootfs_from(master, master->erasesize, master->size,
  56. &rootfs_offset, NULL);
  57. if (err)
  58. return err;
  59. parts = kzalloc(LZMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  60. if (!parts)
  61. return -ENOMEM;
  62. parts[0].name = KERNEL_PART_NAME;
  63. parts[0].offset = 0;
  64. parts[0].size = rootfs_offset;
  65. parts[1].name = ROOTFS_PART_NAME;
  66. parts[1].offset = rootfs_offset;
  67. parts[1].size = master->size - rootfs_offset;
  68. *pparts = parts;
  69. return LZMA_NR_PARTS;
  70. }
  71. static const struct of_device_id mtdsplit_lzma_of_match_table[] = {
  72. { .compatible = "lzma" },
  73. {},
  74. };
  75. MODULE_DEVICE_TABLE(of, mtdsplit_lzma_of_match_table);
  76. static struct mtd_part_parser mtdsplit_lzma_parser = {
  77. .owner = THIS_MODULE,
  78. .name = "lzma-fw",
  79. .of_match_table = mtdsplit_lzma_of_match_table,
  80. .parse_fn = mtdsplit_parse_lzma,
  81. .type = MTD_PARSER_TYPE_FIRMWARE,
  82. };
  83. static int __init mtdsplit_lzma_init(void)
  84. {
  85. register_mtd_parser(&mtdsplit_lzma_parser);
  86. return 0;
  87. }
  88. subsys_initcall(mtdsplit_lzma_init);