5 #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
22 #include "proxmark3.h"
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
33 void WaitForAck(void) {
36 if(ack
.cmd
!= CMD_ACK
) {
42 struct partition partitions
[] = {
43 {0x100000, 0x102000, 1, "bootrom"},
44 {0x102000, 0x110000, 0, "fpga"},
45 {0x110000, 0x140000, 0, "os"},
49 void WriteBlock(unsigned int block_start
, unsigned int len
, unsigned char *buf
)
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
);
57 } else memcpy(temp_buf
, buf
, len
);
59 UsbCommand c
= {CMD_SETUP_WRITE
};
62 for(i
= 0; i
< 240; i
+= 48) {
63 memcpy(c
.d
.asBytes
, temp_buf
+i
, 48);
69 c
.cmd
= CMD_FINISH_WRITE
;
70 c
.arg
[0] = block_start
;
72 // printf("writing block %08x\r", c.arg[0]);
73 memcpy(c
.d
.asBytes
, temp_buf
+240, 16);
80 void LoadFlashFromFile(const char *file
, int start_addr
, int end_addr
)
82 FILE *f
= fopen(file
, "rb");
84 printf("couldn't open file\n");
90 if (memcmp(buf
, "\x7F" "ELF", 4) == 0) {
92 fseek(f
, 0, SEEK_SET
);
94 fread(&header
, 1, sizeof(header
), f
);
95 int count
= header
.e_phnum
;
96 printf("count=%d phoff=%x\n", count
, header
.e_phoff
);
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;
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)
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
;
138 } else printf("Bad file format\n");
141 int PrepareFlash(struct partition
*p
, const char *filename
, unsigned int state
)
143 if(state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
144 UsbCommand c
= {CMD_START_FLASH
, {p
->start
, p
->end
, 0}};
146 /* Only send magic when flashing bootrom */
148 c
.arg
[2] = START_FLASH_MAGIC
;
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");
160 LoadFlashFromFile(filename
, p
->start
, p
->end
);
164 unsigned int GetProxmarkState(void)
166 unsigned int state
= 0;
169 c
.cmd
= CMD_DEVICE_INFO
;
173 ReceiveCommand(&resp
);
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
182 state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
184 case CMD_DEBUG_PRINT_STRING
:
185 state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
187 case CMD_DEVICE_INFO
:
191 fprintf(stderr
, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp
.cmd
);
199 unsigned int EnterFlashState(void)
201 unsigned int state
= GetProxmarkState();
203 if(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
204 /* Already in flash state, we're done. */
208 if(state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
209 fprintf(stderr
,"Entering flash-mode...\n");
211 bzero(&c
, sizeof(c
));
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.
217 c
.cmd
= CMD_START_FLASH
;
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... ");
222 /* Old style handover: Ask the user to press the button, then reset the board */
223 c
.cmd
= CMD_HARDWARE_RESET
;
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... ");
231 while(!UsbConnect()) { Sleep(1000); }
236 while(!(devh
=OpenProxmark(0))) { sleep(1); }
238 fprintf(stderr
,"Found.\n");
240 return GetProxmarkState();
246 /* On first call, have *offset = -1, *length = 0; */
247 int find_next_area(const char *str
, int *offset
, int *length
)
249 if(*str
== '\0') return 0;
250 if((*offset
>= 0) && str
[*offset
+ *length
] == '\0') return 0;
251 *offset
+= 1 + *length
;
253 char *next_comma
= strchr(str
+ *offset
, ',');
254 if(next_comma
== NULL
) {
255 *length
= strlen(str
) - *offset
;
257 *length
= next_comma
-(str
+*offset
);
262 void do_flash(char **argv
) {
263 unsigned int state
= EnterFlashState();
265 if (!(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
)) {
266 fprintf(stderr
, "Proxmark would not enter flash state, abort\n");
270 int offset
=-1, length
=0;
271 int current_area
= 0;
272 while(find_next_area(argv
[1], &offset
, &length
)) {
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.
280 if(!partitions
[i
].precious
|| (strlen(partitions
[i
].name
) == length
))
287 fprintf(stderr
, "Warning: area name '");
288 fwrite(argv
[1]+offset
, length
, 1, stderr
);
289 fprintf(stderr
, "' unknown, ignored\n");
291 fprintf(stderr
, "Flashing %s from %s\n", p
->name
, argv
[2+current_area
]);
292 PrepareFlash(p
, argv
[2+current_area
], state
);