1
0
mirror of git://projects.qi-hardware.com/xburst-tools.git synced 2025-04-21 12:27:27 +03:00

move make device stage to project Makefile

use automake in flash-tool
This commit is contained in:
xiangfu
2009-06-09 19:16:39 +00:00
parent e8667bec6e
commit 83515d8092
16 changed files with 47 additions and 105 deletions

View File

@@ -0,0 +1,12 @@
AM_CFLAGS = -pedantic -Wall -W -O1 -g3 -std=gnu99 -lusb -lconfuse
AM_LDFLAGS = -lusb -lconfuse
inflash_version.h:
echo -e '#ifndef INFLASH_VERSION' \
'\n#define INFLASH_VERSION "'`svnversion`'"' \
'\n#endif' > inflash_version.h
BUILT_SOURCES = inflash_version.h
bin_PROGRAMS = inflash
inflash_SOURCES = cmd.c command_line.c ingenic_cfg.c \
ingenic_usb.c main.c

647
flash-tool/src/cmd.c Normal file
View File

@@ -0,0 +1,647 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
* Author: Xiangfu Liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include "cmd.h"
#include "ingenic_cfg.h"
#include "ingenic_usb.h"
#include "usb_boot_defines.h"
extern int com_argc;
extern char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH];
struct ingenic_dev ingenic_dev;
struct hand hand;
struct nand_in nand_in;
static struct nand_out nand_out;
unsigned int total_size;
unsigned char code_buf[4 * 512 * 1024];
unsigned char check_buf[4 * 512 * 1024];
unsigned char cs[16];
unsigned char ret[8];
static const char IMAGE_TYPE[][30] = {
"with oob and ecc",
"with oob and without ecc",
"without oob",
};
static int load_file(struct ingenic_dev *ingenic_dev, const char *file_path)
{
struct stat fstat;
int fd, status, res = -1;
status = stat(file_path, &fstat);
if (status < 0) {
fprintf(stderr, "Error - can't get file size from '%s': %s\n",
file_path, strerror(errno));
goto out;
}
ingenic_dev->file_len = fstat.st_size;
ingenic_dev->file_buff = code_buf;
fd = open(file_path, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Error - can't open file '%s': %s\n",
file_path, strerror(errno));
goto out;
}
status = read(fd, ingenic_dev->file_buff, ingenic_dev->file_len);
if (status < ingenic_dev->file_len) {
fprintf(stderr, "Error - can't read file '%s': %s\n",
file_path, strerror(errno));
goto close;
}
/* write args to code */
memcpy(ingenic_dev->file_buff + 8, &hand.fw_args,
sizeof(struct fw_args));
res = 1;
close:
close(fd);
out:
return res;
}
/* after upload stage2. must init device */
int init_cfg()
{
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
printf("\n Device unboot! Boot it first!");
return -1;
}
ingenic_dev.file_buff = &hand;
ingenic_dev.file_len = sizeof(hand);
if (usb_send_data_to_ingenic(&ingenic_dev) != 1)
return -1;
if (usb_ingenic_configration(&ingenic_dev, DS_hand) != 1)
return -1;
if (usb_read_data_from_ingenic(&ingenic_dev, ret, 8) != 1)
return -1;
return 1;
}
int boot(char *stage1_path, char *stage2_path){
int status;
status = usb_get_ingenic_cpu(&ingenic_dev);
switch (status) {
case 1: /* Jz4740v1 */
status = 0;
hand.fw_args.cpu_id = 0x4740;
break;
case 2: /* Jz4750v1 */
status = 0;
hand.fw_args.cpu_id = 0x4750;
break;
case 3: /* Boot4740 */
status = 1;
hand.fw_args.cpu_id = 0x4740;
break;
case 4: /* Boot4750 */
status = 1;
hand.fw_args.cpu_id = 0x4750;
break;
default:
return 1;
}
if (status) {
printf("Booted");
return 1;
} else {
printf("Unboot");
printf("\n Now booting device");
/* now we upload the usb boot stage1 */
printf("\n Upload usb boot stage1");
if (load_file(&ingenic_dev, stage1_path) < 1)
return -1;
if (usb_ingenic_upload(&ingenic_dev, 1) < 1)
return -1;
/* now we upload the usb boot stage2 */
sleep(1);
printf("\n Upload usb boot stage2");
if (load_file(&ingenic_dev, stage2_path) < 1)
return -1;
if (usb_ingenic_upload(&ingenic_dev, 2) < 1)
return -1;
printf("\n Boot success!");
}
sleep(1);
printf("\n Now configure Ingenic device");
(init_cfg() == 1)
? printf("\n Configure success!")
: printf("\n Configure fail!");
return 1;
}
/* nand function */
int error_check(unsigned char *org,unsigned char * obj,unsigned int size)
{
unsigned int i;
for (i = 0; i < size; i++) {
if (org[i] != obj[i]) {
printf("\n Check Error! %d = %x : %x ",
i, org[i], obj[i]);
return 0;
}
}
return 1;
}
int nand_markbad(struct nand_in *nand_in)
{
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
printf("\n Device unboot! Boot it first!");
return -1;
}
printf("\n mark bad block : %d ",nand_in->start);
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
usb_ingenic_nand_ops(&ingenic_dev, NAND_MARK_BAD);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
printf("\n Mark bad block at %d ",((ret[3] << 24) |
(ret[2] << 16) |
(ret[1] << 8) |
(ret[0] << 0)) / hand.nand_ppb);
return 0;
}
int nand_program_check(struct nand_in *nand_in,
struct nand_out *nand_out,
unsigned int *start_page)
{
unsigned int i, page_num, cur_page;
unsigned short temp;
if (nand_in->length > (unsigned int)MAX_TRANSFER_SIZE) {
printf("\n Buffer size too long!");
return -1;
}
#ifdef CONFIG_NAND_OUT
unsigned char status_buf[32];
nand_out->status = status_buf;
for (i = 0; i < nand_in->max_chip; i++)
(nand_out->status)[i] = 0; /* set all status to fail */
#endif
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
printf("\n Device unboot! Boot it first!");
return -1;
}
ingenic_dev.file_buff = nand_in->buf;
ingenic_dev.file_len = nand_in->length;
usb_send_data_to_ingenic(&ingenic_dev);
/* dump_data(nand_in->buf, 100); */
for (i = 0; i < nand_in->max_chip; i++) {
if ((nand_in->cs_map)[i]==0)
continue;
if (nand_in->option == NO_OOB) {
page_num = nand_in->length / hand.nand_ps;
if ((nand_in->length % hand.nand_ps) !=0)
page_num++;
} else {
page_num = nand_in->length /
(hand.nand_ps + hand.nand_os);
if ((nand_in->length% (hand.nand_ps + hand.nand_os)) !=0)
page_num++;
}
temp = ((nand_in->option << 12) & 0xf000) +
((i<<4) & 0xff0) + NAND_PROGRAM;
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
usb_ingenic_nand_ops(&ingenic_dev, temp);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
printf(" Finish! ");
usb_send_data_address_to_ingenic(&ingenic_dev, nand_in->start);
/* Read back to check! */
usb_send_data_length_to_ingenic(&ingenic_dev, page_num);
switch (nand_in->option) {
case OOB_ECC:
temp = ((OOB_ECC << 12) & 0xf000) +
((i << 4) & 0xff0) + NAND_READ;
usb_ingenic_nand_ops(&ingenic_dev, temp);
printf("Checking...");
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
page_num * (hand.nand_ps + hand.nand_os));
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
break;
case OOB_NO_ECC: /* do not support data verify */
temp = ((OOB_NO_ECC << 12) & 0xf000) +
((i << 4) & 0xff0) + NAND_READ;
usb_ingenic_nand_ops(&ingenic_dev, temp);
printf("Checking...");
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
page_num * (hand.nand_ps + hand.nand_os));
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
break;
case NO_OOB:
temp = ((NO_OOB << 12) & 0xf000) +
((i << 4) & 0xff0) + NAND_READ;
usb_ingenic_nand_ops(&ingenic_dev, temp);
printf("Checking...");
usb_read_data_from_ingenic(&ingenic_dev, check_buf,
page_num * hand.nand_ps);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
break;
default:
;
}
if (nand_in->start < 1 &&
hand.nand_ps == 4096 &&
hand.fw_args.cpu_id == 0x4740) {
/* (nand_out->status)[i] = 1; */
printf(" no check!");
cur_page = (ret[3] << 24) |
(ret[2] << 16) |
(ret[1] << 8) |
(ret[0] << 0);
printf(" End at %d ",cur_page);
continue;
}
if (nand_in->check(nand_in->buf, check_buf, nand_in->length)) {
/* (nand_out->status)[i] = 1; */
printf(" pass!");
cur_page = (ret[3] << 24) |
(ret[2] << 16) |
(ret[1] << 8) |
(ret[0] << 0);
printf(" End at %d ",cur_page);
} else {
/* (nand_out->status)[i] = 0; */
printf(" fail!");
struct nand_in bad;
cur_page = (ret[3] << 24) |
(ret[2] << 16) |
(ret[1] << 8) |
(ret[0] << 0);
printf(" End at %d ",cur_page);
bad.start = (cur_page - 1) / hand.nand_ppb;
if (cur_page % hand.nand_ppb == 0)
nand_markbad(&bad);
}
}
*start_page = cur_page;
return 0;
}
int nand_erase(struct nand_in *nand_in)
{
unsigned int start_blk, blk_num, end_block;
int i;
start_blk = nand_in->start;
blk_num = nand_in->length;
if (start_blk > (unsigned int)NAND_MAX_BLK_NUM) {
printf("\n Start block number overflow!");
return -1;
}
if (blk_num > (unsigned int)NAND_MAX_BLK_NUM) {
printf("\n Length block number overflow!");
return -1;
}
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
printf("\n Device unboot! Boot it first!");
return -1;
}
for (i = 0; i < nand_in->max_chip; i++) {
if ((nand_in->cs_map)[i]==0)
continue;
printf("\n Erasing No.%d device No.%d flash......",
nand_in->dev, i);
usb_send_data_address_to_ingenic(&ingenic_dev, start_blk);
usb_send_data_length_to_ingenic(&ingenic_dev, blk_num);
unsigned short temp = ((i << 4) & 0xff0) + NAND_ERASE;
usb_ingenic_nand_ops(&ingenic_dev, temp);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
printf(" Finish!");
}
end_block = ((ret[3] << 24) |
(ret[2] << 16) |
(ret[1] << 8) |
(ret[0] << 0)) / hand.nand_ppb;
printf("\n Operation end position : %d ",end_block);
if (!hand.nand_force_erase) {
/* not force erase, show bad block infomation */
printf("\n There are marked bad blocks :%d ",
end_block - start_blk - blk_num );
} else {
/* force erase, no bad block infomation can show */
printf("\n Force erase ,no bad block infomation !" );
}
return 1;
}
int nand_program_file(struct nand_in *nand_in,
struct nand_out *nand_out,
char *fname)
{
int flen, m, j, k;
unsigned int start_page = 0, page_num, code_len, offset, transfer_size;
int fd, status;
struct stat fstat;
struct nand_in n_in;
struct nand_out n_out;
#ifdef CONFIG_NAND_OUT
unsigned char status_buf[32];
nand_out->status = status_buf;
for (i=0; i<nand_in->max_chip; i++)
(nand_out->status)[i] = 0; /* set all status to fail */
#endif
status = stat(fname, &fstat);
if (status < 0) {
fprintf(stderr, "Error - can't get file size from '%s': %s\n",
fname, strerror(errno));
return -1;
}
flen = fstat.st_size;
fd = open(fname, O_RDONLY);
if (fd < 0) {
fprintf(stderr, "Error - can't open file '%s': %s\n",
fname, strerror(errno));
return -1;
}
printf("\n Programing No.%d device...",nand_in->dev);
n_in.start = nand_in->start / hand.nand_ppb;
if (nand_in->option == NO_OOB) {
if (flen % (hand.nand_ppb * hand.nand_ps) == 0)
n_in.length = flen / (hand.nand_ps * hand.nand_ppb);
else
n_in.length = flen / (hand.nand_ps * hand.nand_ppb) + 1;
} else {
if (flen % (hand.nand_ppb * (hand.nand_ps + hand.nand_os)) == 0)
n_in.length = flen /
((hand.nand_ps + hand.nand_os) * hand.nand_ppb);
else
n_in.length = flen /
((hand.nand_ps + hand.nand_os) * hand.nand_ppb)
+ 1;
}
/* printf("\n length %d flen %d ", n_in.length, flen); */
n_in.cs_map = nand_in->cs_map;
n_in.dev = nand_in->dev;
n_in.max_chip = nand_in->max_chip;
if (nand_erase(&n_in) != 1)
return -1;
if (nand_in->option == NO_OOB)
transfer_size = (hand.nand_ppb * hand.nand_ps);
else
transfer_size = (hand.nand_ppb * (hand.nand_ps + hand.nand_os));
m = flen / transfer_size;
j = flen % transfer_size;
printf("\n Total size to send in byte is :%d", flen);
printf("\n Image type : %s", IMAGE_TYPE[nand_in->option]);
printf("\n It will cause %d times buffer transfer.", j == 0 ? m : m + 1);
#ifdef CONFIG_NAND_OUT
for (i = 0; i < nand_in->max_chip; i++)
(nand_out->status)[i] = 1; /* set all status to success! */
#endif
offset = 0;
for (k = 0; k < m; k++) {
if (nand_in->option == NO_OOB)
page_num = transfer_size / hand.nand_ps;
else
page_num = transfer_size / (hand.nand_ps + hand.nand_os);
code_len = transfer_size;
status = read(fd, code_buf, code_len);
if (status < code_len) {
fprintf(stderr, "Error - can't read file '%s': %s\n",
fname, strerror(errno));
return -1;
}
/* read code from file to buffer */
printf("\n No.%d Programming...",k+1);
nand_in->length = code_len; /* code length,not page number! */
nand_in->buf = code_buf;
if (nand_program_check(nand_in, &n_out, &start_page) == -1)
return -1;
if (start_page - nand_in->start > hand.nand_ppb)
printf("\n Skip a old bad block !");
nand_in->start = start_page;
#ifdef CONFIG_NAND_OUT
for (i = 0; i < nand_in->max_chip; i++) {
(nand_out->status)[i] = (nand_out->status)[i] *
(n_out.status)[i];
}
#endif
offset += code_len ;
//lseek(fd, offset, SEEK_SET);
}
if (j) {
code_len = j;
j += hand.nand_ps - (j % hand.nand_ps);
memset(code_buf, 0, j); /* set all to null */
status = read(fd, code_buf, j);
if (status < code_len) {
fprintf(stderr, "Error - can't read file '%s': %s\n",
fname, strerror(errno));
return -1;
}
nand_in->length = j;
nand_in->buf = code_buf;
printf("\n No.%d Programming...", k+1);
if (nand_program_check(nand_in, &n_out, &start_page) == -1)
return -1;
if (start_page - nand_in->start > hand.nand_ppb)
printf(" Skip a old bad block !");
#ifdef CONFIG_NAND_OUT
for (i=0; i < nand_in->max_chip; i++) {
(nand_out->status)[i] = (nand_out->status)[i] *
(n_out.status)[i];
}
#endif
}
close(fd);
return 1;
}
int nand_program_file_planes(struct nand_in *nand_in,
struct nand_out *nand_out,
char *fname)
{
printf(" \n not implement yet !");
return -1;
}
int init_nand_in(void)
{
nand_in.buf = code_buf;
nand_in.check = error_check;
nand_in.dev = 0;
nand_in.cs_map = cs;
memset(nand_in.cs_map, 0, MAX_DEV_NUM);
nand_in.max_chip = 16;
return 0;
}
int nand_prog(void)
{
unsigned int i;
char *image_file;
char *help = "\n Usage: nprog (1) (2) (3) (4) (5)"
"\n (1)\tstart page number"
"\n (2)\timage file name"
"\n (3)\tdevice index number"
"\n (4)\tflash index number"
"\n (5) image type must be:"
"\n \t-n:\tno oob"
"\n \t-o:\twith oob no ecc"
"\n \t-e:\twith oob and ecc";
if (com_argc != 6) {
printf("\n not enough argument.");
printf("%s", help);
return 0;
}
init_nand_in();
nand_in.start = atoi(com_argv[1]);
image_file = com_argv[2];
nand_in.dev = atoi(com_argv[3]);
(nand_in.cs_map)[atoi(com_argv[4])] = 1;
if (!strcmp(com_argv[5], "-e"))
nand_in.option = OOB_ECC;
else if (!strcmp(com_argv[5], "-o"))
nand_in.option = OOB_NO_ECC;
else if (!strcmp(com_argv[5], "-n"))
nand_in.option = NO_OOB;
else
printf("%s", help);
if (hand.nand_plane > 1)
nand_program_file_planes(&nand_in, &nand_out, image_file);
else
nand_program_file(&nand_in, &nand_out, image_file);
#ifdef CONFIG_NAND_OUT
printf("\n Flash check result:");
for (i = 0; i < 16; i++)
printf(" %d", (nand_out.status)[i]);
#endif
return 1;
}
int nand_query(void)
{
int i;
unsigned char csn;
if (com_argc < 3) {
printf("\n Usage:");
printf(" nquery (1) (2) ");
printf("\n (1):device index number"
"\n (2):flash index number");
return -1;
}
init_nand_in();
nand_in.dev = atoi(com_argv[1]);
(nand_in.cs_map)[atoi(com_argv[2])] = 1;
for (i = 0; i < nand_in.max_chip; i++) {
if ((nand_in.cs_map)[i] != 0)
break;
}
if (i >= nand_in.max_chip)
return -1;
if (usb_get_ingenic_cpu(&ingenic_dev) < 3) {
printf("\n Device unboot! Boot it first!");
return -1;
}
csn = i;
printf("\n ID of No.%d device No.%d flash: ", nand_in.dev, csn);
unsigned short ops = ((csn << 4) & 0xff0) + NAND_QUERY;
usb_ingenic_nand_ops(&ingenic_dev, ops);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
printf("\n Vendor ID :0x%x ",(unsigned char)ret[0]);
printf("\n Product ID :0x%x ",(unsigned char)ret[1]);
printf("\n Chip ID :0x%x ",(unsigned char)ret[2]);
printf("\n Page ID :0x%x ",(unsigned char)ret[3]);
printf("\n Plane ID :0x%x ",(unsigned char)ret[4]);
usb_read_data_from_ingenic(&ingenic_dev, ret, 8);
printf("\n Operation status: Success!");
return 1;
}

36
flash-tool/src/cmd.h Normal file
View File

@@ -0,0 +1,36 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Xiangfu Liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#ifndef __CMD_H__
#define __CMD_H__
#include "usb_boot_defines.h"
#define COMMAND_NUM 31
#define MAX_ARGC 10
#define MAX_COMMAND_LENGTH 100
int boot(char *stage1_path, char *stage2_path);
int nand_prog(void);
int nand_query(void);
int nand_erase(struct nand_in *nand_in);
#endif /* __CMD_H__ */

View File

@@ -0,0 +1,218 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Xiangfu Liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "usb_boot_defines.h"
#include "ingenic_usb.h"
#include "cmd.h"
#include "inflash_version.h"
extern struct nand_in nand_in;
int com_argc;
char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH];
static const char COMMAND[][30]=
{
"",
"query",
"querya",
"erase",
"read",
"prog",
"nquery",
"nerase",
"nread",
"nreadraw",
"nreadoob", /* index 10 */
"nprog",
"help",
"version",
"go",
"fconfig",
"exit",
"readnand",
"gpios",
"gpioc",
"boot", /* index 20 */
"list",
"select",
"unselect",
"chip",
"unchip",
"nmake",
"load",
"memtest",
"run"
};
static int handle_help(void)
{
printf("\n Command support in current version:"
"\n help print this help;"
"\n boot boot device and make it in stage2;"
"\n list show current device number can connect;"
"\n fconfig set USB Boot config file;"
"\n nquery query NAND flash info;"
"\n nread read NAND flash data with checking bad block and ECC;"
"\n nreadraw read NAND flash data without checking bad block and ECC;"
"\n nreadoob read NAND flash oob without checking bad block and ECC;"
"\n nerase erase NAND flash;"
"\n nprog program NAND flash with data and ECC;"
"\n nmark mark a bad block in NAND flash;"
"\n go execute program in SDRAM;"
"\n version show current USB Boot software version;"
"\n exit quit from telnet session;"
"\n readnand read data from nand flash and store to SDRAM;"
"\n load load file data to SDRAM;"
"\n run run command script in file;"
"\n memtest do SDRAM test;"
"\n gpios let one GPIO to high level;"
"\n gpioc let one GPIO to low level;");
/* printf("\n nmake read all data from nand flash and store to file(experimental);"); */
return 1;
}
static int handle_version(void)
{
printf("\n USB Boot Software current version: %s", INFLASH_VERSION);
return 1;
}
static int handle_fconfig(void)
{
if (com_argc < 3) {
printf("\n Usage:"
" fconfig (1) (2) "
"\n 1:configration file name"
"\n 2:deivce index number");
return -1;
}
/* usb_infenic_config(atoi(com_argv[2]),com_argv[1]); */
return 1;
}
/* need transfer two para :blk_num ,start_blk */
int handle_nerase(void)
{
int i;
if (com_argc < 5) {
printf("\n Usage:");
printf(" nerase (1) (2) (3) (4) ");
printf("\n 1:start block number"
"\n 2:block length"
"\n 3:device index number"
"\n 4:flash chip index number");
return -1;
}
init_nand_in();
nand_in.start = atoi(com_argv[1]);
nand_in.length = atoi(com_argv[2]);
nand_in.dev = atoi(com_argv[3]);
if (atoi(com_argv[4]) >= MAX_DEV_NUM) {
printf("\n Flash index number overflow!");
return -1;
}
(nand_in.cs_map)[atoi(com_argv[4])] = 1;
if (nand_erase(&nand_in) < 1)
return -1;
return 1;
}
int command_interpret(char * com_buf)
{
char *buf = com_buf;
int k, L, i = 0, j = 0;
L = (int)strlen(buf);
buf[L]=' ';
for (k = 0; k <= L; k++) {
if (*buf == ' ' || *buf == '\n') {
while ( *(++buf) == ' ' );
com_argv[i][j] = '\0';
i++;
if (i > MAX_ARGC) {
printf("\n Para is too much! About!");
return 0;
}
j=0;
continue;
} else {
com_argv[i][j] = *buf;
j++;
if (j > MAX_COMMAND_LENGTH) {
printf("\n Para is too long! About!");
return 0;
}
}
buf++;
}
com_argc = i;
for (i = 1; i <= COMMAND_NUM; i++)
if (!strcmp(COMMAND[i], com_argv[0]))
return i;
return COMMAND_NUM + 1;
}
int command_handle(char *buf)
{
int cmd = command_interpret(buf); /* get the command index */
if (!cmd)
return -1;
switch (cmd) {
case 6:
nand_query();
break;
case 7:
handle_nerase();
break;
case 11:
nand_prog();
break;
case 12:
handle_help();
break;
case 13:
handle_version();
break;
case 16: /* exit */
printf("\n exiting inflash software\n");
return -1; /* return -1 to break the main.c while
* then run usb_ingenic_cleanup*/
case 20:
boot(STAGE1_FILE_PATH, STAGE2_FILE_PATH);
break;
default:
printf("\n Command not support!");
break;
}
return 1;
}

View File

@@ -0,0 +1,27 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Xiangfu Liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#ifndef __COMMAND_LINE_H__
#define __COMMAND_LINE_H__
int command_input(char *buf);
int command_handle(char *buf);
#endif /* __COMMAND_LINE_H__ */

View File

@@ -0,0 +1,198 @@
/*
* put all the configure operate to this file
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include <confuse.h>
#include "ingenic_cfg.h"
#include "usb_boot_defines.h"
extern unsigned int total_size;
int hand_init_def(struct hand *hand)
{
/* nand flash info */
/* hand.nand_start=0; */ /* important !!!! */
hand->pt = JZ4740; /* cpu type */
hand->nand_bw = 8;
hand->nand_rc = 3;
hand->nand_ps = 2048;
hand->nand_ppb = 64;
hand->nand_eccpos = 6;
hand->nand_bbpage = 0;
hand->nand_bbpos = 0;
hand->nand_force_erase = 0;
/* hand.nand_ids=0; */ /* vendor_id & device_id */
hand->fw_args.cpu_id = 0x4740;
hand->fw_args.ext_clk = 12;
hand->fw_args.cpu_speed = 225 / hand->fw_args.ext_clk;
hand->fw_args.phm_div = 3;
hand->fw_args.use_uart = 0;
hand->fw_args.boudrate = 57600;
hand->fw_args.bus_width = 0;
hand->fw_args.bank_num = 1;
hand->fw_args.row_addr = 13;
hand->fw_args.col_addr = 9;
hand->fw_args.is_mobile = 0;
hand->fw_args.is_busshare = 1;
return 1;
}
int check_dump_cfg(struct hand *hand)
{
printf("\n Now checking whether all configure args valid: ");
/* check PLL */
if (hand->fw_args.ext_clk > 27 || hand->fw_args.ext_clk < 12) {
printf("\n EXTCLK setting invalid!");
return 0;
}
if (hand->fw_args.phm_div > 32 || hand->fw_args.ext_clk < 2) {
printf("\n PHMDIV setting invalid!");
return 0;
}
if ((hand->fw_args.cpu_speed * hand->fw_args.ext_clk ) % 12 != 0) {
printf("\n CPUSPEED setting invalid!");
return 0;
}
/* check SDRAM */
if (hand->fw_args.bus_width > 1 ) {
printf("\n SDRAMWIDTH setting invalid!");
return 0;
}
if (hand->fw_args.bank_num > 1 ) {
printf("\n BANKNUM setting invalid!");
return 0;
}
if (hand->fw_args.row_addr > 13 && hand->fw_args.row_addr < 11 ) {
printf("\n ROWADDR setting invalid!");
return 0;
}
if (hand->fw_args.col_addr > 13 && hand->fw_args.col_addr < 11 ) {
printf("\n COLADDR setting invalid!");
return 0;
}
/* check NAND */
if ( hand->nand_ps < 2048 && hand->nand_os > 16 ) {
printf("\n PAGESIZE or OOBSIZE setting invalid!");
return 0;
}
if ( hand->nand_ps < 2048 && hand->nand_ppb > 32 ) {
printf("\n PAGESIZE or PAGEPERBLOCK setting invalid!");
return 0;
}
if ( hand->nand_ps > 512 && hand->nand_os <= 16 ) {
printf("\n PAGESIZE or OOBSIZE setting invalid!");
return 0;
}
if ( hand->nand_ps > 512 && hand->nand_ppb < 64 ) {
printf("\n PAGESIZE or PAGEPERBLOCK setting invalid!");
return 0;
}
printf("\n Current device information:");
printf(" CPU is Jz%x",hand->fw_args.cpu_id);
printf("\n Crystal work at %dMHz, the CCLK up to %dMHz and PMH_CLK up to %dMHz",
hand->fw_args.ext_clk,
(unsigned int)hand->fw_args.cpu_speed * hand->fw_args.ext_clk,
((unsigned int)hand->fw_args.cpu_speed * hand->fw_args.ext_clk) / hand->fw_args.phm_div);
printf("\n Total SDRAM size is %d MB, work in %d bank and %d bit mode",
total_size / 0x100000, 2 * (hand->fw_args.bank_num + 1),
16 * (2 - hand->fw_args.bus_width));
printf("\n Nand page size %d, "
"ECC offset %d, "
"bad block ID %d, "
"use %d plane mode\n",
hand->nand_ps,
hand->nand_eccpos,
hand->nand_bbpage,
hand->nand_plane);
return 1;
}
int parse_configure(struct hand *hand, char * file_path)
{
hand_init_def(hand);
cfg_opt_t opts[] = {
CFG_SIMPLE_INT("EXTCLK", &hand->fw_args.ext_clk),
CFG_SIMPLE_INT("CPUSPEED", &hand->fw_args.cpu_speed),
CFG_SIMPLE_INT("PHMDIV", &hand->fw_args.phm_div),
CFG_SIMPLE_INT("BOUDRATE", &hand->fw_args.boudrate),
CFG_SIMPLE_INT("USEUART", &hand->fw_args.use_uart),
CFG_SIMPLE_INT("BUSWIDTH", &hand->fw_args.bus_width),
CFG_SIMPLE_INT("BANKS", &hand->fw_args.bank_num),
CFG_SIMPLE_INT("ROWADDR", &hand->fw_args.row_addr),
CFG_SIMPLE_INT("COLADDR", &hand->fw_args.col_addr),
CFG_SIMPLE_INT("ISMOBILE", &hand->fw_args.is_mobile),
CFG_SIMPLE_INT("ISBUSSHARE", &hand->fw_args.is_busshare),
CFG_SIMPLE_INT("DEBUGOPS", &hand->fw_args.debug_ops),
CFG_SIMPLE_INT("PINNUM", &hand->fw_args.pin_num),
CFG_SIMPLE_INT("START", &hand->fw_args.start),
CFG_SIMPLE_INT("SIZE", &hand->fw_args.size),
CFG_SIMPLE_INT("NAND_BUSWIDTH", &hand->nand_bw),
CFG_SIMPLE_INT("NAND_ROWCYCLES", &hand->nand_rc),
CFG_SIMPLE_INT("NAND_PAGESIZE", &hand->nand_ps),
CFG_SIMPLE_INT("NAND_PAGEPERBLOCK", &hand->nand_ppb),
CFG_SIMPLE_INT("NAND_FORCEERASE", &hand->nand_force_erase),
CFG_SIMPLE_INT("NAND_OOBSIZE", &hand->nand_os),
CFG_SIMPLE_INT("NAND_ECCPOS", &hand->nand_eccpos),
CFG_SIMPLE_INT("NAND_BADBLACKPOS", &hand->nand_bbpos),
CFG_SIMPLE_INT("NAND_BADBLACKPAGE", &hand->nand_bbpage),
CFG_SIMPLE_INT("NAND_PLANENUM", &hand->nand_plane),
CFG_SIMPLE_INT("NAND_BCHBIT", &hand->nand_bchbit),
CFG_SIMPLE_INT("NAND_WPPIN", &hand->nand_wppin),
CFG_SIMPLE_INT("NAND_BLOCKPERCHIP", &hand->nand_bbpage),
CFG_END()
};
cfg_t *cfg;
cfg = cfg_init(opts, 0);
if (cfg_parse(cfg, file_path) == CFG_PARSE_ERROR)
return -1;
cfg_free(cfg);
hand->fw_args.cpu_id = 0x4740;
if (hand->fw_args.bus_width == 32)
hand->fw_args.bus_width = 0 ;
else
hand->fw_args.bus_width = 1 ;
hand->fw_args.bank_num = hand->fw_args.bank_num / 4;
hand->fw_args.cpu_speed = hand->fw_args.cpu_speed / hand->fw_args.ext_clk;
total_size = (unsigned int)
(2 << (hand->fw_args.row_addr + hand->fw_args.col_addr - 1)) * 2
* (hand->fw_args.bank_num + 1) * 2
* (2 - hand->fw_args.bus_width);
if (check_dump_cfg(hand) < 1)
return -1;
return 1;
}

View File

@@ -0,0 +1,33 @@
/*
* put all the configure operate to this file
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#ifndef __INGENIC_CFG_H__
#define __INGENIC_CFG_H__
#include "usb_boot_defines.h"
#define CONFIG_FILE_PATH "usb_boot.cfg"
int hand_init_def(struct hand *hand);
int check_dump_cfg(struct hand *hand);
int parse_configure(struct hand *hand, char * file_path);
#endif /*__INGENIC_CFG_H__ */

View File

@@ -0,0 +1,380 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
* Author: Xiangfu liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include "ingenic_usb.h"
#include "usb_boot_defines.h"
#include <usb.h>
#include <stdio.h>
#include <string.h>
extern unsigned int total_size;
static int get_ingenic_device(struct ingenic_dev *ingenic_dev)
{
struct usb_bus *usb_busses, *usb_bus;
struct usb_device *usb_dev;
int count = 0;
usb_busses = usb_get_busses();
for (usb_bus = usb_busses; usb_bus != NULL; usb_bus = usb_bus->next) {
for (usb_dev = usb_bus->devices; usb_dev != NULL;
usb_dev = usb_dev->next) {
if ((usb_dev->descriptor.idVendor == VENDOR_ID) &&
(usb_dev->descriptor.idProduct == PRODUCT_ID)) {
ingenic_dev->usb_dev = usb_dev;
count++;
}
}
}
return count;
}
static int get_ingenic_interface(struct ingenic_dev *ingenic_dev)
{
struct usb_config_descriptor *usb_config_desc;
struct usb_interface_descriptor *usb_if_desc;
struct usb_interface *usb_if;
int config_index, if_index, alt_index;
for (config_index = 0;
config_index < ingenic_dev->usb_dev->descriptor.bNumConfigurations;
config_index++) {
usb_config_desc = &ingenic_dev->usb_dev->config[config_index];
if (!usb_config_desc)
return 0;
for (if_index = 0; if_index < usb_config_desc->bNumInterfaces;
if_index++) {
usb_if = &usb_config_desc->interface[if_index];
if (!usb_if)
return 0;
for (alt_index = 0; alt_index < usb_if->num_altsetting;
alt_index++) {
usb_if_desc = &usb_if->altsetting[alt_index];
if (!usb_if_desc)
return 0;
if ((usb_if_desc->bInterfaceClass == 0xff) &&
(usb_if_desc->bInterfaceSubClass == 0)) {
ingenic_dev->interface =
usb_if_desc->bInterfaceNumber;
return 1;
}
}
}
}
return 0;
}
int usb_ingenic_init(struct ingenic_dev *ingenic_dev)
{
int num_ingenic, status = -1;
memset(ingenic_dev, 0, sizeof(struct ingenic_dev));
usb_init();
/* usb_set_debug(255); */
usb_find_busses();
usb_find_devices();
num_ingenic = get_ingenic_device(ingenic_dev);
if (num_ingenic == 0) {
fprintf(stderr, "Error - no Ingenic device found\n");
goto out;
}
if (num_ingenic > 1) {
fprintf(stderr, "Error - too many Ingenic devices found: %i\n",
num_ingenic);
goto out;
}
ingenic_dev->usb_handle = usb_open(ingenic_dev->usb_dev);
if (!ingenic_dev->usb_handle) {
fprintf(stderr, "Error - can't open Ingenic device: %s\n",
usb_strerror());
goto out;
}
if (get_ingenic_interface(ingenic_dev) < 1) {
fprintf(stderr, "Error - can't find Ingenic interface\n");
goto out;
}
if (usb_claim_interface(ingenic_dev->usb_handle, ingenic_dev->interface)
< 0) {
fprintf(stderr, "Error - can't claim Ingenic interface: %s\n",
usb_strerror());
goto out;
}
status = 1;
out:
return status;
}
int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev)
{
int status;
memset(&ingenic_dev->cpu_info_buff, 0,
ARRAY_SIZE(ingenic_dev->cpu_info_buff));
sleep(1);
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_IN | USB_TYPE_VENDOR |USB_RECIP_DEVICE,
/* bRequest */ VR_GET_CPU_INFO,
/* wValue */ 0,
/* wIndex */ 0,
/* Data */ ingenic_dev->cpu_info_buff,
/* wLength */ 8,
USB_TIMEOUT);
if (status != sizeof(ingenic_dev->cpu_info_buff) - 1 ) {
fprintf(stderr, "Error - "
"can't retrieve Ingenic CPU information: %i\n", status);
return status;
}
ingenic_dev->cpu_info_buff[8] = '\0';
printf("\n CPU data: "
"%02x, %02x, %02x, %02x, %02x, %02x, %02x, %02x : %s\n",
ingenic_dev->cpu_info_buff[0], ingenic_dev->cpu_info_buff[1],
ingenic_dev->cpu_info_buff[2], ingenic_dev->cpu_info_buff[3],
ingenic_dev->cpu_info_buff[4], ingenic_dev->cpu_info_buff[5],
ingenic_dev->cpu_info_buff[6], ingenic_dev->cpu_info_buff[7],
ingenic_dev->cpu_info_buff);
if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4740V1")) return 1;
if (!strcmp(ingenic_dev->cpu_info_buff,"JZ4750V1")) return 2;
if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4740")) return 3;
if (!strcmp(ingenic_dev->cpu_info_buff,"Boot4750")) return 4;
return 0;
}
int usb_ingenic_flush_cache(struct ingenic_dev *ingenic_dev)
{
int status;
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ VR_FLUSH_CACHES,
/* wValue */ 0,
/* wIndex */ 0,
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - can't flush cache: %i\n", status);
return status;
}
return 1;
}
int usb_send_data_length_to_ingenic(struct ingenic_dev *ingenic_dev, int len)
{
int status;
/* tell the device the length of the file to be uploaded */
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ VR_SET_DATA_LENGTH,
/* wValue */ STAGE_ADDR_MSB(len),
/* wIndex */ STAGE_ADDR_LSB(len),
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - "
"can't set data length on Ingenic device: %i\n", status);
return -1;
}
return 1;
}
int usb_send_data_address_to_ingenic(struct ingenic_dev *ingenic_dev,
unsigned int stage_addr)
{
int status;
/* tell the device the RAM address to store the file */
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ VR_SET_DATA_ADDRESS,
/* wValue */ STAGE_ADDR_MSB(stage_addr),
/* wIndex */ STAGE_ADDR_LSB(stage_addr),
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - "
"can't set the address on Ingenic device: %i\n", status);
return -1;
}
return 1;
}
int usb_send_data_to_ingenic(struct ingenic_dev *ingenic_dev)
{
int status;
status = usb_bulk_write(ingenic_dev->usb_handle,
/* endpoint */ INGENIC_OUT_ENDPOINT,
/* bulk data */ ingenic_dev->file_buff,
/* bulk data length */ ingenic_dev->file_len,
USB_TIMEOUT);
if (status < ingenic_dev->file_len) {
fprintf(stderr, "Error - "
"can't send bulk data to Ingenic CPU: %i\n", status);
return -1;
}
return 1;
}
int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev,
unsigned char *buff, unsigned int len)
{
int status;
status = usb_bulk_read(ingenic_dev->usb_handle,
/* endpoint */ INGENIC_IN_ENDPOINT,
/* bulk data */ buff,
/* bulk data length */ len,
USB_TIMEOUT);
if (status < len) {
fprintf(stderr, "Error - "
"can't read bulk data from Ingenic device:%i\n", status);
return -1;
}
return 1;
}
int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage)
{
int status;
unsigned int stage2_addr;
stage2_addr = total_size + 0x80000000;
stage2_addr -= CODE_SIZE;
int stage_addr = (stage == 1 ? 0x80002000 : stage2_addr);
usb_send_data_address_to_ingenic(ingenic_dev, stage_addr);
printf("\n Download stage %d program and execute at 0x%08x ",
stage, (stage_addr));
usb_send_data_to_ingenic(ingenic_dev);
if (stage == 2) {
if (usb_get_ingenic_cpu(ingenic_dev) < 1)
return -1;
usb_ingenic_flush_cache(ingenic_dev);
}
/* tell the device to start the uploaded device */
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ (stage == 1 ? VR_PROGRAM_START1 : VR_PROGRAM_START2),
/* wValue */ STAGE_ADDR_MSB(stage_addr),
/* wIndex */ STAGE_ADDR_LSB(stage_addr),
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - can't start the uploaded binary "
"on the Ingenic device: %i\n", status);
return status;
}
if (usb_get_ingenic_cpu(ingenic_dev) < 1)
return -1;
return 1;
}
void usb_ingenic_cleanup(struct ingenic_dev *ingenic_dev)
{
if ((ingenic_dev->usb_handle) && (ingenic_dev->interface))
usb_release_interface(ingenic_dev->usb_handle,
ingenic_dev->interface);
if (ingenic_dev->usb_handle)
usb_close(ingenic_dev->usb_handle);
}
int usb_ingenic_nand_ops(struct ingenic_dev *ingenic_dev, int ops)
{
int status;
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ VR_NAND_OPS,
/* wValue */ ops & 0xffff,
/* wIndex */ 0,
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - "
"can't set Ingenic device nand ops: %i\n", status);
return -1;
}
return 1;
}
int usb_ingenic_configration(struct ingenic_dev *ingenic_dev, int ops)
{
int status;
status = usb_control_msg(ingenic_dev->usb_handle,
/* bmRequestType */ USB_ENDPOINT_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
/* bRequest */ VR_CONFIGRATION,
/* wValue */ ops,
/* wIndex */ 0,
/* Data */ 0,
/* wLength */ 0,
USB_TIMEOUT);
if (status != 0) {
fprintf(stderr, "Error - "
"can't init Ingenic configration: %i\n", status);
return -1;
}
return 1;
}

View File

@@ -0,0 +1,77 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#ifndef __INGENIC_USB_H__
#define __INGENIC_USB_H__
#include <stdint.h>
#define INGENIC_OUT_ENDPOINT 0x01
#define INGENIC_IN_ENDPOINT 0x81
#define VR_GET_CPU_INFO 0x00
#define VR_SET_DATA_ADDRESS 0x01
#define VR_SET_DATA_LENGTH 0x02
#define VR_FLUSH_CACHES 0x03
#define VR_PROGRAM_START1 0x04
#define VR_PROGRAM_START2 0x05
#define VR_NOR_OPS 0x06
#define VR_NAND_OPS 0x07
#define VR_SDRAM_OPS 0x08
#define VR_CONFIGRATION 0x09
#define VR_GET_NUM 0x0a
#define STAGE_ADDR_MSB(addr) ((addr) >> 16)
#define STAGE_ADDR_LSB(addr) ((addr) & 0xffff)
#define USB_PACKET_SIZE 512
#define USB_TIMEOUT 5000
#define VENDOR_ID 0x601a
#define PRODUCT_ID 0x4740
#define STAGE1_FILE_PATH "fw.bin"
#define STAGE2_FILE_PATH "usb_boot.bin"
#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
struct ingenic_dev {
struct usb_device *usb_dev;
struct usb_dev_handle *usb_handle;
uint8_t interface;
char cpu_info_buff[9];
char *file_buff;
unsigned int file_len;
};
int usb_ingenic_init(struct ingenic_dev *ingenic_dev);
int usb_get_ingenic_cpu(struct ingenic_dev *ingenic_dev);
int usb_ingenic_upload(struct ingenic_dev *ingenic_dev, int stage);
void usb_ingenic_cleanup(struct ingenic_dev *ingenic_dev);
int usb_send_data_address_to_ingenic(struct ingenic_dev *ingenic_dev,
unsigned int stage_addr);
int usb_send_data_to_ingenic(struct ingenic_dev *ingenic_dev);
int usb_send_data_length_to_ingenic(struct ingenic_dev *ingenic_dev,
int len);
int usb_ingenic_nand_ops(struct ingenic_dev *ingenic_dev, int ops);
int usb_read_data_from_ingenic(struct ingenic_dev *ingenic_dev,unsigned char *buff, unsigned int len);
#endif /* __INGENIC_USB_H__ */

120
flash-tool/src/main.c Normal file
View File

@@ -0,0 +1,120 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Xiangfu Liu <xiangfu.z@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "inflash_version.h"
#include "command_line.h"
#include "ingenic_usb.h"
#include "ingenic_cfg.h"
extern struct ingenic_dev ingenic_dev;
extern struct hand hand;
static void help(void)
{
printf("Usage: inflash [options] ...(must run as root)\n"
" -h --help\t\t\tPrint this help message\n"
" -v --version\t\t\tPrint the version number\n"
" -c --command\t\t\tDirect run the command\n"
"Report bugs to <xiangfu.z@gmail.com>."
);
}
static void print_version(void)
{
printf("inflash version: %s\n", INFLASH_VERSION);
}
static struct option opts[] = {
{ "help", 0, 0, 'h' },
{ "version", 0, 0, 'v' },
{ "command", 1, 0, 'c' },
{ 0, 0, 0, 0 }
};
int main(int argc, char **argv)
{
printf(" inflash - (C) 2009"
"\n Ingenic Tools Software!"
"\n This program is Free Software and has ABSOLUTELY NO WARRANTY\n");
int command = 0;
char *cptr;
char com_buf[256];
memset(com_buf, 0, 256);
while(1) {
int c, option_index = 0;
c = getopt_long(argc, argv, "hvc:", opts,
&option_index);
if (c == -1)
break;
switch (c) {
case 'h':
help();
exit(EXIT_SUCCESS);
case 'v':
print_version();
exit(EXIT_SUCCESS);
case 'c':
command = 1;
strcpy(com_buf, optarg);
break;
default:
help();
exit(2);
}
}
if ((getuid()) || (getgid())) {
fprintf(stderr, "Error - you must be root to run '%s'\n", argv[0]);
return EXIT_FAILURE;
}
if (usb_ingenic_init(&ingenic_dev) < 1)
return EXIT_FAILURE;
if (parse_configure(&hand, CONFIG_FILE_PATH) < 1)
return EXIT_FAILURE;
if (command) { /* direct run command */
command_handle(com_buf);
printf("\n");
goto out;
}
while (1) {
printf("\n inflash :> ");
cptr = fgets(com_buf, 256, stdin);
if (cptr == NULL)
continue;
if (command_handle(com_buf) == -1 )
break;
}
out:
usb_ingenic_cleanup(&ingenic_dev);
return EXIT_SUCCESS;
}

View File

@@ -0,0 +1,166 @@
/*
* "Ingenic flash tool" - flash the Ingenic CPU via USB
*
* (C) Copyright 2009
* Author: Marek Lindner <lindner_marek@yahoo.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA
*/
#ifndef __USB_BOOT_DEFINES_H__
#define __USB_BOOT_DEFINES_H__
/* #define dprintf(x...) printf(x) */
#define SDRAM_SIZE ( 16 * 1024 * 1024 )
#define CODE_SIZE ( 4 * 1024 * 1024 )
/* #define START_ADDR ( 0x80000000 + SDRAM_SIZE - CODE_SIZE ) */
#define NAND_MAX_BLK_NUM 10000000 /*((Hand.nand_pn / Hand.nand_ppb) + 1)*/
#define NAND_MAX_PAGE_NUM 1073740824 /*Hand.nand_pn */
#define NAND_SECTION_NUM 23
#define MAX_TRANSFER_SIZE 0x100000
#define MAX_LOAD_SIZE 0x3000
#define NAND_MAX_BYTE_NUM (Hand.nand_pn * Hand.nand_ps)
#define IOCTL_INBUF_SIZE 512
#define IOCTL_OUTBUF_SIZE 512
#define MAX_DEV_NUM 16
enum CPUTYPE {
JZ4740,
JZ4750,
};
enum USB_Boot_State {
DISCONNECT,
CONNECT,
BOOT,
UNBOOT
};
enum OPTION {
OOB_ECC,
OOB_NO_ECC,
NO_OOB,
};
enum NOR_OPS_TYPE {
NOR_INIT = 0,
NOR_QUERY,
NOR_WRITE,
NOR_ERASE_CHIP,
NOR_ERASE_SECTOR
};
enum NOR_FLASH_TYPE
{
NOR_AM29 = 0,
NOR_SST28,
NOR_SST39x16,
NOR_SST39x8
};
enum NAND_OPS_TYPE {
NAND_QUERY = 0,
NAND_INIT,
NAND_MARK_BAD,
NAND_READ_OOB,
NAND_READ_RAW,
NAND_ERASE,
NAND_READ,
NAND_PROGRAM,
NAND_READ_TO_RAM
};
enum SDRAM_OPS_TYPE {
SDRAM_LOAD,
};
enum DATA_STRUCTURE_OB {
DS_flash_info ,
DS_hand
};
struct fw_args {
/* CPU ID */
unsigned int cpu_id;
/* PLL args */
unsigned char ext_clk;
unsigned char cpu_speed;
unsigned char phm_div;
unsigned char use_uart;
unsigned int boudrate;
/* SDRAM args */
unsigned char bus_width;
unsigned char bank_num;
unsigned char row_addr;
unsigned char col_addr;
unsigned char is_mobile;
unsigned char is_busshare;
/* debug args */
unsigned char debug_ops;
unsigned char pin_num;
unsigned int start;
unsigned int size;
} __attribute__((packed));
struct hand {
/* nand flash info */
int pt; /* cpu type */
unsigned int nand_bw; /* bus width */
unsigned int nand_rc; /* row cycle */
unsigned int nand_ps; /* page size */
unsigned int nand_ppb; /* page number per block */
unsigned int nand_force_erase;
unsigned int nand_pn; /* page number in total */
unsigned int nand_os; /* oob size */
unsigned int nand_eccpos;
unsigned int nand_bbpage;
unsigned int nand_bbpos;
unsigned int nand_plane;
unsigned int nand_bchbit;
unsigned int nand_wppin;
unsigned int nand_bpc; /* block number per chip */
struct fw_args fw_args;
} __attribute__((packed));
struct nand_in {
unsigned char dev;
unsigned char max_chip;
unsigned char *buf;
unsigned char *cs_map;
unsigned int start;
unsigned int length;
unsigned int option;
int (* check) (unsigned char *,unsigned char *,unsigned int);
};
struct nand_out {
unsigned char *status;
};
struct sdram_in {
unsigned char dev;
unsigned char *buf;
unsigned int start;
unsigned int length;
unsigned int option;
};
#endif /* __USB_BOOT_DEFINES_H__ */