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