format codes

This commit is contained in:
Chiharu Shirasaka 2020-10-12 17:13:52 +09:00
parent 9e49a0152d
commit 0252289f9f
2 changed files with 141 additions and 142 deletions

183
bsd-ce.c
View File

@ -1,113 +1,114 @@
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdint.h> #include <sys/types.h>
#include <unistd.h>
char* outfile = "nk.bin"; char *outfile = "nk.bin";
int verbose = 0; int verbose = 0;
FILE* fout = NULL; FILE *fout = NULL;
FILE* kfile = NULL; FILE *kfile = NULL;
FILE* lfile = NULL; FILE *lfile = NULL;
char* loader = NULL; char *loader = NULL;
unsigned int loader_size = 0; unsigned int loader_size = 0;
#define WRITE(arg,size) \ #define WRITE(arg, size) \
if(!fwrite(arg, size, 1, fout)) \ if (!fwrite(arg, size, 1, fout)) \
errx(1,"fwrite"); errx(1, "fwrite");
unsigned char magic[7] = "B000FF\n"; unsigned char magic[7] = "B000FF\n";
unsigned int start = 0xa0200000; unsigned int start = 0xa0200000;
unsigned int len = 0; unsigned int len = 0;
void void usage() {
usage(){ fprintf(stderr, "usage: bsd-ce [-h] [-o outfile] u-boot.bin\n");
fprintf(stderr, "usage: bsd-ce [-h] [-o outfile] u-boot.bin\n"); fprintf(stderr, "\n");
fprintf(stderr, "\n"); fprintf(stderr, " -h this help\n");
fprintf(stderr, " -h this help\n"); fprintf(stderr, " -o outfile write output to [outfile]\n");
fprintf(stderr, " -o outfile write output to [outfile]\n"); fprintf(stderr, " default: %s\n", outfile);
fprintf(stderr, " default: %s\n", outfile);
} }
int int main(int argc, char *argv[]) {
main(int argc, char* argv[]){ int n, i;
int n, i; unsigned int i32, cksum, ch;
unsigned int i32, cksum, ch; long len;
long len;
while((i = getopt(argc, argv, "hvo:l:")) != -1) { while ((i = getopt(argc, argv, "hvo:l:")) != -1) {
switch(i) { switch (i) {
case 'o': case 'o':
outfile = optarg; outfile = optarg;
break; break;
case 'h': case 'h':
default: default:
usage(); usage();
return 1; return 1;
} }
} }
argc -= optind; argv += optind; argc -= optind;
if (argc != 1) { argv += optind;
fprintf(stderr, "missing kernel name\n"); if (argc != 1) {
usage(); fprintf(stderr, "missing kernel name\n");
return 1; usage();
} return 1;
}
/* load the loader (err...) */
if ((lfile = fopen(argv[0], "rb")) == NULL)
errx(1, "unable to open '%s'", argv[0]);
fseek(lfile, 0, SEEK_END);
loader_size = ftell(lfile);
fseek(lfile, 0, SEEK_SET);
if ((loader = (char *)malloc(loader_size)) == NULL)
errx(1, "can't allocate %u bytes for loader", loader_size);
if (!fread(loader, loader_size, 1, lfile))
errx(1, "can't read loader file");
fclose(lfile);
/* load the loader (err...) */ // next, try to create the output file
if ((lfile = fopen(argv[0], "rb")) == NULL) if ((fout = fopen(outfile, "wb")) == NULL)
errx(1, "unable to open '%s'", argv[0]); errx(1, "unable to create '%s'", outfile);
fseek(lfile, 0, SEEK_END);
loader_size = ftell(lfile);
fseek(lfile, 0, SEEK_SET);
if ((loader = (char*)malloc(loader_size)) == NULL)
errx(1, "can't allocate %u bytes for loader", loader_size);
if (!fread(loader, loader_size, 1, lfile))
errx(1, "can't read loader file");
fclose(lfile);
// next, try to create the output file /*
if ((fout = fopen(outfile, "wb")) == NULL) * Write NK.BIN header - this is a magic identifier, the first address
errx(1, "unable to create '%s'", outfile); * data is stored, and the total length
*/
WRITE(magic, sizeof(magic));
WRITE(&start, sizeof(start));
WRITE(&loader_size, sizeof(loader_size));
/* /* Calculate the loader checksum */
* Write NK.BIN header - this is a magic identifier, the first address cksum = 0;
* data is stored, and the total length for (len = 0; len < loader_size; len++) {
*/ ch = (unsigned char)loader[len];
WRITE(magic, sizeof(magic)); cksum += ch;
WRITE(&start, sizeof(start)); }
WRITE(&loader_size, sizeof(loader_size)); i32 = start;
WRITE(&i32, sizeof(unsigned int));
i32 = loader_size;
WRITE(&i32, sizeof(unsigned int));
/* Calculate the loader checksum */ i32 = cksum;
cksum = 0; WRITE(&i32, sizeof(unsigned int));
for (len = 0; len < loader_size; len++) {
ch = (unsigned char)loader[len]; cksum += ch;
}
i32 = start;
WRITE(&i32, sizeof(unsigned int));
i32 = loader_size; /* Put the loader in place */
WRITE(&i32, sizeof(unsigned int)); for (len = 0; len < loader_size; len++) {
putc(loader[len], fout);
}
i32 = cksum; /*
WRITE(&i32, sizeof(unsigned int)); * Write NK.BIN footer - this is just a series of 3x uint32_t indicating
* a file of zero length, starting at our init code.
*/
i32 = 0;
WRITE(&i32, sizeof(i32));
i32 = start;
WRITE(&i32, sizeof(i32));
i32 = 0;
WRITE(&i32, sizeof(i32));
/* Put the loader in place */ free(loader);
for (len = 0; len < loader_size; len++) { fclose(fout);
putc(loader[len], fout); return 0;
}
/*
* Write NK.BIN footer - this is just a series of 3x uint32_t indicating
* a file of zero length, starting at our init code.
*/
i32 = 0; WRITE(&i32, sizeof(i32));
i32 = start; WRITE(&i32, sizeof(i32));
i32 = 0; WRITE(&i32, sizeof(i32));
free(loader);
fclose(fout);
return 0;
} }

View File

@ -1,65 +1,63 @@
#include <sys/types.h>
#include <fcntl.h> #include <fcntl.h>
#include <gelf.h> #include <gelf.h>
#include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
uint8_t magic[7] = "B000FF\n"; uint8_t magic[7] = "B000FF\n";
#define READ(buf,len) \ #define READ(buf, len) \
if (!fread(buf,len,1,f)) \ if (!fread(buf, len, 1, f)) \
err(1, "read error"); err(1, "read error");
int int main(int argc, char *argv[]) {
main(int argc, char* argv[]) FILE *f;
{ uint8_t marker[7];
FILE* f; uint32_t i32, addr, cksum, len, i;
uint8_t marker[7]; int n, ch, c;
uint32_t i32, addr, cksum, len, i;
int n, ch, c;
if (argc != 2) { if (argc != 2) {
fprintf(stderr,"usage: nk-dump filename\n"); fprintf(stderr, "usage: nk-dump filename\n");
return 1; return 1;
} }
if ((f = fopen(argv[1], "rb")) == NULL) if ((f = fopen(argv[1], "rb")) == NULL)
err(1, "can't open %s", argv[1]); err(1, "can't open %s", argv[1]);
READ(marker, sizeof(marker)); READ(marker, sizeof(marker));
if (memcmp(magic, marker, sizeof(magic))) if (memcmp(magic, marker, sizeof(magic)))
errx(1, "signature does not match"); errx(1, "signature does not match");
READ(&i32, sizeof(i32)); READ(&i32, sizeof(i32));
printf("addr %08x\n", i32); printf("addr %08x\n", i32);
READ(&i32, sizeof(i32)); READ(&i32, sizeof(i32));
printf("length %08x\n", i32); printf("length %08x\n", i32);
n = 0; n = 0;
while (!feof(f)) { while (!feof(f)) {
READ(&addr, sizeof(addr)); READ(&addr, sizeof(addr));
READ(&len, sizeof(len)); READ(&len, sizeof(len));
READ(&cksum, sizeof(cksum)); READ(&cksum, sizeof(cksum));
printf("image %u\n", n); printf("image %u\n", n);
c = 0; c = 0;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
ch = getc(f); c += ch; ch = getc(f);
} c += ch;
printf(" addr %08x\n", addr); }
printf(" len %08x\n", len); printf(" addr %08x\n", addr);
printf(" cksum %08x", cksum); printf(" len %08x\n", len);
if (cksum == c) { printf(" cksum %08x", cksum);
printf(" [ok]\n"); if (cksum == c) {
} else { printf(" [ok]\n");
printf(" != calculated cksum %08x\n", c); } else {
} printf(" != calculated cksum %08x\n", c);
}
n++; n++;
}
fclose(f);
} return 0;
fclose(f);
return 0;
} }
/* vim:set ts=2 sw=2: */ /* vim:set ts=2 sw=2: */