bootloader.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * arch/ia64/hp/sim/boot/bootloader.c
  3. *
  4. * Loads an ELF kernel.
  5. *
  6. * Copyright (C) 1998-2003 Hewlett-Packard Co
  7. * David Mosberger-Tang <davidm@hpl.hp.com>
  8. * Stephane Eranian <eranian@hpl.hp.com>
  9. *
  10. * 01/07/99 S.Eranian modified to pass command line arguments to kernel
  11. */
  12. struct task_struct; /* forward declaration for elf.h */
  13. #include <linux/elf.h>
  14. #include <linux/init.h>
  15. #include <linux/kernel.h>
  16. #include <asm/elf.h>
  17. #include <asm/intrinsics.h>
  18. #include <asm/pal.h>
  19. #include <asm/pgtable.h>
  20. #include <asm/sal.h>
  21. #include "ssc.h"
  22. struct disk_req {
  23. unsigned long addr;
  24. unsigned len;
  25. };
  26. struct disk_stat {
  27. int fd;
  28. unsigned count;
  29. };
  30. extern void jmp_to_kernel (unsigned long bp, unsigned long e_entry);
  31. extern struct ia64_boot_param *sys_fw_init (const char *args, int arglen);
  32. extern void debug_break (void);
  33. static void
  34. cons_write (const char *buf)
  35. {
  36. unsigned long ch;
  37. while ((ch = *buf++) != '\0') {
  38. ssc(ch, 0, 0, 0, SSC_PUTCHAR);
  39. if (ch == '\n')
  40. ssc('\r', 0, 0, 0, SSC_PUTCHAR);
  41. }
  42. }
  43. #define MAX_ARGS 32
  44. void
  45. start_bootloader (void)
  46. {
  47. static char mem[4096];
  48. static char buffer[1024];
  49. unsigned long off;
  50. int fd, i;
  51. struct disk_req req;
  52. struct disk_stat stat;
  53. struct elfhdr *elf;
  54. struct elf_phdr *elf_phdr; /* program header */
  55. unsigned long e_entry, e_phoff, e_phnum;
  56. register struct ia64_boot_param *bp;
  57. char *kpath, *args;
  58. long arglen = 0;
  59. ssc(0, 0, 0, 0, SSC_CONSOLE_INIT);
  60. /*
  61. * S.Eranian: extract the commandline argument from the simulator
  62. *
  63. * The expected format is as follows:
  64. *
  65. * kernelname args...
  66. *
  67. * Both are optional but you can't have the second one without the first.
  68. */
  69. arglen = ssc((long) buffer, 0, 0, 0, SSC_GET_ARGS);
  70. kpath = "vmlinux";
  71. args = buffer;
  72. if (arglen > 0) {
  73. kpath = buffer;
  74. while (*args != ' ' && *args != '\0')
  75. ++args, --arglen;
  76. if (*args == ' ')
  77. *args++ = '\0', --arglen;
  78. }
  79. if (arglen <= 0) {
  80. args = "";
  81. arglen = 1;
  82. }
  83. fd = ssc((long) kpath, 1, 0, 0, SSC_OPEN);
  84. if (fd < 0) {
  85. cons_write(kpath);
  86. cons_write(": file not found, reboot now\n");
  87. for(;;);
  88. }
  89. stat.fd = fd;
  90. off = 0;
  91. req.len = sizeof(mem);
  92. req.addr = (long) mem;
  93. ssc(fd, 1, (long) &req, off, SSC_READ);
  94. ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
  95. elf = (struct elfhdr *) mem;
  96. if (elf->e_ident[0] == 0x7f && strncmp(elf->e_ident + 1, "ELF", 3) != 0) {
  97. cons_write("not an ELF file\n");
  98. return;
  99. }
  100. if (elf->e_type != ET_EXEC) {
  101. cons_write("not an ELF executable\n");
  102. return;
  103. }
  104. if (!elf_check_arch(elf)) {
  105. cons_write("kernel not for this processor\n");
  106. return;
  107. }
  108. e_entry = elf->e_entry;
  109. e_phnum = elf->e_phnum;
  110. e_phoff = elf->e_phoff;
  111. cons_write("loading ");
  112. cons_write(kpath);
  113. cons_write("...\n");
  114. for (i = 0; i < e_phnum; ++i) {
  115. req.len = sizeof(*elf_phdr);
  116. req.addr = (long) mem;
  117. ssc(fd, 1, (long) &req, e_phoff, SSC_READ);
  118. ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
  119. if (stat.count != sizeof(*elf_phdr)) {
  120. cons_write("failed to read phdr\n");
  121. return;
  122. }
  123. e_phoff += sizeof(*elf_phdr);
  124. elf_phdr = (struct elf_phdr *) mem;
  125. if (elf_phdr->p_type != PT_LOAD)
  126. continue;
  127. req.len = elf_phdr->p_filesz;
  128. req.addr = __pa(elf_phdr->p_paddr);
  129. ssc(fd, 1, (long) &req, elf_phdr->p_offset, SSC_READ);
  130. ssc((long) &stat, 0, 0, 0, SSC_WAIT_COMPLETION);
  131. memset((char *)__pa(elf_phdr->p_paddr) + elf_phdr->p_filesz, 0,
  132. elf_phdr->p_memsz - elf_phdr->p_filesz);
  133. }
  134. ssc(fd, 0, 0, 0, SSC_CLOSE);
  135. cons_write("starting kernel...\n");
  136. /* fake an I/O base address: */
  137. ia64_setreg(_IA64_REG_AR_KR0, 0xffffc000000UL);
  138. bp = sys_fw_init(args, arglen);
  139. ssc(0, (long) kpath, 0, 0, SSC_LOAD_SYMBOLS);
  140. debug_break();
  141. jmp_to_kernel((unsigned long) bp, e_entry);
  142. cons_write("kernel returned!\n");
  143. ssc(-1, 0, 0, 0, SSC_EXIT);
  144. }