mirror of
https://github.com/brain-hackers/nkbin_maker
synced 2024-12-31 00:20:05 +09:00
first commit
This commit is contained in:
commit
58e7f456f0
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
bsd-ce
|
||||
nk.bin
|
||||
nk-dump
|
||||
u-boot.bin
|
10
Makefile
Normal file
10
Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
all: bsd-ce nk-dump
|
||||
|
||||
bsd-ce: bsd-ce.c
|
||||
$(CC) -o bsd-ce bsd-ce.c
|
||||
|
||||
nk-dump: nk-dump.c
|
||||
$(CC) -o nk-dump nk-dump.c
|
||||
|
||||
clean:
|
||||
rm -f bsd-ce nk-dump
|
9
README
Normal file
9
README
Normal file
@ -0,0 +1,9 @@
|
||||
This is a nk.bin maker which contains U-Boot
|
||||
Based on bsd-ce(https://rink.nu/projects/bsd-ce.html)
|
||||
|
||||
動作原理:
|
||||
SHARP BrainはOSとしてWindows CE 6.0を搭載している
|
||||
Brainに搭載されているWindows CE用のブートローダー(EBOOT)はSDカードから所定の名前のOSイメージを読み込み
|
||||
仮想アドレス0xA0200000(物理アドレス0x40200000)へとジャンプする
|
||||
ここでBSDカーネルをWindows CEのブートローダーから起動するためのnk.binを生成するプログラムを流用し
|
||||
0xA0200000へU-Bootを読み込むようにパックしたnk.binを生成するのが本プログラムである
|
113
bsd-ce.c
Normal file
113
bsd-ce.c
Normal file
@ -0,0 +1,113 @@
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
char* outfile = "nk.bin";
|
||||
int verbose = 0;
|
||||
FILE* fout = NULL;
|
||||
FILE* kfile = NULL;
|
||||
FILE* lfile = NULL;
|
||||
char* loader = NULL;
|
||||
unsigned int loader_size = 0;
|
||||
|
||||
#define WRITE(arg,size) \
|
||||
if(!fwrite(arg, size, 1, fout)) \
|
||||
errx(1,"fwrite");
|
||||
|
||||
unsigned char magic[7] = "B000FF\n";
|
||||
unsigned int start = 0xa0200000;
|
||||
unsigned int len = 0;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char* argv[]){
|
||||
int n, i;
|
||||
unsigned int i32, cksum, ch;
|
||||
long len;
|
||||
|
||||
while((i = getopt(argc, argv, "hvo:l:")) != -1) {
|
||||
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);
|
||||
|
||||
// next, try to create the output file
|
||||
if ((fout = fopen(outfile, "wb")) == NULL)
|
||||
errx(1, "unable to create '%s'", outfile);
|
||||
|
||||
/*
|
||||
* 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));
|
||||
|
||||
|
||||
/* 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));
|
||||
|
||||
i32 = loader_size;
|
||||
WRITE(&i32, sizeof(unsigned int));
|
||||
|
||||
i32 = cksum;
|
||||
WRITE(&i32, sizeof(unsigned int));
|
||||
|
||||
/* Put the loader in place */
|
||||
for (len = 0; len < loader_size; len++) {
|
||||
putc(loader[len], fout);
|
||||
}
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
65
nk-dump.c
Normal file
65
nk-dump.c
Normal file
@ -0,0 +1,65 @@
|
||||
#include <sys/types.h>
|
||||
#include <fcntl.h>
|
||||
#include <gelf.h>
|
||||
#include <unistd.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
uint8_t magic[7] = "B000FF\n";
|
||||
|
||||
#define READ(buf,len) \
|
||||
if (!fread(buf,len,1,f)) \
|
||||
err(1, "read error");
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
FILE* f;
|
||||
uint8_t marker[7];
|
||||
uint32_t i32, addr, cksum, len, i;
|
||||
int n, ch, c;
|
||||
|
||||
if (argc != 2) {
|
||||
fprintf(stderr,"usage: nk-dump filename\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ((f = fopen(argv[1], "rb")) == NULL)
|
||||
err(1, "can't open %s", argv[1]);
|
||||
|
||||
READ(marker, sizeof(marker));
|
||||
if (memcmp(magic, marker, sizeof(magic)))
|
||||
errx(1, "signature does not match");
|
||||
READ(&i32, sizeof(i32));
|
||||
printf("addr %08x\n", i32);
|
||||
READ(&i32, sizeof(i32));
|
||||
printf("length %08x\n", i32);
|
||||
n = 0;
|
||||
while (!feof(f)) {
|
||||
READ(&addr, sizeof(addr));
|
||||
READ(&len, sizeof(len));
|
||||
READ(&cksum, sizeof(cksum));
|
||||
|
||||
printf("image %u\n", n);
|
||||
c = 0;
|
||||
for (i = 0; i < len; i++) {
|
||||
ch = getc(f); c += ch;
|
||||
}
|
||||
printf(" addr %08x\n", addr);
|
||||
printf(" len %08x\n", len);
|
||||
printf(" cksum %08x", cksum);
|
||||
if (cksum == c) {
|
||||
printf(" [ok]\n");
|
||||
} else {
|
||||
printf(" != calculated cksum %08x\n", c);
|
||||
}
|
||||
|
||||
n++;
|
||||
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* vim:set ts=2 sw=2: */
|
Loading…
Reference in New Issue
Block a user