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