1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
|
/*
* VME Linux/m68k Loader
*
* (c) Copyright 1997 by Nick Holgate
*
* This file is subject to the terms and conditions of the GNU General Public
* License. See the file COPYING for more details.
*/
/*--------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <linux/linkage.h>
#include "bootinfo.h"
#include "loader.h"
#include "version.h"
#include "loaderlib.h"
#include "mvmebug.h"
/*--------------------------------------------------------------------------*/
extern const FILEMAP * find_file_map(const char *path);
extern char debug_mode;
/*--------------------------------------------------------------------------*/
static unsigned long current_file_position = 0;
static const FILEMAP *current_file_map = NULL;
static const FILEMAP *current_file_frag = NULL;
static unsigned long current_frag_position = 0;
static unsigned char disk_id = 0xff;
/*--------------------------------------------------------------------------*/
/* Board ID data structure
*
* Note, bytes 12 and 13 are board no in BCD (0162,0166,0167,0177,etc)
*/
typedef struct {
char bdid[4];
u_char rev, mth, day, yr;
u_short size, reserved;
u_short brdno;
char brdsuffix[2];
u_long options;
u_short clun, dlun, ctype, dnum;
u_long option2;
} t_bdid, *p_bdid;
/*--------------------------------------------------------------------------*/
static
u_long
get_brd_id
( void
)
{ static u_long brd_id = 0;
if (brd_id == 0)
{
p_bdid pkt;
pkt = (p_bdid) MVMEBug_brd_id();
if (*(ulong *)pkt->bdid != 0x42444944)
{
panic("Cannot identify board");
}
brd_id = (u_long) pkt->brdno;
}
return brd_id;
}
/*--------------------------------------------------------------------------*/
void
loader_init
( unsigned long arg
)
{
disk_id = (unsigned char) arg;
put_str("\nMotorola MVME Linux Loader V" VERNUMB "\n\n");
}
/*--------------------------------------------------------------------------*/
#define BCD2BIN(x) (((x)>>4)*10 + ((x)&0x0f))
unsigned long
get_time
( void
)
{ u_char buf[8];
MVMEBug_rtc_read(buf);
return (BCD2BIN(buf[4]) * 24 + BCD2BIN(buf[5])) * 60 + BCD2BIN(buf[6]);
}
/*--------------------------------------------------------------------------*/
void
mem_clear
( void *mem,
unsigned long count
)
{
u_long a = (u_long)mem;
while ((a & 3) && count)
{
*(char *)a = 0;
a++;
count--;
}
while (count > 3)
{
*(long *)a = 0;
a += 4;
count -= 4;
}
while (count)
{
*(char *)a = 0;
a++;
count--;
}
}
/*--------------------------------------------------------------------------*/
void
mem_move
( void *dest,
const void *srce,
unsigned long count
)
{
char *d = dest;
const char *s = srce;
if (d > s)
{
d += count;
s += count;
while (count--)
{
*--d = *--s;
}
}
else
{
while (count--)
{
*d++ = *s++;
}
}
}
/*--------------------------------------------------------------------------*/
int
mem_cmp
( void *mem1,
void *mem2,
unsigned long count
)
{
u_char c = 0;
u_char *p1 = (u_char *) mem1;
u_char *p2 = (u_char *) mem2;
while (c == 0 && count--)
c = *p1++ - *p2++;
return (int) c;
}
/*--------------------------------------------------------------------------*/
/* printf style output formatter
*/
int
vsoutput
( void (*output)(const int),
const char *fmt,
va_list va
)
{ int c;
int pos = 0;
#define BUFSZ 15
while ((c = *fmt++))
{
if (c != '%')
{
(*output)(c);
pos++;
}
else
{
enum {
FF_DEFAULT = 0,
FF_DUMP = 0x01, /* dump string to output */
FF_ALT = 0x02, /* #, alternate format */
FF_SHORT = 0x04, /* h, short arg */
FF_LONG = 0x08, /* l, long arg */
FF_ZERO = 0x10, /* 0, zero fill */
FF_LEFT = 0x20, /* -, left adjust */
FF_PREC = 0x40, /* .*, precision */
FF_NEG = 0x80 /* signed arg */
} flags = FF_DEFAULT;
long n;
unsigned long u;
char buf[BUFSZ + 1];
char *p = buf + BUFSZ;
int sign = '-';
int width = 0;
int prec = 0;
*p = '\0';
/* scan flags */
while (1)
{
switch (c = *fmt++)
{
case '0':
{
flags |= FF_ZERO;
break;
}
case '#': /* alternate format */
{
flags |= FF_ALT;
break;
}
case ' ': /* blank sign */
{
sign = ' ';
break;
}
case '+': /* +/- sign */
{
sign = '+';
break;
}
case '-': /* left justified */
{
flags |= FF_LEFT;
break;
}
default:
{
goto scan_width;
}
}
}
scan_width:
/* scan width */
if (c == '*')
{
/* width from arg list */
width = va_arg(va, int);
c = *fmt++;
}
else
{
while ('0' <= c && c <= '9')
{
width = width * 10 + (c - '0');
c = *fmt++;
}
}
if (c == '.')
{
/* scan precision */
flags |= FF_PREC;
c = *fmt++;
if (c == '*')
{
/* precision from arg list */
prec = va_arg(va, int);
c = *fmt++;
}
else
{
while ('0' <= c && c <= '9')
{
prec = prec * 10 + (c - '0');
c = *fmt++;
}
}
}
/* length modifiers */
if (c == 'h')
{
flags |= FF_SHORT;
c = *fmt++;
}
else if (c == 'l')
{
flags |= FF_LONG;
c = *fmt++;
}
/* do conversion */
switch (c)
{
case '%': /* %% -> % */
{
(*output)(c);
pos++;
break;
}
case 'n': /* save position */
{
*va_arg(va, int *) = pos;
break;
}
case 'c': /* character */
{
u = (flags & FF_SHORT) ? va_arg(va, unsigned short)
: (flags & FF_LONG ) ? va_arg(va, unsigned long )
: va_arg(va, unsigned int);
*--p = u;
flags |= FF_DUMP;
break;
}
case 's': /* string */
{
if ((p = va_arg(va, char *)) == NULL)
{
p = "";
}
if ((flags & FF_PREC) && str_len(p) > prec)
{
pos += prec;
while (--prec >= 0)
{
(*output)(*p++);
}
break;
}
flags |= FF_DUMP;
break;
}
case 'i': case 'd': case 'u': /* decimal */
{
if (c != 'u')
{
/* signed */
n = (flags & FF_SHORT) ? va_arg(va, short)
: (flags & FF_LONG ) ? va_arg(va, long )
: va_arg(va, int);
if (n < 0)
{
flags |= FF_NEG;
}
u = (n < 0) ? -n : n;
}
else
{
u = (flags & FF_SHORT) ? va_arg(va, unsigned short)
: (flags & FF_LONG ) ? va_arg(va, unsigned long )
: va_arg(va, unsigned int);
}
do {
*--p = '0' + u % 10;
u /= 10;
} while (u != 0);
prec -= buf + BUFSZ - p;
while (--prec >= 0)
{
*--p = '0';
}
if (flags & FF_NEG)
{
*--p = '-';
}
else
{
if (sign != '-')
{
*--p = (sign == '+') ? '+' : ' ';
}
}
flags |= FF_DUMP;
break;
}
case 'x': case 'X': /* hex, Hex */
{
u = (flags & FF_SHORT) ? va_arg(va, unsigned short)
: (flags & FF_LONG ) ? va_arg(va, unsigned long )
: va_arg(va, unsigned int);
do {
*--p = "0123456789ABCDEF"[u%16];
u /= 16;
} while (u);
prec -= buf + BUFSZ - p;
while (--prec >= 0)
{
*--p = '0';
}
if (flags & FF_ALT)
{
*--p = 'x';
*--p = '0';
}
flags |= FF_DUMP;
break;
}
case 'o': /* octal */
{
u = (flags & FF_SHORT) ? va_arg(va, unsigned short)
: (flags & FF_LONG ) ? va_arg(va, unsigned long )
: va_arg(va, unsigned int);
do {
*--p = '0' + u % 8;
u /= 8;
} while (u);
prec -= buf + BUFSZ - p;
while (--prec >= 0)
{
*--p = '0';
}
if ((flags & FF_ALT) && *p != '0')
{
*--p = '0';
}
flags |= FF_DUMP;
break;
}
default: /* error */
{
(*output)('%');
(*output)(c);
pos += 2;
break;
}
}
/* copy adjusted string "p" to output */
if (flags & FF_DUMP)
{
int len = str_len(p);
int pad = width - len;
if (!(flags & FF_LEFT))
{
int padder = (flags & FF_ZERO) ? '0' : ' ';
while (--pad >= 0)
{
(*output)(padder);
}
}
while (*p)
{
(*output)(*p++);
}
if (flags & FF_LEFT)
{
while (--pad >= 0)
{
(*output)(' ');
}
}
pos += (len < width) ? width : len;
}
}
}
return pos;
}
/*--------------------------------------------------------------------------*/
/* Print character.
*/
void
put_char
( const int c
)
{
if (c == '\n')
MVMEBug_putchar('\r');
MVMEBug_putchar(c);
}
/*--------------------------------------------------------------------------*/
/* Print formated string.
*/
void
Printf
( const char *fmt,
...
)
{ va_list args;
va_start(args, fmt);
vsoutput(put_char, fmt, args);
va_end(args);
}
/*--------------------------------------------------------------------------*/
/* Print unformated string.
*/
void
put_str
( const char *str
)
{
while (*str)
put_char(*str++);
}
/*--------------------------------------------------------------------------*/
/* Wait for and return character from keyboard.
*/
int
get_char
( unsigned long timeout /* maximum time to wait for character */
)
{
if (timeout)
{
/* get timeout second */
timeout += get_time();
while (MVMEBug_getchar_status() == 0)
{
/* check for timeout */
if (get_time() > timeout)
{
/* timeout */
break;
}
}
if (MVMEBug_getchar_status() == 0)
return -1;
}
return MVMEBug_getchar();
}
/*--------------------------------------------------------------------------*/
/* Report fatal error
*/
void
panic
( const char *fmt,
...
)
{ va_list args;
put_str("\nLILO Panic: ");
va_start(args, fmt);
vsoutput(put_char, fmt, args);
va_end(args);
put_char('\n');
while (1)
;
}
/*--------------------------------------------------------------------------*/
void
set_disk_id
( u_long dlun
)
{
disk_id = (unsigned char) dlun;
}
/*--------------------------------------------------------------------------*/
int
file_open
( const char *path
)
{
if ((current_file_map = find_file_map(path)) == NULL)
{
return -1;
}
current_file_position = 0;
current_frag_position = 0;
current_file_frag = MAP_FIRSTFRAG(current_file_map);
if (debug_mode)
{
const FILEMAP *p = current_file_frag;
u_long n = MAP_NUMFRAGS(current_file_map);
Printf("\nOPEN: file map at %08lx, %ld entries\n",
current_file_map, n);
while (n)
{
Printf(" Frag offset %08lx, length %08lx\n",
p->offset, p->length);
p++;
n--;
}
}
return 0;
}
/*--------------------------------------------------------------------------*/
long
file_tell
( void
)
{
return current_file_position;
}
/*--------------------------------------------------------------------------*/
int
file_seek
( int where,
int whence
)
{ int filesize;
int n;
/* if no open file */
if (current_file_map == NULL)
{
return -1;
}
/* get file size */
filesize = MAP_FILESIZE(current_file_map);
switch (whence)
{
case SEEK_SET: break;
case SEEK_CUR: where += current_file_position;
break;
case SEEK_END: where += filesize;
break;
default: return -1;
}
/* only allow seek within extents of file */
if (where < 0 || where > filesize)
{
return -1;
}
current_file_position = where;
current_file_frag = MAP_FIRSTFRAG(current_file_map);
current_frag_position = 0;
n = current_file_position;
while (n)
{
if (n <= current_file_frag->length)
{
current_frag_position = n;
n = 0;
}
else
{
n -= current_file_frag->length;
current_file_frag++;
}
}
if (current_frag_position == current_file_frag->length)
{
current_file_frag++;
current_frag_position = 0;
}
if (debug_mode)
{
Printf("SEEK: current_file_position %08lx\n",
current_file_position);
Printf(" frag->offset %08lx, frag->len %08lx\n",
current_file_frag->offset, current_file_frag->length);
Printf(" current_frag_position %08lx\n", current_frag_position);
}
return where;
}
/*--------------------------------------------------------------------------*/
int
file_read
( char *buf,
int count
)
{ int filesize;
int cnt = count;
DSKRD_CMND cdb;
if (debug_mode)
Printf("READ: buf=%08lx, len=%08lx\n", buf, cnt);
/* if no open file */
if (current_file_map == NULL)
{
return -1;
}
/* if illegal count */
if (cnt < 0)
{
return -1;
}
filesize = MAP_FILESIZE(current_file_map);
/* limit to amount of data left */
if (cnt > (filesize - current_file_position))
{
cnt = filesize - current_file_position;
}
while (cnt)
{
int l = cnt;
if (l > current_file_frag->length - current_frag_position)
l = current_file_frag->length - current_frag_position;
/* read l bytes */
cdb.clun = 0;
cdb.dlun = disk_id;
cdb.status = 0xdead;
cdb.address = (u_long)buf;
cdb.block = (current_file_frag->offset + current_frag_position) >> 8;
cdb.count = ((l + 511) >> 9) << 1;
cdb.flags = 0;
cdb.mod = 0;
if (debug_mode)
Printf("READ: blk=%08lx, cnt=%08lx\n", cdb.block, cdb.count);
/* Holes in the file result in fragments at disk offset 0;
* our malloc function will have zero-ed memory, so we
* can just skip over this read.
*/
if (cdb.block)
{
MVMEBug_disk_read(&cdb);
if (cdb.status)
{
u_long *p = (u_long *)&cdb;
Printf ("READ FAILED: status=%08lx\n", cdb.status);
if (debug_mode)
Printf(" %08ld %08ld %08ld %08ld\n",
*p, *(p+1), *(p+2), *(p+3));
return -1;
}
}
cnt -= l;
current_frag_position += l;
current_file_position += l;
buf += l;
if (current_frag_position == current_file_frag->length)
{
current_file_frag++;
current_frag_position = 0;
}
}
if (debug_mode)
{
Printf(" current_file_position %08lx\n",
current_file_position);
Printf(" frag->offset %08lx, frag->len %08lx\n",
current_file_frag->offset, current_file_frag->length);
Printf(" current_frag_position %08lx\n", current_frag_position);
}
return count;
}
/*--------------------------------------------------------------------------*/
void
file_close
( void
)
{
current_file_map = NULL;
}
/*--------------------------------------------------------------------------*/
unsigned long
get_compat_booti_version
( void
)
{
switch (get_brd_id())
{
case 0x0162:
case 0x0172: return COMPAT_MVME162_BOOTI_VERSION;
default : return COMPAT_MVME167_BOOTI_VERSION;
}
}
/*--------------------------------------------------------------------------*/
unsigned long
get_booti_version
( void
)
{
return MVME16x_BOOTI_VERSION;
}
/*--------------------------------------------------------------------------*/
unsigned long
get_compat_machtype
( void
)
{
switch (get_brd_id())
{
case 0x0162:
case 0x0172: return COMPAT_MACH_MVME162;
default : return COMPAT_MACH_MVME167;
}
}
/*--------------------------------------------------------------------------*/
unsigned long
get_machtype
( void
)
{
return MACH_MVME16x;
}
/*--------------------------------------------------------------------------*/
int
can_do_symbols
( void
)
{
return FALSE;
}
/*--------------------------------------------------------------------------*/
void
clear_symbols
( void
)
{
}
/*--------------------------------------------------------------------------*/
int
add_symbol
( char *data
)
{
return 0;
}
/*--------------------------------------------------------------------------*/
/*
* This assembler code is copied into local storage, and then executed.
* It copies the kernel and ramdisk images to their final resting places.
*
* It is called with:
*
* a0 = address of this code (very top of memory)
* a1 = kernel destination address (low memory 0x1000)
* a2 = kernel source address
* a3 = ramdisk destination address (just below this code)
* a4 = ramdisk source address
* d0 = kernel size + size of bootinfo data rounded up next multiple of 4
* d1 = ramdisk size rounded to next multiple of 1K
* d2 = non-zero if monitor should be called
*/
__asm(".text\n"
__ALIGN_STR "\n"
".globl " SYMBOL_NAME_STR(startcode_beg) ";\n"
".globl " SYMBOL_NAME_STR(startcode_end) ";\n"
SYMBOL_NAME_STR(startcode_beg) ":
| /* copy the ramdisk to the top of memory */
| /* (from back to front) */
addl %d1,%a4 | src = (u_long *) (rd_src + rd_size);
movel %a3,%a5
addl %d1,%a5 | dest = (u_long *) (rd_dest + rd_size);
bras 2f | do
1: movel -(%a4),-(%a5) | *--dest = *--src;
2: cmpl %a3,%a5 | while (dest > ramdisk_dest)
jgt 1b |
| /* copy kernel text and data, and bootinfo */
movel %a2,%a4 | limit = (u_long *) (kernel_src + kernel_size);
addl %d0,%a4
movel %a1,%a5 | dest = (u_long *) kernel_dest
bras 2f | do
1: movel (%a2)+,(%a5)+ | *dest++ = *src++;
2: cmpl %a4,%a2 | while (src < limit)
jlt 1b |
dc.w 0xf498 | cinva ic | invalidate instruction cache
tstl %d2 | call monitor?
jeq 1f | branch if not
trap #15
dc.w 0x0063
1: jmp (%a1) | start kernel or enter BVMBug
"
SYMBOL_NAME_STR(startcode_end) ":
");
/*--------------------------------------------------------------------------*/
void
print_model
( int cpu,
int hasfpu
)
{
Printf("MVME%x", get_brd_id());
}
/*-----------------------------< end of file >------------------------------*/
|