def create_simg(contents, priority=0, daddr=0, skip_crc32=False, align=False, version=None): """Create an SIMG version of a file. >>> from cxmanage_api.simg import create_simg >>> simg = create_simg(contents='foobarbaz') >>> simg 'SIMG\\x02\\x00\\x00\\x00<\\x00\\x00\\x00\\t\\x00\\x00\\x00\\x00\\x00\\x00 \\x00\\xff\\xff\\xff\\xffK\\xf3\\xea\\x0c\\x00\\x00\\x00\\x00\\x00\\x00 \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00 \\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00foobarbaz' :param contents: Contents of the SIMG file. :type contents: string :param priority: SIMG Header priority value. :type priority: integer :param daddr: SIMG Header daddr value. :type daddr: integer :param skip_crc32: Flag to skip crc32 calculating. :type skip_crc32: boolean :param align: Flag used to turn on/off image offset of 4096. :type align: boolean :param version: Version string. :type version: string :returns: String representation of the SIMG file. :rtype: string """ if (version == None): version = '' header = SIMGHeader() header.priority = priority header.imglen = len(contents) header.daddr = daddr header.version = version if (align): header.imgoff = 4096 # Calculate crc value if (skip_crc32): crc32 = 0 else: crc32 = get_crc32(contents, get_crc32(str(header)[:MIN_HEADER_LENGTH])) # Get SIMG header header.flags = 0xFFFFFFFF header.crc32 = crc32 return str(header).ljust(header.imgoff, chr(0)) + contents
def get_contents(self): """Returns a raw string representation of the uboot environment. >>> uboot.get_contents() 'j4\x88\xb7bootcmd_default=run bootcmd_sata; run bootcmd_pxe ... ' >>> # >>> # Output trimmed for brevity ... >>> # :returns: Raw string representation of the UBoot Environment. :rtype: string """ contents = "" # Add variables for variable in self.variables: contents += "%s=%s\0" % (variable, self.variables[variable]) contents += "\0" # Add padding to end contents += "".join( [chr(255) for _ in range(ENVIRONMENT_SIZE - len(contents) - 4)]) # Add crc32 to beginning crc32 = get_crc32(contents, 0xFFFFFFFF) ^ 0xFFFFFFFF contents = struct.pack("<I", crc32) + contents return contents
def get_contents(self): """Returns a raw string representation of the uboot environment. >>> uboot.get_contents() 'j4\x88\xb7bootcmd_default=run bootcmd_sata; run bootcmd_pxe ... ' >>> # >>> # Output trimmed for brevity ... >>> # :returns: Raw string representation of the UBoot Environment. :rtype: string """ contents = "" # Add variables for variable in self.variables: contents += "%s=%s\0" % (variable, self.variables[variable]) contents += "\0" # Add padding to end contents += "".join([chr(255) for _ in range(ENVIRONMENT_SIZE - len(contents) - 4)]) # Add crc32 to beginning crc32 = get_crc32(contents, 0xFFFFFFFF) ^ 0xFFFFFFFF contents = struct.pack("<I", crc32) + contents return contents
def valid_simg(simg): """Return true if this is a valid SIMG. >>> from cxmanage_api.simg import create_simg >>> simg=create_simg(contents='foobarbaz') >>> from cxmanage_api.simg import valid_simg >>> valid_simg(simg=simg) True :param simg: SIMG string (representation of a SIMG file). :type simg: string :returns: Whether or not the SIMG is valid. :rtype: boolean """ if (not has_simg(simg)): return False header = SIMGHeader(simg[:HEADER_LENGTH]) # Check offset if (header.imgoff < MIN_HEADER_LENGTH): return False # Check length start = header.imgoff end = start + header.imglen contents = simg[start:end] if (len(contents) < header.imglen): return False # Check crc32 crc32 = header.crc32 if (crc32 != 0): header.flags = 0 header.crc32 = 0 if (crc32 != get_crc32(contents, get_crc32(str(header)[:MIN_HEADER_LENGTH]))): return False return True