]> git.zerfleddert.de Git - proxmark3-svn/blob - client/flash.c
fix warning: implicit declaration of function ‘exit’
[proxmark3-svn] / client / flash.c
1 #ifdef WIN32
2 #include <windows.h>
3 #include <setupapi.h>
4 #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
5 BOOL UsbConnect(void);
6 #else
7 #include <proxusb.h>
8 #endif
9
10 #include <stdio.h>
11 #include <string.h>
12 #include <stdlib.h>
13 #include "flash.h"
14 #include "elf.h"
15
16 static uint32_t ExpectedAddr;
17 static uint8_t QueuedToSend[256];
18 static bool AllWritten;
19 #define PHYSICAL_FLASH_START 0x100000
20 #define PHYSICAL_FLASH_END 0x200000
21
22 void WaitForAck(void)
23 {
24 UsbCommand ack;
25 ReceiveCommand(&ack);
26 if (ack.cmd != CMD_ACK) {
27 printf("bad ACK\n");
28 exit(-1);
29 }
30 }
31
32 struct partition partitions[] = {
33 {0x100000, 0x102000, 1, "bootrom"},
34 {0x102000, 0x110000, 0, "fpga"},
35 {0x110000, 0x140000, 0, "os"},
36 {0, 0, 0, NULL},
37 };
38
39 void WriteBlock(unsigned int block_start, unsigned int len, unsigned char *buf)
40 {
41 unsigned char temp_buf[256];
42 if (block_start & 0xFF) {
43 printf("moving stuff forward by %d bytes\n", block_start & 0xFF);
44 memset(temp_buf, 0xFF, block_start & 0xFF);
45 memcpy(temp_buf + (block_start & 0xFF), buf, len - (block_start & 0xFF));
46 block_start &= ~0xFF;
47 } else {
48 memcpy(temp_buf, buf, len);
49 }
50
51 UsbCommand c = {CMD_SETUP_WRITE};
52
53 for (int i = 0; i < 240; i += 48) {
54 memcpy(c.d.asBytes, temp_buf+i, 48);
55 c.arg[0] = (i/4);
56 SendCommand(&c);
57 WaitForAck();
58 }
59
60 c.cmd = CMD_FINISH_WRITE;
61 c.arg[0] = block_start;
62
63 // printf("writing block %08x\r", c.arg[0]);
64 memcpy(c.d.asBytes, temp_buf+240, 16);
65 SendCommand(&c);
66 WaitForAck();
67
68 AllWritten = true;
69 }
70
71 void LoadFlashFromFile(const char *file, int start_addr, int end_addr)
72 {
73 FILE *f = fopen(file, "rb");
74 if (!f) {
75 printf("couldn't open file\n");
76 exit(-1);
77 }
78
79 char buf[4];
80 fread(buf, 1, 4, f);
81 if (memcmp(buf, "\x7F" "ELF", 4) == 0) {
82 fseek(f, 0, SEEK_SET);
83 Elf32_Ehdr header;
84 fread(&header, 1, sizeof(header), f);
85 int count = header.e_phnum;
86 printf("count=%d phoff=%x\n", count, header.e_phoff);
87 Elf32_Phdr phdr;
88
89 for (int i = 0; i < header.e_phnum; ++i) {
90 fseek(f, header.e_phoff + i * sizeof(Elf32_Phdr), SEEK_SET);
91 fread(&phdr, 1, sizeof(phdr), f);
92 printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
93 phdr.p_type, phdr.p_offset, phdr.p_paddr, phdr.p_vaddr, phdr.p_filesz, phdr.p_memsz, phdr.p_flags, phdr.p_align);
94 if (phdr.p_type == PT_LOAD && phdr.p_filesz > 0 && phdr.p_paddr >= PHYSICAL_FLASH_START
95 && (phdr.p_paddr + phdr.p_filesz) < PHYSICAL_FLASH_END) {
96 printf("flashing offset=%x paddr=%x size=%x\n", phdr.p_offset, phdr.p_paddr, phdr.p_filesz);
97 if (phdr.p_offset == 0) {
98 printf("skipping forward 0x2000 because of strange linker thing\n");
99 phdr.p_offset += 0x2000;
100 phdr.p_paddr += 0x2000;
101 phdr.p_filesz -= 0x2000;
102 phdr.p_memsz -= 0x2000;
103 }
104
105 fseek(f, phdr.p_offset, SEEK_SET);
106 ExpectedAddr = phdr.p_paddr;
107 while (ExpectedAddr < (phdr.p_paddr + phdr.p_filesz)) {
108 unsigned int bytes_to_read = phdr.p_paddr + phdr.p_filesz - ExpectedAddr;
109 if (bytes_to_read > 256)
110 bytes_to_read=256;
111 else
112 memset(QueuedToSend, 0xFF, 256);
113 fread(QueuedToSend, 1, bytes_to_read, f);
114 printf("WriteBlock(%x, %d, %02x %02x %02x %02x %02x %02x %02x %02x ... %02x %02x %02x %02x %02x %02x %02x %02x)\n", ExpectedAddr, bytes_to_read,
115 QueuedToSend[0], QueuedToSend[1], QueuedToSend[2], QueuedToSend[3],
116 QueuedToSend[4], QueuedToSend[5], QueuedToSend[6], QueuedToSend[7],
117 QueuedToSend[248], QueuedToSend[249], QueuedToSend[250], QueuedToSend[251],
118 QueuedToSend[252], QueuedToSend[253], QueuedToSend[254], QueuedToSend[255]);
119 WriteBlock(ExpectedAddr, 256, QueuedToSend);
120 ExpectedAddr += bytes_to_read;
121 }
122 }
123 }
124
125 fclose(f);
126 printf("\ndone.\n");
127 return;
128 } else printf("Bad file format\n");
129 }
130
131 int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
132 {
133 if (state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
134 UsbCommand c = {CMD_START_FLASH, {p->start, p->end, 0}};
135
136 /* Only send magic when flashing bootrom */
137 if (p->precious)
138 c.arg[2] = START_FLASH_MAGIC;
139 else
140 c.arg[2] = 0;
141
142 SendCommand(&c);
143 WaitForAck();
144 } else {
145 fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
146 fprintf(stderr, " It is recommended that you update your bootloader\n\n");
147 exit(0);
148 }
149
150 LoadFlashFromFile(filename, p->start, p->end);
151 return 1;
152 }
153
154 unsigned int GetProxmarkState(void)
155 {
156 unsigned int state = 0;
157
158 UsbCommand c;
159 c.cmd = CMD_DEVICE_INFO;
160 SendCommand(&c);
161
162 UsbCommand resp;
163 ReceiveCommand(&resp);
164 /* Three cases:
165 * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
166 * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
167 * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
168 */
169
170 switch (resp.cmd) {
171 case CMD_ACK:
172 state = DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM;
173 break;
174 case CMD_DEBUG_PRINT_STRING:
175 state = DEVICE_INFO_FLAG_CURRENT_MODE_OS;
176 break;
177 case CMD_DEVICE_INFO:
178 state = resp.arg[0];
179 break;
180 default:
181 fprintf(stderr, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp.cmd);
182 exit(-1);
183 break;
184 }
185
186 return state;
187 }
188
189 unsigned int EnterFlashState(void)
190 {
191 unsigned int state = GetProxmarkState();
192
193 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM) {
194 /* Already in flash state, we're done. */
195 return state;
196 }
197
198 if (state & DEVICE_INFO_FLAG_CURRENT_MODE_OS) {
199 fprintf(stderr,"Entering flash-mode...\n");
200 UsbCommand c;
201 bzero(&c, sizeof(c));
202
203 if ((state & DEVICE_INFO_FLAG_BOOTROM_PRESENT) && (state & DEVICE_INFO_FLAG_OSIMAGE_PRESENT)) {
204 /* New style handover: Send CMD_START_FLASH, which will reset the board and
205 * enter the bootrom on the next boot.
206 */
207 c.cmd = CMD_START_FLASH;
208 SendCommand(&c);
209 fprintf(stderr,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
210 fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
211 } else {
212 /* Old style handover: Ask the user to press the button, then reset the board */
213 c.cmd = CMD_HARDWARE_RESET;
214 SendCommand(&c);
215 fprintf(stderr,"(Press and hold down button NOW if your bootloader requires it)\n");
216 fprintf(stderr,"Waiting for Proxmark to reappear on USB... ");
217 }
218
219 #ifdef WIN32
220 Sleep(1000);
221 while (!UsbConnect()) { Sleep(1000); }
222 #else
223 CloseProxmark();
224 sleep(1);
225
226 while (!OpenProxmark(0)) { sleep(1); }
227 #endif
228 fprintf(stderr,"Found.\n");
229
230 return GetProxmarkState();
231 }
232
233 return 0;
234 }
235
236 /* On first call, have *offset = -1, *length = 0; */
237 int find_next_area(const char *str, int *offset, int *length)
238 {
239 if (*str == '\0') return 0;
240 if ((*offset >= 0) && str[*offset + *length] == '\0') return 0;
241 *offset += 1 + *length;
242
243 char *next_comma = strchr(str + *offset, ',');
244 if (next_comma == NULL) {
245 *length = strlen(str) - *offset;
246 } else {
247 *length = next_comma-(str+*offset);
248 }
249 return 1;
250 }
251
252 void do_flash(char **argv)
253 {
254 unsigned int state = EnterFlashState();
255
256 if (!(state & DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM)) {
257 fprintf(stderr, "Proxmark would not enter flash state, abort\n");
258 exit(-1);
259 }
260
261 int offset=-1, length=0;
262 int current_area = 0;
263 while (find_next_area(argv[1], &offset, &length)) {
264 struct partition *p = NULL;
265 for (int i = 0; i<sizeof(partitions)/sizeof(partitions[0]); ++i) {
266 if (strncmp(partitions[i].name, argv[1] + offset, length) == 0) {
267 /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
268 * be written in full. The other names may be abbreviated.
269 */
270 if(!partitions[i].precious || (strlen(partitions[i].name) == length))
271 p = &partitions[i];
272 break;
273 }
274 }
275
276 if (p == NULL) {
277 fprintf(stderr, "Warning: area name '");
278 fwrite(argv[1]+offset, length, 1, stderr);
279 fprintf(stderr, "' unknown, ignored\n");
280 } else {
281 fprintf(stderr, "Flashing %s from %s\n", p->name, argv[2+current_area]);
282 PrepareFlash(p, argv[2+current_area], state);
283 }
284 current_area++;
285 }
286 }
Impressum, Datenschutz