]> oss.titaniummirror.com Git - tinyos-2.x.git/blobdiff - tools/tinyos/misc/tos-build-deluge-image
Super Duper update to Deluge T2. The manual and the testing scripts are temporary...
[tinyos-2.x.git] / tools / tinyos / misc / tos-build-deluge-image
index 1219fa464c3d9b58341cd9a8519f36efac787479..085424011b42e7cccfec016c81ff3a3b68e7e435 100755 (executable)
@@ -31,7 +31,6 @@ DELUGE_PKT_PAYLOAD_SIZE = 23
 DELUGE_BYTES_PER_PAGE = DELUGE_PKTS_PER_PAGE * DELUGE_PKT_PAYLOAD_SIZE
 DELUGE_MAX_PAGES = 128
 
-DELUGE_METADATA_SIZE = 16
 DELUGE_IDENT_SIZE = 128
 
 def sencode(s, dim):
@@ -40,19 +39,15 @@ def sencode(s, dim):
     return s[:dim]
   return s + [0] * (dim - len(s))
 
+# Encode to big endian
 def encode(val, dim):
   output = []
   for i in range(dim):
       output.append(val & 0xFF)
       val = val >> 8
+  output.reverse()
   return output
 
-def decode(v):
-  r = long(0)
-  for i in v[::-1]:
-      r = (r << 8) + i
-  return r
-
 def int2byte(v):
     return "".join([struct.pack("B", i) for i in v])
 
@@ -69,8 +64,11 @@ def crc16(data):
   return crc
 
 def pages(data):
-    return (len(data) / DELUGE_BYTES_PER_PAGE) + \
-               (len(data) % DELUGE_BYTES_PER_PAGE != 0)
+  l = len(data) - 2*DELUGE_MAX_PAGES
+  if l % DELUGE_BYTES_PER_PAGE != 0:
+    sys.stderr.write("ERROR: Bug in padding!")
+    sys.exit()
+  return l / DELUGE_BYTES_PER_PAGE
 
 def align(data):
   mod = len(data) % DELUGE_BYTES_PER_PAGE
@@ -78,50 +76,35 @@ def align(data):
     return data
   return data + [0] * (DELUGE_BYTES_PER_PAGE - mod)
 
-def deluge_metadata(data, img_num):
-  uid = ident['uid_hash']
-  num_pages = pages(data)
-  image_number = img_num
-  crc = crc16([image_number] + [num_pages])
-  completed_pages = num_pages
-  return encode(uid, 4) + \
-         encode(version, 2) + \
-         [image_number] + \
-         [num_pages] + \
-         encode(crc, 2) + \
-         [completed_pages] + \
-         [0] + \
-         encode(len(data) + 16, 2) + \
-         ([0] * 2)
-
-def deluge_ident():
-  tmp = sencode(ident['program_name'], 16) + \
-        sencode(ident['user_id'], 16) + \
-        sencode(ident['hostname'], 16) + \
-        sencode(ident['platform'], 16) + \
+def deluge_ident(data):
+  tmp = encode(ident['uidhash'], 4) + \
         encode(ident['size'], 4) + \
-        encode(ident['unix_time'], 4) + \
-        encode(ident['user_hash'], 4) + \
-        encode(ident['uid_hash'], 4)
+        [pages(data), 0]
+  crc = crc16(tmp)
+  tmp += encode(crc, 2) + \
+         sencode(ident['appname'], 16) + \
+         sencode(ident['username'], 16) + \
+         sencode(ident['hostname'], 16) + \
+         sencode(ident['platform'], 16) + \
+         encode(ident['timestamp'], 4) + \
+         encode(ident['userhash'], 4)
   return tmp + [0] * (DELUGE_IDENT_SIZE - len(tmp))
 
 def deluge_crc(data):
   crc = [0] * DELUGE_MAX_PAGES
-  crc[0] = crc16(data[2*DELUGE_MAX_PAGES:DELUGE_BYTES_PER_PAGE])
-  #sys.stderr.write("crc[0] = 0x%x\n" % (crc[0]))
-  j = 1
-  for i in range(DELUGE_BYTES_PER_PAGE, len(data)-1, DELUGE_BYTES_PER_PAGE):
+  j = 0
+  sys.stderr.write("CRCs:\n  ")
+  for i in range(0, len(data)-1, DELUGE_BYTES_PER_PAGE):
     crc[j] = crc16(data[i:i+DELUGE_BYTES_PER_PAGE])
-    #sys.stderr.write("crc[%d] = 0x%x\n" % (j, crc[j]))
+    sys.stderr.write("0x%04X " % (crc[j]))
+    if (j + 1) % 7 == 0:
+      sys.stderr.write("\n  ")
     j += 1
-  return reduce(operator.add, [encode(i, 2) for i in crc]) + data[2*DELUGE_MAX_PAGES:]
-
-version = 0
+  sys.stderr.write("\n")
+  return reduce(operator.add, [encode(i, 2) for i in crc]) + data
 
 for i in range(len(sys.argv)):
-  if sys.argv[i] == '-v':
-    version = int(sys.argv[i+1])
-  elif sys.argv[i] == '-i':
+  if sys.argv[i] == '-i':
     img_num = int(sys.argv[i+1])
     
 dom = parse(sys.argv[-1])
@@ -130,7 +113,7 @@ ident_list = [(n.localName, n.firstChild.nodeValue)
               for n in dom.getElementsByTagName('ident')[0].childNodes if n.localName != None]
 for (k, v) in ident_list:
   ident[k] = v
-for p in ['unix_time', 'user_hash', 'uid_hash']:
+for p in ['timestamp', 'userhash', 'uidhash']:
   ident[p] = int(ident[p][:-1], 16)
 
 error = "ERROR: getting the image from the XML file failed."
@@ -178,8 +161,9 @@ for line in image.split():
         start_addr = addr
 
 sys.stderr.write('Ihex read complete:\n')
-sys.stderr.write('  Total bytes = %d\n' % reduce(operator.add, [len(l) for (_, l) in all]))
-sys.stderr.write('  Sections = %d\n' % len(all))
+sys.stderr.write('  ' + '\n  '.join(["%5d bytes starting at 0x%X" % (len(l), a) for (a, l) in all]))
+sys.stderr.write('\n')
+sys.stderr.write('  %d bytes in %d sections\n' % (reduce(operator.add, [len(l) for (_, l) in all]), len(all)))
 
 # Usually, there are two sections: one for the code and one for the
 # interrupt vector.
@@ -189,10 +173,10 @@ for (addr, data) in all:
   all_data += encode(addr, 4) + \
               encode(len(data), 4) + \
               data
-ident['size'] = len(all_data)
-all_data = deluge_ident() + all_data
-all_data = align([0] * 2 * DELUGE_MAX_PAGES + all_data)
+padding = [0] * (DELUGE_BYTES_PER_PAGE - len(all_data) % DELUGE_BYTES_PER_PAGE)
+if len(padding) < DELUGE_BYTES_PER_PAGE:
+  all_data += padding
 all_data = deluge_crc(all_data)
-sys.stdout.write(int2byte(deluge_metadata(all_data, img_num)) + \
-      int2byte(all_data))
+ident['size'] = DELUGE_IDENT_SIZE + len(all_data)
+sys.stdout.write(int2byte(deluge_ident(all_data)) + int2byte(all_data))