📄 commands.c
字号:
/* $Id: commands.c,v 1.1.1.3 2003/03/18 22:31:47 anoncvs Exp $ */#include <stdio.h>#include <termio.h>#include <string.h>#include <setjmp.h>#include <sys/endian.h>#include <ctype.h>#include <unistd.h>#include <stdlib.h>#include <fcntl.h>#ifdef _KERNEL#undef _KERNEL#include <sys/ioctl.h>#define _KERNEL#else#include <sys/ioctl.h>#endif#include <machine/cpu.h>#include <pmon.h>static void * dispmem __P((char *, void *, int));char *searching = "searching.. ";char *badhexsym = "%s: neither hex value nor symbol\n";static __inline void era_line __P((char *));static __inline voidera_line(char *p){ while(*p++) printf("\b \b");}/** transp(ac,av), the 'tr' command */inttransp (ac, av) int ac; char *av[];{ int fd, n; jmp_buf intrsave; char *hostport, buf[80], abortch, *trabort; struct termio tbuf, consave, hostsave; trabort = getenv ("trabort"); abortch = str2cc (trabort); if (abortch == 0) { printf ("tr: error: trabort not set\n"); return 1; } hostport = getenv ("hostport"); if (strequ (hostport, "tty0")) { printf ("can't use tty0 as hostport in transparent mode\n"); return 1; } fd = open (hostport, 0); if (fd == -1) { printf ("can't open %s\n", hostport); return 1; } printf ("Entering transparent mode, %s to abort\n", trabort); ioctl (fd, TCGETA, &tbuf); hostsave = tbuf; tbuf.c_lflag &= ~(ICANON | ECHO | ECHOE); tbuf.c_iflag &= ~(ICRNL); tbuf.c_iflag |= IXOFF; /* enable tandem mode */ tbuf.c_oflag &= ~ONLCR; tbuf.c_cc[4] = 1; ioctl (fd, TCSETAF, &tbuf); ioctl (STDIN, TCGETA, &tbuf); consave = tbuf; tbuf.c_lflag &= ~(ICANON | ECHO | ECHOE); tbuf.c_iflag &= ~(ICRNL | IXON); tbuf.c_oflag &= ~ONLCR; tbuf.c_cc[4] = 1; ioctl (STDIN, TCSETAF, &tbuf);/* disable INTR char */ ioctl (STDIN, GETINTR, intrsave); ioctl (STDIN, SETINTR, 0); for (;;) { ioctl (STDIN, FIONREAD, &n); if (n > 0) { if (n > sizeof(buf) - 1) n = sizeof(buf) - 1; n = read (STDIN, buf, n); buf[n] = '\0'; if (strchr (buf, abortch)) break; write (fd, buf, n); } ioctl (fd, FIONREAD, &n); if (n > 0) { if (n > sizeof(buf)) n = sizeof(buf); n = read (fd, buf, n); write (STDOUT, buf, n); } } ioctl (STDIN, TCSETAF, &consave); ioctl (fd, TCSETAF, &hostsave); ioctl (STDIN, SETINTR, intrsave); return 0;}const Optdesc m_opts[] ={ {"-b", "access bytes"}, {"-h", "access half-words"}, {"-w", "access words"},#ifdef HAVE_QUAD {"-d", "access double-words"},#endif {"-n", "non-interactive (no write)"}, {"", "Interactive Options"}, {"<hexval>", "set memory, forward one"}, {"CR", "forward one, no change"}, {"=", "re-read"}, {"^|-", "back one"}, {".", "quit"}, {0}};/** modify(ac,av), the 'm' command */intmodify (ac, av) int ac; char *av[];{ int32_t adr; register_t v; char *p; int datasz = -2; int nowrite = 0; extern int optind; int c; optind = 0; while ((c = getopt (ac, av, "dwhbn")) != EOF) { if (datasz != -2) { printf ("multiple data types specified\n"); return (-1); } switch (c) {#ifdef HAVE_QUAD case 'd': datasz = 8; break;#endif case 'w': datasz = 4; break; case 'h': datasz = 2; break; case 'b': datasz = 1; break; case 'n': nowrite = 1; break; default: return (-1); } } if (optind >= ac) return (-1); if (!get_rsa (&adr, av[optind++])) return (-1); if (datasz == -2) datasz = 1 << matchenv ("datasz"); if (optind < ac) { /* command mode */ for (; optind < ac; optind++) { if (strequ (av[optind], "-s")) { if (++optind >= ac) { printf ("bad arg count\n"); return (-1); } for (p = av[optind]; *p; p++) store_byte ((void *)(adr++), *p); } else { if (!get_rsa_reg (&v, av[optind])) return (-1); if (adr & (datasz - 1)) { printf ("%08x: unaligned address\n", adr); return (1); } switch (datasz) { case 1: store_byte ((void *)adr, v); break; case 2: store_half ((void *)adr, v); break; case 4: store_word ((void *)adr, v); break;#ifdef HAVE_QUAD case 8: store_dword ((void *)adr, v); break;#endif } adr += datasz; } } } else { /* interactive mode */ if (adr & (datasz - 1)) { printf ("%08x: unaligned address\n", adr); return (1); } for (;;) { switch (datasz) { case 1: v = *(unsigned char *)adr; break; case 2: v = *(unsigned short *)adr; break; case 4: v = *(unsigned int *)adr; break;#ifdef HAVE_QUAD case 8: v = *(unsigned long long *)adr; break;#endif }#if __mips >= 3 printf ("%08x %0*llx ", adr, 2 * datasz, v);#else printf ("%08x %0*lx ", adr, 2 * datasz, v);#endif if (nowrite) { printf ("\n"); break; } line[0] = '\0'; get_line (line, 0); for (p = line; *p == ' '; p++); if (*p == '.') break; else if (*p == '\0') adr += datasz; else if (*p == '^' || *p == '-') adr -= datasz; else if (*p == '=') /* reread */; else if (get_rsa_reg (&v, p)) { switch (datasz) { case 1: store_byte ((void *)adr, v); break; case 2: store_half ((void *)adr, v); break; case 4: store_word ((void *)adr, v); break;#ifdef HAVE_QUAD case 8: store_dword ((void *)adr, v); break;#endif } adr += datasz; } } } return (0);}/** search(ac,av), the search command */intsearch (ac, av) int ac; char *av[];{ u_int32_t from, to, adr, i, a; char *s, *d, c; char pat[PATSZ]; int siz, ln; ln = siz = moresz; ioctl (STDIN, CBREAK, NULL); if (!get_rsa (&from, av[1]) || !get_rsa (&to, av[2])) return (-1); for (d = pat, i = 3; i < ac; i++) { if (strequ (av[i], "-s")) { if (++i >= ac) { printf ("bad arg count\n"); return (-1); } for (s = av[i]; *s; s++) *d++ = *s; } else { if (!get_rsa (&a, av[i])) return (-1); c = a; *d++ = c; } } if (to <= from) { printf ("'to' address too small\n"); return (1); } printf ("%s", searching); if (from <= to) { /* forward search */ to -= d - pat - 1; while (from <= to) { s = pat; adr = from++; while (s < d) { if (*s != load_byte (adr)) break; s++; adr++; } if (d <= s) { era_line (searching); dispmem (prnbuf, (void *)(from - 1), 4); if (more (prnbuf, &ln, siz)) break; printf ("%s", searching); } else dotik (1, 0); } if (from > to) era_line (searching); } else { /* backward search */ from -= d - pat - 1; while (to <= from) { s = pat; adr = from--; while (s < d) { if (*s != load_byte (adr)) break; s++; adr++; } if (d <= s) { era_line (searching); dispmem (prnbuf, (void *)(from - 1), 4); if (more (prnbuf, &ln, siz)) break; printf ("%s", searching); } } if (to > from) era_line (searching); } return (0);}/** call(ac,av), the call command */intcall (ac, av) int ac; char *av[];{ int i, j, k; char *arg[10]; int (*func) __P((char *, char *, char *, char *, char *)); arg[0] = 0; k = 0; for (i = 1; i < ac; i++) { if (av[i][0] == '-') { j = 1; while (av[i][j] != 0) { if (av[i][j] == 's') { if (++i >= ac) { printf ("bad arg count\n"); return (-1); } arg[k++] = av[i]; break; } else { printf ("%c: unknown option\n", av[i][j]); return (-1); } j++; } } else { if (!get_rsa ((u_int32_t *)&arg[k], av[i])) return (-1); k++; } } if (arg[0] == 0) { printf ("Function address not specified\n"); return (-1); } func = (void *)arg[0]; i = (*func) (arg[1], arg[2], arg[3], arg[4], arg[5]); printf ("Function returns: 0x%x (%d)\n", i, i); return (0);}#ifdef E2PROMe2program (ac, av) int ac; char *av[];{ uword addr, size, offset; if (!get_rsa (&addr, av[1])) { printf ("bad address\n"); return -1; } if (!get_rsa (&size, av[2])) { printf ("bad size\n"); return -1; } if (!get_rsa (&offset, av[3])) { printf ("bad offset\n"); return -1; } printf ("Address = %x, Size = %x, Offset = %x\n", addr, size, offset); sbd_e2program (addr, size, offset); return (0);}#endifvoidflush_cache (type, adr) int type; void *adr;{ switch (type) { case ICACHE: flushicache((void *)0, memorysize); break; case DCACHE: flushdcache((void *)0, memorysize); break; case IADDR: syncicache((void *)((int)adr & ~3), 4); break; }}const Optdesc flush_opts[] = { {"-i", "flush I-cache"}, {"-d", "flush D-cache"}, {0}};/** flush(ac,av), the 'flush' command */intflush (ac, av) int ac; char *av[];{ extern int optind; int c; int flags = 0; optind = 0; while ((c = getopt (ac, av, "id")) != EOF) switch (c) { case 'd': flags |= 1; break; case 'i': flags |= 2; break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -