fb_nand.c 5.0 KB

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