image-sparse.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 <blk.h>
  39. #include <image-sparse.h>
  40. #include <div64.h>
  41. #include <log.h>
  42. #include <malloc.h>
  43. #include <part.h>
  44. #include <sparse_format.h>
  45. #include <asm/cache.h>
  46. #include <linux/math64.h>
  47. #include <linux/err.h>
  48. static void default_log(const char *ignored, char *response) {}
  49. static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info,
  50. lbaint_t blk, lbaint_t blkcnt,
  51. void *data,
  52. char *response)
  53. {
  54. lbaint_t n = blkcnt, write_blks, blks = 0;
  55. lbaint_t aligned_buf_blks = FASTBOOT_MAX_BLK_WRITE;
  56. uint32_t *aligned_buf = NULL;
  57. if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) {
  58. write_blks = info->write(info, blk, n, data);
  59. if (write_blks < n)
  60. goto write_fail;
  61. return write_blks;
  62. }
  63. aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks);
  64. if (!aligned_buf) {
  65. info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response);
  66. return -ENOMEM;
  67. }
  68. while (blkcnt > 0) {
  69. n = min(aligned_buf_blks, blkcnt);
  70. memcpy(aligned_buf, data, n * info->blksz);
  71. /* write_blks might be > n due to NAND bad-blocks */
  72. write_blks = info->write(info, blk + blks, n, aligned_buf);
  73. if (write_blks < n) {
  74. free(aligned_buf);
  75. goto write_fail;
  76. }
  77. blks += write_blks;
  78. data += n * info->blksz;
  79. blkcnt -= n;
  80. }
  81. free(aligned_buf);
  82. return blks;
  83. write_fail:
  84. if (IS_ERR_VALUE(write_blks)) {
  85. printf("%s: Write failed, block #" LBAFU " [" LBAFU "] (%lld)\n",
  86. __func__, blk + blks, n, (long long)write_blks);
  87. info->mssg("flash write failure", response);
  88. return write_blks;
  89. }
  90. /* write_blks < n */
  91. printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n",
  92. __func__, blk + blks, n);
  93. info->mssg("flash write failure(incomplete)", response);
  94. return -1;
  95. }
  96. int write_sparse_image(struct sparse_storage *info,
  97. const char *part_name, void *data, char *response)
  98. {
  99. lbaint_t blk;
  100. lbaint_t blkcnt;
  101. lbaint_t blks;
  102. uint64_t bytes_written = 0;
  103. unsigned int chunk;
  104. unsigned int offset;
  105. uint64_t chunk_data_sz;
  106. uint32_t *fill_buf = NULL;
  107. uint32_t fill_val;
  108. sparse_header_t *sparse_header;
  109. chunk_header_t *chunk_header;
  110. uint32_t total_blocks = 0;
  111. int fill_buf_num_blks;
  112. int i;
  113. int j;
  114. fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
  115. /* Read and skip over sparse image header */
  116. sparse_header = (sparse_header_t *)data;
  117. data += sparse_header->file_hdr_sz;
  118. if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
  119. /*
  120. * Skip the remaining bytes in a header that is longer than
  121. * we expected.
  122. */
  123. data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
  124. }
  125. if (!info->mssg)
  126. info->mssg = default_log;
  127. debug("=== Sparse Image Header ===\n");
  128. debug("magic: 0x%x\n", sparse_header->magic);
  129. debug("major_version: 0x%x\n", sparse_header->major_version);
  130. debug("minor_version: 0x%x\n", sparse_header->minor_version);
  131. debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  132. debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  133. debug("blk_sz: %d\n", sparse_header->blk_sz);
  134. debug("total_blks: %d\n", sparse_header->total_blks);
  135. debug("total_chunks: %d\n", sparse_header->total_chunks);
  136. /*
  137. * Verify that the sparse block size is a multiple of our
  138. * storage backend block size
  139. */
  140. div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
  141. if (offset) {
  142. printf("%s: Sparse image block size issue [%u]\n",
  143. __func__, sparse_header->blk_sz);
  144. info->mssg("sparse image block size issue", response);
  145. return -1;
  146. }
  147. puts("Flashing Sparse Image\n");
  148. /* Start processing chunks */
  149. blk = info->start;
  150. for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
  151. /* Read and skip over chunk header */
  152. chunk_header = (chunk_header_t *)data;
  153. data += sizeof(chunk_header_t);
  154. if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
  155. debug("=== Chunk Header ===\n");
  156. debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
  157. debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
  158. debug("total_size: 0x%x\n", chunk_header->total_sz);
  159. }
  160. if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
  161. /*
  162. * Skip the remaining bytes in a header that is longer
  163. * than we expected.
  164. */
  165. data += (sparse_header->chunk_hdr_sz -
  166. sizeof(chunk_header_t));
  167. }
  168. chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
  169. blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz);
  170. switch (chunk_header->chunk_type) {
  171. case CHUNK_TYPE_RAW:
  172. if (chunk_header->total_sz !=
  173. (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
  174. info->mssg("Bogus chunk size for chunk type Raw",
  175. response);
  176. return -1;
  177. }
  178. if (blk + blkcnt > info->start + info->size) {
  179. printf(
  180. "%s: Request would exceed partition size!\n",
  181. __func__);
  182. info->mssg("Request would exceed partition size!",
  183. response);
  184. return -1;
  185. }
  186. blks = write_sparse_chunk_raw(info, blk, blkcnt,
  187. data, response);
  188. if (blks < 0)
  189. return -1;
  190. blk += blks;
  191. bytes_written += ((u64)blkcnt) * info->blksz;
  192. total_blocks += chunk_header->chunk_sz;
  193. data += chunk_data_sz;
  194. break;
  195. case CHUNK_TYPE_FILL:
  196. if (chunk_header->total_sz !=
  197. (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
  198. info->mssg("Bogus chunk size for chunk type FILL", response);
  199. return -1;
  200. }
  201. fill_buf = (uint32_t *)
  202. memalign(ARCH_DMA_MINALIGN,
  203. ROUNDUP(
  204. info->blksz * fill_buf_num_blks,
  205. ARCH_DMA_MINALIGN));
  206. if (!fill_buf) {
  207. info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
  208. response);
  209. return -1;
  210. }
  211. fill_val = *(uint32_t *)data;
  212. data = (char *)data + sizeof(uint32_t);
  213. for (i = 0;
  214. i < (info->blksz * fill_buf_num_blks /
  215. sizeof(fill_val));
  216. i++)
  217. fill_buf[i] = fill_val;
  218. if (blk + blkcnt > info->start + info->size) {
  219. printf(
  220. "%s: Request would exceed partition size!\n",
  221. __func__);
  222. info->mssg("Request would exceed partition size!",
  223. response);
  224. return -1;
  225. }
  226. for (i = 0; i < blkcnt;) {
  227. j = blkcnt - i;
  228. if (j > fill_buf_num_blks)
  229. j = fill_buf_num_blks;
  230. blks = info->write(info, blk, j, fill_buf);
  231. /* blks might be > j (eg. NAND bad-blocks) */
  232. if (blks < j) {
  233. printf("%s: %s " LBAFU " [%d]\n",
  234. __func__,
  235. "Write failed, block #",
  236. blk, j);
  237. info->mssg("flash write failure",
  238. response);
  239. free(fill_buf);
  240. return -1;
  241. }
  242. blk += blks;
  243. i += j;
  244. }
  245. bytes_written += ((u64)blkcnt) * info->blksz;
  246. total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz,
  247. sparse_header->blk_sz);
  248. free(fill_buf);
  249. break;
  250. case CHUNK_TYPE_DONT_CARE:
  251. blk += info->reserve(info, blk, blkcnt);
  252. total_blocks += chunk_header->chunk_sz;
  253. break;
  254. case CHUNK_TYPE_CRC32:
  255. if (chunk_header->total_sz !=
  256. sparse_header->chunk_hdr_sz) {
  257. info->mssg("Bogus chunk size for chunk type Dont Care",
  258. response);
  259. return -1;
  260. }
  261. total_blocks += chunk_header->chunk_sz;
  262. data += chunk_data_sz;
  263. break;
  264. default:
  265. printf("%s: Unknown chunk type: %x\n", __func__,
  266. chunk_header->chunk_type);
  267. info->mssg("Unknown chunk type", response);
  268. return -1;
  269. }
  270. }
  271. debug("Wrote %d blocks, expected to write %d blocks\n",
  272. total_blocks, sparse_header->total_blks);
  273. printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
  274. if (total_blocks != sparse_header->total_blks) {
  275. info->mssg("sparse image write failure", response);
  276. return -1;
  277. }
  278. return 0;
  279. }