]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/flash.c
alphabet ;)
[proxmark3-svn] / client / flash.c
CommitLineData
6e4d4ee6 1#ifdef WIN32
2#include <windows.h>
3#include <setupapi.h>
6e4d4ee6 4#define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
5BOOL UsbConnect(void);
6#else
7fe9b0b7 7#include <proxusb.h>
6e4d4ee6 8#endif
7fe9b0b7 9
6e4d4ee6 10#include <stdio.h>
6e4d4ee6 11#include <string.h>
83a9b236 12#include <stdlib.h>
6e4d4ee6 13#include "flash.h"
2cab856f 14#include "elf.h"
6e4d4ee6 15
16static uint32_t ExpectedAddr;
17static uint8_t QueuedToSend[256];
18static bool AllWritten;
19#define PHYSICAL_FLASH_START 0x100000
2cab856f 20#define PHYSICAL_FLASH_END 0x200000
6e4d4ee6 21
7fe9b0b7 22void WaitForAck(void)
23{
24 UsbCommand ack;
25 ReceiveCommand(&ack);
26 if (ack.cmd != CMD_ACK) {
27 printf("bad ACK\n");
28 exit(-1);
29 }
6e4d4ee6 30}
31
32struct partition partitions[] = {
7fe9b0b7 33 {0x100000, 0x102000, 1, "bootrom"},
34 {0x102000, 0x110000, 0, "fpga"},
35 {0x110000, 0x140000, 0, "os"},
36 {0, 0, 0, NULL},
6e4d4ee6 37};
38
2cab856f 39void WriteBlock(unsigned int block_start, unsigned int len, unsigned char *buf)
6e4d4ee6 40{
7fe9b0b7 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;
6e4d4ee6 69}
70
2cab856f 71void LoadFlashFromFile(const char *file, int start_addr, int end_addr)
6e4d4ee6 72{
7fe9b0b7 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;
2cab856f 103 }
6e4d4ee6 104
7fe9b0b7 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");
6e4d4ee6 129}
130
131int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
132{
7fe9b0b7 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;
6e4d4ee6 152}
153
154unsigned int GetProxmarkState(void)
155{
7fe9b0b7 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;
6e4d4ee6 187}
188
189unsigned int EnterFlashState(void)
190{
7fe9b0b7 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 }
6e4d4ee6 218
219#ifdef WIN32
7fe9b0b7 220 Sleep(1000);
221 while (!UsbConnect()) { Sleep(1000); }
6e4d4ee6 222#else
7fe9b0b7 223 CloseProxmark();
224 sleep(1);
6e4d4ee6 225
7fe9b0b7 226 while (!OpenProxmark(0)) { sleep(1); }
6e4d4ee6 227#endif
7fe9b0b7 228 fprintf(stderr,"Found.\n");
229
230 return GetProxmarkState();
231 }
6e4d4ee6 232
7fe9b0b7 233 return 0;
6e4d4ee6 234}
235
236/* On first call, have *offset = -1, *length = 0; */
237int find_next_area(const char *str, int *offset, int *length)
238{
7fe9b0b7 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;
6e4d4ee6 250}
251
7fe9b0b7 252void 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 }
6e4d4ee6 286}
Impressum, Datenschutz