rp2040

RP2040 Programming without SDK
Log | Files | Refs

bin2uf2.c (2077B)


      1 #include <stdio.h>
      2 #include <stdint.h>
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 
      7 size_t
      8 fwrite32l(uint32_t d, FILE *f)
      9 {
     10 	int i;
     11 	uint8_t b;
     12 	for (i = 0; i < 32; i += 8) {
     13 		b = (uint8_t) (d >> i & 0xff);
     14 		fwrite(&b, 1, 1, f);
     15 		if (ferror(f)) {
     16 			fprintf(stderr, "Fwrite32l: write error.\n");
     17 			return 0;
     18 		}
     19 	}
     20 	return 4;
     21 }
     22 
     23 int
     24 main(int argc, char *argv[])
     25 {
     26 	FILE *src = NULL, *dst = NULL;
     27 	size_t sdata = 476;
     28 	int retnum = 0;
     29 
     30 	uint32_t mag1 = 0x0A324655;
     31 	uint32_t mag2 = 0x9E5D5157;
     32 	uint32_t flags = 0x00002000; // familyID present
     33 	uint32_t addr = 0x10000000;
     34 	uint32_t nbyte = 256;
     35 	uint32_t blk = 0;
     36 	uint32_t nblk = 0;
     37 	uint32_t famid = 0xe48bff56;
     38 	uint8_t data[sdata];
     39 	uint32_t mag3 = 0x0AB16F30;
     40 
     41 	memset(data, 0, sdata);	
     42 
     43 	if (argc != 3) {
     44 		fprintf(stderr, "Usage: %s src dst\n", argv[0]);
     45 		exit(1);
     46 	}
     47 
     48 	if ((src = fopen(argv[1], "rb")) == NULL) {
     49 		fprintf(stderr, "Could not open %s.\n", argv[1]);
     50 		retnum = 1;
     51 		goto defer;
     52 	}
     53 	if ((dst = fopen(argv[2], "wb")) == NULL) {
     54 		fprintf(stderr, "Could not open %s.\n", argv[2]);
     55 		retnum = 1;
     56 		goto defer;
     57 	}
     58 	
     59 	while (!feof(src)) {
     60 		fwrite32l(mag1, dst);
     61 		fwrite32l(mag2, dst);
     62 		fwrite32l(flags, dst);
     63 		fwrite32l(addr, dst);
     64 		fwrite32l(nbyte, dst);
     65 		fwrite32l(blk, dst);
     66 		fwrite32l(nblk, dst); // dummy
     67 		fwrite32l(famid, dst);
     68 
     69 		fread(data, 1, nbyte, src);
     70 		if (ferror(src)) {
     71 			fprintf(stderr, "Read error: %s.\n", argv[1]);
     72 			retnum = 1;
     73 			goto defer;
     74 		}
     75 		fwrite(data, 1, sdata, dst);
     76 		if (ferror(src)) {
     77 			fprintf(stderr, "Write error: %s.\n", argv[2]);
     78 			retnum = 1;
     79 			goto defer;
     80 		}
     81 
     82 		fwrite32l(mag3, dst);
     83 
     84 		addr += nbyte;
     85 		blk++;
     86 		nblk++;
     87 	}
     88 
     89 	for (int i = 0; i < nblk; i++) {
     90 		if (i == 0)
     91 			if (fseek(dst, 24, SEEK_SET) < 0) {
     92 				fprintf(stderr, "Seek error: %s.\n argv[2]");
     93 				retnum = 1;
     94 				goto defer;
     95 			}
     96 		fwrite32l(nblk, dst);
     97 		if (i < nblk - 1)
     98 			if(fseek(dst, 512 - 4, SEEK_CUR) < 0){
     99 				fprintf(stderr, "Seek error: %s.\n argv[2]");
    100 				retnum = 1;
    101 				goto defer;
    102 			}
    103 	}
    104 	
    105 defer:
    106 	if (src)
    107 		fclose(src);
    108 	if (dst)
    109 		fclose(dst);
    110 	return retnum;
    111 }