1 /* 2 * Test dataspace operations. 3 * 4 * Copyright (C) 2020, 2021, 2022 Paul Boddie <paul@boddie.org.uk> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License as 8 * published by the Free Software Foundation; either version 2 of 9 * the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA 20 */ 21 22 #include <l4/re/env.h> 23 #include <l4/sys/err.h> 24 25 #include <systypes/fcntl.h> 26 27 #include <stdio.h> 28 #include <string.h> 29 #include <stdlib.h> 30 31 #include <fsclient/file.h> 32 #include <mem/memory_utils.h> 33 34 #include "dataspace_client.h" 35 36 37 38 int main(int argc, char *argv[]) 39 { 40 if (argc < 4) 41 { 42 printf("Need filename, step and sample size.\n"); 43 return 1; 44 } 45 46 /* Obtain filename and access parameters. */ 47 48 char *filename = argv[1]; 49 unsigned long step = atoi(argv[2]); 50 unsigned long sample = atoi(argv[3]); 51 52 /* Allocate a buffer for sampling from the file. */ 53 54 char buf[sample + 1]; 55 56 /* Obtain access to the filesystem. */ 57 58 l4_cap_idx_t server = l4re_env_get_cap("server"); 59 60 /* Invoke the open method to receive the file reference. */ 61 62 file_t file; 63 long err = file_open(&file, filename, O_RDWR, server); 64 65 if (err) 66 { 67 printf("Could not obtain file: %s\n", l4sys_errtostr(err)); 68 return 1; 69 } 70 71 /* A region of the file is mapped but not attached. */ 72 73 err = file_mmap_only(&file, 0, page(10), 0, 0); 74 75 if (err) 76 { 77 printf("Could not map file region: %s\n", l4sys_errtostr(err)); 78 return 1; 79 } 80 81 /* Fix up the file data structure manually. */ 82 83 file.memory = (char *) 0x2000000; 84 85 /* Explicitly map the file into this address space, without region mapper 86 usage. The receive window base must be a multiple of its size. */ 87 88 client_Dataspace dataspace(file.ref); 89 l4_snd_fpage_t region = {0, l4_fpage((l4_addr_t) file.memory, L4_PAGESHIFT + 4, 0)}; 90 91 printf("region = {%lx, {%lx, %d}}\n", region.snd_base, l4_fpage_memaddr(region.fpage), l4_fpage_size(region.fpage)); 92 93 err = dataspace.map(0, 0, L4_FPAGE_RO, ®ion); 94 95 if (err) 96 { 97 printf("Could not invoke map successfully: %s\n", l4sys_errtostr(err)); 98 return 1; 99 } 100 101 for (unsigned long offset = 0; offset < file_populated_span(&file); offset += step) 102 { 103 unsigned long remaining = file_populated_span(&file) - offset; 104 unsigned long sample_remaining = remaining < sample ? remaining : sample; 105 106 printf("%ld bytes from %p...\n", sample_remaining, (file.memory + offset)); 107 strncpy(buf, (file.memory + offset), sample_remaining); 108 buf[sample_remaining] = '\0'; 109 printf("%s\n", buf); 110 } 111 112 printf("File shown.\n"); 113 114 return 0; 115 } 116 117 // vim: tabstop=2 expandtab shiftwidth=2