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