From 6e0f0b8956c8373e7f613b39e6df9738b0d10b09 Mon Sep 17 00:00:00 2001 From: glevand Date: Mon, 27 Aug 2012 09:14:25 -0800 Subject: [PATCH] initial import --- Makefile | 21 ++++++ cmd/cmd.c | 74 +++++++++++++++++++++ cmd/cmd.h | 53 +++++++++++++++ cmd/cmd_create_region.c | 76 ++++++++++++++++++++++ cmd/cmd_delete_region.c | 65 +++++++++++++++++++ cmd/cmd_help.c | 59 +++++++++++++++++ cmd/cmd_list.c | 52 +++++++++++++++ cmd/cmd_print_acl_entry.c | 119 ++++++++++++++++++++++++++++++++++ cmd/cmd_print_device.c | 88 +++++++++++++++++++++++++ cmd/cmd_print_region.c | 94 +++++++++++++++++++++++++++ cmd/cmd_set_acl_entry.c | 98 ++++++++++++++++++++++++++++ dev.c | 149 ++++++++++++++++++++++++++++++++++++++++++ dev.h | 44 +++++++++++++ opts.h | 40 ++++++++++++ ps3sed.c | 161 ++++++++++++++++++++++++++++++++++++++++++++++ 15 files changed, 1193 insertions(+) create mode 100644 Makefile create mode 100644 cmd/cmd.c create mode 100644 cmd/cmd.h create mode 100644 cmd/cmd_create_region.c create mode 100644 cmd/cmd_delete_region.c create mode 100644 cmd/cmd_help.c create mode 100644 cmd/cmd_list.c create mode 100644 cmd/cmd_print_acl_entry.c create mode 100644 cmd/cmd_print_device.c create mode 100644 cmd/cmd_print_region.c create mode 100644 cmd/cmd_set_acl_entry.c create mode 100644 dev.c create mode 100644 dev.h create mode 100644 opts.h create mode 100644 ps3sed.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..02bd834 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ + +CC=gcc +CFLAGS=-Wall -g -O2 +LDFLAGS= +SRC=ps3sed.c dev.c $(wildcard cmd/*.c) +OBJ=$(SRC:.c=.o) +INC= +LIB= +TARGET=ps3sed + +all: $(TARGET) + +$(TARGET): $(OBJ) + $(CC) $(LDFLAGS) $(OBJ) -o $@ $(LIB) + +%.o: %.c + $(CC) $(CFLAGS) $(INC) -c $< -o $@ + +.PHONY: clean +clean: + rm -f $(OBJ) $(TARGET) diff --git a/cmd/cmd.c b/cmd/cmd.c new file mode 100644 index 0000000..61c7035 --- /dev/null +++ b/cmd/cmd.c @@ -0,0 +1,74 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "cmd.h" + +struct cmd * +cmd_find(int cmdc, struct cmd **cmdv, const char *name) +{ + int l, r, m; + int cmp; + + l = 0; + r = cmdc - 1; + + while (l <= r) { + m = (l + r) / 2; + + cmp = strcmp(name, cmdv[m]->name); + if (!cmp) + return (cmdv[m]); + else if (cmp < 0) + r = m - 1; + else + l = m + 1; + + } + + return (NULL); +} + +int +cmd_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ + struct cmd *cmd; + + if (argc <= 0) + return (CMD_EBADCMD); + + cmd = cmd_find(cmdc, cmdv, argv[0]); + if (!cmd) + return (CMD_EBADCMD); + + if ((cmd->min_argc != -1 && (argc - 1) < cmd->min_argc) || + (cmd->max_argc != -1 && (argc - 1) > cmd->max_argc)) + return (CMD_EINVAL); + + return (cmd->exec(cmdc, cmdv, argc - 1, argv + 1)); +} diff --git a/cmd/cmd.h b/cmd/cmd.h new file mode 100644 index 0000000..7b39399 --- /dev/null +++ b/cmd/cmd.h @@ -0,0 +1,53 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _CMD_H +#define _CMD_H + +enum { + CMD_EOK = 0, + CMD_EBADCMD, + CMD_EINVAL, + CMD_EIO, + CMD_EUNKNOWN, +}; + +struct cmd { + const char *name; + const char *help; + const char *usage; + + int min_argc; + int max_argc; + + int (*exec)(int cmdc, struct cmd **cmdv, int argc, char **argv); +}; + +struct cmd *cmd_find(int cmdc, struct cmd **cmdv, const char *name); + +int cmd_exec(int cmdc, struct cmd **cmdv, int argc, char **argv); + +#endif /* _CMD_H */ diff --git a/cmd/cmd_create_region.c b/cmd/cmd_create_region.c new file mode 100644 index 0000000..13840fc --- /dev/null +++ b/cmd/cmd_create_region.c @@ -0,0 +1,76 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include "../dev.h" +#include "cmd.h" + +int +cmd_create_region_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ + uint64_t devid, start_block, nr_blocks, laid, rgnid; + char *endptr; + int ret; + + devid = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + start_block = strtoull(argv[1], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + nr_blocks = strtoull(argv[2], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + laid = strtoull(argv[3], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + ret = dev_create_region(devid, start_block, nr_blocks, laid, &rgnid); + if (ret) + return (CMD_EIO); + + fprintf(stdout, "%llu\n", rgnid); + + return (CMD_EOK); +} + +struct cmd cmd_create_region = { + .name = "create_region", + .help = "create region", + .usage = "create_region ", + + .min_argc = 4, + .max_argc = 4, + + .exec = cmd_create_region_exec, +}; diff --git a/cmd/cmd_delete_region.c b/cmd/cmd_delete_region.c new file mode 100644 index 0000000..1f906d1 --- /dev/null +++ b/cmd/cmd_delete_region.c @@ -0,0 +1,65 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include "../dev.h" +#include "cmd.h" + +int +cmd_delete_region_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ + uint64_t devid, rgnid; + char *endptr; + int ret; + + devid = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + rgnid = strtoull(argv[1], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + ret = dev_delete_region(devid, rgnid); + if (ret) + return (CMD_EIO); + + return (CMD_EOK); +} + +struct cmd cmd_delete_region = { + .name = "delete_region", + .help = "delete region", + .usage = "delete_region ", + + .min_argc = 2, + .max_argc = 2, + + .exec = cmd_delete_region_exec, +}; diff --git a/cmd/cmd_help.c b/cmd/cmd_help.c new file mode 100644 index 0000000..8848233 --- /dev/null +++ b/cmd/cmd_help.c @@ -0,0 +1,59 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include "cmd.h" + +int +cmd_help_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ + struct cmd *cmd; + int i; + + for (i = 0; i < argc; i++) { + cmd = cmd_find(cmdc, cmdv, argv[i]); + if (cmd) { + fprintf(stdout, "%s\n", cmd->help); + fprintf(stdout, "usage: %s\n", cmd->usage); + } else { + fprintf(stdout, "invalid command %s\n", argv[i]); + } + } + + return (CMD_EOK); +} + +struct cmd cmd_help = { + .name = "help", + .help = "print help message of command(s)", + .usage = "help *", + + .min_argc = -1, + .max_argc = -1, + + .exec = cmd_help_exec, +}; diff --git a/cmd/cmd_list.c b/cmd/cmd_list.c new file mode 100644 index 0000000..68ba958 --- /dev/null +++ b/cmd/cmd_list.c @@ -0,0 +1,52 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include + +#include "cmd.h" + +int +cmd_list_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ + int i; + + for (i = 0; i < cmdc; i++) + fprintf(stdout, "%s\n", cmdv[i]->name); + + return (CMD_EOK); +} + +struct cmd cmd_list = { + .name = "list", + .help = "list supported commands", + .usage = "list", + + .min_argc = 0, + .max_argc = 0, + + .exec = cmd_list_exec, +}; diff --git a/cmd/cmd_print_acl_entry.c b/cmd/cmd_print_acl_entry.c new file mode 100644 index 0000000..deeeb30 --- /dev/null +++ b/cmd/cmd_print_acl_entry.c @@ -0,0 +1,119 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include + +#include "../dev.h" +#include "cmd.h" + +static const char *access_rights_str[] = { + "none", + "r", + "w", + "rw", +}; + +int +cmd_print_acl_entry_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + + struct ps3strgmngr_device dev[PS3STRGMNGR_MAX_DEVICES]; + int nr_devices; + char *endptr; + uint64_t devid, rgnid; + int index = -1; + int i, j, k; + uint64_t access_rights; + char buf[32]; + + devid = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + rgnid = strtoull(argv[1], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + if (argc > 2) { + index = strtoul(argv[2], &endptr, 0); + if ((*endptr != '\0') || (index < 0)) + return (CMD_EINVAL); + } + + nr_devices = dev_get_devices(dev, N(dev)); + if (nr_devices < 0) + return (CMD_EIO); + + for (i = 0; i < nr_devices; i++) { + if (devid == dev[i].id) + break; + } + + if (i >= nr_devices) + return (CMD_EINVAL); + + for (j = 0; j < dev[i].nr_regions; j++) { + if (rgnid == dev[i].region[j].id) + break; + } + + if (j >= dev[i].nr_regions) + return (CMD_EINVAL); + + for (k = 0; k < dev[i].region[j].nr_acl_entries; k++) { + if ((argc > 2) && (index != k)) + continue; + + access_rights = dev[i].region[j].acl_entry[k].access_rights; + + sprintf(buf, "%016llx", access_rights); + + fprintf(stdout, "%16llx %20s\n", + dev[i].region[j].acl_entry[k].laid, + access_rights < N(access_rights_str) ? + access_rights_str[access_rights] : buf); + } + + return (CMD_EOK); + +#undef N +} + +struct cmd cmd_print_acl_entry = { + .name = "print_acl_entry", + .help = "print acl entry(s)", + .usage = "print_acl_entry []", + + .min_argc = 2, + .max_argc = 3, + + .exec = cmd_print_acl_entry_exec, +}; diff --git a/cmd/cmd_print_device.c b/cmd/cmd_print_device.c new file mode 100644 index 0000000..56f69aa --- /dev/null +++ b/cmd/cmd_print_device.c @@ -0,0 +1,88 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include + +#include "../dev.h" +#include "cmd.h" + +static const char *dev_type_str[] = { + "disk", + "cdrom", + "flash", + "nor_flash" +}; + +int +cmd_print_device_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + + struct ps3strgmngr_device dev[PS3STRGMNGR_MAX_DEVICES]; + int nr_devices; + char *endptr; + uint64_t id = 0; + int i; + + if (argc) { + id = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + } + + nr_devices = dev_get_devices(dev, N(dev)); + if (nr_devices < 0) + return (CMD_EIO); + + for (i = 0; i < nr_devices; i++) { + if (argc && (id != dev[i].id)) + continue; + + fprintf(stdout, "%10s %4llu %8llu %16llu %8llu\n", + dev_type_str[dev[i].type], + dev[i].id, dev[i].block_size, dev[i].nr_blocks, + dev[i].nr_regions); + } + + return (CMD_EOK); + +#undef N +} + +struct cmd cmd_print_device = { + .name = "print_device", + .help = "print device(s)", + .usage = "print_device []", + + .min_argc = 0, + .max_argc = 1, + + .exec = cmd_print_device_exec, +}; diff --git a/cmd/cmd_print_region.c b/cmd/cmd_print_region.c new file mode 100644 index 0000000..da87d78 --- /dev/null +++ b/cmd/cmd_print_region.c @@ -0,0 +1,94 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include + +#include + +#include "../dev.h" +#include "cmd.h" + +int +cmd_print_region_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + + struct ps3strgmngr_device dev[PS3STRGMNGR_MAX_DEVICES]; + int nr_devices; + char *endptr; + uint64_t devid; + uint64_t id = 0; + int i, j; + + devid = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + if (argc > 1) { + id = strtoull(argv[1], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + } + + nr_devices = dev_get_devices(dev, N(dev)); + if (nr_devices < 0) + return (CMD_EIO); + + for (i = 0; i < nr_devices; i++) { + if (devid == dev[i].id) + break; + } + + if (i >= nr_devices) + return (CMD_EINVAL); + + for (j = 0; j < dev[i].nr_regions; j++) { + if ((argc > 1) && (id != dev[i].region[j].id)) + continue; + + fprintf(stdout, "%4llu %16llu %16llu %4llu\n", + dev[i].region[j].id, + dev[i].region[j].start_block, dev[i].region[j].nr_blocks, + dev[i].region[j].nr_acl_entries); + } + + return (CMD_EOK); + +#undef N +} + +struct cmd cmd_print_region = { + .name = "print_region", + .help = "print region(s)", + .usage = "print_region []", + + .min_argc = 1, + .max_argc = 2, + + .exec = cmd_print_region_exec, +}; diff --git a/cmd/cmd_set_acl_entry.c b/cmd/cmd_set_acl_entry.c new file mode 100644 index 0000000..1abc3f8 --- /dev/null +++ b/cmd/cmd_set_acl_entry.c @@ -0,0 +1,98 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include + +#include "../dev.h" +#include "cmd.h" + +static struct { + const char *str; + uint64_t access_rights; +} access_rights_map[] = { + { "none", 0 }, + { "r", PS3STRGMNGR_REGION_READ }, + { "w", PS3STRGMNGR_REGION_WRITE }, + { "rw", PS3STRGMNGR_REGION_READ | PS3STRGMNGR_REGION_WRITE }, +}; + +int +cmd_set_acl_entry_exec(int cmdc, struct cmd **cmdv, int argc, char **argv) +{ +#define N(a) (sizeof(a) / sizeof((a)[0])) + + uint64_t devid, rgnid, laid; + char *endptr; + uint64_t access_rights = 0; + int i; + int ret; + + devid = strtoull(argv[0], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + rgnid = strtoull(argv[1], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + laid = strtoull(argv[2], &endptr, 0); + if (*endptr != '\0') + return (CMD_EINVAL); + + for (i = 0; i < N(access_rights_map); i++) { + if (!strcmp(argv[3], access_rights_map[i].str)) { + access_rights = access_rights_map[i].access_rights; + break; + } + } + + if (i >= N(access_rights_map)) + return (CMD_EINVAL); + + ret = dev_set_region_acl_entry(devid, rgnid, laid, access_rights); + if (ret) + return (CMD_EIO); + + return (CMD_EOK); + +#undef N +} + +struct cmd cmd_set_acl_entry = { + .name = "set_acl_entry", + .help = "set region acl entry", + .usage = "set_acl_entry ", + + .min_argc = 4, + .max_argc = 4, + + .exec = cmd_set_acl_entry_exec, +}; diff --git a/dev.c b/dev.c new file mode 100644 index 0000000..b246129 --- /dev/null +++ b/dev.c @@ -0,0 +1,149 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include + +#include "dev.h" + +static int fd = -1; + +int +dev_open(const char *device) +{ + if (fd >= 0) + close(fd); + + fd = open(device, O_RDWR); + if (fd < 0) + return (-1); + + return (0); +} + +void +dev_close(void) +{ + if (fd >= 0) + close(fd); +} + +int +dev_get_devices(struct ps3strgmngr_device *dev, int max_devices) +{ + struct ps3strgmngr_ctl_get_devices get_devices; + int ret; + + if (fd < 0) + return (-1); + + ret = ioctl(fd, PS3STRGMNGR_CTL_GET_DEVICES, &get_devices); + if (ret) + return (ret); + + if (max_devices > get_devices.nr_devices) + max_devices = get_devices.nr_devices; + + memcpy(dev, get_devices.device, max_devices * sizeof(*dev)); + + return (max_devices); +} + +int +dev_create_region(uint64_t device_id, uint64_t start_block, + uint64_t nr_blocks, uint64_t laid, uint64_t *region_id) +{ + struct ps3strgmngr_ctl_create_region create_region; + int ret; + + if (fd < 0) + return (-1); + + create_region.device_id = device_id; + create_region.start_block = start_block; + create_region.nr_blocks = nr_blocks; + create_region.laid = laid; + + ret = ioctl(fd, PS3STRGMNGR_CTL_CREATE_REGION, &create_region); + if (ret) + return (ret); + + *region_id = create_region.region_id; + + return (0); +} + +int +dev_delete_region(uint64_t device_id, uint64_t region_id) +{ + struct ps3strgmngr_ctl_delete_region delete_region; + int ret; + + if (fd < 0) + return (-1); + + delete_region.device_id = device_id; + delete_region.region_id = region_id; + + ret = ioctl(fd, PS3STRGMNGR_CTL_DELETE_REGION, &delete_region); + if (ret) + return (ret); + + return (0); +} + +int +dev_set_region_acl_entry(uint64_t device_id, uint64_t region_id, + uint64_t laid, uint64_t access_rights) +{ + struct ps3strgmngr_ctl_set_region_acl_entry set_region_acl_entry; + int ret; + + if (fd < 0) + return (-1); + + set_region_acl_entry.device_id = device_id; + set_region_acl_entry.region_id = region_id; + set_region_acl_entry.laid = laid; + set_region_acl_entry.access_rights = access_rights; + + ret = ioctl(fd, PS3STRGMNGR_CTL_SET_REGION_ACL_ENTRY, + &set_region_acl_entry); + if (ret) + return (ret); + + return (0); +} diff --git a/dev.h b/dev.h new file mode 100644 index 0000000..90cc3a0 --- /dev/null +++ b/dev.h @@ -0,0 +1,44 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _DEV_H +#define _DEV_H + +int dev_open(const char *device); + +void dev_close(void); + +int dev_get_devices(struct ps3strgmngr_device *dev, int max_devices); + +int dev_create_region(uint64_t device_id, uint64_t start_block, + uint64_t nr_blocks, uint64_t laid, uint64_t *region_id); + +int dev_delete_region(uint64_t device_id, uint64_t region_id); + +int dev_set_region_acl_entry(uint64_t device_id, uint64_t region_id, + uint64_t laid, uint64_t access_rights); + +#endif /* _DEV_H */ diff --git a/opts.h b/opts.h new file mode 100644 index 0000000..5caea35 --- /dev/null +++ b/opts.h @@ -0,0 +1,40 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _OPTS_H +#define _OPTS_H + +struct opts { + int do_help; + int do_version; + int do_verbose; + + const char *device; +}; + +extern struct opts opts; + +#endif /* _OPTS_H */ diff --git a/ps3sed.c b/ps3sed.c new file mode 100644 index 0000000..4ae06cd --- /dev/null +++ b/ps3sed.c @@ -0,0 +1,161 @@ +/*- + * Copyright (C) 2011, 2012 glevand + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer, + * without modification, immediately at the beginning of the file. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include +#include +#include +#include + +#include + +#include "opts.h" +#include "dev.h" +#include "cmd/cmd.h" + +static struct option long_opts[] = { + { "help", no_argument, NULL, 'h' }, + { "version", no_argument, NULL, 'V' }, + { "verbose", no_argument, NULL, 'v' }, + { "device", no_argument, NULL, 'd' }, + { NULL, 0, NULL, 0 } +}; + +struct opts opts = { + .device = "/dev/ps3strgmngr" +}; + +extern struct cmd cmd_create_region; +extern struct cmd cmd_delete_region; +extern struct cmd cmd_help; +extern struct cmd cmd_list; +extern struct cmd cmd_print_acl_entry; +extern struct cmd cmd_print_device; +extern struct cmd cmd_print_region; +extern struct cmd cmd_set_acl_entry; + +static struct cmd *cmdv[] = { + &cmd_create_region, + &cmd_delete_region, + &cmd_help, + &cmd_list, + &cmd_print_acl_entry, + &cmd_print_device, + &cmd_print_region, + &cmd_set_acl_entry, +}; + +static void +usage(void) +{ + exit(0); +} + +static void +version(void) +{ + exit(0); +} + +static int +parse_opts(int argc, char **argv, struct opts *opts) +{ + int c; + + while ((c = getopt_long(argc, argv, "hVvd:", long_opts, NULL)) != -1) { + switch (c) { + case 'h': + case '?': + opts->do_help = 1; + return (0); + break; + case 'V': + opts->do_version = 1; + return (0); + break; + case 'v': + opts->do_verbose++; + break; + case 'd': + opts->device = optarg; + break; + default: + fprintf(stderr, "invalid option specified: %c\n", c); + return (-1); + break; + } + } + + if (optind >= argc) { + fprintf(stderr, "no command specified\n"); + return (-1); + } + + return (0); +} + +int +main(int argc, char **argv) +{ + int ret; + + ret = parse_opts(argc, argv, &opts); + if (ret) + exit(255); + + if (opts.do_help) + usage(); + else if (opts.do_version) + version(); + + ret = dev_open(opts.device); + if (ret) { + perror("open device"); + exit(254); + } + + ret = cmd_exec(sizeof(cmdv) / sizeof(cmdv[0]), cmdv, + argc - optind, &argv[optind]); + switch (ret) { + case CMD_EOK: + break; + case CMD_EBADCMD: + fprintf(stderr, "invalid command specified\n"); + break; + case CMD_EINVAL: + fprintf(stderr, "invalid command argument(s) specified\n"); + break; + case CMD_EIO: + fprintf(stderr, "device error\n"); + break; + case CMD_EUNKNOWN: + fprintf(stderr, "unknown command error\n"); + break; + } + + dev_close(); + + exit(ret); +} -- 2.11.4.GIT