blob: cd1b6a6ac8e000c7d3884143d0b05363e020e20e [file] [log] [blame]
Steve Rae7b86dff2014-09-03 10:05:51 -07001/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Steve Rae82fce732014-09-03 10:05:54 -07006 * Portions Copyright 2014 Broadcom Corporation.
Steve Rae7b86dff2014-09-03 10:05:51 -07007 *
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 *
Steve Raefabff732014-09-03 10:05:53 -070032 * 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!
Steve Rae7b86dff2014-09-03 10:05:51 -070035 */
36
Steve Rae82fce732014-09-03 10:05:54 -070037#include <config.h>
38#include <common.h>
39#include <aboot.h>
40#include <malloc.h>
41#include <part.h>
42#include <sparse_format.h>
43
Maxime Ripardaae96742015-10-15 14:34:10 +020044static sparse_header_t *sparse_parse_header(void **data)
45{
46 /* Read and skip over sparse image header */
47 sparse_header_t *sparse_header = (sparse_header_t *) *data;
48
49 *data += sparse_header->file_hdr_sz;
50
51 debug("=== Sparse Image Header ===\n");
52 debug("magic: 0x%x\n", sparse_header->magic);
53 debug("major_version: 0x%x\n", sparse_header->major_version);
54 debug("minor_version: 0x%x\n", sparse_header->minor_version);
55 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
56 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
57 debug("blk_sz: %d\n", sparse_header->blk_sz);
58 debug("total_blks: %d\n", sparse_header->total_blks);
59 debug("total_chunks: %d\n", sparse_header->total_chunks);
60
61 return sparse_header;
62}
63
Steve Rae82fce732014-09-03 10:05:54 -070064void write_sparse_image(block_dev_desc_t *dev_desc,
65 disk_partition_t *info, const char *part_name,
66 void *data, unsigned sz)
Steve Rae7b86dff2014-09-03 10:05:51 -070067{
Steve Rae82fce732014-09-03 10:05:54 -070068 lbaint_t blk;
69 lbaint_t blkcnt;
70 lbaint_t blks;
71 uint32_t bytes_written = 0;
Steve Rae7b86dff2014-09-03 10:05:51 -070072 unsigned int chunk;
73 unsigned int chunk_data_sz;
74 uint32_t *fill_buf = NULL;
75 uint32_t fill_val;
Steve Rae7b86dff2014-09-03 10:05:51 -070076 sparse_header_t *sparse_header;
77 chunk_header_t *chunk_header;
78 uint32_t total_blocks = 0;
Steve Rae7b86dff2014-09-03 10:05:51 -070079 int i;
Steve Rae7b86dff2014-09-03 10:05:51 -070080
Maxime Ripardaae96742015-10-15 14:34:10 +020081 sparse_header = sparse_parse_header(&data);
82 if (!sparse_header) {
83 fastboot_fail("sparse header issue\n");
84 return;
Steve Rae7b86dff2014-09-03 10:05:51 -070085 }
86
Steve Rae82fce732014-09-03 10:05:54 -070087 /* verify sparse_header->blk_sz is an exact multiple of info->blksz */
88 if (sparse_header->blk_sz !=
89 (sparse_header->blk_sz & ~(info->blksz - 1))) {
90 printf("%s: Sparse image block size issue [%u]\n",
91 __func__, sparse_header->blk_sz);
92 fastboot_fail("sparse image block size issue");
93 return;
94 }
95
96 puts("Flashing Sparse Image\n");
97
Steve Rae7b86dff2014-09-03 10:05:51 -070098 /* Start processing chunks */
Steve Rae82fce732014-09-03 10:05:54 -070099 blk = info->start;
Steve Rae7b86dff2014-09-03 10:05:51 -0700100 for (chunk=0; chunk<sparse_header->total_chunks; chunk++)
101 {
102 /* Read and skip over chunk header */
103 chunk_header = (chunk_header_t *) data;
104 data += sizeof(chunk_header_t);
105
Steve Rae82fce732014-09-03 10:05:54 -0700106 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
107 debug("=== Chunk Header ===\n");
108 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
109 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
110 debug("total_size: 0x%x\n", chunk_header->total_sz);
111 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700112
Steve Raefabff732014-09-03 10:05:53 -0700113 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t))
Steve Rae7b86dff2014-09-03 10:05:51 -0700114 {
Steve Raefabff732014-09-03 10:05:53 -0700115 /*
116 * Skip the remaining bytes in a header that is longer
117 * than we expected.
Steve Rae7b86dff2014-09-03 10:05:51 -0700118 */
Steve Raefabff732014-09-03 10:05:53 -0700119 data += (sparse_header->chunk_hdr_sz -
120 sizeof(chunk_header_t));
Steve Rae7b86dff2014-09-03 10:05:51 -0700121 }
122
123 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
Steve Rae82fce732014-09-03 10:05:54 -0700124 blkcnt = chunk_data_sz / info->blksz;
Steve Rae7b86dff2014-09-03 10:05:51 -0700125 switch (chunk_header->chunk_type)
126 {
127 case CHUNK_TYPE_RAW:
Steve Raefabff732014-09-03 10:05:53 -0700128 if (chunk_header->total_sz !=
129 (sparse_header->chunk_hdr_sz + chunk_data_sz))
Steve Rae7b86dff2014-09-03 10:05:51 -0700130 {
Steve Raefabff732014-09-03 10:05:53 -0700131 fastboot_fail(
132 "Bogus chunk size for chunk type Raw");
Steve Rae7b86dff2014-09-03 10:05:51 -0700133 return;
134 }
135
Steve Rae82fce732014-09-03 10:05:54 -0700136 if (blk + blkcnt > info->start + info->size) {
137 printf(
138 "%s: Request would exceed partition size!\n",
139 __func__);
140 fastboot_fail(
141 "Request would exceed partition size!");
142 return;
143 }
144
145 blks = dev_desc->block_write(dev_desc->dev, blk, blkcnt,
146 data);
147 if (blks != blkcnt) {
148 printf("%s: Write failed " LBAFU "\n",
149 __func__, blks);
Steve Rae7b86dff2014-09-03 10:05:51 -0700150 fastboot_fail("flash write failure");
151 return;
152 }
Steve Rae82fce732014-09-03 10:05:54 -0700153 blk += blkcnt;
154 bytes_written += blkcnt * info->blksz;
Steve Rae7b86dff2014-09-03 10:05:51 -0700155 total_blocks += chunk_header->chunk_sz;
156 data += chunk_data_sz;
157 break;
158
159 case CHUNK_TYPE_FILL:
Steve Raefabff732014-09-03 10:05:53 -0700160 if (chunk_header->total_sz !=
161 (sparse_header->chunk_hdr_sz + sizeof(uint32_t)))
Steve Rae7b86dff2014-09-03 10:05:51 -0700162 {
Steve Raefabff732014-09-03 10:05:53 -0700163 fastboot_fail(
164 "Bogus chunk size for chunk type FILL");
Steve Rae7b86dff2014-09-03 10:05:51 -0700165 return;
166 }
167
Steve Raefabff732014-09-03 10:05:53 -0700168 fill_buf = (uint32_t *)
Steve Rae82fce732014-09-03 10:05:54 -0700169 memalign(ARCH_DMA_MINALIGN,
170 ROUNDUP(info->blksz,
171 ARCH_DMA_MINALIGN));
Steve Rae7b86dff2014-09-03 10:05:51 -0700172 if (!fill_buf)
173 {
Steve Raefabff732014-09-03 10:05:53 -0700174 fastboot_fail(
175 "Malloc failed for: CHUNK_TYPE_FILL");
Steve Rae7b86dff2014-09-03 10:05:51 -0700176 return;
177 }
178
179 fill_val = *(uint32_t *)data;
180 data = (char *) data + sizeof(uint32_t);
Steve Rae7b86dff2014-09-03 10:05:51 -0700181
Steve Rae82fce732014-09-03 10:05:54 -0700182 for (i = 0; i < (info->blksz / sizeof(fill_val)); i++)
Steve Rae7b86dff2014-09-03 10:05:51 -0700183 fill_buf[i] = fill_val;
Steve Rae82fce732014-09-03 10:05:54 -0700184
185 if (blk + blkcnt > info->start + info->size) {
186 printf(
187 "%s: Request would exceed partition size!\n",
188 __func__);
189 fastboot_fail(
190 "Request would exceed partition size!");
191 return;
Steve Rae7b86dff2014-09-03 10:05:51 -0700192 }
193
Steve Rae82fce732014-09-03 10:05:54 -0700194 for (i = 0; i < blkcnt; i++) {
195 blks = dev_desc->block_write(dev_desc->dev,
196 blk, 1, fill_buf);
197 if (blks != 1) {
198 printf(
199 "%s: Write failed, block # " LBAFU "\n",
200 __func__, blkcnt);
Steve Rae7b86dff2014-09-03 10:05:51 -0700201 fastboot_fail("flash write failure");
202 free(fill_buf);
203 return;
204 }
Steve Rae82fce732014-09-03 10:05:54 -0700205 blk++;
Steve Rae7b86dff2014-09-03 10:05:51 -0700206 }
Steve Rae82fce732014-09-03 10:05:54 -0700207 bytes_written += blkcnt * info->blksz;
208 total_blocks += chunk_data_sz / sparse_header->blk_sz;
Steve Rae7b86dff2014-09-03 10:05:51 -0700209
210 free(fill_buf);
211 break;
212
213 case CHUNK_TYPE_DONT_CARE:
Rob Herring9b4aa242014-10-16 11:19:36 +0800214 blk += blkcnt;
Steve Rae7b86dff2014-09-03 10:05:51 -0700215 total_blocks += chunk_header->chunk_sz;
216 break;
217
Steve Rae82fce732014-09-03 10:05:54 -0700218 case CHUNK_TYPE_CRC32:
Steve Raefabff732014-09-03 10:05:53 -0700219 if (chunk_header->total_sz !=
220 sparse_header->chunk_hdr_sz)
Steve Rae7b86dff2014-09-03 10:05:51 -0700221 {
Steve Raefabff732014-09-03 10:05:53 -0700222 fastboot_fail(
223 "Bogus chunk size for chunk type Dont Care");
Steve Rae7b86dff2014-09-03 10:05:51 -0700224 return;
225 }
226 total_blocks += chunk_header->chunk_sz;
227 data += chunk_data_sz;
228 break;
229
230 default:
Steve Rae82fce732014-09-03 10:05:54 -0700231 printf("%s: Unknown chunk type: %x\n", __func__,
232 chunk_header->chunk_type);
Steve Rae7b86dff2014-09-03 10:05:51 -0700233 fastboot_fail("Unknown chunk type");
234 return;
235 }
236 }
237
Steve Raefabff732014-09-03 10:05:53 -0700238 debug("Wrote %d blocks, expected to write %d blocks\n",
239 total_blocks, sparse_header->total_blks);
Steve Rae82fce732014-09-03 10:05:54 -0700240 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
Steve Rae7b86dff2014-09-03 10:05:51 -0700241
Steve Raefabff732014-09-03 10:05:53 -0700242 if (total_blocks != sparse_header->total_blks)
Steve Rae7b86dff2014-09-03 10:05:51 -0700243 fastboot_fail("sparse image write failure");
Steve Rae7b86dff2014-09-03 10:05:51 -0700244
Steve Rae7b86dff2014-09-03 10:05:51 -0700245 fastboot_okay("");
Steve Rae7b86dff2014-09-03 10:05:51 -0700246 return;
247}