Subversion Repositories freemyipod

Rev

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

Rev 171 Rev 172
Line 142... Line 142...
142
def command(func):
142
def command(func):
143
    """
143
    """
144
        Decorator for all commands.
144
        Decorator for all commands.
145
        The decorated function is called with (self, all, other, arguments, ...)
145
        The decorated function is called with (self, all, other, arguments, ...)
146
    """
146
    """
147
    def decorator(args):
147
    def decorator(*args):
148
        return func(args[0], *args[1:])
148
        return func(args[0], *args[1:])
149
    func._command = True
149
    func._command = True
150
    decorator.func = func
150
    decorator.func = func
151
    return decorator
151
    return decorator
152
 
152
 
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
        self.getinfo("version")
183
        
184
        
184
    def _parsecommand(self, func, args):
185
    def _parsecommand(self, func, args):
185
        # adds self to the commandline args.
186
        # adds self to the commandline args.
186
        # this is needed because the functions need access to their class.
187
        # this is needed because the functions need access to their class.
187
        args.insert(0, self)
188
        args.insert(0, self)
188
        if func in self.cmddict:
189
        if func in self.cmddict:
189
            try:
190
            try:
190
                self.cmddict[func](args)
191
                self.cmddict[func](*args)
191
            except ArgumentError, e:
192
            except ArgumentError, e:
192
                usage(e)
193
                usage(e)
193
            except ArgumentError:
194
            except ArgumentError:
194
                usage("Syntax Error in function '" + func + "'")
195
                usage("Syntax Error in function '" + func + "'")
195
            except ArgumentTypeError, e:
196
            except ArgumentTypeError, e:
196
                usage(e)
197
                usage(e)
197
            except NotImplementedError:
198
            except NotImplementedError:
198
                self.logger.error("This function is not implemented yet!")
199
                self.logger.error("This function is not implemented yet!")
-
 
200
            except libembios.DeviceNotFoundError:
-
 
201
                self.logger.error("Device not found!")
199
            except libembios.DeviceError, e:
202
            except libembios.DeviceError, e:
200
                self.logger.error(str(e))
203
                self.logger.error(str(e))
201
            except TypeError, e:
204
            except TypeError, e:
202
                if str(e).split(" ", 1)[0] == func + "()":
205
                if str(e).split(" ", 1)[0] == func + "()":
203
                    self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func))
206
                    self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func))
Line 261... Line 264...
261
            Get info on the running emBIOS.
264
            Get info on the running emBIOS.
262
            <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
265
            <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
263
        """
266
        """
264
        if infotype == "version":
267
        if infotype == "version":
265
            resp = self.embios.getversioninfo()
268
            resp = self.embios.getversioninfo()
266
            self.logger.info(libembiosdata.swtypes[resp.swtypeid] + " v" + str(resp.majorv) + "." + str(resp.minorv) +
269
            self.logger.info("Connected to "+libembiosdata.swtypes[resp.swtypeid] + " v" + str(resp.majorv) + "." + str(resp.minorv) +
267
                             "." + str(resp.patchv) + " r" + str(resp.revision) + " running on " + libembiosdata.hwtypes[resp.hwtypeid] + "\n")
270
                             "." + str(resp.patchv) + " r" + str(resp.revision) + " running on " + libembiosdata.hwtypes[resp.hwtypeid] + "\n")
268
        elif infotype == "packetsize":
271
        elif infotype == "packetsize":
269
            resp = self.embios.getpacketsizeinfo()
272
            resp = self.embios.getpacketsizeinfo()
270
            self.logger.info("Maximum packet sizes: "+str(resp))
273
            self.logger.info("Maximum packet sizes: "+str(resp))
271
        elif infotype == "usermemrange":
274
        elif infotype == "usermemrange":
Line 544... Line 547...
544
        entrypoint = self._hexint(entrypoint)
547
        entrypoint = self._hexint(entrypoint)
545
        stackpointer = self._hexint(stackpointer)
548
        stackpointer = self._hexint(stackpointer)
546
        stacksize = self._hexint(stacksize)
549
        stacksize = self._hexint(stacksize)
547
        priority = self._hexint(priority)
550
        priority = self._hexint(priority)
548
        self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
551
        self.embios.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
-
 
552
    
-
 
553
    @command
-
 
554
    def run(self, filename):
-
 
555
        """
-
 
556
            Uploads the emBIOS application to an address in the user memory
-
 
557
            and executes it
-
 
558
        """
-
 
559
        try:
-
 
560
            f = open(filename, "rb")
-
 
561
        except IOError:
-
 
562
            raise ArgumentError("File not readable. Does it exist?")
-
 
563
        data = self.embios.getusermemrange()
-
 
564
        addr = data.lower
-
 
565
        maxsize = data.upper - data.lower
-
 
566
        filesize = os.path.getsize(filename)
-
 
567
        if filesize > maxsize:
-
 
568
            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")
-
 
570
        self.embios.write(addr, f.read())
-
 
571
        self.execute(addr)
549
 
572
 
550
    @command
573
    @command
551
    def run(self, address):
574
    def execute(self, addr):
552
        """
575
        """
553
            Executes the emBIOS application at <address>.
576
            Executes the emBIOS application at <address>.
554
        """
577
        """
555
        address = self._hexint(address)
578
        addr = self._hexint(addr)
-
 
579
        self.logger.info("Starting emBIOS app at "+hex(addr)+"\n")
556
        raise NotImplementedError
580
        self.embios.execimage(addr)
557
 
581
 
558
    @command
582
    @command
559
    def readrawbootflash(self, addr_flash, addr_mem, size):
583
    def readrawbootflash(self, addr_flash, addr_mem, size):
560
        """
584
        """
561
            Reads <size> bytes from bootflash to memory.
585
            Reads <size> bytes from bootflash to memory.
Line 584... Line 608...
584
    @command
608
    @command
585
    def flushcaches(self):
609
    def flushcaches(self):
586
        """
610
        """
587
            Flushes the CPUs data and instruction caches.
611
            Flushes the CPUs data and instruction caches.
588
        """
612
        """
-
 
613
        self.logger.info("Flushing CPU data and instruction caches...")
589
        raise NotImplementedError
614
        self.embios.flushcaches()
-
 
615
        self.logger.info("done\n")
590
    
616
    
591
    @command
617
    @command
592
    def aesencrypt(self, addr, size, keyindex):
618
    def aesencrypt(self, addr, size, keyindex):
593
        """
619
        """
594
            Encrypt a buffer using a hardware key
620
            Encrypts a buffer using a hardware key
595
        """
621
        """
596
        addr = self._hexint(addr)
622
        addr = self._hexint(addr)
597
        size = self._hexint(size)
623
        size = self._hexint(size)
598
        keyindex = self._hexint(keyindex)
624
        keyindex = self._hexint(keyindex)
599
        self.embios.aesencrypt(addr, size, keyindex)
625
        self.embios.aesencrypt(addr, size, keyindex)
600
    
626
    
601
    @command
627
    @command
602
    def aesdecrypt(self, addr, size, keyindex):
628
    def aesdecrypt(self, addr, size, keyindex):
603
        """
629
        """
604
            Decrypt a buffer using a hardware key
630
            Decrypts a buffer using a hardware key
605
        """
631
        """
606
        addr = self._hexint(addr)
632
        addr = self._hexint(addr)
607
        size = self._hexint(size)
633
        size = self._hexint(size)
608
        keyindex = self._hexint(keyindex)
634
        keyindex = self._hexint(keyindex)
609
        self.embios.aesdecrypt(addr, size, keyindex)
635
        self.embios.aesdecrypt(addr, size, keyindex)
-
 
636
    
-
 
637
    @command
-
 
638
    def hmac_sha1(self, addr, size, destination):
-
 
639
        """
-
 
640
            Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination'
-
 
641
        """
-
 
642
        addr = self._hexint(addr)
-
 
643
        size = self._hexint(size)
-
 
644
        destination = self._hexint(destination)
-
 
645
        sha1size = 0x14
-
 
646
        self.logger.info("Generating hmac-sha1 hash from the buffer at "+hex(addr)+" with the size "+hex(size)+
-
 
647
                         " and saving it to "+hex(destination)+" - "+hex(destination+sha1size)+"...")
-
 
648
        self.embios.hmac_sha1(addr, size, destination)
-
 
649
        self.logger.info("done\n")
-
 
650
        data = self.embios.readmem(destination, sha1size)
-
 
651
        hash = ord(data)
-
 
652
        self.logger.info("The generated hash is "+hex(hash))
610
 
653
 
611
if __name__ == "__main__":
654
if __name__ == "__main__":
612
    if len(sys.argv) < 2:
655
    if len(sys.argv) < 2:
613
        usage("No command specified")
656
        usage("No command specified")
614
    interface = Commandline()
657
    interface = Commandline()