Created
September 27, 2011 05:33
-
-
Save suapapa/1244403 to your computer and use it in GitHub Desktop.
dnw.py - python port of dnw client, smdk-usbdl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# -*- coding: utf-8 -*- | |
# dnw.py - python port of dnw client, smdk-usbdl | |
# | |
# Copyright (C) 2011 Homin Lee <[email protected]> | |
# | |
# 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. | |
# need python-usb | |
import usb | |
import sys | |
import os | |
def LOG(msg): | |
print "=>", msg | |
def LOGE(msg): | |
print >> sys.stderr, msg | |
def findDevice(idVendor, idProduct): | |
for bus in usb.busses(): | |
for device in bus.devices: | |
if len(device.configurations) == 1 and \ | |
device.idVendor == idVendor and \ | |
device.idProduct == idProduct: | |
LOG("found %04X:%04X"%(idVendor, idProduct)) | |
return device | |
return None | |
def sendData(device, epNum, data): | |
handle = device.open() | |
handle.claimInterface(0) | |
LOG("uploading %d bytes..."%len(data)) | |
wroteLen = handle.bulkWrite(epNum, data, 5*1000) | |
assert(wroteLen == len(data)) | |
def convertU32ToStr(val): | |
data ="" | |
data += chr(val & 0xff) | |
data += chr((val >> 8) & 0xff) | |
data += chr((val >> 16) & 0xff) | |
data += chr((val >> 32) & 0xff) | |
return data | |
def checkSum(data): | |
cksum = 0 | |
for byte in data: | |
cksum += ord(byte) | |
cksum &= 0xffff | |
LOG("data checksum %04x"%cksum) | |
cksumStr ="" | |
cksumStr += chr(cksum & 0xff) | |
cksumStr += chr((cksum >> 8) & 0xff) | |
return cksumStr | |
def makePacket(filename, dlAddr = 0x50000000): | |
body = open(filename).read() | |
LOG("loaded %d bytes from %s"%(len(body), filename)) | |
packet = "" | |
packet += convertU32ToStr(dlAddr) | |
packet += convertU32ToStr(len(body) + 10) | |
packet += body | |
packet += checkSum(body) | |
return packet | |
if __name__ == '__main__': | |
from optparse import OptionParser | |
optPsr = OptionParser("usage: %prog [-iXXXX:XXXX] your_binary.hex") | |
optPsr.add_option('-d', '--dev_id', type='string', | |
help="USB ID of target. in form of VID:PID") | |
optPsr.add_option('-a', '--dest_addr', type='string', | |
help="Destination address in memory in hex") | |
(opts, args) = optPsr.parse_args() | |
if (not len(args) == 1) or (not os.path.exists(args[0])): | |
LOGE("Not specify a file to upload or not exists"); | |
sys.exit(1) | |
# SMDK's default vid and pid | |
vid, pid = 0x04e8, 0x1234 | |
if opts.dev_id: | |
vid_pid = opts.dev_id.split(':') | |
vid, pid = map(lambda x: int(x, 16), vid_pid) | |
dnwDev = findDevice(vid, pid) | |
if not dnwDev: | |
LOGE("Can't find device, ID %04x:%04x"%(vid, pid)) | |
sys.exit(1) | |
destAddr = 0x50000000 | |
if opts.dest_addr: | |
destAddr = int(opts.dest_addr, 16) | |
packet = makePacket(args[0], destAddr) | |
sendData(dnwDev, 2, packet) | |
LOG("all done") | |
sys.exit(0) | |
# vim: et sw=4 fenc=utf-8: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Need -ubuntu- package,
python-usb
to run this.For now, it search
0x04e8:0x1234
in devices and upload packet via EP no.2 and it work fine in my case.cmdline options for tweak this default setting will be comming in near future.