ps3dm_sm: set_del_encdec_key: fixed description for default key
[ps3dm-utils.git] / ps3dm_srtc.c
blob51999bc58e37613851664da1e5872f6fb90a3e9a
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_SRTC_VERSION "0.0.1"
29 #define PS3DM_SRTC_LAID 0x1070000002000001ull
30 #define PS3DM_SRTC_PAID 0x10700003ff000001ull
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_srtc [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_time PARAM Returns RTC time\n"
61 "\n\n"
62 "Simple example: Get RTC time:\n"
63 " ps3dm_srtc /dev/ps3dmproxy get_time 0\n");
67 * version
69 static void version(void)
71 fprintf(stderr,
72 "ps3dm_srtc " PS3DM_SRTC_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_get_time
131 static int cmd_get_time(int fd, struct opts *opts, int argc, char **argv)
133 uint64_t param;
134 char *endptr;
135 uint8_t buf[128];
136 struct ps3dm_hdr *dm_hdr;
137 struct ps3ss_hdr *ss_hdr;
138 struct ps3ss_srtc_get_time *ss_srtc_get_time;
139 int error;
141 if (optind >= argc) {
142 fprintf(stderr, "No parameter specified\n");
143 return -1;
146 param = strtoul(argv[optind], &endptr, 0);
147 if (*endptr != '\0') {
148 fprintf(stderr, "Invalid parameter specified: %s\n", argv[optind]);
149 return -1;
152 optind++;
154 memset(buf, 0, sizeof(buf));
155 dm_hdr = (struct ps3dm_hdr *) buf;
156 ss_hdr = (struct ps3ss_hdr *)(dm_hdr + 1);
157 ss_srtc_get_time = (struct ps3ss_srtc_get_time *)(ss_hdr + 1);
159 dm_hdr->request_id = 1;
160 dm_hdr->function_id = PS3SS_FID_SRTC;
161 dm_hdr->request_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_srtc_get_time);
162 dm_hdr->response_size = PS3SS_HDR_SIZE + sizeof(struct ps3ss_srtc_get_time);
164 ss_hdr->packet_id = PS3SS_PID_SRTC_GET_TIME;
165 ss_hdr->function_id = PS3SS_FID_SRTC;
166 ss_hdr->laid = PS3DM_SRTC_LAID;
167 ss_hdr->paid = PS3DM_SRTC_PAID;
169 ss_srtc_get_time->field0 = param;
171 error = ps3dm_proxy_do_request(fd, dm_hdr, PS3DM_HDR_SIZE + dm_hdr->request_size,
172 dm_hdr, PS3DM_HDR_SIZE + dm_hdr->response_size);
174 if (error) {
175 fprintf(stderr, "%s: %s\n", opts->device_name, strerror(errno));
176 } else if (ss_hdr->retval != 0) {
177 fprintf(stderr, "%s: SS retval %d\n", opts->device_name, ss_hdr->retval);
178 error = -1;
179 } else {
180 fprintf(stdout, "0x%016lx 0x%016lx\n", ss_srtc_get_time->field8,
181 ss_srtc_get_time->field10);
183 fprintf(stdout, "\n");
186 return error;
190 * main
192 int main(int argc, char **argv)
194 struct opts opts;
195 int fd = 0, error = 0;
197 memset(&opts, 0, sizeof(opts));
199 if (process_opts(argc, argv, &opts)) {
200 usage();
201 error = 1;
202 goto done;
205 if (opts.do_help) {
206 usage();
207 goto done;
208 } else if (opts.do_version) {
209 version();
210 goto done;
213 fd = ps3dm_proxy_open(opts.device_name);
214 if (fd < 0) {
215 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
216 error = 2;
217 goto done;
220 if (!strcmp(opts.cmd, "get_time")) {
221 error = cmd_get_time(fd, &opts, argc, argv);
222 } else {
223 usage();
224 error = 1;
225 goto done;
228 if (error)
229 error = 3;
231 done:
233 if (fd >= 0)
234 ps3dm_proxy_close(fd);
236 exit(error);