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