fb_nand.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2014 Broadcom Corporation.
  3. * Copyright 2015 Free Electrons.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <config.h>
  8. #include <common.h>
  9. #include <fastboot.h>
  10. #include <image-sparse.h>
  11. #include <linux/mtd/mtd.h>
  12. #include <jffs2/jffs2.h>
  13. #include <nand.h>
  14. struct fb_nand_sparse {
  15. struct mtd_info *mtd;
  16. struct part_info *part;
  17. };
  18. __weak int board_fastboot_erase_partition_setup(char *name)
  19. {
  20. return 0;
  21. }
  22. __weak int board_fastboot_write_partition_setup(char *name)
  23. {
  24. return 0;
  25. }
  26. static int fb_nand_lookup(const char *partname,
  27. struct mtd_info **mtd,
  28. struct part_info **part)
  29. {
  30. struct mtd_device *dev;
  31. int ret;
  32. u8 pnum;
  33. ret = mtdparts_init();
  34. if (ret) {
  35. error("Cannot initialize MTD partitions\n");
  36. fastboot_fail("cannot init mtdparts");
  37. return ret;
  38. }
  39. ret = find_dev_and_part(partname, &dev, &pnum, part);
  40. if (ret) {
  41. error("cannot find partition: '%s'", partname);
  42. fastboot_fail("cannot find partition");
  43. return ret;
  44. }
  45. if (dev->id->type != MTD_DEV_TYPE_NAND) {
  46. error("partition '%s' is not stored on a NAND device",
  47. partname);
  48. fastboot_fail("not a NAND device");
  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)
  121. {
  122. struct part_info *part;
  123. struct mtd_info *mtd = NULL;
  124. int ret;
  125. ret = fb_nand_lookup(cmd, &mtd, &part);
  126. if (ret) {
  127. error("invalid NAND device");
  128. fastboot_fail("invalid NAND device");
  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. printf("Flashing sparse image at offset " LBAFU "\n",
  145. sparse.start);
  146. sparse.priv = &sparse_priv;
  147. write_sparse_image(&sparse, cmd, download_buffer,
  148. download_bytes);
  149. } else {
  150. printf("Flashing raw image at offset 0x%llx\n",
  151. part->offset);
  152. ret = _fb_nand_write(mtd, part, download_buffer, part->offset,
  153. download_bytes, NULL);
  154. printf("........ wrote %u bytes to '%s'\n",
  155. download_bytes, part->name);
  156. }
  157. if (ret) {
  158. fastboot_fail("error writing the image");
  159. return;
  160. }
  161. fastboot_okay("");
  162. }
  163. void fb_nand_erase(const char *cmd)
  164. {
  165. struct part_info *part;
  166. struct mtd_info *mtd = NULL;
  167. int ret;
  168. ret = fb_nand_lookup(cmd, &mtd, &part);
  169. if (ret) {
  170. error("invalid NAND device");
  171. fastboot_fail("invalid NAND device");
  172. return;
  173. }
  174. ret = board_fastboot_erase_partition_setup(part->name);
  175. if (ret)
  176. return;
  177. ret = _fb_nand_erase(mtd, part);
  178. if (ret) {
  179. error("failed erasing from device %s", mtd->name);
  180. fastboot_fail("failed erasing from device");
  181. return;
  182. }
  183. fastboot_okay("");
  184. }