Example #1
0
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;
}
Example #2
0
/* dump all of device flash to fn */
int nrf_dump(devp dev, const char *fn){
    unsigned char cmd[2], data[64];
    FILE *fp = NULL;
    int ecode, block, sub_block;
    IHexRecord record;

    if((fp = fopen(fn, "w+")) == NULL){
        ecode = -1;
        goto err;
    }

    /* dump */
    for(block = 0; block < 0x200; block++){
        /* set address MSB */
        if((block % 0x100) == 0){
            cmd[0] = 0x06;
            cmd[1] = (unsigned char)(block / 256);
            if(nrf_cmd(dev, cmd, 2, data, 1)){
                ecode = -2;
                goto err;
            }
        }
        /* request the block */
        cmd[0] = 0x03;
        cmd[1] = (unsigned char)block;
        if(nrf_cmd(dev, cmd, 2, data, 64)){
            ecode = -2;
            goto err;
        }
        /* write two records for this block
         * records are only written if the block contains something not 0xFF
         */
        for(sub_block = 0; sub_block < 2; sub_block++){
            record.type = IHEX_TYPE_00;
            memcpy(record.data, &data[sub_block * 32], 32);
            record.dataLen = 32;
            record.address = block * 64 + sub_block * 32;
            if(memnotchr(record.data, 0xFF, 32) &&
                    Write_IHexRecord(&record, fp)){
                ecode = -3;
                goto err;
            }
        }
    }

    /* write EoF record */
    record.type = IHEX_TYPE_01;
    record.address = 0;
    record.dataLen = 0;
    if(Write_IHexRecord(&record, fp)){
        ecode = -3;
        goto err;
    }

    ecode = 0;
err:
    if(fp){
        fclose(fp);
    }
    return ecode;
}