image-sparse.c 7.9 KB

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