Esempio n. 1
0
    def rip_pages(self, start, end, filepath):
        last = end + 1
        pages = last - start

        f = open(filepath, 'a+')

        # Add S0 header record
        f.write(SRecord(STYPES['S0'], bytearray([0x00, 0x00]), binascii.hexlify("S19 ripped by fuct")).print_srec() + '\r\n')

        for i, page in enumerate(xrange(start, last)):
            addr = 0x8000
            rec_len = 16
            progress = float(i) / pages
            common.print_progress(progress)
            self.__set_page(page)
            page_data = binascii.hexlify(self.__read_page()).upper()

            # Trim empty flash from start and end of pages
            first_index = len(page_data)
            for index in xrange(0, len(page_data), 2):
                if page_data[index:index + 2] != 'FF':
                    first_index = index
                    break
            first_index = (first_index // rec_len) * rec_len

            last_index = 0
            for index in xrange(len(page_data), 0, -2):
                if page_data[index - 2:index] != 'FF':
                    last_index = index
                    break

            page_data = page_data[first_index:last_index]
            addr += first_index // 2

            # Split data into lines of readable length and stringify
            records = ''
            while len(page_data):
                rec_data = page_data[:rec_len * 2]
                records += SRecord(STYPES['S2'], bytearray([page, (addr >> 8) & 0xFF, addr & 0xFF]), rec_data).print_srec() + '\r\n'
                addr += len(rec_data) // 2
                page_data = page_data[rec_len * 2:]

            if len(records):
                f.write(records)

        # Add execution start address record
        f.write(SRecord(STYPES['S8'], bytearray([0x00, 0xC0, 0x00])).print_srec() + '\r\n')

        f.close()

        sys.stdout.write("\r")
        sys.stdout.flush()
        LOG.info("Firmware ripped successfully")
Esempio n. 2
0
    def rip_pages(self, start, end, filepath):
        last = end + 1
        pages = last - start

        # TODO: saving raw data is good enough?
        f = open(filepath, 'a+')
        for i, page in enumerate(xrange(start, last)):
            progress = float(i) / pages
            common.print_progress(progress)
            self.__set_page(page)
            f.write(self.__read_page())
        f.close()

        sys.stdout.write("\r")
        sys.stdout.flush()
        LOG.info("Firmware ripped successfully")
Esempio n. 3
0
    def rip_pages(self, start, end, filepath):
        last = end + 1
        pages = last - start

        # TODO: saving raw data is good enough?
        f = open(filepath, 'a+')
        for i, page in enumerate(xrange(start, last)):
            progress = float(i) / pages
            common.print_progress(progress)
            self.__set_page(page)
            f.write(self.__read_page())
        f.close()

        sys.stdout.write("\r")
        sys.stdout.flush()
        LOG.info("Firmware ripped successfully")
Esempio n. 4
0
    def erase_pages(self, start, end):
        total = end - start
        last = end + 1
        counter = 0
        for page in xrange(start, last):
            self.__set_page(page)
            self.__erase_page()
            if LOG.getEffectiveLevel() == logging.INFO:
                progress = float(counter) / total
                common.print_progress(progress)
            counter += 1
        if LOG.getEffectiveLevel() == logging.INFO:
            sys.stdout.write("\r")
            sys.stdout.flush()

        LOG.info("Firmware erased successfully")

        return True
Esempio n. 5
0
    def erase_pages(self, start, end):
        total = end - start
        last = end + 1
        counter = 0
        for page in xrange(start, last):
            self.__set_page(page)
            self.__erase_page()
            if LOG.getEffectiveLevel() == logging.INFO:
                progress = float(counter) / total
                common.print_progress(progress)
            counter += 1
        if LOG.getEffectiveLevel() == logging.INFO:
            sys.stdout.write("\r")
            sys.stdout.flush()

        LOG.info("Firmware erased successfully")

        return True
Esempio n. 6
0
def __get_consensus(ref, ref_name, align_file):
    print 'Generating pile-up...'
    index_allele_map = ['A', 'C', 'G', 'T']
    allele_index_map = {
        'A': 0,
        'C': 1,
        'G': 2,
        'T': 3,
    }
    NONE = 'None'
    # get consensus pile_up
    consensus_pile = np.zeros((len(ref), 4), dtype=np.uint32)
    with open(align_file, 'r') as alignment_file:
        num_lines = float(sum((1 for line in alignment_file if not '>' in line)))
    with open(align_file, 'r') as alignment_file:
        line_count = 1.0
        for line in alignment_file:
            if '>' in line:
                continue
            alignment1, alignment2 = line.strip().split(',')
            read1, position1 = alignment1.split(':')
            read2, position2 = alignment2.split(':')
            # if paired reads weren't mapped, continue
            if position1 == NONE or position2 == NONE:
                continue
            # convert positions to ints
            position1 = int(position1)
            position2 = int(position2)
            # if paired reads weren't mapped, continue
            if position1 == -1 or position2 == -1:
                continue
            # if spacing between reads out of whack
            # don't contribute to pile-up
            MAX_GAP = 50
            if position2 - position1 < 100 or position2 - position1 > 200:
                continue
            # contribute read1 to consensus pile
            for i in xrange(len(read1)):
                c = read1[i]
                consensus_pile[position1 + i][allele_index_map[c]] += 1
            # contribute read2 to consensus pile
            for i in xrange(len(read2)):
                c = read2[i]
                consensus_pile[position2 + i][allele_index_map[c]] += 1
            line_count += 1.0
            progress = line_count / num_lines
            cm.print_progress(progress)
    cm.print_progress(1)
    print '\tComplete'
    # get consensus string from pileup
    print 'Resolving consensus donor...'
    consensus_file_name = cm.CONS_DIR + cm.CONS_PRE + ref_name + '.txt'
    with open(consensus_file_name, 'w') as consensus_file:
        consensus_file.write('>' + ref_name + '\n')
        for i in xrange(len(consensus_pile)):
            if i > 0 and i % 80 == 0:
                consensus_file.write('\n')
            msum = sum(consensus_pile[i])
            max_pos = np.argmax(consensus_pile[i])
            mmax = consensus_pile[i][max_pos]
            # if it's a true max, write the consensus allele
            if msum > 0 and float(mmax) / float(msum) > 0.25:
                consensus_file.write(index_allele_map[max_pos])
            else:  # otherwise, stick with the ref allele (too much ambiguity)
                consensus_file.write(ref[i])
            cm.print_progress(float(i) / float(len(consensus_pile)))
    cm.print_progress(1)
    print '\tComplete'
    return consensus_file_name