Subversion Repositories freemyipod

Rev

Rev 230 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 230 Rev 236
Line 32... Line 32...
32
 
32
 
33
import libembios
33
import libembios
34
from libembios import Error
34
from libembios import Error
35
import libembiosdata
35
import libembiosdata
36
 
36
 
-
 
37
 
37
class NotImplementedError(Error):
38
class NotImplementedError(Error):
38
    pass
39
    pass
39
 
40
 
40
class ArgumentError(Error):
41
class ArgumentError(Error):
41
    pass
42
    pass
Line 250... Line 251...
250
                raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
251
                raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
251
        elif type(something) == NoneType:
252
        elif type(something) == NoneType:
252
            return None
253
            return None
253
        else:
254
        else:
254
            raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
255
            raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
255
 
-
 
256
    @staticmethod
-
 
257
    def _strcheck(string, values):
-
 
258
        if string in values:
-
 
259
            return string
-
 
260
        else:
-
 
261
            expected = ""
-
 
262
            for item in values:
-
 
263
                expected += "'" + item + "', "
-
 
264
            expected = expected[:-2]
-
 
265
            raise ArgumentTypeError("one out of " + expected, "'" + string + "'")
-
 
266
    
256
    
267
    @staticmethod
257
    @staticmethod
268
    def _hex(integer):
258
    def _hex(integer):
269
        return "0x%x" % integer
259
        return "0x%x" % integer
270
    
260
    
Line 360... Line 350...
360
        integer = self._hexint(integer)
350
        integer = self._hexint(integer)
361
        if integer > 0xFFFFFFFF:
351
        if integer > 0xFFFFFFFF:
362
            raise ArgumentError("Specified integer too long")
352
            raise ArgumentError("Specified integer too long")
363
        data = chr(integer)
353
        data = chr(integer)
364
        self.embios.write(addr, data)
354
        self.embios.write(addr, data)
365
        self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr))
355
        self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr)+"\n")
366
 
356
 
367
    @command
357
    @command
368
    def downloadint(self, addr):
358
    def downloadint(self, addr):
369
        """
359
        """
370
            Downloads a single integer from the device and prints it to the console window
360
            Downloads a single integer from the device and prints it to the console window
371
            <offset>: the address to download the integer from
361
            <addr>: the address to download the integer from
372
        """
362
        """
373
        addr = self._hexint(addr)
363
        addr = self._hexint(addr)
374
        data = self.embios.read(addr, 1)
364
        data = self.embios.read(addr, 1)
375
        integer = ord(data)
365
        integer = ord(data)
376
        self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr))
366
        self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr)+"\n")
377
 
367
 
378
    @command
368
    @command
379
    def i2cread(self, bus, slave, addr, size):
369
    def i2cread(self, bus, slave, addr, size):
380
        """
370
        """
381
            Reads data from an I2C device
371
            Reads data from an I2C device
Line 386... Line 376...
386
        """
376
        """
387
        bus = self._hexint(bus)
377
        bus = self._hexint(bus)
388
        slave = self._hexint(slave)
378
        slave = self._hexint(slave)
389
        addr = self._hexint(addr)
379
        addr = self._hexint(addr)
390
        size = self._hexint(size)
380
        size = self._hexint(size)
-
 
381
        data = self.embios.i2cread(bus, slave, addr, size)
-
 
382
        bytes = struct.unpack("%dB" % len(data), data)
-
 
383
        self.logger.info("Data read from I2C:\n")
391
        for i in range(size):
384
        for index, byte in enumerate(bytes):
392
            print("%02X: %02X" % (addr + i, struct.unpack("B", self.embios.i2cread(bus, slave, addr + i, 1))[0]))
385
            self.logger.info("%02X: %02X\n" % (index, byte))
393
 
386
 
394
    @command
387
    @command
395
    def i2cwrite(self, bus, slave, addr, *args):
388
    def i2cwrite(self, bus, slave, addr, *args):
396
        """
389
        """
397
            Writes data to an I2C device
390
            Writes data to an I2C device
398
            <bus> the bus index
391
            <bus> the bus index
399
            <slave> the slave address
392
            <slave> the slave address
400
            <addr> the start address on the I2C device
393
            <addr> the start address on the I2C device
401
            <db1> ... <dbN> the data in single bytes, encoded in hex,
394
            <db1> ... <dbN> the data in single bytes, encoded in hex,
402
                seperated by whitespaces, eg. 37 56 45 12
395
                seperated by whitespaces, eg. 37 5A 4F EB
403
        """
396
        """
404
        bus = self._hexint(bus)
397
        bus = self._hexint(bus)
405
        slave = self._hexint(slave)
398
        slave = self._hexint(slave)
406
        addr = self._hexint(addr)
399
        addr = self._hexint(addr)
407
        data = ""
400
        data = ""
408
        for arg in args:
401
        for arg in args:
409
            data += chr(self._hexint(arg))
402
            data += chr(self._hexint(arg))
-
 
403
        self.logger.info("Writing data to I2C...\n")
410
        self.embios.i2cwrite(bus, slave, addr, data)
404
        self.embios.i2cwrite(bus, slave, addr, data)
-
 
405
        self.logger.info("done\n")
411
 
406
 
412
    @command
407
    @command
413
    def console(self):
408
    def console(self):
414
        """
409
        """
415
            Reads data from the USB console continuously
410
            Reads data from the USB console continuously
Line 426... Line 421...
426
        """
421
        """
427
        text = ""
422
        text = ""
428
        for word in args:
423
        for word in args:
429
            text += word + " "
424
            text += word + " "
430
        text = text[:-1]
425
        text = text[:-1]
431
        self.logger.info("Writing '"+text+"' to the usb console\n")
426
        self.logger.info("Writing '"+ text +"' to the usb console\n")
432
        self.embios.usbcwrite(text)
427
        self.embios.usbcwrite(text)
433
 
428
 
434
    @command
429
    @command
435
    def readdevconsole(self, bitmask):
430
    def readdevconsole(self, bitmask):
436
        """
431
        """
Line 462... Line 457...
462
        """
457
        """
463
            flushes one or more of the device consoles' buffers.
458
            flushes one or more of the device consoles' buffers.
464
            <bitmask>: the bitmask of the consoles to be flushed
459
            <bitmask>: the bitmask of the consoles to be flushed
465
        """
460
        """
466
        bitmask = self._hexint(bitmask)
461
        bitmask = self._hexint(bitmask)
-
 
462
        self.logger.info("Flushing consoles identified with the bitmask "+self._hex(bitmask)+"\n")
467
        raise NotImplementedError
463
        self.embios.cflush(bitmask)
468
 
464
 
469
    @command
465
    @command
470
    def getprocinfo(self):
466
    def getprocinfo(self):
471
        """
467
        """
472
            Fetches data on the currently running processes
468
            Fetches data on the currently running processes
Line 512... Line 508...
512
    def suspendthread(self, threadid):
508
    def suspendthread(self, threadid):
513
        """
509
        """
514
            Suspends/resumes the thread with thread ID <threadid>
510
            Suspends/resumes the thread with thread ID <threadid>
515
        """
511
        """
516
        threadid = self._hexint(threadid)
512
        threadid = self._hexint(threadid)
-
 
513
        self.logger.info("Suspending the thread with the threadid "+self._hex(threadid)+"\n")
517
        self.embios.resumethread(threadid)
514
        self.embios.suspendthread(threadid)
518
 
515
 
519
    @command
516
    @command
520
    def resumethread(self, threadid):
517
    def resumethread(self, threadid):
521
        """
518
        """
522
            Resumes the thread with thread ID <threadid>
519
            Resumes the thread with thread ID <threadid>
523
        """
520
        """
524
        threadid = self._hexint(threadid)
521
        threadid = self._hexint(threadid)
-
 
522
        self.logger.info("Resuming the thread with the threadid "+self._hex(threadid)+"\n")
525
        self.embios.resumethread(threadid)
523
        self.embios.resumethread(threadid)
526
 
524
 
527
    @command
525
    @command
528
    def killthread(self, threadid):
526
    def killthread(self, threadid):
529
        """
527
        """
530
            Kills the thread with thread ID <threadid>
528
            Kills the thread with thread ID <threadid>
531
        """
529
        """
532
        threadid = self._hexint(threadid)
530
        threadid = self._hexint(threadid)
-
 
531
        self.logger.info("Killing the thread with the threadid " + self._hex(threadid) + "\n")
533
        self.embios.killthread(threadid)
532
        self.embios.killthread(threadid)
534
 
533
 
535
    @command
534
    @command
536
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
535
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
537
        """
536
        """
Line 547... Line 546...
547
        nameptr = self._hexint(nameptr)
546
        nameptr = self._hexint(nameptr)
548
        entrypoint = self._hexint(entrypoint)
547
        entrypoint = self._hexint(entrypoint)
549
        stackpointer = self._hexint(stackpointer)
548
        stackpointer = self._hexint(stackpointer)
550
        stacksize = self._hexint(stacksize)
549
        stacksize = self._hexint(stacksize)
551
        priority = self._hexint(priority)
550
        priority = self._hexint(priority)
552
        self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
551
        data = self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
-
 
552
        name = self.embios.readstring(nameptr)
-
 
553
        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")
553
    
554
    
554
    @command
555
    @command
555
    def run(self, filename):
556
    def run(self, filename):
556
        """
557
        """
557
            Uploads the emBIOS application <filename> to
558
            Uploads the emBIOS application <filename> to
558
            the beginning of the user memory and executes it
559
            the memory and executes it
559
        """
560
        """
-
 
561
        try:
-
 
562
            f = open(filename, 'rb')
-
 
563
        except IOError:
-
 
564
            raise ArgumentError("File not readable. Does it exist?")
-
 
565
        try:
-
 
566
            appheader = struct.unpack("<8sIIIIIIIIII", f.read(48))
-
 
567
            header = appheader[0]
-
 
568
            if header != "emBIexec":
-
 
569
                raise ArgumentError("The specified file is not an emBIOS application")
-
 
570
            baseaddr = appheader[2]
-
 
571
            threadnameptr = appheader[8]
-
 
572
            f.seek(threadnameptr - baseaddr)
-
 
573
            name = ""
-
 
574
            while True:
-
 
575
                char = f.read(1)
-
 
576
                if ord(char) == 0:
-
 
577
                    break
-
 
578
                name += char
-
 
579
            self.logger.info("Writing emBIOS application \"" + name + "\" to the memory at " + self._hex(baseaddr) + "...")
-
 
580
            f.seek(0)
-
 
581
            self.embios.write(baseaddr, f.read())
-
 
582
        except IOError:
-
 
583
            raise ArgumentError("The specified file is not an emBIOS application")
-
 
584
        except struct.error:
-
 
585
            raise ArgumentError("The specified file is not an emBIOS application")
-
 
586
        finally:
-
 
587
            f.close()
560
        #self.execimage(addr)
588
        self.logger.info("done\n")
561
        raise NotImplementedError
589
        self.execimage(baseaddr)
562
 
590
 
563
    @command
591
    @command
564
    def execimage(self, addr):
592
    def execimage(self, addr):
565
        """
593
        """
566
            Executes the emBIOS application at <addr>.
594
            Executes the emBIOS application at <addr>.