Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 1 | /* intelmetool |
| 2 | * |
| 3 | * Copyright (C) 2013-2015 Damien Zammit <damien@zamaudio.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License as |
| 7 | * published by the Free Software Foundation; either version 2 of |
Damien Zammit | f0a9128 | 2019-02-23 12:38:15 +1100 | [diff] [blame] | 8 | * the License, or (at your option) any later version. |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
| 16 | #include "mmap.h" |
| 17 | #include <errno.h> |
Paul Wise | 3c02699 | 2017-05-04 14:12:21 +0800 | [diff] [blame] | 18 | #include <string.h> |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 19 | |
| 20 | #ifndef __DARWIN__ |
| 21 | int fd_mem; |
| 22 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 23 | void *map_physical_exact(off_t phys_addr, void *mapto, size_t len) |
| 24 | { |
| 25 | void *virt_addr; |
| 26 | int err; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 27 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 28 | virt_addr = mmap(mapto, len, PROT_WRITE | PROT_READ, |
| 29 | MAP_SHARED | MAP_FIXED, fd_mem, phys_addr); |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 30 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 31 | if (virt_addr == MAP_FAILED) { |
| 32 | err = errno; |
| 33 | printf("Error mapping physical memory 0x%016jd [0x%zx] ERRNO=%d %s\n", |
| 34 | (intmax_t)phys_addr, len, err, strerror(err)); |
| 35 | return NULL; |
| 36 | } |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 37 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 38 | return virt_addr; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 39 | } |
| 40 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 41 | void *map_physical(off_t phys_addr, size_t len) |
| 42 | { |
| 43 | void *virt_addr; |
| 44 | int err; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 45 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 46 | virt_addr = mmap(NULL, len, PROT_WRITE | PROT_READ, MAP_SHARED, fd_mem, phys_addr); |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 47 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 48 | if (virt_addr == MAP_FAILED) { |
| 49 | err = errno; |
| 50 | printf("Error mapping physical memory 0x%016jd [0x%zx] ERRNO=%d %s\n", |
| 51 | (intmax_t)phys_addr, len, err, strerror(err)); |
| 52 | return NULL; |
| 53 | } |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 54 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 55 | return virt_addr; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 56 | } |
| 57 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 58 | void unmap_physical(void *virt_addr, size_t len) |
| 59 | { |
| 60 | munmap(virt_addr, len); |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 61 | } |
| 62 | #endif |