xref: /openbmc/u-boot/tools/mingw_support.c (revision a89c33db)
1 /*
2  * Copyright 2008 Extreme Engineering Solutions, Inc.
3  *
4  * mmap/munmap implementation derived from:
5  * Clamav Native Windows Port : mmap win32 compatibility layer
6  * Copyright (c) 2005-2006 Gianluigi Tiesi <sherpya@netfarm.it>
7  * Parts by Kees Zeelenberg <kzlg@users.sourceforge.net> (LibGW32C)
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Library General Public
11  * License as published by the Free Software Foundation; either
12  * version 2 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU Library General Public
20  * License along with this software; if not, write to the
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 #include "mingw_support.h"
25 #include <stdio.h>
26 #include <stdint.h>
27 #include <errno.h>
28 #include <io.h>
29 
30 int fsync(int fd)
31 {
32 	return _commit(fd);
33 }
34 
35 void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
36 {
37 	void *map = NULL;
38 	HANDLE handle = INVALID_HANDLE_VALUE;
39 	DWORD cfm_flags = 0, mvf_flags = 0;
40 
41 	switch (prot) {
42 	case PROT_READ | PROT_WRITE:
43 		cfm_flags = PAGE_READWRITE;
44 		mvf_flags = FILE_MAP_ALL_ACCESS;
45 		break;
46 	case PROT_WRITE:
47 		cfm_flags = PAGE_READWRITE;
48 		mvf_flags = FILE_MAP_WRITE;
49 		break;
50 	case PROT_READ:
51 		cfm_flags = PAGE_READONLY;
52 		mvf_flags = FILE_MAP_READ;
53 		break;
54 	default:
55 		return MAP_FAILED;
56 	}
57 
58 	handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
59 				cfm_flags, HIDWORD(len), LODWORD(len), NULL);
60 	if (!handle)
61 		return MAP_FAILED;
62 
63 	map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
64 			LODWORD(offset), len);
65 	CloseHandle(handle);
66 
67 	if (!map)
68 		return MAP_FAILED;
69 
70 	return map;
71 }
72 
73 int munmap(void *addr, size_t len)
74 {
75 	if (!UnmapViewOfFile(addr))
76 		return -1;
77 
78 	return 0;
79 }
80