aboot.c 9.8 KB

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