]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - usr.sbin/isfctl/isfctl.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / usr.sbin / isfctl / isfctl.c
1 /*-
2  * Copyright (c) 2012 SRI International
3  * All rights reserved.
4  *
5  * This software was developed by SRI International and the University of
6  * Cambridge Computer Laboratory under DARPA/AFRL contract (FA8750-10-C-0237)
7  * ("CTSRD"), as part of the DARPA CRASH research programme.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD$
31  */
32
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35
36 #include <err.h>
37 #include <fcntl.h>
38 #include <inttypes.h>
39 #include <stdlib.h>
40 #include <stdio.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 /* XXXBED: should install and include sys/dev/isf.h */
45 struct isf_range {
46         off_t   ir_off;         /* Offset of range to delete (set to 0xFF) */
47         size_t  ir_size;        /* Size of range */
48 };
49
50 #define ISF_ERASE       _IOW('I', 1, struct isf_range)
51
52 #define ISF_ERASE_BLOCK (128 * 1024)
53
54 static enum {UNSET, ERASE} action = UNSET;
55
56 static void
57 usage(void)
58 {
59         fprintf(stderr, "usage: isfctl <device> erase <offset> <size>\n");
60         exit(1);
61 }
62
63 int
64 main(int argc, char **argv)
65 {
66         struct isf_range        ir;
67         int                     fd, i;
68         char                    *p, *dev;
69         
70         if (argc < 2)
71                 usage();
72         argc--; argv++;
73         
74         if (*argv[0] == '/')
75                 dev = argv[0];
76         else
77                 asprintf(&dev, "/dev/%s", argv[0]);
78         argc--; argv++;
79         fd = open(dev, O_RDWR);
80         if (fd < 0)
81                 err(1, "unable to open device -- %s", dev);
82
83         if (strcmp(argv[0], "erase") == 0) {
84                 if (argc != 3)
85                         usage();
86                 action = ERASE;
87                 ir.ir_off = strtol(argv[1], &p, 0);
88                 if (*p)
89                         errx(1, "invalid offset -- %s", argv[2]);
90                 ir.ir_size = strtol(argv[2], &p, 0);
91                 if (*p)
92                         errx(1, "invalid size -- %s", argv[3]);
93                 /*
94                  * If the user requests to delete less than 32K of space
95                  * then assume that they want to delete a number of 128K
96                  * blocks.
97                  */
98                 if (ir.ir_size < 32 * 1024)
99                         ir.ir_size *= 128 * 1024;
100         }
101
102         switch (action) {
103         case ERASE:
104                 i = ioctl(fd, ISF_ERASE, &ir);
105                 if (i < 0)
106                         err(1, "ioctl(%s, %jx, %zx)", dev,
107                             (intmax_t)ir.ir_off, ir.ir_size);
108                 break;
109         default:
110                 usage();
111         }
112
113         close(fd);
114         return (0);
115 }