mtdsplit_bcm63xx.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * Firmware MTD split for BCM63XX, based on bcm63xxpart.c
  3. *
  4. * Copyright (C) 2006-2008 Florian Fainelli <[email protected]>
  5. * Copyright (C) 2006-2008 Mike Albon <[email protected]>
  6. * Copyright (C) 2009-2010 Daniel Dickinson <[email protected]>
  7. * Copyright (C) 2011-2013 Jonas Gorski <[email protected]>
  8. * Copyright (C) 2015 Simon Arlott <[email protected]>
  9. * Copyright (C) 2017 Álvaro Fernández Rojas <[email protected]>
  10. *
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License version 2 as published
  13. * by the Free Software Foundation.
  14. *
  15. */
  16. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/bcm963xx_tag.h>
  18. #include <linux/crc32.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/slab.h>
  23. #include <linux/byteorder/generic.h>
  24. #include <linux/mtd/mtd.h>
  25. #include <linux/mtd/partitions.h>
  26. #include "mtdsplit.h"
  27. /* Ensure strings read from flash structs are null terminated */
  28. #define STR_NULL_TERMINATE(x) \
  29. do { char *_str = (x); _str[sizeof(x) - 1] = 0; } while (0)
  30. #define BCM63XX_NR_PARTS 2
  31. static int bcm63xx_read_image_tag(struct mtd_info *master, loff_t offset,
  32. struct bcm_tag *hdr)
  33. {
  34. int ret;
  35. size_t retlen;
  36. u32 computed_crc;
  37. ret = mtd_read(master, offset, sizeof(*hdr), &retlen, (void *) hdr);
  38. if (ret)
  39. return ret;
  40. if (retlen != sizeof(*hdr))
  41. return -EIO;
  42. computed_crc = crc32_le(IMAGETAG_CRC_START, (u8 *)hdr,
  43. offsetof(struct bcm_tag, header_crc));
  44. if (computed_crc == hdr->header_crc) {
  45. STR_NULL_TERMINATE(hdr->board_id);
  46. STR_NULL_TERMINATE(hdr->tag_version);
  47. pr_info("CFE image tag found at 0x%llx with version %s, "
  48. "board type %s\n", offset, hdr->tag_version,
  49. hdr->board_id);
  50. return 0;
  51. } else {
  52. pr_err("CFE image tag at 0x%llx CRC invalid "
  53. "(expected %08x, actual %08x)\n",
  54. offset, hdr->header_crc, computed_crc);
  55. return 1;
  56. }
  57. }
  58. static int bcm63xx_parse_partitions(struct mtd_info *master,
  59. const struct mtd_partition **pparts,
  60. struct bcm_tag *hdr)
  61. {
  62. struct mtd_partition *parts;
  63. unsigned int flash_image_start;
  64. unsigned int kernel_address;
  65. unsigned int kernel_length;
  66. size_t kernel_offset = 0, kernel_size = 0;
  67. size_t rootfs_offset = 0, rootfs_size = 0;
  68. int kernel_part, rootfs_part;
  69. STR_NULL_TERMINATE(hdr->flash_image_start);
  70. if (kstrtouint(hdr->flash_image_start, 10, &flash_image_start) ||
  71. flash_image_start < BCM963XX_EXTENDED_SIZE) {
  72. pr_err("invalid rootfs address: %*ph\n",
  73. (int) sizeof(hdr->flash_image_start),
  74. hdr->flash_image_start);
  75. return -EINVAL;
  76. }
  77. STR_NULL_TERMINATE(hdr->kernel_address);
  78. if (kstrtouint(hdr->kernel_address, 10, &kernel_address) ||
  79. kernel_address < BCM963XX_EXTENDED_SIZE) {
  80. pr_err("invalid kernel address: %*ph\n",
  81. (int) sizeof(hdr->kernel_address), hdr->kernel_address);
  82. return -EINVAL;
  83. }
  84. STR_NULL_TERMINATE(hdr->kernel_length);
  85. if (kstrtouint(hdr->kernel_length, 10, &kernel_length) ||
  86. !kernel_length) {
  87. pr_err("invalid kernel length: %*ph\n",
  88. (int) sizeof(hdr->kernel_length), hdr->kernel_length);
  89. return -EINVAL;
  90. }
  91. kernel_offset = kernel_address - BCM963XX_EXTENDED_SIZE -
  92. mtdpart_get_offset(master);
  93. kernel_size = kernel_length;
  94. if (flash_image_start < kernel_address) {
  95. /* rootfs first */
  96. rootfs_part = 0;
  97. kernel_part = 1;
  98. rootfs_offset = flash_image_start - BCM963XX_EXTENDED_SIZE -
  99. mtdpart_get_offset(master);
  100. rootfs_size = kernel_offset - rootfs_offset;
  101. } else {
  102. /* kernel first */
  103. kernel_part = 0;
  104. rootfs_part = 1;
  105. rootfs_offset = kernel_offset + kernel_size;
  106. rootfs_size = master->size - rootfs_offset;
  107. }
  108. if (mtd_check_rootfs_magic(master, rootfs_offset, NULL))
  109. pr_warn("rootfs magic not found\n");
  110. parts = kzalloc(BCM63XX_NR_PARTS * sizeof(*parts), GFP_KERNEL);
  111. if (!parts)
  112. return -ENOMEM;
  113. parts[kernel_part].name = KERNEL_PART_NAME;
  114. parts[kernel_part].offset = kernel_offset;
  115. parts[kernel_part].size = kernel_size;
  116. parts[rootfs_part].name = ROOTFS_PART_NAME;
  117. parts[rootfs_part].offset = rootfs_offset;
  118. parts[rootfs_part].size = rootfs_size;
  119. *pparts = parts;
  120. return BCM63XX_NR_PARTS;
  121. }
  122. static int mtdsplit_parse_bcm63xx(struct mtd_info *master,
  123. const struct mtd_partition **pparts,
  124. struct mtd_part_parser_data *data)
  125. {
  126. struct bcm_tag hdr;
  127. loff_t offset;
  128. if (mtd_type_is_nand(master))
  129. return -EINVAL;
  130. /* find bcm63xx_cfe image on erase block boundaries */
  131. for (offset = 0; offset < master->size; offset += master->erasesize) {
  132. if (!bcm63xx_read_image_tag(master, offset, (void *) &hdr))
  133. return bcm63xx_parse_partitions(master, pparts,
  134. (void *) &hdr);
  135. }
  136. return -EINVAL;
  137. }
  138. static const struct of_device_id mtdsplit_fit_of_match_table[] = {
  139. { .compatible = "brcm,bcm963xx-imagetag" },
  140. { },
  141. };
  142. static struct mtd_part_parser mtdsplit_bcm63xx_parser = {
  143. .owner = THIS_MODULE,
  144. .name = "bcm63xx-fw",
  145. .of_match_table = mtdsplit_fit_of_match_table,
  146. .parse_fn = mtdsplit_parse_bcm63xx,
  147. .type = MTD_PARSER_TYPE_FIRMWARE,
  148. };
  149. static int __init mtdsplit_bcm63xx_init(void)
  150. {
  151. register_mtd_parser(&mtdsplit_bcm63xx_parser);
  152. return 0;
  153. }
  154. module_init(mtdsplit_bcm63xx_init);