image-sparse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <div64.h>
  39. #include <errno.h>
  40. #include <image-sparse.h>
  41. #include <malloc.h>
  42. #include <part.h>
  43. #include <sparse_format.h>
  44. #include <linux/math64.h>
  45. typedef struct sparse_buffer {
  46. void *data;
  47. u32 length;
  48. u32 repeat;
  49. u16 type;
  50. } sparse_buffer_t;
  51. static uint32_t last_offset;
  52. static unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
  53. chunk_header_t *chunk)
  54. {
  55. return chunk->total_sz - sparse->chunk_hdr_sz;
  56. }
  57. static unsigned int sparse_block_size_to_storage(unsigned int size,
  58. sparse_storage_t *storage,
  59. sparse_header_t *sparse)
  60. {
  61. return size * sparse->blk_sz / storage->block_sz;
  62. }
  63. static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
  64. {
  65. switch (chunk->chunk_type) {
  66. case CHUNK_TYPE_RAW:
  67. case CHUNK_TYPE_FILL:
  68. return true;
  69. default:
  70. return false;
  71. }
  72. }
  73. static sparse_header_t *sparse_parse_header(void **data)
  74. {
  75. /* Read and skip over sparse image header */
  76. sparse_header_t *sparse_header = (sparse_header_t *) *data;
  77. *data += sparse_header->file_hdr_sz;
  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. return sparse_header;
  88. }
  89. static int sparse_parse_fill_chunk(sparse_header_t *sparse,
  90. chunk_header_t *chunk)
  91. {
  92. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  93. if (chunk_data_sz != sizeof(uint32_t))
  94. return -EINVAL;
  95. return 0;
  96. }
  97. static int sparse_parse_raw_chunk(sparse_header_t *sparse,
  98. chunk_header_t *chunk)
  99. {
  100. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  101. /* Check if the data size is a multiple of the main block size */
  102. if (chunk_data_sz % sparse->blk_sz)
  103. return -EINVAL;
  104. /* Check that the chunk size is consistent */
  105. if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
  106. return -EINVAL;
  107. return 0;
  108. }
  109. static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
  110. void **image)
  111. {
  112. chunk_header_t *chunk = (chunk_header_t *) *image;
  113. int ret;
  114. debug("=== Chunk Header ===\n");
  115. debug("chunk_type: 0x%x\n", chunk->chunk_type);
  116. debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
  117. debug("total_size: 0x%x\n", chunk->total_sz);
  118. switch (chunk->chunk_type) {
  119. case CHUNK_TYPE_RAW:
  120. ret = sparse_parse_raw_chunk(sparse, chunk);
  121. if (ret)
  122. return NULL;
  123. break;
  124. case CHUNK_TYPE_FILL:
  125. ret = sparse_parse_fill_chunk(sparse, chunk);
  126. if (ret)
  127. return NULL;
  128. break;
  129. case CHUNK_TYPE_DONT_CARE:
  130. case CHUNK_TYPE_CRC32:
  131. debug("Ignoring chunk\n");
  132. break;
  133. default:
  134. printf("%s: Unknown chunk type: %x\n", __func__,
  135. chunk->chunk_type);
  136. return NULL;
  137. }
  138. *image += sparse->chunk_hdr_sz;
  139. return chunk;
  140. }
  141. static int sparse_get_fill_buffer(sparse_header_t *sparse,
  142. chunk_header_t *chunk,
  143. sparse_buffer_t *buffer,
  144. unsigned int blk_sz,
  145. void *data)
  146. {
  147. int i;
  148. buffer->type = CHUNK_TYPE_FILL;
  149. /*
  150. * We create a buffer of one block, and ask it to be
  151. * repeated as many times as needed.
  152. */
  153. buffer->length = blk_sz;
  154. buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
  155. buffer->data = memalign(ARCH_DMA_MINALIGN,
  156. ROUNDUP(blk_sz,
  157. ARCH_DMA_MINALIGN));
  158. if (!buffer->data)
  159. return -ENOMEM;
  160. for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
  161. ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
  162. return 0;
  163. }
  164. static int sparse_get_raw_buffer(sparse_header_t *sparse,
  165. chunk_header_t *chunk,
  166. sparse_buffer_t *buffer,
  167. unsigned int blk_sz,
  168. void *data)
  169. {
  170. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  171. buffer->type = CHUNK_TYPE_RAW;
  172. buffer->length = chunk_data_sz;
  173. buffer->data = data;
  174. buffer->repeat = 1;
  175. return 0;
  176. }
  177. static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
  178. chunk_header_t *chunk,
  179. unsigned int blk_sz,
  180. void **image)
  181. {
  182. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  183. sparse_buffer_t *buffer;
  184. void *data = *image;
  185. int ret;
  186. *image += chunk_data_sz;
  187. if (!sparse_chunk_has_buffer(chunk))
  188. return NULL;
  189. buffer = calloc(sizeof(sparse_buffer_t), 1);
  190. if (!buffer)
  191. return NULL;
  192. switch (chunk->chunk_type) {
  193. case CHUNK_TYPE_RAW:
  194. ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
  195. data);
  196. if (ret)
  197. return NULL;
  198. break;
  199. case CHUNK_TYPE_FILL:
  200. ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
  201. data);
  202. if (ret)
  203. return NULL;
  204. break;
  205. default:
  206. return NULL;
  207. }
  208. debug("=== Buffer ===\n");
  209. debug("length: 0x%x\n", buffer->length);
  210. debug("repeat: 0x%x\n", buffer->repeat);
  211. debug("type: 0x%x\n", buffer->type);
  212. debug("data: 0x%p\n", buffer->data);
  213. return buffer;
  214. }
  215. static void sparse_put_data_buffer(sparse_buffer_t *buffer)
  216. {
  217. if (buffer->type == CHUNK_TYPE_FILL)
  218. free(buffer->data);
  219. free(buffer);
  220. }
  221. int store_sparse_image(sparse_storage_t *storage, void *storage_priv,
  222. unsigned int session_id, void *data)
  223. {
  224. unsigned int chunk, offset;
  225. sparse_header_t *sparse_header;
  226. chunk_header_t *chunk_header;
  227. sparse_buffer_t *buffer;
  228. uint32_t start;
  229. uint32_t total_blocks = 0;
  230. uint32_t skipped = 0;
  231. int i;
  232. debug("=== Storage ===\n");
  233. debug("name: %s\n", storage->name);
  234. debug("block_size: 0x%x\n", storage->block_sz);
  235. debug("start: 0x%x\n", storage->start);
  236. debug("size: 0x%x\n", storage->size);
  237. debug("write: 0x%p\n", storage->write);
  238. debug("priv: 0x%p\n", storage_priv);
  239. sparse_header = sparse_parse_header(&data);
  240. if (!sparse_header) {
  241. printf("sparse header issue\n");
  242. return -EINVAL;
  243. }
  244. /*
  245. * Verify that the sparse block size is a multiple of our
  246. * storage backend block size
  247. */
  248. div_u64_rem(sparse_header->blk_sz, storage->block_sz, &offset);
  249. if (offset) {
  250. printf("%s: Sparse image block size issue [%u]\n",
  251. __func__, sparse_header->blk_sz);
  252. return -EINVAL;
  253. }
  254. /*
  255. * If it's a new flashing session, start at the beginning of
  256. * the partition. If not, then simply resume where we were.
  257. */
  258. if (session_id > 0)
  259. start = last_offset;
  260. else
  261. start = storage->start;
  262. printf("Flashing sparse image on partition %s at offset 0x%x (ID: %d)\n",
  263. storage->name, start * storage->block_sz, session_id);
  264. /* Start processing chunks */
  265. for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
  266. uint32_t blkcnt;
  267. chunk_header = sparse_parse_chunk(sparse_header, &data);
  268. if (!chunk_header) {
  269. printf("Unknown chunk type");
  270. return -EINVAL;
  271. }
  272. /*
  273. * If we have a DONT_CARE type, just skip the blocks
  274. * and go on parsing the rest of the chunks
  275. */
  276. if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) {
  277. skipped += sparse_block_size_to_storage(chunk_header->chunk_sz,
  278. storage,
  279. sparse_header);
  280. continue;
  281. }
  282. /* Retrieve the buffer we're going to write */
  283. buffer = sparse_get_data_buffer(sparse_header, chunk_header,
  284. storage->block_sz, &data);
  285. if (!buffer)
  286. continue;
  287. blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
  288. if ((start + total_blocks + blkcnt) >
  289. (storage->start + storage->size)) {
  290. printf("%s: Request would exceed partition size!\n",
  291. __func__);
  292. return -EINVAL;
  293. }
  294. for (i = 0; i < buffer->repeat; i++) {
  295. unsigned long buffer_blk_cnt;
  296. int ret;
  297. buffer_blk_cnt = buffer->length / storage->block_sz;
  298. ret = storage->write(storage, storage_priv,
  299. start + total_blocks,
  300. buffer_blk_cnt,
  301. buffer->data);
  302. if (ret < 0) {
  303. printf("%s: Write %d failed %d\n",
  304. __func__, i, ret);
  305. return ret;
  306. }
  307. total_blocks += ret;
  308. }
  309. sparse_put_data_buffer(buffer);
  310. }
  311. debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n",
  312. total_blocks, skipped,
  313. sparse_block_size_to_storage(sparse_header->total_blks,
  314. storage, sparse_header));
  315. printf("........ wrote %d blocks to '%s'\n", total_blocks,
  316. storage->name);
  317. if ((total_blocks + skipped) !=
  318. sparse_block_size_to_storage(sparse_header->total_blks,
  319. storage, sparse_header)) {
  320. printf("sparse image write failure\n");
  321. return -EIO;
  322. }
  323. last_offset = start + total_blocks;
  324. return 0;
  325. }