#include "BigUnsigned.hh" // Memory management definitions have moved to the bottom of NumberlikeArray.hh. // The templates used by these constructors and converters are at the bottom of // BigUnsigned.hh. BigUnsigned::BigUnsigned(unsigned long x) { initFromPrimitive (x); } BigUnsigned::BigUnsigned(unsigned int x) { initFromPrimitive (x); } BigUnsigned::BigUnsigned(unsigned short x) { initFromPrimitive (x); } BigUnsigned::BigUnsigned( long x) { initFromSignedPrimitive(x); } BigUnsigned::BigUnsigned( int x) { initFromSignedPrimitive(x); } BigUnsigned::BigUnsigned( short x) { initFromSignedPrimitive(x); } unsigned long BigUnsigned::toUnsignedLong () const { return convertToPrimitive (); } unsigned int BigUnsigned::toUnsignedInt () const { return convertToPrimitive (); } unsigned short BigUnsigned::toUnsignedShort() const { return convertToPrimitive (); } long BigUnsigned::toLong () const { return convertToSignedPrimitive< long >(); } int BigUnsigned::toInt () const { return convertToSignedPrimitive< int >(); } short BigUnsigned::toShort () const { return convertToSignedPrimitive< short>(); } // BIT/BLOCK ACCESSORS void BigUnsigned::setBlock(Index i, Blk newBlock) { if (newBlock == 0) { if (i < len) { blk[i] = 0; zapLeadingZeros(); } // If i >= len, no effect. } else { if (i >= len) { // The nonzero block extends the number. allocateAndCopy(i+1); // Zero any added blocks that we aren't setting. for (Index j = len; j < i; j++) blk[j] = 0; len = i+1; } blk[i] = newBlock; } } /* Evidently the compiler wants BigUnsigned:: on the return type because, at * that point, it hasn't yet parsed the BigUnsigned:: on the name to get the * proper scope. */ BigUnsigned::Index BigUnsigned::bitLength() const { if (isZero()) return 0; else { Blk leftmostBlock = getBlock(len - 1); Index leftmostBlockLen = 0; while (leftmostBlock != 0) { leftmostBlock >>= 1; leftmostBlockLen++; } return leftmostBlockLen + (len - 1) * N; } } void BigUnsigned::setBit(Index bi, bool newBit) { Index blockI = bi / N; Blk block = getBlock(blockI), mask = Blk(1) << (bi % N); block = newBit ? (block | mask) : (block & ~mask); setBlock(blockI, block); } // COMPARISON BigUnsigned::CmpRes BigUnsigned::compareTo(const BigUnsigned &x) const { // A bigger length implies a bigger number. if (len < x.len) return less; else if (len > x.len) return greater; else { // Compare blocks one by one from left to right. Index i = len; while (i > 0) { i--; if (blk[i] == x.blk[i]) continue; else if (blk[i] > x.blk[i]) return greater; else return less; } // If no blocks differed, the numbers are equal. return equal; } } // COPY-LESS OPERATIONS /* * On most calls to copy-less operations, it's safe to read the inputs little by * little and write the outputs little by little. However, if one of the * inputs is coming from the same variable into which the output is to be * stored (an "aliased" call), we risk overwriting the input before we read it. * In this case, we first compute the result into a temporary BigUnsigned * variable and then copy it into the requested output variable *this. * Each put-here operation uses the DTRT_ALIASED macro (Do The Right Thing on * aliased calls) to generate code for this check. * * I adopted this approach on 2007.02.13 (see Assignment Operators in * BigUnsigned.hh). Before then, put-here operations rejected aliased calls * with an exception. I think doing the right thing is better. * * Some of the put-here operations can probably handle aliased calls safely * without the extra copy because (for example) they process blocks strictly * right-to-left. At some point I might determine which ones don't need the * copy, but my reasoning would need to be verified very carefully. For now * I'll leave in the copy. */ #define DTRT_ALIASED(cond, op) \ if (cond) { \ BigUnsigned tmpThis; \ tmpThis.op; \ *this = tmpThis; \ return; \ } void BigUnsigned::add(const BigUnsigned &a, const BigUnsigned &b) { DTRT_ALIASED(this == &a || this == &b, add(a, b)); // If one argument is zero, copy the other. if (a.len == 0) { operator =(b); return; } else if (b.len == 0) { operator =(a); return; } // Some variables... // Carries in and out of an addition stage bool carryIn, carryOut; Blk temp; Index i; // a2 points to the longer input, b2 points to the shorter const BigUnsigned *a2, *b2; if (a.len >= b.len) { a2 = &a; b2 = &b; } else { a2 = &b; b2 = &a; } // Set prelimiary length and make room in this BigUnsigned len = a2->len + 1; allocate(len); // For each block index that is present in both inputs... for (i = 0, carryIn = false; i < b2->len; i++) { // Add input blocks temp = a2->blk[i] + b2->blk[i]; // If a rollover occurred, the result is less than either input. // This test is used many times in the BigUnsigned code. carryOut = (temp < a2->blk[i]); // If a carry was input, handle it if (carryIn) { temp++; carryOut |= (temp == 0); } blk[i] = temp; // Save the addition result carryIn = carryOut; // Pass the carry along } // If there is a carry left over, increase blocks until // one does not roll over. for (; i < a2->len && carryIn; i++) { temp = a2->blk[i] + 1; carryIn = (temp == 0); blk[i] = temp; } // If the carry was resolved but the larger number // still has blocks, copy them over. for (; i < a2->len; i++) blk[i] = a2->blk[i]; // Set the extra block if there's still a carry, decrease length otherwise if (carryIn) blk[i] = 1; else len--; } void BigUnsigned::subtract(const BigUnsigned &a, const BigUnsigned &b) { DTRT_ALIASED(this == &a || this == &b, subtract(a, b)); if (b.len == 0) { // If b is zero, copy a. operator =(a); return; } else if (a.len < b.len) // If a is shorter than b, the result is negative. throw "BigUnsigned::subtract: " "Negative result in unsigned calculation"; // Some variables... bool borrowIn, borrowOut; Blk temp; Index i; // Set preliminary length and make room len = a.len; allocate(len); // For each block index that is present in both inputs... for (i = 0, borrowIn = false; i < b.len; i++) { temp = a.blk[i] - b.blk[i]; // If a rev
#!/usr/bin/env python
# Copyright 2017 Luiz Ribeiro <luizribeiro@gmail.com>, Sebastian Kaim <sebb@sebb767.de>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from __future__ import print_function

import os
import sys
import time
import argparse
import usb


def check_keyboard_normal_mode(vendor, product):
    """Returns a device if a ps2avrGB device in normal made (that is in keyboard mode) or None if it is not found."""
    return usb.core.find(idVendor=vendor, idProduct=product)

def check_keyboard_bootloader_mode():
    """Returns True if a ps2avrGB device in bootloader (flashable) mode is found and False otherwise."""
    return (usb.core.find(idVendor=0x16c0, idProduct=0x05df) is not None)

def flash_keyboard(firmware_file):
    """Calls bootloadHID to flash the given file to the device."""
    print('Flashing firmware to device ...')
    if os.system('bootloadHID -r "%s"' % firmware_file) == 0:
        print('\nDone!')
    else:
        print('\nbootloadHID returned an error.')

def print_device_info(dev):
    """Prints all infos for a given USB device"""
    print('Device Information:')
    print('  idVendor: %d (0x%04x)' % (dev.idVendor, dev.idVendor))
    print('  idProduct: %d (0x%04x)' % (dev.idProduct, dev.idProduct))
    print('Manufacturer: %s' % (dev.iManufacturer))
    print('Serial: %s' % (dev.iSerialNumber))
    print('Product: %s' % (dev.iProduct), end='\n\n')

def send_device_to_bootloader_mode(dev):
    """Tries to send a given ps2avrGB keyboard to bootloader mode to allow flashing."""
    try:
        dev.set_configuration()

        request_type = usb.util.build_request_type(
            usb.util.CTRL_OUT,
            usb.util.CTRL_TYPE_CLASS,
            usb.util.CTRL_RECIPIENT_DEVICE)

        USBRQ_HID_SET_REPORT = 0x09
        HID_REPORT_OPTION = 0x0301

        dev.ctrl_transfer(request_type, USBRQ_HID_SET_REPORT, HID_REPORT_OPTION, 0, [0, 0, 0xFF] + [0] * 5)
    except usb.core.USBError:
        # for some reason I keep getting USBError, but it works!
        pass

def auto_int(value):
    """Helper for argparse to enable auto base detection"""
    return int(value, 0)

parser = argparse.ArgumentParser(description='Flash bootloadHID device')
parser.add_argument('--vendor', type=auto_int, default=0x20A0, help='Non bootloader idVendor to search for (default: 0x%(default)04x)')
parser.add_argument('--product', type=auto_int, default=0x422D, help='Non bootloader idProduct to search for (default: 0x%(default)04x)')
parser.add_argument('firmware_hex', type=argparse.FileType('r'), help='Firmware hex file to flash')
args = parser.parse_args()

kb = check_keyboard_normal_mode(args.vendor, args.product)

if kb is not None:
    print('Found a keyboard in normal mode. Attempting to send it to bootloader mode ...', end='')
    send_device_to_bootloader_mode(kb)
    print(' done.')
    print("Hint: If your keyboard can't be set to bootloader mode automatically, plug it in while pressing the bootloader key to do so manually.")
    print("      You can find more infos about this here: https://github.com/qmk/qmk_firmware/tree/master/keyboards/ps2avrGB#setting-the-board-to-bootloader-mode")

attempts = 12  # 60 seconds
found = False
for attempt in range(1, attempts + 1):
    print("Searching for keyboard in bootloader mode (%i/%i) ... " % (attempt, attempts), end='')

    if check_keyboard_bootloader_mode():
        print('Found', end='\n\n')
        flash_keyboard(args.firmware_hex.name)
        found = True
        break
    else:
        print('Nothing.', end='')

        if attempt != attempts:  # no need to wait on the last attempt
            print(' Sleeping 5 seconds.', end='')
            time.sleep(5)

        # print a newline
        print()

if not found:
    print("Couldn't find a flashable keyboard. Aborting.")
    sys.exit(2)