summaryrefslogtreecommitdiff
path: root/utils/rknanoutils/rkboottool/rkboottool.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/rknanoutils/rkboottool/rkboottool.c')
-rw-r--r--utils/rknanoutils/rkboottool/rkboottool.c211
1 files changed, 163 insertions, 48 deletions
diff --git a/utils/rknanoutils/rkboottool/rkboottool.c b/utils/rknanoutils/rkboottool/rkboottool.c
index ee9b17e610..763751e41f 100644
--- a/utils/rknanoutils/rkboottool/rkboottool.c
+++ b/utils/rknanoutils/rkboottool/rkboottool.c
@@ -15,6 +15,11 @@ bool g_debug = false;
15typedef uint8_t packed_bcd_uint8_t; 15typedef uint8_t packed_bcd_uint8_t;
16typedef uint16_t packed_bcd_uint16_t; 16typedef uint16_t packed_bcd_uint16_t;
17 17
18/**
19 * RKnanoFW
20 * contains resources and code stages
21 */
22
18struct rknano_date_t 23struct rknano_date_t
19{ 24{
20 packed_bcd_uint16_t year; 25 packed_bcd_uint16_t year;
@@ -176,7 +181,7 @@ static void save_blob(const struct rknano_blob_t *b, void *buf, uint32_t size,
176 } 181 }
177 encode_page(buff_ptr, out_ptr, len); 182 encode_page(buff_ptr, out_ptr, len);
178 } 183 }
179 184
180 if(f) 185 if(f)
181 { 186 {
182 fwrite(ptr, b->size, 1, f); 187 fwrite(ptr, b->size, 1, f);
@@ -276,6 +281,11 @@ static int do_nanofw_image(uint8_t *buf, unsigned long size)
276 return 0; 281 return 0;
277} 282}
278 283
284/**
285 * RKNano stage
286 * contains code and memory mapping
287 */
288
279struct rknano_stage_header_t 289struct rknano_stage_header_t
280{ 290{
281 uint32_t addr; 291 uint32_t addr;
@@ -283,21 +293,28 @@ struct rknano_stage_header_t
283} __attribute__((packed)); 293} __attribute__((packed));
284 294
285/* 295/*
286 * The [code_pa,code_pa+code_sz[ and [data_pa,data_pa+data_sz[ ranges 296 * NOTE this theory has not been tested against actual code, it's still a guess
287 * are consistent: they never overlap and have no gaps and fill the 297 * The firmware is too big to fit in memory so it's split into sections,
288 * entire space. Furthermore they match the code sequences so it's 298 * each section having a "virtual address" and a "physical address".
289 * reasonable to assume these fields are correct. 299 * Except it gets tricky because the RKNano doesn't have a MMU but a MPU,
290 * The other fields are still quite unsure. */ 300 * so most probably the OF divides the memory into regions (8 would match
301 * hardware capabilities), each being able to contain one of the sections
302 * in the OF file. To gracefully handle jumps between sections, my guess is
303 * that the entire OF is linked as a flat image, cut into pieces and
304 * then each code section get relocated except for jump/calls outside of it:
305 * this will trigger an access fault when trying to access another section, which
306 * the OF can trap and then load the corresponding section.
307 */
291 308
292struct rknano_stage_section_t 309struct rknano_stage_section_t
293{ 310{
294 uint32_t code_pa;
295 uint32_t code_va; 311 uint32_t code_va;
312 uint32_t code_pa;
296 uint32_t code_sz; 313 uint32_t code_sz;
297 uint32_t data_pa;
298 uint32_t data_va; 314 uint32_t data_va;
315 uint32_t data_pa;
299 uint32_t data_sz; 316 uint32_t data_sz;
300 uint32_t bss_va; 317 uint32_t bss_pa;
301 uint32_t bss_sz; 318 uint32_t bss_sz;
302} __attribute__((packed)); 319} __attribute__((packed));
303 320
@@ -319,14 +336,14 @@ static void elf_write(void *user, uint32_t addr, const void *buf, size_t count)
319 fwrite(buf, count, 1, f); 336 fwrite(buf, count, 1, f);
320} 337}
321 338
322static void extract_elf_section(struct elf_params_t *elf, int count) 339static void extract_elf_section(struct elf_params_t *elf)
323{ 340{
324 if(g_out_prefix == NULL) 341 if(g_out_prefix == NULL)
325 return; 342 return;
326 char *filename = xmalloc(strlen(g_out_prefix) + 32); 343 char *filename = xmalloc(strlen(g_out_prefix) + 32);
327 sprintf(filename, "%s%d.elf", g_out_prefix, count); 344 sprintf(filename, "%s.elf", g_out_prefix);
328 if(g_debug) 345 if(g_debug)
329 printf("Write entry %d to %s\n", count, filename); 346 printf("Write stage to %s\n", filename);
330 347
331 FILE *fd = fopen(filename, "wb"); 348 FILE *fd = fopen(filename, "wb");
332 free(filename); 349 free(filename);
@@ -337,67 +354,149 @@ static void extract_elf_section(struct elf_params_t *elf, int count)
337 fclose(fd); 354 fclose(fd);
338} 355}
339 356
357struct range_t
358{
359 unsigned long start, size;
360 int section;
361 int type;
362};
363
364int range_cmp(const void *_a, const void *_b)
365{
366 const struct range_t *a = _a, *b = _b;
367 if(a->start == b->start)
368 return a->size - b->size;
369 return a->start - b->start;
370}
371
372#define RANGE_TXT 0
373#define RANGE_DAT 1
374
340static int do_nanostage_image(uint8_t *buf, unsigned long size) 375static int do_nanostage_image(uint8_t *buf, unsigned long size)
341{ 376{
342 if(size < sizeof(struct rknano_stage_section_t)) 377 if(size < sizeof(struct rknano_stage_section_t))
343 return 1; 378 return 1;
344 struct rknano_stage_header_t *hdr = (void *)buf; 379 struct rknano_stage_header_t *hdr = (void *)buf;
380 size_t hdr_size = sizeof(struct rknano_stage_header_t) +
381 hdr->count * sizeof(struct rknano_stage_section_t);
382 if(size < hdr_size)
383 return 1;
345 384
346 cprintf(BLUE, "Header\n"); 385 cprintf(BLUE, "Header\n");
347 cprintf(GREEN, " Base Address: "); 386 cprintf(GREEN, " Base Address: ");
348 cprintf(YELLOW, "%#08x\n", hdr->addr); 387 cprintf(YELLOW, "%#08x\n", hdr->addr);
349 cprintf(GREEN, " Load count: "); 388 cprintf(GREEN, " Section count: ");
350 cprintf(YELLOW, "%d\n", hdr->count); 389 cprintf(YELLOW, "%d\n", hdr->count);
351
352 struct rknano_stage_section_t *sec = (void *)(hdr + 1);
353 390
391 struct rknano_stage_section_t *sec = (void *)(hdr + 1);
392 struct elf_params_t elf;
393 elf_init(&elf);
394 bool error = false;
395 /* track range for overlap */
396 struct range_t *ranges = malloc(sizeof(struct range_t) * 2 * hdr->count);
397 int nr_ranges = 0;
354 for(unsigned i = 0; i < hdr->count; i++, sec++) 398 for(unsigned i = 0; i < hdr->count; i++, sec++)
355 { 399 {
356 cprintf(BLUE, "Section %d\n", i); 400 cprintf(BLUE, "Section %d\n", i);
357 cprintf(GREEN, " Code: "); 401 cprintf(GREEN, " Code: ");
358 cprintf(YELLOW, "0x%08x", sec->code_pa); 402 cprintf(YELLOW, "0x%08x", sec->code_va);
359 cprintf(RED, "-(txt)-"); 403 cprintf(RED, "-(txt)-");
360 cprintf(YELLOW, "0x%08x", sec->code_pa + sec->code_sz); 404 cprintf(YELLOW, "0x%08x", sec->code_va + sec->code_sz);
361 cprintf(BLUE, " |--> "); 405 cprintf(BLUE, " |--> ");
362 cprintf(YELLOW, "0x%08x", sec->code_va); 406 cprintf(YELLOW, "0x%08x", sec->code_pa);
363 cprintf(RED, "-(txt)-"); 407 cprintf(RED, "-(txt)-");
364 cprintf(YELLOW, "0x%08x\n", sec->code_va + sec->code_sz); 408 cprintf(YELLOW, "0x%08x\n", sec->code_pa + sec->code_sz);
409
410 /* add ranges */
411 ranges[nr_ranges].start = sec->code_va;
412 ranges[nr_ranges].size = sec->code_sz;
413 ranges[nr_ranges].section = i;
414 ranges[nr_ranges].type = RANGE_TXT;
415 ranges[nr_ranges + 1].start = sec->data_va;
416 ranges[nr_ranges + 1].size = sec->data_sz;
417 ranges[nr_ranges + 1].section = i;
418 ranges[nr_ranges + 1].type = RANGE_DAT;
419 nr_ranges += 2;
365 420
366 cprintf(GREEN, " Data: "); 421 cprintf(GREEN, " Data: ");
367 cprintf(YELLOW, "0x%08x", sec->data_pa); 422 cprintf(YELLOW, "0x%08x", sec->data_va);
368 cprintf(RED, "-(dat)-"); 423 cprintf(RED, "-(dat)-");
369 cprintf(YELLOW, "0x%08x", sec->data_pa + sec->data_sz); 424 cprintf(YELLOW, "0x%08x", sec->data_va + sec->data_sz);
370 cprintf(BLUE, " |--> "); 425 cprintf(BLUE, " |--> ");
371 cprintf(YELLOW, "0x%08x", sec->data_va); 426 cprintf(YELLOW, "0x%08x", sec->data_pa);
372 cprintf(RED, "-(dat)-"); 427 cprintf(RED, "-(dat)-");
373 cprintf(YELLOW, "0x%08x\n", sec->data_va + sec->data_sz); 428 cprintf(YELLOW, "0x%08x\n", sec->data_pa + sec->data_sz);
374 429
375 cprintf(GREEN, " Data: "); 430 cprintf(GREEN, " Data: ");
376 cprintf(RED, " "); 431 cprintf(RED, " ");
377 cprintf(BLUE, " |--> "); 432 cprintf(BLUE, " |--> ");
378 cprintf(YELLOW, "0x%08x", sec->bss_va); 433 cprintf(YELLOW, "0x%08x", sec->bss_pa);
379 cprintf(RED, "-(bss)-"); 434 cprintf(RED, "-(bss)-");
380 cprintf(YELLOW, "0x%08x\n", sec->bss_va + sec->bss_sz); 435 cprintf(YELLOW, "0x%08x\n", sec->bss_pa + sec->bss_sz);
436
437#define check_range_(start,sz) \
438 ((start) >= hdr_size && (start) + (sz) <= size)
439#define check_range(start,sz) \
440 ((start) >= hdr->addr && check_range_((start) - hdr->addr, sz))
441 /* check ranges */
442 if(sec->code_sz != 0 && !check_range(sec->code_va, sec->code_sz))
443 {
444 cprintf(GREY, "Invalid stage: out of bound code\n");
445 error = true;
446 break;
447 }
448 if(sec->data_sz != 0 && !check_range(sec->data_va, sec->data_sz))
449 {
450 cprintf(GREY, "Invalid stage: out of bound data\n");
451 error = true;
452 break;
453 }
454#undef check_range_
455#undef check_range
381 456
382#if 0 457 char buffer[32];
383 struct rknano_blob_t blob; 458 if(sec->code_sz != 0)
384 blob.offset = sec->code_pa - hdr->addr; 459 {
385 blob.size = sec->code_sz; 460 sprintf(buffer, ".text.%d", i);
386 save_blob(&blob, buf, size, "entry.", i, NO_ENC); 461 elf_add_load_section(&elf, sec->code_va, sec->code_sz,
387#else 462 buf + sec->code_va - hdr->addr, buffer);
388 struct elf_params_t elf; 463 }
389 elf_init(&elf); 464 if(sec->data_sz != 0)
390 elf_add_load_section(&elf, sec->code_va, sec->code_sz, buf + sec->code_pa - hdr->addr); 465 {
391 elf_add_load_section(&elf, sec->data_va, sec->data_sz, buf + sec->data_pa - hdr->addr); 466 sprintf(buffer, ".data.%d", i);
392 elf_add_fill_section(&elf, sec->bss_va, sec->bss_sz, 0); 467 elf_add_load_section(&elf, sec->data_va, sec->data_sz,
393 extract_elf_section(&elf, i); 468 buf + sec->data_va - hdr->addr, buffer);
394 elf_release(&elf); 469 }
395#endif 470 }
471 /* sort ranges and check overlap */
472 qsort(ranges, nr_ranges, sizeof(struct range_t), range_cmp);
473 for(int i = 1; i < nr_ranges; i++)
474 {
475 if(ranges[i - 1].start + ranges[i - 1].size > ranges[i].start)
476 {
477 error = true;
478 static const char *type[] = {"txt", "dat"};
479 cprintf(GREY, "Section overlap: section %d %s intersects section %d %s\n",
480 ranges[i - 1].section, type[ranges[i - 1].type], ranges[i].section,
481 type[ranges[i].type]);
482 break;
483 }
396 } 484 }
485 if(!error)
486 extract_elf_section(&elf);
487 /* FIXME for full information, we could add segments to the ELF file to
488 * keep the mapping, but it's unclear if that would do any good */
489 elf_release(&elf);
490 free(ranges);
397 491
398 return 0; 492 return 0;
399} 493}
400 494
495/**
496 * RKNano BOOT
497 * contains named bootloader stages
498 */
499
401#define MAGIC_BOOT "BOOT" 500#define MAGIC_BOOT "BOOT"
402#define MAGIC_BOOT_SIZE 4 501#define MAGIC_BOOT_SIZE 4
403 502
@@ -428,6 +527,8 @@ struct rknano_boot_header_t
428 uint32_t field_34; 527 uint32_t field_34;
429} __attribute__((packed)); 528} __attribute__((packed));
430 529
530#define BOOT_CHIP_RKNANO 0x30
531
431struct rknano_boot_entry_t 532struct rknano_boot_entry_t
432{ 533{
433 uint8_t entry_size; // unsure 534 uint8_t entry_size; // unsure
@@ -588,7 +689,7 @@ static int do_boot_image(uint8_t *buf, unsigned long size)
588 cprintf(RED, "OK\n"); 689 cprintf(RED, "OK\n");
589 else 690 else
590 cprintf(RED, "Mismatch\n"); 691 cprintf(RED, "Mismatch\n");
591 692
592#define print(str, name) cprintf(GREEN, " "str": ");cprintf(YELLOW, "%#x\n", (unsigned)hdr->name) 693#define print(str, name) cprintf(GREEN, " "str": ");cprintf(YELLOW, "%#x\n", (unsigned)hdr->name)
593#define print_arr(str, name, sz) \ 694#define print_arr(str, name, sz) \
594 cprintf(GREEN, " "str":");for(int i = 0; i < sz; i++)cprintf(YELLOW, " %#x", (unsigned)hdr->name[i]);printf("\n") 695 cprintf(GREEN, " "str":");for(int i = 0; i < sz; i++)cprintf(YELLOW, " %#x", (unsigned)hdr->name[i]);printf("\n")
@@ -602,8 +703,12 @@ static int do_boot_image(uint8_t *buf, unsigned long size)
602 hdr->hour, hdr->minute, hdr->second); 703 hdr->hour, hdr->minute, hdr->second);
603 704
604 cprintf(GREEN, " Chip: "); 705 cprintf(GREEN, " Chip: ");
605 cprintf(YELLOW, "%#x\n", hdr->chip); 706 cprintf(YELLOW, "%#x ", hdr->chip);
606 707 if(hdr->chip == BOOT_CHIP_RKNANO)
708 cprintf(RED, "(RKNANO)\n");
709 else
710 cprintf(RED, "(unknown)\n");
711
607 print_arr("field_2A", field_2B, 9); 712 print_arr("field_2A", field_2B, 9);
608 print("field_34", field_34); 713 print("field_34", field_34);
609 714
@@ -625,10 +730,15 @@ static int do_boot_image(uint8_t *buf, unsigned long size)
625 cprintf(RED, "OK\n"); 730 cprintf(RED, "OK\n");
626 else 731 else
627 cprintf(RED, "Mismatch\n"); 732 cprintf(RED, "Mismatch\n");
628 733
629 return 0; 734 return 0;
630} 735}
631 736
737/**
738 * RKFW
739 * contains bootloader and update
740 */
741
632typedef struct rknano_blob_t rkfw_blob_t; 742typedef struct rknano_blob_t rkfw_blob_t;
633 743
634#define MAGIC_RKFW "RKFW" 744#define MAGIC_RKFW "RKFW"
@@ -637,7 +747,7 @@ typedef struct rknano_blob_t rkfw_blob_t;
637struct rkfw_header_t 747struct rkfw_header_t
638{ 748{
639 char magic[MAGIC_RKFW_SIZE]; 749 char magic[MAGIC_RKFW_SIZE];
640 uint16_t hdr_size; // UNSURE 750 uint16_t hdr_size;
641 uint32_t version; 751 uint32_t version;
642 uint32_t code; 752 uint32_t code;
643 uint16_t year; 753 uint16_t year;
@@ -652,6 +762,8 @@ struct rkfw_header_t
652 uint8_t pad[61]; 762 uint8_t pad[61];
653} __attribute__((packed)); 763} __attribute__((packed));
654 764
765#define RKFW_CHIP_RKNANO 0x30
766
655static int do_rkfw_image(uint8_t *buf, unsigned long size) 767static int do_rkfw_image(uint8_t *buf, unsigned long size)
656{ 768{
657 if(size < sizeof(struct rkfw_header_t)) 769 if(size < sizeof(struct rkfw_header_t))
@@ -686,8 +798,12 @@ static int do_rkfw_image(uint8_t *buf, unsigned long size)
686 hdr->hour, hdr->minute, hdr->second); 798 hdr->hour, hdr->minute, hdr->second);
687 799
688 cprintf(GREEN, " Chip: "); 800 cprintf(GREEN, " Chip: ");
689 cprintf(YELLOW, "%#x\n", hdr->chip); 801 cprintf(YELLOW, "%#x ", hdr->chip);
690 802 if(hdr->chip == RKFW_CHIP_RKNANO)
803 cprintf(RED, "(RKNANO)\n");
804 else
805 cprintf(RED, "(unknown)\n");
806
691 cprintf(GREEN, " Loader: "); 807 cprintf(GREEN, " Loader: ");
692 print_blob_interval(&hdr->loader); 808 print_blob_interval(&hdr->loader);
693 cprintf(OFF, "\n"); 809 cprintf(OFF, "\n");
@@ -852,7 +968,7 @@ int main(int argc, char **argv)
852 perror("Cannot read file"); 968 perror("Cannot read file");
853 return 1; 969 return 1;
854 } 970 }
855 971
856 fclose(fin); 972 fclose(fin);
857 973
858 if(try_nanofw && !do_nanofw_image(buf, size)) 974 if(try_nanofw && !do_nanofw_image(buf, size))
@@ -873,4 +989,3 @@ int main(int argc, char **argv)
873 989
874 return 0; 990 return 0;
875} 991}
876