parameters: (buf)
return_statement: return output
docstring: Return dictionary of addr=content blocks.
docstring_summary: Return dictionary of addr=content blocks.
docstring_tokens: Return dictionary of addr = content blocks .
function: def convert_from_uf2(buf):
      """Return dictionary of addr=content blocks."""
      global familyid
      numblocks = len(buf) // UF2_BLOCK_SIZE
      if verbose:
          print("File size %u bytes, %u blocks" % (len(buf), numblocks))
      chunk_offset = None
      curraddr = None
      currfamilyid = None
      families_found = {}
      prev_flag = None
      all_flags_same = True
  
      output = {}
      if verbose:
          print("#Block  Magic 0     Magic 1     flags       address     payload sz  blockno     numblocks   family/sz")
          print("------  ----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------")
  
      for blockno in range(numblocks):
          offset = blockno * UF2_BLOCK_SIZE
          block = buf[offset:offset + UF2_BLOCK_SIZE]
          hd = struct.unpack(b"<IIIIIIII", block[0:UF2_HEADER_SIZE])
          if verbose:
              s = "  ".join("0x%08x" % e for e in hd)
              print("%6u  %s" % (blockno, s))
          if hd[0] != UF2_MAGIC_START0 or hd[1] != UF2_MAGIC_START1:
              print("Skipping block at " + offset + ", bad magic")
              continue
          if hd[2] & 1:
              # NO-flash flag set; skip block
              continue
          datalen = hd[4]
          if datalen > UF2_MAX_PAYLOAD_SIZE:
              error("Invalid UF2 data size at 0x%08x" % offset)
          newaddr = hd[3]
          if chunk_offset is None or curraddr != newaddr:
              chunk_offset = newaddr - base_address
              output[chunk_offset] = bytes()
          if (hd[2] & UF2_FLAG_FAMILY_ID) and currfamilyid is None:
              currfamilyid = hd[7]
          if curraddr is None or ((hd[2] & UF2_FLAG_FAMILY_ID) and hd[7] != currfamilyid):
              currfamilyid = hd[7]
              curraddr = newaddr
          if familyid == 0x0 or ((hd[2] & UF2_FLAG_FAMILY_ID) and familyid == hd[7]):
              output[chunk_offset] += block[UF2_HEADER_SIZE : UF2_HEADER_SIZE + datalen]
          curraddr = newaddr + datalen
          if hd[2] & UF2_FLAG_FAMILY_ID:
              if hd[7] in families_found.keys():
                  if families_found[hd[7]] > newaddr:
                      families_found[hd[7]] = newaddr
              else:
                  families_found[hd[7]] = newaddr
          if prev_flag is None:
              prev_flag = hd[2]
          if prev_flag != hd[2]:
              all_flags_same = False
  
      print("----- UF2 File Header Info -----")
      families = load_families()
      for family_hex in families_found.keys():
          family_short_name = ""
          for name, value in families.items():
              if value == family_hex:
                  family_short_name = name
          print("Family ID is %s, hex value is 0x%08x" % (family_short_name, family_hex))
          print("Target Address is 0x%08x" % families_found[family_hex])
      if all_flags_same:
          print("All block flag values consistent, 0x%04x" % hd[2])
      else:
          print("Flags were not all the same")
      print("  Offset      Size")
      for addr, content in output.items():
          print("  0x%08x  0x%06x  %u" % (addr, len(content), len(content)))
      print("--------------------------------")
      if len(families_found) > 1 and familyid == 0x0:
          output = []
  
      return output
function_tokens: def convert_from_uf2 ( buf ) : global familyid numblocks = len ( buf ) // UF2_BLOCK_SIZE if verbose : print ( "File size %u bytes, %u blocks" % ( len ( buf ) , numblocks ) ) chunk_offset = None curraddr = None currfamilyid = None families_found = { } prev_flag = None all_flags_same = True output = { } if verbose : print ( "#Block  Magic 0     Magic 1     flags       address     payload sz  blockno     numblocks   family/sz" ) print ( "------  ----------  ----------  ----------  ----------  ----------  ----------  ----------  ----------" ) for blockno in range ( numblocks ) : offset = blockno * UF2_BLOCK_SIZE block = buf [ offset : offset + UF2_BLOCK_SIZE ] hd = struct . unpack ( b"<IIIIIIII" , block [ 0 : UF2_HEADER_SIZE ] ) if verbose : s = "  " . join ( "0x%08x" % e for e in hd ) print ( "%6u  %s" % ( blockno , s ) ) if hd [ 0 ] != UF2_MAGIC_START0 or hd [ 1 ] != UF2_MAGIC_START1 : print ( "Skipping block at " + offset + ", bad magic" ) continue if hd [ 2 ] & 1 : # NO-flash flag set; skip block continue datalen = hd [ 4 ] if datalen > UF2_MAX_PAYLOAD_SIZE : error ( "Invalid UF2 data size at 0x%08x" % offset ) newaddr = hd [ 3 ] if chunk_offset is None or curraddr != newaddr : chunk_offset = newaddr - base_address output [ chunk_offset ] = bytes ( ) if ( hd [ 2 ] & UF2_FLAG_FAMILY_ID ) and currfamilyid is None : currfamilyid = hd [ 7 ] if curraddr is None or ( ( hd [ 2 ] & UF2_FLAG_FAMILY_ID ) and hd [ 7 ] != currfamilyid ) : currfamilyid = hd [ 7 ] curraddr = newaddr if familyid == 0x0 or ( ( hd [ 2 ] & UF2_FLAG_FAMILY_ID ) and familyid == hd [ 7 ] ) : output [ chunk_offset ] += block [ UF2_HEADER_SIZE : UF2_HEADER_SIZE + datalen ] curraddr = newaddr + datalen if hd [ 2 ] & UF2_FLAG_FAMILY_ID : if hd [ 7 ] in families_found . keys ( ) : if families_found [ hd [ 7 ] ] > newaddr : families_found [ hd [ 7 ] ] = newaddr else : families_found [ hd [ 7 ] ] = newaddr if prev_flag is None : prev_flag = hd [ 2 ] if prev_flag != hd [ 2 ] : all_flags_same = False print ( "----- UF2 File Header Info -----" ) families = load_families ( ) for family_hex in families_found . keys ( ) : family_short_name = "" for name , value in families . items ( ) : if value == family_hex : family_short_name = name print ( "Family ID is %s, hex value is 0x%08x" % ( family_short_name , family_hex ) ) print ( "Target Address is 0x%08x" % families_found [ family_hex ] ) if all_flags_same : print ( "All block flag values consistent, 0x%04x" % hd [ 2 ] ) else : print ( "Flags were not all the same" ) print ( "  Offset      Size" ) for addr , content in output . items ( ) : print ( "  0x%08x  0x%06x  %u" % ( addr , len ( content ) , len ( content ) ) ) print ( "--------------------------------" ) if len ( families_found ) > 1 and familyid == 0x0 : output = [ ] return output