1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37#include <config.h>
38#include <common.h>
39#include <image-sparse.h>
40#include <div64.h>
41#include <malloc.h>
42#include <part.h>
43#include <sparse_format.h>
44#include <fastboot.h>
45
46#include <linux/math64.h>
47
48#ifndef CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE
49#define CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE (1024 * 512)
50#endif
51
52void write_sparse_image(
53 struct sparse_storage *info, const char *part_name,
54 void *data, unsigned sz)
55{
56 lbaint_t blk;
57 lbaint_t blkcnt;
58 lbaint_t blks;
59 uint32_t bytes_written = 0;
60 unsigned int chunk;
61 unsigned int offset;
62 unsigned int chunk_data_sz;
63 uint32_t *fill_buf = NULL;
64 uint32_t fill_val;
65 sparse_header_t *sparse_header;
66 chunk_header_t *chunk_header;
67 uint32_t total_blocks = 0;
68 int fill_buf_num_blks;
69 int i;
70 int j;
71
72 fill_buf_num_blks = CONFIG_FASTBOOT_FLASH_FILLBUF_SIZE / info->blksz;
73
74
75 sparse_header = (sparse_header_t *)data;
76
77 data += sparse_header->file_hdr_sz;
78 if (sparse_header->file_hdr_sz > sizeof(sparse_header_t)) {
79
80
81
82
83 data += (sparse_header->file_hdr_sz - sizeof(sparse_header_t));
84 }
85
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
96
97
98
99
100 div_u64_rem(sparse_header->blk_sz, info->blksz, &offset);
101 if (offset) {
102 printf("%s: Sparse image block size issue [%u]\n",
103 __func__, sparse_header->blk_sz);
104 fastboot_fail("sparse image block size issue");
105 return;
106 }
107
108 puts("Flashing Sparse Image\n");
109
110
111 blk = info->start;
112 for (chunk = 0; chunk < sparse_header->total_chunks; chunk++) {
113
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);
122 }
123
124 if (sparse_header->chunk_hdr_sz > sizeof(chunk_header_t)) {
125
126
127
128
129 data += (sparse_header->chunk_hdr_sz -
130 sizeof(chunk_header_t));
131 }
132
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)) {
139 fastboot_fail(
140 "Bogus chunk size for chunk type Raw");
141 return;
142 }
143
144 if (blk + blkcnt > info->start + info->size) {
145 printf(
146 "%s: Request would exceed partition size!\n",
147 __func__);
148 fastboot_fail(
149 "Request would exceed partition size!");
150 return;
151 }
152
153 blks = info->write(info, blk, blkcnt, data);
154
155 if (blks < blkcnt) {
156 printf("%s: %s" LBAFU " [" LBAFU "]\n",
157 __func__, "Write failed, block #",
158 blk, blks);
159 fastboot_fail(
160 "flash write failure");
161 return;
162 }
163 blk += blks;
164 bytes_written += blkcnt * info->blksz;
165 total_blocks += chunk_header->chunk_sz;
166 data += chunk_data_sz;
167 break;
168
169 case CHUNK_TYPE_FILL:
170 if (chunk_header->total_sz !=
171 (sparse_header->chunk_hdr_sz + sizeof(uint32_t))) {
172 fastboot_fail(
173 "Bogus chunk size for chunk type FILL");
174 return;
175 }
176
177 fill_buf = (uint32_t *)
178 memalign(ARCH_DMA_MINALIGN,
179 ROUNDUP(
180 info->blksz * fill_buf_num_blks,
181 ARCH_DMA_MINALIGN));
182 if (!fill_buf) {
183 fastboot_fail(
184 "Malloc failed for: CHUNK_TYPE_FILL");
185 return;
186 }
187
188 fill_val = *(uint32_t *)data;
189 data = (char *)data + sizeof(uint32_t);
190
191 for (i = 0;
192 i < (info->blksz * fill_buf_num_blks /
193 sizeof(fill_val));
194 i++)
195 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__);
201 fastboot_fail(
202 "Request would exceed partition size!");
203 return;
204 }
205
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
212 if (blks < j) {
213 printf("%s: %s " LBAFU " [%d]\n",
214 __func__,
215 "Write failed, block #",
216 blk, j);
217 fastboot_fail(
218 "flash write failure");
219 free(fill_buf);
220 return;
221 }
222 blk += blks;
223 i += j;
224 }
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:
231 blk += info->reserve(info, blk, blkcnt);
232 total_blocks += chunk_header->chunk_sz;
233 break;
234
235 case CHUNK_TYPE_CRC32:
236 if (chunk_header->total_sz !=
237 sparse_header->chunk_hdr_sz) {
238 fastboot_fail(
239 "Bogus chunk size for chunk type Dont Care");
240 return;
241 }
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);
249 fastboot_fail("Unknown chunk type");
250 return;
251 }
252 }
253
254 debug("Wrote %d blocks, expected to write %d blocks\n",
255 total_blocks, sparse_header->total_blks);
256 printf("........ wrote %u bytes to '%s'\n", bytes_written, part_name);
257
258 if (total_blocks != sparse_header->total_blks)
259 fastboot_fail("sparse image write failure");
260 else
261 fastboot_okay("");
262
263 return;
264}
265