int sendCommandAndWaitForResponse(int fd, uint8 *command) { uint8 response[4] = {0}; int res = 0; res = write(fd, command, HEADER_LENGTH + command[LENGTH_OFFSET]); if( res <= 0 ) { puts("ERROR"); return -1; } res = readWithTimeout(fd, response, 1, 5); if( res != 1 ) { puts("ERROR"); return -1; } else if (response[0]== BOOTLOADER_PROT) { printf("(SKIPPED by bootloader)..."); return 0; } else if ( response[0] != BOOTLOADER_OK ) { printf("ERROR [%02x]\n", response[0]); return -1; } else { return 0; } }
//low lever send command, get reply function int writetopirate(uint8* val){ int res = -1; uint8 buffer[2] = {0}; sendString(cmdLineArgs.dev_fd, 1, val); res = readWithTimeout(cmdLineArgs.dev_fd, buffer, 1, 1); if( memcmp(buffer, "\x01", 1) ) { puts("ERROR"); return -1; } return 0; }
uint8 PIC416Read(uint8 cmd){ uint16 PICread; uint8 buffer[2]={0}; int res = -1; buffer[0]='\xA5'; buffer[1]=cmd; sendString(cmdLineArgs.dev_fd, 2, buffer); res = readWithTimeout(cmdLineArgs.dev_fd, buffer, 1, 1); PICread=buffer[0]; return PICread; }
int MJPEGServer::readLineWithTimeout(int fd, iobuffer *iobuf, char *buffer, size_t len, int timeout) { char c = '\0', *out = buffer; int i; memset(buffer, 0, len); for(i = 0; i < len && c != '\n'; i++) { if(readWithTimeout(fd, iobuf, &c, 1, timeout) <= 0) { /* timeout or error occured */ return -1; } *out++ = c; } return i; }
int PIC416Write(uint8 cmd, uint16 data){ uint8 buffer[4]={0}; int res = -1; buffer[0]='\xA4'; buffer[1]=cmd; buffer[2]=(uint8)(data); buffer[3]=(data>>8); sendString(cmdLineArgs.dev_fd, 4, buffer); res = readWithTimeout(cmdLineArgs.dev_fd, buffer, 1, 1); if( memcmp(buffer, "\x01", 1) ) { puts("ERROR"); return -1; } return 0; }
int main (int argc, const char** argv) { int dev_fd = -1, res = -1; uint8 buffer[256] = {0}; uint8 pages_used[PIC_NUM_PAGES] = {0}; uint8* bin_buff = NULL; puts("+++++++++++++++++++++++++++++++++++++++++++"); puts(" Pirate-Loader for BP with Bootloader v4+ "); puts(" Loader version: " PIRATE_LOADER_VERSION " OS: " OS_NAME(OS)); puts("+++++++++++++++++++++++++++++++++++++++++++\n"); if( (res = parseCommandLine(argc, argv)) < 0 ) { return -1; } else if( res == 0 ) { return 0; } if( !g_hello_only ) { if( !g_hexfile_path ) { fprintf(stderr, "Please specify hexfile path --hex=/path/to/hexfile.hex\n"); return -1; } bin_buff = (uint8*)malloc(0xFFFFFF * sizeof(uint8)); //256kB if( !bin_buff ) { fprintf(stderr, "Could not allocate 256kB buffer\n"); goto Error; } //fill the buffer with 0xFF memset(bin_buff, 0xFFFFFFFF, (0xFFFFFF * sizeof(uint8))); printf("Parsing HEX file [%s]\n", g_hexfile_path); res = readHEX(g_hexfile_path, bin_buff, (0xFFFFFF * sizeof(uint8)), pages_used); if( res <= 0 || res > flashsize ) { fprintf(stderr, "Could not load HEX file, result=%d\n", res); goto Error; } printf("Found %d words (%d bytes)\n", res, res * 3); //printf("Fixing bootloader/userprogram jumps\n"); //fixJumps(bin_buff, pages_used); } if( g_simulate ) { sendFirmware(dev_fd, bin_buff, pages_used); goto Finished; } if( !g_device_path ) { fprintf(stderr, "Please specify serial device path --dev=/dev/...\n"); return -1; } printf("Opening serial device %s...", g_device_path); dev_fd = openPort(g_device_path, 0); if( dev_fd < 0 ) { puts("ERROR"); fprintf(stderr, "Could not open %s\n", g_device_path); goto Error; } puts("OK"); printf("Configuring serial port settings..."); if( configurePort(dev_fd, B115200) < 0 ) { puts("ERROR"); fprintf(stderr, "Could not configure device, errno=%d\n", errno); goto Error; } puts("OK"); printf("Sending Hello to the Bootloader..."); //send HELLO res = write(dev_fd, BOOTLOADER_HELLO_STR, 1); res = readWithTimeout(dev_fd, buffer, 4, 3); if( res != 4 || buffer[3] != BOOTLOADER_OK ) { puts("ERROR"); fprintf(stderr, "No reply from the bootloader, or invalid reply received: %d\n", res); fprintf(stderr, "Please make sure that PGND and PGC are connected, replug the devide and try again\n"); goto Error; } puts("OK\n"); //extra LF for spacing printf("Bootloader version: %d,%02d\n", buffer[1], buffer[2]); printf("Device ID [%02x]:",buffer[0]); switch(buffer[0]) { // case 0xd4: // printf("PIC24FJ64GA002\n"); // break; case 9: printf("PIC24FJ128GB206\n"); // __PIC24FJ128GB206__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 17: printf("PIC24FJ128GB210\n"); // __PIC24FJ128GB210__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 18: printf("PIC24FJ256GB206\n"); // __PIC24FJ256GB206__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 19: printf("PIC24FJ256GB210\n"); // __PIC24FJ256GB210__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 191: printf("PIC24FJ256DA206\n"); // __PIC24FJ256DA206__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 192: printf("PIC24FJ256DA210\n"); // __PIC24FJ256DA210__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 217: printf("PIC24FJ64GB106\n"); // __PIC24FJ64GB106__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 218: printf("PIC24FJ64GB108\n"); // __PIC24FJ64GB108__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 219: printf("PIC24FJ64GB110\n"); // __PIC24FJ64GB110__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 229: printf("PIC24FJ128GB106\n"); // __PIC24FJ128GB106__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 230: printf("PIC24FJ128GB108\n"); // __PIC24FJ128GB108__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 231: printf("PIC24FJ128GB110\n"); // __PIC24FJ128GB110__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 235: printf("PIC24FJ192GB106\n"); // __PIC24FJ192GB106__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 236: printf("PIC24FJ192GB108\n"); // __PIC24FJ192GB108__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 237: printf("PIC24FJ192GB110\n"); // __PIC24FJ192GB110__ family = IS_24FJ; flashsize = 0x20C00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 241: printf("PIC24FJ256GB106\n"); // __PIC24FJ256GB106__ #define IS_24FJ 1 flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 242: printf("PIC24FJ256GB108\n"); // __PIC24FJ256GB108__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 243: printf("PIC24FJ256GB110\n"); // __PIC24FJ256GB110__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 244: printf("PIC24FJ32GB002\n"); // __PIC24FJ32GB002__ family = IS_24FJ; flashsize = 0x5800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 245: printf("PIC24FJ32GB004\n"); // __PIC24FJ32GB004__ family = IS_24FJ; flashsize = 0x5800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 246: printf("PIC24FJ64GB002\n"); // __PIC24FJ64GB002__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 247: printf("PIC24FJ64GB004\n"); // __PIC24FJ64GB004__ family = IS_24FJ; flashsize = 0xAC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 250: printf("PIC24FJ128DA106\n");// __PIC24FJ128DA106__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 251: printf("PIC24FJ128DA110\n"); // __PIC24FJ128DA110__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 252: printf("PIC24FJ128DA206\n"); // __PIC24FJ128DA206__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 253: printf("PIC24FJ128DA210\n"); // __PIC24FJ128DA210__ family = IS_24FJ; flashsize = 0x15800; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 254: printf("PIC24FJ256DA106\n"); // __PIC24FJ256DA106__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 255: printf("PIC24FJ256DA110\n"); // __PIC24FJ256DA110__ family = IS_24FJ; flashsize = 0x2AC00; eesizeb = 0; blstartaddr = 0x400L; blendaddr = 0x23FFL; break; case 206: printf("PIC24FJ16GA002\n"); flashsize = 0x2C00; break; case 207: printf("PIC24FJ16GA004\n"); flashsize = 0x2C00; break; case 208: printf("PIC24FJ32GA002\n"); flashsize = 0x5800; break; case 209: printf("PIC24FJ32GA004\n"); flashsize = 0x5800; break; case 210: printf("PIC24FJ48GA002\n"); flashsize = 0x8400; break; case 211: printf("PIC24FJ48GA004\n"); flashsize = 0x8400; break; case 212: printf("PIC24FJ64GA002\n"); flashsize = 0xAC00; break; case 213: printf("PIC24FJ64GA004\n"); flashsize = 0xAC00; break; case 214: printf("PIC24FJ64GA006\n"); flashsize = 0xAC00; break; printf("PIC24FJ64GA008\n"); flashsize = 0xAC00; case 215: break; case 216: printf("PIC24FJ64GA010\n"); flashsize = 0xAC00; break; case 220: printf("PIC24FJ96GA006\n"); flashsize = 0x10000; break; case 221: printf("PIC24FJ96GA008\n"); flashsize = 0x10000; break; case 222: printf("PIC24FJ96GA010\n"); flashsize = 0x10000; break; case 223: printf("PIC24FJ128GA006\n"); flashsize = 0x15800; break; case 224: printf("PIC24FJ128GA008\n"); flashsize = 0x15800; break; case 225: printf("PIC24FJ128GA010\n"); flashsize = 0x15800; break; case 226: printf("PIC24FJ128GA106\n"); flashsize = 0x15800; break; case 227: printf("PIC24FJ128GA108\n"); flashsize = 0x15800; break; case 228: printf("PIC24FJ128GA110\n"); flashsize = 0x15800; break; case 232: printf("PIC24FJ192GA106\n"); flashsize = 0x20C00; break; case 233: printf("PIC24FJ192GA108\n"); flashsize = 0x20C00; break; case 234: printf("PIC24FJ192GA110\n"); flashsize = 0x20C00; break; case 238: printf("PIC24FJ256GA106\n"); flashsize = 0x2AC00; break; case 239: printf("PIC24FJ256GA108\n"); flashsize = 0x2AC00; break; case 240: printf("PIC24FJ256GA110\n"); flashsize = 0x2AC00; break; default: printf("UNKNOWN"); fprintf(stderr, "Unsupported device (%02x:UNKNOWN)\n", buffer[0]); goto Error; break; } if( !g_hello_only ) { res = sendFirmware(dev_fd, bin_buff, pages_used); if( res > 0 ) { puts("\nFirmware updated successfully :)!"); //printf("Use screen %s 115200 to verify\n", g_device_path); } else { puts("\nError updating firmware :("); goto Error; } } Finished: if( bin_buff ) { free( bin_buff ); } if( dev_fd >= 0 ) { close(dev_fd); } return 0; Error: if( bin_buff ) { free( bin_buff ); } if( dev_fd >= 0 ) { close(dev_fd); } return -1; }
int WdtSocket::read(char *buf, int nbyte, bool tryFull) { return readWithTimeout(buf, nbyte, threadCtx_.getOptions().read_timeout_millis, tryFull); }
int WdtSocket::read(char *buf, int nbyte, bool tryFull) { return readWithTimeout(buf, nbyte, options_.read_timeout_millis, tryFull); }