Ejemplo n.º 1
0
/*
 *  is_fw_update_mode() - Determines whether in firmware update mode or not
 *
 *  INPUT
 *      none
 *  OUTPUT
 *      true/false whether firmware is in update mode
 *
 */
static bool is_fw_update_mode(void)
{
#if DEBUG_LINK
    return true;
#else
    return keepkey_button_down();
#endif
}
Ejemplo n.º 2
0
void u2fhid_read_start(const U2FHID_FRAME *f) {
	U2F_ReadBuffer readbuffer;
	memzero(&readbuffer, sizeof(readbuffer));

	if (!(f->type & TYPE_INIT)) {
		return;
	}

	// Broadcast is reserved for init
	if (f->cid == CID_BROADCAST || f->cid == 0) {
		send_u2fhid_error(f->cid, ERR_INVALID_CID);
		return;
	}

	if ((unsigned)MSG_LEN(*f) > sizeof(reader->buf)) {
		send_u2fhid_error(f->cid, ERR_INVALID_LEN);
		return;
	}

	reader = &readbuffer;
	u2fhid_init_cmd(f);

	usbTiny(1);
	for(;;) {
		// Do we need to wait for more data
		while ((reader->buf_ptr - reader->buf) < (signed)reader->len) {
			uint8_t lastseq = reader->seq;
			uint8_t lastcmd = reader->cmd;
			int counter = U2F_TIMEOUT;
			while (reader->seq == lastseq && reader->cmd == lastcmd) {
				if (counter-- == 0) {
					// timeout
					send_u2fhid_error(cid, ERR_MSG_TIMEOUT);
					cid = 0;
					reader = 0;
					usbTiny(0);
					layoutHome();
					return;
				}
				usbPoll();
			}
		}

		// We have all the data
		switch (reader->cmd) {
		case 0:
			// message was aborted by init
			break;
		case U2FHID_PING:
			u2fhid_ping(reader->buf, reader->len);
			break;
		case U2FHID_MSG:
			u2fhid_msg((APDU *)reader->buf, reader->len);
			break;
		case U2FHID_WINK:
			u2fhid_wink(reader->buf, reader->len);
			break;
		default:
			send_u2fhid_error(cid, ERR_INVALID_CMD);
			break;
		}

		// wait for next commmand/ button press
		reader->cmd = 0;
		reader->seq = 255;
		while (dialog_timeout > 0 && reader->cmd == 0) {
			dialog_timeout--;
			usbPoll(); // may trigger new request
			//buttonUpdate();
			if (keepkey_button_down() &&
				(last_req_state == AUTH || last_req_state == REG)) {
				last_req_state++;
				// standard requires to remember button press for 10 seconds.
				dialog_timeout = 10 * U2F_TIMEOUT;
			}
		}

		if (reader->cmd == 0) {
			last_req_state = INIT;
			cid = 0;
			reader = 0;
			usbTiny(0);
			layoutHome();
			return;
		}
	}
}