Subversion Repositories freemyipod

Rev

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

Rev 172 Rev 173
Line 178... Line 178...
178
        try:
178
        try:
179
            self.embios = libembios.Embios()
179
            self.embios = libembios.Embios()
180
        except libembios.DeviceNotFoundError:
180
        except libembios.DeviceNotFoundError:
181
            self.logger.error("No emBIOS device found!")
181
            self.logger.error("No emBIOS device found!")
182
            end(1)
182
            end(1)
-
 
183
        try:
183
        self.getinfo("version")
184
            self.getinfo("version")
-
 
185
        except libembios.DeviceNotFoundError:
-
 
186
                self.logger.error("Device not found!")
-
 
187
                exit(2)
184
        
188
        
185
    def _parsecommand(self, func, args):
189
    def _parsecommand(self, func, args):
186
        # adds self to the commandline args.
190
        # adds self to the commandline args.
187
        # this is needed because the functions need access to their class.
191
        # this is needed because the functions need access to their class.
188
        args.insert(0, self)
192
        args.insert(0, self)
Line 195... Line 199...
195
                usage("Syntax Error in function '" + func + "'")
199
                usage("Syntax Error in function '" + func + "'")
196
            except ArgumentTypeError, e:
200
            except ArgumentTypeError, e:
197
                usage(e)
201
                usage(e)
198
            except NotImplementedError:
202
            except NotImplementedError:
199
                self.logger.error("This function is not implemented yet!")
203
                self.logger.error("This function is not implemented yet!")
200
            except libembios.DeviceNotFoundError:
-
 
201
                self.logger.error("Device not found!")
-
 
202
            except libembios.DeviceError, e:
204
            except libembios.DeviceError, e:
203
                self.logger.error(str(e))
205
                self.logger.error(str(e))
204
            except TypeError, e:
206
            except TypeError, e:
205
                if str(e).split(" ", 1)[0] == func + "()":
207
                if str(e).split(" ", 1)[0] == func + "()":
206
                    self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func))
208
                    self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func))
Line 255... Line 257...
255
            for item in values:
257
            for item in values:
256
                expected += "'" + item + "', "
258
                expected += "'" + item + "', "
257
            expected = expected[:-2]
259
            expected = expected[:-2]
258
            raise ArgumentTypeError("one out of " + expected, "'" + string + "'")
260
            raise ArgumentTypeError("one out of " + expected, "'" + string + "'")
259
    
261
    
-
 
262
    @staticmethod
-
 
263
    def _hex(integer):
-
 
264
        return "0x%x" % integer
260
    
265
    
261
    @command
266
    @command
262
    def getinfo(self, infotype):
267
    def getinfo(self, infotype):
263
        """
268
        """
264
            Get info on the running emBIOS.
269
            Get info on the running emBIOS.
Line 271... Line 276...
271
        elif infotype == "packetsize":
276
        elif infotype == "packetsize":
272
            resp = self.embios.getpacketsizeinfo()
277
            resp = self.embios.getpacketsizeinfo()
273
            self.logger.info("Maximum packet sizes: "+str(resp))
278
            self.logger.info("Maximum packet sizes: "+str(resp))
274
        elif infotype == "usermemrange":
279
        elif infotype == "usermemrange":
275
            resp = self.embios.getusermemrange()
280
            resp = self.embios.getusermemrange()
276
            self.logger.info("The user memory range is "+hex(resp.lower)+" - "+hex(resp.upper-1))
281
            self.logger.info("The user memory range is "+self._hex(resp.lower)+" - "+self._hex(resp.upper-1))
277
        else:
282
        else:
278
            raise ArgumentTypeError("one out of 'version', 'packetsize', 'usermemrange'", infotype)
283
            raise ArgumentTypeError("one out of 'version', 'packetsize', 'usermemrange'", infotype)
279
    
284
    
280
    @command
285
    @command
281
    def reset(self, force=False):
286
    def reset(self, force=False):
Line 311... Line 316...
311
        addr = self._hexint(addr)
316
        addr = self._hexint(addr)
312
        try:
317
        try:
313
            f = open(filename, 'rb')
318
            f = open(filename, 'rb')
314
        except IOError:
319
        except IOError:
315
            raise ArgumentError("File not readable. Does it exist?")
320
            raise ArgumentError("File not readable. Does it exist?")
316
        self.logger.info("Writing file '"+filename+"' to memory at "+hex(addr)+"...")
321
        self.logger.info("Writing file '"+filename+"' to memory at "+self._hex(addr)+"...")
317
        with f:
322
        with f:
318
            self.embios.write(addr, f.read())
323
            self.embios.write(addr, f.read())
319
        self.logger.info("done\n")
324
        self.logger.info("done\n")
320
        
325
        
321
        
326
        
Line 332... Line 337...
332
        size = self._hexint(size)
337
        size = self._hexint(size)
333
        try:
338
        try:
334
            f = open(filename, 'wb')
339
            f = open(filename, 'wb')
335
        except IOError:
340
        except IOError:
336
            raise ArgumentError("Can not open file for write!")
341
            raise ArgumentError("Can not open file for write!")
337
        self.logger.info("Reading data from address "+hex(addr)+" with the size "+hex(size)+" to '"+filename+"'...")
342
        self.logger.info("Reading data from address "+self._hex(addr)+" with the size "+self._hex(size)+" to '"+filename+"'...")
338
        with f:
343
        with f:
339
            f.write(self.embios.read(addr, size))
344
            f.write(self.embios.read(addr, size))
340
        self.logger.info("done\n")
345
        self.logger.info("done\n")
341
 
346
 
342
    @command
347
    @command
Line 350... Line 355...
350
        integer = self._hexint(integer)
355
        integer = self._hexint(integer)
351
        if integer > 0xFFFFFFFF:
356
        if integer > 0xFFFFFFFF:
352
            raise ArgumentError("Specified integer too long")
357
            raise ArgumentError("Specified integer too long")
353
        data = chr(integer)
358
        data = chr(integer)
354
        self.embios.writemem(addr, data)
359
        self.embios.writemem(addr, data)
355
        self.logger.info("Integer '"+hex(integer)+"' written successfully to "+hex(addr))
360
        self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr))
356
 
361
 
357
    @command
362
    @command
358
    def downloadint(self, addr):
363
    def downloadint(self, addr):
359
        """
364
        """
360
            Downloads a single integer from the device and prints it to the console window
365
            Downloads a single integer from the device and prints it to the console window
361
            <offset>: the address to download the integer from
366
            <offset>: the address to download the integer from
362
        """
367
        """
363
        addr = self._hexint(addr)
368
        addr = self._hexint(addr)
364
        data = self.embios.readmem(addr, 1)
369
        data = self.embios.readmem(addr, 1)
365
        integer = ord(data)
370
        integer = ord(data)
366
        self.logger.info("Integer '"+hex(integer)+"' read from address "+hex(addr))
371
        self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr))
367
 
372
 
368
    @command
373
    @command
369
    def i2crecv(self, bus, slave, addr, size):
374
    def i2crecv(self, bus, slave, addr, size):
370
        """
375
        """
371
            Reads data from an I2C device
376
            Reads data from an I2C device
Line 418... Line 423...
418
    def writeusbconsole_file(self, file, offset=0, length=None):
423
    def writeusbconsole_file(self, file, offset=0, length=None):
419
        """
424
        """
420
            Writes the file <file> to the USB console.
425
            Writes the file <file> to the USB console.
421
            Optional params <offset> <length>: specify the range in <file> to write
426
            Optional params <offset> <length>: specify the range in <file> to write
422
        """
427
        """
423
        # We don't care about file here, this is done when opening it
428
        
424
        offset = self._hexint(offset)
429
        offset = self._hexint(offset)
425
        length = self._hexint(length)
430
        length = self._hexint(length)
426
        raise NotImplementedError
431
        raise NotImplementedError
427
 
432
 
428
    @command
433
    @command
Line 455... Line 460...
455
        """
460
        """
456
            Writes the file <file> to the device consoles specified by <bitmask>
461
            Writes the file <file> to the device consoles specified by <bitmask>
457
            Optional params <offset> <length>: specify the range in <file> to write
462
            Optional params <offset> <length>: specify the range in <file> to write
458
        """
463
        """
459
        bitmask = self._hexint(bitmask)
464
        bitmask = self._hexint(bitmask)
460
        # We don't care about file here, this is done when opening it
465
        
461
        offset = self._hexint(offset)
466
        offset = self._hexint(offset)
462
        length = self._hexint(length)
467
        length = self._hexint(length)
463
        raise NotImplementedError
468
        raise NotImplementedError
464
 
469
 
465
    @command
470
    @command
Line 485... Line 490...
485
 
490
 
486
    @command
491
    @command
487
    def getprocinfo(self):
492
    def getprocinfo(self):
488
        """
493
        """
489
            Fetches data on the currently running processes
494
            Fetches data on the currently running processes
490
            ATTENTION: this function will be print the information to the console window.
-
 
491
                If several threads are running this might overflow the window,
-
 
492
                causing not everything to be shown.
-
 
493
        """
495
        """
-
 
496
        import datetime
-
 
497
        threads = self.embios.getprocinfo()
-
 
498
        self.logger.info("The device has "+str(len(threads))+" running threads:\n\n")
-
 
499
        for thread in threads:
-
 
500
            self.logger.info("  "+thread.name+":\n")
-
 
501
            self.logger.info("    Thread id: "+str(thread.id)+"\n")
-
 
502
            self.logger.info("    Thread type: "+thread.type+"\n")
-
 
503
            self.logger.info("    Thread state: "+thread.state+"\n")
-
 
504
            self.logger.info("    Priority: "+str(thread.priority)+"/256\n")
-
 
505
            self.logger.info("    CPU time (total): "+str(datetime.timedelta(microseconds=thread.cputime_total))+"\n")
-
 
506
            self.logger.info("    Stack address: "+self._hex(thread.stackaddr)+"\n")
-
 
507
            self.logger.info("    Registers:\n")
-
 
508
            for register in range(16):
-
 
509
                self.logger.info("      r"+str(register)+": "+self._hex(thread.regs["r"+str(register)])+"\n")
-
 
510
            self.logger.info("      cpsr: "+self._hex(thread.regs.cpsr))
494
        raise NotImplementedError
511
            self.logger.info("\n")
495
 
512
    
496
    @command
513
    @command
497
    def lockscheduler(self):
514
    def lockscheduler(self):
498
        """
515
        """
499
            Locks (freezes) the scheduler
516
            Locks (freezes) the scheduler
500
        """
517
        """
501
        raise NotImplementedError
518
        self.embios.lockscheduler()
502
 
519
 
503
    @command
520
    @command
504
    def unlockscheduler(self):
521
    def unlockscheduler(self):
505
        """
522
        """
506
            Unlocks (unfreezes) the scheduler
523
            Unlocks (unfreezes) the scheduler
507
        """
524
        """
508
        raise NotImplementedError
525
        self.embios.unlockscheduler()
509
 
526
 
510
    @command
527
    @command
511
    def suspendthread(self, threadid):
528
    def suspendthread(self, threadid):
512
        """
529
        """
513
            Suspends/resumes the thread with thread ID <threadid>
530
            Suspends/resumes the thread with thread ID <threadid>
514
        """
531
        """
515
        threadid = self._hexint(threadid)
532
        threadid = self._hexint(threadid)
516
        raise NotImplementedError
533
        self.embios.resumethread(threadid)
517
 
534
 
518
    @command
535
    @command
519
    def resumethread(self, threadid):
536
    def resumethread(self, threadid):
520
        """
537
        """
521
            Resumes the thread with thread ID <threadid>
538
            Resumes the thread with thread ID <threadid>
522
        """
539
        """
523
        threadid = self._hexint(threadid)
540
        threadid = self._hexint(threadid)
524
        raise NotImplementedError
541
        self.embios.resumethread(threadid)
525
 
542
 
526
    @command
543
    @command
527
    def killthread(self, threadid):
544
    def killthread(self, threadid):
528
        """
545
        """
529
            Kills the thread with thread ID <threadid>
546
            Kills the thread with thread ID <threadid>
530
        """
547
        """
531
        threadid = self._hexint(threadid)
548
        threadid = self._hexint(threadid)
532
        raise NotImplementedError
549
        self.embios.killthread(threadid)
533
 
550
 
534
    @command
551
    @command
535
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
552
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
536
        """
553
        """
537
            Creates a new thread and returns its thread ID
554
            Creates a new thread and returns its thread ID
Line 551... Line 568...
551
        self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
568
        self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
552
    
569
    
553
    @command
570
    @command
554
    def run(self, filename):
571
    def run(self, filename):
555
        """
572
        """
556
            Uploads the emBIOS application to an address in the user memory
573
            Uploads the emBIOS application <filename> to
557
            and executes it
574
            the beginning of the user memory and executes it
558
        """
575
        """
559
        try:
576
        try:
560
            f = open(filename, "rb")
577
            f = open(filename, "rb")
561
        except IOError:
578
        except IOError:
562
            raise ArgumentError("File not readable. Does it exist?")
579
            raise ArgumentError("File not readable. Does it exist?")
Line 564... Line 581...
564
        addr = data.lower
581
        addr = data.lower
565
        maxsize = data.upper - data.lower
582
        maxsize = data.upper - data.lower
566
        filesize = os.path.getsize(filename)
583
        filesize = os.path.getsize(filename)
567
        if filesize > maxsize:
584
        if filesize > maxsize:
568
            raise ArgumentError("The file is too big, it doesn't fit into the user memory.")
585
            raise ArgumentError("The file is too big, it doesn't fit into the user memory.")
569
        self.logger.info("Uploading application to "+hex(addr)+" - "+hex(addr+filesize)+"\n")
586
        self.logger.info("Uploading application to "+self._hex(addr)+" - "+self._hex(addr+filesize)+"\n")
570
        self.embios.write(addr, f.read())
587
        self.embios.write(addr, f.read())
571
        self.execute(addr)
588
        self.execimage(addr)
572
 
589
 
573
    @command
590
    @command
574
    def execute(self, addr):
591
    def execimage(self, addr):
575
        """
592
        """
576
            Executes the emBIOS application at <address>.
593
            Executes the emBIOS application at <addr>.
577
        """
594
        """
578
        addr = self._hexint(addr)
595
        addr = self._hexint(addr)
579
        self.logger.info("Starting emBIOS app at "+hex(addr)+"\n")
596
        self.logger.info("Starting emBIOS app at "+self._hex(addr)+"\n")
580
        self.embios.execimage(addr)
597
        self.embios.execimage(addr)
581
 
598
 
582
    @command
599
    @command
583
    def readrawbootflash(self, addr_flash, addr_mem, size):
600
    def readrawbootflash(self, addr_flash, addr_mem, size):
584
        """
601
        """
Line 641... Line 658...
641
        """
658
        """
642
        addr = self._hexint(addr)
659
        addr = self._hexint(addr)
643
        size = self._hexint(size)
660
        size = self._hexint(size)
644
        destination = self._hexint(destination)
661
        destination = self._hexint(destination)
645
        sha1size = 0x14
662
        sha1size = 0x14
646
        self.logger.info("Generating hmac-sha1 hash from the buffer at "+hex(addr)+" with the size "+hex(size)+
663
        self.logger.info("Generating hmac-sha1 hash from the buffer at "+self._hex(addr)+" with the size "+self._hex(size)+
647
                         " and saving it to "+hex(destination)+" - "+hex(destination+sha1size)+"...")
664
                         " and saving it to "+self._hex(destination)+" - "+self._hex(destination+sha1size)+"...")
648
        self.embios.hmac_sha1(addr, size, destination)
665
        self.embios.hmac_sha1(addr, size, destination)
649
        self.logger.info("done\n")
666
        self.logger.info("done\n")
650
        data = self.embios.readmem(destination, sha1size)
667
        data = self.embios.readmem(destination, sha1size)
651
        hash = ord(data)
668
        hash = ord(data)
652
        self.logger.info("The generated hash is "+hex(hash))
669
        self.logger.info("The generated hash is "+self._hex(hash))
653
 
670
 
654
if __name__ == "__main__":
671
if __name__ == "__main__":
655
    if len(sys.argv) < 2:
672
    if len(sys.argv) < 2:
656
        usage("No command specified")
673
        usage("No command specified")
657
    interface = Commandline()
674
    interface = Commandline()