image-sparse.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * Copyright (c) 2009, Google Inc.
  3. * All rights reserved.
  4. *
  5. * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
  6. * Portions Copyright 2014 Broadcom Corporation.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * * Neither the name of The Linux Foundation nor
  16. * the names of its contributors may be used to endorse or promote
  17. * products derived from this software without specific prior written
  18. * permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  30. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * NOTE:
  33. * Although it is very similar, this license text is not identical
  34. * to the "BSD-3-Clause", therefore, DO NOT MODIFY THIS LICENSE TEXT!
  35. */
  36. #include <config.h>
  37. #include <common.h>
  38. #include <image-sparse.h>
  39. #include <div64.h>
  40. #include <malloc.h>
  41. #include <part.h>
  42. #include <sparse_format.h>
  43. #include <fastboot.h>
  44. #include <linux/math64.h>
  45. void write_sparse_image(
  46. struct sparse_storage *info, const char *part_name,
  47. void *data, unsigned sz)
  48. {
  49. lbaint_t blk;
  50. lbaint_t blkcnt;
  51. lbaint_t blks;
  52. uint32_t bytes_written = 0;
  53. unsigned int chunk;
  54. unsigned int offset;
  55. unsigned int chunk_data_sz;
  56. uint32_t *fill_buf = NULL;
  57. uint32_t fill_val;
  58. sparse_header_t *sparse_header;
  59. chunk_header_t *chunk_header;
  60. uint32_t total_blocks = 0;
  61. int i;
  62. /* Read and skip over sparse image header */
  63. sparse_header = (sparse_header_t *)data;
  64. data += sparse_header->file_hdr_sz;
  65. if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
  66. /*
  67. * Skip the remaining bytes in a header that is longer than
  68. * we expected.
  69. */
  70. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  71. }
  72. debug("=== Sparse Image Header ===\n");
  73. debug("magic: 0x%x\n", sparse_header->magic);
  74. debug("major_version: 0x%x\n", sparse_header->major_version);
  75. debug("minor_version: 0x%x\n", sparse_header->minor_version);
  76. debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  77. debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  78. debug("blk_sz: %d\n", sparse_header->blk_sz);
  79. debug("total_blks: %d\n", sparse_header->total_blks);
  80. debug("total_chunks: %d\n", sparse_header->total_chunks);
  81. /*
  82. * Verify that the sparse block size is a multiple of our
  83. * storage backend block size
  84. */
  85. div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
  86. if (offset) {
  87. printf("%s: Sparse image block size issue [%u]\n",
  88. __func__, sparse_header->blk_sz);
  89. fastboot_fail("sparse image block size issue");
  90. return;
  91. }
  92. puts("Flashing Sparse Image\n");
  93. /* Start processing chunks */
  94. blk = info->start;
  95. for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
  96. /* Read and skip over chunk header */
  97. chunk_header = (chunk_header_t *)data;
  98. data += sizeof(chunk_header_t);
  99. if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
  100. debug("=== Chunk Header ===\n");
  101. debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
  102. debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  103. debug("total_size: 0x%x\n", chunk_header->total_sz);
  104. }
  105. if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
  106. /*
  107. * Skip the remaining bytes in a header that is longer
  108. * than we expected.
  109. */
  110. data += (sparse_header->chunk_hdr_sz -
  111. sizeof(chunk_header_t));
  112. }
  113. chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
  114. blkcnt = chunk_data_sz / info->blksz;
  115. switch (chunk_header->chunk_type) {
  116. case CHUNK_TYPE_RAW:
  117. if (chunk_header->total_sz !=
  118. (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
  119. fastboot_fail(
  120. "Bogus chunk size for chunk type Raw");
  121. return;
  122. }
  123. if (blk + blkcnt > info->start + info->size) {
  124. printf(
  125. "%s: Request would exceed partition size!\n",
  126. __func__);
  127. fastboot_fail(
  128. "Request would exceed partition size!");
  129. return;
  130. }
  131. blks = info->write(info, blk, blkcnt, data);
  132. /* blks might be > blkcnt (eg. NAND bad-blocks) */
  133. if (blks < blkcnt) {
  134. printf("%s: %s" LBAFU " [" LBAFU "]\n",
  135. __func__, "Write failed, block #",
  136. blk, blks);
  137. fastboot_fail(
  138. "flash write failure");
  139. return;
  140. }
  141. blk += blks;
  142. bytes_written += blkcnt * info->blksz;
  143. total_blocks += chunk_header->chunk_sz;
  144. data += chunk_data_sz;
  145. break;
  146. case CHUNK_TYPE_FILL:
  147. if (chunk_header->total_sz !=
  148. (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
  149. fastboot_fail(
  150. "Bogus chunk size for chunk type FILL");
  151. return;
  152. }
  153. fill_buf = (uint32_t *)
  154. memalign(ARCH_DMA_MINALIGN,
  155. ROUNDUP(info->blksz,
  156. ARCH_DMA_MINALIGN));
  157. if (!fill_buf) {
  158. fastboot_fail(
  159. "Malloc failed for: CHUNK_TYPE_FILL");
  160. return;
  161. }
  162. fill_val = *(uint32_t *)data;
  163. data = (char *)data + sizeof(uint32_t);
  164. for (i = 0; i < (info->blksz / sizeof(fill_val)); i++)
  165. fill_buf[i] = fill_val;
  166. if (blk + blkcnt > info->start + info->size) {
  167. printf(
  168. "%s: Request would exceed partition size!\n",
  169. __func__);
  170. fastboot_fail(
  171. "Request would exceed partition size!");
  172. return;
  173. }
  174. for (i = 0; i < blkcnt; i++) {
  175. blks = info->write(info, blk, 1, fill_buf);
  176. /* blks might be > 1 (eg. NAND bad-blocks) */
  177. if (blks < 1) {
  178. printf("%s: %s, block # " LBAFU "\n",
  179. __func__, "Write failed", blk);
  180. fastboot_fail(
  181. "flash write failure");
  182. free(fill_buf);
  183. return;
  184. }
  185. blk += blks;
  186. }
  187. bytes_written += blkcnt * info->blksz;
  188. total_blocks += chunk_data_sz / sparse_header->blk_sz;
  189. free(fill_buf);
  190. break;
  191. case CHUNK_TYPE_DONT_CARE:
  192. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  193. blk += blkcnt;
  194. total_blocks += chunk_header->chunk_sz;
  195. #endif
  196. break;
  197. case CHUNK_TYPE_CRC32:
  198. if (chunk_header->total_sz !=
  199. sparse_header->chunk_hdr_sz) {
  200. fastboot_fail(
  201. "Bogus chunk size for chunk type Dont Care");
  202. return;
  203. }
  204. total_blocks += chunk_header->chunk_sz;
  205. data += chunk_data_sz;
  206. break;
  207. default:
  208. printf("%s: Unknown chunk type: %x\n", __func__,
  209. chunk_header->chunk_type);
  210. fastboot_fail("Unknown chunk type");
  211. return;
  212. }
  213. }
  214. debug("Wrote %d blocks, expected to write %d blocks\n",
  215. total_blocks, sparse_header->total_blks);
  216. printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
  217. if (total_blocks != sparse_header->total_blks)
  218. fastboot_fail("sparse image write failure");
  219. else
  220. fastboot_okay("");
  221. return;
  222. }