-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetsecblk.c
108 lines (88 loc) · 2.4 KB
/
setsecblk.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
* Copyright (C) 2006 Ben Collins <[email protected]>
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <getopt.h>
#include "ugci.h"
static void usage(int exitval) __attribute__((__noreturn__));
static void usage(int exitval)
{
fprintf(exitval ? stderr : stdout, "Usage: setsecblk [--help] [--device id] <new string>\n");
exit(exitval);
}
static void print_secblk(int id, const char *msg,
unsigned char vals[UGCI_SEC_VALUES])
{
int t, ascii = 1;
printf("UGCI(%d): %s:", id, msg);
for (t = 0; t < UGCI_SEC_VALUES; t++) {
printf("%c%02x", t ? ':' : ' ', vals[t]);
if (vals[t] && !isprint(vals[t]))
ascii = 0;
}
printf("\n");
if (ascii) {
vals[UGCI_SEC_VALUES] = '\0';
printf(" '%s'\n", vals);
}
}
int main(int argc, char *argv[])
{
int rd, id = 0;
unsigned char vals[UGCI_SEC_VALUES + 1];
while (1) {
int c;
static struct option long_options[] = {
{"help", 0, NULL, 'h'},
{"device", 1, NULL, 'd'},
{ 0 },
};
c = getopt_long(argc, argv, "hd:", long_options, NULL);
if (c == -1)
break;
switch (c) {
case 'h':
usage(0);
break;
case 'd':
id = atoi(optarg);
break;
}
}
if (argc - optind != 1)
usage(1);
if (strlen(argv[optind]) > UGCI_SEC_VALUES) {
fprintf(stderr, "Security string is too long (max 14 chars)\n");
exit (1);
}
memset(vals, ' ', UGCI_SEC_VALUES);
memcpy(vals, argv[optind], strlen(argv[optind]));
rd = ugci_init(NULL, 0, 1);
printf("Detected %d UGCI device%s\n", rd, rd == 1 ? "" : "s");
if (!rd)
exit(0);
print_secblk(id, "New Block", vals);
if (ugci_set_secblk(id, vals)) {
fprintf(stderr, "UGCI(%d): ", id);
perror("ugci_set_secblk");
} else {
print_secblk(id, "Updated Block", vals);
}
exit(0);
}