SmartAudio/lichee/brandy/u-boot-2011.09/sprite/sparse/sparse.c

322 lines
9.5 KiB
C
Raw Normal View History

2018-07-13 01:31:50 +00:00
/*
* (C) Copyright 2007-2013
* Allwinner Technology Co., Ltd. <www.allwinnertech.com>
* Jerry Wang <wangflord@allwinnertech.com>
*
* See file CREDITS for list of people who contributed to this
* project.
*
* 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_format.h>
#include "sparse.h"
#include "../sprite_verify.h"
#define SPARSE_FORMAT_TYPE_TOTAL_HEAD 0xff00
#define SPARSE_FORMAT_TYPE_CHUNK_HEAD 0xff01
#define SPARSE_FORMAT_TYPE_CHUNK_DATA 0xff02
#define SPARSE_FORMAT_TYPE_CHUNK_FILL_DATA 0xff03
static uint android_format_checksum;
static uint sparse_format_type;
static uint chunk_count;
static int last_rest_size;
static int chunk_length;
static uint flash_start;
static sparse_header_t globl_header;
static uint total_chunks;
/*
************************************************************************************************************
*
* unsparse_probe
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
*
*
************************************************************************************************************
*/
int unsparse_probe(char *source, uint length, uint android_format_flash_start)
{
sparse_header_t *header = (sparse_header_t*) source;
if (header->magic != SPARSE_HEADER_MAGIC)
{
printf("sparse: bad magic\n");
return ANDROID_FORMAT_BAD;
}
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 ANDROID_FORMAT_BAD;
}
android_format_checksum = 0;
last_rest_size = 0;
chunk_count = 0;
chunk_length = 0;
sparse_format_type = SPARSE_FORMAT_TYPE_TOTAL_HEAD;
flash_start = android_format_flash_start;
total_chunks = header->total_chunks;
return ANDROID_FORMAT_DETECT;
}
/*
************************************************************************************************************
*
* DRAM_Write
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
*
*
************************************************************************************************************
*/
int unsparse_direct_write(void *pbuf, uint length)
{
int unenough_length;
int this_rest_size;
int tmp_down_size;
char *tmp_buf, *tmp_dest_buf;
chunk_header_t *chunk;
//<2F><><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD><E3B4AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD>
android_format_checksum += add_sum(pbuf, length);
this_rest_size = last_rest_size + length;
tmp_buf = (char *)pbuf - last_rest_size;
last_rest_size = 0;
while(this_rest_size > 0)
{
switch(sparse_format_type)
{
case SPARSE_FORMAT_TYPE_TOTAL_HEAD:
{
memcpy(&globl_header, tmp_buf, sizeof(sparse_header_t));
this_rest_size -= sizeof(sparse_header_t);
tmp_buf += sizeof(sparse_header_t);
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_HEAD;
break;
}
case SPARSE_FORMAT_TYPE_CHUNK_HEAD:
{
if(this_rest_size < sizeof(chunk_header_t))
{
printf("sparse: chunk head data is not enough\n");
last_rest_size = this_rest_size;
tmp_dest_buf = (char *)pbuf - this_rest_size;
memcpy(tmp_dest_buf, tmp_buf, this_rest_size);
this_rest_size = 0;
break;
}
chunk = (chunk_header_t *)tmp_buf;
/* move to next chunk */
tmp_buf += sizeof(chunk_header_t); //<2F><>ʱtmp_buf<75>Ѿ<EFBFBD>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>chunk<6E><6B><EFBFBD><EFBFBD>data<74><61>ʼ<EFBFBD><CABC>ַ
this_rest_size -= sizeof(chunk_header_t); //ʣ<><CAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
chunk_length = chunk->chunk_sz * globl_header.blk_sz; //<2F><>ǰ<EFBFBD><C7B0><EFBFBD>ݿ<EFBFBD><DDBF><EFBFBD>Ҫд<D2AA><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
printf("chunk %d(%d)\n", chunk_count ++, total_chunks);
switch (chunk->chunk_type)
{
case CHUNK_TYPE_RAW:
if (chunk->total_sz != (chunk_length + sizeof(chunk_header_t)))
{
printf("sparse: bad chunk size for chunk %d, type Raw\n", chunk_count);
return -1;
}
//<2F><><EFBFBD><EFBFBD><EFB2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݲ<EFBFBD><DDB2>֣<EFBFBD>ת<EFBFBD><D7AA><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>״̬
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_DATA;
break;
case CHUNK_TYPE_FILL:
if(chunk->total_sz != sizeof(chunk_header_t) + sizeof(u32))
{
printf("spase : bad chunk size for chunk ,type FILL \n");
return -1;
}
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_FILL_DATA;
break;
case CHUNK_TYPE_DONT_CARE:
if (chunk->total_sz != sizeof(chunk_header_t))
{
printf("sparse: bogus DONT CARE chunk\n");
return -1;
}
flash_start += (chunk_length>>9);
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_HEAD;
break;
default:
printf("sparse: unknown chunk ID %x\n", chunk->chunk_type);
return -1;
}
break;
}
case SPARSE_FORMAT_TYPE_CHUNK_DATA:
{
//<2F><><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><E3B9BB>ǰchunk<6E><6B><EFBFBD><EFBFBD>,<2C><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3A3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
unenough_length = (chunk_length >= this_rest_size)? (chunk_length - this_rest_size):0;
if(!unenough_length)
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E3B9BB>ֱ<EFBFBD><D6B1>д<EFBFBD><D0B4>
if(!sunxi_sprite_write(flash_start, chunk_length>>9, tmp_buf))
{
printf("sparse: flash write failed\n");
return -1;
}
if(chunk_length & 511)
{
printf("data is not sector align 0\n");
return -1;
}
flash_start += (chunk_length>>9);
tmp_buf += chunk_length;
this_rest_size -= chunk_length;
chunk_length = 0;
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_HEAD;
}
else //<2F><><EFBFBD><EFBFBD>ȱʧ<C8B1><CAA7><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD>
{
if(this_rest_size < 8 * 1024) //<2F>ȿ<EFBFBD><C8BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>8k
{
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݷŵ<DDB7><C5B5><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD><C8B4><EFBFBD>һ<EFBFBD>δ<EFBFBD><CEB4><EFBFBD>
tmp_dest_buf = (char *)pbuf - this_rest_size;
memcpy(tmp_dest_buf, tmp_buf, this_rest_size);
last_rest_size = this_rest_size;
this_rest_size = 0;
break;
}
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>16kʱ
//<2F><>ȱʧ<C8B1><CAA7><EFBFBD>ݳ<EFBFBD><DDB3>Ȳ<EFBFBD><C8B2><EFBFBD>4kʱ,<2C><><EFBFBD><EFBFBD>ֻȱ<D6BB><C8B1>ʮ<EFBFBD><CAAE><EFBFBD>ֽ<EFBFBD>
if(unenough_length < 4 * 1024)
{
//<2F><><EFBFBD><EFBFBD>ƴ<EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݣ<EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD>ΰ<EFBFBD>δ<EFBFBD><CEB4>д<EFBFBD><D0B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݺ<EFBFBD>ȱʧ<C8B1><CAA7><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>¼
tmp_down_size = this_rest_size + unenough_length - 4 * 1024;
}
else //<2F><><EFBFBD><EFBFBD><EFB4A6>ȱʧ<C8B1><CAA7><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>8k(<28><><EFBFBD><EFBFBD>)<29><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,ͬʱ<CDAC><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><D2B2><EFBFBD><EFBFBD>16k
{
//ֱ<><D6B1><EFBFBD><EFBFBD>¼<EFBFBD><C2BC>ǰȫ<C7B0><C8AB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>;
tmp_down_size = this_rest_size & (~(512 -1)); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
}
if(!sunxi_sprite_write(flash_start, tmp_down_size>>9, tmp_buf))
{
printf("sparse: flash write failed\n");
return -1;
}
if(tmp_down_size & 511)
{
printf("data is not sector align 1\n");
return -1;
}
tmp_buf += tmp_down_size;
flash_start += (tmp_down_size>>9);
chunk_length -= tmp_down_size;
this_rest_size -= tmp_down_size;
tmp_dest_buf = (char *)pbuf - this_rest_size;
memcpy(tmp_dest_buf, tmp_buf, this_rest_size);
last_rest_size = this_rest_size;
this_rest_size = 0;
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_DATA;
}
break;
}
case SPARSE_FORMAT_TYPE_CHUNK_FILL_DATA:
{
if(this_rest_size >= 4)
{
this_rest_size -= sizeof(u32);
tmp_buf += sizeof(u32);
flash_start += (chunk_length>>9);
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_HEAD;
}
else
{
tmp_dest_buf = (char *)pbuf - this_rest_size;
memcpy(tmp_dest_buf,tmp_buf,this_rest_size);
last_rest_size = this_rest_size;
this_rest_size = 0;
sparse_format_type = SPARSE_FORMAT_TYPE_CHUNK_FILL_DATA;
}
break;
}
default:
{
printf("sparse: unknown status\n");
return -1;
}
}
}
return 0;
}
/*
************************************************************************************************************
*
* unsparse_checksum
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƣ<EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD>
*
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ <EFBFBD><EFBFBD>
*
* ˵<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>
*
*
************************************************************************************************************
*/
uint unsparse_checksum(void)
{
return android_format_checksum;
}