initial version
[ps3encdec-utils.git] / ps3encdec_ioctl.c
blob312566c31e00b40792d2a60d9a7c5aa757712dc4
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 "ps3encdec_dev.h"
25 #define PS3ENCDEC_IOCTL_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: ps3encdec_ioctl [OPTIONS] DEVICE [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 " kgen1 ARG1 EdecKgen1\n"
56 "\n\n"
57 "Simple example: EdecKgen1:\n"
58 " ps3encdec_ioctl /dev/ps3encdec kgen1 0x00 0x01 0x00 0x30 0x72 0xA7 0x88 0xEC \\\n"
59 " 0xFC 0xA4 0x06 0x71 0x4C 0xB1 0x50 0xC9 0xFB 0xE0 0x06 0xC2 0x74 0xB5 \\\n"
60 " 0x84 0xC4 0xE6 0xBD 0x1E 0x55 0x4E 0x36 0xE9 0xC9 0xD6 0x09 0xBC 0xB4 \\\n"
61 " 0x79 0xA6 0xBC 0xDE 0x60 0xA5 0xB2 0x41 0xC7 0x15 0x68 0x68 0x82 0x1D \\\n"
62 " 0x8F 0xD6 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00\n");
66 * version
68 static void version(void)
70 fprintf(stderr,
71 "ps3encdec_ioctl " PS3ENCDEC_IOCTL_VERSION "\n"
72 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
73 "This is free software. You may redistribute copies of it "
74 "under the terms of\n"
75 "the GNU General Public License 2 "
76 "<http://www.gnu.org/licenses/gpl2.html>.\n"
77 "There is NO WARRANTY, to the extent permitted by law.\n");
81 * process_opts
83 static int process_opts(int argc, char **argv, struct opts *opts)
85 int c;
87 while ((c = getopt_long(argc, argv, "hvV", long_opts, NULL)) != -1) {
88 switch (c) {
89 case 'h':
90 case '?':
91 opts->do_help = 1;
92 return 0;
94 case 'v':
95 opts->do_verbose++;
96 break;
98 case 'V':
99 opts->do_version = 1;
100 return 0;
102 default:
103 fprintf(stderr, "Invalid command option: %c\n", c);
104 return -1;
108 if (optind >= argc) {
109 fprintf(stderr, "No device specified\n");
110 return -1;
113 opts->device_name = argv[optind];
114 optind++;
116 if (optind >= argc) {
117 fprintf(stderr, "No command specified\n");
118 return -1;
121 opts->cmd = argv[optind];
122 optind++;
124 return 0;
128 * cmd_kgen1
130 static int cmd_kgen1(int fd, struct opts *opts, int argc, char **argv)
132 uint32_t val;
133 uint64_t cmdbuf_size;
134 uint8_t cmdbuf[0x40];
135 uint8_t respbuf[0x50];
136 char *endptr;
137 int error, i;
139 if (optind >= argc) {
140 fprintf(stderr, "No ARG1 specified\n");
141 return -1;
144 cmdbuf_size = 0;
146 while (optind < argc) {
147 val = strtoul(argv[optind], &endptr, 0);
148 if ((*endptr != '\0') || (val > 0xff)) {
149 fprintf(stderr, "Invalid ARG1 specified: %s\n", argv[optind]);
150 return -1;
153 optind++;
155 cmdbuf[cmdbuf_size++] = val;
157 if (cmdbuf_size == 0x40)
158 break;
161 if (cmdbuf_size < 0x40) {
162 fprintf(stderr, "ARG1 should be of size 0x40 bytes\n");
163 return -1;
166 error = ps3encdec_dev_do_request(fd, 0x81, cmdbuf, sizeof(cmdbuf), respbuf, sizeof(respbuf));
168 if (error) {
169 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
170 } else {
171 for (i = 0; i < sizeof(respbuf); i++)
172 fprintf(stdout, "0x%02x ", respbuf[i]);
174 fprintf(stdout, "\n");
177 return error;
180 * main
182 int main(int argc, char **argv)
184 struct opts opts;
185 int fd = 0, error = 0;
187 memset(&opts, 0, sizeof(opts));
189 if (process_opts(argc, argv, &opts)) {
190 usage();
191 error = 1;
192 goto done;
195 if (opts.do_help) {
196 usage();
197 goto done;
198 } else if (opts.do_version) {
199 version();
200 goto done;
203 fd = ps3encdec_dev_open(opts.device_name);
204 if (fd < 0) {
205 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
206 error = 2;
207 goto done;
210 if (!strcmp(opts.cmd, "kgen1")) {
211 error = cmd_kgen1(fd, &opts, argc, argv);
212 } else {
213 usage();
214 error = 1;
215 goto done;
218 if (error)
219 error = 3;
221 done:
223 if (fd >= 0)
224 ps3encdec_dev_close(fd);
226 exit(error);