tools: add pack kernel tool for rv1108

Change-Id: I9a8e1b2884bcc26144162e3ca838750bef69907a
Signed-off-by: Yifeng Zhao <zyf@rock-chips.com>
Signed-off-by: Zhichao Yu <zhichao.yu@rock-chips.com>
This commit is contained in:
Yifeng Zhao 2020-04-16 20:17:58 +08:00 committed by Jianhong Chen
parent db9b4c53f2
commit c64e256218
1 changed files with 47 additions and 6 deletions

View File

@ -18,10 +18,11 @@ extern uint32_t crc32_rk(uint32_t, const unsigned char *, uint32_t);
#define OPT_UNPACK "--unpack"
#define OPT_UBOOT "--uboot"
#define OPT_TRUSTOS "--trustos"
#define OPT_KERNEL "--kernel"
#define OPT_SIZE "--size"
#define OPT_VERSION "--version"
#define OPT_INFO "--info"
#define OPT_PREPATH "--prepath"
#define OPT_PREPATH "--prepath"
/* pack or unpack */
#define MODE_PACK 0
@ -32,6 +33,7 @@ extern uint32_t crc32_rk(uint32_t, const unsigned char *, uint32_t);
/* image type */
#define IMAGE_UBOOT 0
#define IMAGE_TRUST 1
#define IMAGE_KERNEL 2
/* magic and hash size */
#define LOADER_MAGIC_SIZE 8
@ -62,6 +64,14 @@ extern uint32_t crc32_rk(uint32_t, const unsigned char *, uint32_t);
#define RK_TRUST_MAGIC "TOS "
#define RK_TRUST_RUNNING_ADDR (CONFIG_SYS_TEXT_BASE + SZ_128M + SZ_4M)
#define KERNEL_NAME "kernel"
#define KERNEL_NUM 1
#define KERNEL_MAX_SIZE 30720 * 1024
#define KERNEL_VERSION_STRING "kernel os"
#define RK_KERNEL_MAGIC "KERNEL"
#define RK_KERNEL_RUNNING_ADDR (CONFIG_SYS_TEXT_BASE + SZ_4M)
typedef struct tag_second_loader_hdr {
uint8_t magic[LOADER_MAGIC_SIZE]; /* magic */
uint32_t version;
@ -72,7 +82,8 @@ typedef struct tag_second_loader_hdr {
uint32_t hash_len; /* 20 or 32 , 0 is no hash */
uint8_t hash[LOADER_HASH_SIZE]; /* sha */
uint8_t reserved[1024 - 32 - 32];
unsigned int js_hash; /*js hsah*/
unsigned char reserved[1024-32-32-4];
uint32_t signTag; /* 0x4E474953 */
uint32_t signlen; /* maybe 128 or 256 */
uint8_t rsaHash[256]; /* maybe 128 or 256, using max size 256 */
@ -81,7 +92,7 @@ typedef struct tag_second_loader_hdr {
void usage(const char *prog)
{
fprintf(stderr, "Usage: %s [--pack|--unpack] [--uboot|--trustos]\
fprintf(stderr, "Usage: %s [--pack|--unpack] [--uboot|--trustos|--kernel]\
file_in "
"file_out [load_addr] [--size] [size number]\
[--version] "
@ -112,6 +123,17 @@ unsigned int str2hex(char *str)
return value;
}
static uint32_t js_hash(uint8_t *buf, uint32_t len)
{
uint32_t hash = 0x47C6A7E6;
uint32_t i;
for (i = 0; i < len; i++)
hash ^= ((hash << 5) + buf[i] + (hash >> 2));
return hash;
}
int main(int argc, char *argv[])
{
int mode = -1, image = -1;
@ -152,6 +174,13 @@ int main(int argc, char *argv[])
/* detect whether loader address is delivered */
if ((argv[i + 1]) && (strncmp(argv[i + 1], "--", 2)))
in_loader_addr = str2hex(argv[++i]);
} else if (!strcmp(argv[i], OPT_KERNEL)) {
image = IMAGE_KERNEL;
file_in = argv[++i];
file_out = argv[++i];
/* detect whether loader address is delivered */
if ((argv[i + 1]) && (strncmp(argv[i + 1], "--", 2)))
in_loader_addr = str2hex(argv[++i]);
} else if (!strcmp(argv[i], OPT_SIZE)) {
in_size = strtoul(argv[++i], NULL, 10);
/*
@ -198,6 +227,15 @@ int main(int argc, char *argv[])
max_num = in_num ? in_num : TRUST_NUM;
loader_addr =
(in_loader_addr == -1) ? RK_TRUST_RUNNING_ADDR : in_loader_addr;
} else if (image == IMAGE_KERNEL) {
name = KERNEL_NAME;
magic = RK_KERNEL_MAGIC;
version = KERNEL_VERSION_STRING;
max_size = in_size ? in_size : KERNEL_MAX_SIZE;
max_num = in_num ? in_num : KERNEL_NUM;
loader_addr =
(in_loader_addr == -1) ? RK_KERNEL_RUNNING_ADDR : in_loader_addr;
} else if (mode == MODE_INFO) {
} else {
@ -261,7 +299,7 @@ int main(int argc, char *argv[])
hdr.crc32 = crc32_rk(
0, (const unsigned char *)buf + sizeof(second_loader_hdr), size);
printf("crc = 0x%08x\n", hdr.crc32);
hdr.js_hash = js_hash((uint8_t *)buf + sizeof(second_loader_hdr), size);
#ifndef CONFIG_SECUREBOOT_SHA256
SHA_CTX ctx;
uint8_t *sha;
@ -300,8 +338,11 @@ int main(int argc, char *argv[])
printf("%s version: %s\n", name, version);
memcpy(buf, &hdr, sizeof(second_loader_hdr));
for (i = 0; i < max_num; i++)
fwrite(buf, max_size, 1, fo);
if (image == IMAGE_KERNEL)
fwrite(buf, size + sizeof(second_loader_hdr), 1, fo);
else
for (i = 0; i < max_num; i++)
fwrite(buf, max_size, 1, fo);
printf("pack %s success! \n", file_out);
fclose(fi);