]>
git.ipfire.org Git - people/ms/u-boot.git/blob - common/image-sparse.c
2 * Copyright (c) 2009, Google Inc.
5 * Copyright (c) 2009-2014, The Linux Foundation. All rights reserved.
6 * Portions Copyright 2014 Broadcom Corporation.
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
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.
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!
39 #include <image-sparse.h>
43 #include <sparse_format.h>
46 #include <linux/math64.h>
48 void write_sparse_image(
49 struct sparse_storage
*info
, const char *part_name
,
50 void *data
, unsigned sz
, char *response_str
)
55 uint32_t bytes_written
= 0;
58 unsigned int chunk_data_sz
;
59 uint32_t *fill_buf
= NULL
;
61 sparse_header_t
*sparse_header
;
62 chunk_header_t
*chunk_header
;
63 uint32_t total_blocks
= 0;
66 /* Read and skip over sparse image header */
67 sparse_header
= (sparse_header_t
*)data
;
69 data
+= sparse_header
->file_hdr_sz
;
70 if (sparse_header
->file_hdr_sz
> sizeof(sparse_header_t
)) {
72 * Skip the remaining bytes in a header that is longer than
75 data
+= (sparse_header
->file_hdr_sz
- sizeof(sparse_header_t
));
78 debug("=== Sparse Image Header ===\n");
79 debug("magic: 0x%x\n", sparse_header
->magic
);
80 debug("major_version: 0x%x\n", sparse_header
->major_version
);
81 debug("minor_version: 0x%x\n", sparse_header
->minor_version
);
82 debug("file_hdr_sz: %d\n", sparse_header
->file_hdr_sz
);
83 debug("chunk_hdr_sz: %d\n", sparse_header
->chunk_hdr_sz
);
84 debug("blk_sz: %d\n", sparse_header
->blk_sz
);
85 debug("total_blks: %d\n", sparse_header
->total_blks
);
86 debug("total_chunks: %d\n", sparse_header
->total_chunks
);
89 * Verify that the sparse block size is a multiple of our
90 * storage backend block size
92 div_u64_rem(sparse_header
->blk_sz
, info
->blksz
, &offset
);
94 printf("%s: Sparse image block size issue [%u]\n",
95 __func__
, sparse_header
->blk_sz
);
96 fastboot_fail(response_str
, "sparse image block size issue");
100 puts("Flashing Sparse Image\n");
102 /* Start processing chunks */
104 for (chunk
= 0; chunk
< sparse_header
->total_chunks
; chunk
++) {
105 /* Read and skip over chunk header */
106 chunk_header
= (chunk_header_t
*)data
;
107 data
+= sizeof(chunk_header_t
);
109 if (chunk_header
->chunk_type
!= CHUNK_TYPE_RAW
) {
110 debug("=== Chunk Header ===\n");
111 debug("chunk_type: 0x%x\n", chunk_header
->chunk_type
);
112 debug("chunk_data_sz: 0x%x\n", chunk_header
->chunk_sz
);
113 debug("total_size: 0x%x\n", chunk_header
->total_sz
);
116 if (sparse_header
->chunk_hdr_sz
> sizeof(chunk_header_t
)) {
118 * Skip the remaining bytes in a header that is longer
121 data
+= (sparse_header
->chunk_hdr_sz
-
122 sizeof(chunk_header_t
));
125 chunk_data_sz
= sparse_header
->blk_sz
* chunk_header
->chunk_sz
;
126 blkcnt
= chunk_data_sz
/ info
->blksz
;
127 switch (chunk_header
->chunk_type
) {
129 if (chunk_header
->total_sz
!=
130 (sparse_header
->chunk_hdr_sz
+ chunk_data_sz
)) {
131 fastboot_fail(response_str
,
132 "Bogus chunk size for chunk type Raw");
136 if (blk
+ blkcnt
> info
->start
+ info
->size
) {
138 "%s: Request would exceed partition size!\n",
140 fastboot_fail(response_str
,
141 "Request would exceed partition size!");
145 blks
= info
->write(info
, blk
, blkcnt
, data
);
146 /* blks might be > blkcnt (eg. NAND bad-blocks) */
148 printf("%s: %s" LBAFU
" [" LBAFU
"]\n",
149 __func__
, "Write failed, block #",
151 fastboot_fail(response_str
,
152 "flash write failure");
156 bytes_written
+= blkcnt
* info
->blksz
;
157 total_blocks
+= chunk_header
->chunk_sz
;
158 data
+= chunk_data_sz
;
161 case CHUNK_TYPE_FILL
:
162 if (chunk_header
->total_sz
!=
163 (sparse_header
->chunk_hdr_sz
+ sizeof(uint32_t))) {
164 fastboot_fail(response_str
,
165 "Bogus chunk size for chunk type FILL");
169 fill_buf
= (uint32_t *)
170 memalign(ARCH_DMA_MINALIGN
,
174 fastboot_fail(response_str
,
175 "Malloc failed for: CHUNK_TYPE_FILL");
179 fill_val
= *(uint32_t *)data
;
180 data
= (char *)data
+ sizeof(uint32_t);
182 for (i
= 0; i
< (info
->blksz
/ sizeof(fill_val
)); i
++)
183 fill_buf
[i
] = fill_val
;
185 if (blk
+ blkcnt
> info
->start
+ info
->size
) {
187 "%s: Request would exceed partition size!\n",
189 fastboot_fail(response_str
,
190 "Request would exceed partition size!");
194 for (i
= 0; i
< blkcnt
; i
++) {
195 blks
= info
->write(info
, blk
, 1, fill_buf
);
196 /* blks might be > 1 (eg. NAND bad-blocks) */
198 printf("%s: %s, block # " LBAFU
"\n",
199 __func__
, "Write failed", blk
);
200 fastboot_fail(response_str
,
201 "flash write failure");
207 bytes_written
+= blkcnt
* info
->blksz
;
208 total_blocks
+= chunk_data_sz
/ sparse_header
->blk_sz
;
212 case CHUNK_TYPE_DONT_CARE
:
213 #ifdef CONFIG_FASTBOOT_FLASH_MMC_DEV
215 total_blocks
+= chunk_header
->chunk_sz
;
219 case CHUNK_TYPE_CRC32
:
220 if (chunk_header
->total_sz
!=
221 sparse_header
->chunk_hdr_sz
) {
222 fastboot_fail(response_str
,
223 "Bogus chunk size for chunk type Dont Care");
226 total_blocks
+= chunk_header
->chunk_sz
;
227 data
+= chunk_data_sz
;
231 printf("%s: Unknown chunk type: %x\n", __func__
,
232 chunk_header
->chunk_type
);
233 fastboot_fail(response_str
, "Unknown chunk type");
238 debug("Wrote %d blocks, expected to write %d blocks\n",
239 total_blocks
, sparse_header
->total_blks
);
240 printf("........ wrote %u bytes to '%s'\n", bytes_written
, part_name
);
242 if (total_blocks
!= sparse_header
->total_blks
)
243 fastboot_fail(response_str
, "sparse image write failure");
245 fastboot_okay(response_str
, "");