blob: 8f8a67e158044d37909822252f67c4a1f716ecd1 [file] [log] [blame]
Steve Raeb4e4bbe2014-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 Raee6ca1ad2014-09-03 10:05:54 -07006 * Portions Copyright 2014 Broadcom Corporation.
Steve Raeb4e4bbe2014-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 Rae1c39d852014-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 Raeb4e4bbe2014-09-03 10:05:51 -070035 */
36
Steve Raee6ca1ad2014-09-03 10:05:54 -070037#include <config.h>
38#include <common.h>
Simon Glasse6f6f9e2020-05-10 11:39:58 -060039#include <blk.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020040#include <image-sparse.h>
Steve Raecc0f08cd2016-06-07 11:19:36 -070041#include <div64.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060042#include <log.h>
Steve Raee6ca1ad2014-09-03 10:05:54 -070043#include <malloc.h>
44#include <part.h>
45#include <sparse_format.h>
Simon Glass90526e92020-05-10 11:39:56 -060046#include <asm/cache.h>
Steve Raee6ca1ad2014-09-03 10:05:54 -070047
Maxime Ripard40aeeda2015-10-15 14:34:12 +020048#include <linux/math64.h>
qianfan Zhao62649162021-11-16 09:35:38 +080049#include <linux/err.h>
Maxime Ripard40aeeda2015-10-15 14:34:12 +020050
Alex Kiernanc4ded032018-05-29 15:30:40 +000051static void default_log(const char *ignored, char *response) {}
Jassi Brar2f83f212018-04-06 12:05:09 +053052
qianfan Zhao62649162021-11-16 09:35:38 +080053static lbaint_t write_sparse_chunk_raw(struct sparse_storage *info,
54 lbaint_t blk, lbaint_t blkcnt,
55 void *data,
56 char *response)
57{
Mattijs Korpershoekb1aade82023-07-07 10:13:34 +020058 lbaint_t n = blkcnt, write_blks, blks = 0;
59 lbaint_t aligned_buf_blks = FASTBOOT_MAX_BLK_WRITE;
qianfan Zhao62649162021-11-16 09:35:38 +080060 uint32_t *aligned_buf = NULL;
61
62 if (CONFIG_IS_ENABLED(SYS_DCACHE_OFF)) {
63 write_blks = info->write(info, blk, n, data);
64 if (write_blks < n)
65 goto write_fail;
66
67 return write_blks;
68 }
69
70 aligned_buf = memalign(ARCH_DMA_MINALIGN, info->blksz * aligned_buf_blks);
71 if (!aligned_buf) {
72 info->mssg("Malloc failed for: CHUNK_TYPE_RAW", response);
73 return -ENOMEM;
74 }
75
76 while (blkcnt > 0) {
77 n = min(aligned_buf_blks, blkcnt);
78 memcpy(aligned_buf, data, n * info->blksz);
79
80 /* write_blks might be > n due to NAND bad-blocks */
81 write_blks = info->write(info, blk + blks, n, aligned_buf);
82 if (write_blks < n) {
83 free(aligned_buf);
84 goto write_fail;
85 }
86
87 blks += write_blks;
88 data += n * info->blksz;
89 blkcnt -= n;
90 }
91
92 free(aligned_buf);
93 return blks;
94
95write_fail:
96 if (IS_ERR_VALUE(write_blks)) {
97 printf("%s: Write failed, block #" LBAFU " [" LBAFU "] (%lld)\n",
98 __func__, blk + blks, n, (long long)write_blks);
99 info->mssg("flash write failure", response);
100 return write_blks;
101 }
102
103 /* write_blks < n */
104 printf("%s: Write failed, block #" LBAFU " [" LBAFU "]\n",
105 __func__, blk + blks, n);
106 info->mssg("flash write failure(incomplete)", response);
107 return -1;
108}
109
Jassi Brar2f83f212018-04-06 12:05:09 +0530110int write_sparse_image(struct sparse_storage *info,
Alex Kiernanc4ded032018-05-29 15:30:40 +0000111 const char *part_name, void *data, char *response)
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200112{
Steve Raecc0f08cd2016-06-07 11:19:36 -0700113 lbaint_t blk;
114 lbaint_t blkcnt;
115 lbaint_t blks;
Sean Anderson89be8e32021-05-27 12:02:34 -0400116 uint64_t bytes_written = 0;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700117 unsigned int chunk;
118 unsigned int offset;
Sean Anderson89be8e32021-05-27 12:02:34 -0400119 uint64_t chunk_data_sz;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700120 uint32_t *fill_buf = NULL;
121 uint32_t fill_val;
122 sparse_header_t *sparse_header;
123 chunk_header_t *chunk_header;
124 uint32_t total_blocks = 0;
Steve Rae0abd63b2016-06-07 11:19:39 -0700125 int fill_buf_num_blks;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700126 int i;
Steve Rae0abd63b2016-06-07 11:19:39 -0700127 int j;
128
Alex Kiernanc232d142018-05-29 15:30:52 +0000129 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200130
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200131 /* Read and skip over sparse image header */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700132 sparse_header = (sparse_header_t *)data;
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200133
Steve Raecc0f08cd2016-06-07 11:19:36 -0700134 data += sparse_header->file_hdr_sz;
135 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
136 /*
137 * Skip the remaining bytes in a header that is longer than
138 * we expected.
139 */
140 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
141 }
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200142
Jassi Brar2f83f212018-04-06 12:05:09 +0530143 if (!info->mssg)
144 info->mssg = default_log;
145
Maxime Ripardbb83c0f2015-10-15 14:34:10 +0200146 debug("=== Sparse Image Header ===\n");
147 debug("magic: 0x%x\n", sparse_header->magic);
148 debug("major_version: 0x%x\n", sparse_header->major_version);
149 debug("minor_version: 0x%x\n", sparse_header->minor_version);
150 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
151 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
152 debug("blk_sz: %d\n", sparse_header->blk_sz);
153 debug("total_blks: %d\n", sparse_header->total_blks);
154 debug("total_chunks: %d\n", sparse_header->total_chunks);
155
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200156 /*
157 * Verify that the sparse block size is a multiple of our
158 * storage backend block size
159 */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700160 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200161 if (offset) {
Steve Raee6ca1ad2014-09-03 10:05:54 -0700162 printf("%s: Sparse image block size issue [%u]\n",
163 __func__, sparse_header->blk_sz);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000164 info->mssg("sparse image block size issue", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530165 return -1;
Steve Raee6ca1ad2014-09-03 10:05:54 -0700166 }
167
Steve Rae64ece842016-06-07 11:19:35 -0700168 puts("Flashing Sparse Image\n");
Steve Raee6ca1ad2014-09-03 10:05:54 -0700169
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700170 /* Start processing chunks */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700171 blk = info->start;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200172 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Steve Raecc0f08cd2016-06-07 11:19:36 -0700173 /* Read and skip over chunk header */
174 chunk_header = (chunk_header_t *)data;
175 data += sizeof(chunk_header_t);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200176
Steve Raecc0f08cd2016-06-07 11:19:36 -0700177 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
178 debug("=== Chunk Header ===\n");
179 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
180 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
181 debug("total_size: 0x%x\n", chunk_header->total_sz);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700182 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200183
Steve Raecc0f08cd2016-06-07 11:19:36 -0700184 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
185 /*
186 * Skip the remaining bytes in a header that is longer
187 * than we expected.
188 */
189 data += (sparse_header->chunk_hdr_sz -
190 sizeof(chunk_header_t));
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200191 }
192
Sean Anderson89be8e32021-05-27 12:02:34 -0400193 chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
194 blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700195 switch (chunk_header->chunk_type) {
196 case CHUNK_TYPE_RAW:
197 if (chunk_header->total_sz !=
198 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000199 info->mssg("Bogus chunk size for chunk type Raw",
200 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530201 return -1;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200202 }
203
Steve Raecc0f08cd2016-06-07 11:19:36 -0700204 if (blk + blkcnt > info->start + info->size) {
205 printf(
206 "%s: Request would exceed partition size!\n",
207 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000208 info->mssg("Request would exceed partition size!",
209 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530210 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700211 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200212
qianfan Zhao62649162021-11-16 09:35:38 +0800213 blks = write_sparse_chunk_raw(info, blk, blkcnt,
214 data, response);
215 if (blks < 0)
Jassi Brar2f83f212018-04-06 12:05:09 +0530216 return -1;
qianfan Zhao62649162021-11-16 09:35:38 +0800217
Steve Raecc0f08cd2016-06-07 11:19:36 -0700218 blk += blks;
Sean Anderson89be8e32021-05-27 12:02:34 -0400219 bytes_written += ((u64)blkcnt) * info->blksz;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700220 total_blocks += chunk_header->chunk_sz;
221 data += chunk_data_sz;
222 break;
223
224 case CHUNK_TYPE_FILL:
225 if (chunk_header->total_sz !=
226 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000227 info->mssg("Bogus chunk size for chunk type FILL", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530228 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700229 }
230
231 fill_buf = (uint32_t *)
232 memalign(ARCH_DMA_MINALIGN,
Steve Rae0abd63b2016-06-07 11:19:39 -0700233 ROUNDUP(
234 info->blksz * fill_buf_num_blks,
235 ARCH_DMA_MINALIGN));
Steve Raecc0f08cd2016-06-07 11:19:36 -0700236 if (!fill_buf) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000237 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
238 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530239 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700240 }
241
242 fill_val = *(uint32_t *)data;
243 data = (char *)data + sizeof(uint32_t);
244
Steve Rae0abd63b2016-06-07 11:19:39 -0700245 for (i = 0;
246 i < (info->blksz * fill_buf_num_blks /
247 sizeof(fill_val));
248 i++)
Steve Raecc0f08cd2016-06-07 11:19:36 -0700249 fill_buf[i] = fill_val;
250
251 if (blk + blkcnt > info->start + info->size) {
252 printf(
253 "%s: Request would exceed partition size!\n",
254 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000255 info->mssg("Request would exceed partition size!",
256 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530257 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700258 }
259
Steve Rae0abd63b2016-06-07 11:19:39 -0700260 for (i = 0; i < blkcnt;) {
261 j = blkcnt - i;
262 if (j > fill_buf_num_blks)
263 j = fill_buf_num_blks;
264 blks = info->write(info, blk, j, fill_buf);
265 /* blks might be > j (eg. NAND bad-blocks) */
266 if (blks < j) {
267 printf("%s: %s " LBAFU " [%d]\n",
268 __func__,
269 "Write failed, block #",
270 blk, j);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000271 info->mssg("flash write failure",
272 response);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700273 free(fill_buf);
Jassi Brar2f83f212018-04-06 12:05:09 +0530274 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700275 }
276 blk += blks;
Steve Rae0abd63b2016-06-07 11:19:39 -0700277 i += j;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700278 }
Sean Anderson89be8e32021-05-27 12:02:34 -0400279 bytes_written += ((u64)blkcnt) * info->blksz;
280 total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz,
281 sparse_header->blk_sz);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700282 free(fill_buf);
283 break;
284
285 case CHUNK_TYPE_DONT_CARE:
Steve Rae2c724042016-06-07 11:19:38 -0700286 blk += info->reserve(info, blk, blkcnt);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700287 total_blocks += chunk_header->chunk_sz;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700288 break;
289
290 case CHUNK_TYPE_CRC32:
291 if (chunk_header->total_sz !=
292 sparse_header->chunk_hdr_sz) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000293 info->mssg("Bogus chunk size for chunk type Dont Care",
294 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530295 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700296 }
297 total_blocks += chunk_header->chunk_sz;
298 data += chunk_data_sz;
299 break;
300
301 default:
302 printf("%s: Unknown chunk type: %x\n", __func__,
303 chunk_header->chunk_type);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000304 info->mssg("Unknown chunk type", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530305 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700306 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700307 }
308
Steve Raee3793542016-02-09 11:19:11 -0800309 debug("Wrote %d blocks, expected to write %d blocks\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700310 total_blocks, sparse_header->total_blks);
Sean Anderson89be8e32021-05-27 12:02:34 -0400311 printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700312
Jassi Brar2f83f212018-04-06 12:05:09 +0530313 if (total_blocks != sparse_header->total_blks) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000314 info->mssg("sparse image write failure", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530315 return -1;
316 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700317
Jassi Brar2f83f212018-04-06 12:05:09 +0530318 return 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700319}