ps3encdec_ioctl: new command kgenflash
[ps3encdec-utils.git] / ps3encdec_ioctl.c
blobd972dd846a966a2c871f48cb9f01f9121d895ad8
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 " kgenflash EdecKgenFlash\n"
57 "\n\n"
58 "Simple example: EdecKgen1:\n"
59 " ps3encdec_ioctl /dev/ps3encdec kgen1 0x00 0x01 0x00 0x30 0x72 0xA7 0x88 0xEC \\\n"
60 " 0xFC 0xA4 0x06 0x71 0x4C 0xB1 0x50 0xC9 0xFB 0xE0 0x06 0xC2 0x74 0xB5 \\\n"
61 " 0x84 0xC4 0xE6 0xBD 0x1E 0x55 0x4E 0x36 0xE9 0xC9 0xD6 0x09 0xBC 0xB4 \\\n"
62 " 0x79 0xA6 0xBC 0xDE 0x60 0xA5 0xB2 0x41 0xC7 0x15 0x68 0x68 0x82 0x1D \\\n"
63 " 0x8F 0xD6 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00\n");
67 * version
69 static void version(void)
71 fprintf(stderr,
72 "ps3encdec_ioctl " PS3ENCDEC_IOCTL_VERSION "\n"
73 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
74 "This is free software. You may redistribute copies of it "
75 "under the terms of\n"
76 "the GNU General Public License 2 "
77 "<http://www.gnu.org/licenses/gpl2.html>.\n"
78 "There is NO WARRANTY, to the extent permitted by law.\n");
82 * process_opts
84 static int process_opts(int argc, char **argv, struct opts *opts)
86 int c;
88 while ((c = getopt_long(argc, argv, "hvV", long_opts, NULL)) != -1) {
89 switch (c) {
90 case 'h':
91 case '?':
92 opts->do_help = 1;
93 return 0;
95 case 'v':
96 opts->do_verbose++;
97 break;
99 case 'V':
100 opts->do_version = 1;
101 return 0;
103 default:
104 fprintf(stderr, "Invalid command option: %c\n", c);
105 return -1;
109 if (optind >= argc) {
110 fprintf(stderr, "No device specified\n");
111 return -1;
114 opts->device_name = argv[optind];
115 optind++;
117 if (optind >= argc) {
118 fprintf(stderr, "No command specified\n");
119 return -1;
122 opts->cmd = argv[optind];
123 optind++;
125 return 0;
129 * cmd_kgen1
131 static int cmd_kgen1(int fd, struct opts *opts, int argc, char **argv)
133 uint32_t val;
134 uint64_t cmdbuf_size;
135 uint8_t cmdbuf[0x40];
136 uint8_t respbuf[0x50];
137 char *endptr;
138 int error, i;
140 if (optind >= argc) {
141 fprintf(stderr, "No ARG1 specified\n");
142 return -1;
145 cmdbuf_size = 0;
147 while (optind < argc) {
148 val = strtoul(argv[optind], &endptr, 0);
149 if ((*endptr != '\0') || (val > 0xff)) {
150 fprintf(stderr, "Invalid ARG1 specified: %s\n", argv[optind]);
151 return -1;
154 optind++;
156 cmdbuf[cmdbuf_size++] = val;
158 if (cmdbuf_size == 0x40)
159 break;
162 if (cmdbuf_size < 0x40) {
163 fprintf(stderr, "ARG1 should be of size 0x40 bytes\n");
164 return -1;
167 error = ps3encdec_dev_do_request(fd, 0x81, cmdbuf, sizeof(cmdbuf), respbuf, sizeof(respbuf));
169 if (error) {
170 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
171 } else {
172 for (i = 0; i < sizeof(respbuf); i++)
173 fprintf(stdout, "0x%02x ", respbuf[i]);
175 fprintf(stdout, "\n");
178 return error;
182 * cmd_kgenflash
184 static int cmd_kgenflash(int fd, struct opts *opts, int argc, char **argv)
186 int error;
188 error = ps3encdec_dev_do_request(fd, 0x84, NULL, 0, NULL, 0);
190 if (error)
191 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
193 return error;
197 * main
199 int main(int argc, char **argv)
201 struct opts opts;
202 int fd = 0, error = 0;
204 memset(&opts, 0, sizeof(opts));
206 if (process_opts(argc, argv, &opts)) {
207 usage();
208 error = 1;
209 goto done;
212 if (opts.do_help) {
213 usage();
214 goto done;
215 } else if (opts.do_version) {
216 version();
217 goto done;
220 fd = ps3encdec_dev_open(opts.device_name);
221 if (fd < 0) {
222 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
223 error = 2;
224 goto done;
227 if (!strcmp(opts.cmd, "kgen1")) {
228 error = cmd_kgen1(fd, &opts, argc, argv);
229 } else if (!strcmp(opts.cmd, "kgenflash")) {
230 error = cmd_kgenflash(fd, &opts, argc, argv);
231 } else {
232 usage();
233 error = 1;
234 goto done;
237 if (error)
238 error = 3;
240 done:
242 if (fd >= 0)
243 ps3encdec_dev_close(fd);
245 exit(error);