Root/usbboot/src/command_line.c

1/*
2 * Copyright(C) 2009 Qi Hardware Inc.,
3 * Authors: Xiangfu Liu <xiangfu@sharism.cc>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
22#include "usb_boot_defines.h"
23#include "ingenic_usb.h"
24#include "cmd.h"
25#include "xburst-tools_version.h"
26 
27extern struct nand_in nand_in;
28extern struct sdram_in sdram_in;
29extern unsigned char code_buf[4 * 512 * 1024];
30
31int com_argc;
32char com_argv[MAX_ARGC][MAX_COMMAND_LENGTH];
33char * stage1;
34char * stage2;
35
36static int handle_help(void)
37{
38    printf(
39    " boot boot device and make it in stage2\n"
40    " nprog program NAND flash\n"
41    " nquery query NAND flash info\n"
42    " nerase erase NAND flash\n"
43    " nmark mark a bad block in NAND flash\n"
44    " nread read NAND flash data with checking bad block and ECC\n"
45    " nreadraw read NAND flash data without checking bad block and ECC\n"
46    " nreadoob read NAND flash oob\n"
47    " gpios set one GPIO to high level\n"
48    " gpioc set one GPIO to low level\n"
49    " load load file data to SDRAM\n"
50    " go execute program in SDRAM\n"
51    " memtest memory test\n"
52    " help print this help\n"
53    " exit \n");
54
55    return 0;
56}
57
58/* need transfer two para :blk_num ,start_blk */
59int handle_nerase(void)
60{
61    if (com_argc < 5) {
62        printf(" Usage: nerase (1) (2) (3) (4)\n"
63               " 1:start block number\n"
64               " 2:block length\n"
65               " 3:device index number\n"
66               " 4:flash chip index number\n");
67        return -1;
68    }
69
70    init_nand_in();
71
72    nand_in.start = atoi(com_argv[1]);
73    nand_in.length = atoi(com_argv[2]);
74    nand_in.dev = atoi(com_argv[3]);
75    if (atoi(com_argv[4]) >= MAX_DEV_NUM) {
76        printf(" Flash index number overflow!\n");
77        return -1;
78    }
79    (nand_in.cs_map)[atoi(com_argv[4])] = 1;
80
81    if (nand_erase(&nand_in) < 1)
82        return -1;
83
84    return 0;
85}
86
87int handle_nmark(void)
88{
89    if (com_argc < 4) {
90        printf(" Usage: nerase (1) (2) (3)\n"
91               " 1:bad block number\n"
92               " 2:device index number\n"
93               " 3:flash chip index number\n");
94        return -1;
95    }
96
97    init_nand_in();
98
99    nand_in.start = atoi(com_argv[1]);
100    nand_in.dev = atoi(com_argv[2]);
101
102    if (atoi(com_argv[3]) >= MAX_DEV_NUM) {
103        printf(" Flash index number overflow!\n");
104        return -1;
105    }
106    (nand_in.cs_map)[atoi(com_argv[3])] = 1;
107
108    nand_markbad(&nand_in);
109    return 0;
110}
111
112int handle_memtest(void)
113{
114    unsigned int start, size;
115    if (com_argc != 2 && com_argc != 4) {
116        printf(" Usage: memtest (1) [2] [3]\n"
117               " 1:device index number\n"
118               " 2:SDRAM start address\n"
119               " 3:test size\n");
120        return -1;
121    }
122
123    if (com_argc == 4) {
124        start = strtoul(com_argv[2], NULL, 0);
125        size = strtoul(com_argv[3], NULL, 0);
126    } else {
127        start = 0;
128        size = 0;
129    }
130
131    debug_memory(atoi(com_argv[1]), start, size);
132    return 0;
133}
134
135int handle_gpio(int mode)
136{
137    if (com_argc < 3) {
138        printf(" Usage:"
139               " gpios (1) (2)\n"
140               " 1:GPIO pin number\n"
141               " 2:device index number\n");
142        return -1;
143    }
144
145    debug_gpio(atoi(com_argv[2]), mode, atoi(com_argv[1]));
146    return 0;
147}
148
149int handle_load(void)
150{
151    if (com_argc < 4) {
152        printf(" Usage:"
153               " load (1) (2) (3) \n"
154               " 1:SDRAM start address\n"
155               " 2:image file name\n"
156               " 3:device index number\n");
157
158        return -1;
159    }
160
161    sdram_in.start=strtoul(com_argv[1], NULL, 0);
162    sdram_in.dev = atoi(com_argv[3]);
163    sdram_in.buf = code_buf;
164
165    sdram_load_file(&sdram_in, com_argv[2]);
166    return 0;
167}
168
169int command_handle(char *buf)
170{
171    char *p = strtok(buf, "\n ");
172    if(p == NULL)
173        return 0;
174
175    com_argc = 0;
176    strcpy(com_argv[com_argc++], p);
177
178    while (p = strtok(NULL, "\n "))
179        strcpy(com_argv[com_argc++], p);
180
181    if (!strcmp("boot", com_argv[0]))
182        boot(stage1, stage2);
183    else if (!strcmp("nprog", com_argv[0]))
184        nand_prog();
185    else if (!strcmp("nquery", com_argv[0]))
186        nand_query();
187    else if (!strcmp("nerase", com_argv[0]))
188        handle_nerase();
189    else if (!strcmp("nmark", com_argv[0]))
190        handle_nmark();
191    else if (!strcmp("nread", com_argv[0]))
192        nand_read(NAND_READ);
193    else if (!strcmp("nreadraw", com_argv[0]))
194        nand_read(NAND_READ_RAW);
195    else if (!strcmp("nreadoob", com_argv[0]))
196        nand_read(NAND_READ_OOB);
197    else if (!strcmp("gpios", com_argv[0]))
198        handle_gpio(2);
199    else if (!strcmp("gpioc", com_argv[0]))
200        handle_gpio(3);
201    else if (!strcmp("load", com_argv[0]))
202        handle_load();
203    else if (!strcmp("go", com_argv[0]))
204        debug_go();
205    else if (!strcmp("memtest", com_argv[0]))
206        handle_memtest();
207    else if (!strcmp("reset", com_argv[0]))
208        device_reset(0);
209    else if (!strcmp("help", com_argv[0]))
210        handle_help();
211    else if (!strcmp("exit", com_argv[0]))
212        return -1;
213    else
214        printf(" type `help` show all commands\n", com_argv[0]);
215
216    return 0;
217}
218

Archive Download this file



interactive