bootloader.c 3.8 KB

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