static void my_recv(int sock, const char *who, pid_t pid) { u_char packet[PACKETLEN]; ssize_t len; size_t totlen; int error; totlen = 0; bzero(packet, sizeof(packet)); while (totlen < PACKETLEN) { len = recv(sock, packet + totlen, sizeof(packet) - totlen, 0); if (len < 0) { errno = error; (void)kill(pid, SIGTERM); errno = error; err(-1, "%s: recv()", who); } if (len == 0) { (void)kill(pid, SIGTERM); errx(-1, "%s: recv(): EOF", who); } if (packet_check(packet, totlen, len) < 0) { (void)kill(pid, SIGTERM); errx(-1, "%s: recv(): got bad data", who); } totlen += len; } }
static int test_ctlr (int ctlr, int index) { int res = -1; char packet_send[MAX_PACKET_LENGTH]; char packet_recv[MAX_PACKET_LENGTH]; int length; int i; int l; ctlr_proc[ctlr].init (index); for (i = 0; i < TEST_NUM; i++) { for (l = MIN_PACKET_LENGTH; l <= MAX_PACKET_LENGTH; l++) { packet_fill (packet_send, l); ctlr_proc[ctlr].send (index, packet_send, l); length = ctlr_proc[ctlr].recv (index, packet_recv, MAX_PACKET_LENGTH); if (length != l || packet_check (packet_recv, length) < 0) { goto Done; } } } res = 0; Done: ctlr_proc[ctlr].halt (index); /* * SCC2 Ethernet parameter RAM space overlaps * the SPI parameter RAM space. So we need to restore * the SPI configuration after SCC2 ethernet test. */ #if defined(CONFIG_SPI) if (ctlr == CTLR_SCC && index == 1) { spi_init_f (); spi_init_r (); } #endif if (res != 0) { post_log ("ethernet %s%d test failed\n", ctlr_name[ctlr], index + 1); } return res; }
void serial_process(void) { uint8_t serial_channel = serial_readFrame(); packet_t *packet; if( serial_getMessageLen() == 16 ){ uint8_t *msg = serial_getMessage(); if( serial_channel == '0' ){ aes_decrypt(msg); packet = (packet_t *) msg; if( packet_check(packet) ){ cmd_new(packet->cmd, packet->data); } }else if(serial_channel != 0) { bus_sendFrame(serial_channel, msg, 16); } } }
int main(int argc, char *argv[]) { char optchar; int longindex, i,res, crcerr; u8 writecmd; u32 bufbytepos; u8 *buffer,*lastrow; u16 wordaddr; u16 rowsexceptlast, toprow; u16 crc16; u16 load_size, load_size_bytes, load_address; u16 bootloader_size; u32 max_app_size; u32 bytes_received, bytes_sent; ihx_t *ihx; response_t *r; config_area_t *cf; serioStuff *s; dictionary *dict = NULL; static char exten[20]; char *q; /* Die if packet structures screwed up */ assert(sizeof(packet_t) == PACKET_SIZE); progname = argv[0]; // Argument pre scan for config file name change first while((optchar=getopt_long(argc, argv, SHORT_OPTIONS, long_options, &longindex)) != EOF) { //Handle each argument. switch(optchar) { // Was it a long option? case 0: // Hrmm, something we don't know about? fatal("Unhandled long getopt option '%s'", long_options[longindex].name); // If it was an error, exit right here. case '?': exit(1); // Was it a config file name? case 'z': memset(config_file, 0, MAX_PATH); strncpy(config_file, optarg, MAX_PATH); flags.configfileoverride = 1; break; default: // Don't care about the rest of the args right now break; } } optind = 1; // Reset option index for second scan later if((dict = iniparser_load(config_file))){ char *s; s = iniparser_getstring(dict, "general:file", NULL); if(s) strncpy(file, s, MAX_PATH - 1); s = iniparser_getstring(dict, "general:port", NULL); if(s) strncpy(port, s, MAX_PATH - 1); s = iniparser_getstring(dict, "general:han-host",NULL); if(s) strncpy(host, s, MAX_PATH - 1); s = iniparser_getstring(dict, "general:han-service",NULL); if(s) strncpy(service, s, MAX_PATH - 1); s = iniparser_getstring(dict, "general:product-id", NULL); if(s){ if(sscanf(s, "%X", &i) != 1) fatal("In pcl.conf, product ID needs to be a hexadecimal value"); productid = (u16) i; } iniparser_freedict(dict); } else if(flags.configfileoverride){ fatal("Cannot open config file: %s\n", config_file); } /* Parse the arguments. */ while((optchar=getopt_long(argc, argv, SHORT_OPTIONS, long_options, &longindex)) != EOF) { /* Handle each argument. */ switch(optchar) { /* Was it a long option? */ case 0: /* Hrmm, something we don't know about? */ fatal("Unhandled long getopt option '%s'", long_options[longindex].name); /* If it was an error, exit right here. */ case '?': exit(1); /* Was it han node address request? */ case 'a': if(sscanf(optarg, "%hx", &hannodeaddr) != 1) fatal("Invalid node address"); if(hannodeaddr > 32) fatal("Node address out of range"); flags.hanmode = 1; break; /* Was it a check app request? */ case 'c': flags.checkapp = 1; break; /* Was it a debug level set? */ case 'd': /* Save the value. */ debuglvl=strtol(optarg, NULL, 10); if((debuglvl < 0 || debuglvl > DEBUG_MAX)) { fatal("Invalid debug level"); } break; case 'e': flags.eeprom = 1; break; /* Was it a file request? */ case 'f': memset(file, 0, MAX_PATH); strncpy(file, optarg, MAX_PATH); break; /* Was it a help request? */ case 'h': show_help(); exit(0); case 'i': flags.interrogateonly = 1; break; case 'o': if(sscanf(optarg, "%X", &i) != 1) fatal("Product ID needs hexadecimal value"); productid = (u16) i; break; /* Was it a port request? */ case 'p': memset(port, 0, MAX_PATH); strncpy(port, optarg, MAX_PATH); break; /* Was it a reset request */ case 'r': flags.reset = 1; break; /* Was it a verbose request? */ case 'v': flags.verbose = 1; break; case 'V': printf("PCL version %s\n", PCL_VERSION); exit(0); /* Was it Execute App ? */ case 'x': flags.execute = 1; break; case 'z': /* handled previously */ break; /* It was something weird.. */ default: panic("Unhandled getopt return value %c", optchar); } } /* If there were any extra arguments, we should complain. */ if(optind < argc) { fatal("Extra argument on commandline, '%s'", argv[optind]); } if(flags.eeprom && (flags.execute | flags.checkapp)) fatal("-e is not valid with -x or -c"); if(!(flags.interrogateonly | flags.execute | flags.checkapp | flags.eeprom)) fatal("What do you want me to do, anyhow? Must specify -e, -c, -i, or -x"); r = (response_t *) ((flags.hanmode) ? packet.han.payload : packet.pbl.payload); cf = (config_area_t *) r->config; // Send a query packet packet_init(); if(flags.hanmode){ // If HAN address specified res = hanclient_connect_setup("","", service, host); if(!res){ // if connect setup OK printf("Test for hand running\n"); client_command.request = HAN_CCMD_DAEMON_INFO; res = hanclient_send_command_return_res(&client_command); printf("Hand %s running\n", (res) ? "is not" : "is"); if(!res){ // If hand is loaded, send boot loader entry command debug(DEBUG_EXPECTED,"Hand version: %s", client_command.cmd.info.version); // Check for structure size match between us and han daemon if(client_command.cmd.info.cmdpktsize != sizeof(struct han_packet)){ debug(DEBUG_UNEXPECTED, "Hand command structure incompatible"); fatal(not_comp); } if(client_command.cmd.info.rawsize != sizeof(struct han_raw)){ debug(DEBUG_UNEXPECTED, "Hand raw command structure incompatible"); fatal(not_comp); } flags.handisrunning = 1; // Set flag indicating comm is going to go through hand memset(&client_command,0,sizeof(Client_Command)); // Send boot loader entry command client_command.request = HAN_CCMD_SENDPKT; client_command.cmd.pkt.nodecommand = HAN_CMD_GEBL; client_command.cmd.pkt.numnodeparams = 2; client_command.cmd.pkt.nodeparams[0] = 0x55; client_command.cmd.pkt.nodeparams[1] = 0xAA; client_command.cmd.pkt.nodeaddress = hannodeaddr; printf("Sending boot loader entry request\n"); res = hanclient_send_command_return_res(&client_command); res = res | client_command.commstatus; printf("Boot loader %s entered via HAN command\n", (res) ? "not" : "was"); if(!res){ printf("Waiting for boot loader to initialize..."); fflush(stdout); usleep(3000000); // Wait for boot loader to activate printf("OK\n"); } } } // Set up packet parameters for addressable mode packet.han.param = 0x55AA; // Not required by protocol packet.han.pkttype = HDC; packet.han.addr = (u8) hannodeaddr; packet_size = sizeof(packet_t); } else{ // Non-addressable operating mode packet.pbl.param = 0x55AA; // Not required by protocol packet_size = sizeof(packet_t_pbl); } if(!flags.handisrunning){ // If not going through hand if(!port[0]) fatal("Missing port (-p) option on command line or config file"); if(!(s = serio_open(port, (flags.hanmode) ? 9600 : 57600))) fatal("Can't open serial port %s\n", port); serio_flush_input(s); packet_finalize(); debug(DEBUG_ACTION, "Transmit Packet CRC: 0x%04X", (flags.hanmode) ? packet.han.crc16 : packet.pbl.crc16); if((bytes_sent = packet_tx(s, packet.buffer, packet_size, 5000000)) < 0) fatal("Packet write error"); debug(DEBUG_ACTION, "Bytes Sent: %d", bytes_sent); if(bytes_sent != packet_size) fatal("Packet write incomplete"); packet_init(); // Just to be sure we get something // Wait for response bytes_received = packet_rx(s, packet.buffer, packet_size, 5000000); debug(DEBUG_ACTION, "Bytes Received: %d", bytes_received); if(bytes_received < 0) fatal("Packet read error"); if((bytes_received != packet_size)) // Must see a packet, not just an ACK fatal("Packet read incomplete, received %d bytes", bytes_received); if(packet_check()) fatal("Packet CRC error"); } else{ // Send packets through hand */ debug(DEBUG_ACTION,"Sending packets through hand"); packet_finalize(); debug(DEBUG_ACTION, "Transmit Packet CRC: 0x%04X", (flags.hanmode) ? packet.han.crc16 : packet.pbl.crc16); for(i = 0; i < PACKET_RETRIES; i++){ client_command.cmd.raw.txlen = packet_format(client_command.cmd.raw.txbuffer, &packet.han, packet_size); client_command.cmd.raw.rxexpectlen = 255; client_command.cmd.raw.txtimeout = 100000; client_command.cmd.raw.rxtimeout = 500000; client_command.request = HAN_CCMD_RAW_PACKET; res = hanclient_send_command_return_res(&client_command); if((!res) && (client_command.cmd.raw.rxexpectlen)) bytes_received = packet_unformat(packet.buffer, client_command.cmd.raw.rxbuffer, 255); else bytes_received = 0; if(bytes_received) crcerr = packet_check(); else crcerr = 0; if((bytes_received == packet_size) && (!crcerr)) // Must see a packet with a good CRC, not just an ACK break; debug(DEBUG_UNEXPECTED,"Query packet receive error, try = %d", i); debug(DEBUG_UNEXPECTED,"bytes_received = %d crcerr = %d", bytes_received, crcerr); } if(i == PACKET_RETRIES) fatal("Too many packet retries!"); } if(flags.verbose || flags.interrogateonly){ printf("Loader Size in Words: 0x%04X\n", r->lsize); printf("App. Size In Words : 0x%04X\n", r->appsize); printf("Product ID : 0x%04X\n", r->prodid); printf("Boot Program Version: 0x%02X\n", r->bootvers); printf("Protocol Number : 0x%02X\n", r->proto); printf("Device User 1 : 0x%04X\n", cf->user1); printf("Device User 2 : 0x%04X\n", cf->user2); printf("Device User 3 : 0x%04X\n", cf->user3); printf("Device User 4 : 0x%04X\n", cf->user4); printf("Device ID : 0x%04X\n", cf->deviceid); printf("Device Config 1 : 0x%04X\n", cf->config1); printf("Device Config 2 : 0x%04X\n", cf->config2); } if((flags.execute) && (!file[0])){ /* Special case for execute without load */ printf("Check App: "); if(send_command(s, BC_CHECK_APP, 0, NULL)){ printf("FAILED\n"); exit(1); } else printf("PASSED\n"); printf("Executing App... "); if(send_command(s, BC_EXEC_APP, 0, NULL)){ printf("FAILED\n"); exit(1); } printf("\n"); exit(0); } if(flags.interrogateonly) /* If interrogate only, exit now */ exit(0); if(r->prodid != productid) fatal("Wrong product ID: specified: %04X, device reports: %04X", productid, r->prodid); if(r->bootvers > BOOT_VERSION_SUPPORTED) fatal("Do not know how to deal with bootversion %d\n", r->bootvers); if(r->proto) fatal("Does not support protocol version %d\n", r->proto); max_app_size = r->appsize; bootloader_size = r->lsize; if(!file[0]) fatal("Missing file (-f) option on command line"); // Allocate buffer if(!(buffer = malloc(max_app_size << 1))) fatal("No memory for buffer"); if(!flags.eeprom){ /* Fill buffer with erase pattern */ for(i = 0 ; i < max_app_size << 1; i++) buffer[i] = (i & 1) ? 0x3F : 0xFF; } /* Locate the extension if it exists */ for(i = strlen(file), q = NULL; i >= 0 ; i--){ if(file[i] == '/'){ break; } if(file[i] == '.'){ q = file + i + 1; break; } } if(q) strncpy(exten, q, 18); if(!strcmp(exten, "hex")){ /* Hex files */ if(!(ihx = ihx_read( file, buffer, max_app_size << 1))) fatal("Could not open and/or read hex file"); load_address = ihx->load_address >> 1; load_size = ihx->size >> 1; load_size_bytes = ihx->size; ihx_free(ihx); } else if(!strcmp(exten, "bin")){