Rev 534 | Rev 548 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 534 | Rev 544 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2005 Jakub Jermar |
2 | * Copyright (C) 2005 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <console/kconsole.h> |
29 | #include <console/kconsole.h> |
30 | #include <console/console.h> |
30 | #include <console/console.h> |
31 | #include <console/chardev.h> |
31 | #include <console/chardev.h> |
32 | #include <print.h> |
32 | #include <print.h> |
33 | #include <panic.h> |
33 | #include <panic.h> |
34 | #include <typedefs.h> |
34 | #include <typedefs.h> |
35 | #include <arch/types.h> |
35 | #include <arch/types.h> |
36 | #include <list.h> |
36 | #include <list.h> |
37 | #include <arch.h> |
37 | #include <arch.h> |
38 | #include <func.h> |
38 | #include <func.h> |
39 | #include <macros.h> |
39 | #include <macros.h> |
40 | #include <debug.h> |
40 | #include <debug.h> |
41 | 41 | ||
42 | #define MAX_CMDLINE 256 |
42 | #define MAX_CMDLINE 256 |
43 | 43 | ||
44 | /** Simple kernel console. |
44 | /** Simple kernel console. |
45 | * |
45 | * |
46 | * The console is realized by kernel thread kconsole. |
46 | * The console is realized by kernel thread kconsole. |
47 | * It doesn't understand any useful command on its own, |
47 | * It doesn't understand any useful command on its own, |
48 | * but makes it possible for other kernel subsystems to |
48 | * but makes it possible for other kernel subsystems to |
49 | * register their own commands. |
49 | * register their own commands. |
50 | */ |
50 | */ |
51 | 51 | ||
52 | /** Locking. |
52 | /** Locking. |
53 | * |
53 | * |
54 | * There is a list of cmd_info_t structures. This list |
54 | * There is a list of cmd_info_t structures. This list |
55 | * is protected by cmd_lock spinlock. Note that specially |
55 | * is protected by cmd_lock spinlock. Note that specially |
56 | * the link elements of cmd_info_t are protected by |
56 | * the link elements of cmd_info_t are protected by |
57 | * this lock. |
57 | * this lock. |
58 | * |
58 | * |
59 | * Each cmd_info_t also has its own lock, which protects |
59 | * Each cmd_info_t also has its own lock, which protects |
60 | * all elements thereof except the link element. |
60 | * all elements thereof except the link element. |
61 | * |
61 | * |
62 | * cmd_lock must be acquired before any cmd_info lock. |
62 | * cmd_lock must be acquired before any cmd_info lock. |
63 | * When locking two cmd info structures, structure with |
63 | * When locking two cmd info structures, structure with |
64 | * lower address must be locked first. |
64 | * lower address must be locked first. |
65 | */ |
65 | */ |
66 | 66 | ||
67 | spinlock_t cmd_lock; /**< Lock protecting command list. */ |
67 | spinlock_t cmd_lock; /**< Lock protecting command list. */ |
68 | link_t cmd_head; /**< Command list. */ |
68 | link_t cmd_head; /**< Command list. */ |
69 | 69 | ||
70 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
70 | static cmd_info_t *parse_cmdline(char *cmdline, size_t len); |
71 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
71 | static bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end); |
72 | 72 | ||
73 | /** Data and methods for 'help' command. */ |
73 | /** Data and methods for 'help' command. */ |
74 | static int cmd_help(cmd_arg_t *argv); |
74 | static int cmd_help(cmd_arg_t *argv); |
75 | static cmd_info_t help_info; |
75 | static cmd_info_t help_info; |
76 | 76 | ||
77 | /** Data and methods for 'description' command. */ |
77 | /** Data and methods for 'description' command. */ |
78 | static int cmd_desc(cmd_arg_t *argv); |
78 | static int cmd_desc(cmd_arg_t *argv); |
79 | static void desc_help(void); |
79 | static void desc_help(void); |
80 | static cmd_info_t desc_info; |
80 | static cmd_info_t desc_info; |
81 | static char desc_buf[MAX_CMDLINE+1]; |
81 | static char desc_buf[MAX_CMDLINE+1]; |
82 | static cmd_arg_t desc_argv = { |
82 | static cmd_arg_t desc_argv = { |
83 | .type = ARG_TYPE_STRING, |
83 | .type = ARG_TYPE_STRING, |
84 | .buffer = desc_buf, |
84 | .buffer = desc_buf, |
85 | .len = sizeof(desc_buf) |
85 | .len = sizeof(desc_buf) |
86 | }; |
86 | }; |
87 | 87 | ||
- | 88 | /** Data and methods for 'halt' command. */ |
|
- | 89 | static int cmd_halt(cmd_arg_t *argv); |
|
- | 90 | static cmd_info_t halt_info; |
|
- | 91 | ||
88 | /** Initialize kconsole data structures. */ |
92 | /** Initialize kconsole data structures. */ |
89 | void kconsole_init(void) |
93 | void kconsole_init(void) |
90 | { |
94 | { |
91 | spinlock_initialize(&cmd_lock); |
95 | spinlock_initialize(&cmd_lock); |
92 | list_initialize(&cmd_head); |
96 | list_initialize(&cmd_head); |
93 | 97 | ||
94 | help_info.name = "help"; |
98 | help_info.name = "help"; |
95 | help_info.description = "List supported commands."; |
99 | help_info.description = "List supported commands."; |
96 | help_info.func = cmd_help; |
100 | help_info.func = cmd_help; |
97 | help_info.help = NULL; |
101 | help_info.help = NULL; |
98 | help_info.argc = 0; |
102 | help_info.argc = 0; |
99 | help_info.argv = NULL; |
103 | help_info.argv = NULL; |
100 | 104 | ||
101 | spinlock_initialize(&help_info.lock); |
105 | spinlock_initialize(&help_info.lock); |
102 | link_initialize(&help_info.link); |
106 | link_initialize(&help_info.link); |
103 | 107 | ||
104 | if (!cmd_register(&help_info)) |
108 | if (!cmd_register(&help_info)) |
105 | panic("could not register command %s\n", help_info.name); |
109 | panic("could not register command %s\n", help_info.name); |
106 | 110 | ||
107 | 111 | ||
108 | desc_info.name = "describe"; |
112 | desc_info.name = "describe"; |
109 | desc_info.description = "Describe specified command."; |
113 | desc_info.description = "Describe specified command."; |
110 | desc_info.help = desc_help; |
114 | desc_info.help = desc_help; |
111 | desc_info.func = cmd_desc; |
115 | desc_info.func = cmd_desc; |
112 | desc_info.argc = 1; |
116 | desc_info.argc = 1; |
113 | desc_info.argv = &desc_argv; |
117 | desc_info.argv = &desc_argv; |
114 | 118 | ||
115 | spinlock_initialize(&desc_info.lock); |
119 | spinlock_initialize(&desc_info.lock); |
116 | link_initialize(&desc_info.link); |
120 | link_initialize(&desc_info.link); |
117 | 121 | ||
118 | if (!cmd_register(&desc_info)) |
122 | if (!cmd_register(&desc_info)) |
119 | panic("could not register command %s\n", desc_info.name); |
123 | panic("could not register command %s\n", desc_info.name); |
- | 124 | ||
- | 125 | ||
- | 126 | halt_info.name = "halt"; |
|
- | 127 | halt_info.description = "Halt the kernel."; |
|
- | 128 | halt_info.func = cmd_halt; |
|
- | 129 | halt_info.help = NULL; |
|
- | 130 | halt_info.argc = 0; |
|
- | 131 | halt_info.argv = NULL; |
|
- | 132 | ||
- | 133 | spinlock_initialize(&halt_info.lock); |
|
- | 134 | link_initialize(&halt_info.link); |
|
- | 135 | ||
- | 136 | if (!cmd_register(&halt_info)) |
|
- | 137 | panic("could not register command %s\n", halt_info.name); |
|
120 | } |
138 | } |
121 | 139 | ||
122 | 140 | ||
123 | /** Register kconsole command. |
141 | /** Register kconsole command. |
124 | * |
142 | * |
125 | * @param cmd Structure describing the command. |
143 | * @param cmd Structure describing the command. |
126 | * |
144 | * |
127 | * @return 0 on failure, 1 on success. |
145 | * @return 0 on failure, 1 on success. |
128 | */ |
146 | */ |
129 | int cmd_register(cmd_info_t *cmd) |
147 | int cmd_register(cmd_info_t *cmd) |
130 | { |
148 | { |
131 | ipl_t ipl; |
149 | ipl_t ipl; |
132 | link_t *cur; |
150 | link_t *cur; |
133 | 151 | ||
134 | ipl = interrupts_disable(); |
152 | ipl = interrupts_disable(); |
135 | spinlock_lock(&cmd_lock); |
153 | spinlock_lock(&cmd_lock); |
136 | 154 | ||
137 | /* |
155 | /* |
138 | * Make sure the command is not already listed. |
156 | * Make sure the command is not already listed. |
139 | */ |
157 | */ |
140 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
158 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
141 | cmd_info_t *hlp; |
159 | cmd_info_t *hlp; |
142 | 160 | ||
143 | hlp = list_get_instance(cur, cmd_info_t, link); |
161 | hlp = list_get_instance(cur, cmd_info_t, link); |
144 | 162 | ||
145 | if (hlp == cmd) { |
163 | if (hlp == cmd) { |
146 | /* The command is already there. */ |
164 | /* The command is already there. */ |
147 | spinlock_unlock(&cmd_lock); |
165 | spinlock_unlock(&cmd_lock); |
148 | interrupts_restore(ipl); |
166 | interrupts_restore(ipl); |
149 | return 0; |
167 | return 0; |
150 | } |
168 | } |
151 | 169 | ||
152 | /* Avoid deadlock. */ |
170 | /* Avoid deadlock. */ |
153 | if (hlp < cmd) { |
171 | if (hlp < cmd) { |
154 | spinlock_lock(&hlp->lock); |
172 | spinlock_lock(&hlp->lock); |
155 | spinlock_lock(&cmd->lock); |
173 | spinlock_lock(&cmd->lock); |
156 | } else { |
174 | } else { |
157 | spinlock_lock(&cmd->lock); |
175 | spinlock_lock(&cmd->lock); |
158 | spinlock_lock(&hlp->lock); |
176 | spinlock_lock(&hlp->lock); |
159 | } |
177 | } |
160 | 178 | ||
161 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
179 | if ((strncmp(hlp->name, cmd->name, strlen(cmd->name)) == 0)) { |
162 | /* The command is already there. */ |
180 | /* The command is already there. */ |
163 | spinlock_unlock(&hlp->lock); |
181 | spinlock_unlock(&hlp->lock); |
164 | spinlock_unlock(&cmd->lock); |
182 | spinlock_unlock(&cmd->lock); |
165 | spinlock_unlock(&cmd_lock); |
183 | spinlock_unlock(&cmd_lock); |
166 | interrupts_restore(ipl); |
184 | interrupts_restore(ipl); |
167 | return 0; |
185 | return 0; |
168 | } |
186 | } |
169 | 187 | ||
170 | spinlock_unlock(&hlp->lock); |
188 | spinlock_unlock(&hlp->lock); |
171 | spinlock_unlock(&cmd->lock); |
189 | spinlock_unlock(&cmd->lock); |
172 | } |
190 | } |
173 | 191 | ||
174 | /* |
192 | /* |
175 | * Now the command can be added. |
193 | * Now the command can be added. |
176 | */ |
194 | */ |
177 | list_append(&cmd->link, &cmd_head); |
195 | list_append(&cmd->link, &cmd_head); |
178 | 196 | ||
179 | spinlock_unlock(&cmd_lock); |
197 | spinlock_unlock(&cmd_lock); |
180 | interrupts_restore(ipl); |
198 | interrupts_restore(ipl); |
181 | return 1; |
199 | return 1; |
182 | } |
200 | } |
183 | 201 | ||
184 | /** Kernel console managing thread. |
202 | /** Kernel console managing thread. |
185 | * |
203 | * |
186 | * @param arg Not used. |
204 | * @param arg Not used. |
187 | */ |
205 | */ |
188 | void kconsole(void *arg) |
206 | void kconsole(void *arg) |
189 | { |
207 | { |
190 | char cmdline[MAX_CMDLINE+1]; |
208 | char cmdline[MAX_CMDLINE+1]; |
191 | cmd_info_t *cmd_info; |
209 | cmd_info_t *cmd_info; |
192 | count_t len; |
210 | count_t len; |
193 | 211 | ||
194 | if (!stdin) { |
212 | if (!stdin) { |
195 | printf("%s: no stdin\n", __FUNCTION__); |
213 | printf("%s: no stdin\n", __FUNCTION__); |
196 | return; |
214 | return; |
197 | } |
215 | } |
198 | 216 | ||
199 | while (true) { |
217 | while (true) { |
200 | printf("%s> ", __FUNCTION__); |
218 | printf("%s> ", __FUNCTION__); |
201 | if (!(len = gets(stdin, cmdline, sizeof(cmdline)))) |
219 | if (!(len = gets(stdin, cmdline, sizeof(cmdline)))) |
202 | continue; |
220 | continue; |
203 | cmdline[len] = '\0'; |
221 | cmdline[len] = '\0'; |
204 | cmd_info = parse_cmdline(cmdline, len); |
222 | cmd_info = parse_cmdline(cmdline, len); |
205 | if (!cmd_info) |
223 | if (!cmd_info) |
206 | continue; |
224 | continue; |
207 | (void) cmd_info->func(cmd_info->argv); |
225 | (void) cmd_info->func(cmd_info->argv); |
208 | } |
226 | } |
209 | } |
227 | } |
210 | 228 | ||
211 | /** Parse command line. |
229 | /** Parse command line. |
212 | * |
230 | * |
213 | * @param cmdline Command line as read from input device. |
231 | * @param cmdline Command line as read from input device. |
214 | * @param len Command line length. |
232 | * @param len Command line length. |
215 | * |
233 | * |
216 | * @return Structure describing the command. |
234 | * @return Structure describing the command. |
217 | */ |
235 | */ |
218 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
236 | cmd_info_t *parse_cmdline(char *cmdline, size_t len) |
219 | { |
237 | { |
220 | index_t start = 0, end = 0; |
238 | index_t start = 0, end = 0; |
221 | cmd_info_t *cmd = NULL; |
239 | cmd_info_t *cmd = NULL; |
222 | link_t *cur; |
240 | link_t *cur; |
223 | ipl_t ipl; |
241 | ipl_t ipl; |
224 | int i; |
242 | int i; |
225 | 243 | ||
226 | if (!parse_argument(cmdline, len, &start, &end)) { |
244 | if (!parse_argument(cmdline, len, &start, &end)) { |
227 | /* Command line did not contain alphanumeric word. */ |
245 | /* Command line did not contain alphanumeric word. */ |
228 | return NULL; |
246 | return NULL; |
229 | } |
247 | } |
230 | 248 | ||
231 | ipl = interrupts_disable(); |
249 | ipl = interrupts_disable(); |
232 | spinlock_lock(&cmd_lock); |
250 | spinlock_lock(&cmd_lock); |
233 | 251 | ||
234 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
252 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
235 | cmd_info_t *hlp; |
253 | cmd_info_t *hlp; |
236 | 254 | ||
237 | hlp = list_get_instance(cur, cmd_info_t, link); |
255 | hlp = list_get_instance(cur, cmd_info_t, link); |
238 | spinlock_lock(&hlp->lock); |
256 | spinlock_lock(&hlp->lock); |
239 | 257 | ||
240 | if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
258 | if (strncmp(hlp->name, &cmdline[start], (end - start) + 1) == 0) { |
241 | cmd = hlp; |
259 | cmd = hlp; |
242 | break; |
260 | break; |
243 | } |
261 | } |
244 | 262 | ||
245 | spinlock_unlock(&hlp->lock); |
263 | spinlock_unlock(&hlp->lock); |
246 | } |
264 | } |
247 | 265 | ||
248 | spinlock_unlock(&cmd_lock); |
266 | spinlock_unlock(&cmd_lock); |
249 | 267 | ||
250 | if (!cmd) { |
268 | if (!cmd) { |
251 | /* Unknown command. */ |
269 | /* Unknown command. */ |
252 | printf("Unknown command.\n"); |
270 | printf("Unknown command.\n"); |
253 | interrupts_restore(ipl); |
271 | interrupts_restore(ipl); |
254 | return NULL; |
272 | return NULL; |
255 | } |
273 | } |
256 | 274 | ||
257 | /* cmd == hlp is locked */ |
275 | /* cmd == hlp is locked */ |
258 | 276 | ||
259 | /* |
277 | /* |
260 | * The command line must be further analyzed and |
278 | * The command line must be further analyzed and |
261 | * the parameters therefrom must be matched and |
279 | * the parameters therefrom must be matched and |
262 | * converted to those specified in the cmd info |
280 | * converted to those specified in the cmd info |
263 | * structure. |
281 | * structure. |
264 | */ |
282 | */ |
265 | 283 | ||
266 | for (i = 0; i < cmd->argc; i++) { |
284 | for (i = 0; i < cmd->argc; i++) { |
267 | char *buf; |
285 | char *buf; |
268 | start = end + 1; |
286 | start = end + 1; |
269 | if (!parse_argument(cmdline, len, &start, &end)) { |
287 | if (!parse_argument(cmdline, len, &start, &end)) { |
270 | printf("Too few arguments.\n"); |
288 | printf("Too few arguments.\n"); |
271 | spinlock_unlock(&cmd->lock); |
289 | spinlock_unlock(&cmd->lock); |
272 | interrupts_restore(ipl); |
290 | interrupts_restore(ipl); |
273 | return NULL; |
291 | return NULL; |
274 | } |
292 | } |
275 | 293 | ||
276 | switch (cmd->argv[i].type) { |
294 | switch (cmd->argv[i].type) { |
277 | case ARG_TYPE_STRING: |
295 | case ARG_TYPE_STRING: |
278 | buf = cmd->argv[i].buffer; |
296 | buf = cmd->argv[i].buffer; |
279 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1)); |
297 | strncpy(buf, (const char *) &cmdline[start], min((end - start) + 1, cmd->argv[i].len - 1)); |
280 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
298 | buf[min((end - start) + 1, cmd->argv[i].len - 1)] = '\0'; |
281 | break; |
299 | break; |
282 | case ARG_TYPE_INT: |
300 | case ARG_TYPE_INT: |
283 | case ARG_TYPE_INVALID: |
301 | case ARG_TYPE_INVALID: |
284 | default: |
302 | default: |
285 | panic("invalid argument type\n"); |
303 | panic("invalid argument type\n"); |
286 | break; |
304 | break; |
287 | } |
305 | } |
288 | } |
306 | } |
289 | 307 | ||
290 | start = end + 1; |
308 | start = end + 1; |
291 | if (parse_argument(cmdline, len, &start, &end)) { |
309 | if (parse_argument(cmdline, len, &start, &end)) { |
292 | printf("Too many arguments.\n"); |
310 | printf("Too many arguments.\n"); |
293 | spinlock_unlock(&cmd->lock); |
311 | spinlock_unlock(&cmd->lock); |
294 | interrupts_restore(ipl); |
312 | interrupts_restore(ipl); |
295 | return NULL; |
313 | return NULL; |
296 | } |
314 | } |
297 | 315 | ||
298 | spinlock_unlock(&cmd->lock); |
316 | spinlock_unlock(&cmd->lock); |
299 | interrupts_restore(ipl); |
317 | interrupts_restore(ipl); |
300 | return cmd; |
318 | return cmd; |
301 | } |
319 | } |
302 | 320 | ||
303 | /** Parse argument. |
321 | /** Parse argument. |
304 | * |
322 | * |
305 | * Find start and end positions of command line argument. |
323 | * Find start and end positions of command line argument. |
306 | * |
324 | * |
307 | * @param cmdline Command line as read from the input device. |
325 | * @param cmdline Command line as read from the input device. |
308 | * @param len Number of characters in cmdline. |
326 | * @param len Number of characters in cmdline. |
309 | * @param start On entry, 'start' contains pointer to the index |
327 | * @param start On entry, 'start' contains pointer to the index |
310 | * of first unprocessed character of cmdline. |
328 | * of first unprocessed character of cmdline. |
311 | * On successful exit, it marks beginning of the next argument. |
329 | * On successful exit, it marks beginning of the next argument. |
312 | * @param end Undefined on entry. On exit, 'end' points to the last character |
330 | * @param end Undefined on entry. On exit, 'end' points to the last character |
313 | * of the next argument. |
331 | * of the next argument. |
314 | * |
332 | * |
315 | * @return false on failure, true on success. |
333 | * @return false on failure, true on success. |
316 | */ |
334 | */ |
317 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
335 | bool parse_argument(char *cmdline, size_t len, index_t *start, index_t *end) |
318 | { |
336 | { |
319 | int i; |
337 | int i; |
320 | bool found_start = false; |
338 | bool found_start = false; |
321 | 339 | ||
322 | ASSERT(start != NULL); |
340 | ASSERT(start != NULL); |
323 | ASSERT(end != NULL); |
341 | ASSERT(end != NULL); |
324 | 342 | ||
325 | for (i = *start; i < len; i++) { |
343 | for (i = *start; i < len; i++) { |
326 | if (!found_start) { |
344 | if (!found_start) { |
327 | if (is_white(cmdline[i])) |
345 | if (is_white(cmdline[i])) |
328 | (*start)++; |
346 | (*start)++; |
329 | else |
347 | else |
330 | found_start = true; |
348 | found_start = true; |
331 | } else { |
349 | } else { |
332 | if (is_white(cmdline[i])) |
350 | if (is_white(cmdline[i])) |
333 | break; |
351 | break; |
334 | } |
352 | } |
335 | } |
353 | } |
336 | *end = i - 1; |
354 | *end = i - 1; |
337 | 355 | ||
338 | return found_start; |
356 | return found_start; |
339 | } |
357 | } |
340 | 358 | ||
341 | 359 | ||
342 | /** List supported commands. |
360 | /** List supported commands. |
343 | * |
361 | * |
344 | * @param argv Argument vector. |
362 | * @param argv Argument vector. |
345 | * |
363 | * |
346 | * @return 0 on failure, 1 on success. |
364 | * @return 0 on failure, 1 on success. |
347 | */ |
365 | */ |
348 | int cmd_help(cmd_arg_t *argv) |
366 | int cmd_help(cmd_arg_t *argv) |
349 | { |
367 | { |
350 | link_t *cur; |
368 | link_t *cur; |
351 | ipl_t ipl; |
369 | ipl_t ipl; |
352 | 370 | ||
353 | ipl = interrupts_disable(); |
371 | ipl = interrupts_disable(); |
354 | spinlock_lock(&cmd_lock); |
372 | spinlock_lock(&cmd_lock); |
355 | 373 | ||
356 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
374 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
357 | cmd_info_t *hlp; |
375 | cmd_info_t *hlp; |
358 | 376 | ||
359 | hlp = list_get_instance(cur, cmd_info_t, link); |
377 | hlp = list_get_instance(cur, cmd_info_t, link); |
360 | spinlock_lock(&hlp->lock); |
378 | spinlock_lock(&hlp->lock); |
361 | 379 | ||
362 | printf("%s - %s\n", hlp->name, hlp->description); |
380 | printf("%s - %s\n", hlp->name, hlp->description); |
363 | 381 | ||
364 | spinlock_unlock(&hlp->lock); |
382 | spinlock_unlock(&hlp->lock); |
365 | } |
383 | } |
366 | 384 | ||
367 | spinlock_unlock(&cmd_lock); |
385 | spinlock_unlock(&cmd_lock); |
368 | interrupts_restore(ipl); |
386 | interrupts_restore(ipl); |
369 | 387 | ||
370 | return 1; |
388 | return 1; |
371 | } |
389 | } |
372 | 390 | ||
373 | /** Describe specified command. |
391 | /** Describe specified command. |
374 | * |
392 | * |
375 | * @param argv Argument vector. |
393 | * @param argv Argument vector. |
376 | * |
394 | * |
377 | * @return 0 on failure, 1 on success. |
395 | * @return 0 on failure, 1 on success. |
378 | */ |
396 | */ |
379 | int cmd_desc(cmd_arg_t *argv) |
397 | int cmd_desc(cmd_arg_t *argv) |
380 | { |
398 | { |
381 | link_t *cur; |
399 | link_t *cur; |
382 | ipl_t ipl; |
400 | ipl_t ipl; |
383 | 401 | ||
384 | ipl = interrupts_disable(); |
402 | ipl = interrupts_disable(); |
385 | spinlock_lock(&cmd_lock); |
403 | spinlock_lock(&cmd_lock); |
386 | 404 | ||
387 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
405 | for (cur = cmd_head.next; cur != &cmd_head; cur = cur->next) { |
388 | cmd_info_t *hlp; |
406 | cmd_info_t *hlp; |
389 | 407 | ||
390 | hlp = list_get_instance(cur, cmd_info_t, link); |
408 | hlp = list_get_instance(cur, cmd_info_t, link); |
391 | spinlock_lock(&hlp->lock); |
409 | spinlock_lock(&hlp->lock); |
392 | 410 | ||
393 | if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { |
411 | if (strncmp(hlp->name, (const char *) argv->buffer, strlen(hlp->name)) == 0) { |
394 | printf("%s - %s\n", hlp->name, hlp->description); |
412 | printf("%s - %s\n", hlp->name, hlp->description); |
395 | if (hlp->help) |
413 | if (hlp->help) |
396 | hlp->help(); |
414 | hlp->help(); |
397 | spinlock_unlock(&hlp->lock); |
415 | spinlock_unlock(&hlp->lock); |
398 | break; |
416 | break; |
399 | } |
417 | } |
400 | 418 | ||
401 | spinlock_unlock(&hlp->lock); |
419 | spinlock_unlock(&hlp->lock); |
402 | } |
420 | } |
403 | 421 | ||
404 | spinlock_unlock(&cmd_lock); |
422 | spinlock_unlock(&cmd_lock); |
405 | interrupts_restore(ipl); |
423 | interrupts_restore(ipl); |
406 | 424 | ||
407 | return 1; |
425 | return 1; |
- | 426 | } |
|
- | 427 | ||
- | 428 | /** Halt the kernel. |
|
- | 429 | * |
|
- | 430 | * @param argv Argument vector. |
|
- | 431 | * |
|
- | 432 | * @return 0 on failure, 1 on success. |
|
- | 433 | */ |
|
- | 434 | int cmd_halt(cmd_arg_t *argv) |
|
- | 435 | { |
|
- | 436 | halt(); |
|
- | 437 | return 1; |
|
408 | } |
438 | } |
409 | 439 | ||
410 | /** Print detailed description of 'describe' command. */ |
440 | /** Print detailed description of 'describe' command. */ |
411 | void desc_help(void) |
441 | void desc_help(void) |
412 | { |
442 | { |
413 | printf("Syntax: describe command_name\n"); |
443 | printf("Syntax: describe command_name\n"); |
414 | } |
444 | } |
415 | 445 |