Subversion Repositories freemyipod

Rev

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

Rev 506 Rev 516
Line 49... Line 49...
49
    def __init__(self, expected, seen=False):
49
    def __init__(self, expected, seen=False):
50
        self.expected = expected
50
        self.expected = expected
51
        self.seen = seen
51
        self.seen = seen
52
    def __str__(self):
52
    def __str__(self):
53
        if self.seen:
53
        if self.seen:
54
            return "Expected " + str(self.expected) + " but saw " + str(self.seen)
54
            return "Expected %s but got %s" % (self.expected, self.seen)
55
        else:
55
        else:
56
            return "Expected " + str(self.expected) + ", but saw something else"
56
            return "Expected %s, but saw something else" % self.expected
57
 
57
 
58
 
58
 
59
def usage(errormsg=None, specific=False, docstring=True):
59
def usage(errormsg=None, specific=False, docstring=True):
60
    """
60
    """
61
        Prints the usage information.
61
        Prints the usage information.
Line 153... Line 153...
153
            except libemcore.DeviceError, e:
153
            except libemcore.DeviceError, e:
154
                self.logger.error(str(e) + "\n")
154
                self.logger.error(str(e) + "\n")
155
            except TypeError, e:
155
            except TypeError, e:
156
                # Only act on TypeErrors for the function we called, not on TypeErrors raised by another function.
156
                # Only act on TypeErrors for the function we called, not on TypeErrors raised by another function.
157
                if str(e).split(" ", 1)[0] == func + "()":
157
                if str(e).split(" ", 1)[0] == func + "()":
158
                    self.logger.error(usage("Argument Error in '" + func + "': Wrong argument count", specific=func) + "\n")
158
                    self.logger.error(usage("Argument Error in '%s': Wrong argument count" % func, specific=func))
159
                else:
159
                else:
160
                    raise
160
                    raise
161
            except libemcore.usb.core.USBError:
161
            except libemcore.usb.core.USBError:
162
                self.logger.error("There is a problem with the USB connection.\n")
162
                self.logger.error("There is a problem with the USB connection.\n")
163
        else:
163
        else:
Line 179... Line 179...
179
            falselist = ['false', '0', 'f', 'n', 'no']
179
            falselist = ['false', '0', 'f', 'n', 'no']
180
            if something.lower() in truelist:
180
            if something.lower() in truelist:
181
                return True
181
                return True
182
            elif something.lower() in falselist:
182
            elif something.lower() in falselist:
183
                return False
183
                return False
184
        raise ArgumentTypeError("bool", "'"+str(something)+"'")
184
        raise ArgumentTypeError("bool", "'%s'" % something)
185
 
185
 
186
    @staticmethod
186
    @staticmethod
187
    def _hexint(something):
187
    def _hexint(something):
188
        """
188
        """
189
            Converts quite everything to a hexadecimal represented integer.
189
            Converts quite everything to a hexadecimal represented integer.
Line 194... Line 194...
194
            return something
194
            return something
195
        elif type(something) == str:
195
        elif type(something) == str:
196
            try:
196
            try:
197
                return int(something, 16)
197
                return int(something, 16)
198
            except ValueError:
198
            except ValueError:
199
                raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
199
                raise ArgumentTypeError("hexadecimal coded integer", "'%s'" % something)
200
        elif something is None:
200
        elif something is None:
201
            return None
201
            return None
202
        else:
202
        else:
203
            raise ArgumentTypeError("hexadecimal coded integer", "'"+str(something)+"'")
203
            raise ArgumentTypeError("hexadecimal coded integer", "'%s'" % something)
204
    
-
 
205
    @staticmethod
-
 
206
    def _hex(integer):
-
 
207
        return "0x%x" % integer
-
 
208
    
204
    
209
    @command
205
    @command
210
    def help(self):
206
    def help(self):
211
        """ Displays this help """
207
        """ Displays this help """
212
        usage(docstring = True)
208
        usage(docstring = True)
Line 217... Line 213...
217
            Get info on the running emCORE.
213
            Get info on the running emCORE.
218
            <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
214
            <infotype> may be either of 'version', 'packetsize', 'usermemrange'.
219
        """
215
        """
220
        if infotype == "version":
216
        if infotype == "version":
221
            hwtype = gethwname(self.emcore.lib.dev.hwtypeid)
217
            hwtype = gethwname(self.emcore.lib.dev.hwtypeid)
222
            self.logger.info("Connected to " + \
218
            self.logger.info("Connected to %s v%d.%d.%d r%d running on %s\n" % (
223
                             libemcoredata.swtypes[self.emcore.lib.dev.swtypeid] + \
219
                             libemcoredata.swtypes[self.emcore.lib.dev.swtypeid],
224
                             " v" + str(self.emcore.lib.dev.version.majorv) + \
220
                             self.emcore.lib.dev.version.majorv,
225
                             "." + str(self.emcore.lib.dev.version.minorv) + \
221
                             self.emcore.lib.dev.version.minorv,
226
                             "." + str(self.emcore.lib.dev.version.patchv) + \
222
                             self.emcore.lib.dev.version.patchv,
227
                             " r" + str(self.emcore.lib.dev.version.revision) + \
223
                             self.emcore.lib.dev.version.revision,
228
                             " running on " + hwtype + "\n")
224
                             hwtype))
229
        
225
        
230
        elif infotype == "packetsize":
226
        elif infotype == "packetsize":
-
 
227
            self.logger.info("Maximum packet sizes:\n")
231
            self.logger.info("Maximum packet sizes: \n command out: " + str(self.emcore.lib.dev.packetsizelimit.cout) + \
228
            self.logger.info("command out: %d\n" % self.emcore.lib.dev.packetsizelimit.cout, 4)
232
                             "\n command in: " + str(self.emcore.lib.dev.packetsizelimit.cin) + \
229
            self.logger.info("command in: %d\n" % self.emcore.lib.dev.packetsizelimit.cin, 4)
233
                             "\n data in: " + str(self.emcore.lib.dev.packetsizelimit.din) + \
230
            self.logger.info("data out: %d\n" % self.emcore.lib.dev.packetsizelimit.dout, 4)
234
                             "\n data out: " + str(self.emcore.lib.dev.packetsizelimit.dout)+ "\n")
231
            self.logger.info("data in: %d\n" % self.emcore.lib.dev.packetsizelimit.din, 4)
235
        
232
        
236
        elif infotype == "usermemrange":
233
        elif infotype == "usermemrange":
237
            resp = self.emcore.getusermemrange()
234
            resp = self.emcore.getusermemrange()
238
            self.logger.info("The user memory range is " + \
235
            self.logger.info("The user memory range is 0x%X - 0x%X" % (
239
                             self._hex(self.emcore.lib.dev.usermem.lower) + \
236
                             self.emcore.lib.dev.usermem.lower,
240
                             " - " + \
-
 
241
                             self._hex(self.emcore.lib.dev.usermem.upper - 1) + "\n")
237
                             self.emcore.lib.dev.usermem.upper - 1))
242
        
238
        
243
        else:
239
        else:
244
            raise ArgumentTypeError("one out of 'version', 'packetsize', 'usermemrange'", infotype)
240
            raise ArgumentTypeError("one out of 'version', 'packetsize', 'usermemrange'", infotype)
245
    
241
    
246
    @command
242
    @command
Line 278... Line 274...
278
        try:
274
        try:
279
            f = open(filename, 'rb')
275
            f = open(filename, 'rb')
280
        except IOError:
276
        except IOError:
281
            raise ArgumentError("File not readable. Does it exist?")
277
            raise ArgumentError("File not readable. Does it exist?")
282
        if addr is not None:
278
        if addr is not None:
283
            self.logger.info("Writing file '" + filename + \
279
            self.logger.info("Writing file '%s' to memory at 0x%X...\n" % (filename, addr))
284
                            "' to memory at " + self._hex(addr) + "...\n")
-
 
285
        else:
280
        else:
286
            self.logger.info("Writing file '" + filename + " to an allocated memory region...\n")
281
            self.logger.info("Writing file '%s' to an allocated memory region...\n" % filename)
287
        with f:
282
        with f:
288
            if addr is not None:
283
            if addr is not None:
289
                self.emcore.write(addr, f.read())
284
                self.emcore.write(addr, f.read())
290
            else:
285
            else:
291
                addr = self.emcore.upload(f.read())
286
                addr = self.emcore.upload(f.read())
292
            size = f.tell()
287
            size = f.tell()
293
        f.close()
288
        f.close()
294
        self.logger.info("Done uploading " + str(size) + " bytes to 0x" + self._hex(addr) + "\n")
289
        self.logger.info("Done uploading %d bytes to 0x%X\n" % (size, addr))
295
        return addr, size
290
        return addr, size
296
    
291
    
297
    @command
292
    @command
298
    def downloadfile(self, addr, size, filename):
293
    def downloadfile(self, addr, size, filename):
299
        """
294
        """
Line 306... Line 301...
306
        size = self._hexint(size)
301
        size = self._hexint(size)
307
        try:
302
        try:
308
            f = open(filename, 'wb')
303
            f = open(filename, 'wb')
309
        except IOError:
304
        except IOError:
310
            raise ArgumentError("Can not open file for write!")
305
            raise ArgumentError("Can not open file for write!")
311
        self.logger.info("Reading data from address " + self._hex(addr) + \
306
        self.logger.info("Reading data from address 0x%X with the size 0x%X to '%s'..." %
312
                         " with the size " + self._hex(size) + \
-
 
313
                         " to '"+filename+"'...")
307
                        (addr, size, filename))
314
        with f:
308
        with f:
315
            f.write(self.emcore.read(addr, size))
309
            f.write(self.emcore.read(addr, size))
316
        f.close()
310
        f.close()
317
        self.logger.info("done\n")
311
        self.logger.info("done\n")
318
 
312
 
Line 327... Line 321...
327
        integer = self._hexint(integer)
321
        integer = self._hexint(integer)
328
        if integer > 0xFFFFFFFF:
322
        if integer > 0xFFFFFFFF:
329
            raise ArgumentError("Specified integer too long")
323
            raise ArgumentError("Specified integer too long")
330
        data = struct.pack("I", integer)
324
        data = struct.pack("I", integer)
331
        self.emcore.write(addr, data)
325
        self.emcore.write(addr, data)
332
        self.logger.info("Integer '" + self._hex(integer) + \
326
        self.logger.info("Integer '0x%X' written successfully to 0x%X\n" % (integer, addr))
333
                         "' written successfully to " + self._hex(addr) + "\n")
-
 
334
 
327
 
335
    @command
328
    @command
336
    def downloadint(self, addr):
329
    def downloadint(self, addr):
337
        """
330
        """
338
            Downloads a single integer from the device and prints it to the console window
331
            Downloads a single integer from the device and prints it to the console window
339
            <addr>: the address to download the integer from
332
            <addr>: the address to download the integer from
340
        """
333
        """
341
        addr = self._hexint(addr)
334
        addr = self._hexint(addr)
342
        data = self.emcore.read(addr, 4)
335
        data = self.emcore.read(addr, 4)
343
        integer = struct.unpack("I", data)[0]
336
        integer = struct.unpack("I", data)[0]
344
        self.logger.info("Integer '" + self._hex(integer) + \
337
        self.logger.info("Read '0x%X' from address 0x%X\n" % (integer, addr))
345
                         "' read from address " + self._hex(addr) + "\n")
-
 
346
 
338
 
347
    @command
339
    @command
348
    def i2cread(self, bus, slave, addr, size):
340
    def i2cread(self, bus, slave, addr, size):
349
        """
341
        """
350
            Reads data from an I2C device
342
            Reads data from an I2C device
Line 400... Line 392...
400
        """
392
        """
401
        text = ""
393
        text = ""
402
        for word in args:
394
        for word in args:
403
            text += word + " "
395
            text += word + " "
404
        text = text[:-1]
396
        text = text[:-1]
405
        self.logger.info("Writing '"+ text +"' to the usb console\n")
397
        self.logger.info("Writing '%s' to the usb console\n" % text)
406
        self.emcore.usbcwrite(text)
398
        self.emcore.usbcwrite(text)
407
 
399
 
408
    @command
400
    @command
409
    def readdevconsole(self, bitmask):
401
    def readdevconsole(self, bitmask):
410
        """
402
        """
Line 426... Line 418...
426
        bitmask = self._hexint(bitmask)
418
        bitmask = self._hexint(bitmask)
427
        text = ""
419
        text = ""
428
        for word in args:
420
        for word in args:
429
            text += word + " "
421
            text += word + " "
430
        text = text[:-1]
422
        text = text[:-1]
431
        self.logger.info("Writing '" + text + \
-
 
432
                         "' to the device consoles identified with " + self._hex(bitmask) + "\n")
423
        self.logger.info("Writing '%s' to the device consoles identified with 0x%X\n" % (text, bitmask))
433
        self.emcore.cwrite(text, bitmask)
424
        self.emcore.cwrite(text, bitmask)
434
 
425
 
435
    @command
426
    @command
436
    def flushconsolebuffers(self, bitmask):
427
    def flushconsolebuffers(self, bitmask):
437
        """
428
        """
438
            flushes one or more of the device consoles' buffers.
429
            flushes one or more of the device consoles' buffers.
439
            <bitmask>: the bitmask of the consoles to be flushed
430
            <bitmask>: the bitmask of the consoles to be flushed
440
        """
431
        """
441
        bitmask = self._hexint(bitmask)
432
        bitmask = self._hexint(bitmask)
442
        self.logger.info("Flushing consoles identified with the bitmask " + \
433
        self.logger.info("Flushing consoles identified with the bitmask 0x%X\n" % bitmask)
443
                         self._hex(bitmask) + "\n")
-
 
444
        self.emcore.cflush(bitmask)
434
        self.emcore.cflush(bitmask)
445
 
435
 
446
    @command
436
    @command
447
    def getprocinfo(self):
437
    def getprocinfo(self):
448
        """
438
        """
Line 461... Line 451...
461
        cpuload = threadload + coreload
451
        cpuload = threadload + coreload
462
        self.logger.info("Threads: %d, CPU load: %.1f%%, kernel load: %.1f%%, user load: %.1f%%\n\n"
452
        self.logger.info("Threads: %d, CPU load: %.1f%%, kernel load: %.1f%%, user load: %.1f%%\n\n"
463
                         % (len(threads), cpuload * 100, coreload * 100, threadload * 100))
453
                         % (len(threads), cpuload * 100, coreload * 100, threadload * 100))
464
        self.logger.info("Thread dump:\n")
454
        self.logger.info("Thread dump:\n")
465
        for thread in threads:
455
        for thread in threads:
466
            self.logger.info(thread.name+":\n", 2)
456
            self.logger.info("%s:\n" % thread.name, 2)
467
            self.logger.info("Threadstruct address: " + self._hex(thread.addr)+"\n", 4)
457
            self.logger.info("Threadstruct address: 0x%X\n" % thread.addr, 4)
468
            self.logger.info("Thread type: "    + str(thread.thread_type)+"\n", 4)
458
            self.logger.info("Thread type: %s\n" % thread.thread_type, 4)
469
            self.logger.info("Thread state: "   + str(thread.state)+"\n", 4)
459
            self.logger.info("Thread state: %s\n" % thread.state, 4)
470
            self.logger.info("Block type: "     + str(thread.block_type)+"\n", 4)
460
            self.logger.info("Block type: %s\n" % thread.block_type, 4)
471
            self.logger.info("Blocked by: "     + self._hex(thread.blocked_by)+"\n", 4)
461
            self.logger.info("Blocked by: 0x%X\n" % thread.blocked_by, 4)
472
            self.logger.info("Priority: "       + str(thread.priority)+"/255\n", 4)
462
            self.logger.info("Priority: %d/255\n" % thread.priority, 4)
473
            self.logger.info("Current CPU load: %.1f%%\n" % ((thread.cpuload * 100) / 255.), 4)
463
            self.logger.info("Current CPU load: %.1f%%\n" % ((thread.cpuload * 100) / 255.), 4)
474
            self.logger.info("CPU time (total): "+str(datetime.timedelta(microseconds = thread.cputime_total))+"\n", 4)
464
            self.logger.info("CPU time (total): %s\n" % datetime.timedelta(microseconds = thread.cputime_total), 4)
475
            self.logger.info("Stack address: "  + self._hex(thread.stack)+"\n", 4)
465
            self.logger.info("Stack address: 0x%X\n" % thread.stack, 4)
476
            self.logger.info("Registers:\n", 4)
466
            self.logger.info("Registers:\n", 4)
477
            for registerrange in range(4):
467
            for registerrange in range(4):
478
                self.logger.info("      ")
468
                self.logger.info("      ")
479
                for register in range(registerrange, 16, 4):
469
                for register in range(registerrange, 16, 4):
480
                    registerrepr = "r"+str(register)
470
                    registerrepr = "r"+str(register)
Line 498... Line 488...
498
        """
488
        """
499
        self.logger.info("Will now unlock scheduler\n")
489
        self.logger.info("Will now unlock scheduler\n")
500
        self.emcore.unlockscheduler()
490
        self.emcore.unlockscheduler()
501
    
491
    
502
    @command
492
    @command
503
    def suspendthread(self, threadid):
493
    def suspendthread(self, threadaddr):
504
        """
494
        """
505
            Suspends/resumes the thread with thread ID <threadid>
495
            Suspends the thread with the thread address <threadaddr>
506
        """
496
        """
507
        threadid = self._hexint(threadid)
497
        threadaddr = self._hexint(threadaddr)
508
        self.logger.info("Suspending the thread with the threadid "+self._hex(threadid)+"\n")
498
        self.logger.info("Suspending the thread with the threadaddr 0x%X\n" % threadaddr)
509
        self.emcore.suspendthread(threadid)
499
        self.emcore.suspendthread(threadaddr)
510
 
500
 
511
    @command
501
    @command
512
    def resumethread(self, threadid):
502
    def resumethread(self, threadaddr):
513
        """
503
        """
514
            Resumes the thread with thread ID <threadid>
504
            Resumes the thread with the thread address <threadaddr>
515
        """
505
        """
516
        threadid = self._hexint(threadid)
506
        threadaddr = self._hexint(threadaddr)
517
        self.logger.info("Resuming the thread with the threadid "+self._hex(threadid)+"\n")
507
        self.logger.info("Resuming the thread with the threadaddr 0x%X\n" % threadaddr)
518
        self.emcore.resumethread(threadid)
508
        self.emcore.resumethread(threadaddr)
519
 
509
 
520
    @command
510
    @command
521
    def killthread(self, threadid):
511
    def killthread(self, threadaddr):
522
        """
512
        """
523
            Kills the thread with thread ID <threadid>
513
            Kills the thread with the thread address <threadaddr>
524
        """
514
        """
525
        threadid = self._hexint(threadid)
515
        threadaddr = self._hexint(threadaddr)
526
        self.logger.info("Killing the thread with the threadid " + self._hex(threadid) + "\n")
516
        self.logger.info("Killing the thread with the threadaddr 0x%X\n" % threadaddr)
527
        self.emcore.killthread(threadid)
517
        self.emcore.killthread(threadaddr)
528
 
518
 
529
    @command
519
    @command
530
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
520
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
531
        """
521
        """
532
            Creates a new thread and returns its thread ID
522
            Creates a new thread and returns its thread pointer
533
            <namepointer>: a pointer to the thread's name
523
            <namepointer>: a pointer to the thread's name
534
            <entrypoint>: a pointer to the entrypoint of the thread
524
            <entrypoint>: a pointer to the entrypoint of the thread
535
            <stackpointer>: a pointer to the stack of the thread
525
            <stackpointer>: a pointer to the stack of the thread
536
            <stacksize>: the size of the thread's stack
526
            <stacksize>: the size of the thread's stack
537
            <type>: the thread type, vaild are: 0 => user thread, 1 => system thread
527
            <threadtype>: the thread type, vaild are: 0 => user thread, 1 => system thread
538
            <priority>: the priority of the thread, from 1 to 255
528
            <priority>: the priority of the thread, from 1 to 255
539
            <state>: the thread's initial state, valid are: 1 => ready, 0 => suspended
529
            <state>: the thread's initial state, valid are: 1 => ready, 0 => suspended
540
        """
530
        """
541
        nameptr = self._hexint(nameptr)
531
        nameptr = self._hexint(nameptr)
542
        entrypoint = self._hexint(entrypoint)
532
        entrypoint = self._hexint(entrypoint)
543
        stackpointer = self._hexint(stackpointer)
533
        stackptr = self._hexint(stackptr)
544
        stacksize = self._hexint(stacksize)
534
        stacksize = self._hexint(stacksize)
545
        priority = self._hexint(priority)
535
        priority = self._hexint(priority)
546
        data = self.emcore.createthread(nameptr, entrypoint, stackptr, stacksize, type, priority, state)
536
        data = self.emcore.createthread(nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state)
547
        name = self.emcore.readstring(nameptr)
537
        name = self.emcore.readstring(nameptr)
548
        self.logger.info("Created a thread with the threadid " + data.id + \
538
        self.logger.info("Created a thread with the thread pointer 0x%X, the name \"%s\", the entrypoint at 0x%X," \
549
                         ", the name \"" + name + "\"" + \
-
 
550
                         ", the entrypoint at " + self._hex(entrypoint) + \
-
 
551
                         ", the stack at " + self._hex(stackpointer) + \
539
                         "the stack at 0x%X with a size of 0x%X and a priority of %d/255\n" %
552
                         " with a size of " + self._hex(stacksize) + \
540
                        (data.threadptr, name, entrypoint, stackptr, stacksize, priority))
553
                         " and a priority of " + self._hex(priority) + "\n")
-
 
554
    
541
    
555
    @command
542
    @command
556
    def run(self, filename):
543
    def run(self, filename):
557
        """
544
        """
558
            Uploads the emCORE application <filename> to
545
            Uploads the emCORE application <filename> to
Line 562... Line 549...
562
            f = open(filename, 'rb')
549
            f = open(filename, 'rb')
563
        except IOError:
550
        except IOError:
564
            raise ArgumentError("File not readable. Does it exist?")
551
            raise ArgumentError("File not readable. Does it exist?")
565
        with f:
552
        with f:
566
            data = self.emcore.run(f.read())
553
            data = self.emcore.run(f.read())
567
        self.logger.info("Executed emCORE application as thread " + self._hex(data.thread) + "\n")
554
        self.logger.info("Executed emCORE application as thread 0x%X\n" % data.thread)
568
 
555
 
569
    @command
556
    @command
570
    def execimage(self, addr):
557
    def execimage(self, addr):
571
        """
558
        """
572
            Executes the emCORE application at <addr>.
559
            Executes the emCORE application at <addr>.
573
        """
560
        """
574
        addr = self._hexint(addr)
561
        addr = self._hexint(addr)
575
        self.logger.info("Starting emCORE app at "+self._hex(addr)+"\n")
562
        self.logger.info("Starting emCORE app at 0x%X\n" % addr)
576
        self.emcore.execimage(addr)
563
        self.emcore.execimage(addr)
577
    
564
    
578
    @command
565
    @command
579
    def flushcaches(self):
566
    def flushcaches(self):
580
        """
567
        """
Line 592... Line 579...
592
            <addr_mem>: the address in memory to copy the data to
579
            <addr_mem>: the address in memory to copy the data to
593
        """
580
        """
594
        addr_flash = self._hexint(addr_flash)
581
        addr_flash = self._hexint(addr_flash)
595
        addr_mem = self._hexint(addr_mem)
582
        addr_mem = self._hexint(addr_mem)
596
        size = self._hexint(size)
583
        size = self._hexint(size)
597
        self.logger.info("Dumping boot flash addresses "+self._hex(addr_flash)+" - "+
584
        self.logger.info("Dumping boot flash from 0x%X - 0x%X to 0x%X - 0x%X\n" %
598
                         hex(addr_flash+size)+" to "+self._hex(addr_mem)+" - "+self._hex(addr_mem+size)+"\n")
585
                        (addr_flash, addr_flash + size, addr_mem, addr_mem + size))
599
        self.emcore.bootflashread(addr_mem, addr_flash, size)
586
        self.emcore.bootflashread(addr_mem, addr_flash, size)
600
    
587
    
601
    @command
588
    @command
602
    def writebootflash(self, addr_flash, addr_mem, size, force=False):
589
    def writebootflash(self, addr_flash, addr_mem, size, force=False):
603
        """
590
        """
Line 610... Line 597...
610
        """
597
        """
611
        addr_flash = self._hexint(addr_flash)
598
        addr_flash = self._hexint(addr_flash)
612
        addr_mem = self._hexint(addr_mem)
599
        addr_mem = self._hexint(addr_mem)
613
        size = self._hexint(size)
600
        size = self._hexint(size)
614
        force = self._bool(force)
601
        force = self._bool(force)
615
        self.logger.warn("Writing boot flash from the memory in "+self._hex(addr_mem)+" - "+
602
        self.logger.warn("Writing boot flash from the memory in 0x%X - 0x%X to 0x%X - 0x%X\n" %
616
                         hex(addr_mem+size)+" to "+self._hex(addr_flash)+" - "+self._hex(addr_flash+size)+"\n")
603
                        (addr_mem, addr_mem + size, addr_flash, addr_flash + size))
617
        if force == False:
604
        if force == False:
618
            self.logger.warn("If this was not what you intended press Ctrl-C NOW")
605
            self.logger.warn("If this was not what you intended press Ctrl-C NOW")
619
            for i in range(10):
606
            for i in range(10):
620
                self.logger.info(".")
607
                self.logger.info(".")
621
                time.sleep(1)
608
                time.sleep(1)
Line 635... Line 622...
635
    @command
622
    @command
636
    def execfirmware(self, targetaddr, addr, size):
623
    def execfirmware(self, targetaddr, addr, size):
637
        """
624
        """
638
            Moves the firmware at <addr> with <size> to <targetaddr> and executes it
625
            Moves the firmware at <addr> with <size> to <targetaddr> and executes it
639
        """
626
        """
-
 
627
        targetaddr = self._hexint(targetaddr)
640
        addr = self._hexint(addr)
628
        addr = self._hexint(addr)
641
        self.logger.info("Running firmware at "+self._hex(targetaddr)+". Bye.\n")
629
        self.logger.info("Running firmware at 0x%X. Bye.\n" % targetaddr)
642
        self.emcore.execfirmware(targetaddr, addr, size)
630
        self.emcore.execfirmware(targetaddr, addr, size)
643
    
631
    
644
    @command
632
    @command
645
    def aesencrypt(self, addr, size, keyindex):
633
    def aesencrypt(self, addr, size, keyindex):
646
        """
634
        """
Line 677... Line 665...
677
        """
665
        """
678
        addr = self._hexint(addr)
666
        addr = self._hexint(addr)
679
        size = self._hexint(size)
667
        size = self._hexint(size)
680
        destination = self._hexint(destination)
668
        destination = self._hexint(destination)
681
        sha1size = 0x14
669
        sha1size = 0x14
682
        self.logger.info("Generating hmac-sha1 hash from the buffer at " + self._hex(addr) + \
670
        self.logger.info("Generating hmac-sha1 hash from the buffer at 0x%X with the size 0x%X and saving it to 0x%X - 0x%X\n" %
683
                         " with the size " + self._hex(size) + " and saving it to " + \
-
 
684
                         self._hex(destination) + " - " + self._hex(destination+sha1size) + "...")
671
                        (addr, size, destination, destination + sha1size))
685
        self.emcore.hmac_sha1(addr, size, destination)
672
        self.emcore.hmac_sha1(addr, size, destination)
686
        self.logger.info("done\n")
673
        self.logger.info("done\n")
687
        data = self.emcore.read(destination, sha1size)
674
        data = self.emcore.read(destination, sha1size)
688
        hash = ord(data)
675
        hash = ord(data)
689
        self.logger.info("The generated hash is "+self._hex(hash)+"\n")
676
        self.logger.info("The generated hash is 0x%X\n" % hash)
690
 
677
 
691
    @command
678
    @command
692
    def ipodnano2g_getnandinfo(self):
679
    def ipodnano2g_getnandinfo(self):
693
        """
680
        """
694
            Target-specific function: ipodnano2g
681
            Target-specific function: ipodnano2g
695
            Gathers some information about the NAND chip used
682
            Gathers some information about the NAND chip used
696
        """
683
        """
697
        data = self.emcore.ipodnano2g_getnandinfo()
684
        data = self.emcore.ipodnano2g_getnandinfo()
698
        self.logger.info("NAND chip type: "         + self._hex(data["type"])+"\n")
685
        self.logger.info("NAND chip type: 0x%X\n" % data["type"])
699
        self.logger.info("Number of banks: "        + str(data["banks"])+"\n")
686
        self.logger.info("Number of banks: %d\n" % data["banks"])
700
        self.logger.info("Number of blocks: "       + str(data["blocks"])+"\n")
687
        self.logger.info("Number of blocks: %d\n" % data["blocks"])
701
        self.logger.info("Number of user blocks: "  + str(data["userblocks"])+"\n")
688
        self.logger.info("Number of user blocks: %d\n" % data["userblocks"])
702
        self.logger.info("Pages per block: "        + str(data["pagesperblock"])+"\n")
689
        self.logger.info("Pages per block: %d\n" % data["pagesperblock"])
703
 
690
 
704
    @command
691
    @command
705
    def ipodnano2g_nandread(self, addr, start, count, doecc=True, checkempty=True):
692
    def ipodnano2g_nandread(self, addr, start, count, doecc=True, checkempty=True):
706
        """
693
        """
707
            Target-specific function: ipodnano2g
694
            Target-specific function: ipodnano2g
Line 715... Line 702...
715
        addr = self._hexint(addr)
702
        addr = self._hexint(addr)
716
        start = self._hexint(start)
703
        start = self._hexint(start)
717
        count = self._hexint(count)
704
        count = self._hexint(count)
718
        doecc = self._bool(doecc)
705
        doecc = self._bool(doecc)
719
        checkempty = self._bool(checkempty)
706
        checkempty = self._bool(checkempty)
720
        self.logger.info("Reading " + self._hex(count) + " NAND pages starting at " + \
707
        self.logger.info("Reading 0x%X NAND pages starting at 0x%X to memory at 0x%X..." %
721
                         self._hex(start) + " to " + self._hex(addr) + "...")
708
                        (count, start, addr))
722
        self.emcore.ipodnano2g_nandread(addr, start, count, doecc, checkempty)
709
        self.emcore.ipodnano2g_nandread(addr, start, count, doecc, checkempty)
723
        self.logger.info("done\n")
710
        self.logger.info("done\n")
724
 
711
 
725
    @command
712
    @command
726
    def ipodnano2g_nandwrite(self, addr, start, count, doecc=True):
713
    def ipodnano2g_nandwrite(self, addr, start, count, doecc=True):
Line 734... Line 721...
734
        """
721
        """
735
        addr = self._hexint(addr)
722
        addr = self._hexint(addr)
736
        start = self._hexint(start)
723
        start = self._hexint(start)
737
        count = self._hexint(count)
724
        count = self._hexint(count)
738
        doecc = self._bool(doecc)
725
        doecc = self._bool(doecc)
739
        self.logger.info("Writing " + self._hex(count) + " NAND pages starting at " + \
726
        self.logger.info("Writing 0x%X NAND pages starting at 0x%X from memory at 0x%X..." %
740
                         self._hex(start) + " from " + self._hex(addr) + "...")
727
                        (count, start, addr))
741
        self.emcore.ipodnano2g_nandwrite(addr, start, count, doecc)
728
        self.emcore.ipodnano2g_nandwrite(addr, start, count, doecc)
742
        self.logger.info("done\n")
729
        self.logger.info("done\n")
743
 
730
 
744
    @command
731
    @command
745
    def ipodnano2g_nanderase(self, addr, start, count):
732
    def ipodnano2g_nanderase(self, addr, start, count):
Line 751... Line 738...
751
            <count>: block count
738
            <count>: block count
752
        """
739
        """
753
        addr = self._hexint(addr)
740
        addr = self._hexint(addr)
754
        start = self._hexint(start)
741
        start = self._hexint(start)
755
        count = self._hexint(count)
742
        count = self._hexint(count)
756
        self.logger.info("Erasing " + self._hex(count) + " NAND blocks starting at " + \
743
        self.logger.info("Erasing 0x%X NAND pages starting at 0x%X and logging to 0x%X..." %
757
                         self._hex(start) + " and logging to " + self._hex(addr) + "...")
744
                        (count, start, addr))
758
        self.emcore.ipodnano2g_nanderase(addr, start, count)
745
        self.emcore.ipodnano2g_nanderase(addr, start, count)
759
        self.logger.info("done\n")
746
        self.logger.info("done\n")
760
 
747
 
761
    @command
748
    @command
762
    def ipodnano2g_dumpnand(self, filenameprefix):
749
    def ipodnano2g_dumpnand(self, filenameprefix):
Line 772... Line 759...
772
            datafile = open(filenameprefix+"_data.bin", 'wb')
759
            datafile = open(filenameprefix+"_data.bin", 'wb')
773
            sparefile = open(filenameprefix+"_spare.bin", 'wb')
760
            sparefile = open(filenameprefix+"_spare.bin", 'wb')
774
            statusfile = open(filenameprefix+"_status.bin", 'wb')
761
            statusfile = open(filenameprefix+"_status.bin", 'wb')
775
        except IOError:
762
        except IOError:
776
            raise ArgumentError("Can not open file for writing!")
763
            raise ArgumentError("Can not open file for writing!")
777
        infofile.write("NAND chip type: "       + self._hex(info["type"]) + "\r\n")
764
        infofile.write("NAND chip type: 0x%X\r\n" % info["type"])
778
        infofile.write("Number of banks: "      + str(info["banks"]) + "\r\n")
765
        infofile.write("Number of banks: %d\r\n" % info["banks"])
779
        infofile.write("Number of blocks: "     + str(info["blocks"]) + "\r\n")
766
        infofile.write("Number of blocks: %d\r\n" % info["blocks"])
780
        infofile.write("Number of user blocks: "+ str(info["userblocks"]) + "\r\n")
767
        infofile.write("Number of user blocks: %d\r\n" % info["userblocks"])
781
        infofile.write("Pages per block: "      + str(info["pagesperblock"]) + "\r\n")
768
        infofile.write("Pages per block: %d\r\n" % info["pagesperblock"])
782
        for i in range(info["banks"] * info["blocks"] * info["pagesperblock"] / 8192):
769
        for i in range(info["banks"] * info["blocks"] * info["pagesperblock"] / 8192):
783
            self.logger.info(".")
770
            self.logger.info(".")
784
            self.emcore.ipodnano2g_nandread(0x08000000, i * 8192, 8192, 1, 1)
771
            self.emcore.ipodnano2g_nandread(0x08000000, i * 8192, 8192, 1, 1)
785
            datafile.write(self.emcore.read(0x08000000, 0x01000000))
772
            datafile.write(self.emcore.read(0x08000000, 0x01000000))
786
            sparefile.write(self.emcore.read(0x09000000, 0x00080000))
773
            sparefile.write(self.emcore.read(0x09000000, 0x00080000))
Line 842... Line 829...
842
            Gathers some information about a storage volume used
829
            Gathers some information about a storage volume used
843
            <volume>: volume id
830
            <volume>: volume id
844
        """
831
        """
845
        volume = self._hexint(volume)
832
        volume = self._hexint(volume)
846
        data = self.emcore.storage_get_info(volume)
833
        data = self.emcore.storage_get_info(volume)
847
        self.logger.info("Sector size: "+str(data["sectorsize"])+"\n")
834
        self.logger.info("Sector size: %d\n" % data["sectorsize"])
848
        self.logger.info("Number of sectors: "+str(data["numsectors"])+"\n")
835
        self.logger.info("Number of sectors: %d\n" % data["numsectors"])
849
        self.logger.info("Vendor: "+data["vendor"]+"\n")
836
        self.logger.info("Vendor: %s\n" % data["vendor"])
850
        self.logger.info("Product: "+data["product"]+"\n")
837
        self.logger.info("Product: %s\n" % data["product"])
851
        self.logger.info("Revision: "+data["revision"]+"\n")
838
        self.logger.info("Revision: %s\n" % data["revision"])
852
 
839
 
853
    @command
840
    @command
854
    def readrawstorage(self, volume, sector, count, addr):
841
    def readrawstorage(self, volume, sector, count, addr):
855
        """
842
        """
856
            Reads <count> sectors starting at <sector> from storage <volume> to memory at <addr>.
843
            Reads <count> sectors starting at <sector> from storage <volume> to memory at <addr>.
Line 959... Line 946...
959
    @command
946
    @command
960
    def mkdir(self, dirname):
947
    def mkdir(self, dirname):
961
        """
948
        """
962
            Creates a directory with the name <dirname>
949
            Creates a directory with the name <dirname>
963
        """
950
        """
964
        self.logger.info("Creating directory " + dirname + "...")
951
        self.logger.info("Creating directory %s..." % dirname)
965
        self.emcore.dir_create(dirname)
952
        self.emcore.dir_create(dirname)
966
        self.logger.info(" done\n")
953
        self.logger.info(" done\n")
967
 
954
 
968
    @command
955
    @command
969
    def rmdir(self, dirname):
956
    def rmdir(self, dirname):
970
        """
957
        """
971
            Removes an empty directory with the name <dirname>
958
            Removes an empty directory with the name <dirname>
972
        """
959
        """
973
        self.logger.info("Removing directory " + dirname + "...")
960
        self.logger.info("Removing directory %s..." % dirname)
974
        self.emcore.dir_remove(dirname)
961
        self.emcore.dir_remove(dirname)
975
        self.logger.info(" done\n")
962
        self.logger.info(" done\n")
976
 
963
 
977
    @command
964
    @command
978
    def rm(self, filename):
965
    def rm(self, filename):
979
        """
966
        """
980
            Removes a file with the name <filename>
967
            Removes a file with the name <filename>
981
        """
968
        """
982
        self.logger.info("Removing file " + filename + "...")
969
        self.logger.info("Removing file %s..." % filename)
983
        self.emcore.file_unlink(filename)
970
        self.emcore.file_unlink(filename)
984
        self.logger.info(" done\n")
971
        self.logger.info(" done\n")
985
 
972
 
986
    @command
973
    @command
987
    def rmtree(self, path):
974
    def rmtree(self, path):
Line 1004... Line 991...
1004
    @command
991
    @command
1005
    def mv(self, oldname, newname):
992
    def mv(self, oldname, newname):
1006
        """
993
        """
1007
            Renames or moves file or directory <oldname> to <newname>
994
            Renames or moves file or directory <oldname> to <newname>
1008
        """
995
        """
1009
        self.logger.info("Renaming " + oldname + " to " + newname + "...")
996
        self.logger.info("Renaming %s to %s..." % (oldname, newname))
1010
        self.emcore.file_rename(oldname, newname)
997
        self.emcore.file_rename(oldname, newname)
1011
        self.logger.info(" done\n")
998
        self.logger.info(" done\n")
1012
 
999
 
1013
    @command
1000
    @command
1014
    def get(self, remotename, localname, buffsize = 0x10000, buffer = None):
1001
    def get(self, remotename, localname, buffsize = 0x10000, buffer = None):
Line 1034... Line 1021...
1034
                    malloc = True
1021
                    malloc = True
1035
                else:
1022
                else:
1036
                    buffer = self._hexint(buffer)
1023
                    buffer = self._hexint(buffer)
1037
                    malloc = False
1024
                    malloc = False
1038
                try:
1025
                try:
1039
                    self.logger.info("Downloading file " + remotename + " to " + localname + "...")
1026
                    self.logger.info("Downloading file %s to %s..." % (remotename, localname))
1040
                    while size > 0:
1027
                    while size > 0:
1041
                        bytes = self.emcore.file_read(fd, buffsize, buffer).rc
1028
                        bytes = self.emcore.file_read(fd, buffsize, buffer).rc
1042
                        f.write(self.emcore.read(buffer, bytes))
1029
                        f.write(self.emcore.read(buffer, bytes))
1043
                        size = size - bytes
1030
                        size = size - bytes
1044
                finally:
1031
                finally:
Line 1106... Line 1093...
1106
                malloc = True
1093
                malloc = True
1107
            else:
1094
            else:
1108
                buffer = self._hexint(buffer)
1095
                buffer = self._hexint(buffer)
1109
                malloc = False
1096
                malloc = False
1110
            try:
1097
            try:
1111
                self.logger.info("Uploading file " + localname + " to " + remotename + "...")
1098
                self.logger.info("Uploading file %s to %s..." % (localname, remotename))
1112
                fd = self.emcore.file_open(remotename, 0x15)
1099
                fd = self.emcore.file_open(remotename, 0x15)
1113
                try:
1100
                try:
1114
                    while True:
1101
                    while True:
1115
                        data = f.read(buffsize)
1102
                        data = f.read(buffsize)
1116
                        if len(data) == 0: break
1103
                        if len(data) == 0: break
Line 1165... Line 1152...
1165
        """
1152
        """
1166
            Lists all files in the specified path
1153
            Lists all files in the specified path
1167
            [path]: the path which is listed
1154
            [path]: the path which is listed
1168
        """
1155
        """
1169
        handle = self.emcore.dir_open(path)
1156
        handle = self.emcore.dir_open(path)
1170
        self.logger.info("Directory listing of " + path + ":\n")
1157
        self.logger.info("Directory listing of %s:\n" % path)
1171
        while True:
1158
        while True:
1172
            try:
1159
            try:
1173
                entry = self.emcore.dir_read(handle)
1160
                entry = self.emcore.dir_read(handle)
1174
            except: break
1161
            except: break
1175
            if entry.attributes & 0x10: size = "DIR"
1162
            if entry.attributes & 0x10: size = "DIR"