ps3dm_sm: set_del_encdec_key: fixed description for default key
[ps3dm-utils.git] / ps3dm_get_repo_node_val.c
blob1df3ab5b3522f723f70b0289f86470bfd923fa0c
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 <stdint.h>
20 #include <string.h>
21 #include <getopt.h>
22 #include <errno.h>
24 #include "ps3dm_proxy.h"
26 #define PS3DM_GET_REPO_NODE_VAL_VERSION "0.0.1"
28 struct opts {
29 char *device_name;
30 uint64_t lpar_id;
31 uint64_t key[4];
32 int do_help;
33 int do_verbose;
34 int do_version;
37 static struct option long_opts[] = {
38 { "help", no_argument, NULL, 'h' },
39 { "verbose", no_argument, NULL, 'v' },
40 { "version", no_argument, NULL, 'V' },
41 { NULL, 0, NULL, 0 }
45 * usage
47 static void usage(void) {
48 fprintf(stderr,
49 "Usage: ps3dm_get_repo_node_val [OPTIONS] DEVICE LPARID KEY0 KEY1 KEY2 KEY3\n"
50 "\n"
51 "Options:\n"
52 " -h, --help Show this message and exit\n"
53 " -v, --verbose Increase verbosity\n"
54 " -V, --version Show version information and exit\n"
55 "\n\n"
56 "Simple example: Get value of repository node ss.laid.1:\n"
57 " ps3dm_get_repo_node_val /dev/ps3dmproxy 1 0x0000000073730000 "
58 "0x6c61696400000000 0x0000000000000001 0x0000000000000000\n");
62 * version
64 static void version(void)
66 fprintf(stderr,
67 "ps3dm_get_repo_node_val " PS3DM_GET_REPO_NODE_VAL_VERSION "\n"
68 "Copyright (C) 2011 graf_chokolo <grafchokolo@googlemail.com>\n"
69 "This is free software. You may redistribute copies of it "
70 "under the terms of\n"
71 "the GNU General Public License 2 "
72 "<http://www.gnu.org/licenses/gpl2.html>.\n"
73 "There is NO WARRANTY, to the extent permitted by law.\n");
77 * process_opts
79 static int process_opts(int argc, char **argv, struct opts *opts)
81 int c, i;
82 char *opt, *endptr;
84 while ((c = getopt_long(argc, argv, "hvV", long_opts, NULL)) != -1) {
85 switch (c) {
86 case 'h':
87 case '?':
88 opts->do_help = 1;
89 return 0;
91 case 'v':
92 opts->do_verbose++;
93 break;
95 case 'V':
96 opts->do_version = 1;
97 return 0;
99 default:
100 fprintf(stderr, "Invalid command option: %c\n", c);
101 return -1;
105 if (optind >= argc) {
106 fprintf(stderr, "No device specified\n");
107 return -1;
110 opts->device_name = argv[optind];
111 optind++;
113 if (optind >= argc) {
114 fprintf(stderr, "No lpar id specified\n");
115 return -1;
118 opt = argv[optind++];
119 opts->lpar_id = strtoull(opt, &endptr, 0);
120 if ((*opt == '\0') || (*endptr != '\0')) {
121 fprintf(stderr, "Invalid lpar id '%s'\n", opt);
122 return -1;
125 for (i = 0; i < 4; i++) {
126 if (optind >= argc) {
127 fprintf(stderr, "No key #%d specified\n", i);
128 return -1;
131 opt = argv[optind++];
132 opts->key[i] = strtoull(opt, &endptr, 0);
133 if ((*opt == '\0') || (*endptr != '\0')) {
134 fprintf(stderr, "Invalid key #%d '%s'\n", i, opt);
135 return -1;
139 return 0;
143 * main
145 int main(int argc, char **argv)
147 struct opts opts;
148 int fd = 0, error = 0;
149 uint64_t val[2];
151 memset(&opts, 0, sizeof(opts));
153 if (process_opts(argc, argv, &opts)) {
154 usage();
155 error = 1;
156 goto done;
159 if (opts.do_help) {
160 usage();
161 goto done;
162 } else if (opts.do_version) {
163 version();
164 goto done;
167 fd = ps3dm_proxy_open(opts.device_name);
168 if (fd < 0) {
169 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
170 error = 2;
171 goto done;
174 if (opts.do_verbose) {
175 fprintf(stderr, "lpar id 0x%016lx\n", opts.lpar_id);
177 fprintf(stderr, "repo node key 0x%016lx 0x%016lx 0x%016lx 0x%016lx\n",
178 opts.key[0], opts.key[1], opts.key[2], opts.key[3]);
181 error = ps3dm_proxy_get_repo_node_val(fd, opts.lpar_id, opts.key, val);
182 if (error) {
183 fprintf(stderr, "%s: %s\n", opts.device_name, strerror(errno));
184 error = 3;
185 goto done;
188 fprintf(stdout, "0x%016lx 0x%016lx\n", val[0], val[1]);
190 done:
192 if (fd >= 0)
193 ps3dm_proxy_close(fd);
195 exit(error);