mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-03-10 06:57:19 +08:00
removed support for s19 files in the flasher and replaced it
with elf parsing. can we stop specifying the "partition" yet?
This commit is contained in:
parent
6e4d4ee609
commit
2cab856f9f
4 changed files with 99 additions and 101 deletions
31
client/elf.h
Normal file
31
client/elf.h
Normal file
|
@ -0,0 +1,31 @@
|
||||||
|
#ifndef __ELF_H__
|
||||||
|
#define __ELF_H__
|
||||||
|
|
||||||
|
#define EI_NIDENT 16
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
unsigned char e_ident[EI_NIDENT];
|
||||||
|
uint16_t e_type;
|
||||||
|
uint16_t e_machine;
|
||||||
|
uint32_t e_version;
|
||||||
|
uint32_t e_entry;
|
||||||
|
uint32_t e_phoff;
|
||||||
|
uint32_t e_shoff;
|
||||||
|
uint32_t e_flags;
|
||||||
|
uint16_t e_ehsize;
|
||||||
|
uint16_t e_phentsize;
|
||||||
|
uint16_t e_phnum;
|
||||||
|
uint16_t e_shentsize;
|
||||||
|
uint16_t e_shnum;
|
||||||
|
uint16_t e_shtrndx;
|
||||||
|
} __attribute__((__packed__)) Elf32_Ehdr;
|
||||||
|
|
||||||
|
#define PT_NULL 0
|
||||||
|
#define PT_LOAD 1
|
||||||
|
#define PT_DYNAMIC 2
|
||||||
|
#define PT_INTERP 3
|
||||||
|
#define PT_NOTE 4
|
||||||
|
#define PT_SHLIB 5
|
||||||
|
#define PT_PHDR 6
|
||||||
|
#endif
|
||||||
|
|
163
client/flash.c
163
client/flash.c
|
@ -22,11 +22,13 @@ BOOL UsbConnect(void);
|
||||||
#include "proxmark3.h"
|
#include "proxmark3.h"
|
||||||
#endif
|
#endif
|
||||||
#include "flash.h"
|
#include "flash.h"
|
||||||
|
#include "elf.h"
|
||||||
|
|
||||||
static uint32_t ExpectedAddr;
|
static uint32_t ExpectedAddr;
|
||||||
static uint8_t QueuedToSend[256];
|
static uint8_t QueuedToSend[256];
|
||||||
static bool AllWritten;
|
static bool AllWritten;
|
||||||
#define PHYSICAL_FLASH_START 0x100000
|
#define PHYSICAL_FLASH_START 0x100000
|
||||||
|
#define PHYSICAL_FLASH_END 0x200000
|
||||||
|
|
||||||
void WaitForAck(void) {
|
void WaitForAck(void) {
|
||||||
UsbCommand ack;
|
UsbCommand ack;
|
||||||
|
@ -47,147 +49,112 @@ struct partition partitions[] = {
|
||||||
/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
|
/* If translate is set, subtract PHYSICAL_FLASH_START to translate for old
|
||||||
* bootroms.
|
* bootroms.
|
||||||
*/
|
*/
|
||||||
void FlushPrevious(int translate)
|
void WriteBlock(unsigned int block_start, unsigned int len, unsigned char *buf)
|
||||||
{
|
{
|
||||||
UsbCommand c;
|
unsigned char temp_buf[256];
|
||||||
memset(&c, 0, sizeof(c));
|
if (block_start & 0xFF) {
|
||||||
|
printf("moving stuff forward by %d bytes\n", block_start & 0xFF);
|
||||||
|
memset(temp_buf, 0xFF, block_start & 0xFF);
|
||||||
|
memcpy(temp_buf + (block_start & 0xFF), buf, len);
|
||||||
|
block_start &= ~0xFF;
|
||||||
|
} else memcpy(temp_buf, buf, len);
|
||||||
|
|
||||||
printf("expected = %08x flush, ", ExpectedAddr);
|
UsbCommand c = {CMD_SETUP_WRITE};
|
||||||
|
|
||||||
|
// printf("expected = %08x flush, ", ExpectedAddr);
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
for(i = 0; i < 240; i += 48) {
|
for(i = 0; i < 240; i += 48) {
|
||||||
c.cmd = CMD_SETUP_WRITE;
|
memcpy(c.d.asBytes, temp_buf+i, 48);
|
||||||
memcpy(c.d.asBytes, QueuedToSend+i, 48);
|
|
||||||
c.arg[0] = (i/4);
|
c.arg[0] = (i/4);
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
WaitForAck();
|
WaitForAck();
|
||||||
}
|
}
|
||||||
|
|
||||||
c.cmd = CMD_FINISH_WRITE;
|
c.cmd = CMD_FINISH_WRITE;
|
||||||
c.arg[0] = (ExpectedAddr-1) & (~255);
|
c.arg[0] = block_start;
|
||||||
if(translate) {
|
|
||||||
c.arg[0] -= PHYSICAL_FLASH_START;
|
printf("writing block %08x\r", c.arg[0]);
|
||||||
}
|
memcpy(c.d.asBytes, temp_buf+240, 16);
|
||||||
printf("c.arg[0] = %08x\r", c.arg[0]);
|
|
||||||
memcpy(c.d.asBytes, QueuedToSend+240, 16);
|
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
WaitForAck();
|
WaitForAck();
|
||||||
|
|
||||||
AllWritten = true;
|
AllWritten = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Where must be between start_addr (inclusive) and end_addr (exclusive).
|
void LoadFlashFromFile(const char *file, int start_addr, int end_addr)
|
||||||
*/
|
|
||||||
void GotByte(uint32_t where, uint8_t which, int start_addr, int end_addr, int translate)
|
|
||||||
{
|
{
|
||||||
AllWritten = false;
|
FILE *f = fopen(file, "rb");
|
||||||
|
|
||||||
if(where < start_addr || where >= end_addr) {
|
|
||||||
printf("bad: got byte at %08x, outside of range %08x-%08x\n", where, start_addr, end_addr);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if(where != ExpectedAddr) {
|
|
||||||
printf("bad: got at %08x, expected at %08x\n", where, ExpectedAddr);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
QueuedToSend[where & 255] = which;
|
|
||||||
ExpectedAddr++;
|
|
||||||
|
|
||||||
if((where & 255) == 255) {
|
|
||||||
// we have completed a full page
|
|
||||||
FlushPrevious(translate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int HexVal(int c)
|
|
||||||
{
|
|
||||||
c = tolower(c);
|
|
||||||
if(c >= '0' && c <= '9') {
|
|
||||||
return c - '0';
|
|
||||||
} else if(c >= 'a' && c <= 'f') {
|
|
||||||
return (c - 'a') + 10;
|
|
||||||
} else {
|
|
||||||
printf("bad hex digit '%c'\n", c);
|
|
||||||
exit(-1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t HexByte(char *s)
|
|
||||||
{
|
|
||||||
return (HexVal(s[0]) << 4) | HexVal(s[1]);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LoadFlashFromSRecords(const char *file, int start_addr, int end_addr, int translate)
|
|
||||||
{
|
|
||||||
ExpectedAddr = start_addr;
|
|
||||||
|
|
||||||
FILE *f = fopen(file, "r");
|
|
||||||
if(!f) {
|
if(!f) {
|
||||||
printf("couldn't open file\n");
|
printf("couldn't open file\n");
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
char line[512];
|
char buf[4];
|
||||||
while(fgets(line, sizeof(line), f)) {
|
fread(buf, 1, 4, f);
|
||||||
if(memcmp(line, "S3", 2)==0) {
|
if (memcmp(buf, "\x7F" "ELF", 4) == 0) {
|
||||||
char *s = line + 2;
|
int i;
|
||||||
int len = HexByte(s) - 5;
|
fseek(f, 0, SEEK_SET);
|
||||||
s += 2;
|
Elf32_Ehdr header;
|
||||||
|
fread(&header, 1, sizeof(header), f);
|
||||||
|
int count = header.e_phnum;
|
||||||
|
printf("count=%d phoff=%x\n", count, header.e_phoff);
|
||||||
|
Elf32_Phdr phdr;
|
||||||
|
|
||||||
char addrStr[9];
|
for (i=0; i<header.e_phnum; i++) {
|
||||||
memcpy(addrStr, s, 8);
|
fseek(f, header.e_phoff + i * sizeof(Elf32_Phdr), SEEK_SET);
|
||||||
addrStr[8] = '\0';
|
fread(&phdr, 1, sizeof(phdr), f);
|
||||||
uint32_t addr;
|
// printf("type=%d offset=%x paddr=%x vaddr=%x filesize=%x memsize=%x flags=%x align=%x\n",
|
||||||
sscanf(addrStr, "%x", &addr);
|
// phdr.p_type, phdr.p_offset, phdr.p_paddr, phdr.p_vaddr, phdr.p_filesz, phdr.p_memsz, phdr.p_flags, phdr.p_align);
|
||||||
s += 8;
|
if (phdr.p_type == PT_LOAD && phdr.p_filesz > 0 && phdr.p_paddr >= PHYSICAL_FLASH_START
|
||||||
|
&& (phdr.p_paddr + phdr.p_filesz) < PHYSICAL_FLASH_END) {
|
||||||
/* Accept files that are located at PHYSICAL_FLASH_START, and files that are located at 0 */
|
printf("flashing offset=%x paddr=%x size=%x\n", phdr.p_offset, phdr.p_paddr, phdr.p_filesz);
|
||||||
if(addr < PHYSICAL_FLASH_START)
|
if (phdr.p_offset == 0) {
|
||||||
addr += PHYSICAL_FLASH_START;
|
printf("skipping forward 0x2000 because of strange linker thing\n");
|
||||||
|
phdr.p_offset += 0x2000;
|
||||||
int i;
|
phdr.p_paddr += 0x2000;
|
||||||
for(i = 0; i < len; i++) {
|
|
||||||
while((addr+i) > ExpectedAddr) {
|
|
||||||
GotByte(ExpectedAddr, 0xff, start_addr, end_addr, translate);
|
|
||||||
}
|
}
|
||||||
GotByte(addr+i, HexByte(s), start_addr, end_addr, translate);
|
|
||||||
s += 2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if(!AllWritten) FlushPrevious(translate);
|
fseek(f, phdr.p_offset, SEEK_SET);
|
||||||
|
ExpectedAddr = phdr.p_paddr;
|
||||||
|
while (ExpectedAddr < (phdr.p_paddr + phdr.p_filesz)) {
|
||||||
|
unsigned int bytes_to_read = phdr.p_paddr + phdr.p_filesz - ExpectedAddr;
|
||||||
|
if (bytes_to_read > 256) bytes_to_read=256;
|
||||||
|
fread(QueuedToSend, 1, bytes_to_read, f);
|
||||||
|
// printf("read %d bytes\n", bytes_to_read);
|
||||||
|
printf("WriteBlock(%x, %d, %p)\n", ExpectedAddr, bytes_to_read, QueuedToSend);
|
||||||
|
WriteBlock(ExpectedAddr, bytes_to_read, QueuedToSend);
|
||||||
|
ExpectedAddr += bytes_to_read;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
printf("\ndone.\n");
|
printf("\ndone.\n");
|
||||||
|
return;
|
||||||
|
} else printf("Bad file format\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
|
int PrepareFlash(struct partition *p, const char *filename, unsigned int state)
|
||||||
{
|
{
|
||||||
int translate = 0;
|
|
||||||
if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
|
if(state & DEVICE_INFO_FLAG_UNDERSTANDS_START_FLASH) {
|
||||||
UsbCommand c;
|
UsbCommand c = {CMD_START_FLASH, {p->start, p->end, 0}};
|
||||||
c.cmd = CMD_START_FLASH;
|
|
||||||
c.arg[0] = p->start;
|
|
||||||
c.arg[1] = p->end;
|
|
||||||
|
|
||||||
/* Only send magic when flashing bootrom */
|
/* Only send magic when flashing bootrom */
|
||||||
if(p->precious) {
|
if(p->precious)
|
||||||
c.arg[2] = START_FLASH_MAGIC;
|
c.arg[2] = START_FLASH_MAGIC;
|
||||||
} else {
|
else
|
||||||
c.arg[2] = 0;
|
c.arg[2] = 0;
|
||||||
}
|
|
||||||
SendCommand(&c);
|
SendCommand(&c);
|
||||||
WaitForAck();
|
WaitForAck();
|
||||||
translate = 0;
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
|
fprintf(stderr, "Warning: Your bootloader does not understand the new START_FLASH command\n");
|
||||||
fprintf(stderr, " It is recommended that you update your bootloader\n\n");
|
fprintf(stderr, " It is recommended that you update your bootloader\n\n");
|
||||||
translate = 1;
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
LoadFlashFromSRecords(filename, p->start, p->end, translate);
|
LoadFlashFromFile(filename, p->start, p->end);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,7 @@ static void usage(char **argv)
|
||||||
|
|
||||||
fprintf(stderr, "\n");
|
fprintf(stderr, "\n");
|
||||||
fprintf(stderr, " image is the path to the corresponding image\n\n");
|
fprintf(stderr, " image is the path to the corresponding image\n\n");
|
||||||
fprintf(stderr, "Example: %s os,fpga path/to/osimage.s19 path/to/fpgaimage.s19\n", argv[0]);
|
fprintf(stderr, "Example: %s os,fpga path/to/osimage.elf path/to/fpgaimage.elf\n", argv[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
|
|
|
@ -223,7 +223,7 @@ static void usage(char **argv)
|
||||||
int i;
|
int i;
|
||||||
printf("Usage: %s gui\n", argv[0]);
|
printf("Usage: %s gui\n", argv[0]);
|
||||||
printf(" %s offline\n", argv[0]);
|
printf(" %s offline\n", argv[0]);
|
||||||
printf(" %s areas file.s19\n", argv[0]);
|
printf(" %s areas file.elf\n", argv[0]);
|
||||||
printf(" Known areas are:");
|
printf(" Known areas are:");
|
||||||
for(i=0; partitions[i].name != NULL; i++) {
|
for(i=0; partitions[i].name != NULL; i++) {
|
||||||
fprintf(stderr, " %s", partitions[i].name);
|
fprintf(stderr, " %s", partitions[i].name);
|
||||||
|
|
Loading…
Reference in a new issue