aboot.c 7.2 KB

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