Beispiel #1
0
/**
 * @brief Build auth success reply
 */
static int build_authsucc_response(SSH *ssh) {
	byte *p;

	p = ssh->PacketBuffer;
	memset(p, 0, TCPPACKETSIZE);

	packet_begin(&p, SSH_MSG_USERAUTH_SUCCESS);

	ssh->Length = (p - ssh->PacketBuffer);

	packet_finalize(ssh);

	return 0;
}
Beispiel #2
0
/**
 * @brief Build auth fail reply
 */
static int build_authfail_response(SSH *ssh) {
	byte *p;

	p = ssh->PacketBuffer;
	memset(p, 0, TCPPACKETSIZE);

	packet_begin(&p, SSH_MSG_USERAUTH_FAILURE);			/* Type message */
	packet_add_name_list(&p, PASSWORD_STR);				/* String password */
	packet_add_byte(&p, 0);								/* False */

	ssh->Length = (p - ssh->PacketBuffer);

	packet_finalize(ssh);

	return 0;
}
Beispiel #3
0
Datei: pcl.c Projekt: hwstar/pbl
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")){
Beispiel #4
0
Datei: pcl.c Projekt: hwstar/pbl
static int send_command(serioStuff *s, u8 cmd, u16 param, void *payload)
{
	int retries, res;
	int bytes_sent, bytes_received;
	u8 ack,nak;
	unsigned char resp = 0x55; 
	static u16 seqno = 0;


	packet_init();

	if(flags.hanmode){
		packet.han.pkttype = HDC;
		packet.han.addr = (u8) hannodeaddr; 
		packet.han.cmd = cmd;
		packet.han.param = param;
		packet.han.seq = seqno;
		ack = HDC_ACK;
		nak = HDC_NAK;
		
	}
	else{
		packet.pbl.cmd = cmd;
		packet.pbl.param = param;
		packet.pbl.seq = seqno;
		ack = ACK;
		nak = NAK;
	}
	if(payload)
		memcpy((flags.hanmode) ? packet.han.payload : packet.pbl.payload, payload, LOADER_PAYLOAD);
	packet_finalize();
	debug(DEBUG_ACTION,"Command: 0x%02X Sequence Number: %d, CRC: 0x%04X", cmd, seqno, (flags.hanmode)? packet.han.crc16 : packet.pbl.crc16);

	if(!flags.handisrunning){ // Hand not running?
		serio_flush_input(s);
		for(retries = PACKET_RETRIES;;){
			if((bytes_sent = packet_tx(s, packet.buffer, packet_size, 5000000)) < 0){
				debug(DEBUG_ACTION, "Command Packet Write Error");
				return FAIL;
			}
			debug(DEBUG_ACTION, "Bytes Sent: %d", bytes_sent);
			if(bytes_sent != packet_size){
				debug(DEBUG_ACTION, "Command Packet Write Incomplete");
				return FAIL;
			}

			for(;;){
				bytes_received = serio_read(s, &resp, 1, 5000000);
				if(bytes_received < 0){
					if(errno != EAGAIN){
						debug(DEBUG_UNEXPECTED, "Response Read Error: %s", strerror(errno));
						return FAIL;
					}
					else{
						continue;
					}
				}
				else{
					break;
				}
			}
	
			if(bytes_received != 1){
				debug(DEBUG_ACTION, "Read Timeout Error");
				return FAIL;
			}

			if((resp == ack)||(resp == nak)){
				debug(DEBUG_ACTION, "Response: %s",(resp == ack)?"ACK":"NAK");
			}
			else{
				debug(DEBUG_ACTION, "Response: 0x%02X", (unsigned int) resp);
			}

			if((cmd == BC_CHECK_APP) && (resp == STX)){
				debug(DEBUG_UNEXPECTED, "Check App Failed");
				return FAIL;
			}

			if((resp != ack)){				
				debug(DEBUG_ACTION, "Did not get ACK");
				if((resp == nak) && (retries--)){
					debug(DEBUG_UNEXPECTED, "***Retrying packet***");
					usleep(100000);
					continue;
				}
				return FAIL; // Retries used up or didn't get ACK or NAK. Fail.
			}
			break;
		}
	}
	else{ // Hand is running
		for(retries = 0; retries < PACKET_RETRIES; retries++){
			client_command.cmd.raw.txlen = packet_format(client_command.cmd.raw.txbuffer, &packet.han, packet_size);
			client_command.cmd.raw.rxexpectlen = 1;
			client_command.cmd.raw.txtimeout = 100000;
			client_command.cmd.raw.rxtimeout = 1000000;
			client_command.request = HAN_CCMD_RAW_PACKET;
			res = hanclient_send_command_return_res(&client_command);
			bytes_received = 0;
			if(!res)
				bytes_received = client_command.cmd.raw.rxexpectlen;
			if(bytes_received == 1){
				resp = client_command.cmd.raw.rxbuffer[0];
				if((resp == ack)||(resp == nak)){
					debug(DEBUG_ACTION, "Response: %s",(resp == ack)?"ACK":"NAK");
				}
				else{
					debug(DEBUG_ACTION, "Response: 0x%02X", (unsigned int) resp);
				}

				if((cmd == BC_CHECK_APP) && (resp == STX)){
					debug(DEBUG_UNEXPECTED, "Check App Failed");
					return FAIL;
				}

				if((resp != ack)){				
					debug(DEBUG_ACTION, "Did not get ACK");
					if(resp == nak){
						debug(DEBUG_UNEXPECTED, "***Retrying packet***, try = %d", retries);
						usleep(100000);
						continue;
					}
				}
				else
					break; // Success
			}
			debug(DEBUG_UNEXPECTED,"Received invalid or no response, try = %d", retries);
			usleep(100000);
		}
		if(retries == PACKET_RETRIES){
			debug(DEBUG_EXPECTED, "Too many packet retries!");
			return FAIL;
		}
	}

	seqno++;
	return PASS;
}