ps3dm_sm: set_del_encdec_key: fixed description for default key
[ps3dm-utils.git] / ps3dm_iim.c
blob49334158d7ce5b92272d9f691f578c356c8793f5
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 "ps3dm_proxy.h"
24 #include "ps3dm.h"
25 #include "ps3ss.h"
27 #define PS3DM_IIM_VERSION "0.0.1"
29 #define PS3DM_IIM_LAID 0x1070000002000001ull
30 #define PS3DM_IIM_PAID 0x1070000300000001ull
32 struct opts
34 char *device_name;
35 char *cmd;
36 int do_help;
37 int do_verbose;
38 int do_version;
41 static struct option long_opts[] = {
42 { "help", no_argument, NULL, 'h' },
43 { "verbose", no_argument, NULL, 'v' },
44 { "version", no_argument, NULL, 'V' },
45 { NULL, 0, NULL, 0 }
49 * usage
51 static void usage(void) {
52 fprintf(stderr,
53 "Usage: ps3dm_iim [OPTIONS] DEVICE COMMAND [ARGS]\n"
54 "\n"
55 "Options:\n"
56 " -h, --help Show this message and exit\n"
57 " -v, --verbose Increase verbosity\n"
58 " -V, --version Show version information and exit\n"
59 "Commands:\n"
60 " get_data_size INDEX Returns data size by index\n"
61 " get_data INDEX Returns data by index\n"
62 " INDEX:\n"
63 " 0x0 EID0\n"
64 " 0x4 EID4\n"
65 " 0x1000 metldr\n"
66 " get_cisd_size Returns cISD size\n"
67 "\n\n"
68 "Simple example: Get size of EID0:\n"
69 " ps3dm_iim /dev/ps3dmproxy get_data_size 0\n");
73 * version
75 static void version(void)
77 fprintf(stderr,
78 "ps3dm_iim " PS3DM_IIM_VERSION "\n"
79 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
80 "This is free software. You may redistribute copies of it "
81 "under the terms of\n"
82 "the GNU General Public License 2 "
83 "<http://www.gnu.org/licenses/gpl2.html>.\n"
84 "There is NO WARRANTY, to the extent permitted by law.\n");
88 * process_opts
90 static int process_opts(int argc, char **argv, struct opts *opts)
92 int c;
94 while ((c = getopt_long(argc, argv, "hvV", long_opts, NULL)) != -1) {
95 switch (c) {
96 case 'h':
97 case '?':
98 opts->do_help = 1;
99 return 0;
101 case 'v':
102 opts->do_verbose++;
103 break;
105 case 'V':
106 opts->do_version = 1;
107 return 0;
109 default:
110 fprintf(stderr, "Invalid command option: %c\n", c);
111 return -1;
115 if (optind >= argc) {
116 fprintf(stderr, "No device specified\n");
117 return -1;
120 opts->device_name = argv[optind];
121 optind++;
123 if (optind >= argc) {
124 fprintf(stderr, "No command specified\n");
125 return -1;
128 opts->cmd = argv[optind];
129 optind++;
131 return 0;
135 * cmd_get_data_size
137 static int cmd_get_data_size(int fd, struct opts *opts, int argc, char **argv)
139 uint64_t data_index;
140 char *endptr;
141 uint8_t buf[128];
142 struct ps3dm_hdr *dm_hdr;
143 struct ps3ss_hdr *ss_hdr;
144 struct ps3ss_iim_get_data_size *ss_iim_get_data_size;
145 int error;
147 if (optind >= argc) {
148 fprintf(stderr, "No data index specified\n");
149 return -1;
152 data_index = strtoul(argv[optind], &endptr, 0);
153 if (*endptr != '\0') {
154 fprintf(stderr, "Invalid data index specified: %s\n", argv[optind]);
155 return -1;
158 optind++;
160 memset(buf, 0, sizeof(buf));
161 dm_hdr = (struct ps3dm_hdr *) buf;
162 ss_hdr = (struct ps3ss_hdr *)(dm_hdr + 1);
163 ss_iim_get_data_size = (struct ps3ss_iim_get_data_size *)(ss_hdr + 1);
165 dm_hdr->request_id = 1;
166 dm_hdr->function_id = PS3SS_FID_IIM;
167 dm_hdr->request_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_data_size);
168 dm_hdr->response_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_data_size);
170 ss_hdr->packet_id = PS3SS_PID_IIM_GET_DATA_SIZE;
171 ss_hdr->function_id = PS3SS_FID_IIM;
172 ss_hdr->laid = PS3DM_IIM_LAID;
173 ss_hdr->paid = PS3DM_IIM_PAID;
175 ss_iim_get_data_size->index = data_index;
177 error = ps3dm_proxy_do_request(fd, dm_hdr, PS3DM_HDR_SIZE + dm_hdr->request_size,
178 dm_hdr, PS3DM_HDR_SIZE + dm_hdr->response_size);
180 if (error) {
181 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
182 } else if (ss_hdr->retval) {
183 fprintf(stderr, "%s: SS retval %d\n", opts->device_name, ss_hdr->retval);
184 error = -1;
185 } else {
186 fprintf(stdout, "0x%016lx\n", ss_iim_get_data_size->size);
189 return error;
193 * cmd_get_data
195 static int cmd_get_data(int fd, struct opts *opts, int argc, char **argv)
197 #define BUF_SIZE 0x900
199 uint64_t data_index;
200 char *endptr;
201 uint8_t buf[128 + BUF_SIZE];
202 struct ps3dm_hdr *dm_hdr;
203 struct ps3ss_hdr *ss_hdr;
204 struct ps3ss_iim_get_data *ss_iim_get_data;
205 uint64_t data_size;
206 int error, i;
208 if (optind >= argc) {
209 fprintf(stderr, "No data index specified\n");
210 return -1;
213 data_index = strtoul(argv[optind], &endptr, 0);
214 if (*endptr != '\0') {
215 fprintf(stderr, "Invalid data index specified: %s\n", argv[optind]);
216 return -1;
219 optind++;
221 memset(buf, 0, sizeof(buf));
222 dm_hdr = (struct ps3dm_hdr *) buf;
223 ss_hdr = (struct ps3ss_hdr *)(dm_hdr + 1);
224 ss_iim_get_data = (struct ps3ss_iim_get_data *)(ss_hdr + 1);
226 dm_hdr->request_id = 1;
227 dm_hdr->function_id = PS3SS_FID_IIM;
228 dm_hdr->request_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_data) +
229 BUF_SIZE + sizeof(uint64_t);
230 dm_hdr->response_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_data) +
231 BUF_SIZE + sizeof(uint64_t);
233 ss_hdr->packet_id = PS3SS_PID_IIM_GET_DATA;
234 ss_hdr->function_id = PS3SS_FID_IIM;
235 ss_hdr->laid = PS3DM_IIM_LAID;
236 ss_hdr->paid = PS3DM_IIM_PAID;
238 ss_iim_get_data->index = data_index;
239 ss_iim_get_data->buf_size = BUF_SIZE;
241 error = ps3dm_proxy_do_request(fd, dm_hdr, PS3DM_HDR_SIZE + dm_hdr->request_size,
242 dm_hdr, PS3DM_HDR_SIZE + dm_hdr->response_size);
244 if (error) {
245 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
246 } else if (ss_hdr->retval) {
247 fprintf(stderr, "%s: SS retval %d\n", opts->device_name, ss_hdr->retval);
248 error = -1;
249 } else {
250 data_size = *(uint64_t *) (ss_iim_get_data->buf + ss_iim_get_data->buf_size);
252 for (i = 0; i < data_size; i++)
253 fprintf(stdout, "%c", ss_iim_get_data->buf[i]);
256 return error;
258 #undef BUF_SIZE
262 * cmd_get_cisd_size
264 static int cmd_get_cisd_size(int fd, struct opts *opts, int argc, char **argv)
266 uint8_t buf[128];
267 struct ps3dm_hdr *dm_hdr;
268 struct ps3ss_hdr *ss_hdr;
269 struct ps3ss_iim_get_cisd_size *ss_iim_get_cisd_size;
270 int error;
272 memset(buf, 0, sizeof(buf));
273 dm_hdr = (struct ps3dm_hdr *) buf;
274 ss_hdr = (struct ps3ss_hdr *)(dm_hdr + 1);
275 ss_iim_get_cisd_size = (struct ps3ss_iim_get_cisd_size *)(ss_hdr + 1);
277 dm_hdr->request_id = 1;
278 dm_hdr->function_id = PS3SS_FID_IIM;
279 dm_hdr->request_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_cisd_size);
280 dm_hdr->response_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_iim_get_cisd_size);
282 ss_hdr->packet_id = PS3SS_PID_IIM_GET_CISD_SIZE;
283 ss_hdr->function_id = PS3SS_FID_IIM;
284 ss_hdr->laid = PS3DM_IIM_LAID;
285 ss_hdr->paid = PS3DM_IIM_PAID;
287 error = ps3dm_proxy_do_request(fd, dm_hdr, PS3DM_HDR_SIZE + dm_hdr->request_size,
288 dm_hdr, PS3DM_HDR_SIZE + dm_hdr->response_size);
290 if (error) {
291 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
292 } else if (ss_hdr->retval) {
293 fprintf(stderr, "%s: SS retval %d\n", opts->device_name, ss_hdr->retval);
294 error = -1;
295 } else {
296 fprintf(stdout, "0x%016lx\n", ss_iim_get_cisd_size->size);
299 return error;
303 * main
305 int main(int argc, char **argv)
307 struct opts opts;
308 int fd = 0, error = 0;
310 memset(&opts, 0, sizeof(opts));
312 if (process_opts(argc, argv, &opts)) {
313 usage();
314 error = 1;
315 goto done;
318 if (opts.do_help) {
319 usage();
320 goto done;
321 } else if (opts.do_version) {
322 version();
323 goto done;
326 fd = ps3dm_proxy_open(opts.device_name);
327 if (fd < 0) {
328 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
329 error = 2;
330 goto done;
333 if (!strcmp(opts.cmd, "get_data")) {
334 error = cmd_get_data(fd, &opts, argc, argv);
335 } else if (!strcmp(opts.cmd, "get_data_size")) {
336 error = cmd_get_data_size(fd, &opts, argc, argv);
337 } else if (!strcmp(opts.cmd, "get_cisd_size")) {
338 error = cmd_get_cisd_size(fd, &opts, argc, argv);
339 } else {
340 usage();
341 error = 1;
342 goto done;
345 if (error)
346 error = 3;
348 done:
350 if (fd >= 0)
351 ps3dm_proxy_close(fd);
353 exit(error);