fb_nand.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2014 Broadcom Corporation.
  4. * Copyright 2015 Free Electrons.
  5. */
  6. #include <config.h>
  7. #include <common.h>
  8. #include <fastboot.h>
  9. #include <image-sparse.h>
  10. #include <linux/mtd/mtd.h>
  11. #include <jffs2/jffs2.h>
  12. #include <nand.h>
  13. struct fb_nand_sparse {
  14. struct mtd_info *mtd;
  15. struct part_info *part;
  16. };
  17. __weak int board_fastboot_erase_partition_setup(char *name)
  18. {
  19. return 0;
  20. }
  21. __weak int board_fastboot_write_partition_setup(char *name)
  22. {
  23. return 0;
  24. }
  25. static int fb_nand_lookup(const char *partname,
  26. struct mtd_info **mtd,
  27. struct part_info **part,
  28. char *response)
  29. {
  30. struct mtd_device *dev;
  31. int ret;
  32. u8 pnum;
  33. ret = mtdparts_init();
  34. if (ret) {
  35. pr_err("Cannot initialize MTD partitions\n");
  36. fastboot_fail("cannot init mtdparts", response);
  37. return ret;
  38. }
  39. ret = find_dev_and_part(partname, &dev, &pnum, part);
  40. if (ret) {
  41. pr_err("cannot find partition: '%s'", partname);
  42. fastboot_fail("cannot find partition", response);
  43. return ret;
  44. }
  45. if (dev->id->type != MTD_DEV_TYPE_NAND) {
  46. pr_err("partition '%s' is not stored on a NAND device",
  47. partname);
  48. fastboot_fail("not a NAND device", response);
  49. return -EINVAL;
  50. }
  51. *mtd = get_nand_dev_by_index(dev->id->num);
  52. return 0;
  53. }
  54. static int _fb_nand_erase(struct mtd_info *mtd, struct part_info *part)
  55. {
  56. nand_erase_options_t opts;
  57. int ret;
  58. memset(&opts, 0, sizeof(opts));
  59. opts.offset = part->offset;
  60. opts.length = part->size;
  61. opts.quiet = 1;
  62. printf("Erasing blocks 0x%llx to 0x%llx\n",
  63. part->offset, part->offset + part->size);
  64. ret = nand_erase_opts(mtd, &opts);
  65. if (ret)
  66. return ret;
  67. printf("........ erased 0x%llx bytes from '%s'\n",
  68. part->size, part->name);
  69. return 0;
  70. }
  71. static int _fb_nand_write(struct mtd_info *mtd, struct part_info *part,
  72. void *buffer, unsigned int offset,
  73. unsigned int length, size_t *written)
  74. {
  75. int flags = WITH_WR_VERIFY;
  76. #ifdef CONFIG_FASTBOOT_FLASH_NAND_TRIMFFS
  77. flags |= WITH_DROP_FFS;
  78. #endif
  79. return nand_write_skip_bad(mtd, offset, &length, written,
  80. part->size - (offset - part->offset),
  81. buffer, flags);
  82. }
  83. static lbaint_t fb_nand_sparse_write(struct sparse_storage *info,
  84. lbaint_t blk, lbaint_t blkcnt, const void *buffer)
  85. {
  86. struct fb_nand_sparse *sparse = info->priv;
  87. size_t written;
  88. int ret;
  89. ret = _fb_nand_write(sparse->mtd, sparse->part, (void *)buffer,
  90. blk * info->blksz,
  91. blkcnt * info->blksz, &written);
  92. if (ret < 0) {
  93. printf("Failed to write sparse chunk\n");
  94. return ret;
  95. }
  96. /* TODO - verify that the value "written" includes the "bad-blocks" ... */
  97. /*
  98. * the return value must be 'blkcnt' ("good-blocks") plus the
  99. * number of "bad-blocks" encountered within this space...
  100. */
  101. return written / info->blksz;
  102. }
  103. static lbaint_t fb_nand_sparse_reserve(struct sparse_storage *info,
  104. lbaint_t blk, lbaint_t blkcnt)
  105. {
  106. int bad_blocks = 0;
  107. /*
  108. * TODO - implement a function to determine the total number
  109. * of blocks which must be used in order to reserve the specified
  110. * number ("blkcnt") of "good-blocks", starting at "blk"...
  111. * ( possibly something like the "check_skip_len()" function )
  112. */
  113. /*
  114. * the return value must be 'blkcnt' ("good-blocks") plus the
  115. * number of "bad-blocks" encountered within this space...
  116. */
  117. return blkcnt + bad_blocks;
  118. }
  119. void fb_nand_flash_write(const char *cmd, void *download_buffer,
  120. unsigned int download_bytes, char *response)
  121. {
  122. struct part_info *part;
  123. struct mtd_info *mtd = NULL;
  124. int ret;
  125. ret = fb_nand_lookup(cmd, &mtd, &part, response);
  126. if (ret) {
  127. pr_err("invalid NAND device");
  128. fastboot_fail("invalid NAND device", response);
  129. return;
  130. }
  131. ret = board_fastboot_write_partition_setup(part->name);
  132. if (ret)
  133. return;
  134. if (is_sparse_image(download_buffer)) {
  135. struct fb_nand_sparse sparse_priv;
  136. struct sparse_storage sparse;
  137. sparse_priv.mtd = mtd;
  138. sparse_priv.part = part;
  139. sparse.blksz = mtd->writesize;
  140. sparse.start = part->offset / sparse.blksz;
  141. sparse.size = part->size / sparse.blksz;
  142. sparse.write = fb_nand_sparse_write;
  143. sparse.reserve = fb_nand_sparse_reserve;
  144. sparse.mssg = fastboot_fail;
  145. printf("Flashing sparse image at offset " LBAFU "\n",
  146. sparse.start);
  147. sparse.priv = &sparse_priv;
  148. ret = write_sparse_image(&sparse, cmd, download_buffer,
  149. response);
  150. if (!ret)
  151. fastboot_okay(NULL, response);
  152. } else {
  153. printf("Flashing raw image at offset 0x%llx\n",
  154. part->offset);
  155. ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
  156. download_bytes, NULL);
  157. printf("........ wrote %u bytes to '%s'\n",
  158. download_bytes, part->name);
  159. }
  160. if (ret) {
  161. fastboot_fail("error writing the image", response);
  162. return;
  163. }
  164. fastboot_okay(NULL, response);
  165. }
  166. void fb_nand_erase(const char *cmd, char *response)
  167. {
  168. struct part_info *part;
  169. struct mtd_info *mtd = NULL;
  170. int ret;
  171. ret = fb_nand_lookup(cmd, &mtd, &part, response);
  172. if (ret) {
  173. pr_err("invalid NAND device");
  174. fastboot_fail("invalid NAND device", response);
  175. return;
  176. }
  177. ret = board_fastboot_erase_partition_setup(part->name);
  178. if (ret)
  179. return;
  180. ret = _fb_nand_erase(mtd, part);
  181. if (ret) {
  182. pr_err("failed erasing from device %s", mtd->name);
  183. fastboot_fail("failed erasing from device", response);
  184. return;
  185. }
  186. fastboot_okay(NULL, response);
  187. }