arch/arm64: Support FEAT_CCIDX
[coreboot2.git] / util / nvramtool / win32mmap.c
blobf44dec88fc817d5a97a083a93b1afc038e72caf2
1 #include "common.h"
2 #include <windows.h>
4 static inline size_t xsize_t(off_t len)
6 if (len > (size_t) len)
7 die("Cannot handle files this big");
8 return (size_t)len;
11 void *win32_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
13 HANDLE hmap;
14 void *temp;
15 off_t len;
16 struct stat st;
17 uint64_t o = offset;
18 uint32_t l = o & 0xFFFFFFFF;
19 uint32_t h = (o >> 32) & 0xFFFFFFFF;
21 if (!fstat(fd, &st))
22 len = st.st_size;
23 else
24 printf("mmap: could not determine filesize");
26 if ((length + offset) > len)
27 length = xsize_t(len - offset);
29 if (!(flags & MAP_PRIVATE))
30 printf("Invalid usage of mmap when built with USE_WIN32_MMAP");
32 hmap = CreateFileMapping((HANDLE)_get_osfhandle(fd), 0, PAGE_WRITECOPY,
33 0, 0, 0);
35 if (!hmap)
36 return MAP_FAILED;
38 temp = MapViewOfFileEx(hmap, FILE_MAP_COPY, h, l, length, start);
40 if (!CloseHandle(hmap))
41 printf("unable to close file mapping handle");
43 return temp ? temp : MAP_FAILED;
46 int win32_munmap(void *start, size_t length)
48 return !UnmapViewOfFile(start);