summaryrefslogtreecommitdiffstats
path: root/src/console.c
blob: 3aa406c5e27d356e8490e7d0076007a371c84ad6 (plain)
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
/*
 *  0xFFFF - Open Free Fiasco Firmware Flasher
 *  Copyright (C) 2007  pancake <pancake@youterm.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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "main.h"
#include "query.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>

void cmd_exit(char *line)
{
	exit(0);
}

void cmd_help(char *line)
{
	printf("connect           connects via usb to nolo\n");
	printf("reboot            reboots remote host\n");
	printf("info              shows info of the remote system\n");
	printf("linfo             shows info of the local system\n");
	printf("shell             opens a shell (/bin/sh)\n");
	printf("badblocks [dev]   checks bad blocks on mtd (/dev/mtd1)\n");
	printf("dump [dir]        dumps the contents of /dev/mtd to dir\n");
	printf("nanddump [dev] [start] [len] [out] [ignore-badblocks] [ignore-oob]\n");
	printf("                  f.ex: nanddump /dev/mtd0 0x0 0x4000 xloader.bin 1 1\n");
	printf("exit              exits the shell\n");
	fflush(stdout);
}

void cmd_info(char *line)
{
	get_hw_revision(); // get hardware revision:
	get_root_device(); // only for flashing
	get_usb_mode();
	get_rd_mode();
	get_rd_flags();
}

void cmd_nanddump(char *line)
{
	char dev[128];
	int from;
	int length;
	char out[128];
	int ignbb;
	int ignoob = -1;

	sscanf(line, "%127s 0x%x 0x%x %127s %d %d",
		(char *)&dev, (unsigned int*)&from, (unsigned int *)&length,
		(char *)&out, (int*)&ignbb, (int *)&ignoob);

	if (ignoob == -1) {
		eprintf("Invalid arguments.\n");
		eprintf("nanddump [dev] [start] [len] [out] [ignore-badblocks] [ignore-oob]\n");
		eprintf("                  f.ex: nanddump /dev/mtd0 0x0 0x4000 xloader.bin 1 1\n");
	} else
		nanddump(dev, from, length, out, ignbb, ignoob);
}

void cmd_dump(char *line)
{
	if (!line[0]) {
		printf("Usage: dump [path]\n");
		return;
	}
	while(line[0]==' ') line = line +1;
	reverse_extract_pieces(line);
}

void cmd_badblocks(char *line)
{
	if (!line[0]) {
		printf("Usage: dump [path]\n");
		return;
	}
	while(line[0]==' ') line = line +1;
	check_badblocks(line);
}

void cmd_connect(char *line)
{
	connect_via_usb();
}

void cmd_reboot(char *line)
{
	reboot_board();
}

void cmd_shell(char *line)
{
	system("/bin/sh");
}

#define CMDS 11
#define IS_CMD(x) !strcmp(console_commands[i].name, x)
#define CALL_CMD(x) console_commands[x].callback((char *)line)
#define FOREACH_CMD(x) for(x=0;x<CMDS;x++)

struct cmd_t {
	char *name;
	void (*callback)(char *);
} console_commands[CMDS] = {
	{ .name = "exit",      .callback = &cmd_exit },
	{ .name = "q",         .callback = &cmd_exit },
	{ .name = "connect",   .callback = &cmd_connect },
	{ .name = "reboot",    .callback = &cmd_reboot },
	{ .name = "badblocks", .callback = &cmd_badblocks},
	{ .name = "help",      .callback = &cmd_help },
	{ .name = "?",         .callback = &cmd_help },
	{ .name = "info",      .callback = &cmd_info },
	{ .name = "dump",      .callback = &cmd_dump },
	{ .name = "nanddump",  .callback = &cmd_nanddump },
	{ .name = "shell",     .callback = &cmd_shell }
};

static int console_command(const char *line)
{
	int i;
	char command[11];

	command[0] = '\0';
	sscanf(line, "%10s", command);
	line = line + strlen(command);

	FOREACH_CMD(i)
		if (IS_CMD(command))
			CALL_CMD(i);
	//
	return 1;
}

int console_prompt()
{
	char line[1024];

	do {
		write(1, "0xFFFF> ", 8);
		line[0] = '\0';
		fgets(line, 1024, stdin);
		if (feof(stdin)) {
			write(1, "\n", 1);
			break;
		}
		if (line[0]) line[strlen(line)-1] = '\0';
		line[1023] = '\0';
	} while(console_command(line));

	return 0;
}