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 /* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
52 void WriteBlock(unsigned int block_start
, unsigned int len
, unsigned char *buf
)
54 unsigned char temp_buf
[256];
55 if (block_start
& 0xFF) {
56 printf("moving stuff forward by %d bytes\n", block_start
& 0xFF);
57 memset(temp_buf
, 0xFF, block_start
& 0xFF);
58 memcpy(temp_buf
+ (block_start
& 0xFF), buf
, len
);
60 } else memcpy(temp_buf
, buf
, len
);
62 UsbCommand c
= {CMD_SETUP_WRITE
};
64 // printf("expected = %08x flush, ", ExpectedAddr);
67 for(i
= 0; i
< 240; i
+= 48) {
68 memcpy(c
.d
.asBytes
, temp_buf
+i
, 48);
74 c
.cmd
= CMD_FINISH_WRITE
;
75 c
.arg
[0] = block_start
;
77 printf("writing block %08x\r", c
.arg
[0]);
78 memcpy(c
.d
.asBytes
, temp_buf
+240, 16);
85 void LoadFlashFromFile(const char *file
, int start_addr
, int end_addr
)
87 FILE *f
= fopen(file
, "rb");
89 printf("couldn't open file\n");
95 if (memcmp(buf
, "\x7F" "ELF", 4) == 0) {
97 fseek(f
, 0, SEEK_SET
);
99 fread(&header
, 1, sizeof(header
), f
);
100 int count
= header
.e_phnum
;
101 printf("count=%d phoff=%x\n", count
, header
.e_phoff
);
104 for (i
=0; i
<header
.e_phnum
; i
++) {
105 fseek(f
, header
.e_phoff
+ i
* sizeof(Elf32_Phdr
), SEEK_SET
);
106 fread(&phdr
, 1, sizeof(phdr
), f
);
107 // printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
108 // phdr.p_type, phdr.p_offset, phdr.p_paddr, phdr.p_vaddr, phdr.p_filesz, phdr.p_memsz, phdr.p_flags, phdr.p_align);
109 if (phdr
.p_type
== PT_LOAD
&& phdr
.p_filesz
> 0 && phdr
.p_paddr
>= PHYSICAL_FLASH_START
110 && (phdr
.p_paddr
+ phdr
.p_filesz
) < PHYSICAL_FLASH_END
) {
111 printf("flashing offset=%x paddr=%x size=%x\n", phdr
.p_offset
, phdr
.p_paddr
, phdr
.p_filesz
);
112 if (phdr
.p_offset
== 0) {
113 printf("skipping forward 0x2000 because of strange linker thing\n");
114 phdr
.p_offset
+= 0x2000;
115 phdr
.p_paddr
+= 0x2000;
118 fseek(f
, phdr
.p_offset
, SEEK_SET
);
119 ExpectedAddr
= phdr
.p_paddr
;
120 while (ExpectedAddr
< (phdr
.p_paddr
+ phdr
.p_filesz
)) {
121 unsigned int bytes_to_read
= phdr
.p_paddr
+ phdr
.p_filesz
- ExpectedAddr
;
122 if (bytes_to_read
> 256) bytes_to_read
=256;
123 fread(QueuedToSend
, 1, bytes_to_read
, f
);
124 // printf("read %d bytes\n", bytes_to_read);
125 printf("WriteBlock(%x, %d, %p)\n", ExpectedAddr
, bytes_to_read
, QueuedToSend
);
126 WriteBlock(ExpectedAddr
, bytes_to_read
, QueuedToSend
);
127 ExpectedAddr
+= bytes_to_read
;
135 } else printf("Bad file format\n");
138 int PrepareFlash(struct partition
*p
, const char *filename
, unsigned int state
)
140 if(state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
141 UsbCommand c
= {CMD_START_FLASH
, {p
->start
, p
->end
, 0}};
143 /* Only send magic when flashing bootrom */
145 c
.arg
[2] = START_FLASH_MAGIC
;
152 fprintf(stderr
, "Warning: Your bootloader does not understand the new START_FLASH command\n");
153 fprintf(stderr
, " It is recommended that you update your bootloader\n\n");
157 LoadFlashFromFile(filename
, p
->start
, p
->end
);
161 unsigned int GetProxmarkState(void)
163 unsigned int state
= 0;
166 c
.cmd
= CMD_DEVICE_INFO
;
170 ReceiveCommand(&resp
);
172 * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
173 * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
174 * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
179 state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
181 case CMD_DEBUG_PRINT_STRING
:
182 state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
184 case CMD_DEVICE_INFO
:
188 fprintf(stderr
, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp
.cmd
);
196 unsigned int EnterFlashState(void)
198 unsigned int state
= GetProxmarkState();
200 if(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
201 /* Already in flash state, we're done. */
205 if(state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
206 fprintf(stderr
,"Entering flash-mode...\n");
208 bzero(&c
, sizeof(c
));
210 if( (state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
) && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
) ) {
211 /* New style handover: Send CMD_START_FLASH, which will reset the board and
212 * enter the bootrom on the next boot.
214 c
.cmd
= CMD_START_FLASH
;
216 fprintf(stderr
,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
217 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
219 /* Old style handover: Ask the user to press the button, then reset the board */
220 c
.cmd
= CMD_HARDWARE_RESET
;
222 fprintf(stderr
,"(Press and hold down button NOW if your bootloader requires it)\n");
223 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
228 while(!UsbConnect()) { Sleep(1000); }
233 while(!(devh
=OpenProxmark(0))) { sleep(1); }
235 fprintf(stderr
,"Found.\n");
237 return GetProxmarkState();
243 /* On first call, have *offset = -1, *length = 0; */
244 int find_next_area(const char *str
, int *offset
, int *length
)
246 if(*str
== '\0') return 0;
247 if((*offset
>= 0) && str
[*offset
+ *length
] == '\0') return 0;
248 *offset
+= 1 + *length
;
250 char *next_comma
= strchr(str
+ *offset
, ',');
251 if(next_comma
== NULL
) {
252 *length
= strlen(str
) - *offset
;
254 *length
= next_comma
-(str
+*offset
);
259 void do_flash(char **argv
) {
260 unsigned int state
= EnterFlashState();
262 if (!(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
)) {
263 fprintf(stderr
, "Proxmark would not enter flash state, abort\n");
267 int offset
=-1, length
=0;
268 int current_area
= 0;
269 while(find_next_area(argv
[1], &offset
, &length
)) {
271 struct partition
*p
= NULL
;
272 for (i
=0; i
<sizeof(partitions
)/sizeof(partitions
[0]); i
++) {
273 if (strncmp(partitions
[i
].name
, argv
[1] + offset
, length
) == 0) {
274 /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
275 * be written in full. The other names may be abbreviated.
277 if(!partitions
[i
].precious
|| (strlen(partitions
[i
].name
) == length
))
284 fprintf(stderr
, "Warning: area name '");
285 fwrite(argv
[1]+offset
, length
, 1, stderr
);
286 fprintf(stderr
, "' unknown, ignored\n");
288 fprintf(stderr
, "Flashing %s from %s\n", p
->name
, argv
[2+current_area
]);
289 PrepareFlash(p
, argv
[2+current_area
], state
);