image-sparse.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. #ifndef CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE
  46. #define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512)
  47. #endif
  48. void write_sparse_image(
  49. struct sparse_storage *info, const char *part_name,
  50. void *data, unsigned sz)
  51. {
  52. lbaint_t blk;
  53. lbaint_t blkcnt;
  54. lbaint_t blks;
  55. uint32_t bytes_written = 0;
  56. unsigned int chunk;
  57. unsigned int offset;
  58. unsigned int chunk_data_sz;
  59. uint32_t *fill_buf = NULL;
  60. uint32_t fill_val;
  61. sparse_header_t *sparse_header;
  62. chunk_header_t *chunk_header;
  63. uint32_t total_blocks = 0;
  64. int fill_buf_num_blks;
  65. int i;
  66. int j;
  67. fill_buf_num_blks = CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE / info->blksz;
  68. /* Read and skip over sparse image header */
  69. sparse_header = (sparse_header_t *)data;
  70. data += sparse_header->file_hdr_sz;
  71. if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
  72. /*
  73. * Skip the remaining bytes in a header that is longer than
  74. * we expected.
  75. */
  76. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  77. }
  78. debug("=== Sparse Image Header ===\n");
  79. debug("magic: 0x%x\n", sparse_header->magic);
  80. debug("major_version: 0x%x\n", sparse_header->major_version);
  81. debug("minor_version: 0x%x\n", sparse_header->minor_version);
  82. debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  83. debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  84. debug("blk_sz: %d\n", sparse_header->blk_sz);
  85. debug("total_blks: %d\n", sparse_header->total_blks);
  86. debug("total_chunks: %d\n", sparse_header->total_chunks);
  87. /*
  88. * Verify that the sparse block size is a multiple of our
  89. * storage backend block size
  90. */
  91. div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
  92. if (offset) {
  93. printf("%s: Sparse image block size issue [%u]\n",
  94. __func__, sparse_header->blk_sz);
  95. fastboot_fail("sparse image block size issue");
  96. return;
  97. }
  98. puts("Flashing Sparse Image\n");
  99. /* Start processing chunks */
  100. blk = info->start;
  101. for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
  102. /* Read and skip over chunk header */
  103. chunk_header = (chunk_header_t *)data;
  104. data += sizeof(chunk_header_t);
  105. if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
  106. debug("=== Chunk Header ===\n");
  107. debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
  108. debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  109. debug("total_size: 0x%x\n", chunk_header->total_sz);
  110. }
  111. if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
  112. /*
  113. * Skip the remaining bytes in a header that is longer
  114. * than we expected.
  115. */
  116. data += (sparse_header->chunk_hdr_sz -
  117. sizeof(chunk_header_t));
  118. }
  119. chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
  120. blkcnt = chunk_data_sz / info->blksz;
  121. switch (chunk_header->chunk_type) {
  122. case CHUNK_TYPE_RAW:
  123. if (chunk_header->total_sz !=
  124. (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
  125. fastboot_fail(
  126. "Bogus chunk size for chunk type Raw");
  127. return;
  128. }
  129. if (blk + blkcnt > info->start + info->size) {
  130. printf(
  131. "%s: Request would exceed partition size!\n",
  132. __func__);
  133. fastboot_fail(
  134. "Request would exceed partition size!");
  135. return;
  136. }
  137. blks = info->write(info, blk, blkcnt, data);
  138. /* blks might be > blkcnt (eg. NAND bad-blocks) */
  139. if (blks < blkcnt) {
  140. printf("%s: %s" LBAFU " [" LBAFU "]\n",
  141. __func__, "Write failed, block #",
  142. blk, blks);
  143. fastboot_fail(
  144. "flash write failure");
  145. return;
  146. }
  147. blk += blks;
  148. bytes_written += blkcnt * info->blksz;
  149. total_blocks += chunk_header->chunk_sz;
  150. data += chunk_data_sz;
  151. break;
  152. case CHUNK_TYPE_FILL:
  153. if (chunk_header->total_sz !=
  154. (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
  155. fastboot_fail(
  156. "Bogus chunk size for chunk type FILL");
  157. return;
  158. }
  159. fill_buf = (uint32_t *)
  160. memalign(ARCH_DMA_MINALIGN,
  161. ROUNDUP(
  162. info->blksz * fill_buf_num_blks,
  163. ARCH_DMA_MINALIGN));
  164. if (!fill_buf) {
  165. fastboot_fail(
  166. "Malloc failed for: CHUNK_TYPE_FILL");
  167. return;
  168. }
  169. fill_val = *(uint32_t *)data;
  170. data = (char *)data + sizeof(uint32_t);
  171. for (i = 0;
  172. i < (info->blksz * fill_buf_num_blks /
  173. sizeof(fill_val));
  174. i++)
  175. fill_buf[i] = fill_val;
  176. if (blk + blkcnt > info->start + info->size) {
  177. printf(
  178. "%s: Request would exceed partition size!\n",
  179. __func__);
  180. fastboot_fail(
  181. "Request would exceed partition size!");
  182. return;
  183. }
  184. for (i = 0; i < blkcnt;) {
  185. j = blkcnt - i;
  186. if (j > fill_buf_num_blks)
  187. j = fill_buf_num_blks;
  188. blks = info->write(info, blk, j, fill_buf);
  189. /* blks might be > j (eg. NAND bad-blocks) */
  190. if (blks < j) {
  191. printf("%s: %s " LBAFU " [%d]\n",
  192. __func__,
  193. "Write failed, block #",
  194. blk, j);
  195. fastboot_fail(
  196. "flash write failure");
  197. free(fill_buf);
  198. return;
  199. }
  200. blk += blks;
  201. i += j;
  202. }
  203. bytes_written += blkcnt * info->blksz;
  204. total_blocks += chunk_data_sz / sparse_header->blk_sz;
  205. free(fill_buf);
  206. break;
  207. case CHUNK_TYPE_DONT_CARE:
  208. blk += info->reserve(info, blk, blkcnt);
  209. total_blocks += chunk_header->chunk_sz;
  210. break;
  211. case CHUNK_TYPE_CRC32:
  212. if (chunk_header->total_sz !=
  213. sparse_header->chunk_hdr_sz) {
  214. fastboot_fail(
  215. "Bogus chunk size for chunk type Dont Care");
  216. return;
  217. }
  218. total_blocks += chunk_header->chunk_sz;
  219. data += chunk_data_sz;
  220. break;
  221. default:
  222. printf("%s: Unknown chunk type: %x\n", __func__,
  223. chunk_header->chunk_type);
  224. fastboot_fail("Unknown chunk type");
  225. return;
  226. }
  227. }
  228. debug("Wrote %d blocks, expected to write %d blocks\n",
  229. total_blocks, sparse_header->total_blks);
  230. printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
  231. if (total_blocks != sparse_header->total_blks)
  232. fastboot_fail("sparse image write failure");
  233. else
  234. fastboot_okay("");
  235. return;
  236. }