1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Copyright (C) 2018 Pengutronix, Uwe Kleine-König <oss-tools@pengutronix.de>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <unistd.h>
#include "fileaccpriv.h"
#define container_of(ptr, type, member) \
(type *)((char *)(ptr) - (char *) &((type *)0)->member)
static off_t mmap_pagesize(void) __attribute__((const));
static off_t mmap_pagesize(void)
{
static off_t pagesize;
if (pagesize == 0)
pagesize = sysconf(_SC_PAGE_SIZE);
if (pagesize == 0)
pagesize = 4096;
return pagesize;
}
struct memtool_mmap_fd {
struct memtool_fd mfd;
struct stat s;
int fd;
};
static ssize_t mmap_read(struct memtool_fd *handle, off_t offset,
void *buf, size_t nbytes, int width)
{
struct memtool_mmap_fd *mmap_fd =
container_of(handle, struct memtool_mmap_fd, mfd);
struct stat *s = &mmap_fd->s;
off_t map_start, map_off;
void *map;
size_t i = 0;
int ret;
if (S_ISREG(s->st_mode)) {
if (s->st_size <= offset) {
errno = EINVAL;
perror("File to small");
return -1;
}
if (s->st_size < offset + nbytes)
/* truncating */
nbytes = s->st_size - offset;
}
map_start = offset & ~(mmap_pagesize() - 1);
map_off = offset - map_start;
map = mmap(NULL, nbytes + map_off, PROT_READ,
MAP_SHARED, mmap_fd->fd, map_start);
if (map == MAP_FAILED) {
perror("mmap");
return -1;
}
while (i * width + width <= nbytes) {
switch (width) {
case 1:
((uint8_t *)buf)[i] = ((uint8_t *)(map + map_off))[i];
break;
case 2:
((uint16_t *)buf)[i] = ((uint16_t *)(map + map_off))[i];
break;
case 4:
((uint32_t *)buf)[i] = ((uint32_t *)(map + map_off))[i];
break;
case 8:
((uint64_t *)buf)[i] = ((uint64_t *)(map + map_off))[i];
break;
}
++i;
}
ret = munmap(map, nbytes + map_off);
if (ret < 0) {
perror("munmap");
return -1;
}
return i * width;
}
static ssize_t mmap_write(struct memtool_fd *handle, off_t offset,
const void *buf, size_t nbytes, int width)
{
struct memtool_mmap_fd *mmap_fd =
container_of(handle, struct memtool_mmap_fd, mfd);
struct stat *s = &mmap_fd->s;
off_t map_start, map_off;
void *map;
size_t i = 0;
int ret;
if (S_ISREG(s->st_mode) && s->st_size < offset + nbytes) {
ret = posix_fallocate(mmap_fd->fd, offset, nbytes);
if (ret) {
errno = ret;
perror("fallocate");
return -1;
}
s->st_size = offset + nbytes;
}
map_start = offset & ~(mmap_pagesize() - 1);
map_off = offset - map_start;
map = mmap(NULL, nbytes + map_off, PROT_WRITE,
MAP_SHARED, mmap_fd->fd, map_start);
if (map == MAP_FAILED) {
perror("mmap");
return -1;
}
while (i * width + width <= nbytes) {
switch (width) {
case 1:
((uint8_t *)(map + map_off))[i] = ((uint8_t *)buf)[i];
break;
case 2:
((uint16_t *)(map + map_off))[i] = ((uint16_t *)buf)[i];
break;
case 4:
((uint32_t *)(map + map_off))[i] = ((uint32_t *)buf)[i];
break;
case 8:
((uint64_t *)(map + map_off))[i] = ((uint64_t *)buf)[i];
break;
}
++i;
}
ret = munmap(map, nbytes + map_off);
if (ret < 0) {
perror("munmap");
return -1;
}
return i * width;
}
static int mmap_close(struct memtool_fd *handle)
{
struct memtool_mmap_fd *mmap_fd =
container_of(handle, struct memtool_mmap_fd, mfd);
int ret;
ret = close(mmap_fd->fd);
free(mmap_fd);
return ret;
}
struct memtool_fd *mmap_open(const char *spec, int flags)
{
struct memtool_mmap_fd *mmap_fd;
int ret;
mmap_fd = malloc(sizeof(*mmap_fd));
if (!mmap_fd) {
fprintf(stderr, "Failure to allocate mmap_fd\n");
return NULL;
}
mmap_fd->mfd.read = mmap_read;
mmap_fd->mfd.write = mmap_write;
mmap_fd->mfd.close = mmap_close;
mmap_fd->fd = open(spec, flags, S_IRUSR | S_IWUSR);
if (mmap_fd->fd < 0) {
perror("open");
free(mmap_fd);
return NULL;
}
ret = fstat(mmap_fd->fd, &mmap_fd->s);
if (ret) {
perror("fstat");
close(mmap_fd->fd);
free(mmap_fd);
return NULL;
}
return &mmap_fd->mfd;
}
|