4 #define bzero(b,len) (memset((b), '\0', (len)), (void) 0)
16 static uint32_t ExpectedAddr
;
17 static uint8_t QueuedToSend
[256];
18 static bool AllWritten
;
19 #define PHYSICAL_FLASH_START 0x100000
20 #define PHYSICAL_FLASH_END 0x200000
26 if (ack
.cmd
!= CMD_ACK
) {
32 struct partition partitions
[] = {
33 {0x100000, 0x102000, 1, "bootrom"},
34 {0x102000, 0x110000, 0, "fpga"},
35 {0x110000, 0x140000, 0, "os"},
39 void WriteBlock(unsigned int block_start
, unsigned int len
, unsigned char *buf
)
41 unsigned char temp_buf
[256];
42 if (block_start
& 0xFF) {
43 printf("moving stuff forward by %d bytes\n", block_start
& 0xFF);
44 memset(temp_buf
, 0xFF, block_start
& 0xFF);
45 memcpy(temp_buf
+ (block_start
& 0xFF), buf
, len
- (block_start
& 0xFF));
48 memcpy(temp_buf
, buf
, len
);
51 UsbCommand c
= {CMD_SETUP_WRITE
};
53 for (int i
= 0; i
< 240; i
+= 48) {
54 memcpy(c
.d
.asBytes
, temp_buf
+i
, 48);
60 c
.cmd
= CMD_FINISH_WRITE
;
61 c
.arg
[0] = block_start
;
63 // printf("writing block %08x\r", c.arg[0]);
64 memcpy(c
.d
.asBytes
, temp_buf
+240, 16);
71 void LoadFlashFromFile(const char *file
, int start_addr
, int end_addr
)
73 FILE *f
= fopen(file
, "rb");
75 printf("couldn't open file\n");
81 if (memcmp(buf
, "\x7F" "ELF", 4) == 0) {
82 fseek(f
, 0, SEEK_SET
);
84 fread(&header
, 1, sizeof(header
), f
);
85 int count
= header
.e_phnum
;
86 printf("count=%d phoff=%x\n", count
, header
.e_phoff
);
89 for (int i
= 0; i
< header
.e_phnum
; ++i
) {
90 fseek(f
, header
.e_phoff
+ i
* sizeof(Elf32_Phdr
), SEEK_SET
);
91 fread(&phdr
, 1, sizeof(phdr
), f
);
92 printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
93 phdr
.p_type
, phdr
.p_offset
, phdr
.p_paddr
, phdr
.p_vaddr
, phdr
.p_filesz
, phdr
.p_memsz
, phdr
.p_flags
, phdr
.p_align
);
94 if (phdr
.p_type
== PT_LOAD
&& phdr
.p_filesz
> 0 && phdr
.p_paddr
>= PHYSICAL_FLASH_START
95 && (phdr
.p_paddr
+ phdr
.p_filesz
) < PHYSICAL_FLASH_END
) {
96 printf("flashing offset=%x paddr=%x size=%x\n", phdr
.p_offset
, phdr
.p_paddr
, phdr
.p_filesz
);
97 if (phdr
.p_offset
== 0) {
98 printf("skipping forward 0x2000 because of strange linker thing\n");
99 phdr
.p_offset
+= 0x2000;
100 phdr
.p_paddr
+= 0x2000;
101 phdr
.p_filesz
-= 0x2000;
102 phdr
.p_memsz
-= 0x2000;
105 fseek(f
, phdr
.p_offset
, SEEK_SET
);
106 ExpectedAddr
= phdr
.p_paddr
;
107 while (ExpectedAddr
< (phdr
.p_paddr
+ phdr
.p_filesz
)) {
108 unsigned int bytes_to_read
= phdr
.p_paddr
+ phdr
.p_filesz
- ExpectedAddr
;
109 if (bytes_to_read
> 256)
112 memset(QueuedToSend
, 0xFF, 256);
113 fread(QueuedToSend
, 1, bytes_to_read
, f
);
114 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
,
115 QueuedToSend
[0], QueuedToSend
[1], QueuedToSend
[2], QueuedToSend
[3],
116 QueuedToSend
[4], QueuedToSend
[5], QueuedToSend
[6], QueuedToSend
[7],
117 QueuedToSend
[248], QueuedToSend
[249], QueuedToSend
[250], QueuedToSend
[251],
118 QueuedToSend
[252], QueuedToSend
[253], QueuedToSend
[254], QueuedToSend
[255]);
119 WriteBlock(ExpectedAddr
, 256, QueuedToSend
);
120 ExpectedAddr
+= bytes_to_read
;
128 } else printf("Bad file format\n");
131 int PrepareFlash(struct partition
*p
, const char *filename
, unsigned int state
)
133 if (state
& DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH
) {
134 UsbCommand c
= {CMD_START_FLASH
, {p
->start
, p
->end
, 0}};
136 /* Only send magic when flashing bootrom */
138 c
.arg
[2] = START_FLASH_MAGIC
;
145 fprintf(stderr
, "Warning: Your bootloader does not understand the new START_FLASH command\n");
146 fprintf(stderr
, " It is recommended that you update your bootloader\n\n");
150 LoadFlashFromFile(filename
, p
->start
, p
->end
);
154 unsigned int GetProxmarkState(void)
156 unsigned int state
= 0;
159 c
.cmd
= CMD_DEVICE_INFO
;
163 ReceiveCommand(&resp
);
165 * 1. The old bootrom code will ignore CMD_DEVICE_INFO, but respond with an ACK
166 * 2. The old os code will respond with CMD_DEBUG_PRINT_STRING and "unknown command"
167 * 3. The new bootrom and os codes will respond with CMD_DEVICE_INFO and flags
172 state
= DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
;
174 case CMD_DEBUG_PRINT_STRING
:
175 state
= DEVICE_INFO_FLAG_CURRENT_MODE_OS
;
177 case CMD_DEVICE_INFO
:
181 fprintf(stderr
, "Couldn't get proxmark state, bad response type: 0x%04X\n", resp
.cmd
);
189 unsigned int EnterFlashState(void)
191 unsigned int state
= GetProxmarkState();
193 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
) {
194 /* Already in flash state, we're done. */
198 if (state
& DEVICE_INFO_FLAG_CURRENT_MODE_OS
) {
199 fprintf(stderr
,"Entering flash-mode...\n");
201 bzero(&c
, sizeof(c
));
203 if ((state
& DEVICE_INFO_FLAG_BOOTROM_PRESENT
) && (state
& DEVICE_INFO_FLAG_OSIMAGE_PRESENT
)) {
204 /* New style handover: Send CMD_START_FLASH, which will reset the board and
205 * enter the bootrom on the next boot.
207 c
.cmd
= CMD_START_FLASH
;
209 fprintf(stderr
,"(You don't have to do anything. Press and release the button only if you want to abort)\n");
210 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
212 /* Old style handover: Ask the user to press the button, then reset the board */
213 c
.cmd
= CMD_HARDWARE_RESET
;
215 fprintf(stderr
,"(Press and hold down button NOW if your bootloader requires it)\n");
216 fprintf(stderr
,"Waiting for Proxmark to reappear on USB... ");
221 while (!UsbConnect()) { Sleep(1000); }
226 while (!OpenProxmark(0)) { sleep(1); }
228 fprintf(stderr
,"Found.\n");
230 return GetProxmarkState();
236 /* On first call, have *offset = -1, *length = 0; */
237 int find_next_area(const char *str
, int *offset
, int *length
)
239 if (*str
== '\0') return 0;
240 if ((*offset
>= 0) && str
[*offset
+ *length
] == '\0') return 0;
241 *offset
+= 1 + *length
;
243 char *next_comma
= strchr(str
+ *offset
, ',');
244 if (next_comma
== NULL
) {
245 *length
= strlen(str
) - *offset
;
247 *length
= next_comma
-(str
+*offset
);
252 void do_flash(char **argv
)
254 unsigned int state
= EnterFlashState();
256 if (!(state
& DEVICE_INFO_FLAG_CURRENT_MODE_BOOTROM
)) {
257 fprintf(stderr
, "Proxmark would not enter flash state, abort\n");
261 int offset
=-1, length
=0;
262 int current_area
= 0;
263 while (find_next_area(argv
[1], &offset
, &length
)) {
264 struct partition
*p
= NULL
;
265 for (int i
= 0; i
<sizeof(partitions
)/sizeof(partitions
[0]); ++i
) {
266 if (strncmp(partitions
[i
].name
, argv
[1] + offset
, length
) == 0) {
267 /* Check if the name matches the bootrom partition, and if so, require "bootrom" to
268 * be written in full. The other names may be abbreviated.
270 if(!partitions
[i
].precious
|| (strlen(partitions
[i
].name
) == length
))
277 fprintf(stderr
, "Warning: area name '");
278 fwrite(argv
[1]+offset
, length
, 1, stderr
);
279 fprintf(stderr
, "' unknown, ignored\n");
281 fprintf(stderr
, "Flashing %s from %s\n", p
->name
, argv
[2+current_area
]);
282 PrepareFlash(p
, argv
[2+current_area
], state
);