aboot.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * aboot.c
  3. *
  4. * This file is part of aboot, the SRM bootloader for Linux/Alpha
  5. * Copyright (C) 1996 Linus Torvalds, David Mosberger, and Michael Schwingen.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/version.h>
  23. #include <asm/console.h>
  24. #include "hwrpb.h"
  25. #include "system.h"
  26. #include <elf.h>
  27. #include <alloca.h>
  28. #include <errno.h>
  29. #include "aboot.h"
  30. #include "config.h"
  31. #include "cons.h"
  32. #include "setjmp.h"
  33. #include "utils.h"
  34. #include "string.h"
  35. struct bootfs * bfs = 0; /* filesystem to boot from */
  36. char * dest_addr = 0;
  37. jmp_buf jump_buffer;
  38. unsigned long start_addr = START_ADDR; /* default only */
  39. struct segment* chunks;
  40. int nchunks;
  41. char boot_file[256] = "";
  42. char initrd_file[256] = "";
  43. char kernel_args[256] = "";
  44. char * bss_start = 0;
  45. long bss_size = 0; /* size of bss */
  46. unsigned long initrd_start = 0;
  47. unsigned long initrd_size = 0;
  48. /* Offsets from start_addr (yes, they are negative, because start_addr
  49. is actually the load address plus the text offset) */
  50. #define PARAM_OFFSET -0x6000 /* load address + 0xa000 */
  51. #define STACK_OFFSET -0xe000 /* load address + 0x2000 */
  52. /* eventually we may have to deal with 44-bit physical addressing and
  53. also possibly larger pages */
  54. unsigned long page_offset = 0xfffffc0000000000;
  55. unsigned long page_shift = 13;
  56. static unsigned long entry_addr = START_ADDR;
  57. /*
  58. * The decompression code calls this function after decompressing the
  59. * first block of the object file. The first block must contain all
  60. * the relevant header information.
  61. */
  62. long
  63. first_block (const char *buf, long blocksize)
  64. {
  65. Elf64_Ehdr *elf;
  66. Elf64_Phdr *phdrs;
  67. int i, j;
  68. elf = (Elf64_Ehdr *) buf;
  69. if (elf->e_ident[0] != 0x7f
  70. || elf->e_ident[1] != 'E'
  71. || elf->e_ident[2] != 'L'
  72. || elf->e_ident[3] != 'F')
  73. {
  74. /* Fail silently, it might be a compressed file */
  75. return -1;
  76. }
  77. if (elf->e_ident[EI_CLASS] != ELFCLASS64
  78. || elf->e_ident[EI_DATA] != ELFDATA2LSB
  79. || elf->e_machine != EM_ALPHA)
  80. {
  81. printf("aboot: ELF executable not for this machine\n");
  82. return -1;
  83. }
  84. /* Looks like an ELF binary. */
  85. if (elf->e_type != ET_EXEC) {
  86. printf("aboot: not an executable ELF file\n");
  87. return -1;
  88. }
  89. if (elf->e_phoff + elf->e_phnum * sizeof(*phdrs) > (unsigned) blocksize)
  90. {
  91. printf("aboot: "
  92. "ELF program headers not in first block (%ld)\n",
  93. (long) elf->e_phoff);
  94. return -1;
  95. }
  96. phdrs = (struct elf_phdr *) (buf + elf->e_phoff);
  97. chunks = malloc(sizeof(struct segment) * elf->e_phnum);
  98. start_addr = phdrs[0].p_vaddr; /* assume they are sorted */
  99. entry_addr = elf->e_entry;
  100. #ifdef DEBUG
  101. printf("aboot: %d program headers, start address %#lx, entry %#lx\n",
  102. elf->e_phnum, start_addr, entry_addr);
  103. #endif
  104. for (i = j = 0; i < elf->e_phnum; ++i) {
  105. int status;
  106. if (phdrs[i].p_type != PT_LOAD)
  107. continue;
  108. chunks[j].addr = phdrs[i].p_vaddr;
  109. chunks[j].offset = phdrs[i].p_offset;
  110. chunks[j].size = phdrs[i].p_filesz;
  111. #ifdef DEBUG
  112. printf("aboot: PHDR %d vaddr %#lx offset %#lx size %#lx\n",
  113. i, chunks[j].addr, chunks[j].offset, chunks[j].size);
  114. #endif
  115. #ifndef TESTING
  116. status = check_memory(chunks[j].addr, chunks[j].size);
  117. if (status) {
  118. printf("aboot: Can't load kernel.\n"
  119. " Memory at %lx - %lx (PHDR %i) "
  120. "is %s\n",
  121. chunks[j].addr,
  122. chunks[j].addr + chunks[j].size - 1,
  123. i,
  124. (status == -ENOMEM) ?
  125. "Not Found" :
  126. "Busy (Reserved)");
  127. return -1;
  128. }
  129. #endif
  130. if (phdrs[i].p_memsz > phdrs[i].p_filesz) {
  131. if (bss_size > 0) {
  132. printf("aboot: Can't load kernel.\n"
  133. " Multiple BSS segments"
  134. " (PHDR %d)\n", i);
  135. return -1;
  136. }
  137. bss_start = (char *) (phdrs[i].p_vaddr +
  138. phdrs[i].p_filesz);
  139. bss_size = (phdrs[i].p_memsz - phdrs[i].p_filesz);
  140. }
  141. j++;
  142. }
  143. nchunks = j;
  144. #ifdef DEBUG
  145. printf("aboot: bss at 0x%p, size %#lx\n", bss_start, bss_size);
  146. #endif
  147. return 0;
  148. }
  149. static void
  150. get_boot_args(void)
  151. {
  152. long result;
  153. /* get boot command line: */
  154. #ifdef TESTING
  155. const char *e;
  156. if ((e = getenv("BOOTED_FILE"))) {
  157. strncpy(boot_file, e, sizeof(boot_file)-1);
  158. boot_file[sizeof(boot_file)-1] = 0;
  159. } else {
  160. strcpy(boot_file, "vmlinux.gz");
  161. }
  162. if ((e = getenv("BOOTED_OSFLAGS"))) {
  163. strncpy(kernel_args, e, sizeof(kernel_args)-1);
  164. kernel_args[sizeof(kernel_args)-1] = 0;
  165. } else {
  166. strcpy(kernel_args, "i");
  167. }
  168. #else
  169. result = cons_getenv(ENV_BOOTED_FILE, boot_file, sizeof(boot_file));
  170. if (result < 0) {
  171. printf("aboot: warning: can't get ENV_BOOTED_FILE "
  172. "(result=%lx)!\n", result);
  173. strcpy(boot_file, "vmlinux.gz");
  174. }
  175. result = cons_getenv(ENV_BOOTED_OSFLAGS,
  176. kernel_args, sizeof(kernel_args));
  177. if (result < 0) {
  178. printf("aboot: warning: can't get ENV_BOOTED_OSFLAGS "
  179. "(result=%lx)!\n", result);
  180. strcpy(kernel_args, "i");
  181. }
  182. #endif /* TESTING */
  183. }
  184. #ifdef TESTING
  185. long config_file_partition = 1;
  186. void halt()
  187. {
  188. exit(0);
  189. }
  190. void unzip_error(char *x)
  191. {
  192. printf("unzip: %s\n", x);
  193. }
  194. int main()
  195. {
  196. extern long load_kernel();
  197. long result;
  198. get_boot_args();
  199. result = load_kernel();
  200. if (result < 0) {
  201. printf("aboot: kernel load failed (%ld)\n", result);
  202. return 0;
  203. }
  204. printf("aboot: starting kernel %s with arguments %s\n",
  205. boot_file, kernel_args);
  206. return 0;
  207. }
  208. #else /* not TESTING */
  209. /*
  210. * Head transfers control to this function. Don't call it main() to avoid
  211. * gcc doing magic initialization things that we don't want.
  212. */
  213. void
  214. main_ (void)
  215. {
  216. extern long load_kernel (void);
  217. long i, result;
  218. cons_init();
  219. printf("aboot: Linux/Alpha SRM bootloader version "ABOOT_VERSION"\n");
  220. /* don't know how to deal with this yet */
  221. if (INIT_HWRPB->pagesize != 8192) {
  222. printf("aboot: expected 8kB pages, got %ldkB\n",
  223. INIT_HWRPB->pagesize >> 10);
  224. cons_close_console();
  225. return;
  226. }
  227. pal_init();
  228. get_boot_args();
  229. result = load_kernel();
  230. if (result < 0) {
  231. printf("aboot: kernel load failed (%ld)\n", result);
  232. cons_close_console();
  233. return;
  234. }
  235. printf("aboot: starting kernel %s with arguments %s\n",
  236. boot_file, kernel_args);
  237. strcpy((char*)start_addr + PARAM_OFFSET, kernel_args);
  238. *(unsigned long *)(start_addr + PARAM_OFFSET + 0x100)
  239. = initrd_start;
  240. *(unsigned long *)(start_addr + PARAM_OFFSET + 0x108)
  241. = initrd_size;
  242. cons_close_console();
  243. run_kernel(entry_addr, start_addr + STACK_OFFSET);
  244. cons_open_console();
  245. printf("aboot: kernel returned unexpectedly. Halting slowly...\n");
  246. for (i = 0 ; i < 0x100000000 ; i++)
  247. /* nothing */;
  248. cons_close_console();
  249. halt();
  250. }
  251. #endif /* TESTING */