Esempio n. 1
0
int main (int argc,char *argv[])
{
   const char *progname;
   int err = 0,fd,option = OPT_INFO;
   int open_flag;
   (progname = strrchr (argv[0],'/')) ? progname++ : (progname = argv[0]);

   /* parse command-line options */
   if (argc == 3 && !strcmp (argv[1],"info"))
	 option = OPT_INFO;
   else if (argc == 6 && !strcmp (argv[1],"read"))
	 option = OPT_READ;
   else if (argc == 6 && !strcmp (argv[1],"write"))
	 option = OPT_WRITE;
   else if (argc == 5 && !strcmp (argv[1],"erase"))
	 option = OPT_ERASE;
   else
	 showusage (progname);

   /* open device */
   open_flag = (option==OPT_INFO || option==OPT_READ) ? O_RDONLY : O_RDWR;
   if ((fd = open (argv[2],O_SYNC | open_flag)) < 0)
	 {
		perror ("open()");
		exit (1);
	 }

   switch (option)
	 {
	  case OPT_INFO:
		showinfo (fd);
		break;
	  case OPT_READ:
		err = flash_to_file (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0),argv[5]);
		break;
	  case OPT_WRITE:
		err = file_to_flash (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0),argv[5]);
		break;
	  case OPT_ERASE:
		err = erase_flash (fd,strtol (argv[3],NULL,0),strtol (argv[4],NULL,0));
		break;
	 }

   /* close device */
   if (close (fd) < 0)
	 perror ("close()");

   exit (err);
}
Esempio n. 2
0
int main( void )
{
	// TODO: default implementation is bloated
	//clock_prescale_set( clock_div_1 );
	
	// Allow user to see registers before any disruption
	bootLoaderInit();
	
	initHardware(); // gives time for jumper pull-ups to stabilize
	
	while ( bootLoaderCondition() )
	{
		// Run USB until we have some action to take and that transaction is complete
		uchar prevTxLen;
		do {
			prevTxLen = usbTxLen;
			wait_usb_interrupt();
		}
		while ( !(prevCommand != cmd_info &&
				usbTxLen == USBPID_NAK && prevTxLen != USBPID_NAK) );
		
		// Stops once we have a command and we've just transmitted the final reply
		// back to host
		
		// Now we can ignore USB until our host program makes another request
		
		if ( prevCommand == cmd_erase )
			erase_flash();
		else if ( prevCommand == cmd_write )
			write_flash();
		else
			break;
	}
	
	leaveBootloader();
}
Esempio n. 3
0
void flash(char *file, unsigned char skip_validation) {
	FILE *fp;
	long flash_length;
	char *flash_data;
	struct mpsse_context *mpsse;

	printf("reading input file..");
	fflush(stdout);

	fp = fopen(file, "r");
	if (!fp) {
		printf("failed!\n");
		fprintf(stderr, "error: could not open '%s'", file);
		exit(1);
	}

	fseek(fp, 0L, SEEK_END);
	flash_length = ftell(fp);

	if (!flash_length) {
		printf("failed!\n");
		fprintf(stderr, "error: file length was 0 bytes\n");
		fclose(fp);
		exit(1);
	}

	if (flash_length > N25Q128A_TOTAL_BYTES) {
		printf("failed!\n");
		fprintf(stderr, "error: file length exceeds flash capacity\n");
		fclose(fp);
		exit(1);
	}

	rewind(fp);

	flash_data = (char *)malloc(flash_length * sizeof(char));

	if (!flash_data) {
		printf("failed!\n");
		fprintf(stderr, "error: could not allocate memory");
		fclose(fp);
		exit(1);
	}

	if (fread(flash_data, 1, flash_length, fp) != flash_length) {
		printf("failed!\n");
		fprintf(stderr, "error: could not read entire file");
		free(flash_data);
		fclose(fp);
		exit(1);
	}

	fclose(fp);

	printf("\n");

	mpsse = initialize_mpsse();

	if (!mpsse)
		exit(1);

	printf("flashing chip..");
	fflush(stdout);

	if (set_spartan_program_b(mpsse, 0) != MPSSE_OK)
		goto program_b_error;
	if (erase_flash(mpsse) != MPSSE_OK)
		goto error;
	if (program_flash(mpsse, flash_data, flash_length) != MPSSE_OK)
		goto error;

	if (!skip_validation) {
		unsigned char matched;
		printf("\nvalidating..");
		fflush(stdout);
		if (validate_flash(mpsse, flash_data, flash_length,
			     &matched) != MPSSE_OK)
			goto error;
		if (!matched) {
			printf("failed!\n");
			fprintf(stderr, "error: flash mismatch\n");
			goto validation_error;
		}
	}

	printf("success!\n");
	if (set_spartan_program_b(mpsse, 1) != MPSSE_OK)
		goto program_b_error_quiet;
	free(flash_data);
	Close(mpsse);
	return;

error:
	printf("failed!\n");
	fprintf(stderr, "mpsse error: %s\n", ErrorString(mpsse));
validation_error:
	if (set_spartan_program_b(mpsse, 1) != MPSSE_OK)
		goto program_b_error_quiet;
	free(flash_data);
	Close(mpsse);
	exit(1);
program_b_error:
	printf("failed!\n");
program_b_error_quiet:
	fprintf(stderr, "error: unable to change PROGRAM_B pin!\n");
	fprintf(stderr, "mpsse error: %s\n", ErrorString(mpsse));
	free(flash_data);
	Close(mpsse);
	exit(1);
}
Esempio n. 4
0
void link_cmd_ioctl(link_data_t * args){
	int err;
	int size;
	size = _IOCTL_SIZE(args->op.ioctl.request);
	bootloader_attr_t attr;
	bootloader_writepage_t wattr;

	dstr("IOCTL REQ: "); dhex(args->op.ioctl.request); dstr("\n");

	switch(args->op.ioctl.request){
	case I_BOOTLOADER_ERASE:
		//the erase takes awhile -- so send the reply a little early
		link_protocol_slavewrite(phy_handle, &args->reply, sizeof(args->reply), NULL, NULL);
		args->op.cmd = 0;

		erase_flash();
		is_erased = true;
		return;
	case I_BOOTLOADER_GETATTR:
		//write data to io_buf
		attr.version = BCDVERSION;
		_hwpl_core_getserialno(attr.serialno);

		err = link_protocol_slavewrite(phy_handle, &attr, size, NULL, NULL);
		if ( err == -1 ){
			args->op.cmd = 0;
			args->reply.err = -1;
		}

		attr.startaddr = PROGRAM_START_ADDR;
		break;
	case I_BOOTLOADER_RESET:
		if( args->op.ioctl.arg == 0 ){
			link_cmd_reset(args);
		} else {
			link_cmd_reset_bootloader(args);
		}
		break;
	case I_BOOTLOADER_WRITEPAGE:

#ifdef __SECURE
		//decrypt incoming data

#endif

		err = link_protocol_slaveread(phy_handle, &wattr, size, NULL, NULL);
		if( err < 0 ){
			dstr("failed to read data\n");
			break;
		}

		args->reply.err = flash_writepage(FLASH_PORT, (flash_writepage_t*)&wattr);
		if( args->reply.err < 0 ){
			dstr("Failed to write flash\n");
		}

		break;
	default:
		args->reply.err_number = EINVAL;
		args->reply.err = -1;
		break;
	}

	if ( args->reply.err < 0 ){
		args->reply.err_number = errno;
	}

}
Esempio n. 5
0
int __attribute__ ((section (".data"))) reprogram_helper(uint32_t ex_flash_addr, int type)
{
  
   
   
   uint32_t addr = ex_flash_addr;
   uint16_t c_checksum;
   uint16_t a_checksum;
   boolean write = (type == RP_WRITE);

   if (write)
      mos_disable_ints();
       
   dev_open(DEV_TELOS_FLASH);
   dev_mode(DEV_TELOS_FLASH, DEV_MODE_ON);  

   if (!write)
      printf("verifying...\n");
   

   
   if (write)
      erase_flash(0x4000, MSP_FLASH_ERASE_ALL);

   dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
   dev_read(DEV_TELOS_FLASH, buffer, 6);  
   
   uint16_t sect_count = *(uint16_t*)(buffer + 4);
  
   //printf("%d sectors to write\n", sect_count);
   //printf("looking for file at %x\n", (uint16_t)ex_flash_addr);
   
   uint16_t sect_size;
   uint16_t sect_addr;
   
   // skip past header
   addr += 6;
   dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
  
   int i;
   
   for(i = 0; i < sect_count; ++i)
   {
      // read the sector size
      LED_ON(i + 1);
      dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
 

      dev_read(DEV_TELOS_FLASH, &sect_size, 2);
      addr += 2;
      
      dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);

      // read the sector address
      dev_read(DEV_TELOS_FLASH, &sect_addr, 2);
      addr += 2;
      
      // begin computing checksum;
      c_checksum = 0;
      c_checksum ^= sect_size;
      c_checksum ^= sect_addr;
      
      //printf("[%d]  %d bytes at address %x\n", i, sect_size, sect_addr);

      uint16_t blocks = sect_size / MSP_FLASH_BLOCK_SIZE;
      uint16_t left_over = sect_size % MSP_FLASH_BLOCK_SIZE;
      
      while(blocks--)
      {
	 dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
	 dev_read(DEV_TELOS_FLASH, buffer, sizeof(buffer));
	
	 msp_flash_address = sect_addr;

	 if (write)
	    dev_write(DEV_MSP_FLASH, buffer, sizeof(buffer));
	 else
	 {
	    uint16_t* iter = (uint16_t*)buffer;
	    uint16_t* iter_end = (uint16_t*)(buffer + sizeof(buffer));
	    for( ; iter != iter_end; ++iter)
	       c_checksum ^= *iter;
	    
	    
	 }
	 
	 sect_addr += sizeof(buffer);
	 addr += sizeof(buffer);
       }
      
      
      if (left_over)
      {
	 
	 dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
	 dev_read(DEV_TELOS_FLASH, buffer, left_over);

	 
	 msp_flash_address = sect_addr;
	 if (write)
	    dev_write(DEV_MSP_FLASH, buffer, left_over);
	 else
	 {
	    uint16_t* iter = (uint16_t*)buffer;
	    uint16_t* iter_end = (uint16_t*)(buffer + left_over);
	    for( ; iter != iter_end; ++iter)
	       c_checksum ^= *iter;
	    
	 }
	 
	 addr += left_over;
      }

      dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
      dev_read(DEV_TELOS_FLASH, &a_checksum, sizeof(a_checksum));
      //printf("checksum (computed): %x\n checksum (actual): %x\n", c_checksum, a_checksum);

      if (!write)
      {
	 if (c_checksum != a_checksum)
	    return FALSE;
      }
      
      addr += 2;
     
      
      dev_ioctl(DEV_TELOS_FLASH, DEV_SEEK, addr);
      
   }

   if (write)
      asm volatile ("br #0x4000h\n");
   
   // shhh
   //dev_close(DEV_TELOS_FLASH);
   
   return TRUE;
 
   
}
Esempio n. 6
0
int main (void)
{
	// Short circuit test
	/*
	DDRB |= (1 << 6) | (1 << 7); // LEDS
	DDRD = (1<<7) | (1<<6); // ROMSEL, R/W
	PORTD = (1<<7) | (1<<6); // ROMSEL, R/W	

	while(1) 
	{
		int i;
		for (i = 0; i < 8; i++)
		{
			DDRA = 0;
			PORTA = 0xFF;
			DDRF = 0;
			PORTF = 0xFF;
			PORTA &= ~(1<<i);
			DDRA |= 1<<i;
			LED_RED_OFF;
			LED_GREEN_OFF;
			_delay_ms(500);
			if ((PINA != PORTA) || (PINF != PORTF))
			{
				LED_RED_ON;
			} else {
				LED_GREEN_ON;
			}
			_delay_ms(500);
		}
		for (i = 0; i < 8; i++)
		{
			DDRA = 0;
			PORTA = 0xFF;
			DDRF = 0;
			PORTF = 0xFF;
			PORTF &= ~(1<<i);
			DDRF |= 1<<i;
			LED_RED_OFF;
			LED_GREEN_OFF;
			_delay_ms(500);
			if ((PINA != PORTA) || (PINF != PORTF))
			{
				LED_RED_ON;
			} else {
				LED_GREEN_ON;
			}
			_delay_ms(500);
		}

	}
*/
/*
	DDRB |= (1 << 6) | (1 << 7); // LEDS
	DDRD = (1<<7) | (1<<6); // ROMSEL, R/W
	PORTD = (1<<7) | (1<<6); // ROMSEL, R/W

	while(1) 
	{
		int i;
		for (i = 0; i < 8; i++)
		{
			DDRC = 0;
			PORTC = 0xFF;
			PORTC &= ~(1<<i);
			DDRC |= 1<<i;
			LED_RED_OFF;
			LED_GREEN_OFF;
			_delay_ms(500);
			if ((PINC != PORTC))
			{
				LED_RED_ON;
			} else {
				LED_GREEN_ON;
			}
			_delay_ms(500);
		}

	}
*/

	sei();
	USART_init();
	USART_init2();	
		
#ifdef UPDATE_CHR_MODULE
  unsigned int bd = (F_CPU / (16UL * 19200UL)) - 1;
  UBRR0L = bd & 0xFF;
  UBRR0H = bd >> 8;
  UBRR1L = bd & 0xFF;
  UBRR1H = bd >> 8;
	LED_RED_ON;
	LED_GREEN_ON;		

	while(1)	{	}
#endif

	init_ports();	

	LED_RED_OFF;
	LED_GREEN_OFF;
	
	// MMC1 test
	/*
	set_address(0x8000);
	PHI2_LOW;
	MODE_WRITE;
	PORTC = 0;
	while (1)
	{
		LED_GREEN_OFF;
		ROMSEL_HI;
		_delay_ms(200);
		LED_GREEN_ON;
		ROMSEL_LOW;
		_delay_ms(200);
	}
	*/
	/*
	write_prg_byte(0x8000, 0x80);
	write_prg_byte(0x8000, 0); 
	write_prg_byte(0x8000, 0);
	write_prg_byte(0x8000, 1);
	write_prg_byte(0x8000, 1);
	write_prg_byte(0x8000, 0);
	
	write_prg_byte(0xe000, 0); 
	write_prg_byte(0xe000, 0);
	write_prg_byte(0xe000, 0);
	write_prg_byte(0xe000, 0);
	write_prg_byte(0xe000, 0);
	
	_delay_ms(500);
	
	MODE_READ;
	//set_address(0x8000);
	PORTA = 0;
	while(1);
	*/	
	
	comm_init();
	comm_start(COMMAND_PRG_STARTED, 0);

	uint16_t address;
	uint16_t length;	
	
	unsigned long int t = 0;
	char led_down = 0;
	int led_bright = 0;
	
	while (1)
	{
		TCCR1A |= (1<<COM1C1) | (1<<COM1B1) | (1<<WGM10);
		TCCR1B |= (1<<CS10);
		if (t++ >= 10000)
		{
			if (!led_down)
			{
				led_bright++;
				if (led_bright >= 110) led_down = 1;
			} else {
				led_bright--;
				if (!led_bright) led_down = 0;
			}
			if (led_bright >= 100) OCR1B = led_bright - 100;
			if (led_down)
			{
				int led_bright2 = 110-led_bright;
				if (led_bright2 <= 20)
				{
					if (led_bright2 > 10) led_bright2 = 20 - led_bright2;
					OCR1C = led_bright2*2;
				}
			}
			t = 0;
		}
		
		if (comm_recv_done)
		{
			t = led_down = led_bright = 0;
			TCCR1A = OCR1B = OCR1C = 0;
			
			switch (comm_recv_command)
			{
				case COMMAND_PRG_INIT:
					comm_start(COMMAND_PRG_STARTED, 0);
					break;
					
				case COMMAND_PRG_READ_REQUEST:
					address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8);
					length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8);
					read_prg_send(address, length);
					break;

				case COMMAND_PRG_WRITE_REQUEST:
					address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8);
					length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8);
					write_prg(address, length, (uint8_t*)&recv_buffer[4]);
					comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;

				case COMMAND_PHI2_INIT:
					phi2_init();
					comm_start(COMMAND_PHI2_INIT_DONE, 0);
					break;

				case COMMAND_RESET:
					reset_phi2();
					comm_start(COMMAND_RESET_ACK, 0);
					break;
					
				case COMMAND_EPROM_PREPARE:
					write_eprom_prepare();
					break;

				case COMMAND_PRG_EPROM_WRITE_REQUEST:
					address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8);
					length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8);
					write_eprom(address, length, (uint8_t*)&recv_buffer[4]);
					comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;

				case COMMAND_PRG_FLASH_ERASE_REQUEST:
					if (erase_flash())
						comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;

				case COMMAND_PRG_FLASH_WRITE_REQUEST:
					address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8);
					length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8);
					if (write_flash(address, length, (uint8_t*)&recv_buffer[4]))
						comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;

				case COMMAND_COOLGIRL_ERASE_SECTOR_REQUEST:
					if (erase_coolgirl_sector())
						comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;

				case COMMAND_COOLGIRL_WRITE_REQUEST:
					address = recv_buffer[0] | ((uint16_t)recv_buffer[1]<<8);
					length = recv_buffer[2] | ((uint16_t)recv_buffer[3]<<8);
					if (write_coolgirl(address, length, (uint8_t*)&recv_buffer[4]))
						comm_start(COMMAND_PRG_WRITE_DONE, 0);
					break;
			}
			comm_recv_done = 0;
		}		
	}
}