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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* (C) Copyright 2011
* Texas Instruments, <www.ti.com>
* Author: Vikram Pandita <vikram.pandita@ti.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*/
#include <config.h>
#include <common.h>
#include <sparse.h>
#include <mmc.h>
#include <fastboot.h>
//#define SDEBUG
#define SPARSE_HEADER_MAJOR_VER 1
#define SZ_128M 0x08000000
#define SZ_512M 0x20000000
#define SZ_640M 0x28000000
#define SZ_768M 0x30000000
#define SZ_896M 0x38000000
extern struct cmd_fastboot_interface priv;
int _unsparse(unsigned char init, unsigned char *source_input, u32 sector_input, u32 section_size,
unsigned mmcc, int (*WRITE)(unsigned mmcc, unsigned long sector, unsigned long len, unsigned char *src))
{
static sparse_header_t *header = (void*)0;
static u32 i = 0, outlen = 0, len_mmc = 0, sector = 0;
static unsigned char *source;
static u32 left;
static unsigned char *BOUNDARY = (void*)SZ_640M ; /*512M+128M*/
//printf("_unsparse: write to mmc slot[%d] @ %x ptn address, header->total_chunks=%d\n", mmcc, sector, header->total_chunks);
if (init == 2) {
BOUNDARY = (void*)0xffffffff;
}
printf("init:%d, source:0x%08X, sector:%d, section_size:%d, BOUNDARY:0x%x\n",
init, source_input, sector_input, section_size, BOUNDARY);
if (init >= 1) {
i = 0, outlen = 0, len_mmc = 0;
source = source_input; /* Only keep the 1st time source_input */
sector = sector_input;
header = (void*) source;
printf("total_blks:%d, blk_sz:%d(%d sectors)\n",
header->total_blks,
header->blk_sz,
(header->total_blks * (header->blk_sz / 512)));
if ((header->total_blks * (header->blk_sz / 512)) > section_size ) {
printf("sparse: section size %d sectors limit: exceeded\n",
section_size);
return 1;
}
if (header->magic != SPARSE_HEADER_MAGIC) {
printf("sparse: bad magic\n");
return 1;
}
if ((header->major_version != SPARSE_HEADER_MAJOR_VER) ||
(header->file_hdr_sz != sizeof(sparse_header_t)) ||
(header->chunk_hdr_sz != sizeof(chunk_header_t))) {
printf("sparse: incompatible format\n");
return 1;
}
/**
* backup the sparse header in 126M
*/
memcpy((void*)(SZ_128M-0x200000), header, sizeof(sparse_header_t));
header = (void*) (SZ_128M-0x200000);
/* todo: ensure image will fit */
/* Skip the header now */
source += header->file_hdr_sz;
}
for (; i < header->total_chunks; i++) {
unsigned int len = 0;
int r;
chunk_header_t *chunk = (void*) source;
/* move to next chunk */
source += sizeof(chunk_header_t);
if (chunk->chunk_type == CHUNK_TYPE_RAW) {
/**
* +----------+-------------+
* | |chunk_header | source
* +----------+-------------+
*
* +------------+-----------+
* | |chunk_header | source
* +------------+-----------+
*/
/*init source address eq or ge boundary*/
if (source >= BOUNDARY) {
left = (unsigned int)(BOUNDARY -(unsigned char *) chunk);
memcpy((void *)(SZ_128M - left), (void *)chunk, left);
source = (unsigned char *)(SZ_128M - left);
printf(" Case 1 chunk header source >= BOUNDARY left :0x%x ,source:0x%x\n", left ,source);
return 0;
}
/**
* +--+---------------------+
* | |chunk_header | source|
* +--+---------------------+
*/
/*chunk end eq boundary*/
if ((source + (chunk->chunk_sz * header->blk_sz)) == BOUNDARY) {
/////////////////////////////////////////////////
len = chunk->chunk_sz * header->blk_sz;
if (chunk->total_sz != (len + sizeof(chunk_header_t))) {
printf(" 1 _unsparse: bad chunk size for chunk 0x%x, type Raw\n", i);
return 1;
}
outlen += len/512;
if (outlen > section_size) {
printf("_unsparse: section size %d sector limit: exceeded\n", section_size);
return 1;
}
#ifdef SDEBUG
printf("source eq boundary _unsparse: RAW chunk->chunk_sz=%x header->blk_sz=0x%x: write(sector=0x%x,len=0x%x),outlen=0x%x KB\n",
chunk->chunk_sz, header->blk_sz, sector, len, outlen/2);
#endif
len_mmc = len /512;
if (len % 512)
len_mmc++;
r = WRITE(mmcc, sector, len_mmc, source);
if (r < 0) {
printf("_unsparse: mmc write failed\n");
return 1;
}
sector += len_mmc;
source += len;
source = (unsigned char *)SZ_128M;
i++;
printf(" Case 2 data end = BOUNDARY left :0x%x ,source:0x%x\n", left ,source);
return 0; //success for next image
}
/**
*
* +--------+---------------+
* | |chunk_header | source|
* +--------+---------------+
*
*/
/*chunk data gt boundary */
if (source + (chunk->chunk_sz * header->blk_sz) > BOUNDARY) {
/////////////////////////////////////////////////
len = chunk->chunk_sz * header->blk_sz;
if (chunk->total_sz != (len + sizeof(chunk_header_t))) {
printf(" 2 _unsparse: bad chunk size for chunk 0x%x, type Raw,chunk->total_sz :0x%x ,len :0x%x\n", i ,chunk->total_sz, len);
return 1;
}
len = (unsigned int)((BOUNDARY - source) / header->blk_sz) * header->blk_sz;
left = (unsigned int)(BOUNDARY - source) % header->blk_sz;
outlen += len/512;
if (outlen > section_size) {
printf("_unsparse: section size %d MB limit: exceeded\n", section_size /(1024*1024));
return 1;
}
#ifdef SDEBUG
printf("source over boundary _unsparse: RAW chunk->chunk_sz=0x%x header->blk_sz=0x%x: write(sector=0x%x,len=0x%x),outlen=0x%x KB ,i =0x%x\n",
chunk->chunk_sz, header->blk_sz, sector, len, outlen/1024 ,i );
#endif
len_mmc = len /512;
r = WRITE(mmcc, sector, len_mmc, source);
if (r < 0) {
printf("_unsparse: mmc write failed\n");
return 1;
}
sector += len_mmc;
chunk->chunk_sz -= (len / header->blk_sz);
chunk->total_sz -= len;
memcpy((void *)(SZ_128M - sizeof(chunk_header_t) - left), (void *)chunk, sizeof(chunk_header_t));
memcpy((void *)(SZ_128M - left), (void *)(source + len), left);
source =(unsigned char *)( SZ_128M - sizeof(chunk_header_t) - left);
chunk = (void *)source;
printf(" Case 3 data >= BOUNDARY left :0x%x ,source:0x%x\n", left ,source);
return 0; //success for next image
}
}
if (chunk->chunk_type == CHUNK_TYPE_DONT_CARE) {
/**
* +----------+-------------+
* | |chunk_header | source
* +----------+-------------+
*
* +------------+-----------+
* | |chunk_header | source
* +------------+-----------+
*/
if (source >= BOUNDARY) {
left = (unsigned int)(BOUNDARY - (unsigned char *)chunk);
memcpy((void *)(SZ_128M - left), (void *)chunk, left);
source =(unsigned char *)( SZ_128M - left);
printf("Case 4 x type source >= BOUNDARY left :0x%x ,source:0x%x\n", left ,source);
return 0;
}
}
switch (chunk->chunk_type) {
case CHUNK_TYPE_RAW:
len = chunk->chunk_sz * header->blk_sz;
if (chunk->total_sz != (len + sizeof(chunk_header_t))) {
printf(" 3 _unsparse: bad RAW chunk size for chunk 0x%x, chunk->total_sz :0x%x ,len :0x%x\n", i, chunk->total_sz, len );
return 1;
}
outlen += len/512;
if (outlen > section_size) {
printf("_unsparse: section size %d sector limit: exceeded\n", section_size);
return 1;
}
#ifdef SDEBUG
printf("_unsparse: RAW chunk->chunk_sz=%x header->blk_sz=0x%x: write(sector=0x%x,len=0x%x),outlen=0x%x KB\n",
chunk->chunk_sz, header->blk_sz, sector, len, outlen/2);
#endif
len_mmc = len /512;
if (len % 512)
len_mmc++;
r = WRITE(mmcc, sector, len_mmc, source);
if (r < 0) {
printf("_unsparse: mmc write failed\n");
return 1;
}
sector += len_mmc;
source += len;
break;
case CHUNK_TYPE_DONT_CARE:
if (chunk->total_sz != sizeof(chunk_header_t)) {
printf(" _unsparse: bogus DONT CARE chunk\n");
return 1;
}
len = chunk->chunk_sz * header->blk_sz;
#ifdef SDEBUG
printf("_unsparse: DONT_CARE blk=%d bsz=%d: skip(sector=%d,len=%d)\n",
chunk->chunk_sz, header->blk_sz, sector, len);
#endif
outlen += len/512;
if (outlen > section_size) {
printf("_unsparse: section size %d sector limit: exceeded\n", section_size);
return 1;
}
sector += (len / 512);
break;
default:
printf("_unsparse: unknown chunk ID %04x\n", chunk->chunk_type);
return 1;
}
}
return 0;
}
u8 do_unsparse(unsigned char init, unsigned char *source, u32 sector, u32 section_size, char *slot_no)
{
unsigned mmcc = simple_strtoul(slot_no, NULL, 16);
//printf("%s, slot_no:%x, mmcc:%x,sector:0x%x,section_size:0x%x \n",__FUNCTION__, slot_no, mmcc, sector, section_size);
if (_unsparse(init, source, sector, section_size, mmcc, mmc_fb_write))
return 1;
return 0;
}
|