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
23 #include "ps3stor_mgr.h"
25 #define PS3STOR_REGION_VERSION "0.0.1"
36 static struct option long_opts
[] = {
37 { "help", no_argument
, NULL
, 'h' },
38 { "verbose", no_argument
, NULL
, 'v' },
39 { "version", no_argument
, NULL
, 'V' },
46 static void usage(void) {
48 "Usage: ps3stor_region [OPTIONS] DEVICE COMMAND [ARGS]\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"
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"
60 "Simple example: Create a HDD region:\n"
61 " ps3stor_region /dev/ps3stormgr create 3 0x8 0x1000 0x1070000002000001\n");
67 static void version(void)
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");
82 static int process_opts(int argc
, char **argv
, struct opts
*opts
)
86 while ((c
= getopt_long(argc
, argv
, "hvV", long_opts
, NULL
)) != -1) {
102 fprintf(stderr
, "Invalid command option: %c\n", c
);
107 if (optind
>= argc
) {
108 fprintf(stderr
, "No device specified\n");
112 opts
->device_name
= argv
[optind
];
115 if (optind
>= argc
) {
116 fprintf(stderr
, "No command specified\n");
120 opts
->cmd
= argv
[optind
];
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
;
135 if (optind
>= argc
) {
136 fprintf(stderr
, "No device identifier specified\n");
140 dev_id
= strtoull(argv
[optind
], &endptr
, 0);
141 if (*endptr
!= '\0') {
142 fprintf(stderr
, "Invalid device identifier specified\n");
148 if (optind
>= argc
) {
149 fprintf(stderr
, "No start sector specified\n");
153 start_sector
= strtoull(argv
[optind
], &endptr
, 0);
154 if (*endptr
!= '\0') {
155 fprintf(stderr
, "Invalid start sector specified\n");
161 if (optind
>= argc
) {
162 fprintf(stderr
, "No sector count specified\n");
166 sector_count
= strtoull(argv
[optind
], &endptr
, 0);
167 if (*endptr
!= '\0') {
168 fprintf(stderr
, "Invalid sector count specified\n");
174 if (optind
>= argc
) {
175 fprintf(stderr
, "No LPAR authentication identifier specified\n");
179 laid
= strtoull(argv
[optind
], &endptr
, 0);
180 if (*endptr
!= '\0') {
181 fprintf(stderr
, "Invalid LPAR authentication identifier specified\n");
187 error
= ps3stor_mgr_create_region(fd
, dev_id
, start_sector
, sector_count
, laid
,
191 fprintf(stderr
, "%s: %s\n", opts
->device_name
, strerror(errno
));
193 fprintf(stdout
, "0x%016lx\n", region_id
);
201 static int cmd_delete(int fd
, struct opts
*opts
, int argc
, char **argv
)
203 uint64_t dev_id
, region_id
;
207 if (optind
>= argc
) {
208 fprintf(stderr
, "No device identifier specified\n");
212 dev_id
= strtoull(argv
[optind
], &endptr
, 0);
213 if (*endptr
!= '\0') {
214 fprintf(stderr
, "Invalid device identifier specified\n");
220 if (optind
>= argc
) {
221 fprintf(stderr
, "No region identifier specified\n");
225 region_id
= strtoull(argv
[optind
], &endptr
, 0);
226 if (*endptr
!= '\0') {
227 fprintf(stderr
, "Invalid region identifier specified\n");
233 error
= ps3stor_mgr_delete_region(fd
, dev_id
, region_id
);
236 fprintf(stderr
, "%s: %s\n", opts
->device_name
, strerror(errno
));
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
;
250 if (optind
>= argc
) {
251 fprintf(stderr
, "No device identifier specified\n");
255 dev_id
= strtoull(argv
[optind
], &endptr
, 0);
256 if (*endptr
!= '\0') {
257 fprintf(stderr
, "Invalid device identifier specified\n");
263 if (optind
>= argc
) {
264 fprintf(stderr
, "No region identifier specified\n");
268 region_id
= strtoull(argv
[optind
], &endptr
, 0);
269 if (*endptr
!= '\0') {
270 fprintf(stderr
, "Invalid region identifier specified\n");
276 if (optind
>= argc
) {
277 fprintf(stderr
, "No LPAR authentication identifier specified\n");
281 laid
= strtoull(argv
[optind
], &endptr
, 0);
282 if (*endptr
!= '\0') {
283 fprintf(stderr
, "Invalid LPAR authentication identifier specified\n");
289 if (optind
>= argc
) {
290 fprintf(stderr
, "No access rights specified\n");
294 access_rights
= strtoull(argv
[optind
], &endptr
, 0);
295 if (*endptr
!= '\0') {
296 fprintf(stderr
, "Invalid access rights specified\n");
302 error
= ps3stor_mgr_set_region_acl(fd
, dev_id
, region_id
, laid
, access_rights
);
305 fprintf(stderr
, "%s: %s\n", opts
->device_name
, strerror(errno
));
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
;
319 if (optind
>= argc
) {
320 fprintf(stderr
, "No device identifier specified\n");
324 dev_id
= strtoull(argv
[optind
], &endptr
, 0);
325 if (*endptr
!= '\0') {
326 fprintf(stderr
, "Invalid device identifier specified\n");
332 if (optind
>= argc
) {
333 fprintf(stderr
, "No region identifier specified\n");
337 region_id
= strtoull(argv
[optind
], &endptr
, 0);
338 if (*endptr
!= '\0') {
339 fprintf(stderr
, "Invalid region identifier specified\n");
345 if (optind
>= argc
) {
346 fprintf(stderr
, "No entry index specified\n");
350 entry_index
= strtoull(argv
[optind
], &endptr
, 0);
351 if (*endptr
!= '\0') {
352 fprintf(stderr
, "Invalid entry index specified\n");
358 error
= ps3stor_mgr_get_region_acl(fd
, dev_id
, region_id
, entry_index
, &laid
, &access_rights
);
361 fprintf(stderr
, "%s: %s\n", opts
->device_name
, strerror(errno
));
363 fprintf(stdout
, "0x%016lx 0x%016lx\n", laid
, access_rights
);
371 int main(int argc
, char **argv
)
374 int fd
= 0, error
= 0;
376 memset(&opts
, 0, sizeof(opts
));
378 if (process_opts(argc
, argv
, &opts
)) {
387 } else if (opts
.do_version
) {
392 fd
= ps3stor_mgr_open(opts
.device_name
);
394 fprintf(stderr
, "%s: %s\n", opts
.device_name
, strerror(errno
));
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
);
419 ps3stor_mgr_close(fd
);