mtdsplit_owrt_prolog.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (C) 2026 Linus Walleij <[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. * The idea about the "executable prolog" is simple: this is an assembly
  9. * prolog that OpenWrt adds in front of the kernel to move it around in
  10. * memory. Since it is 512 bytes, we can also store the kernel file size
  11. * in the prolog and use it for splitting the partition.
  12. *
  13. * This format has been designed to be easy to create an executable prolog
  14. * with a parseable size from bash scripts:
  15. *
  16. * cat executable_prolog > image.bin
  17. * echo "OPENWRT-PROLOG-512" >> image.bin
  18. * stat -c %s zImage >> image.image
  19. * dd if=image.bin of=image.new bs=512
  20. * mv image.new image.bin
  21. *
  22. * will create a 512 bytes executable prolog with the needed tag somewhere
  23. * in the header area. (The executable_prolog needs to be small.)
  24. */
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/kernel.h>
  28. #include <linux/slab.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/of.h>
  32. #include "mtdsplit.h"
  33. #define OWRT_PROLOG_SIZE 512
  34. #define OWRT_PROLOG_MAX_OVERHEAD 32
  35. #define OWRT_PROLOG_MAGIC "OPENWRT-PROLOG-512"
  36. #define OWRT_PROLOG_NR_PARTS 2
  37. static int mtdsplit_parse_owrt_prolog(struct mtd_info *master,
  38. const struct mtd_partition **pparts,
  39. struct mtd_part_parser_data *data)
  40. {
  41. struct mtd_partition *parts;
  42. char buf[OWRT_PROLOG_SIZE];
  43. size_t retlen;
  44. unsigned long kernel_size, rootfs_offset;
  45. int ret;
  46. int i;
  47. ret = mtd_read(master, 0, sizeof(buf), &retlen, buf);
  48. if (ret)
  49. return ret;
  50. if (retlen != sizeof(buf))
  51. return -EIO;
  52. for (i = 0; i < (OWRT_PROLOG_SIZE - OWRT_PROLOG_MAX_OVERHEAD); i++) {
  53. if (!strncmp(OWRT_PROLOG_MAGIC, buf + i, strlen(OWRT_PROLOG_MAGIC)))
  54. break;
  55. }
  56. if (i == (OWRT_PROLOG_SIZE - OWRT_PROLOG_MAX_OVERHEAD)) {
  57. pr_err("%s: no OpenWrt prolog found\n", master->name);
  58. return -EINVAL;
  59. }
  60. pr_debug("%s: OpenWrt prolog found at offset %d\n", master->name, i);
  61. i += strlen(OWRT_PROLOG_MAGIC);
  62. i++; /* Skip linefeed after the magic */
  63. ret = kstrtol(buf + i, 10, &kernel_size);
  64. if (ret)
  65. return ret;
  66. /*
  67. * From the MTD point of view, the prolog is part of
  68. * the kernel.
  69. */
  70. kernel_size += OWRT_PROLOG_SIZE;
  71. pr_debug("%s: OpenWrt prolog kernel size %08lx\n",
  72. master->name, kernel_size);
  73. /* rootfs starts at the next 0x20000 (128k) boundary: */
  74. rootfs_offset = round_up(kernel_size, 0x20000);
  75. pr_debug("%s: OpenWrt prolog rootfs offset %08lx\n",
  76. master->name, rootfs_offset);
  77. if (rootfs_offset >= master->size)
  78. return -EINVAL;
  79. ret = mtd_check_rootfs_magic(master, rootfs_offset, NULL);
  80. if (ret)
  81. return ret;
  82. parts = kzalloc(OWRT_PROLOG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  83. if (!parts)
  84. return -ENOMEM;
  85. parts[0].name = KERNEL_PART_NAME;
  86. parts[0].offset = 0;
  87. parts[0].size = kernel_size;
  88. parts[1].name = ROOTFS_PART_NAME;
  89. parts[1].offset = rootfs_offset;
  90. parts[1].size = master->size - rootfs_offset;
  91. *pparts = parts;
  92. return OWRT_PROLOG_NR_PARTS;
  93. }
  94. static const struct of_device_id mtdsplit_owrt_prolog_of_match_table[] = {
  95. { .compatible = "openwrt,executable-prolog" },
  96. {},
  97. };
  98. static struct mtd_part_parser mtdsplit_owrt_prolog_parser = {
  99. .owner = THIS_MODULE,
  100. .name = "executable-prolog",
  101. .of_match_table = mtdsplit_owrt_prolog_of_match_table,
  102. .parse_fn = mtdsplit_parse_owrt_prolog,
  103. .type = MTD_PARSER_TYPE_FIRMWARE,
  104. };
  105. static int __init mtdsplit_owrt_prolog_init(void)
  106. {
  107. register_mtd_parser(&mtdsplit_owrt_prolog_parser);
  108. return 0;
  109. }
  110. subsys_initcall(mtdsplit_owrt_prolog_init);