forked from Agile-IoT/iotp-riot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
189 lines (162 loc) · 4.61 KB
/
main.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* main.c
*
* Created on: Feb 6, 2018
* Author: Eduard Brehm
*/
#include <stdlib.h>
#include <stdio.h>
#include "eid.h"
#include <string.h>
#include <xtimer.h>
#include "shell.h"
#include "periph/flashpage.h"
char stack[THREAD_STACKSIZE_MAIN];
int IK_SIZE=32;
int TS_SIZE=6;
char ik[32 + 1];
char ts_str[6 + 1];
static void read_mem(void *addr, size_t len, char* output)
{
unsigned pos = 0;
uint8_t *mem = (uint8_t *)addr;
for (unsigned i = 0; i < len; i++) {
output[pos] = (char) mem[pos];
pos++;
}
}
int read_ik(int argc, char **argv) {
if(argc > 1) {
void *addr;
int page;
addr = flashpage_addr(atoi(argv[1]));
uint8_t page_mem[FLASHPAGE_SIZE];
page = flashpage_page(addr);
flashpage_read(page, page_mem);
// printf("Read flash page %i into local page buffer\n", page);
// puts("Local page buffer:");
ik[IK_SIZE] = '\0';
read_mem(page_mem, IK_SIZE, ik);
printf("%s\n", ik);
}
return 0;
}
int read_ts(int argc, char **argv) {
if(argc > 1) {
void *addr;
int page;
addr = flashpage_addr(atoi(argv[1]));
uint8_t page_mem[FLASHPAGE_SIZE];
page = flashpage_page(addr);
flashpage_read(page, page_mem);
// printf("Read flash page %i into local page buffer\n", page);
// puts("Local page buffer:");
ts_str[TS_SIZE] = '\0';
read_mem(page_mem, TS_SIZE, ts_str);
printf("%s\n", ts_str);
}
return 0;
}
int read_internal(int argc, char **argv) {
if(argc > 2) {
void *addr;
int page;
addr = flashpage_addr(atoi(argv[1]));
uint8_t page_mem[FLASHPAGE_SIZE];
page = flashpage_page(addr);
flashpage_read(page, page_mem);
// printf("Read flash page %i into local page buffer\n", page);
// puts("Local page buffer:");
int size = atoi(argv[2]);
char out[size + 1];
out[size] = '\0';
read_mem(page_mem, size, out);
printf("%s\n", out);
}
return 0;
}
int write_internal(int argc, char **argv) {
if(argc > 2) {
uint8_t page_mem[FLASHPAGE_SIZE];
void *addr;
void *data = argv[1];
int page;
addr = flashpage_addr(atoi(argv[2]));
page = flashpage_page(addr);
flashpage_write(page, NULL);
printf("successfully erased page %i (addr %p)\n",
page, flashpage_addr(page));
memcpy(&page_mem, data, strlen(data));
if (flashpage_write_and_verify(page, page_mem) != FLASHPAGE_OK) {
printf("error: verification for page %i failed\n", page);
return 1;
}
printf("wrote local page to flash page %i at addr %p\n",
page, flashpage_addr(page));
return 0;
}
return 1;
}
int genEID(int argc, char **argv)
{
(void) argc;
if(argc > 2) {
char *ikString = argv[1];
int scaler = 0;
int beacon_time_seconds = atoi(argv[2]);
uint8_t eid[16] = {0};
generateEID(ikString, scaler, beacon_time_seconds, eid);
printf("Generated OTP: ");
printHex(eid, 8);
} else {
printf("Too few arguments\n");
}
return 0;
}
static int cmd_info(int argc, char **argv)
{
(void)argc;
(void)argv;
printf("Flash start addr:\t0x%08x\n", (int)CPU_FLASH_BASE);
printf("Page size:\t\t%i\n", (int)FLASHPAGE_SIZE);
printf("Number of pages:\t%i\n", (int)FLASHPAGE_NUMOF);
return 0;
}
static int run_otp(int argc, char **argv)
{
if(argc > 2) {
char buffer[16 + 1] = {0};
while(true) {
char *readik[2] = {"read_internal", argv[1]};
read_ik(2, readik);
char *readts[3] = {"read_internal", argv[2]};
read_ts(2, readts);
int ts = atoi(ts_str);
printf("identity key found: %s\n", ik);
printf("counter found: %d\n", ts);
buffer[16] = '\0';
sprintf(buffer, "%d", ts++);
char *gen[3] = {"genEID", ik, buffer};
genEID(3, gen);
sprintf(buffer, "%d", ts);
char *write[3] = {"write_internal", buffer, argv[2]};
write_internal(3, write);
xtimer_sleep(5);
}
}
return 0;
}
static const shell_command_t commands[] = {
{ "geneid", "Generates an EID", genEID },
{ "info", "Show information about pages", cmd_info },
{ "write_internal", "Write a string to internal flashpage", write_internal },
{ "read_internal", "Read internal flashpage", read_internal },
{ "run_otp", "Write a string to internal flashpage", run_otp },
{ NULL, NULL, NULL }
};
int main (void) {
char line_buf[SHELL_DEFAULT_BUFSIZE];
cmd_info(0, NULL);
shell_run(commands, line_buf, SHELL_DEFAULT_BUFSIZE);
return 0;
}