📄 e100boot.c
字号:
if (all_ids && one_id_only) { all_ids = FALSE; id = ntohl(h->id); printf("Booting device with random id %8.8x.\n", id); } } else { printf("Got ACK from a new id, %8.8lx. Ignoring.\n", (unsigned long)ntohl(h->id)); return; } nACK++; //Added by Andrey for counting accepted packets new_ack = TRUE; last_ack_received = ntohl(h->seq); if (db1) printf("last_ack_received=%d\n",last_ack_received); //Andrey if (last_ack_received > highest_ack_received) { highest_ack_received = last_ack_received; } } else if ((ntohl(h->type) == REJECT)) { nREJ++; //Added by Andrey for counting rejected packets }#if 0 last_rec_packet->data = realloc(last_rec_packet->data, hdr->caplen); memcpy(last_rec_packet->data, p, hdr->caplen);#if 1 last_rec_packet->next = (struct packet_buf*) calloc(1, sizeof(struct packet_buf)); last_rec_packet = last_rec_packet->next; if (last_rec_packet != NULL) { last_rec_packet->data = malloc(PACKET_SIZE); } if (last_rec_packet == NULL) { printf("Failed to alloc memory!\n"); }#endif#endif if (db1) printf("#########################################################\n"); } if (db2) printf("< Handler\n");}/*****************************************************************************#*# FUNCTION NAME: timeout*#*# PARAMETERS: *#*# DESCRIPTION: *#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 980817 ronny Initial version*#*#***************************************************************************/inttimeout(struct timeval *tvThen, int ms){ struct timeval tvNow; struct timeval tvDiff; (void) gettimeofday(&tvNow, NULL); tvDiff = timeval_subtract(&tvNow, tvThen); if (db4) printf("sec %d.%d\n", (int)tvDiff.tv_sec, (int)tvDiff.tv_usec); if (ms * 1000 < (tvDiff.tv_sec * 1000000 + tvDiff.tv_usec)) { if (db4) printf("TIMEOUT\n"); return(TRUE); } return(FALSE);}/*****************************************************************************#*# FUNCTION NAME: ack_on_seq*#*# PARAMETERS: *#*# DESCRIPTION: *#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 980817 ronny Initial version*#*#***************************************************************************/struct packet_buf *packet_with_seq(int seq){ static int last_seq = 0; struct packet_buf *p = first_packet; struct packet_header_T *h; D(); if (seq < last_seq) { p = first_packet; } while(p) { h = (struct packet_header_T*)p->data; if (ntohl(h->seq) == seq) { return(p); } p = p->next; } return(NULL);}struct packet_buf *ack_on_seq(int seq){ struct packet_buf *p = &first_rec_packet; struct packet_header_T *h; if (db1) printf("***> ack_on_seq: %d.\n", seq); while (p) { /* printf("\nPacket at %x.\n", p);*/ /* DecodeSvintoBoot(p->data);*/ h = (struct packet_header_T*)p->data; if ( (ntohl(h->type) == ACK) && (ntohl(h->seq) == seq) ) { if (all_ids || ntohl(h->id) == id) { printf("***< ack_on_seq %d, ok.\n", seq); return(p); } } p = p->next; } if (db1) printf("***< ack_on_seq, no.\n"); return(NULL);}inthighest_seq_received(){ struct packet_buf *p = &first_rec_packet; struct packet_header_T *h; int highest_seq = -1; if (db1) printf("***> highest_seq_received\n"); while (p) { /* printf("\nPacket at %x.\n", p);*/ /* DecodeSvintoBoot(p->data);*/ h = (struct packet_header_T*)p->data; if ((ntohl(h->type) == ACK) && (all_ids || (ntohl(h->id) == id))) { if ((int)ntohl(h->seq) > highest_seq) { highest_seq = ntohl(h->seq); if (db4) printf("Highest seq: %d\n", highest_seq); } } p = p->next; } if (db1) printf("***< highest_seq_received: %d\n", highest_seq); return(highest_seq);}/*****************************************************************************#*# FUNCTION NAME: PrintPacket*#*# PARAMETERS: *#*# DESCRIPTION: *#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 961022 ronny Initial version*#*#***************************************************************************/voidPrintPacket(const unsigned char *p, int size, int type){ int i; /* printf("size %d\n", size);*/ for (i = 0; i != size; i++) { if (i % p_packet_bpl == 0) printf("\n%-4.4d: ", i); if (type == UDEC) printf("%-3d ", p[i]); else if (type == HEX) printf("%-2.2x ", p[i]); else if (type == CHAR) { if (isprint(p[i])) printf("%-3c ", p[i]); else printf("%-3d ", p[i]); } else if (type == ASCII) { if (isprint(p[i])) printf("%c", p[i]); else printf("."); } } printf("\n");}/*****************************************************************************#*# FUNCTION NAME: DecodeSvintoBoot*#*# PARAMETERS: *#*# DESCRIPTION: *#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 961022 ronny Initial version*#*#***************************************************************************/void DecodeSvintoBoot(const unsigned char *p){ char *str; volatile struct packet_header_T *ph = (struct packet_header_T*)p; /* printf("size %d \n", sizeof(struct packet_header_T));*/ if (db4) printf("\n>DecodeSvintoBoot. Packet at 0x%x\n", (unsigned int)p); printf("%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x -> ", p[6],p[7],p[8],p[9],p[10], p[11]); printf("%2.2x-%2.2x-%2.2x-%2.2x-%2.2x-%2.2x\n", p[0],p[1],p[2],p[3],p[4], p[5]); printf("length : %4.4lx\n", (long)ntohs(ph->length)); printf("snap1 : %8.8lx\n", (long)ntohl(ph->snap1)); printf("snap2 : %8.8lx\n", (long)ntohl(ph->snap2)); switch (ntohl(ph->tag)) { case 0xffffffff: str = "(client > ETRAX)"; break; case 0xfffffffe: str = "(client < ETRAX)"; break; default: str = "(unknown)"; break; } printf("tag : %8.8lx %s\n", (unsigned long)ntohl(ph->tag), str); printf("seq : %8.8lx\n", (unsigned long)ntohl(ph->seq)); switch (ntohl(ph->type)) { case ACK: str = "(ACK)"; break; case BOOT_PACKET: str = "(bootpacket)"; break; case BOOT_CMDS: str = "(bootcmds)"; break; default: str = "(unknown)"; break; } printf("(type : %8.8lx %s)\n", (unsigned long)ntohl(ph->type), str); printf("(id : %8.8lx)\n", (unsigned long)ntohl(ph->id)); id = ntohl(ph->id);}/*****************************************************************************#*# FUNCTION NAME: SigHandler*#*# PARAMETERS: *#*# DESCRIPTION: *#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 961022 ronny Initial version*#*#***************************************************************************/voidSigHandler(int sig){ exit(1); }/*****************************************************************************#*# FUNCTION NAME: Fopen*#*# PARAMETERS: Name and mode, both strings.*#*# DESCRIPTION: Opens a file and returns its fd, or NULL.*#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 961022 ronny Initial version*#*#***************************************************************************/FILE *Fopen(char *name, char *mode){ FILE *fd; if (db2) printf(">>> Fopen '%s', mode '%s'\n", name, mode);#if defined(_WIN32) fd = _fsopen(name, mode, _SH_DENYNO);#else fd = fopen(name, mode);#endif if (fd == NULL) { printf("<<< Fopen failed on '%s', mode '%s'\n", name, mode); return ((FILE*) NULL); } if (strncmp(mode, "a", 1) == 0) { if (db3) printf("* Append mode, seeking to end.\n"); fseek(fd, 0L, SEEK_SET); } if (db2) printf("<<< Fopen: '%s'\n", name); return(fd);}/*****************************************************************************#*# FUNCTION NAME: ParseArgs*#*# PARAMETERS: Standard command line args. *#*# DESCRIPTION: Parses command line arguments.*#*#---------------------------------------------------------------------------*# HISTORY*#*# DATE NAME CHANGES*# ---- ---- -------*# 960909 ronny Initial version*#***************************************************************************/voidParseArgs (int argc, char *argv[]){ int argi; int i; int printHelp = FALSE; char dbStr[100]; /* Debug option string. */ int number; int argCount; char **argVect; struct stat st; if (db4) printf(">>> ParseArgs\n"); argCount = argc; argVect = argv; for (argi = 1; argi < argCount; argi++) { if (db4) printf("argv[%d] = '%s'\n", argi, argVect[argi]); if (strncmp(argVect[argi], "--from", 6) == 0) { if (GetStringOption(&argi, argCount, argVect, host1, "--from") == 0) { printHelp = TRUE; } else { printf("Host: %s %s\n", host1, host2); if (sscanf(host1, "%x-%x-%x-%x-%x-%x", &i, &i, &i, &i, &i, &i) == 6) { printf("Ethernet address\n"); } } } else if (strncmp(argVect[argi], "--device", 8) == 0) { if (GetStringOption(&argi, argCount, argVect, device, "--device") == 0) { printHelp = TRUE; } } else if (strncmp(argVect[argi], "--tofiles", 9) == 0) { toFiles = TRUE; } else if (strncmp(argVect[argi], "--tofile", 8) == 0) { // added by Andrey - should be after "--tofiles?" GetStringOption(&argi, argCount, argVect, ofilename, "--tofile"); if ((singlefd = fopen(ofilename, "w+")) == NULL) { printf("Cannot open/create '%s'. %s.\n", ofilename, strerror(errno)); exit(1); } }/* else if (strncmp(argVect[argi], "--bootfile", 10) == 0) { GetStringOption(&argi, argCount, argVect, boot_loader_file, "--bootfile"); }*/ else if (strncmp(argVect[argi], "--to", 4) == 0) { if ((GetStringOption(&argi, argCount, argVect, host2, "--to") == 0)) { printHelp = TRUE; } else { printf("Host: %s %s\n", host1, host2); both_addresses = TRUE; if (sscanf(host2, "%x-%x-%x-%x-%x-%x", &i, &i, &i, &i, &i, &i) == 6) { printf("Ethernet address\n"); } } } else if (strncmp(argVect[argi], "--printp", 8) == 0) { pPacket = 1; } else if (strncmp(argVect[argi], "--printascii", 11) == 0) { pPacket = 1; printPacketType = ASCII; } else if (strncmp(argVect[argi], "--printudec", 11) == 0) { pPacket = 1; printPacketType = UDEC; } else if (strncmp(argVect[argi], "--printhex", 10) == 0) { pPacket = 1; printPacketType = HEX; } else if (strncmp(argVect[argi], "--bpl", 5) == 0) { if (GetNumberOption(&argi, argCount, argVect, &p_packet_bpl, "--bpl", 10) == 0) { printHelp = TRUE; } } else if (strncmp(argVect[argi], "--promisc", 11) == 0) { promisc = 1; } else if (strncmp(argVect[argi], "--5400", 6) == 0) { boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000000; boot_cmds[boot_cmds_cnt++] = 0x000095b6; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000008; boot_cmds[boot_cmds_cnt++] = 0x0000e751; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb000000c; boot_cmds[boot_cmds_cnt++] = 0x12604040; } else if (strncmp(argVect[argi], "--5600", 6) == 0) { boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000000; boot_cmds[boot_cmds_cnt++] = 0x000095b6; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000008; boot_cmds[boot_cmds_cnt++] = 0x00006751; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb000000c; boot_cmds[boot_cmds_cnt++] = 0x12204040; } else if (strncmp(argVect[argi], "--testcard", 10) == 0) { boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000000; boot_cmds[boot_cmds_cnt++] = 0x000010b3; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000008; boot_cmds[boot_cmds_cnt++] = 0x00006543; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb000000c; boot_cmds[boot_cmds_cnt++] = 0x12966060; } else if (strncmp(argVect[argi], "--devboard", 10) == 0) { /* Printing on serial port will not work until PORT_PB is set... */ boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000038; boot_cmds[boot_cmds_cnt++] = 0x01001ef3; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000030; boot_cmds[boot_cmds_cnt++] = 0x00001df0; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000000; boot_cmds[boot_cmds_cnt++] = 0x000095a6; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb000000c; boot_cmds[boot_cmds_cnt++] = 0x1a200040; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000008; boot_cmds[boot_cmds_cnt++] = 0x00005611; boot_cmds[boot_cmds_cnt++] = SET_REGISTER; boot_cmds[boot_cmds_cnt++] = 0xb0000004; boot_cmds[boot_cmds_cnt++] = 0x00000104; } else if (strncmp(argVect[argi], "--verify", 8) == 0) { boot_cmds[boot_cmds_cnt++] = MEM_VERIFY; GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--verify", 16); GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--verify", 16); } else if (strncmp(argVect[argi], "--setreg", 8) == 0) { boot_cmds[boot_cmds_cnt++] = SET_REGISTER; GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--setreg", 16); GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--setreg", 16); } else if (strncmp(argVect[argi], "--getreg", 8) == 0) { boot_cmds[boot_cmds_cnt++] = GET_REGISTER; GetNumberOption(&argi, argCount, argVect, &boot_cmds[boot_cmds_cnt++], "--getreg", 16);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -