bootloader.c 3.7 KB

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