+
+ uint8_t isOK = resp.arg[0] & 0xFF;
+ if ( !isOK ) {
+ PrintAndLog("Failed dumping tag data");
+ return 2;
+ }
+
+ uint16_t readlen = resp.arg[1];
+ uint8_t *data = malloc(readlen);
+ if (!data) {
+ PrintAndLog("Fail, cannot allocate memory");
+ return 3;
+ }
+ memset(data, 0, readlen);
+
+ if ( readlen != dumplen )
+ PrintAndLog("Fail, only managed to read 0x%02X bytes of 0x%02X", readlen, dumplen);
+
+ // copy data from device
+ GetEMLFromBigBuf(data, readlen, 0);
+ if ( !WaitForResponseTimeout(CMD_ACK, NULL, 2500)) {
+ PrintAndLog("Fail, transfer from device time-out");
+ free(data);
+ return 4;
+ }
+
+ // user supplied filename?
+ if (fileNlen < 1)
+ sprintf(fnameptr,"%02X%02X%02X%02X.bin", data[0], data[1], data[2], data[3]);
+ else
+ sprintf(fnameptr + fileNlen,".bin");
+
+ if ((f = fopen(filename,"wb")) == NULL) {
+ PrintAndLog("Could not create file name %s", filename);
+ if (data)
+ free(data);
+ return 5;
+ }
+ fwrite(data, 1, readlen, f);
+ fclose(f);
+ free(data);
+ PrintAndLog("Wrote %d bytes to %s", readlen, filename);
+ return 0;
+}
+
+int CmdLegicELoad(const char *Cmd) {
+ FILE * f;
+ char filename[FILE_PATH_SIZE];
+ char *fnameptr = filename;
+ int len, numofbytes;
+ int nameParamNo = 1;
+
+ char cmdp = param_getchar(Cmd, 0);
+ if ( cmdp == 'h' || cmdp == 'H' || cmdp == 0x00)
+ return usage_legic_eload();
+
+ switch (cmdp) {
+ case '0' : numofbytes = 22; break;
+ case '1' :
+ case '\0': numofbytes = 256; break;
+ case '2' : numofbytes = 1024; break;
+ default : numofbytes = 256; nameParamNo = 0;break;
+ }
+
+ // set up buffer
+ uint8_t *data = malloc(numofbytes);
+ if (!data) {
+ PrintAndLog("Fail, cannot allocate memory");
+ return 3;
+ }
+ memset(data, 0, numofbytes);
+
+ // set up file
+ len = param_getstr(Cmd, nameParamNo, filename);
+ if (len > FILE_PATH_SIZE - 5)
+ len = FILE_PATH_SIZE - 5;
+ fnameptr += len;
+ sprintf(fnameptr, ".bin");
+
+ // open file
+ if ((f = fopen(filename,"rb")) == NULL) {
+ PrintAndLog("File %s not found or locked", filename);
+ free(data);
+ return 1;
+ }
+
+ // load file
+ size_t bytes_read = fread(data, 1, numofbytes, f);
+ if ( bytes_read == 0){
+ PrintAndLog("File reading error");
+ free(data);
+ fclose(f);
+ return 2;
+ }
+ fclose(f);
+
+ // transfer to device
+ legic_seteml(data, 0, numofbytes);
+
+ free(data);
+ PrintAndLog("\nLoaded %d bytes from file: %s to emulator memory", numofbytes, filename);