freemyipod r178 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r177‎ | r178 | r179 >
Date:23:26, 26 August 2010
Author:farthen
Status:new
Tags:
Comment:
libembios/embios.py: make some lowlevel r/w functions private, fix a silly bug
Modified paths:
  • /embios/trunk/tools/embios.py (modified) (history)
  • /embios/trunk/tools/libembios.py (modified) (history)

Diff [purge]

Index: embios/trunk/tools/embios.py
@@ -325,6 +325,7 @@
326326 self.logger.info("Writing file '"+filename+"' to memory at "+self._hex(addr)+"...")
327327 with f:
328328 self.embios.write(addr, f.read())
 329+ f.close()
329330 self.logger.info("done\n")
330331
331332 @command
@@ -344,6 +345,7 @@
345346 self.logger.info("Reading data from address "+self._hex(addr)+" with the size "+self._hex(size)+" to '"+filename+"'...")
346347 with f:
347348 f.write(self.embios.read(addr, size))
 349+ f.close()
348350 self.logger.info("done\n")
349351
350352 @command
@@ -358,7 +360,7 @@
359361 if integer > 0xFFFFFFFF:
360362 raise ArgumentError("Specified integer too long")
361363 data = chr(integer)
362 - self.embios.writemem(addr, data)
 364+ self.embios.write(addr, data)
363365 self.logger.info("Integer '"+self._hex(integer)+"' written successfully to "+self._hex(addr))
364366
365367 @command
@@ -368,7 +370,7 @@
369371 <offset>: the address to download the integer from
370372 """
371373 addr = self._hexint(addr)
372 - data = self.embios.readmem(addr, 1)
 374+ data = self.embios.read(addr, 1)
373375 integer = ord(data)
374376 self.logger.info("Integer '"+self._hex(integer)+"' read from address "+self._hex(addr))
375377
@@ -495,7 +497,7 @@
496498 """
497499 self.logger.info("Will now lock scheduler\n")
498500 self.embios.lockscheduler()
499 -
 501+
500502 @command
501503 def unlockscheduler(self):
502504 """
@@ -503,7 +505,7 @@
504506 """
505507 self.logger.info("Will now unlock scheduler\n")
506508 self.embios.unlockscheduler()
507 -
 509+
508510 @command
509511 def suspendthread(self, threadid):
510512 """
@@ -664,7 +666,7 @@
665667 " and saving it to "+self._hex(destination)+" - "+self._hex(destination+sha1size)+"...")
666668 self.embios.hmac_sha1(addr, size, destination)
667669 self.logger.info("done\n")
668 - data = self.embios.readmem(destination, sha1size)
 670+ data = self.embios.read(destination, sha1size)
669671 hash = ord(data)
670672 self.logger.info("The generated hash is "+self._hex(hash))
671673
Index: embios/trunk/tools/libembios.py
@@ -88,6 +88,33 @@
8989 tailsize = end - tailaddr
9090 return (headsize, tailaddr - bodyaddr, tailsize)
9191
 92+ def _readmem(self, addr, size):
 93+ """ Reads the memory from location 'addr' with size 'size'
 94+ from the device.
 95+ """
 96+ resp = self.lib.monitorcommand(struct.pack("IIII", 4, addr, size, 0), "III%ds" % size, (None, None, None, "data"))
 97+ return resp.data
 98+
 99+ def _writemem(self, addr, data):
 100+ """ Writes the data in 'data' to the location 'addr'
 101+ in the memory of the device.
 102+ """
 103+ return self.lib.monitorcommand(struct.pack("IIII%ds" % len(data), 5, addr, len(data), 0, data), "III", (None, None, None))
 104+
 105+ def _readdma(self, addr, size):
 106+ """ Reads the memory from location 'addr' with size 'size'
 107+ from the device. This uses DMA and the data in endpoint.
 108+ """
 109+ self.lib.monitorcommand(struct.pack("IIII", 6, addr, size, 0), "III", (None, None, None))
 110+ return struct.unpack("%ds" % size, self.lib.dev.din(size))[0]
 111+
 112+ def _writedma(self, addr, data):
 113+ """ Writes the data in 'data' to the location 'addr'
 114+ in the memory of the device. This uses DMA and the data out endpoint.
 115+ """
 116+ self.lib.monitorcommand(struct.pack("IIII", 7, addr, len(data), 0), "III", (None, None, None))
 117+ return self.lib.dev.dout(data)
 118+
92119 def getversioninfo(self):
93120 """ This returns the emBIOS version and device information. """
94121 return self.lib.monitorcommand(struct.pack("IIII", 1, 0, 0, 0), "IBBBBI", ("revision", "majorv", "minorv", "patchv", "swtypeid", "hwtypeid"))
@@ -131,19 +158,19 @@
132159 data = ""
133160 (headsize, bodysize, tailsize) = self._alignsplit(addr, size, cin_maxsize, 16)
134161 if headsize != 0:
135 - data += self.readmem(addr, headsize)
 162+ data += self._readmem(addr, headsize)
136163 addr += headsize
137164 while bodysize > 0:
138165 if bodysize >= 2 * cin_maxsize:
139166 readsize = min(bodysize, din_maxsize)
140 - data += self.readdma(addr, readsize)
 167+ data += self._readdma(addr, readsize)
141168 else:
142169 readsize = min(bodysize, cin_maxsize)
143 - data += self.readmem(addr, readsize)
 170+ data += self._readmem(addr, readsize)
144171 addr += readsize
145172 bodysize -= readsize
146173 if tailsize != 0:
147 - data += self.readmem(addr, tailsize)
 174+ data += self._readmem(addr, tailsize)
148175 return data
149176
150177 def write(self, addr, data):
@@ -156,50 +183,23 @@
157184 (headsize, bodysize, tailsize) = self._alignsplit(addr, len(data), cout_maxsize, 16)
158185 offset = 0
159186 if headsize != 0:
160 - self.writemem(addr, data[offset:offset+headsize])
 187+ self._writemem(addr, data[offset:offset+headsize])
161188 offset += headsize
162189 addr += headsize
163190 while bodysize > 0:
164191 if bodysize >= 2 * cout_maxsize:
165192 writesize = min(bodysize, dout_maxsize)
166 - self.writedma(addr, data[offset:offset+writesize])
 193+ self._writedma(addr, data[offset:offset+writesize])
167194 else:
168195 writesize = min(bodysize, cout_maxsize)
169 - self.writemem(addr, data[offset:offset+writesize])
 196+ self._writemem(addr, data[offset:offset+writesize])
170197 offset += writesize
171198 addr += writesize
172199 bodysize -= writesize
173200 if tailsize != 0:
174 - self.writemem(addr, data[offset:offset+tailsize])
 201+ self._writemem(addr, data[offset:offset+tailsize])
175202 return data
176203
177 - def readmem(self, addr, size):
178 - """ Reads the memory from location 'addr' with size 'size'
179 - from the device.
180 - """
181 - resp = self.lib.monitorcommand(struct.pack("IIII", 4, addr, size, 0), "III%ds" % size, (None, None, None, "data"))
182 - return resp.data
183 -
184 - def writemem(self, addr, data):
185 - """ Writes the data in 'data' to the location 'addr'
186 - in the memory of the device.
187 - """
188 - return self.lib.monitorcommand(struct.pack("IIII%ds" % len(data), 5, addr, len(data), 0, data), "III", (None, None, None))
189 -
190 - def readdma(self, addr, size):
191 - """ Reads the memory from location 'addr' with size 'size'
192 - from the device. This uses DMA and the data in endpoint.
193 - """
194 - self.lib.monitorcommand(struct.pack("IIII", 6, addr, size, 0), "III", (None, None, None))
195 - return struct.unpack("%ds" % size, self.lib.dev.din(size))[0]
196 -
197 - def writedma(self, addr, data):
198 - """ Writes the data in 'data' to the location 'addr'
199 - in the memory of the device. This uses DMA and the data out endpoint.
200 - """
201 - self.lib.monitorcommand(struct.pack("IIII", 7, addr, len(data), 0), "III", (None, None, None))
202 - return self.lib.dev.dout(data)
203 -
204204 def readstring(self, addr, maxlength = 256):
205205 """ Reads a zero terminated string from memory
206206 Reads only a maximum of 'maxlength' chars.
@@ -207,7 +207,7 @@
208208 cin_maxsize = self.lib.dev.packetsizelimit["cin"] - self.lib.headersize
209209 string = ""
210210 while (len(string) < maxlength or maxlength < 0):
211 - data = self.readmem(addr, min(maxlength - len(string), cin_maxsize))
 211+ data = self._readmem(addr, min(maxlength - len(string), cin_maxsize))
212212 length = data.find("\0")
213213 if length >= 0:
214214 string += data[:length]
@@ -342,9 +342,6 @@
343343 threads.append(info)
344344 id += 1
345345 return threads
346 -
347 -
348 - return self.lib.monitorcommand(struct.pack("IIII", 15, offset, size, 0), "III%ds" % size, ("structver", "tablesize", None, "data"))
349346
350347 def lockscheduler(self, freeze=True):
351348 """ Freezes/Unfreezes the scheduler """