ps3stor_region: fixed ps3stor_mgr_get_region_acl
[ps3stor-utils.git] / ps3stor_region.c
blob1817166cb3288a8f44d2821bce6c12fd9b48b157
2 /*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; version 2 of the License.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <getopt.h>
21 #include <errno.h>
23 #include "ps3stor_mgr.h"
25 #define PS3STOR_REGION_VERSION "0.0.1"
27 struct opts
29 char *device_name;
30 char *cmd;
31 int do_help;
32 int do_verbose;
33 int do_version;
36 static struct option long_opts[] = {
37 { "help", no_argument, NULL, 'h' },
38 { "verbose", no_argument, NULL, 'v' },
39 { "version", no_argument, NULL, 'V' },
40 { NULL, 0, NULL, 0 }
44 * usage
46 static void usage(void) {
47 fprintf(stderr,
48 "Usage: ps3stor_region [OPTIONS] DEVICE COMMAND [ARGS]\n"
49 "\n"
50 "Options:\n"
51 " -h, --help Show this message and exit\n"
52 " -v, --verbose Increase verbosity\n"
53 " -V, --version Show version information and exit\n"
54 "Commands:\n"
55 " create DEVID START COUNT LAID Creates storage region\n"
56 " delete DEVID REGIONID Deletes storage region\n"
57 " set_acl DEVID REGIONID LAID RIGHTS Sets region access rights\n"
58 " get_acl DEVID REGIONID INDEX Returns region access rights\n"
59 "\n\n"
60 "Simple example: Create a HDD region:\n"
61 " ps3stor_region /dev/ps3stormgr create 3 0x8 0x1000 0x1070000002000001\n");
65 * version
67 static void version(void)
69 fprintf(stderr,
70 "ps3stor_region " PS3STOR_REGION_VERSION "\n"
71 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
72 "This is free software. You may redistribute copies of it "
73 "under the terms of\n"
74 "the GNU General Public License 2 "
75 "<http://www.gnu.org/licenses/gpl2.html>.\n"
76 "There is NO WARRANTY, to the extent permitted by law.\n");
80 * process_opts
82 static int process_opts(int argc, char **argv, struct opts *opts)
84 int c;
86 while ((c = getopt_long(argc, argv, "hvV", long_opts, NULL)) != -1) {
87 switch (c) {
88 case 'h':
89 case '?':
90 opts->do_help = 1;
91 return 0;
93 case 'v':
94 opts->do_verbose++;
95 break;
97 case 'V':
98 opts->do_version = 1;
99 return 0;
101 default:
102 fprintf(stderr, "Invalid command option: %c\n", c);
103 return -1;
107 if (optind >= argc) {
108 fprintf(stderr, "No device specified\n");
109 return -1;
112 opts->device_name = argv[optind];
113 optind++;
115 if (optind >= argc) {
116 fprintf(stderr, "No command specified\n");
117 return -1;
120 opts->cmd = argv[optind];
121 optind++;
123 return 0;
127 * cmd_create
129 static int cmd_create(int fd, struct opts *opts, int argc, char **argv)
131 uint64_t dev_id, start_sector, sector_count, laid, region_id;
132 char *endptr;
133 int error;
135 if (optind >= argc) {
136 fprintf(stderr, "No device identifier specified\n");
137 return -1;
140 dev_id = strtoull(argv[optind], &endptr, 0);
141 if (*endptr != '\0') {
142 fprintf(stderr, "Invalid device identifier specified\n");
143 return -1;
146 optind++;
148 if (optind >= argc) {
149 fprintf(stderr, "No start sector specified\n");
150 return -1;
153 start_sector = strtoull(argv[optind], &endptr, 0);
154 if (*endptr != '\0') {
155 fprintf(stderr, "Invalid start sector specified\n");
156 return -1;
159 optind++;
161 if (optind >= argc) {
162 fprintf(stderr, "No sector count specified\n");
163 return -1;
166 sector_count = strtoull(argv[optind], &endptr, 0);
167 if (*endptr != '\0') {
168 fprintf(stderr, "Invalid sector count specified\n");
169 return -1;
172 optind++;
174 if (optind >= argc) {
175 fprintf(stderr, "No LPAR authentication identifier specified\n");
176 return -1;
179 laid = strtoull(argv[optind], &endptr, 0);
180 if (*endptr != '\0') {
181 fprintf(stderr, "Invalid LPAR authentication identifier specified\n");
182 return -1;
185 optind++;
187 error = ps3stor_mgr_create_region(fd, dev_id, start_sector, sector_count, laid,
188 &region_id);
190 if (error)
191 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
192 else
193 fprintf(stdout, "0x%016lx\n", region_id);
195 return error;
199 * cmd_delete
201 static int cmd_delete(int fd, struct opts *opts, int argc, char **argv)
203 uint64_t dev_id, region_id;
204 char *endptr;
205 int error;
207 if (optind >= argc) {
208 fprintf(stderr, "No device identifier specified\n");
209 return -1;
212 dev_id = strtoull(argv[optind], &endptr, 0);
213 if (*endptr != '\0') {
214 fprintf(stderr, "Invalid device identifier specified\n");
215 return -1;
218 optind++;
220 if (optind >= argc) {
221 fprintf(stderr, "No region identifier specified\n");
222 return -1;
225 region_id = strtoull(argv[optind], &endptr, 0);
226 if (*endptr != '\0') {
227 fprintf(stderr, "Invalid region identifier specified\n");
228 return -1;
231 optind++;
233 error = ps3stor_mgr_delete_region(fd, dev_id, region_id);
235 if (error)
236 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
238 return error;
242 * cmd_set_acl
244 static int cmd_set_acl(int fd, struct opts *opts, int argc, char **argv)
246 uint64_t dev_id, region_id, laid, access_rights;
247 char *endptr;
248 int error;
250 if (optind >= argc) {
251 fprintf(stderr, "No device identifier specified\n");
252 return -1;
255 dev_id = strtoull(argv[optind], &endptr, 0);
256 if (*endptr != '\0') {
257 fprintf(stderr, "Invalid device identifier specified\n");
258 return -1;
261 optind++;
263 if (optind >= argc) {
264 fprintf(stderr, "No region identifier specified\n");
265 return -1;
268 region_id = strtoull(argv[optind], &endptr, 0);
269 if (*endptr != '\0') {
270 fprintf(stderr, "Invalid region identifier specified\n");
271 return -1;
274 optind++;
276 if (optind >= argc) {
277 fprintf(stderr, "No LPAR authentication identifier specified\n");
278 return -1;
281 laid = strtoull(argv[optind], &endptr, 0);
282 if (*endptr != '\0') {
283 fprintf(stderr, "Invalid LPAR authentication identifier specified\n");
284 return -1;
287 optind++;
289 if (optind >= argc) {
290 fprintf(stderr, "No access rights specified\n");
291 return -1;
294 access_rights = strtoull(argv[optind], &endptr, 0);
295 if (*endptr != '\0') {
296 fprintf(stderr, "Invalid access rights specified\n");
297 return -1;
300 optind++;
302 error = ps3stor_mgr_set_region_acl(fd, dev_id, region_id, laid, access_rights);
304 if (error)
305 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
307 return error;
311 * cmd_get_acl
313 static int cmd_get_acl(int fd, struct opts *opts, int argc, char **argv)
315 uint64_t dev_id, region_id, entry_index, laid, access_rights;
316 char *endptr;
317 int error;
319 if (optind >= argc) {
320 fprintf(stderr, "No device identifier specified\n");
321 return -1;
324 dev_id = strtoull(argv[optind], &endptr, 0);
325 if (*endptr != '\0') {
326 fprintf(stderr, "Invalid device identifier specified\n");
327 return -1;
330 optind++;
332 if (optind >= argc) {
333 fprintf(stderr, "No region identifier specified\n");
334 return -1;
337 region_id = strtoull(argv[optind], &endptr, 0);
338 if (*endptr != '\0') {
339 fprintf(stderr, "Invalid region identifier specified\n");
340 return -1;
343 optind++;
345 if (optind >= argc) {
346 fprintf(stderr, "No entry index specified\n");
347 return -1;
350 entry_index = strtoull(argv[optind], &endptr, 0);
351 if (*endptr != '\0') {
352 fprintf(stderr, "Invalid entry index specified\n");
353 return -1;
356 optind++;
358 error = ps3stor_mgr_get_region_acl(fd, dev_id, region_id, entry_index, &laid, &access_rights);
360 if (error)
361 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
362 else
363 fprintf(stdout, "0x%016lx 0x%016lx\n", laid, access_rights);
365 return error;
369 * main
371 int main(int argc, char **argv)
373 struct opts opts;
374 int fd = 0, error = 0;
376 memset(&opts, 0, sizeof(opts));
378 if (process_opts(argc, argv, &opts)) {
379 usage();
380 error = 1;
381 goto done;
384 if (opts.do_help) {
385 usage();
386 goto done;
387 } else if (opts.do_version) {
388 version();
389 goto done;
392 fd = ps3stor_mgr_open(opts.device_name);
393 if (fd < 0) {
394 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
395 error = 2;
396 goto done;
399 if (!strcmp(opts.cmd, "create")) {
400 error = cmd_create(fd, &opts, argc, argv);
401 } else if (!strcmp(opts.cmd, "delete")) {
402 error = cmd_delete(fd, &opts, argc, argv);
403 } else if (!strcmp(opts.cmd, "set_acl")) {
404 error = cmd_set_acl(fd, &opts, argc, argv);
405 } else if (!strcmp(opts.cmd, "get_acl")) {
406 error = cmd_get_acl(fd, &opts, argc, argv);
407 } else {
408 usage();
409 error = 1;
410 goto done;
413 if (error)
414 error = 3;
416 done:
418 if (fd >= 0)
419 ps3stor_mgr_close(fd);
421 exit(error);