2 * Copyright 2012 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
11 * 2. Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
29 * The views and conclusions contained in the software and
30 * documentation are those of the authors and should not be
31 * interpreted as representing official policies, either expressed
32 * or implied, of GRNET S.A.
40 #include <sys/types.h>
41 #include <sys/ioctl.h>
51 printf("segtool [<command> <arg>]* \n"
53 " create <size_in_bytes>\n"
56 " map <offset> <size_in_bytes>\n"
57 " unmap <size_in_bytes>\n"
58 " dump <offset> <size_in_bytes>\n"
60 " fill <offset> <size_in_bytes> <char_in_hex>\n"
68 #define ALLOC_MIN 4096
69 #define ALLOC_MAX 1048576
71 void inputbuf(FILE *fp, char **retbuf, uint64_t *retsize)
73 static uint64_t alloc_size;
79 if (alloc_size < ALLOC_MIN)
80 alloc_size = ALLOC_MIN;
82 if (alloc_size > ALLOC_MAX)
83 alloc_size = ALLOC_MAX;
85 p = realloc(buf, alloc_size);
96 r = fread(buf + size, 1, alloc_size - size, fp);
100 if (size >= alloc_size) {
101 p = realloc(buf, alloc_size * 2);
119 static int opendev(void)
121 int fd = open("/dev/segdev", O_RDWR);
123 perror("/dev/segdev");
127 static char *segment;
128 static unsigned long mapped_size;
130 int cmd_create(uint64_t size)
132 int r, fd = opendev();
136 r = ioctl(fd, SEGDEV_IOC_CREATESEG, size);
144 int cmd_destroy(void)
146 int r, fd = opendev();
150 r = ioctl(fd, SEGDEV_IOC_DESTROYSEG, 0);
152 perror("DESTROYSEG");
160 long r, fd = opendev();
164 r = ioctl(fd, SEGDEV_IOC_SEGSIZE, 0);
168 printf("Segment size: %lu bytes\n", r);
173 int cmd_map(uint64_t offset, uint64_t size)
176 long r = -1, fd = opendev();
185 r = ioctl(fd, SEGDEV_IOC_SEGSIZE, 0);
193 if (offset + size > r) {
194 printf("segment size would be exceeded\n");
199 //seg = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, offset);
200 seg = mmap( (void*) 0x37fd0000, size,
201 PROT_READ | PROT_WRITE,
202 MAP_SHARED | MAP_FIXED,
204 if (seg == MAP_FAILED) {
216 int cmd_unmap(uint64_t size)
218 long r = -1, fd = opendev();
229 r = munmap(segment, size);
244 int c, fd = open("/dev/tty", O_RDONLY);
250 int cmd_dump(uint64_t offset, uint64_t size)
252 long r = -1, fd = opendev();
257 printf("segment not mapped\n");
262 size = mapped_size - offset;
264 if (offset + size > mapped_size) {
265 printf("mapped segment size would be exceeded\n");
269 for (r = offset; r < offset + size; r++)
270 if (fputc(segment[r], stdout) == EOF)
280 int cmd_load(uint64_t offset)
282 long r = -1, fd = opendev();
289 printf("segment not mapped\n");
293 for (pos = offset; pos < mapped_size; pos++) {
294 int c = fgetc(stdin);
304 int cmd_fill(uint64_t offset, uint64_t size, int fill)
306 long r = -1, fd = opendev();
307 uint64_t misscount = 0;
313 printf("segment not mapped\n");
318 size = mapped_size - offset;
320 if (offset + size > mapped_size) {
321 printf("mapped segment size would be exceeded\n");
325 memset(segment + offset, fill, size);
326 for (size += offset; offset < size; offset++)
327 if (segment[offset] != (char)fill)
331 printf("fill misscount(!) %lu\n", misscount);
339 unsigned long i, count;
340 unsigned long *longs;
343 printf("segment not mapped\n");
347 longs = (void *)segment;
348 count = mapped_size / sizeof(long);
349 for (i = 0; i < count; i++)
355 int cmd_checkmark(void)
357 unsigned long i, count;
358 unsigned long *longs;
361 printf("segment not mapped\n");
365 longs = (void *)segment;
366 count = mapped_size / sizeof(long);
367 for (i = 0; i < count; i++)
369 printf("%lu != %lu\n", i, longs[i]);
373 int main(int argc, char **argv) {
380 for (i = 1; i < argc; i++) {
382 if (!strcmp(argv[i], "info")) {
387 if (!strcmp(argv[i], "create") && (i + 1 < argc)) {
388 ret = cmd_create(atol(argv[i+1]));
393 if (!strcmp(argv[i], "destroy")) {
398 if (!strcmp(argv[i], "wait")) {
403 if (!strcmp(argv[i], "mark")) {
408 if (!strcmp(argv[i], "checkmark")) {
409 ret = cmd_checkmark();
413 if (!strcmp(argv[i], "map") && (i + 2 < argc)) {
414 ret = cmd_map(atol(argv[i+1]), atol(argv[i+2]));
419 if (!strcmp(argv[i], "unmap") && (i + 1 < argc)) {
420 ret = cmd_unmap(atol(argv[i+1]));
425 if (!strcmp(argv[i], "fill") && (i + 3 < argc)) {
426 ret = cmd_fill( atol(argv[i+1]),
428 strtoul(argv[i+3], NULL, 16));
433 if (!strcmp(argv[i], "dump") && (i + 2 < argc)) {
434 ret = cmd_dump(atol(argv[i+1]), atol(argv[i+2]));
439 if (!strcmp(argv[i], "load") && (i + 1 < argc)) {
440 ret = cmd_load(atol(argv[i+1]));