freemyipod r236 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r235‎ | r236 | r237 >
Date:01:45, 31 October 2010
Author:farthen
Status:new
Tags:
Comment:
Fix emBIOS app running by checking the header of the app first.
Also improve i2cread, fix some bugs and clean up the code.
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -34,6 +34,7 @@
3535 from libembios import Error
3636 import libembiosdata
3737
 38+
3839 class NotImplementedError(Error):
3940 pass
4041
@@ -252,17 +253,6 @@
253254 return None
254255 else:
255256 raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
256 -
257 - @staticmethod
258 - def _strcheck(string, values):
259 - if string in values:
260 - return string
261 - else:
262 - expected = ""
263 - for item in values:
264 - expected += "'" + item + "', "
265 - expected = expected[:-2]
266 - raise ArgumentTypeError("one out of " + expected, "'" + string + "'")
267257
268258 @staticmethod
269259 def _hex(integer):
@@ -362,18 +352,18 @@
363353 raise ArgumentError("Specified integer too long")
364354 data = chr(integer)
365355 self.embios.write(addr, data)
366 - self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr))
 356+ self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr)+"\n")
367357
368358 @command
369359 def downloadint(self, addr):
370360 """
371361 Downloads a single integer from the device and prints it to the console window
372 - <offset>: the address to download the integer from
 362+ <addr>: the address to download the integer from
373363 """
374364 addr = self._hexint(addr)
375365 data = self.embios.read(addr, 1)
376366 integer = ord(data)
377 - self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr))
 367+ self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr)+"\n")
378368
379369 @command
380370 def i2cread(self, bus, slave, addr, size):
@@ -388,8 +378,11 @@
389379 slave = self._hexint(slave)
390380 addr = self._hexint(addr)
391381 size = self._hexint(size)
392 - for i in range(size):
393 - print("%02X: %02X" % (addr + i, struct.unpack("B", self.embios.i2cread(bus, slave, addr + i, 1))[0]))
 382+ data = self.embios.i2cread(bus, slave, addr, size)
 383+ bytes = struct.unpack("%dB" % len(data), data)
 384+ self.logger.info("Data read from I2C:\n")
 385+ for index, byte in enumerate(bytes):
 386+ self.logger.info("%02X: %02X\n" % (index, byte))
394387
395388 @command
396389 def i2cwrite(self, bus, slave, addr, *args):
@@ -399,7 +392,7 @@
400393 <slave> the slave address
401394 <addr> the start address on the I2C device
402395 <db1> ... <dbN> the data in single bytes, encoded in hex,
403 - seperated by whitespaces, eg. 37 56 45 12
 396+ seperated by whitespaces, eg. 37 5A 4F EB
404397 """
405398 bus = self._hexint(bus)
406399 slave = self._hexint(slave)
@@ -407,7 +400,9 @@
408401 data = ""
409402 for arg in args:
410403 data += chr(self._hexint(arg))
 404+ self.logger.info("Writing data to I2C...\n")
411405 self.embios.i2cwrite(bus, slave, addr, data)
 406+ self.logger.info("done\n")
412407
413408 @command
414409 def console(self):
@@ -428,7 +423,7 @@
429424 for word in args:
430425 text += word + " "
431426 text = text[:-1]
432 - self.logger.info("Writing '"+text+"' to the usb console\n")
 427+ self.logger.info("Writing '"+ text +"' to the usb console\n")
433428 self.embios.usbcwrite(text)
434429
435430 @command
@@ -464,7 +459,8 @@
465460 <bitmask>: the bitmask of the consoles to be flushed
466461 """
467462 bitmask = self._hexint(bitmask)
468 - raise NotImplementedError
 463+ self.logger.info("Flushing consoles identified with the bitmask "+self._hex(bitmask)+"\n")
 464+ self.embios.cflush(bitmask)
469465
470466 @command
471467 def getprocinfo(self):
@@ -514,7 +510,8 @@
515511 Suspends/resumes the thread with thread ID <threadid>
516512 """
517513 threadid = self._hexint(threadid)
518 - self.embios.resumethread(threadid)
 514+ self.logger.info("Suspending the thread with the threadid "+self._hex(threadid)+"\n")
 515+ self.embios.suspendthread(threadid)
519516
520517 @command
521518 def resumethread(self, threadid):
@@ -522,6 +519,7 @@
523520 Resumes the thread with thread ID <threadid>
524521 """
525522 threadid = self._hexint(threadid)
 523+ self.logger.info("Resuming the thread with the threadid "+self._hex(threadid)+"\n")
526524 self.embios.resumethread(threadid)
527525
528526 @command
@@ -530,6 +528,7 @@
531529 Kills the thread with thread ID <threadid>
532530 """
533531 threadid = self._hexint(threadid)
 532+ self.logger.info("Killing the thread with the threadid " + self._hex(threadid) + "\n")
534533 self.embios.killthread(threadid)
535534
536535 @command
@@ -549,16 +548,45 @@
550549 stackpointer = self._hexint(stackpointer)
551550 stacksize = self._hexint(stacksize)
552551 priority = self._hexint(priority)
553 - self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
 552+ data = self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
 553+ name = self.embios.readstring(nameptr)
 554+ self.logger.info("Created a thread with the threadid " + data.id + ", the name \"" + name + "\", the entrypoint at " + self._hex(entrypoint) + ", the stack at " + self._hex(stackpointer) + " with a size of " + self._hex(stacksize) + " and a priority of " + self._hex(priority) + "\n")
554555
555556 @command
556557 def run(self, filename):
557558 """
558559 Uploads the emBIOS application <filename> to
559 - the beginning of the user memory and executes it
 560+ the memory and executes it
560561 """
561 - #self.execimage(addr)
562 - raise NotImplementedError
 562+ try:
 563+ f = open(filename, 'rb')
 564+ except IOError:
 565+ raise ArgumentError("File not readable. Does it exist?")
 566+ try:
 567+ appheader = struct.unpack("<8sIIIIIIIIII", f.read(48))
 568+ header = appheader[0]
 569+ if header != "emBIexec":
 570+ raise ArgumentError("The specified file is not an emBIOS application")
 571+ baseaddr = appheader[2]
 572+ threadnameptr = appheader[8]
 573+ f.seek(threadnameptr - baseaddr)
 574+ name = ""
 575+ while True:
 576+ char = f.read(1)
 577+ if ord(char) == 0:
 578+ break
 579+ name += char
 580+ self.logger.info("Writing emBIOS application \"" + name + "\" to the memory at " + self._hex(baseaddr) + "...")
 581+ f.seek(0)
 582+ self.embios.write(baseaddr, f.read())
 583+ except IOError:
 584+ raise ArgumentError("The specified file is not an emBIOS application")
 585+ except struct.error:
 586+ raise ArgumentError("The specified file is not an emBIOS application")
 587+ finally:
 588+ f.close()
 589+ self.logger.info("done\n")
 590+ self.execimage(baseaddr)
563591
564592 @command
565593 def execimage(self, addr):
Index: embios/trunk/tools/libembios.py
@@ -219,12 +219,11 @@
220220
221221 def i2cread(self, index, slaveaddr, startaddr, size):
222222 """ Reads data from an i2c slave """
223 - if size > 256 or size < 1:
224 - raise ValueError("Size must be a number between 1 and 256")
225 - if size == 256:
226 - size = 0
227 - resp = self.lib.monitorcommand(struct.pack("IBBBBII", 8, index, slaveaddr, startaddr, size, 0, 0), "III%ds" % size, (None, None, None, "data"))
228 - return resp.data
 223+ data = ""
 224+ for i in range(size):
 225+ resp = self.lib.monitorcommand(struct.pack("IBBBBII", 8, index, slaveaddr, startaddr + i, 1, 0, 0), "III1s", (None, None, None, "data"))
 226+ data += resp.data
 227+ return data
229228
230229 def i2cwrite(self, index, slaveaddr, startaddr, data):
231230 """ Writes data to an i2c slave """