]> Git Repo - J-u-boot.git/blame - lib/image-sparse.c
board: stm32mp1: Remove gpio_hog_probe_all() from board
[J-u-boot.git] / lib / image-sparse.c
CommitLineData
b4e4bbe5
SR
1/*
2 * Copyright (c) 2009, Google Inc.
3 * All rights reserved.
4 *
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
e6ca1ad6 6 * Portions Copyright 2014 Broadcom Corporation.
b4e4bbe5
SR
7 *
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 *
1c39d856
SR
32 * 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!
b4e4bbe5
SR
35 */
36
e6ca1ad6
SR
37#include <config.h>
38#include <common.h>
e6f6f9e6 39#include <blk.h>
3d4ef38d 40#include <image-sparse.h>
cc0f08cd 41#include <div64.h>
f7ae49fc 42#include <log.h>
e6ca1ad6
SR
43#include <malloc.h>
44#include <part.h>
45#include <sparse_format.h>
90526e9f 46#include <asm/cache.h>
e6ca1ad6 47
40aeeda3
MR
48#include <linux/math64.h>
49
c4ded03e 50static void default_log(const char *ignored, char *response) {}
2f83f219
JB
51
52int write_sparse_image(struct sparse_storage *info,
c4ded03e 53 const char *part_name, void *data, char *response)
7bfc3b13 54{
cc0f08cd
SR
55 lbaint_t blk;
56 lbaint_t blkcnt;
57 lbaint_t blks;
89be8e31 58 uint64_t bytes_written = 0;
cc0f08cd
SR
59 unsigned int chunk;
60 unsigned int offset;
89be8e31 61 uint64_t chunk_data_sz;
cc0f08cd
SR
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;
0abd63b2 67 int fill_buf_num_blks;
cc0f08cd 68 int i;
0abd63b2
SR
69 int j;
70
c232d14d 71 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
7bfc3b13 72
bb83c0f3 73 /* Read and skip over sparse image header */
cc0f08cd 74 sparse_header = (sparse_header_t *)data;
bb83c0f3 75
cc0f08cd
SR
76 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 }
bb83c0f3 84
2f83f219
JB
85 if (!info->mssg)
86 info->mssg = default_log;
87
bb83c0f3
MR
88 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
40aeeda3
MR
98 /*
99 * Verify that the sparse block size is a multiple of our
100 * storage backend block size
101 */
cc0f08cd 102 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
40aeeda3 103 if (offset) {
e6ca1ad6
SR
104 printf("%s: Sparse image block size issue [%u]\n",
105 __func__, sparse_header->blk_sz);
c4ded03e 106 info->mssg("sparse image block size issue", response);
2f83f219 107 return -1;
e6ca1ad6
SR
108 }
109
64ece848 110 puts("Flashing Sparse Image\n");
e6ca1ad6 111
b4e4bbe5 112 /* Start processing chunks */
cc0f08cd 113 blk = info->start;
7bfc3b13 114 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
cc0f08cd
SR
115 /* Read and skip over chunk header */
116 chunk_header = (chunk_header_t *)data;
117 data += sizeof(chunk_header_t);
118
119 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);
e6ca1ad6 124 }
b4e4bbe5 125
cc0f08cd
SR
126 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));
b4e4bbe5
SR
133 }
134
89be8e31
SA
135 chunk_data_sz = ((u64)sparse_header->blk_sz) * chunk_header->chunk_sz;
136 blkcnt = DIV_ROUND_UP_ULL(chunk_data_sz, info->blksz);
cc0f08cd
SR
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)) {
c4ded03e
AK
141 info->mssg("Bogus chunk size for chunk type Raw",
142 response);
2f83f219 143 return -1;
cc0f08cd
SR
144 }
145
146 if (blk + blkcnt > info->start + info->size) {
147 printf(
148 "%s: Request would exceed partition size!\n",
149 __func__);
c4ded03e
AK
150 info->mssg("Request would exceed partition size!",
151 response);
2f83f219 152 return -1;
cc0f08cd 153 }
e6ca1ad6 154
cc0f08cd
SR
155 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);
c4ded03e 161 info->mssg("flash write failure", response);
2f83f219 162 return -1;
cc0f08cd
SR
163 }
164 blk += blks;
89be8e31 165 bytes_written += ((u64)blkcnt) * info->blksz;
cc0f08cd
SR
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))) {
c4ded03e 173 info->mssg("Bogus chunk size for chunk type FILL", response);
2f83f219 174 return -1;
cc0f08cd 175 }
b4e4bbe5 176
cc0f08cd
SR
177 fill_buf = (uint32_t *)
178 memalign(ARCH_DMA_MINALIGN,
0abd63b2
SR
179 ROUNDUP(
180 info->blksz * fill_buf_num_blks,
181 ARCH_DMA_MINALIGN));
cc0f08cd 182 if (!fill_buf) {
c4ded03e
AK
183 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
184 response);
2f83f219 185 return -1;
cc0f08cd 186 }
b4e4bbe5 187
cc0f08cd
SR
188 fill_val = *(uint32_t *)data;
189 data = (char *)data + sizeof(uint32_t);
a5d1e04a 190
0abd63b2
SR
191 for (i = 0;
192 i < (info->blksz * fill_buf_num_blks /
193 sizeof(fill_val));
194 i++)
cc0f08cd 195 fill_buf[i] = fill_val;
a5d1e04a 196
cc0f08cd
SR
197 if (blk + blkcnt > info->start + info->size) {
198 printf(
199 "%s: Request would exceed partition size!\n",
200 __func__);
c4ded03e
AK
201 info->mssg("Request would exceed partition size!",
202 response);
2f83f219 203 return -1;
b4e4bbe5 204 }
b4e4bbe5 205
0abd63b2
SR
206 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);
c4ded03e
AK
217 info->mssg("flash write failure",
218 response);
cc0f08cd 219 free(fill_buf);
2f83f219 220 return -1;
cc0f08cd
SR
221 }
222 blk += blks;
0abd63b2 223 i += j;
cc0f08cd 224 }
89be8e31
SA
225 bytes_written += ((u64)blkcnt) * info->blksz;
226 total_blocks += DIV_ROUND_UP_ULL(chunk_data_sz,
227 sparse_header->blk_sz);
cc0f08cd
SR
228 free(fill_buf);
229 break;
7bfc3b13 230
cc0f08cd 231 case CHUNK_TYPE_DONT_CARE:
2c724046 232 blk += info->reserve(info, blk, blkcnt);
cc0f08cd 233 total_blocks += chunk_header->chunk_sz;
cc0f08cd
SR
234 break;
235
236 case CHUNK_TYPE_CRC32:
237 if (chunk_header->total_sz !=
238 sparse_header->chunk_hdr_sz) {
c4ded03e
AK
239 info->mssg("Bogus chunk size for chunk type Dont Care",
240 response);
2f83f219 241 return -1;
cc0f08cd
SR
242 }
243 total_blocks += chunk_header->chunk_sz;
244 data += chunk_data_sz;
245 break;
246
247 default:
248 printf("%s: Unknown chunk type: %x\n", __func__,
249 chunk_header->chunk_type);
c4ded03e 250 info->mssg("Unknown chunk type", response);
2f83f219 251 return -1;
cc0f08cd 252 }
b4e4bbe5
SR
253 }
254
e3793541 255 debug("Wrote %d blocks, expected to write %d blocks\n",
cc0f08cd 256 total_blocks, sparse_header->total_blks);
89be8e31 257 printf("........ wrote %llu bytes to '%s'\n", bytes_written, part_name);
a5d1e04a 258
2f83f219 259 if (total_blocks != sparse_header->total_blks) {
c4ded03e 260 info->mssg("sparse image write failure", response);
2f83f219
JB
261 return -1;
262 }
b4e4bbe5 263
2f83f219 264 return 0;
b4e4bbe5 265}
This page took 0.364902 seconds and 4 git commands to generate.