image-sparse.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 (unsigned int)lldiv((uint64_t)size * sparse->blk_sz,
  62. storage->block_sz);
  63. }
  64. static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
  65. {
  66. switch (chunk->chunk_type) {
  67. case CHUNK_TYPE_RAW:
  68. case CHUNK_TYPE_FILL:
  69. return true;
  70. default:
  71. return false;
  72. }
  73. }
  74. static sparse_header_t *sparse_parse_header(void **data)
  75. {
  76. /* Read and skip over sparse image header */
  77. sparse_header_t *sparse_header = (sparse_header_t *) *data;
  78. *data += sparse_header->file_hdr_sz;
  79. debug("=== Sparse Image Header ===\n");
  80. debug("magic: 0x%x\n", sparse_header->magic);
  81. debug("major_version: 0x%x\n", sparse_header->major_version);
  82. debug("minor_version: 0x%x\n", sparse_header->minor_version);
  83. debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  84. debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  85. debug("blk_sz: %d\n", sparse_header->blk_sz);
  86. debug("total_blks: %d\n", sparse_header->total_blks);
  87. debug("total_chunks: %d\n", sparse_header->total_chunks);
  88. return sparse_header;
  89. }
  90. static int sparse_parse_fill_chunk(sparse_header_t *sparse,
  91. chunk_header_t *chunk)
  92. {
  93. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  94. if (chunk_data_sz != sizeof(uint32_t))
  95. return -EINVAL;
  96. return 0;
  97. }
  98. static int sparse_parse_raw_chunk(sparse_header_t *sparse,
  99. chunk_header_t *chunk)
  100. {
  101. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  102. /* Check if the data size is a multiple of the main block size */
  103. if (chunk_data_sz % sparse->blk_sz)
  104. return -EINVAL;
  105. /* Check that the chunk size is consistent */
  106. if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
  107. return -EINVAL;
  108. return 0;
  109. }
  110. static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
  111. void **image)
  112. {
  113. chunk_header_t *chunk = (chunk_header_t *) *image;
  114. int ret;
  115. debug("=== Chunk Header ===\n");
  116. debug("chunk_type: 0x%x\n", chunk->chunk_type);
  117. debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
  118. debug("total_size: 0x%x\n", chunk->total_sz);
  119. switch (chunk->chunk_type) {
  120. case CHUNK_TYPE_RAW:
  121. ret = sparse_parse_raw_chunk(sparse, chunk);
  122. if (ret)
  123. return NULL;
  124. break;
  125. case CHUNK_TYPE_FILL:
  126. ret = sparse_parse_fill_chunk(sparse, chunk);
  127. if (ret)
  128. return NULL;
  129. break;
  130. case CHUNK_TYPE_DONT_CARE:
  131. case CHUNK_TYPE_CRC32:
  132. debug("Ignoring chunk\n");
  133. break;
  134. default:
  135. printf("%s: Unknown chunk type: %x\n", __func__,
  136. chunk->chunk_type);
  137. return NULL;
  138. }
  139. *image += sparse->chunk_hdr_sz;
  140. return chunk;
  141. }
  142. static int sparse_get_fill_buffer(sparse_header_t *sparse,
  143. chunk_header_t *chunk,
  144. sparse_buffer_t *buffer,
  145. unsigned int blk_sz,
  146. void *data)
  147. {
  148. int i;
  149. buffer->type = CHUNK_TYPE_FILL;
  150. /*
  151. * We create a buffer of one block, and ask it to be
  152. * repeated as many times as needed.
  153. */
  154. buffer->length = blk_sz;
  155. buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
  156. buffer->data = memalign(ARCH_DMA_MINALIGN,
  157. ROUNDUP(blk_sz,
  158. ARCH_DMA_MINALIGN));
  159. if (!buffer->data)
  160. return -ENOMEM;
  161. for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
  162. ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
  163. return 0;
  164. }
  165. static int sparse_get_raw_buffer(sparse_header_t *sparse,
  166. chunk_header_t *chunk,
  167. sparse_buffer_t *buffer,
  168. unsigned int blk_sz,
  169. void *data)
  170. {
  171. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  172. buffer->type = CHUNK_TYPE_RAW;
  173. buffer->length = chunk_data_sz;
  174. buffer->data = data;
  175. buffer->repeat = 1;
  176. return 0;
  177. }
  178. static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
  179. chunk_header_t *chunk,
  180. unsigned int blk_sz,
  181. void **image)
  182. {
  183. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  184. sparse_buffer_t *buffer;
  185. void *data = *image;
  186. int ret;
  187. *image += chunk_data_sz;
  188. if (!sparse_chunk_has_buffer(chunk))
  189. return NULL;
  190. buffer = calloc(sizeof(sparse_buffer_t), 1);
  191. if (!buffer)
  192. return NULL;
  193. switch (chunk->chunk_type) {
  194. case CHUNK_TYPE_RAW:
  195. ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
  196. data);
  197. if (ret)
  198. return NULL;
  199. break;
  200. case CHUNK_TYPE_FILL:
  201. ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
  202. data);
  203. if (ret)
  204. return NULL;
  205. break;
  206. default:
  207. return NULL;
  208. }
  209. debug("=== Buffer ===\n");
  210. debug("length: 0x%x\n", buffer->length);
  211. debug("repeat: 0x%x\n", buffer->repeat);
  212. debug("type: 0x%x\n", buffer->type);
  213. debug("data: 0x%p\n", buffer->data);
  214. return buffer;
  215. }
  216. static void sparse_put_data_buffer(sparse_buffer_t *buffer)
  217. {
  218. if (buffer->type == CHUNK_TYPE_FILL)
  219. free(buffer->data);
  220. free(buffer);
  221. }
  222. int store_sparse_image(sparse_storage_t *storage, void *storage_priv,
  223. unsigned int session_id, void *data)
  224. {
  225. unsigned int chunk, offset;
  226. sparse_header_t *sparse_header;
  227. chunk_header_t *chunk_header;
  228. sparse_buffer_t *buffer;
  229. uint32_t start;
  230. uint32_t total_blocks = 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. blkcnt = sparse_block_size_to_storage(chunk_header->chunk_sz,
  278. storage,
  279. sparse_header);
  280. #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
  281. total_blocks += blkcnt;
  282. #endif
  283. continue;
  284. }
  285. /* Retrieve the buffer we're going to write */
  286. buffer = sparse_get_data_buffer(sparse_header, chunk_header,
  287. storage->block_sz, &data);
  288. if (!buffer)
  289. continue;
  290. blkcnt = (buffer->length / storage->block_sz) * buffer->repeat;
  291. if ((start + total_blocks + blkcnt) >
  292. (storage->start + storage->size)) {
  293. printf("%s: Request would exceed partition size!\n",
  294. __func__);
  295. return -EINVAL;
  296. }
  297. for (i = 0; i < buffer->repeat; i++) {
  298. unsigned long buffer_blk_cnt;
  299. int ret;
  300. buffer_blk_cnt = buffer->length / storage->block_sz;
  301. ret = storage->write(storage, storage_priv,
  302. start + total_blocks,
  303. buffer_blk_cnt,
  304. buffer->data);
  305. if (ret < 0) {
  306. printf("%s: Write %d failed %d\n",
  307. __func__, i, ret);
  308. return ret;
  309. }
  310. total_blocks += ret;
  311. }
  312. sparse_put_data_buffer(buffer);
  313. }
  314. debug("Wrote %d blocks, expected to write %d blocks\n",
  315. total_blocks,
  316. sparse_block_size_to_storage(sparse_header->total_blks,
  317. storage, sparse_header));
  318. printf("........ wrote %d blocks to '%s'\n", total_blocks,
  319. storage->name);
  320. if (total_blocks !=
  321. sparse_block_size_to_storage(sparse_header->total_blks,
  322. storage, sparse_header)) {
  323. printf("sparse image write failure\n");
  324. return -EIO;
  325. }
  326. last_offset = start + total_blocks;
  327. return 0;
  328. }