blob: ddf5772cf825ca6ae2f74542491b5e0ae9e76235 [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>
Maxime Ripard6af49bc2015-10-15 14:34:19 +020039#include <image-sparse.h>
Steve Rae8cf58db2016-06-07 11:19:36 -070040#include <div64.h>
Steve Rae82fce732014-09-03 10:05:54 -070041#include <malloc.h>
42#include <part.h>
43#include <sparse_format.h>
Steve Rae8cf58db2016-06-07 11:19:36 -070044#include <fastboot.h>
Steve Rae82fce732014-09-03 10:05:54 -070045
Maxime Ripard61715e72015-10-15 14:34:12 +020046#include <linux/math64.h>
47
Steve Rae7765cf52016-06-07 11:19:39 -070048#ifndef CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE
49#define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512)
50#endif
51
Steve Rae8cf58db2016-06-07 11:19:36 -070052void write_sparse_image(
53 struct sparse_storage *info, const char *part_name,
Steve Rae76336b22016-06-07 11:19:37 -070054 void *data, unsigned sz)
Maxime Ripard93c56102015-10-15 14:34:11 +020055{
Steve Rae8cf58db2016-06-07 11:19:36 -070056 lbaint_t blk;
57 lbaint_t blkcnt;
58 lbaint_t blks;
59 uint32_t bytes_written = 0;
60 unsigned int chunk;
61 unsigned int offset;
62 unsigned int chunk_data_sz;
63 uint32_t *fill_buf = NULL;
64 uint32_t fill_val;
65 sparse_header_t *sparse_header;
66 chunk_header_t *chunk_header;
67 uint32_t total_blocks = 0;
Steve Rae7765cf52016-06-07 11:19:39 -070068 int fill_buf_num_blks;
Steve Rae8cf58db2016-06-07 11:19:36 -070069 int i;
Steve Rae7765cf52016-06-07 11:19:39 -070070 int j;
71
72 fill_buf_num_blks = CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE / info->blksz;
Maxime Ripard93c56102015-10-15 14:34:11 +020073
Maxime Ripardaae96742015-10-15 14:34:10 +020074 /* Read and skip over sparse image header */
Steve Rae8cf58db2016-06-07 11:19:36 -070075 sparse_header = (sparse_header_t *)data;
Maxime Ripardaae96742015-10-15 14:34:10 +020076
Steve Rae8cf58db2016-06-07 11:19:36 -070077 data += sparse_header->file_hdr_sz;
78 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
79 /*
80 * Skip the remaining bytes in a header that is longer than
81 * we expected.
82 */
83 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
84 }
Maxime Ripardaae96742015-10-15 14:34:10 +020085
86 debug("=== Sparse Image Header ===\n");
87 debug("magic: 0x%x\n", sparse_header->magic);
88 debug("major_version: 0x%x\n", sparse_header->major_version);
89 debug("minor_version: 0x%x\n", sparse_header->minor_version);
90 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
91 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
92 debug("blk_sz: %d\n", sparse_header->blk_sz);
93 debug("total_blks: %d\n", sparse_header->total_blks);
94 debug("total_chunks: %d\n", sparse_header->total_chunks);
95
Maxime Ripard93c56102015-10-15 14:34:11 +020096 /*
Maxime Ripard61715e72015-10-15 14:34:12 +020097 * Verify that the sparse block size is a multiple of our
98 * storage backend block size
99 */
Steve Rae8cf58db2016-06-07 11:19:36 -0700100 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
Maxime Ripard61715e72015-10-15 14:34:12 +0200101 if (offset) {
Steve Rae82fce732014-09-03 10:05:54 -0700102 printf("%s: Sparse image block size issue [%u]\n",
103 __func__, sparse_header->blk_sz);
Steve Rae76336b22016-06-07 11:19:37 -0700104 fastboot_fail("sparse image block size issue");
Steve Rae8cf58db2016-06-07 11:19:36 -0700105 return;
Steve Rae82fce732014-09-03 10:05:54 -0700106 }
107
Steve Raef1f112f2016-06-07 11:19:35 -0700108 puts("Flashing Sparse Image\n");
Steve Rae82fce732014-09-03 10:05:54 -0700109
Steve Rae7b86dff2014-09-03 10:05:51 -0700110 /* Start processing chunks */
Steve Rae8cf58db2016-06-07 11:19:36 -0700111 blk = info->start;
Maxime Ripard93c56102015-10-15 14:34:11 +0200112 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Steve Rae8cf58db2016-06-07 11:19:36 -0700113 /* Read and skip over chunk header */
114 chunk_header = (chunk_header_t *)data;
115 data += sizeof(chunk_header_t);
Maxime Riparda58e9c72015-10-15 14:34:14 +0200116
Steve Rae8cf58db2016-06-07 11:19:36 -0700117 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
118 debug("=== Chunk Header ===\n");
119 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
120 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
121 debug("total_size: 0x%x\n", chunk_header->total_sz);
Steve Rae82fce732014-09-03 10:05:54 -0700122 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700123
Steve Rae8cf58db2016-06-07 11:19:36 -0700124 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
125 /*
126 * Skip the remaining bytes in a header that is longer
127 * than we expected.
128 */
129 data += (sparse_header->chunk_hdr_sz -
130 sizeof(chunk_header_t));
Steve Rae7b86dff2014-09-03 10:05:51 -0700131 }
132
Steve Rae8cf58db2016-06-07 11:19:36 -0700133 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
134 blkcnt = chunk_data_sz / info->blksz;
135 switch (chunk_header->chunk_type) {
136 case CHUNK_TYPE_RAW:
137 if (chunk_header->total_sz !=
138 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
Steve Rae76336b22016-06-07 11:19:37 -0700139 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700140 "Bogus chunk size for chunk type Raw");
141 return;
142 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700143
Steve Rae8cf58db2016-06-07 11:19:36 -0700144 if (blk + blkcnt > info->start + info->size) {
145 printf(
146 "%s: Request would exceed partition size!\n",
147 __func__);
Steve Rae76336b22016-06-07 11:19:37 -0700148 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700149 "Request would exceed partition size!");
150 return;
151 }
Steve Rae82fce732014-09-03 10:05:54 -0700152
Steve Rae8cf58db2016-06-07 11:19:36 -0700153 blks = info->write(info, blk, blkcnt, data);
154 /* blks might be > blkcnt (eg. NAND bad-blocks) */
155 if (blks < blkcnt) {
156 printf("%s: %s" LBAFU " [" LBAFU "]\n",
157 __func__, "Write failed, block #",
158 blk, blks);
Steve Rae76336b22016-06-07 11:19:37 -0700159 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700160 "flash write failure");
161 return;
162 }
163 blk += blks;
164 bytes_written += blkcnt * info->blksz;
165 total_blocks += chunk_header->chunk_sz;
166 data += chunk_data_sz;
167 break;
Steve Rae7b86dff2014-09-03 10:05:51 -0700168
Steve Rae8cf58db2016-06-07 11:19:36 -0700169 case CHUNK_TYPE_FILL:
170 if (chunk_header->total_sz !=
171 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
Steve Rae76336b22016-06-07 11:19:37 -0700172 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700173 "Bogus chunk size for chunk type FILL");
174 return;
175 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700176
Steve Rae8cf58db2016-06-07 11:19:36 -0700177 fill_buf = (uint32_t *)
178 memalign(ARCH_DMA_MINALIGN,
Steve Rae7765cf52016-06-07 11:19:39 -0700179 ROUNDUP(
180 info->blksz * fill_buf_num_blks,
181 ARCH_DMA_MINALIGN));
Steve Rae8cf58db2016-06-07 11:19:36 -0700182 if (!fill_buf) {
Steve Rae76336b22016-06-07 11:19:37 -0700183 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700184 "Malloc failed for: CHUNK_TYPE_FILL");
185 return;
186 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700187
Steve Rae8cf58db2016-06-07 11:19:36 -0700188 fill_val = *(uint32_t *)data;
189 data = (char *)data + sizeof(uint32_t);
190
Steve Rae7765cf52016-06-07 11:19:39 -0700191 for (i = 0;
192 i < (info->blksz * fill_buf_num_blks /
193 sizeof(fill_val));
194 i++)
Steve Rae8cf58db2016-06-07 11:19:36 -0700195 fill_buf[i] = fill_val;
196
197 if (blk + blkcnt > info->start + info->size) {
198 printf(
199 "%s: Request would exceed partition size!\n",
200 __func__);
Steve Rae76336b22016-06-07 11:19:37 -0700201 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700202 "Request would exceed partition size!");
203 return;
Steve Rae7b86dff2014-09-03 10:05:51 -0700204 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700205
Steve Rae7765cf52016-06-07 11:19:39 -0700206 for (i = 0; i < blkcnt;) {
207 j = blkcnt - i;
208 if (j > fill_buf_num_blks)
209 j = fill_buf_num_blks;
210 blks = info->write(info, blk, j, fill_buf);
211 /* blks might be > j (eg. NAND bad-blocks) */
212 if (blks < j) {
213 printf("%s: %s " LBAFU " [%d]\n",
214 __func__,
215 "Write failed, block #",
216 blk, j);
Steve Rae76336b22016-06-07 11:19:37 -0700217 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700218 "flash write failure");
219 free(fill_buf);
220 return;
221 }
222 blk += blks;
Steve Rae7765cf52016-06-07 11:19:39 -0700223 i += j;
Steve Rae8cf58db2016-06-07 11:19:36 -0700224 }
225 bytes_written += blkcnt * info->blksz;
226 total_blocks += chunk_data_sz / sparse_header->blk_sz;
227 free(fill_buf);
228 break;
Maxime Ripard93c56102015-10-15 14:34:11 +0200229
Steve Rae8cf58db2016-06-07 11:19:36 -0700230 case CHUNK_TYPE_DONT_CARE:
Steve Rae8a99be82016-06-07 11:19:38 -0700231 blk += info->reserve(info, blk, blkcnt);
Steve Rae8cf58db2016-06-07 11:19:36 -0700232 total_blocks += chunk_header->chunk_sz;
Steve Rae8cf58db2016-06-07 11:19:36 -0700233 break;
234
235 case CHUNK_TYPE_CRC32:
236 if (chunk_header->total_sz !=
237 sparse_header->chunk_hdr_sz) {
Steve Rae76336b22016-06-07 11:19:37 -0700238 fastboot_fail(
Steve Rae8cf58db2016-06-07 11:19:36 -0700239 "Bogus chunk size for chunk type Dont Care");
240 return;
241 }
242 total_blocks += chunk_header->chunk_sz;
243 data += chunk_data_sz;
244 break;
245
246 default:
247 printf("%s: Unknown chunk type: %x\n", __func__,
248 chunk_header->chunk_type);
Steve Rae76336b22016-06-07 11:19:37 -0700249 fastboot_fail("Unknown chunk type");
Steve Rae8cf58db2016-06-07 11:19:36 -0700250 return;
251 }
Steve Rae7b86dff2014-09-03 10:05:51 -0700252 }
253
Steve Rae407f0f52016-02-09 11:19:11 -0800254 debug("Wrote %d blocks, expected to write %d blocks\n",
Steve Rae8cf58db2016-06-07 11:19:36 -0700255 total_blocks, sparse_header->total_blks);
256 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
Steve Rae7b86dff2014-09-03 10:05:51 -0700257
Steve Rae8cf58db2016-06-07 11:19:36 -0700258 if (total_blocks != sparse_header->total_blks)
Steve Rae76336b22016-06-07 11:19:37 -0700259 fastboot_fail("sparse image write failure");
Steve Rae8cf58db2016-06-07 11:19:36 -0700260 else
Steve Rae76336b22016-06-07 11:19:37 -0700261 fastboot_okay("");
Steve Rae7b86dff2014-09-03 10:05:51 -0700262
Steve Rae8cf58db2016-06-07 11:19:36 -0700263 return;
Steve Rae7b86dff2014-09-03 10:05:51 -0700264}