main.c revision 1.29 1 /* $NetBSD: main.c,v 1.29 2018/02/08 09:05:18 dholland Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Tohru Nishimura.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/reboot.h>
34
35 #include <lib/libsa/stand.h>
36 #include <lib/libsa/loadfile.h>
37 #include <lib/libkern/libkern.h>
38
39 #include <machine/bootinfo.h>
40
41 #include "globals.h"
42
43 static const struct bootarg {
44 const char *name;
45 int value;
46 } bootargs[] = {
47 { "multi", RB_AUTOBOOT },
48 { "auto", RB_AUTOBOOT },
49 { "ask", RB_ASKNAME },
50 { "single", RB_SINGLE },
51 { "ddb", RB_KDB },
52 { "userconf", RB_USERCONF },
53 { "norm", AB_NORMAL },
54 { "quiet", AB_QUIET },
55 { "verb", AB_VERBOSE },
56 { "silent", AB_SILENT },
57 { "debug", AB_DEBUG },
58 { "altboot", -1 }
59 };
60
61 /* default PATA drive configuration is "10": single master on first channel */
62 static char *drive_config = "10";
63
64 void *bootinfo; /* low memory reserved to pass bootinfo structures */
65 int bi_size; /* BOOTINFO_MAXSIZE */
66 char *bi_next;
67
68 void bi_init(void *);
69 void bi_add(void *, int, int);
70
71 struct btinfo_memory bi_mem;
72 struct btinfo_console bi_cons;
73 struct btinfo_clock bi_clk;
74 struct btinfo_prodfamily bi_fam;
75 struct btinfo_model bi_model;
76 struct btinfo_bootpath bi_path;
77 struct btinfo_rootdevice bi_rdev;
78 struct btinfo_net bi_net;
79 struct btinfo_modulelist *btinfo_modulelist;
80 size_t btinfo_modulelist_size;
81
82 struct boot_module {
83 char *bm_kmod;
84 ssize_t bm_len;
85 struct boot_module *bm_next;
86 };
87 struct boot_module *boot_modules;
88 char module_base[80];
89 uint32_t kmodloadp;
90 int modules_enabled = 0;
91
92 void module_add(const char *);
93 void module_load(const char *);
94 int module_open(struct boot_module *);
95
96 void main(int, char **, char *, char *);
97
98 extern char bootprog_name[], bootprog_rev[];
99 extern char newaltboot[], newaltboot_end[];
100
101 struct pcidev lata[2];
102 struct pcidev lnif[2];
103 struct pcidev lusb[3];
104 int nata, nnif, nusb;
105
106 int brdtype;
107 uint32_t busclock, cpuclock;
108
109 static int check_bootname(char *);
110 static int input_cmdline(char **, int);
111 static int parse_cmdline(char **, int, char *, char *);
112 static int is_space(char);
113 #ifdef DEBUG
114 static void sat_test(void);
115 static void findflash(void);
116 #endif
117
118 #define BNAME_DEFAULT "wd0:"
119 #define MAX_ARGS 10
120
121 void
122 main(int argc, char *argv[], char *bootargs_start, char *bootargs_end)
123 {
124 unsigned long marks[MARK_MAX];
125 struct brdprop *brdprop;
126 char *new_argv[MAX_ARGS];
127 char *bname;
128 ssize_t len;
129 int err, fd, howto, i, n;
130
131 printf("\n>> %s altboot, revision %s\n", bootprog_name, bootprog_rev);
132
133 brdprop = brd_lookup(brdtype);
134 printf(">> %s, cpu %u MHz, bus %u MHz, %dMB SDRAM\n", brdprop->verbose,
135 cpuclock / 1000000, busclock / 1000000, bi_mem.memsize >> 20);
136
137 nata = pcilookup(PCI_CLASS_IDE, lata, 2);
138 if (nata == 0)
139 nata = pcilookup(PCI_CLASS_RAID, lata, 2);
140 if (nata == 0)
141 nata = pcilookup(PCI_CLASS_MISCSTORAGE, lata, 2);
142 if (nata == 0)
143 nata = pcilookup(PCI_CLASS_SCSI, lata, 2);
144 nnif = pcilookup(PCI_CLASS_ETH, lnif, 2);
145 nusb = pcilookup(PCI_CLASS_USB, lusb, 3);
146
147 #ifdef DEBUG
148 if (nata == 0)
149 printf("No IDE/SATA found\n");
150 else for (n = 0; n < nata; n++) {
151 int b, d, f, bdf, pvd;
152 bdf = lata[n].bdf;
153 pvd = lata[n].pvd;
154 pcidecomposetag(bdf, &b, &d, &f);
155 printf("%04x.%04x DSK %02d:%02d:%02d\n",
156 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
157 }
158 if (nnif == 0)
159 printf("no NET found\n");
160 else for (n = 0; n < nnif; n++) {
161 int b, d, f, bdf, pvd;
162 bdf = lnif[n].bdf;
163 pvd = lnif[n].pvd;
164 pcidecomposetag(bdf, &b, &d, &f);
165 printf("%04x.%04x NET %02d:%02d:%02d\n",
166 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
167 }
168 if (nusb == 0)
169 printf("no USB found\n");
170 else for (n = 0; n < nusb; n++) {
171 int b, d, f, bdf, pvd;
172 bdf = lusb[0].bdf;
173 pvd = lusb[0].pvd;
174 pcidecomposetag(bdf, &b, &d, &f);
175 printf("%04x.%04x USB %02d:%02d:%02d\n",
176 PCI_VENDOR(pvd), PCI_PRODUCT(pvd), b, d, f);
177 }
178 #endif
179
180 pcisetup();
181 pcifixup();
182
183 /*
184 * When argc is too big then it is probably a pointer, which could
185 * indicate that we were launched as a Linux kernel module using
186 * "bootm".
187 */
188 if (argc > MAX_ARGS) {
189 if (argv != NULL) {
190 /*
191 * initrd image was loaded:
192 * check if it contains a valid altboot command line
193 */
194 char *p = (char *)argv;
195
196 if (strncmp(p, "altboot:", 8) == 0) {
197 *p = 0;
198 for (p = p + 8; *p >= ' '; p++);
199 argc = parse_cmdline(new_argv, MAX_ARGS,
200 ((char *)argv) + 8, p);
201 argv = new_argv;
202 } else
203 argc = 0; /* boot default */
204 } else {
205 /* parse standard Linux bootargs */
206 argc = parse_cmdline(new_argv, MAX_ARGS,
207 bootargs_start, bootargs_end);
208 argv = new_argv;
209 }
210 }
211
212 /* look for a PATA drive configuration string under the arguments */
213 for (n = 1; n < argc; n++) {
214 if (strncmp(argv[n], "ide:", 4) == 0 &&
215 argv[n][4] >= '0' && argv[n][4] <= '2') {
216 drive_config = &argv[n][4];
217 break;
218 }
219 }
220
221 /* initialize a disk driver */
222 for (i = 0, n = 0; i < nata; i++)
223 n += dskdv_init(&lata[i]);
224 if (n == 0)
225 printf("IDE/SATA device driver was not found\n");
226
227 /* initialize a network interface */
228 for (n = 0; n < nnif; n++)
229 if (netif_init(&lnif[n]) != 0)
230 break;
231 if (n >= nnif)
232 printf("no NET device driver was found\n");
233
234 /* wait 2s for user to enter interactive mode */
235 for (n = 200; n >= 0; n--) {
236 if (n % 100 == 0)
237 printf("\rHit any key to enter interactive mode: %d",
238 n / 100);
239 if (tstchar()) {
240 #ifdef DEBUG
241 unsigned c;
242
243 c = toupper(getchar());
244 if (c == 'C') {
245 /* controller test terminal */
246 sat_test();
247 n = 200;
248 continue;
249 }
250 else if (c == 'F') {
251 /* find strings in Flash ROM */
252 findflash();
253 n = 200;
254 continue;
255 }
256 #else
257 (void)getchar();
258 #endif
259 /* enter command line */
260 argv = new_argv;
261 argc = input_cmdline(argv, MAX_ARGS);
262 break;
263 }
264 delay(10000);
265 }
266 putchar('\n');
267
268 howto = RB_AUTOBOOT; /* default is autoboot = 0 */
269
270 /* get boot options and determine bootname */
271 for (n = 1; n < argc; n++) {
272 if (strncmp(argv[n], "ide:", 4) == 0)
273 continue; /* ignore drive configuration argument */
274
275 for (i = 0; i < sizeof(bootargs) / sizeof(bootargs[0]); i++) {
276 if (strncasecmp(argv[n], bootargs[i].name,
277 strlen(bootargs[i].name)) == 0) {
278 howto |= bootargs[i].value;
279 break;
280 }
281 }
282 if (i >= sizeof(bootargs) / sizeof(bootargs[0]))
283 break; /* break on first unknown string */
284 }
285
286 /*
287 * If no device name is given, we construct a list of drives
288 * which have valid disklabels.
289 */
290 if (n >= argc) {
291 static const size_t blen = sizeof("wdN:");
292 n = 0;
293 argc = 0;
294 argv = alloc(MAX_UNITS * (sizeof(char *) + blen));
295 bname = (char *)(argv + MAX_UNITS);
296 for (i = 0; i < MAX_UNITS; i++) {
297 if (!dlabel_valid(i))
298 continue;
299 snprintf(bname, blen, "wd%d:", i);
300 argv[argc++] = bname;
301 bname += blen;
302 }
303 /* use default drive if no valid disklabel is found */
304 if (argc == 0) {
305 argc = 1;
306 argv[0] = BNAME_DEFAULT;
307 }
308 }
309
310 /* try to boot off kernel from the drive list */
311 while (n < argc) {
312 bname = argv[n++];
313
314 if (check_bootname(bname) == 0) {
315 printf("%s not a valid bootname\n", bname);
316 continue;
317 }
318
319 if ((fd = open(bname, 0)) < 0) {
320 if (errno == ENOENT)
321 printf("\"%s\" not found\n", bi_path.bootpath);
322 continue;
323 }
324 printf("loading \"%s\" ", bi_path.bootpath);
325 marks[MARK_START] = 0;
326
327 if (howto == -1) {
328 /* load another altboot binary and replace ourselves */
329 len = read(fd, (void *)0x100000, 0x1000000 - 0x100000);
330 if (len == -1)
331 goto loadfail;
332 close(fd);
333 netif_shutdown_all();
334
335 memcpy((void *)0xf0000, newaltboot,
336 newaltboot_end - newaltboot);
337 __syncicache((void *)0xf0000,
338 newaltboot_end - newaltboot);
339 printf("Restarting...\n");
340 run((void *)1, argv, (void *)0x100000, (void *)len,
341 (void *)0xf0000);
342 }
343
344 err = fdloadfile(fd, marks, LOAD_KERNEL);
345 close(fd);
346 if (err < 0)
347 continue;
348
349 printf("entry=%p, ssym=%p, esym=%p\n",
350 (void *)marks[MARK_ENTRY],
351 (void *)marks[MARK_SYM],
352 (void *)marks[MARK_END]);
353
354 bootinfo = (void *)0x4000;
355 bi_init(bootinfo);
356 bi_add(&bi_cons, BTINFO_CONSOLE, sizeof(bi_cons));
357 bi_add(&bi_mem, BTINFO_MEMORY, sizeof(bi_mem));
358 bi_add(&bi_clk, BTINFO_CLOCK, sizeof(bi_clk));
359 bi_add(&bi_path, BTINFO_BOOTPATH, sizeof(bi_path));
360 bi_add(&bi_rdev, BTINFO_ROOTDEVICE, sizeof(bi_rdev));
361 bi_add(&bi_fam, BTINFO_PRODFAMILY, sizeof(bi_fam));
362 if (brdtype == BRD_SYNOLOGY || brdtype == BRD_DLINKDSM) {
363 /* need to pass this MAC address to kernel */
364 bi_add(&bi_net, BTINFO_NET, sizeof(bi_net));
365 }
366 bi_add(&bi_model, BTINFO_MODEL, sizeof(bi_model));
367
368 if (modules_enabled) {
369 if (fsmod != NULL)
370 module_add(fsmod);
371 kmodloadp = marks[MARK_END];
372 btinfo_modulelist = NULL;
373 module_load(bname);
374 if (btinfo_modulelist != NULL &&
375 btinfo_modulelist->num > 0)
376 bi_add(btinfo_modulelist, BTINFO_MODULELIST,
377 btinfo_modulelist_size);
378 }
379
380 launchfixup();
381 netif_shutdown_all();
382
383 __syncicache((void *)marks[MARK_ENTRY],
384 (u_int)marks[MARK_SYM] - (u_int)marks[MARK_ENTRY]);
385
386 run((void *)marks[MARK_SYM], (void *)marks[MARK_END],
387 (void *)howto, bootinfo, (void *)marks[MARK_ENTRY]);
388
389 /* should never come here */
390 printf("exec returned. Restarting...\n");
391 _rtt();
392 }
393 loadfail:
394 printf("load failed. Restarting...\n");
395 _rtt();
396 }
397
398 void
399 bi_init(void *addr)
400 {
401 struct btinfo_magic bi_magic;
402
403 memset(addr, 0, BOOTINFO_MAXSIZE);
404 bi_next = (char *)addr;
405 bi_size = 0;
406
407 bi_magic.magic = BOOTINFO_MAGIC;
408 bi_add(&bi_magic, BTINFO_MAGIC, sizeof(bi_magic));
409 }
410
411 void
412 bi_add(void *new, int type, int size)
413 {
414 struct btinfo_common *bi;
415
416 if (bi_size + size > BOOTINFO_MAXSIZE)
417 return; /* XXX error? */
418
419 bi = new;
420 bi->next = size;
421 bi->type = type;
422 memcpy(bi_next, new, size);
423 bi_next += size;
424 }
425
426 void
427 module_add(const char *name)
428 {
429 struct boot_module *bm, *bmp;
430
431 while (*name == ' ' || *name == '\t')
432 ++name;
433
434 bm = alloc(sizeof(struct boot_module) + strlen(name) + 1);
435 if (bm == NULL) {
436 printf("couldn't allocate module %s\n", name);
437 return;
438 }
439
440 bm->bm_kmod = (char *)(bm + 1);
441 bm->bm_len = -1;
442 bm->bm_next = NULL;
443 strcpy(bm->bm_kmod, name);
444 if ((bmp = boot_modules) == NULL)
445 boot_modules = bm;
446 else {
447 while (bmp->bm_next != NULL)
448 bmp = bmp->bm_next;
449 bmp->bm_next = bm;
450 }
451 }
452
453 #define PAGE_SIZE 4096
454 #define alignpg(x) (((x)+PAGE_SIZE-1) & ~(PAGE_SIZE-1))
455
456 void
457 module_load(const char *kernel_path)
458 {
459 struct boot_module *bm;
460 struct bi_modulelist_entry *bi;
461 struct stat st;
462 char *p;
463 int size, fd;
464
465 strcpy(module_base, kernel_path);
466 if ((p = strchr(module_base, ':')) == NULL)
467 return; /* eeh?! */
468 p += 1;
469 size = sizeof(module_base) - (p - module_base);
470
471 if (netbsd_version / 1000000 % 100 == 99) {
472 /* -current */
473 snprintf(p, size,
474 "/stand/sandpoint/%d.%d.%d/modules",
475 netbsd_version / 100000000,
476 netbsd_version / 1000000 % 100,
477 netbsd_version / 100 % 100);
478 }
479 else if (netbsd_version != 0) {
480 /* release */
481 snprintf(p, size,
482 "/stand/sandpoint/%d.%d/modules",
483 netbsd_version / 100000000,
484 netbsd_version / 1000000 % 100);
485 }
486
487 /*
488 * 1st pass; determine module existence
489 */
490 size = 0;
491 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
492 fd = module_open(bm);
493 if (fd == -1)
494 continue;
495 if (fstat(fd, &st) == -1 || st.st_size == -1) {
496 printf("WARNING: couldn't stat %s\n", bm->bm_kmod);
497 close(fd);
498 continue;
499 }
500 bm->bm_len = (int)st.st_size;
501 close(fd);
502 size += sizeof(struct bi_modulelist_entry);
503 }
504 if (size == 0)
505 return;
506
507 size += sizeof(struct btinfo_modulelist);
508 btinfo_modulelist = alloc(size);
509 if (btinfo_modulelist == NULL) {
510 printf("WARNING: couldn't allocate module list\n");
511 return;
512 }
513 btinfo_modulelist_size = size;
514 btinfo_modulelist->num = 0;
515
516 /*
517 * 2nd pass; load modules into memory
518 */
519 kmodloadp = alignpg(kmodloadp);
520 bi = (struct bi_modulelist_entry *)(btinfo_modulelist + 1);
521 for (bm = boot_modules; bm != NULL; bm = bm->bm_next) {
522 if (bm->bm_len == -1)
523 continue; /* already found unavailable */
524 fd = module_open(bm);
525 printf("module \"%s\" ", bm->bm_kmod);
526 size = read(fd, (char *)kmodloadp, SSIZE_MAX);
527 if (size < bm->bm_len)
528 printf("WARNING: couldn't load");
529 else {
530 snprintf(bi->kmod, sizeof(bi->kmod), "%s", bm->bm_kmod);
531 bi->type = BI_MODULE_ELF;
532 bi->len = size;
533 bi->base = kmodloadp;
534 btinfo_modulelist->num += 1;
535 printf("loaded at 0x%08x size 0x%x", kmodloadp, size);
536 kmodloadp += alignpg(size);
537 bi += 1;
538 }
539 printf("\n");
540 close(fd);
541 }
542 btinfo_modulelist->endpa = kmodloadp;
543 }
544
545 int
546 module_open(struct boot_module *bm)
547 {
548 char path[80];
549 int fd;
550
551 snprintf(path, sizeof(path),
552 "%s/%s/%s.kmod", module_base, bm->bm_kmod, bm->bm_kmod);
553 fd = open(path, 0);
554 return fd;
555 }
556
557 /*
558 * Return the drive configuration for the requested channel 'ch'.
559 * Channel 2 is the first channel of the next IDE controller.
560 * 0: for no drive present on channel
561 * 1: for master drive present on channel, no slave
562 * 2: for master and slave drive present
563 */
564 int
565 get_drive_config(int ch)
566 {
567 if (drive_config != NULL) {
568 if (strlen(drive_config) <= ch)
569 return 0; /* an unspecified channel is unused */
570 if (drive_config[ch] >= '0' && drive_config[ch] <= '2')
571 return drive_config[ch] - '0';
572 }
573 return -1;
574 }
575
576 void *
577 allocaligned(size_t size, size_t align)
578 {
579 uint32_t p;
580
581 if (align-- < 2)
582 return alloc(size);
583 p = (uint32_t)alloc(size + align);
584 return (void *)((p + align) & ~align);
585 }
586
587 static int hex2nibble(char c)
588 {
589
590 if (c >= 'a')
591 c &= ~0x20;
592 if (c >= 'A' && c <= 'F')
593 c -= 'A' - ('9' + 1);
594 else if (c < '0' || c > '9')
595 return -1;
596
597 return c - '0';
598 }
599
600 uint32_t
601 read_hex(const char *s)
602 {
603 int n;
604 uint32_t val;
605
606 val = 0;
607 while ((n = hex2nibble(*s++)) >= 0)
608 val = (val << 4) | n;
609 return val;
610 }
611
612 static int
613 check_bootname(char *s)
614 {
615 /*
616 * nfs:
617 * nfs:<bootfile>
618 * tftp:
619 * tftp:<bootfile>
620 * wd[N[P]]:<bootfile>
621 * mem:<address>
622 *
623 * net is a synonym of nfs.
624 */
625 if (strncmp(s, "nfs:", 4) == 0 || strncmp(s, "net:", 4) == 0 ||
626 strncmp(s, "tftp:", 5) == 0 || strncmp(s, "mem:", 4) == 0)
627 return 1;
628 if (s[0] == 'w' && s[1] == 'd') {
629 s += 2;
630 if (*s != ':' && *s >= '0' && *s <= '3') {
631 ++s;
632 if (*s != ':' && *s >= 'a' && *s <= 'p')
633 ++s;
634 }
635 return *s == ':';
636 }
637 return 0;
638 }
639
640 static int input_cmdline(char **argv, int maxargc)
641 {
642 char *cmdline;
643
644 printf("\nbootargs> ");
645 cmdline = alloc(256);
646 kgets(cmdline, 256);
647
648 return parse_cmdline(argv, maxargc, cmdline,
649 cmdline + strlen(cmdline));
650 }
651
652 static int
653 parse_cmdline(char **argv, int maxargc, char *p, char *end)
654 {
655 int argc;
656
657 argv[0] = "";
658 for (argc = 1; argc < maxargc && p < end; argc++) {
659 while (is_space(*p))
660 p++;
661 if (p >= end)
662 break;
663 argv[argc] = p;
664 while (!is_space(*p) && p < end)
665 p++;
666 *p++ = '\0';
667 }
668
669 return argc;
670 }
671
672 static int
673 is_space(char c)
674 {
675
676 return c > '\0' && c <= ' ';
677 }
678
679 #ifdef DEBUG
680 static void
681 findflash(void)
682 {
683 char buf[256];
684 int i, n;
685 unsigned char c, *p;
686
687 for (;;) {
688 printf("\nfind> ");
689 kgets(buf, sizeof(buf));
690 if (tolower((unsigned)buf[0]) == 'x')
691 break;
692 for (i = 0, n = 0, c = 0; buf[i]; i++) {
693 c <<= 4;
694 c |= hex2nibble(buf[i]);
695 if (i & 1)
696 buf[n++] = c;
697 }
698 printf("Searching for:");
699 for (i = 0; i < n; i++)
700 printf(" %02x", buf[i]);
701 printf("\n");
702 for (p = (unsigned char *)0xff000000;
703 p <= (unsigned char *)(0xffffffff-n); p++) {
704 for (i = 0; i < n; i++) {
705 if (p[i] != buf[i])
706 break;
707 }
708 if (i >= n)
709 printf("Found at %08x\n", (unsigned)p);
710 }
711 }
712 }
713
714 static void
715 sat_test(void)
716 {
717 char buf[1024];
718 int i, j, n, pos;
719 unsigned char c;
720
721 putchar('\n');
722 for (;;) {
723 do {
724 for (pos = 0; pos < 1024 && sat_tstch() != 0; pos++)
725 buf[pos] = sat_getch();
726 if (pos > 1023)
727 break;
728 delay(100000);
729 } while (sat_tstch());
730
731 for (i = 0; i < pos; i += 16) {
732 if ((n = i + 16) > pos)
733 n = pos;
734 for (j = 0; j < n; j++)
735 printf("%02x ", (unsigned)buf[i + j]);
736 for (; j < 16; j++)
737 printf(" ");
738 putchar('\"');
739 for (j = 0; j < n; j++) {
740 c = buf[i + j];
741 putchar((c >= 0x20 && c <= 0x7e) ? c : '.');
742 }
743 printf("\"\n");
744 }
745
746 printf("controller> ");
747 kgets(buf, sizeof(buf));
748 if (buf[0] == '*' && buf[1] == 'X')
749 break;
750
751 if (buf[0] == '0' && tolower((unsigned)buf[1]) == 'x') {
752 for (i = 2, n = 0, c = 0; buf[i]; i++) {
753 c <<= 4;
754 c |= hex2nibble(buf[i]);
755 if (i & 1)
756 buf[n++] = c;
757 }
758 } else
759 n = strlen(buf);
760
761 if (n > 0)
762 sat_write(buf, n);
763 }
764 }
765 #endif
766