]> cloudbase.mooo.com Git - z180-stamp.git/blame - avr/cmd_boot.c
cmd_attach.c, cmd_boot.c, cmd_loadcpm3.c: use cmd_error()
[z180-stamp.git] / avr / cmd_boot.c
CommitLineData
35edb766 1/*
04b3ea0e 2 * (C) Copyright 2014-2016 Leo C. <erbl259-lmu@yahoo.de>
35edb766
L
3 *
4 * (C) Copyright 2000-2003
5 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
6 *
19a463f4 7 * SPDX-License-Identifier: GPL-2.0
35edb766 8 */
534e1dfc
L
9
10/*
11 * Misc boot support
12 */
7eecbdac 13#include "cmd_boot.h"
8a7decea 14#include <ctype.h>
89adce76 15#include <util/atomic.h>
534e1dfc 16
04b3ea0e
L
17#include "cli_readline.h" /* console_buffer[] */
18#include "cli.h" /* run_command() */
612a6965 19#include "env.h"
2d914b45 20#include "eval_arg.h"
89adce76 21#include "con-utils.h"
a2907f2e 22#include "getopt-min.h"
534e1dfc 23#include "z80-if.h"
cb4fb1ed 24#include "z180-serv.h" /* restart_z180_serv() */
8a7decea 25#include "debug.h"
534e1dfc
L
26
27/* ugly hack to get Z180 loadfile into flash memory */
28#define const const FLASH
29#include "../z180/hdrom.h"
a2907f2e 30#include "../z180/cfboot.h"
534e1dfc
L
31#undef const
32
33
34
a2907f2e
L
35static void z80_load_mem(int_fast8_t verbosity,
36 const FLASH unsigned char data[],
37 const FLASH unsigned long *sections,
38 const FLASH unsigned long address[],
39 const FLASH unsigned long length_of_sections[])
534e1dfc 40{
a2907f2e
L
41 uint32_t sec_base = 0;
42
43 if (verbosity > 1)
44 printf_P(PSTR("Loading Z180 memory... \n"));
45
46 for (unsigned sec = 0; sec < *sections; sec++) {
47 if (verbosity > 0) {
48 printf_P(PSTR(" From: 0x%.5lX to: 0x%.5lX (%5li bytes)\n"),
49 address[sec],
50 address[sec]+length_of_sections[sec] - 1,
51 length_of_sections[sec]);
52 }
53
54 z80_write_block_P((const FLASH unsigned char *) &data[sec_base], /* src */
55 address[sec], /* dest */
56 length_of_sections[sec]); /* len */
57 sec_base += length_of_sections[sec];
534e1dfc
L
58 }
59}
60
5e8ac5e0 61command_ret_t do_loadf(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
534e1dfc 62{
5e8ac5e0 63 ERRNUM res = ESUCCESS;
534e1dfc 64
6035a17b 65 if (z80_bus_state() & ZST_RUNNING) {
5e8ac5e0
L
66 res = ERUNNING;
67 } else if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
68 res = EBUSTO;
612a6965 69 }
5e8ac5e0
L
70 if (res != ESUCCESS)
71 cmd_error(CMD_RET_FAILURE, res, NULL);
72
a2907f2e
L
73 z80_load_mem(2, hdrom,
74 &hdrom_sections,
75 hdrom_address,
76 hdrom_length_of_sections);
77
612a6965 78 z80_bus_cmd(Release);
6035a17b 79
d0581f88 80 return CMD_RET_SUCCESS;
534e1dfc
L
81}
82
83
a2907f2e
L
84void print_vars(char *title)
85{
86 uint8_t buf[5];
87 zstate_t state = z80_bus_state();
88
89 if((state & ZST_ACQUIRED) == 0)
90 z80_bus_cmd(Request);
91
92 z80_read_block(buf, 9, sizeof buf);
93
94 if((state & ZST_ACQUIRED) == 0)
95 z80_bus_cmd(Release);
96
97 printf_P(PSTR("%s: stage: %d, flag: 0x%.02x, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
98 title, buf[0], buf[1], buf[2], buf[3], buf[4]);
99}
100
101
102/*
103 * bootcf [options]
104 *
105 * -a address (100h)
106 * -s start sector (0)
107 * -c sector count (7)
108 * -i Partition id (52)
109 * -n load only
110 * -t timeout (10000)
111 * -v verbose
112 */
113
5e8ac5e0 114command_ret_t do_bootcf(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
a2907f2e
L
115{
116 struct {
117 uint8_t jr[2];
118 uint16_t loadaddr;
119 uint8_t sec_start;
120 uint8_t sec_cnt;
121 uint8_t part_id;
122 uint16_t timeout;
16af58ea 123 uint8_t stages;
a2907f2e
L
124 } boot_param;
125
126 struct {
16af58ea 127 uint8_t stages;
a2907f2e
L
128 uint8_t done;
129 uint8_t result;
130 uint8_t ide_stat;
131 uint8_t ide_error;
132 } boot_res;
133
134 int_fast8_t verbosity = 0;
16af58ea 135 uint8_t default_stages;
a2907f2e 136 uint32_t val;
5e8ac5e0 137 ERRNUM res = ESUCCESS;
a2907f2e
L
138
139 /* get default values */
140 memcpy_P(&boot_param, cfboot, sizeof boot_param);
16af58ea 141 default_stages = boot_param.stages;
a2907f2e
L
142
143 /* reset getopt() */
144 optind = 0;
145
146 int opt;
147 while ((opt = getopt(argc, argv, PSTR("vna:s:c:t:i:"))) != -1) {
148 switch (opt) {
149 case 'v':
150 verbosity++;
151 break;
152 case 'n':
16af58ea
L
153 if (boot_param.stages > 0)
154 boot_param.stages--;
a2907f2e
L
155 break;
156 case 'a':
2d914b45 157 val = eval_arg(optarg, NULL);
a2907f2e
L
158 if (val < 0x100 || val > 0xFE00) {
159 printf_P(PSTR("Address out of range: 0x%.4lX\n"), val);
160 return CMD_RET_FAILURE;
161 }
162 boot_param.loadaddr = val;
163 break;
164 case 's':
2d914b45 165 val = eval_arg(optarg, NULL);
a2907f2e
L
166 if (val > 255) {
167 printf_P(PSTR("Start sector out of range: 0x%lX\n"), val);
168 return CMD_RET_FAILURE;
169 }
170 boot_param.sec_start = val;
171 break;
172 case 'c':
2d914b45 173 val = eval_arg(optarg, NULL);
a2907f2e
L
174 if (val > 127) {
175 printf_P(PSTR("Sector count out of range: 0x%lX\n"), val);
176 return CMD_RET_FAILURE;
177 }
178 boot_param.sec_cnt = val;
179 break;
180 case 't':
2d914b45 181 val = eval_arg(optarg, NULL);
a2907f2e
L
182 if (val < 0x1 || val > 0xFFFF) {
183 printf_P(PSTR("Timeout value out of range: 0x%lX\n"), val);
184 return CMD_RET_FAILURE;
185 }
16af58ea 186 boot_param.timeout = val;
a2907f2e
L
187 break;
188 case 'i':
2d914b45 189 val = eval_arg(optarg, NULL);
a2907f2e
L
190 if (val < 0x01 || val > 0xFF) {
191 printf_P(PSTR("Partition id out of range: 0x%lX\n"), val);
192 return CMD_RET_FAILURE;
193 }
194 boot_param.part_id = val;
195 break;
196 default: /* '?' */
197 return CMD_RET_USAGE;
198 }
199 }
200
201 /* remaining arguments */
202 argc -= optind;
203 if (argc) {
204 my_puts_P(PSTR("Argument error!\n"));
205 return CMD_RET_USAGE;
206 }
207
208 if ((val = (uint32_t) boot_param.loadaddr + boot_param.sec_cnt * 512) >= 0xFF00) {
209 printf_P(PSTR("Top address out of range: 0x%.4lX\n"), val);
210 return CMD_RET_FAILURE;
211 }
212
a2907f2e 213 if (z80_bus_state() & ZST_RUNNING) {
5e8ac5e0
L
214 res = ERUNNING;
215 } else if (!(z80_bus_cmd(Request) & ZST_ACQUIRED)) {
216 res = EBUSTO;
a2907f2e 217 }
5e8ac5e0
L
218 if (res != ESUCCESS)
219 cmd_error(CMD_RET_FAILURE, res, NULL);
220
a2907f2e
L
221 z80_load_mem(verbosity, cfboot,
222 &cfboot_sections,
223 cfboot_address,
224 cfboot_length_of_sections);
225
226 z80_write_block((const uint8_t *) &boot_param,
227 cfboot_address[0], sizeof boot_param);
228 z80_bus_cmd(Release);
229
16af58ea 230 if (boot_param.stages == 0) {
a2907f2e
L
231 printf_P(PSTR("Bootloader loaded at: 0x%.4X\n"), (uint16_t) cfboot_address[0]);
232 } else {
233 printf_P(PSTR("Executing %d of %d Bootloader stages...\n"),
16af58ea 234 boot_param.stages, default_stages);
a2907f2e
L
235
236 z80_bus_cmd(Run);
237 z80_bus_cmd(Release);
238
239 clear_ctrlc(); /* forget any previous Control C */
240 for (boot_res.done = 0; boot_res.done != 0xFF;) {
241 _delay_ms(8);
242 /* check for ctrl-c to abort... */
243 if (had_ctrlc() || ctrlc()) {
244 break;
245 }
246 z80_bus_cmd(Request);
247 z80_read_block((uint8_t *) &boot_res,
248 cfboot_address[0]+sizeof boot_param - 1, sizeof boot_res);
249 z80_bus_cmd(Release);
250 }
251
252 if (boot_res.done != 0xFF) {
253 z80_bus_cmd(Reset);
254 my_puts_P(PSTR("Abort\n"));
255 } else {
16af58ea
L
256 if (boot_param.stages == default_stages &&
257 boot_res.stages == 0 &&
258 boot_res.result == 0) {
a2907f2e
L
259 my_puts_P(PSTR("Booting...\n"));
260 } else {
261 z80_bus_cmd(Reset);
16af58ea 262 boot_res.stages++;
a2907f2e 263 printf_P(PSTR("Bootloader stopped at stage %d, result: %d, IDE stat/error: 0x%.02x/0x%.02x\n"),
16af58ea 264 boot_param.stages - boot_res.stages,
a2907f2e
L
265 boot_res.result, boot_res.ide_stat, boot_res.ide_error);
266 }
267 }
268 }
269
270 return CMD_RET_SUCCESS;
271}
272
5e8ac5e0 273command_ret_t do_busreq_pulse(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
534e1dfc 274{
f338df2a 275 uint16_t count=1;
534e1dfc 276
6035a17b 277 if (!(z80_bus_state() & ZST_RUNNING)) {
f338df2a 278 printf_P(PSTR("## CPU is not running!\n"));
d0581f88 279 return CMD_RET_FAILURE;
534e1dfc
L
280 }
281
f338df2a 282 if (argc > 1)
2d914b45 283 count = (uint16_t) eval_arg(argv[1], NULL);
f338df2a 284
62f624d3 285 z80_bus_cmd(Request);
f338df2a 286 while (count--)
62f624d3 287 z80_bus_cmd(M_Cycle);
534e1dfc 288
d0581f88 289 return CMD_RET_SUCCESS;
f338df2a
L
290}
291
292
5e8ac5e0 293command_ret_t do_go(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc, char * const argv[])
f338df2a
L
294{
295 uint32_t addr;
296
f338df2a
L
297 if (argc < 2)
298 return CMD_RET_USAGE;
2d914b45 299 addr = eval_arg(argv[1], NULL);
f338df2a 300 if (addr >= (1UL<<16)) {
534e1dfc
L
301 printf_P(PSTR("## Startaddress 0x%05lx too high.\n"
302 " (Out of logical address space (0x00000-0x0ffff))\n"),
303 addr);
d0581f88 304 return CMD_RET_FAILURE;
6035a17b 305 }
f338df2a 306
6035a17b 307 if (z80_bus_state() & ZST_RUNNING) {
5e8ac5e0 308 printf_P(PSTR("## CPU already running!\n"));
d0581f88 309 return CMD_RET_FAILURE;
534e1dfc
L
310 }
311
f338df2a
L
312 printf_P(PSTR("## Starting application at 0x%04lx ...\n"), addr);
313
314 if (addr != 0) {
315 uint8_t tmp[3];
6035a17b 316
62f624d3 317 z80_bus_cmd(Request);
19a463f4 318 z80_read_block (tmp, 0, 3);
f338df2a
L
319 z80_write(0, 0xc3);
320 z80_write(1, addr);
321 z80_write(2, (addr >> 8));
322
62f624d3
L
323 z80_bus_cmd(Run);
324 z80_bus_cmd(M_Cycle);
325 z80_bus_cmd(M_Cycle);
19a463f4 326 z80_write_block(tmp, 0, 3);
f338df2a 327 } else
62f624d3 328 z80_bus_cmd(Run);
6035a17b 329
62f624d3 330 z80_bus_cmd(Release);
f338df2a 331
d0581f88 332 return CMD_RET_SUCCESS;
534e1dfc
L
333}
334
8a7decea
L
335static
336void reset_cpu(bus_cmd_t mode)
337{
338 restart_z180_serv();
339 z80_bus_cmd(mode);
340}
341
342
5e8ac5e0 343command_ret_t do_reset(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
534e1dfc 344{
612a6965 345 printf_P(PSTR("CPU now in reset state.\n"));
534e1dfc 346
8a7decea 347 reset_cpu(Reset);
d0581f88 348 return CMD_RET_SUCCESS;
534e1dfc
L
349}
350
5e8ac5e0 351command_ret_t do_restart(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
534e1dfc 352{
8a7decea 353 reset_cpu(Restart);
534e1dfc 354
d0581f88 355 return CMD_RET_SUCCESS;
534e1dfc
L
356}
357
8a7decea
L
358static
359void print_con_usage(char esc)
360{ printf_P(PSTR("\n"
361 "------------------------------------------------\n"
362 " ?,H - This Help\n"
8a7decea 363 " Q,X - Return to command line\n"
b08e079d
L
364 " R - Reset (Restart) CPU\n"
365 " : - Execute monitor command\n"
612a6965
L
366 " \\ - code input:\n"
367 " \\nnn 3 decimal digits character code\n"
368 " \\Xhh 2 hexadecimal digits character code\n"
369 " ^%c - (Escape char) Type again to send itself\n"
8a7decea
L
370 "key>"
371 ), esc + 0x40);
372}
89adce76 373
5e8ac5e0 374command_ret_t do_console(cmd_tbl_t *cmdtp UNUSED, uint_fast8_t flag UNUSED, int argc UNUSED, char * const argv[] UNUSED)
89adce76
L
375{
376 int ch;
8a7decea 377 uint8_t pending;
8a7decea
L
378 uint8_t code = 0;
379 uint8_t state = 0;
612a6965 380 char esc_char = (char) getenv_ulong(PSTR(ENV_ESC_CHAR), 16, CONFIG_ESC_CHAR);
ea6971b8 381
b08e079d
L
382 printf_P(PSTR("Connecting to CPU. Escape character is '^%c'.\n"),
383 esc_char + 0x40);
89adce76
L
384
385 while (1) {
386
8a7decea 387 ATOMIC_BLOCK(ATOMIC_FORCEON) {
89adce76
L
388 pending = (Stat & S_CON_PENDING) != 0;
389 Stat &= ~S_CON_PENDING;
390 }
3ad4143b
L
391 if (pending) {
392 uint8_t count = 100;
393 while ((ch = z80_memfifo_getc(fifo_conout)) >= 0 && --count)
89adce76 394 putchar(ch);
3ad4143b 395 }
89adce76
L
396
397 if ((ch = my_getchar(0)) >= 0) {
398 switch (state) {
399 case 0:
612a6965 400 if (ch == esc_char) {
89adce76
L
401 state = 1;
402 /* TODO: Timer starten */
403 } else {
404 z80_memfifo_putc(fifo_conin, ch);
ea6971b8 405 }
89adce76 406 break;
8a7decea 407 case 2:
5e8ac5e0 408 my_puts_P(PSTR("\n"
8a7decea 409 "------------------------------------------------\n"));
89adce76 410 case 1:
8a7decea
L
411 state = 0;
412 switch (toupper(ch)) {
89adce76 413
8a7decea
L
414 case '?':
415 case 'H':
612a6965 416 print_con_usage(esc_char);
8a7decea 417 state = 2;
89adce76
L
418 break;
419
8a7decea
L
420 case 'R':
421 reset_cpu(Restart);
89adce76
L
422 break;
423
8a7decea 424 case 'X':
89adce76 425 case 'Q':
5e8ac5e0 426 putchar('\n');
89adce76
L
427 goto quit;
428 break;
429
b08e079d
L
430 case ':':
431 putchar('\n');
8ed66016 432 int cmdlen = cli_readline(PSTR(": "), 1);
b08e079d
L
433 if (cmdlen > 0)
434 run_command(console_buffer, 0);
435 break;
436
8a7decea
L
437 case '\\':
438 code = 0;
439 state = 3;
440 break;
441
8a7decea 442 default:
612a6965
L
443 if (ch == esc_char)
444 z80_memfifo_putc(fifo_conin, ch);
8a7decea
L
445 break;
446 }
447 break;
448 case 3:
449 if (toupper(ch) == 'X') {
450 state = 6;
451 break;
452 }
453 /* fall thru */
454 case 4:
455 case 5:
456 if (isdigit(ch)) {
457 code = code * 10 + ch - '0';
458 state++;
b08e079d
L
459 } else {
460 if (state > 3)
461 z80_memfifo_putc(fifo_conin, code);
462 z80_memfifo_putc(fifo_conin, ch);
463 state = 0;
8a7decea
L
464 }
465 if (state > 5) {
466 z80_memfifo_putc(fifo_conin, code);
467 state = 0;
468 }
469 break;
470 case 6:
471 case 7:
472 if (isxdigit(ch)) {
473 ch = toupper(ch);
474 if (ch >= 'A')
475 ch -= 'A' - 10;
476 code = code * 16 + ch - '0';
477 state++;
b08e079d
L
478 }else {
479 if (state > 6)
480 z80_memfifo_putc(fifo_conin, code);
481 z80_memfifo_putc(fifo_conin, ch);
482 state = 0;
8a7decea
L
483 }
484 if (state > 7) {
485 z80_memfifo_putc(fifo_conin, code);
486 state = 0;
89adce76 487 }
89adce76
L
488 break;
489 }
490 }
89adce76
L
491 }
492quit:
89adce76
L
493 return CMD_RET_SUCCESS;
494}