4 #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
19 static uint32_t ExpectedAddr
;
20 static uint8_t QueuedToSend
[256];
21 static BOOL AllWritten
;
22 #define PHYSICAL_FLASH_START 0x100000
23 #define PHYSICAL_FLASH_END 0x200000
29 if (ack
.cmd
!= CMD_ACK
) {
35 struct partition partitions
[] = {
36 {0x100000, 0x102000, 1, "bootrom"},
37 {0x102000, 0x110000, 0, "fpga"},
38 {0x110000, 0x140000, 0, "os"},
42 void WriteBlock(unsigned int block_start
, unsigned int len
, unsigned char *buf
)
44 unsigned char temp_buf
[256];
45 if (block_start
& 0xFF) {
46 printf("moving stuff forward by %d bytes\n", block_start
& 0xFF);
47 memset(temp_buf
, 0xFF, block_start
& 0xFF);
48 memcpy(temp_buf
+ (block_start
& 0xFF), buf
, len
- (block_start
& 0xFF));
51 memcpy(temp_buf
, buf
, len
);
54 UsbCommand c
= {CMD_SETUP_WRITE
};
56 for (int i
= 0; i
< 240; i
+= 48) {
57 memcpy(c
.d
.asBytes
, temp_buf
+i
, 48);
63 c
.cmd
= CMD_FINISH_WRITE
;
64 c
.arg
[0] = block_start
;
66 // printf("writing block %08x\r", c.arg[0]);
67 memcpy(c
.d
.asBytes
, temp_buf
+240, 16);
74 void LoadFlashFromFile(const char *file
, int start_addr
, int end_addr
)
76 FILE *f
= fopen(file
, "rb");
78 printf("couldn't open file\n");
84 if (memcmp(buf
, "\x7F" "ELF", 4) == 0) {
85 fseek(f
, 0, SEEK_SET
);
87 fread(&header
, 1, sizeof(header
), f
);
88 int count
= header
.e_phnum
;
89 printf("count=%d phoff=%x\n", count
, header
.e_phoff
);
92 for (int i
= 0; i
< header
.e_phnum
; ++i
) {
93 fseek(f
, header
.e_phoff
+ i
* sizeof(Elf32_Phdr
), SEEK_SET
);
94 fread(&phdr
, 1, sizeof(phdr
), f
);
95 printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
96 phdr
.p_type
, phdr
.p_offset
, phdr
.p_paddr
, phdr
.p_vaddr
, phdr
.p_filesz
, phdr
.p_memsz
, phdr
.p_flags
, phdr
.p_align
);
97 if (phdr
.p_type
== PT_LOAD
&& phdr
.p_filesz
> 0 && phdr
.p_paddr
>= PHYSICAL_FLASH_START
98 && (phdr
.p_paddr
+ phdr
.p_filesz
) < PHYSICAL_FLASH_END
) {
99 printf("flashing offset=%x paddr=%x size=%x\n", phdr
.p_offset
, phdr
.p_paddr
, phdr
.p_filesz
);
100 if (phdr
.p_offset
== 0) {
101 printf("skipping forward 0x2000 because of strange linker thing\n");
102 phdr
.p_offset
+= 0x2000;
103 phdr
.p_paddr
+= 0x2000;
104 phdr
.p_filesz
-= 0x2000;
105 phdr
.p_memsz
-= 0x2000;
108 fseek(f
, phdr
.p_offset
, SEEK_SET
);
109 ExpectedAddr
= phdr
.p_paddr
;
110 while (ExpectedAddr
< (phdr
.p_paddr
+ phdr
.p_filesz
)) {
111 unsigned int bytes_to_read
= phdr
.p_paddr
+ phdr
.p_filesz
- ExpectedAddr
;
112 if (bytes_to_read
> 256)
115 memset(QueuedToSend
, 0xFF, 256);
116 fread(QueuedToSend
, 1, bytes_to_read
, f
);
117 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
,
118 QueuedToSend
[0], QueuedToSend
[1], QueuedToSend
[2], QueuedToSend
[3],
119 QueuedToSend
[4], QueuedToSend
[5], QueuedToSend
[6], QueuedToSend
[7],
120 QueuedToSend
[248], QueuedToSend
[249], QueuedToSend
[250], QueuedToSend
[251],
121 QueuedToSend
[252], QueuedToSend
[253], QueuedToSend
[254], QueuedToSend
[255]);
122 WriteBlock(ExpectedAddr
, 256, QueuedToSend
);
123 ExpectedAddr
+= bytes_to_read
;
131 } else printf("Bad file format\n");
134 int PrepareFlash(struct partition
*p
, const char *filename
, unsigned int state
)
136 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
137 UsbCommand c
= {CMD_START_FLASH
, {p
->start
, p
->end
, 0}};
139 /* Only send magic when flashing bootrom */
141 c
.arg
[2] = START_FLASH_MAGIC
;
148 fprintf(stderr
, "Warning: Your bootloader does not understand the new START_FLASH command\n");
149 fprintf(stderr
, " It is recommended that you update your bootloader\n\n");
153 LoadFlashFromFile(filename
, p
->start
, p
->end
);
157 unsigned int GetProxmarkState(void)
159 unsigned int state
= 0;
162 c
.cmd
= CMD_DEVICE_INFO
;
166 ReceiveCommand(&resp
);
168 * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
169 * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
170 * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
175 state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
177 case CMD_DEBUG_PRINT_STRING
:
178 state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
180 case CMD_DEVICE_INFO
:
184 fprintf(stderr
, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp
.cmd
);
192 unsigned int EnterFlashState(void)
194 unsigned int state
= GetProxmarkState();
196 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
197 /* Already in flash state, we're done. */
201 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
202 fprintf(stderr
,"Entering flash-mode...\n");
204 bzero(&c
, sizeof(c
));
206 if ((state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
) && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
)) {
207 /* New style handover: Send CMD_START_FLASH, which will reset the board and
208 * enter the bootrom on the next boot.
210 c
.cmd
= CMD_START_FLASH
;
212 fprintf(stderr
,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
213 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
215 /* Old style handover: Ask the user to press the button, then reset the board */
216 c
.cmd
= CMD_HARDWARE_RESET
;
218 fprintf(stderr
,"(Press and hold down button NOW if your bootloader requires it)\n");
219 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
224 while (!UsbConnect()) { Sleep(1000); }
229 while (!OpenProxmark(0)) { sleep(1); }
231 fprintf(stderr
,"Found.\n");
233 return GetProxmarkState();
239 /* On first call, have *offset = -1, *length = 0; */
240 int find_next_area(const char *str
, int *offset
, int *length
)
242 if (*str
== '\0') return 0;
243 if ((*offset
>= 0) && str
[*offset
+ *length
] == '\0') return 0;
244 *offset
+= 1 + *length
;
246 char *next_comma
= strchr(str
+ *offset
, ',');
247 if (next_comma
== NULL
) {
248 *length
= strlen(str
) - *offset
;
250 *length
= next_comma
-(str
+*offset
);
255 void do_flash(char **argv
)
257 unsigned int state
= EnterFlashState();
259 if (!(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
)) {
260 fprintf(stderr
, "Proxmark would not enter flash state, abort\n");
264 int offset
=-1, length
=0;
265 int current_area
= 0;
266 while (find_next_area(argv
[1], &offset
, &length
)) {
267 struct partition
*p
= NULL
;
268 for (int i
= 0; i
<sizeof(partitions
)/sizeof(partitions
[0]); ++i
) {
269 if (strncmp(partitions
[i
].name
, argv
[1] + offset
, length
) == 0) {
270 /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
271 * be written in full. The other names may be abbreviated.
273 if(!partitions
[i
].precious
|| (strlen(partitions
[i
].name
) == length
))
280 fprintf(stderr
, "Warning: area name '");
281 fwrite(argv
[1]+offset
, length
, 1, stderr
);
282 fprintf(stderr
, "' unknown, ignored\n");
284 fprintf(stderr
, "Flashing %s from %s\n", p
->name
, argv
[2+current_area
]);
285 PrepareFlash(p
, argv
[2+current_area
], state
);