Skip to content

Commit

Permalink
Stuff works more
Browse files Browse the repository at this point in the history
Reader has debug mode
Reader can show each page
Added memptr to reader to simulate navigating through pages
  • Loading branch information
marecl committed Jun 11, 2019
1 parent 865883b commit 260ede9
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 43 deletions.
118 changes: 86 additions & 32 deletions MULoaderReader/MULoaderReader.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,128 @@
#include "RC522.h"
#include <avr/boot.h>

uint8_t pageBuffer[256];
RC522 rfid(10);
/* Move this to AVR Studio */

#define S_DEBUG

void setup() {
RC522 rfid(10);
SPI.begin();
#ifdef S_DEBUG
Serial.begin(2000000);
Serial.println("XD");
Serial.println(F("MostUseless Bootloader Reader"));
#endif
rfid.PCD_Init();

RC522::MIFARE_Key key;
memset(key.keyByte, 0xFF, 6);

while (millis() < 10000 && !rfid.PICC_IsNewCardPresent());

/* Jump to code after timeout */
while (millis() < 10000 && !rfid.PICC_IsNewCardPresent());
if (millis() > 10000) asm("jmp 0x0000");

while (!rfid.PICC_ReadCardSerial());

uint8_t blocks = 1;
/* Sketch metadata */
uint16_t siz = 0;
uint16_t flashptr = 0;
uint8_t parts = 0;
uint8_t pages = 0;
uint8_t pageExtra = 0;
uint8_t blocks = 2;
uint8_t blockExtra = 0;

/* Buffer and SPM stuff */
uint8_t pageoffset = 0;
uint8_t memptr = 0;

/*
We don't need to worry about extra data in last
page if we already set buffer as empty :)
*/
uint8_t pageBuffer[256];
memset(pageBuffer, 0xFF, 256);

for (uint8_t block = 1; flashptr < blocks; block++) {
if ((block + 1) % 4 == 0)
/*
Reading every block and using page offset
to save progress if more parts are needed
*/

for (uint8_t block = 1; block < blocks; block++) {
if ((block + 1) % 4 == 0) {
blocks++;
continue;
}

uint8_t buffer[18];

byte len = 18;
const byte len = 18;

rfid.PCD_Authenticate(RC522::PICC_CMD_MF_AUTH_KEY_A, block, &key, &(rfid.uid));
rfid.MIFARE_Read(block, buffer, &len);

/*
2 bytes - code size
1 byte - parts [token]
1 byte - full pages (including not full)
1 byte - amount of data in last page
1 byte - full blocks
1 byte - amount of data in last block
*/
if (block == 1) {
siz = word(buffer[0], buffer[1]);
blocks = siz / 16;
if (siz % 16 != 0)
blocks++;
pages = siz / 256;
if (siz % 256 != 0)
pages++;
Serial.println(siz);
Serial.println(blocks);
parts = buffer[2];
pages = buffer[3];
pageExtra = buffer[4];
blocks = buffer[5] + 2;
blockExtra = buffer[6];
#ifdef S_DEBUG
Serial.print(F("Sketch size:\t"));
Serial.print(siz);
Serial.print(F(" bytes\r\nParts [tokens]:\t"));
Serial.println(parts);
Serial.print(F("Pages total:\t"));
Serial.println(pages);
Serial.print(F("In last page:\t"));
Serial.print(pageExtra);
Serial.println(F(" bytes"));
Serial.print(F("Blocks total:\t"));
Serial.println(blocks - 2);
Serial.print(F("In last block:\t"));
Serial.print(blockExtra);
Serial.println(F(" bytes"));
#endif
continue;
}

/* Write to flash 16 bytes at a time */
/* 3498 bytes free after removing serial */

//erasePage(flashptr);

/* E0 01 B9 07 B9 08 CF FF */
for (uint8_t a = 0; a < 16; a++) {
pageBuffer[(pageoffset * 16) + a] = buffer[a];
}
pageoffset++;

/* Check if page is full */
if (pageoffset < 16 && block != blocks - 1) continue;
pageoffset = 0;

#ifdef S_DEBUG
Serial.print(F("\r\n ---- Page "));
Serial.print(memptr + 1);
Serial.println(F(" ----"));
for (uint16_t x = 1; x <= 256; x++) {
Serial.print(pageBuffer[x - 1] < 0x10 ? " 0" : " ");
Serial.print(pageBuffer[x - 1], HEX);
if (x % 16 == 0)
Serial.println();
}
#endif

flashptr++;
/* Write each page to flash */
/* 3888 bytes free after removing serial */

/*for (uint8_t x = 0; x < 16; x++) {
Serial.print(buffer[x] < 0x10 ? " 0" : " ");
Serial.print(buffer[x], HEX);
}
Serial.println();
*/


/* Finished writing to flash */

memptr++;
memset(pageBuffer, 0xFF, 256);
}
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1(); // Stop encryption on PCD
Expand Down
108 changes: 97 additions & 11 deletions MULoaderWriter/MULoaderWriter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,97 @@
MFRC522 rfid(10, 9);

/* Maybe load from serial? */
/*
YES, IT HAS TO BE THIS LONG
*/
byte prog[] = {0x01, 0xE0, 0x07, 0x07, 0x08, 0xB9, 0xFE, 0xCF,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD,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D,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,

0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD
};

void setup() {
Serial.begin(2000000);
SPI.begin();
rfid.PCD_Init();
rfid.PCD_DumpVersionToSerial();
Serial.println(F("Ready to write some shit"));
Serial.println(F("MostUseless Bootloader Writer"));
Serial.flush();
}

Expand All @@ -26,19 +108,21 @@ void loop() {
if (!rfid.PICC_ReadCardSerial())
return;

Serial.print(F("Card UID:")); //Dump UID
Serial.print(F("Card UID:"));
for (byte i = 0; i < rfid.uid.size; i++) {
Serial.print(rfid.uid.uidByte[i] < 0x10 ? " 0" : " ");
Serial.print(rfid.uid.uidByte[i], HEX);
}
Serial.print(F("\r\nPICC type: ")); // Dump PICC type
Serial.print(F("\r\nPICC type: "));
MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
Serial.println(rfid.PICC_GetTypeName(piccType));

uint16_t s = sizeof(prog);
uint8_t blocks = (s / 16);
if (s % 16 != 0) blocks += 1;
if (blocks > 46) { //736 bytes per card

/* 736 bytes per card */
if (blocks > 46) {
Serial.println(F("Parts not implemented yet!"));
return;
}
Expand Down Expand Up @@ -69,8 +153,8 @@ void loop() {
/*
2 bytes - code size
1 byte - parts [token]
1 byte - full sectors (including not full)
1 byte - amount of data in last sector
1 byte - full pages (including not full)
1 byte - amount of data in last page
1 byte - full blocks
1 byte - amount of data in last block
*/
Expand All @@ -86,14 +170,16 @@ void loop() {
Serial.print(s);
Serial.print(F(" bytes\r\nParts [tokens]:\t"));
Serial.println(buffer[2]);
Serial.print(F("Sectors total:\t"));
Serial.print(F("Pages total:\t"));
Serial.println(buffer[3]);
Serial.print(F("In last sector:\t"));
Serial.println(buffer[4]);
Serial.print(F("In last page:\t"));
Serial.print(buffer[4]);
Serial.println(F(" bytes"));
Serial.print(F("Blocks total:\t"));
Serial.println(buffer[5]);
Serial.print(F("In last block:\t"));
Serial.println(buffer[6]);
Serial.print(buffer[6]);
Serial.println(F(" bytes"));
}

status = rfid.MIFARE_Write(block, buffer, 16);
Expand All @@ -111,5 +197,5 @@ void loop() {
delay(100);
rfid.PICC_DumpToSerial(&(rfid.uid));
rfid.PICC_HaltA();
rfid.PCD_StopCrypto1(); // Stop encryption on PCD
rfid.PCD_StopCrypto1();
}

0 comments on commit 260ede9

Please sign in to comment.