Patrick Georgi | afd4c87 | 2020-05-05 23:43:18 +0200 | [diff] [blame] | 1 | /* intelmetool */ |
Patrick Georgi | ac95903 | 2020-05-05 22:49:26 +0200 | [diff] [blame] | 2 | /* SPDX-License-Identifier: GPL-2.0-or-later */ |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 3 | |
| 4 | #include "mmap.h" |
| 5 | #include <errno.h> |
Paul Wise | 3c02699 | 2017-05-04 14:12:21 +0800 | [diff] [blame] | 6 | #include <string.h> |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 7 | |
| 8 | #ifndef __DARWIN__ |
| 9 | int fd_mem; |
| 10 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 11 | void *map_physical_exact(off_t phys_addr, void *mapto, size_t len) |
| 12 | { |
| 13 | void *virt_addr; |
| 14 | int err; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 15 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 16 | virt_addr = mmap(mapto, len, PROT_WRITE | PROT_READ, |
| 17 | MAP_SHARED | MAP_FIXED, fd_mem, phys_addr); |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 18 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 19 | if (virt_addr == MAP_FAILED) { |
| 20 | err = errno; |
Mate Kukri | 5c769ab | 2024-03-17 18:26:28 +0000 | [diff] [blame] | 21 | printf("Error mapping physical memory 0x%016jx [0x%zx] ERRNO=%d %s\n", |
| 22 | (uintmax_t)phys_addr, len, err, strerror(err)); |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 23 | return NULL; |
| 24 | } |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 25 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 26 | return virt_addr; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 27 | } |
| 28 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 29 | void *map_physical(off_t phys_addr, size_t len) |
| 30 | { |
| 31 | void *virt_addr; |
| 32 | int err; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 33 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 34 | 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] | 35 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 36 | if (virt_addr == MAP_FAILED) { |
| 37 | err = errno; |
Mate Kukri | 5c769ab | 2024-03-17 18:26:28 +0000 | [diff] [blame] | 38 | printf("Error mapping physical memory 0x%016jx [0x%zx] ERRNO=%d %s\n", |
| 39 | (uintmax_t)phys_addr, len, err, strerror(err)); |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 40 | return NULL; |
| 41 | } |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 42 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 43 | return virt_addr; |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 44 | } |
| 45 | |
Maximilian Schander | df5b83f | 2017-10-28 18:33:07 +0200 | [diff] [blame] | 46 | void unmap_physical(void *virt_addr, size_t len) |
| 47 | { |
| 48 | munmap(virt_addr, len); |
Philipp Deppenwiese | d8fe443 | 2016-03-18 00:52:54 +0100 | [diff] [blame] | 49 | } |
| 50 | #endif |