Subversion Repositories freemyipod

Rev

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

Rev 532 Rev 548
Line 131... Line 131...
131
    @command()
131
    @command()
132
    def _readmem(self, addr, size):
132
    def _readmem(self, addr, size):
133
        """ Reads the memory from location 'addr' with size 'size'
133
        """ Reads the memory from location 'addr' with size 'size'
134
            from the device.
134
            from the device.
135
        """
135
        """
136
        resp = self.lib.monitorcommand(struct.pack("IIII", 4, addr, size, 0), "III%ds" % size, (None, None, None, "data"))
136
        resp = self.lib.monitorcommand(struct.pack("<IIII", 4, addr, size, 0), "III%ds" % size, (None, None, None, "data"))
137
        return resp.data
137
        return resp.data
138
    
138
    
139
    @command()
139
    @command()
140
    def _writemem(self, addr, data):
140
    def _writemem(self, addr, data):
141
        """ Writes the data in 'data' to the location 'addr'
141
        """ Writes the data in 'data' to the location 'addr'
142
            in the memory of the device.
142
            in the memory of the device.
143
        """
143
        """
144
        return self.lib.monitorcommand(struct.pack("IIII%ds" % len(data), 5, addr, len(data), 0, data), "III", (None, None, None))
144
        return self.lib.monitorcommand(struct.pack("<IIII%ds" % len(data), 5, addr, len(data), 0, data), "III", (None, None, None))
145
    
145
    
146
    @command()
146
    @command()
147
    def _readdma(self, addr, size):
147
    def _readdma(self, addr, size):
148
        """ Reads the memory from location 'addr' with size 'size'
148
        """ Reads the memory from location 'addr' with size 'size'
149
            from the device. This uses DMA and the data in endpoint.
149
            from the device. This uses DMA and the data in endpoint.
150
        """
150
        """
151
        self.lib.monitorcommand(struct.pack("IIII", 6, addr, size, 0), "III", (None, None, None))
151
        self.lib.monitorcommand(struct.pack("<IIII", 6, addr, size, 0), "III", (None, None, None))
152
        return struct.unpack("%ds" % size, self.lib.dev.din(size))[0]
152
        return struct.unpack("<%ds" % size, self.lib.dev.din(size))[0]
153
    
153
    
154
    @command()
154
    @command()
155
    def _writedma(self, addr, data):
155
    def _writedma(self, addr, data):
156
        """ Writes the data in 'data' to the location 'addr'
156
        """ Writes the data in 'data' to the location 'addr'
157
            in the memory of the device. This uses DMA and the data out endpoint.
157
            in the memory of the device. This uses DMA and the data out endpoint.
158
        """
158
        """
159
        self.lib.monitorcommand(struct.pack("IIII", 7, addr, len(data), 0), "III", (None, None, None))
159
        self.lib.monitorcommand(struct.pack("<IIII", 7, addr, len(data), 0), "III", (None, None, None))
160
        return self.lib.dev.dout(data)
160
        return self.lib.dev.dout(data)
161
    
161
    
162
    @command()
162
    @command()
163
    def getversioninfo(self):
163
    def getversioninfo(self):
164
        """ This returns the emCORE version and device information. """
164
        """ This returns the emCORE version and device information. """
165
        resp = self.lib.monitorcommand(struct.pack("IIII", 1, 0, 0, 0), "IBBBBI", ("revision", "majorv", "minorv", "patchv", "swtypeid", "hwtypeid"))
165
        resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 0, 0, 0), "IBBBBI", ("revision", "majorv", "minorv", "patchv", "swtypeid", "hwtypeid"))
166
        self.lib.dev.version.revision = resp.revision
166
        self.lib.dev.version.revision = resp.revision
167
        self.lib.dev.version.majorv = resp.majorv
167
        self.lib.dev.version.majorv = resp.majorv
168
        self.lib.dev.version.minorv = resp.minorv
168
        self.lib.dev.version.minorv = resp.minorv
169
        self.lib.dev.version.patchv = resp.patchv
169
        self.lib.dev.version.patchv = resp.patchv
170
        self.logger.debug("Device Software Type ID = 0x%X\n" % resp.swtypeid)
170
        self.logger.debug("Device Software Type ID = 0x%X\n" % resp.swtypeid)
Line 176... Line 176...
176
    @command()
176
    @command()
177
    def getpacketsizeinfo(self):
177
    def getpacketsizeinfo(self):
178
        """ This returns the emCORE max packet size information.
178
        """ This returns the emCORE max packet size information.
179
            It also sets the properties of the device object accordingly.
179
            It also sets the properties of the device object accordingly.
180
        """
180
        """
181
        resp = self.lib.monitorcommand(struct.pack("IIII", 1, 1, 0, 0), "HHII", ("coutmax", "cinmax", "doutmax", "dinmax"))
181
        resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 1, 0, 0), "HHII", ("coutmax", "cinmax", "doutmax", "dinmax"))
182
        self.logger.debug("Device cout packet size limit = %d\n" % resp.coutmax)
182
        self.logger.debug("Device cout packet size limit = %d\n" % resp.coutmax)
183
        self.lib.dev.packetsizelimit.cout = resp.coutmax
183
        self.lib.dev.packetsizelimit.cout = resp.coutmax
184
        self.logger.debug("Device cin packet size limit = %d\n" % resp.cinmax)
184
        self.logger.debug("Device cin packet size limit = %d\n" % resp.cinmax)
185
        self.lib.dev.packetsizelimit.cin = resp.cinmax
185
        self.lib.dev.packetsizelimit.cin = resp.cinmax
186
        self.logger.debug("Device din packet size limit = %d\n" % resp.doutmax)
186
        self.logger.debug("Device din packet size limit = %d\n" % resp.doutmax)
Line 190... Line 190...
190
        return resp
190
        return resp
191
    
191
    
192
    @command()
192
    @command()
193
    def getusermemrange(self):
193
    def getusermemrange(self):
194
        """ This returns the memory range the user has access to. """
194
        """ This returns the memory range the user has access to. """
195
        resp = self.lib.monitorcommand(struct.pack("IIII", 1, 2, 0, 0), "III", ("lower", "upper", None))
195
        resp = self.lib.monitorcommand(struct.pack("<IIII", 1, 2, 0, 0), "III", ("lower", "upper", None))
196
        self.logger.debug("Device user memory = 0x%X - 0x%X\n" % (resp.lower, resp.upper))
196
        self.logger.debug("Device user memory = 0x%X - 0x%X\n" % (resp.lower, resp.upper))
197
        self.lib.dev.usermem.lower = resp.lower
197
        self.lib.dev.usermem.lower = resp.lower
198
        self.lib.dev.usermem.upper = resp.upper
198
        self.lib.dev.usermem.upper = resp.upper
199
        return resp
199
        return resp
200
    
200
    
201
    @command()
201
    @command()
202
    def reset(self, force=False):
202
    def reset(self, force=False):
203
        """ Reboot the device """
203
        """ Reboot the device """
204
        if force:
204
        if force:
205
            return self.lib.monitorcommand(struct.pack("IIII", 2, 0, 0, 0))
205
            return self.lib.monitorcommand(struct.pack("<IIII", 2, 0, 0, 0))
206
        else:
206
        else:
207
            return self.lib.monitorcommand(struct.pack("IIII", 2, 1, 0, 0), "III", (None, None, None))
207
            return self.lib.monitorcommand(struct.pack("<IIII", 2, 1, 0, 0), "III", (None, None, None))
208
    
208
    
209
    @command()
209
    @command()
210
    def poweroff(self, force=False):
210
    def poweroff(self, force=False):
211
        """ Powers the device off. """
211
        """ Powers the device off. """
212
        if force:
212
        if force:
213
            return self.lib.monitorcommand(struct.pack("IIII", 3, 0, 0, 0))
213
            return self.lib.monitorcommand(struct.pack("<IIII", 3, 0, 0, 0))
214
        else:
214
        else:
215
            return self.lib.monitorcommand(struct.pack("IIII", 3, 1, 0, 0), "III", (None, None, None))
215
            return self.lib.monitorcommand(struct.pack("<IIII", 3, 1, 0, 0), "III", (None, None, None))
216
    
216
    
217
    @command()
217
    @command()
218
    def read(self, addr, size):
218
    def read(self, addr, size):
219
        """ Reads the memory from location 'addr' with size 'size'
219
        """ Reads the memory from location 'addr' with size 'size'
220
            from the device. This cares about too long packages
220
            from the device. This cares about too long packages
Line 300... Line 300...
300
    @command()
300
    @command()
301
    def i2cread(self, index, slaveaddr, startaddr, size):
301
    def i2cread(self, index, slaveaddr, startaddr, size):
302
        """ Reads data from an i2c slave """
302
        """ Reads data from an i2c slave """
303
        data = ""
303
        data = ""
304
        for i in range(size):
304
        for i in range(size):
305
            resp = self.lib.monitorcommand(struct.pack("IBBBBII", 8, index, slaveaddr, startaddr + i, 1, 0, 0), "III1s", (None, None, None, "data"))
305
            resp = self.lib.monitorcommand(struct.pack("<IBBBBII", 8, index, slaveaddr, startaddr + i, 1, 0, 0), "III1s", (None, None, None, "data"))
306
            data += resp.data
306
            data += resp.data
307
        return data
307
        return data
308
    
308
    
309
    @command()
309
    @command()
310
    def i2cwrite(self, index, slaveaddr, startaddr, data):
310
    def i2cwrite(self, index, slaveaddr, startaddr, data):
Line 312... Line 312...
312
        size = len(data)
312
        size = len(data)
313
        if size > 256 or size < 1:
313
        if size > 256 or size < 1:
314
            raise ArgumentError("Size must be a number between 1 and 256")
314
            raise ArgumentError("Size must be a number between 1 and 256")
315
        if size == 256:
315
        if size == 256:
316
            size = 0
316
            size = 0
317
        return self.lib.monitorcommand(struct.pack("IBBBBII%ds" % size, 9, index, slaveaddr, startaddr, size, 0, 0, data), "III", (None, None, None))
317
        return self.lib.monitorcommand(struct.pack("<IBBBBII%ds" % size, 9, index, slaveaddr, startaddr, size, 0, 0, data), "III", (None, None, None))
318
    
318
    
319
    @command()
319
    @command()
320
    def usbcread(self):
320
    def usbcread(self):
321
        """ Reads one packet with the maximal cin size """
321
        """ Reads one packet with the maximal cin size """
322
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
322
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
323
        resp = self.lib.monitorcommand(struct.pack("IIII", 10, cin_maxsize, 0, 0), "III%ds" % cin_maxsize, ("validsize", "buffersize", "queuesize", "data"))
323
        resp = self.lib.monitorcommand(struct.pack("<IIII", 10, cin_maxsize, 0, 0), "III%ds" % cin_maxsize, ("validsize", "buffersize", "queuesize", "data"))
324
        resp.data = resp.data[:resp.validsize]
324
        resp.data = resp.data[:resp.validsize]
325
        resp.maxsize = cin_maxsize
325
        resp.maxsize = cin_maxsize
326
        return resp
326
        return resp
327
    
327
    
328
    @command()
328
    @command()
Line 330... Line 330...
330
        """ Writes data to the USB console """
330
        """ Writes data to the USB console """
331
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
331
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
332
        size = len(data)
332
        size = len(data)
333
        while len(data) > 0:
333
        while len(data) > 0:
334
            writesize = min(cin_maxsize, len(data))
334
            writesize = min(cin_maxsize, len(data))
335
            resp = self.lib.monitorcommand(struct.pack("IIII%ds" % writesize, 11, writesize, 0, 0, data[:writesize]), "III", ("validsize", "buffersize", "freesize"))
335
            resp = self.lib.monitorcommand(struct.pack("<IIII%ds" % writesize, 11, writesize, 0, 0, data[:writesize]), "III", ("validsize", "buffersize", "freesize"))
336
            data = data[resp.validsize:]
336
            data = data[resp.validsize:]
337
        return size
337
        return size
338
    
338
    
339
    @command()
339
    @command()
340
    def cread(self, bitmask=0x1):
340
    def cread(self, bitmask=0x1):
341
        """ Reads one packet with the maximal cin size from the device consoles
341
        """ Reads one packet with the maximal cin size from the device consoles
342
            identified with the specified bitmask
342
            identified with the specified bitmask
343
        """
343
        """
344
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
344
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
345
        resp = self.lib.monitorcommand(struct.pack("IIII", 13, bitmask, cin_maxsize, 0), "III%ds" % cin_maxsize, ("size", None, None))
345
        resp = self.lib.monitorcommand(struct.pack("<IIII", 13, bitmask, cin_maxsize, 0), "III%ds" % cin_maxsize, ("size", None, None))
346
        resp.data = resp.data[size:]
346
        resp.data = resp.data[size:]
347
        resp.maxsize = cin_maxsize
347
        resp.maxsize = cin_maxsize
348
        return resp
348
        return resp
349
    
349
    
350
    @command()
350
    @command()
Line 354... Line 354...
354
        """
354
        """
355
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
355
        cin_maxsize = self.lib.dev.packetsizelimit.cin - self.lib.headersize
356
        size = len(data)
356
        size = len(data)
357
        while len(data) > 0:
357
        while len(data) > 0:
358
            writesize = min(cin_maxsize, len(data))
358
            writesize = min(cin_maxsize, len(data))
359
            resp = self.lib.monitorcommand(struct.pack("IIII%ds" % writesize, 12, bitmask, writesize, 0, data[:writesize]), "III", (None, None, None))
359
            resp = self.lib.monitorcommand(struct.pack("<IIII%ds" % writesize, 12, bitmask, writesize, 0, data[:writesize]), "III", (None, None, None))
360
            data = data[writesize:]
360
            data = data[writesize:]
361
        return size
361
        return size
362
    
362
    
363
    @command()
363
    @command()
364
    def cflush(self, bitmask):
364
    def cflush(self, bitmask):
365
        """ Flushes the consoles specified with 'bitmask' """
365
        """ Flushes the consoles specified with 'bitmask' """
366
        return self.lib.monitorcommand(struct.pack("IIII", 14, bitmask, 0, 0), "III", (None, None, None))
366
        return self.lib.monitorcommand(struct.pack("<IIII", 14, bitmask, 0, 0), "III", (None, None, None))
367
    
367
    
368
    @command()
368
    @command()
369
    def getprocinfo(self):
369
    def getprocinfo(self):
370
        """ Gets current state of the scheduler """
370
        """ Gets current state of the scheduler """
371
        schedulerstate = self.lockscheduler()
371
        schedulerstate = self.lockscheduler()
372
        resp = self.lib.monitorcommand(struct.pack("IIII", 15, 0, 0, 0), "III", ("structver", "structptr", None))
372
        resp = self.lib.monitorcommand(struct.pack("<IIII", 15, 0, 0, 0), "III", ("structver", "structptr", None))
373
        if resp.structver != 2:
373
        if resp.structver != 2:
374
            raise DeviceError("Unsupported thread struct version!")
374
            raise DeviceError("Unsupported thread struct version!")
375
        
375
        
376
        threads = []
376
        threads = []
377
        structptr = resp.structptr
377
        structptr = resp.structptr
Line 393... Line 393...
393
        return threads
393
        return threads
394
    
394
    
395
    @command()
395
    @command()
396
    def lockscheduler(self, freeze=True):
396
    def lockscheduler(self, freeze=True):
397
        """ Freezes/Unfreezes the scheduler """
397
        """ Freezes/Unfreezes the scheduler """
398
        resp = self.lib.monitorcommand(struct.pack("IIII", 16, 1 if freeze else 0, 0, 0), "III", ("before", None, None))
398
        resp = self.lib.monitorcommand(struct.pack("<IIII", 16, 1 if freeze else 0, 0, 0), "III", ("before", None, None))
399
        return True if resp.before == 1 else False
399
        return True if resp.before == 1 else False
400
    
400
    
401
    @command()
401
    @command()
402
    def unlockscheduler(self):
402
    def unlockscheduler(self):
403
        """ Unfreezes the scheduler """
403
        """ Unfreezes the scheduler """
404
        return self.lib.monitorcommand(struct.pack("IIII", 16, 0, 0, 0), "III", ("before", None, None))
404
        return self.lib.monitorcommand(struct.pack("<IIII", 16, 0, 0, 0), "III", ("before", None, None))
405
    
405
    
406
    @command()
406
    @command()
407
    def suspendthread(self, id, suspend=True):
407
    def suspendthread(self, id, suspend=True):
408
        """ Suspends the thread with the specified id """
408
        """ Suspends the thread with the specified id """
409
        resp = self.lib.monitorcommand(struct.pack("IIII", 17, 1 if suspend else 0, id, 0), "III", ("before", None, None))
409
        resp = self.lib.monitorcommand(struct.pack("<IIII", 17, 1 if suspend else 0, id, 0), "III", ("before", None, None))
410
        return True if resp.before == 1 else False
410
        return True if resp.before == 1 else False
411
    
411
    
412
    @command()
412
    @command()
413
    def resumethread(self, id):
413
    def resumethread(self, id):
414
        """ Resumes the thread with the specified id """
414
        """ Resumes the thread with the specified id """
415
        return self.lib.monitorcommand(struct.pack("IIII", 17, 0, id, 0), "III", ("before", None, None))
415
        return self.lib.monitorcommand(struct.pack("<IIII", 17, 0, id, 0), "III", ("before", None, None))
416
    
416
    
417
    @command()
417
    @command()
418
    def killthread(self, id):
418
    def killthread(self, id):
419
        """ Kills the thread with the specified id """
419
        """ Kills the thread with the specified id """
420
        return self.lib.monitorcommand(struct.pack("IIII", 18, id, 0, 0), "III", ("before", None, None))
420
        return self.lib.monitorcommand(struct.pack("<IIII", 18, id, 0, 0), "III", ("before", None, None))
421
    
421
    
422
    @command()
422
    @command()
423
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
423
    def createthread(self, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state):
424
        """ Creates a thread with the specified attributes """
424
        """ Creates a thread with the specified attributes """
425
        if threadtype == "user":
425
        if threadtype == "user":
Line 434... Line 434...
434
            state = 0
434
            state = 0
435
        elif state == "suspended":
435
        elif state == "suspended":
436
            state = 1
436
            state = 1
437
        else:
437
        else:
438
            raise ArgumentError("State must be either 'ready' or 'suspended'")
438
            raise ArgumentError("State must be either 'ready' or 'suspended'")
439
        resp = self.lib.monitorcommand(struct.pack("IIIIIIII", 19, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state), "III", ("threadptr", None, None))
439
        resp = self.lib.monitorcommand(struct.pack("<IIIIIIII", 19, nameptr, entrypoint, stackptr, stacksize, threadtype, priority, state), "III", ("threadptr", None, None))
440
        if resp.threadptr < 0:
440
        if resp.threadptr < 0:
441
            raise DeviceError("The device returned the error code %d" % resp.threadptr)
441
            raise DeviceError("The device returned the error code %d" % resp.threadptr)
442
        return resp
442
        return resp
443
    
443
    
444
    @command()
444
    @command()
445
    def flushcaches(self):
445
    def flushcaches(self):
446
        """ Flushes the CPU instruction and data cache """
446
        """ Flushes the CPU instruction and data cache """
447
        return self.lib.monitorcommand(struct.pack("IIII", 20, 0, 0, 0), "III", (None, None, None))
447
        return self.lib.monitorcommand(struct.pack("<IIII", 20, 0, 0, 0), "III", (None, None, None))
448
    
448
    
449
    @command()
449
    @command()
450
    def execimage(self, addr):
450
    def execimage(self, addr):
451
        """ Runs the emCORE app at 'addr' """
451
        """ Runs the emCORE app at 'addr' """
452
        return self.lib.monitorcommand(struct.pack("IIII", 21, addr, 0, 0), "III", ("thread", None, None))
452
        return self.lib.monitorcommand(struct.pack("<IIII", 21, addr, 0, 0), "III", ("thread", None, None))
453
    
453
    
454
    @command()
454
    @command()
455
    def run(self, app):
455
    def run(self, app):
456
        """ Uploads and runs the emCORE app in the string 'app' """
456
        """ Uploads and runs the emCORE app in the string 'app' """
457
        if app[:8] != "emCOexec":
457
        if app[:8] != "emCOexec":
Line 464... Line 464...
464
    @command(timeout = 5000)
464
    @command(timeout = 5000)
465
    def bootflashread(self, memaddr, flashaddr, size):
465
    def bootflashread(self, memaddr, flashaddr, size):
466
        """ Copies the data in the bootflash at 'flashaddr' of the specified size
466
        """ Copies the data in the bootflash at 'flashaddr' of the specified size
467
            to the memory at addr 'memaddr'
467
            to the memory at addr 'memaddr'
468
        """
468
        """
469
        return self.lib.monitorcommand(struct.pack("IIII", 22, memaddr, flashaddr, size), "III", (None, None, None))
469
        return self.lib.monitorcommand(struct.pack("<IIII", 22, memaddr, flashaddr, size), "III", (None, None, None))
470
    
470
    
471
    @command(timeout = 30000)
471
    @command(timeout = 30000)
472
    def bootflashwrite(self, memaddr, flashaddr, size):
472
    def bootflashwrite(self, memaddr, flashaddr, size):
473
        """ Copies the data in the memory at 'memaddr' of the specified size
473
        """ Copies the data in the memory at 'memaddr' of the specified size
474
            to the boot flash at addr 'flashaddr'
474
            to the boot flash at addr 'flashaddr'
475
        """
475
        """
476
        return self.lib.monitorcommand(struct.pack("IIII", 23, memaddr, flashaddr, size), "III", (None, None, None))
476
        return self.lib.monitorcommand(struct.pack("<IIII", 23, memaddr, flashaddr, size), "III", (None, None, None))
477
    
477
    
478
    @command()
478
    @command()
479
    def execfirmware(self, targetaddr, addr, size):
479
    def execfirmware(self, targetaddr, addr, size):
480
        """ Moves the firmware at 'addr' with size 'size' to 'targetaddr' and passes all control to it. """
480
        """ Moves the firmware at 'addr' with size 'size' to 'targetaddr' and passes all control to it. """
481
        self.logger.debug("Moving firmware at 0x%X with the size %d to 0x%X and executing it\n" % (addr, size, targetaddr))
481
        self.logger.debug("Moving firmware at 0x%X with the size %d to 0x%X and executing it\n" % (addr, size, targetaddr))
482
        return self.lib.monitorcommand(struct.pack("IIII", 24, targetaddr, addr, size))
482
        return self.lib.monitorcommand(struct.pack("<IIII", 24, targetaddr, addr, size))
483
    
483
    
484
    @command(timeout = 30000)
484
    @command(timeout = 30000)
485
    def aesencrypt(self, addr, size, keyindex):
485
    def aesencrypt(self, addr, size, keyindex):
486
        """ Encrypts the buffer at 'addr' with the specified size
486
        """ Encrypts the buffer at 'addr' with the specified size
487
            with the hardware AES key index 'keyindex'
487
            with the hardware AES key index 'keyindex'
488
        """
488
        """
489
        return self.lib.monitorcommand(struct.pack("IBBHII", 25, 1, 0, keyindex, addr, size), "III", (None, None, None))
489
        return self.lib.monitorcommand(struct.pack("<IBBHII", 25, 1, 0, keyindex, addr, size), "III", (None, None, None))
490
    
490
    
491
    @command(timeout = 30000)
491
    @command(timeout = 30000)
492
    def aesdecrypt(self, addr, size, keyindex):
492
    def aesdecrypt(self, addr, size, keyindex):
493
        """ Decrypts the buffer at 'addr' with the specified size
493
        """ Decrypts the buffer at 'addr' with the specified size
494
            with the hardware AES key index 'keyindex'
494
            with the hardware AES key index 'keyindex'
495
        """
495
        """
496
        return self.lib.monitorcommand(struct.pack("IBBHII", 25, 0, 0, keyindex, addr, size), "III", (None, None, None))
496
        return self.lib.monitorcommand(struct.pack("<IBBHII", 25, 0, 0, keyindex, addr, size), "III", (None, None, None))
497
    
497
    
498
    @command(timeout = 30000)
498
    @command(timeout = 30000)
499
    def hmac_sha1(self, addr, size, destination):
499
    def hmac_sha1(self, addr, size, destination):
500
        """ Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination' """
500
        """ Generates a HMAC-SHA1 hash of the buffer and saves it to 'destination' """
501
        return self.lib.monitorcommand(struct.pack("IIII", 26, addr, size, destination), "III", (None, None, None))
501
        return self.lib.monitorcommand(struct.pack("<IIII", 26, addr, size, destination), "III", (None, None, None))
502
 
502
 
503
    @command(target = 0x47324e49)
503
    @command(target = 0x47324e49)
504
    def ipodnano2g_getnandinfo(self):
504
    def ipodnano2g_getnandinfo(self):
505
        """ Target-specific function: ipodnano2g
505
        """ Target-specific function: ipodnano2g
506
            Gathers some information about the NAND chip used
506
            Gathers some information about the NAND chip used
507
        """
507
        """
508
        return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IHHHH", ("type", "pagesperblock", "banks", "userblocks", "blocks"))
508
        return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0001, 0, 0, 0), "IHHHH", ("type", "pagesperblock", "banks", "userblocks", "blocks"))
509
    
509
    
510
    @command(timeout = 30000, target = 0x47324e49)
510
    @command(timeout = 30000, target = 0x47324e49)
511
    def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
511
    def ipodnano2g_nandread(self, addr, start, count, doecc, checkempty):
512
        """ Target-specific function: ipodnano2g
512
        """ Target-specific function: ipodnano2g
513
            Reads data from the NAND chip into memory
513
            Reads data from the NAND chip into memory
514
        """
514
        """
515
        return self.lib.monitorcommand(struct.pack("IIII", 0xffff0002, addr | (0x80000000 if doecc else 0) | (0x40000000 if checkempty else 0), start, count), "III", (None, None, None))
515
        return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0002, addr | (0x80000000 if doecc else 0) | (0x40000000 if checkempty else 0), start, count), "III", (None, None, None))
516
    
516
    
517
    @command(timeout = 30000, target = 0x47324e49)
517
    @command(timeout = 30000, target = 0x47324e49)
518
    def ipodnano2g_nandwrite(self, addr, start, count, doecc):
518
    def ipodnano2g_nandwrite(self, addr, start, count, doecc):
519
        """ Target-specific function: ipodnano2g
519
        """ Target-specific function: ipodnano2g
520
            Writes data to the NAND chip
520
            Writes data to the NAND chip
521
        """
521
        """
522
        return self.lib.monitorcommand(struct.pack("IIII", 0xffff0003, addr | (0x80000000 if doecc else 0), start, count), "III", (None, None, None))
522
        return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0003, addr | (0x80000000 if doecc else 0), start, count), "III", (None, None, None))
523
    
523
    
524
    @command(timeout = 30000, target = 0x47324e49)
524
    @command(timeout = 30000, target = 0x47324e49)
525
    def ipodnano2g_nanderase(self, addr, start, count):
525
    def ipodnano2g_nanderase(self, addr, start, count):
526
        """ Target-specific function: ipodnano2g
526
        """ Target-specific function: ipodnano2g
527
            Erases blocks on the NAND chip and stores the results to memory
527
            Erases blocks on the NAND chip and stores the results to memory
528
        """
528
        """
529
        return self.lib.monitorcommand(struct.pack("IIII", 0xffff0004, addr, start, count), "III", (None, None, None))
529
        return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0004, addr, start, count), "III", (None, None, None))
530
    
530
    
531
    @command(target = 0x4c435049)
531
    @command(target = 0x4c435049)
532
    def ipodclassic_gethddinfo(self):
532
    def ipodclassic_gethddinfo(self):
533
        """ Target-specific function: ipodclassic
533
        """ Target-specific function: ipodclassic
534
            Gather information about the hard disk drive
534
            Gather information about the hard disk drive
535
        """
535
        """
536
        return self.lib.monitorcommand(struct.pack("IIII", 0xffff0001, 0, 0, 0), "IQQII", ("identifyptr", "totalsectors", "virtualsectors", "bbtptr", "bbtsize"))
536
        return self.lib.monitorcommand(struct.pack("<IIII", 0xffff0001, 0, 0, 0), "IQQII", ("identifyptr", "totalsectors", "virtualsectors", "bbtptr", "bbtsize"))
537
    
537
    
538
    @command(timeout = 30000, target = 0x4c435049)
538
    @command(timeout = 30000, target = 0x4c435049)
539
    def ipodclassic_hddaccess(self, type, sector, count, addr):
539
    def ipodclassic_hddaccess(self, type, sector, count, addr):
540
        """ Target-specific function: ipodclassic
540
        """ Target-specific function: ipodclassic
541
            Access the hard disk, type = 0 (read) / 1 (write)
541
            Access the hard disk, type = 0 (read) / 1 (write)
542
        """
542
        """
543
        rc = self.lib.monitorcommand(struct.pack("IIQIIII", 0xffff0002, type, sector, count, addr, 0, 0), "III", ("rc", None, None))
543
        rc = self.lib.monitorcommand(struct.pack("<IIQIIII", 0xffff0002, type, sector, count, addr, 0, 0), "III", ("rc", None, None))
544
        if (rc > 0x80000000):
544
        if (rc > 0x80000000):
545
            raise DeviceError("HDD access (type=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (type, sector, count, addr, rc))
545
            raise DeviceError("HDD access (type=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (type, sector, count, addr, rc))
546
    
546
    
547
    @command(target = 0x4c435049)
547
    @command(target = 0x4c435049)
548
    def ipodclassic_writebbt(self, bbt, tempaddr):
548
    def ipodclassic_writebbt(self, bbt, tempaddr):
Line 573... Line 573...
573
    
573
    
574
    @command()
574
    @command()
575
    def storage_get_info(self, volume):
575
    def storage_get_info(self, volume):
576
        """ Get information about a storage device """
576
        """ Get information about a storage device """
577
        self.logger.debug("Getting storage information\n")
577
        self.logger.debug("Getting storage information\n")
578
        result = self.lib.monitorcommand(struct.pack("IIII", 27, volume, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
578
        result = self.lib.monitorcommand(struct.pack("<IIII", 27, volume, 0, 0), "IIIIIIII", ("version", None, None, "sectorsize", "numsectors", "vendorptr", "productptr", "revisionptr"))
579
        if result.version != 1:
579
        if result.version != 1:
580
            raise ValueError("Unknown version of storage_info struct: %d" % result.version)
580
            raise ValueError("Unknown version of storage_info struct: %d" % result.version)
581
        result.vendor = self.readstring(result.vendorptr)
581
        result.vendor = self.readstring(result.vendorptr)
582
        result.product = self.readstring(result.productptr)
582
        result.product = self.readstring(result.productptr)
583
        result.revision = self.readstring(result.revisionptr)
583
        result.revision = self.readstring(result.revisionptr)
Line 591... Line 591...
591
    
591
    
592
    @command(timeout = 50000)
592
    @command(timeout = 50000)
593
    def storage_read_sectors_md(self, volume, sector, count, addr):
593
    def storage_read_sectors_md(self, volume, sector, count, addr):
594
        """ Read sectors from as storage device """
594
        """ Read sectors from as storage device """
595
        self.logger.debug("Reading %d sectors from disk at volume %d, sector %d to memory at 0x%X\n" % (count, volume, sector, addr))
595
        self.logger.debug("Reading %d sectors from disk at volume %d, sector %d to memory at 0x%X\n" % (count, volume, sector, addr))
596
        result = self.lib.monitorcommand(struct.pack("IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
596
        result = self.lib.monitorcommand(struct.pack("<IIQIIII", 28, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
597
        self.logger.debug("Read sectors, result: 0x%X\n" % result.rc)
597
        self.logger.debug("Read sectors, result: 0x%X\n" % result.rc)
598
        if result.rc > 0x80000000:
598
        if result.rc > 0x80000000:
599
            raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
599
            raise DeviceError("storage_read_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
600
 
600
 
601
    @command(timeout = 50000)
601
    @command(timeout = 50000)
602
    def storage_write_sectors_md(self, volume, sector, count, addr):
602
    def storage_write_sectors_md(self, volume, sector, count, addr):
603
        """ Read sectors from as storage device """
603
        """ Read sectors from as storage device """
604
        self.logger.debug("Writing %d sectors from memory at 0x%X to disk at volume %d, sector %d\n" % (count, addr, volume, sector))
604
        self.logger.debug("Writing %d sectors from memory at 0x%X to disk at volume %d, sector %d\n" % (count, addr, volume, sector))
605
        result = self.lib.monitorcommand(struct.pack("IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
605
        result = self.lib.monitorcommand(struct.pack("<IIQIIII", 29, volume, sector, count, addr, 0, 0), "III", ("rc", None, None))
606
        self.logger.debug("Wrote sectors, result: 0x%X\n" % result.rc)
606
        self.logger.debug("Wrote sectors, result: 0x%X\n" % result.rc)
607
        if result.rc > 0x80000000:
607
        if result.rc > 0x80000000:
608
            raise DeviceError("storage_write_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
608
            raise DeviceError("storage_write_sectors_md(volume=%d, sector=%d, count=%d, addr=0x%08X) failed with RC 0x%08X" % (volume, sector, count, addr, rc))
609
    
609
    
610
    @command(timeout = 30000)
610
    @command(timeout = 30000)
611
    def fat_enable_flushing(self, state):
611
    def fat_enable_flushing(self, state):
612
        """ Enables/disables flushing the FAT cache after every transaction """
612
        """ Enables/disables flushing the FAT cache after every transaction """
613
        if state != 0: self.logger.debug("Enabling FAT flushing\n")
613
        if state != 0: self.logger.debug("Enabling FAT flushing\n")
614
        else: self.logger.debug("Disabling FAT flushing\n")
614
        else: self.logger.debug("Disabling FAT flushing\n")
615
        self.lib.monitorcommand(struct.pack("IIII", 58, state, 0, 0), "III", (None, None, None))
615
        self.lib.monitorcommand(struct.pack("<IIII", 58, state, 0, 0), "III", (None, None, None))
616
        if state != 0: self.logger.debug("Enabled FAT flushing\n")
616
        if state != 0: self.logger.debug("Enabled FAT flushing\n")
617
        else: self.logger.debug("Disabled FAT flushing\n")
617
        else: self.logger.debug("Disabled FAT flushing\n")
618
    
618
    
619
    @command(timeout = 30000)
619
    @command(timeout = 30000)
620
    def file_open(self, filename, mode):
620
    def file_open(self, filename, mode):
621
        """ Opens a file and returns the handle """
621
        """ Opens a file and returns the handle """
622
        self.logger.debug("Opening remote file %s with mode %d\n" % (filename, mode))
622
        self.logger.debug("Opening remote file %s with mode %d\n" % (filename, mode))
623
        result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(filename), 30, mode, 0, 0, filename, 0), "III", ("fd", None, None))
623
        result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(filename), 30, mode, 0, 0, filename, 0), "III", ("fd", None, None))
624
        if result.fd > 0x80000000:
624
        if result.fd > 0x80000000:
625
            raise DeviceError("file_open(filename=\"%s\", mode=0x%X) failed with RC=0x%08X, errno=%d" % (filename, mode, result.fd, self.errno()))
625
            raise DeviceError("file_open(filename=\"%s\", mode=0x%X) failed with RC=0x%08X, errno=%d" % (filename, mode, result.fd, self.errno()))
626
        self.logger.debug("Opened file as handle 0x%X\n" % result.fd)
626
        self.logger.debug("Opened file as handle 0x%X\n" % result.fd)
627
        return result.fd
627
        return result.fd
628
    
628
    
629
    @command(timeout = 30000)
629
    @command(timeout = 30000)
630
    def file_size(self, fd):
630
    def file_size(self, fd):
631
        """ Gets the size of a file referenced by a handle """
631
        """ Gets the size of a file referenced by a handle """
632
        self.logger.debug("Getting file size of handle 0x%X\n" % fd)
632
        self.logger.debug("Getting file size of handle 0x%X\n" % fd)
633
        result = self.lib.monitorcommand(struct.pack("IIII", 31, fd, 0, 0), "III", ("size", None, None))
633
        result = self.lib.monitorcommand(struct.pack("<IIII", 31, fd, 0, 0), "III", ("size", None, None))
634
        if result.size > 0x80000000:
634
        if result.size > 0x80000000:
635
            raise DeviceError("file_size(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.size, self.errno()))
635
            raise DeviceError("file_size(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.size, self.errno()))
636
        self.logger.debug("Got file size: %d bytes\n" % result.size)
636
        self.logger.debug("Got file size: %d bytes\n" % result.size)
637
        return result.size
637
        return result.size
638
    
638
    
Line 644... Line 644...
644
            malloc = True
644
            malloc = True
645
        else:
645
        else:
646
            malloc = False
646
            malloc = False
647
        self.logger.debug("Reading %d bytes from file handle 0x%X to 0x%X\n" % (size, fd, addr))
647
        self.logger.debug("Reading %d bytes from file handle 0x%X to 0x%X\n" % (size, fd, addr))
648
        try:
648
        try:
649
            result = self.lib.monitorcommand(struct.pack("IIII", 32, fd, addr, size), "III", ("rc", None, None))
649
            result = self.lib.monitorcommand(struct.pack("<IIII", 32, fd, addr, size), "III", ("rc", None, None))
650
            if result.rc > 0x80000000:
650
            if result.rc > 0x80000000:
651
                raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
651
                raise DeviceError("file_read(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
652
        except:
652
        except:
653
            if malloc == True:
653
            if malloc == True:
654
                self.free(addr)
654
                self.free(addr)
Line 658... Line 658...
658
    
658
    
659
    @command(timeout = 30000)
659
    @command(timeout = 30000)
660
    def file_write(self, fd, size, addr):
660
    def file_write(self, fd, size, addr):
661
        """ Writes data from a file referenced by a handle. """
661
        """ Writes data from a file referenced by a handle. """
662
        self.logger.debug("Writing %d bytes from 0x%X to file handle 0x%X\n" % (size, addr, fd))
662
        self.logger.debug("Writing %d bytes from 0x%X to file handle 0x%X\n" % (size, addr, fd))
663
        result = self.lib.monitorcommand(struct.pack("IIII", 33, fd, addr, size), "III", ("rc", None, None))
663
        result = self.lib.monitorcommand(struct.pack("<IIII", 33, fd, addr, size), "III", ("rc", None, None))
664
        if result.rc > 0x80000000:
664
        if result.rc > 0x80000000:
665
            raise DeviceError("file_write(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
665
            raise DeviceError("file_write(fd=%d, addr=0x%08X, size=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, addr, size, result.rc, self.errno()))
666
        self.logger.debug("File write result: 0x%X\n" % result.rc)
666
        self.logger.debug("File write result: 0x%X\n" % result.rc)
667
        return result.rc
667
        return result.rc
668
    
668
    
669
    @command(timeout = 30000)
669
    @command(timeout = 30000)
670
    def file_seek(self, fd, offset, whence):
670
    def file_seek(self, fd, offset, whence):
671
        """ Seeks the file handle to the specified position in the file """
671
        """ Seeks the file handle to the specified position in the file """
672
        self.logger.debug("Seeking file handle 0x%X to whence=%d, offset=0x%X\n" % (fd, whence, offset))
672
        self.logger.debug("Seeking file handle 0x%X to whence=%d, offset=0x%X\n" % (fd, whence, offset))
673
        result = self.lib.monitorcommand(struct.pack("IIII", 34, fd, offset, whence), "III", ("rc", None, None))
673
        result = self.lib.monitorcommand(struct.pack("<IIII", 34, fd, offset, whence), "III", ("rc", None, None))
674
        if result.rc > 0x80000000:
674
        if result.rc > 0x80000000:
675
            raise DeviceError("file_seek(fd=%d, offset=0x%08X, whence=%d) failed with RC=0x%08X, errno=%d" % (fd, offset, whence, result.rc, self.errno()))
675
            raise DeviceError("file_seek(fd=%d, offset=0x%08X, whence=%d) failed with RC=0x%08X, errno=%d" % (fd, offset, whence, result.rc, self.errno()))
676
        self.logger.debug("File seek result: 0x%X\n" % (result.rc))
676
        self.logger.debug("File seek result: 0x%X\n" % (result.rc))
677
        return result.rc
677
        return result.rc
678
    
678
    
679
    @command(timeout = 30000)
679
    @command(timeout = 30000)
680
    def file_truncate(self, fd, length):
680
    def file_truncate(self, fd, length):
681
        """ Truncates a file referenced by a handle to a specified length """
681
        """ Truncates a file referenced by a handle to a specified length """
682
        self.logger.debug("Truncating file with handle 0x%X to 0x%X bytes\n" % (fd, length))
682
        self.logger.debug("Truncating file with handle 0x%X to 0x%X bytes\n" % (fd, length))
683
        result = self.lib.monitorcommand(struct.pack("IIII", 35, fd, offset, 0), "III", ("rc", None, None))
683
        result = self.lib.monitorcommand(struct.pack("<IIII", 35, fd, offset, 0), "III", ("rc", None, None))
684
        if result.rc > 0x80000000:
684
        if result.rc > 0x80000000:
685
            raise DeviceError("file_truncate(fd=%d, length=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, length, result.rc, self.errno()))
685
            raise DeviceError("file_truncate(fd=%d, length=0x%08X) failed with RC=0x%08X, errno=%d" % (fd, length, result.rc, self.errno()))
686
        self.logger.debug("File truncate result: 0x%X\n" % (result.rc))
686
        self.logger.debug("File truncate result: 0x%X\n" % (result.rc))
687
        return result.rc
687
        return result.rc
688
    
688
    
689
    @command(timeout = 30000)
689
    @command(timeout = 30000)
690
    def file_sync(self, fd):
690
    def file_sync(self, fd):
691
        """ Flushes a file handles' buffers """
691
        """ Flushes a file handles' buffers """
692
        self.logger.debug("Flushing buffers of file with handle 0x%X\n" % (fd))
692
        self.logger.debug("Flushing buffers of file with handle 0x%X\n" % (fd))
693
        result = self.lib.monitorcommand(struct.pack("IIII", 36, fd, 0, 0), "III", ("rc", None, None))
693
        result = self.lib.monitorcommand(struct.pack("<IIII", 36, fd, 0, 0), "III", ("rc", None, None))
694
        if result.rc > 0x80000000:
694
        if result.rc > 0x80000000:
695
            raise DeviceError("file_sync(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
695
            raise DeviceError("file_sync(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
696
        self.logger.debug("File flush result: 0x%X\n" % (result.rc))
696
        self.logger.debug("File flush result: 0x%X\n" % (result.rc))
697
        return result.rc
697
        return result.rc
698
    
698
    
699
    @command(timeout = 30000)
699
    @command(timeout = 30000)
700
    def file_close(self, fd):
700
    def file_close(self, fd):
701
        """ Closes a file handle """
701
        """ Closes a file handle """
702
        self.logger.debug("Closing file handle 0x%X\n" % (fd))
702
        self.logger.debug("Closing file handle 0x%X\n" % (fd))
703
        result = self.lib.monitorcommand(struct.pack("IIII", 37, fd, 0, 0), "III", ("rc", None, None))
703
        result = self.lib.monitorcommand(struct.pack("<IIII", 37, fd, 0, 0), "III", ("rc", None, None))
704
        if result.rc > 0x80000000:
704
        if result.rc > 0x80000000:
705
            raise DeviceError("file_close(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
705
            raise DeviceError("file_close(fd=%d) failed with RC=0x%08X, errno=%d" % (fd, result.rc, self.errno()))
706
        self.logger.debug("File close result: 0x%X\n" % (result.rc))
706
        self.logger.debug("File close result: 0x%X\n" % (result.rc))
707
        return result.rc
707
        return result.rc
708
    
708
    
709
    @command(timeout = 30000)
709
    @command(timeout = 30000)
710
    def file_close_all(self):
710
    def file_close_all(self):
711
        """ Closes all file handles opened through the debugger """
711
        """ Closes all file handles opened through the debugger """
712
        self.logger.debug("Closing all files that were opened via USB\n")
712
        self.logger.debug("Closing all files that were opened via USB\n")
713
        result = self.lib.monitorcommand(struct.pack("IIII", 38, 0, 0, 0), "III", ("rc", None, None))
713
        result = self.lib.monitorcommand(struct.pack("<IIII", 38, 0, 0, 0), "III", ("rc", None, None))
714
        if result.rc > 0x80000000:
714
        if result.rc > 0x80000000:
715
            raise DeviceError("file_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
715
            raise DeviceError("file_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
716
        self.logger.debug("Closed %d files\n" % (result.rc))
716
        self.logger.debug("Closed %d files\n" % (result.rc))
717
        return result.rc
717
        return result.rc
718
    
718
    
719
    @command(timeout = 30000)
719
    @command(timeout = 30000)
720
    def file_kill_all(self, volume):
720
    def file_kill_all(self, volume):
721
        """ Kills all file handles of a volume (in the whole system) """
721
        """ Kills all file handles of a volume (in the whole system) """
722
        self.logger.debug("Killing all file handles of volume %d\n" % (volume))
722
        self.logger.debug("Killing all file handles of volume %d\n" % (volume))
723
        result = self.lib.monitorcommand(struct.pack("IIII", 39, volume, 0, 0), "III", ("rc", None, None))
723
        result = self.lib.monitorcommand(struct.pack("<IIII", 39, volume, 0, 0), "III", ("rc", None, None))
724
        if result.rc > 0x80000000:
724
        if result.rc > 0x80000000:
725
            raise DeviceError("file_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
725
            raise DeviceError("file_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
726
        self.logger.debug("Closed %d files\n" % (result.rc))
726
        self.logger.debug("Closed %d files\n" % (result.rc))
727
        return result.rc
727
        return result.rc
728
    
728
    
729
    @command(timeout = 30000)
729
    @command(timeout = 30000)
730
    def file_unlink(self, filename):
730
    def file_unlink(self, filename):
731
        """ Removes a file """
731
        """ Removes a file """
732
        self.logger.debug("Deleting file %s\n" % (filename))
732
        self.logger.debug("Deleting file %s\n" % (filename))
733
        result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(filename), 40, 0, 0, 0, filename, 0), "III", ("rc", None, None))
733
        result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(filename), 40, 0, 0, 0, filename, 0), "III", ("rc", None, None))
734
        if result.rc > 0x80000000:
734
        if result.rc > 0x80000000:
735
            raise DeviceError("file_unlink(filename=\"%s\") failed with RC=0x%08X, errno=%d" % (filename, result.rc, self.errno()))
735
            raise DeviceError("file_unlink(filename=\"%s\") failed with RC=0x%08X, errno=%d" % (filename, result.rc, self.errno()))
736
        self.logger.debug("Delete file result: 0x%X\n" % (result.rc))
736
        self.logger.debug("Delete file result: 0x%X\n" % (result.rc))
737
        return result.rc
737
        return result.rc
738
    
738
    
739
    @command(timeout = 30000)
739
    @command(timeout = 30000)
740
    def file_rename(self, oldname, newname):
740
    def file_rename(self, oldname, newname):
741
        """ Renames a file """
741
        """ Renames a file """
742
        self.logger.debug("Renaming file %s to %s\n" % (oldname, newname))
742
        self.logger.debug("Renaming file %s to %s\n" % (oldname, newname))
743
        result = self.lib.monitorcommand(struct.pack("IIII248s%dsB" % min(247, len(newname)), 41, 0, 0, 0, oldname, newname, 0), "III", ("rc", None, None))
743
        result = self.lib.monitorcommand(struct.pack("<IIII248s%dsB" % min(247, len(newname)), 41, 0, 0, 0, oldname, newname, 0), "III", ("rc", None, None))
744
        if result.rc > 0x80000000:
744
        if result.rc > 0x80000000:
745
            raise DeviceError("file_rename(oldname=\"%s\", newname=\"%s\") failed with RC=0x%08X, errno=%d" % (oldname, newname, result.rc, self.errno()))
745
            raise DeviceError("file_rename(oldname=\"%s\", newname=\"%s\") failed with RC=0x%08X, errno=%d" % (oldname, newname, result.rc, self.errno()))
746
        self.logger.debug("Rename file result: 0x%X\n" % (result.rc))
746
        self.logger.debug("Rename file result: 0x%X\n" % (result.rc))
747
        return result.rc
747
        return result.rc
748
    
748
    
749
    @command(timeout = 30000)
749
    @command(timeout = 30000)
750
    def dir_open(self, dirname):
750
    def dir_open(self, dirname):
751
        """ Opens a directory and returns the handle """
751
        """ Opens a directory and returns the handle """
752
        self.logger.debug("Opening directory %s\n" % (dirname))
752
        self.logger.debug("Opening directory %s\n" % (dirname))
753
        result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 42, 0, 0, 0, dirname, 0), "III", ("handle", None, None))
753
        result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 42, 0, 0, 0, dirname, 0), "III", ("handle", None, None))
754
        if result.handle == 0:
754
        if result.handle == 0:
755
            raise DeviceError("dir_open(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.handle, self.errno()))
755
            raise DeviceError("dir_open(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.handle, self.errno()))
756
        self.logger.debug("Opened directory as handle 0x%X\n" % (result.handle))
756
        self.logger.debug("Opened directory as handle 0x%X\n" % (result.handle))
757
        return result.handle
757
        return result.handle
758
    
758
    
759
    @command(timeout = 30000)
759
    @command(timeout = 30000)
760
    def dir_read(self, handle):
760
    def dir_read(self, handle):
761
        """ Reads the next entry from a directory """
761
        """ Reads the next entry from a directory """
762
        self.logger.debug("Reading next entry of directory handle 0x%X\n" % (handle))
762
        self.logger.debug("Reading next entry of directory handle 0x%X\n" % (handle))
763
        result = self.lib.monitorcommand(struct.pack("IIII", 43, handle, 0, 0), "III", ("version", "maxpath", "ptr"))
763
        result = self.lib.monitorcommand(struct.pack("<IIII", 43, handle, 0, 0), "III", ("version", "maxpath", "ptr"))
764
        if result.ptr == 0:
764
        if result.ptr == 0:
765
            raise DeviceError("dir_read(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.ptr, self.errno()))
765
            raise DeviceError("dir_read(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.ptr, self.errno()))
766
        if result.version != 1:
766
        if result.version != 1:
767
            raise ValueError("Unknown version of dirent struct: %d" % result.version)
767
            raise ValueError("Unknown version of dirent struct: %d" % result.version)
768
        dirent = self.read(result.ptr, result.maxpath + 16)
768
        dirent = self.read(result.ptr, result.maxpath + 16)
769
        ret = Bunch()
769
        ret = Bunch()
770
        (ret.name, ret.attributes, ret.size, ret.startcluster, ret.wrtdate, ret.wrttime) = struct.unpack("%dsIIIHH" % result.maxpath, dirent)
770
        (ret.name, ret.attributes, ret.size, ret.startcluster, ret.wrtdate, ret.wrttime) = struct.unpack("<%dsIIIHH" % result.maxpath, dirent)
771
        ret.name = ret.name[:ret.name.index('\x00')]
771
        ret.name = ret.name[:ret.name.index('\x00')]
772
        self.logger.debug("Read directory entry:\n")
772
        self.logger.debug("Read directory entry:\n")
773
        self.logger.debug("Name: %s\n" % ret.name)
773
        self.logger.debug("Name: %s\n" % ret.name)
774
        self.logger.debug("Attributes: 0x%X\n" % ret.attributes)
774
        self.logger.debug("Attributes: 0x%X\n" % ret.attributes)
775
        self.logger.debug("Size: %d\n" % ret.size)
775
        self.logger.debug("Size: %d\n" % ret.size)
Line 780... Line 780...
780
    
780
    
781
    @command(timeout = 30000)
781
    @command(timeout = 30000)
782
    def dir_close(self, handle):
782
    def dir_close(self, handle):
783
        """ Closes a directory handle """
783
        """ Closes a directory handle """
784
        self.logger.debug("Closing directory handle 0x%X\n" % (handle))
784
        self.logger.debug("Closing directory handle 0x%X\n" % (handle))
785
        result = self.lib.monitorcommand(struct.pack("IIII", 44, handle, 0, 0), "III", ("rc", None, None))
785
        result = self.lib.monitorcommand(struct.pack("<IIII", 44, handle, 0, 0), "III", ("rc", None, None))
786
        if result.rc > 0x80000000:
786
        if result.rc > 0x80000000:
787
            raise DeviceError("dir_close(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.rc, self.errno()))
787
            raise DeviceError("dir_close(handle=0x%08X) failed with RC=0x%08X, errno=%d" % (handle, result.rc, self.errno()))
788
        self.logger.debug("Close directory result: 0x%X\n" % (result.rc))
788
        self.logger.debug("Close directory result: 0x%X\n" % (result.rc))
789
        return result.rc
789
        return result.rc
790
    
790
    
791
    @command(timeout = 30000)
791
    @command(timeout = 30000)
792
    def dir_close_all(self):
792
    def dir_close_all(self):
793
        """ Closes all directory handles opened through the debugger """
793
        """ Closes all directory handles opened through the debugger """
794
        self.logger.debug("Closing all directories that were opened via USB\n")
794
        self.logger.debug("Closing all directories that were opened via USB\n")
795
        result = self.lib.monitorcommand(struct.pack("IIII", 45, 0, 0, 0), "III", ("rc", None, None))
795
        result = self.lib.monitorcommand(struct.pack("<IIII", 45, 0, 0, 0), "III", ("rc", None, None))
796
        if result.rc > 0x80000000:
796
        if result.rc > 0x80000000:
797
            raise DeviceError("dir_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
797
            raise DeviceError("dir_close_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
798
        self.logger.debug("Closed %d directories\n" % (result.rc))
798
        self.logger.debug("Closed %d directories\n" % (result.rc))
799
        return result.rc
799
        return result.rc
800
    
800
    
801
    @command(timeout = 30000)
801
    @command(timeout = 30000)
802
    def dir_kill_all(self, volume):
802
    def dir_kill_all(self, volume):
803
        """ Kills all directory handles of a volume (in the whole system) """
803
        """ Kills all directory handles of a volume (in the whole system) """
804
        self.logger.debug("Closing all directories of volume %d\n" % (volume))
804
        self.logger.debug("Closing all directories of volume %d\n" % (volume))
805
        result = self.lib.monitorcommand(struct.pack("IIII", 46, volume, 0, 0), "III", ("rc", None, None))
805
        result = self.lib.monitorcommand(struct.pack("<IIII", 46, volume, 0, 0), "III", ("rc", None, None))
806
        if result.rc > 0x80000000:
806
        if result.rc > 0x80000000:
807
            raise DeviceError("dir_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
807
            raise DeviceError("dir_kill_all() failed with RC=0x%08X, errno=%d" % (result.rc, self.errno()))
808
        self.logger.debug("Closed %d directories\n" % (result.rc))
808
        self.logger.debug("Closed %d directories\n" % (result.rc))
809
        return result.rc
809
        return result.rc
810
    
810
    
811
    @command(timeout = 30000)
811
    @command(timeout = 30000)
812
    def dir_create(self, dirname):
812
    def dir_create(self, dirname):
813
        """ Creates a directory """
813
        """ Creates a directory """
814
        self.logger.debug("Creating directory %s\n" % (dirname))
814
        self.logger.debug("Creating directory %s\n" % (dirname))
815
        result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 47, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
815
        result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 47, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
816
        if result.rc > 0x80000000:
816
        if result.rc > 0x80000000:
817
            raise DeviceError("dir_create(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
817
            raise DeviceError("dir_create(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
818
        self.logger.debug("Create directory result: 0x%X\n" % (result.rc))
818
        self.logger.debug("Create directory result: 0x%X\n" % (result.rc))
819
        return result.rc
819
        return result.rc
820
    
820
    
821
    @command(timeout = 30000)
821
    @command(timeout = 30000)
822
    def dir_remove(self, dirname):
822
    def dir_remove(self, dirname):
823
        """ Removes an (empty) directory """
823
        """ Removes an (empty) directory """
824
        self.logger.debug("Removing directory %s\n" % (dirname))
824
        self.logger.debug("Removing directory %s\n" % (dirname))
825
        result = self.lib.monitorcommand(struct.pack("IIII%dsB" % len(dirname), 48, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
825
        result = self.lib.monitorcommand(struct.pack("<IIII%dsB" % len(dirname), 48, 0, 0, 0, dirname, 0), "III", ("rc", None, None))
826
        if result.rc > 0x80000000:
826
        if result.rc > 0x80000000:
827
            raise DeviceError("dir_remove(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
827
            raise DeviceError("dir_remove(dirname=\"%s\") failed with RC=0x%08X, errno=%d" % (dirname, result.rc, self.errno()))
828
        self.logger.debug("Remove directory result: 0x%X\n" % (result.rc))
828
        self.logger.debug("Remove directory result: 0x%X\n" % (result.rc))
829
        return result.rc
829
        return result.rc
830
    
830
    
831
    @command()
831
    @command()
832
    def errno(self):
832
    def errno(self):
833
        """ Returns the number of the last error that happened """
833
        """ Returns the number of the last error that happened """
834
        self.logger.debug("Getting last error number\n")
834
        self.logger.debug("Getting last error number\n")
835
        result = self.lib.monitorcommand(struct.pack("IIII", 49, 0, 0, 0), "III", ("errno", None, None))
835
        result = self.lib.monitorcommand(struct.pack("<IIII", 49, 0, 0, 0), "III", ("errno", None, None))
836
        self.logger.debug("Last error: 0x%X\n" % (result.errno))
836
        self.logger.debug("Last error: 0x%X\n" % (result.errno))
837
        return result.errno
837
        return result.errno
838
    
838
    
839
    @command()
839
    @command()
840
    def disk_mount(self, volume):
840
    def disk_mount(self, volume):
841
        """ Mounts a volume """
841
        """ Mounts a volume """
842
        self.logger.debug("Mounting volume %d\n" % (volume))
842
        self.logger.debug("Mounting volume %d\n" % (volume))
843
        result = self.lib.monitorcommand(struct.pack("IIII", 50, volume, 0, 0), "III", ("rc", None, None))
843
        result = self.lib.monitorcommand(struct.pack("<IIII", 50, volume, 0, 0), "III", ("rc", None, None))
844
        if result.rc > 0x80000000:
844
        if result.rc > 0x80000000:
845
            raise DeviceError("disk_mount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
845
            raise DeviceError("disk_mount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
846
        self.logger.debug("Mount volume result: 0x%X\n" % (result.rc))
846
        self.logger.debug("Mount volume result: 0x%X\n" % (result.rc))
847
        return result.rc
847
        return result.rc
848
    
848
    
849
    @command()
849
    @command()
850
    def disk_unmount(self, volume):
850
    def disk_unmount(self, volume):
851
        """ Unmounts a volume """
851
        """ Unmounts a volume """
852
        self.logger.debug("Unmounting volume %d\n" % (volume))
852
        self.logger.debug("Unmounting volume %d\n" % (volume))
853
        result = self.lib.monitorcommand(struct.pack("IIII", 51, volume, 0, 0), "III", ("rc", None, None))
853
        result = self.lib.monitorcommand(struct.pack("<IIII", 51, volume, 0, 0), "III", ("rc", None, None))
854
        if result.rc > 0x80000000:
854
        if result.rc > 0x80000000:
855
            raise DeviceError("disk_unmount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
855
            raise DeviceError("disk_unmount(volume=%d) failed with RC=0x%08X, errno=%d" % (volume, result.rc, self.errno()))
856
        self.logger.debug("Unmount volume result: 0x%X\n" % (result.rc))
856
        self.logger.debug("Unmount volume result: 0x%X\n" % (result.rc))
857
        return result.rc
857
        return result.rc
858
    
858
    
859
    @command()
859
    @command()
860
    def malloc(self, size):
860
    def malloc(self, size):
861
        """ Allocates 'size' bytes and returns a pointer to the allocated memory """
861
        """ Allocates 'size' bytes and returns a pointer to the allocated memory """
862
        self.logger.debug("Allocating %d bytes of memory\n" % size)
862
        self.logger.debug("Allocating %d bytes of memory\n" % size)
863
        result = self.lib.monitorcommand(struct.pack("IIII", 52, size, 0, 0), "III", ("ptr", None, None))
863
        result = self.lib.monitorcommand(struct.pack("<IIII", 52, size, 0, 0), "III", ("ptr", None, None))
864
        self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
864
        self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
865
        return result.ptr
865
        return result.ptr
866
    
866
    
867
    @command()
867
    @command()
868
    def memalign(self, align, size):
868
    def memalign(self, align, size):
869
        """ Allocates 'size' bytes aligned to 'align' and returns a pointer to the allocated memory """
869
        """ Allocates 'size' bytes aligned to 'align' and returns a pointer to the allocated memory """
870
        self.logger.debug("Allocating %d bytes of memory aligned to 0x%X\n" % (size, align))
870
        self.logger.debug("Allocating %d bytes of memory aligned to 0x%X\n" % (size, align))
871
        result = self.lib.monitorcommand(struct.pack("IIII", 53, align, size, 0), "III", ("ptr", None, None))
871
        result = self.lib.monitorcommand(struct.pack("<IIII", 53, align, size, 0), "III", ("ptr", None, None))
872
        self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
872
        self.logger.debug("Allocated %d bytes of memory at 0x%X\n" % (size, result.ptr))
873
        return result.ptr
873
        return result.ptr
874
    
874
    
875
    @command()
875
    @command()
876
    def realloc(self, ptr, size):
876
    def realloc(self, ptr, size):
877
        """ The size of the memory block pointed to by 'ptr' is changed to the 'size' bytes,
877
        """ The size of the memory block pointed to by 'ptr' is changed to the 'size' bytes,
878
            expanding or reducing the amount of memory available in the block.
878
            expanding or reducing the amount of memory available in the block.
879
            Returns a pointer to the reallocated memory.
879
            Returns a pointer to the reallocated memory.
880
        """
880
        """
881
        self.logger.debug("Reallocating 0x%X to have the new size %d\n" % (ptr, size))
881
        self.logger.debug("Reallocating 0x%X to have the new size %d\n" % (ptr, size))
882
        result = self.lib.monitorcommand(struct.pack("IIII", 54, ptr, size, 0), "III", ("ptr", None, None))
882
        result = self.lib.monitorcommand(struct.pack("<IIII", 54, ptr, size, 0), "III", ("ptr", None, None))
883
        self.logger.debug("Reallocated memory at 0x%X to 0x%X with the new size %d\n" % (ptr, result.ptr, size))
883
        self.logger.debug("Reallocated memory at 0x%X to 0x%X with the new size %d\n" % (ptr, result.ptr, size))
884
        return result.ptr
884
        return result.ptr
885
    
885
    
886
    @command()
886
    @command()
887
    def reownalloc(self, ptr, owner):
887
    def reownalloc(self, ptr, owner):
888
        """ Changes the owner of the memory allocation 'ptr' to the thread struct at addr 'owner' """
888
        """ Changes the owner of the memory allocation 'ptr' to the thread struct at addr 'owner' """
889
        self.logger.debug("Changing owner of the memory region 0x%X to 0x%X\n" % (ptr, owner))
889
        self.logger.debug("Changing owner of the memory region 0x%X to 0x%X\n" % (ptr, owner))
890
        return self.lib.monitorcommand(struct.pack("IIII", 55, ptr, owner, 0), "III", (None, None, None))
890
        return self.lib.monitorcommand(struct.pack("<IIII", 55, ptr, owner, 0), "III", (None, None, None))
891
    
891
    
892
    @command()
892
    @command()
893
    def free(self, ptr):
893
    def free(self, ptr):
894
        """ Frees the memory space pointed to by 'ptr' """
894
        """ Frees the memory space pointed to by 'ptr' """
895
        self.logger.debug("Freeing the memory region at 0x%X\n" % ptr)
895
        self.logger.debug("Freeing the memory region at 0x%X\n" % ptr)
896
        return self.lib.monitorcommand(struct.pack("IIII", 56, ptr, 0, 0), "III", (None, None, None))
896
        return self.lib.monitorcommand(struct.pack("<IIII", 56, ptr, 0, 0), "III", (None, None, None))
897
    
897
    
898
    @command()
898
    @command()
899
    def free_all(self):
899
    def free_all(self):
900
        """ Frees all memory allocations created by the monitor thread """
900
        """ Frees all memory allocations created by the monitor thread """
901
        self.logger.debug("Freeing all memory allocations created by the monitor thread\n")
901
        self.logger.debug("Freeing all memory allocations created by the monitor thread\n")
902
        return self.lib.monitorcommand(struct.pack("IIII", 57, 0, 0, 0), "III", (None, None, None))
902
        return self.lib.monitorcommand(struct.pack("<IIII", 57, 0, 0, 0), "III", (None, None, None))
903
 
903
 
904
 
904
 
905
class Lib(object):
905
class Lib(object):
906
    def __init__(self, logger):
906
    def __init__(self, logger):
907
        self.logger = logger
907
        self.logger = logger