blob: 9632c6f352942029232ebdfb24ac086a08e02568 [file] [log] [blame]
Steve Rae2c724042016-06-07 11:19:38 -07001
Steve Raeb4e4bbe2014-09-03 10:05:51 -07002/*
3 * Copyright (c) 2009, Google Inc.
4 * All rights reserved.
5 *
6 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
Steve Raee6ca1ad2014-09-03 10:05:54 -07007 * Portions Copyright 2014 Broadcom Corporation.
Steve Raeb4e4bbe2014-09-03 10:05:51 -07008 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * * Neither the name of The Linux Foundation nor
17 * the names of its contributors may be used to endorse or promote
18 * products derived from this software without specific prior written
19 * permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
Steve Rae1c39d852014-09-03 10:05:53 -070033 * NOTE:
34 * Although it is very similar, this license text is not identical
35 * to the "BSD-3-Clause", therefore, DO NOT MODIFY THIS LICENSE TEXT!
Steve Raeb4e4bbe2014-09-03 10:05:51 -070036 */
37
Steve Raee6ca1ad2014-09-03 10:05:54 -070038#include <config.h>
39#include <common.h>
Maxime Ripard3d4ef382015-10-15 14:34:19 +020040#include <image-sparse.h>
Steve Raecc0f08cd2016-06-07 11:19:36 -070041#include <div64.h>
Steve Raee6ca1ad2014-09-03 10:05:54 -070042#include <malloc.h>
43#include <part.h>
44#include <sparse_format.h>
Steve Raecc0f08cd2016-06-07 11:19:36 -070045#include <fastboot.h>
Steve Raee6ca1ad2014-09-03 10:05:54 -070046
Maxime Ripard40aeeda2015-10-15 14:34:12 +020047#include <linux/math64.h>
48
Steve Raecc0f08cd2016-06-07 11:19:36 -070049void write_sparse_image(
50 struct sparse_storage *info, const char *part_name,
Steve Rae9bc34792016-06-07 11:19:37 -070051 void *data, unsigned sz)
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020052{
Steve Raecc0f08cd2016-06-07 11:19:36 -070053 lbaint_t blk;
54 lbaint_t blkcnt;
55 lbaint_t blks;
56 uint32_t bytes_written = 0;
57 unsigned int chunk;
58 unsigned int offset;
59 unsigned int chunk_data_sz;
60 uint32_t *fill_buf = NULL;
61 uint32_t fill_val;
62 sparse_header_t *sparse_header;
63 chunk_header_t *chunk_header;
64 uint32_t total_blocks = 0;
65 int i;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +020066
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020067 /* Read and skip over sparse image header */
Steve Raecc0f08cd2016-06-07 11:19:36 -070068 sparse_header = (sparse_header_t *)data;
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020069
Steve Raecc0f08cd2016-06-07 11:19:36 -070070 data += sparse_header->file_hdr_sz;
71 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
72 /*
73 * Skip the remaining bytes in a header that is longer than
74 * we expected.
75 */
76 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
77 }
Maxime Ripardbb83c0f2015-10-15 14:34:10 +020078
79 debug("=== Sparse Image Header ===\n");
80 debug("magic: 0x%x\n", sparse_header->magic);
81 debug("major_version: 0x%x\n", sparse_header->major_version);
82 debug("minor_version: 0x%x\n", sparse_header->minor_version);
83 debug("file_hdr_sz: %d\n", sparse_header->file_hdr_sz);
84 debug("chunk_hdr_sz: %d\n", sparse_header->chunk_hdr_sz);
85 debug("blk_sz: %d\n", sparse_header->blk_sz);
86 debug("total_blks: %d\n", sparse_header->total_blks);
87 debug("total_chunks: %d\n", sparse_header->total_chunks);
88
Maxime Ripard40aeeda2015-10-15 14:34:12 +020089 /*
90 * Verify that the sparse block size is a multiple of our
91 * storage backend block size
92 */
Steve Raecc0f08cd2016-06-07 11:19:36 -070093 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
Maxime Ripard40aeeda2015-10-15 14:34:12 +020094 if (offset) {
Steve Raee6ca1ad2014-09-03 10:05:54 -070095 printf("%s: Sparse image block size issue [%u]\n",
96 __func__, sparse_header->blk_sz);
Steve Rae9bc34792016-06-07 11:19:37 -070097 fastboot_fail("sparse image block size issue");
Steve Raecc0f08cd2016-06-07 11:19:36 -070098 return;
Steve Raee6ca1ad2014-09-03 10:05:54 -070099 }
100
Steve Rae64ece842016-06-07 11:19:35 -0700101 puts("Flashing Sparse Image\n");
Steve Raee6ca1ad2014-09-03 10:05:54 -0700102
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700103 /* Start processing chunks */
Steve Raecc0f08cd2016-06-07 11:19:36 -0700104 blk = info->start;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200105 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
Steve Raecc0f08cd2016-06-07 11:19:36 -0700106 /* Read and skip over chunk header */
107 chunk_header = (chunk_header_t *)data;
108 data += sizeof(chunk_header_t);
Maxime Riparda5d1e042015-10-15 14:34:14 +0200109
Steve Raecc0f08cd2016-06-07 11:19:36 -0700110 if (chunk_header->chunk_type != CHUNK_TYPE_RAW) {
111 debug("=== Chunk Header ===\n");
112 debug("chunk_type: 0x%x\n", chunk_header->chunk_type);
113 debug("chunk_data_sz: 0x%x\n", chunk_header->chunk_sz);
114 debug("total_size: 0x%x\n", chunk_header->total_sz);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700115 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200116
Steve Raecc0f08cd2016-06-07 11:19:36 -0700117 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
118 /*
119 * Skip the remaining bytes in a header that is longer
120 * than we expected.
121 */
122 data += (sparse_header->chunk_hdr_sz -
123 sizeof(chunk_header_t));
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200124 }
125
Steve Raecc0f08cd2016-06-07 11:19:36 -0700126 chunk_data_sz = sparse_header->blk_sz * chunk_header->chunk_sz;
127 blkcnt = chunk_data_sz / info->blksz;
128 switch (chunk_header->chunk_type) {
129 case CHUNK_TYPE_RAW:
130 if (chunk_header->total_sz !=
131 (sparse_header->chunk_hdr_sz + chunk_data_sz)) {
Steve Rae9bc34792016-06-07 11:19:37 -0700132 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700133 "Bogus chunk size for chunk type Raw");
134 return;
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200135 }
136
Steve Raecc0f08cd2016-06-07 11:19:36 -0700137 if (blk + blkcnt > info->start + info->size) {
138 printf(
139 "%s: Request would exceed partition size!\n",
140 __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700141 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700142 "Request would exceed partition size!");
143 return;
144 }
Maxime Ripard7bfc3b12015-10-15 14:34:11 +0200145
Steve Raecc0f08cd2016-06-07 11:19:36 -0700146 blks = info->write(info, blk, blkcnt, data);
147 /* blks might be > blkcnt (eg. NAND bad-blocks) */
148 if (blks < blkcnt) {
149 printf("%s: %s" LBAFU " [" LBAFU "]\n",
150 __func__, "Write failed, block #",
151 blk, blks);
Steve Rae9bc34792016-06-07 11:19:37 -0700152 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700153 "flash write failure");
154 return;
155 }
156 blk += blks;
157 bytes_written += blkcnt * info->blksz;
158 total_blocks += chunk_header->chunk_sz;
159 data += chunk_data_sz;
160 break;
161
162 case CHUNK_TYPE_FILL:
163 if (chunk_header->total_sz !=
164 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
Steve Rae9bc34792016-06-07 11:19:37 -0700165 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700166 "Bogus chunk size for chunk type FILL");
167 return;
168 }
169
170 fill_buf = (uint32_t *)
171 memalign(ARCH_DMA_MINALIGN,
172 ROUNDUP(info->blksz,
173 ARCH_DMA_MINALIGN));
174 if (!fill_buf) {
Steve Rae9bc34792016-06-07 11:19:37 -0700175 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700176 "Malloc failed for: CHUNK_TYPE_FILL");
177 return;
178 }
179
180 fill_val = *(uint32_t *)data;
181 data = (char *)data + sizeof(uint32_t);
182
183 for (i = 0; i < (info->blksz / sizeof(fill_val)); i++)
184 fill_buf[i] = fill_val;
185
186 if (blk + blkcnt > info->start + info->size) {
187 printf(
188 "%s: Request would exceed partition size!\n",
189 __func__);
Steve Rae9bc34792016-06-07 11:19:37 -0700190 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700191 "Request would exceed partition size!");
192 return;
193 }
194
195 for (i = 0; i < blkcnt; i++) {
196 blks = info->write(info, blk, 1, fill_buf);
197 /* blks might be > 1 (eg. NAND bad-blocks) */
198 if (blks < 1) {
199 printf("%s: %s, block # " LBAFU "\n",
200 __func__, "Write failed", blk);
Steve Rae9bc34792016-06-07 11:19:37 -0700201 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700202 "flash write failure");
203 free(fill_buf);
204 return;
205 }
206 blk += blks;
207 }
208 bytes_written += blkcnt * info->blksz;
209 total_blocks += chunk_data_sz / sparse_header->blk_sz;
210 free(fill_buf);
211 break;
212
213 case CHUNK_TYPE_DONT_CARE:
Steve Rae2c724042016-06-07 11:19:38 -0700214 blk += info->reserve(info, blk, blkcnt);
Steve Raecc0f08cd2016-06-07 11:19:36 -0700215 total_blocks += chunk_header->chunk_sz;
Steve Raecc0f08cd2016-06-07 11:19:36 -0700216 break;
217
218 case CHUNK_TYPE_CRC32:
219 if (chunk_header->total_sz !=
220 sparse_header->chunk_hdr_sz) {
Steve Rae9bc34792016-06-07 11:19:37 -0700221 fastboot_fail(
Steve Raecc0f08cd2016-06-07 11:19:36 -0700222 "Bogus chunk size for chunk type Dont Care");
223 return;
224 }
225 total_blocks += chunk_header->chunk_sz;
226 data += chunk_data_sz;
227 break;
228
229 default:
230 printf("%s: Unknown chunk type: %x\n", __func__,
231 chunk_header->chunk_type);
Steve Rae9bc34792016-06-07 11:19:37 -0700232 fastboot_fail("Unknown chunk type");
Steve Raecc0f08cd2016-06-07 11:19:36 -0700233 return;
234 }
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700235 }
236
Steve Raee3793542016-02-09 11:19:11 -0800237 debug("Wrote %d blocks, expected to write %d blocks\n",
Steve Raecc0f08cd2016-06-07 11:19:36 -0700238 total_blocks, sparse_header->total_blks);
239 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700240
Steve Raecc0f08cd2016-06-07 11:19:36 -0700241 if (total_blocks != sparse_header->total_blks)
Steve Rae9bc34792016-06-07 11:19:37 -0700242 fastboot_fail("sparse image write failure");
Steve Raecc0f08cd2016-06-07 11:19:36 -0700243 else
Steve Rae9bc34792016-06-07 11:19:37 -0700244 fastboot_okay("");
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700245
Steve Raecc0f08cd2016-06-07 11:19:36 -0700246 return;
Steve Raeb4e4bbe2014-09-03 10:05:51 -0700247}