aboot.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 <aboot.h>
  39. #include <div64.h>
  40. #include <errno.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 unsigned int sparse_get_chunk_data_size(sparse_header_t *sparse,
  52. chunk_header_t *chunk)
  53. {
  54. return chunk->total_sz - sparse->chunk_hdr_sz;
  55. }
  56. static bool sparse_chunk_has_buffer(chunk_header_t *chunk)
  57. {
  58. switch (chunk->chunk_type) {
  59. case CHUNK_TYPE_RAW:
  60. case CHUNK_TYPE_FILL:
  61. return true;
  62. default:
  63. return false;
  64. }
  65. }
  66. static sparse_header_t *sparse_parse_header(void **data)
  67. {
  68. /* Read and skip over sparse image header */
  69. sparse_header_t *sparse_header = (sparse_header_t *) *data;
  70. *data += sparse_header->file_hdr_sz;
  71. debug("=== Sparse Image Header ===\n");
  72. debug("magic: 0x%x\n", sparse_header->magic);
  73. debug("major_version: 0x%x\n", sparse_header->major_version);
  74. debug("minor_version: 0x%x\n", sparse_header->minor_version);
  75. debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
  76. debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
  77. debug("blk_sz: %d\n", sparse_header->blk_sz);
  78. debug("total_blks: %d\n", sparse_header->total_blks);
  79. debug("total_chunks: %d\n", sparse_header->total_chunks);
  80. return sparse_header;
  81. }
  82. static int sparse_parse_fill_chunk(sparse_header_t *sparse,
  83. chunk_header_t *chunk)
  84. {
  85. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  86. if (chunk_data_sz != sizeof(uint32_t))
  87. return -EINVAL;
  88. return 0;
  89. }
  90. static int sparse_parse_raw_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. /* Check if the data size is a multiple of the main block size */
  95. if (chunk_data_sz % sparse->blk_sz)
  96. return -EINVAL;
  97. /* Check that the chunk size is consistent */
  98. if ((chunk_data_sz / sparse->blk_sz) != chunk->chunk_sz)
  99. return -EINVAL;
  100. return 0;
  101. }
  102. static chunk_header_t *sparse_parse_chunk(sparse_header_t *sparse,
  103. void **image)
  104. {
  105. chunk_header_t *chunk = (chunk_header_t *) *image;
  106. int ret;
  107. debug("=== Chunk Header ===\n");
  108. debug("chunk_type: 0x%x\n", chunk->chunk_type);
  109. debug("chunk_data_sz: 0x%x\n", chunk->chunk_sz);
  110. debug("total_size: 0x%x\n", chunk->total_sz);
  111. switch (chunk->chunk_type) {
  112. case CHUNK_TYPE_RAW:
  113. ret = sparse_parse_raw_chunk(sparse, chunk);
  114. if (ret)
  115. return NULL;
  116. break;
  117. case CHUNK_TYPE_FILL:
  118. ret = sparse_parse_fill_chunk(sparse, chunk);
  119. if (ret)
  120. return NULL;
  121. break;
  122. case CHUNK_TYPE_DONT_CARE:
  123. case CHUNK_TYPE_CRC32:
  124. debug("Ignoring chunk\n");
  125. break;
  126. default:
  127. printf("%s: Unknown chunk type: %x\n", __func__,
  128. chunk->chunk_type);
  129. return NULL;
  130. }
  131. *image += sparse->chunk_hdr_sz;
  132. return chunk;
  133. }
  134. static int sparse_get_fill_buffer(sparse_header_t *sparse,
  135. chunk_header_t *chunk,
  136. sparse_buffer_t *buffer,
  137. unsigned int blk_sz,
  138. void *data)
  139. {
  140. int i;
  141. buffer->type = CHUNK_TYPE_FILL;
  142. /*
  143. * We create a buffer of one block, and ask it to be
  144. * repeated as many times as needed.
  145. */
  146. buffer->length = blk_sz;
  147. buffer->repeat = (chunk->chunk_sz * sparse->blk_sz) / blk_sz;
  148. buffer->data = memalign(ARCH_DMA_MINALIGN,
  149. ROUNDUP(blk_sz,
  150. ARCH_DMA_MINALIGN));
  151. if (!buffer->data)
  152. return -ENOMEM;
  153. for (i = 0; i < (buffer->length / sizeof(uint32_t)); i++)
  154. ((uint32_t *)buffer->data)[i] = *(uint32_t *)(data);
  155. return 0;
  156. }
  157. static int sparse_get_raw_buffer(sparse_header_t *sparse,
  158. chunk_header_t *chunk,
  159. sparse_buffer_t *buffer,
  160. unsigned int blk_sz,
  161. void *data)
  162. {
  163. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  164. buffer->type = CHUNK_TYPE_RAW;
  165. buffer->length = chunk_data_sz;
  166. buffer->data = data;
  167. buffer->repeat = 1;
  168. return 0;
  169. }
  170. static sparse_buffer_t *sparse_get_data_buffer(sparse_header_t *sparse,
  171. chunk_header_t *chunk,
  172. unsigned int blk_sz,
  173. void **image)
  174. {
  175. unsigned int chunk_data_sz = sparse_get_chunk_data_size(sparse, chunk);
  176. sparse_buffer_t *buffer;
  177. void *data = *image;
  178. int ret;
  179. *image += chunk_data_sz;
  180. if (!sparse_chunk_has_buffer(chunk))
  181. return NULL;
  182. buffer = calloc(sizeof(sparse_buffer_t), 1);
  183. if (!buffer)
  184. return NULL;
  185. switch (chunk->chunk_type) {
  186. case CHUNK_TYPE_RAW:
  187. ret = sparse_get_raw_buffer(sparse, chunk, buffer, blk_sz,
  188. data);
  189. if (ret)
  190. return NULL;
  191. break;
  192. case CHUNK_TYPE_FILL:
  193. ret = sparse_get_fill_buffer(sparse, chunk, buffer, blk_sz,
  194. data);
  195. if (ret)
  196. return NULL;
  197. break;
  198. default:
  199. return NULL;
  200. }
  201. debug("=== Buffer ===\n");
  202. debug("length: 0x%x\n", buffer->length);
  203. debug("repeat: 0x%x\n", buffer->repeat);
  204. debug("type: 0x%x\n", buffer->type);
  205. debug("data: 0x%p\n", buffer->data);
  206. return buffer;
  207. }
  208. static void sparse_put_data_buffer(sparse_buffer_t *buffer)
  209. {
  210. if (buffer->type == CHUNK_TYPE_FILL)
  211. free(buffer->data);
  212. free(buffer);
  213. }
  214. void write_sparse_image(block_dev_desc_t *dev_desc,
  215. disk_partition_t *info, const char *part_name,
  216. void *data, unsigned sz)
  217. {
  218. lbaint_t start;
  219. lbaint_t blkcnt;
  220. unsigned int chunk, offset;
  221. sparse_header_t *sparse_header;
  222. chunk_header_t *chunk_header;
  223. sparse_buffer_t *buffer;
  224. uint32_t total_blocks = 0;
  225. uint32_t skipped = 0;
  226. int i;
  227. sparse_header = sparse_parse_header(&data);
  228. if (!sparse_header) {
  229. fastboot_fail("sparse header issue\n");
  230. return;
  231. }
  232. /*
  233. * Verify that the sparse block size is a multiple of our
  234. * storage backend block size
  235. */
  236. div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
  237. if (offset) {
  238. printf("%s: Sparse image block size issue [%u]\n",
  239. __func__, sparse_header->blk_sz);
  240. fastboot_fail("sparse image block size issue");
  241. return;
  242. }
  243. puts("Flashing Sparse Image\n");
  244. /* Start processing chunks */
  245. start = info->start;
  246. for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
  247. chunk_header = sparse_parse_chunk(sparse_header, &data);
  248. if (!chunk_header) {
  249. fastboot_fail("Unknown chunk type");
  250. return;
  251. }
  252. /*
  253. * If we have a DONT_CARE type, just skip the blocks
  254. * and go on parsing the rest of the chunks
  255. */
  256. if (chunk_header->chunk_type == CHUNK_TYPE_DONT_CARE) {
  257. skipped += chunk_header->chunk_sz;
  258. continue;
  259. }
  260. /* Retrieve the buffer we're going to write */
  261. buffer = sparse_get_data_buffer(sparse_header, chunk_header,
  262. info->blksz, &data);
  263. if (!buffer)
  264. continue;
  265. blkcnt = (buffer->length / info->blksz) * buffer->repeat;
  266. if ((start + total_blocks + blkcnt) >
  267. (info->start + info->size)) {
  268. printf("%s: Request would exceed partition size!\n",
  269. __func__);
  270. fastboot_fail("Request would exceed partition size!");
  271. return;
  272. }
  273. for (i = 0; i < buffer->repeat; i++) {
  274. unsigned long buffer_blk_cnt;
  275. unsigned long buffer_blks;
  276. buffer_blk_cnt = buffer->length / info->blksz;
  277. buffer_blks = dev_desc->block_write(dev_desc->dev,
  278. start + total_blocks,
  279. buffer_blk_cnt, buffer->data);
  280. if (buffer_blks != buffer_blk_cnt) {
  281. printf("%s: Write %d failed " LBAFU "\n",
  282. __func__, i, buffer_blks);
  283. fastboot_fail("flash write failure");
  284. return;
  285. }
  286. total_blocks += buffer_blk_cnt;
  287. }
  288. sparse_put_data_buffer(buffer);
  289. }
  290. debug("Wrote %d blocks, skipped %d, expected to write %d blocks\n",
  291. total_blocks, skipped, sparse_header->total_blks);
  292. printf("........ wrote %d blocks to '%s'\n", total_blocks, part_name);
  293. if (total_blocks != sparse_header->total_blks)
  294. fastboot_fail("sparse image write failure");
  295. fastboot_okay("");
  296. return;
  297. }