/** * @brief Reads a decimal integer (actually, 0-254, not 255) * @param line * @param paramnum * @return -1 if error */ uint8_t param_getdec(const char *line, int paramnum, uint8_t *destination) { uint8_t val = param_get8ex(line, paramnum, 255, 10); if( (int8_t) val == -1) return 1; (*destination) = val; return 0; }
int CmdLFPCF7931Write(const char *Cmd) { uint8_t ctmp = param_getchar(Cmd, 0); if (strlen(Cmd) < 1 || ctmp == 'h' || ctmp == 'H') return usage_pcf7931_write(); uint8_t block = 0, bytepos = 0, data = 0; if ( param_getdec(Cmd, 0, &block) ) return usage_pcf7931_write(); if ( param_getdec(Cmd, 1, &bytepos) ) return usage_pcf7931_write(); if ( (block > 7) || (bytepos > 15) ) return usage_pcf7931_write(); data = param_get8ex(Cmd, 2, 0, 16); PrintAndLog("Writing block: %d", block); PrintAndLog(" pos: %d", bytepos); PrintAndLog(" data: 0x%02X", data); UsbCommand c = {CMD_PCF7931_WRITE, { block, bytepos, data} }; memcpy(c.d.asDwords, configPcf.Pwd, sizeof(configPcf.Pwd) ); c.d.asDwords[7] = (configPcf.OffsetWidth + 128); c.d.asDwords[8] = (configPcf.OffsetPosition + 128); c.d.asDwords[9] = configPcf.InitDelay; clearCommandBuffer(); SendCommand(&c); //no ack? return 0; }
uint8_t param_get8(const char *line, int paramnum) { return param_get8ex(line, paramnum, 0, 10); }
int CmdHF15Restore(const char*Cmd) { FILE *file; uint8_t uid[8]={0x00}; char filename[FILE_PATH_SIZE] = {0x00}; char buff[255] = {0x00}; size_t blocksize=4; uint8_t cmdp = 0; char newCmdPrefix[255] = {0x00}, tmpCmd[255] = {0x00}; char param[FILE_PATH_SIZE]=""; char hex[255]=""; uint8_t retries = 3, tried = 0; int retval=0; size_t bytes_read; uint8_t i=0; while(param_getchar(Cmd, cmdp) != 0x00) { switch(tolower(param_getchar(Cmd, cmdp))) { case '-': param_getstr(Cmd, cmdp, param, sizeof(param)); switch(param[1]) { case '2': case 'o': strncpy(newCmdPrefix, " ",sizeof(newCmdPrefix)-1); strncat(newCmdPrefix, param, sizeof(newCmdPrefix)-1); break; default: PrintAndLogEx(WARNING, "Unknown parameter '%s'", param); return usage_15_restore(); } break; case 'f': param_getstr(Cmd, cmdp+1, filename, FILE_PATH_SIZE); cmdp++; break; case 'r': retries=param_get8ex(Cmd, cmdp+1, 3, 10); cmdp++; break; case 'b': blocksize=param_get8ex(Cmd, cmdp+1, 4, 10); cmdp++; break; case 'u': param_getstr(Cmd, cmdp+1, buff, FILE_PATH_SIZE); cmdp++; snprintf(filename,sizeof(filename),"hf-15-dump-%s-bin",buff); break; case 'h': return usage_15_restore(); default: PrintAndLogEx(WARNING, "Unknown parameter '%c'", param_getchar(Cmd, cmdp)); return usage_15_restore(); break; } cmdp++; } PrintAndLogEx(INFO,"Blocksize: %u",blocksize); if(filename[0]=='\0') { PrintAndLogEx(WARNING,"Please provide a filename"); return 1; } if ((file = fopen(filename,"rb")) == NULL) { PrintAndLogEx(WARNING, "Could not find file %s", filename); return 2; } if (!getUID(uid)) { PrintAndLogEx(WARNING, "No tag found"); return 3; } while (1) { tried=0; hex[0]=0x00; tmpCmd[0]=0x00; bytes_read = fread( buff, 1, blocksize, file ); if ( bytes_read == 0) { PrintAndLogEx(SUCCESS, "File reading done (%s).", filename); fclose(file); return 0; } else if ( bytes_read != blocksize) { PrintAndLogEx(WARNING, "File reading error (%s), %u bytes read instead of %u bytes.", filename, bytes_read, blocksize); fclose(file); return 2; } for(int j=0;j<blocksize;j++) snprintf(hex+j*2,3,"%02X", (unsigned char)buff[j]); for(int j=0;j<sizeof(uid)/sizeof(uid[0]);j++) snprintf(buff+j*2,3,"%02X", uid[j]); //TODO: Addressed mode currently not work //snprintf(tmpCmd, sizeof(tmpCmd), "%s %s %d %s", newCmdPrefix, buff, i, hex); snprintf(tmpCmd, sizeof(tmpCmd), "%s u %d %s", newCmdPrefix, i, hex); PrintAndLogEx(DEBUG, "Command to be sent: %s", tmpCmd); for(tried=0;tried<retries;tried++) if(!(retval=CmdHF15Write(tmpCmd))) break; if(tried >= retries) return retval; i++; } fclose(file); }