int main (int argc, const char * argv[]) { FILE *fp, *fp_write; AtmelGenericRecord arec; IHexRecord irec; SRecord srec; int addr; int nodeid; if (argc < 3) { fprintf(stderr, "Usage: %s <file format> <file>\n", argv[0]); fprintf(stderr, "This program will print the records saved in a generic,\nIntel HEX, or Motorola S-Record formatted file.\n\n"); fprintf(stderr, "<file format> can be generic, ihex, or srecord.\n"); fprintf(stderr, "<file> is the path to the file containing the records.\n"); return -1; } fp = fopen(argv[2], "r"); if (fp == NULL) { perror("Error opening file!"); return -1; } fp_write = fopen(argv[3], "w"); if (fp_write == NULL) { perror("Error opening file!"); return -1; } addr = strtol(argv[4], (char **)NULL, 16); nodeid = strtol(argv[5], (char **)NULL, 10); if (strcasecmp(argv[1], "generic") == 0) { while (Read_AtmelGenericRecord(&arec, fp) == ATMEL_GENERIC_OK) { Print_AtmelGenericRecord(&arec); printf("\n"); } } else if (strcasecmp(argv[1], "ihex") == 0) { while (Read_IHexRecord(&irec, fp) == IHEX_OK) { Write_IHexRecord(&irec, fp_write, addr, nodeid); } } else if (strcasecmp(argv[1], "srecord") == 0) { while (Read_SRecord(&srec, fp) == SRECORD_OK) { Print_SRecord(&srec); printf("\n"); } } else { fprintf(stderr, "Invalid file format specified!\n"); fclose(fp); fclose(fp_write); return -1; } fclose(fp); return 0; }
int byte_stream_srecord_read(struct ByteStream *self, uint8_t *data, uint32_t *address) { struct byte_stream_srecord_state *state = (struct byte_stream_srecord_state *)self->state; int ret; if (state->availBytes == 0) { do { /* Read the next record */ ret = Read_SRecord(&(state->sRec), self->in); switch (ret) { case SRECORD_OK: break; case SRECORD_ERROR_NEWLINE: continue; case SRECORD_ERROR_EOF: return STREAM_EOF; case SRECORD_ERROR_FILE: self->error = "Error reading Motorola S-Record formatted file!"; return STREAM_ERROR_INPUT; case SRECORD_ERROR_INVALID_RECORD: self->error = "Invalid Motorola S-Record formatted file!"; return STREAM_ERROR_INPUT; default: self->error = "Unknown error reading Motorola S-Record formatted file!"; return STREAM_ERROR_INPUT; } /* Continue reading until we get a data record */ } while (state->sRec.type != SRECORD_TYPE_S1 && state->sRec.type != SRECORD_TYPE_S2 && state->sRec.type != SRECORD_TYPE_S3); /* Update our available bytes counter */ state->availBytes = state->sRec.dataLen; } if (state->availBytes) { /* Copy over next low byte (assuming little-endian) */ *data = state->sRec.data[state->sRec.dataLen - state->availBytes]; *address = (uint32_t)state->sRec.address + (uint32_t)(state->sRec.dataLen - state->availBytes); state->availBytes--; } return 0; }