nkbin_maker/bsd-ce.c

116 lines
3.0 KiB
C
Raw Normal View History

#include <err.h>
2020-10-09 18:29:19 +09:00
#include <fcntl.h>
2020-10-12 17:13:52 +09:00
#include <stdint.h>
2020-10-09 18:29:19 +09:00
#include <stdio.h>
#include <stdlib.h>
2020-10-12 17:13:52 +09:00
#include <sys/types.h>
#include <unistd.h>
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
char *outfile = "nk.bin";
int verbose = 0;
FILE *fout = NULL;
FILE *kfile = NULL;
FILE *lfile = NULL;
char *loader = NULL;
2020-10-09 18:29:19 +09:00
unsigned int loader_size = 0;
2020-10-12 17:13:52 +09:00
#define WRITE(arg, size) \
if (!fwrite(arg, size, 1, fout)) \
errx(1, "fwrite");
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
unsigned char magic[7] = "B000FF\n";
2020-10-09 18:29:19 +09:00
unsigned int start = 0xa0200000;
2020-10-12 17:13:52 +09:00
unsigned int len = 0;
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
void usage() {
fprintf(stderr, "usage: bsd-ce [-h] [-o outfile] u-boot.bin\n");
fprintf(stderr, "\n");
fprintf(stderr, " -h this help\n");
fprintf(stderr, " -o outfile write output to [outfile]\n");
fprintf(stderr, " default: %s\n", outfile);
2020-10-09 18:29:19 +09:00
}
2020-10-12 17:13:52 +09:00
int main(int argc, char *argv[]) {
int n, i;
unsigned int i32, cksum, ch;
long len;
2020-10-16 22:41:10 +09:00
while ((i = getopt(argc, argv, "ho:")) != -1) {
2020-10-12 17:13:52 +09:00
switch (i) {
case 'o':
outfile = optarg;
break;
case 'h':
default:
usage();
return 1;
}
}
argc -= optind;
argv += optind;
if (argc != 1) {
fprintf(stderr, "missing kernel name\n");
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);
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
// next, try to create the output file
if ((fout = fopen(outfile, "wb")) == NULL)
errx(1, "unable to create '%s'", outfile);
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
/*
* Write NK.BIN header - this is a magic identifier, the first address
* data is stored, and the total length
*/
WRITE(magic, sizeof(magic));
WRITE(&start, sizeof(start));
WRITE(&loader_size, sizeof(loader_size));
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
/* Calculate the loader checksum */
cksum = 0;
for (len = 0; len < loader_size; len++) {
ch = (unsigned char)loader[len];
cksum += ch;
}
i32 = start;
WRITE(&i32, sizeof(unsigned int));
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
i32 = loader_size;
WRITE(&i32, sizeof(unsigned int));
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
i32 = cksum;
WRITE(&i32, sizeof(unsigned int));
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
/* Put the loader in place */
for (len = 0; len < loader_size; len++) {
putc(loader[len], fout);
}
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
/*
* 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));
2020-10-09 18:29:19 +09:00
2020-10-12 17:13:52 +09:00
free(loader);
fclose(fout);
return 0;
2020-10-09 18:29:19 +09:00
}