image-sparse.c 9.2 KB

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