]> Git Repo - J-u-boot.git/blame - lib/image-sparse.c
common: Drop net.h from common header
[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>
3d4ef38d 39#include <image-sparse.h>
cc0f08cd 40#include <div64.h>
e6ca1ad6
SR
41#include <malloc.h>
42#include <part.h>
43#include <sparse_format.h>
90526e9f 44#include <asm/cache.h>
e6ca1ad6 45
40aeeda3
MR
46#include <linux/math64.h>
47
c4ded03e 48static void default_log(const char *ignored, char *response) {}
2f83f219
JB
49
50int write_sparse_image(struct sparse_storage *info,
c4ded03e 51 const char *part_name, void *data, char *response)
7bfc3b13 52{
cc0f08cd
SR
53 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;
0abd63b2 65 int fill_buf_num_blks;
cc0f08cd 66 int i;
0abd63b2
SR
67 int j;
68
c232d14d 69 fill_buf_num_blks = CONFIG_IMAGE_SPARSE_FILLBUF_SIZE / info->blksz;
7bfc3b13 70
bb83c0f3 71 /* Read and skip over sparse image header */
cc0f08cd 72 sparse_header = (sparse_header_t *)data;
bb83c0f3 73
cc0f08cd
SR
74 data += sparse_header->file_hdr_sz;
75 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
76 /*
77 * Skip the remaining bytes in a header that is longer than
78 * we expected.
79 */
80 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
81 }
bb83c0f3 82
2f83f219
JB
83 if (!info->mssg)
84 info->mssg = default_log;
85
bb83c0f3
MR
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
40aeeda3
MR
96 /*
97 * Verify that the sparse block size is a multiple of our
98 * storage backend block size
99 */
cc0f08cd 100 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
40aeeda3 101 if (offset) {
e6ca1ad6
SR
102 printf("%s: Sparse image block size issue [%u]\n",
103 __func__, sparse_header->blk_sz);
c4ded03e 104 info->mssg("sparse image block size issue", response);
2f83f219 105 return -1;
e6ca1ad6
SR
106 }
107
64ece848 108 puts("Flashing Sparse Image\n");
e6ca1ad6 109
b4e4bbe5 110 /* Start processing chunks */
cc0f08cd 111 blk = info->start;
7bfc3b13 112 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
cc0f08cd
SR
113 /* Read and skip over chunk header */
114 chunk_header = (chunk_header_t *)data;
115 data += sizeof(chunk_header_t);
116
117 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);
e6ca1ad6 122 }
b4e4bbe5 123
cc0f08cd
SR
124 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));
b4e4bbe5
SR
131 }
132
cc0f08cd
SR
133 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)) {
c4ded03e
AK
139 info->mssg("Bogus chunk size for chunk type Raw",
140 response);
2f83f219 141 return -1;
cc0f08cd
SR
142 }
143
144 if (blk + blkcnt > info->start + info->size) {
145 printf(
146 "%s: Request would exceed partition size!\n",
147 __func__);
c4ded03e
AK
148 info->mssg("Request would exceed partition size!",
149 response);
2f83f219 150 return -1;
cc0f08cd 151 }
e6ca1ad6 152
cc0f08cd
SR
153 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);
c4ded03e 159 info->mssg("flash write failure", response);
2f83f219 160 return -1;
cc0f08cd
SR
161 }
162 blk += blks;
163 bytes_written += blkcnt * info->blksz;
164 total_blocks += chunk_header->chunk_sz;
165 data += chunk_data_sz;
166 break;
167
168 case CHUNK_TYPE_FILL:
169 if (chunk_header->total_sz !=
170 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
c4ded03e 171 info->mssg("Bogus chunk size for chunk type FILL", response);
2f83f219 172 return -1;
cc0f08cd 173 }
b4e4bbe5 174
cc0f08cd
SR
175 fill_buf = (uint32_t *)
176 memalign(ARCH_DMA_MINALIGN,
0abd63b2
SR
177 ROUNDUP(
178 info->blksz * fill_buf_num_blks,
179 ARCH_DMA_MINALIGN));
cc0f08cd 180 if (!fill_buf) {
c4ded03e
AK
181 info->mssg("Malloc failed for: CHUNK_TYPE_FILL",
182 response);
2f83f219 183 return -1;
cc0f08cd 184 }
b4e4bbe5 185
cc0f08cd
SR
186 fill_val = *(uint32_t *)data;
187 data = (char *)data + sizeof(uint32_t);
a5d1e04a 188
0abd63b2
SR
189 for (i = 0;
190 i < (info->blksz * fill_buf_num_blks /
191 sizeof(fill_val));
192 i++)
cc0f08cd 193 fill_buf[i] = fill_val;
a5d1e04a 194
cc0f08cd
SR
195 if (blk + blkcnt > info->start + info->size) {
196 printf(
197 "%s: Request would exceed partition size!\n",
198 __func__);
c4ded03e
AK
199 info->mssg("Request would exceed partition size!",
200 response);
2f83f219 201 return -1;
b4e4bbe5 202 }
b4e4bbe5 203
0abd63b2
SR
204 for (i = 0; i < blkcnt;) {
205 j = blkcnt - i;
206 if (j > fill_buf_num_blks)
207 j = fill_buf_num_blks;
208 blks = info->write(info, blk, j, fill_buf);
209 /* blks might be > j (eg. NAND bad-blocks) */
210 if (blks < j) {
211 printf("%s: %s " LBAFU " [%d]\n",
212 __func__,
213 "Write failed, block #",
214 blk, j);
c4ded03e
AK
215 info->mssg("flash write failure",
216 response);
cc0f08cd 217 free(fill_buf);
2f83f219 218 return -1;
cc0f08cd
SR
219 }
220 blk += blks;
0abd63b2 221 i += j;
cc0f08cd
SR
222 }
223 bytes_written += blkcnt * info->blksz;
224 total_blocks += chunk_data_sz / sparse_header->blk_sz;
225 free(fill_buf);
226 break;
7bfc3b13 227
cc0f08cd 228 case CHUNK_TYPE_DONT_CARE:
2c724046 229 blk += info->reserve(info, blk, blkcnt);
cc0f08cd 230 total_blocks += chunk_header->chunk_sz;
cc0f08cd
SR
231 break;
232
233 case CHUNK_TYPE_CRC32:
234 if (chunk_header->total_sz !=
235 sparse_header->chunk_hdr_sz) {
c4ded03e
AK
236 info->mssg("Bogus chunk size for chunk type Dont Care",
237 response);
2f83f219 238 return -1;
cc0f08cd
SR
239 }
240 total_blocks += chunk_header->chunk_sz;
241 data += chunk_data_sz;
242 break;
243
244 default:
245 printf("%s: Unknown chunk type: %x\n", __func__,
246 chunk_header->chunk_type);
c4ded03e 247 info->mssg("Unknown chunk type", response);
2f83f219 248 return -1;
cc0f08cd 249 }
b4e4bbe5
SR
250 }
251
e3793541 252 debug("Wrote %d blocks, expected to write %d blocks\n",
cc0f08cd
SR
253 total_blocks, sparse_header->total_blks);
254 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
a5d1e04a 255
2f83f219 256 if (total_blocks != sparse_header->total_blks) {
c4ded03e 257 info->mssg("sparse image write failure", response);
2f83f219
JB
258 return -1;
259 }
b4e4bbe5 260
2f83f219 261 return 0;
b4e4bbe5 262}
This page took 0.339218 seconds and 4 git commands to generate.