blob: 187ac28cd364b7f9a9e0d28ee88451ab959ef547 [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>
49
Alex Kiernanc4ded032018-05-29 15:30:40 +000050static void default_log(const char *ignored, char *response) {}
Jassi Brar2f83f212018-04-06 12:05:09 +053051
52int write_sparse_image(struct sparse_storage *info,
Alex Kiernanc4ded032018-05-29 15:30:40 +000053 const char *part_name, void *data, char *response)
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020054{
Steve Raecc0f08cd2016-06-07 11:19:36 -070055 lbaint_t blk;
56 lbaint_t blkcnt;
57 lbaint_t blks;
58 uint32_t bytes_written = 0;
59 unsigned int chunk;
60 unsigned int offset;
61 unsigned int chunk_data_sz;
62 uint32_t *fill_buf = NULL;
63 uint32_t fill_val;
64 sparse_header_t *sparse_header;
65 chunk_header_t *chunk_header;
66 uint32_t total_blocks = 0;
Steve Rae0abd63b2016-06-07 11:19:39 -070067 int fill_buf_num_blks;
Steve Raecc0f08cd2016-06-07 11:19:36 -070068 int i;
Steve Rae0abd63b2016-06-07 11:19:39 -070069 int j;
70
Alex Kiernanc232d142018-05-29 15:30:52 +000071 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020072
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020073 /* Read and skip over sparse image header */
Steve Raecc0f08cd2016-06-07 11:19:36 -070074 sparse_header = (sparse_header_t *)data;
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020075
Steve Raecc0f08cd2016-06-07 11:19:36 -070076 data += sparse_header->file_hdr_sz;
77 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
78 /*
79 * Skip the remaining bytes in a header that is longer than
80 * we expected.
81 */
82 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
83 }
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020084
Jassi Brar2f83f212018-04-06 12:05:09 +053085 if (!info->mssg)
86 info->mssg = default_log;
87
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020088 debug("=== Sparse Image Header ===\n");
89 debug("magic: 0x%x\n", sparse_header->magic);
90 debug("major_version: 0x%x\n", sparse_header->major_version);
91 debug("minor_version: 0x%x\n", sparse_header->minor_version);
92 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
93 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
94 debug("blk_sz: %d\n", sparse_header->blk_sz);
95 debug("total_blks: %d\n", sparse_header->total_blks);
96 debug("total_chunks: %d\n", sparse_header->total_chunks);
97
Maxime Ripard40aeeda2015-10-15 14:34:12 +020098 /*
99 * Verify that the sparse block size is a multiple of our
100 * storage backend block size
101 */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700102 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
Maxime Ripard40aeeda2015-10-15 14:34:12 +0200103 if (offset) {
Steve Raee6ca1ad2014-09-03 10:05:54 -0700104 printf("%s: Sparse image block size issue [%u]\n",
105 __func__, sparse_header->blk_sz);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000106 info->mssg("sparse image block size issue", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530107 return -1;
Steve Raee6ca1ad2014-09-03 10:05:54 -0700108 }
109
Steve Rae64ece842016-06-07 11:19:35 -0700110 puts("Flashing Sparse Image\n");
Steve Raee6ca1ad2014-09-03 10:05:54 -0700111
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700112 /* Start processing chunks */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700113 blk = info->start;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200114 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Steve Raecc0f08cd2016-06-07 11:19:36 -0700115 /* Read and skip over chunk header */
116 chunk_header = (chunk_header_t *)data;
117 data += sizeof(chunk_header_t);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200118
Steve Raecc0f08cd2016-06-07 11:19:36 -0700119 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
120 debug("=== Chunk Header ===\n");
121 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
122 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
123 debug("total_size: 0x%x\n", chunk_header->total_sz);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700124 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200125
Steve Raecc0f08cd2016-06-07 11:19:36 -0700126 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
127 /*
128 * Skip the remaining bytes in a header that is longer
129 * than we expected.
130 */
131 data += (sparse_header->chunk_hdr_sz -
132 sizeof(chunk_header_t));
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200133 }
134
Steve Raecc0f08cd2016-06-07 11:19:36 -0700135 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
136 blkcnt = chunk_data_sz / info->blksz;
137 switch (chunk_header->chunk_type) {
138 case CHUNK_TYPE_RAW:
139 if (chunk_header->total_sz !=
140 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000141 info->mssg("Bogus chunk size for chunk type Raw",
142 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530143 return -1;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200144 }
145
Steve Raecc0f08cd2016-06-07 11:19:36 -0700146 if (blk + blkcnt > info->start + info->size) {
147 printf(
148 "%s: Request would exceed partition size!\n",
149 __func__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000150 info->mssg("Request would exceed partition size!",
151 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530152 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700153 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200154
Steve Raecc0f08cd2016-06-07 11:19:36 -0700155 blks = info->write(info, blk, blkcnt, data);
156 /* blks might be > blkcnt (eg. NAND bad-blocks) */
157 if (blks < blkcnt) {
158 printf("%s: %s" LBAFU " [" LBAFU "]\n",
159 __func__, "Write failed, block #",
160 blk, blks);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000161 info->mssg("flash write failure", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530162 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700163 }
164 blk += blks;
165 bytes_written += blkcnt * info->blksz;
166 total_blocks += chunk_header->chunk_sz;
167 data += chunk_data_sz;
168 break;
169
170 case CHUNK_TYPE_FILL:
171 if (chunk_header->total_sz !=
172 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000173 info->mssg("Bogus chunk size for chunk type FILL", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530174 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700175 }
176
177 fill_buf = (uint32_t *)
178 memalign(ARCH_DMA_MINALIGN,
Steve Rae0abd63b2016-06-07 11:19:39 -0700179 ROUNDUP(
180 info->blksz * fill_buf_num_blks,
181 ARCH_DMA_MINALIGN));
Steve Raecc0f08cd2016-06-07 11:19:36 -0700182 if (!fill_buf) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000183 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
184 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530185 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700186 }
187
188 fill_val = *(uint32_t *)data;
189 data = (char *)data + sizeof(uint32_t);
190
Steve Rae0abd63b2016-06-07 11:19:39 -0700191 for (i = 0;
192 i < (info->blksz * fill_buf_num_blks /
193 sizeof(fill_val));
194 i++)
Steve Raecc0f08cd2016-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__);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000201 info->mssg("Request would exceed partition size!",
202 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530203 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700204 }
205
Steve Rae0abd63b2016-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);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000217 info->mssg("flash write failure",
218 response);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700219 free(fill_buf);
Jassi Brar2f83f212018-04-06 12:05:09 +0530220 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700221 }
222 blk += blks;
Steve Rae0abd63b2016-06-07 11:19:39 -0700223 i += j;
Steve Raecc0f08cd2016-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;
229
230 case CHUNK_TYPE_DONT_CARE:
Steve Rae2c724042016-06-07 11:19:38 -0700231 blk += info->reserve(info, blk, blkcnt);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700232 total_blocks += chunk_header->chunk_sz;
Steve Raecc0f08cd2016-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) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000238 info->mssg("Bogus chunk size for chunk type Dont Care",
239 response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530240 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700241 }
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);
Alex Kiernanc4ded032018-05-29 15:30:40 +0000249 info->mssg("Unknown chunk type", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530250 return -1;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700251 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700252 }
253
Steve Raee3793542016-02-09 11:19:11 -0800254 debug("Wrote %d blocks, expected to write %d blocks\n",
Steve Raecc0f08cd2016-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 Raeb4e4bbe2014-09-03 10:05:51 -0700257
Jassi Brar2f83f212018-04-06 12:05:09 +0530258 if (total_blocks != sparse_header->total_blks) {
Alex Kiernanc4ded032018-05-29 15:30:40 +0000259 info->mssg("sparse image write failure", response);
Jassi Brar2f83f212018-04-06 12:05:09 +0530260 return -1;
261 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700262
Jassi Brar2f83f212018-04-06 12:05:09 +0530263 return 0;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700264}