bootloader.c 3.9 KB

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