mtdsplit_lzma.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <asm/unaligned.h>
  16. #include "mtdsplit.h"
  17. #define LZMA_NR_PARTS 2
  18. #define LZMA_PROPERTIES_SIZE 5
  19. struct lzma_header {
  20. u8 props[LZMA_PROPERTIES_SIZE];
  21. u8 size_low[4];
  22. u8 size_high[4];
  23. };
  24. static int mtdsplit_parse_lzma(struct mtd_info *master,
  25. const struct mtd_partition **pparts,
  26. struct mtd_part_parser_data *data)
  27. {
  28. struct lzma_header hdr;
  29. size_t hdr_len, retlen;
  30. size_t rootfs_offset;
  31. u32 t;
  32. struct mtd_partition *parts;
  33. int err;
  34. hdr_len = sizeof(hdr);
  35. err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
  36. if (err)
  37. return err;
  38. if (retlen != hdr_len)
  39. return -EIO;
  40. /* verify LZMA properties */
  41. if (hdr.props[0] >= (9 * 5 * 5))
  42. return -EINVAL;
  43. t = get_unaligned_le32(&hdr.props[1]);
  44. if (!is_power_of_2(t))
  45. return -EINVAL;
  46. t = get_unaligned_le32(&hdr.size_high);
  47. if (t)
  48. return -EINVAL;
  49. err = mtd_find_rootfs_from(master, master->erasesize, master->size,
  50. &rootfs_offset, NULL);
  51. if (err)
  52. return err;
  53. parts = kzalloc(LZMA_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  54. if (!parts)
  55. return -ENOMEM;
  56. parts[0].name = KERNEL_PART_NAME;
  57. parts[0].offset = 0;
  58. parts[0].size = rootfs_offset;
  59. parts[1].name = ROOTFS_PART_NAME;
  60. parts[1].offset = rootfs_offset;
  61. parts[1].size = master->size - rootfs_offset;
  62. *pparts = parts;
  63. return LZMA_NR_PARTS;
  64. }
  65. static const struct of_device_id mtdsplit_lzma_of_match_table[] = {
  66. { .compatible = "lzma" },
  67. {},
  68. };
  69. MODULE_DEVICE_TABLE(of, mtdsplit_lzma_of_match_table);
  70. static struct mtd_part_parser mtdsplit_lzma_parser = {
  71. .owner = THIS_MODULE,
  72. .name = "lzma-fw",
  73. .of_match_table = mtdsplit_lzma_of_match_table,
  74. .parse_fn = mtdsplit_parse_lzma,
  75. .type = MTD_PARSER_TYPE_FIRMWARE,
  76. };
  77. static int __init mtdsplit_lzma_init(void)
  78. {
  79. register_mtd_parser(&mtdsplit_lzma_parser);
  80. return 0;
  81. }
  82. subsys_initcall(mtdsplit_lzma_init);