Search code examples
cmemoryuefignu-efi

How to dump raw memory into a struct fast?


I'm in an identity-mapped memory context (UEFI on x86_64 platform) and I want to dump some contiguous memory content into a structure. Say my structure has this shape:

typedef struct _mystr {
    char char_arr[7];
    uint32_t u_d;
    uint8_t u_b;
} __attribute__((packed)) mystr;

Supposing I have in mem_ptr the address to the requested memory's offset 0, what's the best way to copy its content into a mystr instance? Is there a way to do that without iterating through memory with a loop (which seems super boring)?


EDIT: Nicolas Jean suggested the use of memcpy but unfortunately in an EFI developing context the use of a C standard library makes little sense. However, efilib.h from gnu-efi has CopyMem(IN VOID * dst, IN CONST VOID * src, IN UINTN len) which carries out the same task.


Solution

  • Copying the memory content with memcpy is probably what you are looking for.

    mystr mystr_instance;
    memcpy(&mystr_instance, mem_ptr, sizeof(mystr_instance));