From 3e3c7ed35913fbb84344e5d42aaa34aa2043314b Mon Sep 17 00:00:00 2001 From: glevand Date: Mon, 10 Sep 2012 10:19:52 -0800 Subject: [PATCH] initial import --- Makefile | 17 ++++++++ dev.c | 77 +++++++++++++++++++++++++++++++++++++ dev.h | 33 ++++++++++++++++ ps3lv1call.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100644 Makefile create mode 100644 dev.c create mode 100644 dev.h create mode 100644 ps3lv1call.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e2e548a --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ + +CC=gcc +CFLAGS=-O2 -Wall -g +LDFLAGS= + +all: ps3lv1call + +ps3lv1call: ps3lv1call.o dev.o + $(CC) $(LDFLAGS) -o $@ $^ + +%.o: %.c + $(CC) $(CFLAGS) -c $< + +.PHONY: clean +clean: + rm -f dev.o + rm -f ps3lv1call.o ps3lv1call diff --git a/dev.c b/dev.c new file mode 100644 index 0000000..1a06879 --- /dev/null +++ b/dev.c @@ -0,0 +1,77 @@ +/*- + * Copyright (C) 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 + +/* + * dev_do_lv1call + */ +int dev_do_lv1call(const char *device, uint64_t num_lv1call, + uint64_t *in_args, int num_in_args, uint64_t *out_args, int64_t *result) +{ + int fd; + uint64_t buf[9]; + int len, n; + + if (num_in_args > 8) + return (-1); + + fd = open(device, O_RDWR); + if (fd < 0) + return (-1); + + buf[0] = num_lv1call; + memcpy(buf + 1, in_args, num_in_args * sizeof(uint64_t)); + + len = (1 + num_in_args) * sizeof(uint64_t); + + n = write(fd, buf, len); + if (n != len) { + close(fd); + return (-1); + } + + n = read(fd, buf, sizeof(buf)); + if (n < sizeof(int64_t)) { + close(fd); + return (-1); + } + + close(fd); + + n = (n - sizeof(int64_t)) / sizeof(uint64_t); + + *result = (int64_t) buf[0]; + memcpy(out_args, buf + 1, n * sizeof(uint64_t)); + + return (n); +} diff --git a/dev.h b/dev.h new file mode 100644 index 0000000..2b51740 --- /dev/null +++ b/dev.h @@ -0,0 +1,33 @@ +/*- + * Copyright (C) 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_do_lv1call(const char *device, uint64_t num_lv1call, + uint64_t *in_args, int num_in_args, uint64_t *out_args, int64_t *result); + +#endif diff --git a/ps3lv1call.c b/ps3lv1call.c new file mode 100644 index 0000000..a070212 --- /dev/null +++ b/ps3lv1call.c @@ -0,0 +1,124 @@ +/*- + * Copyright (C) 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" + +static struct option long_opts[] = { + { "device", no_argument, NULL, 'd' }, + { NULL, 0, NULL, 0 } +}; + +static const char *device = "/dev/ps3lv1call"; +static uint64_t num_lv1call; +static uint64_t in_args[8]; +static int num_in_args; + +/* + * parse_opts + */ +static int parse_opts(int argc, char **argv) +{ + int c, i; + char *endptr; + + while ((c = getopt_long(argc, argv, "d:", long_opts, NULL)) != -1) { + switch (c) { + case 'd': + device = optarg; + break; + default: + fprintf(stderr, "invalid option specified: %c\n", c); + return (-1); + break; + } + } + + if (optind >= argc) { + fprintf(stderr, "no lv1 call number specified\n"); + return (-1); + } + + num_lv1call = strtoull(argv[optind], &endptr, 0); + if ((*endptr != '\0') || (num_lv1call > 255)) { + fprintf(stderr, "invalid lv1 call number specified: %s\n", argv[optind]); + return (-1); + } + + optind++; + + num_in_args = argc - optind; + if (num_in_args > 8) { + fprintf(stderr, "too many lv1 call input arguments specified\n"); + return (-1); + } + + for (i = 0; i < num_in_args; i++) { + in_args[i] = strtoull(argv[optind + i], &endptr, 0); + if (*endptr != '\0') { + fprintf(stderr, "invalid lv1 call input argument specified: %s\n", + argv[optind + i]); + return (-1); + } + } + + return (0); +} + +/* + * main + */ +int main(int argc, char **argv) +{ + uint64_t out_args[7]; + int64_t result; + int n, i, ret; + + ret = parse_opts(argc, argv); + if (ret) + return (255); + + n = dev_do_lv1call(device, num_lv1call, in_args, num_in_args, out_args, &result); + if (n < 0) { + fprintf(stderr, "device error %d\n", errno); + return (1); + } + + if (result) { + fprintf(stderr, "lv1 call %llu error %lld\n", num_lv1call, result); + return (2); + } + + for (i = 0; i < n; i++) + fprintf(stdout, "%016llx%s", out_args[i], i == (n - 1) ? "\n" : " "); + + return (0); +} -- 2.11.4.GIT