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 "command_line.h"
25#include "cmd.h"
26 
27static int handle_help(void)
28{
29    printf(
30        " boot boot device and make it in stage2\n"
31        " reset reset device\n"
32        " nquery query NAND flash info\n"
33        " nprog program NAND flash\n"
34        " nerase erase NAND flash\n"
35        " nmark mark a bad block in NAND flash\n"
36        " nread read NAND flash data with checking bad block and ECC\n"
37        " nreadraw read NAND flash data without checking bad block and ECC\n"
38        " nreadoob read NAND flash oob\n"
39        " gpios set one GPIO to high level\n"
40        " gpioc set one GPIO to low level\n"
41        " load load file data to SDRAM\n"
42        " go execute program in SDRAM\n"
43        " memtest memory test\n"
44        " help print this help\n"
45        " exit\n");
46
47    return 0;
48}
49
50/* need transfer two para :blk_num ,start_blk */
51int handle_nerase(void)
52{
53    if (com_argc < 5) {
54        printf(" Usage: nerase (1) (2) (3) (4)\n"
55               " 1:start block number\n"
56               " 2:block length\n"
57               " 3:device index number\n"
58               " 4:flash chip index number\n");
59        return -1;
60    }
61
62    init_nand_in();
63
64    nand_in.start = atoi(com_argv[1]);
65    nand_in.length = atoi(com_argv[2]);
66    nand_in.dev = atoi(com_argv[3]);
67    if (atoi(com_argv[4]) >= MAX_DEV_NUM) {
68        printf(" Flash index number overflow!\n");
69        return -1;
70    }
71    (nand_in.cs_map)[atoi(com_argv[4])] = 1;
72
73    if (nand_erase(&nand_in) < 1)
74        return -1;
75
76    return 0;
77}
78
79int handle_nmark(void)
80{
81    if (com_argc < 4) {
82        printf(" Usage: nerase (1) (2) (3)\n"
83               " 1:bad block number\n"
84               " 2:device index number\n"
85               " 3:flash chip index number\n");
86        return -1;
87    }
88
89    init_nand_in();
90
91    nand_in.start = atoi(com_argv[1]);
92    nand_in.dev = atoi(com_argv[2]);
93
94    if (atoi(com_argv[3]) >= MAX_DEV_NUM) {
95        printf(" Flash index number overflow!\n");
96        return -1;
97    }
98    (nand_in.cs_map)[atoi(com_argv[3])] = 1;
99
100    nand_markbad(&nand_in);
101    return 0;
102}
103
104int handle_memtest(void)
105{
106    unsigned int start, size;
107    if (com_argc != 2 && com_argc != 4) {
108        printf(" Usage: memtest (1) [2] [3]\n"
109               " 1:device index number\n"
110               " 2:SDRAM start address\n"
111               " 3:test size\n");
112        return -1;
113    }
114
115    if (com_argc == 4) {
116        start = strtoul(com_argv[2], NULL, 0);
117        size = strtoul(com_argv[3], NULL, 0);
118    } else {
119        start = 0;
120        size = 0;
121    }
122
123    debug_memory(start, size);
124    return 0;
125}
126
127int handle_gpio(int mode)
128{
129    if (com_argc < 3) {
130        printf(" Usage:"
131               " gpios (1) (2)\n"
132               " 1:GPIO pin number\n"
133               " 2:device index number\n");
134        return -1;
135    }
136
137    debug_gpio(mode, atoi(com_argv[1]));
138    return 0;
139}
140
141int handle_load(void)
142{
143    if (com_argc < 4) {
144        printf(" Usage:"
145               " load (1) (2) (3) \n"
146               " 1:SDRAM start address\n"
147               " 2:image file name\n"
148               " 3:device index number\n");
149
150        return -1;
151    }
152
153    sdram_in.start=strtoul(com_argv[1], NULL, 0);
154    sdram_in.dev = atoi(com_argv[3]);
155    sdram_in.buf = code_buf;
156
157    sdram_load_file(&sdram_in, com_argv[2]);
158    return 0;
159}
160
161int command_handle(char *buf)
162{
163    char *p = strtok(buf, "\n ");
164    if(p == NULL)
165        return 0;
166
167    com_argc = 0;
168    strcpy(com_argv[com_argc++], p);
169
170    while ((p = strtok(NULL, "\n ")) != NULL)
171        strcpy(com_argv[com_argc++], p);
172
173    if (!strcmp("boot", com_argv[0]))
174        boot(stage1, stage2);
175    else if (!strcmp("nprog", com_argv[0]))
176        nand_prog();
177    else if (!strcmp("nquery", com_argv[0]))
178        nand_query();
179    else if (!strcmp("nerase", com_argv[0]))
180        handle_nerase();
181    else if (!strcmp("nmark", com_argv[0]))
182        handle_nmark();
183    else if (!strcmp("nread", com_argv[0]))
184        nand_read(NAND_READ);
185    else if (!strcmp("nreadraw", com_argv[0]))
186        nand_read(NAND_READ_RAW);
187    else if (!strcmp("nreadoob", com_argv[0]))
188        nand_read(NAND_READ_OOB);
189    else if (!strcmp("gpios", com_argv[0]))
190        handle_gpio(2);
191    else if (!strcmp("gpioc", com_argv[0]))
192        handle_gpio(3);
193    else if (!strcmp("load", com_argv[0]))
194        handle_load();
195    else if (!strcmp("go", com_argv[0]))
196        debug_go();
197    else if (!strcmp("memtest", com_argv[0]))
198        handle_memtest();
199    else if (!strcmp("reset", com_argv[0]))
200        device_reset(0);
201    else if (!strcmp("help", com_argv[0]))
202        handle_help();
203    else if (!strcmp("exit", com_argv[0]))
204        return -1;
205    else
206        printf(" type `help` show all commands\n");
207
208    return 0;
209}
210

Archive Download this file



interactive