Subversion Repositories freemyipod

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
82 benedikt93 1
#!/usr/bin/env python
64 benedikt93 2
#
3
#
82 benedikt93 4
#    Copyright 2010 TheSeven, benedikt93
64 benedikt93 5
#
6
#
82 benedikt93 7
#    This file is part of emBIOS.
64 benedikt93 8
#
82 benedikt93 9
#    emBIOS is free software: you can redistribute it and/or
64 benedikt93 10
#    modify it under the terms of the GNU General Public License as
11
#    published by the Free Software Foundation, either version 2 of the
12
#    License, or (at your option) any later version.
13
#
82 benedikt93 14
#    emBIOS is distributed in the hope that it will be useful,
64 benedikt93 15
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
17
#    See the GNU General Public License for more details.
18
#
82 benedikt93 19
#    You should have received a copy of the GNU General Public License
20
#    along with emBIOS.  If not, see <http://www.gnu.org/licenses/>.
64 benedikt93 21
#
22
#
23
 
82 benedikt93 24
# note: handles commands 1 to 20
64 benedikt93 25
 
26
import sys
27
import time
28
import libembios
102 benedikt93 29
import struct
64 benedikt93 30
 
31
 
32
def usage():
33
  print ""
34
  print "Please provide a command and (if needed) parameters as command line arguments"
35
  print ""
36
  print "Available commands:"
37
  print ""
38
  print "  getinfo <infotype>"
39
  print "    Get info on the running emBIOS."
40
  print "    <infotype> may be either off 'version', 'packetsize', 'usermemrange'."
41
  print ""
42
  print "  reset <force>"
43
  print "    Resets the device"
44
  print "    If <force> is 1, the reset will be forced, otherwise it will be gracefully,"
45
  print "    which may take some time."
46
  print ""
47
  print "  poweroff <force>"
48
  print "    Powers the device off"
49
  print "    If <force> is 1, the poweroff will be forced, otherwise it will be gracefully,"
50
  print "    which may take some time."
51
  print ""
82 benedikt93 52
  print ""
53
  print "  uploadfile <offset> <file> <usedma> <freezescheduler>"
54
  print "    Uploads a file to the iPod"
55
  print "      <offset>: the address to upload the file to"
56
  print "      <file>: the path to the file"
57
  print "      <usedma>: if 0, DMA will not be used when uploading the file,"
58
  print "          otherwise it will be used. It can be omitted, default is then 1"
59
  print "      <freezescheduler>: if not 0, the scheduler will be frozen during DMA access"
60
  print "          to prevent non-consistent data after the transfer."
61
  print "          It can be omitted, default is then 0"
62
  print ""
63
  print "  downloadfile <offset> <size> <file> <usedma> <freezescheduler>"
64
  print "    Uploads a file to the iPod"
65
  print "      <offset>: the address to upload the file to"
66
  print "      <size>: the number of bytes to be read"
67
  print "      <file>: the path to the file"
68
  print "      <usedma>: if 0, DMA will not be used when downloading the file,"
69
  print "          otherwise it will be used. It can be omitted, default is then 1"
70
  print "      <freezescheduler>: if not 0, the scheduler will be frozen during DMA access"
71
  print "          to prevent non-consistent data after the transfer"
72
  print "          It can be omitted, default is then 0"
73
  print ""
74
  print "  uploadint <offset> <data>"
75
  print "    Uploads a single integer to the iPod"
76
  print "      <offset>: the address to upload the integer to"
77
  print "      <data>: the integer to upload"
78
  print ""
79
  print "  downloadint <offset>"
80
  print "    Downloads a single integer from the iPod and prints it to the console window"
81
  print "      <offset>: the address to download the integer from"
82
  print ""
83
  print ""
67 benedikt93 84
  print "  i2crecv <bus> <slave> <addr> <size>"
85
  print "    Reads data from an I2C device"
86
  print "      <bus> the bus index"
87
  print "      <slave> the slave address"
88
  print "      <addr> the start address on the I2C device"
89
  print "      <size> the number of bytes to read"
90
  print ""
91
  print "  i2csend <bus> <slave> <addr> <db1> <db2> ... <dbN>"
92
  print "    Writes data to an I2C device"
93
  print "      <bus> the bus index"
94
  print "      <slave> the slave address"
95
  print "      <addr> the start address on the I2C device"
82 benedikt93 96
  print "      <db1> ... <dbN> the data in single bytes, seperated by whitespaces,"
97
  print "                      eg. 0x37 0x56 0x45 0x12"
67 benedikt93 98
  print ""
82 benedikt93 99
  print ""
100
  print "  readusbconsole <size> <outtype> <file>"
101
  print "    Reads data from the USB console."
102
  print "      <size>: the number of bytes to read"
103
  print "      <outtype>: defines how to output the result:"
104
  print "        'file': writes the result to file <file>"
105
  print "        'printstring': writes the result as string to the console window"
106
  print "        'printhex': writes the result in hexedit notation to the console window"
107
  print "      <file>: the file to write the result to, can be omitted"
108
  print "               if <outtype> is not 'file'"
109
  print ""
110
  print "  writeusbconsole file <file> <offset> <length>"
111
  print "    Writes the file <file> to the USB console."
112
  print "      Optional params <offset> <length>: specify the range in <file> to write"
113
  print "  writeusbconsole direct <i1> <i2> ... <iN>"
114
  print "    Writes the integers <i1> ... <iN> to the USB console."
119 benedikt93 115
  print "  writeusbconsole string <str>"
116
  print "    Writes the string <str> to the USB console."
82 benedikt93 117
  print ""
118
  print "  readdevconsole <bitmask> <size> <outtype> <file>"
119
  print "    Reads data from one or more of the device's consoles."
120
  print "      <bitmask>: the bitmask of the consoles to read from"
121
  print "      <size>: the number of bytes to read"
122
  print "      <outtype>: defines how to output the result:"
123
  print "        'file': writes the result to file <file>"
124
  print "        'printstring': writes the result as string to the console window"
125
  print "        'printhex': writes the result in hexedit notation to the console window"
126
  print "      <file>: the file to write the result to, can be omitted"
127
  print "               if <outtype> is not 'file'"
128
  print ""
129
  print "  writedevconsole file <bitmask> <file> <offset> <length>"
130
  print "    Writes the file <file> to the device consoles specified by <bitmask>"
131
  print "      Optional params <offset> <length>: specify the range in <file> to write"
132
  print "  writedevconsole direct <bitmask> <i1> <i2> ... <iN>"
133
  print "    Writes the integers <i1> ... <iN> to the device consoles specified"
134
  print "                         by <bitmask>"
119 benedikt93 135
  print "  writedevconsole string <bitmask> <str>"
136
  print "    Writes the string <str> to the device consoles specified by <bitmask>"
82 benedikt93 137
  print ""
138
  print "  flushconsolebuffers <bitmask>"
139
  print "    flushes one or more of the device consoles' buffers."
140
  print "      <bitmask>: the bitmask of the consoles to be flushed"
141
  print ""
142
  print ""
102 benedikt93 143
  print "  getprocessinformation / getprocinfo"
67 benedikt93 144
  print "    Fetches data on the currently running processes"
145
  print "     ATTENTION: this function will be print the information to the console window."
146
  print "                If several threads are running this might overflow the window,"
147
  print "                causing not everything to be shown."
148
  print ""
64 benedikt93 149
  print "  lockscheduler"
150
  print "    Locks (freezes) the scheduler"
151
  print ""
152
  print "  unlockscheduler"
153
  print "    Unlocks the scheduler"
154
  print ""
155
  print "  suspendthread <threadid>"
156
  print "    Suspends/resumes the thread with thread ID <threadid>"
157
  print ""
158
  print "  resumethread <threadid>"
159
  print "    Resumes the thread with thread ID <threadid>"
160
  print ""
161
  print "  killthread <threadid>"
162
  print "    Kills the thread with thread ID <threadid>"
163
  print ""
86 benedikt93 164
  print "  createthread <namepointer> <entrypoint> <stackpointer> <stacksize> <type> <priority> <state>"
64 benedikt93 165
  print "    Creates a new thread and returns its thread ID"
166
  print "      <namepointer> a pointer to the thread's name"
167
  print "      <entrypoint> a pointer to the entrypoint of the thread"
168
  print "      <stackpointer> a pointer to the stack of the thread"
169
  print "      <stacksize> the size of the thread's stack"
170
  print "      <type> the thread type, vaild are: 0 => user thread, 1 => system thread"
171
  print "      <priority> the priority of the thread, from 1 to 255"
172
  print "      <state> the thread's initial state, valid are: 1 => ready, 0 => suspended"
173
  print ""
94 benedikt93 174
  print "  execimage <offset>"
175
  print "    Executes the emBIOS executable image at <offset>."
82 benedikt93 176
  print ""
135 benedikt93 177
  print "  execfirmware <offset>"
178
  print "    Executes the firmware image at the specified address <offset>."
94 benedikt93 179
  print ""
135 benedikt93 180
  print ""
96 benedikt93 181
  print "  readrawbootflash <addr_bootflsh> <addr_mem> <size>"
182
  print "    Reads <size> bytes from bootflash to memory."
183
  print "      <addr_bootflsh>: the address in bootflash to read from"
184
  print "      <addr_mem>: the address in memory to copy the data to"
185
  print ""
186
  print "  writerawbootflash <addr_mem> <addr_bootflsh> <size>"
187
  print "    Writes <size> bytes from memory to bootflash."
188
  print "    Don't call this unless you really know what you're doing."
189
  print "      <addr_mem>: the address in memory to copy the data from"
190
  print "      <addr_bootflsh>: the address in bootflash to write to"
191
  print ""
192
  print ""
64 benedikt93 193
  print "  flushcaches"
194
  print "    Flushes the CPUs data and instruction caches."
195
  print ""
196
  print "All numbers are hexadecimal!"
197
  exit(2)
198
 
199
 
200
def parsecommand(dev, argv):
201
  if len(argv) < 2: usage()
202
 
203
  elif argv[1] == "getinfo":
204
    if len(argv) != 3: usage()
205
    dev.getinfo(argv[2])
206
 
207
  elif argv[1] == "reset":
208
    if len(argv) != 3: usage()
86 benedikt93 209
    dev.reset(int(argv[2]), 16)
64 benedikt93 210
 
211
  elif argv[1] == "poweroff":
212
    if len(argv) != 3: usage()
86 benedikt93 213
    dev.poweroff(int(argv[2]), 16)
64 benedikt93 214
 
82 benedikt93 215
 
216
  elif argv[1] == "uploadfile":
217
    if len(argv) < 4 or len(argv) > 6: usage()
218
    if len(argv) > 4:
86 benedikt93 219
      usedma = int(argv[4], 16)
82 benedikt93 220
      if len(argv) > 5:
86 benedikt93 221
        freezesched = int(argv[5], 16)
82 benedikt93 222
      else:
223
        freezesched = 0
224
    else:
225
      freezesched = 0
226
      usedma = 1
86 benedikt93 227
    dev.uploadfile(int(argv[2], 16), argv[3], usedma, freezesched)
82 benedikt93 228
 
229
  elif argv[1] == "downloadfile":
230
    if len(argv) < 5 or len(argv) > 7: usage()
231
    if len(argv) > 5:
86 benedikt93 232
      usedma = int(argv[5], 16)
82 benedikt93 233
      if len(argv) > 6:
86 benedikt93 234
        freezesched = int(argv[6], 16)
82 benedikt93 235
      else:
236
        freezesched = 0
237
    else:
238
      freezesched = 0
239
      usedma = 1
86 benedikt93 240
    dev.downloadfile(int(argv[2], 16), int(argv[3], 16), argv[4], usedma, freezesched)
82 benedikt93 241
 
242
  elif argv[1] == "uploadint":
243
    if len(argv) != 4: usage()
86 benedikt93 244
    dev.uploadint(int(argv[2], 16), int(argv[3], 16))
82 benedikt93 245
 
246
  elif argv[1] == "downloadint":
247
    if len(argv) != 3: usage()
86 benedikt93 248
    dev.downloadint(int(argv[2], 16))
82 benedikt93 249
 
250
 
67 benedikt93 251
  elif argv[1] == "i2cread":
252
    if len(argv) != 6: usage()
86 benedikt93 253
    dev.i2crecv(int(argv[2], 16), int(argv[3], 16), int(argv[4], 16), int(argv[5], 16))
64 benedikt93 254
 
67 benedikt93 255
  elif argv[1] == "i2csend":
256
    if len(argv) < 6: usage()
257
    data = ""
258
    ptr = 5
79 benedikt93 259
    while ptr < len(argv):
86 benedikt93 260
      data += struct.pack("<B", int(argv[ptr], 16))
67 benedikt93 261
      ptr += 1
86 benedikt93 262
    dev.i2csend(int(argv[2], 16), int(argv[3], 16), int(argv[4], 16), data)
82 benedikt93 263
 
264
 
265
  elif argv[1] == "readusbconsole":
266
    if len(argv) not in [4, 5]: usage()
267
    if len(argv) == 4: argv[4] = ""
86 benedikt93 268
    dev.readusbcon(int(argv[2], 16), argv[3], argv[4])
82 benedikt93 269
 
270
  elif argv[1] == "writeusbconsole":
271
    if len(argv) < 4: usage()
67 benedikt93 272
 
82 benedikt93 273
    if argv[2] == "file":
274
      f = open(argv[3], "rb")
275
      data = f.read()
276
 
277
      if len(argv) > 4:
86 benedikt93 278
        offset = int(argv[4], 16)
82 benedikt93 279
      else:
280
        offset = 0
281
      if len(argv) > 5:
86 benedikt93 282
        size = int(argv[5], 16)
82 benedikt93 283
      else:
284
        size = len(data)
285
      if len(argv) > 6: usage()
286
 
287
      dev.writeusbcon(data, 0, offset, size)
288
 
289
    if argv[2] == "direct":
290
      data = ""
291
      ptr = 3
292
      while ptr < len(argv):
86 benedikt93 293
        data += struct.pack("<I", int(argv[ptr], 16))
82 benedikt93 294
        ptr += 1
295
      dev.writeusbcon(data)
119 benedikt93 296
 
297
    if argv[2] == "string":
298
      if len(argv) > 4: usage()
299
      dev.writeusbcon(argv[3])
300
 
301
    else: usage()
82 benedikt93 302
 
303
  elif argv[1] == "readdevconsole":
304
    if len(argv) not in [5, 6]: usage()
305
    if len(argv) == 5: argv[5] = ""
86 benedikt93 306
    dev.readusbcon(int(argv[2], 16), int(argv[3], 16), argv[4], argv[5])
82 benedikt93 307
 
308
  elif argv[1] == "writedevconsole":
309
    if len(argv) < 5: usage()
310
 
311
    if argv[2] == "file":
312
      f = open(argv[4], "rb")
313
      data = f.read()
314
 
315
      if len(argv) > 5:
86 benedikt93 316
        offset = int(argv[5], 16)
82 benedikt93 317
      else:
318
        offset = 0
319
      if len(argv) > 6:
86 benedikt93 320
        size = int(argv[6], 16)
82 benedikt93 321
      else:
322
        size = len(data)
323
      if len(argv) > 7: usage()
324
 
86 benedikt93 325
      dev.writeusbcon(int(argv[3], 16), data, 0, offset, size)
82 benedikt93 326
 
327
    if argv[2] == "direct":
328
      data = ""
329
      ptr = 4
330
      while ptr < len(argv):
86 benedikt93 331
        data += struct.pack("<I", int(argv[ptr], 16))
82 benedikt93 332
        ptr += 1
86 benedikt93 333
      dev.writeusbcon(int(argv[3], 16), data)
119 benedikt93 334
 
335
    if argv[2] == "string":
336
      if len(argv) > 5: usage()
337
      dev.writedevcon(int(argv[3], 16), argv[4])
338
 
339
    else: usage()
82 benedikt93 340
 
341
  elif argv[1] == "flushconsolebuffers":
342
    if len(argv) != 3: usage()
86 benedikt93 343
    dev.flushconsolebuffers(int(argv[2], 16))
82 benedikt93 344
 
345
 
67 benedikt93 346
  elif argv[1] == "getprocessinformation" or argv[1] == "getprocinfo":
102 benedikt93 347
    if len(argv) != 2: usage()
348
    dev.getprocinfo()
67 benedikt93 349
 
64 benedikt93 350
  elif argv[1] == "lockscheduler":
351
    if len(argv) != 2: usage()
352
    dev.freezescheduler(1)
353
 
354
  elif argv[1] == "unlockscheduler":
355
    if len(argv) != 2: usage()
356
    dev.freezescheduler(0)
357
 
358
  elif argv[1] == "suspendthread":
359
    if len(argv) != 3: usage()
86 benedikt93 360
    dev.suspendthread(1, int(argv[2], 16))
64 benedikt93 361
 
362
  elif argv[1] == "resumethread":
363
    if len(argv) != 3: usage()
86 benedikt93 364
    dev.suspendthread(0, int(argv[2], 16))
64 benedikt93 365
 
366
  elif argv[1] == "killthread":
367
    if len(argv) != 3: usage()
86 benedikt93 368
    dev.killthread(int(argv[2], 16))
64 benedikt93 369
 
370
  elif argv[1] == "createthread":
371
    if len(argv) != 9: usage()
86 benedikt93 372
    dev.createthread(int(argv[2], 16), int(argv[3], 16), int(argv[4], 16), int(argv[5], 16), int(argv[6], 16), int(argv[7], 16), int(argv[8], 16))
64 benedikt93 373
 
94 benedikt93 374
  elif argv[1] == "execimage":
96 benedikt93 375
    if len(argv) != 3: usage()
94 benedikt93 376
    dev.execimage(int(argv[2], 16))
82 benedikt93 377
 
139 benedikt93 378
  elif argv[1] == "execfirmware":
379
    if len(argv) != 3: usage()
380
    dev.execfirmware(int(argv[2], 16))
94 benedikt93 381
 
96 benedikt93 382
  elif argv[1] == "readrawbootflash":
383
    if len(argv) != 5: usage()
97 benedikt93 384
    dev.readrawbootflash(int(argv[2], 16), int(argv[3], 16), int(argv[4], 16))
96 benedikt93 385
 
386
  elif argv[1] == "writerawbootflash":
387
    if len(argv) != 5: usage()
97 benedikt93 388
    dev.writerawbootflash(int(argv[2], 16), int(argv[3], 16), int(argv[4], 16))
389
 
390
 
67 benedikt93 391
  elif argv[1] == "flushcaches":
392
    if len(argv) != 2: usage()
393
    dev.flushcaches()
394
 
64 benedikt93 395
  else: usage()
396
 
397
dev = libembios.embios()
398
parsecommand(dev, sys.argv)